|
|
@ -1,9 +1,4 @@
|
|
|
|
/*
|
|
|
|
package ru.ulstu.extractor.gitrepository;
|
|
|
|
* 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;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.eclipse.jgit.api.CreateBranchCommand;
|
|
|
|
import org.eclipse.jgit.api.CreateBranchCommand;
|
|
|
@ -15,12 +10,13 @@ import org.eclipse.jgit.internal.storage.file.FileRepository;
|
|
|
|
import org.eclipse.jgit.lib.ObjectId;
|
|
|
|
import org.eclipse.jgit.lib.ObjectId;
|
|
|
|
import org.eclipse.jgit.lib.ObjectLoader;
|
|
|
|
import org.eclipse.jgit.lib.ObjectLoader;
|
|
|
|
import org.eclipse.jgit.lib.ObjectReader;
|
|
|
|
import org.eclipse.jgit.lib.ObjectReader;
|
|
|
|
import org.eclipse.jgit.lib.Repository;
|
|
|
|
|
|
|
|
import org.eclipse.jgit.revwalk.RevCommit;
|
|
|
|
import org.eclipse.jgit.revwalk.RevCommit;
|
|
|
|
import org.eclipse.jgit.treewalk.TreeWalk;
|
|
|
|
import org.eclipse.jgit.treewalk.TreeWalk;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
|
|
|
|
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
|
|
|
|
import ru.ulstu.extractor.heuristic.model.EntityUnit;
|
|
|
|
import ru.ulstu.extractor.heuristic.model.EntityUnit;
|
|
|
@ -31,6 +27,7 @@ import ru.ulstu.extractor.model.Branch;
|
|
|
|
import ru.ulstu.extractor.model.Commit;
|
|
|
|
import ru.ulstu.extractor.model.Commit;
|
|
|
|
import ru.ulstu.extractor.model.FileChange;
|
|
|
|
import ru.ulstu.extractor.model.FileChange;
|
|
|
|
import ru.ulstu.extractor.model.LineChange;
|
|
|
|
import ru.ulstu.extractor.model.LineChange;
|
|
|
|
|
|
|
|
import ru.ulstu.extractor.model.Repository;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.File;
|
|
|
@ -61,18 +58,20 @@ public class GitRepositoryService {
|
|
|
|
private String customProjectsDir;
|
|
|
|
private String customProjectsDir;
|
|
|
|
private final ExecutorService executorService = Executors.newFixedThreadPool(4);
|
|
|
|
private final ExecutorService executorService = Executors.newFixedThreadPool(4);
|
|
|
|
private final ExecutorService executorServiceCommits = Executors.newFixedThreadPool(4);
|
|
|
|
private final ExecutorService executorServiceCommits = Executors.newFixedThreadPool(4);
|
|
|
|
|
|
|
|
|
|
|
|
private final StructuralUnitService structuralUnitService;
|
|
|
|
private final StructuralUnitService structuralUnitService;
|
|
|
|
|
|
|
|
private final GitRepositoryRepository gitRepositoryRepository;
|
|
|
|
|
|
|
|
|
|
|
|
public GitRepositoryService(StructuralUnitService structuralUnitService) {
|
|
|
|
public GitRepositoryService(StructuralUnitService structuralUnitService,
|
|
|
|
|
|
|
|
GitRepositoryRepository gitRepositoryRepository) {
|
|
|
|
this.structuralUnitService = structuralUnitService;
|
|
|
|
this.structuralUnitService = structuralUnitService;
|
|
|
|
|
|
|
|
this.gitRepositoryRepository = gitRepositoryRepository;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Branch> getRemoteBranches(String url) throws GitAPIException, IOException {
|
|
|
|
public List<Branch> getRemoteBranches(String url) throws GitAPIException, IOException {
|
|
|
|
LOG.debug("Get remote branches of {}. Clone", url);
|
|
|
|
LOG.debug("Get remote branches of {}. Clone", url);
|
|
|
|
cloneOrUpdateRepo(url);
|
|
|
|
cloneOrUpdateRepo(url);
|
|
|
|
LOG.debug("Get remote branches of {}. Get branches", url);
|
|
|
|
LOG.debug("Get remote branches of {}. Get branches", url);
|
|
|
|
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
|
|
|
org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
List<Branch> branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE)
|
|
|
|
List<Branch> branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE)
|
|
|
|
.call()
|
|
|
|
.call()
|
|
|
@ -88,7 +87,7 @@ public class GitRepositoryService {
|
|
|
|
LOG.debug("Get local branches of {}. Clone", url);
|
|
|
|
LOG.debug("Get local branches of {}. Clone", url);
|
|
|
|
cloneOrUpdateRepo(url);
|
|
|
|
cloneOrUpdateRepo(url);
|
|
|
|
LOG.debug("Get local branches of {}. Get branches", url);
|
|
|
|
LOG.debug("Get local branches of {}. Get branches", url);
|
|
|
|
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
|
|
|
org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
List<Branch> branches = git.branchList()
|
|
|
|
List<Branch> branches = git.branchList()
|
|
|
|
.call()
|
|
|
|
.call()
|
|
|
@ -105,7 +104,7 @@ public class GitRepositoryService {
|
|
|
|
if (needUpdate) {
|
|
|
|
if (needUpdate) {
|
|
|
|
cloneOrUpdateRepo(repositoryUrl, branchName);
|
|
|
|
cloneOrUpdateRepo(repositoryUrl, branchName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
|
|
|
|
org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
Git git = new Git(localRepo);
|
|
|
|
|
|
|
|
|
|
|
|
List<RevCommit> commits = new ArrayList<>();
|
|
|
|
List<RevCommit> commits = new ArrayList<>();
|
|
|
@ -144,7 +143,7 @@ public class GitRepositoryService {
|
|
|
|
return list;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void checkoutBranch(String repositoryUrl, Git git, Repository localRepo, String branchName) throws GitAPIException, IOException {
|
|
|
|
private void checkoutBranch(String repositoryUrl, Git git, org.eclipse.jgit.lib.Repository localRepo, String branchName) throws GitAPIException, IOException {
|
|
|
|
LOG.debug("Checkout branch {} {}", repositoryUrl, branchName);
|
|
|
|
LOG.debug("Checkout branch {} {}", repositoryUrl, branchName);
|
|
|
|
git.pull().call();
|
|
|
|
git.pull().call();
|
|
|
|
if (!localRepo.getBranch().equals(branchName)) {
|
|
|
|
if (!localRepo.getBranch().equals(branchName)) {
|
|
|
@ -192,7 +191,7 @@ public class GitRepositoryService {
|
|
|
|
|
|
|
|
|
|
|
|
private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException {
|
|
|
|
private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException {
|
|
|
|
Git git;
|
|
|
|
Git git;
|
|
|
|
Repository localRepo;
|
|
|
|
org.eclipse.jgit.lib.Repository localRepo;
|
|
|
|
if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) {
|
|
|
|
if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) {
|
|
|
|
localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
|
|
|
|
localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
|
|
|
|
git = new Git(localRepo);
|
|
|
|
git = new Git(localRepo);
|
|
|
@ -240,7 +239,7 @@ public class GitRepositoryService {
|
|
|
|
return file.exists();
|
|
|
|
return file.exists();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) {
|
|
|
|
private List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, org.eclipse.jgit.lib.Repository localRepo) {
|
|
|
|
if (laterCommit == null || earlierCommit == null) {
|
|
|
|
if (laterCommit == null || earlierCommit == null) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -256,7 +255,7 @@ public class GitRepositoryService {
|
|
|
|
return parseOutputDiff(output, localRepo, laterCommit);
|
|
|
|
return parseOutputDiff(output, localRepo, laterCommit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<FileChange> parseOutputDiff(String output, Repository repository, RevCommit commit) {
|
|
|
|
private List<FileChange> parseOutputDiff(String output, org.eclipse.jgit.lib.Repository repository, RevCommit commit) {
|
|
|
|
List<FileChange> changes = new ArrayList<>();
|
|
|
|
List<FileChange> changes = new ArrayList<>();
|
|
|
|
String[] strings = output.split("\n");
|
|
|
|
String[] strings = output.split("\n");
|
|
|
|
FileChange fileChange = new FileChange();
|
|
|
|
FileChange fileChange = new FileChange();
|
|
|
@ -322,7 +321,7 @@ public class GitRepositoryService {
|
|
|
|
return changes;
|
|
|
|
return changes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getContent(Repository repository, RevCommit commit, String path) {
|
|
|
|
private String getContent(org.eclipse.jgit.lib.Repository repository, RevCommit commit, String path) {
|
|
|
|
try (TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree())) {
|
|
|
|
try (TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree())) {
|
|
|
|
if (treeWalk != null) {
|
|
|
|
if (treeWalk != null) {
|
|
|
|
ObjectId blobId = treeWalk.getObjectId(0);
|
|
|
|
ObjectId blobId = treeWalk.getObjectId(0);
|
|
|
@ -346,4 +345,8 @@ public class GitRepositoryService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Optional.empty();
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Page<Repository> findAll(Pageable pageable) {
|
|
|
|
|
|
|
|
return gitRepositoryRepository.findAll(pageable);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|