packfile.c: fix a typo in `each_file_in_pack_dir_fn()`'s declaration
[alt-git.git] / t / t0003-attributes.sh
blob26e082f05b4473a553b4de9b7747b928179439cd
1 #!/bin/sh
3 test_description=gitattributes
5 TEST_PASSES_SANITIZE_LEAK=true
6 TEST_CREATE_REPO_NO_TEMPLATE=1
7 . ./test-lib.sh
9 attr_check_basic () {
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
17 attr_check () {
18 attr_check_basic "$@" &&
19 test_must_be_empty err
22 attr_check_quote () {
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_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
44 test_cmp expect actual &&
45 test_must_be_empty err
48 test_expect_success 'open-quoted pathname' '
49 echo "\"a test=a" >.gitattributes &&
50 attr_check a unspecified
53 test_expect_success 'setup' '
54 mkdir -p a/b/d a/c b &&
56 echo "[attr]notest !test" &&
57 echo "\" d \" test=d" &&
58 echo " e test=e" &&
59 echo " e\" test=e" &&
60 echo "f test=f" &&
61 echo "a/i test=a/i" &&
62 echo "onoff test -test" &&
63 echo "offon -test test" &&
64 echo "no notest" &&
65 echo "A/e/F test=A/e/F"
66 ) >.gitattributes &&
68 echo "g test=a/g" &&
69 echo "b/g test=a/b/g"
70 ) >a/.gitattributes &&
72 echo "h test=a/b/h" &&
73 echo "d/* test=a/b/d/*" &&
74 echo "d/yes notest"
75 ) >a/b/.gitattributes &&
77 echo "global test=global"
78 ) >"$HOME"/global-gitattributes &&
79 cat <<-EOF >expect-all
80 f: test: f
81 a/f: test: f
82 a/c/f: test: f
83 a/g: test: a/g
84 a/b/g: test: a/b/g
85 b/g: test: unspecified
86 a/b/h: test: a/b/h
87 a/b/d/g: test: a/b/d/*
88 onoff: test: unset
89 offon: test: set
90 no: notest: set
91 no: test: unspecified
92 a/b/d/no: notest: set
93 a/b/d/no: test: a/b/d/*
94 a/b/d/yes: notest: set
95 a/b/d/yes: test: unspecified
96 EOF
99 test_expect_success 'setup branches' '
100 mkdir -p foo/bar &&
101 test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
102 "f test=f\na/i test=n\n" tag-1 &&
103 test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
104 "g test=g\na/i test=m\n" tag-2 &&
105 rm foo/bar/.gitattributes
108 test_expect_success 'command line checks' '
109 test_must_fail git check-attr &&
110 test_must_fail git check-attr -- &&
111 test_must_fail git check-attr test &&
112 test_must_fail git check-attr test -- &&
113 test_must_fail git check-attr -- f &&
114 test_must_fail git check-attr --source &&
115 test_must_fail git check-attr --source not-a-valid-ref &&
116 echo "f" | test_must_fail git check-attr --stdin &&
117 echo "f" | test_must_fail git check-attr --stdin -- f &&
118 echo "f" | test_must_fail git check-attr --stdin test -- f &&
119 test_must_fail git check-attr "" -- f
122 test_expect_success 'attribute test' '
124 attr_check " d " d &&
125 attr_check e e &&
126 attr_check_quote e\" e\\\" e &&
128 attr_check f f &&
129 attr_check a/f f &&
130 attr_check a/c/f f &&
131 attr_check a/g a/g &&
132 attr_check a/b/g a/b/g &&
133 attr_check b/g unspecified &&
134 attr_check a/b/h a/b/h &&
135 attr_check a/b/d/g "a/b/d/*" &&
136 attr_check onoff unset &&
137 attr_check offon set &&
138 attr_check no unspecified &&
139 attr_check a/b/d/no "a/b/d/*" &&
140 attr_check a/b/d/yes unspecified
143 test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
145 attr_check F unspecified "-c core.ignorecase=0" &&
146 attr_check a/F unspecified "-c core.ignorecase=0" &&
147 attr_check a/c/F unspecified "-c core.ignorecase=0" &&
148 attr_check a/G unspecified "-c core.ignorecase=0" &&
149 attr_check a/B/g a/g "-c core.ignorecase=0" &&
150 attr_check a/b/G unspecified "-c core.ignorecase=0" &&
151 attr_check a/b/H unspecified "-c core.ignorecase=0" &&
152 attr_check a/b/D/g a/g "-c core.ignorecase=0" &&
153 attr_check oNoFf unspecified "-c core.ignorecase=0" &&
154 attr_check oFfOn unspecified "-c core.ignorecase=0" &&
155 attr_check NO unspecified "-c core.ignorecase=0" &&
156 attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
157 attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
158 attr_check a/E/f f "-c core.ignorecase=0"
162 test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
164 attr_check F f "-c core.ignorecase=1" &&
165 attr_check a/F f "-c core.ignorecase=1" &&
166 attr_check a/c/F f "-c core.ignorecase=1" &&
167 attr_check a/G a/g "-c core.ignorecase=1" &&
168 attr_check a/B/g a/b/g "-c core.ignorecase=1" &&
169 attr_check a/b/G a/b/g "-c core.ignorecase=1" &&
170 attr_check a/b/H a/b/h "-c core.ignorecase=1" &&
171 attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=1" &&
172 attr_check oNoFf unset "-c core.ignorecase=1" &&
173 attr_check oFfOn set "-c core.ignorecase=1" &&
174 attr_check NO unspecified "-c core.ignorecase=1" &&
175 attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=1" &&
176 attr_check a/b/d/YES unspecified "-c core.ignorecase=1" &&
177 attr_check a/E/f "A/e/F" "-c core.ignorecase=1"
181 test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
182 attr_check a/B/D/g a/g "-c core.ignorecase=0" &&
183 attr_check A/B/D/NO unspecified "-c core.ignorecase=0" &&
184 attr_check A/b/h a/b/h "-c core.ignorecase=1" &&
185 attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=1" &&
186 attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=1"
189 test_expect_success 'unnormalized paths' '
190 attr_check ./f f &&
191 attr_check ./a/g a/g &&
192 attr_check a/./g a/g &&
193 attr_check a/c/../b/g a/b/g
196 test_expect_success 'relative paths' '
197 (cd a && attr_check ../f f) &&
198 (cd a && attr_check f f) &&
199 (cd a && attr_check i a/i) &&
200 (cd a && attr_check g a/g) &&
201 (cd a && attr_check b/g a/b/g) &&
202 (cd b && attr_check ../a/f f) &&
203 (cd b && attr_check ../a/g a/g) &&
204 (cd b && attr_check ../a/b/g a/b/g)
207 test_expect_success 'prefixes are not confused with leading directories' '
208 attr_check a_plus/g unspecified &&
209 cat >expect <<-\EOF &&
210 a/g: test: a/g
211 a_plus/g: test: unspecified
213 git check-attr test a/g a_plus/g >actual &&
214 test_cmp expect actual
217 test_expect_success 'core.attributesfile' '
218 attr_check global unspecified &&
219 git config core.attributesfile "$HOME/global-gitattributes" &&
220 attr_check global global &&
221 git config core.attributesfile "~/global-gitattributes" &&
222 attr_check global global &&
223 echo "global test=precedence" >>.gitattributes &&
224 attr_check global precedence
227 test_expect_success 'attribute test: read paths from stdin' '
228 grep -v notest <expect-all >expect &&
229 sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
230 test_cmp expect actual
233 test_expect_success 'setup --all option' '
234 grep -v unspecified <expect-all | sort >specified-all &&
235 sed -e "s/:.*//" <expect-all | uniq >stdin-all
238 test_expect_success 'attribute test: --all option' '
239 git check-attr --stdin --all <stdin-all >tmp &&
240 sort tmp >actual &&
241 test_cmp specified-all actual
244 test_expect_success 'attribute test: --cached option' '
245 git check-attr --cached --stdin --all <stdin-all >tmp &&
246 sort tmp >actual &&
247 test_must_be_empty actual &&
248 git add .gitattributes a/.gitattributes a/b/.gitattributes &&
249 git check-attr --cached --stdin --all <stdin-all >tmp &&
250 sort tmp >actual &&
251 test_cmp specified-all actual
254 test_expect_success 'root subdir attribute test' '
255 attr_check a/i a/i &&
256 attr_check subdir/a/i unspecified
259 test_expect_success 'negative patterns' '
260 echo "!f test=bar" >.gitattributes &&
261 git check-attr test -- '"'"'!f'"'"' 2>errors &&
262 test_i18ngrep "Negative patterns are ignored" errors
265 test_expect_success 'patterns starting with exclamation' '
266 echo "\!f test=foo" >.gitattributes &&
267 attr_check "!f" foo
270 test_expect_success '"**" test' '
271 echo "**/f foo=bar" >.gitattributes &&
272 cat <<\EOF >expect &&
273 f: foo: bar
274 a/f: foo: bar
275 a/b/f: foo: bar
276 a/b/c/f: foo: bar
278 git check-attr foo -- "f" >actual 2>err &&
279 git check-attr foo -- "a/f" >>actual 2>>err &&
280 git check-attr foo -- "a/b/f" >>actual 2>>err &&
281 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
282 test_cmp expect actual &&
283 test_must_be_empty err
286 test_expect_success '"**" with no slashes test' '
287 echo "a**f foo=bar" >.gitattributes &&
288 git check-attr foo -- "f" >actual &&
289 cat <<\EOF >expect &&
290 f: foo: unspecified
291 af: foo: bar
292 axf: foo: bar
293 a/f: foo: unspecified
294 a/b/f: foo: unspecified
295 a/b/c/f: foo: unspecified
297 git check-attr foo -- "f" >actual 2>err &&
298 git check-attr foo -- "af" >>actual 2>err &&
299 git check-attr foo -- "axf" >>actual 2>err &&
300 git check-attr foo -- "a/f" >>actual 2>>err &&
301 git check-attr foo -- "a/b/f" >>actual 2>>err &&
302 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
303 test_cmp expect actual &&
304 test_must_be_empty err
307 test_expect_success 'using --git-dir and --work-tree' '
308 mkdir unreal real &&
309 git init real &&
310 echo "file test=in-real" >real/.gitattributes &&
312 cd unreal &&
313 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
317 test_expect_success 'using --source' '
318 attr_check_source foo/bar/f f tag-1 &&
319 attr_check_source foo/bar/a/i n tag-1 &&
320 attr_check_source foo/bar/f unspecified tag-2 &&
321 attr_check_source foo/bar/a/i m tag-2 &&
322 attr_check_source foo/bar/g g tag-2 &&
323 attr_check_source foo/bar/g unspecified tag-1
326 test_expect_success 'setup bare' '
327 git clone --template= --bare . bare.git
330 test_expect_success 'bare repository: check that .gitattribute is ignored' '
332 cd bare.git &&
334 echo "f test=f" &&
335 echo "a/i test=a/i"
336 ) >.gitattributes &&
337 attr_check f unspecified &&
338 attr_check a/f unspecified &&
339 attr_check a/c/f unspecified &&
340 attr_check a/i unspecified &&
341 attr_check subdir/a/i unspecified
345 test_expect_success 'bare repository: with --source' '
347 cd bare.git &&
348 attr_check_source foo/bar/f f tag-1 &&
349 attr_check_source foo/bar/a/i n tag-1 &&
350 attr_check_source foo/bar/f unspecified tag-2 &&
351 attr_check_source foo/bar/a/i m tag-2 &&
352 attr_check_source foo/bar/g g tag-2 &&
353 attr_check_source foo/bar/g unspecified tag-1
357 test_expect_success 'bare repository: check that --cached honors index' '
359 cd bare.git &&
360 GIT_INDEX_FILE=../.git/index \
361 git check-attr --cached --stdin --all <../stdin-all |
362 sort >actual &&
363 test_cmp ../specified-all actual
367 test_expect_success 'bare repository: test info/attributes' '
369 cd bare.git &&
370 mkdir info &&
372 echo "f test=f" &&
373 echo "a/i test=a/i"
374 ) >info/attributes &&
375 attr_check f f &&
376 attr_check a/f f &&
377 attr_check a/c/f f &&
378 attr_check a/i a/i &&
379 attr_check subdir/a/i unspecified
383 test_expect_success 'binary macro expanded by -a' '
384 echo "file binary" >.gitattributes &&
385 cat >expect <<-\EOF &&
386 file: binary: set
387 file: diff: unset
388 file: merge: unset
389 file: text: unset
391 git check-attr -a file >actual &&
392 test_cmp expect actual
395 test_expect_success 'query binary macro directly' '
396 echo "file binary" >.gitattributes &&
397 echo file: binary: set >expect &&
398 git check-attr binary file >actual &&
399 test_cmp expect actual
402 test_expect_success SYMLINKS 'set up symlink tests' '
403 echo "* test" >attr &&
404 rm -f .gitattributes
407 test_expect_success SYMLINKS 'symlinks respected in core.attributesFile' '
408 test_when_finished "rm symlink" &&
409 ln -s attr symlink &&
410 test_config core.attributesFile "$(pwd)/symlink" &&
411 attr_check file set
414 test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
415 test_when_finished "rm .git/info/attributes" &&
416 mkdir .git/info &&
417 ln -s ../../attr .git/info/attributes &&
418 attr_check file set
421 test_expect_success SYMLINKS 'symlinks not respected in-tree' '
422 test_when_finished "rm -rf .gitattributes subdir" &&
423 ln -s attr .gitattributes &&
424 mkdir subdir &&
425 ln -s ../attr subdir/.gitattributes &&
426 attr_check_basic subdir/file unspecified &&
427 test_i18ngrep "unable to access.*gitattributes" err
430 test_expect_success 'large attributes line ignored in tree' '
431 test_when_finished "rm .gitattributes" &&
432 printf "path %02043d" 1 >.gitattributes &&
433 git check-attr --all path >actual 2>err &&
434 echo "warning: ignoring overly long attributes line 1" >expect &&
435 test_cmp expect err &&
436 test_must_be_empty actual
439 test_expect_success 'large attributes line ignores trailing content in tree' '
440 test_when_finished "rm .gitattributes" &&
441 # older versions of Git broke lines at 2048 bytes; the 2045 bytes
442 # of 0-padding here is accounting for the three bytes of "a 1", which
443 # would knock "trailing" to the "next" line, where it would be
444 # erroneously parsed.
445 printf "a %02045dtrailing attribute\n" 1 >.gitattributes &&
446 git check-attr --all trailing >actual 2>err &&
447 echo "warning: ignoring overly long attributes line 1" >expect &&
448 test_cmp expect err &&
449 test_must_be_empty actual
452 test_expect_success EXPENSIVE 'large attributes file ignored in tree' '
453 test_when_finished "rm .gitattributes" &&
454 dd if=/dev/zero of=.gitattributes bs=1048576 count=101 2>/dev/null &&
455 git check-attr --all path >/dev/null 2>err &&
456 echo "warning: ignoring overly large gitattributes file ${SQ}.gitattributes${SQ}" >expect &&
457 test_cmp expect err
460 test_expect_success 'large attributes line ignored in index' '
461 test_when_finished "git update-index --remove .gitattributes" &&
462 blob=$(printf "path %02043d" 1 | git hash-object -w --stdin) &&
463 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
464 git check-attr --cached --all path >actual 2>err &&
465 echo "warning: ignoring overly long attributes line 1" >expect &&
466 test_cmp expect err &&
467 test_must_be_empty actual
470 test_expect_success 'large attributes line ignores trailing content in index' '
471 test_when_finished "git update-index --remove .gitattributes" &&
472 blob=$(printf "a %02045dtrailing attribute\n" 1 | git hash-object -w --stdin) &&
473 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
474 git check-attr --cached --all trailing >actual 2>err &&
475 echo "warning: ignoring overly long attributes line 1" >expect &&
476 test_cmp expect err &&
477 test_must_be_empty actual
480 test_expect_success EXPENSIVE 'large attributes file ignored in index' '
481 test_when_finished "git update-index --remove .gitattributes" &&
482 blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) &&
483 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
484 git check-attr --cached --all path >/dev/null 2>err &&
485 echo "warning: ignoring overly large gitattributes blob ${SQ}.gitattributes${SQ}" >expect &&
486 test_cmp expect err
489 test_done