Skip to content

Commit

Permalink
Merge pull request #72 from blackwhale/add-git-tag-on-commit
Browse files Browse the repository at this point in the history
add git.tag to get the tag on a certain commit
  • Loading branch information
TheSnoozer authored Nov 27, 2023
2 parents 8728c39 + 81f9ff5 commit 2c24fc7
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <pre>
* git tag --points-at HEAD
* </pre>
*/
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.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/pl/project13/core/GitDataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ protected void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Map<String

//
maybePut(properties, GitCommitPropertyConstant.TAGS, this::getTags);
maybePut(properties, GitCommitPropertyConstant.TAG, this::getTag);

maybePut(properties,GitCommitPropertyConstant.CLOSEST_TAG_NAME, this::getClosestTagName);
maybePut(properties,GitCommitPropertyConstant.CLOSEST_TAG_COMMIT_COUNT, this::getClosestTagCommitCount);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/pl/project13/core/GitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public interface GitProvider {

String getTags() throws GitCommitIdExecutionException;

String getTag() throws GitCommitIdExecutionException;

String getClosestTagName() throws GitCommitIdExecutionException;

String getClosestTagCommitCount() throws GitCommitIdExecutionException;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/pl/project13/core/JGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,19 @@ public String getTags() throws GitCommitIdExecutionException {
}
}

@Override
public String getTag() throws GitCommitIdExecutionException {
try {
Repository repo = getGitRepository();
ObjectId headId = evalCommit.toObjectId();
Collection<String> 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();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/pl/project13/core/NativeGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/pl/project13/core/jgit/JGitCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ private Collection<String> getTags(final Git git, final ObjectId objectId, final
.collect(Collectors.toList());
}

public Collection<String> getTag(Repository repo, final ObjectId objectId) throws GitAPIException {
try (Git git = Git.wrap(repo)) {
Collection<String> tags = getTagsOnObjectId(git, objectId);
return tags;
}
}

private Collection<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2c24fc7

Please sign in to comment.