blob: ecf43ab54545a22ad52495af7cd728b04ac245bb [file] [log] [blame]
Junio C Hamanocf94ccd2008-02-07 00:02:08 -08001#!/bin/sh
2
3test_description=gitattributes
4
Ævar Arnfjörð Bjarmasonc1500642021-10-12 15:56:37 +02005TEST_PASSES_SANITIZE_LEAK=true
Ævar Arnfjörð Bjarmason8da0b022022-06-03 13:15:06 +02006TEST_CREATE_REPO_NO_TEMPLATE=1
Junio C Hamanocf94ccd2008-02-07 00:02:08 -08007. ./test-lib.sh
8
Jeff King2ef579e22021-02-16 09:44:32 -05009attr_check_basic () {
Denton Liu37384392019-12-20 10:15:50 -080010 path="$1" expect="$2" git_opts="$3" &&
Junio C Hamanocf94ccd2008-02-07 00:02:08 -080011
Denton Liu37384392019-12-20 10:15:50 -080012 git $git_opts check-attr test -- "$path" >actual 2>err &&
13 echo "$path: test: $expect" >expect &&
Jeff King2ef579e22021-02-16 09:44:32 -050014 test_cmp expect actual
15}
16
17attr_check () {
18 attr_check_basic "$@" &&
Denton Liu99c049b2019-12-20 10:15:51 -080019 test_must_be_empty err
Junio C Hamanocf94ccd2008-02-07 00:02:08 -080020}
21
Nguyễn Thái Ngọc Duy860a74d2017-01-27 18:01:50 -080022attr_check_quote () {
Denton Liu37384392019-12-20 10:15:50 -080023 path="$1" quoted_path="$2" expect="$3" &&
Nguyễn Thái Ngọc Duy860a74d2017-01-27 18:01:50 -080024
25 git check-attr test -- "$path" >actual &&
26 echo "\"$quoted_path\": test: $expect" >expect &&
27 test_cmp expect actual
Karthik Nayak47cfc9b2023-01-14 09:30:38 +010028}
Nguyễn Thái Ngọc Duy860a74d2017-01-27 18:01:50 -080029
Karthik Nayak47cfc9b2023-01-14 09:30:38 +010030attr_check_source () {
31 path="$1" expect="$2" source="$3" git_opts="$4" &&
32
Karthik Nayak47cfc9b2023-01-14 09:30:38 +010033 echo "$path: test: $expect" >expect &&
John Cai44451a22023-05-06 04:15:29 +000034
35 git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
36 test_cmp expect actual &&
37 test_must_be_empty err &&
38
39 git $git_opts --attr-source="$source" check-attr test -- "$path" >actual 2>err &&
40 test_cmp expect actual &&
41 test_must_be_empty err
42
John Cai9f9c40c2023-10-13 17:39:30 +000043 git $git_opts -c "attr.tree=$source" check-attr test -- "$path" >actual 2>err &&
44 test_cmp expect actual &&
45 test_must_be_empty err
46
John Cai44451a22023-05-06 04:15:29 +000047 GIT_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
Karthik Nayak47cfc9b2023-01-14 09:30:38 +010048 test_cmp expect actual &&
49 test_must_be_empty err
Nguyễn Thái Ngọc Duy860a74d2017-01-27 18:01:50 -080050}
51
52test_expect_success 'open-quoted pathname' '
53 echo "\"a test=a" >.gitattributes &&
Denton Liuf46c2432019-12-20 10:15:52 -080054 attr_check a unspecified
Nguyễn Thái Ngọc Duy860a74d2017-01-27 18:01:50 -080055'
56
Junio C Hamanocf94ccd2008-02-07 00:02:08 -080057test_expect_success 'setup' '
Michael Haggerty0216af82011-08-04 06:47:45 +020058 mkdir -p a/b/d a/c b &&
Junio C Hamanocf94ccd2008-02-07 00:02:08 -080059 (
Eric Sunshine75651fd2018-07-01 20:23:55 -040060 echo "[attr]notest !test" &&
61 echo "\" d \" test=d" &&
62 echo " e test=e" &&
63 echo " e\" test=e" &&
64 echo "f test=f" &&
65 echo "a/i test=a/i" &&
66 echo "onoff test -test" &&
67 echo "offon -test test" &&
68 echo "no notest" &&
Brandon Casey6eba6212011-10-11 10:53:31 -050069 echo "A/e/F test=A/e/F"
Junio C Hamanocf94ccd2008-02-07 00:02:08 -080070 ) >.gitattributes &&
71 (
72 echo "g test=a/g" &&
73 echo "b/g test=a/b/g"
74 ) >a/.gitattributes &&
75 (
76 echo "h test=a/b/h" &&
Eric Sunshine75651fd2018-07-01 20:23:55 -040077 echo "d/* test=a/b/d/*" &&
Henrik Grubbströmec775c42010-04-06 14:46:44 +020078 echo "d/yes notest"
Michael Haggertydcc04362011-08-04 06:36:15 +020079 ) >a/b/.gitattributes &&
Petr Onderka6df42ab2010-09-01 00:42:43 +020080 (
81 echo "global test=global"
Michael Haggertyc9d8f0a2011-08-04 06:36:31 +020082 ) >"$HOME"/global-gitattributes &&
Junio C Hamano78cec752011-09-22 16:34:05 -070083 cat <<-EOF >expect-all
84 f: test: f
85 a/f: test: f
86 a/c/f: test: f
87 a/g: test: a/g
88 a/b/g: test: a/b/g
89 b/g: test: unspecified
90 a/b/h: test: a/b/h
91 a/b/d/g: test: a/b/d/*
92 onoff: test: unset
93 offon: test: set
94 no: notest: set
95 no: test: unspecified
96 a/b/d/no: notest: set
97 a/b/d/no: test: a/b/d/*
98 a/b/d/yes: notest: set
99 a/b/d/yes: test: unspecified
100 EOF
Junio C Hamanocf94ccd2008-02-07 00:02:08 -0800101'
102
Karthik Nayak47cfc9b2023-01-14 09:30:38 +0100103test_expect_success 'setup branches' '
104 mkdir -p foo/bar &&
105 test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
106 "f test=f\na/i test=n\n" tag-1 &&
107 test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
108 "g test=g\na/i test=m\n" tag-2 &&
109 rm foo/bar/.gitattributes
110'
111
Michael Haggertyc0b13b22011-08-04 06:36:14 +0200112test_expect_success 'command line checks' '
Michael Haggerty09d7dd72011-08-04 06:36:16 +0200113 test_must_fail git check-attr &&
114 test_must_fail git check-attr -- &&
Michael Haggertyfdf6be82011-08-04 06:36:29 +0200115 test_must_fail git check-attr test &&
116 test_must_fail git check-attr test -- &&
Michael Haggerty09d7dd72011-08-04 06:36:16 +0200117 test_must_fail git check-attr -- f &&
Karthik Nayak47cfc9b2023-01-14 09:30:38 +0100118 test_must_fail git check-attr --source &&
119 test_must_fail git check-attr --source not-a-valid-ref &&
Michael Haggerty09d7dd72011-08-04 06:36:16 +0200120 echo "f" | test_must_fail git check-attr --stdin &&
121 echo "f" | test_must_fail git check-attr --stdin -- f &&
122 echo "f" | test_must_fail git check-attr --stdin test -- f &&
Michael Haggertyc0b13b22011-08-04 06:36:14 +0200123 test_must_fail git check-attr "" -- f
Michael Haggertyc0b13b22011-08-04 06:36:14 +0200124'
125
Junio C Hamanocf94ccd2008-02-07 00:02:08 -0800126test_expect_success 'attribute test' '
Nguyễn Thái Ngọc Duy860a74d2017-01-27 18:01:50 -0800127
128 attr_check " d " d &&
129 attr_check e e &&
130 attr_check_quote e\" e\\\" e &&
131
Junio C Hamanocf94ccd2008-02-07 00:02:08 -0800132 attr_check f f &&
133 attr_check a/f f &&
134 attr_check a/c/f f &&
135 attr_check a/g a/g &&
136 attr_check a/b/g a/b/g &&
137 attr_check b/g unspecified &&
138 attr_check a/b/h a/b/h &&
Matthieu Moy520ea852010-08-28 20:18:36 +0200139 attr_check a/b/d/g "a/b/d/*" &&
140 attr_check onoff unset &&
141 attr_check offon set &&
142 attr_check no unspecified &&
143 attr_check a/b/d/no "a/b/d/*" &&
Henrik Grubbströmec775c42010-04-06 14:46:44 +0200144 attr_check a/b/d/yes unspecified
Junio C Hamanocf94ccd2008-02-07 00:02:08 -0800145'
146
Brandon Casey6eba6212011-10-11 10:53:31 -0500147test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
148
Denton Liuf46c2432019-12-20 10:15:52 -0800149 attr_check F unspecified "-c core.ignorecase=0" &&
150 attr_check a/F unspecified "-c core.ignorecase=0" &&
151 attr_check a/c/F unspecified "-c core.ignorecase=0" &&
152 attr_check a/G unspecified "-c core.ignorecase=0" &&
153 attr_check a/B/g a/g "-c core.ignorecase=0" &&
154 attr_check a/b/G unspecified "-c core.ignorecase=0" &&
155 attr_check a/b/H unspecified "-c core.ignorecase=0" &&
156 attr_check a/b/D/g a/g "-c core.ignorecase=0" &&
157 attr_check oNoFf unspecified "-c core.ignorecase=0" &&
158 attr_check oFfOn unspecified "-c core.ignorecase=0" &&
Brandon Casey6eba6212011-10-11 10:53:31 -0500159 attr_check NO unspecified "-c core.ignorecase=0" &&
Denton Liuf46c2432019-12-20 10:15:52 -0800160 attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
Brandon Casey6eba6212011-10-11 10:53:31 -0500161 attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
Denton Liuf46c2432019-12-20 10:15:52 -0800162 attr_check a/E/f f "-c core.ignorecase=0"
Brandon Casey6eba6212011-10-11 10:53:31 -0500163
164'
165
166test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
167
168 attr_check F f "-c core.ignorecase=1" &&
169 attr_check a/F f "-c core.ignorecase=1" &&
170 attr_check a/c/F f "-c core.ignorecase=1" &&
171 attr_check a/G a/g "-c core.ignorecase=1" &&
172 attr_check a/B/g a/b/g "-c core.ignorecase=1" &&
173 attr_check a/b/G a/b/g "-c core.ignorecase=1" &&
174 attr_check a/b/H a/b/h "-c core.ignorecase=1" &&
175 attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=1" &&
176 attr_check oNoFf unset "-c core.ignorecase=1" &&
177 attr_check oFfOn set "-c core.ignorecase=1" &&
178 attr_check NO unspecified "-c core.ignorecase=1" &&
179 attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=1" &&
180 attr_check a/b/d/YES unspecified "-c core.ignorecase=1" &&
181 attr_check a/E/f "A/e/F" "-c core.ignorecase=1"
182
183'
184
Brandon Casey6eba6212011-10-11 10:53:31 -0500185test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
Denton Liuf46c2432019-12-20 10:15:52 -0800186 attr_check a/B/D/g a/g "-c core.ignorecase=0" &&
187 attr_check A/B/D/NO unspecified "-c core.ignorecase=0" &&
Brandon Casey6eba6212011-10-11 10:53:31 -0500188 attr_check A/b/h a/b/h "-c core.ignorecase=1" &&
189 attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=1" &&
190 attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=1"
191'
192
Michael Haggertyf5114a42011-08-04 06:47:46 +0200193test_expect_success 'unnormalized paths' '
Michael Haggertyd4d4f8d2011-08-04 06:47:44 +0200194 attr_check ./f f &&
195 attr_check ./a/g a/g &&
196 attr_check a/./g a/g &&
197 attr_check a/c/../b/g a/b/g
Michael Haggertyd4d4f8d2011-08-04 06:47:44 +0200198'
199
Michael Haggertyf5114a42011-08-04 06:47:46 +0200200test_expect_success 'relative paths' '
Michael Haggerty0216af82011-08-04 06:47:45 +0200201 (cd a && attr_check ../f f) &&
202 (cd a && attr_check f f) &&
203 (cd a && attr_check i a/i) &&
204 (cd a && attr_check g a/g) &&
205 (cd a && attr_check b/g a/b/g) &&
206 (cd b && attr_check ../a/f f) &&
207 (cd b && attr_check ../a/g a/g) &&
208 (cd b && attr_check ../a/b/g a/b/g)
Michael Haggerty0216af82011-08-04 06:47:45 +0200209'
210
Jeff King1afca442012-01-10 13:08:21 -0500211test_expect_success 'prefixes are not confused with leading directories' '
212 attr_check a_plus/g unspecified &&
213 cat >expect <<-\EOF &&
214 a/g: test: a/g
215 a_plus/g: test: unspecified
216 EOF
217 git check-attr test a/g a_plus/g >actual &&
218 test_cmp expect actual
219'
220
Petr Onderka6df42ab2010-09-01 00:42:43 +0200221test_expect_success 'core.attributesfile' '
222 attr_check global unspecified &&
223 git config core.attributesfile "$HOME/global-gitattributes" &&
224 attr_check global global &&
225 git config core.attributesfile "~/global-gitattributes" &&
226 attr_check global global &&
Junio C Hamano78cec752011-09-22 16:34:05 -0700227 echo "global test=precedence" >>.gitattributes &&
Petr Onderka6df42ab2010-09-01 00:42:43 +0200228 attr_check global precedence
229'
230
Dmitry Potapovb4666852008-10-07 04:16:52 +0400231test_expect_success 'attribute test: read paths from stdin' '
Junio C Hamano78cec752011-09-22 16:34:05 -0700232 grep -v notest <expect-all >expect &&
233 sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
Dmitry Potapovb4666852008-10-07 04:16:52 +0400234 test_cmp expect actual
235'
236
Karthik Nayakc847e8c2023-01-14 09:30:37 +0100237test_expect_success 'setup --all option' '
Jay Soffianb2b3e9c2011-09-22 17:44:20 -0400238 grep -v unspecified <expect-all | sort >specified-all &&
Karthik Nayakc847e8c2023-01-14 09:30:37 +0100239 sed -e "s/:.*//" <expect-all | uniq >stdin-all
240'
241
242test_expect_success 'attribute test: --all option' '
Shubham Mishraae5d5692022-02-23 17:23:47 +0530243 git check-attr --stdin --all <stdin-all >tmp &&
244 sort tmp >actual &&
Jay Soffianb2b3e9c2011-09-22 17:44:20 -0400245 test_cmp specified-all actual
246'
247
248test_expect_success 'attribute test: --cached option' '
Shubham Mishraae5d5692022-02-23 17:23:47 +0530249 git check-attr --cached --stdin --all <stdin-all >tmp &&
250 sort tmp >actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200251 test_must_be_empty actual &&
Jay Soffianb2b3e9c2011-09-22 17:44:20 -0400252 git add .gitattributes a/.gitattributes a/b/.gitattributes &&
Shubham Mishraae5d5692022-02-23 17:23:47 +0530253 git check-attr --cached --stdin --all <stdin-all >tmp &&
254 sort tmp >actual &&
Jay Soffianb2b3e9c2011-09-22 17:44:20 -0400255 test_cmp specified-all actual
Michael Haggerty4ca0f182011-08-04 06:36:30 +0200256'
257
Matthew Ogilvie82881b32008-04-22 12:19:12 -0600258test_expect_success 'root subdir attribute test' '
Matthew Ogilvie82881b32008-04-22 12:19:12 -0600259 attr_check a/i a/i &&
260 attr_check subdir/a/i unspecified
Matthew Ogilvie82881b32008-04-22 12:19:12 -0600261'
262
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700263test_expect_success 'negative patterns' '
264 echo "!f test=bar" >.gitattributes &&
Thomas Rast8b1bd022013-03-01 21:06:17 +0100265 git check-attr test -- '"'"'!f'"'"' 2>errors &&
266 test_i18ngrep "Negative patterns are ignored" errors
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700267'
268
269test_expect_success 'patterns starting with exclamation' '
270 echo "\!f test=foo" >.gitattributes &&
271 attr_check "!f" foo
272'
273
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +0700274test_expect_success '"**" test' '
275 echo "**/f foo=bar" >.gitattributes &&
276 cat <<\EOF >expect &&
277f: foo: bar
278a/f: foo: bar
279a/b/f: foo: bar
280a/b/c/f: foo: bar
281EOF
282 git check-attr foo -- "f" >actual 2>err &&
283 git check-attr foo -- "a/f" >>actual 2>>err &&
284 git check-attr foo -- "a/b/f" >>actual 2>>err &&
285 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
286 test_cmp expect actual &&
Denton Liu99c049b2019-12-20 10:15:51 -0800287 test_must_be_empty err
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +0700288'
289
290test_expect_success '"**" with no slashes test' '
291 echo "a**f foo=bar" >.gitattributes &&
292 git check-attr foo -- "f" >actual &&
293 cat <<\EOF >expect &&
294f: foo: unspecified
295af: foo: bar
296axf: foo: bar
297a/f: foo: unspecified
298a/b/f: foo: unspecified
299a/b/c/f: foo: unspecified
300EOF
301 git check-attr foo -- "f" >actual 2>err &&
302 git check-attr foo -- "af" >>actual 2>err &&
303 git check-attr foo -- "axf" >>actual 2>err &&
304 git check-attr foo -- "a/f" >>actual 2>>err &&
305 git check-attr foo -- "a/b/f" >>actual 2>>err &&
306 git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
307 test_cmp expect actual &&
Denton Liu99c049b2019-12-20 10:15:51 -0800308 test_must_be_empty err
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +0700309'
310
Junio C Hamanocdbf6232014-02-06 10:19:33 -0800311test_expect_success 'using --git-dir and --work-tree' '
312 mkdir unreal real &&
313 git init real &&
314 echo "file test=in-real" >real/.gitattributes &&
315 (
316 cd unreal &&
317 attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
318 )
319'
320
Karthik Nayak47cfc9b2023-01-14 09:30:38 +0100321test_expect_success 'using --source' '
322 attr_check_source foo/bar/f f tag-1 &&
323 attr_check_source foo/bar/a/i n tag-1 &&
324 attr_check_source foo/bar/f unspecified tag-2 &&
325 attr_check_source foo/bar/a/i m tag-2 &&
326 attr_check_source foo/bar/g g tag-2 &&
327 attr_check_source foo/bar/g unspecified tag-1
328'
329
René Scharfe2d35d552008-06-08 17:16:11 +0200330test_expect_success 'setup bare' '
Ævar Arnfjörð Bjarmason8da0b022022-06-03 13:15:06 +0200331 git clone --template= --bare . bare.git
René Scharfe2d35d552008-06-08 17:16:11 +0200332'
333
334test_expect_success 'bare repository: check that .gitattribute is ignored' '
René Scharfe2d35d552008-06-08 17:16:11 +0200335 (
Junio C Hamanoc4a7bce2014-02-06 10:16:27 -0800336 cd bare.git &&
337 (
Eric Sunshine75651fd2018-07-01 20:23:55 -0400338 echo "f test=f" &&
Junio C Hamanoc4a7bce2014-02-06 10:16:27 -0800339 echo "a/i test=a/i"
340 ) >.gitattributes &&
341 attr_check f unspecified &&
342 attr_check a/f unspecified &&
343 attr_check a/c/f unspecified &&
344 attr_check a/i unspecified &&
345 attr_check subdir/a/i unspecified
346 )
René Scharfe2d35d552008-06-08 17:16:11 +0200347'
348
John Cai9f9c40c2023-10-13 17:39:30 +0000349bad_attr_source_err="fatal: bad --attr-source or GIT_ATTR_SOURCE"
350
351test_expect_success '--attr-source is bad' '
352 test_when_finished rm -rf empty &&
353 git init empty &&
354 (
355 cd empty &&
356 echo "$bad_attr_source_err" >expect_err &&
357 test_must_fail git --attr-source=HEAD check-attr test -- f/path 2>err &&
358 test_cmp expect_err err
359 )
360'
361
362test_expect_success 'attr.tree when HEAD is unborn' '
363 test_when_finished rm -rf empty &&
364 git init empty &&
365 (
366 cd empty &&
367 echo "f/path: test: unspecified" >expect &&
368 git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
369 test_must_be_empty err &&
370 test_cmp expect actual
371 )
372'
373
374test_expect_success 'bad attr source defaults to reading .gitattributes file' '
375 test_when_finished rm -rf empty &&
376 git init empty &&
377 (
378 cd empty &&
379 echo "f/path test=val" >.gitattributes &&
380 echo "f/path: test: val" >expect &&
381 git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
382 test_must_be_empty err &&
383 test_cmp expect actual
384 )
385'
John Cai23865352023-10-13 17:39:29 +0000386
387test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
388 test_when_finished rm -rf test bare_with_gitattribute &&
389 git init test &&
390 test_commit -C test gitattributes .gitattributes "f/path test=val" &&
391 git clone --bare test bare_with_gitattribute &&
392 echo "f/path: test: val" >expect &&
393 git -C bare_with_gitattribute check-attr test -- f/path >actual &&
394 test_cmp expect actual
395'
396
John Cai9f9c40c2023-10-13 17:39:30 +0000397test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tree' '
398 test_when_finished rm -rf empty &&
399 git init empty &&
400 (
401 cd empty &&
402 git checkout -b attr-source &&
403 test_commit "val1" .gitattributes "f/path test=val1" &&
404 git checkout -b attr-tree &&
405 test_commit "val2" .gitattributes "f/path test=val2" &&
406 git checkout attr-source &&
407 echo "f/path: test: val1" >expect &&
408 GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree --attr-source=attr-source \
409 check-attr test -- f/path >actual &&
410 test_cmp expect actual &&
411 GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree \
412 check-attr test -- f/path >actual &&
413 test_cmp expect actual
414 )
415'
416
Karthik Nayak47cfc9b2023-01-14 09:30:38 +0100417test_expect_success 'bare repository: with --source' '
418 (
419 cd bare.git &&
420 attr_check_source foo/bar/f f tag-1 &&
421 attr_check_source foo/bar/a/i n tag-1 &&
422 attr_check_source foo/bar/f unspecified tag-2 &&
423 attr_check_source foo/bar/a/i m tag-2 &&
424 attr_check_source foo/bar/g g tag-2 &&
425 attr_check_source foo/bar/g unspecified tag-1
426 )
427'
428
Jay Soffianb2b3e9c2011-09-22 17:44:20 -0400429test_expect_success 'bare repository: check that --cached honors index' '
Junio C Hamanoc4a7bce2014-02-06 10:16:27 -0800430 (
431 cd bare.git &&
432 GIT_INDEX_FILE=../.git/index \
433 git check-attr --cached --stdin --all <../stdin-all |
434 sort >actual &&
435 test_cmp ../specified-all actual
436 )
Jay Soffianb2b3e9c2011-09-22 17:44:20 -0400437'
438
René Scharfe2d35d552008-06-08 17:16:11 +0200439test_expect_success 'bare repository: test info/attributes' '
René Scharfe2d35d552008-06-08 17:16:11 +0200440 (
Junio C Hamanoc4a7bce2014-02-06 10:16:27 -0800441 cd bare.git &&
Ævar Arnfjörð Bjarmason8da0b022022-06-03 13:15:06 +0200442 mkdir info &&
Junio C Hamanoc4a7bce2014-02-06 10:16:27 -0800443 (
Eric Sunshine75651fd2018-07-01 20:23:55 -0400444 echo "f test=f" &&
Junio C Hamanoc4a7bce2014-02-06 10:16:27 -0800445 echo "a/i test=a/i"
446 ) >info/attributes &&
447 attr_check f f &&
448 attr_check a/f f &&
449 attr_check a/c/f f &&
450 attr_check a/i a/i &&
451 attr_check subdir/a/i unspecified
452 )
René Scharfe2d35d552008-06-08 17:16:11 +0200453'
454
Jeff King7b958492019-01-18 16:34:58 -0500455test_expect_success 'binary macro expanded by -a' '
456 echo "file binary" >.gitattributes &&
457 cat >expect <<-\EOF &&
458 file: binary: set
459 file: diff: unset
460 file: merge: unset
461 file: text: unset
462 EOF
463 git check-attr -a file >actual &&
464 test_cmp expect actual
465'
466
Jeff King7b958492019-01-18 16:34:58 -0500467test_expect_success 'query binary macro directly' '
468 echo "file binary" >.gitattributes &&
469 echo file: binary: set >expect &&
470 git check-attr binary file >actual &&
471 test_cmp expect actual
472'
473
Jeff King2ef579e22021-02-16 09:44:32 -0500474test_expect_success SYMLINKS 'set up symlink tests' '
475 echo "* test" >attr &&
476 rm -f .gitattributes
477'
478
479test_expect_success SYMLINKS 'symlinks respected in core.attributesFile' '
480 test_when_finished "rm symlink" &&
481 ln -s attr symlink &&
482 test_config core.attributesFile "$(pwd)/symlink" &&
483 attr_check file set
484'
485
486test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
487 test_when_finished "rm .git/info/attributes" &&
Ævar Arnfjörð Bjarmason8da0b022022-06-03 13:15:06 +0200488 mkdir .git/info &&
Jeff King2ef579e22021-02-16 09:44:32 -0500489 ln -s ../../attr .git/info/attributes &&
490 attr_check file set
491'
492
493test_expect_success SYMLINKS 'symlinks not respected in-tree' '
494 test_when_finished "rm -rf .gitattributes subdir" &&
495 ln -s attr .gitattributes &&
496 mkdir subdir &&
497 ln -s ../attr subdir/.gitattributes &&
498 attr_check_basic subdir/file unspecified &&
499 test_i18ngrep "unable to access.*gitattributes" err
500'
501
Patrick Steinhardtdfa6b322022-12-01 15:45:48 +0100502test_expect_success 'large attributes line ignored in tree' '
503 test_when_finished "rm .gitattributes" &&
504 printf "path %02043d" 1 >.gitattributes &&
505 git check-attr --all path >actual 2>err &&
506 echo "warning: ignoring overly long attributes line 1" >expect &&
507 test_cmp expect err &&
508 test_must_be_empty actual
509'
510
Patrick Steinhardtd74b1fd2022-12-01 15:45:44 +0100511test_expect_success 'large attributes line ignores trailing content in tree' '
512 test_when_finished "rm .gitattributes" &&
513 # older versions of Git broke lines at 2048 bytes; the 2045 bytes
514 # of 0-padding here is accounting for the three bytes of "a 1", which
515 # would knock "trailing" to the "next" line, where it would be
516 # erroneously parsed.
517 printf "a %02045dtrailing attribute\n" 1 >.gitattributes &&
518 git check-attr --all trailing >actual 2>err &&
Patrick Steinhardtdfa6b322022-12-01 15:45:48 +0100519 echo "warning: ignoring overly long attributes line 1" >expect &&
520 test_cmp expect err &&
521 test_must_be_empty actual
522'
523
Patrick Steinhardt3c500322022-12-01 15:45:53 +0100524test_expect_success EXPENSIVE 'large attributes file ignored in tree' '
525 test_when_finished "rm .gitattributes" &&
Torsten Bögershausen5458ba02023-01-22 07:28:39 +0100526 dd if=/dev/zero of=.gitattributes bs=1048576 count=101 2>/dev/null &&
Patrick Steinhardt3c500322022-12-01 15:45:53 +0100527 git check-attr --all path >/dev/null 2>err &&
528 echo "warning: ignoring overly large gitattributes file ${SQ}.gitattributes${SQ}" >expect &&
529 test_cmp expect err
530'
531
Patrick Steinhardtdfa6b322022-12-01 15:45:48 +0100532test_expect_success 'large attributes line ignored in index' '
533 test_when_finished "git update-index --remove .gitattributes" &&
534 blob=$(printf "path %02043d" 1 | git hash-object -w --stdin) &&
535 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
536 git check-attr --cached --all path >actual 2>err &&
537 echo "warning: ignoring overly long attributes line 1" >expect &&
538 test_cmp expect err &&
Patrick Steinhardtd74b1fd2022-12-01 15:45:44 +0100539 test_must_be_empty actual
540'
541
542test_expect_success 'large attributes line ignores trailing content in index' '
543 test_when_finished "git update-index --remove .gitattributes" &&
544 blob=$(printf "a %02045dtrailing attribute\n" 1 | git hash-object -w --stdin) &&
545 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
546 git check-attr --cached --all trailing >actual 2>err &&
Patrick Steinhardtdfa6b322022-12-01 15:45:48 +0100547 echo "warning: ignoring overly long attributes line 1" >expect &&
548 test_cmp expect err &&
Patrick Steinhardtd74b1fd2022-12-01 15:45:44 +0100549 test_must_be_empty actual
550'
551
Patrick Steinhardt3c500322022-12-01 15:45:53 +0100552test_expect_success EXPENSIVE 'large attributes file ignored in index' '
553 test_when_finished "git update-index --remove .gitattributes" &&
Torsten Bögershausen5458ba02023-01-22 07:28:39 +0100554 blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) &&
Patrick Steinhardt3c500322022-12-01 15:45:53 +0100555 git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
556 git check-attr --cached --all path >/dev/null 2>err &&
557 echo "warning: ignoring overly large gitattributes blob ${SQ}.gitattributes${SQ}" >expect &&
558 test_cmp expect err
559'
560
Junio C Hamanocf94ccd2008-02-07 00:02:08 -0800561test_done