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