|
|
|
@ -1,3 +1,8 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
|
|
|
|
|
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package ru.ulstu.extractor.service;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
@ -7,7 +12,6 @@ import org.eclipse.jgit.api.ListBranchCommand;
|
|
|
|
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
|
|
|
|
import org.eclipse.jgit.diff.DiffFormatter;
|
|
|
|
|
import org.eclipse.jgit.internal.storage.file.FileRepository;
|
|
|
|
|
import org.eclipse.jgit.lib.Ref;
|
|
|
|
|
import org.eclipse.jgit.lib.Repository;
|
|
|
|
|
import org.eclipse.jgit.revwalk.RevCommit;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
@ -37,39 +41,32 @@ 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))) {
|
|
|
|
|
public List<Branch> getRemoteBranches(String url) throws GitAPIException, IOException {
|
|
|
|
|
cloneOrUpdateRepo(url);
|
|
|
|
|
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
|
|
|
|
git = new Git(localRepo);
|
|
|
|
|
git.pull().call();
|
|
|
|
|
localRepo.close();
|
|
|
|
|
} else {
|
|
|
|
|
git = Git.cloneRepository()
|
|
|
|
|
.setURI(url)
|
|
|
|
|
.setDirectory(getProjectDirectoryFile(url))
|
|
|
|
|
.call();
|
|
|
|
|
}
|
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
|
List<Branch> branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE)
|
|
|
|
|
.call()
|
|
|
|
|
.stream()
|
|
|
|
|
.map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, "")))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
git.close();
|
|
|
|
|
localRepo.close();
|
|
|
|
|
return branches;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
public List<Branch> getLocalBranches(String url) throws GitAPIException, IOException {
|
|
|
|
|
cloneOrUpdateRepo(url);
|
|
|
|
|
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
|
List<Branch> branches = git.branchList()
|
|
|
|
|
.call()
|
|
|
|
|
.stream()
|
|
|
|
|
.map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, "")))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
git.close();
|
|
|
|
|
localRepo.close();
|
|
|
|
|
return branches;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Commit> getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException {
|
|
|
|
@ -78,12 +75,18 @@ public class GitRepositoryService {
|
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
|
git.pull().call();
|
|
|
|
|
if (!localRepo.getBranch().equals(branchName)) {
|
|
|
|
|
Ref ref = git.checkout().
|
|
|
|
|
setCreateBranch(true).
|
|
|
|
|
setName(branchName).
|
|
|
|
|
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
|
|
|
|
|
setStartPoint("origin/" + branchName).
|
|
|
|
|
call();
|
|
|
|
|
if (getLocalBranches(repositoryUrl).stream().anyMatch(localBranch -> localBranch.getName().contains(branchName))) {
|
|
|
|
|
git.checkout()
|
|
|
|
|
.setName(branchName)
|
|
|
|
|
.call();
|
|
|
|
|
} else {
|
|
|
|
|
git.checkout()
|
|
|
|
|
.setCreateBranch(true)
|
|
|
|
|
.setName(branchName)
|
|
|
|
|
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
|
|
|
|
|
.setStartPoint("origin/" + branchName)
|
|
|
|
|
.call();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<RevCommit> commits = new ArrayList<>();
|
|
|
|
|
git.log().call().forEach(commits::add);
|
|
|
|
@ -107,7 +110,46 @@ public class GitRepositoryService {
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) {
|
|
|
|
|
public void remove(String repositoryUrl) throws IOException {
|
|
|
|
|
FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private 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();
|
|
|
|
|
localRepo.close();
|
|
|
|
|
} else {
|
|
|
|
|
git = Git.cloneRepository()
|
|
|
|
|
.setURI(url)
|
|
|
|
|
.setDirectory(getProjectDirectoryFile(url))
|
|
|
|
|
.call();
|
|
|
|
|
}
|
|
|
|
|
git.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) {
|
|
|
|
|
if (laterCommit == null || earlierCommit == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -205,22 +247,4 @@ public class GitRepositoryService {
|
|
|
|
|
}
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Branch> getBranches(String url) throws GitAPIException, IOException {
|
|
|
|
|
cloneOrUpdateRepo(url);
|
|
|
|
|
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
|
List<Branch> branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE)
|
|
|
|
|
.call()
|
|
|
|
|
.stream()
|
|
|
|
|
.map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, "")))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
git.close();
|
|
|
|
|
localRepo.close();
|
|
|
|
|
return branches;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void remove(String repositoryUrl) throws IOException {
|
|
|
|
|
FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|