]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6135-pathspec-with-attrs.sh
Sync with Git 2.45.1
[thirdparty/git.git] / t / t6135-pathspec-with-attrs.sh
CommitLineData
b0db7046
BW
1#!/bin/sh
2
3test_description='test labels in pathspecs'
4. ./test-lib.sh
5
6test_expect_success 'setup a tree' '
7 cat <<-\EOF >expect &&
8 fileA
9 fileAB
10 fileAC
11 fileB
12 fileBC
13 fileC
14 fileNoLabel
15 fileSetLabel
16 fileUnsetLabel
17 fileValue
18 fileWrongLabel
19 sub/fileA
20 sub/fileAB
21 sub/fileAC
22 sub/fileB
23 sub/fileBC
24 sub/fileC
25 sub/fileNoLabel
26 sub/fileSetLabel
27 sub/fileUnsetLabel
28 sub/fileValue
29 sub/fileWrongLabel
30 EOF
31 mkdir sub &&
32 while read path
33 do
5a0b97b3 34 echo content >$path &&
b0db7046
BW
35 git add $path || return 1
36 done <expect &&
37 git commit -m "initial commit" &&
38 git ls-files >actual &&
39 test_cmp expect actual
40'
41
42test_expect_success 'pathspec with no attr' '
43 test_must_fail git ls-files ":(attr:)"
44'
45
46test_expect_success 'pathspec with labels and non existent .gitattributes' '
47 git ls-files ":(attr:label)" >actual &&
48 test_must_be_empty actual
49'
50
5a0b97b3
NTND
51test_expect_success 'pathspec with labels and non existent .gitattributes (2)' '
52 test_must_fail git grep content HEAD -- ":(attr:label)"
53'
54
b0db7046
BW
55test_expect_success 'setup .gitattributes' '
56 cat <<-\EOF >.gitattributes &&
57 fileA labelA
58 fileB labelB
59 fileC labelC
60 fileAB labelA labelB
61 fileAC labelA labelC
62 fileBC labelB labelC
63 fileUnsetLabel -label
64 fileSetLabel label
65 fileValue label=foo
66 fileWrongLabel label☺
1164c723
JW
67 newFileA* labelA
68 newFileB* labelB
b0db7046 69 EOF
f4a8fde0
JH
70 echo fileSetLabel label1 >sub/.gitattributes &&
71 git add .gitattributes sub/.gitattributes &&
b0db7046
BW
72 git commit -m "add attributes"
73'
74
1164c723
JW
75test_expect_success 'setup .gitignore' '
76 cat <<-\EOF >.gitignore &&
77 actual
78 expect
79 pathspec_file
80 EOF
81 git add .gitignore &&
82 git commit -m "add gitignore"
83'
84
b0db7046
BW
85test_expect_success 'check specific set attr' '
86 cat <<-\EOF >expect &&
87 fileSetLabel
88 sub/fileSetLabel
89 EOF
90 git ls-files ":(attr:label)" >actual &&
91 test_cmp expect actual
92'
93
7e360bc6
JH
94test_expect_success 'check set attr with pathspec pattern' '
95 echo sub/fileSetLabel >expect &&
96
97 git ls-files ":(attr:label)sub" >actual &&
98 test_cmp expect actual &&
99
100 git ls-files ":(attr:label)sub/" >actual &&
101 test_cmp expect actual
102'
103
104test_expect_success 'check specific set attr in tree-ish' '
5a0b97b3
NTND
105 cat <<-\EOF >expect &&
106 HEAD:fileSetLabel
107 HEAD:sub/fileSetLabel
108 EOF
109 git grep -l content HEAD ":(attr:label)" >actual &&
110 test_cmp expect actual
111'
112
7e360bc6
JH
113test_expect_success 'check specific set attr with pathspec pattern in tree-ish' '
114 echo HEAD:sub/fileSetLabel >expect &&
115
116 git grep -l content HEAD ":(attr:label)sub" >actual &&
117 test_cmp expect actual &&
118
119 git grep -l content HEAD ":(attr:label)sub/" >actual &&
120 test_cmp expect actual
121'
122
b0db7046
BW
123test_expect_success 'check specific unset attr' '
124 cat <<-\EOF >expect &&
125 fileUnsetLabel
126 sub/fileUnsetLabel
127 EOF
128 git ls-files ":(attr:-label)" >actual &&
129 test_cmp expect actual
130'
131
5a0b97b3
NTND
132test_expect_success 'check specific unset attr (2)' '
133 cat <<-\EOF >expect &&
134 HEAD:fileUnsetLabel
135 HEAD:sub/fileUnsetLabel
136 EOF
137 git grep -l content HEAD ":(attr:-label)" >actual &&
138 test_cmp expect actual
139'
140
b0db7046
BW
141test_expect_success 'check specific value attr' '
142 cat <<-\EOF >expect &&
143 fileValue
144 sub/fileValue
145 EOF
146 git ls-files ":(attr:label=foo)" >actual &&
147 test_cmp expect actual &&
148 git ls-files ":(attr:label=bar)" >actual &&
149 test_must_be_empty actual
150'
151
5a0b97b3
NTND
152test_expect_success 'check specific value attr (2)' '
153 cat <<-\EOF >expect &&
154 HEAD:fileValue
155 HEAD:sub/fileValue
156 EOF
157 git grep -l content HEAD ":(attr:label=foo)" >actual &&
158 test_cmp expect actual &&
159 test_must_fail git grep -l content HEAD ":(attr:label=bar)"
160'
161
b0db7046
BW
162test_expect_success 'check unspecified attr' '
163 cat <<-\EOF >expect &&
164 .gitattributes
1164c723 165 .gitignore
b0db7046
BW
166 fileA
167 fileAB
168 fileAC
169 fileB
170 fileBC
171 fileC
172 fileNoLabel
173 fileWrongLabel
f4a8fde0 174 sub/.gitattributes
b0db7046
BW
175 sub/fileA
176 sub/fileAB
177 sub/fileAC
178 sub/fileB
179 sub/fileBC
180 sub/fileC
181 sub/fileNoLabel
182 sub/fileWrongLabel
183 EOF
184 git ls-files ":(attr:!label)" >actual &&
185 test_cmp expect actual
186'
187
5a0b97b3
NTND
188test_expect_success 'check unspecified attr (2)' '
189 cat <<-\EOF >expect &&
190 HEAD:.gitattributes
1164c723 191 HEAD:.gitignore
5a0b97b3
NTND
192 HEAD:fileA
193 HEAD:fileAB
194 HEAD:fileAC
195 HEAD:fileB
196 HEAD:fileBC
197 HEAD:fileC
198 HEAD:fileNoLabel
199 HEAD:fileWrongLabel
f4a8fde0 200 HEAD:sub/.gitattributes
5a0b97b3
NTND
201 HEAD:sub/fileA
202 HEAD:sub/fileAB
203 HEAD:sub/fileAC
204 HEAD:sub/fileB
205 HEAD:sub/fileBC
206 HEAD:sub/fileC
207 HEAD:sub/fileNoLabel
208 HEAD:sub/fileWrongLabel
209 EOF
210 git grep -l ^ HEAD ":(attr:!label)" >actual &&
211 test_cmp expect actual
212'
213
b0db7046
BW
214test_expect_success 'check multiple unspecified attr' '
215 cat <<-\EOF >expect &&
216 .gitattributes
1164c723 217 .gitignore
b0db7046
BW
218 fileC
219 fileNoLabel
220 fileWrongLabel
f4a8fde0 221 sub/.gitattributes
b0db7046
BW
222 sub/fileC
223 sub/fileNoLabel
224 sub/fileWrongLabel
225 EOF
226 git ls-files ":(attr:!labelB !labelA !label)" >actual &&
227 test_cmp expect actual
228'
229
230test_expect_success 'check label with more labels but excluded path' '
231 cat <<-\EOF >expect &&
232 fileAB
233 fileB
234 fileBC
235 EOF
236 git ls-files ":(attr:labelB)" ":(exclude)sub/" >actual &&
237 test_cmp expect actual
238'
239
240test_expect_success 'check label excluding other labels' '
241 cat <<-\EOF >expect &&
242 fileAB
243 fileB
244 fileBC
245 sub/fileAB
246 sub/fileB
247 EOF
248 git ls-files ":(attr:labelB)" ":(exclude,attr:labelC)sub/" >actual &&
249 test_cmp expect actual
250'
251
252test_expect_success 'fail on multiple attr specifiers in one pathspec item' '
253 test_must_fail git ls-files . ":(attr:labelB,attr:labelC)" 2>actual &&
6789275d 254 test_grep "Only one" actual
b0db7046
BW
255'
256
1164c723 257test_expect_success 'fail if attr magic is used in places not implemented' '
b0db7046
BW
258 # The main purpose of this test is to check that we actually fail
259 # when you attempt to use attr magic in commands that do not implement
1164c723
JW
260 # attr magic. This test does not advocate check-ignore to stay that way.
261 # When you teach the command to grok the pathspec, you need to find
262 # another command to replace it for the test.
263 test_must_fail git check-ignore ":(attr:labelB)" 2>actual &&
6789275d 264 test_grep "magic not supported" actual
b0db7046
BW
265'
266
1164c723
JW
267test_expect_success 'check that attr magic works for git stash push' '
268 cat <<-\EOF >expect &&
269 A sub/newFileA-foo
270 EOF
271 >sub/newFileA-foo &&
272 >sub/newFileB-foo &&
273 git stash push --include-untracked -- ":(exclude,attr:labelB)" &&
274 git stash show --include-untracked --name-status >actual &&
275 test_cmp expect actual
276'
277
278test_expect_success 'check that attr magic works for git add --all' '
279 cat <<-\EOF >expect &&
280 sub/newFileA-foo
281 EOF
282 >sub/newFileA-foo &&
283 >sub/newFileB-foo &&
284 git add --all ":(exclude,attr:labelB)" &&
285 git diff --name-only --cached >actual &&
286 git restore -W -S . &&
287 test_cmp expect actual
288'
289
290test_expect_success 'check that attr magic works for git add -u' '
291 cat <<-\EOF >expect &&
292 sub/fileA
293 EOF
294 >sub/newFileA-foo &&
295 >sub/newFileB-foo &&
296 >sub/fileA &&
297 >sub/fileB &&
298 git add -u ":(exclude,attr:labelB)" &&
299 git diff --name-only --cached >actual &&
300 git restore -S -W . && rm sub/new* &&
301 test_cmp expect actual
302'
303
304test_expect_success 'check that attr magic works for git add <path>' '
305 cat <<-\EOF >expect &&
306 fileA
307 fileB
308 sub/fileA
309 EOF
310 >fileA &&
311 >fileB &&
312 >sub/fileA &&
313 >sub/fileB &&
314 git add ":(exclude,attr:labelB)sub/*" &&
315 git diff --name-only --cached >actual &&
316 git restore -S -W . &&
317 test_cmp expect actual
318'
319
320test_expect_success 'check that attr magic works for git -add .' '
321 cat <<-\EOF >expect &&
322 sub/fileA
323 EOF
324 >fileA &&
325 >fileB &&
326 >sub/fileA &&
327 >sub/fileB &&
328 cd sub &&
329 git add . ":(exclude,attr:labelB)" &&
330 cd .. &&
331 git diff --name-only --cached >actual &&
332 git restore -S -W . &&
333 test_cmp expect actual
334'
335
336test_expect_success 'check that attr magic works for git add --pathspec-from-file' '
337 cat <<-\EOF >pathspec_file &&
338 :(exclude,attr:labelB)
339 EOF
340 cat <<-\EOF >expect &&
341 sub/newFileA-foo
342 EOF
343 >sub/newFileA-foo &&
344 >sub/newFileB-foo &&
345 git add --all --pathspec-from-file=pathspec_file &&
346 git diff --name-only --cached >actual &&
347 test_cmp expect actual
348'
349
b0db7046
BW
350test_expect_success 'abort on giving invalid label on the command line' '
351 test_must_fail git ls-files . ":(attr:☺)"
352'
353
354test_expect_success 'abort on asking for wrong magic' '
355 test_must_fail git ls-files . ":(attr:-label=foo)" &&
356 test_must_fail git ls-files . ":(attr:!label=foo)"
357'
358
c5af19f9
BW
359test_expect_success 'check attribute list' '
360 cat <<-EOF >>.gitattributes &&
361 * whitespace=indent,trail,space
362 EOF
363 git ls-files ":(attr:whitespace=indent\,trail\,space)" >actual &&
364 git ls-files >expect &&
365 test_cmp expect actual
366'
367
368test_expect_success 'backslash cannot be the last character' '
369 test_must_fail git ls-files ":(attr:label=foo\\ labelA=bar)" 2>actual &&
6789275d 370 test_grep "not allowed as last character in attr value" actual
c5af19f9
BW
371'
372
373test_expect_success 'backslash cannot be used as a value' '
374 test_must_fail git ls-files ":(attr:label=f\\\oo)" 2>actual &&
6789275d 375 test_grep "for value matching" actual
c5af19f9
BW
376'
377
f4a8fde0
JH
378test_expect_success 'reading from .gitattributes in a subdirectory (1)' '
379 git ls-files ":(attr:label1)" >actual &&
380 test_write_lines "sub/fileSetLabel" >expect &&
381 test_cmp expect actual
382'
383
384test_expect_success 'reading from .gitattributes in a subdirectory (2)' '
385 git ls-files ":(attr:label1)sub" >actual &&
386 test_write_lines "sub/fileSetLabel" >expect &&
387 test_cmp expect actual
388'
389
390test_expect_success 'reading from .gitattributes in a subdirectory (3)' '
391 git ls-files ":(attr:label1)sub/" >actual &&
392 test_write_lines "sub/fileSetLabel" >expect &&
393 test_cmp expect actual
394'
395
2232a88a
JW
396test_expect_success POSIXPERM 'pathspec with builtin_objectmode attr can be used' '
397 >mode_exec_file_1 &&
398
399 git status -s ":(attr:builtin_objectmode=100644)mode_exec_*" >actual &&
400 echo ?? mode_exec_file_1 >expect &&
401 test_cmp expect actual &&
402
403 git add mode_exec_file_1 &&
404 chmod +x mode_exec_file_1 &&
405 git status -s ":(attr:builtin_objectmode=100755)mode_exec_*" >actual &&
406 echo AM mode_exec_file_1 >expect &&
407 test_cmp expect actual
408'
409
410test_expect_success POSIXPERM 'builtin_objectmode attr can be excluded' '
411 >mode_1_regular &&
412 >mode_1_exec &&
413 chmod +x mode_1_exec &&
414 git status -s ":(exclude,attr:builtin_objectmode=100644)" "mode_1_*" >actual &&
415 echo ?? mode_1_exec >expect &&
416 test_cmp expect actual &&
417
418 git status -s ":(exclude,attr:builtin_objectmode=100755)" "mode_1_*" >actual &&
419 echo ?? mode_1_regular >expect &&
420 test_cmp expect actual
421'
422
b0db7046 423test_done