From 4c1cfe52f2f8a4dc0b0cea915a5cb47e4c1f8666 Mon Sep 17 00:00:00 2001 From: Marc ROZANC Date: Sat, 21 Sep 2024 19:26:36 +0200 Subject: [PATCH 1/8] Implement distanceOrZero to have 0 instead of depth when tag is not found --- .../gitversioning/commons/GitDescription.java | 8 ++++++- .../qoomon/gitversioning/commons/GitUtil.java | 6 ++--- .../GitVersioningModelProcessor.java | 22 ++++++++++++------- .../gitversioning/commons/GitUtilTest.java | 19 +++++++++++++++- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/me/qoomon/gitversioning/commons/GitDescription.java b/src/main/java/me/qoomon/gitversioning/commons/GitDescription.java index 34499987..6a3222e4 100644 --- a/src/main/java/me/qoomon/gitversioning/commons/GitDescription.java +++ b/src/main/java/me/qoomon/gitversioning/commons/GitDescription.java @@ -4,11 +4,13 @@ public class GitDescription { private final String commit; private final String tag; private final int distance; + private final boolean foundTag; - public GitDescription(String commit, String tag, int distance) { + public GitDescription(String commit, String tag, int distance, boolean foundTag) { this.commit = commit; this.tag = tag; this.distance = distance; + this.foundTag = foundTag; } public String getCommit() { @@ -23,6 +25,10 @@ public int getDistance() { return distance; } + public int getDistanceOrZero() { + return foundTag ? distance : 0; + } + @Override public String toString() { return tag + "-" + distance + "-g" + commit.substring(0,7); diff --git a/src/main/java/me/qoomon/gitversioning/commons/GitUtil.java b/src/main/java/me/qoomon/gitversioning/commons/GitUtil.java index 77ed29f3..1b185f09 100644 --- a/src/main/java/me/qoomon/gitversioning/commons/GitUtil.java +++ b/src/main/java/me/qoomon/gitversioning/commons/GitUtil.java @@ -53,7 +53,7 @@ public static List tagsPointAt(ObjectId revObjectId, Repository reposito public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern, Repository repository, boolean firstParent) throws IOException { Repository commonRepository = worktreesFix_getCommonRepository(repository); if (revObjectId == null) { - return new GitDescription(NO_COMMIT, "root", 0); + return new GitDescription(NO_COMMIT, "root", 0, false); } Map> objectIdListMap = reverseTagRefMap(repository); @@ -72,7 +72,7 @@ public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern, .findFirst(); if (matchingTag.isPresent()) { - return new GitDescription(revObjectId.getName(), matchingTag.get(), depth); + return new GitDescription(revObjectId.getName(), matchingTag.get(), depth, true); } depth++; } @@ -81,7 +81,7 @@ public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern, throw new IllegalStateException("couldn't find matching tag in shallow git repository"); } - return new GitDescription(revObjectId.getName(), "root", depth); + return new GitDescription(revObjectId.getName(), "root", depth, false); } } diff --git a/src/main/java/me/qoomon/maven/gitversioning/GitVersioningModelProcessor.java b/src/main/java/me/qoomon/maven/gitversioning/GitVersioningModelProcessor.java index 28c90f98..844c7432 100644 --- a/src/main/java/me/qoomon/maven/gitversioning/GitVersioningModelProcessor.java +++ b/src/main/java/me/qoomon/maven/gitversioning/GitVersioningModelProcessor.java @@ -966,14 +966,9 @@ private Map> generateGlobalFormatPlaceholderMap(GitSitu placeholderMap.put("describe.tag.version.label.next", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label").get(), 1))); final Lazy descriptionDistance = Lazy.by(() -> description.get().getDistance()); - placeholderMap.put("describe.distance", Lazy.by(() -> String.valueOf(descriptionDistance.get()))); - placeholderMap.put("describe.distance.snapshot", Lazy.by(() -> (descriptionDistance.get() == 0 ? "" : "-SNAPSHOT"))); - - placeholderMap.put("describe.tag.version.patch.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch").get(), descriptionDistance.get()))); - placeholderMap.put("describe.tag.version.patch.next.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch.next").get(), descriptionDistance.get()))); - - placeholderMap.put("describe.tag.version.label.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label").get(), descriptionDistance.get()))); - placeholderMap.put("describe.tag.version.label.next.plus.describe.distance", Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label.next").get(), descriptionDistance.get()))); + setDescribeDistancePlaceholders(placeholderMap, descriptionDistance, "distance"); + final Lazy descriptionDistanceOrZero = Lazy.by(() -> description.get().getDistanceOrZero()); + setDescribeDistancePlaceholders(placeholderMap, descriptionDistanceOrZero, "distanceOrZero"); // describe tag pattern groups final Lazy> describeTagPatternValues = Lazy.by( @@ -1002,6 +997,17 @@ private Map> generateGlobalFormatPlaceholderMap(GitSitu return placeholderMap; } + private static void setDescribeDistancePlaceholders(Map> placeholderMap, Lazy descriptionDistance, String distanceName) { + placeholderMap.put("describe." + distanceName, Lazy.by(() -> String.valueOf(descriptionDistance.get()))); + placeholderMap.put("describe." + distanceName + ".snapshot", Lazy.by(() -> (descriptionDistance.get() == 0 ? "" : "-SNAPSHOT"))); + + placeholderMap.put("describe.tag.version.patch.plus.describe." + distanceName, Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch").get(), descriptionDistance.get()))); + placeholderMap.put("describe.tag.version.patch.next.plus.describe." + distanceName, Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.patch.next").get(), descriptionDistance.get()))); + + placeholderMap.put("describe.tag.version.label.plus.describe." + distanceName, Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label").get(), descriptionDistance.get()))); + placeholderMap.put("describe.tag.version.label.next.plus.describe." + distanceName, Lazy.by(() -> increase(placeholderMap.get("describe.tag.version.label.next").get(), descriptionDistance.get()))); + } + private Matcher matchVersion(String input) { Matcher matcher = VERSION_PATTERN.matcher(input); //noinspection ResultOfMethodCallIgnored diff --git a/src/test/java/me/qoomon/gitversioning/commons/GitUtilTest.java b/src/test/java/me/qoomon/gitversioning/commons/GitUtilTest.java index 5aed5d23..679ace1f 100644 --- a/src/test/java/me/qoomon/gitversioning/commons/GitUtilTest.java +++ b/src/test/java/me/qoomon/gitversioning/commons/GitUtilTest.java @@ -1,6 +1,7 @@ package me.qoomon.gitversioning.commons; +import org.assertj.core.api.SoftAssertions; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Status; import org.eclipse.jgit.api.errors.GitAPIException; @@ -16,6 +17,7 @@ import java.util.regex.Pattern; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.SoftAssertions.assertSoftly; import static org.eclipse.jgit.lib.Constants.HEAD; import static org.eclipse.jgit.lib.Constants.MASTER; @@ -192,9 +194,24 @@ void describe() throws Exception { // then assertThat(description).satisfies(it -> { assertThat(it.getCommit()).isEqualTo(givenCommit.getName()); - assertThat(it.getDistance()).isEqualTo(0); + assertThat(it.getDistance()).isZero(); + assertThat(it.getDistanceOrZero()).isZero(); assertThat(it.getTag()).isEqualTo(givenTagName); }); } + @Test + void distanceOrZeroIsZeroWhenNoTagMatches() throws Exception { + // given + Git git = Git.init().setInitialBranch(MASTER).setDirectory(tempDir.toFile()).call(); + + final var softly = new SoftAssertions(); + for (int i = 0; i < 3; ++i) { + GitDescription description = GitUtil.describe(head(git), Pattern.compile("v.+"), git.getRepository(), true); + softly.assertThat(description.getDistanceOrZero()).isZero(); + softly.assertThat(description.getDistance()).isEqualTo(i); + git.commit().setMessage("commit " + (i + 1)).setAllowEmpty(true).call(); + } + softly.assertAll(); + } } From e9f82b41b9a3d4a5870cfe08d6cb2fc1b351cc96 Mon Sep 17 00:00:00 2001 From: Marc ROZANC Date: Sun, 22 Sep 2024 02:10:51 +0200 Subject: [PATCH 2/8] Update documentation --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 8b2a4af8..af8fb45f 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,9 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY` - `${describe}` Will resolve to `git describe` output - `${describe.distance}` The distance count to last matching tag +- `${describe.distanceOrZero}` The distance count to last matching tag or 0 if no tag is found - `${describe.distance.snapshot}` Empty string on matching tag, `-SNAPSHOT` if `describe.distance > 0` +- `${describe.distanceOrZero.snapshot}` Empty string on matching tag, `-SNAPSHOT` if `describe.distanceOrZero > 0` - `${describe.tag}` The matching tag of `git describe` - `${describe.tag.version}` the tag version determined by regex `(?(?(?\d+)(?:\.(?\d+)(?:\.(?\d+))?)?)(?:-(?