Merge branch 'rs/t4202-invert-grep-test-fix'
[git/debian.git] / t / t0003-attributes.sh
blobb9ed612af13262c0467aed2be927bd4704a221e4
1 #!/bin/sh
3 test_description=gitattributes
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 attr_check_basic () {
9 path="$1" expect="$2" git_opts="$3" &&
11 git $git_opts check-attr test -- "$path" >actual 2>err &&
12 echo "$path: test: $expect" >expect &&
13 test_cmp expect actual
16 attr_check () {
17 attr_check_basic "$@" &&
18 test_must_be_empty err
21 attr_check_quote () {
22 path="$1" quoted_path="$2" expect="$3" &&
24 git check-attr test -- "$path" >actual &&
25 echo "\"$quoted_path\": test: $expect" >expect &&
26 test_cmp expect actual
30 test_expect_success 'open-quoted pathname' '
31 echo "\"a test=a" >.gitattributes &&
32 attr_check a unspecified
36 test_expect_success 'setup' '
37 mkdir -p a/b/d a/c b &&
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" &&
48 echo "A/e/F test=A/e/F"
49 ) >.gitattributes &&
51 echo "g test=a/g" &&
52 echo "b/g test=a/b/g"
53 ) >a/.gitattributes &&
55 echo "h test=a/b/h" &&
56 echo "d/* test=a/b/d/*" &&
57 echo "d/yes notest"
58 ) >a/b/.gitattributes &&
60 echo "global test=global"
61 ) >"$HOME"/global-gitattributes &&
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
82 test_expect_success 'command line checks' '
83 test_must_fail git check-attr &&
84 test_must_fail git check-attr -- &&
85 test_must_fail git check-attr test &&
86 test_must_fail git check-attr test -- &&
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 &&
91 test_must_fail git check-attr "" -- f
94 test_expect_success 'attribute test' '
96 attr_check " d " d &&
97 attr_check e e &&
98 attr_check_quote e\" e\\\" e &&
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 &&
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/*" &&
112 attr_check a/b/d/yes unspecified
115 test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
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" &&
127 attr_check NO unspecified "-c core.ignorecase=0" &&
128 attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
129 attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
130 attr_check a/E/f f "-c core.ignorecase=0"
134 test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
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"
153 test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
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" &&
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"
161 test_expect_success 'unnormalized paths' '
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
168 test_expect_success 'relative paths' '
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)
179 test_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
185 git check-attr test a/g a_plus/g >actual &&
186 test_cmp expect actual
189 test_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 &&
195 echo "global test=precedence" >>.gitattributes &&
196 attr_check global precedence
199 test_expect_success 'attribute test: read paths from stdin' '
200 grep -v notest <expect-all >expect &&
201 sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
202 test_cmp expect actual
205 test_expect_success 'attribute test: --all option' '
206 grep -v unspecified <expect-all | sort >specified-all &&
207 sed -e "s/:.*//" <expect-all | uniq >stdin-all &&
208 git check-attr --stdin --all <stdin-all | sort >actual &&
209 test_cmp specified-all actual
212 test_expect_success 'attribute test: --cached option' '
213 git check-attr --cached --stdin --all <stdin-all | sort >actual &&
214 test_must_be_empty actual &&
215 git add .gitattributes a/.gitattributes a/b/.gitattributes &&
216 git check-attr --cached --stdin --all <stdin-all | sort >actual &&
217 test_cmp specified-all actual
220 test_expect_success 'root subdir attribute test' '
221 attr_check a/i a/i &&
222 attr_check subdir/a/i unspecified
225 test_expect_success 'negative patterns' '
226 echo "!f test=bar" >.gitattributes &&
227 git check-attr test -- '"'"'!f'"'"' 2>errors &&
228 test_i18ngrep "Negative patterns are ignored" errors
231 test_expect_success 'patterns starting with exclamation' '
232 echo "\!f test=foo" >.gitattributes &&
233 attr_check "!f" foo
236 test_expect_success '"**" test' '
237 echo "**/f foo=bar" >.gitattributes &&
238 cat <<\EOF >expect &&
239 f: foo: bar
240 a/f: foo: bar
241 a/b/f: foo: bar
242 a/b/c/f: foo: bar
244 git check-attr foo -- "f" >actual 2>err &&
245 git check-attr foo -- "a/f" >>actual 2>>err &&
246 git check-attr foo -- "a/b/f" >>actual 2>>err &&
247 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
248 test_cmp expect actual &&
249 test_must_be_empty err
252 test_expect_success '"**" with no slashes test' '
253 echo "a**f foo=bar" >.gitattributes &&
254 git check-attr foo -- "f" >actual &&
255 cat <<\EOF >expect &&
256 f: foo: unspecified
257 af: foo: bar
258 axf: foo: bar
259 a/f: foo: unspecified
260 a/b/f: foo: unspecified
261 a/b/c/f: foo: unspecified
263 git check-attr foo -- "f" >actual 2>err &&
264 git check-attr foo -- "af" >>actual 2>err &&
265 git check-attr foo -- "axf" >>actual 2>err &&
266 git check-attr foo -- "a/f" >>actual 2>>err &&
267 git check-attr foo -- "a/b/f" >>actual 2>>err &&
268 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
269 test_cmp expect actual &&
270 test_must_be_empty err
273 test_expect_success 'using --git-dir and --work-tree' '
274 mkdir unreal real &&
275 git init real &&
276 echo "file test=in-real" >real/.gitattributes &&
278 cd unreal &&
279 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
283 test_expect_success 'setup bare' '
284 git clone --bare . bare.git
287 test_expect_success 'bare repository: check that .gitattribute is ignored' '
289 cd bare.git &&
291 echo "f test=f" &&
292 echo "a/i test=a/i"
293 ) >.gitattributes &&
294 attr_check f unspecified &&
295 attr_check a/f unspecified &&
296 attr_check a/c/f unspecified &&
297 attr_check a/i unspecified &&
298 attr_check subdir/a/i unspecified
302 test_expect_success 'bare repository: check that --cached honors index' '
304 cd bare.git &&
305 GIT_INDEX_FILE=../.git/index \
306 git check-attr --cached --stdin --all <../stdin-all |
307 sort >actual &&
308 test_cmp ../specified-all actual
312 test_expect_success 'bare repository: test info/attributes' '
314 cd bare.git &&
316 echo "f test=f" &&
317 echo "a/i test=a/i"
318 ) >info/attributes &&
319 attr_check f f &&
320 attr_check a/f f &&
321 attr_check a/c/f f &&
322 attr_check a/i a/i &&
323 attr_check subdir/a/i unspecified
327 test_expect_success 'binary macro expanded by -a' '
328 echo "file binary" >.gitattributes &&
329 cat >expect <<-\EOF &&
330 file: binary: set
331 file: diff: unset
332 file: merge: unset
333 file: text: unset
335 git check-attr -a file >actual &&
336 test_cmp expect actual
339 test_expect_success 'query binary macro directly' '
340 echo "file binary" >.gitattributes &&
341 echo file: binary: set >expect &&
342 git check-attr binary file >actual &&
343 test_cmp expect actual
346 test_expect_success SYMLINKS 'set up symlink tests' '
347 echo "* test" >attr &&
348 rm -f .gitattributes
351 test_expect_success SYMLINKS 'symlinks respected in core.attributesFile' '
352 test_when_finished "rm symlink" &&
353 ln -s attr symlink &&
354 test_config core.attributesFile "$(pwd)/symlink" &&
355 attr_check file set
358 test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
359 test_when_finished "rm .git/info/attributes" &&
360 ln -s ../../attr .git/info/attributes &&
361 attr_check file set
364 test_expect_success SYMLINKS 'symlinks not respected in-tree' '
365 test_when_finished "rm -rf .gitattributes subdir" &&
366 ln -s attr .gitattributes &&
367 mkdir subdir &&
368 ln -s ../attr subdir/.gitattributes &&
369 attr_check_basic subdir/file unspecified &&
370 test_i18ngrep "unable to access.*gitattributes" err
373 test_done