Skip to content

Commit c5af19f

Browse files
bmwillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b0db704 commit c5af19f

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

pathspec.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,51 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
8989
strbuf_addf(sb, ",prefix:%d)", prefixlen);
9090
}
9191

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+
92137
static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
93138
{
94139
struct string_list_item *si;
@@ -131,10 +176,9 @@ static void parse_pathspec_attr_match(struct pathspec_item *item, const char *va
131176
if (attr[attr_len] != '=')
132177
am->match_mode = MATCH_SET;
133178
else {
179+
const char *v = &attr[attr_len + 1];
134180
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);
138182
}
139183
break;
140184
}
@@ -239,7 +283,7 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,
239283
const char *nextat;
240284

241285
for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
242-
size_t len = strcspn(pos, ",)");
286+
size_t len = strcspn_escaped(pos, ",)");
243287
int i;
244288

245289
if (pos[len] == ',')

t/t6135-pathspec-with-attrs.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,23 @@ test_expect_success 'abort on asking for wrong magic' '
178178
test_must_fail git ls-files . ":(attr:!label=foo)"
179179
'
180180

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+
181200
test_done

0 commit comments

Comments
 (0)