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

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

Closes #25

See merge request romanov73/git-extractor!23
merge-requests/24/merge
Anton Romanov 3 years ago
commit 4629562e0f

@ -5,46 +5,39 @@
package ru.ulstu.extractor.heuristic.controller; 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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
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;
import ru.ulstu.extractor.heuristic.model.ResourceUnit; import ru.ulstu.extractor.heuristic.model.ResourceUnit;
import ru.ulstu.extractor.heuristic.service.StructuralUnitService;
import ru.ulstu.extractor.service.GitRepositoryService; import ru.ulstu.extractor.service.GitRepositoryService;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("StructuralUnitController") @RequestMapping("StructuralUnitController")
public class StructuralUnitController { public class StructuralUnitController {
private final StructuralUnitService structuralUnitService;
private final GitRepositoryService gitRepositoryService; private final GitRepositoryService gitRepositoryService;
public StructuralUnitController(StructuralUnitService structuralUnitService, public StructuralUnitController(GitRepositoryService gitRepositoryService) {
GitRepositoryService gitRepositoryService) {
this.structuralUnitService = structuralUnitService;
this.gitRepositoryService = gitRepositoryService; this.gitRepositoryService = gitRepositoryService;
} }
@GetMapping("get-entities") @GetMapping("get-entities")
public List<EntityUnit> getEntities(String repositoryUrl) throws IOException { public List<EntityUnit> getEntities(String repositoryUrl, String branchName) throws IOException, GitAPIException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); return gitRepositoryService.getEntities(repositoryUrl, branchName);
return structuralUnitService.getEntities(rootPath);
} }
@GetMapping("get-business-logic") @GetMapping("get-business-logic")
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl) throws IOException { public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl, String branchName) throws IOException, GitAPIException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); return gitRepositoryService.getBusinessLogic(repositoryUrl, branchName);
return structuralUnitService.getBusinessLogic(rootPath);
} }
@GetMapping("get-resources") @GetMapping("get-resources")
public List<ResourceUnit> getResource(String repositoryUrl) throws IOException { public List<ResourceUnit> getResource(String repositoryUrl, String branchName) throws IOException, GitAPIException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); return gitRepositoryService.getResource(repositoryUrl, branchName);
return structuralUnitService.getResource(rootPath);
} }
} }

@ -20,6 +20,9 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
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.EntityUnit;
import ru.ulstu.extractor.heuristic.model.ResourceUnit;
import ru.ulstu.extractor.heuristic.service.StructuralUnitService; import ru.ulstu.extractor.heuristic.service.StructuralUnitService;
import ru.ulstu.extractor.model.Author; import ru.ulstu.extractor.model.Author;
import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Branch;
@ -85,24 +88,10 @@ 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 {
cloneOrUpdateRepo(repositoryUrl); 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);
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<>(); List<RevCommit> commits = new ArrayList<>();
git.log().call().forEach(commits::add); git.log().call().forEach(commits::add);
@ -125,32 +114,76 @@ public class GitRepositoryService {
return list; 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) { public File getProjectDirectoryFile(String url) {
validateUrl(url); validateUrl(url);
return Path.of(getProjectDirectory(url)) return Path.of(getProjectDirectory(url))
.toFile(); .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 { public void remove(String repositoryUrl) throws IOException {
FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl)); FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl));
} }
private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException { private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException {
Git git; Git git;
if (projectDirExists(getProjectDirectoryFile(url))) { Repository localRepo;
Repository localRepo = new FileRepository(getProjectGitDirectory(url)); if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) {
localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
git = new Git(localRepo); git = new Git(localRepo);
git.pull().call(); git.pull().call();
localRepo.close(); localRepo.close();
} else { } else {
git = Git.cloneRepository() git = Git.cloneRepository()
.setURI(url) .setURI(repositoryUrl)
.setDirectory(getProjectDirectoryFile(url)) .setDirectory(getProjectDirectoryFile(repositoryUrl))
.call(); .call();
localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
}
if (branchName != null && !branchName.isEmpty()) {
checkoutBranch(repositoryUrl, git, localRepo, branchName);
} }
git.close(); git.close();
} }
private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException {
cloneOrUpdateRepo(url, null);
}
private String getProjectDirectory(String url) { private String getProjectDirectory(String url) {
return (isBlank(customProjectsDir) return (isBlank(customProjectsDir)
? System.getProperty("java.io.tmpdir") ? System.getProperty("java.io.tmpdir")
@ -268,32 +301,4 @@ public class GitRepositoryService {
} }
return Optional.empty(); 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();
}
} }

Loading…
Cancel
Save