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