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