fix git pull

This commit is contained in:
Anton Romanov 2021-02-18 08:16:15 +04:00
parent 685eb9584d
commit ac2e20da0e
3 changed files with 47 additions and 9 deletions

View File

@ -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<RevCommit> 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();
}
}

View File

@ -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);
}
}

View File

@ -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
logging.level.ru.ulstu=DEBUG
extractor.custom-projects-dir=