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 <drafnel@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Casey committed Oct 11, 2011 at 10:53 UTC 6eba6210d9824445c29b75f9c618abbec5184afd
2 files changed +61 -3
attr.c
+3 -2
@@ -11,6 +11,7 @@
11 #include "cache.h"
12 #include "exec_cmd.h"
13 #include "attr.h"
14 +#include "dir.h"
15
16 const char git_attr__true[] = "(builtin)true";
17 const char git_attr__false[] = "\0(builtin)false";
@@ -631,7 +632,7 @@ static int path_matches(const char *pathname, int pathlen,
632 /* match basename */
633 const char *basename = strrchr(pathname, '/');
634 basename = basename ? basename + 1 : pathname;
634 - return (fnmatch(pattern, basename, 0) == 0);
635 + return (fnmatch_icase(pattern, basename, 0) == 0);
636 }
637 /*
638 * match with FNM_PATHNAME; the pattern has base implicitly
@@ -645,7 +646,7 @@ static int path_matches(const char *pathname, int pathlen,
646 return 0;
647 if (baselen != 0)
648 baselen++;
648 - return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
649 + return fnmatch_icase(pattern, pathname + baselen, FNM_PATHNAME) == 0;
650 }
651
652 static int macroexpand_one(int attr_nr, int rem);
t/t0003-attributes.sh
+58 -1
@@ -9,7 +9,7 @@ attr_check () {
9 path="$1"
10 expect="$2"
11
12 - git check-attr test -- "$path" >actual 2>err &&
12 + git $3 check-attr test -- "$path" >actual 2>err &&
13 echo "$path: test: $2" >expect &&
14 test_cmp expect actual &&
15 test_line_count = 0 err
@@ -27,6 +27,7 @@ test_expect_success 'setup' '
27 echo "onoff test -test"
28 echo "offon -test test"
29 echo "no notest"
30 + echo "A/e/F test=A/e/F"
31 ) >.gitattributes &&
32 (
33 echo "g test=a/g" &&
@@ -93,6 +94,62 @@ test_expect_success 'attribute test' '
94
95 '
96
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 +
153 test_expect_success 'unnormalized paths' '
154
155 attr_check ./f f &&