]> git.ipfire.org Git - thirdparty/git.git/blob - pathspec.c
config: don't include config.h by default
[thirdparty/git.git] / pathspec.c
1 #include "cache.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "pathspec.h"
5 #include "attr.h"
6
7 /*
8 * Finds which of the given pathspecs match items in the index.
9 *
10 * For each pathspec, sets the corresponding entry in the seen[] array
11 * (which should be specs items long, i.e. the same size as pathspec)
12 * to the nature of the "closest" (i.e. most specific) match found for
13 * that pathspec in the index, if it was a closer type of match than
14 * the existing entry. As an optimization, matching is skipped
15 * altogether if seen[] already only contains non-zero entries.
16 *
17 * If seen[] has not already been written to, it may make sense
18 * to use find_pathspecs_matching_against_index() instead.
19 */
20 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
21 char *seen)
22 {
23 int num_unmatched = 0, i;
24
25 /*
26 * Since we are walking the index as if we were walking the directory,
27 * we have to mark the matched pathspec as seen; otherwise we will
28 * mistakenly think that the user gave a pathspec that did not match
29 * anything.
30 */
31 for (i = 0; i < pathspec->nr; i++)
32 if (!seen[i])
33 num_unmatched++;
34 if (!num_unmatched)
35 return;
36 for (i = 0; i < active_nr; i++) {
37 const struct cache_entry *ce = active_cache[i];
38 ce_path_match(ce, pathspec, seen);
39 }
40 }
41
42 /*
43 * Finds which of the given pathspecs match items in the index.
44 *
45 * This is a one-shot wrapper around add_pathspec_matches_against_index()
46 * which allocates, populates, and returns a seen[] array indicating the
47 * nature of the "closest" (i.e. most specific) matches which each of the
48 * given pathspecs achieves against all items in the index.
49 */
50 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
51 {
52 char *seen = xcalloc(pathspec->nr, 1);
53 add_pathspec_matches_against_index(pathspec, seen);
54 return seen;
55 }
56
57 /*
58 * Magic pathspec
59 *
60 * Possible future magic semantics include stuff like:
61 *
62 * { PATHSPEC_RECURSIVE, '*', "recursive" },
63 * { PATHSPEC_REGEXP, '\0', "regexp" },
64 *
65 */
66
67 static struct pathspec_magic {
68 unsigned bit;
69 char mnemonic; /* this cannot be ':'! */
70 const char *name;
71 } pathspec_magic[] = {
72 { PATHSPEC_FROMTOP, '/', "top" },
73 { PATHSPEC_LITERAL, '\0', "literal" },
74 { PATHSPEC_GLOB, '\0', "glob" },
75 { PATHSPEC_ICASE, '\0', "icase" },
76 { PATHSPEC_EXCLUDE, '!', "exclude" },
77 { PATHSPEC_ATTR, '\0', "attr" },
78 };
79
80 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
81 {
82 int i;
83 strbuf_addstr(sb, ":(");
84 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
85 if (magic & pathspec_magic[i].bit) {
86 if (sb->buf[sb->len - 1] != '(')
87 strbuf_addch(sb, ',');
88 strbuf_addstr(sb, pathspec_magic[i].name);
89 }
90 strbuf_addf(sb, ",prefix:%d)", prefixlen);
91 }
92
93 static size_t strcspn_escaped(const char *s, const char *stop)
94 {
95 const char *i;
96
97 for (i = s; *i; i++) {
98 /* skip the escaped character */
99 if (i[0] == '\\' && i[1]) {
100 i++;
101 continue;
102 }
103
104 if (strchr(stop, *i))
105 break;
106 }
107 return i - s;
108 }
109
110 static inline int invalid_value_char(const char ch)
111 {
112 if (isalnum(ch) || strchr(",-_", ch))
113 return 0;
114 return -1;
115 }
116
117 static char *attr_value_unescape(const char *value)
118 {
119 const char *src;
120 char *dst, *ret;
121
122 ret = xmallocz(strlen(value));
123 for (src = value, dst = ret; *src; src++, dst++) {
124 if (*src == '\\') {
125 if (!src[1])
126 die(_("Escape character '\\' not allowed as "
127 "last character in attr value"));
128 src++;
129 }
130 if (invalid_value_char(*src))
131 die("cannot use '%c' for value matching", *src);
132 *dst = *src;
133 }
134 *dst = '\0';
135 return ret;
136 }
137
138 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
139 {
140 struct string_list_item *si;
141 struct string_list list = STRING_LIST_INIT_DUP;
142
143 if (item->attr_check || item->attr_match)
144 die(_("Only one 'attr:' specification is allowed."));
145
146 if (!value || !*value)
147 die(_("attr spec must not be empty"));
148
149 string_list_split(&list, value, ' ', -1);
150 string_list_remove_empty_items(&list, 0);
151
152 item->attr_check = attr_check_alloc();
153 item->attr_match = xcalloc(list.nr, sizeof(struct attr_match));
154
155 for_each_string_list_item(si, &list) {
156 size_t attr_len;
157 char *attr_name;
158 const struct git_attr *a;
159
160 int j = item->attr_match_nr++;
161 const char *attr = si->string;
162 struct attr_match *am = &item->attr_match[j];
163
164 switch (*attr) {
165 case '!':
166 am->match_mode = MATCH_UNSPECIFIED;
167 attr++;
168 attr_len = strlen(attr);
169 break;
170 case '-':
171 am->match_mode = MATCH_UNSET;
172 attr++;
173 attr_len = strlen(attr);
174 break;
175 default:
176 attr_len = strcspn(attr, "=");
177 if (attr[attr_len] != '=')
178 am->match_mode = MATCH_SET;
179 else {
180 const char *v = &attr[attr_len + 1];
181 am->match_mode = MATCH_VALUE;
182 am->value = attr_value_unescape(v);
183 }
184 break;
185 }
186
187 attr_name = xmemdupz(attr, attr_len);
188 a = git_attr(attr_name);
189 if (!a)
190 die(_("invalid attribute name %s"), attr_name);
191
192 attr_check_append(item->attr_check, a);
193
194 free(attr_name);
195 }
196
197 if (item->attr_check->nr != item->attr_match_nr)
198 die("BUG: should have same number of entries");
199
200 string_list_clear(&list, 0);
201 }
202
203 static inline int get_literal_global(void)
204 {
205 static int literal = -1;
206
207 if (literal < 0)
208 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
209
210 return literal;
211 }
212
213 static inline int get_glob_global(void)
214 {
215 static int glob = -1;
216
217 if (glob < 0)
218 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
219
220 return glob;
221 }
222
223 static inline int get_noglob_global(void)
224 {
225 static int noglob = -1;
226
227 if (noglob < 0)
228 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
229
230 return noglob;
231 }
232
233 static inline int get_icase_global(void)
234 {
235 static int icase = -1;
236
237 if (icase < 0)
238 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
239
240 return icase;
241 }
242
243 static int get_global_magic(int element_magic)
244 {
245 int global_magic = 0;
246
247 if (get_literal_global())
248 global_magic |= PATHSPEC_LITERAL;
249
250 /* --glob-pathspec is overridden by :(literal) */
251 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
252 global_magic |= PATHSPEC_GLOB;
253
254 if (get_glob_global() && get_noglob_global())
255 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
256
257 if (get_icase_global())
258 global_magic |= PATHSPEC_ICASE;
259
260 if ((global_magic & PATHSPEC_LITERAL) &&
261 (global_magic & ~PATHSPEC_LITERAL))
262 die(_("global 'literal' pathspec setting is incompatible "
263 "with all other global pathspec settings"));
264
265 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
266 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
267 global_magic |= PATHSPEC_LITERAL;
268
269 return global_magic;
270 }
271
272 /*
273 * Parse the pathspec element looking for long magic
274 *
275 * saves all magic in 'magic'
276 * if prefix magic is used, save the prefix length in 'prefix_len'
277 * returns the position in 'elem' after all magic has been parsed
278 */
279 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
280 struct pathspec_item *item,
281 const char *elem)
282 {
283 const char *pos;
284 const char *nextat;
285
286 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
287 size_t len = strcspn_escaped(pos, ",)");
288 int i;
289
290 if (pos[len] == ',')
291 nextat = pos + len + 1; /* handle ',' */
292 else
293 nextat = pos + len; /* handle ')' and '\0' */
294
295 if (!len)
296 continue;
297
298 if (starts_with(pos, "prefix:")) {
299 char *endptr;
300 *prefix_len = strtol(pos + 7, &endptr, 10);
301 if (endptr - pos != len)
302 die(_("invalid parameter for pathspec magic 'prefix'"));
303 continue;
304 }
305
306 if (starts_with(pos, "attr:")) {
307 char *attr_body = xmemdupz(pos + 5, len - 5);
308 parse_pathspec_attr_match(item, attr_body);
309 *magic |= PATHSPEC_ATTR;
310 free(attr_body);
311 continue;
312 }
313
314 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
315 if (strlen(pathspec_magic[i].name) == len &&
316 !strncmp(pathspec_magic[i].name, pos, len)) {
317 *magic |= pathspec_magic[i].bit;
318 break;
319 }
320 }
321
322 if (ARRAY_SIZE(pathspec_magic) <= i)
323 die(_("Invalid pathspec magic '%.*s' in '%s'"),
324 (int) len, pos, elem);
325 }
326
327 if (*pos != ')')
328 die(_("Missing ')' at the end of pathspec magic in '%s'"),
329 elem);
330 pos++;
331
332 return pos;
333 }
334
335 /*
336 * Parse the pathspec element looking for short magic
337 *
338 * saves all magic in 'magic'
339 * returns the position in 'elem' after all magic has been parsed
340 */
341 static const char *parse_short_magic(unsigned *magic, const char *elem)
342 {
343 const char *pos;
344
345 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
346 char ch = *pos;
347 int i;
348
349 /* Special case alias for '!' */
350 if (ch == '^') {
351 *magic |= PATHSPEC_EXCLUDE;
352 continue;
353 }
354
355 if (!is_pathspec_magic(ch))
356 break;
357
358 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
359 if (pathspec_magic[i].mnemonic == ch) {
360 *magic |= pathspec_magic[i].bit;
361 break;
362 }
363 }
364
365 if (ARRAY_SIZE(pathspec_magic) <= i)
366 die(_("Unimplemented pathspec magic '%c' in '%s'"),
367 ch, elem);
368 }
369
370 if (*pos == ':')
371 pos++;
372
373 return pos;
374 }
375
376 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
377 struct pathspec_item *item,
378 const char *elem)
379 {
380 if (elem[0] != ':' || get_literal_global())
381 return elem; /* nothing to do */
382 else if (elem[1] == '(')
383 /* longhand */
384 return parse_long_magic(magic, prefix_len, item, elem);
385 else
386 /* shorthand */
387 return parse_short_magic(magic, elem);
388 }
389
390 static void strip_submodule_slash_cheap(struct pathspec_item *item)
391 {
392 if (item->len >= 1 && item->match[item->len - 1] == '/') {
393 int i = cache_name_pos(item->match, item->len - 1);
394
395 if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
396 item->len--;
397 item->match[item->len] = '\0';
398 }
399 }
400 }
401
402 static void strip_submodule_slash_expensive(struct pathspec_item *item)
403 {
404 int i;
405
406 for (i = 0; i < active_nr; i++) {
407 struct cache_entry *ce = active_cache[i];
408 int ce_len = ce_namelen(ce);
409
410 if (!S_ISGITLINK(ce->ce_mode))
411 continue;
412
413 if (item->len <= ce_len || item->match[ce_len] != '/' ||
414 memcmp(ce->name, item->match, ce_len))
415 continue;
416
417 if (item->len == ce_len + 1) {
418 /* strip trailing slash */
419 item->len--;
420 item->match[item->len] = '\0';
421 } else {
422 die(_("Pathspec '%s' is in submodule '%.*s'"),
423 item->original, ce_len, ce->name);
424 }
425 }
426 }
427
428 static void die_inside_submodule_path(struct pathspec_item *item)
429 {
430 int i;
431
432 for (i = 0; i < active_nr; i++) {
433 struct cache_entry *ce = active_cache[i];
434 int ce_len = ce_namelen(ce);
435
436 if (!S_ISGITLINK(ce->ce_mode))
437 continue;
438
439 if (item->len < ce_len ||
440 !(item->match[ce_len] == '/' || item->match[ce_len] == '\0') ||
441 memcmp(ce->name, item->match, ce_len))
442 continue;
443
444 die(_("Pathspec '%s' is in submodule '%.*s'"),
445 item->original, ce_len, ce->name);
446 }
447 }
448
449 /*
450 * Perform the initialization of a pathspec_item based on a pathspec element.
451 */
452 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
453 const char *prefix, int prefixlen,
454 const char *elt)
455 {
456 unsigned magic = 0, element_magic = 0;
457 const char *copyfrom = elt;
458 char *match;
459 int pathspec_prefix = -1;
460
461 item->attr_check = NULL;
462 item->attr_match = NULL;
463 item->attr_match_nr = 0;
464
465 /* PATHSPEC_LITERAL_PATH ignores magic */
466 if (flags & PATHSPEC_LITERAL_PATH) {
467 magic = PATHSPEC_LITERAL;
468 } else {
469 copyfrom = parse_element_magic(&element_magic,
470 &pathspec_prefix,
471 item,
472 elt);
473 magic |= element_magic;
474 magic |= get_global_magic(element_magic);
475 }
476
477 item->magic = magic;
478
479 if (pathspec_prefix >= 0 &&
480 (prefixlen || (prefix && *prefix)))
481 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
482
483 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
484 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
485
486 /* Create match string which will be used for pathspec matching */
487 if (pathspec_prefix >= 0) {
488 match = xstrdup(copyfrom);
489 prefixlen = pathspec_prefix;
490 } else if (magic & PATHSPEC_FROMTOP) {
491 match = xstrdup(copyfrom);
492 prefixlen = 0;
493 } else {
494 match = prefix_path_gently(prefix, prefixlen,
495 &prefixlen, copyfrom);
496 if (!match)
497 die(_("%s: '%s' is outside repository"), elt, copyfrom);
498 }
499
500 item->match = match;
501 item->len = strlen(item->match);
502 item->prefix = prefixlen;
503
504 /*
505 * Prefix the pathspec (keep all magic) and assign to
506 * original. Useful for passing to another command.
507 */
508 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
509 !get_literal_global()) {
510 struct strbuf sb = STRBUF_INIT;
511
512 /* Preserve the actual prefix length of each pattern */
513 prefix_magic(&sb, prefixlen, element_magic);
514
515 strbuf_addstr(&sb, match);
516 item->original = strbuf_detach(&sb, NULL);
517 } else {
518 item->original = xstrdup(elt);
519 }
520
521 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
522 strip_submodule_slash_cheap(item);
523
524 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
525 strip_submodule_slash_expensive(item);
526
527 if (magic & PATHSPEC_LITERAL) {
528 item->nowildcard_len = item->len;
529 } else {
530 item->nowildcard_len = simple_length(item->match);
531 if (item->nowildcard_len < prefixlen)
532 item->nowildcard_len = prefixlen;
533 }
534
535 item->flags = 0;
536 if (magic & PATHSPEC_GLOB) {
537 /*
538 * FIXME: should we enable ONESTAR in _GLOB for
539 * pattern "* * / * . c"?
540 */
541 } else {
542 if (item->nowildcard_len < item->len &&
543 item->match[item->nowildcard_len] == '*' &&
544 no_wildcard(item->match + item->nowildcard_len + 1))
545 item->flags |= PATHSPEC_ONESTAR;
546 }
547
548 /* sanity checks, pathspec matchers assume these are sane */
549 if (item->nowildcard_len > item->len ||
550 item->prefix > item->len) {
551 /*
552 * This case can be triggered by the user pointing us to a
553 * pathspec inside a submodule, which is an input error.
554 * Detect that here and complain, but fallback in the
555 * non-submodule case to a BUG, as we have no idea what
556 * would trigger that.
557 */
558 die_inside_submodule_path(item);
559 die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
560 }
561 }
562
563 static int pathspec_item_cmp(const void *a_, const void *b_)
564 {
565 struct pathspec_item *a, *b;
566
567 a = (struct pathspec_item *)a_;
568 b = (struct pathspec_item *)b_;
569 return strcmp(a->match, b->match);
570 }
571
572 static void NORETURN unsupported_magic(const char *pattern,
573 unsigned magic)
574 {
575 struct strbuf sb = STRBUF_INIT;
576 int i;
577 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
578 const struct pathspec_magic *m = pathspec_magic + i;
579 if (!(magic & m->bit))
580 continue;
581 if (sb.len)
582 strbuf_addstr(&sb, ", ");
583
584 if (m->mnemonic)
585 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
586 m->name, m->mnemonic);
587 else
588 strbuf_addf(&sb, "'%s'", m->name);
589 }
590 /*
591 * We may want to substitute "this command" with a command
592 * name. E.g. when add--interactive dies when running
593 * "checkout -p"
594 */
595 die(_("%s: pathspec magic not supported by this command: %s"),
596 pattern, sb.buf);
597 }
598
599 /*
600 * Given command line arguments and a prefix, convert the input to
601 * pathspec. die() if any magic in magic_mask is used.
602 */
603 void parse_pathspec(struct pathspec *pathspec,
604 unsigned magic_mask, unsigned flags,
605 const char *prefix, const char **argv)
606 {
607 struct pathspec_item *item;
608 const char *entry = argv ? *argv : NULL;
609 int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
610
611 memset(pathspec, 0, sizeof(*pathspec));
612
613 if (flags & PATHSPEC_MAXDEPTH_VALID)
614 pathspec->magic |= PATHSPEC_MAXDEPTH;
615
616 /* No arguments, no prefix -> no pathspec */
617 if (!entry && !prefix)
618 return;
619
620 if ((flags & PATHSPEC_PREFER_CWD) &&
621 (flags & PATHSPEC_PREFER_FULL))
622 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
623
624 /* No arguments with prefix -> prefix pathspec */
625 if (!entry) {
626 if (flags & PATHSPEC_PREFER_FULL)
627 return;
628
629 if (!(flags & PATHSPEC_PREFER_CWD))
630 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
631
632 pathspec->items = item = xcalloc(1, sizeof(*item));
633 item->match = xstrdup(prefix);
634 item->original = xstrdup(prefix);
635 item->nowildcard_len = item->len = strlen(prefix);
636 item->prefix = item->len;
637 pathspec->nr = 1;
638 return;
639 }
640
641 n = 0;
642 warn_empty_string = 1;
643 while (argv[n]) {
644 if (*argv[n] == '\0' && warn_empty_string) {
645 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
646 "please use . instead if you meant to match all paths"));
647 warn_empty_string = 0;
648 }
649 n++;
650 }
651
652 pathspec->nr = n;
653 ALLOC_ARRAY(pathspec->items, n + 1);
654 item = pathspec->items;
655 prefixlen = prefix ? strlen(prefix) : 0;
656
657 for (i = 0; i < n; i++) {
658 entry = argv[i];
659
660 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
661
662 if (item[i].magic & PATHSPEC_EXCLUDE)
663 nr_exclude++;
664 if (item[i].magic & magic_mask)
665 unsupported_magic(entry, item[i].magic & magic_mask);
666
667 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
668 has_symlink_leading_path(item[i].match, item[i].len)) {
669 die(_("pathspec '%s' is beyond a symbolic link"), entry);
670 }
671
672 if (item[i].nowildcard_len < item[i].len)
673 pathspec->has_wildcard = 1;
674 pathspec->magic |= item[i].magic;
675 }
676
677 /*
678 * If everything is an exclude pattern, add one positive pattern
679 * that matches everyting. We allocated an extra one for this.
680 */
681 if (nr_exclude == n) {
682 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
683 init_pathspec_item(item + n, 0, prefix, plen, "");
684 pathspec->nr++;
685 }
686
687 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
688 if (flags & PATHSPEC_KEEP_ORDER)
689 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
690 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
691 }
692 }
693
694 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
695 {
696 int i, j;
697
698 *dst = *src;
699 ALLOC_ARRAY(dst->items, dst->nr);
700 COPY_ARRAY(dst->items, src->items, dst->nr);
701
702 for (i = 0; i < dst->nr; i++) {
703 struct pathspec_item *d = &dst->items[i];
704 struct pathspec_item *s = &src->items[i];
705
706 d->match = xstrdup(s->match);
707 d->original = xstrdup(s->original);
708
709 ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
710 COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
711 for (j = 0; j < d->attr_match_nr; j++) {
712 const char *value = s->attr_match[j].value;
713 d->attr_match[j].value = xstrdup_or_null(value);
714 }
715
716 d->attr_check = attr_check_dup(s->attr_check);
717 }
718 }
719
720 void clear_pathspec(struct pathspec *pathspec)
721 {
722 int i, j;
723
724 for (i = 0; i < pathspec->nr; i++) {
725 free(pathspec->items[i].match);
726 free(pathspec->items[i].original);
727
728 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
729 free(pathspec->items[i].attr_match[j].value);
730 free(pathspec->items[i].attr_match);
731
732 if (pathspec->items[i].attr_check)
733 attr_check_free(pathspec->items[i].attr_check);
734 }
735
736 free(pathspec->items);
737 pathspec->items = NULL;
738 pathspec->nr = 0;
739 }