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