]> git.ipfire.org Git - thirdparty/git.git/commitdiff
dir: fix directory-matching bug
authorDerrick Stolee <dstolee@microsoft.com>
Tue, 2 Nov 2021 14:40:06 +0000 (10:40 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 3 Nov 2021 17:10:36 +0000 (10:10 -0700)
This reverts the change from ed49584 (dir: fix pattern matching on dirs,
2021-09-24), which claimed to fix a directory-matching problem without a
test case. It turns out to _create_ a bug, but it is a bit subtle.

The bug would have been revealed by the first of two tests being added to
t0008-ignores.sh. The first uses a pattern "/git/" inside the a/.gitignores
file, which matches against 'a/git/foo' but not 'a/git-foo/bar'. This test
would fail before the revert.

The second test shows what happens if the test instead uses a pattern "git/"
and this test passes both before and after the revert.

The difference in these two cases are due to how
last_matching_pattern_from_list() checks patterns both if they have the
PATTERN_FLAG_MUSTBEDIR and PATTERN_FLAG_NODIR flags. In the case of "git/",
the PATTERN_FLAG_NODIR is also provided, making the change in behavior in
match_pathname() not affect the end result of
last_matching_pattern_from_list().

Reported-by: Glen Choo <chooglen@google.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c
t/t0008-ignores.sh

diff --git a/dir.c b/dir.c
index 174d336c30e0397423623c770f56a74066c5cce3..9ea6cfe61cbf347456fc6da8c9e155b0d42e36dd 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1294,7 +1294,7 @@ int match_pathname(const char *pathname, int pathlen,
                 * then our prefix match is all we need; we
                 * do not need to call fnmatch at all.
                 */
-               if (!patternlen && (!namelen || (flags & PATTERN_FLAG_MUSTBEDIR)))
+               if (!patternlen && !namelen)
                        return 1;
        }
 
index a594b4aa7d045cb0c03c0eea8591912ee052dca8..46a5038fed271da9013494c37e177444476dbb1c 100755 (executable)
@@ -802,6 +802,32 @@ test_expect_success 'existing directory and file' '
        grep top-level-dir actual
 '
 
+test_expect_success 'exact prefix matching (with root)' '
+       test_when_finished rm -r a &&
+       mkdir -p a/git a/git-foo &&
+       touch a/git/foo a/git-foo/bar &&
+       echo /git/ >a/.gitignore &&
+       git check-ignore a/git a/git/foo a/git-foo a/git-foo/bar >actual &&
+       cat >expect <<-\EOF &&
+       a/git
+       a/git/foo
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'exact prefix matching (without root)' '
+       test_when_finished rm -r a &&
+       mkdir -p a/git a/git-foo &&
+       touch a/git/foo a/git-foo/bar &&
+       echo git/ >a/.gitignore &&
+       git check-ignore a/git a/git/foo a/git-foo a/git-foo/bar >actual &&
+       cat >expect <<-\EOF &&
+       a/git
+       a/git/foo
+       EOF
+       test_cmp expect actual
+'
+
 ############################################################################
 #
 # test whitespace handling