3 test_description='test config file include directives'
6 # Force setup_explicit_git_dir() to run until the end. This is needed
7 # by some tests to make sure real_path() is called on $GIT_DIR. The
8 # caller needs to make sure git commands are run from a subdirectory
9 # though or real_path() will not be called.
10 force_setup_explicit_git_dir() {
12 GIT_WORK_TREE="$(pwd)"
13 export GIT_DIR GIT_WORK_TREE
16 test_expect_success 'include file by absolute path' '
17 echo "[test]one = 1" >one &&
18 echo "[include]path = \"$(pwd)/one\"" >.gitconfig &&
20 git config test.one >actual &&
21 test_cmp expect actual
24 test_expect_success 'include file by relative path' '
25 echo "[test]one = 1" >one &&
26 echo "[include]path = one" >.gitconfig &&
28 git config test.one >actual &&
29 test_cmp expect actual
32 test_expect_success 'chained relative paths' '
34 echo "[test]three = 3" >subdir/three &&
35 echo "[include]path = three" >subdir/two &&
36 echo "[include]path = subdir/two" >.gitconfig &&
38 git config test.three >actual &&
39 test_cmp expect actual
42 test_expect_success 'include paths get tilde-expansion' '
43 echo "[test]one = 1" >one &&
44 echo "[include]path = ~/one" >.gitconfig &&
46 git config test.one >actual &&
47 test_cmp expect actual
50 test_expect_success 'include options can still be examined' '
51 echo "[test]one = 1" >one &&
52 echo "[include]path = one" >.gitconfig &&
54 git config include.path >actual &&
55 test_cmp expect actual
58 test_expect_success 'listing includes option and expansion' '
59 echo "[test]one = 1" >one &&
60 echo "[include]path = one" >.gitconfig &&
61 cat >expect <<-\EOF &&
65 git config --list >actual.full &&
66 grep -v -e ^core -e ^extensions actual.full >actual &&
67 test_cmp expect actual
70 test_expect_success 'single file lookup does not expand includes by default' '
71 echo "[test]one = 1" >one &&
72 echo "[include]path = one" >.gitconfig &&
73 test_must_fail git config -f .gitconfig test.one &&
74 test_must_fail git config --global test.one &&
76 git config --includes -f .gitconfig test.one >actual &&
77 test_cmp expect actual
80 test_expect_success 'single file list does not expand includes by default' '
81 echo "[test]one = 1" >one &&
82 echo "[include]path = one" >.gitconfig &&
83 echo "include.path=one" >expect &&
84 git config -f .gitconfig --list >actual &&
85 test_cmp expect actual
88 test_expect_success 'writing config file does not expand includes' '
89 echo "[test]one = 1" >one &&
90 echo "[include]path = one" >.gitconfig &&
91 git config test.two 2 &&
93 git config --no-includes test.two >actual &&
94 test_cmp expect actual &&
95 test_must_fail git config --no-includes test.one
98 test_expect_success 'config modification does not affect includes' '
99 echo "[test]one = 1" >one &&
100 echo "[include]path = one" >.gitconfig &&
101 git config test.one 2 &&
103 git config -f one test.one >actual &&
104 test_cmp expect actual &&
105 cat >expect <<-\EOF &&
109 git config --get-all test.one >actual &&
110 test_cmp expect actual
113 test_expect_success 'missing include files are ignored' '
114 cat >.gitconfig <<-\EOF &&
115 [include]path = non-existent
119 git config test.value >actual &&
120 test_cmp expect actual
123 test_expect_success 'absolute includes from command line work' '
124 echo "[test]one = 1" >one &&
126 git -c include.path="$(pwd)/one" config test.one >actual &&
127 test_cmp expect actual
130 test_expect_success 'relative includes from command line fail' '
131 echo "[test]one = 1" >one &&
132 test_must_fail git -c include.path=one config test.one
135 test_expect_success 'absolute includes from blobs work' '
136 echo "[test]one = 1" >one &&
137 echo "[include]path=$(pwd)/one" >blob &&
138 blob=$(git hash-object -w blob) &&
140 git config --blob=$blob test.one >actual &&
141 test_cmp expect actual
144 test_expect_success 'relative includes from blobs fail' '
145 echo "[test]one = 1" >one &&
146 echo "[include]path=one" >blob &&
147 blob=$(git hash-object -w blob) &&
148 test_must_fail git config --blob=$blob test.one
151 test_expect_success 'absolute includes from stdin work' '
152 echo "[test]one = 1" >one &&
154 echo "[include]path=\"$(pwd)/one\"" |
155 git config --file - test.one >actual &&
156 test_cmp expect actual
159 test_expect_success 'relative includes from stdin line fail' '
160 echo "[test]one = 1" >one &&
161 echo "[include]path=one" |
162 test_must_fail git config --file - test.one
165 test_expect_success 'conditional include, both unanchored' '
169 echo "[includeIf \"gitdir:foo/\"]path=bar" >>.git/config &&
170 echo "[test]one=1" >.git/bar &&
172 git config test.one >actual &&
173 test_cmp expect actual
177 test_expect_success 'conditional include, $HOME expansion' '
180 echo "[includeIf \"gitdir:~/foo/\"]path=bar2" >>.git/config &&
181 echo "[test]two=2" >.git/bar2 &&
183 git config test.two >actual &&
184 test_cmp expect actual
188 test_expect_success 'conditional include, full pattern' '
191 echo "[includeIf \"gitdir:**/foo/**\"]path=bar3" >>.git/config &&
192 echo "[test]three=3" >.git/bar3 &&
194 git config test.three >actual &&
195 test_cmp expect actual
199 test_expect_success 'conditional include, relative path' '
200 echo "[includeIf \"gitdir:./foo/.git\"]path=bar4" >>.gitconfig &&
201 echo "[test]four=4" >bar4 &&
205 git config test.four >actual &&
206 test_cmp expect actual
210 test_expect_success 'conditional include, both unanchored, icase' '
213 echo "[includeIf \"gitdir/i:FOO/\"]path=bar5" >>.git/config &&
214 echo "[test]five=5" >.git/bar5 &&
216 git config test.five >actual &&
217 test_cmp expect actual
221 test_expect_success 'conditional include, early config reading' '
224 echo "[includeIf \"gitdir:foo/\"]path=bar6" >>.git/config &&
225 echo "[test]six=6" >.git/bar6 &&
227 test-tool config read_early_config test.six >actual &&
228 test_cmp expect actual
232 test_expect_success 'conditional include with /**/' '
235 cat >>$REPO/.git/config <<-\EOF &&
236 [includeIf "gitdir:**/foo/**/bar/**"]
239 echo "[test]seven=7" >$REPO/.git/bar7 &&
241 git -C $REPO config test.seven >actual &&
242 test_cmp expect actual
245 test_expect_success SYMLINKS 'conditional include, set up symlinked $HOME' '
247 ln -s real-home home &&
249 HOME="$TRASH_DIRECTORY/home" &&
259 test_expect_success SYMLINKS 'conditional include, $HOME expansion with symlinks' '
261 HOME="$TRASH_DIRECTORY/home" &&
265 echo "[includeIf \"gitdir:~/foo/\"]path=bar2" >>.git/config &&
266 echo "[test]two=2" >.git/bar2 &&
268 force_setup_explicit_git_dir &&
269 git -C sub config test.two >actual &&
270 test_cmp expect actual
274 test_expect_success SYMLINKS 'conditional include, relative path with symlinks' '
275 echo "[includeIf \"gitdir:./foo/.git\"]path=bar4" >home/.gitconfig &&
276 echo "[test]four=4" >home/bar4 &&
278 HOME="$TRASH_DIRECTORY/home" &&
283 force_setup_explicit_git_dir &&
284 git -C sub config test.four >actual &&
285 test_cmp expect actual
289 test_expect_success SYMLINKS 'conditional include, gitdir matching symlink' '
293 echo "[includeIf \"gitdir:bar/\"]path=bar7" >>.git/config &&
294 echo "[test]seven=7" >.git/bar7 &&
296 git config test.seven >actual &&
297 test_cmp expect actual
301 test_expect_success SYMLINKS 'conditional include, gitdir matching symlink, icase' '
304 echo "[includeIf \"gitdir/i:BAR/\"]path=bar8" >>.git/config &&
305 echo "[test]eight=8" >.git/bar8 &&
307 git config test.eight >actual &&
308 test_cmp expect actual
312 test_expect_success 'conditional include, onbranch' '
313 echo "[includeIf \"onbranch:foo-branch\"]path=bar9" >>.git/config &&
314 echo "[test]nine=9" >.git/bar9 &&
315 git checkout -b main &&
316 test_must_fail git config test.nine &&
317 git checkout -b foo-branch &&
319 git config test.nine >actual &&
320 test_cmp expect actual
323 test_expect_success 'conditional include, onbranch, wildcard' '
324 echo "[includeIf \"onbranch:?oo-*/**\"]path=bar10" >>.git/config &&
325 echo "[test]ten=10" >.git/bar10 &&
326 git checkout -b not-foo-branch/a &&
327 test_must_fail git config test.ten &&
330 git checkout -b foo-branch/a/b/c &&
331 git config test.ten >actual &&
332 test_cmp expect actual &&
334 git checkout -b moo-bar/a &&
335 git config test.ten >actual &&
336 test_cmp expect actual
339 test_expect_success 'conditional include, onbranch, implicit /** for /' '
340 echo "[includeIf \"onbranch:foo-dir/\"]path=bar11" >>.git/config &&
341 echo "[test]eleven=11" >.git/bar11 &&
342 git checkout -b not-foo-dir/a &&
343 test_must_fail git config test.eleven &&
346 git checkout -b foo-dir/a/b/c &&
347 git config test.eleven >actual &&
348 test_cmp expect actual
351 test_expect_success 'include cycles are detected' '
352 git init --bare cycle &&
353 git -C cycle config include.path cycle &&
354 git config -f cycle/cycle include.path config &&
355 test_must_fail git -C cycle config --get-all test.value 2>stderr &&
356 grep "exceeded maximum include depth" stderr