3 test_description
=gitattributes
5 TEST_PASSES_SANITIZE_LEAK
=true
6 TEST_CREATE_REPO_NO_TEMPLATE
=1
10 path
="$1" expect
="$2" git_opts
="$3" &&
12 git
$git_opts check-attr
test -- "$path" >actual
2>err
&&
13 echo "$path: test: $expect" >expect
&&
14 test_cmp expect actual
18 attr_check_basic
"$@" &&
19 test_must_be_empty err
23 path
="$1" quoted_path
="$2" expect
="$3" &&
25 git check-attr
test -- "$path" >actual
&&
26 echo "\"$quoted_path\": test: $expect" >expect
&&
27 test_cmp expect actual
30 attr_check_source
() {
31 path
="$1" expect
="$2" source="$3" git_opts
="$4" &&
33 echo "$path: test: $expect" >expect
&&
35 git
$git_opts check-attr
--source $source test -- "$path" >actual
2>err
&&
36 test_cmp expect actual
&&
37 test_must_be_empty err
&&
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
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
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
52 test_expect_success
'open-quoted pathname' '
53 echo "\"a test=a" >.gitattributes &&
54 attr_check a unspecified
57 test_expect_success
'setup' '
58 mkdir -p a/b/d a/c b &&
60 echo "[attr]notest !test" &&
61 echo "\" d \" test=d" &&
65 echo "a/i test=a/i" &&
66 echo "onoff test -test" &&
67 echo "offon -test test" &&
69 echo "A/e/F test=A/e/F"
74 ) >a/.gitattributes &&
76 echo "h test=a/b/h" &&
77 echo "d/* test=a/b/d/*" &&
79 ) >a/b/.gitattributes &&
81 echo "global test=global"
82 ) >"$HOME"/global-gitattributes &&
83 cat <<-EOF >expect-all
89 b/g: test: unspecified
91 a/b/d/g: test: a/b/d/*
97 a/b/d/no: test: a/b/d/*
98 a/b/d/yes: notest: set
99 a/b/d/yes: test: unspecified
103 test_expect_success
'setup branches' '
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
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
126 test_expect_success
'attribute test' '
128 attr_check " d " d &&
130 attr_check_quote e\" e\\\" e &&
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
147 test_expect_success
'attribute matching is case sensitive when core.ignorecase=0' '
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"
166 test_expect_success
'attribute matching is case insensitive when core.ignorecase=1' '
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"
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"
193 test_expect_success
'unnormalized paths' '
195 attr_check ./a/g a/g &&
196 attr_check a/./g a/g &&
197 attr_check a/c/../b/g a/b/g
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)
211 test_expect_success
'prefixes are not confused with leading directories' '
212 attr_check a_plus/g unspecified &&
213 cat >expect <<-\EOF &&
215 a_plus/g: test: unspecified
217 git check-attr test a/g a_plus/g >actual &&
218 test_cmp expect actual
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
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
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
242 test_expect_success
'attribute test: --all option' '
243 git check-attr --stdin --all <stdin-all >tmp &&
245 test_cmp specified-all actual
248 test_expect_success
'attribute test: --cached option' '
249 git check-attr --cached --stdin --all <stdin-all >tmp &&
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 &&
255 test_cmp specified-all actual
258 test_expect_success
'root subdir attribute test' '
259 attr_check a/i a/i &&
260 attr_check subdir/a/i unspecified
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
269 test_expect_success
'patterns starting with exclamation' '
270 echo "\!f test=foo" >.gitattributes &&
274 test_expect_success
'"**" test' '
275 echo "**/f foo=bar" >.gitattributes &&
276 cat <<\EOF >expect &&
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
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 &&
297 a/f: foo: unspecified
298 a/b/f: foo: unspecified
299 a/b/c/f: foo: unspecified
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
311 test_expect_success
'using --git-dir and --work-tree' '
314 echo "file test=in-real" >real/.gitattributes &&
317 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
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
330 test_expect_success
'setup bare' '
331 git clone --template= --bare . bare.git
334 test_expect_success
'bare repository: check that .gitattribute is ignored' '
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
349 bad_attr_source_err
="fatal: bad --attr-source or GIT_ATTR_SOURCE"
351 test_expect_success
'--attr-source is bad' '
352 test_when_finished rm -rf 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
362 test_expect_success
'attr.tree when HEAD is unborn' '
363 test_when_finished rm -rf 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
374 test_expect_success
'bad attr source defaults to reading .gitattributes file' '
375 test_when_finished rm -rf 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
387 test_expect_success
'bare repo defaults to reading .gitattributes from HEAD' '
388 test_when_finished rm -rf test bare_with_gitattribute &&
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
397 test_expect_success
'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tree' '
398 test_when_finished rm -rf 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
417 test_expect_success
'bare repository: with --source' '
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
429 test_expect_success
'bare repository: check that --cached honors index' '
432 GIT_INDEX_FILE=../.git/index \
433 git check-attr --cached --stdin --all <../stdin-all |
435 test_cmp ../specified-all actual
439 test_expect_success
'bare repository: test info/attributes' '
446 ) >info/attributes &&
449 attr_check a/c/f f &&
450 attr_check a/i a/i &&
451 attr_check subdir/a/i unspecified
455 test_expect_success
'binary macro expanded by -a' '
456 echo "file binary" >.gitattributes &&
457 cat >expect <<-\EOF &&
463 git check-attr -a file >actual &&
464 test_cmp expect actual
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
474 test_expect_success SYMLINKS
'set up symlink tests' '
475 echo "* test" >attr &&
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" &&
486 test_expect_success SYMLINKS
'symlinks respected in info/attributes' '
487 test_when_finished "rm .git/info/attributes" &&
489 ln -s ../../attr .git/info/attributes &&
493 test_expect_success SYMLINKS
'symlinks not respected in-tree' '
494 test_when_finished "rm -rf .gitattributes subdir" &&
495 ln -s attr .gitattributes &&
497 ln -s ../attr subdir/.gitattributes &&
498 attr_check_basic subdir/file unspecified &&
499 test_grep "unable to access.*gitattributes" err
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
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
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 &&
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
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
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 &&