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