Skip to content

Commit e33a0f8

Browse files
committed
fix: mapping commits to lowest possible semantic tag (refs #176)
1 parent c96f61f commit e33a0f8

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java

+24-15
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private List<RevCommit> getCommitList(
294294
final RevCommit to = toBoundary.getRevision();
295295
final LogCommand logCommand = this.git.log().addRange(from, to);
296296
if (pathFilters != null && !pathFilters.isEmpty()) {
297-
for (String pathFilter : pathFilters) {
297+
for (final String pathFilter : pathFilters) {
298298
logCommand.addPath(pathFilter);
299299
}
300300
}
@@ -490,8 +490,16 @@ private void pruneCommitsPerTag(final Map<String, Set<GitCommit>> commitsPerTag)
490490
}
491491

492492
private boolean isMappedToAnotherTag(
493-
final Map<String, String> tagPerCommitsHash, final String thisCommitHash) {
494-
return tagPerCommitsHash.containsKey(thisCommitHash);
493+
final Map<String, String> tagPerCommitsHash,
494+
final String thisCommitHash,
495+
final String thisTagName) {
496+
final String existingTagName = tagPerCommitsHash.get(thisCommitHash);
497+
if (existingTagName == null) {
498+
/** It was not mapped. */
499+
return false;
500+
}
501+
/** If mapped, map it to the lowest version. Where it was first released. */
502+
return isFirstTagSemanticallyHighest(thisTagName, existingTagName);
495503
}
496504

497505
private String noteThatTheCommitWasMapped(
@@ -547,7 +555,7 @@ private void populateCommitPerTag(
547555
final PriorityQueue<TraversalWork> moreWork)
548556
throws Exception {
549557
final String thisCommitHash = to.getName();
550-
if (this.isMappedToAnotherTag(tagPerCommitsHash, thisCommitHash)) {
558+
if (this.isMappedToAnotherTag(tagPerCommitsHash, thisCommitHash, currentTagName)) {
551559
return;
552560
}
553561
if (this.thisIsANewTag(tagPerCommitHash, thisCommitHash)) {
@@ -584,31 +592,32 @@ private boolean shouldPrioritizeNewWork(
584592
final TraversalWork existingWork, final TraversalWork newWork) {
585593
final String existingTagName = existingWork.getCurrentTagName();
586594
final String newTagName = newWork.getCurrentTagName();
587-
return shouldPrioritizeNewWork(existingTagName, newTagName);
595+
return isFirstTagSemanticallyHighest(existingTagName, newTagName);
588596
}
589597

590-
static boolean shouldPrioritizeNewWork(final String existingTagName, final String newTagName) {
591-
if (existingTagName == null && newTagName == null) {
598+
static boolean isFirstTagSemanticallyHighest(
599+
final String firstTagName, final String secondTagName) {
600+
if (firstTagName == null && secondTagName == null) {
592601
return false;
593602
}
594-
if (existingTagName == null && newTagName != null) {
603+
if (firstTagName == null && secondTagName != null) {
595604
return true;
596605
}
597-
if (newTagName == null) {
606+
if (secondTagName == null) {
598607
return false;
599608
}
600-
if (existingTagName.equals(newTagName)) {
609+
if (firstTagName.equals(secondTagName)) {
601610
return false;
602611
}
603-
final boolean newTagIsSemantic = SemanticVersioning.isSemantic(newTagName);
612+
final boolean newTagIsSemantic = SemanticVersioning.isSemantic(secondTagName);
604613
if (newTagIsSemantic) {
605-
final boolean existingTagNameIsSemantic = SemanticVersioning.isSemantic(existingTagName);
614+
final boolean existingTagNameIsSemantic = SemanticVersioning.isSemantic(firstTagName);
606615
if (!existingTagNameIsSemantic) {
607616
return true;
608617
}
609618
final SemanticVersion highest =
610-
SemanticVersioning.getHighestVersion(Arrays.asList(existingTagName, newTagName));
611-
if (highest.findTag().orElse("").equals(existingTagName)) {
619+
SemanticVersioning.getHighestVersion(Arrays.asList(firstTagName, secondTagName));
620+
if (highest.findTag().orElse("").equals(firstTagName)) {
612621
return true;
613622
}
614623
}
@@ -653,7 +662,7 @@ private GitCommit toGitCommit(final RevCommit revCommit) {
653662
merge);
654663
}
655664

656-
public void setPathFilters(List<String> pathFilters) {
665+
public void setPathFilters(final List<String> pathFilters) {
657666
this.pathFilters = pathFilters;
658667
}
659668

src/test/java/se/bjurr/gitchangelog/internal/git/GitRepoTest.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,19 @@ public void testThatRepoFilterIncreasesTheNumberOfCommitsUsingMultiplePathFilter
417417

418418
@Test
419419
public void priorityTags() throws Exception {
420-
assertThat(GitRepo.shouldPrioritizeNewWork(null, null)).isFalse();
420+
assertThat(GitRepo.isFirstTagSemanticallyHighest(null, null)).isFalse();
421421

422-
assertThat(GitRepo.shouldPrioritizeNewWork(null, "hello")).isTrue();
423-
assertThat(GitRepo.shouldPrioritizeNewWork("hello", null)).isFalse();
424-
assertThat(GitRepo.shouldPrioritizeNewWork("hello", "hello")).isFalse();
422+
assertThat(GitRepo.isFirstTagSemanticallyHighest(null, "hello")).isTrue();
423+
assertThat(GitRepo.isFirstTagSemanticallyHighest("hello", null)).isFalse();
424+
assertThat(GitRepo.isFirstTagSemanticallyHighest("hello", "hello")).isFalse();
425425

426-
assertThat(GitRepo.shouldPrioritizeNewWork("1.2.3", "hello")).isFalse();
427-
assertThat(GitRepo.shouldPrioritizeNewWork("hello", "1.2.3")).isTrue();
428-
assertThat(GitRepo.shouldPrioritizeNewWork("hello", "hello")).isFalse();
426+
assertThat(GitRepo.isFirstTagSemanticallyHighest("1.2.3", "hello")).isFalse();
427+
assertThat(GitRepo.isFirstTagSemanticallyHighest("hello", "1.2.3")).isTrue();
428+
assertThat(GitRepo.isFirstTagSemanticallyHighest("hello", "hello")).isFalse();
429429

430-
assertThat(GitRepo.shouldPrioritizeNewWork("1.2.3", "1.2.3")).isFalse();
431-
assertThat(GitRepo.shouldPrioritizeNewWork("1.2.3", "1.2.4")).isFalse();
432-
assertThat(GitRepo.shouldPrioritizeNewWork("1.2.4", "1.2.3")).isTrue();
430+
assertThat(GitRepo.isFirstTagSemanticallyHighest("1.2.3", "1.2.3")).isFalse();
431+
assertThat(GitRepo.isFirstTagSemanticallyHighest("1.2.3", "1.2.4")).isFalse();
432+
assertThat(GitRepo.isFirstTagSemanticallyHighest("1.2.4", "1.2.3")).isTrue();
433433
}
434434

435435
private GitRepo getGitRepo() throws Exception {

0 commit comments

Comments
 (0)