diff --git a/src/main/java/pl/project13/core/GitCommitPropertyConstant.java b/src/main/java/pl/project13/core/GitCommitPropertyConstant.java index 13958df..2947fcd 100644 --- a/src/main/java/pl/project13/core/GitCommitPropertyConstant.java +++ b/src/main/java/pl/project13/core/GitCommitPropertyConstant.java @@ -187,6 +187,14 @@ public class GitCommitPropertyConstant { * The closest tag may depend on your git describe config that may or may not take lightweight tags into consideration. */ public static final String CLOSEST_TAG_NAME = "closest.tag.name"; + /** + * Represents the tag on the current commit. + * Similar to running + *
+   *     git tag --points-at HEAD
+   * 
+ */ + public static final String TAG = "tag"; /** * Represents the number of commits to the closest available tag. * The closest tag may depend on your git describe config that may or may not take lightweight tags into consideration. diff --git a/src/main/java/pl/project13/core/GitDataProvider.java b/src/main/java/pl/project13/core/GitDataProvider.java index d2c6f54..06e7228 100644 --- a/src/main/java/pl/project13/core/GitDataProvider.java +++ b/src/main/java/pl/project13/core/GitDataProvider.java @@ -305,6 +305,7 @@ protected void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Map tags = jGitCommon.getTag(repo, headId); + return String.join(",", tags); + } catch (GitAPIException e) { + log.error(String.format("Unable to extract tag from commit: %s", evalCommit.getName()), e); + return ""; + } + } + @Override public String getClosestTagName() throws GitCommitIdExecutionException { Repository repo = getGitRepository(); diff --git a/src/main/java/pl/project13/core/NativeGitProvider.java b/src/main/java/pl/project13/core/NativeGitProvider.java index e3c2f97..1e5702a 100644 --- a/src/main/java/pl/project13/core/NativeGitProvider.java +++ b/src/main/java/pl/project13/core/NativeGitProvider.java @@ -275,6 +275,12 @@ public String getTags() throws GitCommitIdExecutionException { return result.replace('\n', ','); } + @Override + public String getTag() throws GitCommitIdExecutionException { + final String result = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "tag --points-at " + evaluateOnCommit); + return result.replace('\n', ','); + } + @Override public String getRemoteOriginUrl() throws GitCommitIdExecutionException { return getOriginRemote(canonical, nativeGitTimeoutInMs); diff --git a/src/main/java/pl/project13/core/jgit/JGitCommon.java b/src/main/java/pl/project13/core/jgit/JGitCommon.java index 94d2e1d..b429005 100644 --- a/src/main/java/pl/project13/core/jgit/JGitCommon.java +++ b/src/main/java/pl/project13/core/jgit/JGitCommon.java @@ -78,6 +78,30 @@ private Collection getTags(final Git git, final ObjectId objectId, final .collect(Collectors.toList()); } + public Collection getTag(Repository repo, final ObjectId objectId) throws GitAPIException { + try (Git git = Git.wrap(repo)) { + Collection tags = getTagsOnObjectId(git, objectId); + return tags; + } + } + + private Collection getTagsOnObjectId(final Git git, final ObjectId objectId) throws GitAPIException { + return git.tagList().call() + .stream() + .filter(tagRef -> { + try { + if (objectId.name().equals(tagRef.getObjectId().name())) { + return true; + } + } catch (Exception ignored) { + log.debug(String.format("Failed while getTagOnObjectId [%s] -- ", tagRef)); + } + return false; + }) + .map(tagRef -> trimFullTagName(tagRef.getName())) + .collect(Collectors.toList()); + } + public String getClosestTagName(@Nonnull String evaluateOnCommit, @Nonnull Repository repo, GitDescribeConfig gitDescribe) { // TODO: Why does some tests fail when it gets headCommit from JGitprovider? RevCommit headCommit = findEvalCommitObjectId(evaluateOnCommit, repo); diff --git a/src/test/java/pl/project13/core/GitCommitIdPluginIntegrationTest.java b/src/test/java/pl/project13/core/GitCommitIdPluginIntegrationTest.java index e00e868..8bdc28a 100644 --- a/src/test/java/pl/project13/core/GitCommitIdPluginIntegrationTest.java +++ b/src/test/java/pl/project13/core/GitCommitIdPluginIntegrationTest.java @@ -980,6 +980,30 @@ public void shouldGenerateClosestTagInformationWhenOnATag(boolean useNativeGit) assertPropertyPresentAndEqual(properties, "git.closest.tag.commit.count", "0"); } + @Test + @Parameters(method = "useNativeGit") + public void shouldGenerateTagInformationWhenOnATag(boolean useNativeGit) throws Exception { + // given + File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.ON_A_TAG); + + GitDescribeConfig gitDescribeConfig = createGitDescribeConfig(false, 7); + gitDescribeConfig.setDirty("-dirty"); // checking if dirty works as expected + + GitCommitIdPlugin.Callback cb = + new GitCommitIdTestCallback() + .setDotGitDirectory(dotGitDirectory) + .setUseNativeGit(useNativeGit) + .setGitDescribeConfig(gitDescribeConfig) + .build(); + Properties properties = new Properties(); + + // when + GitCommitIdPlugin.runPlugin(cb, properties); + + // then + assertPropertyPresentAndEqual(properties, "git.tag", "v1.0.0"); + } + @Test @Parameters(method = "useNativeGit") public void shouldGenerateClosestTagInformationWhenOnATagAndDirty(boolean useNativeGit) throws Exception {