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