Merge branch 'rs/diff-parseopts-cleanup'
[git/gitster.git] / t / t6135-pathspec-with-attrs.sh
blob120dcd74a51434303b8bd7e71d10a9cb120bf574
1 #!/bin/sh
3 test_description='test labels in pathspecs'
4 . ./test-lib.sh
6 test_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
34 echo content >$path &&
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
42 test_expect_success 'pathspec with no attr' '
43 test_must_fail git ls-files ":(attr:)"
46 test_expect_success 'pathspec with labels and non existent .gitattributes' '
47 git ls-files ":(attr:label)" >actual &&
48 test_must_be_empty actual
51 test_expect_success 'pathspec with labels and non existent .gitattributes (2)' '
52 test_must_fail git grep content HEAD -- ":(attr:label)"
55 test_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☺
67 newFileA* labelA
68 newFileB* labelB
69 EOF
70 echo fileSetLabel label1 >sub/.gitattributes &&
71 git add .gitattributes sub/.gitattributes &&
72 git commit -m "add attributes"
75 test_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"
85 test_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
94 test_expect_success 'check set attr with pathspec pattern' '
95 echo sub/fileSetLabel >expect &&
97 git ls-files ":(attr:label)sub" >actual &&
98 test_cmp expect actual &&
100 git ls-files ":(attr:label)sub/" >actual &&
101 test_cmp expect actual
104 test_expect_success 'check specific set attr in tree-ish' '
105 cat <<-\EOF >expect &&
106 HEAD:fileSetLabel
107 HEAD:sub/fileSetLabel
109 git grep -l content HEAD ":(attr:label)" >actual &&
110 test_cmp expect actual
113 test_expect_success 'check specific set attr with pathspec pattern in tree-ish' '
114 echo HEAD:sub/fileSetLabel >expect &&
116 git grep -l content HEAD ":(attr:label)sub" >actual &&
117 test_cmp expect actual &&
119 git grep -l content HEAD ":(attr:label)sub/" >actual &&
120 test_cmp expect actual
123 test_expect_success 'check specific unset attr' '
124 cat <<-\EOF >expect &&
125 fileUnsetLabel
126 sub/fileUnsetLabel
128 git ls-files ":(attr:-label)" >actual &&
129 test_cmp expect actual
132 test_expect_success 'check specific unset attr (2)' '
133 cat <<-\EOF >expect &&
134 HEAD:fileUnsetLabel
135 HEAD:sub/fileUnsetLabel
137 git grep -l content HEAD ":(attr:-label)" >actual &&
138 test_cmp expect actual
141 test_expect_success 'check specific value attr' '
142 cat <<-\EOF >expect &&
143 fileValue
144 sub/fileValue
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
152 test_expect_success 'check specific value attr (2)' '
153 cat <<-\EOF >expect &&
154 HEAD:fileValue
155 HEAD:sub/fileValue
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)"
162 test_expect_success 'check unspecified attr' '
163 cat <<-\EOF >expect &&
164 .gitattributes
165 .gitignore
166 fileA
167 fileAB
168 fileAC
169 fileB
170 fileBC
171 fileC
172 fileNoLabel
173 fileWrongLabel
174 sub/.gitattributes
175 sub/fileA
176 sub/fileAB
177 sub/fileAC
178 sub/fileB
179 sub/fileBC
180 sub/fileC
181 sub/fileNoLabel
182 sub/fileWrongLabel
184 git ls-files ":(attr:!label)" >actual &&
185 test_cmp expect actual
188 test_expect_success 'check unspecified attr (2)' '
189 cat <<-\EOF >expect &&
190 HEAD:.gitattributes
191 HEAD:.gitignore
192 HEAD:fileA
193 HEAD:fileAB
194 HEAD:fileAC
195 HEAD:fileB
196 HEAD:fileBC
197 HEAD:fileC
198 HEAD:fileNoLabel
199 HEAD:fileWrongLabel
200 HEAD:sub/.gitattributes
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
210 git grep -l ^ HEAD ":(attr:!label)" >actual &&
211 test_cmp expect actual
214 test_expect_success 'check multiple unspecified attr' '
215 cat <<-\EOF >expect &&
216 .gitattributes
217 .gitignore
218 fileC
219 fileNoLabel
220 fileWrongLabel
221 sub/.gitattributes
222 sub/fileC
223 sub/fileNoLabel
224 sub/fileWrongLabel
226 git ls-files ":(attr:!labelB !labelA !label)" >actual &&
227 test_cmp expect actual
230 test_expect_success 'check label with more labels but excluded path' '
231 cat <<-\EOF >expect &&
232 fileAB
233 fileB
234 fileBC
236 git ls-files ":(attr:labelB)" ":(exclude)sub/" >actual &&
237 test_cmp expect actual
240 test_expect_success 'check label excluding other labels' '
241 cat <<-\EOF >expect &&
242 fileAB
243 fileB
244 fileBC
245 sub/fileAB
246 sub/fileB
248 git ls-files ":(attr:labelB)" ":(exclude,attr:labelC)sub/" >actual &&
249 test_cmp expect actual
252 test_expect_success 'fail on multiple attr specifiers in one pathspec item' '
253 test_must_fail git ls-files . ":(attr:labelB,attr:labelC)" 2>actual &&
254 test_grep "Only one" actual
257 test_expect_success 'fail if attr magic is used in places not implemented' '
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
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 &&
264 test_grep "magic not supported" actual
267 test_expect_success 'check that attr magic works for git stash push' '
268 cat <<-\EOF >expect &&
269 A sub/newFileA-foo
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
278 test_expect_success 'check that attr magic works for git add --all' '
279 cat <<-\EOF >expect &&
280 sub/newFileA-foo
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
290 test_expect_success 'check that attr magic works for git add -u' '
291 cat <<-\EOF >expect &&
292 sub/fileA
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
304 test_expect_success 'check that attr magic works for git add <path>' '
305 cat <<-\EOF >expect &&
306 fileA
307 fileB
308 sub/fileA
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
320 test_expect_success 'check that attr magic works for git -add .' '
321 cat <<-\EOF >expect &&
322 sub/fileA
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
336 test_expect_success 'check that attr magic works for git add --pathspec-from-file' '
337 cat <<-\EOF >pathspec_file &&
338 :(exclude,attr:labelB)
340 cat <<-\EOF >expect &&
341 sub/newFileA-foo
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
350 test_expect_success 'abort on giving invalid label on the command line' '
351 test_must_fail git ls-files . ":(attr:☺)"
354 test_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)"
359 test_expect_success 'check attribute list' '
360 cat <<-EOF >>.gitattributes &&
361 * whitespace=indent,trail,space
363 git ls-files ":(attr:whitespace=indent\,trail\,space)" >actual &&
364 git ls-files >expect &&
365 test_cmp expect actual
368 test_expect_success 'backslash cannot be the last character' '
369 test_must_fail git ls-files ":(attr:label=foo\\ labelA=bar)" 2>actual &&
370 test_grep "not allowed as last character in attr value" actual
373 test_expect_success 'backslash cannot be used as a value' '
374 test_must_fail git ls-files ":(attr:label=f\\\oo)" 2>actual &&
375 test_grep "for value matching" actual
378 test_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
384 test_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
390 test_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
396 test_expect_success POSIXPERM 'pathspec with builtin_objectmode attr can be used' '
397 >mode_exec_file_1 &&
399 git status -s ":(attr:builtin_objectmode=100644)mode_exec_*" >actual &&
400 echo ?? mode_exec_file_1 >expect &&
401 test_cmp expect actual &&
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
410 test_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 &&
418 git status -s ":(exclude,attr:builtin_objectmode=100755)" "mode_1_*" >actual &&
419 echo ?? mode_1_regular >expect &&
420 test_cmp expect actual
423 test_done