Skip to content

Commit 4c1cfe5

Browse files
Marc ROZANCmrozanc
authored andcommitted
Implement distanceOrZero to have 0 instead of depth when tag is not found
1 parent 699d147 commit 4c1cfe5

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

src/main/java/me/qoomon/gitversioning/commons/GitDescription.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ public class GitDescription {
44
private final String commit;
55
private final String tag;
66
private final int distance;
7+
private final boolean foundTag;
78

8-
public GitDescription(String commit, String tag, int distance) {
9+
public GitDescription(String commit, String tag, int distance, boolean foundTag) {
910
this.commit = commit;
1011
this.tag = tag;
1112
this.distance = distance;
13+
this.foundTag = foundTag;
1214
}
1315

1416
public String getCommit() {
@@ -23,6 +25,10 @@ public int getDistance() {
2325
return distance;
2426
}
2527

28+
public int getDistanceOrZero() {
29+
return foundTag ? distance : 0;
30+
}
31+
2632
@Override
2733
public String toString() {
2834
return tag + "-" + distance + "-g" + commit.substring(0,7);

src/main/java/me/qoomon/gitversioning/commons/GitUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static List<String> tagsPointAt(ObjectId revObjectId, Repository reposito
5353
public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern, Repository repository, boolean firstParent) throws IOException {
5454
Repository commonRepository = worktreesFix_getCommonRepository(repository);
5555
if (revObjectId == null) {
56-
return new GitDescription(NO_COMMIT, "root", 0);
56+
return new GitDescription(NO_COMMIT, "root", 0, false);
5757
}
5858

5959
Map<ObjectId, List<String>> objectIdListMap = reverseTagRefMap(repository);
@@ -72,7 +72,7 @@ public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern,
7272
.findFirst();
7373

7474
if (matchingTag.isPresent()) {
75-
return new GitDescription(revObjectId.getName(), matchingTag.get(), depth);
75+
return new GitDescription(revObjectId.getName(), matchingTag.get(), depth, true);
7676
}
7777
depth++;
7878
}
@@ -81,7 +81,7 @@ public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern,
8181
throw new IllegalStateException("couldn't find matching tag in shallow git repository");
8282
}
8383

84-
return new GitDescription(revObjectId.getName(), "root", depth);
84+
return new GitDescription(revObjectId.getName(), "root", depth, false);
8585
}
8686
}
8787

src/main/java/me/qoomon/maven/gitversioning/GitVersioningModelProcessor.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -966,14 +966,9 @@ private Map<String, Supplier<String>> generateGlobalFormatPlaceholderMap(GitSitu
966966
placeholderMap.put("describe.tag.version.label.next", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label").get(), 1)));
967967

968968
final Lazy<Integer> descriptionDistance = Lazy.by(() -> description.get().getDistance());
969-
placeholderMap.put("describe.distance", Lazy.by(() -> String.valueOf(descriptionDistance.get())));
970-
placeholderMap.put("describe.distance.snapshot", Lazy.by(() -> (descriptionDistance.get() == 0 ? "" : "-SNAPSHOT")));
971-
972-
placeholderMap.put("describe.tag.version.patch.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch").get(), descriptionDistance.get())));
973-
placeholderMap.put("describe.tag.version.patch.next.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch.next").get(), descriptionDistance.get())));
974-
975-
placeholderMap.put("describe.tag.version.label.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label").get(), descriptionDistance.get())));
976-
placeholderMap.put("describe.tag.version.label.next.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label.next").get(), descriptionDistance.get())));
969+
setDescribeDistancePlaceholders(placeholderMap, descriptionDistance, "distance");
970+
final Lazy<Integer> descriptionDistanceOrZero = Lazy.by(() -> description.get().getDistanceOrZero());
971+
setDescribeDistancePlaceholders(placeholderMap, descriptionDistanceOrZero, "distanceOrZero");
977972

978973
// describe tag pattern groups
979974
final Lazy<Map<String, String>> describeTagPatternValues = Lazy.by(
@@ -1002,6 +997,17 @@ private Map<String, Supplier<String>> generateGlobalFormatPlaceholderMap(GitSitu
1002997
return placeholderMap;
1003998
}
1004999

1000+
private static void setDescribeDistancePlaceholders(Map<String, Supplier<String>> placeholderMap, Lazy<Integer> descriptionDistance, String distanceName) {
1001+
placeholderMap.put("describe." + distanceName, Lazy.by(() -> String.valueOf(descriptionDistance.get())));
1002+
placeholderMap.put("describe." + distanceName + ".snapshot", Lazy.by(() -> (descriptionDistance.get() == 0 ? "" : "-SNAPSHOT")));
1003+
1004+
placeholderMap.put("describe.tag.version.patch.plus.describe." + distanceName, Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch").get(), descriptionDistance.get())));
1005+
placeholderMap.put("describe.tag.version.patch.next.plus.describe." + distanceName, Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch.next").get(), descriptionDistance.get())));
1006+
1007+
placeholderMap.put("describe.tag.version.label.plus.describe." + distanceName, Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label").get(), descriptionDistance.get())));
1008+
placeholderMap.put("describe.tag.version.label.next.plus.describe." + distanceName, Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label.next").get(), descriptionDistance.get())));
1009+
}
1010+
10051011
private Matcher matchVersion(String input) {
10061012
Matcher matcher = VERSION_PATTERN.matcher(input);
10071013
//noinspection ResultOfMethodCallIgnored

src/test/java/me/qoomon/gitversioning/commons/GitUtilTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.qoomon.gitversioning.commons;
22

33

4+
import org.assertj.core.api.SoftAssertions;
45
import org.eclipse.jgit.api.Git;
56
import org.eclipse.jgit.api.Status;
67
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -16,6 +17,7 @@
1617
import java.util.regex.Pattern;
1718

1819
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
1921
import static org.eclipse.jgit.lib.Constants.HEAD;
2022
import static org.eclipse.jgit.lib.Constants.MASTER;
2123

@@ -192,9 +194,24 @@ void describe() throws Exception {
192194
// then
193195
assertThat(description).satisfies(it -> {
194196
assertThat(it.getCommit()).isEqualTo(givenCommit.getName());
195-
assertThat(it.getDistance()).isEqualTo(0);
197+
assertThat(it.getDistance()).isZero();
198+
assertThat(it.getDistanceOrZero()).isZero();
196199
assertThat(it.getTag()).isEqualTo(givenTagName);
197200
});
198201
}
199202

203+
@Test
204+
void distanceOrZeroIsZeroWhenNoTagMatches() throws Exception {
205+
// given
206+
Git git = Git.init().setInitialBranch(MASTER).setDirectory(tempDir.toFile()).call();
207+
208+
final var softly = new SoftAssertions();
209+
for (int i = 0; i < 3; ++i) {
210+
GitDescription description = GitUtil.describe(head(git), Pattern.compile("v.+"), git.getRepository(), true);
211+
softly.assertThat(description.getDistanceOrZero()).isZero();
212+
softly.assertThat(description.getDistance()).isEqualTo(i);
213+
git.commit().setMessage("commit " + (i + 1)).setAllowEmpty(true).call();
214+
}
215+
softly.assertAll();
216+
}
200217
}

0 commit comments

Comments
 (0)