Makefile: add OPEN_RETURNS_EINTR knob
[git] / t / t0003-attributes.sh
1 #!/bin/sh
2
3 test_description=gitattributes
4
5 . ./test-lib.sh
6
7 attr_check () {
8         path="$1" expect="$2" git_opts="$3" &&
9
10         git $git_opts check-attr test -- "$path" >actual 2>err &&
11         echo "$path: test: $expect" >expect &&
12         test_cmp expect actual &&
13         test_must_be_empty err
14 }
15
16 attr_check_quote () {
17         path="$1" quoted_path="$2" expect="$3" &&
18
19         git check-attr test -- "$path" >actual &&
20         echo "\"$quoted_path\": test: $expect" >expect &&
21         test_cmp expect actual
22
23 }
24
25 test_expect_success 'open-quoted pathname' '
26         echo "\"a test=a" >.gitattributes &&
27         attr_check a unspecified
28 '
29
30
31 test_expect_success 'setup' '
32         mkdir -p a/b/d a/c b &&
33         (
34                 echo "[attr]notest !test" &&
35                 echo "\" d \"   test=d" &&
36                 echo " e        test=e" &&
37                 echo " e\"      test=e" &&
38                 echo "f test=f" &&
39                 echo "a/i test=a/i" &&
40                 echo "onoff test -test" &&
41                 echo "offon -test test" &&
42                 echo "no notest" &&
43                 echo "A/e/F test=A/e/F"
44         ) >.gitattributes &&
45         (
46                 echo "g test=a/g" &&
47                 echo "b/g test=a/b/g"
48         ) >a/.gitattributes &&
49         (
50                 echo "h test=a/b/h" &&
51                 echo "d/* test=a/b/d/*" &&
52                 echo "d/yes notest"
53         ) >a/b/.gitattributes &&
54         (
55                 echo "global test=global"
56         ) >"$HOME"/global-gitattributes &&
57         cat <<-EOF >expect-all
58         f: test: f
59         a/f: test: f
60         a/c/f: test: f
61         a/g: test: a/g
62         a/b/g: test: a/b/g
63         b/g: test: unspecified
64         a/b/h: test: a/b/h
65         a/b/d/g: test: a/b/d/*
66         onoff: test: unset
67         offon: test: set
68         no: notest: set
69         no: test: unspecified
70         a/b/d/no: notest: set
71         a/b/d/no: test: a/b/d/*
72         a/b/d/yes: notest: set
73         a/b/d/yes: test: unspecified
74         EOF
75 '
76
77 test_expect_success 'command line checks' '
78         test_must_fail git check-attr &&
79         test_must_fail git check-attr -- &&
80         test_must_fail git check-attr test &&
81         test_must_fail git check-attr test -- &&
82         test_must_fail git check-attr -- f &&
83         echo "f" | test_must_fail git check-attr --stdin &&
84         echo "f" | test_must_fail git check-attr --stdin -- f &&
85         echo "f" | test_must_fail git check-attr --stdin test -- f &&
86         test_must_fail git check-attr "" -- f
87 '
88
89 test_expect_success 'attribute test' '
90
91         attr_check " d " d &&
92         attr_check e e &&
93         attr_check_quote e\" e\\\" e &&
94
95         attr_check f f &&
96         attr_check a/f f &&
97         attr_check a/c/f f &&
98         attr_check a/g a/g &&
99         attr_check a/b/g a/b/g &&
100         attr_check b/g unspecified &&
101         attr_check a/b/h a/b/h &&
102         attr_check a/b/d/g "a/b/d/*" &&
103         attr_check onoff unset &&
104         attr_check offon set &&
105         attr_check no unspecified &&
106         attr_check a/b/d/no "a/b/d/*" &&
107         attr_check a/b/d/yes unspecified
108 '
109
110 test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
111
112         attr_check F unspecified "-c core.ignorecase=0" &&
113         attr_check a/F unspecified "-c core.ignorecase=0" &&
114         attr_check a/c/F unspecified "-c core.ignorecase=0" &&
115         attr_check a/G unspecified "-c core.ignorecase=0" &&
116         attr_check a/B/g a/g "-c core.ignorecase=0" &&
117         attr_check a/b/G unspecified "-c core.ignorecase=0" &&
118         attr_check a/b/H unspecified "-c core.ignorecase=0" &&
119         attr_check a/b/D/g a/g "-c core.ignorecase=0" &&
120         attr_check oNoFf unspecified "-c core.ignorecase=0" &&
121         attr_check oFfOn unspecified "-c core.ignorecase=0" &&
122         attr_check NO unspecified "-c core.ignorecase=0" &&
123         attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
124         attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
125         attr_check a/E/f f "-c core.ignorecase=0"
126
127 '
128
129 test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
130
131         attr_check F f "-c core.ignorecase=1" &&
132         attr_check a/F f "-c core.ignorecase=1" &&
133         attr_check a/c/F f "-c core.ignorecase=1" &&
134         attr_check a/G a/g "-c core.ignorecase=1" &&
135         attr_check a/B/g a/b/g "-c core.ignorecase=1" &&
136         attr_check a/b/G a/b/g "-c core.ignorecase=1" &&
137         attr_check a/b/H a/b/h "-c core.ignorecase=1" &&
138         attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=1" &&
139         attr_check oNoFf unset "-c core.ignorecase=1" &&
140         attr_check oFfOn set "-c core.ignorecase=1" &&
141         attr_check NO unspecified "-c core.ignorecase=1" &&
142         attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=1" &&
143         attr_check a/b/d/YES unspecified "-c core.ignorecase=1" &&
144         attr_check a/E/f "A/e/F" "-c core.ignorecase=1"
145
146 '
147
148 test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
149         attr_check a/B/D/g a/g "-c core.ignorecase=0" &&
150         attr_check A/B/D/NO unspecified "-c core.ignorecase=0" &&
151         attr_check A/b/h a/b/h "-c core.ignorecase=1" &&
152         attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=1" &&
153         attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=1"
154 '
155
156 test_expect_success 'unnormalized paths' '
157         attr_check ./f f &&
158         attr_check ./a/g a/g &&
159         attr_check a/./g a/g &&
160         attr_check a/c/../b/g a/b/g
161 '
162
163 test_expect_success 'relative paths' '
164         (cd a && attr_check ../f f) &&
165         (cd a && attr_check f f) &&
166         (cd a && attr_check i a/i) &&
167         (cd a && attr_check g a/g) &&
168         (cd a && attr_check b/g a/b/g) &&
169         (cd b && attr_check ../a/f f) &&
170         (cd b && attr_check ../a/g a/g) &&
171         (cd b && attr_check ../a/b/g a/b/g)
172 '
173
174 test_expect_success 'prefixes are not confused with leading directories' '
175         attr_check a_plus/g unspecified &&
176         cat >expect <<-\EOF &&
177         a/g: test: a/g
178         a_plus/g: test: unspecified
179         EOF
180         git check-attr test a/g a_plus/g >actual &&
181         test_cmp expect actual
182 '
183
184 test_expect_success 'core.attributesfile' '
185         attr_check global unspecified &&
186         git config core.attributesfile "$HOME/global-gitattributes" &&
187         attr_check global global &&
188         git config core.attributesfile "~/global-gitattributes" &&
189         attr_check global global &&
190         echo "global test=precedence" >>.gitattributes &&
191         attr_check global precedence
192 '
193
194 test_expect_success 'attribute test: read paths from stdin' '
195         grep -v notest <expect-all >expect &&
196         sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
197         test_cmp expect actual
198 '
199
200 test_expect_success 'attribute test: --all option' '
201         grep -v unspecified <expect-all | sort >specified-all &&
202         sed -e "s/:.*//" <expect-all | uniq >stdin-all &&
203         git check-attr --stdin --all <stdin-all | sort >actual &&
204         test_cmp specified-all actual
205 '
206
207 test_expect_success 'attribute test: --cached option' '
208         git check-attr --cached --stdin --all <stdin-all | sort >actual &&
209         test_must_be_empty actual &&
210         git add .gitattributes a/.gitattributes a/b/.gitattributes &&
211         git check-attr --cached --stdin --all <stdin-all | sort >actual &&
212         test_cmp specified-all actual
213 '
214
215 test_expect_success 'root subdir attribute test' '
216         attr_check a/i a/i &&
217         attr_check subdir/a/i unspecified
218 '
219
220 test_expect_success 'negative patterns' '
221         echo "!f test=bar" >.gitattributes &&
222         git check-attr test -- '"'"'!f'"'"' 2>errors &&
223         test_i18ngrep "Negative patterns are ignored" errors
224 '
225
226 test_expect_success 'patterns starting with exclamation' '
227         echo "\!f test=foo" >.gitattributes &&
228         attr_check "!f" foo
229 '
230
231 test_expect_success '"**" test' '
232         echo "**/f foo=bar" >.gitattributes &&
233         cat <<\EOF >expect &&
234 f: foo: bar
235 a/f: foo: bar
236 a/b/f: foo: bar
237 a/b/c/f: foo: bar
238 EOF
239         git check-attr foo -- "f" >actual 2>err &&
240         git check-attr foo -- "a/f" >>actual 2>>err &&
241         git check-attr foo -- "a/b/f" >>actual 2>>err &&
242         git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
243         test_cmp expect actual &&
244         test_must_be_empty err
245 '
246
247 test_expect_success '"**" with no slashes test' '
248         echo "a**f foo=bar" >.gitattributes &&
249         git check-attr foo -- "f" >actual &&
250         cat <<\EOF >expect &&
251 f: foo: unspecified
252 af: foo: bar
253 axf: foo: bar
254 a/f: foo: unspecified
255 a/b/f: foo: unspecified
256 a/b/c/f: foo: unspecified
257 EOF
258         git check-attr foo -- "f" >actual 2>err &&
259         git check-attr foo -- "af" >>actual 2>err &&
260         git check-attr foo -- "axf" >>actual 2>err &&
261         git check-attr foo -- "a/f" >>actual 2>>err &&
262         git check-attr foo -- "a/b/f" >>actual 2>>err &&
263         git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
264         test_cmp expect actual &&
265         test_must_be_empty err
266 '
267
268 test_expect_success 'using --git-dir and --work-tree' '
269         mkdir unreal real &&
270         git init real &&
271         echo "file test=in-real" >real/.gitattributes &&
272         (
273                 cd unreal &&
274                 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
275         )
276 '
277
278 test_expect_success 'setup bare' '
279         git clone --bare . bare.git
280 '
281
282 test_expect_success 'bare repository: check that .gitattribute is ignored' '
283         (
284                 cd bare.git &&
285                 (
286                         echo "f test=f" &&
287                         echo "a/i test=a/i"
288                 ) >.gitattributes &&
289                 attr_check f unspecified &&
290                 attr_check a/f unspecified &&
291                 attr_check a/c/f unspecified &&
292                 attr_check a/i unspecified &&
293                 attr_check subdir/a/i unspecified
294         )
295 '
296
297 test_expect_success 'bare repository: check that --cached honors index' '
298         (
299                 cd bare.git &&
300                 GIT_INDEX_FILE=../.git/index \
301                 git check-attr --cached --stdin --all <../stdin-all |
302                 sort >actual &&
303                 test_cmp ../specified-all actual
304         )
305 '
306
307 test_expect_success 'bare repository: test info/attributes' '
308         (
309                 cd bare.git &&
310                 (
311                         echo "f test=f" &&
312                         echo "a/i test=a/i"
313                 ) >info/attributes &&
314                 attr_check f f &&
315                 attr_check a/f f &&
316                 attr_check a/c/f f &&
317                 attr_check a/i a/i &&
318                 attr_check subdir/a/i unspecified
319         )
320 '
321
322 test_expect_success 'binary macro expanded by -a' '
323         echo "file binary" >.gitattributes &&
324         cat >expect <<-\EOF &&
325         file: binary: set
326         file: diff: unset
327         file: merge: unset
328         file: text: unset
329         EOF
330         git check-attr -a file >actual &&
331         test_cmp expect actual
332 '
333
334
335 test_expect_success 'query binary macro directly' '
336         echo "file binary" >.gitattributes &&
337         echo file: binary: set >expect &&
338         git check-attr binary file >actual &&
339         test_cmp expect actual
340 '
341
342 test_done