]> git.ipfire.org Git - thirdparty/git.git/commitdiff
dir: avoid prematurely marking nonbare repositories as matches
authorElijah Newren <newren@gmail.com>
Wed, 12 Aug 2020 07:12:36 +0000 (07:12 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 12 Aug 2020 19:26:47 +0000 (12:26 -0700)
Nonbare repositories are special directories.  Unlike normal directories
that we might recurse into to list the files they contain, nonbare
repositories must themselves match and then we always report only on the
nonbare repository directory itself and not on any of its contents.

Separately, when traversing directories to try to find untracked or
excluded files, we often think in terms of paths either matching the
specified pathspec, or not matching them.  However, there is a special
value that do_match_pathspec() uses named
MATCHED_RECURSIVELY_LEADING_PATHSPEC which means "this directory does
not match any pathspec BUT it is possible a file or directory underneath
it does."  That special value prevents us from prematurely thinking that
some directory and everything under it is irrelevant, but also allows us
to differentiate from "this is a match".

The combination of these two special cases was previously uncovered.
Add a test to the testsuite to cover it, and make sure that we return a
nonbare repository as a non-match if the best match it got was
MATCHED_RECURSIVELY_LEADING_PATHSPEC.

Reported-by: christian w <usebees@gmail.com>
Simplified-testcase-and-bisection-by: Kyle Meyer <kyle@kyleam.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c
t/t3000-ls-files-others.sh

diff --git a/dir.c b/dir.c
index 1045cc9c6f58702aba6e9036a8bce91a431c108a..b5082ca05f4d20be46d635405b48805a5f82f7de 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1792,9 +1792,12 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
                nested_repo = is_nonbare_repository_dir(&sb);
                strbuf_release(&sb);
        }
-       if (nested_repo)
-               return ((dir->flags & DIR_SKIP_NESTED_GIT) ? path_none :
-                       (excluded ? path_excluded : path_untracked));
+       if (nested_repo) {
+               if ((dir->flags & DIR_SKIP_NESTED_GIT) ||
+                   (matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC))
+                       return path_none;
+               return excluded ? path_excluded : path_untracked;
+       }
 
        if (!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)) {
                if (excluded &&
index 1b9327b780d8e48848a1add1f35ca3ee5a3ed051..740ce56eab5c78b18a85834e9dc9c091d195c2d1 100755 (executable)
@@ -212,4 +212,20 @@ test_expect_success 'ls-files -o --directory to get immediate paths under one di
        )
 '
 
+test_expect_success 'ls-files -o avoids listing untracked non-matching gitdir' '
+       test_when_finished "rm -rf nested/untracked/deep/empty" &&
+       (
+               cd nested &&
+
+               git init untracked/deep/empty &&
+               git ls-files --others "untracked/*.c" >actual &&
+
+               cat <<-EOF >expect &&
+               untracked/deep/foo.c
+               EOF
+
+               test_cmp expect actual
+       )
+'
+
 test_done