From ac2e20da0eb0fb4f8c1cccb55cfb7a41dde15b2b Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 18 Feb 2021 08:16:15 +0400 Subject: [PATCH] fix git pull --- .../ulstu/extractor/GitRepositoryService.java | 47 ++++++++++++++++--- .../ru/ulstu/extractor/RepoController.java | 6 ++- src/main/resources/application.properties | 3 +- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/GitRepositoryService.java index e6cf44e..0fffc3d 100644 --- a/src/main/java/ru/ulstu/extractor/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/GitRepositoryService.java @@ -2,21 +2,56 @@ package ru.ulstu.extractor; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.io.File; +import java.io.IOException; import java.nio.file.Path; +import static org.apache.logging.log4j.util.Strings.isBlank; + @Service public class GitRepositoryService { - private final static String LOCAL_PATH = "c:\\temp\\1"; + @Value("${extractor.custom-projects-dir}") + private String customProjectsDir; - public void clone(String url) throws GitAPIException { - Git git = Git.cloneRepository() - .setURI(url) - .setDirectory(Path.of(LOCAL_PATH).toFile()) - .call(); + + public void cloneOrUpdateRepo(String url) throws GitAPIException, IOException { + Git git; + if (projectDirExists(getProjectDirectoryFile(url))) { + Repository localRepo = new FileRepository(getProjectGitDirectory(url)); + git = new Git(localRepo); + git.pull().call(); + } else { + git = Git.cloneRepository() + .setURI(url) + .setDirectory(getProjectDirectoryFile(url)) + .call(); + } Iterable commits = git.log().call(); commits.forEach(c -> System.out.println(c.getFullMessage())); } + + private String getProjectDirectory(String url) { + return (isBlank(customProjectsDir) + ? System.getProperty("java.io.tmpdir") + : customProjectsDir) + url.substring(url.lastIndexOf('/')); + } + + private String getProjectGitDirectory(String url) { + return getProjectDirectory(url) + "/.git"; + } + + private File getProjectDirectoryFile(String url) { + return Path.of(getProjectDirectory(url)) + .toFile(); + } + + private boolean projectDirExists(File file) { + return file.exists(); + } } diff --git a/src/main/java/ru/ulstu/extractor/RepoController.java b/src/main/java/ru/ulstu/extractor/RepoController.java index 42c9600..3d3b2de 100644 --- a/src/main/java/ru/ulstu/extractor/RepoController.java +++ b/src/main/java/ru/ulstu/extractor/RepoController.java @@ -6,6 +6,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.io.IOException; + import static ru.ulstu.extractor.RepoController.URL; @RestController @@ -19,7 +21,7 @@ public class RepoController { } @GetMapping("clone") - public void cloneRepository(@RequestParam("url") String url) throws GitAPIException { - gitRepositoryService.clone(url); + public void cloneRepository(@RequestParam("url") String url) throws GitAPIException, IOException { + gitRepositoryService.cloneOrUpdateRepo(url); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c672a7e..c5d4c5d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,5 @@ spring.main.banner-mode=off server.port=8080 # Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF -logging.level.ru.ulstu=DEBUG \ No newline at end of file +logging.level.ru.ulstu=DEBUG +extractor.custom-projects-dir= \ No newline at end of file