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