attr: support quoting pathname patterns in C style

Full pattern must be quoted. So 'pat"t"ern attr' will give exactly 'pat"t"ern', not 'pattern'. Also clarify that leading whitespaces are not part of the pattern and document comment syntax. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 27, 2017 at 18:01 UTC 860a74d9d909002b16c50b58050908756065125d
3 files changed +44 -5
Documentation/gitattributes.txt
+5 -3
@@ -21,9 +21,11 @@ Each line in `gitattributes` file is of form:
21 pattern attr1 attr2 ...
22
23 That is, a pattern followed by an attributes list,
24 -separated by whitespaces. When the pattern matches the
25 -path in question, the attributes listed on the line are given to
26 -the path.
24 +separated by whitespaces. Leading and trailing whitespaces are
25 +ignored. Lines that begin with '#' are ignored. Patterns
26 +that begin with a double quote are quoted in C style.
27 +When the pattern matches the path in question, the attributes
28 +listed on the line are given to the path.
29
30 Each attribute can be in one of these states for a given path:
31
attr.c
+13 -2
@@ -13,6 +13,7 @@
13 #include "attr.h"
14 #include "dir.h"
15 #include "utf8.h"
16 +#include "quote.h"
17
18 const char git_attr__true[] = "(builtin)true";
19 const char git_attr__false[] = "\0(builtin)false";
@@ -212,12 +213,21 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
213 const char *cp, *name, *states;
214 struct match_attr *res = NULL;
215 int is_macro;
216 + struct strbuf pattern = STRBUF_INIT;
217
218 cp = line + strspn(line, blank);
219 if (!*cp || *cp == '#')
220 return NULL;
221 name = cp;
220 - namelen = strcspn(name, blank);
222 +
223 + if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) {
224 + name = pattern.buf;
225 + namelen = pattern.len;
226 + } else {
227 + namelen = strcspn(name, blank);
228 + states = name + namelen;
229 + }
230 +
231 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
232 starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
233 if (!macro_ok) {
@@ -239,7 +249,6 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
249 else
250 is_macro = 0;
251
242 - states = name + namelen;
252 states += strspn(states, blank);
253
254 /* First pass to count the attr_states */
@@ -282,9 +291,11 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
291 cannot_trust_maybe_real = 1;
292 }
293
294 + strbuf_release(&pattern);
295 return res;
296
297 fail_return:
298 + strbuf_release(&pattern);
299 free(res);
300 return NULL;
301 }
t/t0003-attributes.sh
+26
@@ -13,10 +13,31 @@ attr_check () {
13 test_line_count = 0 err
14 }
15
16 +attr_check_quote () {
17 +
18 + path="$1"
19 + quoted_path="$2"
20 + expect="$3"
21 +
22 + git check-attr test -- "$path" >actual &&
23 + echo "\"$quoted_path\": test: $expect" >expect &&
24 + test_cmp expect actual
25 +
26 +}
27 +
28 +test_expect_success 'open-quoted pathname' '
29 + echo "\"a test=a" >.gitattributes &&
30 + test_must_fail attr_check a a
31 +'
32 +
33 +
34 test_expect_success 'setup' '
35 mkdir -p a/b/d a/c b &&
36 (
37 echo "[attr]notest !test"
38 + echo "\" d \" test=d"
39 + echo " e test=e"
40 + echo " e\" test=e"
41 echo "f test=f"
42 echo "a/i test=a/i"
43 echo "onoff test -test"
@@ -69,6 +90,11 @@ test_expect_success 'command line checks' '
90 '
91
92 test_expect_success 'attribute test' '
93 +
94 + attr_check " d " d &&
95 + attr_check e e &&
96 + attr_check_quote e\" e\\\" e &&
97 +
98 attr_check f f &&
99 attr_check a/f f &&
100 attr_check a/c/f f &&