Skip to content

Commit

Permalink
fix: parsing newest commits first (refs #147)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Feb 16, 2023
1 parent 8722814 commit 5508682
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 92 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
implementation 'org.eclipse.jgit:org.eclipse.jgit:5.13.1.202206130422-r'
implementation 'org.gitlab:java-gitlab-api:4.1.1'

testImplementation 'org.slf4j:slf4j-simple:2.0.6'
testImplementation 'org.slf4j:slf4j-simple:1.7.30' // Same as JGit
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.assertj:assertj-core:3.23.1'
testImplementation 'com.approvaltests:approvaltests:18.5.0'
Expand Down
59 changes: 29 additions & 30 deletions src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
Expand Down Expand Up @@ -425,45 +426,46 @@ private boolean notFirstIncludedCommit(final ObjectId from, final ObjectId to) {
/** This can be done recursively but will result in {@link StackOverflowError} for large repos. */
private void populateComitPerTag(
final RevCommit from,
final ObjectId to,
final RevCommit to,
final Map<String, Ref> tagPerCommitHash,
final Map<String, String> tagPerCommitsHash,
final Map<String, Set<GitCommit>> commitsPerTag,
final Map<String, Date> datePerTag,
final String startingTagName)
throws Exception {
final Set<TraversalWork> moreWork =
this.populateCommitPerTag(
from,
to,
commitsPerTag,
tagPerCommitHash,
tagPerCommitsHash,
datePerTag,
startingTagName);
do {
final Set<TraversalWork> evenMoreWork = new TreeSet<>();
for (final TraversalWork tw : new ArrayList<>(moreWork)) {
moreWork.remove(tw);
final Set<TraversalWork> newWork =

final RevCommit thisCommit = this.revWalk.lookupCommit(to);
this.revWalk.parseHeaders(thisCommit);

final PriorityQueue<TraversalWork> moreWork =
new PriorityQueue<>(
this.populateCommitPerTag(
from,
tw.getTo(),
to,
commitsPerTag,
tagPerCommitHash,
tagPerCommitsHash,
datePerTag,
tw.getCurrentTagName());
evenMoreWork.addAll(newWork);
}
moreWork.addAll(evenMoreWork);
startingTagName));

while (!moreWork.isEmpty()) {
final TraversalWork next = moreWork.remove();
moreWork.addAll(
this.populateCommitPerTag(
from,
next.getTo(),
commitsPerTag,
tagPerCommitHash,
tagPerCommitsHash,
datePerTag,
next.getCurrentTagName()));
LOG.debug("Work left: " + moreWork.size());
} while (!moreWork.isEmpty());
}
}

private Set<TraversalWork> populateCommitPerTag(
final RevCommit from,
final ObjectId to,
final RevCommit to,
final Map<String, Set<GitCommit>> commitsPerTagName,
final Map<String, Ref> tagPerCommitHash,
final Map<String, String> tagPerCommitsHash,
Expand All @@ -474,21 +476,20 @@ private Set<TraversalWork> populateCommitPerTag(
if (this.isMappedToAnotherTag(tagPerCommitsHash, thisCommitHash)) {
return new TreeSet<>();
}
final RevCommit thisCommit = this.revWalk.lookupCommit(to);
this.revWalk.parseHeaders(thisCommit);
if (this.thisIsANewTag(tagPerCommitHash, thisCommitHash)) {
currentTagName = this.getTagName(tagPerCommitHash, thisCommitHash);
}
if (currentTagName != null) {
if (this.addCommitToCurrentTag(commitsPerTagName, currentTagName, thisCommit)) {
datePerTag.put(currentTagName, new Date(thisCommit.getCommitTime() * 1000L));
if (this.addCommitToCurrentTag(commitsPerTagName, currentTagName, to)) {
datePerTag.put(currentTagName, new Date(to.getCommitTime() * 1000L));
}
this.noteThatTheCommitWasMapped(tagPerCommitsHash, currentTagName, thisCommitHash);
}
if (this.notFirstIncludedCommit(from, to)) {
final Set<TraversalWork> work = new TreeSet<>();
for (final RevCommit parent : thisCommit.getParents()) {
for (final RevCommit parent : to.getParents()) {
if (this.shouldInclude(parent)) {
this.revWalk.parseHeaders(parent);
work.add(new TraversalWork(parent, currentTagName));
}
}
Expand Down Expand Up @@ -541,9 +542,7 @@ private GitCommit toGitCommit(final RevCommit revCommit) {
merge);
}

/**
* @param pathFilter use when filtering commits
*/
/** @param pathFilter use when filtering commits */
public void setTreeFilter(final String pathFilter) {
this.pathFilter = pathFilter == null ? "" : pathFilter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public int compareTo(final TraversalWork o) {
}

int compareTo(final int selfCommitTime, final int otherCommitTime) {
return Integer.valueOf(selfCommitTime) //
.compareTo(otherCommitTime);
return Integer.valueOf(otherCommitTime) //
.compareTo(selfCommitTime);
}

@Override
Expand Down

This file was deleted.

0 comments on commit 5508682

Please # to comment.