From c684d53b157dd51fd59483c10b86cd20099b2dd0 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 22 Apr 2021 13:01:52 +0400 Subject: [PATCH] #25-Fixing bug --- .../controller/StructuralUnitController.java | 23 ++---- .../service/GitRepositoryService.java | 75 +++++++++++++------ 2 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java b/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java index ff584ae..639d420 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java @@ -5,46 +5,39 @@ package ru.ulstu.extractor.heuristic.controller; +import org.eclipse.jgit.api.errors.GitAPIException; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit; import ru.ulstu.extractor.heuristic.model.EntityUnit; import ru.ulstu.extractor.heuristic.model.ResourceUnit; -import ru.ulstu.extractor.heuristic.service.StructuralUnitService; import ru.ulstu.extractor.service.GitRepositoryService; -import java.io.File; import java.io.IOException; import java.util.List; @RestController @RequestMapping("StructuralUnitController") public class StructuralUnitController { - private final StructuralUnitService structuralUnitService; private final GitRepositoryService gitRepositoryService; - public StructuralUnitController(StructuralUnitService structuralUnitService, - GitRepositoryService gitRepositoryService) { - this.structuralUnitService = structuralUnitService; + public StructuralUnitController(GitRepositoryService gitRepositoryService) { this.gitRepositoryService = gitRepositoryService; } @GetMapping("get-entities") - public List getEntities(String repositoryUrl) throws IOException { - File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); - return structuralUnitService.getEntities(rootPath); + public List getEntities(String repositoryUrl, String branchName) throws IOException, GitAPIException { + return gitRepositoryService.getEntities(repositoryUrl, branchName); } @GetMapping("get-business-logic") - public List getBusinessLogic(String repositoryUrl) throws IOException { - File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); - return structuralUnitService.getBusinessLogic(rootPath); + public List getBusinessLogic(String repositoryUrl, String branchName) throws IOException, GitAPIException { + return gitRepositoryService.getBusinessLogic(repositoryUrl, branchName); } @GetMapping("get-resources") - public List getResource(String repositoryUrl) throws IOException { - File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); - return structuralUnitService.getResource(rootPath); + public List getResource(String repositoryUrl, String branchName) throws IOException, GitAPIException { + return gitRepositoryService.getResource(repositoryUrl, branchName); } } diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index de16237..cf543b6 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -20,6 +20,9 @@ import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.treewalk.TreeWalk; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit; +import ru.ulstu.extractor.heuristic.model.EntityUnit; +import ru.ulstu.extractor.heuristic.model.ResourceUnit; import ru.ulstu.extractor.heuristic.service.StructuralUnitService; import ru.ulstu.extractor.model.Author; import ru.ulstu.extractor.model.Branch; @@ -85,24 +88,10 @@ public class GitRepositoryService { } public List getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException { - cloneOrUpdateRepo(repositoryUrl); + cloneOrUpdateRepo(repositoryUrl, branchName); Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); Git git = new Git(localRepo); - git.pull().call(); - if (!localRepo.getBranch().equals(branchName)) { - 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 commits = new ArrayList<>(); git.log().call().forEach(commits::add); @@ -125,32 +114,76 @@ public class GitRepositoryService { return list; } + private void checkoutBranch(String repositoryUrl, Git git, Repository localRepo, String branchName) throws GitAPIException, IOException { + git.pull().call(); + if (!localRepo.getBranch().equals(branchName)) { + 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(); + } + } + git.pull().call(); + } + public File getProjectDirectoryFile(String url) { validateUrl(url); return Path.of(getProjectDirectory(url)) .toFile(); } + public List getEntities(String repositoryUrl, String branchName) throws IOException, GitAPIException { + cloneOrUpdateRepo(repositoryUrl, branchName); + return structuralUnitService.getEntities(getProjectDirectoryFile(repositoryUrl)); + } + + public List getBusinessLogic(String repositoryUrl, String branchName) throws IOException, GitAPIException { + cloneOrUpdateRepo(repositoryUrl, branchName); + return structuralUnitService.getBusinessLogic(getProjectDirectoryFile(repositoryUrl)); + } + + public List getResource(String repositoryUrl, String branchName) throws IOException, GitAPIException { + cloneOrUpdateRepo(repositoryUrl, branchName); + return structuralUnitService.getResource(getProjectDirectoryFile(repositoryUrl)); + } + + public void remove(String repositoryUrl) throws IOException { FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl)); } - private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException { + private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException { Git git; - if (projectDirExists(getProjectDirectoryFile(url))) { - Repository localRepo = new FileRepository(getProjectGitDirectory(url)); + Repository localRepo; + if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) { + localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); git = new Git(localRepo); git.pull().call(); localRepo.close(); } else { git = Git.cloneRepository() - .setURI(url) - .setDirectory(getProjectDirectoryFile(url)) + .setURI(repositoryUrl) + .setDirectory(getProjectDirectoryFile(repositoryUrl)) .call(); + localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); + } + if (branchName != null && !branchName.isEmpty()) { + checkoutBranch(repositoryUrl, git, localRepo, branchName); } git.close(); } + private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException { + cloneOrUpdateRepo(url, null); + } + private String getProjectDirectory(String url) { return (isBlank(customProjectsDir) ? System.getProperty("java.io.tmpdir")