Browse Source

Fix DescribeCommand with multiple match options

when multiple match options are given in git describe the result must
not depend on the order of the match options. JGit wrongly picked the
first match using the match options in the order they were defined. Fix
this by concatenating the streams of matching tags for all match options
and then choosing the first match on the concatenated stream sorted in
tie break order.

See https://git-scm.com/docs/git-describe#git-describe---matchltpatterngt

Change-Id: Id01433d35fa16fb4c30526605bee041ac1d954b2
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.11
Matthias Sohn 6 years ago
parent
commit
df6263644b
  1. 4
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java
  2. 18
      org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java

4
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java

@ -156,9 +156,9 @@ public class DescribeCommandTest extends RepositoryTestCase {
assertEquals("v1.1.1-1-gb89dead", describe(c2, "v1.1*")); assertEquals("v1.1.1-1-gb89dead", describe(c2, "v1.1*"));
// Ensure that ordering of match precedence is preserved as per Git behaviour // Ensure that ordering of match precedence is preserved as per Git behaviour
assertEquals("v1.0.1", describe(c1, "v1.0*", "v1.1*")); assertEquals("v1.1.1", describe(c1, "v1.0*", "v1.1*"));
assertEquals("v1.1.1", describe(c1, "v1.1*", "v1.0*")); assertEquals("v1.1.1", describe(c1, "v1.1*", "v1.0*"));
assertEquals("v1.0.1-1-gb89dead", describe(c2, "v1.0*", "v1.1*")); assertEquals("v1.1.1-1-gb89dead", describe(c2, "v1.0*", "v1.1*"));
assertEquals("v1.1.1-1-gb89dead", describe(c2, "v1.1*", "v1.0*")); assertEquals("v1.1.1-1-gb89dead", describe(c2, "v1.1*", "v1.0*"));
} else { } else {
// no timestamps so no guarantees on which tag is chosen // no timestamps so no guarantees on which tag is chosen

18
org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java

@ -55,6 +55,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.JGitInternalException;
@ -230,18 +231,15 @@ public class DescribeCommand extends GitCommand<String> {
Collections.sort(tags, TAG_TIE_BREAKER); Collections.sort(tags, TAG_TIE_BREAKER);
return Optional.of(tags.get(0)); return Optional.of(tags.get(0));
} else { } else {
// Find the first tag that matches one of the matchers; precedence according to matcher definition order // Find the first tag that matches in the stream of all tags
// filtered by matchers ordered by tie break order
Stream<Ref> matchingTags = Stream.empty();
for (IMatcher matcher : matchers) { for (IMatcher matcher : matchers) {
Optional<Ref> match = tags.stream() Stream<Ref> m = tags.stream().filter(
.filter(tag -> matcher.matches(tag.getName(), false, tag -> matcher.matches(tag.getName(), false, false));
false)) matchingTags = Stream.of(matchingTags, m).flatMap(i -> i);
.sorted(TAG_TIE_BREAKER)
.findFirst();
if (match.isPresent()) {
return match;
} }
} return matchingTags.sorted(TAG_TIE_BREAKER).findFirst();
return Optional.empty();
} }
} }

Loading…
Cancel
Save