3 test_description
='test config file include directives'
4 TEST_PASSES_SANITIZE_LEAK
=true
7 # Force setup_explicit_git_dir() to run until the end. This is needed
8 # by some tests to make sure real_path() is called on $GIT_DIR. The
9 # caller needs to make sure git commands are run from a subdirectory
10 # though or real_path() will not be called.
11 force_setup_explicit_git_dir
() {
13 GIT_WORK_TREE
="$(pwd)"
14 export GIT_DIR GIT_WORK_TREE
17 test_expect_success
'include file by absolute path' '
18 echo "[test]one = 1" >one &&
19 echo "[include]path = \"$(pwd)/one\"" >.gitconfig &&
21 git config test.one >actual &&
22 test_cmp expect actual
25 test_expect_success
'include file by relative path' '
26 echo "[test]one = 1" >one &&
27 echo "[include]path = one" >.gitconfig &&
29 git config test.one >actual &&
30 test_cmp expect actual
33 test_expect_success
'chained relative paths' '
35 echo "[test]three = 3" >subdir/three &&
36 echo "[include]path = three" >subdir/two &&
37 echo "[include]path = subdir/two" >.gitconfig &&
39 git config test.three >actual &&
40 test_cmp expect actual
43 test_expect_success
'include paths get tilde-expansion' '
44 echo "[test]one = 1" >one &&
45 echo "[include]path = ~/one" >.gitconfig &&
47 git config test.one >actual &&
48 test_cmp expect actual
51 test_expect_success
'include options can still be examined' '
52 echo "[test]one = 1" >one &&
53 echo "[include]path = one" >.gitconfig &&
55 git config include.path >actual &&
56 test_cmp expect actual
59 test_expect_success
'listing includes option and expansion' '
60 echo "[test]one = 1" >one &&
61 echo "[include]path = one" >.gitconfig &&
62 cat >expect <<-\EOF &&
66 git config --list >actual.full &&
67 grep -v -e ^core -e ^extensions actual.full >actual &&
68 test_cmp expect actual
71 test_expect_success
'single file lookup does not expand includes by default' '
72 echo "[test]one = 1" >one &&
73 echo "[include]path = one" >.gitconfig &&
74 test_must_fail git config -f .gitconfig test.one &&
75 test_must_fail git config --global test.one &&
77 git config --includes -f .gitconfig test.one >actual &&
78 test_cmp expect actual
81 test_expect_success
'single file list does not expand includes by default' '
82 echo "[test]one = 1" >one &&
83 echo "[include]path = one" >.gitconfig &&
84 echo "include.path=one" >expect &&
85 git config -f .gitconfig --list >actual &&
86 test_cmp expect actual
89 test_expect_success
'writing config file does not expand includes' '
90 echo "[test]one = 1" >one &&
91 echo "[include]path = one" >.gitconfig &&
92 git config test.two 2 &&
94 git config --no-includes test.two >actual &&
95 test_cmp expect actual &&
96 test_must_fail git config --no-includes test.one
99 test_expect_success
'config modification does not affect includes' '
100 echo "[test]one = 1" >one &&
101 echo "[include]path = one" >.gitconfig &&
102 git config test.one 2 &&
104 git config -f one test.one >actual &&
105 test_cmp expect actual &&
106 cat >expect <<-\EOF &&
110 git config --get-all test.one >actual &&
111 test_cmp expect actual
114 test_expect_success
'missing include files are ignored' '
115 cat >.gitconfig <<-\EOF &&
116 [include]path = non-existent
120 git config test.value >actual &&
121 test_cmp expect actual
124 test_expect_success
'absolute includes from command line work' '
125 echo "[test]one = 1" >one &&
127 git -c include.path="$(pwd)/one" config test.one >actual &&
128 test_cmp expect actual
131 test_expect_success
'relative includes from command line fail' '
132 echo "[test]one = 1" >one &&
133 test_must_fail git -c include.path=one config test.one
136 test_expect_success
'absolute includes from blobs work' '
137 echo "[test]one = 1" >one &&
138 echo "[include]path=$(pwd)/one" >blob &&
139 blob=$(git hash-object -w blob) &&
141 git config --blob=$blob test.one >actual &&
142 test_cmp expect actual
145 test_expect_success
'relative includes from blobs fail' '
146 echo "[test]one = 1" >one &&
147 echo "[include]path=one" >blob &&
148 blob=$(git hash-object -w blob) &&
149 test_must_fail git config --blob=$blob test.one
152 test_expect_success
'absolute includes from stdin work' '
153 echo "[test]one = 1" >one &&
155 echo "[include]path=\"$(pwd)/one\"" |
156 git config --file - test.one >actual &&
157 test_cmp expect actual
160 test_expect_success
'relative includes from stdin line fail' '
161 echo "[test]one = 1" >one &&
162 echo "[include]path=one" |
163 test_must_fail git config --file - test.one
166 test_expect_success
'conditional include, both unanchored' '
170 echo "[includeIf \"gitdir:foo/\"]path=bar" >>.git/config &&
171 echo "[test]one=1" >.git/bar &&
173 git config test.one >actual &&
174 test_cmp expect actual
178 test_expect_success
'conditional include, $HOME expansion' '
181 echo "[includeIf \"gitdir:~/foo/\"]path=bar2" >>.git/config &&
182 echo "[test]two=2" >.git/bar2 &&
184 git config test.two >actual &&
185 test_cmp expect actual
189 test_expect_success
'conditional include, full pattern' '
192 echo "[includeIf \"gitdir:**/foo/**\"]path=bar3" >>.git/config &&
193 echo "[test]three=3" >.git/bar3 &&
195 git config test.three >actual &&
196 test_cmp expect actual
200 test_expect_success
'conditional include, relative path' '
201 echo "[includeIf \"gitdir:./foo/.git\"]path=bar4" >>.gitconfig &&
202 echo "[test]four=4" >bar4 &&
206 git config test.four >actual &&
207 test_cmp expect actual
211 test_expect_success
'conditional include, both unanchored, icase' '
214 echo "[includeIf \"gitdir/i:FOO/\"]path=bar5" >>.git/config &&
215 echo "[test]five=5" >.git/bar5 &&
217 git config test.five >actual &&
218 test_cmp expect actual
222 test_expect_success
'conditional include, early config reading' '
225 echo "[includeIf \"gitdir:foo/\"]path=bar6" >>.git/config &&
226 echo "[test]six=6" >.git/bar6 &&
228 test-tool config read_early_config test.six >actual &&
229 test_cmp expect actual
233 test_expect_success
'conditional include with /**/' '
236 cat >>$REPO/.git/config <<-\EOF &&
237 [includeIf "gitdir:**/foo/**/bar/**"]
240 echo "[test]seven=7" >$REPO/.git/bar7 &&
242 git -C $REPO config test.seven >actual &&
243 test_cmp expect actual
246 test_expect_success SYMLINKS
'conditional include, set up symlinked $HOME' '
248 ln -s real-home home &&
250 HOME="$TRASH_DIRECTORY/home" &&
260 test_expect_success SYMLINKS
'conditional include, $HOME expansion with symlinks' '
262 HOME="$TRASH_DIRECTORY/home" &&
266 echo "[includeIf \"gitdir:~/foo/\"]path=bar2" >>.git/config &&
267 echo "[test]two=2" >.git/bar2 &&
269 force_setup_explicit_git_dir &&
270 git -C sub config test.two >actual &&
271 test_cmp expect actual
275 test_expect_success SYMLINKS
'conditional include, relative path with symlinks' '
276 echo "[includeIf \"gitdir:./foo/.git\"]path=bar4" >home/.gitconfig &&
277 echo "[test]four=4" >home/bar4 &&
279 HOME="$TRASH_DIRECTORY/home" &&
284 force_setup_explicit_git_dir &&
285 git -C sub config test.four >actual &&
286 test_cmp expect actual
290 test_expect_success SYMLINKS
'conditional include, gitdir matching symlink' '
294 echo "[includeIf \"gitdir:bar/\"]path=bar7" >>.git/config &&
295 echo "[test]seven=7" >.git/bar7 &&
297 git config test.seven >actual &&
298 test_cmp expect actual
302 test_expect_success SYMLINKS
'conditional include, gitdir matching symlink, icase' '
305 echo "[includeIf \"gitdir/i:BAR/\"]path=bar8" >>.git/config &&
306 echo "[test]eight=8" >.git/bar8 &&
308 git config test.eight >actual &&
309 test_cmp expect actual
313 test_expect_success
'conditional include, onbranch' '
314 echo "[includeIf \"onbranch:foo-branch\"]path=bar9" >>.git/config &&
315 echo "[test]nine=9" >.git/bar9 &&
316 git checkout -b main &&
317 test_must_fail git config test.nine &&
318 git checkout -b foo-branch &&
320 git config test.nine >actual &&
321 test_cmp expect actual
324 test_expect_success
'conditional include, onbranch, wildcard' '
325 echo "[includeIf \"onbranch:?oo-*/**\"]path=bar10" >>.git/config &&
326 echo "[test]ten=10" >.git/bar10 &&
327 git checkout -b not-foo-branch/a &&
328 test_must_fail git config test.ten &&
331 git checkout -b foo-branch/a/b/c &&
332 git config test.ten >actual &&
333 test_cmp expect actual &&
335 git checkout -b moo-bar/a &&
336 git config test.ten >actual &&
337 test_cmp expect actual
340 test_expect_success
'conditional include, onbranch, implicit /** for /' '
341 echo "[includeIf \"onbranch:foo-dir/\"]path=bar11" >>.git/config &&
342 echo "[test]eleven=11" >.git/bar11 &&
343 git checkout -b not-foo-dir/a &&
344 test_must_fail git config test.eleven &&
347 git checkout -b foo-dir/a/b/c &&
348 git config test.eleven >actual &&
349 test_cmp expect actual
352 test_expect_success
'include cycles are detected' '
353 git init --bare cycle &&
354 git -C cycle config include.path cycle &&
355 git config -f cycle/cycle include.path config &&
356 test_must_fail git -C cycle config --get-all test.value 2>stderr &&
357 grep "exceeded maximum include depth" stderr