diff --git a/dir.c b/dir.c index 1045cc9c6f..b5082ca05f 100644 --- 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 && diff --git a/t/t3000-ls-files-others.sh b/t/t3000-ls-files-others.sh index 1b9327b780..740ce56eab 100755 --- a/t/t3000-ls-files-others.sh +++ b/t/t3000-ls-files-others.sh @@ -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