summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorHåvard Wall <haavardw@gmail.com>2018-10-17 15:34:51 +0200
committerHåvard Wall <haavardw@gmail.com>2018-11-09 08:54:44 +0100
commitf9de9175477577c6c7d0a22ebe5a1c9a1c299c1a (patch)
tree192bb166301eb535fee9b63dcd23d585306ecd93 /org.eclipse.jgit
parent9b6d30f2c145243bfdb1708bef847538a0df640d (diff)
downloadjgit-f9de9175477577c6c7d0a22ebe5a1c9a1c299c1a.tar.gz
jgit-f9de9175477577c6c7d0a22ebe5a1c9a1c299c1a.zip
Fix git-describe tie-breakers
Correct behaviour as git 1.7.1.1 is to resolve tie-breakers to choose the most recent tag. https://github.com/git/git/blob/master/Documentation/RelNotes/1.7.1.1.txt: * "git describe" did not tie-break tags that point at the same commit correctly; newer ones are preferred by paying attention to the tagger date now. Bug: 538610 Change-Id: Ib0b2a301997bb7f75935baf7005473f4de952a64 Signed-off-by: Håvard Wall <haavardw@gmail.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java23
1 files changed, 22 insertions, 1 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java
index 01fe4aa9ee..18e8a7f4f9 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java
@@ -50,6 +50,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
+import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -71,6 +72,7 @@ import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevFlag;
import org.eclipse.jgit.revwalk.RevFlagSet;
+import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
/**
@@ -203,11 +205,29 @@ public class DescribeCommand extends GitCommand<String> {
return this;
}
+ private final Comparator<Ref> TAG_TIE_BREAKER = new Comparator<Ref>() {
+
+ @Override
+ public int compare(Ref o1, Ref o2) {
+ try {
+ return tagDate(o2).compareTo(tagDate(o1));
+ } catch (IOException e) {
+ return 0;
+ }
+ }
+
+ private Date tagDate(Ref tag) throws IOException {
+ RevTag t = w.parseTag(tag.getObjectId());
+ w.parseBody(t);
+ return t.getTaggerIdent().getWhen();
+ }
+ };
+
private Optional<Ref> getBestMatch(List<Ref> tags) {
if (tags == null || tags.size() == 0) {
return Optional.empty();
} else if (matchers.size() == 0) {
- // No matchers, simply return the first tag entry
+ Collections.sort(tags, TAG_TIE_BREAKER);
return Optional.of(tags.get(0));
} else {
// Find the first tag that matches one of the matchers; precedence according to matcher definition order
@@ -215,6 +235,7 @@ public class DescribeCommand extends GitCommand<String> {
Optional<Ref> match = tags.stream()
.filter(tag -> matcher.matches(tag.getName(), false,
false))
+ .sorted(TAG_TIE_BREAKER)
.findFirst();
if (match.isPresent()) {
return match;