read-cache*.h: move declarations for read-cache.c functions from cache.h
[alt-git.git] / pathspec.c
blobf52ce60e41a932f2f6cd1528caab450b79a12908
1 #include "cache.h"
2 #include "abspath.h"
3 #include "config.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"
17 * Finds which of the given pathspecs match items in the index.
19 * For each pathspec, sets the corresponding entry in the seen[] array
20 * (which should be specs items long, i.e. the same size as pathspec)
21 * to the nature of the "closest" (i.e. most specific) match found for
22 * that pathspec in the index, if it was a closer type of match than
23 * the existing entry. As an optimization, matching is skipped
24 * altogether if seen[] already only contains non-zero entries.
26 * If seen[] has not already been written to, it may make sense
27 * to use find_pathspecs_matching_against_index() instead.
29 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
30 struct index_state *istate,
31 char *seen,
32 enum ps_skip_worktree_action sw_action)
34 int num_unmatched = 0, i;
37 * Since we are walking the index as if we were walking the directory,
38 * we have to mark the matched pathspec as seen; otherwise we will
39 * mistakenly think that the user gave a pathspec that did not match
40 * anything.
42 for (i = 0; i < pathspec->nr; i++)
43 if (!seen[i])
44 num_unmatched++;
45 if (!num_unmatched)
46 return;
47 for (i = 0; i < istate->cache_nr; i++) {
48 const struct cache_entry *ce = istate->cache[i];
49 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
50 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
51 continue;
52 ce_path_match(istate, ce, pathspec, seen);
57 * Finds which of the given pathspecs match items in the index.
59 * This is a one-shot wrapper around add_pathspec_matches_against_index()
60 * which allocates, populates, and returns a seen[] array indicating the
61 * nature of the "closest" (i.e. most specific) matches which each of the
62 * given pathspecs achieves against all items in the index.
64 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
65 struct index_state *istate,
66 enum ps_skip_worktree_action sw_action)
68 char *seen = xcalloc(pathspec->nr, 1);
69 add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
70 return seen;
73 char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
75 struct index_state *istate = the_repository->index;
76 char *seen = xcalloc(pathspec->nr, 1);
77 int i;
79 for (i = 0; i < istate->cache_nr; i++) {
80 struct cache_entry *ce = istate->cache[i];
81 if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
82 ce_path_match(istate, ce, pathspec, seen);
85 return seen;
89 * Magic pathspec
91 * Possible future magic semantics include stuff like:
93 * { PATHSPEC_RECURSIVE, '*', "recursive" },
94 * { PATHSPEC_REGEXP, '\0', "regexp" },
98 static struct pathspec_magic {
99 unsigned bit;
100 char mnemonic; /* this cannot be ':'! */
101 const char *name;
102 } pathspec_magic[] = {
103 { PATHSPEC_FROMTOP, '/', "top" },
104 { PATHSPEC_LITERAL, '\0', "literal" },
105 { PATHSPEC_GLOB, '\0', "glob" },
106 { PATHSPEC_ICASE, '\0', "icase" },
107 { PATHSPEC_EXCLUDE, '!', "exclude" },
108 { PATHSPEC_ATTR, '\0', "attr" },
111 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
113 int i;
114 strbuf_addstr(sb, ":(");
115 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
116 if (magic & pathspec_magic[i].bit) {
117 if (sb->buf[sb->len - 1] != '(')
118 strbuf_addch(sb, ',');
119 strbuf_addstr(sb, pathspec_magic[i].name);
121 strbuf_addf(sb, ",prefix:%d)", prefixlen);
124 static size_t strcspn_escaped(const char *s, const char *stop)
126 const char *i;
128 for (i = s; *i; i++) {
129 /* skip the escaped character */
130 if (i[0] == '\\' && i[1]) {
131 i++;
132 continue;
135 if (strchr(stop, *i))
136 break;
138 return i - s;
141 static inline int invalid_value_char(const char ch)
143 if (isalnum(ch) || strchr(",-_", ch))
144 return 0;
145 return -1;
148 static char *attr_value_unescape(const char *value)
150 const char *src;
151 char *dst, *ret;
153 ret = xmallocz(strlen(value));
154 for (src = value, dst = ret; *src; src++, dst++) {
155 if (*src == '\\') {
156 if (!src[1])
157 die(_("Escape character '\\' not allowed as "
158 "last character in attr value"));
159 src++;
161 if (invalid_value_char(*src))
162 die("cannot use '%c' for value matching", *src);
163 *dst = *src;
165 *dst = '\0';
166 return ret;
169 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
171 struct string_list_item *si;
172 struct string_list list = STRING_LIST_INIT_DUP;
174 if (item->attr_check || item->attr_match)
175 die(_("Only one 'attr:' specification is allowed."));
177 if (!value || !*value)
178 die(_("attr spec must not be empty"));
180 string_list_split(&list, value, ' ', -1);
181 string_list_remove_empty_items(&list, 0);
183 item->attr_check = attr_check_alloc();
184 CALLOC_ARRAY(item->attr_match, list.nr);
186 for_each_string_list_item(si, &list) {
187 size_t attr_len;
188 char *attr_name;
189 const struct git_attr *a;
191 int j = item->attr_match_nr++;
192 const char *attr = si->string;
193 struct attr_match *am = &item->attr_match[j];
195 switch (*attr) {
196 case '!':
197 am->match_mode = MATCH_UNSPECIFIED;
198 attr++;
199 attr_len = strlen(attr);
200 break;
201 case '-':
202 am->match_mode = MATCH_UNSET;
203 attr++;
204 attr_len = strlen(attr);
205 break;
206 default:
207 attr_len = strcspn(attr, "=");
208 if (attr[attr_len] != '=')
209 am->match_mode = MATCH_SET;
210 else {
211 const char *v = &attr[attr_len + 1];
212 am->match_mode = MATCH_VALUE;
213 am->value = attr_value_unescape(v);
215 break;
218 attr_name = xmemdupz(attr, attr_len);
219 a = git_attr(attr_name);
220 if (!a)
221 die(_("invalid attribute name %s"), attr_name);
223 attr_check_append(item->attr_check, a);
225 free(attr_name);
228 if (item->attr_check->nr != item->attr_match_nr)
229 BUG("should have same number of entries");
231 string_list_clear(&list, 0);
234 static inline int get_literal_global(void)
236 static int literal = -1;
238 if (literal < 0)
239 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
241 return literal;
244 static inline int get_glob_global(void)
246 static int glob = -1;
248 if (glob < 0)
249 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
251 return glob;
254 static inline int get_noglob_global(void)
256 static int noglob = -1;
258 if (noglob < 0)
259 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
261 return noglob;
264 static inline int get_icase_global(void)
266 static int icase = -1;
268 if (icase < 0)
269 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
271 return icase;
274 static int get_global_magic(int element_magic)
276 int global_magic = 0;
278 if (get_literal_global())
279 global_magic |= PATHSPEC_LITERAL;
281 /* --glob-pathspec is overridden by :(literal) */
282 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
283 global_magic |= PATHSPEC_GLOB;
285 if (get_glob_global() && get_noglob_global())
286 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
288 if (get_icase_global())
289 global_magic |= PATHSPEC_ICASE;
291 if ((global_magic & PATHSPEC_LITERAL) &&
292 (global_magic & ~PATHSPEC_LITERAL))
293 die(_("global 'literal' pathspec setting is incompatible "
294 "with all other global pathspec settings"));
296 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
297 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
298 global_magic |= PATHSPEC_LITERAL;
300 return global_magic;
304 * Parse the pathspec element looking for long magic
306 * saves all magic in 'magic'
307 * if prefix magic is used, save the prefix length in 'prefix_len'
308 * returns the position in 'elem' after all magic has been parsed
310 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
311 struct pathspec_item *item,
312 const char *elem)
314 const char *pos;
315 const char *nextat;
317 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
318 size_t len = strcspn_escaped(pos, ",)");
319 int i;
321 if (pos[len] == ',')
322 nextat = pos + len + 1; /* handle ',' */
323 else
324 nextat = pos + len; /* handle ')' and '\0' */
326 if (!len)
327 continue;
329 if (starts_with(pos, "prefix:")) {
330 char *endptr;
331 *prefix_len = strtol(pos + 7, &endptr, 10);
332 if (endptr - pos != len)
333 die(_("invalid parameter for pathspec magic 'prefix'"));
334 continue;
337 if (starts_with(pos, "attr:")) {
338 char *attr_body = xmemdupz(pos + 5, len - 5);
339 parse_pathspec_attr_match(item, attr_body);
340 *magic |= PATHSPEC_ATTR;
341 free(attr_body);
342 continue;
345 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
346 if (strlen(pathspec_magic[i].name) == len &&
347 !strncmp(pathspec_magic[i].name, pos, len)) {
348 *magic |= pathspec_magic[i].bit;
349 break;
353 if (ARRAY_SIZE(pathspec_magic) <= i)
354 die(_("Invalid pathspec magic '%.*s' in '%s'"),
355 (int) len, pos, elem);
358 if (*pos != ')')
359 die(_("Missing ')' at the end of pathspec magic in '%s'"),
360 elem);
361 pos++;
363 return pos;
367 * Parse the pathspec element looking for short magic
369 * saves all magic in 'magic'
370 * returns the position in 'elem' after all magic has been parsed
372 static const char *parse_short_magic(unsigned *magic, const char *elem)
374 const char *pos;
376 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
377 char ch = *pos;
378 int i;
380 /* Special case alias for '!' */
381 if (ch == '^') {
382 *magic |= PATHSPEC_EXCLUDE;
383 continue;
386 if (!is_pathspec_magic(ch))
387 break;
389 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
390 if (pathspec_magic[i].mnemonic == ch) {
391 *magic |= pathspec_magic[i].bit;
392 break;
396 if (ARRAY_SIZE(pathspec_magic) <= i)
397 die(_("Unimplemented pathspec magic '%c' in '%s'"),
398 ch, elem);
401 if (*pos == ':')
402 pos++;
404 return pos;
407 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
408 struct pathspec_item *item,
409 const char *elem)
411 if (elem[0] != ':' || get_literal_global())
412 return elem; /* nothing to do */
413 else if (elem[1] == '(')
414 /* longhand */
415 return parse_long_magic(magic, prefix_len, item, elem);
416 else
417 /* shorthand */
418 return parse_short_magic(magic, elem);
422 * Perform the initialization of a pathspec_item based on a pathspec element.
424 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
425 const char *prefix, int prefixlen,
426 const char *elt)
428 unsigned magic = 0, element_magic = 0;
429 const char *copyfrom = elt;
430 char *match;
431 int pathspec_prefix = -1;
433 item->attr_check = NULL;
434 item->attr_match = NULL;
435 item->attr_match_nr = 0;
437 /* PATHSPEC_LITERAL_PATH ignores magic */
438 if (flags & PATHSPEC_LITERAL_PATH) {
439 magic = PATHSPEC_LITERAL;
440 } else {
441 copyfrom = parse_element_magic(&element_magic,
442 &pathspec_prefix,
443 item,
444 elt);
445 magic |= element_magic;
446 magic |= get_global_magic(element_magic);
449 item->magic = magic;
451 if (pathspec_prefix >= 0 &&
452 (prefixlen || (prefix && *prefix)))
453 BUG("'prefix' magic is supposed to be used at worktree's root");
455 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
456 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
458 /* Create match string which will be used for pathspec matching */
459 if (pathspec_prefix >= 0) {
460 match = xstrdup(copyfrom);
461 prefixlen = pathspec_prefix;
462 } else if (magic & PATHSPEC_FROMTOP) {
463 match = xstrdup(copyfrom);
464 prefixlen = 0;
465 } else {
466 match = prefix_path_gently(prefix, prefixlen,
467 &prefixlen, copyfrom);
468 if (!match) {
469 const char *hint_path = get_git_work_tree();
470 if (!hint_path)
471 hint_path = get_git_dir();
472 die(_("%s: '%s' is outside repository at '%s'"), elt,
473 copyfrom, absolute_path(hint_path));
477 item->match = match;
478 item->len = strlen(item->match);
479 item->prefix = prefixlen;
482 * Prefix the pathspec (keep all magic) and assign to
483 * original. Useful for passing to another command.
485 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
486 !get_literal_global()) {
487 struct strbuf sb = STRBUF_INIT;
489 /* Preserve the actual prefix length of each pattern */
490 prefix_magic(&sb, prefixlen, element_magic);
492 strbuf_addstr(&sb, match);
493 item->original = strbuf_detach(&sb, NULL);
494 } else {
495 item->original = xstrdup(elt);
498 if (magic & PATHSPEC_LITERAL) {
499 item->nowildcard_len = item->len;
500 } else {
501 item->nowildcard_len = simple_length(item->match);
502 if (item->nowildcard_len < prefixlen)
503 item->nowildcard_len = prefixlen;
506 item->flags = 0;
507 if (magic & PATHSPEC_GLOB) {
509 * FIXME: should we enable ONESTAR in _GLOB for
510 * pattern "* * / * . c"?
512 } else {
513 if (item->nowildcard_len < item->len &&
514 item->match[item->nowildcard_len] == '*' &&
515 no_wildcard(item->match + item->nowildcard_len + 1))
516 item->flags |= PATHSPEC_ONESTAR;
519 /* sanity checks, pathspec matchers assume these are sane */
520 if (item->nowildcard_len > item->len ||
521 item->prefix > item->len) {
522 BUG("error initializing pathspec_item");
526 static int pathspec_item_cmp(const void *a_, const void *b_)
528 struct pathspec_item *a, *b;
530 a = (struct pathspec_item *)a_;
531 b = (struct pathspec_item *)b_;
532 return strcmp(a->match, b->match);
535 void pathspec_magic_names(unsigned magic, struct strbuf *out)
537 int i;
538 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
539 const struct pathspec_magic *m = pathspec_magic + i;
540 if (!(magic & m->bit))
541 continue;
542 if (out->len)
543 strbuf_addstr(out, ", ");
545 if (m->mnemonic)
546 strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
547 m->name, m->mnemonic);
548 else
549 strbuf_addf(out, "'%s'", m->name);
553 static void NORETURN unsupported_magic(const char *pattern,
554 unsigned magic)
556 struct strbuf sb = STRBUF_INIT;
557 pathspec_magic_names(magic, &sb);
559 * We may want to substitute "this command" with a command
560 * name. E.g. when "git add -p" or "git add -i" dies when running
561 * "checkout -p"
563 die(_("%s: pathspec magic not supported by this command: %s"),
564 pattern, sb.buf);
567 void parse_pathspec(struct pathspec *pathspec,
568 unsigned magic_mask, unsigned flags,
569 const char *prefix, const char **argv)
571 struct pathspec_item *item;
572 const char *entry = argv ? *argv : NULL;
573 int i, n, prefixlen, nr_exclude = 0;
575 memset(pathspec, 0, sizeof(*pathspec));
577 if (flags & PATHSPEC_MAXDEPTH_VALID)
578 pathspec->magic |= PATHSPEC_MAXDEPTH;
580 /* No arguments, no prefix -> no pathspec */
581 if (!entry && !prefix)
582 return;
584 if ((flags & PATHSPEC_PREFER_CWD) &&
585 (flags & PATHSPEC_PREFER_FULL))
586 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
588 /* No arguments with prefix -> prefix pathspec */
589 if (!entry) {
590 if (flags & PATHSPEC_PREFER_FULL)
591 return;
593 if (!(flags & PATHSPEC_PREFER_CWD))
594 BUG("PATHSPEC_PREFER_CWD requires arguments");
596 pathspec->items = CALLOC_ARRAY(item, 1);
597 item->match = xstrdup(prefix);
598 item->original = xstrdup(prefix);
599 item->nowildcard_len = item->len = strlen(prefix);
600 item->prefix = item->len;
601 pathspec->nr = 1;
602 return;
605 n = 0;
606 while (argv[n]) {
607 if (*argv[n] == '\0')
608 die("empty string is not a valid pathspec. "
609 "please use . instead if you meant to match all paths");
610 n++;
613 pathspec->nr = n;
614 ALLOC_ARRAY(pathspec->items, n + 1);
615 item = pathspec->items;
616 prefixlen = prefix ? strlen(prefix) : 0;
618 for (i = 0; i < n; i++) {
619 entry = argv[i];
621 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
623 if (item[i].magic & PATHSPEC_EXCLUDE)
624 nr_exclude++;
625 if (item[i].magic & magic_mask)
626 unsupported_magic(entry, item[i].magic & magic_mask);
628 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
629 has_symlink_leading_path(item[i].match, item[i].len)) {
630 die(_("pathspec '%s' is beyond a symbolic link"), entry);
633 if (item[i].nowildcard_len < item[i].len)
634 pathspec->has_wildcard = 1;
635 pathspec->magic |= item[i].magic;
639 * If everything is an exclude pattern, add one positive pattern
640 * that matches everything. We allocated an extra one for this.
642 if (nr_exclude == n) {
643 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
644 init_pathspec_item(item + n, 0, prefix, plen, ".");
645 pathspec->nr++;
648 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
649 if (flags & PATHSPEC_KEEP_ORDER)
650 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
651 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
655 void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
656 unsigned flags, const char *prefix,
657 const char *file, int nul_term_line)
659 struct strvec parsed_file = STRVEC_INIT;
660 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
661 strbuf_getline;
662 struct strbuf buf = STRBUF_INIT;
663 struct strbuf unquoted = STRBUF_INIT;
664 FILE *in;
666 if (!strcmp(file, "-"))
667 in = stdin;
668 else
669 in = xfopen(file, "r");
671 while (getline_fn(&buf, in) != EOF) {
672 if (!nul_term_line && buf.buf[0] == '"') {
673 strbuf_reset(&unquoted);
674 if (unquote_c_style(&unquoted, buf.buf, NULL))
675 die(_("line is badly quoted: %s"), buf.buf);
676 strbuf_swap(&buf, &unquoted);
678 strvec_push(&parsed_file, buf.buf);
679 strbuf_reset(&buf);
682 strbuf_release(&unquoted);
683 strbuf_release(&buf);
684 if (in != stdin)
685 fclose(in);
687 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
688 strvec_clear(&parsed_file);
691 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
693 int i, j;
695 *dst = *src;
696 DUP_ARRAY(dst->items, src->items, dst->nr);
698 for (i = 0; i < dst->nr; i++) {
699 struct pathspec_item *d = &dst->items[i];
700 struct pathspec_item *s = &src->items[i];
702 d->match = xstrdup(s->match);
703 d->original = xstrdup(s->original);
705 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
706 for (j = 0; j < d->attr_match_nr; j++) {
707 const char *value = s->attr_match[j].value;
708 d->attr_match[j].value = xstrdup_or_null(value);
711 d->attr_check = attr_check_dup(s->attr_check);
715 void clear_pathspec(struct pathspec *pathspec)
717 int i, j;
719 for (i = 0; i < pathspec->nr; i++) {
720 free(pathspec->items[i].match);
721 free(pathspec->items[i].original);
723 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
724 free(pathspec->items[i].attr_match[j].value);
725 free(pathspec->items[i].attr_match);
727 if (pathspec->items[i].attr_check)
728 attr_check_free(pathspec->items[i].attr_check);
731 FREE_AND_NULL(pathspec->items);
732 pathspec->nr = 0;
735 int match_pathspec_attrs(struct index_state *istate,
736 const char *name, int namelen,
737 const struct pathspec_item *item)
739 int i;
740 char *to_free = NULL;
742 if (name[namelen])
743 name = to_free = xmemdupz(name, namelen);
745 git_check_attr(istate, name, item->attr_check);
747 free(to_free);
749 for (i = 0; i < item->attr_match_nr; i++) {
750 const char *value;
751 int matched;
752 enum attr_match_mode match_mode;
754 value = item->attr_check->items[i].value;
755 match_mode = item->attr_match[i].match_mode;
757 if (ATTR_TRUE(value))
758 matched = (match_mode == MATCH_SET);
759 else if (ATTR_FALSE(value))
760 matched = (match_mode == MATCH_UNSET);
761 else if (ATTR_UNSET(value))
762 matched = (match_mode == MATCH_UNSPECIFIED);
763 else
764 matched = (match_mode == MATCH_VALUE &&
765 !strcmp(item->attr_match[i].value, value));
766 if (!matched)
767 return 0;
770 return 1;
773 int pathspec_needs_expanded_index(struct index_state *istate,
774 const struct pathspec *pathspec)
776 unsigned int i, pos;
777 int res = 0;
778 char *skip_worktree_seen = NULL;
781 * If index is not sparse, no index expansion is needed.
783 if (!istate->sparse_index)
784 return 0;
787 * When using a magic pathspec, assume for the sake of simplicity that
788 * the index needs to be expanded to match all matchable files.
790 if (pathspec->magic)
791 return 1;
793 for (i = 0; i < pathspec->nr; i++) {
794 struct pathspec_item item = pathspec->items[i];
797 * If the pathspec item has a wildcard, the index should be expanded
798 * if the pathspec has the possibility of matching a subset of entries inside
799 * of a sparse directory (but not the entire directory).
801 * If the pathspec item is a literal path, the index only needs to be expanded
802 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
803 * expand for in-cone files) and b) it doesn't match any sparse directories
804 * (since we can reset whole sparse directories without expanding them).
806 if (item.nowildcard_len < item.len) {
808 * Special case: if the pattern is a path inside the cone
809 * followed by only wildcards, the pattern cannot match
810 * partial sparse directories, so we know we don't need to
811 * expand the index.
813 * Examples:
814 * - in-cone/foo***: doesn't need expanded index
815 * - not-in-cone/bar*: may need expanded index
816 * - **.c: may need expanded index
818 if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
819 path_in_cone_mode_sparse_checkout(item.original, istate))
820 continue;
822 for (pos = 0; pos < istate->cache_nr; pos++) {
823 struct cache_entry *ce = istate->cache[pos];
825 if (!S_ISSPARSEDIR(ce->ce_mode))
826 continue;
829 * If the pre-wildcard length is longer than the sparse
830 * directory name and the sparse directory is the first
831 * component of the pathspec, need to expand the index.
833 if (item.nowildcard_len > ce_namelen(ce) &&
834 !strncmp(item.original, ce->name, ce_namelen(ce))) {
835 res = 1;
836 break;
840 * If the pre-wildcard length is shorter than the sparse
841 * directory and the pathspec does not match the whole
842 * directory, need to expand the index.
844 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
845 wildmatch(item.original, ce->name, 0)) {
846 res = 1;
847 break;
850 } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
851 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
852 res = 1;
854 if (res > 0)
855 break;
858 free(skip_worktree_seen);
859 return res;