Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add git.tag to get the tag on a certain commit #72

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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