Merge branch 'main' of github.com:git/git
[alt-git.git] / t / t0003-attributes.sh
blobf7ee2f2ff0e6ee3be99ecd4d545c376963653482
1 #!/bin/sh
3 test_description=gitattributes
5 TEST_PASSES_SANITIZE_LEAK=true
6 TEST_CREATE_REPO_NO_TEMPLATE=1
7 . ./test-lib.sh
9 attr_check_basic () {
10 path="$1" expect="$2" git_opts="$3" &&
12 git $git_opts check-attr test -- "$path" >actual 2>err &&
13 echo "$path: test: $expect" >expect &&
14 test_cmp expect actual
17 attr_check () {
18 attr_check_basic "$@" &&
19 test_must_be_empty err
22 attr_check_quote () {
23 path="$1" quoted_path="$2" expect="$3" &&
25 git check-attr test -- "$path" >actual &&
26 echo "\"$quoted_path\": test: $expect" >expect &&
27 test_cmp expect actual
31 test_expect_success 'open-quoted pathname' '
32 echo "\"a test=a" >.gitattributes &&
33 attr_check a unspecified
37 test_expect_success 'setup' '
38 mkdir -p a/b/d a/c b &&
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 &&
52 echo "g test=a/g" &&
53 echo "b/g test=a/b/g"
54 ) >a/.gitattributes &&
56 echo "h test=a/b/h" &&
57 echo "d/* test=a/b/d/*" &&
58 echo "d/yes notest"
59 ) >a/b/.gitattributes &&
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
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
95 test_expect_success 'attribute test' '
97 attr_check " d " d &&
98 attr_check e e &&
99 attr_check_quote e\" e\\\" e &&
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
116 test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
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"
135 test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
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"
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"
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
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)
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
186 git check-attr test a/g a_plus/g >actual &&
187 test_cmp expect actual
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
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
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
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
224 test_expect_success 'root subdir attribute test' '
225 attr_check a/i a/i &&
226 attr_check subdir/a/i unspecified
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
235 test_expect_success 'patterns starting with exclamation' '
236 echo "\!f test=foo" >.gitattributes &&
237 attr_check "!f" foo
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
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
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
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
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 &&
282 cd unreal &&
283 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
287 test_expect_success 'setup bare' '
288 git clone --template= --bare . bare.git
291 test_expect_success 'bare repository: check that .gitattribute is ignored' '
293 cd bare.git &&
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
306 test_expect_success 'bare repository: check that --cached honors index' '
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
316 test_expect_success 'bare repository: test info/attributes' '
318 cd bare.git &&
319 mkdir info &&
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
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
340 git check-attr -a file >actual &&
341 test_cmp expect actual
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
351 test_expect_success SYMLINKS 'set up symlink tests' '
352 echo "* test" >attr &&
353 rm -f .gitattributes
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
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
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
379 test_done