Skip to content

Commit 3d12d1d

Browse files
author
TheSnoozer
committed
#221: ensure that git.closest.tag.name and git.closest.tag.count prefer annotated tags (like the discribe command) and also works with patterns like 'light*'
1 parent f3db1d4 commit 3d12d1d

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/main/java/pl/project13/jgit/DescribeCommand.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,6 @@ private String createMatchPattern() {
353353
return ".*";
354354
}
355355

356-
return "^refs/tags/\\Q" +
357-
matchOption.get().replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q") +
358-
"\\E$";
356+
return jGitCommon.createMatchPattern(matchOption.get());
359357
}
360358
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private Pair<RevCommit, String> getClosestRevCommit(@NotNull Repository repo, Re
106106
if (gitDescribe != null) {
107107
includeLightweightTags = gitDescribe.getTags();
108108
if (!"*".equals(gitDescribe.getMatch())) {
109-
matchPattern = gitDescribe.getMatch();
109+
matchPattern = createMatchPattern(gitDescribe.getMatch());
110110
}
111111
}
112112
Map<ObjectId, List<String>> tagObjectIdToName = findTagObjectIds(repo, includeLightweightTags, matchPattern);
@@ -121,6 +121,12 @@ private Pair<RevCommit, String> getClosestRevCommit(@NotNull Repository repo, Re
121121
return Pair.of(revCommit, tagName);
122122
}
123123

124+
protected String createMatchPattern(String pattern) {
125+
return "^refs/tags/\\Q" +
126+
pattern.replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q") +
127+
"\\E$";
128+
}
129+
124130
protected Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern) {
125131
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, includeLightweightTags, matchPattern);
126132
Map<ObjectId, List<String>> commitIdsToTagNames = transformRevTagsMapToDateSortedTagNames(commitIdsToTags);

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,73 @@ public void shouldGenerateClosestTagInformationWithIncludeLightweightTagsForClos
914914
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.closest.tag.commit.count", "1");
915915
}
916916

917+
@Test
918+
@Parameters(method = "useNativeGit")
919+
public void shouldGenerateClosestTagInformationWithIncludeLightweightTagsForClosestTagAndPreferAnnotatedTags(boolean useNativeGit) throws Exception {
920+
// given
921+
mavenSandbox
922+
.withParentProject("my-jar-project", "jar")
923+
.withNoChildProject()
924+
.withGitRepoInParent(AvailableGitTestRepo.WITH_COMMIT_THAT_HAS_TWO_TAGS)
925+
.create();
926+
927+
MavenProject targetProject = mavenSandbox.getParentProject();
928+
setProjectToExecuteMojoIn(targetProject);
929+
930+
GitDescribeConfig gitDescribe = createGitDescribeConfig(true, 9);
931+
gitDescribe.setDirty("-customDirtyMark");
932+
gitDescribe.setTags(true); // include lightweight tags
933+
934+
mojo.setGitDescribe(gitDescribe);
935+
mojo.setUseNativeGit(useNativeGit);
936+
937+
// when
938+
mojo.execute();
939+
940+
// then
941+
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.commit.id.abbrev", "b6a73ed");
942+
943+
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.commit.id.describe", "newest-tag-1-gb6a73ed74-customDirtyMark");
944+
945+
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.closest.tag.name", "newest-tag");
946+
947+
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.closest.tag.commit.count", "1");
948+
}
949+
950+
@Test
951+
@Parameters(method = "useNativeGit")
952+
public void shouldGenerateClosestTagInformationWithIncludeLightweightTagsForClosestTagAndFilter(boolean useNativeGit) throws Exception {
953+
// given
954+
mavenSandbox
955+
.withParentProject("my-jar-project", "jar")
956+
.withNoChildProject()
957+
.withGitRepoInParent(AvailableGitTestRepo.WITH_COMMIT_THAT_HAS_TWO_TAGS)
958+
.create();
959+
960+
MavenProject targetProject = mavenSandbox.getParentProject();
961+
setProjectToExecuteMojoIn(targetProject);
962+
963+
GitDescribeConfig gitDescribe = createGitDescribeConfig(true, 9);
964+
gitDescribe.setDirty("-customDirtyMark");
965+
gitDescribe.setTags(true); // include lightweight tags
966+
gitDescribe.setMatch("light*");
967+
968+
mojo.setGitDescribe(gitDescribe);
969+
mojo.setUseNativeGit(useNativeGit);
970+
971+
// when
972+
mojo.execute();
973+
974+
// then
975+
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.commit.id.abbrev", "b6a73ed");
976+
977+
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.commit.id.describe", "lightweight-tag-1-gb6a73ed74-customDirtyMark");
978+
979+
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.closest.tag.name", "lightweight-tag");
980+
981+
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.closest.tag.commit.count", "1");
982+
}
983+
917984
private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev) {
918985
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
919986
gitDescribeConfig.setTags(true);

0 commit comments

Comments
 (0)