jgit: use RevWalk to count ancestors with same timestamp
diff --git a/plugins/org.hawk.jgit/src/org/hawk/git/JGitRepository.java b/plugins/org.hawk.jgit/src/org/hawk/git/JGitRepository.java
index 8ecd6bf..f683696 100644
--- a/plugins/org.hawk.jgit/src/org/hawk/git/JGitRepository.java
+++ b/plugins/org.hawk.jgit/src/org/hawk/git/JGitRepository.java
@@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
+import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
@@ -40,6 +41,7 @@
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
+import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.RevisionSyntaxException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
@@ -424,8 +426,7 @@
*/
private Collection<VcsCommitItem> diff(RevCommit current, RevCommit previous) throws IOException {
Collection<VcsCommitItem> result = new ArrayList<VcsCommitItem>();
- try (
- DiffFormatter diffFmt = new DiffFormatter(NullOutputStream.INSTANCE)) {
+ try (DiffFormatter diffFmt = new DiffFormatter(NullOutputStream.INSTANCE)) {
diffFmt.setRepository(repository);
VcsCommit commit = asVcsCommit(current);
for (DiffEntry diff : diffFmt.scan(
@@ -462,7 +463,7 @@
}
}
- private VcsCommit asVcsCommit(RevCommit current) {
+ private VcsCommit asVcsCommit(RevCommit current) throws MissingObjectException, IncorrectObjectTypeException, IOException {
final PersonIdent authorIdent = current.getAuthorIdent();
/*
@@ -476,16 +477,25 @@
* commit more than 1000 times in a second (which is normally very unlikely).
*/
Date commitDate = authorIdent.getWhen();
- int nCommitsSameTimestamp = 0;
if (current.getParentCount() > 0) {
- RevCommit parent = current.getParent(0);
- while (parent != null && parent.getAuthorIdent().getWhen().equals(commitDate)) {
- ++nCommitsSameTimestamp;
- parent = parent.getParentCount() > 0 ? parent.getParent(0) : null;
- }
- if (nCommitsSameTimestamp > 0) {
- Instant newInstant = commitDate.toInstant().plus(Duration.ofMillis(nCommitsSameTimestamp));
- commitDate = Date.from(newInstant);
+ try (RevWalk walk = new RevWalk(repository)) {
+ walk.markStart(walk.parseCommit(current.getId()));
+
+ int nCommitsSameTimestamp = 0;
+ final Iterator<RevCommit> itCommit = walk.iterator();
+ while (itCommit.hasNext()) {
+ RevCommit ancestor = itCommit.next();
+ if (ancestor.getAuthorIdent().getWhen().equals(commitDate)) {
+ ++nCommitsSameTimestamp;
+ } else {
+ break;
+ }
+ }
+
+ if (nCommitsSameTimestamp > 0) {
+ Instant newInstant = commitDate.toInstant().plus(Duration.ofMillis(nCommitsSameTimestamp));
+ commitDate = Date.from(newInstant);
+ }
}
}