fix git branch checkout
This commit is contained in:
parent
d2f4363b64
commit
dc99cd6ab2
@ -5,6 +5,9 @@
|
||||
|
||||
package ru.ulstu.extractor.controller;
|
||||
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@ -18,6 +21,7 @@ import ru.ulstu.extractor.model.mvc.RepoForm;
|
||||
import ru.ulstu.extractor.service.GitRepositoryService;
|
||||
import ru.ulstu.extractor.service.IndexService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static ru.ulstu.extractor.controller.Route.FILTER_COMMITS;
|
||||
@ -25,6 +29,7 @@ import static ru.ulstu.extractor.controller.Route.INDEXING_NEW_REPOSITORY;
|
||||
|
||||
@Controller
|
||||
public class GitIndexingController {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(GitIndexingController.class);
|
||||
private final GitRepositoryService gitRepositoryService;
|
||||
private final IndexService indexService;
|
||||
|
||||
@ -43,8 +48,7 @@ public class GitIndexingController {
|
||||
@RequestMapping(value = INDEXING_NEW_REPOSITORY, method = RequestMethod.POST, params = "send")
|
||||
public String getBranch(@ModelAttribute RepoForm repoForm, Model model) {
|
||||
try {
|
||||
gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo());
|
||||
List<Branch> branches = gitRepositoryService.getBranches(repoForm.getRepo());
|
||||
List<Branch> branches = gitRepositoryService.getRemoteBranches(repoForm.getRepo());
|
||||
model.addAttribute("branches", branches);
|
||||
} catch (Exception ex) {
|
||||
model.addAttribute("error", ex.getMessage());
|
||||
@ -53,14 +57,15 @@ public class GitIndexingController {
|
||||
}
|
||||
|
||||
@RequestMapping(value = INDEXING_NEW_REPOSITORY, method = RequestMethod.POST, params = "next")
|
||||
public String setBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) {
|
||||
public String indexBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) {
|
||||
model.addAttribute("filterForm", new FilterForm(repoForm.getRepo()));
|
||||
if (repoForm.getBranch() == null) {
|
||||
return INDEXING_NEW_REPOSITORY;
|
||||
} else {
|
||||
try {
|
||||
indexService.index(repoForm.getRepo(), repoForm.getBranch());
|
||||
} catch (Exception ex) {
|
||||
} catch (IOException | GitAPIException ex) {
|
||||
ex.printStackTrace();
|
||||
model.addAttribute("error", ex.getMessage());
|
||||
return INDEXING_NEW_REPOSITORY;
|
||||
}
|
||||
|
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* 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.controller;
|
||||
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
@ -7,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.ulstu.extractor.model.Commit;
|
||||
import ru.ulstu.extractor.service.GitRepositoryService;
|
||||
import ru.ulstu.extractor.service.IndexService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
@ -18,14 +24,12 @@ import static ru.ulstu.extractor.controller.RepoController.URL;
|
||||
public class RepoController {
|
||||
public static final String URL = "/";
|
||||
private final GitRepositoryService gitRepositoryService;
|
||||
private final IndexService indexService;
|
||||
|
||||
public RepoController(GitRepositoryService gitRepositoryService) {
|
||||
public RepoController(GitRepositoryService gitRepositoryService,
|
||||
IndexService indexService) {
|
||||
this.gitRepositoryService = gitRepositoryService;
|
||||
}
|
||||
|
||||
@GetMapping("clone")
|
||||
public void cloneRepository(@RequestParam("url") String url) throws GitAPIException, IOException {
|
||||
gitRepositoryService.cloneOrUpdateRepo(url);
|
||||
this.indexService = indexService;
|
||||
}
|
||||
|
||||
@GetMapping("commits")
|
||||
@ -34,4 +38,11 @@ public class RepoController {
|
||||
return gitRepositoryService.getCommits(repositoryUrl, branchName);
|
||||
}
|
||||
|
||||
@GetMapping("index")
|
||||
public Boolean indexRepository(@RequestParam("repositoryUrl") String repositoryUrl,
|
||||
@RequestParam("branchName") String branchName) throws GitAPIException, IOException {
|
||||
indexService.index(repositoryUrl, branchName);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* 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;
|
||||
@ -7,7 +12,6 @@ import org.eclipse.jgit.api.ListBranchCommand;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.diff.DiffFormatter;
|
||||
import org.eclipse.jgit.internal.storage.file.FileRepository;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@ -37,7 +41,80 @@ public class GitRepositoryService {
|
||||
@Value("${extractor.custom-projects-dir}")
|
||||
private String customProjectsDir;
|
||||
|
||||
public void cloneOrUpdateRepo(String url) throws GitAPIException, IOException {
|
||||
public List<Branch> getRemoteBranches(String url) throws GitAPIException, IOException {
|
||||
cloneOrUpdateRepo(url);
|
||||
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
||||
Git git = new Git(localRepo);
|
||||
List<Branch> branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE)
|
||||
.call()
|
||||
.stream()
|
||||
.map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, "")))
|
||||
.collect(Collectors.toList());
|
||||
git.close();
|
||||
localRepo.close();
|
||||
return branches;
|
||||
}
|
||||
|
||||
public List<Branch> getLocalBranches(String url) throws GitAPIException, IOException {
|
||||
cloneOrUpdateRepo(url);
|
||||
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
||||
Git git = new Git(localRepo);
|
||||
List<Branch> branches = git.branchList()
|
||||
.call()
|
||||
.stream()
|
||||
.map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, "")))
|
||||
.collect(Collectors.toList());
|
||||
git.close();
|
||||
localRepo.close();
|
||||
return branches;
|
||||
}
|
||||
|
||||
public List<Commit> getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException {
|
||||
cloneOrUpdateRepo(repositoryUrl);
|
||||
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);
|
||||
|
||||
List<Commit> list = new ArrayList<>();
|
||||
RevCommit prevCommit = null;
|
||||
for (RevCommit revCommit : commits) {
|
||||
Commit commit = new Commit(
|
||||
revCommit.getFullMessage(),
|
||||
new Author(revCommit.getAuthorIdent().getName()),
|
||||
Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())),
|
||||
revCommit.getName());
|
||||
if (prevCommit != null) {
|
||||
commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo));
|
||||
}
|
||||
list.add(commit);
|
||||
prevCommit = revCommit;
|
||||
}
|
||||
git.close();
|
||||
localRepo.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
public void remove(String repositoryUrl) throws IOException {
|
||||
FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl));
|
||||
}
|
||||
|
||||
private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException {
|
||||
Git git;
|
||||
if (projectDirExists(getProjectDirectoryFile(url))) {
|
||||
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
||||
@ -72,42 +149,7 @@ public class GitRepositoryService {
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
public List<Commit> getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException {
|
||||
cloneOrUpdateRepo(repositoryUrl);
|
||||
Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
|
||||
Git git = new Git(localRepo);
|
||||
git.pull().call();
|
||||
if (!localRepo.getBranch().equals(branchName)) {
|
||||
Ref ref = 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);
|
||||
|
||||
List<Commit> list = new ArrayList<>();
|
||||
RevCommit prevCommit = null;
|
||||
for (RevCommit revCommit : commits) {
|
||||
Commit commit = new Commit(
|
||||
revCommit.getFullMessage(),
|
||||
new Author(revCommit.getAuthorIdent().getName()),
|
||||
Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())),
|
||||
revCommit.getName());
|
||||
if (prevCommit != null) {
|
||||
commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo));
|
||||
}
|
||||
list.add(commit);
|
||||
prevCommit = revCommit;
|
||||
}
|
||||
git.close();
|
||||
localRepo.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
public List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) {
|
||||
private List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) {
|
||||
if (laterCommit == null || earlierCommit == null) {
|
||||
return null;
|
||||
}
|
||||
@ -205,22 +247,4 @@ public class GitRepositoryService {
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public List<Branch> getBranches(String url) throws GitAPIException, IOException {
|
||||
cloneOrUpdateRepo(url);
|
||||
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
|
||||
Git git = new Git(localRepo);
|
||||
List<Branch> branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE)
|
||||
.call()
|
||||
.stream()
|
||||
.map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, "")))
|
||||
.collect(Collectors.toList());
|
||||
git.close();
|
||||
localRepo.close();
|
||||
return branches;
|
||||
}
|
||||
|
||||
public void remove(String repositoryUrl) throws IOException {
|
||||
FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl));
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* 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 com.sun.istack.NotNull;
|
||||
@ -5,6 +10,7 @@ import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.ulstu.extractor.model.Author;
|
||||
import ru.ulstu.extractor.model.Branch;
|
||||
import ru.ulstu.extractor.model.Commit;
|
||||
@ -38,6 +44,7 @@ public class IndexService {
|
||||
this.authorRepository = authorRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException {
|
||||
Repository repository = repositoryRepository.findByUrl(repositoryUrl);
|
||||
if (repository == null) {
|
||||
@ -45,14 +52,13 @@ public class IndexService {
|
||||
}
|
||||
Branch branch = branchRepository.findByRepositoryAndName(repository, branchName);
|
||||
if (branch == null) {
|
||||
branchRepository.save(new Branch(repository, branchName));
|
||||
branch = branchRepository.save(new Branch(repository, branchName));
|
||||
}
|
||||
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName);
|
||||
commitRepository.deleteAll(branch.getCommits());
|
||||
branch.getCommits().clear();
|
||||
branch.setCommits(commits);
|
||||
branchRepository.save(branch);
|
||||
gitRepositoryService.remove(repositoryUrl);
|
||||
}
|
||||
|
||||
public List<Author> getRepositoryAuthors(@NotNull String repositoryUrl, @NotNull String branchName) {
|
||||
|
@ -1,5 +1,11 @@
|
||||
#
|
||||
# Copyright (C) 2021 Anton Romanov - All Rights Reserved
|
||||
# You may use, distribute and modify this code, please write to: romanov73@gmail.com.
|
||||
#
|
||||
|
||||
spring.main.banner-mode=off
|
||||
server.port=8080
|
||||
server.jetty.connection-idle-timeout=1000s
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.ru.ulstu=DEBUG
|
||||
extractor.custom-projects-dir=
|
||||
|
@ -48,6 +48,8 @@
|
||||
</div>
|
||||
<div class="container-fluid">
|
||||
<ul id="messages" class="feedback-panel">
|
||||
<div class="alert alert-danger" role="alert" th:if="${error}" th:text="${error}">
|
||||
</div>
|
||||
</ul>
|
||||
</div>
|
||||
<div layout:fragment="content">
|
||||
|
@ -9,7 +9,6 @@
|
||||
layout:decorate="~{default}">
|
||||
<div class="container" layout:fragment="content">
|
||||
<form action="#" th:action="${@route.INDEXING_NEW_REPOSITORY}" th:object="${repoForm}" method="post">
|
||||
<p style="color:red" th:text="${error}"></p>
|
||||
<button class="btn btn-outline-dark dropdown-toggle" type="button" data-toggle="collapse"
|
||||
data-target="#collapseOne" aria-expanded="false" aria-controls="collapseExample"
|
||||
th:if="${repoForm.repo != null}"
|
||||
|
Loading…
Reference in New Issue
Block a user