summaryrefslogtreecommitdiffstats
path: root/t/t0003-attributes.sh
blob: aee2298f01331acdf3d73c1657f1a29ed1c8616b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/bin/sh

test_description=gitattributes

TEST_PASSES_SANITIZE_LEAK=true
TEST_CREATE_REPO_NO_TEMPLATE=1
. ./test-lib.sh

attr_check_basic () {
	path="$1" expect="$2" git_opts="$3" &&

	git $git_opts check-attr test -- "$path" >actual 2>err &&
	echo "$path: test: $expect" >expect &&
	test_cmp expect actual
}

attr_check () {
	attr_check_basic "$@" &&
	test_must_be_empty err
}

attr_check_quote () {
	path="$1" quoted_path="$2" expect="$3" &&

	git check-attr test -- "$path" >actual &&
	echo "\"$quoted_path\": test: $expect" >expect &&
	test_cmp expect actual
}

attr_check_source () {
	path="$1" expect="$2" source="$3" git_opts="$4" &&

	echo "$path: test: $expect" >expect &&

	git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
	test_cmp expect actual &&
	test_must_be_empty err &&

	git $git_opts --attr-source="$source" check-attr test -- "$path" >actual 2>err &&
	test_cmp expect actual &&
	test_must_be_empty err

	git $git_opts -c "attr.tree=$source" check-attr test -- "$path" >actual 2>err &&
	test_cmp expect actual &&
	test_must_be_empty err

	GIT_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
	test_cmp expect actual &&
	test_must_be_empty err
}

test_expect_success 'open-quoted pathname' '
	echo "\"a test=a" >.gitattributes &&
	attr_check a unspecified
'

test_expect_success 'setup' '
	mkdir -p a/b/d a/c b &&
	(
		echo "[attr]notest !test" &&
		echo "\" d \"	test=d" &&
		echo " e	test=e" &&
		echo " e\"	test=e" &&
		echo "f	test=f" &&
		echo "a/i test=a/i" &&
		echo "onoff test -test" &&
		echo "offon -test test" &&
		echo "no notest" &&
		echo "A/e/F test=A/e/F"
	) >.gitattributes &&
	(
		echo "g test=a/g" &&
		echo "b/g test=a/b/g"
	) >a/.gitattributes &&
	(
		echo "h test=a/b/h" &&
		echo "d/* test=a/b/d/*" &&
		echo "d/yes notest"
	) >a/b/.gitattributes &&
	(
		echo "global test=global"
	) >"$HOME"/global-gitattributes &&
	cat <<-EOF >expect-all
	f: test: f
	a/f: test: f
	a/c/f: test: f
	a/g: test: a/g
	a/b/g: test: a/b/g
	b/g: test: unspecified
	a/b/h: test: a/b/h
	a/b/d/g: test: a/b/d/*
	onoff: test: unset
	offon: test: set
	no: notest: set
	no: test: unspecified
	a/b/d/no: notest: set
	a/b/d/no: test: a/b/d/*
	a/b/d/yes: notest: set
	a/b/d/yes: test: unspecified
	EOF
'

test_expect_success 'setup branches' '
	mkdir -p foo/bar &&
	test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
		"f test=f\na/i test=n\n" tag-1 &&
	test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
		"g test=g\na/i test=m\n" tag-2 &&
	rm foo/bar/.gitattributes
'

test_expect_success 'command line checks' '
	test_must_fail git check-attr &&
	test_must_fail git check-attr -- &&
	test_must_fail git check-attr test &&
	test_must_fail git check-attr test -- &&
	test_must_fail git check-attr -- f &&
	test_must_fail git check-attr --source &&
	test_must_fail git check-attr --source not-a-valid-ref &&
	echo "f" | test_must_fail git check-attr --stdin &&
	echo "f" | test_must_fail git check-attr --stdin -- f &&
	echo "f" | test_must_fail git check-attr --stdin test -- f &&
	test_must_fail git check-attr "" -- f
'

test_expect_success 'attribute test' '

	attr_check " d " d &&
	attr_check e e &&
	attr_check_quote e\" e\\\" e &&

	attr_check f f &&
	attr_check a/f f &&
	attr_check a/c/f f &&
	attr_check a/g a/g &&
	attr_check a/b/g a/b/g &&
	attr_check b/g unspecified &&
	attr_check a/b/h a/b/h &&
	attr_check a/b/d/g "a/b/d/*" &&
	attr_check onoff unset &&
	attr_check offon set &&
	attr_check no unspecified &&
	attr_check a/b/d/no "a/b/d/*" &&
	attr_check a/b/d/yes unspecified
'

test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '

	attr_check F unspecified "-c core.ignorecase=0" &&
	attr_check a/F unspecified "-c core.ignorecase=0" &&
	attr_check a/c/F unspecified "-c core.ignorecase=0" &&
	attr_check a/G unspecified "-c core.ignorecase=0" &&
	attr_check a/B/g a/g "-c core.ignorecase=0" &&
	attr_check a/b/G unspecified "-c core.ignorecase=0" &&
	attr_check a/b/H unspecified "-c core.ignorecase=0" &&
	attr_check a/b/D/g a/g "-c core.ignorecase=0" &&
	attr_check oNoFf unspecified "-c core.ignorecase=0" &&
	attr_check oFfOn unspecified "-c core.ignorecase=0" &&
	attr_check NO unspecified "-c core.ignorecase=0" &&
	attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
	attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
	attr_check a/E/f f "-c core.ignorecase=0"

'

test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '

	attr_check F f "-c core.ignorecase=1" &&
	attr_check a/F f "-c core.ignorecase=1" &&
	attr_check a/c/F f "-c core.ignorecase=1" &&
	attr_check a/G a/g "-c core.ignorecase=1" &&
	attr_check a/B/g a/b/g "-c core.ignorecase=1" &&
	attr_check a/b/G a/b/g "-c core.ignorecase=1" &&
	attr_check a/b/H a/b/h "-c core.ignorecase=1" &&
	attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=1" &&
	attr_check oNoFf unset "-c core.ignorecase=1" &&
	attr_check oFfOn set "-c core.ignorecase=1" &&
	attr_check NO unspecified "-c core.ignorecase=1" &&
	attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=1" &&
	attr_check a/b/d/YES unspecified "-c core.ignorecase=1" &&
	attr_check a/E/f "A/e/F" "-c core.ignorecase=1"

'

test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
	attr_check a/B/D/g a/g "-c core.ignorecase=0" &&
	attr_check A/B/D/NO unspecified "-c core.ignorecase=0" &&
	attr_check A/b/h a/b/h "-c core.ignorecase=1" &&
	attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=1" &&
	attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=1"
'

test_expect_success 'unnormalized paths' '
	attr_check ./f f &&
	attr_check ./a/g a/g &&
	attr_check a/./g a/g &&
	attr_check a/c/../b/g a/b/g
'

test_expect_success 'relative paths' '
	(cd a && attr_check ../f f) &&
	(cd a && attr_check f f) &&
	(cd a && attr_check i a/i) &&
	(cd a && attr_check g a/g) &&
	(cd a && attr_check b/g a/b/g) &&
	(cd b && attr_check ../a/f f) &&
	(cd b && attr_check ../a/g a/g) &&
	(cd b && attr_check ../a/b/g a/b/g)
'

test_expect_success 'prefixes are not confused with leading directories' '
	attr_check a_plus/g unspecified &&
	cat >expect <<-\EOF &&
	a/g: test: a/g
	a_plus/g: test: unspecified
	EOF
	git check-attr test a/g a_plus/g >actual &&
	test_cmp expect actual
'

test_expect_success 'core.attributesfile' '
	attr_check global unspecified &&
	git config core.attributesfile "$HOME/global-gitattributes" &&
	attr_check global global &&
	git config core.attributesfile "~/global-gitattributes" &&
	attr_check global global &&
	echo "global test=precedence" >>.gitattributes &&
	attr_check global precedence
'

test_expect_success 'attribute test: read paths from stdin' '
	grep -v notest <expect-all >expect &&
	sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
	test_cmp expect actual
'

test_expect_success 'setup --all option' '
	grep -v unspecified <expect-all | sort >specified-all &&
	sed -e "s/:.*//" <expect-all | uniq >stdin-all
'

test_expect_success 'attribute test: --all option' '
	git check-attr --stdin --all <stdin-all >tmp &&
	sort tmp >actual &&
	test_cmp specified-all actual
'

test_expect_success 'attribute test: --cached option' '
	git check-attr --cached --stdin --all <stdin-all >tmp &&
	sort tmp >actual &&
	test_must_be_empty actual &&
	git add .gitattributes a/.gitattributes a/b/.gitattributes &&
	git check-attr --cached --stdin --all <stdin-all >tmp &&
	sort tmp >actual &&
	test_cmp specified-all actual
'

test_expect_success 'root subdir attribute test' '
	attr_check a/i a/i &&
	attr_check subdir/a/i unspecified
'

test_expect_success 'negative patterns' '
	echo "!f test=bar" >.gitattributes &&
	git check-attr test -- '"'"'!f'"'"' 2>errors &&
	test_grep "Negative patterns are ignored" errors
'

test_expect_success 'patterns starting with exclamation' '
	echo "\!f test=foo" >.gitattributes &&
	attr_check "!f" foo
'

test_expect_success '"**" test' '
	echo "**/f foo=bar" >.gitattributes &&
	cat <<\EOF >expect &&
f: foo: bar
a/f: foo: bar
a/b/f: foo: bar
a/b/c/f: foo: bar
EOF
	git check-attr foo -- "f" >actual 2>err &&
	git check-attr foo -- "a/f" >>actual 2>>err &&
	git check-attr foo -- "a/b/f" >>actual 2>>err &&
	git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
	test_cmp expect actual &&
	test_must_be_empty err
'

test_expect_success '"**" with no slashes test' '
	echo "a**f foo=bar" >.gitattributes &&
	git check-attr foo -- "f" >actual &&
	cat <<\EOF >expect &&
f: foo: unspecified
af: foo: bar
axf: foo: bar
a/f: foo: unspecified
a/b/f: foo: unspecified
a/b/c/f: foo: unspecified
EOF
	git check-attr foo -- "f" >actual 2>err &&
	git check-attr foo -- "af" >>actual 2>err &&
	git check-attr foo -- "axf" >>actual 2>err &&
	git check-attr foo -- "a/f" >>actual 2>>err &&
	git check-attr foo -- "a/b/f" >>actual 2>>err &&
	git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
	test_cmp expect actual &&
	test_must_be_empty err
'

test_expect_success 'using --git-dir and --work-tree' '
	mkdir unreal real &&
	git init real &&
	echo "file test=in-real" >real/.gitattributes &&
	(
		cd unreal &&
		attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
	)
'

test_expect_success 'using --source' '
	attr_check_source foo/bar/f f tag-1 &&
	attr_check_source foo/bar/a/i n tag-1 &&
	attr_check_source foo/bar/f unspecified tag-2 &&
	attr_check_source foo/bar/a/i m tag-2 &&
	attr_check_source foo/bar/g g tag-2 &&
	attr_check_source foo/bar/g unspecified tag-1
'

test_expect_success 'setup bare' '
	git clone --template= --bare . bare.git
'

test_expect_success 'bare repository: check that .gitattribute is ignored' '
	(
		cd bare.git &&
		(
			echo "f	test=f" &&
			echo "a/i test=a/i"
		) >.gitattributes &&
		attr_check f unspecified &&
		attr_check a/f unspecified &&
		attr_check a/c/f unspecified &&
		attr_check a/i unspecified &&
		attr_check subdir/a/i unspecified
	)
'

bad_attr_source_err="fatal: bad --attr-source or GIT_ATTR_SOURCE"

test_expect_success '--attr-source is bad' '
	test_when_finished rm -rf empty &&
	git init empty &&
	(
		cd empty &&
		echo "$bad_attr_source_err" >expect_err &&
		test_must_fail git --attr-source=HEAD check-attr test -- f/path 2>err &&
		test_cmp expect_err err
	)
'

test_expect_success 'attr.tree when HEAD is unborn' '
	test_when_finished rm -rf empty &&
	git init empty &&
	(
		cd empty &&
		echo "f/path: test: unspecified" >expect &&
		git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
		test_must_be_empty err &&
		test_cmp expect actual
	)
'

test_expect_success 'bad attr source defaults to reading .gitattributes file' '
	test_when_finished rm -rf empty &&
	git init empty &&
	(
		cd empty &&
		echo "f/path test=val" >.gitattributes &&
		echo "f/path: test: val" >expect &&
		git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
		test_must_be_empty err &&
		test_cmp expect actual
	)
'

test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
	test_when_finished rm -rf test bare_with_gitattribute &&
	git init test &&
	test_commit -C test gitattributes .gitattributes "f/path test=val" &&
	git clone --bare test bare_with_gitattribute &&
	echo "f/path: test: val" >expect &&
	git -C bare_with_gitattribute check-attr test -- f/path >actual &&
	test_cmp expect actual
'

test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tree' '
	test_when_finished rm -rf empty &&
	git init empty &&
	(
		cd empty &&
		git checkout -b attr-source &&
		test_commit "val1" .gitattributes "f/path test=val1" &&
		git checkout -b attr-tree &&
		test_commit "val2" .gitattributes "f/path test=val2" &&
		git checkout attr-source &&
		echo "f/path: test: val1" >expect &&
		GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree --attr-source=attr-source \
		check-attr test -- f/path >actual &&
		test_cmp expect actual &&
		GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree \
		check-attr test -- f/path >actual &&
		test_cmp expect actual
	)
'

test_expect_success 'bare repository: with --source' '
	(
		cd bare.git &&
		attr_check_source foo/bar/f f tag-1 &&
		attr_check_source foo/bar/a/i n tag-1 &&
		attr_check_source foo/bar/f unspecified tag-2 &&
		attr_check_source foo/bar/a/i m tag-2 &&
		attr_check_source foo/bar/g g tag-2 &&
		attr_check_source foo/bar/g unspecified tag-1
	)
'

test_expect_success 'bare repository: check that --cached honors index' '
	(
		cd bare.git &&
		GIT_INDEX_FILE=../.git/index \
		git check-attr --cached --stdin --all <../stdin-all |
		sort >actual &&
		test_cmp ../specified-all actual
	)
'

test_expect_success 'bare repository: test info/attributes' '
	(
		cd bare.git &&
		mkdir info &&
		(
			echo "f	test=f" &&
			echo "a/i test=a/i"
		) >info/attributes &&
		attr_check f f &&
		attr_check a/f f &&
		attr_check a/c/f f &&
		attr_check a/i a/i &&
		attr_check subdir/a/i unspecified
	)
'

test_expect_success 'binary macro expanded by -a' '
	echo "file binary" >.gitattributes &&
	cat >expect <<-\EOF &&
	file: binary: set
	file: diff: unset
	file: merge: unset
	file: text: unset
	EOF
	git check-attr -a file >actual &&
	test_cmp expect actual
'

test_expect_success 'query binary macro directly' '
	echo "file binary" >.gitattributes &&
	echo file: binary: set >expect &&
	git check-attr binary file >actual &&
	test_cmp expect actual
'

test_expect_success SYMLINKS 'set up symlink tests' '
	echo "* test" >attr &&
	rm -f .gitattributes
'

test_expect_success SYMLINKS 'symlinks respected in core.attributesFile' '
	test_when_finished "rm symlink" &&
	ln -s attr symlink &&
	test_config core.attributesFile "$(pwd)/symlink" &&
	attr_check file set
'

test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
	test_when_finished "rm .git/info/attributes" &&
	mkdir .git/info &&
	ln -s ../../attr .git/info/attributes &&
	attr_check file set
'

test_expect_success SYMLINKS 'symlinks not respected in-tree' '
	test_when_finished "rm -rf .gitattributes subdir" &&
	ln -s attr .gitattributes &&
	mkdir subdir &&
	ln -s ../attr subdir/.gitattributes &&
	attr_check_basic subdir/file unspecified &&
	test_grep "unable to access.*gitattributes" err
'

test_expect_success 'large attributes line ignored in tree' '
	test_when_finished "rm .gitattributes" &&
	printf "path %02043d" 1 >.gitattributes &&
	git check-attr --all path >actual 2>err &&
	echo "warning: ignoring overly long attributes line 1" >expect &&
	test_cmp expect err &&
	test_must_be_empty actual
'

test_expect_success 'large attributes line ignores trailing content in tree' '
	test_when_finished "rm .gitattributes" &&
	# older versions of Git broke lines at 2048 bytes; the 2045 bytes
	# of 0-padding here is accounting for the three bytes of "a 1", which
	# would knock "trailing" to the "next" line, where it would be
	# erroneously parsed.
	printf "a %02045dtrailing attribute\n" 1 >.gitattributes &&
	git check-attr --all trailing >actual 2>err &&
	echo "warning: ignoring overly long attributes line 1" >expect &&
	test_cmp expect err &&
	test_must_be_empty actual
'

test_expect_success EXPENSIVE 'large attributes file ignored in tree' '
	test_when_finished "rm .gitattributes" &&
	dd if=/dev/zero of=.gitattributes bs=1048576 count=101 2>/dev/null &&
	git check-attr --all path >/dev/null 2>err &&
	echo "warning: ignoring overly large gitattributes file ${SQ}.gitattributes${SQ}" >expect &&
	test_cmp expect err
'

test_expect_success 'large attributes line ignored in index' '
	test_when_finished "git update-index --remove .gitattributes" &&
	blob=$(printf "path %02043d" 1 | git hash-object -w --stdin) &&
	git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
	git check-attr --cached --all path >actual 2>err &&
	echo "warning: ignoring overly long attributes line 1" >expect &&
	test_cmp expect err &&
	test_must_be_empty actual
'

test_expect_success 'large attributes line ignores trailing content in index' '
	test_when_finished "git update-index --remove .gitattributes" &&
	blob=$(printf "a %02045dtrailing attribute\n" 1 | git hash-object -w --stdin) &&
	git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
	git check-attr --cached --all trailing >actual 2>err &&
	echo "warning: ignoring overly long attributes line 1" >expect &&
	test_cmp expect err &&
	test_must_be_empty actual
'

test_expect_success EXPENSIVE 'large attributes file ignored in index' '
	test_when_finished "git update-index --remove .gitattributes" &&
	blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) &&
	git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
	git check-attr --cached --all path >/dev/null 2>err &&
	echo "warning: ignoring overly large gitattributes blob ${SQ}.gitattributes${SQ}" >expect &&
	test_cmp expect err
'

test_done