]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0003-attributes.sh
Merge branch 'ob/t9001-indent-fix'
[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 33 echo "$path: test: $expect" >expect &&
44451a2e
JC
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_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
47cfc9bd
KN
44 test_cmp expect actual &&
45 test_must_be_empty err
860a74d9
NTND
46}
47
48test_expect_success 'open-quoted pathname' '
49 echo "\"a test=a" >.gitattributes &&
f46c243e 50 attr_check a unspecified
860a74d9
NTND
51'
52
cf94ccda 53test_expect_success 'setup' '
0216af83 54 mkdir -p a/b/d a/c b &&
cf94ccda 55 (
75651fd7
ES
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" &&
6eba6210 65 echo "A/e/F test=A/e/F"
cf94ccda
JH
66 ) >.gitattributes &&
67 (
68 echo "g test=a/g" &&
69 echo "b/g test=a/b/g"
70 ) >a/.gitattributes &&
71 (
72 echo "h test=a/b/h" &&
75651fd7 73 echo "d/* test=a/b/d/*" &&
ec775c41 74 echo "d/yes notest"
dcc04366 75 ) >a/b/.gitattributes &&
6df42ab9
PO
76 (
77 echo "global test=global"
c9d8f0ac 78 ) >"$HOME"/global-gitattributes &&
78cec757
JH
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
cf94ccda
JH
97'
98
47cfc9bd
KN
99test_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
106'
107
c0b13b21 108test_expect_success 'command line checks' '
09d7dd7a
MH
109 test_must_fail git check-attr &&
110 test_must_fail git check-attr -- &&
fdf6be82
MH
111 test_must_fail git check-attr test &&
112 test_must_fail git check-attr test -- &&
09d7dd7a 113 test_must_fail git check-attr -- f &&
47cfc9bd
KN
114 test_must_fail git check-attr --source &&
115 test_must_fail git check-attr --source not-a-valid-ref &&
09d7dd7a
MH
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 &&
c0b13b21 119 test_must_fail git check-attr "" -- f
c0b13b21
MH
120'
121
cf94ccda 122test_expect_success 'attribute test' '
860a74d9
NTND
123
124 attr_check " d " d &&
125 attr_check e e &&
126 attr_check_quote e\" e\\\" e &&
127
cf94ccda
JH
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 &&
520ea857
MM
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/*" &&
ec775c41 140 attr_check a/b/d/yes unspecified
cf94ccda
JH
141'
142
6eba6210
BC
143test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
144
f46c243e
DL
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" &&
6eba6210 155 attr_check NO unspecified "-c core.ignorecase=0" &&
f46c243e 156 attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
6eba6210 157 attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
f46c243e 158 attr_check a/E/f f "-c core.ignorecase=0"
6eba6210
BC
159
160'
161
162test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
163
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"
178
179'
180
6eba6210 181test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
f46c243e
DL
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" &&
6eba6210
BC
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"
187'
188
f5114a40 189test_expect_success 'unnormalized paths' '
d4d4f8df
MH
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
d4d4f8df
MH
194'
195
f5114a40 196test_expect_success 'relative paths' '
0216af83
MH
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)
0216af83
MH
205'
206
1afca444
JK
207test_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
212 EOF
213 git check-attr test a/g a_plus/g >actual &&
214 test_cmp expect actual
215'
216
6df42ab9
PO
217test_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 &&
78cec757 223 echo "global test=precedence" >>.gitattributes &&
6df42ab9
PO
224 attr_check global precedence
225'
226
b4666852 227test_expect_success 'attribute test: read paths from stdin' '
78cec757
JH
228 grep -v notest <expect-all >expect &&
229 sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
b4666852
DP
230 test_cmp expect actual
231'
232
c847e8c2 233test_expect_success 'setup --all option' '
b2b3e9c2 234 grep -v unspecified <expect-all | sort >specified-all &&
c847e8c2
KN
235 sed -e "s/:.*//" <expect-all | uniq >stdin-all
236'
237
238test_expect_success 'attribute test: --all option' '
ae5d569b
SM
239 git check-attr --stdin --all <stdin-all >tmp &&
240 sort tmp >actual &&
b2b3e9c2
JS
241 test_cmp specified-all actual
242'
243
244test_expect_success 'attribute test: --cached option' '
ae5d569b
SM
245 git check-attr --cached --stdin --all <stdin-all >tmp &&
246 sort tmp >actual &&
1c5e94f4 247 test_must_be_empty actual &&
b2b3e9c2 248 git add .gitattributes a/.gitattributes a/b/.gitattributes &&
ae5d569b
SM
249 git check-attr --cached --stdin --all <stdin-all >tmp &&
250 sort tmp >actual &&
b2b3e9c2 251 test_cmp specified-all actual
4ca0f188
MH
252'
253
82881b38 254test_expect_success 'root subdir attribute test' '
82881b38
MO
255 attr_check a/i a/i &&
256 attr_check subdir/a/i unspecified
82881b38
MO
257'
258
82dce998
NTND
259test_expect_success 'negative patterns' '
260 echo "!f test=bar" >.gitattributes &&
8b1bd024
TR
261 git check-attr test -- '"'"'!f'"'"' 2>errors &&
262 test_i18ngrep "Negative patterns are ignored" errors
82dce998
NTND
263'
264
265test_expect_success 'patterns starting with exclamation' '
266 echo "\!f test=foo" >.gitattributes &&
267 attr_check "!f" foo
268'
269
237ec6e4
NTND
270test_expect_success '"**" test' '
271 echo "**/f foo=bar" >.gitattributes &&
272 cat <<\EOF >expect &&
273f: foo: bar
274a/f: foo: bar
275a/b/f: foo: bar
276a/b/c/f: foo: bar
277EOF
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 &&
99c049bc 283 test_must_be_empty err
237ec6e4
NTND
284'
285
286test_expect_success '"**" with no slashes test' '
287 echo "a**f foo=bar" >.gitattributes &&
288 git check-attr foo -- "f" >actual &&
289 cat <<\EOF >expect &&
290f: foo: unspecified
291af: foo: bar
292axf: foo: bar
293a/f: foo: unspecified
294a/b/f: foo: unspecified
295a/b/c/f: foo: unspecified
296EOF
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 &&
99c049bc 304 test_must_be_empty err
237ec6e4
NTND
305'
306
cdbf6232
JH
307test_expect_success 'using --git-dir and --work-tree' '
308 mkdir unreal real &&
309 git init real &&
310 echo "file test=in-real" >real/.gitattributes &&
311 (
312 cd unreal &&
313 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
314 )
315'
316
47cfc9bd
KN
317test_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
324'
325
2d35d556 326test_expect_success 'setup bare' '
8da0b02d 327 git clone --template= --bare . bare.git
2d35d556
RS
328'
329
330test_expect_success 'bare repository: check that .gitattribute is ignored' '
2d35d556 331 (
c4a7bce1
JH
332 cd bare.git &&
333 (
75651fd7 334 echo "f test=f" &&
c4a7bce1
JH
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
342 )
2d35d556
RS
343'
344
47cfc9bd
KN
345test_expect_success 'bare repository: with --source' '
346 (
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
354 )
355'
356
b2b3e9c2 357test_expect_success 'bare repository: check that --cached honors index' '
c4a7bce1
JH
358 (
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
364 )
b2b3e9c2
JS
365'
366
2d35d556 367test_expect_success 'bare repository: test info/attributes' '
2d35d556 368 (
c4a7bce1 369 cd bare.git &&
8da0b02d 370 mkdir info &&
c4a7bce1 371 (
75651fd7 372 echo "f test=f" &&
c4a7bce1
JH
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
380 )
2d35d556
RS
381'
382
7b95849b
JK
383test_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
390 EOF
391 git check-attr -a file >actual &&
392 test_cmp expect actual
393'
394
7b95849b
JK
395test_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
400'
401
2ef579e2
JK
402test_expect_success SYMLINKS 'set up symlink tests' '
403 echo "* test" >attr &&
404 rm -f .gitattributes
405'
406
407test_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
412'
413
414test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
415 test_when_finished "rm .git/info/attributes" &&
8da0b02d 416 mkdir .git/info &&
2ef579e2
JK
417 ln -s ../../attr .git/info/attributes &&
418 attr_check file set
419'
420
421test_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
428'
429
dfa6b32b
PS
430test_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
437'
438
d74b1fd5
PS
439test_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 &&
dfa6b32b
PS
447 echo "warning: ignoring overly long attributes line 1" >expect &&
448 test_cmp expect err &&
449 test_must_be_empty actual
450'
451
3c50032f
PS
452test_expect_success EXPENSIVE 'large attributes file ignored in tree' '
453 test_when_finished "rm .gitattributes" &&
5458ba0a 454 dd if=/dev/zero of=.gitattributes bs=1048576 count=101 2>/dev/null &&
3c50032f
PS
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
458'
459
dfa6b32b
PS
460test_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 &&
d74b1fd5
PS
467 test_must_be_empty actual
468'
469
470test_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 &&
dfa6b32b
PS
475 echo "warning: ignoring overly long attributes line 1" >expect &&
476 test_cmp expect err &&
d74b1fd5
PS
477 test_must_be_empty actual
478'
479
3c50032f
PS
480test_expect_success EXPENSIVE 'large attributes file ignored in index' '
481 test_when_finished "git update-index --remove .gitattributes" &&
5458ba0a 482 blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) &&
3c50032f
PS
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
487'
488
cf94ccda 489test_done