Skip to content

Commit 198e5e4

Browse files
committed
fix: fix git describe tag selection, if multiple tags point to head
1 parent c85bc48 commit 198e5e4

File tree

6 files changed

+88
-53
lines changed

6 files changed

+88
-53
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 6.1.3
4+
5+
##### Fixes
6+
- fix git describe tag selection, if multiple tags point to head
7+
8+
39
## 6.1.1
410

511
##### Fixes

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
}
1313

1414
group 'me.qoomon'
15-
version '6.1.2'
15+
version '6.1.3'
1616
sourceCompatibility = JavaVersion.VERSION_1_8
1717
targetCompatibility = JavaVersion.VERSION_1_8
1818

src/main/java/me/qoomon/gitversioning/commons/GitUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static List<String> tagsPointAt(Repository repository, ObjectId revObject
5454
public static List<String> tagsPointAt(Repository repository, ObjectId revObjectId,
5555
Map<ObjectId, List<Ref>> reverseTagRefMap) {
5656
return reverseTagRefMap.getOrDefault(revObjectId, emptyList()).stream()
57-
.sorted(new RefNameComparator(repository))
57+
.sorted(new TagComparator(repository))
5858
.map(ref -> shortenRefName(ref.getName()))
5959
.collect(toList());
6060
}
@@ -82,7 +82,7 @@ public static GitDescription describe(Repository repository, ObjectId revObjectI
8282
depth++;
8383

8484
Optional<Ref> matchingTag = reverseTagRefMap.getOrDefault(rev, emptyList()).stream()
85-
.sorted(new RefNameComparator(repository))
85+
.sorted(new TagComparator(repository))
8686
.filter(tag -> tagPattern.matcher(shortenRefName(tag.getName())).matches())
8787
.findFirst();
8888

src/main/java/me/qoomon/gitversioning/commons/RefNameComparator.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package me.qoomon.gitversioning.commons;
2+
3+
import org.eclipse.jgit.lib.Ref;
4+
import org.eclipse.jgit.lib.Repository;
5+
import org.eclipse.jgit.revwalk.*;
6+
7+
import java.io.IOException;
8+
import java.util.Comparator;
9+
import java.util.Date;
10+
11+
import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
12+
13+
public class TagComparator implements Comparator<Ref> {
14+
15+
private final RevWalk revWalk;
16+
17+
public TagComparator(Repository repository) {
18+
this.revWalk = new RevWalk(repository);
19+
}
20+
21+
@Override
22+
public int compare(Ref ref1, Ref ref2) {
23+
try {
24+
RevTag revTag1 = revWalk.parseTag(ref1.getObjectId());
25+
RevTag revTag2 = revWalk.parseTag(ref2.getObjectId());
26+
27+
// both tags are annotated tags
28+
if (revTag1.getType() == OBJ_TAG && revTag2.getType() == OBJ_TAG) {
29+
Date revTag1Date = revTag1.getTaggerIdent().getWhen();
30+
Date revTag2Date = revTag2.getTaggerIdent().getWhen();
31+
// most recent tags first
32+
return -revTag1Date.compareTo(revTag2Date);
33+
}
34+
35+
// only ref1 is annotated tag
36+
if (revTag1.getType() == OBJ_TAG) {
37+
return -1;
38+
}
39+
40+
// only ref2 is annotated tag
41+
if (revTag2.getType() == OBJ_TAG) {
42+
return 1;
43+
}
44+
45+
// both tags are lightweight tags
46+
return revTag1.name().compareTo(revTag1.getName());
47+
} catch (IOException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
51+
}

src/test/java/me/qoomon/gitversioning/commons/GitUtilTest.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.eclipse.jgit.api.Git;
55
import org.eclipse.jgit.api.Status;
66
import org.eclipse.jgit.api.errors.GitAPIException;
7+
import org.eclipse.jgit.api.errors.InvalidRefNameException;
78
import org.eclipse.jgit.lib.ObjectId;
89
import org.eclipse.jgit.revwalk.RevCommit;
910
import org.junit.jupiter.api.Test;
@@ -13,6 +14,7 @@
1314
import java.io.IOException;
1415
import java.nio.file.Path;
1516
import java.util.List;
17+
import java.util.regex.Pattern;
1618

1719
import static org.assertj.core.api.Assertions.assertThat;
1820
import static org.eclipse.jgit.lib.Constants.HEAD;
@@ -88,7 +90,7 @@ void tagsPointAt_emptyRepo() throws GitAPIException, IOException {
8890
// given
8991
Git git = Git.init().setInitialBranch(MASTER).setDirectory(tempDir.toFile()).call();
9092
// when
91-
93+
9294
List<String> tags = GitUtil.tagsPointAt(git.getRepository(), head(git));
9395

9496
// then
@@ -167,10 +169,33 @@ void tagsPointAt_lightweightTag() throws GitAPIException, IOException {
167169
assertThat(tags).containsExactlyInAnyOrder(givenTagName);
168170
}
169171

172+
private static ObjectId head(Git git) throws IOException {
173+
return git.getRepository().resolve(HEAD);
174+
}
175+
170176
@Test
177+
void describe() throws Exception {
178+
// given
179+
Git git = Git.init().setInitialBranch(MASTER).setDirectory(tempDir.toFile()).call();
171180

181+
RevCommit givenCommit = git.commit().setMessage("initial commit").setAllowEmpty(true).call();
172182

173-
private static ObjectId head(Git git) throws IOException {
174-
return git.getRepository().resolve(HEAD);
183+
git.tag().setName("v2.1").setAnnotated(true).setObjectId(givenCommit).setMessage(".").call();
184+
git.tag().setName("v1.2").setAnnotated(true).setObjectId(givenCommit).setMessage(".").call();
185+
git.tag().setName("v2.3").setAnnotated(true).setObjectId(givenCommit).setMessage(".").call();
186+
Thread.sleep(1000);
187+
String givenTagName = "v1.3";
188+
git.tag().setName(givenTagName).setAnnotated(true).setObjectId(givenCommit).setMessage(".").call();
189+
190+
// when
191+
GitDescription description = GitUtil.describe(git.getRepository(), head(git), Pattern.compile("v.+"));
192+
193+
// then
194+
assertThat(description).satisfies(it -> {
195+
assertThat(it.getCommit()).isEqualTo(givenCommit.getName());
196+
assertThat(it.getDistance()).isEqualTo(0);
197+
assertThat(it.getTag()).isEqualTo(givenTagName);
198+
});
175199
}
200+
176201
}

0 commit comments

Comments
 (0)