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