]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0003-attributes.sh
Merge branch 'ms/rebase-insnformat-doc-fix' into maint-2.43
[thirdparty/git.git] / t / t0003-attributes.sh
1 #!/bin/sh
2
3 test_description=gitattributes
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 TEST_CREATE_REPO_NO_TEMPLATE=1
7 . ./test-lib.sh
8
9 attr_check_basic () {
10 path="$1" expect="$2" git_opts="$3" &&
11
12 git $git_opts check-attr test -- "$path" >actual 2>err &&
13 echo "$path: test: $expect" >expect &&
14 test_cmp expect actual
15 }
16
17 attr_check () {
18 attr_check_basic "$@" &&
19 test_must_be_empty err
20 }
21
22 attr_check_quote () {
23 path="$1" quoted_path="$2" expect="$3" &&
24
25 git check-attr test -- "$path" >actual &&
26 echo "\"$quoted_path\": test: $expect" >expect &&
27 test_cmp expect actual
28 }
29
30 attr_check_source () {
31 path="$1" expect="$2" source="$3" git_opts="$4" &&
32
33 echo "$path: test: $expect" >expect &&
34
35 git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
36 test_cmp expect actual &&
37 test_must_be_empty err &&
38
39 git $git_opts --attr-source="$source" check-attr test -- "$path" >actual 2>err &&
40 test_cmp expect actual &&
41 test_must_be_empty err
42
43 git $git_opts -c "attr.tree=$source" check-attr test -- "$path" >actual 2>err &&
44 test_cmp expect actual &&
45 test_must_be_empty err
46
47 GIT_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
48 test_cmp expect actual &&
49 test_must_be_empty err
50 }
51
52 test_expect_success 'open-quoted pathname' '
53 echo "\"a test=a" >.gitattributes &&
54 attr_check a unspecified
55 '
56
57 test_expect_success 'setup' '
58 mkdir -p a/b/d a/c b &&
59 (
60 echo "[attr]notest !test" &&
61 echo "\" d \" test=d" &&
62 echo " e test=e" &&
63 echo " e\" test=e" &&
64 echo "f test=f" &&
65 echo "a/i test=a/i" &&
66 echo "onoff test -test" &&
67 echo "offon -test test" &&
68 echo "no notest" &&
69 echo "A/e/F test=A/e/F"
70 ) >.gitattributes &&
71 (
72 echo "g test=a/g" &&
73 echo "b/g test=a/b/g"
74 ) >a/.gitattributes &&
75 (
76 echo "h test=a/b/h" &&
77 echo "d/* test=a/b/d/*" &&
78 echo "d/yes notest"
79 ) >a/b/.gitattributes &&
80 (
81 echo "global test=global"
82 ) >"$HOME"/global-gitattributes &&
83 cat <<-EOF >expect-all
84 f: test: f
85 a/f: test: f
86 a/c/f: test: f
87 a/g: test: a/g
88 a/b/g: test: a/b/g
89 b/g: test: unspecified
90 a/b/h: test: a/b/h
91 a/b/d/g: test: a/b/d/*
92 onoff: test: unset
93 offon: test: set
94 no: notest: set
95 no: test: unspecified
96 a/b/d/no: notest: set
97 a/b/d/no: test: a/b/d/*
98 a/b/d/yes: notest: set
99 a/b/d/yes: test: unspecified
100 EOF
101 '
102
103 test_expect_success 'setup branches' '
104 mkdir -p foo/bar &&
105 test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
106 "f test=f\na/i test=n\n" tag-1 &&
107 test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
108 "g test=g\na/i test=m\n" tag-2 &&
109 rm foo/bar/.gitattributes
110 '
111
112 test_expect_success 'command line checks' '
113 test_must_fail git check-attr &&
114 test_must_fail git check-attr -- &&
115 test_must_fail git check-attr test &&
116 test_must_fail git check-attr test -- &&
117 test_must_fail git check-attr -- f &&
118 test_must_fail git check-attr --source &&
119 test_must_fail git check-attr --source not-a-valid-ref &&
120 echo "f" | test_must_fail git check-attr --stdin &&
121 echo "f" | test_must_fail git check-attr --stdin -- f &&
122 echo "f" | test_must_fail git check-attr --stdin test -- f &&
123 test_must_fail git check-attr "" -- f
124 '
125
126 test_expect_success 'attribute test' '
127
128 attr_check " d " d &&
129 attr_check e e &&
130 attr_check_quote e\" e\\\" e &&
131
132 attr_check f f &&
133 attr_check a/f f &&
134 attr_check a/c/f f &&
135 attr_check a/g a/g &&
136 attr_check a/b/g a/b/g &&
137 attr_check b/g unspecified &&
138 attr_check a/b/h a/b/h &&
139 attr_check a/b/d/g "a/b/d/*" &&
140 attr_check onoff unset &&
141 attr_check offon set &&
142 attr_check no unspecified &&
143 attr_check a/b/d/no "a/b/d/*" &&
144 attr_check a/b/d/yes unspecified
145 '
146
147 test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
148
149 attr_check F unspecified "-c core.ignorecase=0" &&
150 attr_check a/F unspecified "-c core.ignorecase=0" &&
151 attr_check a/c/F unspecified "-c core.ignorecase=0" &&
152 attr_check a/G unspecified "-c core.ignorecase=0" &&
153 attr_check a/B/g a/g "-c core.ignorecase=0" &&
154 attr_check a/b/G unspecified "-c core.ignorecase=0" &&
155 attr_check a/b/H unspecified "-c core.ignorecase=0" &&
156 attr_check a/b/D/g a/g "-c core.ignorecase=0" &&
157 attr_check oNoFf unspecified "-c core.ignorecase=0" &&
158 attr_check oFfOn unspecified "-c core.ignorecase=0" &&
159 attr_check NO unspecified "-c core.ignorecase=0" &&
160 attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
161 attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
162 attr_check a/E/f f "-c core.ignorecase=0"
163
164 '
165
166 test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
167
168 attr_check F f "-c core.ignorecase=1" &&
169 attr_check a/F f "-c core.ignorecase=1" &&
170 attr_check a/c/F f "-c core.ignorecase=1" &&
171 attr_check a/G a/g "-c core.ignorecase=1" &&
172 attr_check a/B/g a/b/g "-c core.ignorecase=1" &&
173 attr_check a/b/G a/b/g "-c core.ignorecase=1" &&
174 attr_check a/b/H a/b/h "-c core.ignorecase=1" &&
175 attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=1" &&
176 attr_check oNoFf unset "-c core.ignorecase=1" &&
177 attr_check oFfOn set "-c core.ignorecase=1" &&
178 attr_check NO unspecified "-c core.ignorecase=1" &&
179 attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=1" &&
180 attr_check a/b/d/YES unspecified "-c core.ignorecase=1" &&
181 attr_check a/E/f "A/e/F" "-c core.ignorecase=1"
182
183 '
184
185 test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
186 attr_check a/B/D/g a/g "-c core.ignorecase=0" &&
187 attr_check A/B/D/NO unspecified "-c core.ignorecase=0" &&
188 attr_check A/b/h a/b/h "-c core.ignorecase=1" &&
189 attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=1" &&
190 attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=1"
191 '
192
193 test_expect_success 'unnormalized paths' '
194 attr_check ./f f &&
195 attr_check ./a/g a/g &&
196 attr_check a/./g a/g &&
197 attr_check a/c/../b/g a/b/g
198 '
199
200 test_expect_success 'relative paths' '
201 (cd a && attr_check ../f f) &&
202 (cd a && attr_check f f) &&
203 (cd a && attr_check i a/i) &&
204 (cd a && attr_check g a/g) &&
205 (cd a && attr_check b/g a/b/g) &&
206 (cd b && attr_check ../a/f f) &&
207 (cd b && attr_check ../a/g a/g) &&
208 (cd b && attr_check ../a/b/g a/b/g)
209 '
210
211 test_expect_success 'prefixes are not confused with leading directories' '
212 attr_check a_plus/g unspecified &&
213 cat >expect <<-\EOF &&
214 a/g: test: a/g
215 a_plus/g: test: unspecified
216 EOF
217 git check-attr test a/g a_plus/g >actual &&
218 test_cmp expect actual
219 '
220
221 test_expect_success 'core.attributesfile' '
222 attr_check global unspecified &&
223 git config core.attributesfile "$HOME/global-gitattributes" &&
224 attr_check global global &&
225 git config core.attributesfile "~/global-gitattributes" &&
226 attr_check global global &&
227 echo "global test=precedence" >>.gitattributes &&
228 attr_check global precedence
229 '
230
231 test_expect_success 'attribute test: read paths from stdin' '
232 grep -v notest <expect-all >expect &&
233 sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
234 test_cmp expect actual
235 '
236
237 test_expect_success 'setup --all option' '
238 grep -v unspecified <expect-all | sort >specified-all &&
239 sed -e "s/:.*//" <expect-all | uniq >stdin-all
240 '
241
242 test_expect_success 'attribute test: --all option' '
243 git check-attr --stdin --all <stdin-all >tmp &&
244 sort tmp >actual &&
245 test_cmp specified-all actual
246 '
247
248 test_expect_success 'attribute test: --cached option' '
249 git check-attr --cached --stdin --all <stdin-all >tmp &&
250 sort tmp >actual &&
251 test_must_be_empty actual &&
252 git add .gitattributes a/.gitattributes a/b/.gitattributes &&
253 git check-attr --cached --stdin --all <stdin-all >tmp &&
254 sort tmp >actual &&
255 test_cmp specified-all actual
256 '
257
258 test_expect_success 'root subdir attribute test' '
259 attr_check a/i a/i &&
260 attr_check subdir/a/i unspecified
261 '
262
263 test_expect_success 'negative patterns' '
264 echo "!f test=bar" >.gitattributes &&
265 git check-attr test -- '"'"'!f'"'"' 2>errors &&
266 test_grep "Negative patterns are ignored" errors
267 '
268
269 test_expect_success 'patterns starting with exclamation' '
270 echo "\!f test=foo" >.gitattributes &&
271 attr_check "!f" foo
272 '
273
274 test_expect_success '"**" test' '
275 echo "**/f foo=bar" >.gitattributes &&
276 cat <<\EOF >expect &&
277 f: foo: bar
278 a/f: foo: bar
279 a/b/f: foo: bar
280 a/b/c/f: foo: bar
281 EOF
282 git check-attr foo -- "f" >actual 2>err &&
283 git check-attr foo -- "a/f" >>actual 2>>err &&
284 git check-attr foo -- "a/b/f" >>actual 2>>err &&
285 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
286 test_cmp expect actual &&
287 test_must_be_empty err
288 '
289
290 test_expect_success '"**" with no slashes test' '
291 echo "a**f foo=bar" >.gitattributes &&
292 git check-attr foo -- "f" >actual &&
293 cat <<\EOF >expect &&
294 f: foo: unspecified
295 af: foo: bar
296 axf: foo: bar
297 a/f: foo: unspecified
298 a/b/f: foo: unspecified
299 a/b/c/f: foo: unspecified
300 EOF
301 git check-attr foo -- "f" >actual 2>err &&
302 git check-attr foo -- "af" >>actual 2>err &&
303 git check-attr foo -- "axf" >>actual 2>err &&
304 git check-attr foo -- "a/f" >>actual 2>>err &&
305 git check-attr foo -- "a/b/f" >>actual 2>>err &&
306 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
307 test_cmp expect actual &&
308 test_must_be_empty err
309 '
310
311 test_expect_success 'using --git-dir and --work-tree' '
312 mkdir unreal real &&
313 git init real &&
314 echo "file test=in-real" >real/.gitattributes &&
315 (
316 cd unreal &&
317 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
318 )
319 '
320
321 test_expect_success 'using --source' '
322 attr_check_source foo/bar/f f tag-1 &&
323 attr_check_source foo/bar/a/i n tag-1 &&
324 attr_check_source foo/bar/f unspecified tag-2 &&
325 attr_check_source foo/bar/a/i m tag-2 &&
326 attr_check_source foo/bar/g g tag-2 &&
327 attr_check_source foo/bar/g unspecified tag-1
328 '
329
330 test_expect_success 'setup bare' '
331 git clone --template= --bare . bare.git
332 '
333
334 test_expect_success 'bare repository: check that .gitattribute is ignored' '
335 (
336 cd bare.git &&
337 (
338 echo "f test=f" &&
339 echo "a/i test=a/i"
340 ) >.gitattributes &&
341 attr_check f unspecified &&
342 attr_check a/f unspecified &&
343 attr_check a/c/f unspecified &&
344 attr_check a/i unspecified &&
345 attr_check subdir/a/i unspecified
346 )
347 '
348
349 bad_attr_source_err="fatal: bad --attr-source or GIT_ATTR_SOURCE"
350
351 test_expect_success '--attr-source is bad' '
352 test_when_finished rm -rf empty &&
353 git init empty &&
354 (
355 cd empty &&
356 echo "$bad_attr_source_err" >expect_err &&
357 test_must_fail git --attr-source=HEAD check-attr test -- f/path 2>err &&
358 test_cmp expect_err err
359 )
360 '
361
362 test_expect_success 'attr.tree when HEAD is unborn' '
363 test_when_finished rm -rf empty &&
364 git init empty &&
365 (
366 cd empty &&
367 echo "f/path: test: unspecified" >expect &&
368 git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
369 test_must_be_empty err &&
370 test_cmp expect actual
371 )
372 '
373
374 test_expect_success 'bad attr source defaults to reading .gitattributes file' '
375 test_when_finished rm -rf empty &&
376 git init empty &&
377 (
378 cd empty &&
379 echo "f/path test=val" >.gitattributes &&
380 echo "f/path: test: val" >expect &&
381 git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
382 test_must_be_empty err &&
383 test_cmp expect actual
384 )
385 '
386
387 test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
388 test_when_finished rm -rf test bare_with_gitattribute &&
389 git init test &&
390 test_commit -C test gitattributes .gitattributes "f/path test=val" &&
391 git clone --bare test bare_with_gitattribute &&
392 echo "f/path: test: val" >expect &&
393 git -C bare_with_gitattribute check-attr test -- f/path >actual &&
394 test_cmp expect actual
395 '
396
397 test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tree' '
398 test_when_finished rm -rf empty &&
399 git init empty &&
400 (
401 cd empty &&
402 git checkout -b attr-source &&
403 test_commit "val1" .gitattributes "f/path test=val1" &&
404 git checkout -b attr-tree &&
405 test_commit "val2" .gitattributes "f/path test=val2" &&
406 git checkout attr-source &&
407 echo "f/path: test: val1" >expect &&
408 GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree --attr-source=attr-source \
409 check-attr test -- f/path >actual &&
410 test_cmp expect actual &&
411 GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree \
412 check-attr test -- f/path >actual &&
413 test_cmp expect actual
414 )
415 '
416
417 test_expect_success 'bare repository: with --source' '
418 (
419 cd bare.git &&
420 attr_check_source foo/bar/f f tag-1 &&
421 attr_check_source foo/bar/a/i n tag-1 &&
422 attr_check_source foo/bar/f unspecified tag-2 &&
423 attr_check_source foo/bar/a/i m tag-2 &&
424 attr_check_source foo/bar/g g tag-2 &&
425 attr_check_source foo/bar/g unspecified tag-1
426 )
427 '
428
429 test_expect_success 'bare repository: check that --cached honors index' '
430 (
431 cd bare.git &&
432 GIT_INDEX_FILE=../.git/index \
433 git check-attr --cached --stdin --all <../stdin-all |
434 sort >actual &&
435 test_cmp ../specified-all actual
436 )
437 '
438
439 test_expect_success 'bare repository: test info/attributes' '
440 (
441 cd bare.git &&
442 mkdir info &&
443 (
444 echo "f test=f" &&
445 echo "a/i test=a/i"
446 ) >info/attributes &&
447 attr_check f f &&
448 attr_check a/f f &&
449 attr_check a/c/f f &&
450 attr_check a/i a/i &&
451 attr_check subdir/a/i unspecified
452 )
453 '
454
455 test_expect_success 'binary macro expanded by -a' '
456 echo "file binary" >.gitattributes &&
457 cat >expect <<-\EOF &&
458 file: binary: set
459 file: diff: unset
460 file: merge: unset
461 file: text: unset
462 EOF
463 git check-attr -a file >actual &&
464 test_cmp expect actual
465 '
466
467 test_expect_success 'query binary macro directly' '
468 echo "file binary" >.gitattributes &&
469 echo file: binary: set >expect &&
470 git check-attr binary file >actual &&
471 test_cmp expect actual
472 '
473
474 test_expect_success SYMLINKS 'set up symlink tests' '
475 echo "* test" >attr &&
476 rm -f .gitattributes
477 '
478
479 test_expect_success SYMLINKS 'symlinks respected in core.attributesFile' '
480 test_when_finished "rm symlink" &&
481 ln -s attr symlink &&
482 test_config core.attributesFile "$(pwd)/symlink" &&
483 attr_check file set
484 '
485
486 test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
487 test_when_finished "rm .git/info/attributes" &&
488 mkdir .git/info &&
489 ln -s ../../attr .git/info/attributes &&
490 attr_check file set
491 '
492
493 test_expect_success SYMLINKS 'symlinks not respected in-tree' '
494 test_when_finished "rm -rf .gitattributes subdir" &&
495 ln -s attr .gitattributes &&
496 mkdir subdir &&
497 ln -s ../attr subdir/.gitattributes &&
498 attr_check_basic subdir/file unspecified &&
499 test_grep "unable to access.*gitattributes" err
500 '
501
502 test_expect_success 'large attributes line ignored in tree' '
503 test_when_finished "rm .gitattributes" &&
504 printf "path %02043d" 1 >.gitattributes &&
505 git check-attr --all path >actual 2>err &&
506 echo "warning: ignoring overly long attributes line 1" >expect &&
507 test_cmp expect err &&
508 test_must_be_empty actual
509 '
510
511 test_expect_success 'large attributes line ignores trailing content in tree' '
512 test_when_finished "rm .gitattributes" &&
513 # older versions of Git broke lines at 2048 bytes; the 2045 bytes
514 # of 0-padding here is accounting for the three bytes of "a 1", which
515 # would knock "trailing" to the "next" line, where it would be
516 # erroneously parsed.
517 printf "a %02045dtrailing attribute\n" 1 >.gitattributes &&
518 git check-attr --all trailing >actual 2>err &&
519 echo "warning: ignoring overly long attributes line 1" >expect &&
520 test_cmp expect err &&
521 test_must_be_empty actual
522 '
523
524 test_expect_success EXPENSIVE 'large attributes file ignored in tree' '
525 test_when_finished "rm .gitattributes" &&
526 dd if=/dev/zero of=.gitattributes bs=1048576 count=101 2>/dev/null &&
527 git check-attr --all path >/dev/null 2>err &&
528 echo "warning: ignoring overly large gitattributes file ${SQ}.gitattributes${SQ}" >expect &&
529 test_cmp expect err
530 '
531
532 test_expect_success 'large attributes line ignored in index' '
533 test_when_finished "git update-index --remove .gitattributes" &&
534 blob=$(printf "path %02043d" 1 | git hash-object -w --stdin) &&
535 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
536 git check-attr --cached --all path >actual 2>err &&
537 echo "warning: ignoring overly long attributes line 1" >expect &&
538 test_cmp expect err &&
539 test_must_be_empty actual
540 '
541
542 test_expect_success 'large attributes line ignores trailing content in index' '
543 test_when_finished "git update-index --remove .gitattributes" &&
544 blob=$(printf "a %02045dtrailing attribute\n" 1 | git hash-object -w --stdin) &&
545 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
546 git check-attr --cached --all trailing >actual 2>err &&
547 echo "warning: ignoring overly long attributes line 1" >expect &&
548 test_cmp expect err &&
549 test_must_be_empty actual
550 '
551
552 test_expect_success EXPENSIVE 'large attributes file ignored in index' '
553 test_when_finished "git update-index --remove .gitattributes" &&
554 blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) &&
555 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
556 git check-attr --cached --all path >/dev/null 2>err &&
557 echo "warning: ignoring overly large gitattributes blob ${SQ}.gitattributes${SQ}" >expect &&
558 test_cmp expect err
559 '
560
561 test_done