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