add threads

This commit is contained in:
Anton Romanov 2021-04-27 17:05:39 +04:00
parent 97cefbfbdf
commit 138960cfb7

View File

@ -18,6 +18,8 @@ import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository; 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.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit; import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
@ -43,12 +45,16 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.apache.logging.log4j.util.Strings.isBlank; import static org.apache.logging.log4j.util.Strings.isBlank;
@Service @Service
public class GitRepositoryService { public class GitRepositoryService {
private final static Logger LOG = LoggerFactory.getLogger(GitRepositoryService.class);
private static final String BRANCH_PREFIX = "refs/remotes/origin/"; private static final String BRANCH_PREFIX = "refs/remotes/origin/";
@Value("${extractor.custom-projects-dir}") @Value("${extractor.custom-projects-dir}")
private String customProjectsDir; private String customProjectsDir;
@ -60,7 +66,9 @@ public class GitRepositoryService {
} }
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);
cloneOrUpdateRepo(url); cloneOrUpdateRepo(url);
LOG.debug("Get remote branches of {}. Get branches", url);
Repository localRepo = new FileRepository(getProjectGitDirectory(url)); 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)
@ -74,7 +82,9 @@ public class GitRepositoryService {
} }
public List<Branch> getLocalBranches(String url) throws GitAPIException, IOException { public List<Branch> getLocalBranches(String url) throws GitAPIException, IOException {
LOG.debug("Get local branches of {}. Clone", url);
cloneOrUpdateRepo(url); cloneOrUpdateRepo(url);
LOG.debug("Get local branches of {}. Get branches", url);
Repository localRepo = new FileRepository(getProjectGitDirectory(url)); 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()
@ -88,6 +98,7 @@ public class GitRepositoryService {
} }
public List<Commit> getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException { public List<Commit> getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException {
LOG.debug("Get commits of {}. Branch {}", repositoryUrl, branchName);
cloneOrUpdateRepo(repositoryUrl, branchName); cloneOrUpdateRepo(repositoryUrl, branchName);
Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
Git git = new Git(localRepo); Git git = new Git(localRepo);
@ -97,7 +108,10 @@ public class GitRepositoryService {
List<Commit> list = new ArrayList<>(); List<Commit> list = new ArrayList<>();
RevCommit prevCommit = null; RevCommit prevCommit = null;
LOG.debug("Start analyse {} commits", commits.size());
int counter = commits.size();
for (RevCommit revCommit : commits) { for (RevCommit revCommit : commits) {
LOG.debug(" {} of {} commits", counter--, commits.size());
Commit commit = new Commit( Commit commit = new Commit(
revCommit.getFullMessage(), revCommit.getFullMessage(),
new Author(revCommit.getAuthorIdent().getName()), new Author(revCommit.getAuthorIdent().getName()),
@ -115,6 +129,7 @@ public class GitRepositoryService {
} }
private void checkoutBranch(String repositoryUrl, Git git, Repository localRepo, String branchName) throws GitAPIException, IOException { private void checkoutBranch(String repositoryUrl, Git git, Repository localRepo, String branchName) throws GitAPIException, IOException {
LOG.debug("Checkout branch {} {}", repositoryUrl, branchName);
git.pull().call(); git.pull().call();
if (!localRepo.getBranch().equals(branchName)) { if (!localRepo.getBranch().equals(branchName)) {
if (getLocalBranches(repositoryUrl).stream().anyMatch(localBranch -> localBranch.getName().contains(branchName))) { if (getLocalBranches(repositoryUrl).stream().anyMatch(localBranch -> localBranch.getName().contains(branchName))) {
@ -236,12 +251,20 @@ public class GitRepositoryService {
if (maybeFileName.isPresent()) { if (maybeFileName.isPresent()) {
fileChange = new FileChange(); fileChange = new FileChange();
fileChange.setFile(maybeFileName.get()); fileChange.setFile(maybeFileName.get());
fileChange.setContainsEntity( ExecutorService executorService = Executors.newFixedThreadPool(2);
structuralUnitService.containsEntity(getContent(repository, commit, maybeFileName.get())) Future<Boolean> futureEntity = executorService.submit(() -> {
); return structuralUnitService.containsEntity(getContent(repository, commit, maybeFileName.get()));
fileChange.setContainsBusinessLogic( });
structuralUnitService.containsBusinessLogic(getContent(repository, commit, maybeFileName.get()))
); Future<Boolean> futureBL = executorService.submit(() -> {
return structuralUnitService.containsBusinessLogic(getContent(repository, commit, maybeFileName.get()));
});
try {
fileChange.setContainsBusinessLogic(futureBL.get());
fileChange.setContainsEntity(futureEntity.get());
} catch (Exception ex) {
LOG.warn(ex.getMessage());
}
/// вытащить другие изменения из коммита /// вытащить другие изменения из коммита
changes.add(fileChange); changes.add(fileChange);
} }