summaryrefslogtreecommitdiffstats
path: root/t/t6135-pathspec-with-attrs.sh
blob: 120dcd74a51434303b8bd7e71d10a9cb120bf574 (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
#!/bin/sh

test_description='test labels in pathspecs'
. ./test-lib.sh

test_expect_success 'setup a tree' '
	cat <<-\EOF >expect &&
	fileA
	fileAB
	fileAC
	fileB
	fileBC
	fileC
	fileNoLabel
	fileSetLabel
	fileUnsetLabel
	fileValue
	fileWrongLabel
	sub/fileA
	sub/fileAB
	sub/fileAC
	sub/fileB
	sub/fileBC
	sub/fileC
	sub/fileNoLabel
	sub/fileSetLabel
	sub/fileUnsetLabel
	sub/fileValue
	sub/fileWrongLabel
	EOF
	mkdir sub &&
	while read path
	do
		echo content >$path &&
		git add $path || return 1
	done <expect &&
	git commit -m "initial commit" &&
	git ls-files >actual &&
	test_cmp expect actual
'

test_expect_success 'pathspec with no attr' '
	test_must_fail git ls-files ":(attr:)"
'

test_expect_success 'pathspec with labels and non existent .gitattributes' '
	git ls-files ":(attr:label)" >actual &&
	test_must_be_empty actual
'

test_expect_success 'pathspec with labels and non existent .gitattributes (2)' '
	test_must_fail git grep content HEAD -- ":(attr:label)"
'

test_expect_success 'setup .gitattributes' '
	cat <<-\EOF >.gitattributes &&
	fileA labelA
	fileB labelB
	fileC labelC
	fileAB labelA labelB
	fileAC labelA labelC
	fileBC labelB labelC
	fileUnsetLabel -label
	fileSetLabel label
	fileValue label=foo
	fileWrongLabel label☺
	newFileA* labelA
	newFileB* labelB
	EOF
	echo fileSetLabel label1 >sub/.gitattributes &&
	git add .gitattributes sub/.gitattributes &&
	git commit -m "add attributes"
'

test_expect_success 'setup .gitignore' '
	cat <<-\EOF >.gitignore &&
	actual
	expect
	pathspec_file
	EOF
	git add .gitignore &&
	git commit -m "add gitignore"
'

test_expect_success 'check specific set attr' '
	cat <<-\EOF >expect &&
	fileSetLabel
	sub/fileSetLabel
	EOF
	git ls-files ":(attr:label)" >actual &&
	test_cmp expect actual
'

test_expect_success 'check set attr with pathspec pattern' '
	echo sub/fileSetLabel >expect &&

	git ls-files ":(attr:label)sub" >actual &&
	test_cmp expect actual &&

	git ls-files ":(attr:label)sub/" >actual &&
	test_cmp expect actual
'

test_expect_success 'check specific set attr in tree-ish' '
	cat <<-\EOF >expect &&
	HEAD:fileSetLabel
	HEAD:sub/fileSetLabel
	EOF
	git grep -l content HEAD ":(attr:label)" >actual &&
	test_cmp expect actual
'

test_expect_success 'check specific set attr with pathspec pattern in tree-ish' '
	echo HEAD:sub/fileSetLabel >expect &&

	git grep -l content HEAD ":(attr:label)sub" >actual &&
	test_cmp expect actual &&

	git grep -l content HEAD ":(attr:label)sub/" >actual &&
	test_cmp expect actual
'

test_expect_success 'check specific unset attr' '
	cat <<-\EOF >expect &&
	fileUnsetLabel
	sub/fileUnsetLabel
	EOF
	git ls-files ":(attr:-label)" >actual &&
	test_cmp expect actual
'

test_expect_success 'check specific unset attr (2)' '
	cat <<-\EOF >expect &&
	HEAD:fileUnsetLabel
	HEAD:sub/fileUnsetLabel
	EOF
	git grep -l content HEAD ":(attr:-label)" >actual &&
	test_cmp expect actual
'

test_expect_success 'check specific value attr' '
	cat <<-\EOF >expect &&
	fileValue
	sub/fileValue
	EOF
	git ls-files ":(attr:label=foo)" >actual &&
	test_cmp expect actual &&
	git ls-files ":(attr:label=bar)" >actual &&
	test_must_be_empty actual
'

test_expect_success 'check specific value attr (2)' '
	cat <<-\EOF >expect &&
	HEAD:fileValue
	HEAD:sub/fileValue
	EOF
	git grep -l content HEAD ":(attr:label=foo)" >actual &&
	test_cmp expect actual &&
	test_must_fail git grep -l content HEAD ":(attr:label=bar)"
'

test_expect_success 'check unspecified attr' '
	cat <<-\EOF >expect &&
	.gitattributes
	.gitignore
	fileA
	fileAB
	fileAC
	fileB
	fileBC
	fileC
	fileNoLabel
	fileWrongLabel
	sub/.gitattributes
	sub/fileA
	sub/fileAB
	sub/fileAC
	sub/fileB
	sub/fileBC
	sub/fileC
	sub/fileNoLabel
	sub/fileWrongLabel
	EOF
	git ls-files ":(attr:!label)" >actual &&
	test_cmp expect actual
'

test_expect_success 'check unspecified attr (2)' '
	cat <<-\EOF >expect &&
	HEAD:.gitattributes
	HEAD:.gitignore
	HEAD:fileA
	HEAD:fileAB
	HEAD:fileAC
	HEAD:fileB
	HEAD:fileBC
	HEAD:fileC
	HEAD:fileNoLabel
	HEAD:fileWrongLabel
	HEAD:sub/.gitattributes
	HEAD:sub/fileA
	HEAD:sub/fileAB
	HEAD:sub/fileAC
	HEAD:sub/fileB
	HEAD:sub/fileBC
	HEAD:sub/fileC
	HEAD:sub/fileNoLabel
	HEAD:sub/fileWrongLabel
	EOF
	git grep -l ^ HEAD ":(attr:!label)" >actual &&
	test_cmp expect actual
'

test_expect_success 'check multiple unspecified attr' '
	cat <<-\EOF >expect &&
	.gitattributes
	.gitignore
	fileC
	fileNoLabel
	fileWrongLabel
	sub/.gitattributes
	sub/fileC
	sub/fileNoLabel
	sub/fileWrongLabel
	EOF
	git ls-files ":(attr:!labelB !labelA !label)" >actual &&
	test_cmp expect actual
'

test_expect_success 'check label with more labels but excluded path' '
	cat <<-\EOF >expect &&
	fileAB
	fileB
	fileBC
	EOF
	git ls-files ":(attr:labelB)" ":(exclude)sub/" >actual &&
	test_cmp expect actual
'

test_expect_success 'check label excluding other labels' '
	cat <<-\EOF >expect &&
	fileAB
	fileB
	fileBC
	sub/fileAB
	sub/fileB
	EOF
	git ls-files ":(attr:labelB)" ":(exclude,attr:labelC)sub/" >actual &&
	test_cmp expect actual
'

test_expect_success 'fail on multiple attr specifiers in one pathspec item' '
	test_must_fail git ls-files . ":(attr:labelB,attr:labelC)" 2>actual &&
	test_grep "Only one" actual
'

test_expect_success 'fail if attr magic is used in places not implemented' '
	# The main purpose of this test is to check that we actually fail
	# when you attempt to use attr magic in commands that do not implement
	# attr magic. This test does not advocate check-ignore to stay that way.
	# When you teach the command to grok the pathspec, you need to find
	# another command to replace it for the test.
	test_must_fail git check-ignore ":(attr:labelB)" 2>actual &&
	test_grep "magic not supported" actual
'

test_expect_success 'check that attr magic works for git stash push' '
	cat <<-\EOF >expect &&
	A	sub/newFileA-foo
	EOF
	>sub/newFileA-foo &&
	>sub/newFileB-foo &&
	git stash push --include-untracked -- ":(exclude,attr:labelB)" &&
	git stash show --include-untracked --name-status >actual &&
	test_cmp expect actual
'

test_expect_success 'check that attr magic works for git add --all' '
	cat <<-\EOF >expect &&
	sub/newFileA-foo
	EOF
	>sub/newFileA-foo &&
	>sub/newFileB-foo &&
	git add --all ":(exclude,attr:labelB)" &&
	git diff --name-only --cached >actual &&
	git restore -W -S . &&
	test_cmp expect actual
'

test_expect_success 'check that attr magic works for git add -u' '
	cat <<-\EOF >expect &&
	sub/fileA
	EOF
	>sub/newFileA-foo &&
	>sub/newFileB-foo &&
	>sub/fileA &&
	>sub/fileB &&
	git add -u ":(exclude,attr:labelB)" &&
	git diff --name-only --cached  >actual &&
	git restore -S -W . && rm sub/new* &&
	test_cmp expect actual
'

test_expect_success 'check that attr magic works for git add <path>' '
	cat <<-\EOF >expect &&
	fileA
	fileB
	sub/fileA
	EOF
	>fileA &&
	>fileB &&
	>sub/fileA &&
	>sub/fileB &&
	git add ":(exclude,attr:labelB)sub/*" &&
	git diff --name-only --cached >actual &&
	git restore -S -W . &&
	test_cmp expect actual
'

test_expect_success 'check that attr magic works for git -add .' '
	cat <<-\EOF >expect &&
	sub/fileA
	EOF
	>fileA &&
	>fileB &&
	>sub/fileA &&
	>sub/fileB &&
	cd sub &&
	git add . ":(exclude,attr:labelB)" &&
	cd .. &&
	git diff --name-only --cached >actual &&
	git restore -S -W . &&
	test_cmp expect actual
'

test_expect_success 'check that attr magic works for git add --pathspec-from-file' '
	cat <<-\EOF >pathspec_file &&
	:(exclude,attr:labelB)
	EOF
	cat <<-\EOF >expect &&
	sub/newFileA-foo
	EOF
	>sub/newFileA-foo &&
	>sub/newFileB-foo &&
	git add --all --pathspec-from-file=pathspec_file &&
	git diff --name-only --cached >actual &&
	test_cmp expect actual
'

test_expect_success 'abort on giving invalid label on the command line' '
	test_must_fail git ls-files . ":(attr:☺)"
'

test_expect_success 'abort on asking for wrong magic' '
	test_must_fail git ls-files . ":(attr:-label=foo)" &&
	test_must_fail git ls-files . ":(attr:!label=foo)"
'

test_expect_success 'check attribute list' '
	cat <<-EOF >>.gitattributes &&
	* whitespace=indent,trail,space
	EOF
	git ls-files ":(attr:whitespace=indent\,trail\,space)" >actual &&
	git ls-files >expect &&
	test_cmp expect actual
'

test_expect_success 'backslash cannot be the last character' '
	test_must_fail git ls-files ":(attr:label=foo\\ labelA=bar)" 2>actual &&
	test_grep "not allowed as last character in attr value" actual
'

test_expect_success 'backslash cannot be used as a value' '
	test_must_fail git ls-files ":(attr:label=f\\\oo)" 2>actual &&
	test_grep "for value matching" actual
'

test_expect_success 'reading from .gitattributes in a subdirectory (1)' '
	git ls-files ":(attr:label1)" >actual &&
	test_write_lines "sub/fileSetLabel" >expect &&
	test_cmp expect actual
'

test_expect_success 'reading from .gitattributes in a subdirectory (2)' '
	git ls-files ":(attr:label1)sub" >actual &&
	test_write_lines "sub/fileSetLabel" >expect &&
	test_cmp expect actual
'

test_expect_success 'reading from .gitattributes in a subdirectory (3)' '
	git ls-files ":(attr:label1)sub/" >actual &&
	test_write_lines "sub/fileSetLabel" >expect &&
	test_cmp expect actual
'

test_expect_success POSIXPERM 'pathspec with builtin_objectmode attr can be used' '
	>mode_exec_file_1 &&

	git status -s ":(attr:builtin_objectmode=100644)mode_exec_*" >actual &&
	echo ?? mode_exec_file_1 >expect &&
	test_cmp expect actual &&

	git add mode_exec_file_1 &&
	chmod +x mode_exec_file_1 &&
	git status -s ":(attr:builtin_objectmode=100755)mode_exec_*" >actual &&
	echo AM mode_exec_file_1 >expect &&
	test_cmp expect actual
'

test_expect_success POSIXPERM 'builtin_objectmode attr can be excluded' '
	>mode_1_regular &&
	>mode_1_exec  &&
	chmod +x mode_1_exec &&
	git status -s ":(exclude,attr:builtin_objectmode=100644)" "mode_1_*" >actual &&
	echo ?? mode_1_exec >expect &&
	test_cmp expect actual &&

	git status -s ":(exclude,attr:builtin_objectmode=100755)" "mode_1_*" >actual &&
	echo ?? mode_1_regular >expect &&
	test_cmp expect actual
'

test_done