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