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