Skip to content

Commit f6c64c6

Browse files
committed
Merge branch 'bw/attr-pathspec'
The pathspec mechanism learned to further limit the paths that match the pattern to those that have specified attributes attached via the gitattributes mechanism. * bw/attr-pathspec: pathspec: allow escaped query values pathspec: allow querying for attributes
2 parents 60cf6cd + c5af19f commit f6c64c6

File tree

7 files changed

+446
-10
lines changed

7 files changed

+446
-10
lines changed

Documentation/glossary-content.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,27 @@ full pathname may have special meaning:
384384
+
385385
Glob magic is incompatible with literal magic.
386386

387+
attr;;
388+
After `attr:` comes a space separated list of "attribute
389+
requirements", all of which must be met in order for the
390+
path to be considered a match; this is in addition to the
391+
usual non-magic pathspec pattern matching.
392+
See linkgit:gitattributes[5].
393+
+
394+
Each of the attribute requirements for the path takes one of
395+
these forms:
396+
397+
- "`ATTR`" requires that the attribute `ATTR` be set.
398+
399+
- "`-ATTR`" requires that the attribute `ATTR` be unset.
400+
401+
- "`ATTR=VALUE`" requires that the attribute `ATTR` be
402+
set to the string `VALUE`.
403+
404+
- "`!ATTR`" requires that the attribute `ATTR` be
405+
unspecified.
406+
+
407+
387408
exclude;;
388409
After a path matches any non-exclude pathspec, it will be run
389410
through all exclude pathspec (magic signature: `!` or its

attr.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,23 @@ struct attr_check *attr_check_initl(const char *one, ...)
603603
return check;
604604
}
605605

606+
struct attr_check *attr_check_dup(const struct attr_check *check)
607+
{
608+
struct attr_check *ret;
609+
610+
if (!check)
611+
return NULL;
612+
613+
ret = attr_check_alloc();
614+
615+
ret->nr = check->nr;
616+
ret->alloc = check->alloc;
617+
ALLOC_ARRAY(ret->items, ret->nr);
618+
COPY_ARRAY(ret->items, check->items, ret->nr);
619+
620+
return ret;
621+
}
622+
606623
struct attr_check_item *attr_check_append(struct attr_check *check,
607624
const struct git_attr *attr)
608625
{

attr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct attr_check {
4444

4545
extern struct attr_check *attr_check_alloc(void);
4646
extern struct attr_check *attr_check_initl(const char *, ...);
47+
extern struct attr_check *attr_check_dup(const struct attr_check *check);
4748

4849
extern struct attr_check_item *attr_check_append(struct attr_check *check,
4950
const struct git_attr *attr);

dir.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
#include "cache.h"
1111
#include "dir.h"
12+
#include "attr.h"
1213
#include "refs.h"
1314
#include "wildmatch.h"
1415
#include "pathspec.h"
@@ -134,7 +135,8 @@ static size_t common_prefix_len(const struct pathspec *pathspec)
134135
PATHSPEC_LITERAL |
135136
PATHSPEC_GLOB |
136137
PATHSPEC_ICASE |
137-
PATHSPEC_EXCLUDE);
138+
PATHSPEC_EXCLUDE |
139+
PATHSPEC_ATTR);
138140

139141
for (n = 0; n < pathspec->nr; n++) {
140142
size_t i = 0, len = 0, item_len;
@@ -209,6 +211,36 @@ int within_depth(const char *name, int namelen,
209211
#define DO_MATCH_DIRECTORY (1<<1)
210212
#define DO_MATCH_SUBMODULE (1<<2)
211213

214+
static int match_attrs(const char *name, int namelen,
215+
const struct pathspec_item *item)
216+
{
217+
int i;
218+
219+
git_check_attr(name, item->attr_check);
220+
for (i = 0; i < item->attr_match_nr; i++) {
221+
const char *value;
222+
int matched;
223+
enum attr_match_mode match_mode;
224+
225+
value = item->attr_check->items[i].value;
226+
match_mode = item->attr_match[i].match_mode;
227+
228+
if (ATTR_TRUE(value))
229+
matched = (match_mode == MATCH_SET);
230+
else if (ATTR_FALSE(value))
231+
matched = (match_mode == MATCH_UNSET);
232+
else if (ATTR_UNSET(value))
233+
matched = (match_mode == MATCH_UNSPECIFIED);
234+
else
235+
matched = (match_mode == MATCH_VALUE &&
236+
!strcmp(item->attr_match[i].value, value));
237+
if (!matched)
238+
return 0;
239+
}
240+
241+
return 1;
242+
}
243+
212244
/*
213245
* Does 'match' match the given name?
214246
* A match is found if
@@ -261,6 +293,9 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix,
261293
strncmp(item->match, name - prefix, item->prefix))
262294
return 0;
263295

296+
if (item->attr_match_nr && !match_attrs(name, namelen, item))
297+
return 0;
298+
264299
/* If the match was just the prefix, we matched */
265300
if (!*match)
266301
return MATCHED_RECURSIVELY;
@@ -339,7 +374,8 @@ static int do_match_pathspec(const struct pathspec *ps,
339374
PATHSPEC_LITERAL |
340375
PATHSPEC_GLOB |
341376
PATHSPEC_ICASE |
342-
PATHSPEC_EXCLUDE);
377+
PATHSPEC_EXCLUDE |
378+
PATHSPEC_ATTR);
343379

344380
if (!ps->nr) {
345381
if (!ps->recursive ||
@@ -1361,7 +1397,8 @@ static int simplify_away(const char *path, int pathlen,
13611397
PATHSPEC_LITERAL |
13621398
PATHSPEC_GLOB |
13631399
PATHSPEC_ICASE |
1364-
PATHSPEC_EXCLUDE);
1400+
PATHSPEC_EXCLUDE |
1401+
PATHSPEC_ATTR);
13651402

13661403
for (i = 0; i < pathspec->nr; i++) {
13671404
const struct pathspec_item *item = &pathspec->items[i];

pathspec.c

Lines changed: 153 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "dir.h"
33
#include "pathspec.h"
4+
#include "attr.h"
45

56
/*
67
* Finds which of the given pathspecs match items in the index.
@@ -72,6 +73,7 @@ static struct pathspec_magic {
7273
{ PATHSPEC_GLOB, '\0', "glob" },
7374
{ PATHSPEC_ICASE, '\0', "icase" },
7475
{ PATHSPEC_EXCLUDE, '!', "exclude" },
76+
{ PATHSPEC_ATTR, '\0', "attr" },
7577
};
7678

7779
static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
@@ -87,6 +89,116 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
8789
strbuf_addf(sb, ",prefix:%d)", prefixlen);
8890
}
8991

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;
140+
struct string_list list = STRING_LIST_INIT_DUP;
141+
142+
if (item->attr_check || item->attr_match)
143+
die(_("Only one 'attr:' specification is allowed."));
144+
145+
if (!value || !*value)
146+
die(_("attr spec must not be empty"));
147+
148+
string_list_split(&list, value, ' ', -1);
149+
string_list_remove_empty_items(&list, 0);
150+
151+
item->attr_check = attr_check_alloc();
152+
item->attr_match = xcalloc(list.nr, sizeof(struct attr_match));
153+
154+
for_each_string_list_item(si, &list) {
155+
size_t attr_len;
156+
char *attr_name;
157+
const struct git_attr *a;
158+
159+
int j = item->attr_match_nr++;
160+
const char *attr = si->string;
161+
struct attr_match *am = &item->attr_match[j];
162+
163+
switch (*attr) {
164+
case '!':
165+
am->match_mode = MATCH_UNSPECIFIED;
166+
attr++;
167+
attr_len = strlen(attr);
168+
break;
169+
case '-':
170+
am->match_mode = MATCH_UNSET;
171+
attr++;
172+
attr_len = strlen(attr);
173+
break;
174+
default:
175+
attr_len = strcspn(attr, "=");
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;
181+
am->value = attr_value_unescape(v);
182+
}
183+
break;
184+
}
185+
186+
attr_name = xmemdupz(attr, attr_len);
187+
a = git_attr(attr_name);
188+
if (!a)
189+
die(_("invalid attribute name %s"), attr_name);
190+
191+
attr_check_append(item->attr_check, a);
192+
193+
free(attr_name);
194+
}
195+
196+
if (item->attr_check->nr != item->attr_match_nr)
197+
die("BUG: should have same number of entries");
198+
199+
string_list_clear(&list, 0);
200+
}
201+
90202
static inline int get_literal_global(void)
91203
{
92204
static int literal = -1;
@@ -164,13 +276,14 @@ static int get_global_magic(int element_magic)
164276
* returns the position in 'elem' after all magic has been parsed
165277
*/
166278
static const char *parse_long_magic(unsigned *magic, int *prefix_len,
279+
struct pathspec_item *item,
167280
const char *elem)
168281
{
169282
const char *pos;
170283
const char *nextat;
171284

172285
for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
173-
size_t len = strcspn(pos, ",)");
286+
size_t len = strcspn_escaped(pos, ",)");
174287
int i;
175288

176289
if (pos[len] == ',')
@@ -189,6 +302,14 @@ static const char *parse_long_magic(unsigned *magic, int *prefix_len,
189302
continue;
190303
}
191304

305+
if (starts_with(pos, "attr:")) {
306+
char *attr_body = xmemdupz(pos + 5, len - 5);
307+
parse_pathspec_attr_match(item, attr_body);
308+
*magic |= PATHSPEC_ATTR;
309+
free(attr_body);
310+
continue;
311+
}
312+
192313
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
193314
if (strlen(pathspec_magic[i].name) == len &&
194315
!strncmp(pathspec_magic[i].name, pos, len)) {
@@ -252,13 +373,14 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)
252373
}
253374

254375
static const char *parse_element_magic(unsigned *magic, int *prefix_len,
376+
struct pathspec_item *item,
255377
const char *elem)
256378
{
257379
if (elem[0] != ':' || get_literal_global())
258380
return elem; /* nothing to do */
259381
else if (elem[1] == '(')
260382
/* longhand */
261-
return parse_long_magic(magic, prefix_len, elem);
383+
return parse_long_magic(magic, prefix_len, item, elem);
262384
else
263385
/* shorthand */
264386
return parse_short_magic(magic, elem);
@@ -335,12 +457,17 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
335457
char *match;
336458
int pathspec_prefix = -1;
337459

460+
item->attr_check = NULL;
461+
item->attr_match = NULL;
462+
item->attr_match_nr = 0;
463+
338464
/* PATHSPEC_LITERAL_PATH ignores magic */
339465
if (flags & PATHSPEC_LITERAL_PATH) {
340466
magic = PATHSPEC_LITERAL;
341467
} else {
342468
copyfrom = parse_element_magic(&element_magic,
343469
&pathspec_prefix,
470+
item,
344471
elt);
345472
magic |= element_magic;
346473
magic |= get_global_magic(element_magic);
@@ -565,26 +692,46 @@ void parse_pathspec(struct pathspec *pathspec,
565692

566693
void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
567694
{
568-
int i;
695+
int i, j;
569696

570697
*dst = *src;
571698
ALLOC_ARRAY(dst->items, dst->nr);
572699
COPY_ARRAY(dst->items, src->items, dst->nr);
573700

574701
for (i = 0; i < dst->nr; i++) {
575-
dst->items[i].match = xstrdup(src->items[i].match);
576-
dst->items[i].original = xstrdup(src->items[i].original);
702+
struct pathspec_item *d = &dst->items[i];
703+
struct pathspec_item *s = &src->items[i];
704+
705+
d->match = xstrdup(s->match);
706+
d->original = xstrdup(s->original);
707+
708+
ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
709+
COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
710+
for (j = 0; j < d->attr_match_nr; j++) {
711+
const char *value = s->attr_match[j].value;
712+
d->attr_match[j].value = xstrdup_or_null(value);
713+
}
714+
715+
d->attr_check = attr_check_dup(s->attr_check);
577716
}
578717
}
579718

580719
void clear_pathspec(struct pathspec *pathspec)
581720
{
582-
int i;
721+
int i, j;
583722

584723
for (i = 0; i < pathspec->nr; i++) {
585724
free(pathspec->items[i].match);
586725
free(pathspec->items[i].original);
726+
727+
for (j = 0; j < pathspec->items[j].attr_match_nr; j++)
728+
free(pathspec->items[i].attr_match[j].value);
729+
free(pathspec->items[i].attr_match);
730+
731+
if (pathspec->items[i].attr_check)
732+
attr_check_free(pathspec->items[i].attr_check);
587733
}
734+
588735
free(pathspec->items);
589736
pathspec->items = NULL;
590737
pathspec->nr = 0;

0 commit comments

Comments
 (0)