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