2021-02-17 16:41:22 +04:00
|
|
|
package ru.ulstu.extractor;
|
|
|
|
|
|
|
|
import org.eclipse.jgit.api.Git;
|
|
|
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
2021-02-18 08:16:15 +04:00
|
|
|
import org.eclipse.jgit.internal.storage.file.FileRepository;
|
|
|
|
import org.eclipse.jgit.lib.Repository;
|
2021-02-17 16:41:22 +04:00
|
|
|
import org.eclipse.jgit.revwalk.RevCommit;
|
2021-02-18 08:16:15 +04:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2021-02-17 16:41:22 +04:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2021-02-18 08:16:15 +04:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2021-02-17 16:41:22 +04:00
|
|
|
import java.nio.file.Path;
|
|
|
|
|
2021-02-18 08:16:15 +04:00
|
|
|
import static org.apache.logging.log4j.util.Strings.isBlank;
|
|
|
|
|
2021-02-17 16:41:22 +04:00
|
|
|
@Service
|
|
|
|
public class GitRepositoryService {
|
2021-02-18 08:16:15 +04:00
|
|
|
@Value("${extractor.custom-projects-dir}")
|
|
|
|
private String customProjectsDir;
|
|
|
|
|
2021-02-17 16:41:22 +04:00
|
|
|
|
2021-02-18 08:16:15 +04:00
|
|
|
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();
|
|
|
|
}
|
2021-02-17 16:41:22 +04:00
|
|
|
Iterable<RevCommit> commits = git.log().call();
|
|
|
|
commits.forEach(c -> System.out.println(c.getFullMessage()));
|
|
|
|
}
|
2021-02-18 08:16:15 +04:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2021-02-17 16:41:22 +04:00
|
|
|
}
|