reftable/reader: introduce refcounting
[git/gitster.git] / pathspec.c
blobfe1f0f41af081f5e171cff399a561a5bf798168e
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "abspath.h"
5 #include "parse.h"
6 #include "dir.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "pathspec.h"
10 #include "attr.h"
11 #include "read-cache.h"
12 #include "repository.h"
13 #include "setup.h"
14 #include "strvec.h"
15 #include "symlinks.h"
16 #include "quote.h"
17 #include "wildmatch.h"
20 * Finds which of the given pathspecs match items in the index.
22 * For each pathspec, sets the corresponding entry in the seen[] array
23 * (which should be specs items long, i.e. the same size as pathspec)
24 * to the nature of the "closest" (i.e. most specific) match found for
25 * that pathspec in the index, if it was a closer type of match than
26 * the existing entry. As an optimization, matching is skipped
27 * altogether if seen[] already only contains non-zero entries.
29 * If seen[] has not already been written to, it may make sense
30 * to use find_pathspecs_matching_against_index() instead.
32 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
33 struct index_state *istate,
34 char *seen,
35 enum ps_skip_worktree_action sw_action)
37 int num_unmatched = 0, i;
40 * Since we are walking the index as if we were walking the directory,
41 * we have to mark the matched pathspec as seen; otherwise we will
42 * mistakenly think that the user gave a pathspec that did not match
43 * anything.
45 for (i = 0; i < pathspec->nr; i++)
46 if (!seen[i])
47 num_unmatched++;
48 if (!num_unmatched)
49 return;
50 for (i = 0; i < istate->cache_nr; i++) {
51 const struct cache_entry *ce = istate->cache[i];
52 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
53 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
54 continue;
55 ce_path_match(istate, ce, pathspec, seen);
60 * Finds which of the given pathspecs match items in the index.
62 * This is a one-shot wrapper around add_pathspec_matches_against_index()
63 * which allocates, populates, and returns a seen[] array indicating the
64 * nature of the "closest" (i.e. most specific) matches which each of the
65 * given pathspecs achieves against all items in the index.
67 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
68 struct index_state *istate,
69 enum ps_skip_worktree_action sw_action)
71 char *seen = xcalloc(pathspec->nr, 1);
72 add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
73 return seen;
76 char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
78 struct index_state *istate = the_repository->index;
79 char *seen = xcalloc(pathspec->nr, 1);
80 int i;
82 for (i = 0; i < istate->cache_nr; i++) {
83 struct cache_entry *ce = istate->cache[i];
84 if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
85 ce_path_match(istate, ce, pathspec, seen);
88 return seen;
92 * Magic pathspec
94 * Possible future magic semantics include stuff like:
96 * { PATHSPEC_RECURSIVE, '*', "recursive" },
97 * { PATHSPEC_REGEXP, '\0', "regexp" },
101 static struct pathspec_magic {
102 unsigned bit;
103 char mnemonic; /* this cannot be ':'! */
104 const char *name;
105 } pathspec_magic[] = {
106 { PATHSPEC_FROMTOP, '/', "top" },
107 { PATHSPEC_LITERAL, '\0', "literal" },
108 { PATHSPEC_GLOB, '\0', "glob" },
109 { PATHSPEC_ICASE, '\0', "icase" },
110 { PATHSPEC_EXCLUDE, '!', "exclude" },
111 { PATHSPEC_ATTR, '\0', "attr" },
114 static void prefix_magic(struct strbuf *sb, int prefixlen,
115 unsigned magic, const char *element)
117 /* No magic was found in element, just add prefix magic */
118 if (!magic) {
119 strbuf_addf(sb, ":(prefix:%d)", prefixlen);
120 return;
124 * At this point, we know that parse_element_magic() was able
125 * to extract some pathspec magic from element. So we know
126 * element is correctly formatted in either shorthand or
127 * longhand form
129 if (element[1] != '(') {
130 /* Process an element in shorthand form (e.g. ":!/<match>") */
131 strbuf_addstr(sb, ":(");
132 for (int i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
133 if ((magic & pathspec_magic[i].bit) &&
134 pathspec_magic[i].mnemonic) {
135 if (sb->buf[sb->len - 1] != '(')
136 strbuf_addch(sb, ',');
137 strbuf_addstr(sb, pathspec_magic[i].name);
140 } else {
141 /* For the longhand form, we copy everything up to the final ')' */
142 size_t len = strchr(element, ')') - element;
143 strbuf_add(sb, element, len);
145 strbuf_addf(sb, ",prefix:%d)", prefixlen);
148 static size_t strcspn_escaped(const char *s, const char *stop)
150 const char *i;
152 for (i = s; *i; i++) {
153 /* skip the escaped character */
154 if (i[0] == '\\' && i[1]) {
155 i++;
156 continue;
159 if (strchr(stop, *i))
160 break;
162 return i - s;
165 static inline int invalid_value_char(const char ch)
167 if (isalnum(ch) || strchr(",-_", ch))
168 return 0;
169 return -1;
172 static char *attr_value_unescape(const char *value)
174 const char *src;
175 char *dst, *ret;
177 ret = xmallocz(strlen(value));
178 for (src = value, dst = ret; *src; src++, dst++) {
179 if (*src == '\\') {
180 if (!src[1])
181 die(_("Escape character '\\' not allowed as "
182 "last character in attr value"));
183 src++;
185 if (invalid_value_char(*src))
186 die("cannot use '%c' for value matching", *src);
187 *dst = *src;
189 *dst = '\0';
190 return ret;
193 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
195 struct string_list_item *si;
196 struct string_list list = STRING_LIST_INIT_DUP;
198 if (item->attr_check || item->attr_match)
199 die(_("Only one 'attr:' specification is allowed."));
201 if (!value || !*value)
202 die(_("attr spec must not be empty"));
204 string_list_split(&list, value, ' ', -1);
205 string_list_remove_empty_items(&list, 0);
207 item->attr_check = attr_check_alloc();
208 CALLOC_ARRAY(item->attr_match, list.nr);
210 for_each_string_list_item(si, &list) {
211 size_t attr_len;
212 char *attr_name;
213 const struct git_attr *a;
215 int j = item->attr_match_nr++;
216 const char *attr = si->string;
217 struct attr_match *am = &item->attr_match[j];
219 switch (*attr) {
220 case '!':
221 am->match_mode = MATCH_UNSPECIFIED;
222 attr++;
223 attr_len = strlen(attr);
224 break;
225 case '-':
226 am->match_mode = MATCH_UNSET;
227 attr++;
228 attr_len = strlen(attr);
229 break;
230 default:
231 attr_len = strcspn(attr, "=");
232 if (attr[attr_len] != '=')
233 am->match_mode = MATCH_SET;
234 else {
235 const char *v = &attr[attr_len + 1];
236 am->match_mode = MATCH_VALUE;
237 am->value = attr_value_unescape(v);
239 break;
242 attr_name = xmemdupz(attr, attr_len);
243 a = git_attr(attr_name);
244 if (!a)
245 die(_("invalid attribute name %s"), attr_name);
247 attr_check_append(item->attr_check, a);
249 free(attr_name);
252 if (item->attr_check->nr != item->attr_match_nr)
253 BUG("should have same number of entries");
255 string_list_clear(&list, 0);
258 static inline int get_literal_global(void)
260 static int literal = -1;
262 if (literal < 0)
263 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
265 return literal;
268 static inline int get_glob_global(void)
270 static int glob = -1;
272 if (glob < 0)
273 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
275 return glob;
278 static inline int get_noglob_global(void)
280 static int noglob = -1;
282 if (noglob < 0)
283 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
285 return noglob;
288 static inline int get_icase_global(void)
290 static int icase = -1;
292 if (icase < 0)
293 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
295 return icase;
298 static int get_global_magic(int element_magic)
300 int global_magic = 0;
302 if (get_literal_global())
303 global_magic |= PATHSPEC_LITERAL;
305 /* --glob-pathspec is overridden by :(literal) */
306 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
307 global_magic |= PATHSPEC_GLOB;
309 if (get_glob_global() && get_noglob_global())
310 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
312 if (get_icase_global())
313 global_magic |= PATHSPEC_ICASE;
315 if ((global_magic & PATHSPEC_LITERAL) &&
316 (global_magic & ~PATHSPEC_LITERAL))
317 die(_("global 'literal' pathspec setting is incompatible "
318 "with all other global pathspec settings"));
320 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
321 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
322 global_magic |= PATHSPEC_LITERAL;
324 return global_magic;
328 * Parse the pathspec element looking for long magic
330 * saves all magic in 'magic'
331 * if prefix magic is used, save the prefix length in 'prefix_len'
332 * returns the position in 'elem' after all magic has been parsed
334 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
335 struct pathspec_item *item,
336 const char *elem)
338 const char *pos;
339 const char *nextat;
341 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
342 size_t len = strcspn_escaped(pos, ",)");
343 int i;
345 if (pos[len] == ',')
346 nextat = pos + len + 1; /* handle ',' */
347 else
348 nextat = pos + len; /* handle ')' and '\0' */
350 if (!len)
351 continue;
353 if (starts_with(pos, "prefix:")) {
354 char *endptr;
355 *prefix_len = strtol(pos + 7, &endptr, 10);
356 if (endptr - pos != len)
357 die(_("invalid parameter for pathspec magic 'prefix'"));
358 continue;
361 if (starts_with(pos, "attr:")) {
362 char *attr_body = xmemdupz(pos + 5, len - 5);
363 parse_pathspec_attr_match(item, attr_body);
364 *magic |= PATHSPEC_ATTR;
365 free(attr_body);
366 continue;
369 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
370 if (strlen(pathspec_magic[i].name) == len &&
371 !strncmp(pathspec_magic[i].name, pos, len)) {
372 *magic |= pathspec_magic[i].bit;
373 break;
377 if (ARRAY_SIZE(pathspec_magic) <= i)
378 die(_("Invalid pathspec magic '%.*s' in '%s'"),
379 (int) len, pos, elem);
382 if (*pos != ')')
383 die(_("Missing ')' at the end of pathspec magic in '%s'"),
384 elem);
385 pos++;
387 return pos;
391 * Parse the pathspec element looking for short magic
393 * saves all magic in 'magic'
394 * returns the position in 'elem' after all magic has been parsed
396 static const char *parse_short_magic(unsigned *magic, const char *elem)
398 const char *pos;
400 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
401 char ch = *pos;
402 int i;
404 /* Special case alias for '!' */
405 if (ch == '^') {
406 *magic |= PATHSPEC_EXCLUDE;
407 continue;
410 if (!is_pathspec_magic(ch))
411 break;
413 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
414 if (pathspec_magic[i].mnemonic == ch) {
415 *magic |= pathspec_magic[i].bit;
416 break;
420 if (ARRAY_SIZE(pathspec_magic) <= i)
421 die(_("Unimplemented pathspec magic '%c' in '%s'"),
422 ch, elem);
425 if (*pos == ':')
426 pos++;
428 return pos;
431 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
432 struct pathspec_item *item,
433 const char *elem)
435 if (elem[0] != ':' || get_literal_global())
436 return elem; /* nothing to do */
437 else if (elem[1] == '(')
438 /* longhand */
439 return parse_long_magic(magic, prefix_len, item, elem);
440 else
441 /* shorthand */
442 return parse_short_magic(magic, elem);
446 * Perform the initialization of a pathspec_item based on a pathspec element.
448 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
449 const char *prefix, int prefixlen,
450 const char *elt)
452 unsigned magic = 0, element_magic = 0;
453 const char *copyfrom = elt;
454 char *match;
455 int pathspec_prefix = -1;
457 item->attr_check = NULL;
458 item->attr_match = NULL;
459 item->attr_match_nr = 0;
461 /* PATHSPEC_LITERAL_PATH ignores magic */
462 if (flags & PATHSPEC_LITERAL_PATH) {
463 magic = PATHSPEC_LITERAL;
464 } else {
465 copyfrom = parse_element_magic(&element_magic,
466 &pathspec_prefix,
467 item,
468 elt);
469 magic |= element_magic;
470 magic |= get_global_magic(element_magic);
473 item->magic = magic;
475 if (pathspec_prefix >= 0 &&
476 (prefixlen || (prefix && *prefix)))
477 BUG("'prefix' magic is supposed to be used at worktree's root");
479 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
480 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
482 /* Create match string which will be used for pathspec matching */
483 if (pathspec_prefix >= 0) {
484 match = xstrdup(copyfrom);
485 prefixlen = pathspec_prefix;
486 } else if (magic & PATHSPEC_FROMTOP) {
487 match = xstrdup(copyfrom);
488 prefixlen = 0;
489 } else {
490 match = prefix_path_gently(prefix, prefixlen,
491 &prefixlen, copyfrom);
492 if (!match) {
493 const char *hint_path;
495 if (!have_git_dir())
496 die(_("'%s' is outside the directory tree"),
497 copyfrom);
498 hint_path = get_git_work_tree();
499 if (!hint_path)
500 hint_path = get_git_dir();
501 die(_("%s: '%s' is outside repository at '%s'"), elt,
502 copyfrom, absolute_path(hint_path));
506 item->match = match;
507 item->len = strlen(item->match);
508 item->prefix = prefixlen;
511 * Prefix the pathspec (keep all magic) and assign to
512 * original. Useful for passing to another command.
514 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
515 !get_literal_global()) {
516 struct strbuf sb = STRBUF_INIT;
518 /* Preserve the actual prefix length of each pattern */
519 prefix_magic(&sb, prefixlen, element_magic, elt);
521 strbuf_addstr(&sb, match);
522 item->original = strbuf_detach(&sb, NULL);
523 } else {
524 item->original = xstrdup(elt);
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) {
551 BUG("error initializing pathspec_item");
555 static int pathspec_item_cmp(const void *a_, const void *b_)
557 struct pathspec_item *a, *b;
559 a = (struct pathspec_item *)a_;
560 b = (struct pathspec_item *)b_;
561 return strcmp(a->match, b->match);
564 void pathspec_magic_names(unsigned magic, struct strbuf *out)
566 int i;
567 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
568 const struct pathspec_magic *m = pathspec_magic + i;
569 if (!(magic & m->bit))
570 continue;
571 if (out->len)
572 strbuf_addstr(out, ", ");
574 if (m->mnemonic)
575 strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
576 m->name, m->mnemonic);
577 else
578 strbuf_addf(out, "'%s'", m->name);
582 static void NORETURN unsupported_magic(const char *pattern,
583 unsigned magic)
585 struct strbuf sb = STRBUF_INIT;
586 pathspec_magic_names(magic, &sb);
588 * We may want to substitute "this command" with a command
589 * name. E.g. when "git add -p" or "git add -i" dies when running
590 * "checkout -p"
592 die(_("%s: pathspec magic not supported by this command: %s"),
593 pattern, sb.buf);
596 void parse_pathspec(struct pathspec *pathspec,
597 unsigned magic_mask, unsigned flags,
598 const char *prefix, const char **argv)
600 struct pathspec_item *item;
601 const char *entry = argv ? *argv : NULL;
602 int i, n, prefixlen, nr_exclude = 0;
604 memset(pathspec, 0, sizeof(*pathspec));
606 if (flags & PATHSPEC_MAXDEPTH_VALID)
607 pathspec->magic |= PATHSPEC_MAXDEPTH;
609 /* No arguments, no prefix -> no pathspec */
610 if (!entry && !prefix)
611 return;
613 if ((flags & PATHSPEC_PREFER_CWD) &&
614 (flags & PATHSPEC_PREFER_FULL))
615 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
617 /* No arguments with prefix -> prefix pathspec */
618 if (!entry) {
619 if (flags & PATHSPEC_PREFER_FULL)
620 return;
622 if (!(flags & PATHSPEC_PREFER_CWD))
623 BUG("PATHSPEC_PREFER_CWD requires arguments");
625 pathspec->items = CALLOC_ARRAY(item, 1);
626 item->match = xstrdup(prefix);
627 item->original = xstrdup(prefix);
628 item->nowildcard_len = item->len = strlen(prefix);
629 item->prefix = item->len;
630 pathspec->nr = 1;
631 return;
634 n = 0;
635 while (argv[n]) {
636 if (*argv[n] == '\0')
637 die("empty string is not a valid pathspec. "
638 "please use . instead if you meant to match all paths");
639 n++;
642 pathspec->nr = n;
643 ALLOC_ARRAY(pathspec->items, n + 1);
644 item = pathspec->items;
645 prefixlen = prefix ? strlen(prefix) : 0;
647 for (i = 0; i < n; i++) {
648 entry = argv[i];
650 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
652 if (item[i].magic & PATHSPEC_EXCLUDE)
653 nr_exclude++;
654 if (item[i].magic & magic_mask)
655 unsupported_magic(entry, item[i].magic & magic_mask);
657 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
658 has_symlink_leading_path(item[i].match, item[i].len)) {
659 die(_("pathspec '%s' is beyond a symbolic link"), entry);
662 if (item[i].nowildcard_len < item[i].len)
663 pathspec->has_wildcard = 1;
664 pathspec->magic |= item[i].magic;
668 * If everything is an exclude pattern, add one positive pattern
669 * that matches everything. We allocated an extra one for this.
671 if (nr_exclude == n) {
672 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
673 init_pathspec_item(item + n, 0, prefix, plen, ".");
674 pathspec->nr++;
677 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
678 if (flags & PATHSPEC_KEEP_ORDER)
679 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
680 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
684 void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
685 unsigned flags, const char *prefix,
686 const char *file, int nul_term_line)
688 struct strvec parsed_file = STRVEC_INIT;
689 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
690 strbuf_getline;
691 struct strbuf buf = STRBUF_INIT;
692 struct strbuf unquoted = STRBUF_INIT;
693 FILE *in;
695 if (!strcmp(file, "-"))
696 in = stdin;
697 else
698 in = xfopen(file, "r");
700 while (getline_fn(&buf, in) != EOF) {
701 if (!nul_term_line && buf.buf[0] == '"') {
702 strbuf_reset(&unquoted);
703 if (unquote_c_style(&unquoted, buf.buf, NULL))
704 die(_("line is badly quoted: %s"), buf.buf);
705 strbuf_swap(&buf, &unquoted);
707 strvec_push(&parsed_file, buf.buf);
708 strbuf_reset(&buf);
711 strbuf_release(&unquoted);
712 strbuf_release(&buf);
713 if (in != stdin)
714 fclose(in);
716 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
717 strvec_clear(&parsed_file);
720 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
722 int i, j;
724 *dst = *src;
725 DUP_ARRAY(dst->items, src->items, dst->nr);
727 for (i = 0; i < dst->nr; i++) {
728 struct pathspec_item *d = &dst->items[i];
729 struct pathspec_item *s = &src->items[i];
731 d->match = xstrdup(s->match);
732 d->original = xstrdup(s->original);
734 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
735 for (j = 0; j < d->attr_match_nr; j++) {
736 const char *value = s->attr_match[j].value;
737 d->attr_match[j].value = xstrdup_or_null(value);
740 d->attr_check = attr_check_dup(s->attr_check);
744 void clear_pathspec(struct pathspec *pathspec)
746 int i, j;
748 for (i = 0; i < pathspec->nr; i++) {
749 free(pathspec->items[i].match);
750 free(pathspec->items[i].original);
752 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
753 free(pathspec->items[i].attr_match[j].value);
754 free(pathspec->items[i].attr_match);
756 if (pathspec->items[i].attr_check)
757 attr_check_free(pathspec->items[i].attr_check);
760 FREE_AND_NULL(pathspec->items);
761 pathspec->nr = 0;
764 int match_pathspec_attrs(struct index_state *istate,
765 const char *name, int namelen,
766 const struct pathspec_item *item)
768 int i;
769 char *to_free = NULL;
771 if (name[namelen])
772 name = to_free = xmemdupz(name, namelen);
774 git_check_attr(istate, name, item->attr_check);
776 free(to_free);
778 for (i = 0; i < item->attr_match_nr; i++) {
779 const char *value;
780 int matched;
781 enum attr_match_mode match_mode;
783 value = item->attr_check->items[i].value;
784 match_mode = item->attr_match[i].match_mode;
786 if (ATTR_TRUE(value))
787 matched = (match_mode == MATCH_SET);
788 else if (ATTR_FALSE(value))
789 matched = (match_mode == MATCH_UNSET);
790 else if (ATTR_UNSET(value))
791 matched = (match_mode == MATCH_UNSPECIFIED);
792 else
793 matched = (match_mode == MATCH_VALUE &&
794 !strcmp(item->attr_match[i].value, value));
795 if (!matched)
796 return 0;
799 return 1;
802 int pathspec_needs_expanded_index(struct index_state *istate,
803 const struct pathspec *pathspec)
805 unsigned int i, pos;
806 int res = 0;
807 char *skip_worktree_seen = NULL;
810 * If index is not sparse, no index expansion is needed.
812 if (!istate->sparse_index)
813 return 0;
816 * When using a magic pathspec, assume for the sake of simplicity that
817 * the index needs to be expanded to match all matchable files.
819 if (pathspec->magic)
820 return 1;
822 for (i = 0; i < pathspec->nr; i++) {
823 struct pathspec_item item = pathspec->items[i];
826 * If the pathspec item has a wildcard, the index should be expanded
827 * if the pathspec has the possibility of matching a subset of entries inside
828 * of a sparse directory (but not the entire directory).
830 * If the pathspec item is a literal path, the index only needs to be expanded
831 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
832 * expand for in-cone files) and b) it doesn't match any sparse directories
833 * (since we can reset whole sparse directories without expanding them).
835 if (item.nowildcard_len < item.len) {
837 * Special case: if the pattern is a path inside the cone
838 * followed by only wildcards, the pattern cannot match
839 * partial sparse directories, so we know we don't need to
840 * expand the index.
842 * Examples:
843 * - in-cone/foo***: doesn't need expanded index
844 * - not-in-cone/bar*: may need expanded index
845 * - **.c: may need expanded index
847 if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
848 path_in_cone_mode_sparse_checkout(item.original, istate))
849 continue;
851 for (pos = 0; pos < istate->cache_nr; pos++) {
852 struct cache_entry *ce = istate->cache[pos];
854 if (!S_ISSPARSEDIR(ce->ce_mode))
855 continue;
858 * If the pre-wildcard length is longer than the sparse
859 * directory name and the sparse directory is the first
860 * component of the pathspec, need to expand the index.
862 if (item.nowildcard_len > ce_namelen(ce) &&
863 !strncmp(item.original, ce->name, ce_namelen(ce))) {
864 res = 1;
865 break;
869 * If the pre-wildcard length is shorter than the sparse
870 * directory and the pathspec does not match the whole
871 * directory, need to expand the index.
873 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
874 wildmatch(item.original, ce->name, 0)) {
875 res = 1;
876 break;
879 } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
880 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
881 res = 1;
883 if (res > 0)
884 break;
887 free(skip_worktree_seen);
888 return res;