git
/
pathspec.c
889 строк · 22.4 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "abspath.h"5#include "parse.h"6#include "dir.h"7#include "environment.h"8#include "gettext.h"9#include "pathspec.h"10#include "attr.h"11#include "read-cache.h"12#include "repository.h"13#include "setup.h"14#include "strvec.h"15#include "symlinks.h"16#include "quote.h"17#include "wildmatch.h"18
19/*
20* Finds which of the given pathspecs match items in the index.
21*
22* For each pathspec, sets the corresponding entry in the seen[] array
23* (which should be specs items long, i.e. the same size as pathspec)
24* to the nature of the "closest" (i.e. most specific) match found for
25* that pathspec in the index, if it was a closer type of match than
26* the existing entry. As an optimization, matching is skipped
27* altogether if seen[] already only contains non-zero entries.
28*
29* If seen[] has not already been written to, it may make sense
30* to use find_pathspecs_matching_against_index() instead.
31*/
32void add_pathspec_matches_against_index(const struct pathspec *pathspec,33struct index_state *istate,34char *seen,35enum ps_skip_worktree_action sw_action)36{
37int num_unmatched = 0, i;38
39/*40* Since we are walking the index as if we were walking the directory,
41* we have to mark the matched pathspec as seen; otherwise we will
42* mistakenly think that the user gave a pathspec that did not match
43* anything.
44*/
45for (i = 0; i < pathspec->nr; i++)46if (!seen[i])47num_unmatched++;48if (!num_unmatched)49return;50for (i = 0; i < istate->cache_nr; i++) {51const struct cache_entry *ce = istate->cache[i];52if (sw_action == PS_IGNORE_SKIP_WORKTREE &&53(ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))54continue;55ce_path_match(istate, ce, pathspec, seen);56}57}
58
59/*
60* Finds which of the given pathspecs match items in the index.
61*
62* This is a one-shot wrapper around add_pathspec_matches_against_index()
63* which allocates, populates, and returns a seen[] array indicating the
64* nature of the "closest" (i.e. most specific) matches which each of the
65* given pathspecs achieves against all items in the index.
66*/
67char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,68struct index_state *istate,69enum ps_skip_worktree_action sw_action)70{
71char *seen = xcalloc(pathspec->nr, 1);72add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);73return seen;74}
75
76char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)77{
78struct index_state *istate = the_repository->index;79char *seen = xcalloc(pathspec->nr, 1);80int i;81
82for (i = 0; i < istate->cache_nr; i++) {83struct cache_entry *ce = istate->cache[i];84if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))85ce_path_match(istate, ce, pathspec, seen);86}87
88return seen;89}
90
91/*
92* Magic pathspec
93*
94* Possible future magic semantics include stuff like:
95*
96* { PATHSPEC_RECURSIVE, '*', "recursive" },
97* { PATHSPEC_REGEXP, '\0', "regexp" },
98*
99*/
100
101static struct pathspec_magic {102unsigned bit;103char mnemonic; /* this cannot be ':'! */104const char *name;105} pathspec_magic[] = {106{ PATHSPEC_FROMTOP, '/', "top" },107{ PATHSPEC_LITERAL, '\0', "literal" },108{ PATHSPEC_GLOB, '\0', "glob" },109{ PATHSPEC_ICASE, '\0', "icase" },110{ PATHSPEC_EXCLUDE, '!', "exclude" },111{ PATHSPEC_ATTR, '\0', "attr" },112};113
114static void prefix_magic(struct strbuf *sb, int prefixlen,115unsigned magic, const char *element)116{
117/* No magic was found in element, just add prefix magic */118if (!magic) {119strbuf_addf(sb, ":(prefix:%d)", prefixlen);120return;121}122
123/*124* At this point, we know that parse_element_magic() was able
125* to extract some pathspec magic from element. So we know
126* element is correctly formatted in either shorthand or
127* longhand form
128*/
129if (element[1] != '(') {130/* Process an element in shorthand form (e.g. ":!/<match>") */131strbuf_addstr(sb, ":(");132for (int i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {133if ((magic & pathspec_magic[i].bit) &&134pathspec_magic[i].mnemonic) {135if (sb->buf[sb->len - 1] != '(')136strbuf_addch(sb, ',');137strbuf_addstr(sb, pathspec_magic[i].name);138}139}140} else {141/* For the longhand form, we copy everything up to the final ')' */142size_t len = strchr(element, ')') - element;143strbuf_add(sb, element, len);144}145strbuf_addf(sb, ",prefix:%d)", prefixlen);146}
147
148static size_t strcspn_escaped(const char *s, const char *stop)149{
150const char *i;151
152for (i = s; *i; i++) {153/* skip the escaped character */154if (i[0] == '\\' && i[1]) {155i++;156continue;157}158
159if (strchr(stop, *i))160break;161}162return i - s;163}
164
165static inline int invalid_value_char(const char ch)166{
167if (isalnum(ch) || strchr(",-_", ch))168return 0;169return -1;170}
171
172static char *attr_value_unescape(const char *value)173{
174const char *src;175char *dst, *ret;176
177ret = xmallocz(strlen(value));178for (src = value, dst = ret; *src; src++, dst++) {179if (*src == '\\') {180if (!src[1])181die(_("Escape character '\\' not allowed as "182"last character in attr value"));183src++;184}185if (invalid_value_char(*src))186die("cannot use '%c' for value matching", *src);187*dst = *src;188}189*dst = '\0';190return ret;191}
192
193static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)194{
195struct string_list_item *si;196struct string_list list = STRING_LIST_INIT_DUP;197
198if (item->attr_check || item->attr_match)199die(_("Only one 'attr:' specification is allowed."));200
201if (!value || !*value)202die(_("attr spec must not be empty"));203
204string_list_split(&list, value, ' ', -1);205string_list_remove_empty_items(&list, 0);206
207item->attr_check = attr_check_alloc();208CALLOC_ARRAY(item->attr_match, list.nr);209
210for_each_string_list_item(si, &list) {211size_t attr_len;212char *attr_name;213const struct git_attr *a;214
215int j = item->attr_match_nr++;216const char *attr = si->string;217struct attr_match *am = &item->attr_match[j];218
219switch (*attr) {220case '!':221am->match_mode = MATCH_UNSPECIFIED;222attr++;223attr_len = strlen(attr);224break;225case '-':226am->match_mode = MATCH_UNSET;227attr++;228attr_len = strlen(attr);229break;230default:231attr_len = strcspn(attr, "=");232if (attr[attr_len] != '=')233am->match_mode = MATCH_SET;234else {235const char *v = &attr[attr_len + 1];236am->match_mode = MATCH_VALUE;237am->value = attr_value_unescape(v);238}239break;240}241
242attr_name = xmemdupz(attr, attr_len);243a = git_attr(attr_name);244if (!a)245die(_("invalid attribute name %s"), attr_name);246
247attr_check_append(item->attr_check, a);248
249free(attr_name);250}251
252if (item->attr_check->nr != item->attr_match_nr)253BUG("should have same number of entries");254
255string_list_clear(&list, 0);256}
257
258static inline int get_literal_global(void)259{
260static int literal = -1;261
262if (literal < 0)263literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);264
265return literal;266}
267
268static inline int get_glob_global(void)269{
270static int glob = -1;271
272if (glob < 0)273glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);274
275return glob;276}
277
278static inline int get_noglob_global(void)279{
280static int noglob = -1;281
282if (noglob < 0)283noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);284
285return noglob;286}
287
288static inline int get_icase_global(void)289{
290static int icase = -1;291
292if (icase < 0)293icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);294
295return icase;296}
297
298static int get_global_magic(int element_magic)299{
300int global_magic = 0;301
302if (get_literal_global())303global_magic |= PATHSPEC_LITERAL;304
305/* --glob-pathspec is overridden by :(literal) */306if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))307global_magic |= PATHSPEC_GLOB;308
309if (get_glob_global() && get_noglob_global())310die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));311
312if (get_icase_global())313global_magic |= PATHSPEC_ICASE;314
315if ((global_magic & PATHSPEC_LITERAL) &&316(global_magic & ~PATHSPEC_LITERAL))317die(_("global 'literal' pathspec setting is incompatible "318"with all other global pathspec settings"));319
320/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */321if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))322global_magic |= PATHSPEC_LITERAL;323
324return global_magic;325}
326
327/*
328* Parse the pathspec element looking for long magic
329*
330* saves all magic in 'magic'
331* if prefix magic is used, save the prefix length in 'prefix_len'
332* returns the position in 'elem' after all magic has been parsed
333*/
334static const char *parse_long_magic(unsigned *magic, int *prefix_len,335struct pathspec_item *item,336const char *elem)337{
338const char *pos;339const char *nextat;340
341for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {342size_t len = strcspn_escaped(pos, ",)");343int i;344
345if (pos[len] == ',')346nextat = pos + len + 1; /* handle ',' */347else348nextat = pos + len; /* handle ')' and '\0' */349
350if (!len)351continue;352
353if (starts_with(pos, "prefix:")) {354char *endptr;355*prefix_len = strtol(pos + 7, &endptr, 10);356if (endptr - pos != len)357die(_("invalid parameter for pathspec magic 'prefix'"));358continue;359}360
361if (starts_with(pos, "attr:")) {362char *attr_body = xmemdupz(pos + 5, len - 5);363parse_pathspec_attr_match(item, attr_body);364*magic |= PATHSPEC_ATTR;365free(attr_body);366continue;367}368
369for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {370if (strlen(pathspec_magic[i].name) == len &&371!strncmp(pathspec_magic[i].name, pos, len)) {372*magic |= pathspec_magic[i].bit;373break;374}375}376
377if (ARRAY_SIZE(pathspec_magic) <= i)378die(_("Invalid pathspec magic '%.*s' in '%s'"),379(int) len, pos, elem);380}381
382if (*pos != ')')383die(_("Missing ')' at the end of pathspec magic in '%s'"),384elem);385pos++;386
387return pos;388}
389
390/*
391* Parse the pathspec element looking for short magic
392*
393* saves all magic in 'magic'
394* returns the position in 'elem' after all magic has been parsed
395*/
396static const char *parse_short_magic(unsigned *magic, const char *elem)397{
398const char *pos;399
400for (pos = elem + 1; *pos && *pos != ':'; pos++) {401char ch = *pos;402int i;403
404/* Special case alias for '!' */405if (ch == '^') {406*magic |= PATHSPEC_EXCLUDE;407continue;408}409
410if (!is_pathspec_magic(ch))411break;412
413for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {414if (pathspec_magic[i].mnemonic == ch) {415*magic |= pathspec_magic[i].bit;416break;417}418}419
420if (ARRAY_SIZE(pathspec_magic) <= i)421die(_("Unimplemented pathspec magic '%c' in '%s'"),422ch, elem);423}424
425if (*pos == ':')426pos++;427
428return pos;429}
430
431static const char *parse_element_magic(unsigned *magic, int *prefix_len,432struct pathspec_item *item,433const char *elem)434{
435if (elem[0] != ':' || get_literal_global())436return elem; /* nothing to do */437else if (elem[1] == '(')438/* longhand */439return parse_long_magic(magic, prefix_len, item, elem);440else441/* shorthand */442return parse_short_magic(magic, elem);443}
444
445/*
446* Perform the initialization of a pathspec_item based on a pathspec element.
447*/
448static void init_pathspec_item(struct pathspec_item *item, unsigned flags,449const char *prefix, int prefixlen,450const char *elt)451{
452unsigned magic = 0, element_magic = 0;453const char *copyfrom = elt;454char *match;455int pathspec_prefix = -1;456
457item->attr_check = NULL;458item->attr_match = NULL;459item->attr_match_nr = 0;460
461/* PATHSPEC_LITERAL_PATH ignores magic */462if (flags & PATHSPEC_LITERAL_PATH) {463magic = PATHSPEC_LITERAL;464} else {465copyfrom = parse_element_magic(&element_magic,466&pathspec_prefix,467item,468elt);469magic |= element_magic;470magic |= get_global_magic(element_magic);471}472
473item->magic = magic;474
475if (pathspec_prefix >= 0 &&476(prefixlen || (prefix && *prefix)))477BUG("'prefix' magic is supposed to be used at worktree's root");478
479if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))480die(_("%s: 'literal' and 'glob' are incompatible"), elt);481
482/* Create match string which will be used for pathspec matching */483if (pathspec_prefix >= 0) {484match = xstrdup(copyfrom);485prefixlen = pathspec_prefix;486} else if (magic & PATHSPEC_FROMTOP) {487match = xstrdup(copyfrom);488prefixlen = 0;489} else {490match = prefix_path_gently(prefix, prefixlen,491&prefixlen, copyfrom);492if (!match) {493const char *hint_path;494
495if (!have_git_dir())496die(_("'%s' is outside the directory tree"),497copyfrom);498hint_path = get_git_work_tree();499if (!hint_path)500hint_path = get_git_dir();501die(_("%s: '%s' is outside repository at '%s'"), elt,502copyfrom, absolute_path(hint_path));503}504}505
506item->match = match;507item->len = strlen(item->match);508item->prefix = prefixlen;509
510/*511* Prefix the pathspec (keep all magic) and assign to
512* original. Useful for passing to another command.
513*/
514if ((flags & PATHSPEC_PREFIX_ORIGIN) &&515!get_literal_global()) {516struct strbuf sb = STRBUF_INIT;517
518/* Preserve the actual prefix length of each pattern */519prefix_magic(&sb, prefixlen, element_magic, elt);520
521strbuf_addstr(&sb, match);522item->original = strbuf_detach(&sb, NULL);523} else {524item->original = xstrdup(elt);525}526
527if (magic & PATHSPEC_LITERAL) {528item->nowildcard_len = item->len;529} else {530item->nowildcard_len = simple_length(item->match);531if (item->nowildcard_len < prefixlen)532item->nowildcard_len = prefixlen;533}534
535item->flags = 0;536if (magic & PATHSPEC_GLOB) {537/*538* FIXME: should we enable ONESTAR in _GLOB for
539* pattern "* * / * . c"?
540*/
541} else {542if (item->nowildcard_len < item->len &&543item->match[item->nowildcard_len] == '*' &&544no_wildcard(item->match + item->nowildcard_len + 1))545item->flags |= PATHSPEC_ONESTAR;546}547
548/* sanity checks, pathspec matchers assume these are sane */549if (item->nowildcard_len > item->len ||550item->prefix > item->len) {551BUG("error initializing pathspec_item");552}553}
554
555static int pathspec_item_cmp(const void *a_, const void *b_)556{
557struct pathspec_item *a, *b;558
559a = (struct pathspec_item *)a_;560b = (struct pathspec_item *)b_;561return strcmp(a->match, b->match);562}
563
564void pathspec_magic_names(unsigned magic, struct strbuf *out)565{
566int i;567for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {568const struct pathspec_magic *m = pathspec_magic + i;569if (!(magic & m->bit))570continue;571if (out->len)572strbuf_addstr(out, ", ");573
574if (m->mnemonic)575strbuf_addf(out, _("'%s' (mnemonic: '%c')"),576m->name, m->mnemonic);577else578strbuf_addf(out, "'%s'", m->name);579}580}
581
582static void NORETURN unsupported_magic(const char *pattern,583unsigned magic)584{
585struct strbuf sb = STRBUF_INIT;586pathspec_magic_names(magic, &sb);587/*588* We may want to substitute "this command" with a command
589* name. E.g. when "git add -p" or "git add -i" dies when running
590* "checkout -p"
591*/
592die(_("%s: pathspec magic not supported by this command: %s"),593pattern, sb.buf);594}
595
596void parse_pathspec(struct pathspec *pathspec,597unsigned magic_mask, unsigned flags,598const char *prefix, const char **argv)599{
600struct pathspec_item *item;601const char *entry = argv ? *argv : NULL;602int i, n, prefixlen, nr_exclude = 0;603
604memset(pathspec, 0, sizeof(*pathspec));605
606if (flags & PATHSPEC_MAXDEPTH_VALID)607pathspec->magic |= PATHSPEC_MAXDEPTH;608
609/* No arguments, no prefix -> no pathspec */610if (!entry && !prefix)611return;612
613if ((flags & PATHSPEC_PREFER_CWD) &&614(flags & PATHSPEC_PREFER_FULL))615BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");616
617/* No arguments with prefix -> prefix pathspec */618if (!entry) {619if (flags & PATHSPEC_PREFER_FULL)620return;621
622if (!(flags & PATHSPEC_PREFER_CWD))623BUG("PATHSPEC_PREFER_CWD requires arguments");624
625pathspec->items = CALLOC_ARRAY(item, 1);626item->match = xstrdup(prefix);627item->original = xstrdup(prefix);628item->nowildcard_len = item->len = strlen(prefix);629item->prefix = item->len;630pathspec->nr = 1;631return;632}633
634n = 0;635while (argv[n]) {636if (*argv[n] == '\0')637die("empty string is not a valid pathspec. "638"please use . instead if you meant to match all paths");639n++;640}641
642pathspec->nr = n;643ALLOC_ARRAY(pathspec->items, n + 1);644item = pathspec->items;645prefixlen = prefix ? strlen(prefix) : 0;646
647for (i = 0; i < n; i++) {648entry = argv[i];649
650init_pathspec_item(item + i, flags, prefix, prefixlen, entry);651
652if (item[i].magic & PATHSPEC_EXCLUDE)653nr_exclude++;654if (item[i].magic & magic_mask)655unsupported_magic(entry, item[i].magic & magic_mask);656
657if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&658has_symlink_leading_path(item[i].match, item[i].len)) {659die(_("pathspec '%s' is beyond a symbolic link"), entry);660}661
662if (item[i].nowildcard_len < item[i].len)663pathspec->has_wildcard = 1;664pathspec->magic |= item[i].magic;665}666
667/*668* If everything is an exclude pattern, add one positive pattern
669* that matches everything. We allocated an extra one for this.
670*/
671if (nr_exclude == n) {672int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;673init_pathspec_item(item + n, 0, prefix, plen, ".");674pathspec->nr++;675}676
677if (pathspec->magic & PATHSPEC_MAXDEPTH) {678if (flags & PATHSPEC_KEEP_ORDER)679BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");680QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);681}682}
683
684void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,685unsigned flags, const char *prefix,686const char *file, int nul_term_line)687{
688struct strvec parsed_file = STRVEC_INIT;689strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :690strbuf_getline;691struct strbuf buf = STRBUF_INIT;692struct strbuf unquoted = STRBUF_INIT;693FILE *in;694
695if (!strcmp(file, "-"))696in = stdin;697else698in = xfopen(file, "r");699
700while (getline_fn(&buf, in) != EOF) {701if (!nul_term_line && buf.buf[0] == '"') {702strbuf_reset(&unquoted);703if (unquote_c_style(&unquoted, buf.buf, NULL))704die(_("line is badly quoted: %s"), buf.buf);705strbuf_swap(&buf, &unquoted);706}707strvec_push(&parsed_file, buf.buf);708strbuf_reset(&buf);709}710
711strbuf_release(&unquoted);712strbuf_release(&buf);713if (in != stdin)714fclose(in);715
716parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);717strvec_clear(&parsed_file);718}
719
720void copy_pathspec(struct pathspec *dst, const struct pathspec *src)721{
722int i, j;723
724*dst = *src;725DUP_ARRAY(dst->items, src->items, dst->nr);726
727for (i = 0; i < dst->nr; i++) {728struct pathspec_item *d = &dst->items[i];729struct pathspec_item *s = &src->items[i];730
731d->match = xstrdup(s->match);732d->original = xstrdup(s->original);733
734DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);735for (j = 0; j < d->attr_match_nr; j++) {736const char *value = s->attr_match[j].value;737d->attr_match[j].value = xstrdup_or_null(value);738}739
740d->attr_check = attr_check_dup(s->attr_check);741}742}
743
744void clear_pathspec(struct pathspec *pathspec)745{
746int i, j;747
748for (i = 0; i < pathspec->nr; i++) {749free(pathspec->items[i].match);750free(pathspec->items[i].original);751
752for (j = 0; j < pathspec->items[i].attr_match_nr; j++)753free(pathspec->items[i].attr_match[j].value);754free(pathspec->items[i].attr_match);755
756if (pathspec->items[i].attr_check)757attr_check_free(pathspec->items[i].attr_check);758}759
760FREE_AND_NULL(pathspec->items);761pathspec->nr = 0;762}
763
764int match_pathspec_attrs(struct index_state *istate,765const char *name, int namelen,766const struct pathspec_item *item)767{
768int i;769char *to_free = NULL;770
771if (name[namelen])772name = to_free = xmemdupz(name, namelen);773
774git_check_attr(istate, name, item->attr_check);775
776free(to_free);777
778for (i = 0; i < item->attr_match_nr; i++) {779const char *value;780int matched;781enum attr_match_mode match_mode;782
783value = item->attr_check->items[i].value;784match_mode = item->attr_match[i].match_mode;785
786if (ATTR_TRUE(value))787matched = (match_mode == MATCH_SET);788else if (ATTR_FALSE(value))789matched = (match_mode == MATCH_UNSET);790else if (ATTR_UNSET(value))791matched = (match_mode == MATCH_UNSPECIFIED);792else793matched = (match_mode == MATCH_VALUE &&794!strcmp(item->attr_match[i].value, value));795if (!matched)796return 0;797}798
799return 1;800}
801
802int pathspec_needs_expanded_index(struct index_state *istate,803const struct pathspec *pathspec)804{
805unsigned int i, pos;806int res = 0;807char *skip_worktree_seen = NULL;808
809/*810* If index is not sparse, no index expansion is needed.
811*/
812if (!istate->sparse_index)813return 0;814
815/*816* When using a magic pathspec, assume for the sake of simplicity that
817* the index needs to be expanded to match all matchable files.
818*/
819if (pathspec->magic)820return 1;821
822for (i = 0; i < pathspec->nr; i++) {823struct pathspec_item item = pathspec->items[i];824
825/*826* If the pathspec item has a wildcard, the index should be expanded
827* if the pathspec has the possibility of matching a subset of entries inside
828* of a sparse directory (but not the entire directory).
829*
830* If the pathspec item is a literal path, the index only needs to be expanded
831* if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
832* expand for in-cone files) and b) it doesn't match any sparse directories
833* (since we can reset whole sparse directories without expanding them).
834*/
835if (item.nowildcard_len < item.len) {836/*837* Special case: if the pattern is a path inside the cone
838* followed by only wildcards, the pattern cannot match
839* partial sparse directories, so we know we don't need to
840* expand the index.
841*
842* Examples:
843* - in-cone/foo***: doesn't need expanded index
844* - not-in-cone/bar*: may need expanded index
845* - **.c: may need expanded index
846*/
847if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&848path_in_cone_mode_sparse_checkout(item.original, istate))849continue;850
851for (pos = 0; pos < istate->cache_nr; pos++) {852struct cache_entry *ce = istate->cache[pos];853
854if (!S_ISSPARSEDIR(ce->ce_mode))855continue;856
857/*858* If the pre-wildcard length is longer than the sparse
859* directory name and the sparse directory is the first
860* component of the pathspec, need to expand the index.
861*/
862if (item.nowildcard_len > ce_namelen(ce) &&863!strncmp(item.original, ce->name, ce_namelen(ce))) {864res = 1;865break;866}867
868/*869* If the pre-wildcard length is shorter than the sparse
870* directory and the pathspec does not match the whole
871* directory, need to expand the index.
872*/
873if (!strncmp(item.original, ce->name, item.nowildcard_len) &&874wildmatch(item.original, ce->name, 0)) {875res = 1;876break;877}878}879} else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&880!matches_skip_worktree(pathspec, i, &skip_worktree_seen))881res = 1;882
883if (res > 0)884break;885}886
887free(skip_worktree_seen);888return res;889}
890