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