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