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