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