#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());
return "newRepo";
}
redirectAttributes.addAttribute("url", repoForm.getRepo());
redirectAttributes.addAttribute("branch", repoForm.getBranch());
redirectAttributes.addAttribute("repositoryUrl", repoForm.getRepo());
redirectAttributes.addAttribute("branchName", repoForm.getBranch());
return "redirect:/filtering";
}
}

View File

@ -18,7 +18,7 @@ public class Commit extends BaseEntity {
private String hash;
private Date date;
private String message;
@ManyToOne
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Author author;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "commit_id", unique = true)
@ -28,10 +28,11 @@ public class Commit extends BaseEntity {
public Commit() {
}
public Commit(String message, Author author, Date date) {
public Commit(String message, Author author, Date date, String hash) {
this.message = message;
this.author = author;
this.date = date;
this.hash = hash;
}
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.Repository;
import java.util.Optional;
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 ru.ulstu.extractor.model.Repository;
import java.util.Optional;
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));
Git git = new Git(localRepo);
git.pull().call();
Ref ref = git.checkout().
setCreateBranch(true).
setName(branchName).
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
setStartPoint("origin/" + branchName).
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);
@ -92,7 +93,8 @@ public class GitRepositoryService {
Commit commit = new Commit(
revCommit.getFullMessage(),
new Author(revCommit.getAuthorIdent().getName()),
Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())));
Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())),
revCommit.getName());
if (prevCommit != null) {
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 {
Repository repository = repositoryRepository.findByUrl(repositoryUrl)
.orElse(repositoryRepository.save(new Repository(repositoryUrl)));
Branch branch = branchRepository.findByRepositoryAndName(repository, branchName)
.orElse(branchRepository.save(new Branch(repository, branchName)));
Repository repository = repositoryRepository.findByUrl(repositoryUrl);
if (repository == null) {
repositoryRepository.save(new Repository(repositoryUrl));
}
Branch branch = branchRepository.findByRepositoryAndName(repository, branchName);
if (branch == null) {
branchRepository.save(new Branch(repository, branchName));
}
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName);
branch.setCommits(commits);
branchRepository.save(branch);
}
}