The fifth batch
[alt-git.git] / pathspec.c
blob89663645e13dd49499b7f6cf8b1fdf663145981d
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "abspath.h"
6 #include "parse.h"
7 #include "dir.h"
8 #include "environment.h"
9 #include "gettext.h"
10 #include "pathspec.h"
11 #include "attr.h"
12 #include "read-cache.h"
13 #include "repository.h"
14 #include "setup.h"
15 #include "strvec.h"
16 #include "symlinks.h"
17 #include "quote.h"
18 #include "wildmatch.h"
21 * Finds which of the given pathspecs match items in the index.
23 * For each pathspec, sets the corresponding entry in the seen[] array
24 * (which should be specs items long, i.e. the same size as pathspec)
25 * to the nature of the "closest" (i.e. most specific) match found for
26 * that pathspec in the index, if it was a closer type of match than
27 * the existing entry. As an optimization, matching is skipped
28 * altogether if seen[] already only contains non-zero entries.
30 * If seen[] has not already been written to, it may make sense
31 * to use find_pathspecs_matching_against_index() instead.
33 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
34 struct index_state *istate,
35 char *seen,
36 enum ps_skip_worktree_action sw_action)
38 int num_unmatched = 0, i;
41 * Since we are walking the index as if we were walking the directory,
42 * we have to mark the matched pathspec as seen; otherwise we will
43 * mistakenly think that the user gave a pathspec that did not match
44 * anything.
46 for (i = 0; i < pathspec->nr; i++)
47 if (!seen[i])
48 num_unmatched++;
49 if (!num_unmatched)
50 return;
51 for (i = 0; i < istate->cache_nr; i++) {
52 const struct cache_entry *ce = istate->cache[i];
53 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
54 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
55 continue;
56 ce_path_match(istate, ce, pathspec, seen);
61 * Finds which of the given pathspecs match items in the index.
63 * This is a one-shot wrapper around add_pathspec_matches_against_index()
64 * which allocates, populates, and returns a seen[] array indicating the
65 * nature of the "closest" (i.e. most specific) matches which each of the
66 * given pathspecs achieves against all items in the index.
68 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
69 struct index_state *istate,
70 enum ps_skip_worktree_action sw_action)
72 char *seen = xcalloc(pathspec->nr, 1);
73 add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
74 return seen;
77 char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
79 struct index_state *istate = the_repository->index;
80 char *seen = xcalloc(pathspec->nr, 1);
81 int i;
83 for (i = 0; i < istate->cache_nr; i++) {
84 struct cache_entry *ce = istate->cache[i];
85 if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
86 ce_path_match(istate, ce, pathspec, seen);
89 return seen;
93 * Magic pathspec
95 * Possible future magic semantics include stuff like:
97 * { PATHSPEC_RECURSIVE, '*', "recursive" },
98 * { PATHSPEC_REGEXP, '\0', "regexp" },
102 static struct pathspec_magic {
103 unsigned bit;
104 char mnemonic; /* this cannot be ':'! */
105 const char *name;
106 } pathspec_magic[] = {
107 { PATHSPEC_FROMTOP, '/', "top" },
108 { PATHSPEC_LITERAL, '\0', "literal" },
109 { PATHSPEC_GLOB, '\0', "glob" },
110 { PATHSPEC_ICASE, '\0', "icase" },
111 { PATHSPEC_EXCLUDE, '!', "exclude" },
112 { PATHSPEC_ATTR, '\0', "attr" },
115 static void prefix_magic(struct strbuf *sb, int prefixlen,
116 unsigned magic, const char *element)
118 /* No magic was found in element, just add prefix magic */
119 if (!magic) {
120 strbuf_addf(sb, ":(prefix:%d)", prefixlen);
121 return;
125 * At this point, we know that parse_element_magic() was able
126 * to extract some pathspec magic from element. So we know
127 * element is correctly formatted in either shorthand or
128 * longhand form
130 if (element[1] != '(') {
131 /* Process an element in shorthand form (e.g. ":!/<match>") */
132 strbuf_addstr(sb, ":(");
133 for (int i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
134 if ((magic & pathspec_magic[i].bit) &&
135 pathspec_magic[i].mnemonic) {
136 if (sb->buf[sb->len - 1] != '(')
137 strbuf_addch(sb, ',');
138 strbuf_addstr(sb, pathspec_magic[i].name);
141 } else {
142 /* For the longhand form, we copy everything up to the final ')' */
143 size_t len = strchr(element, ')') - element;
144 strbuf_add(sb, element, len);
146 strbuf_addf(sb, ",prefix:%d)", prefixlen);
149 static size_t strcspn_escaped(const char *s, const char *stop)
151 const char *i;
153 for (i = s; *i; i++) {
154 /* skip the escaped character */
155 if (i[0] == '\\' && i[1]) {
156 i++;
157 continue;
160 if (strchr(stop, *i))
161 break;
163 return i - s;
166 static inline int invalid_value_char(const char ch)
168 if (isalnum(ch) || strchr(",-_", ch))
169 return 0;
170 return -1;
173 static char *attr_value_unescape(const char *value)
175 const char *src;
176 char *dst, *ret;
178 ret = xmallocz(strlen(value));
179 for (src = value, dst = ret; *src; src++, dst++) {
180 if (*src == '\\') {
181 if (!src[1])
182 die(_("Escape character '\\' not allowed as "
183 "last character in attr value"));
184 src++;
186 if (invalid_value_char(*src))
187 die("cannot use '%c' for value matching", *src);
188 *dst = *src;
190 *dst = '\0';
191 return ret;
194 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
196 struct string_list_item *si;
197 struct string_list list = STRING_LIST_INIT_DUP;
199 if (item->attr_check || item->attr_match)
200 die(_("Only one 'attr:' specification is allowed."));
202 if (!value || !*value)
203 die(_("attr spec must not be empty"));
205 string_list_split(&list, value, ' ', -1);
206 string_list_remove_empty_items(&list, 0);
208 item->attr_check = attr_check_alloc();
209 CALLOC_ARRAY(item->attr_match, list.nr);
211 for_each_string_list_item(si, &list) {
212 size_t attr_len;
213 char *attr_name;
214 const struct git_attr *a;
216 int j = item->attr_match_nr++;
217 const char *attr = si->string;
218 struct attr_match *am = &item->attr_match[j];
220 switch (*attr) {
221 case '!':
222 am->match_mode = MATCH_UNSPECIFIED;
223 attr++;
224 attr_len = strlen(attr);
225 break;
226 case '-':
227 am->match_mode = MATCH_UNSET;
228 attr++;
229 attr_len = strlen(attr);
230 break;
231 default:
232 attr_len = strcspn(attr, "=");
233 if (attr[attr_len] != '=')
234 am->match_mode = MATCH_SET;
235 else {
236 const char *v = &attr[attr_len + 1];
237 am->match_mode = MATCH_VALUE;
238 am->value = attr_value_unescape(v);
240 break;
243 attr_name = xmemdupz(attr, attr_len);
244 a = git_attr(attr_name);
245 if (!a)
246 die(_("invalid attribute name %s"), attr_name);
248 attr_check_append(item->attr_check, a);
250 free(attr_name);
253 if (item->attr_check->nr != item->attr_match_nr)
254 BUG("should have same number of entries");
256 string_list_clear(&list, 0);
259 static inline int get_literal_global(void)
261 static int literal = -1;
263 if (literal < 0)
264 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
266 return literal;
269 static inline int get_glob_global(void)
271 static int glob = -1;
273 if (glob < 0)
274 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
276 return glob;
279 static inline int get_noglob_global(void)
281 static int noglob = -1;
283 if (noglob < 0)
284 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
286 return noglob;
289 static inline int get_icase_global(void)
291 static int icase = -1;
293 if (icase < 0)
294 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
296 return icase;
299 static int get_global_magic(int element_magic)
301 int global_magic = 0;
303 if (get_literal_global())
304 global_magic |= PATHSPEC_LITERAL;
306 /* --glob-pathspec is overridden by :(literal) */
307 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
308 global_magic |= PATHSPEC_GLOB;
310 if (get_glob_global() && get_noglob_global())
311 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
313 if (get_icase_global())
314 global_magic |= PATHSPEC_ICASE;
316 if ((global_magic & PATHSPEC_LITERAL) &&
317 (global_magic & ~PATHSPEC_LITERAL))
318 die(_("global 'literal' pathspec setting is incompatible "
319 "with all other global pathspec settings"));
321 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
322 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
323 global_magic |= PATHSPEC_LITERAL;
325 return global_magic;
329 * Parse the pathspec element looking for long magic
331 * saves all magic in 'magic'
332 * if prefix magic is used, save the prefix length in 'prefix_len'
333 * returns the position in 'elem' after all magic has been parsed
335 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
336 struct pathspec_item *item,
337 const char *elem)
339 const char *pos;
340 const char *nextat;
342 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
343 size_t len = strcspn_escaped(pos, ",)");
344 int i;
346 if (pos[len] == ',')
347 nextat = pos + len + 1; /* handle ',' */
348 else
349 nextat = pos + len; /* handle ')' and '\0' */
351 if (!len)
352 continue;
354 if (starts_with(pos, "prefix:")) {
355 char *endptr;
356 *prefix_len = strtol(pos + 7, &endptr, 10);
357 if (endptr - pos != len)
358 die(_("invalid parameter for pathspec magic 'prefix'"));
359 continue;
362 if (starts_with(pos, "attr:")) {
363 char *attr_body = xmemdupz(pos + 5, len - 5);
364 parse_pathspec_attr_match(item, attr_body);
365 *magic |= PATHSPEC_ATTR;
366 free(attr_body);
367 continue;
370 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
371 if (strlen(pathspec_magic[i].name) == len &&
372 !strncmp(pathspec_magic[i].name, pos, len)) {
373 *magic |= pathspec_magic[i].bit;
374 break;
378 if (ARRAY_SIZE(pathspec_magic) <= i)
379 die(_("Invalid pathspec magic '%.*s' in '%s'"),
380 (int) len, pos, elem);
383 if (*pos != ')')
384 die(_("Missing ')' at the end of pathspec magic in '%s'"),
385 elem);
386 pos++;
388 return pos;
392 * Parse the pathspec element looking for short magic
394 * saves all magic in 'magic'
395 * returns the position in 'elem' after all magic has been parsed
397 static const char *parse_short_magic(unsigned *magic, const char *elem)
399 const char *pos;
401 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
402 char ch = *pos;
403 int i;
405 /* Special case alias for '!' */
406 if (ch == '^') {
407 *magic |= PATHSPEC_EXCLUDE;
408 continue;
411 if (!is_pathspec_magic(ch))
412 break;
414 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
415 if (pathspec_magic[i].mnemonic == ch) {
416 *magic |= pathspec_magic[i].bit;
417 break;
421 if (ARRAY_SIZE(pathspec_magic) <= i)
422 die(_("Unimplemented pathspec magic '%c' in '%s'"),
423 ch, elem);
426 if (*pos == ':')
427 pos++;
429 return pos;
432 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
433 struct pathspec_item *item,
434 const char *elem)
436 if (elem[0] != ':' || get_literal_global())
437 return elem; /* nothing to do */
438 else if (elem[1] == '(')
439 /* longhand */
440 return parse_long_magic(magic, prefix_len, item, elem);
441 else
442 /* shorthand */
443 return parse_short_magic(magic, elem);
447 * Perform the initialization of a pathspec_item based on a pathspec element.
449 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
450 const char *prefix, int prefixlen,
451 const char *elt)
453 unsigned magic = 0, element_magic = 0;
454 const char *copyfrom = elt;
455 char *match;
456 int pathspec_prefix = -1;
458 item->attr_check = NULL;
459 item->attr_match = NULL;
460 item->attr_match_nr = 0;
462 /* PATHSPEC_LITERAL_PATH ignores magic */
463 if (flags & PATHSPEC_LITERAL_PATH) {
464 magic = PATHSPEC_LITERAL;
465 } else {
466 copyfrom = parse_element_magic(&element_magic,
467 &pathspec_prefix,
468 item,
469 elt);
470 magic |= element_magic;
471 magic |= get_global_magic(element_magic);
474 item->magic = magic;
476 if (pathspec_prefix >= 0 &&
477 (prefixlen || (prefix && *prefix)))
478 BUG("'prefix' magic is supposed to be used at worktree's root");
480 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
481 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
483 /* Create match string which will be used for pathspec matching */
484 if (pathspec_prefix >= 0) {
485 match = xstrdup(copyfrom);
486 prefixlen = pathspec_prefix;
487 } else if (magic & PATHSPEC_FROMTOP) {
488 match = xstrdup(copyfrom);
489 prefixlen = 0;
490 } else {
491 match = prefix_path_gently(prefix, prefixlen,
492 &prefixlen, copyfrom);
493 if (!match) {
494 const char *hint_path;
496 if (!have_git_dir())
497 die(_("'%s' is outside the directory tree"),
498 copyfrom);
499 hint_path = repo_get_work_tree(the_repository);
500 if (!hint_path)
501 hint_path = repo_get_git_dir(the_repository);
502 die(_("%s: '%s' is outside repository at '%s'"), elt,
503 copyfrom, absolute_path(hint_path));
507 item->match = match;
508 item->len = strlen(item->match);
509 item->prefix = prefixlen;
512 * Prefix the pathspec (keep all magic) and assign to
513 * original. Useful for passing to another command.
515 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
516 !get_literal_global()) {
517 struct strbuf sb = STRBUF_INIT;
519 /* Preserve the actual prefix length of each pattern */
520 prefix_magic(&sb, prefixlen, element_magic, elt);
522 strbuf_addstr(&sb, match);
523 item->original = strbuf_detach(&sb, NULL);
524 } else {
525 item->original = xstrdup(elt);
528 if (magic & PATHSPEC_LITERAL) {
529 item->nowildcard_len = item->len;
530 } else {
531 item->nowildcard_len = simple_length(item->match);
532 if (item->nowildcard_len < prefixlen)
533 item->nowildcard_len = prefixlen;
536 item->flags = 0;
537 if (magic & PATHSPEC_GLOB) {
539 * FIXME: should we enable ONESTAR in _GLOB for
540 * pattern "* * / * . c"?
542 } else {
543 if (item->nowildcard_len < item->len &&
544 item->match[item->nowildcard_len] == '*' &&
545 no_wildcard(item->match + item->nowildcard_len + 1))
546 item->flags |= PATHSPEC_ONESTAR;
549 /* sanity checks, pathspec matchers assume these are sane */
550 if (item->nowildcard_len > item->len ||
551 item->prefix > item->len) {
552 BUG("error initializing pathspec_item");
556 static int pathspec_item_cmp(const void *a_, const void *b_)
558 struct pathspec_item *a, *b;
560 a = (struct pathspec_item *)a_;
561 b = (struct pathspec_item *)b_;
562 return strcmp(a->match, b->match);
565 void pathspec_magic_names(unsigned magic, struct strbuf *out)
567 int i;
568 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
569 const struct pathspec_magic *m = pathspec_magic + i;
570 if (!(magic & m->bit))
571 continue;
572 if (out->len)
573 strbuf_addstr(out, ", ");
575 if (m->mnemonic)
576 strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
577 m->name, m->mnemonic);
578 else
579 strbuf_addf(out, "'%s'", m->name);
583 static void NORETURN unsupported_magic(const char *pattern,
584 unsigned magic)
586 struct strbuf sb = STRBUF_INIT;
587 pathspec_magic_names(magic, &sb);
589 * We may want to substitute "this command" with a command
590 * name. E.g. when "git add -p" or "git add -i" dies when running
591 * "checkout -p"
593 die(_("%s: pathspec magic not supported by this command: %s"),
594 pattern, sb.buf);
597 void parse_pathspec(struct pathspec *pathspec,
598 unsigned magic_mask, unsigned flags,
599 const char *prefix, const char **argv)
601 struct pathspec_item *item;
602 const char *entry = argv ? *argv : NULL;
603 int i, n, prefixlen, nr_exclude = 0;
605 memset(pathspec, 0, sizeof(*pathspec));
607 if (flags & PATHSPEC_MAXDEPTH_VALID)
608 pathspec->magic |= PATHSPEC_MAXDEPTH;
610 /* No arguments, no prefix -> no pathspec */
611 if (!entry && !prefix)
612 return;
614 if ((flags & PATHSPEC_PREFER_CWD) &&
615 (flags & PATHSPEC_PREFER_FULL))
616 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
618 /* No arguments with prefix -> prefix pathspec */
619 if (!entry) {
620 if (flags & PATHSPEC_PREFER_FULL)
621 return;
623 if (!(flags & PATHSPEC_PREFER_CWD))
624 BUG("PATHSPEC_PREFER_CWD requires arguments");
626 pathspec->items = CALLOC_ARRAY(item, 1);
627 item->match = xstrdup(prefix);
628 item->original = xstrdup(prefix);
629 item->nowildcard_len = item->len = strlen(prefix);
630 item->prefix = item->len;
631 pathspec->nr = 1;
632 return;
635 n = 0;
636 while (argv[n]) {
637 if (*argv[n] == '\0')
638 die("empty string is not a valid pathspec. "
639 "please use . instead if you meant to match all paths");
640 n++;
643 pathspec->nr = n;
644 ALLOC_ARRAY(pathspec->items, n + 1);
645 item = pathspec->items;
646 prefixlen = prefix ? strlen(prefix) : 0;
648 for (i = 0; i < n; i++) {
649 entry = argv[i];
651 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
653 if (item[i].magic & PATHSPEC_EXCLUDE)
654 nr_exclude++;
655 if (item[i].magic & magic_mask)
656 unsupported_magic(entry, item[i].magic & magic_mask);
658 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
659 has_symlink_leading_path(item[i].match, item[i].len)) {
660 die(_("pathspec '%s' is beyond a symbolic link"), entry);
663 if (item[i].nowildcard_len < item[i].len)
664 pathspec->has_wildcard = 1;
665 pathspec->magic |= item[i].magic;
669 * If everything is an exclude pattern, add one positive pattern
670 * that matches everything. We allocated an extra one for this.
672 if (nr_exclude == n) {
673 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
674 init_pathspec_item(item + n, 0, prefix, plen, ".");
675 pathspec->nr++;
678 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
679 if (flags & PATHSPEC_KEEP_ORDER)
680 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
681 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
685 void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
686 unsigned flags, const char *prefix,
687 const char *file, int nul_term_line)
689 struct strvec parsed_file = STRVEC_INIT;
690 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
691 strbuf_getline;
692 struct strbuf buf = STRBUF_INIT;
693 struct strbuf unquoted = STRBUF_INIT;
694 FILE *in;
696 if (!strcmp(file, "-"))
697 in = stdin;
698 else
699 in = xfopen(file, "r");
701 while (getline_fn(&buf, in) != EOF) {
702 if (!nul_term_line && buf.buf[0] == '"') {
703 strbuf_reset(&unquoted);
704 if (unquote_c_style(&unquoted, buf.buf, NULL))
705 die(_("line is badly quoted: %s"), buf.buf);
706 strbuf_swap(&buf, &unquoted);
708 strvec_push(&parsed_file, buf.buf);
709 strbuf_reset(&buf);
712 strbuf_release(&unquoted);
713 strbuf_release(&buf);
714 if (in != stdin)
715 fclose(in);
717 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
718 strvec_clear(&parsed_file);
721 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
723 int i, j;
725 *dst = *src;
726 DUP_ARRAY(dst->items, src->items, dst->nr);
728 for (i = 0; i < dst->nr; i++) {
729 struct pathspec_item *d = &dst->items[i];
730 struct pathspec_item *s = &src->items[i];
732 d->match = xstrdup(s->match);
733 d->original = xstrdup(s->original);
735 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
736 for (j = 0; j < d->attr_match_nr; j++) {
737 const char *value = s->attr_match[j].value;
738 d->attr_match[j].value = xstrdup_or_null(value);
741 d->attr_check = attr_check_dup(s->attr_check);
745 void clear_pathspec(struct pathspec *pathspec)
747 int i, j;
749 for (i = 0; i < pathspec->nr; i++) {
750 free(pathspec->items[i].match);
751 free(pathspec->items[i].original);
753 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
754 free(pathspec->items[i].attr_match[j].value);
755 free(pathspec->items[i].attr_match);
757 if (pathspec->items[i].attr_check)
758 attr_check_free(pathspec->items[i].attr_check);
761 FREE_AND_NULL(pathspec->items);
762 pathspec->nr = 0;
765 int match_pathspec_attrs(struct index_state *istate,
766 const char *name, int namelen,
767 const struct pathspec_item *item)
769 int i;
770 char *to_free = NULL;
772 if (name[namelen])
773 name = to_free = xmemdupz(name, namelen);
775 git_check_attr(istate, name, item->attr_check);
777 free(to_free);
779 for (i = 0; i < item->attr_match_nr; i++) {
780 const char *value;
781 int matched;
782 enum attr_match_mode match_mode;
784 value = item->attr_check->items[i].value;
785 match_mode = item->attr_match[i].match_mode;
787 if (ATTR_TRUE(value))
788 matched = (match_mode == MATCH_SET);
789 else if (ATTR_FALSE(value))
790 matched = (match_mode == MATCH_UNSET);
791 else if (ATTR_UNSET(value))
792 matched = (match_mode == MATCH_UNSPECIFIED);
793 else
794 matched = (match_mode == MATCH_VALUE &&
795 !strcmp(item->attr_match[i].value, value));
796 if (!matched)
797 return 0;
800 return 1;
803 int pathspec_needs_expanded_index(struct index_state *istate,
804 const struct pathspec *pathspec)
806 unsigned int i, pos;
807 int res = 0;
808 char *skip_worktree_seen = NULL;
811 * If index is not sparse, no index expansion is needed.
813 if (!istate->sparse_index)
814 return 0;
817 * When using a magic pathspec, assume for the sake of simplicity that
818 * the index needs to be expanded to match all matchable files.
820 if (pathspec->magic)
821 return 1;
823 for (i = 0; i < pathspec->nr; i++) {
824 struct pathspec_item item = pathspec->items[i];
827 * If the pathspec item has a wildcard, the index should be expanded
828 * if the pathspec has the possibility of matching a subset of entries inside
829 * of a sparse directory (but not the entire directory).
831 * If the pathspec item is a literal path, the index only needs to be expanded
832 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
833 * expand for in-cone files) and b) it doesn't match any sparse directories
834 * (since we can reset whole sparse directories without expanding them).
836 if (item.nowildcard_len < item.len) {
838 * Special case: if the pattern is a path inside the cone
839 * followed by only wildcards, the pattern cannot match
840 * partial sparse directories, so we know we don't need to
841 * expand the index.
843 * Examples:
844 * - in-cone/foo***: doesn't need expanded index
845 * - not-in-cone/bar*: may need expanded index
846 * - **.c: may need expanded index
848 if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
849 path_in_cone_mode_sparse_checkout(item.original, istate))
850 continue;
852 for (pos = 0; pos < istate->cache_nr; pos++) {
853 struct cache_entry *ce = istate->cache[pos];
855 if (!S_ISSPARSEDIR(ce->ce_mode))
856 continue;
859 * If the pre-wildcard length is longer than the sparse
860 * directory name and the sparse directory is the first
861 * component of the pathspec, need to expand the index.
863 if (item.nowildcard_len > ce_namelen(ce) &&
864 !strncmp(item.original, ce->name, ce_namelen(ce))) {
865 res = 1;
866 break;
870 * If the pre-wildcard length is shorter than the sparse
871 * directory and the pathspec does not match the whole
872 * directory, need to expand the index.
874 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
875 wildmatch(item.original, ce->name, 0)) {
876 res = 1;
877 break;
880 } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
881 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
882 res = 1;
884 if (res > 0)
885 break;
888 free(skip_worktree_seen);
889 return res;