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 { @Value("${extractor.custom-projects-dir}") private String customProjectsDir; 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(); } }