]> git.ipfire.org Git - thirdparty/git.git/blame - pathspec.c
Merge branch 'la/doc-interpret-trailers'
[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
8e32caaa 534void pathspec_magic_names(unsigned magic, struct strbuf *out)
87323bda 535{
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;
8e32caaa
JK
541 if (out->len)
542 strbuf_addstr(out, ", ");
2aee5849
BW
543
544 if (m->mnemonic)
8e32caaa 545 strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
2aee5849 546 m->name, m->mnemonic);
87323bda 547 else
8e32caaa 548 strbuf_addf(out, "'%s'", m->name);
87323bda 549 }
8e32caaa
JK
550}
551
552static void NORETURN unsupported_magic(const char *pattern,
553 unsigned magic)
554{
555 struct strbuf sb = STRBUF_INIT;
556 pathspec_magic_names(magic, &sb);
87323bda
NTND
557 /*
558 * We may want to substitute "this command" with a command
5a7d41d8 559 * name. E.g. when "git add -p" or "git add -i" dies when running
87323bda
NTND
560 * "checkout -p"
561 */
562 die(_("%s: pathspec magic not supported by this command: %s"),
563 pattern, sb.buf);
9d67b61f 564}
512aaf94 565
87323bda
NTND
566void parse_pathspec(struct pathspec *pathspec,
567 unsigned magic_mask, unsigned flags,
568 const char *prefix, const char **argv)
512aaf94 569{
87323bda
NTND
570 struct pathspec_item *item;
571 const char *entry = argv ? *argv : NULL;
9e4e8a64 572 int i, n, prefixlen, nr_exclude = 0;
87323bda
NTND
573
574 memset(pathspec, 0, sizeof(*pathspec));
575
6330a171
NTND
576 if (flags & PATHSPEC_MAXDEPTH_VALID)
577 pathspec->magic |= PATHSPEC_MAXDEPTH;
578
87323bda
NTND
579 /* No arguments, no prefix -> no pathspec */
580 if (!entry && !prefix)
581 return;
582
fc12261f
NTND
583 if ((flags & PATHSPEC_PREFER_CWD) &&
584 (flags & PATHSPEC_PREFER_FULL))
033abf97 585 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
fc12261f 586
87323bda
NTND
587 /* No arguments with prefix -> prefix pathspec */
588 if (!entry) {
fc12261f
NTND
589 if (flags & PATHSPEC_PREFER_FULL)
590 return;
591
592 if (!(flags & PATHSPEC_PREFER_CWD))
033abf97 593 BUG("PATHSPEC_PREFER_CWD requires arguments");
fc12261f 594
ca56dadb 595 pathspec->items = CALLOC_ARRAY(item, 1);
8aee769f
BW
596 item->match = xstrdup(prefix);
597 item->original = xstrdup(prefix);
87323bda 598 item->nowildcard_len = item->len = strlen(prefix);
645a29c4 599 item->prefix = item->len;
87323bda 600 pathspec->nr = 1;
87323bda 601 return;
512aaf94 602 }
87323bda
NTND
603
604 n = 0;
d426430e 605 while (argv[n]) {
9e4e8a64
EX
606 if (*argv[n] == '\0')
607 die("empty string is not a valid pathspec. "
608 "please use . instead if you meant to match all paths");
87323bda 609 n++;
d426430e 610 }
87323bda
NTND
611
612 pathspec->nr = n;
859b7f1d 613 ALLOC_ARRAY(pathspec->items, n + 1);
b32fa95f 614 item = pathspec->items;
87323bda
NTND
615 prefixlen = prefix ? strlen(prefix) : 0;
616
617 for (i = 0; i < n; i++) {
87323bda
NTND
618 entry = argv[i];
619
27ec4282 620 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
db7e8598 621
ef79b1f8
NTND
622 if (item[i].magic & PATHSPEC_EXCLUDE)
623 nr_exclude++;
87323bda 624 if (item[i].magic & magic_mask)
2aee5849 625 unsupported_magic(entry, item[i].magic & magic_mask);
87450244
NTND
626
627 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
628 has_symlink_leading_path(item[i].match, item[i].len)) {
629 die(_("pathspec '%s' is beyond a symbolic link"), entry);
630 }
631
87323bda
NTND
632 if (item[i].nowildcard_len < item[i].len)
633 pathspec->has_wildcard = 1;
634 pathspec->magic |= item[i].magic;
635 }
636
859b7f1d
LT
637 /*
638 * If everything is an exclude pattern, add one positive pattern
64127575 639 * that matches everything. We allocated an extra one for this.
859b7f1d
LT
640 */
641 if (nr_exclude == n) {
642 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
b02fdbc8 643 init_pathspec_item(item + n, 0, prefix, plen, ".");
859b7f1d
LT
644 pathspec->nr++;
645 }
931eab64
NTND
646
647 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
648 if (flags & PATHSPEC_KEEP_ORDER)
033abf97 649 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
9ed0d8d6 650 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
931eab64 651 }
64acde94
NTND
652}
653
24e4750c
AM
654void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
655 unsigned flags, const char *prefix,
656 const char *file, int nul_term_line)
657{
c972bf4c 658 struct strvec parsed_file = STRVEC_INIT;
24e4750c
AM
659 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
660 strbuf_getline;
661 struct strbuf buf = STRBUF_INIT;
662 struct strbuf unquoted = STRBUF_INIT;
663 FILE *in;
664
665 if (!strcmp(file, "-"))
666 in = stdin;
667 else
668 in = xfopen(file, "r");
669
670 while (getline_fn(&buf, in) != EOF) {
671 if (!nul_term_line && buf.buf[0] == '"') {
672 strbuf_reset(&unquoted);
673 if (unquote_c_style(&unquoted, buf.buf, NULL))
674 die(_("line is badly quoted: %s"), buf.buf);
675 strbuf_swap(&buf, &unquoted);
676 }
c972bf4c 677 strvec_push(&parsed_file, buf.buf);
24e4750c
AM
678 strbuf_reset(&buf);
679 }
680
681 strbuf_release(&unquoted);
682 strbuf_release(&buf);
683 if (in != stdin)
684 fclose(in);
685
d70a9eb6 686 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
c972bf4c 687 strvec_clear(&parsed_file);
24e4750c
AM
688}
689
e4d92cdc
NTND
690void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
691{
b0db7046 692 int i, j;
8aee769f 693
e4d92cdc 694 *dst = *src;
6e578410 695 DUP_ARRAY(dst->items, src->items, dst->nr);
8aee769f
BW
696
697 for (i = 0; i < dst->nr; i++) {
b0db7046
BW
698 struct pathspec_item *d = &dst->items[i];
699 struct pathspec_item *s = &src->items[i];
700
701 d->match = xstrdup(s->match);
702 d->original = xstrdup(s->original);
703
6e578410 704 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
b0db7046
BW
705 for (j = 0; j < d->attr_match_nr; j++) {
706 const char *value = s->attr_match[j].value;
707 d->attr_match[j].value = xstrdup_or_null(value);
708 }
709
710 d->attr_check = attr_check_dup(s->attr_check);
8aee769f 711 }
e4d92cdc 712}
9a087274 713
ed6e8038 714void clear_pathspec(struct pathspec *pathspec)
9a087274 715{
b0db7046 716 int i, j;
8aee769f
BW
717
718 for (i = 0; i < pathspec->nr; i++) {
719 free(pathspec->items[i].match);
720 free(pathspec->items[i].original);
b0db7046 721
5ce10c0a 722 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
b0db7046
BW
723 free(pathspec->items[i].attr_match[j].value);
724 free(pathspec->items[i].attr_match);
725
726 if (pathspec->items[i].attr_check)
727 attr_check_free(pathspec->items[i].attr_check);
8aee769f 728 }
b0db7046 729
6a83d902 730 FREE_AND_NULL(pathspec->items);
8aee769f 731 pathspec->nr = 0;
512aaf94 732}
22af33be 733
847a9e5d 734int match_pathspec_attrs(struct index_state *istate,
22af33be
NTND
735 const char *name, int namelen,
736 const struct pathspec_item *item)
737{
738 int i;
739 char *to_free = NULL;
740
741 if (name[namelen])
742 name = to_free = xmemdupz(name, namelen);
743
44451a2e 744 git_check_attr(istate, name, item->attr_check);
22af33be
NTND
745
746 free(to_free);
747
748 for (i = 0; i < item->attr_match_nr; i++) {
749 const char *value;
750 int matched;
751 enum attr_match_mode match_mode;
752
753 value = item->attr_check->items[i].value;
754 match_mode = item->attr_match[i].match_mode;
755
756 if (ATTR_TRUE(value))
757 matched = (match_mode == MATCH_SET);
758 else if (ATTR_FALSE(value))
759 matched = (match_mode == MATCH_UNSET);
760 else if (ATTR_UNSET(value))
761 matched = (match_mode == MATCH_UNSPECIFIED);
762 else
763 matched = (match_mode == MATCH_VALUE &&
764 !strcmp(item->attr_match[i].value, value));
765 if (!matched)
766 return 0;
767 }
768
769 return 1;
770}
b29ad383
SY
771
772int pathspec_needs_expanded_index(struct index_state *istate,
773 const struct pathspec *pathspec)
774{
775 unsigned int i, pos;
776 int res = 0;
777 char *skip_worktree_seen = NULL;
778
779 /*
780 * If index is not sparse, no index expansion is needed.
781 */
782 if (!istate->sparse_index)
783 return 0;
784
785 /*
786 * When using a magic pathspec, assume for the sake of simplicity that
787 * the index needs to be expanded to match all matchable files.
788 */
789 if (pathspec->magic)
790 return 1;
791
792 for (i = 0; i < pathspec->nr; i++) {
793 struct pathspec_item item = pathspec->items[i];
794
795 /*
796 * If the pathspec item has a wildcard, the index should be expanded
797 * if the pathspec has the possibility of matching a subset of entries inside
798 * of a sparse directory (but not the entire directory).
799 *
800 * If the pathspec item is a literal path, the index only needs to be expanded
801 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
802 * expand for in-cone files) and b) it doesn't match any sparse directories
803 * (since we can reset whole sparse directories without expanding them).
804 */
805 if (item.nowildcard_len < item.len) {
806 /*
807 * Special case: if the pattern is a path inside the cone
808 * followed by only wildcards, the pattern cannot match
809 * partial sparse directories, so we know we don't need to
810 * expand the index.
811 *
812 * Examples:
813 * - in-cone/foo***: doesn't need expanded index
814 * - not-in-cone/bar*: may need expanded index
815 * - **.c: may need expanded index
816 */
817 if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
818 path_in_cone_mode_sparse_checkout(item.original, istate))
819 continue;
820
821 for (pos = 0; pos < istate->cache_nr; pos++) {
822 struct cache_entry *ce = istate->cache[pos];
823
824 if (!S_ISSPARSEDIR(ce->ce_mode))
825 continue;
826
827 /*
828 * If the pre-wildcard length is longer than the sparse
829 * directory name and the sparse directory is the first
830 * component of the pathspec, need to expand the index.
831 */
832 if (item.nowildcard_len > ce_namelen(ce) &&
833 !strncmp(item.original, ce->name, ce_namelen(ce))) {
834 res = 1;
835 break;
836 }
837
838 /*
839 * If the pre-wildcard length is shorter than the sparse
840 * directory and the pathspec does not match the whole
841 * directory, need to expand the index.
842 */
843 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
844 wildmatch(item.original, ce->name, 0)) {
845 res = 1;
846 break;
847 }
848 }
849 } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
850 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
851 res = 1;
852
853 if (res > 0)
854 break;
855 }
856
857 free(skip_worktree_seen);
858 return res;
859}