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