Skip to content

Commit 263fcde

Browse files
author
TheSnoozer
committed
#339: git.tags yields wrong results for JGit
1 parent 71c744a commit 263fcde

2 files changed

Lines changed: 46 additions & 9 deletions

File tree

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,31 @@ public JGitCommon(LoggerBridge log) {
5252
this.log = log;
5353
}
5454

55-
public Collection<String> getTags(Repository repo, final ObjectId headId) throws GitAPIException {
55+
public Collection<String> getTags(Repository repo, final ObjectId objectId) throws GitAPIException {
5656
try (Git git = Git.wrap(repo)) {
5757
try (RevWalk walk = new RevWalk(repo)) {
58-
Collection<String> tags = getTags(git, headId, walk);
58+
Collection<String> tags = getTags(git, objectId, walk);
5959
walk.dispose();
6060
return tags;
6161
}
6262
}
6363
}
6464

65-
private Collection<String> getTags(final Git git, final ObjectId headId, final RevWalk finalWalk) throws GitAPIException {
65+
private Collection<String> getTags(final Git git, final ObjectId objectId, final RevWalk finalWalk) throws GitAPIException {
6666
List<Ref> tagRefs = git.tagList().call();
6767
Collection<Ref> tagsForHeadCommit = Collections2.filter(tagRefs, new Predicate<Ref>() {
68-
@Override public boolean apply(Ref tagRef) {
69-
boolean lightweightTag = tagRef.getObjectId().equals(headId);
68+
@Override
69+
public boolean apply(Ref tagRef) {
7070
try {
71-
// TODO make this configurable (most users shouldn't really care too much what kind of tag it is though)
72-
return lightweightTag || finalWalk.parseTag(tagRef.getObjectId()).getObject().getId().equals(headId); // or normal tag
73-
} catch (IOException e) {
74-
return false;
71+
final RevCommit tagCommit = finalWalk.parseCommit(tagRef.getObjectId());
72+
final RevCommit objectCommit = finalWalk.parseCommit(objectId);
73+
if (finalWalk.isMergedInto(objectCommit, tagCommit)) {
74+
return true;
75+
}
76+
} catch (Exception ignored) {
77+
log.debug("Failed while getTags [{}] -- ", tagRef, ignored);
7578
}
79+
return false;
7680
}
7781
});
7882
Collection<String> tags = Collections2.transform(tagsForHeadCommit, new Function<Ref, String>() {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,39 @@ public void shouldExtractTagsOnGivenCommit(boolean useNativeGit) throws Exceptio
890890
.containsOnly("lightweight-tag", "newest-tag");
891891
}
892892

893+
@Test
894+
@Parameters(method = "useNativeGit")
895+
public void shouldExtractTagsOnGivenCommitWithOldestCommit(boolean useNativeGit) throws Exception {
896+
// given
897+
mavenSandbox.withParentProject("my-jar-project", "jar")
898+
.withNoChildProject()
899+
.withGitRepoInParent(AvailableGitTestRepo.WITH_COMMIT_THAT_HAS_TWO_TAGS)
900+
.create();
901+
902+
try (final Git git = git("my-jar-project")) {
903+
git.reset().setMode(ResetCommand.ResetType.HARD).setRef("9597545").call();
904+
}
905+
906+
MavenProject targetProject = mavenSandbox.getParentProject();
907+
setProjectToExecuteMojoIn(targetProject);
908+
909+
mojo.setGitDescribe(null);
910+
mojo.setUseNativeGit(useNativeGit);
911+
912+
// when
913+
mojo.execute();
914+
915+
// then
916+
Properties properties = targetProject.getProperties();
917+
assertGitPropertiesPresentInProject(properties);
918+
919+
assertThat(properties).satisfies(new ContainsKeyCondition("git.tags"));
920+
assertThat(properties.get("git.tags").toString()).doesNotContain("refs/tags/");
921+
922+
assertThat(Splitter.on(",").split(properties.get("git.tags").toString()))
923+
.containsOnly("annotated-tag", "lightweight-tag", "newest-tag");
924+
}
925+
893926
@Test
894927
@Parameters(method = "useNativeGit")
895928
public void runGitDescribeWithMatchOption(boolean useNativeGit) throws Exception {

0 commit comments

Comments
 (0)