config: unify code paths to get global config paths
[git] / t / t6135-pathspec-with-attrs.sh
1 #!/bin/sh
2
3 test_description='test labels in pathspecs'
4 . ./test-lib.sh
5
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
33         do
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
40 '
41
42 test_expect_success 'pathspec with no attr' '
43         test_must_fail git ls-files ":(attr:)"
44 '
45
46 test_expect_success 'pathspec with labels and non existent .gitattributes' '
47         git ls-files ":(attr:label)" >actual &&
48         test_must_be_empty actual
49 '
50
51 test_expect_success 'pathspec with labels and non existent .gitattributes (2)' '
52         test_must_fail git grep content HEAD -- ":(attr:label)"
53 '
54
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         EOF
68         git add .gitattributes &&
69         git commit -m "add attributes"
70 '
71
72 test_expect_success 'check specific set attr' '
73         cat <<-\EOF >expect &&
74         fileSetLabel
75         sub/fileSetLabel
76         EOF
77         git ls-files ":(attr:label)" >actual &&
78         test_cmp expect actual
79 '
80
81 test_expect_success 'check specific set attr (2)' '
82         cat <<-\EOF >expect &&
83         HEAD:fileSetLabel
84         HEAD:sub/fileSetLabel
85         EOF
86         git grep -l content HEAD ":(attr:label)" >actual &&
87         test_cmp expect actual
88 '
89
90 test_expect_success 'check specific unset attr' '
91         cat <<-\EOF >expect &&
92         fileUnsetLabel
93         sub/fileUnsetLabel
94         EOF
95         git ls-files ":(attr:-label)" >actual &&
96         test_cmp expect actual
97 '
98
99 test_expect_success 'check specific unset attr (2)' '
100         cat <<-\EOF >expect &&
101         HEAD:fileUnsetLabel
102         HEAD:sub/fileUnsetLabel
103         EOF
104         git grep -l content HEAD ":(attr:-label)" >actual &&
105         test_cmp expect actual
106 '
107
108 test_expect_success 'check specific value attr' '
109         cat <<-\EOF >expect &&
110         fileValue
111         sub/fileValue
112         EOF
113         git ls-files ":(attr:label=foo)" >actual &&
114         test_cmp expect actual &&
115         git ls-files ":(attr:label=bar)" >actual &&
116         test_must_be_empty actual
117 '
118
119 test_expect_success 'check specific value attr (2)' '
120         cat <<-\EOF >expect &&
121         HEAD:fileValue
122         HEAD:sub/fileValue
123         EOF
124         git grep -l content HEAD ":(attr:label=foo)" >actual &&
125         test_cmp expect actual &&
126         test_must_fail git grep -l content HEAD ":(attr:label=bar)"
127 '
128
129 test_expect_success 'check unspecified attr' '
130         cat <<-\EOF >expect &&
131         .gitattributes
132         fileA
133         fileAB
134         fileAC
135         fileB
136         fileBC
137         fileC
138         fileNoLabel
139         fileWrongLabel
140         sub/fileA
141         sub/fileAB
142         sub/fileAC
143         sub/fileB
144         sub/fileBC
145         sub/fileC
146         sub/fileNoLabel
147         sub/fileWrongLabel
148         EOF
149         git ls-files ":(attr:!label)" >actual &&
150         test_cmp expect actual
151 '
152
153 test_expect_success 'check unspecified attr (2)' '
154         cat <<-\EOF >expect &&
155         HEAD:.gitattributes
156         HEAD:fileA
157         HEAD:fileAB
158         HEAD:fileAC
159         HEAD:fileB
160         HEAD:fileBC
161         HEAD:fileC
162         HEAD:fileNoLabel
163         HEAD:fileWrongLabel
164         HEAD:sub/fileA
165         HEAD:sub/fileAB
166         HEAD:sub/fileAC
167         HEAD:sub/fileB
168         HEAD:sub/fileBC
169         HEAD:sub/fileC
170         HEAD:sub/fileNoLabel
171         HEAD:sub/fileWrongLabel
172         EOF
173         git grep -l ^ HEAD ":(attr:!label)" >actual &&
174         test_cmp expect actual
175 '
176
177 test_expect_success 'check multiple unspecified attr' '
178         cat <<-\EOF >expect &&
179         .gitattributes
180         fileC
181         fileNoLabel
182         fileWrongLabel
183         sub/fileC
184         sub/fileNoLabel
185         sub/fileWrongLabel
186         EOF
187         git ls-files ":(attr:!labelB !labelA !label)" >actual &&
188         test_cmp expect actual
189 '
190
191 test_expect_success 'check label with more labels but excluded path' '
192         cat <<-\EOF >expect &&
193         fileAB
194         fileB
195         fileBC
196         EOF
197         git ls-files ":(attr:labelB)" ":(exclude)sub/" >actual &&
198         test_cmp expect actual
199 '
200
201 test_expect_success 'check label excluding other labels' '
202         cat <<-\EOF >expect &&
203         fileAB
204         fileB
205         fileBC
206         sub/fileAB
207         sub/fileB
208         EOF
209         git ls-files ":(attr:labelB)" ":(exclude,attr:labelC)sub/" >actual &&
210         test_cmp expect actual
211 '
212
213 test_expect_success 'fail on multiple attr specifiers in one pathspec item' '
214         test_must_fail git ls-files . ":(attr:labelB,attr:labelC)" 2>actual &&
215         test_i18ngrep "Only one" actual
216 '
217
218 test_expect_success 'fail if attr magic is used places not implemented' '
219         # The main purpose of this test is to check that we actually fail
220         # when you attempt to use attr magic in commands that do not implement
221         # attr magic. This test does not advocate git-add to stay that way,
222         # though, but git-add is convenient as it has its own internal pathspec
223         # parsing.
224         test_must_fail git add ":(attr:labelB)" 2>actual &&
225         test_i18ngrep "magic not supported" actual
226 '
227
228 test_expect_success 'abort on giving invalid label on the command line' '
229         test_must_fail git ls-files . ":(attr:☺)"
230 '
231
232 test_expect_success 'abort on asking for wrong magic' '
233         test_must_fail git ls-files . ":(attr:-label=foo)" &&
234         test_must_fail git ls-files . ":(attr:!label=foo)"
235 '
236
237 test_expect_success 'check attribute list' '
238         cat <<-EOF >>.gitattributes &&
239         * whitespace=indent,trail,space
240         EOF
241         git ls-files ":(attr:whitespace=indent\,trail\,space)" >actual &&
242         git ls-files >expect &&
243         test_cmp expect actual
244 '
245
246 test_expect_success 'backslash cannot be the last character' '
247         test_must_fail git ls-files ":(attr:label=foo\\ labelA=bar)" 2>actual &&
248         test_i18ngrep "not allowed as last character in attr value" actual
249 '
250
251 test_expect_success 'backslash cannot be used as a value' '
252         test_must_fail git ls-files ":(attr:label=f\\\oo)" 2>actual &&
253         test_i18ngrep "for value matching" actual
254 '
255
256 test_done