]> git.ipfire.org Git - thirdparty/git.git/blame - pathspec.c
mingw: use lowercase includes for some Windows headers
[thirdparty/git.git] / pathspec.c
CommitLineData
6f525e71 1#include "cache.h"
0b027f6c 2#include "abspath.h"
b2141fc1 3#include "config.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"
d1cbe1e6 9#include "repository.h"
e38da487 10#include "setup.h"
dbbcd44f 11#include "strvec.h"
cb2a5135 12#include "symlinks.h"
24e4750c 13#include "quote.h"
6f525e71
AS
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
4b78d7bc 26 * to use find_pathspecs_matching_against_index() instead.
6f525e71 27 */
84b8b5d1 28void add_pathspec_matches_against_index(const struct pathspec *pathspec,
847a9e5d 29 struct index_state *istate,
719630eb
MT
30 char *seen,
31 enum ps_skip_worktree_action sw_action)
6f525e71
AS
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 */
84b8b5d1 41 for (i = 0; i < pathspec->nr; i++)
6f525e71
AS
42 if (!seen[i])
43 num_unmatched++;
44 if (!num_unmatched)
45 return;
08de9151
BW
46 for (i = 0; i < istate->cache_nr; i++) {
47 const struct cache_entry *ce = istate->cache[i];
49fdd51a
DS
48 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
49 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
719630eb 50 continue;
d17ef3a9 51 ce_path_match(istate, ce, pathspec, seen);
6f525e71
AS
52 }
53}
54
55/*
56 * Finds which of the given pathspecs match items in the index.
57 *
4b78d7bc
AS
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.
6f525e71 62 */
08de9151 63char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
fe069dce 64 struct index_state *istate,
719630eb 65 enum ps_skip_worktree_action sw_action)
6f525e71 66{
84b8b5d1 67 char *seen = xcalloc(pathspec->nr, 1);
719630eb 68 add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
6f525e71
AS
69 return seen;
70}
9d67b61f 71
a20f7047
MT
72char *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];
49fdd51a 80 if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
a20f7047
MT
81 ce_path_match(istate, ce, pathspec, seen);
82 }
83
6f525e71
AS
84 return seen;
85}
9d67b61f
AS
86
87/*
64acde94
NTND
88 * Magic pathspec
89 *
64acde94
NTND
90 * Possible future magic semantics include stuff like:
91 *
64acde94
NTND
92 * { PATHSPEC_RECURSIVE, '*', "recursive" },
93 * { PATHSPEC_REGEXP, '\0', "regexp" },
94 *
95 */
64acde94
NTND
96
97static struct pathspec_magic {
98 unsigned bit;
99 char mnemonic; /* this cannot be ':'! */
100 const char *name;
101} pathspec_magic[] = {
4f1bf4d2
BW
102 { PATHSPEC_FROMTOP, '/', "top" },
103 { PATHSPEC_LITERAL, '\0', "literal" },
104 { PATHSPEC_GLOB, '\0', "glob" },
105 { PATHSPEC_ICASE, '\0', "icase" },
106 { PATHSPEC_EXCLUDE, '!', "exclude" },
b0db7046 107 { PATHSPEC_ATTR, '\0', "attr" },
64acde94
NTND
108};
109
5d8f084a 110static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
1649612a
NTND
111{
112 int i;
113 strbuf_addstr(sb, ":(");
114 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
5d8f084a 115 if (magic & pathspec_magic[i].bit) {
1649612a
NTND
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
c5af19f9
BW
123static 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
140static inline int invalid_value_char(const char ch)
141{
142 if (isalnum(ch) || strchr(",-_", ch))
143 return 0;
144 return -1;
145}
146
147static 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
b0db7046
BW
168static 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();
ca56dadb 183 CALLOC_ARRAY(item->attr_match, list.nr);
b0db7046
BW
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 {
c5af19f9 210 const char *v = &attr[attr_len + 1];
b0db7046 211 am->match_mode = MATCH_VALUE;
c5af19f9 212 am->value = attr_value_unescape(v);
b0db7046
BW
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)
033abf97 228 BUG("should have same number of entries");
b0db7046
BW
229
230 string_list_clear(&list, 0);
231}
232
db7e8598
BW
233static 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
243static 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
253static 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
263static 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
273static 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
8881fde0
BW
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 */
309static const char *parse_long_magic(unsigned *magic, int *prefix_len,
b0db7046 310 struct pathspec_item *item,
8881fde0
BW
311 const char *elem)
312{
313 const char *pos;
314 const char *nextat;
315
316 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
c5af19f9 317 size_t len = strcspn_escaped(pos, ",)");
8881fde0
BW
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
b0db7046
BW
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
8881fde0
BW
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
b4bebdce
BW
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 */
371static 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
42ebeb9d
LT
379 /* Special case alias for '!' */
380 if (ch == '^') {
381 *magic |= PATHSPEC_EXCLUDE;
382 continue;
383 }
384
b4bebdce
BW
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
1b6112c5 406static const char *parse_element_magic(unsigned *magic, int *prefix_len,
b0db7046 407 struct pathspec_item *item,
1b6112c5
BW
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 */
b0db7046 414 return parse_long_magic(magic, prefix_len, item, elem);
1b6112c5
BW
415 else
416 /* shorthand */
417 return parse_short_magic(magic, elem);
418}
419
64acde94 420/*
27ec4282 421 * Perform the initialization of a pathspec_item based on a pathspec element.
9d67b61f 422 */
27ec4282
BW
423static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
424 const char *prefix, int prefixlen,
425 const char *elt)
9d67b61f 426{
db7e8598 427 unsigned magic = 0, element_magic = 0;
5d8f084a 428 const char *copyfrom = elt;
87323bda 429 char *match;
5590215b 430 int pathspec_prefix = -1;
64acde94 431
b0db7046
BW
432 item->attr_check = NULL;
433 item->attr_match = NULL;
434 item->attr_match_nr = 0;
435
db7e8598 436 /* PATHSPEC_LITERAL_PATH ignores magic */
1b6112c5 437 if (flags & PATHSPEC_LITERAL_PATH) {
db7e8598 438 magic = PATHSPEC_LITERAL;
1b6112c5
BW
439 } else {
440 copyfrom = parse_element_magic(&element_magic,
441 &pathspec_prefix,
b0db7046 442 item,
1b6112c5
BW
443 elt);
444 magic |= element_magic;
db7e8598 445 magic |= get_global_magic(element_magic);
1b6112c5 446 }
87323bda 447
27ec4282
BW
448 item->magic = magic;
449
233c3e6c
NTND
450 if (pathspec_prefix >= 0 &&
451 (prefixlen || (prefix && *prefix)))
033abf97 452 BUG("'prefix' magic is supposed to be used at worktree's root");
233c3e6c 453
bd30c2e4
NTND
454 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
455 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
456
4f1bf4d2 457 /* Create match string which will be used for pathspec matching */
233c3e6c
NTND
458 if (pathspec_prefix >= 0) {
459 match = xstrdup(copyfrom);
460 prefixlen = pathspec_prefix;
461 } else if (magic & PATHSPEC_FROMTOP) {
87323bda 462 match = xstrdup(copyfrom);
645a29c4
NTND
463 prefixlen = 0;
464 } else {
4f1bf4d2
BW
465 match = prefix_path_gently(prefix, prefixlen,
466 &prefixlen, copyfrom);
5c203986
ES
467 if (!match) {
468 const char *hint_path = get_git_work_tree();
469 if (!hint_path)
470 hint_path = get_git_dir();
e0020b2f 471 die(_("%s: '%s' is outside repository at '%s'"), elt,
5c203986
ES
472 copyfrom, absolute_path(hint_path));
473 }
645a29c4 474 }
4f1bf4d2 475
34305f77 476 item->match = match;
4f1bf4d2
BW
477 item->len = strlen(item->match);
478 item->prefix = prefixlen;
479
dad2586a
NTND
480 /*
481 * Prefix the pathspec (keep all magic) and assign to
482 * original. Useful for passing to another command.
483 */
5d8f084a 484 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
be4dbbbe 485 !get_literal_global()) {
dad2586a 486 struct strbuf sb = STRBUF_INIT;
5d8f084a
BW
487
488 /* Preserve the actual prefix length of each pattern */
489 prefix_magic(&sb, prefixlen, element_magic);
490
dad2586a
NTND
491 strbuf_addstr(&sb, match);
492 item->original = strbuf_detach(&sb, NULL);
8aee769f
BW
493 } else {
494 item->original = xstrdup(elt);
495 }
b69bb3fc 496
4f1bf4d2 497 if (magic & PATHSPEC_LITERAL) {
87323bda 498 item->nowildcard_len = item->len;
4f1bf4d2 499 } else {
87323bda 500 item->nowildcard_len = simple_length(item->match);
645a29c4
NTND
501 if (item->nowildcard_len < prefixlen)
502 item->nowildcard_len = prefixlen;
503 }
4f1bf4d2 504
87323bda 505 item->flags = 0;
bd30c2e4
NTND
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;
9d67b61f 516 }
645a29c4
NTND
517
518 /* sanity checks, pathspec matchers assume these are sane */
2d81c48f
SB
519 if (item->nowildcard_len > item->len ||
520 item->prefix > item->len) {
c3c3486b 521 BUG("error initializing pathspec_item");
2d81c48f 522 }
87323bda
NTND
523}
524
525static 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
534static void NORETURN unsupported_magic(const char *pattern,
2aee5849 535 unsigned magic)
87323bda
NTND
536{
537 struct strbuf sb = STRBUF_INIT;
93f3ddb2
BW
538 int i;
539 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
87323bda
NTND
540 const struct pathspec_magic *m = pathspec_magic + i;
541 if (!(magic & m->bit))
542 continue;
543 if (sb.len)
2aee5849
BW
544 strbuf_addstr(&sb, ", ");
545
546 if (m->mnemonic)
547 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
548 m->name, m->mnemonic);
87323bda
NTND
549 else
550 strbuf_addf(&sb, "'%s'", m->name);
87323bda
NTND
551 }
552 /*
553 * We may want to substitute "this command" with a command
5a7d41d8 554 * name. E.g. when "git add -p" or "git add -i" dies when running
87323bda
NTND
555 * "checkout -p"
556 */
557 die(_("%s: pathspec magic not supported by this command: %s"),
558 pattern, sb.buf);
9d67b61f 559}
512aaf94 560
87323bda
NTND
561void parse_pathspec(struct pathspec *pathspec,
562 unsigned magic_mask, unsigned flags,
563 const char *prefix, const char **argv)
512aaf94 564{
87323bda
NTND
565 struct pathspec_item *item;
566 const char *entry = argv ? *argv : NULL;
9e4e8a64 567 int i, n, prefixlen, nr_exclude = 0;
87323bda
NTND
568
569 memset(pathspec, 0, sizeof(*pathspec));
570
6330a171
NTND
571 if (flags & PATHSPEC_MAXDEPTH_VALID)
572 pathspec->magic |= PATHSPEC_MAXDEPTH;
573
87323bda
NTND
574 /* No arguments, no prefix -> no pathspec */
575 if (!entry && !prefix)
576 return;
577
fc12261f
NTND
578 if ((flags & PATHSPEC_PREFER_CWD) &&
579 (flags & PATHSPEC_PREFER_FULL))
033abf97 580 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
fc12261f 581
87323bda
NTND
582 /* No arguments with prefix -> prefix pathspec */
583 if (!entry) {
fc12261f
NTND
584 if (flags & PATHSPEC_PREFER_FULL)
585 return;
586
587 if (!(flags & PATHSPEC_PREFER_CWD))
033abf97 588 BUG("PATHSPEC_PREFER_CWD requires arguments");
fc12261f 589
ca56dadb 590 pathspec->items = CALLOC_ARRAY(item, 1);
8aee769f
BW
591 item->match = xstrdup(prefix);
592 item->original = xstrdup(prefix);
87323bda 593 item->nowildcard_len = item->len = strlen(prefix);
645a29c4 594 item->prefix = item->len;
87323bda 595 pathspec->nr = 1;
87323bda 596 return;
512aaf94 597 }
87323bda
NTND
598
599 n = 0;
d426430e 600 while (argv[n]) {
9e4e8a64
EX
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");
87323bda 604 n++;
d426430e 605 }
87323bda
NTND
606
607 pathspec->nr = n;
859b7f1d 608 ALLOC_ARRAY(pathspec->items, n + 1);
b32fa95f 609 item = pathspec->items;
87323bda
NTND
610 prefixlen = prefix ? strlen(prefix) : 0;
611
612 for (i = 0; i < n; i++) {
87323bda
NTND
613 entry = argv[i];
614
27ec4282 615 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
db7e8598 616
ef79b1f8
NTND
617 if (item[i].magic & PATHSPEC_EXCLUDE)
618 nr_exclude++;
87323bda 619 if (item[i].magic & magic_mask)
2aee5849 620 unsupported_magic(entry, item[i].magic & magic_mask);
87450244
NTND
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
87323bda
NTND
627 if (item[i].nowildcard_len < item[i].len)
628 pathspec->has_wildcard = 1;
629 pathspec->magic |= item[i].magic;
630 }
631
859b7f1d
LT
632 /*
633 * If everything is an exclude pattern, add one positive pattern
64127575 634 * that matches everything. We allocated an extra one for this.
859b7f1d
LT
635 */
636 if (nr_exclude == n) {
637 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
b02fdbc8 638 init_pathspec_item(item + n, 0, prefix, plen, ".");
859b7f1d
LT
639 pathspec->nr++;
640 }
931eab64
NTND
641
642 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
643 if (flags & PATHSPEC_KEEP_ORDER)
033abf97 644 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
9ed0d8d6 645 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
931eab64 646 }
64acde94
NTND
647}
648
24e4750c
AM
649void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
650 unsigned flags, const char *prefix,
651 const char *file, int nul_term_line)
652{
c972bf4c 653 struct strvec parsed_file = STRVEC_INIT;
24e4750c
AM
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 }
c972bf4c 672 strvec_push(&parsed_file, buf.buf);
24e4750c
AM
673 strbuf_reset(&buf);
674 }
675
676 strbuf_release(&unquoted);
677 strbuf_release(&buf);
678 if (in != stdin)
679 fclose(in);
680
d70a9eb6 681 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
c972bf4c 682 strvec_clear(&parsed_file);
24e4750c
AM
683}
684
e4d92cdc
NTND
685void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
686{
b0db7046 687 int i, j;
8aee769f 688
e4d92cdc 689 *dst = *src;
6e578410 690 DUP_ARRAY(dst->items, src->items, dst->nr);
8aee769f
BW
691
692 for (i = 0; i < dst->nr; i++) {
b0db7046
BW
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
6e578410 699 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
b0db7046
BW
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);
8aee769f 706 }
e4d92cdc 707}
9a087274 708
ed6e8038 709void clear_pathspec(struct pathspec *pathspec)
9a087274 710{
b0db7046 711 int i, j;
8aee769f
BW
712
713 for (i = 0; i < pathspec->nr; i++) {
714 free(pathspec->items[i].match);
715 free(pathspec->items[i].original);
b0db7046 716
5ce10c0a 717 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
b0db7046
BW
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);
8aee769f 723 }
b0db7046 724
6a83d902 725 FREE_AND_NULL(pathspec->items);
8aee769f 726 pathspec->nr = 0;
512aaf94 727}
22af33be 728
847a9e5d 729int match_pathspec_attrs(struct index_state *istate,
22af33be
NTND
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
44451a2e 739 git_check_attr(istate, name, item->attr_check);
22af33be
NTND
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}
b29ad383
SY
766
767int 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}