Merge branch '25-fixing-bug' into 'master'

Resolve "Исправить ошибку"

Closes #25

See merge request romanov73/git-extractor!23
This commit is contained in:
Anton Romanov 2021-04-22 09:06:42 +00:00
commit 4629562e0f
2 changed files with 62 additions and 64 deletions

View File

@ -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<EntityUnit> getEntities(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
return structuralUnitService.getEntities(rootPath);
public List<EntityUnit> getEntities(String repositoryUrl, String branchName) throws IOException, GitAPIException {
return gitRepositoryService.getEntities(repositoryUrl, branchName);
}
@GetMapping("get-business-logic")
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
return structuralUnitService.getBusinessLogic(rootPath);
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl, String branchName) throws IOException, GitAPIException {
return gitRepositoryService.getBusinessLogic(repositoryUrl, branchName);
}
@GetMapping("get-resources")
public List<ResourceUnit> getResource(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
return structuralUnitService.getResource(rootPath);
public List<ResourceUnit> getResource(String repositoryUrl, String branchName) throws IOException, GitAPIException {
return gitRepositoryService.getResource(repositoryUrl, branchName);
}
}

View File

@ -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<Commit> 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<RevCommit> 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<EntityUnit> getEntities(String repositoryUrl, String branchName) throws IOException, GitAPIException {
cloneOrUpdateRepo(repositoryUrl, branchName);
return structuralUnitService.getEntities(getProjectDirectoryFile(repositoryUrl));
}
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl, String branchName) throws IOException, GitAPIException {
cloneOrUpdateRepo(repositoryUrl, branchName);
return structuralUnitService.getBusinessLogic(getProjectDirectoryFile(repositoryUrl));
}
public List<ResourceUnit> 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")
@ -268,32 +301,4 @@ public class GitRepositoryService {
}
return Optional.empty();
}
private Optional<String> getLineTo(String commitString) {
String startString = "+";
String startPlus = "+++";
if (commitString.startsWith(startString)) {
String lineTo = commitString.substring(commitString.indexOf(startString) + startString.length());
if (commitString.startsWith(startPlus)) {
return Optional.empty();
} else {
return Optional.of(lineTo);
}
}
return Optional.empty();
}
private Optional<String> getLineFrom(String commitString) {
String startString = "-";
String startMinus = "---";
if (commitString.startsWith(startString)) {
String lineFrom = commitString.substring(commitString.indexOf(startString) + startString.length());
if (commitString.startsWith(startMinus)) {
return Optional.empty();
} else {
return Optional.of(lineFrom);
}
}
return Optional.empty();
}
}