repack: fix generating multi-pack-index with only non-local packs
[alt-git.git] / t / t0003-attributes.sh
blob89b306cb114debb61e787e6d80417cd362cc367f
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 git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
34 echo "$path: test: $expect" >expect &&
35 test_cmp expect actual &&
36 test_must_be_empty err
39 test_expect_success 'open-quoted pathname' '
40 echo "\"a test=a" >.gitattributes &&
41 attr_check a unspecified
44 test_expect_success 'setup' '
45 mkdir -p a/b/d a/c b &&
47 echo "[attr]notest !test" &&
48 echo "\" d \" test=d" &&
49 echo " e test=e" &&
50 echo " e\" test=e" &&
51 echo "f test=f" &&
52 echo "a/i test=a/i" &&
53 echo "onoff test -test" &&
54 echo "offon -test test" &&
55 echo "no notest" &&
56 echo "A/e/F test=A/e/F"
57 ) >.gitattributes &&
59 echo "g test=a/g" &&
60 echo "b/g test=a/b/g"
61 ) >a/.gitattributes &&
63 echo "h test=a/b/h" &&
64 echo "d/* test=a/b/d/*" &&
65 echo "d/yes notest"
66 ) >a/b/.gitattributes &&
68 echo "global test=global"
69 ) >"$HOME"/global-gitattributes &&
70 cat <<-EOF >expect-all
71 f: test: f
72 a/f: test: f
73 a/c/f: test: f
74 a/g: test: a/g
75 a/b/g: test: a/b/g
76 b/g: test: unspecified
77 a/b/h: test: a/b/h
78 a/b/d/g: test: a/b/d/*
79 onoff: test: unset
80 offon: test: set
81 no: notest: set
82 no: test: unspecified
83 a/b/d/no: notest: set
84 a/b/d/no: test: a/b/d/*
85 a/b/d/yes: notest: set
86 a/b/d/yes: test: unspecified
87 EOF
90 test_expect_success 'setup branches' '
91 mkdir -p foo/bar &&
92 test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
93 "f test=f\na/i test=n\n" tag-1 &&
94 test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
95 "g test=g\na/i test=m\n" tag-2 &&
96 rm foo/bar/.gitattributes
99 test_expect_success 'command line checks' '
100 test_must_fail git check-attr &&
101 test_must_fail git check-attr -- &&
102 test_must_fail git check-attr test &&
103 test_must_fail git check-attr test -- &&
104 test_must_fail git check-attr -- f &&
105 test_must_fail git check-attr --source &&
106 test_must_fail git check-attr --source not-a-valid-ref &&
107 echo "f" | test_must_fail git check-attr --stdin &&
108 echo "f" | test_must_fail git check-attr --stdin -- f &&
109 echo "f" | test_must_fail git check-attr --stdin test -- f &&
110 test_must_fail git check-attr "" -- f
113 test_expect_success 'attribute test' '
115 attr_check " d " d &&
116 attr_check e e &&
117 attr_check_quote e\" e\\\" e &&
119 attr_check f f &&
120 attr_check a/f f &&
121 attr_check a/c/f f &&
122 attr_check a/g a/g &&
123 attr_check a/b/g a/b/g &&
124 attr_check b/g unspecified &&
125 attr_check a/b/h a/b/h &&
126 attr_check a/b/d/g "a/b/d/*" &&
127 attr_check onoff unset &&
128 attr_check offon set &&
129 attr_check no unspecified &&
130 attr_check a/b/d/no "a/b/d/*" &&
131 attr_check a/b/d/yes unspecified
134 test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
136 attr_check F unspecified "-c core.ignorecase=0" &&
137 attr_check a/F unspecified "-c core.ignorecase=0" &&
138 attr_check a/c/F unspecified "-c core.ignorecase=0" &&
139 attr_check a/G unspecified "-c core.ignorecase=0" &&
140 attr_check a/B/g a/g "-c core.ignorecase=0" &&
141 attr_check a/b/G unspecified "-c core.ignorecase=0" &&
142 attr_check a/b/H unspecified "-c core.ignorecase=0" &&
143 attr_check a/b/D/g a/g "-c core.ignorecase=0" &&
144 attr_check oNoFf unspecified "-c core.ignorecase=0" &&
145 attr_check oFfOn unspecified "-c core.ignorecase=0" &&
146 attr_check NO unspecified "-c core.ignorecase=0" &&
147 attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
148 attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
149 attr_check a/E/f f "-c core.ignorecase=0"
153 test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
155 attr_check F f "-c core.ignorecase=1" &&
156 attr_check a/F f "-c core.ignorecase=1" &&
157 attr_check a/c/F f "-c core.ignorecase=1" &&
158 attr_check a/G a/g "-c core.ignorecase=1" &&
159 attr_check a/B/g a/b/g "-c core.ignorecase=1" &&
160 attr_check a/b/G a/b/g "-c core.ignorecase=1" &&
161 attr_check a/b/H a/b/h "-c core.ignorecase=1" &&
162 attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=1" &&
163 attr_check oNoFf unset "-c core.ignorecase=1" &&
164 attr_check oFfOn set "-c core.ignorecase=1" &&
165 attr_check NO unspecified "-c core.ignorecase=1" &&
166 attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=1" &&
167 attr_check a/b/d/YES unspecified "-c core.ignorecase=1" &&
168 attr_check a/E/f "A/e/F" "-c core.ignorecase=1"
172 test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
173 attr_check a/B/D/g a/g "-c core.ignorecase=0" &&
174 attr_check A/B/D/NO unspecified "-c core.ignorecase=0" &&
175 attr_check A/b/h a/b/h "-c core.ignorecase=1" &&
176 attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=1" &&
177 attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=1"
180 test_expect_success 'unnormalized paths' '
181 attr_check ./f f &&
182 attr_check ./a/g a/g &&
183 attr_check a/./g a/g &&
184 attr_check a/c/../b/g a/b/g
187 test_expect_success 'relative paths' '
188 (cd a && attr_check ../f f) &&
189 (cd a && attr_check f f) &&
190 (cd a && attr_check i a/i) &&
191 (cd a && attr_check g a/g) &&
192 (cd a && attr_check b/g a/b/g) &&
193 (cd b && attr_check ../a/f f) &&
194 (cd b && attr_check ../a/g a/g) &&
195 (cd b && attr_check ../a/b/g a/b/g)
198 test_expect_success 'prefixes are not confused with leading directories' '
199 attr_check a_plus/g unspecified &&
200 cat >expect <<-\EOF &&
201 a/g: test: a/g
202 a_plus/g: test: unspecified
204 git check-attr test a/g a_plus/g >actual &&
205 test_cmp expect actual
208 test_expect_success 'core.attributesfile' '
209 attr_check global unspecified &&
210 git config core.attributesfile "$HOME/global-gitattributes" &&
211 attr_check global global &&
212 git config core.attributesfile "~/global-gitattributes" &&
213 attr_check global global &&
214 echo "global test=precedence" >>.gitattributes &&
215 attr_check global precedence
218 test_expect_success 'attribute test: read paths from stdin' '
219 grep -v notest <expect-all >expect &&
220 sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
221 test_cmp expect actual
224 test_expect_success 'setup --all option' '
225 grep -v unspecified <expect-all | sort >specified-all &&
226 sed -e "s/:.*//" <expect-all | uniq >stdin-all
229 test_expect_success 'attribute test: --all option' '
230 git check-attr --stdin --all <stdin-all >tmp &&
231 sort tmp >actual &&
232 test_cmp specified-all actual
235 test_expect_success 'attribute test: --cached option' '
236 git check-attr --cached --stdin --all <stdin-all >tmp &&
237 sort tmp >actual &&
238 test_must_be_empty actual &&
239 git add .gitattributes a/.gitattributes a/b/.gitattributes &&
240 git check-attr --cached --stdin --all <stdin-all >tmp &&
241 sort tmp >actual &&
242 test_cmp specified-all actual
245 test_expect_success 'root subdir attribute test' '
246 attr_check a/i a/i &&
247 attr_check subdir/a/i unspecified
250 test_expect_success 'negative patterns' '
251 echo "!f test=bar" >.gitattributes &&
252 git check-attr test -- '"'"'!f'"'"' 2>errors &&
253 test_i18ngrep "Negative patterns are ignored" errors
256 test_expect_success 'patterns starting with exclamation' '
257 echo "\!f test=foo" >.gitattributes &&
258 attr_check "!f" foo
261 test_expect_success '"**" test' '
262 echo "**/f foo=bar" >.gitattributes &&
263 cat <<\EOF >expect &&
264 f: foo: bar
265 a/f: foo: bar
266 a/b/f: foo: bar
267 a/b/c/f: foo: bar
269 git check-attr foo -- "f" >actual 2>err &&
270 git check-attr foo -- "a/f" >>actual 2>>err &&
271 git check-attr foo -- "a/b/f" >>actual 2>>err &&
272 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
273 test_cmp expect actual &&
274 test_must_be_empty err
277 test_expect_success '"**" with no slashes test' '
278 echo "a**f foo=bar" >.gitattributes &&
279 git check-attr foo -- "f" >actual &&
280 cat <<\EOF >expect &&
281 f: foo: unspecified
282 af: foo: bar
283 axf: foo: bar
284 a/f: foo: unspecified
285 a/b/f: foo: unspecified
286 a/b/c/f: foo: unspecified
288 git check-attr foo -- "f" >actual 2>err &&
289 git check-attr foo -- "af" >>actual 2>err &&
290 git check-attr foo -- "axf" >>actual 2>err &&
291 git check-attr foo -- "a/f" >>actual 2>>err &&
292 git check-attr foo -- "a/b/f" >>actual 2>>err &&
293 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
294 test_cmp expect actual &&
295 test_must_be_empty err
298 test_expect_success 'using --git-dir and --work-tree' '
299 mkdir unreal real &&
300 git init real &&
301 echo "file test=in-real" >real/.gitattributes &&
303 cd unreal &&
304 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
308 test_expect_success 'using --source' '
309 attr_check_source foo/bar/f f tag-1 &&
310 attr_check_source foo/bar/a/i n tag-1 &&
311 attr_check_source foo/bar/f unspecified tag-2 &&
312 attr_check_source foo/bar/a/i m tag-2 &&
313 attr_check_source foo/bar/g g tag-2 &&
314 attr_check_source foo/bar/g unspecified tag-1
317 test_expect_success 'setup bare' '
318 git clone --template= --bare . bare.git
321 test_expect_success 'bare repository: check that .gitattribute is ignored' '
323 cd bare.git &&
325 echo "f test=f" &&
326 echo "a/i test=a/i"
327 ) >.gitattributes &&
328 attr_check f unspecified &&
329 attr_check a/f unspecified &&
330 attr_check a/c/f unspecified &&
331 attr_check a/i unspecified &&
332 attr_check subdir/a/i unspecified
336 test_expect_success 'bare repository: with --source' '
338 cd bare.git &&
339 attr_check_source foo/bar/f f tag-1 &&
340 attr_check_source foo/bar/a/i n tag-1 &&
341 attr_check_source foo/bar/f unspecified tag-2 &&
342 attr_check_source foo/bar/a/i m tag-2 &&
343 attr_check_source foo/bar/g g tag-2 &&
344 attr_check_source foo/bar/g unspecified tag-1
348 test_expect_success 'bare repository: check that --cached honors index' '
350 cd bare.git &&
351 GIT_INDEX_FILE=../.git/index \
352 git check-attr --cached --stdin --all <../stdin-all |
353 sort >actual &&
354 test_cmp ../specified-all actual
358 test_expect_success 'bare repository: test info/attributes' '
360 cd bare.git &&
361 mkdir info &&
363 echo "f test=f" &&
364 echo "a/i test=a/i"
365 ) >info/attributes &&
366 attr_check f f &&
367 attr_check a/f f &&
368 attr_check a/c/f f &&
369 attr_check a/i a/i &&
370 attr_check subdir/a/i unspecified
374 test_expect_success 'binary macro expanded by -a' '
375 echo "file binary" >.gitattributes &&
376 cat >expect <<-\EOF &&
377 file: binary: set
378 file: diff: unset
379 file: merge: unset
380 file: text: unset
382 git check-attr -a file >actual &&
383 test_cmp expect actual
386 test_expect_success 'query binary macro directly' '
387 echo "file binary" >.gitattributes &&
388 echo file: binary: set >expect &&
389 git check-attr binary file >actual &&
390 test_cmp expect actual
393 test_expect_success SYMLINKS 'set up symlink tests' '
394 echo "* test" >attr &&
395 rm -f .gitattributes
398 test_expect_success SYMLINKS 'symlinks respected in core.attributesFile' '
399 test_when_finished "rm symlink" &&
400 ln -s attr symlink &&
401 test_config core.attributesFile "$(pwd)/symlink" &&
402 attr_check file set
405 test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
406 test_when_finished "rm .git/info/attributes" &&
407 mkdir .git/info &&
408 ln -s ../../attr .git/info/attributes &&
409 attr_check file set
412 test_expect_success SYMLINKS 'symlinks not respected in-tree' '
413 test_when_finished "rm -rf .gitattributes subdir" &&
414 ln -s attr .gitattributes &&
415 mkdir subdir &&
416 ln -s ../attr subdir/.gitattributes &&
417 attr_check_basic subdir/file unspecified &&
418 test_i18ngrep "unable to access.*gitattributes" err
421 test_expect_success 'large attributes line ignored in tree' '
422 test_when_finished "rm .gitattributes" &&
423 printf "path %02043d" 1 >.gitattributes &&
424 git check-attr --all path >actual 2>err &&
425 echo "warning: ignoring overly long attributes line 1" >expect &&
426 test_cmp expect err &&
427 test_must_be_empty actual
430 test_expect_success 'large attributes line ignores trailing content in tree' '
431 test_when_finished "rm .gitattributes" &&
432 # older versions of Git broke lines at 2048 bytes; the 2045 bytes
433 # of 0-padding here is accounting for the three bytes of "a 1", which
434 # would knock "trailing" to the "next" line, where it would be
435 # erroneously parsed.
436 printf "a %02045dtrailing attribute\n" 1 >.gitattributes &&
437 git check-attr --all trailing >actual 2>err &&
438 echo "warning: ignoring overly long attributes line 1" >expect &&
439 test_cmp expect err &&
440 test_must_be_empty actual
443 test_expect_success EXPENSIVE 'large attributes file ignored in tree' '
444 test_when_finished "rm .gitattributes" &&
445 dd if=/dev/zero of=.gitattributes bs=1048576 count=101 2>/dev/null &&
446 git check-attr --all path >/dev/null 2>err &&
447 echo "warning: ignoring overly large gitattributes file ${SQ}.gitattributes${SQ}" >expect &&
448 test_cmp expect err
451 test_expect_success 'large attributes line ignored in index' '
452 test_when_finished "git update-index --remove .gitattributes" &&
453 blob=$(printf "path %02043d" 1 | git hash-object -w --stdin) &&
454 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
455 git check-attr --cached --all path >actual 2>err &&
456 echo "warning: ignoring overly long attributes line 1" >expect &&
457 test_cmp expect err &&
458 test_must_be_empty actual
461 test_expect_success 'large attributes line ignores trailing content in index' '
462 test_when_finished "git update-index --remove .gitattributes" &&
463 blob=$(printf "a %02045dtrailing attribute\n" 1 | git hash-object -w --stdin) &&
464 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
465 git check-attr --cached --all trailing >actual 2>err &&
466 echo "warning: ignoring overly long attributes line 1" >expect &&
467 test_cmp expect err &&
468 test_must_be_empty actual
471 test_expect_success EXPENSIVE 'large attributes file ignored in index' '
472 test_when_finished "git update-index --remove .gitattributes" &&
473 blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) &&
474 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
475 git check-attr --cached --all path >/dev/null 2>err &&
476 echo "warning: ignoring overly large gitattributes blob ${SQ}.gitattributes${SQ}" >expect &&
477 test_cmp expect err
480 test_done