1e5df2316e6624f6e9dd9b6aaa25a874b5409525
1#include "cache.h"
2#include "dir.h"
3#include "pathspec.h"
4#include "attr.h"
5
6/*
7 * Finds which of the given pathspecs match items in the index.
8 *
9 * For each pathspec, sets the corresponding entry in the seen[] array
10 * (which should be specs items long, i.e. the same size as pathspec)
11 * to the nature of the "closest" (i.e. most specific) match found for
12 * that pathspec in the index, if it was a closer type of match than
13 * the existing entry. As an optimization, matching is skipped
14 * altogether if seen[] already only contains non-zero entries.
15 *
16 * If seen[] has not already been written to, it may make sense
17 * to use find_pathspecs_matching_against_index() instead.
18 */
19void add_pathspec_matches_against_index(const struct pathspec *pathspec,
20 char *seen)
21{
22 int num_unmatched = 0, i;
23
24 /*
25 * Since we are walking the index as if we were walking the directory,
26 * we have to mark the matched pathspec as seen; otherwise we will
27 * mistakenly think that the user gave a pathspec that did not match
28 * anything.
29 */
30 for (i = 0; i < pathspec->nr; i++)
31 if (!seen[i])
32 num_unmatched++;
33 if (!num_unmatched)
34 return;
35 for (i = 0; i < active_nr; i++) {
36 const struct cache_entry *ce = active_cache[i];
37 ce_path_match(ce, pathspec, seen);
38 }
39}
40
41/*
42 * Finds which of the given pathspecs match items in the index.
43 *
44 * This is a one-shot wrapper around add_pathspec_matches_against_index()
45 * which allocates, populates, and returns a seen[] array indicating the
46 * nature of the "closest" (i.e. most specific) matches which each of the
47 * given pathspecs achieves against all items in the index.
48 */
49char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
50{
51 char *seen = xcalloc(pathspec->nr, 1);
52 add_pathspec_matches_against_index(pathspec, seen);
53 return seen;
54}
55
56/*
57 * Magic pathspec
58 *
59 * Possible future magic semantics include stuff like:
60 *
61 * { PATHSPEC_RECURSIVE, '*', "recursive" },
62 * { PATHSPEC_REGEXP, '\0', "regexp" },
63 *
64 */
65
66static struct pathspec_magic {
67 unsigned bit;
68 char mnemonic; /* this cannot be ':'! */
69 const char *name;
70} pathspec_magic[] = {
71 { PATHSPEC_FROMTOP, '/', "top" },
72 { PATHSPEC_LITERAL, '\0', "literal" },
73 { PATHSPEC_GLOB, '\0', "glob" },
74 { PATHSPEC_ICASE, '\0', "icase" },
75 { PATHSPEC_EXCLUDE, '!', "exclude" },
76 { PATHSPEC_ATTR, '\0', "attr" },
77};
78
79static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
80{
81 int i;
82 strbuf_addstr(sb, ":(");
83 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
84 if (magic & pathspec_magic[i].bit) {
85 if (sb->buf[sb->len - 1] != '(')
86 strbuf_addch(sb, ',');
87 strbuf_addstr(sb, pathspec_magic[i].name);
88 }
89 strbuf_addf(sb, ",prefix:%d)", prefixlen);
90}
91
92static 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
109static inline int invalid_value_char(const char ch)
110{
111 if (isalnum(ch) || strchr(",-_", ch))
112 return 0;
113 return -1;
114}
115
116static 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
137static 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
202static inline int get_literal_global(void)
203{
204 static int literal = -1;
205
206 if (literal < 0)
207 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
208
209 return literal;
210}
211
212static inline int get_glob_global(void)
213{
214 static int glob = -1;
215
216 if (glob < 0)
217 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
218
219 return glob;
220}
221
222static inline int get_noglob_global(void)
223{
224 static int noglob = -1;
225
226 if (noglob < 0)
227 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
228
229 return noglob;
230}
231
232static inline int get_icase_global(void)
233{
234 static int icase = -1;
235
236 if (icase < 0)
237 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
238
239 return icase;
240}
241
242static int get_global_magic(int element_magic)
243{
244 int global_magic = 0;
245
246 if (get_literal_global())
247 global_magic |= PATHSPEC_LITERAL;
248
249 /* --glob-pathspec is overridden by :(literal) */
250 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
251 global_magic |= PATHSPEC_GLOB;
252
253 if (get_glob_global() && get_noglob_global())
254 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
255
256 if (get_icase_global())
257 global_magic |= PATHSPEC_ICASE;
258
259 if ((global_magic & PATHSPEC_LITERAL) &&
260 (global_magic & ~PATHSPEC_LITERAL))
261 die(_("global 'literal' pathspec setting is incompatible "
262 "with all other global pathspec settings"));
263
264 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
265 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
266 global_magic |= PATHSPEC_LITERAL;
267
268 return global_magic;
269}
270
271/*
272 * Parse the pathspec element looking for long magic
273 *
274 * saves all magic in 'magic'
275 * if prefix magic is used, save the prefix length in 'prefix_len'
276 * returns the position in 'elem' after all magic has been parsed
277 */
278static const char *parse_long_magic(unsigned *magic, int *prefix_len,
279 struct pathspec_item *item,
280 const char *elem)
281{
282 const char *pos;
283 const char *nextat;
284
285 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
286 size_t len = strcspn_escaped(pos, ",)");
287 int i;
288
289 if (pos[len] == ',')
290 nextat = pos + len + 1; /* handle ',' */
291 else
292 nextat = pos + len; /* handle ')' and '\0' */
293
294 if (!len)
295 continue;
296
297 if (starts_with(pos, "prefix:")) {
298 char *endptr;
299 *prefix_len = strtol(pos + 7, &endptr, 10);
300 if (endptr - pos != len)
301 die(_("invalid parameter for pathspec magic 'prefix'"));
302 continue;
303 }
304
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
313 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
314 if (strlen(pathspec_magic[i].name) == len &&
315 !strncmp(pathspec_magic[i].name, pos, len)) {
316 *magic |= pathspec_magic[i].bit;
317 break;
318 }
319 }
320
321 if (ARRAY_SIZE(pathspec_magic) <= i)
322 die(_("Invalid pathspec magic '%.*s' in '%s'"),
323 (int) len, pos, elem);
324 }
325
326 if (*pos != ')')
327 die(_("Missing ')' at the end of pathspec magic in '%s'"),
328 elem);
329 pos++;
330
331 return pos;
332}
333
334/*
335 * Parse the pathspec element looking for short magic
336 *
337 * saves all magic in 'magic'
338 * returns the position in 'elem' after all magic has been parsed
339 */
340static const char *parse_short_magic(unsigned *magic, const char *elem)
341{
342 const char *pos;
343
344 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
345 char ch = *pos;
346 int i;
347
348 /* Special case alias for '!' */
349 if (ch == '^') {
350 *magic |= PATHSPEC_EXCLUDE;
351 continue;
352 }
353
354 if (!is_pathspec_magic(ch))
355 break;
356
357 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
358 if (pathspec_magic[i].mnemonic == ch) {
359 *magic |= pathspec_magic[i].bit;
360 break;
361 }
362 }
363
364 if (ARRAY_SIZE(pathspec_magic) <= i)
365 die(_("Unimplemented pathspec magic '%c' in '%s'"),
366 ch, elem);
367 }
368
369 if (*pos == ':')
370 pos++;
371
372 return pos;
373}
374
375static const char *parse_element_magic(unsigned *magic, int *prefix_len,
376 struct pathspec_item *item,
377 const char *elem)
378{
379 if (elem[0] != ':' || get_literal_global())
380 return elem; /* nothing to do */
381 else if (elem[1] == '(')
382 /* longhand */
383 return parse_long_magic(magic, prefix_len, item, elem);
384 else
385 /* shorthand */
386 return parse_short_magic(magic, elem);
387}
388
389/*
390 * Perform the initialization of a pathspec_item based on a pathspec element.
391 */
392static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
393 const char *prefix, int prefixlen,
394 const char *elt)
395{
396 unsigned magic = 0, element_magic = 0;
397 const char *copyfrom = elt;
398 char *match;
399 int pathspec_prefix = -1;
400
401 item->attr_check = NULL;
402 item->attr_match = NULL;
403 item->attr_match_nr = 0;
404
405 /* PATHSPEC_LITERAL_PATH ignores magic */
406 if (flags & PATHSPEC_LITERAL_PATH) {
407 magic = PATHSPEC_LITERAL;
408 } else {
409 copyfrom = parse_element_magic(&element_magic,
410 &pathspec_prefix,
411 item,
412 elt);
413 magic |= element_magic;
414 magic |= get_global_magic(element_magic);
415 }
416
417 item->magic = magic;
418
419 if (pathspec_prefix >= 0 &&
420 (prefixlen || (prefix && *prefix)))
421 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
422
423 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
424 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
425
426 /* Create match string which will be used for pathspec matching */
427 if (pathspec_prefix >= 0) {
428 match = xstrdup(copyfrom);
429 prefixlen = pathspec_prefix;
430 } else if (magic & PATHSPEC_FROMTOP) {
431 match = xstrdup(copyfrom);
432 prefixlen = 0;
433 } else {
434 match = prefix_path_gently(prefix, prefixlen,
435 &prefixlen, copyfrom);
436 if (!match)
437 die(_("%s: '%s' is outside repository"), elt, copyfrom);
438 }
439
440 item->match = match;
441 item->len = strlen(item->match);
442 item->prefix = prefixlen;
443
444 /*
445 * Prefix the pathspec (keep all magic) and assign to
446 * original. Useful for passing to another command.
447 */
448 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
449 !get_literal_global()) {
450 struct strbuf sb = STRBUF_INIT;
451
452 /* Preserve the actual prefix length of each pattern */
453 prefix_magic(&sb, prefixlen, element_magic);
454
455 strbuf_addstr(&sb, match);
456 item->original = strbuf_detach(&sb, NULL);
457 } else {
458 item->original = xstrdup(elt);
459 }
460
461 if (magic & PATHSPEC_LITERAL) {
462 item->nowildcard_len = item->len;
463 } else {
464 item->nowildcard_len = simple_length(item->match);
465 if (item->nowildcard_len < prefixlen)
466 item->nowildcard_len = prefixlen;
467 }
468
469 item->flags = 0;
470 if (magic & PATHSPEC_GLOB) {
471 /*
472 * FIXME: should we enable ONESTAR in _GLOB for
473 * pattern "* * / * . c"?
474 */
475 } else {
476 if (item->nowildcard_len < item->len &&
477 item->match[item->nowildcard_len] == '*' &&
478 no_wildcard(item->match + item->nowildcard_len + 1))
479 item->flags |= PATHSPEC_ONESTAR;
480 }
481
482 /* sanity checks, pathspec matchers assume these are sane */
483 if (item->nowildcard_len > item->len ||
484 item->prefix > item->len) {
485 die ("BUG: error initializing pathspec_item");
486 }
487}
488
489static int pathspec_item_cmp(const void *a_, const void *b_)
490{
491 struct pathspec_item *a, *b;
492
493 a = (struct pathspec_item *)a_;
494 b = (struct pathspec_item *)b_;
495 return strcmp(a->match, b->match);
496}
497
498static void NORETURN unsupported_magic(const char *pattern,
499 unsigned magic)
500{
501 struct strbuf sb = STRBUF_INIT;
502 int i;
503 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
504 const struct pathspec_magic *m = pathspec_magic + i;
505 if (!(magic & m->bit))
506 continue;
507 if (sb.len)
508 strbuf_addstr(&sb, ", ");
509
510 if (m->mnemonic)
511 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
512 m->name, m->mnemonic);
513 else
514 strbuf_addf(&sb, "'%s'", m->name);
515 }
516 /*
517 * We may want to substitute "this command" with a command
518 * name. E.g. when add--interactive dies when running
519 * "checkout -p"
520 */
521 die(_("%s: pathspec magic not supported by this command: %s"),
522 pattern, sb.buf);
523}
524
525/*
526 * Given command line arguments and a prefix, convert the input to
527 * pathspec. die() if any magic in magic_mask is used.
528 */
529void parse_pathspec(struct pathspec *pathspec,
530 unsigned magic_mask, unsigned flags,
531 const char *prefix, const char **argv)
532{
533 struct pathspec_item *item;
534 const char *entry = argv ? *argv : NULL;
535 int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
536
537 memset(pathspec, 0, sizeof(*pathspec));
538
539 if (flags & PATHSPEC_MAXDEPTH_VALID)
540 pathspec->magic |= PATHSPEC_MAXDEPTH;
541
542 /* No arguments, no prefix -> no pathspec */
543 if (!entry && !prefix)
544 return;
545
546 if ((flags & PATHSPEC_PREFER_CWD) &&
547 (flags & PATHSPEC_PREFER_FULL))
548 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
549
550 /* No arguments with prefix -> prefix pathspec */
551 if (!entry) {
552 if (flags & PATHSPEC_PREFER_FULL)
553 return;
554
555 if (!(flags & PATHSPEC_PREFER_CWD))
556 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
557
558 pathspec->items = item = xcalloc(1, sizeof(*item));
559 item->match = xstrdup(prefix);
560 item->original = xstrdup(prefix);
561 item->nowildcard_len = item->len = strlen(prefix);
562 item->prefix = item->len;
563 pathspec->nr = 1;
564 return;
565 }
566
567 n = 0;
568 warn_empty_string = 1;
569 while (argv[n]) {
570 if (*argv[n] == '\0' && warn_empty_string) {
571 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
572 "please use . instead if you meant to match all paths"));
573 warn_empty_string = 0;
574 }
575 n++;
576 }
577
578 pathspec->nr = n;
579 ALLOC_ARRAY(pathspec->items, n + 1);
580 item = pathspec->items;
581 prefixlen = prefix ? strlen(prefix) : 0;
582
583 for (i = 0; i < n; i++) {
584 entry = argv[i];
585
586 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
587
588 if (item[i].magic & PATHSPEC_EXCLUDE)
589 nr_exclude++;
590 if (item[i].magic & magic_mask)
591 unsupported_magic(entry, item[i].magic & magic_mask);
592
593 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
594 has_symlink_leading_path(item[i].match, item[i].len)) {
595 die(_("pathspec '%s' is beyond a symbolic link"), entry);
596 }
597
598 if (item[i].nowildcard_len < item[i].len)
599 pathspec->has_wildcard = 1;
600 pathspec->magic |= item[i].magic;
601 }
602
603 /*
604 * If everything is an exclude pattern, add one positive pattern
605 * that matches everyting. We allocated an extra one for this.
606 */
607 if (nr_exclude == n) {
608 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
609 init_pathspec_item(item + n, 0, prefix, plen, "");
610 pathspec->nr++;
611 }
612
613 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
614 if (flags & PATHSPEC_KEEP_ORDER)
615 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
616 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
617 }
618}
619
620void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
621{
622 int i, j;
623
624 *dst = *src;
625 ALLOC_ARRAY(dst->items, dst->nr);
626 COPY_ARRAY(dst->items, src->items, dst->nr);
627
628 for (i = 0; i < dst->nr; i++) {
629 struct pathspec_item *d = &dst->items[i];
630 struct pathspec_item *s = &src->items[i];
631
632 d->match = xstrdup(s->match);
633 d->original = xstrdup(s->original);
634
635 ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
636 COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
637 for (j = 0; j < d->attr_match_nr; j++) {
638 const char *value = s->attr_match[j].value;
639 d->attr_match[j].value = xstrdup_or_null(value);
640 }
641
642 d->attr_check = attr_check_dup(s->attr_check);
643 }
644}
645
646void clear_pathspec(struct pathspec *pathspec)
647{
648 int i, j;
649
650 for (i = 0; i < pathspec->nr; i++) {
651 free(pathspec->items[i].match);
652 free(pathspec->items[i].original);
653
654 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
655 free(pathspec->items[i].attr_match[j].value);
656 free(pathspec->items[i].attr_match);
657
658 if (pathspec->items[i].attr_check)
659 attr_check_free(pathspec->items[i].attr_check);
660 }
661
662 free(pathspec->items);
663 pathspec->items = NULL;
664 pathspec->nr = 0;
665}