pathspec: allow escaped query values

In our own .gitattributes file we have attributes such as: *.[ch] whitespace=indent,trail,space When querying for attributes we want to be able to ask for the exact value, i.e. git ls-files :(attr:whitespace=indent,trail,space) should work, but the commas are used in the attr magic to introduce the next attr, such that this query currently fails with fatal: Invalid pathspec magic 'trail' in ':(attr:whitespace=indent,trail,space)' This change allows escaping characters by a backslash, such that the query git ls-files :(attr:whitespace=indent\,trail\,space) will match all path that have the value "indent,trail,space" for the whitespace attribute. To accomplish this, we need to modify two places. First `parse_long_magic` needs to not stop early upon seeing a comma or closing paren that is escaped. As a second step we need to remove any escaping from the attr value. Based on a patch by Stefan Beller <sbeller@google.com> Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Mar 13, 2017 at 11:23 UTC c5af19f9ab4cd8582689ca9d9c84f188f4442d10
2 files changed +67 -4
pathspec.c
+48 -4
@@ -89,6 +89,51 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
89 strbuf_addf(sb, ",prefix:%d)", prefixlen);
90 }
91
92 +static size_t strcspn_escaped(const char *s, const char *stop)
93 +{
94 + const char *i;
95 +
96 + for (i = s; *i; i++) {
97 + /* skip the escaped character */
98 + if (i[0] == '\\' && i[1]) {
99 + i++;
100 + continue;
101 + }
102 +
103 + if (strchr(stop, *i))
104 + break;
105 + }
106 + return i - s;
107 +}
108 +
109 +static inline int invalid_value_char(const char ch)
110 +{
111 + if (isalnum(ch) || strchr(",-_", ch))
112 + return 0;
113 + return -1;
114 +}
115 +
116 +static char *attr_value_unescape(const char *value)
117 +{
118 + const char *src;
119 + char *dst, *ret;
120 +
121 + ret = xmallocz(strlen(value));
122 + for (src = value, dst = ret; *src; src++, dst++) {
123 + if (*src == '\\') {
124 + if (!src[1])
125 + die(_("Escape character '\\' not allowed as "
126 + "last character in attr value"));
127 + src++;
128 + }
129 + if (invalid_value_char(*src))
130 + die("cannot use '%c' for value matching", *src);
131 + *dst = *src;
132 + }
133 + *dst = '\0';
134 + return ret;
135 +}
136 +
137 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
138 {
139 struct string_list_item *si;
@@ -131,10 +176,9 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *va
176 if (attr[attr_len] != '=')
177 am->match_mode = MATCH_SET;
178 else {
179 + const char *v = &attr[attr_len + 1];
180 am->match_mode = MATCH_VALUE;
135 - am->value = xstrdup(&attr[attr_len + 1]);
136 - if (strchr(am->value, '\\'))
137 - die(_("attr spec values must not contain backslashes"));
181 + am->value = attr_value_unescape(v);
182 }
183 break;
184 }
@@ -239,7 +283,7 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,
283 const char *nextat;
284
285 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
242 - size_t len = strcspn(pos, ",)");
286 + size_t len = strcspn_escaped(pos, ",)");
287 int i;
288
289 if (pos[len] == ',')
t/t6135-pathspec-with-attrs.sh
+19
@@ -178,4 +178,23 @@ test_expect_success 'abort on asking for wrong magic' '
178 test_must_fail git ls-files . ":(attr:!label=foo)"
179 '
180
181 +test_expect_success 'check attribute list' '
182 + cat <<-EOF >>.gitattributes &&
183 + * whitespace=indent,trail,space
184 + EOF
185 + git ls-files ":(attr:whitespace=indent\,trail\,space)" >actual &&
186 + git ls-files >expect &&
187 + test_cmp expect actual
188 +'
189 +
190 +test_expect_success 'backslash cannot be the last character' '
191 + test_must_fail git ls-files ":(attr:label=foo\\ labelA=bar)" 2>actual &&
192 + test_i18ngrep "not allowed as last character in attr value" actual
193 +'
194 +
195 +test_expect_success 'backslash cannot be used as a value' '
196 + test_must_fail git ls-files ":(attr:label=f\\\oo)" 2>actual &&
197 + test_i18ngrep "for value matching" actual
198 +'
199 +
200 test_done