7 * Finds which of the given pathspecs match items in the index.
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.
16 * If seen[] has not already been written to, it may make sense
17 * to use find_pathspecs_matching_against_index() instead.
19 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
22 int num_unmatched = 0, i;
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
30 for (i = 0; i < pathspec->nr; i++)
35 for (i = 0; i < active_nr; i++) {
36 const struct cache_entry *ce = active_cache[i];
37 ce_path_match(ce, pathspec, seen);
42 * Finds which of the given pathspecs match items in the index.
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.
49 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
51 char *seen = xcalloc(pathspec->nr, 1);
52 add_pathspec_matches_against_index(pathspec, seen);
59 * Possible future magic semantics include stuff like:
61 * { PATHSPEC_RECURSIVE, '*', "recursive" },
62 * { PATHSPEC_REGEXP, '\0', "regexp" },
66 static struct pathspec_magic {
68 char mnemonic; /* this cannot be ':'! */
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" },
79 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
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);
89 strbuf_addf(sb, ",prefix:%d)", prefixlen);
92 static size_t strcspn_escaped(const char *s, const char *stop)
96 for (i = s; *i; i++) {
97 /* skip the escaped character */
98 if (i[0] == '\\' && i[1]) {
103 if (strchr(stop, *i))
109 static inline int invalid_value_char(const char ch)
111 if (isalnum(ch) || strchr(",-_", ch))
116 static char *attr_value_unescape(const char *value)
121 ret = xmallocz(strlen(value));
122 for (src = value, dst = ret; *src; src++, dst++) {
125 die(_("Escape character '\\' not allowed as "
126 "last character in attr value"));
129 if (invalid_value_char(*src))
130 die("cannot use '%c' for value matching", *src);
137 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
139 struct string_list_item *si;
140 struct string_list list = STRING_LIST_INIT_DUP;
142 if (item->attr_check || item->attr_match)
143 die(_("Only one 'attr:' specification is allowed."));
145 if (!value || !*value)
146 die(_("attr spec must not be empty"));
148 string_list_split(&list, value, ' ', -1);
149 string_list_remove_empty_items(&list, 0);
151 item->attr_check = attr_check_alloc();
152 item->attr_match = xcalloc(list.nr, sizeof(struct attr_match));
154 for_each_string_list_item(si, &list) {
157 const struct git_attr *a;
159 int j = item->attr_match_nr++;
160 const char *attr = si->string;
161 struct attr_match *am = &item->attr_match[j];
165 am->match_mode = MATCH_UNSPECIFIED;
167 attr_len = strlen(attr);
170 am->match_mode = MATCH_UNSET;
172 attr_len = strlen(attr);
175 attr_len = strcspn(attr, "=");
176 if (attr[attr_len] != '=')
177 am->match_mode = MATCH_SET;
179 const char *v = &attr[attr_len + 1];
180 am->match_mode = MATCH_VALUE;
181 am->value = attr_value_unescape(v);
186 attr_name = xmemdupz(attr, attr_len);
187 a = git_attr(attr_name);
189 die(_("invalid attribute name %s"), attr_name);
191 attr_check_append(item->attr_check, a);
196 if (item->attr_check->nr != item->attr_match_nr)
197 die("BUG: should have same number of entries");
199 string_list_clear(&list, 0);
202 static inline int get_literal_global(void)
204 static int literal = -1;
207 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
212 static inline int get_glob_global(void)
214 static int glob = -1;
217 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
222 static inline int get_noglob_global(void)
224 static int noglob = -1;
227 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
232 static inline int get_icase_global(void)
234 static int icase = -1;
237 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
242 static int get_global_magic(int element_magic)
244 int global_magic = 0;
246 if (get_literal_global())
247 global_magic |= PATHSPEC_LITERAL;
249 /* --glob-pathspec is overridden by :(literal) */
250 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
251 global_magic |= PATHSPEC_GLOB;
253 if (get_glob_global() && get_noglob_global())
254 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
256 if (get_icase_global())
257 global_magic |= PATHSPEC_ICASE;
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"));
264 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
265 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
266 global_magic |= PATHSPEC_LITERAL;
272 * Parse the pathspec element looking for long magic
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
278 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
279 struct pathspec_item *item,
285 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
286 size_t len = strcspn_escaped(pos, ",)");
290 nextat = pos + len + 1; /* handle ',' */
292 nextat = pos + len; /* handle ')' and '\0' */
297 if (starts_with(pos, "prefix:")) {
299 *prefix_len = strtol(pos + 7, &endptr, 10);
300 if (endptr - pos != len)
301 die(_("invalid parameter for pathspec magic 'prefix'"));
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;
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;
321 if (ARRAY_SIZE(pathspec_magic) <= i)
322 die(_("Invalid pathspec magic '%.*s' in '%s'"),
323 (int) len, pos, elem);
327 die(_("Missing ')' at the end of pathspec magic in '%s'"),
335 * Parse the pathspec element looking for short magic
337 * saves all magic in 'magic'
338 * returns the position in 'elem' after all magic has been parsed
340 static const char *parse_short_magic(unsigned *magic, const char *elem)
344 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
348 /* Special case alias for '!' */
350 *magic |= PATHSPEC_EXCLUDE;
354 if (!is_pathspec_magic(ch))
357 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
358 if (pathspec_magic[i].mnemonic == ch) {
359 *magic |= pathspec_magic[i].bit;
364 if (ARRAY_SIZE(pathspec_magic) <= i)
365 die(_("Unimplemented pathspec magic '%c' in '%s'"),
375 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
376 struct pathspec_item *item,
379 if (elem[0] != ':' || get_literal_global())
380 return elem; /* nothing to do */
381 else if (elem[1] == '(')
383 return parse_long_magic(magic, prefix_len, item, elem);
386 return parse_short_magic(magic, elem);
389 static void strip_submodule_slash_cheap(struct pathspec_item *item)
391 if (item->len >= 1 && item->match[item->len - 1] == '/') {
392 int i = cache_name_pos(item->match, item->len - 1);
394 if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
396 item->match[item->len] = '\0';
401 static void strip_submodule_slash_expensive(struct pathspec_item *item)
405 for (i = 0; i < active_nr; i++) {
406 struct cache_entry *ce = active_cache[i];
407 int ce_len = ce_namelen(ce);
409 if (!S_ISGITLINK(ce->ce_mode))
412 if (item->len <= ce_len || item->match[ce_len] != '/' ||
413 memcmp(ce->name, item->match, ce_len))
416 if (item->len == ce_len + 1) {
417 /* strip trailing slash */
419 item->match[item->len] = '\0';
421 die(_("Pathspec '%s' is in submodule '%.*s'"),
422 item->original, ce_len, ce->name);
428 * Perform the initialization of a pathspec_item based on a pathspec element.
430 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
431 const char *prefix, int prefixlen,
434 unsigned magic = 0, element_magic = 0;
435 const char *copyfrom = elt;
437 int pathspec_prefix = -1;
439 item->attr_check = NULL;
440 item->attr_match = NULL;
441 item->attr_match_nr = 0;
443 /* PATHSPEC_LITERAL_PATH ignores magic */
444 if (flags & PATHSPEC_LITERAL_PATH) {
445 magic = PATHSPEC_LITERAL;
447 copyfrom = parse_element_magic(&element_magic,
451 magic |= element_magic;
452 magic |= get_global_magic(element_magic);
457 if (pathspec_prefix >= 0 &&
458 (prefixlen || (prefix && *prefix)))
459 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
461 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
462 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
464 /* Create match string which will be used for pathspec matching */
465 if (pathspec_prefix >= 0) {
466 match = xstrdup(copyfrom);
467 prefixlen = pathspec_prefix;
468 } else if (magic & PATHSPEC_FROMTOP) {
469 match = xstrdup(copyfrom);
472 match = prefix_path_gently(prefix, prefixlen,
473 &prefixlen, copyfrom);
475 die(_("%s: '%s' is outside repository"), elt, copyfrom);
479 item->len = strlen(item->match);
480 item->prefix = prefixlen;
483 * Prefix the pathspec (keep all magic) and assign to
484 * original. Useful for passing to another command.
486 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
487 !get_literal_global()) {
488 struct strbuf sb = STRBUF_INIT;
490 /* Preserve the actual prefix length of each pattern */
491 prefix_magic(&sb, prefixlen, element_magic);
493 strbuf_addstr(&sb, match);
494 item->original = strbuf_detach(&sb, NULL);
496 item->original = xstrdup(elt);
499 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
500 strip_submodule_slash_cheap(item);
502 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
503 strip_submodule_slash_expensive(item);
505 if (magic & PATHSPEC_LITERAL) {
506 item->nowildcard_len = item->len;
508 item->nowildcard_len = simple_length(item->match);
509 if (item->nowildcard_len < prefixlen)
510 item->nowildcard_len = prefixlen;
514 if (magic & PATHSPEC_GLOB) {
516 * FIXME: should we enable ONESTAR in _GLOB for
517 * pattern "* * / * . c"?
520 if (item->nowildcard_len < item->len &&
521 item->match[item->nowildcard_len] == '*' &&
522 no_wildcard(item->match + item->nowildcard_len + 1))
523 item->flags |= PATHSPEC_ONESTAR;
526 /* sanity checks, pathspec matchers assume these are sane */
527 if (item->nowildcard_len > item->len ||
528 item->prefix > item->len) {
529 die ("BUG: error initializing pathspec_item");
533 static int pathspec_item_cmp(const void *a_, const void *b_)
535 struct pathspec_item *a, *b;
537 a = (struct pathspec_item *)a_;
538 b = (struct pathspec_item *)b_;
539 return strcmp(a->match, b->match);
542 static void NORETURN unsupported_magic(const char *pattern,
545 struct strbuf sb = STRBUF_INIT;
547 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
548 const struct pathspec_magic *m = pathspec_magic + i;
549 if (!(magic & m->bit))
552 strbuf_addstr(&sb, ", ");
555 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
556 m->name, m->mnemonic);
558 strbuf_addf(&sb, "'%s'", m->name);
561 * We may want to substitute "this command" with a command
562 * name. E.g. when add--interactive dies when running
565 die(_("%s: pathspec magic not supported by this command: %s"),
570 * Given command line arguments and a prefix, convert the input to
571 * pathspec. die() if any magic in magic_mask is used.
573 void parse_pathspec(struct pathspec *pathspec,
574 unsigned magic_mask, unsigned flags,
575 const char *prefix, const char **argv)
577 struct pathspec_item *item;
578 const char *entry = argv ? *argv : NULL;
579 int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
581 memset(pathspec, 0, sizeof(*pathspec));
583 if (flags & PATHSPEC_MAXDEPTH_VALID)
584 pathspec->magic |= PATHSPEC_MAXDEPTH;
586 /* No arguments, no prefix -> no pathspec */
587 if (!entry && !prefix)
590 if ((flags & PATHSPEC_PREFER_CWD) &&
591 (flags & PATHSPEC_PREFER_FULL))
592 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
594 /* No arguments with prefix -> prefix pathspec */
596 if (flags & PATHSPEC_PREFER_FULL)
599 if (!(flags & PATHSPEC_PREFER_CWD))
600 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
602 pathspec->items = item = xcalloc(1, sizeof(*item));
603 item->match = xstrdup(prefix);
604 item->original = xstrdup(prefix);
605 item->nowildcard_len = item->len = strlen(prefix);
606 item->prefix = item->len;
612 warn_empty_string = 1;
614 if (*argv[n] == '\0' && warn_empty_string) {
615 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
616 "please use . instead if you meant to match all paths"));
617 warn_empty_string = 0;
623 ALLOC_ARRAY(pathspec->items, n + 1);
624 item = pathspec->items;
625 prefixlen = prefix ? strlen(prefix) : 0;
627 for (i = 0; i < n; i++) {
630 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
632 if (item[i].magic & PATHSPEC_EXCLUDE)
634 if (item[i].magic & magic_mask)
635 unsupported_magic(entry, item[i].magic & magic_mask);
637 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
638 has_symlink_leading_path(item[i].match, item[i].len)) {
639 die(_("pathspec '%s' is beyond a symbolic link"), entry);
642 if (item[i].nowildcard_len < item[i].len)
643 pathspec->has_wildcard = 1;
644 pathspec->magic |= item[i].magic;
648 * If everything is an exclude pattern, add one positive pattern
649 * that matches everyting. We allocated an extra one for this.
651 if (nr_exclude == n) {
652 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
653 init_pathspec_item(item + n, 0, prefix, plen, "");
657 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
658 if (flags & PATHSPEC_KEEP_ORDER)
659 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
660 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
664 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
669 ALLOC_ARRAY(dst->items, dst->nr);
670 COPY_ARRAY(dst->items, src->items, dst->nr);
672 for (i = 0; i < dst->nr; i++) {
673 struct pathspec_item *d = &dst->items[i];
674 struct pathspec_item *s = &src->items[i];
676 d->match = xstrdup(s->match);
677 d->original = xstrdup(s->original);
679 ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
680 COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
681 for (j = 0; j < d->attr_match_nr; j++) {
682 const char *value = s->attr_match[j].value;
683 d->attr_match[j].value = xstrdup_or_null(value);
686 d->attr_check = attr_check_dup(s->attr_check);
690 void clear_pathspec(struct pathspec *pathspec)
694 for (i = 0; i < pathspec->nr; i++) {
695 free(pathspec->items[i].match);
696 free(pathspec->items[i].original);
698 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
699 free(pathspec->items[i].attr_match[j].value);
700 free(pathspec->items[i].attr_match);
702 if (pathspec->items[i].attr_check)
703 attr_check_free(pathspec->items[i].attr_check);
706 free(pathspec->items);
707 pathspec->items = NULL;