config: don't implicitly use gitdir or commondir
[git.git] / pathspec.c
blobe4659b1440c94d0f65ef486a488b604d6195d81c
1 #include "cache.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "pathspec.h"
5 #include "attr.h"
7 /*
8 * Finds which of the given pathspecs match items in the index.
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.
17 * If seen[] has not already been written to, it may make sense
18 * to use find_pathspecs_matching_against_index() instead.
20 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
21 char *seen)
23 int num_unmatched = 0, i;
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.
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);
43 * Finds which of the given pathspecs match items in the index.
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.
50 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
52 char *seen = xcalloc(pathspec->nr, 1);
53 add_pathspec_matches_against_index(pathspec, seen);
54 return seen;
58 * Magic pathspec
60 * Possible future magic semantics include stuff like:
62 * { PATHSPEC_RECURSIVE, '*', "recursive" },
63 * { PATHSPEC_REGEXP, '\0', "regexp" },
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" },
80 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
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);
90 strbuf_addf(sb, ",prefix:%d)", prefixlen);
93 static size_t strcspn_escaped(const char *s, const char *stop)
95 const char *i;
97 for (i = s; *i; i++) {
98 /* skip the escaped character */
99 if (i[0] == '\\' && i[1]) {
100 i++;
101 continue;
104 if (strchr(stop, *i))
105 break;
107 return i - s;
110 static inline int invalid_value_char(const char ch)
112 if (isalnum(ch) || strchr(",-_", ch))
113 return 0;
114 return -1;
117 static char *attr_value_unescape(const char *value)
119 const char *src;
120 char *dst, *ret;
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++;
130 if (invalid_value_char(*src))
131 die("cannot use '%c' for value matching", *src);
132 *dst = *src;
134 *dst = '\0';
135 return ret;
138 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
140 struct string_list_item *si;
141 struct string_list list = STRING_LIST_INIT_DUP;
143 if (item->attr_check || item->attr_match)
144 die(_("Only one 'attr:' specification is allowed."));
146 if (!value || !*value)
147 die(_("attr spec must not be empty"));
149 string_list_split(&list, value, ' ', -1);
150 string_list_remove_empty_items(&list, 0);
152 item->attr_check = attr_check_alloc();
153 item->attr_match = xcalloc(list.nr, sizeof(struct attr_match));
155 for_each_string_list_item(si, &list) {
156 size_t attr_len;
157 char *attr_name;
158 const struct git_attr *a;
160 int j = item->attr_match_nr++;
161 const char *attr = si->string;
162 struct attr_match *am = &item->attr_match[j];
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);
184 break;
187 attr_name = xmemdupz(attr, attr_len);
188 a = git_attr(attr_name);
189 if (!a)
190 die(_("invalid attribute name %s"), attr_name);
192 attr_check_append(item->attr_check, a);
194 free(attr_name);
197 if (item->attr_check->nr != item->attr_match_nr)
198 die("BUG: should have same number of entries");
200 string_list_clear(&list, 0);
203 static inline int get_literal_global(void)
205 static int literal = -1;
207 if (literal < 0)
208 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
210 return literal;
213 static inline int get_glob_global(void)
215 static int glob = -1;
217 if (glob < 0)
218 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
220 return glob;
223 static inline int get_noglob_global(void)
225 static int noglob = -1;
227 if (noglob < 0)
228 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
230 return noglob;
233 static inline int get_icase_global(void)
235 static int icase = -1;
237 if (icase < 0)
238 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
240 return icase;
243 static int get_global_magic(int element_magic)
245 int global_magic = 0;
247 if (get_literal_global())
248 global_magic |= PATHSPEC_LITERAL;
250 /* --glob-pathspec is overridden by :(literal) */
251 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
252 global_magic |= PATHSPEC_GLOB;
254 if (get_glob_global() && get_noglob_global())
255 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
257 if (get_icase_global())
258 global_magic |= PATHSPEC_ICASE;
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"));
265 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
266 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
267 global_magic |= PATHSPEC_LITERAL;
269 return global_magic;
273 * Parse the pathspec element looking for long magic
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
279 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
280 struct pathspec_item *item,
281 const char *elem)
283 const char *pos;
284 const char *nextat;
286 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
287 size_t len = strcspn_escaped(pos, ",)");
288 int i;
290 if (pos[len] == ',')
291 nextat = pos + len + 1; /* handle ',' */
292 else
293 nextat = pos + len; /* handle ')' and '\0' */
295 if (!len)
296 continue;
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;
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;
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;
322 if (ARRAY_SIZE(pathspec_magic) <= i)
323 die(_("Invalid pathspec magic '%.*s' in '%s'"),
324 (int) len, pos, elem);
327 if (*pos != ')')
328 die(_("Missing ')' at the end of pathspec magic in '%s'"),
329 elem);
330 pos++;
332 return pos;
336 * Parse the pathspec element looking for short magic
338 * saves all magic in 'magic'
339 * returns the position in 'elem' after all magic has been parsed
341 static const char *parse_short_magic(unsigned *magic, const char *elem)
343 const char *pos;
345 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
346 char ch = *pos;
347 int i;
349 /* Special case alias for '!' */
350 if (ch == '^') {
351 *magic |= PATHSPEC_EXCLUDE;
352 continue;
355 if (!is_pathspec_magic(ch))
356 break;
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;
365 if (ARRAY_SIZE(pathspec_magic) <= i)
366 die(_("Unimplemented pathspec magic '%c' in '%s'"),
367 ch, elem);
370 if (*pos == ':')
371 pos++;
373 return pos;
376 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
377 struct pathspec_item *item,
378 const char *elem)
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);
390 static void strip_submodule_slash_cheap(struct pathspec_item *item)
392 if (item->len >= 1 && item->match[item->len - 1] == '/') {
393 int i = cache_name_pos(item->match, item->len - 1);
395 if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
396 item->len--;
397 item->match[item->len] = '\0';
402 static void strip_submodule_slash_expensive(struct pathspec_item *item)
404 int i;
406 for (i = 0; i < active_nr; i++) {
407 struct cache_entry *ce = active_cache[i];
408 int ce_len = ce_namelen(ce);
410 if (!S_ISGITLINK(ce->ce_mode))
411 continue;
413 if (item->len <= ce_len || item->match[ce_len] != '/' ||
414 memcmp(ce->name, item->match, ce_len))
415 continue;
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);
428 static void die_inside_submodule_path(struct pathspec_item *item)
430 int i;
432 for (i = 0; i < active_nr; i++) {
433 struct cache_entry *ce = active_cache[i];
434 int ce_len = ce_namelen(ce);
436 if (!S_ISGITLINK(ce->ce_mode))
437 continue;
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;
444 die(_("Pathspec '%s' is in submodule '%.*s'"),
445 item->original, ce_len, ce->name);
450 * Perform the initialization of a pathspec_item based on a pathspec element.
452 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
453 const char *prefix, int prefixlen,
454 const char *elt)
456 unsigned magic = 0, element_magic = 0;
457 const char *copyfrom = elt;
458 char *match;
459 int pathspec_prefix = -1;
461 item->attr_check = NULL;
462 item->attr_match = NULL;
463 item->attr_match_nr = 0;
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);
477 item->magic = magic;
479 if (pathspec_prefix >= 0 &&
480 (prefixlen || (prefix && *prefix)))
481 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
483 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
484 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
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);
500 item->match = match;
501 item->len = strlen(item->match);
502 item->prefix = prefixlen;
505 * Prefix the pathspec (keep all magic) and assign to
506 * original. Useful for passing to another command.
508 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
509 !get_literal_global()) {
510 struct strbuf sb = STRBUF_INIT;
512 /* Preserve the actual prefix length of each pattern */
513 prefix_magic(&sb, prefixlen, element_magic);
515 strbuf_addstr(&sb, match);
516 item->original = strbuf_detach(&sb, NULL);
517 } else {
518 item->original = xstrdup(elt);
521 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
522 strip_submodule_slash_cheap(item);
524 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
525 strip_submodule_slash_expensive(item);
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;
535 item->flags = 0;
536 if (magic & PATHSPEC_GLOB) {
538 * FIXME: should we enable ONESTAR in _GLOB for
539 * pattern "* * / * . c"?
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;
548 /* sanity checks, pathspec matchers assume these are sane */
549 if (item->nowildcard_len > item->len ||
550 item->prefix > item->len) {
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.
558 die_inside_submodule_path(item);
559 die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
563 static int pathspec_item_cmp(const void *a_, const void *b_)
565 struct pathspec_item *a, *b;
567 a = (struct pathspec_item *)a_;
568 b = (struct pathspec_item *)b_;
569 return strcmp(a->match, b->match);
572 static void NORETURN unsupported_magic(const char *pattern,
573 unsigned magic)
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, ", ");
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);
591 * We may want to substitute "this command" with a command
592 * name. E.g. when add--interactive dies when running
593 * "checkout -p"
595 die(_("%s: pathspec magic not supported by this command: %s"),
596 pattern, sb.buf);
600 * Given command line arguments and a prefix, convert the input to
601 * pathspec. die() if any magic in magic_mask is used.
603 void parse_pathspec(struct pathspec *pathspec,
604 unsigned magic_mask, unsigned flags,
605 const char *prefix, const char **argv)
607 struct pathspec_item *item;
608 const char *entry = argv ? *argv : NULL;
609 int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
611 memset(pathspec, 0, sizeof(*pathspec));
613 if (flags & PATHSPEC_MAXDEPTH_VALID)
614 pathspec->magic |= PATHSPEC_MAXDEPTH;
616 /* No arguments, no prefix -> no pathspec */
617 if (!entry && !prefix)
618 return;
620 if ((flags & PATHSPEC_PREFER_CWD) &&
621 (flags & PATHSPEC_PREFER_FULL))
622 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
624 /* No arguments with prefix -> prefix pathspec */
625 if (!entry) {
626 if (flags & PATHSPEC_PREFER_FULL)
627 return;
629 if (!(flags & PATHSPEC_PREFER_CWD))
630 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
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;
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;
649 n++;
652 pathspec->nr = n;
653 ALLOC_ARRAY(pathspec->items, n + 1);
654 item = pathspec->items;
655 prefixlen = prefix ? strlen(prefix) : 0;
657 for (i = 0; i < n; i++) {
658 entry = argv[i];
660 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
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);
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);
672 if (item[i].nowildcard_len < item[i].len)
673 pathspec->has_wildcard = 1;
674 pathspec->magic |= item[i].magic;
678 * If everything is an exclude pattern, add one positive pattern
679 * that matches everyting. We allocated an extra one for this.
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++;
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);
694 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
696 int i, j;
698 *dst = *src;
699 ALLOC_ARRAY(dst->items, dst->nr);
700 COPY_ARRAY(dst->items, src->items, dst->nr);
702 for (i = 0; i < dst->nr; i++) {
703 struct pathspec_item *d = &dst->items[i];
704 struct pathspec_item *s = &src->items[i];
706 d->match = xstrdup(s->match);
707 d->original = xstrdup(s->original);
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);
716 d->attr_check = attr_check_dup(s->attr_check);
720 void clear_pathspec(struct pathspec *pathspec)
722 int i, j;
724 for (i = 0; i < pathspec->nr; i++) {
725 free(pathspec->items[i].match);
726 free(pathspec->items[i].original);
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);
732 if (pathspec->items[i].attr_check)
733 attr_check_free(pathspec->items[i].attr_check);
736 free(pathspec->items);
737 pathspec->items = NULL;
738 pathspec->nr = 0;