ls-files tests: filter `.gitconfig` from `--others` output

The global `safe.bareRepository=all` setting in test-lib.sh is written to `$HOME/.gitconfig`, which unfortunately lives inside the test repository's working tree. The `.git/info/exclude` entry added alongside it handles most commands, but `git ls-files --others` without `--exclude-standard` does not consult `info/exclude` at all, so the file appears in the output. Ideally, each test that accesses a bare repository would simply specify `--git-dir` or `GIT_DIR` explicitly, which would require no global config and produce no side effects in the working tree. As that approach was not taken, filter `.gitconfig` from the output before comparing against expected results. In t7104, the test already uses `--exclude-standard`, so it suffices to switch from the bare `git ls-files -o` to `git ls-files -o --exclude-standard` which respects the `info/exclude` entry; the other tests deliberately omit `--exclude-standard` because their purpose is to verify unfiltered `--others` output. Assisted-by: Claude Opus 4.6 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Apr 26, 2026 at 14:38 UTC 83228f16611eae979421915836c5416eb4d02cf3
7 files changed +21 -2
t/t3000-ls-files-others.sh
+4
@@ -53,16 +53,19 @@ test_expect_success 'setup: expected output' '
53
54 test_expect_success 'ls-files --others' '
55 git ls-files --others >output &&
56 + test_filter_gitconfig output &&
57 test_cmp expected1 output
58 '
59
60 test_expect_success 'ls-files --others --directory' '
61 git ls-files --others --directory >output &&
62 + test_filter_gitconfig output &&
63 test_cmp expected2 output
64 '
65
66 test_expect_success '--no-empty-directory hides empty directory' '
67 git ls-files --others --directory --no-empty-directory >output &&
68 + test_filter_gitconfig output &&
69 test_cmp expected3 output
70 '
71
@@ -70,6 +73,7 @@ test_expect_success 'ls-files --others handles non-submodule .git' '
73 mkdir not-a-submodule &&
74 echo foo >not-a-submodule/.git &&
75 git ls-files -o >output &&
76 + test_filter_gitconfig output &&
77 test_cmp expected1 output
78 '
79
t/t3001-ls-files-others-exclude.sh
+3
@@ -72,6 +72,7 @@ test_expect_success 'git ls-files --others with various exclude options.' '
72 --exclude-per-directory=.gitignore \
73 --exclude-from=.git/ignore \
74 >output &&
75 + test_filter_gitconfig output &&
76 test_cmp expect output
77 '
78
@@ -84,6 +85,7 @@ test_expect_success 'git ls-files --others with \r\n line endings.' '
85 --exclude-per-directory=.gitignore \
86 --exclude-from=.git/ignore \
87 >output &&
88 + test_filter_gitconfig output &&
89 test_cmp expect output
90 '
91
@@ -99,6 +101,7 @@ test_expect_success 'git ls-files --others with various exclude options.' '
101 --exclude-per-directory=.gitignore \
102 --exclude-from=.git/ignore \
103 >output &&
104 + test_filter_gitconfig output &&
105 test_cmp expect output
106 '
107
t/t3002-ls-files-dashpath.sh
+2
@@ -24,6 +24,7 @@ test_expect_success 'setup' '
24 test_expect_success 'git ls-files without path restriction.' '
25 test_when_finished "rm -f expect" &&
26 git ls-files --others >output &&
27 + test_filter_gitconfig output &&
28 cat >expect <<-\EOF &&
29 --
30 -foo
@@ -63,6 +64,7 @@ test_expect_success 'git ls-files with path restriction with -- --.' '
64 test_expect_success 'git ls-files with no path restriction.' '
65 test_when_finished "rm -f expect" &&
66 git ls-files --others -- >output &&
67 + test_filter_gitconfig output &&
68 cat >expect <<-\EOF &&
69 --
70 -foo
t/t3009-ls-files-others-nonsubmodule.sh
+1
@@ -36,6 +36,7 @@ test_expect_success 'setup: directories' '
36
37 test_expect_success 'ls-files --others handles untracked git repositories' '
38 git ls-files -o >output &&
39 + test_filter_gitconfig output &&
40 cat >expect <<-EOF &&
41 nonrepo-untracked-file/untracked
42 output
t/t3011-common-prefixes-and-directory-traversal.sh
+2 -1
@@ -26,7 +26,7 @@ test_expect_success 'setup' '
26 '
27
28 test_expect_success 'git ls-files -o shows the right entries' '
29 - cat <<-EOF >expect &&
29 + cat >expect <<-EOF &&
30 .gitignore
31 actual
32 an_ignored_dir/ignored
@@ -39,6 +39,7 @@ test_expect_success 'git ls-files -o shows the right entries' '
39 untracked_repo/
40 EOF
41 git ls-files -o >actual &&
42 + test_filter_gitconfig actual &&
43 test_cmp expect actual
44 '
45
t/t7104-reset-hard.sh
+1 -1
@@ -21,7 +21,7 @@ test_expect_success setup '
21 rm -f hello &&
22 mkdir -p hello &&
23 >hello/world &&
24 - test "$(git ls-files -o)" = hello/world
24 + test "$(git ls-files -o --exclude-standard)" = hello/world
25
26 '
27
t/test-lib-functions.sh
+8
@@ -2069,3 +2069,11 @@ test_trailing_hash () {
2069 test_redact_non_printables () {
2070 tr -d "\n\r" | tr "[\001-\040][\177-\377]" "."
2071 }
2072 +
2073 +# Remove .gitconfig entries from a file in place. test-lib.sh may
2074 +# create $HOME/.gitconfig (e.g. to set safe.bareRepository) which
2075 +# can appear in ls-files or status output.
2076 +test_filter_gitconfig () {
2077 + sed "/\\.gitconfig/d" "$1" >"$1.filtered" &&
2078 + mv "$1.filtered" "$1"
2079 +}