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