#16 -- Save commits to DB

This commit is contained in:
Anton Romanov 2021-03-29 14:40:20 +04:00
parent 14a7983f49
commit cf9f4ea82b
6 changed files with 26 additions and 22 deletions

View File

@ -57,8 +57,8 @@ public class GitIndexingController {
model.addAttribute("error", ex.getMessage()); model.addAttribute("error", ex.getMessage());
return "newRepo"; return "newRepo";
} }
redirectAttributes.addAttribute("url", repoForm.getRepo()); redirectAttributes.addAttribute("repositoryUrl", repoForm.getRepo());
redirectAttributes.addAttribute("branch", repoForm.getBranch()); redirectAttributes.addAttribute("branchName", repoForm.getBranch());
return "redirect:/filtering"; return "redirect:/filtering";
} }
} }

View File

@ -18,7 +18,7 @@ public class Commit extends BaseEntity {
private String hash; private String hash;
private Date date; private Date date;
private String message; private String message;
@ManyToOne @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Author author; private Author author;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "commit_id", unique = true) @JoinColumn(name = "commit_id", unique = true)
@ -28,10 +28,11 @@ public class Commit extends BaseEntity {
public Commit() { public Commit() {
} }
public Commit(String message, Author author, Date date) { public Commit(String message, Author author, Date date, String hash) {
this.message = message; this.message = message;
this.author = author; this.author = author;
this.date = date; this.date = date;
this.hash = hash;
} }
public String getMessage() { public String getMessage() {

View File

@ -4,8 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Repository; import ru.ulstu.extractor.model.Repository;
import java.util.Optional;
public interface BranchRepository extends JpaRepository<Branch, Integer> { public interface BranchRepository extends JpaRepository<Branch, Integer> {
Optional<Branch> findByRepositoryAndName(Repository repository, String name); Branch findByRepositoryAndName(Repository repository, String name);
} }

View File

@ -3,8 +3,6 @@ package ru.ulstu.extractor.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.Repository; import ru.ulstu.extractor.model.Repository;
import java.util.Optional;
public interface RepositoryRepository extends JpaRepository<Repository, Integer> { public interface RepositoryRepository extends JpaRepository<Repository, Integer> {
Optional<Repository> findByUrl(String url); Repository findByUrl(String url);
} }

View File

@ -76,13 +76,14 @@ public class GitRepositoryService {
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(); git.pull().call();
Ref ref = git.checkout(). if (!localRepo.getBranch().equals(branchName)) {
setCreateBranch(true). Ref ref = git.checkout().
setName(branchName). setCreateBranch(true).
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). setName(branchName).
setStartPoint("origin/" + branchName). setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
call(); 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);
@ -92,7 +93,8 @@ public class GitRepositoryService {
Commit commit = new Commit( Commit commit = new Commit(
revCommit.getFullMessage(), revCommit.getFullMessage(),
new Author(revCommit.getAuthorIdent().getName()), new Author(revCommit.getAuthorIdent().getName()),
Date.from(Instant.ofEpochSecond(revCommit.getCommitTime()))); Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())),
revCommit.getName());
if (prevCommit != null) { if (prevCommit != null) {
commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo)); commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo));
} }

View File

@ -31,11 +31,16 @@ public class IndexService {
} }
public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException {
Repository repository = repositoryRepository.findByUrl(repositoryUrl) Repository repository = repositoryRepository.findByUrl(repositoryUrl);
.orElse(repositoryRepository.save(new Repository(repositoryUrl))); if (repository == null) {
Branch branch = branchRepository.findByRepositoryAndName(repository, branchName) repositoryRepository.save(new Repository(repositoryUrl));
.orElse(branchRepository.save(new Branch(repository, branchName))); }
Branch branch = branchRepository.findByRepositoryAndName(repository, branchName);
if (branch == null) {
branchRepository.save(new Branch(repository, branchName));
}
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName); List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName);
branch.setCommits(commits); branch.setCommits(commits);
branchRepository.save(branch);
} }
} }