From 10ab407fa6414a1c4ae08696c78e078cce078519 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Tue, 30 Apr 2019 19:11:16 +0200 Subject: [PATCH] DescribeCommand: use glob match instead of path match Otherwise tags may fail to match if their name contains slashes. Canonical git also uses its wildcard matcher in glob mode.[1] [1] https://github.com/git/git/blob/v2.21.0/builtin/describe.c#L182 Bug: 546703 Change-Id: I122c7959974fa1fc6a53dfc65837e4314a8badd4 Signed-off-by: Thomas Wolf --- .../eclipse/jgit/api/DescribeCommandTest.java | 17 +++++++++++++++++ .../org/eclipse/jgit/api/DescribeCommand.java | 17 +++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java index fd3aa2884..df9ae6a0f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java @@ -421,6 +421,23 @@ public class DescribeCommandTest extends RepositoryTestCase { } } + @Test + public void globMatchWithSlashes() throws Exception { + ObjectId c1 = modify("aaa"); + tag("a/b/version"); + ObjectId c2 = modify("bbb"); + tag("a/b/version2"); + if (useAnnotatedTags || describeUseAllTags) { + assertEquals("a/b/version", describe(c1, "*/version*")); + assertEquals("a/b/version2", describe(c2, "*/version*")); + } else { + assertNull(describe(c1)); + assertNull(describe(c1, "*/version*")); + assertNull(describe(c2)); + assertNull(describe(c2, "*/version*")); + } + } + private ObjectId merge(ObjectId c2) throws GitAPIException { return git.merge().include(c2).call().getNewHead(); } 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 db3a3d947..9ad77e65f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java @@ -63,8 +63,7 @@ import org.eclipse.jgit.api.errors.RefNotFoundException; import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.InvalidPatternException; import org.eclipse.jgit.errors.MissingObjectException; -import org.eclipse.jgit.ignore.internal.IMatcher; -import org.eclipse.jgit.ignore.internal.PathMatcher; +import org.eclipse.jgit.fnmatch.FileNameMatcher; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; @@ -104,7 +103,7 @@ public class DescribeCommand extends GitCommand { /** * Pattern matchers to be applied to tags under consideration. */ - private List matchers = new ArrayList<>(); + private List matchers = new ArrayList<>(); /** * Whether to use all tags (incl. lightweight) or not. @@ -242,7 +241,7 @@ public class DescribeCommand extends GitCommand { */ public DescribeCommand setMatch(String... patterns) throws InvalidPatternException { for (String p : patterns) { - matchers.add(PathMatcher.createPathMatcher(p, null, false)); + matchers.add(new FileNameMatcher(p, null)); } return this; } @@ -275,9 +274,15 @@ public class DescribeCommand extends GitCommand { // Find the first tag that matches in the stream of all tags // filtered by matchers ordered by tie break order Stream matchingTags = Stream.empty(); - for (IMatcher matcher : matchers) { + for (FileNameMatcher matcher : matchers) { Stream m = tags.stream().filter( - tag -> matcher.matches(tag.getName(), false, false)); + tag -> { + matcher.append( + tag.getName().substring(R_TAGS.length())); + boolean result = matcher.isMatch(); + matcher.reset(); + return result; + }); matchingTags = Stream.of(matchingTags, m).flatMap(i -> i); } return matchingTags.sorted(TAG_TIE_BREAKER).findFirst();