Skip to content

Commit 6eba621

Browse files
drafnelgitster
authored andcommitted
attr.c: respect core.ignorecase when matching attribute patterns
When core.ignorecase is true, the file globs configured in the .gitattributes file should be matched case-insensitively against the paths in the working directory. Let's do so. Plus, add some tests. The last set of tests is performed only on a case-insensitive filesystem. Those tests make sure that git handles the case where the .gitignore file resides in a subdirectory and the user supplies a path that does not match the case in the filesystem. In that case^H^H^H^Hsituation, part of the path supplied by the user is effectively interpreted case-insensitively, and part of it is dependent on the setting of core.ignorecase. git will currently only match the portion of the path below the directory holding the .gitignore file according to the setting of core.ignorecase. This is also partly future-proofing. Currently, git builds the attr stack based on the path supplied by the user, so we don't have to do anything special (like use strcmp_icase) to handle the parts of that path that don't match the filesystem with respect to case. If git instead built the attr stack by scanning the repository, then the paths in the origin field would not necessarily match the paths supplied by the user. If someone makes a change like that in the future, these tests will notice. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64589a0 commit 6eba621

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

attr.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "cache.h"
1212
#include "exec_cmd.h"
1313
#include "attr.h"
14+
#include "dir.h"
1415

1516
const char git_attr__true[] = "(builtin)true";
1617
const char git_attr__false[] = "\0(builtin)false";
@@ -631,7 +632,7 @@ static int path_matches(const char *pathname, int pathlen,
631632
/* match basename */
632633
const char *basename = strrchr(pathname, '/');
633634
basename = basename ? basename + 1 : pathname;
634-
return (fnmatch(pattern, basename, 0) == 0);
635+
return (fnmatch_icase(pattern, basename, 0) == 0);
635636
}
636637
/*
637638
* match with FNM_PATHNAME; the pattern has base implicitly
@@ -645,7 +646,7 @@ static int path_matches(const char *pathname, int pathlen,
645646
return 0;
646647
if (baselen != 0)
647648
baselen++;
648-
return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
649+
return fnmatch_icase(pattern, pathname + baselen, FNM_PATHNAME) == 0;
649650
}
650651

651652
static int macroexpand_one(int attr_nr, int rem);

t/t0003-attributes.sh

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ attr_check () {
99
path="$1"
1010
expect="$2"
1111

12-
git check-attr test -- "$path" >actual 2>err &&
12+
git $3 check-attr test -- "$path" >actual 2>err &&
1313
echo "$path: test: $2" >expect &&
1414
test_cmp expect actual &&
1515
test_line_count = 0 err
@@ -27,6 +27,7 @@ test_expect_success 'setup' '
2727
echo "onoff test -test"
2828
echo "offon -test test"
2929
echo "no notest"
30+
echo "A/e/F test=A/e/F"
3031
) >.gitattributes &&
3132
(
3233
echo "g test=a/g" &&
@@ -93,6 +94,62 @@ test_expect_success 'attribute test' '
9394
9495
'
9596

97+
test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
98+
99+
test_must_fail attr_check F f "-c core.ignorecase=0" &&
100+
test_must_fail attr_check a/F f "-c core.ignorecase=0" &&
101+
test_must_fail attr_check a/c/F f "-c core.ignorecase=0" &&
102+
test_must_fail attr_check a/G a/g "-c core.ignorecase=0" &&
103+
test_must_fail attr_check a/B/g a/b/g "-c core.ignorecase=0" &&
104+
test_must_fail attr_check a/b/G a/b/g "-c core.ignorecase=0" &&
105+
test_must_fail attr_check a/b/H a/b/h "-c core.ignorecase=0" &&
106+
test_must_fail attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=0" &&
107+
test_must_fail attr_check oNoFf unset "-c core.ignorecase=0" &&
108+
test_must_fail attr_check oFfOn set "-c core.ignorecase=0" &&
109+
attr_check NO unspecified "-c core.ignorecase=0" &&
110+
test_must_fail attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=0" &&
111+
attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
112+
test_must_fail attr_check a/E/f "A/e/F" "-c core.ignorecase=0"
113+
114+
'
115+
116+
test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
117+
118+
attr_check F f "-c core.ignorecase=1" &&
119+
attr_check a/F f "-c core.ignorecase=1" &&
120+
attr_check a/c/F f "-c core.ignorecase=1" &&
121+
attr_check a/G a/g "-c core.ignorecase=1" &&
122+
attr_check a/B/g a/b/g "-c core.ignorecase=1" &&
123+
attr_check a/b/G a/b/g "-c core.ignorecase=1" &&
124+
attr_check a/b/H a/b/h "-c core.ignorecase=1" &&
125+
attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=1" &&
126+
attr_check oNoFf unset "-c core.ignorecase=1" &&
127+
attr_check oFfOn set "-c core.ignorecase=1" &&
128+
attr_check NO unspecified "-c core.ignorecase=1" &&
129+
attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=1" &&
130+
attr_check a/b/d/YES unspecified "-c core.ignorecase=1" &&
131+
attr_check a/E/f "A/e/F" "-c core.ignorecase=1"
132+
133+
'
134+
135+
test_expect_success 'check whether FS is case-insensitive' '
136+
mkdir junk &&
137+
echo good >junk/CamelCase &&
138+
echo bad >junk/camelcase &&
139+
if test "$(cat junk/CamelCase)" != good
140+
then
141+
test_set_prereq CASE_INSENSITIVE_FS
142+
fi
143+
'
144+
145+
test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
146+
test_must_fail attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=0" &&
147+
test_must_fail attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=0" &&
148+
attr_check A/b/h a/b/h "-c core.ignorecase=1" &&
149+
attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=1" &&
150+
attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=1"
151+
'
152+
96153
test_expect_success 'unnormalized paths' '
97154
98155
attr_check ./f f &&

0 commit comments

Comments
 (0)