ws.h: move declarations for ws.c functions from cache.h
[alt-git.git] / pathspec.c
blobec335a214e275ff1787fc33e72735d0ba005c2ac
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 "symlinks.h"
12 #include "quote.h"
15 * Finds which of the given pathspecs match items in the index.
17 * For each pathspec, sets the corresponding entry in the seen[] array
18 * (which should be specs items long, i.e. the same size as pathspec)
19 * to the nature of the "closest" (i.e. most specific) match found for
20 * that pathspec in the index, if it was a closer type of match than
21 * the existing entry. As an optimization, matching is skipped
22 * altogether if seen[] already only contains non-zero entries.
24 * If seen[] has not already been written to, it may make sense
25 * to use find_pathspecs_matching_against_index() instead.
27 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
28 struct index_state *istate,
29 char *seen,
30 enum ps_skip_worktree_action sw_action)
32 int num_unmatched = 0, i;
35 * Since we are walking the index as if we were walking the directory,
36 * we have to mark the matched pathspec as seen; otherwise we will
37 * mistakenly think that the user gave a pathspec that did not match
38 * anything.
40 for (i = 0; i < pathspec->nr; i++)
41 if (!seen[i])
42 num_unmatched++;
43 if (!num_unmatched)
44 return;
45 for (i = 0; i < istate->cache_nr; i++) {
46 const struct cache_entry *ce = istate->cache[i];
47 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
48 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
49 continue;
50 ce_path_match(istate, ce, pathspec, seen);
55 * Finds which of the given pathspecs match items in the index.
57 * This is a one-shot wrapper around add_pathspec_matches_against_index()
58 * which allocates, populates, and returns a seen[] array indicating the
59 * nature of the "closest" (i.e. most specific) matches which each of the
60 * given pathspecs achieves against all items in the index.
62 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
63 struct index_state *istate,
64 enum ps_skip_worktree_action sw_action)
66 char *seen = xcalloc(pathspec->nr, 1);
67 add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
68 return seen;
71 char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
73 struct index_state *istate = the_repository->index;
74 char *seen = xcalloc(pathspec->nr, 1);
75 int i;
77 for (i = 0; i < istate->cache_nr; i++) {
78 struct cache_entry *ce = istate->cache[i];
79 if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
80 ce_path_match(istate, ce, pathspec, seen);
83 return seen;
87 * Magic pathspec
89 * Possible future magic semantics include stuff like:
91 * { PATHSPEC_RECURSIVE, '*', "recursive" },
92 * { PATHSPEC_REGEXP, '\0', "regexp" },
96 static struct pathspec_magic {
97 unsigned bit;
98 char mnemonic; /* this cannot be ':'! */
99 const char *name;
100 } pathspec_magic[] = {
101 { PATHSPEC_FROMTOP, '/', "top" },
102 { PATHSPEC_LITERAL, '\0', "literal" },
103 { PATHSPEC_GLOB, '\0', "glob" },
104 { PATHSPEC_ICASE, '\0', "icase" },
105 { PATHSPEC_EXCLUDE, '!', "exclude" },
106 { PATHSPEC_ATTR, '\0', "attr" },
109 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
111 int i;
112 strbuf_addstr(sb, ":(");
113 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
114 if (magic & pathspec_magic[i].bit) {
115 if (sb->buf[sb->len - 1] != '(')
116 strbuf_addch(sb, ',');
117 strbuf_addstr(sb, pathspec_magic[i].name);
119 strbuf_addf(sb, ",prefix:%d)", prefixlen);
122 static size_t strcspn_escaped(const char *s, const char *stop)
124 const char *i;
126 for (i = s; *i; i++) {
127 /* skip the escaped character */
128 if (i[0] == '\\' && i[1]) {
129 i++;
130 continue;
133 if (strchr(stop, *i))
134 break;
136 return i - s;
139 static inline int invalid_value_char(const char ch)
141 if (isalnum(ch) || strchr(",-_", ch))
142 return 0;
143 return -1;
146 static char *attr_value_unescape(const char *value)
148 const char *src;
149 char *dst, *ret;
151 ret = xmallocz(strlen(value));
152 for (src = value, dst = ret; *src; src++, dst++) {
153 if (*src == '\\') {
154 if (!src[1])
155 die(_("Escape character '\\' not allowed as "
156 "last character in attr value"));
157 src++;
159 if (invalid_value_char(*src))
160 die("cannot use '%c' for value matching", *src);
161 *dst = *src;
163 *dst = '\0';
164 return ret;
167 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
169 struct string_list_item *si;
170 struct string_list list = STRING_LIST_INIT_DUP;
172 if (item->attr_check || item->attr_match)
173 die(_("Only one 'attr:' specification is allowed."));
175 if (!value || !*value)
176 die(_("attr spec must not be empty"));
178 string_list_split(&list, value, ' ', -1);
179 string_list_remove_empty_items(&list, 0);
181 item->attr_check = attr_check_alloc();
182 CALLOC_ARRAY(item->attr_match, list.nr);
184 for_each_string_list_item(si, &list) {
185 size_t attr_len;
186 char *attr_name;
187 const struct git_attr *a;
189 int j = item->attr_match_nr++;
190 const char *attr = si->string;
191 struct attr_match *am = &item->attr_match[j];
193 switch (*attr) {
194 case '!':
195 am->match_mode = MATCH_UNSPECIFIED;
196 attr++;
197 attr_len = strlen(attr);
198 break;
199 case '-':
200 am->match_mode = MATCH_UNSET;
201 attr++;
202 attr_len = strlen(attr);
203 break;
204 default:
205 attr_len = strcspn(attr, "=");
206 if (attr[attr_len] != '=')
207 am->match_mode = MATCH_SET;
208 else {
209 const char *v = &attr[attr_len + 1];
210 am->match_mode = MATCH_VALUE;
211 am->value = attr_value_unescape(v);
213 break;
216 attr_name = xmemdupz(attr, attr_len);
217 a = git_attr(attr_name);
218 if (!a)
219 die(_("invalid attribute name %s"), attr_name);
221 attr_check_append(item->attr_check, a);
223 free(attr_name);
226 if (item->attr_check->nr != item->attr_match_nr)
227 BUG("should have same number of entries");
229 string_list_clear(&list, 0);
232 static inline int get_literal_global(void)
234 static int literal = -1;
236 if (literal < 0)
237 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
239 return literal;
242 static inline int get_glob_global(void)
244 static int glob = -1;
246 if (glob < 0)
247 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
249 return glob;
252 static inline int get_noglob_global(void)
254 static int noglob = -1;
256 if (noglob < 0)
257 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
259 return noglob;
262 static inline int get_icase_global(void)
264 static int icase = -1;
266 if (icase < 0)
267 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
269 return icase;
272 static int get_global_magic(int element_magic)
274 int global_magic = 0;
276 if (get_literal_global())
277 global_magic |= PATHSPEC_LITERAL;
279 /* --glob-pathspec is overridden by :(literal) */
280 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
281 global_magic |= PATHSPEC_GLOB;
283 if (get_glob_global() && get_noglob_global())
284 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
286 if (get_icase_global())
287 global_magic |= PATHSPEC_ICASE;
289 if ((global_magic & PATHSPEC_LITERAL) &&
290 (global_magic & ~PATHSPEC_LITERAL))
291 die(_("global 'literal' pathspec setting is incompatible "
292 "with all other global pathspec settings"));
294 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
295 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
296 global_magic |= PATHSPEC_LITERAL;
298 return global_magic;
302 * Parse the pathspec element looking for long magic
304 * saves all magic in 'magic'
305 * if prefix magic is used, save the prefix length in 'prefix_len'
306 * returns the position in 'elem' after all magic has been parsed
308 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
309 struct pathspec_item *item,
310 const char *elem)
312 const char *pos;
313 const char *nextat;
315 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
316 size_t len = strcspn_escaped(pos, ",)");
317 int i;
319 if (pos[len] == ',')
320 nextat = pos + len + 1; /* handle ',' */
321 else
322 nextat = pos + len; /* handle ')' and '\0' */
324 if (!len)
325 continue;
327 if (starts_with(pos, "prefix:")) {
328 char *endptr;
329 *prefix_len = strtol(pos + 7, &endptr, 10);
330 if (endptr - pos != len)
331 die(_("invalid parameter for pathspec magic 'prefix'"));
332 continue;
335 if (starts_with(pos, "attr:")) {
336 char *attr_body = xmemdupz(pos + 5, len - 5);
337 parse_pathspec_attr_match(item, attr_body);
338 *magic |= PATHSPEC_ATTR;
339 free(attr_body);
340 continue;
343 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
344 if (strlen(pathspec_magic[i].name) == len &&
345 !strncmp(pathspec_magic[i].name, pos, len)) {
346 *magic |= pathspec_magic[i].bit;
347 break;
351 if (ARRAY_SIZE(pathspec_magic) <= i)
352 die(_("Invalid pathspec magic '%.*s' in '%s'"),
353 (int) len, pos, elem);
356 if (*pos != ')')
357 die(_("Missing ')' at the end of pathspec magic in '%s'"),
358 elem);
359 pos++;
361 return pos;
365 * Parse the pathspec element looking for short magic
367 * saves all magic in 'magic'
368 * returns the position in 'elem' after all magic has been parsed
370 static const char *parse_short_magic(unsigned *magic, const char *elem)
372 const char *pos;
374 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
375 char ch = *pos;
376 int i;
378 /* Special case alias for '!' */
379 if (ch == '^') {
380 *magic |= PATHSPEC_EXCLUDE;
381 continue;
384 if (!is_pathspec_magic(ch))
385 break;
387 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
388 if (pathspec_magic[i].mnemonic == ch) {
389 *magic |= pathspec_magic[i].bit;
390 break;
394 if (ARRAY_SIZE(pathspec_magic) <= i)
395 die(_("Unimplemented pathspec magic '%c' in '%s'"),
396 ch, elem);
399 if (*pos == ':')
400 pos++;
402 return pos;
405 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
406 struct pathspec_item *item,
407 const char *elem)
409 if (elem[0] != ':' || get_literal_global())
410 return elem; /* nothing to do */
411 else if (elem[1] == '(')
412 /* longhand */
413 return parse_long_magic(magic, prefix_len, item, elem);
414 else
415 /* shorthand */
416 return parse_short_magic(magic, elem);
420 * Perform the initialization of a pathspec_item based on a pathspec element.
422 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
423 const char *prefix, int prefixlen,
424 const char *elt)
426 unsigned magic = 0, element_magic = 0;
427 const char *copyfrom = elt;
428 char *match;
429 int pathspec_prefix = -1;
431 item->attr_check = NULL;
432 item->attr_match = NULL;
433 item->attr_match_nr = 0;
435 /* PATHSPEC_LITERAL_PATH ignores magic */
436 if (flags & PATHSPEC_LITERAL_PATH) {
437 magic = PATHSPEC_LITERAL;
438 } else {
439 copyfrom = parse_element_magic(&element_magic,
440 &pathspec_prefix,
441 item,
442 elt);
443 magic |= element_magic;
444 magic |= get_global_magic(element_magic);
447 item->magic = magic;
449 if (pathspec_prefix >= 0 &&
450 (prefixlen || (prefix && *prefix)))
451 BUG("'prefix' magic is supposed to be used at worktree's root");
453 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
454 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
456 /* Create match string which will be used for pathspec matching */
457 if (pathspec_prefix >= 0) {
458 match = xstrdup(copyfrom);
459 prefixlen = pathspec_prefix;
460 } else if (magic & PATHSPEC_FROMTOP) {
461 match = xstrdup(copyfrom);
462 prefixlen = 0;
463 } else {
464 match = prefix_path_gently(prefix, prefixlen,
465 &prefixlen, copyfrom);
466 if (!match) {
467 const char *hint_path = get_git_work_tree();
468 if (!hint_path)
469 hint_path = get_git_dir();
470 die(_("%s: '%s' is outside repository at '%s'"), elt,
471 copyfrom, absolute_path(hint_path));
475 item->match = match;
476 item->len = strlen(item->match);
477 item->prefix = prefixlen;
480 * Prefix the pathspec (keep all magic) and assign to
481 * original. Useful for passing to another command.
483 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
484 !get_literal_global()) {
485 struct strbuf sb = STRBUF_INIT;
487 /* Preserve the actual prefix length of each pattern */
488 prefix_magic(&sb, prefixlen, element_magic);
490 strbuf_addstr(&sb, match);
491 item->original = strbuf_detach(&sb, NULL);
492 } else {
493 item->original = xstrdup(elt);
496 if (magic & PATHSPEC_LITERAL) {
497 item->nowildcard_len = item->len;
498 } else {
499 item->nowildcard_len = simple_length(item->match);
500 if (item->nowildcard_len < prefixlen)
501 item->nowildcard_len = prefixlen;
504 item->flags = 0;
505 if (magic & PATHSPEC_GLOB) {
507 * FIXME: should we enable ONESTAR in _GLOB for
508 * pattern "* * / * . c"?
510 } else {
511 if (item->nowildcard_len < item->len &&
512 item->match[item->nowildcard_len] == '*' &&
513 no_wildcard(item->match + item->nowildcard_len + 1))
514 item->flags |= PATHSPEC_ONESTAR;
517 /* sanity checks, pathspec matchers assume these are sane */
518 if (item->nowildcard_len > item->len ||
519 item->prefix > item->len) {
520 BUG("error initializing pathspec_item");
524 static int pathspec_item_cmp(const void *a_, const void *b_)
526 struct pathspec_item *a, *b;
528 a = (struct pathspec_item *)a_;
529 b = (struct pathspec_item *)b_;
530 return strcmp(a->match, b->match);
533 static void NORETURN unsupported_magic(const char *pattern,
534 unsigned magic)
536 struct strbuf sb = STRBUF_INIT;
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 (sb.len)
543 strbuf_addstr(&sb, ", ");
545 if (m->mnemonic)
546 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
547 m->name, m->mnemonic);
548 else
549 strbuf_addf(&sb, "'%s'", m->name);
552 * We may want to substitute "this command" with a command
553 * name. E.g. when "git add -p" or "git add -i" dies when running
554 * "checkout -p"
556 die(_("%s: pathspec magic not supported by this command: %s"),
557 pattern, sb.buf);
560 void parse_pathspec(struct pathspec *pathspec,
561 unsigned magic_mask, unsigned flags,
562 const char *prefix, const char **argv)
564 struct pathspec_item *item;
565 const char *entry = argv ? *argv : NULL;
566 int i, n, prefixlen, nr_exclude = 0;
568 memset(pathspec, 0, sizeof(*pathspec));
570 if (flags & PATHSPEC_MAXDEPTH_VALID)
571 pathspec->magic |= PATHSPEC_MAXDEPTH;
573 /* No arguments, no prefix -> no pathspec */
574 if (!entry && !prefix)
575 return;
577 if ((flags & PATHSPEC_PREFER_CWD) &&
578 (flags & PATHSPEC_PREFER_FULL))
579 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
581 /* No arguments with prefix -> prefix pathspec */
582 if (!entry) {
583 if (flags & PATHSPEC_PREFER_FULL)
584 return;
586 if (!(flags & PATHSPEC_PREFER_CWD))
587 BUG("PATHSPEC_PREFER_CWD requires arguments");
589 pathspec->items = CALLOC_ARRAY(item, 1);
590 item->match = xstrdup(prefix);
591 item->original = xstrdup(prefix);
592 item->nowildcard_len = item->len = strlen(prefix);
593 item->prefix = item->len;
594 pathspec->nr = 1;
595 return;
598 n = 0;
599 while (argv[n]) {
600 if (*argv[n] == '\0')
601 die("empty string is not a valid pathspec. "
602 "please use . instead if you meant to match all paths");
603 n++;
606 pathspec->nr = n;
607 ALLOC_ARRAY(pathspec->items, n + 1);
608 item = pathspec->items;
609 prefixlen = prefix ? strlen(prefix) : 0;
611 for (i = 0; i < n; i++) {
612 entry = argv[i];
614 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
616 if (item[i].magic & PATHSPEC_EXCLUDE)
617 nr_exclude++;
618 if (item[i].magic & magic_mask)
619 unsupported_magic(entry, item[i].magic & magic_mask);
621 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
622 has_symlink_leading_path(item[i].match, item[i].len)) {
623 die(_("pathspec '%s' is beyond a symbolic link"), entry);
626 if (item[i].nowildcard_len < item[i].len)
627 pathspec->has_wildcard = 1;
628 pathspec->magic |= item[i].magic;
632 * If everything is an exclude pattern, add one positive pattern
633 * that matches everything. We allocated an extra one for this.
635 if (nr_exclude == n) {
636 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
637 init_pathspec_item(item + n, 0, prefix, plen, ".");
638 pathspec->nr++;
641 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
642 if (flags & PATHSPEC_KEEP_ORDER)
643 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
644 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
648 void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
649 unsigned flags, const char *prefix,
650 const char *file, int nul_term_line)
652 struct strvec parsed_file = STRVEC_INIT;
653 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
654 strbuf_getline;
655 struct strbuf buf = STRBUF_INIT;
656 struct strbuf unquoted = STRBUF_INIT;
657 FILE *in;
659 if (!strcmp(file, "-"))
660 in = stdin;
661 else
662 in = xfopen(file, "r");
664 while (getline_fn(&buf, in) != EOF) {
665 if (!nul_term_line && buf.buf[0] == '"') {
666 strbuf_reset(&unquoted);
667 if (unquote_c_style(&unquoted, buf.buf, NULL))
668 die(_("line is badly quoted: %s"), buf.buf);
669 strbuf_swap(&buf, &unquoted);
671 strvec_push(&parsed_file, buf.buf);
672 strbuf_reset(&buf);
675 strbuf_release(&unquoted);
676 strbuf_release(&buf);
677 if (in != stdin)
678 fclose(in);
680 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
681 strvec_clear(&parsed_file);
684 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
686 int i, j;
688 *dst = *src;
689 DUP_ARRAY(dst->items, src->items, dst->nr);
691 for (i = 0; i < dst->nr; i++) {
692 struct pathspec_item *d = &dst->items[i];
693 struct pathspec_item *s = &src->items[i];
695 d->match = xstrdup(s->match);
696 d->original = xstrdup(s->original);
698 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
699 for (j = 0; j < d->attr_match_nr; j++) {
700 const char *value = s->attr_match[j].value;
701 d->attr_match[j].value = xstrdup_or_null(value);
704 d->attr_check = attr_check_dup(s->attr_check);
708 void clear_pathspec(struct pathspec *pathspec)
710 int i, j;
712 for (i = 0; i < pathspec->nr; i++) {
713 free(pathspec->items[i].match);
714 free(pathspec->items[i].original);
716 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
717 free(pathspec->items[i].attr_match[j].value);
718 free(pathspec->items[i].attr_match);
720 if (pathspec->items[i].attr_check)
721 attr_check_free(pathspec->items[i].attr_check);
724 FREE_AND_NULL(pathspec->items);
725 pathspec->nr = 0;
728 int match_pathspec_attrs(struct index_state *istate,
729 const char *name, int namelen,
730 const struct pathspec_item *item)
732 int i;
733 char *to_free = NULL;
735 if (name[namelen])
736 name = to_free = xmemdupz(name, namelen);
738 git_check_attr(istate, NULL, name, item->attr_check);
740 free(to_free);
742 for (i = 0; i < item->attr_match_nr; i++) {
743 const char *value;
744 int matched;
745 enum attr_match_mode match_mode;
747 value = item->attr_check->items[i].value;
748 match_mode = item->attr_match[i].match_mode;
750 if (ATTR_TRUE(value))
751 matched = (match_mode == MATCH_SET);
752 else if (ATTR_FALSE(value))
753 matched = (match_mode == MATCH_UNSET);
754 else if (ATTR_UNSET(value))
755 matched = (match_mode == MATCH_UNSPECIFIED);
756 else
757 matched = (match_mode == MATCH_VALUE &&
758 !strcmp(item->attr_match[i].value, value));
759 if (!matched)
760 return 0;
763 return 1;
766 int pathspec_needs_expanded_index(struct index_state *istate,
767 const struct pathspec *pathspec)
769 unsigned int i, pos;
770 int res = 0;
771 char *skip_worktree_seen = NULL;
774 * If index is not sparse, no index expansion is needed.
776 if (!istate->sparse_index)
777 return 0;
780 * When using a magic pathspec, assume for the sake of simplicity that
781 * the index needs to be expanded to match all matchable files.
783 if (pathspec->magic)
784 return 1;
786 for (i = 0; i < pathspec->nr; i++) {
787 struct pathspec_item item = pathspec->items[i];
790 * If the pathspec item has a wildcard, the index should be expanded
791 * if the pathspec has the possibility of matching a subset of entries inside
792 * of a sparse directory (but not the entire directory).
794 * If the pathspec item is a literal path, the index only needs to be expanded
795 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
796 * expand for in-cone files) and b) it doesn't match any sparse directories
797 * (since we can reset whole sparse directories without expanding them).
799 if (item.nowildcard_len < item.len) {
801 * Special case: if the pattern is a path inside the cone
802 * followed by only wildcards, the pattern cannot match
803 * partial sparse directories, so we know we don't need to
804 * expand the index.
806 * Examples:
807 * - in-cone/foo***: doesn't need expanded index
808 * - not-in-cone/bar*: may need expanded index
809 * - **.c: may need expanded index
811 if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
812 path_in_cone_mode_sparse_checkout(item.original, istate))
813 continue;
815 for (pos = 0; pos < istate->cache_nr; pos++) {
816 struct cache_entry *ce = istate->cache[pos];
818 if (!S_ISSPARSEDIR(ce->ce_mode))
819 continue;
822 * If the pre-wildcard length is longer than the sparse
823 * directory name and the sparse directory is the first
824 * component of the pathspec, need to expand the index.
826 if (item.nowildcard_len > ce_namelen(ce) &&
827 !strncmp(item.original, ce->name, ce_namelen(ce))) {
828 res = 1;
829 break;
833 * If the pre-wildcard length is shorter than the sparse
834 * directory and the pathspec does not match the whole
835 * directory, need to expand the index.
837 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
838 wildmatch(item.original, ce->name, 0)) {
839 res = 1;
840 break;
843 } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
844 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
845 res = 1;
847 if (res > 0)
848 break;
851 free(skip_worktree_seen);
852 return res;