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