]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0003-attributes.sh
Merge branch 'jk/fsck-indices-in-worktrees'
[thirdparty/git.git] / t / t0003-attributes.sh
CommitLineData
cf94ccda
JH
1#!/bin/sh
2
3test_description=gitattributes
4
c150064d 5TEST_PASSES_SANITIZE_LEAK=true
8da0b02d 6TEST_CREATE_REPO_NO_TEMPLATE=1
cf94ccda
JH
7. ./test-lib.sh
8
2ef579e2 9attr_check_basic () {
3738439c 10 path="$1" expect="$2" git_opts="$3" &&
cf94ccda 11
3738439c
DL
12 git $git_opts check-attr test -- "$path" >actual 2>err &&
13 echo "$path: test: $expect" >expect &&
2ef579e2
JK
14 test_cmp expect actual
15}
16
17attr_check () {
18 attr_check_basic "$@" &&
99c049bc 19 test_must_be_empty err
cf94ccda
JH
20}
21
860a74d9 22attr_check_quote () {
3738439c 23 path="$1" quoted_path="$2" expect="$3" &&
860a74d9
NTND
24
25 git check-attr test -- "$path" >actual &&
26 echo "\"$quoted_path\": test: $expect" >expect &&
27 test_cmp expect actual
47cfc9bd
KN
28}
29
30attr_check_source () {
31 path="$1" expect="$2" source="$3" git_opts="$4" &&
860a74d9 32
47cfc9bd
KN
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
860a74d9
NTND
37}
38
39test_expect_success 'open-quoted pathname' '
40 echo "\"a test=a" >.gitattributes &&
f46c243e 41 attr_check a unspecified
860a74d9
NTND
42'
43
cf94ccda 44test_expect_success 'setup' '
0216af83 45 mkdir -p a/b/d a/c b &&
cf94ccda 46 (
75651fd7
ES
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" &&
6eba6210 56 echo "A/e/F test=A/e/F"
cf94ccda
JH
57 ) >.gitattributes &&
58 (
59 echo "g test=a/g" &&
60 echo "b/g test=a/b/g"
61 ) >a/.gitattributes &&
62 (
63 echo "h test=a/b/h" &&
75651fd7 64 echo "d/* test=a/b/d/*" &&
ec775c41 65 echo "d/yes notest"
dcc04366 66 ) >a/b/.gitattributes &&
6df42ab9
PO
67 (
68 echo "global test=global"
c9d8f0ac 69 ) >"$HOME"/global-gitattributes &&
78cec757
JH
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
cf94ccda
JH
88'
89
47cfc9bd
KN
90test_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
97'
98
c0b13b21 99test_expect_success 'command line checks' '
09d7dd7a
MH
100 test_must_fail git check-attr &&
101 test_must_fail git check-attr -- &&
fdf6be82
MH
102 test_must_fail git check-attr test &&
103 test_must_fail git check-attr test -- &&
09d7dd7a 104 test_must_fail git check-attr -- f &&
47cfc9bd
KN
105 test_must_fail git check-attr --source &&
106 test_must_fail git check-attr --source not-a-valid-ref &&
09d7dd7a
MH
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 &&
c0b13b21 110 test_must_fail git check-attr "" -- f
c0b13b21
MH
111'
112
cf94ccda 113test_expect_success 'attribute test' '
860a74d9
NTND
114
115 attr_check " d " d &&
116 attr_check e e &&
117 attr_check_quote e\" e\\\" e &&
118
cf94ccda
JH
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 &&
520ea857
MM
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/*" &&
ec775c41 131 attr_check a/b/d/yes unspecified
cf94ccda
JH
132'
133
6eba6210
BC
134test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
135
f46c243e
DL
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" &&
6eba6210 146 attr_check NO unspecified "-c core.ignorecase=0" &&
f46c243e 147 attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
6eba6210 148 attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
f46c243e 149 attr_check a/E/f f "-c core.ignorecase=0"
6eba6210
BC
150
151'
152
153test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
154
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"
169
170'
171
6eba6210 172test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
f46c243e
DL
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" &&
6eba6210
BC
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"
178'
179
f5114a40 180test_expect_success 'unnormalized paths' '
d4d4f8df
MH
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
d4d4f8df
MH
185'
186
f5114a40 187test_expect_success 'relative paths' '
0216af83
MH
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)
0216af83
MH
196'
197
1afca444
JK
198test_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
203 EOF
204 git check-attr test a/g a_plus/g >actual &&
205 test_cmp expect actual
206'
207
6df42ab9
PO
208test_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 &&
78cec757 214 echo "global test=precedence" >>.gitattributes &&
6df42ab9
PO
215 attr_check global precedence
216'
217
b4666852 218test_expect_success 'attribute test: read paths from stdin' '
78cec757
JH
219 grep -v notest <expect-all >expect &&
220 sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
b4666852
DP
221 test_cmp expect actual
222'
223
c847e8c2 224test_expect_success 'setup --all option' '
b2b3e9c2 225 grep -v unspecified <expect-all | sort >specified-all &&
c847e8c2
KN
226 sed -e "s/:.*//" <expect-all | uniq >stdin-all
227'
228
229test_expect_success 'attribute test: --all option' '
ae5d569b
SM
230 git check-attr --stdin --all <stdin-all >tmp &&
231 sort tmp >actual &&
b2b3e9c2
JS
232 test_cmp specified-all actual
233'
234
235test_expect_success 'attribute test: --cached option' '
ae5d569b
SM
236 git check-attr --cached --stdin --all <stdin-all >tmp &&
237 sort tmp >actual &&
1c5e94f4 238 test_must_be_empty actual &&
b2b3e9c2 239 git add .gitattributes a/.gitattributes a/b/.gitattributes &&
ae5d569b
SM
240 git check-attr --cached --stdin --all <stdin-all >tmp &&
241 sort tmp >actual &&
b2b3e9c2 242 test_cmp specified-all actual
4ca0f188
MH
243'
244
82881b38 245test_expect_success 'root subdir attribute test' '
82881b38
MO
246 attr_check a/i a/i &&
247 attr_check subdir/a/i unspecified
82881b38
MO
248'
249
82dce998
NTND
250test_expect_success 'negative patterns' '
251 echo "!f test=bar" >.gitattributes &&
8b1bd024
TR
252 git check-attr test -- '"'"'!f'"'"' 2>errors &&
253 test_i18ngrep "Negative patterns are ignored" errors
82dce998
NTND
254'
255
256test_expect_success 'patterns starting with exclamation' '
257 echo "\!f test=foo" >.gitattributes &&
258 attr_check "!f" foo
259'
260
237ec6e4
NTND
261test_expect_success '"**" test' '
262 echo "**/f foo=bar" >.gitattributes &&
263 cat <<\EOF >expect &&
264f: foo: bar
265a/f: foo: bar
266a/b/f: foo: bar
267a/b/c/f: foo: bar
268EOF
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 &&
99c049bc 274 test_must_be_empty err
237ec6e4
NTND
275'
276
277test_expect_success '"**" with no slashes test' '
278 echo "a**f foo=bar" >.gitattributes &&
279 git check-attr foo -- "f" >actual &&
280 cat <<\EOF >expect &&
281f: foo: unspecified
282af: foo: bar
283axf: foo: bar
284a/f: foo: unspecified
285a/b/f: foo: unspecified
286a/b/c/f: foo: unspecified
287EOF
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 &&
99c049bc 295 test_must_be_empty err
237ec6e4
NTND
296'
297
cdbf6232
JH
298test_expect_success 'using --git-dir and --work-tree' '
299 mkdir unreal real &&
300 git init real &&
301 echo "file test=in-real" >real/.gitattributes &&
302 (
303 cd unreal &&
304 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
305 )
306'
307
47cfc9bd
KN
308test_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
315'
316
2d35d556 317test_expect_success 'setup bare' '
8da0b02d 318 git clone --template= --bare . bare.git
2d35d556
RS
319'
320
321test_expect_success 'bare repository: check that .gitattribute is ignored' '
2d35d556 322 (
c4a7bce1
JH
323 cd bare.git &&
324 (
75651fd7 325 echo "f test=f" &&
c4a7bce1
JH
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
333 )
2d35d556
RS
334'
335
47cfc9bd
KN
336test_expect_success 'bare repository: with --source' '
337 (
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
345 )
346'
347
b2b3e9c2 348test_expect_success 'bare repository: check that --cached honors index' '
c4a7bce1
JH
349 (
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
355 )
b2b3e9c2
JS
356'
357
2d35d556 358test_expect_success 'bare repository: test info/attributes' '
2d35d556 359 (
c4a7bce1 360 cd bare.git &&
8da0b02d 361 mkdir info &&
c4a7bce1 362 (
75651fd7 363 echo "f test=f" &&
c4a7bce1
JH
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
371 )
2d35d556
RS
372'
373
7b95849b
JK
374test_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
381 EOF
382 git check-attr -a file >actual &&
383 test_cmp expect actual
384'
385
7b95849b
JK
386test_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
391'
392
2ef579e2
JK
393test_expect_success SYMLINKS 'set up symlink tests' '
394 echo "* test" >attr &&
395 rm -f .gitattributes
396'
397
398test_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
403'
404
405test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
406 test_when_finished "rm .git/info/attributes" &&
8da0b02d 407 mkdir .git/info &&
2ef579e2
JK
408 ln -s ../../attr .git/info/attributes &&
409 attr_check file set
410'
411
412test_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
419'
420
dfa6b32b
PS
421test_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
428'
429
d74b1fd5
PS
430test_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 &&
dfa6b32b
PS
438 echo "warning: ignoring overly long attributes line 1" >expect &&
439 test_cmp expect err &&
440 test_must_be_empty actual
441'
442
3c50032f
PS
443test_expect_success EXPENSIVE 'large attributes file ignored in tree' '
444 test_when_finished "rm .gitattributes" &&
5458ba0a 445 dd if=/dev/zero of=.gitattributes bs=1048576 count=101 2>/dev/null &&
3c50032f
PS
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
449'
450
dfa6b32b
PS
451test_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 &&
d74b1fd5
PS
458 test_must_be_empty actual
459'
460
461test_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 &&
dfa6b32b
PS
466 echo "warning: ignoring overly long attributes line 1" >expect &&
467 test_cmp expect err &&
d74b1fd5
PS
468 test_must_be_empty actual
469'
470
3c50032f
PS
471test_expect_success EXPENSIVE 'large attributes file ignored in index' '
472 test_when_finished "git update-index --remove .gitattributes" &&
5458ba0a 473 blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) &&
3c50032f
PS
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
478'
479
cf94ccda 480test_done