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