Skip to content

Commit 2c24fc7

Browse files
authored
Merge pull request #72 from blackwhale/add-git-tag-on-commit
add git.tag to get the tag on a certain commit
2 parents 8728c39 + 81f9ff5 commit 2c24fc7

File tree

7 files changed

+78
-0
lines changed

7 files changed

+78
-0
lines changed

src/main/java/pl/project13/core/GitCommitPropertyConstant.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ public class GitCommitPropertyConstant {
187187
* The closest tag may depend on your git describe config that may or may not take lightweight tags into consideration.
188188
*/
189189
public static final String CLOSEST_TAG_NAME = "closest.tag.name";
190+
/**
191+
* Represents the tag on the current commit.
192+
* Similar to running
193+
* <pre>
194+
* git tag --points-at HEAD
195+
* </pre>
196+
*/
197+
public static final String TAG = "tag";
190198
/**
191199
* Represents the number of commits to the closest available tag.
192200
* The closest tag may depend on your git describe config that may or may not take lightweight tags into consideration.

src/main/java/pl/project13/core/GitDataProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ protected void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Map<String
305305

306306
//
307307
maybePut(properties, GitCommitPropertyConstant.TAGS, this::getTags);
308+
maybePut(properties, GitCommitPropertyConstant.TAG, this::getTag);
308309

309310
maybePut(properties,GitCommitPropertyConstant.CLOSEST_TAG_NAME, this::getClosestTagName);
310311
maybePut(properties,GitCommitPropertyConstant.CLOSEST_TAG_COMMIT_COUNT, this::getClosestTagCommitCount);

src/main/java/pl/project13/core/GitProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public interface GitProvider {
5757

5858
String getTags() throws GitCommitIdExecutionException;
5959

60+
String getTag() throws GitCommitIdExecutionException;
61+
6062
String getClosestTagName() throws GitCommitIdExecutionException;
6163

6264
String getClosestTagCommitCount() throws GitCommitIdExecutionException;

src/main/java/pl/project13/core/JGitProvider.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ public String getTags() throws GitCommitIdExecutionException {
256256
}
257257
}
258258

259+
@Override
260+
public String getTag() throws GitCommitIdExecutionException {
261+
try {
262+
Repository repo = getGitRepository();
263+
ObjectId headId = evalCommit.toObjectId();
264+
Collection<String> tags = jGitCommon.getTag(repo, headId);
265+
return String.join(",", tags);
266+
} catch (GitAPIException e) {
267+
log.error(String.format("Unable to extract tag from commit: %s", evalCommit.getName()), e);
268+
return "";
269+
}
270+
}
271+
259272
@Override
260273
public String getClosestTagName() throws GitCommitIdExecutionException {
261274
Repository repo = getGitRepository();

src/main/java/pl/project13/core/NativeGitProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ public String getTags() throws GitCommitIdExecutionException {
275275
return result.replace('\n', ',');
276276
}
277277

278+
@Override
279+
public String getTag() throws GitCommitIdExecutionException {
280+
final String result = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "tag --points-at " + evaluateOnCommit);
281+
return result.replace('\n', ',');
282+
}
283+
278284
@Override
279285
public String getRemoteOriginUrl() throws GitCommitIdExecutionException {
280286
return getOriginRemote(canonical, nativeGitTimeoutInMs);

src/main/java/pl/project13/core/jgit/JGitCommon.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,30 @@ private Collection<String> getTags(final Git git, final ObjectId objectId, final
7878
.collect(Collectors.toList());
7979
}
8080

81+
public Collection<String> getTag(Repository repo, final ObjectId objectId) throws GitAPIException {
82+
try (Git git = Git.wrap(repo)) {
83+
Collection<String> tags = getTagsOnObjectId(git, objectId);
84+
return tags;
85+
}
86+
}
87+
88+
private Collection<String> getTagsOnObjectId(final Git git, final ObjectId objectId) throws GitAPIException {
89+
return git.tagList().call()
90+
.stream()
91+
.filter(tagRef -> {
92+
try {
93+
if (objectId.name().equals(tagRef.getObjectId().name())) {
94+
return true;
95+
}
96+
} catch (Exception ignored) {
97+
log.debug(String.format("Failed while getTagOnObjectId [%s] -- ", tagRef));
98+
}
99+
return false;
100+
})
101+
.map(tagRef -> trimFullTagName(tagRef.getName()))
102+
.collect(Collectors.toList());
103+
}
104+
81105
public String getClosestTagName(@Nonnull String evaluateOnCommit, @Nonnull Repository repo, GitDescribeConfig gitDescribe) {
82106
// TODO: Why does some tests fail when it gets headCommit from JGitprovider?
83107
RevCommit headCommit = findEvalCommitObjectId(evaluateOnCommit, repo);

src/test/java/pl/project13/core/GitCommitIdPluginIntegrationTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,30 @@ public void shouldGenerateClosestTagInformationWhenOnATag(boolean useNativeGit)
980980
assertPropertyPresentAndEqual(properties, "git.closest.tag.commit.count", "0");
981981
}
982982

983+
@Test
984+
@Parameters(method = "useNativeGit")
985+
public void shouldGenerateTagInformationWhenOnATag(boolean useNativeGit) throws Exception {
986+
// given
987+
File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.ON_A_TAG);
988+
989+
GitDescribeConfig gitDescribeConfig = createGitDescribeConfig(false, 7);
990+
gitDescribeConfig.setDirty("-dirty"); // checking if dirty works as expected
991+
992+
GitCommitIdPlugin.Callback cb =
993+
new GitCommitIdTestCallback()
994+
.setDotGitDirectory(dotGitDirectory)
995+
.setUseNativeGit(useNativeGit)
996+
.setGitDescribeConfig(gitDescribeConfig)
997+
.build();
998+
Properties properties = new Properties();
999+
1000+
// when
1001+
GitCommitIdPlugin.runPlugin(cb, properties);
1002+
1003+
// then
1004+
assertPropertyPresentAndEqual(properties, "git.tag", "v1.0.0");
1005+
}
1006+
9831007
@Test
9841008
@Parameters(method = "useNativeGit")
9851009
public void shouldGenerateClosestTagInformationWhenOnATagAndDirty(boolean useNativeGit) throws Exception {

0 commit comments

Comments
 (0)