Merge branch 'js/win32-retry-pipe-write-on-enospc'
[alt-git.git] / pathspec.c
blob2133b9fe60a87c201186dc3f1840276104903f7f
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "parse.h"
4 #include "dir.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "pathspec.h"
8 #include "attr.h"
9 #include "read-cache.h"
10 #include "repository.h"
11 #include "setup.h"
12 #include "strvec.h"
13 #include "symlinks.h"
14 #include "quote.h"
15 #include "wildmatch.h"
18 * Finds which of the given pathspecs match items in the index.
20 * For each pathspec, sets the corresponding entry in the seen[] array
21 * (which should be specs items long, i.e. the same size as pathspec)
22 * to the nature of the "closest" (i.e. most specific) match found for
23 * that pathspec in the index, if it was a closer type of match than
24 * the existing entry. As an optimization, matching is skipped
25 * altogether if seen[] already only contains non-zero entries.
27 * If seen[] has not already been written to, it may make sense
28 * to use find_pathspecs_matching_against_index() instead.
30 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
31 struct index_state *istate,
32 char *seen,
33 enum ps_skip_worktree_action sw_action)
35 int num_unmatched = 0, i;
38 * Since we are walking the index as if we were walking the directory,
39 * we have to mark the matched pathspec as seen; otherwise we will
40 * mistakenly think that the user gave a pathspec that did not match
41 * anything.
43 for (i = 0; i < pathspec->nr; i++)
44 if (!seen[i])
45 num_unmatched++;
46 if (!num_unmatched)
47 return;
48 for (i = 0; i < istate->cache_nr; i++) {
49 const struct cache_entry *ce = istate->cache[i];
50 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
51 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
52 continue;
53 ce_path_match(istate, ce, pathspec, seen);
58 * Finds which of the given pathspecs match items in the index.
60 * This is a one-shot wrapper around add_pathspec_matches_against_index()
61 * which allocates, populates, and returns a seen[] array indicating the
62 * nature of the "closest" (i.e. most specific) matches which each of the
63 * given pathspecs achieves against all items in the index.
65 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
66 struct index_state *istate,
67 enum ps_skip_worktree_action sw_action)
69 char *seen = xcalloc(pathspec->nr, 1);
70 add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
71 return seen;
74 char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
76 struct index_state *istate = the_repository->index;
77 char *seen = xcalloc(pathspec->nr, 1);
78 int i;
80 for (i = 0; i < istate->cache_nr; i++) {
81 struct cache_entry *ce = istate->cache[i];
82 if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
83 ce_path_match(istate, ce, pathspec, seen);
86 return seen;
90 * Magic pathspec
92 * Possible future magic semantics include stuff like:
94 * { PATHSPEC_RECURSIVE, '*', "recursive" },
95 * { PATHSPEC_REGEXP, '\0', "regexp" },
99 static struct pathspec_magic {
100 unsigned bit;
101 char mnemonic; /* this cannot be ':'! */
102 const char *name;
103 } pathspec_magic[] = {
104 { PATHSPEC_FROMTOP, '/', "top" },
105 { PATHSPEC_LITERAL, '\0', "literal" },
106 { PATHSPEC_GLOB, '\0', "glob" },
107 { PATHSPEC_ICASE, '\0', "icase" },
108 { PATHSPEC_EXCLUDE, '!', "exclude" },
109 { PATHSPEC_ATTR, '\0', "attr" },
112 static void prefix_magic(struct strbuf *sb, int prefixlen,
113 unsigned magic, const char *element)
115 /* No magic was found in element, just add prefix magic */
116 if (!magic) {
117 strbuf_addf(sb, ":(prefix:%d)", prefixlen);
118 return;
122 * At this point, we know that parse_element_magic() was able
123 * to extract some pathspec magic from element. So we know
124 * element is correctly formatted in either shorthand or
125 * longhand form
127 if (element[1] != '(') {
128 /* Process an element in shorthand form (e.g. ":!/<match>") */
129 strbuf_addstr(sb, ":(");
130 for (int i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
131 if ((magic & pathspec_magic[i].bit) &&
132 pathspec_magic[i].mnemonic) {
133 if (sb->buf[sb->len - 1] != '(')
134 strbuf_addch(sb, ',');
135 strbuf_addstr(sb, pathspec_magic[i].name);
138 } else {
139 /* For the longhand form, we copy everything up to the final ')' */
140 size_t len = strchr(element, ')') - element;
141 strbuf_add(sb, element, len);
143 strbuf_addf(sb, ",prefix:%d)", prefixlen);
146 static size_t strcspn_escaped(const char *s, const char *stop)
148 const char *i;
150 for (i = s; *i; i++) {
151 /* skip the escaped character */
152 if (i[0] == '\\' && i[1]) {
153 i++;
154 continue;
157 if (strchr(stop, *i))
158 break;
160 return i - s;
163 static inline int invalid_value_char(const char ch)
165 if (isalnum(ch) || strchr(",-_", ch))
166 return 0;
167 return -1;
170 static char *attr_value_unescape(const char *value)
172 const char *src;
173 char *dst, *ret;
175 ret = xmallocz(strlen(value));
176 for (src = value, dst = ret; *src; src++, dst++) {
177 if (*src == '\\') {
178 if (!src[1])
179 die(_("Escape character '\\' not allowed as "
180 "last character in attr value"));
181 src++;
183 if (invalid_value_char(*src))
184 die("cannot use '%c' for value matching", *src);
185 *dst = *src;
187 *dst = '\0';
188 return ret;
191 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
193 struct string_list_item *si;
194 struct string_list list = STRING_LIST_INIT_DUP;
196 if (item->attr_check || item->attr_match)
197 die(_("Only one 'attr:' specification is allowed."));
199 if (!value || !*value)
200 die(_("attr spec must not be empty"));
202 string_list_split(&list, value, ' ', -1);
203 string_list_remove_empty_items(&list, 0);
205 item->attr_check = attr_check_alloc();
206 CALLOC_ARRAY(item->attr_match, list.nr);
208 for_each_string_list_item(si, &list) {
209 size_t attr_len;
210 char *attr_name;
211 const struct git_attr *a;
213 int j = item->attr_match_nr++;
214 const char *attr = si->string;
215 struct attr_match *am = &item->attr_match[j];
217 switch (*attr) {
218 case '!':
219 am->match_mode = MATCH_UNSPECIFIED;
220 attr++;
221 attr_len = strlen(attr);
222 break;
223 case '-':
224 am->match_mode = MATCH_UNSET;
225 attr++;
226 attr_len = strlen(attr);
227 break;
228 default:
229 attr_len = strcspn(attr, "=");
230 if (attr[attr_len] != '=')
231 am->match_mode = MATCH_SET;
232 else {
233 const char *v = &attr[attr_len + 1];
234 am->match_mode = MATCH_VALUE;
235 am->value = attr_value_unescape(v);
237 break;
240 attr_name = xmemdupz(attr, attr_len);
241 a = git_attr(attr_name);
242 if (!a)
243 die(_("invalid attribute name %s"), attr_name);
245 attr_check_append(item->attr_check, a);
247 free(attr_name);
250 if (item->attr_check->nr != item->attr_match_nr)
251 BUG("should have same number of entries");
253 string_list_clear(&list, 0);
256 static inline int get_literal_global(void)
258 static int literal = -1;
260 if (literal < 0)
261 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
263 return literal;
266 static inline int get_glob_global(void)
268 static int glob = -1;
270 if (glob < 0)
271 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
273 return glob;
276 static inline int get_noglob_global(void)
278 static int noglob = -1;
280 if (noglob < 0)
281 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
283 return noglob;
286 static inline int get_icase_global(void)
288 static int icase = -1;
290 if (icase < 0)
291 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
293 return icase;
296 static int get_global_magic(int element_magic)
298 int global_magic = 0;
300 if (get_literal_global())
301 global_magic |= PATHSPEC_LITERAL;
303 /* --glob-pathspec is overridden by :(literal) */
304 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
305 global_magic |= PATHSPEC_GLOB;
307 if (get_glob_global() && get_noglob_global())
308 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
310 if (get_icase_global())
311 global_magic |= PATHSPEC_ICASE;
313 if ((global_magic & PATHSPEC_LITERAL) &&
314 (global_magic & ~PATHSPEC_LITERAL))
315 die(_("global 'literal' pathspec setting is incompatible "
316 "with all other global pathspec settings"));
318 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
319 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
320 global_magic |= PATHSPEC_LITERAL;
322 return global_magic;
326 * Parse the pathspec element looking for long magic
328 * saves all magic in 'magic'
329 * if prefix magic is used, save the prefix length in 'prefix_len'
330 * returns the position in 'elem' after all magic has been parsed
332 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
333 struct pathspec_item *item,
334 const char *elem)
336 const char *pos;
337 const char *nextat;
339 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
340 size_t len = strcspn_escaped(pos, ",)");
341 int i;
343 if (pos[len] == ',')
344 nextat = pos + len + 1; /* handle ',' */
345 else
346 nextat = pos + len; /* handle ')' and '\0' */
348 if (!len)
349 continue;
351 if (starts_with(pos, "prefix:")) {
352 char *endptr;
353 *prefix_len = strtol(pos + 7, &endptr, 10);
354 if (endptr - pos != len)
355 die(_("invalid parameter for pathspec magic 'prefix'"));
356 continue;
359 if (starts_with(pos, "attr:")) {
360 char *attr_body = xmemdupz(pos + 5, len - 5);
361 parse_pathspec_attr_match(item, attr_body);
362 *magic |= PATHSPEC_ATTR;
363 free(attr_body);
364 continue;
367 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
368 if (strlen(pathspec_magic[i].name) == len &&
369 !strncmp(pathspec_magic[i].name, pos, len)) {
370 *magic |= pathspec_magic[i].bit;
371 break;
375 if (ARRAY_SIZE(pathspec_magic) <= i)
376 die(_("Invalid pathspec magic '%.*s' in '%s'"),
377 (int) len, pos, elem);
380 if (*pos != ')')
381 die(_("Missing ')' at the end of pathspec magic in '%s'"),
382 elem);
383 pos++;
385 return pos;
389 * Parse the pathspec element looking for short magic
391 * saves all magic in 'magic'
392 * returns the position in 'elem' after all magic has been parsed
394 static const char *parse_short_magic(unsigned *magic, const char *elem)
396 const char *pos;
398 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
399 char ch = *pos;
400 int i;
402 /* Special case alias for '!' */
403 if (ch == '^') {
404 *magic |= PATHSPEC_EXCLUDE;
405 continue;
408 if (!is_pathspec_magic(ch))
409 break;
411 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
412 if (pathspec_magic[i].mnemonic == ch) {
413 *magic |= pathspec_magic[i].bit;
414 break;
418 if (ARRAY_SIZE(pathspec_magic) <= i)
419 die(_("Unimplemented pathspec magic '%c' in '%s'"),
420 ch, elem);
423 if (*pos == ':')
424 pos++;
426 return pos;
429 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
430 struct pathspec_item *item,
431 const char *elem)
433 if (elem[0] != ':' || get_literal_global())
434 return elem; /* nothing to do */
435 else if (elem[1] == '(')
436 /* longhand */
437 return parse_long_magic(magic, prefix_len, item, elem);
438 else
439 /* shorthand */
440 return parse_short_magic(magic, elem);
444 * Perform the initialization of a pathspec_item based on a pathspec element.
446 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
447 const char *prefix, int prefixlen,
448 const char *elt)
450 unsigned magic = 0, element_magic = 0;
451 const char *copyfrom = elt;
452 char *match;
453 int pathspec_prefix = -1;
455 item->attr_check = NULL;
456 item->attr_match = NULL;
457 item->attr_match_nr = 0;
459 /* PATHSPEC_LITERAL_PATH ignores magic */
460 if (flags & PATHSPEC_LITERAL_PATH) {
461 magic = PATHSPEC_LITERAL;
462 } else {
463 copyfrom = parse_element_magic(&element_magic,
464 &pathspec_prefix,
465 item,
466 elt);
467 magic |= element_magic;
468 magic |= get_global_magic(element_magic);
471 item->magic = magic;
473 if (pathspec_prefix >= 0 &&
474 (prefixlen || (prefix && *prefix)))
475 BUG("'prefix' magic is supposed to be used at worktree's root");
477 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
478 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
480 /* Create match string which will be used for pathspec matching */
481 if (pathspec_prefix >= 0) {
482 match = xstrdup(copyfrom);
483 prefixlen = pathspec_prefix;
484 } else if (magic & PATHSPEC_FROMTOP) {
485 match = xstrdup(copyfrom);
486 prefixlen = 0;
487 } else {
488 match = prefix_path_gently(prefix, prefixlen,
489 &prefixlen, copyfrom);
490 if (!match) {
491 const char *hint_path;
493 if (!have_git_dir())
494 die(_("'%s' is outside the directory tree"),
495 copyfrom);
496 hint_path = get_git_work_tree();
497 if (!hint_path)
498 hint_path = get_git_dir();
499 die(_("%s: '%s' is outside repository at '%s'"), elt,
500 copyfrom, absolute_path(hint_path));
504 item->match = match;
505 item->len = strlen(item->match);
506 item->prefix = prefixlen;
509 * Prefix the pathspec (keep all magic) and assign to
510 * original. Useful for passing to another command.
512 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
513 !get_literal_global()) {
514 struct strbuf sb = STRBUF_INIT;
516 /* Preserve the actual prefix length of each pattern */
517 prefix_magic(&sb, prefixlen, element_magic, elt);
519 strbuf_addstr(&sb, match);
520 item->original = strbuf_detach(&sb, NULL);
521 } else {
522 item->original = xstrdup(elt);
525 if (magic & PATHSPEC_LITERAL) {
526 item->nowildcard_len = item->len;
527 } else {
528 item->nowildcard_len = simple_length(item->match);
529 if (item->nowildcard_len < prefixlen)
530 item->nowildcard_len = prefixlen;
533 item->flags = 0;
534 if (magic & PATHSPEC_GLOB) {
536 * FIXME: should we enable ONESTAR in _GLOB for
537 * pattern "* * / * . c"?
539 } else {
540 if (item->nowildcard_len < item->len &&
541 item->match[item->nowildcard_len] == '*' &&
542 no_wildcard(item->match + item->nowildcard_len + 1))
543 item->flags |= PATHSPEC_ONESTAR;
546 /* sanity checks, pathspec matchers assume these are sane */
547 if (item->nowildcard_len > item->len ||
548 item->prefix > item->len) {
549 BUG("error initializing pathspec_item");
553 static int pathspec_item_cmp(const void *a_, const void *b_)
555 struct pathspec_item *a, *b;
557 a = (struct pathspec_item *)a_;
558 b = (struct pathspec_item *)b_;
559 return strcmp(a->match, b->match);
562 void pathspec_magic_names(unsigned magic, struct strbuf *out)
564 int i;
565 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
566 const struct pathspec_magic *m = pathspec_magic + i;
567 if (!(magic & m->bit))
568 continue;
569 if (out->len)
570 strbuf_addstr(out, ", ");
572 if (m->mnemonic)
573 strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
574 m->name, m->mnemonic);
575 else
576 strbuf_addf(out, "'%s'", m->name);
580 static void NORETURN unsupported_magic(const char *pattern,
581 unsigned magic)
583 struct strbuf sb = STRBUF_INIT;
584 pathspec_magic_names(magic, &sb);
586 * We may want to substitute "this command" with a command
587 * name. E.g. when "git add -p" or "git add -i" dies when running
588 * "checkout -p"
590 die(_("%s: pathspec magic not supported by this command: %s"),
591 pattern, sb.buf);
594 void parse_pathspec(struct pathspec *pathspec,
595 unsigned magic_mask, unsigned flags,
596 const char *prefix, const char **argv)
598 struct pathspec_item *item;
599 const char *entry = argv ? *argv : NULL;
600 int i, n, prefixlen, nr_exclude = 0;
602 memset(pathspec, 0, sizeof(*pathspec));
604 if (flags & PATHSPEC_MAXDEPTH_VALID)
605 pathspec->magic |= PATHSPEC_MAXDEPTH;
607 /* No arguments, no prefix -> no pathspec */
608 if (!entry && !prefix)
609 return;
611 if ((flags & PATHSPEC_PREFER_CWD) &&
612 (flags & PATHSPEC_PREFER_FULL))
613 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
615 /* No arguments with prefix -> prefix pathspec */
616 if (!entry) {
617 if (flags & PATHSPEC_PREFER_FULL)
618 return;
620 if (!(flags & PATHSPEC_PREFER_CWD))
621 BUG("PATHSPEC_PREFER_CWD requires arguments");
623 pathspec->items = CALLOC_ARRAY(item, 1);
624 item->match = xstrdup(prefix);
625 item->original = xstrdup(prefix);
626 item->nowildcard_len = item->len = strlen(prefix);
627 item->prefix = item->len;
628 pathspec->nr = 1;
629 return;
632 n = 0;
633 while (argv[n]) {
634 if (*argv[n] == '\0')
635 die("empty string is not a valid pathspec. "
636 "please use . instead if you meant to match all paths");
637 n++;
640 pathspec->nr = n;
641 ALLOC_ARRAY(pathspec->items, n + 1);
642 item = pathspec->items;
643 prefixlen = prefix ? strlen(prefix) : 0;
645 for (i = 0; i < n; i++) {
646 entry = argv[i];
648 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
650 if (item[i].magic & PATHSPEC_EXCLUDE)
651 nr_exclude++;
652 if (item[i].magic & magic_mask)
653 unsupported_magic(entry, item[i].magic & magic_mask);
655 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
656 has_symlink_leading_path(item[i].match, item[i].len)) {
657 die(_("pathspec '%s' is beyond a symbolic link"), entry);
660 if (item[i].nowildcard_len < item[i].len)
661 pathspec->has_wildcard = 1;
662 pathspec->magic |= item[i].magic;
666 * If everything is an exclude pattern, add one positive pattern
667 * that matches everything. We allocated an extra one for this.
669 if (nr_exclude == n) {
670 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
671 init_pathspec_item(item + n, 0, prefix, plen, ".");
672 pathspec->nr++;
675 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
676 if (flags & PATHSPEC_KEEP_ORDER)
677 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
678 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
682 void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
683 unsigned flags, const char *prefix,
684 const char *file, int nul_term_line)
686 struct strvec parsed_file = STRVEC_INIT;
687 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
688 strbuf_getline;
689 struct strbuf buf = STRBUF_INIT;
690 struct strbuf unquoted = STRBUF_INIT;
691 FILE *in;
693 if (!strcmp(file, "-"))
694 in = stdin;
695 else
696 in = xfopen(file, "r");
698 while (getline_fn(&buf, in) != EOF) {
699 if (!nul_term_line && buf.buf[0] == '"') {
700 strbuf_reset(&unquoted);
701 if (unquote_c_style(&unquoted, buf.buf, NULL))
702 die(_("line is badly quoted: %s"), buf.buf);
703 strbuf_swap(&buf, &unquoted);
705 strvec_push(&parsed_file, buf.buf);
706 strbuf_reset(&buf);
709 strbuf_release(&unquoted);
710 strbuf_release(&buf);
711 if (in != stdin)
712 fclose(in);
714 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
715 strvec_clear(&parsed_file);
718 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
720 int i, j;
722 *dst = *src;
723 DUP_ARRAY(dst->items, src->items, dst->nr);
725 for (i = 0; i < dst->nr; i++) {
726 struct pathspec_item *d = &dst->items[i];
727 struct pathspec_item *s = &src->items[i];
729 d->match = xstrdup(s->match);
730 d->original = xstrdup(s->original);
732 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
733 for (j = 0; j < d->attr_match_nr; j++) {
734 const char *value = s->attr_match[j].value;
735 d->attr_match[j].value = xstrdup_or_null(value);
738 d->attr_check = attr_check_dup(s->attr_check);
742 void clear_pathspec(struct pathspec *pathspec)
744 int i, j;
746 for (i = 0; i < pathspec->nr; i++) {
747 free(pathspec->items[i].match);
748 free(pathspec->items[i].original);
750 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
751 free(pathspec->items[i].attr_match[j].value);
752 free(pathspec->items[i].attr_match);
754 if (pathspec->items[i].attr_check)
755 attr_check_free(pathspec->items[i].attr_check);
758 FREE_AND_NULL(pathspec->items);
759 pathspec->nr = 0;
762 int match_pathspec_attrs(struct index_state *istate,
763 const char *name, int namelen,
764 const struct pathspec_item *item)
766 int i;
767 char *to_free = NULL;
769 if (name[namelen])
770 name = to_free = xmemdupz(name, namelen);
772 git_check_attr(istate, name, item->attr_check);
774 free(to_free);
776 for (i = 0; i < item->attr_match_nr; i++) {
777 const char *value;
778 int matched;
779 enum attr_match_mode match_mode;
781 value = item->attr_check->items[i].value;
782 match_mode = item->attr_match[i].match_mode;
784 if (ATTR_TRUE(value))
785 matched = (match_mode == MATCH_SET);
786 else if (ATTR_FALSE(value))
787 matched = (match_mode == MATCH_UNSET);
788 else if (ATTR_UNSET(value))
789 matched = (match_mode == MATCH_UNSPECIFIED);
790 else
791 matched = (match_mode == MATCH_VALUE &&
792 !strcmp(item->attr_match[i].value, value));
793 if (!matched)
794 return 0;
797 return 1;
800 int pathspec_needs_expanded_index(struct index_state *istate,
801 const struct pathspec *pathspec)
803 unsigned int i, pos;
804 int res = 0;
805 char *skip_worktree_seen = NULL;
808 * If index is not sparse, no index expansion is needed.
810 if (!istate->sparse_index)
811 return 0;
814 * When using a magic pathspec, assume for the sake of simplicity that
815 * the index needs to be expanded to match all matchable files.
817 if (pathspec->magic)
818 return 1;
820 for (i = 0; i < pathspec->nr; i++) {
821 struct pathspec_item item = pathspec->items[i];
824 * If the pathspec item has a wildcard, the index should be expanded
825 * if the pathspec has the possibility of matching a subset of entries inside
826 * of a sparse directory (but not the entire directory).
828 * If the pathspec item is a literal path, the index only needs to be expanded
829 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
830 * expand for in-cone files) and b) it doesn't match any sparse directories
831 * (since we can reset whole sparse directories without expanding them).
833 if (item.nowildcard_len < item.len) {
835 * Special case: if the pattern is a path inside the cone
836 * followed by only wildcards, the pattern cannot match
837 * partial sparse directories, so we know we don't need to
838 * expand the index.
840 * Examples:
841 * - in-cone/foo***: doesn't need expanded index
842 * - not-in-cone/bar*: may need expanded index
843 * - **.c: may need expanded index
845 if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
846 path_in_cone_mode_sparse_checkout(item.original, istate))
847 continue;
849 for (pos = 0; pos < istate->cache_nr; pos++) {
850 struct cache_entry *ce = istate->cache[pos];
852 if (!S_ISSPARSEDIR(ce->ce_mode))
853 continue;
856 * If the pre-wildcard length is longer than the sparse
857 * directory name and the sparse directory is the first
858 * component of the pathspec, need to expand the index.
860 if (item.nowildcard_len > ce_namelen(ce) &&
861 !strncmp(item.original, ce->name, ce_namelen(ce))) {
862 res = 1;
863 break;
867 * If the pre-wildcard length is shorter than the sparse
868 * directory and the pathspec does not match the whole
869 * directory, need to expand the index.
871 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
872 wildmatch(item.original, ce->name, 0)) {
873 res = 1;
874 break;
877 } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
878 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
879 res = 1;
881 if (res > 0)
882 break;
885 free(skip_worktree_seen);
886 return res;