Merge branch 'fc/doc-checkout-markup-updates'
[alt-git.git] / pathspec.c
blob6972d515f0c7dbb93292361720603d9a80bfa44e
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 "setup.h"
10 #include "strvec.h"
11 #include "quote.h"
14 * Finds which of the given pathspecs match items in the index.
16 * For each pathspec, sets the corresponding entry in the seen[] array
17 * (which should be specs items long, i.e. the same size as pathspec)
18 * to the nature of the "closest" (i.e. most specific) match found for
19 * that pathspec in the index, if it was a closer type of match than
20 * the existing entry. As an optimization, matching is skipped
21 * altogether if seen[] already only contains non-zero entries.
23 * If seen[] has not already been written to, it may make sense
24 * to use find_pathspecs_matching_against_index() instead.
26 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
27 struct index_state *istate,
28 char *seen,
29 enum ps_skip_worktree_action sw_action)
31 int num_unmatched = 0, i;
34 * Since we are walking the index as if we were walking the directory,
35 * we have to mark the matched pathspec as seen; otherwise we will
36 * mistakenly think that the user gave a pathspec that did not match
37 * anything.
39 for (i = 0; i < pathspec->nr; i++)
40 if (!seen[i])
41 num_unmatched++;
42 if (!num_unmatched)
43 return;
44 for (i = 0; i < istate->cache_nr; i++) {
45 const struct cache_entry *ce = istate->cache[i];
46 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
47 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
48 continue;
49 ce_path_match(istate, ce, pathspec, seen);
54 * Finds which of the given pathspecs match items in the index.
56 * This is a one-shot wrapper around add_pathspec_matches_against_index()
57 * which allocates, populates, and returns a seen[] array indicating the
58 * nature of the "closest" (i.e. most specific) matches which each of the
59 * given pathspecs achieves against all items in the index.
61 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
62 struct index_state *istate,
63 enum ps_skip_worktree_action sw_action)
65 char *seen = xcalloc(pathspec->nr, 1);
66 add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
67 return seen;
70 char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
72 struct index_state *istate = the_repository->index;
73 char *seen = xcalloc(pathspec->nr, 1);
74 int i;
76 for (i = 0; i < istate->cache_nr; i++) {
77 struct cache_entry *ce = istate->cache[i];
78 if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
79 ce_path_match(istate, ce, pathspec, seen);
82 return seen;
86 * Magic pathspec
88 * Possible future magic semantics include stuff like:
90 * { PATHSPEC_RECURSIVE, '*', "recursive" },
91 * { PATHSPEC_REGEXP, '\0', "regexp" },
95 static struct pathspec_magic {
96 unsigned bit;
97 char mnemonic; /* this cannot be ':'! */
98 const char *name;
99 } pathspec_magic[] = {
100 { PATHSPEC_FROMTOP, '/', "top" },
101 { PATHSPEC_LITERAL, '\0', "literal" },
102 { PATHSPEC_GLOB, '\0', "glob" },
103 { PATHSPEC_ICASE, '\0', "icase" },
104 { PATHSPEC_EXCLUDE, '!', "exclude" },
105 { PATHSPEC_ATTR, '\0', "attr" },
108 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
110 int i;
111 strbuf_addstr(sb, ":(");
112 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
113 if (magic & pathspec_magic[i].bit) {
114 if (sb->buf[sb->len - 1] != '(')
115 strbuf_addch(sb, ',');
116 strbuf_addstr(sb, pathspec_magic[i].name);
118 strbuf_addf(sb, ",prefix:%d)", prefixlen);
121 static size_t strcspn_escaped(const char *s, const char *stop)
123 const char *i;
125 for (i = s; *i; i++) {
126 /* skip the escaped character */
127 if (i[0] == '\\' && i[1]) {
128 i++;
129 continue;
132 if (strchr(stop, *i))
133 break;
135 return i - s;
138 static inline int invalid_value_char(const char ch)
140 if (isalnum(ch) || strchr(",-_", ch))
141 return 0;
142 return -1;
145 static char *attr_value_unescape(const char *value)
147 const char *src;
148 char *dst, *ret;
150 ret = xmallocz(strlen(value));
151 for (src = value, dst = ret; *src; src++, dst++) {
152 if (*src == '\\') {
153 if (!src[1])
154 die(_("Escape character '\\' not allowed as "
155 "last character in attr value"));
156 src++;
158 if (invalid_value_char(*src))
159 die("cannot use '%c' for value matching", *src);
160 *dst = *src;
162 *dst = '\0';
163 return ret;
166 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
168 struct string_list_item *si;
169 struct string_list list = STRING_LIST_INIT_DUP;
171 if (item->attr_check || item->attr_match)
172 die(_("Only one 'attr:' specification is allowed."));
174 if (!value || !*value)
175 die(_("attr spec must not be empty"));
177 string_list_split(&list, value, ' ', -1);
178 string_list_remove_empty_items(&list, 0);
180 item->attr_check = attr_check_alloc();
181 CALLOC_ARRAY(item->attr_match, list.nr);
183 for_each_string_list_item(si, &list) {
184 size_t attr_len;
185 char *attr_name;
186 const struct git_attr *a;
188 int j = item->attr_match_nr++;
189 const char *attr = si->string;
190 struct attr_match *am = &item->attr_match[j];
192 switch (*attr) {
193 case '!':
194 am->match_mode = MATCH_UNSPECIFIED;
195 attr++;
196 attr_len = strlen(attr);
197 break;
198 case '-':
199 am->match_mode = MATCH_UNSET;
200 attr++;
201 attr_len = strlen(attr);
202 break;
203 default:
204 attr_len = strcspn(attr, "=");
205 if (attr[attr_len] != '=')
206 am->match_mode = MATCH_SET;
207 else {
208 const char *v = &attr[attr_len + 1];
209 am->match_mode = MATCH_VALUE;
210 am->value = attr_value_unescape(v);
212 break;
215 attr_name = xmemdupz(attr, attr_len);
216 a = git_attr(attr_name);
217 if (!a)
218 die(_("invalid attribute name %s"), attr_name);
220 attr_check_append(item->attr_check, a);
222 free(attr_name);
225 if (item->attr_check->nr != item->attr_match_nr)
226 BUG("should have same number of entries");
228 string_list_clear(&list, 0);
231 static inline int get_literal_global(void)
233 static int literal = -1;
235 if (literal < 0)
236 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
238 return literal;
241 static inline int get_glob_global(void)
243 static int glob = -1;
245 if (glob < 0)
246 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
248 return glob;
251 static inline int get_noglob_global(void)
253 static int noglob = -1;
255 if (noglob < 0)
256 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
258 return noglob;
261 static inline int get_icase_global(void)
263 static int icase = -1;
265 if (icase < 0)
266 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
268 return icase;
271 static int get_global_magic(int element_magic)
273 int global_magic = 0;
275 if (get_literal_global())
276 global_magic |= PATHSPEC_LITERAL;
278 /* --glob-pathspec is overridden by :(literal) */
279 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
280 global_magic |= PATHSPEC_GLOB;
282 if (get_glob_global() && get_noglob_global())
283 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
285 if (get_icase_global())
286 global_magic |= PATHSPEC_ICASE;
288 if ((global_magic & PATHSPEC_LITERAL) &&
289 (global_magic & ~PATHSPEC_LITERAL))
290 die(_("global 'literal' pathspec setting is incompatible "
291 "with all other global pathspec settings"));
293 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
294 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
295 global_magic |= PATHSPEC_LITERAL;
297 return global_magic;
301 * Parse the pathspec element looking for long magic
303 * saves all magic in 'magic'
304 * if prefix magic is used, save the prefix length in 'prefix_len'
305 * returns the position in 'elem' after all magic has been parsed
307 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
308 struct pathspec_item *item,
309 const char *elem)
311 const char *pos;
312 const char *nextat;
314 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
315 size_t len = strcspn_escaped(pos, ",)");
316 int i;
318 if (pos[len] == ',')
319 nextat = pos + len + 1; /* handle ',' */
320 else
321 nextat = pos + len; /* handle ')' and '\0' */
323 if (!len)
324 continue;
326 if (starts_with(pos, "prefix:")) {
327 char *endptr;
328 *prefix_len = strtol(pos + 7, &endptr, 10);
329 if (endptr - pos != len)
330 die(_("invalid parameter for pathspec magic 'prefix'"));
331 continue;
334 if (starts_with(pos, "attr:")) {
335 char *attr_body = xmemdupz(pos + 5, len - 5);
336 parse_pathspec_attr_match(item, attr_body);
337 *magic |= PATHSPEC_ATTR;
338 free(attr_body);
339 continue;
342 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
343 if (strlen(pathspec_magic[i].name) == len &&
344 !strncmp(pathspec_magic[i].name, pos, len)) {
345 *magic |= pathspec_magic[i].bit;
346 break;
350 if (ARRAY_SIZE(pathspec_magic) <= i)
351 die(_("Invalid pathspec magic '%.*s' in '%s'"),
352 (int) len, pos, elem);
355 if (*pos != ')')
356 die(_("Missing ')' at the end of pathspec magic in '%s'"),
357 elem);
358 pos++;
360 return pos;
364 * Parse the pathspec element looking for short magic
366 * saves all magic in 'magic'
367 * returns the position in 'elem' after all magic has been parsed
369 static const char *parse_short_magic(unsigned *magic, const char *elem)
371 const char *pos;
373 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
374 char ch = *pos;
375 int i;
377 /* Special case alias for '!' */
378 if (ch == '^') {
379 *magic |= PATHSPEC_EXCLUDE;
380 continue;
383 if (!is_pathspec_magic(ch))
384 break;
386 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
387 if (pathspec_magic[i].mnemonic == ch) {
388 *magic |= pathspec_magic[i].bit;
389 break;
393 if (ARRAY_SIZE(pathspec_magic) <= i)
394 die(_("Unimplemented pathspec magic '%c' in '%s'"),
395 ch, elem);
398 if (*pos == ':')
399 pos++;
401 return pos;
404 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
405 struct pathspec_item *item,
406 const char *elem)
408 if (elem[0] != ':' || get_literal_global())
409 return elem; /* nothing to do */
410 else if (elem[1] == '(')
411 /* longhand */
412 return parse_long_magic(magic, prefix_len, item, elem);
413 else
414 /* shorthand */
415 return parse_short_magic(magic, elem);
419 * Perform the initialization of a pathspec_item based on a pathspec element.
421 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
422 const char *prefix, int prefixlen,
423 const char *elt)
425 unsigned magic = 0, element_magic = 0;
426 const char *copyfrom = elt;
427 char *match;
428 int pathspec_prefix = -1;
430 item->attr_check = NULL;
431 item->attr_match = NULL;
432 item->attr_match_nr = 0;
434 /* PATHSPEC_LITERAL_PATH ignores magic */
435 if (flags & PATHSPEC_LITERAL_PATH) {
436 magic = PATHSPEC_LITERAL;
437 } else {
438 copyfrom = parse_element_magic(&element_magic,
439 &pathspec_prefix,
440 item,
441 elt);
442 magic |= element_magic;
443 magic |= get_global_magic(element_magic);
446 item->magic = magic;
448 if (pathspec_prefix >= 0 &&
449 (prefixlen || (prefix && *prefix)))
450 BUG("'prefix' magic is supposed to be used at worktree's root");
452 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
453 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
455 /* Create match string which will be used for pathspec matching */
456 if (pathspec_prefix >= 0) {
457 match = xstrdup(copyfrom);
458 prefixlen = pathspec_prefix;
459 } else if (magic & PATHSPEC_FROMTOP) {
460 match = xstrdup(copyfrom);
461 prefixlen = 0;
462 } else {
463 match = prefix_path_gently(prefix, prefixlen,
464 &prefixlen, copyfrom);
465 if (!match) {
466 const char *hint_path = get_git_work_tree();
467 if (!hint_path)
468 hint_path = get_git_dir();
469 die(_("%s: '%s' is outside repository at '%s'"), elt,
470 copyfrom, absolute_path(hint_path));
474 item->match = match;
475 item->len = strlen(item->match);
476 item->prefix = prefixlen;
479 * Prefix the pathspec (keep all magic) and assign to
480 * original. Useful for passing to another command.
482 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
483 !get_literal_global()) {
484 struct strbuf sb = STRBUF_INIT;
486 /* Preserve the actual prefix length of each pattern */
487 prefix_magic(&sb, prefixlen, element_magic);
489 strbuf_addstr(&sb, match);
490 item->original = strbuf_detach(&sb, NULL);
491 } else {
492 item->original = xstrdup(elt);
495 if (magic & PATHSPEC_LITERAL) {
496 item->nowildcard_len = item->len;
497 } else {
498 item->nowildcard_len = simple_length(item->match);
499 if (item->nowildcard_len < prefixlen)
500 item->nowildcard_len = prefixlen;
503 item->flags = 0;
504 if (magic & PATHSPEC_GLOB) {
506 * FIXME: should we enable ONESTAR in _GLOB for
507 * pattern "* * / * . c"?
509 } else {
510 if (item->nowildcard_len < item->len &&
511 item->match[item->nowildcard_len] == '*' &&
512 no_wildcard(item->match + item->nowildcard_len + 1))
513 item->flags |= PATHSPEC_ONESTAR;
516 /* sanity checks, pathspec matchers assume these are sane */
517 if (item->nowildcard_len > item->len ||
518 item->prefix > item->len) {
519 BUG("error initializing pathspec_item");
523 static int pathspec_item_cmp(const void *a_, const void *b_)
525 struct pathspec_item *a, *b;
527 a = (struct pathspec_item *)a_;
528 b = (struct pathspec_item *)b_;
529 return strcmp(a->match, b->match);
532 static void NORETURN unsupported_magic(const char *pattern,
533 unsigned magic)
535 struct strbuf sb = STRBUF_INIT;
536 int i;
537 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
538 const struct pathspec_magic *m = pathspec_magic + i;
539 if (!(magic & m->bit))
540 continue;
541 if (sb.len)
542 strbuf_addstr(&sb, ", ");
544 if (m->mnemonic)
545 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
546 m->name, m->mnemonic);
547 else
548 strbuf_addf(&sb, "'%s'", m->name);
551 * We may want to substitute "this command" with a command
552 * name. E.g. when "git add -p" or "git add -i" dies when running
553 * "checkout -p"
555 die(_("%s: pathspec magic not supported by this command: %s"),
556 pattern, sb.buf);
559 void parse_pathspec(struct pathspec *pathspec,
560 unsigned magic_mask, unsigned flags,
561 const char *prefix, const char **argv)
563 struct pathspec_item *item;
564 const char *entry = argv ? *argv : NULL;
565 int i, n, prefixlen, nr_exclude = 0;
567 memset(pathspec, 0, sizeof(*pathspec));
569 if (flags & PATHSPEC_MAXDEPTH_VALID)
570 pathspec->magic |= PATHSPEC_MAXDEPTH;
572 /* No arguments, no prefix -> no pathspec */
573 if (!entry && !prefix)
574 return;
576 if ((flags & PATHSPEC_PREFER_CWD) &&
577 (flags & PATHSPEC_PREFER_FULL))
578 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
580 /* No arguments with prefix -> prefix pathspec */
581 if (!entry) {
582 if (flags & PATHSPEC_PREFER_FULL)
583 return;
585 if (!(flags & PATHSPEC_PREFER_CWD))
586 BUG("PATHSPEC_PREFER_CWD requires arguments");
588 pathspec->items = CALLOC_ARRAY(item, 1);
589 item->match = xstrdup(prefix);
590 item->original = xstrdup(prefix);
591 item->nowildcard_len = item->len = strlen(prefix);
592 item->prefix = item->len;
593 pathspec->nr = 1;
594 return;
597 n = 0;
598 while (argv[n]) {
599 if (*argv[n] == '\0')
600 die("empty string is not a valid pathspec. "
601 "please use . instead if you meant to match all paths");
602 n++;
605 pathspec->nr = n;
606 ALLOC_ARRAY(pathspec->items, n + 1);
607 item = pathspec->items;
608 prefixlen = prefix ? strlen(prefix) : 0;
610 for (i = 0; i < n; i++) {
611 entry = argv[i];
613 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
615 if (item[i].magic & PATHSPEC_EXCLUDE)
616 nr_exclude++;
617 if (item[i].magic & magic_mask)
618 unsupported_magic(entry, item[i].magic & magic_mask);
620 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
621 has_symlink_leading_path(item[i].match, item[i].len)) {
622 die(_("pathspec '%s' is beyond a symbolic link"), entry);
625 if (item[i].nowildcard_len < item[i].len)
626 pathspec->has_wildcard = 1;
627 pathspec->magic |= item[i].magic;
631 * If everything is an exclude pattern, add one positive pattern
632 * that matches everything. We allocated an extra one for this.
634 if (nr_exclude == n) {
635 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
636 init_pathspec_item(item + n, 0, prefix, plen, ".");
637 pathspec->nr++;
640 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
641 if (flags & PATHSPEC_KEEP_ORDER)
642 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
643 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
647 void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
648 unsigned flags, const char *prefix,
649 const char *file, int nul_term_line)
651 struct strvec parsed_file = STRVEC_INIT;
652 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
653 strbuf_getline;
654 struct strbuf buf = STRBUF_INIT;
655 struct strbuf unquoted = STRBUF_INIT;
656 FILE *in;
658 if (!strcmp(file, "-"))
659 in = stdin;
660 else
661 in = xfopen(file, "r");
663 while (getline_fn(&buf, in) != EOF) {
664 if (!nul_term_line && buf.buf[0] == '"') {
665 strbuf_reset(&unquoted);
666 if (unquote_c_style(&unquoted, buf.buf, NULL))
667 die(_("line is badly quoted: %s"), buf.buf);
668 strbuf_swap(&buf, &unquoted);
670 strvec_push(&parsed_file, buf.buf);
671 strbuf_reset(&buf);
674 strbuf_release(&unquoted);
675 strbuf_release(&buf);
676 if (in != stdin)
677 fclose(in);
679 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
680 strvec_clear(&parsed_file);
683 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
685 int i, j;
687 *dst = *src;
688 DUP_ARRAY(dst->items, src->items, dst->nr);
690 for (i = 0; i < dst->nr; i++) {
691 struct pathspec_item *d = &dst->items[i];
692 struct pathspec_item *s = &src->items[i];
694 d->match = xstrdup(s->match);
695 d->original = xstrdup(s->original);
697 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
698 for (j = 0; j < d->attr_match_nr; j++) {
699 const char *value = s->attr_match[j].value;
700 d->attr_match[j].value = xstrdup_or_null(value);
703 d->attr_check = attr_check_dup(s->attr_check);
707 void clear_pathspec(struct pathspec *pathspec)
709 int i, j;
711 for (i = 0; i < pathspec->nr; i++) {
712 free(pathspec->items[i].match);
713 free(pathspec->items[i].original);
715 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
716 free(pathspec->items[i].attr_match[j].value);
717 free(pathspec->items[i].attr_match);
719 if (pathspec->items[i].attr_check)
720 attr_check_free(pathspec->items[i].attr_check);
723 FREE_AND_NULL(pathspec->items);
724 pathspec->nr = 0;
727 int match_pathspec_attrs(struct index_state *istate,
728 const char *name, int namelen,
729 const struct pathspec_item *item)
731 int i;
732 char *to_free = NULL;
734 if (name[namelen])
735 name = to_free = xmemdupz(name, namelen);
737 git_check_attr(istate, NULL, name, item->attr_check);
739 free(to_free);
741 for (i = 0; i < item->attr_match_nr; i++) {
742 const char *value;
743 int matched;
744 enum attr_match_mode match_mode;
746 value = item->attr_check->items[i].value;
747 match_mode = item->attr_match[i].match_mode;
749 if (ATTR_TRUE(value))
750 matched = (match_mode == MATCH_SET);
751 else if (ATTR_FALSE(value))
752 matched = (match_mode == MATCH_UNSET);
753 else if (ATTR_UNSET(value))
754 matched = (match_mode == MATCH_UNSPECIFIED);
755 else
756 matched = (match_mode == MATCH_VALUE &&
757 !strcmp(item->attr_match[i].value, value));
758 if (!matched)
759 return 0;
762 return 1;
765 int pathspec_needs_expanded_index(struct index_state *istate,
766 const struct pathspec *pathspec)
768 unsigned int i, pos;
769 int res = 0;
770 char *skip_worktree_seen = NULL;
773 * If index is not sparse, no index expansion is needed.
775 if (!istate->sparse_index)
776 return 0;
779 * When using a magic pathspec, assume for the sake of simplicity that
780 * the index needs to be expanded to match all matchable files.
782 if (pathspec->magic)
783 return 1;
785 for (i = 0; i < pathspec->nr; i++) {
786 struct pathspec_item item = pathspec->items[i];
789 * If the pathspec item has a wildcard, the index should be expanded
790 * if the pathspec has the possibility of matching a subset of entries inside
791 * of a sparse directory (but not the entire directory).
793 * If the pathspec item is a literal path, the index only needs to be expanded
794 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
795 * expand for in-cone files) and b) it doesn't match any sparse directories
796 * (since we can reset whole sparse directories without expanding them).
798 if (item.nowildcard_len < item.len) {
800 * Special case: if the pattern is a path inside the cone
801 * followed by only wildcards, the pattern cannot match
802 * partial sparse directories, so we know we don't need to
803 * expand the index.
805 * Examples:
806 * - in-cone/foo***: doesn't need expanded index
807 * - not-in-cone/bar*: may need expanded index
808 * - **.c: may need expanded index
810 if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
811 path_in_cone_mode_sparse_checkout(item.original, istate))
812 continue;
814 for (pos = 0; pos < istate->cache_nr; pos++) {
815 struct cache_entry *ce = istate->cache[pos];
817 if (!S_ISSPARSEDIR(ce->ce_mode))
818 continue;
821 * If the pre-wildcard length is longer than the sparse
822 * directory name and the sparse directory is the first
823 * component of the pathspec, need to expand the index.
825 if (item.nowildcard_len > ce_namelen(ce) &&
826 !strncmp(item.original, ce->name, ce_namelen(ce))) {
827 res = 1;
828 break;
832 * If the pre-wildcard length is shorter than the sparse
833 * directory and the pathspec does not match the whole
834 * directory, need to expand the index.
836 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
837 wildmatch(item.original, ce->name, 0)) {
838 res = 1;
839 break;
842 } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
843 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
844 res = 1;
846 if (res > 0)
847 break;
850 free(skip_worktree_seen);
851 return res;