]>
Commit | Line | Data |
---|---|---|
6f525e71 AS |
1 | #include "cache.h" |
2 | #include "dir.h" | |
3 | #include "pathspec.h" | |
4 | ||
5 | /* | |
6 | * Finds which of the given pathspecs match items in the index. | |
7 | * | |
8 | * For each pathspec, sets the corresponding entry in the seen[] array | |
9 | * (which should be specs items long, i.e. the same size as pathspec) | |
10 | * to the nature of the "closest" (i.e. most specific) match found for | |
11 | * that pathspec in the index, if it was a closer type of match than | |
12 | * the existing entry. As an optimization, matching is skipped | |
13 | * altogether if seen[] already only contains non-zero entries. | |
14 | * | |
15 | * If seen[] has not already been written to, it may make sense | |
4b78d7bc | 16 | * to use find_pathspecs_matching_against_index() instead. |
6f525e71 | 17 | */ |
84b8b5d1 NTND |
18 | void add_pathspec_matches_against_index(const struct pathspec *pathspec, |
19 | char *seen) | |
6f525e71 AS |
20 | { |
21 | int num_unmatched = 0, i; | |
22 | ||
23 | /* | |
24 | * Since we are walking the index as if we were walking the directory, | |
25 | * we have to mark the matched pathspec as seen; otherwise we will | |
26 | * mistakenly think that the user gave a pathspec that did not match | |
27 | * anything. | |
28 | */ | |
84b8b5d1 | 29 | for (i = 0; i < pathspec->nr; i++) |
6f525e71 AS |
30 | if (!seen[i]) |
31 | num_unmatched++; | |
32 | if (!num_unmatched) | |
33 | return; | |
34 | for (i = 0; i < active_nr; i++) { | |
35 | struct cache_entry *ce = active_cache[i]; | |
84b8b5d1 | 36 | match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, seen); |
6f525e71 AS |
37 | } |
38 | } | |
39 | ||
40 | /* | |
41 | * Finds which of the given pathspecs match items in the index. | |
42 | * | |
4b78d7bc AS |
43 | * This is a one-shot wrapper around add_pathspec_matches_against_index() |
44 | * which allocates, populates, and returns a seen[] array indicating the | |
45 | * nature of the "closest" (i.e. most specific) matches which each of the | |
46 | * given pathspecs achieves against all items in the index. | |
6f525e71 | 47 | */ |
84b8b5d1 | 48 | char *find_pathspecs_matching_against_index(const struct pathspec *pathspec) |
6f525e71 | 49 | { |
84b8b5d1 NTND |
50 | char *seen = xcalloc(pathspec->nr, 1); |
51 | add_pathspec_matches_against_index(pathspec, seen); | |
6f525e71 AS |
52 | return seen; |
53 | } | |
9d67b61f | 54 | |
64acde94 NTND |
55 | /* |
56 | * Magic pathspec | |
57 | * | |
64acde94 NTND |
58 | * Possible future magic semantics include stuff like: |
59 | * | |
60 | * { PATHSPEC_NOGLOB, '!', "noglob" }, | |
61 | * { PATHSPEC_ICASE, '\0', "icase" }, | |
62 | * { PATHSPEC_RECURSIVE, '*', "recursive" }, | |
63 | * { PATHSPEC_REGEXP, '\0', "regexp" }, | |
64 | * | |
65 | */ | |
64acde94 NTND |
66 | |
67 | static struct pathspec_magic { | |
68 | unsigned bit; | |
69 | char mnemonic; /* this cannot be ':'! */ | |
70 | const char *name; | |
71 | } pathspec_magic[] = { | |
72 | { PATHSPEC_FROMTOP, '/', "top" }, | |
5c6933d2 | 73 | { PATHSPEC_LITERAL, 0, "literal" }, |
64acde94 NTND |
74 | }; |
75 | ||
76 | /* | |
77 | * Take an element of a pathspec and check for magic signatures. | |
87323bda | 78 | * Append the result to the prefix. Return the magic bitmap. |
64acde94 NTND |
79 | * |
80 | * For now, we only parse the syntax and throw out anything other than | |
81 | * "top" magic. | |
82 | * | |
83 | * NEEDSWORK: This needs to be rewritten when we start migrating | |
84 | * get_pathspec() users to use the "struct pathspec" interface. For | |
85 | * example, a pathspec element may be marked as case-insensitive, but | |
86 | * the prefix part must always match literally, and a single stupid | |
87 | * string cannot express such a case. | |
88 | */ | |
87323bda NTND |
89 | static unsigned prefix_pathspec(struct pathspec_item *item, |
90 | unsigned *p_short_magic, | |
91 | const char **raw, unsigned flags, | |
92 | const char *prefix, int prefixlen, | |
93 | const char *elt) | |
64acde94 | 94 | { |
341003e7 | 95 | static int literal_global = -1; |
5c6933d2 | 96 | unsigned magic = 0, short_magic = 0, global_magic = 0; |
233c3e6c | 97 | const char *copyfrom = elt, *long_magic_end = NULL; |
87323bda | 98 | char *match; |
233c3e6c | 99 | int i, pathspec_prefix = -1; |
64acde94 | 100 | |
341003e7 NTND |
101 | if (literal_global < 0) |
102 | literal_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0); | |
5c6933d2 NTND |
103 | if (literal_global) |
104 | global_magic |= PATHSPEC_LITERAL; | |
341003e7 | 105 | |
a16bf9dd | 106 | if (elt[0] != ':' || literal_global) { |
64acde94 NTND |
107 | ; /* nothing to do */ |
108 | } else if (elt[1] == '(') { | |
109 | /* longhand */ | |
110 | const char *nextat; | |
111 | for (copyfrom = elt + 2; | |
112 | *copyfrom && *copyfrom != ')'; | |
113 | copyfrom = nextat) { | |
114 | size_t len = strcspn(copyfrom, ",)"); | |
115 | if (copyfrom[len] == ',') | |
116 | nextat = copyfrom + len + 1; | |
117 | else | |
118 | /* handle ')' and '\0' */ | |
119 | nextat = copyfrom + len; | |
120 | if (!len) | |
121 | continue; | |
233c3e6c | 122 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
64acde94 NTND |
123 | if (strlen(pathspec_magic[i].name) == len && |
124 | !strncmp(pathspec_magic[i].name, copyfrom, len)) { | |
125 | magic |= pathspec_magic[i].bit; | |
126 | break; | |
127 | } | |
233c3e6c NTND |
128 | if (!prefixcmp(copyfrom, "prefix:")) { |
129 | char *endptr; | |
130 | pathspec_prefix = strtol(copyfrom + 7, | |
131 | &endptr, 10); | |
132 | if (endptr - copyfrom != len) | |
133 | die(_("invalid parameter for pathspec magic 'prefix'")); | |
134 | /* "i" would be wrong, but it does not matter */ | |
135 | break; | |
136 | } | |
137 | } | |
64acde94 | 138 | if (ARRAY_SIZE(pathspec_magic) <= i) |
f01d9820 | 139 | die(_("Invalid pathspec magic '%.*s' in '%s'"), |
64acde94 NTND |
140 | (int) len, copyfrom, elt); |
141 | } | |
142 | if (*copyfrom != ')') | |
f01d9820 | 143 | die(_("Missing ')' at the end of pathspec magic in '%s'"), elt); |
233c3e6c | 144 | long_magic_end = copyfrom; |
64acde94 NTND |
145 | copyfrom++; |
146 | } else { | |
147 | /* shorthand */ | |
148 | for (copyfrom = elt + 1; | |
149 | *copyfrom && *copyfrom != ':'; | |
150 | copyfrom++) { | |
151 | char ch = *copyfrom; | |
152 | ||
153 | if (!is_pathspec_magic(ch)) | |
154 | break; | |
155 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) | |
156 | if (pathspec_magic[i].mnemonic == ch) { | |
87323bda | 157 | short_magic |= pathspec_magic[i].bit; |
64acde94 NTND |
158 | break; |
159 | } | |
160 | if (ARRAY_SIZE(pathspec_magic) <= i) | |
f01d9820 | 161 | die(_("Unimplemented pathspec magic '%c' in '%s'"), |
64acde94 NTND |
162 | ch, elt); |
163 | } | |
164 | if (*copyfrom == ':') | |
165 | copyfrom++; | |
166 | } | |
167 | ||
87323bda NTND |
168 | magic |= short_magic; |
169 | *p_short_magic = short_magic; | |
5c6933d2 | 170 | magic |= global_magic; |
87323bda | 171 | |
233c3e6c NTND |
172 | if (pathspec_prefix >= 0 && |
173 | (prefixlen || (prefix && *prefix))) | |
174 | die("BUG: 'prefix' magic is supposed to be used at worktree's root"); | |
175 | ||
176 | if (pathspec_prefix >= 0) { | |
177 | match = xstrdup(copyfrom); | |
178 | prefixlen = pathspec_prefix; | |
179 | } else if (magic & PATHSPEC_FROMTOP) { | |
87323bda | 180 | match = xstrdup(copyfrom); |
645a29c4 NTND |
181 | prefixlen = 0; |
182 | } else { | |
183 | match = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom); | |
184 | if (!match) | |
185 | die(_("%s: '%s' is outside repository"), elt, copyfrom); | |
186 | } | |
87323bda | 187 | *raw = item->match = match; |
dad2586a NTND |
188 | /* |
189 | * Prefix the pathspec (keep all magic) and assign to | |
190 | * original. Useful for passing to another command. | |
191 | */ | |
192 | if (flags & PATHSPEC_PREFIX_ORIGIN) { | |
193 | struct strbuf sb = STRBUF_INIT; | |
233c3e6c | 194 | const char *start = elt; |
341003e7 | 195 | if (prefixlen && !literal_global) { |
233c3e6c NTND |
196 | /* Preserve the actual prefix length of each pattern */ |
197 | if (long_magic_end) { | |
198 | strbuf_add(&sb, start, long_magic_end - start); | |
199 | strbuf_addf(&sb, ",prefix:%d", prefixlen); | |
200 | start = long_magic_end; | |
201 | } else { | |
202 | if (*start == ':') | |
203 | start++; | |
204 | strbuf_addf(&sb, ":(prefix:%d)", prefixlen); | |
205 | } | |
206 | } | |
207 | strbuf_add(&sb, start, copyfrom - start); | |
dad2586a NTND |
208 | strbuf_addstr(&sb, match); |
209 | item->original = strbuf_detach(&sb, NULL); | |
210 | } else | |
211 | item->original = elt; | |
87323bda | 212 | item->len = strlen(item->match); |
645a29c4 | 213 | item->prefix = prefixlen; |
b69bb3fc NTND |
214 | |
215 | if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) && | |
216 | (item->len >= 1 && item->match[item->len - 1] == '/') && | |
217 | (i = cache_name_pos(item->match, item->len - 1)) >= 0 && | |
218 | S_ISGITLINK(active_cache[i]->ce_mode)) { | |
219 | item->len--; | |
220 | match[item->len] = '\0'; | |
221 | } | |
222 | ||
87450244 NTND |
223 | if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE) |
224 | for (i = 0; i < active_nr; i++) { | |
225 | struct cache_entry *ce = active_cache[i]; | |
226 | int ce_len = ce_namelen(ce); | |
227 | ||
228 | if (!S_ISGITLINK(ce->ce_mode)) | |
229 | continue; | |
230 | ||
231 | if (item->len <= ce_len || match[ce_len] != '/' || | |
232 | memcmp(ce->name, match, ce_len)) | |
233 | continue; | |
234 | if (item->len == ce_len + 1) { | |
235 | /* strip trailing slash */ | |
236 | item->len--; | |
237 | match[item->len] = '\0'; | |
238 | } else | |
239 | die (_("Pathspec '%s' is in submodule '%.*s'"), | |
240 | elt, ce_len, ce->name); | |
241 | } | |
242 | ||
5c6933d2 | 243 | if (magic & PATHSPEC_LITERAL) |
87323bda | 244 | item->nowildcard_len = item->len; |
645a29c4 | 245 | else { |
87323bda | 246 | item->nowildcard_len = simple_length(item->match); |
645a29c4 NTND |
247 | if (item->nowildcard_len < prefixlen) |
248 | item->nowildcard_len = prefixlen; | |
249 | } | |
87323bda NTND |
250 | item->flags = 0; |
251 | if (item->nowildcard_len < item->len && | |
252 | item->match[item->nowildcard_len] == '*' && | |
253 | no_wildcard(item->match + item->nowildcard_len + 1)) | |
254 | item->flags |= PATHSPEC_ONESTAR; | |
645a29c4 NTND |
255 | |
256 | /* sanity checks, pathspec matchers assume these are sane */ | |
257 | assert(item->nowildcard_len <= item->len && | |
258 | item->prefix <= item->len); | |
87323bda NTND |
259 | return magic; |
260 | } | |
261 | ||
262 | static int pathspec_item_cmp(const void *a_, const void *b_) | |
263 | { | |
264 | struct pathspec_item *a, *b; | |
265 | ||
266 | a = (struct pathspec_item *)a_; | |
267 | b = (struct pathspec_item *)b_; | |
268 | return strcmp(a->match, b->match); | |
269 | } | |
270 | ||
271 | static void NORETURN unsupported_magic(const char *pattern, | |
272 | unsigned magic, | |
273 | unsigned short_magic) | |
274 | { | |
275 | struct strbuf sb = STRBUF_INIT; | |
276 | int i, n; | |
277 | for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { | |
278 | const struct pathspec_magic *m = pathspec_magic + i; | |
279 | if (!(magic & m->bit)) | |
280 | continue; | |
281 | if (sb.len) | |
282 | strbuf_addstr(&sb, " "); | |
283 | if (short_magic & m->bit) | |
284 | strbuf_addf(&sb, "'%c'", m->mnemonic); | |
285 | else | |
286 | strbuf_addf(&sb, "'%s'", m->name); | |
287 | n++; | |
288 | } | |
289 | /* | |
290 | * We may want to substitute "this command" with a command | |
291 | * name. E.g. when add--interactive dies when running | |
292 | * "checkout -p" | |
293 | */ | |
294 | die(_("%s: pathspec magic not supported by this command: %s"), | |
295 | pattern, sb.buf); | |
296 | } | |
297 | ||
298 | /* | |
299 | * Given command line arguments and a prefix, convert the input to | |
300 | * pathspec. die() if any magic in magic_mask is used. | |
301 | */ | |
302 | void parse_pathspec(struct pathspec *pathspec, | |
303 | unsigned magic_mask, unsigned flags, | |
304 | const char *prefix, const char **argv) | |
305 | { | |
306 | struct pathspec_item *item; | |
307 | const char *entry = argv ? *argv : NULL; | |
308 | int i, n, prefixlen; | |
309 | ||
310 | memset(pathspec, 0, sizeof(*pathspec)); | |
311 | ||
6330a171 NTND |
312 | if (flags & PATHSPEC_MAXDEPTH_VALID) |
313 | pathspec->magic |= PATHSPEC_MAXDEPTH; | |
314 | ||
87323bda NTND |
315 | /* No arguments, no prefix -> no pathspec */ |
316 | if (!entry && !prefix) | |
317 | return; | |
318 | ||
fc12261f NTND |
319 | if ((flags & PATHSPEC_PREFER_CWD) && |
320 | (flags & PATHSPEC_PREFER_FULL)) | |
321 | die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible"); | |
322 | ||
87323bda NTND |
323 | /* No arguments with prefix -> prefix pathspec */ |
324 | if (!entry) { | |
325 | static const char *raw[2]; | |
326 | ||
fc12261f NTND |
327 | if (flags & PATHSPEC_PREFER_FULL) |
328 | return; | |
329 | ||
330 | if (!(flags & PATHSPEC_PREFER_CWD)) | |
331 | die("BUG: PATHSPEC_PREFER_CWD requires arguments"); | |
332 | ||
87323bda NTND |
333 | pathspec->items = item = xmalloc(sizeof(*item)); |
334 | memset(item, 0, sizeof(*item)); | |
335 | item->match = prefix; | |
d2ce1331 | 336 | item->original = prefix; |
87323bda | 337 | item->nowildcard_len = item->len = strlen(prefix); |
645a29c4 | 338 | item->prefix = item->len; |
87323bda NTND |
339 | raw[0] = prefix; |
340 | raw[1] = NULL; | |
341 | pathspec->nr = 1; | |
b3920bbd | 342 | pathspec->_raw = raw; |
87323bda NTND |
343 | return; |
344 | } | |
345 | ||
346 | n = 0; | |
347 | while (argv[n]) | |
348 | n++; | |
349 | ||
350 | pathspec->nr = n; | |
351 | pathspec->items = item = xmalloc(sizeof(*item) * n); | |
b3920bbd | 352 | pathspec->_raw = argv; |
87323bda NTND |
353 | prefixlen = prefix ? strlen(prefix) : 0; |
354 | ||
355 | for (i = 0; i < n; i++) { | |
356 | unsigned short_magic; | |
357 | entry = argv[i]; | |
358 | ||
359 | item[i].magic = prefix_pathspec(item + i, &short_magic, | |
360 | argv + i, flags, | |
361 | prefix, prefixlen, entry); | |
362 | if (item[i].magic & magic_mask) | |
363 | unsupported_magic(entry, | |
364 | item[i].magic & magic_mask, | |
365 | short_magic); | |
87450244 NTND |
366 | |
367 | if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) && | |
368 | has_symlink_leading_path(item[i].match, item[i].len)) { | |
369 | die(_("pathspec '%s' is beyond a symbolic link"), entry); | |
370 | } | |
371 | ||
87323bda NTND |
372 | if (item[i].nowildcard_len < item[i].len) |
373 | pathspec->has_wildcard = 1; | |
374 | pathspec->magic |= item[i].magic; | |
375 | } | |
376 | ||
931eab64 NTND |
377 | |
378 | if (pathspec->magic & PATHSPEC_MAXDEPTH) { | |
379 | if (flags & PATHSPEC_KEEP_ORDER) | |
380 | die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible"); | |
6330a171 NTND |
381 | qsort(pathspec->items, pathspec->nr, |
382 | sizeof(struct pathspec_item), pathspec_item_cmp); | |
931eab64 | 383 | } |
64acde94 NTND |
384 | } |
385 | ||
386 | /* | |
387 | * N.B. get_pathspec() is deprecated in favor of the "struct pathspec" | |
87323bda | 388 | * based interface - see pathspec.c:parse_pathspec(). |
64acde94 NTND |
389 | * |
390 | * Arguments: | |
391 | * - prefix - a path relative to the root of the working tree | |
392 | * - pathspec - a list of paths underneath the prefix path | |
393 | * | |
394 | * Iterates over pathspec, prepending each path with prefix, | |
395 | * and return the resulting list. | |
396 | * | |
397 | * If pathspec is empty, return a singleton list containing prefix. | |
398 | * | |
399 | * If pathspec and prefix are both empty, return an empty list. | |
400 | * | |
401 | * This is typically used by built-in commands such as add.c, in order | |
402 | * to normalize argv arguments provided to the built-in into a list of | |
403 | * paths to process, all relative to the root of the working tree. | |
404 | */ | |
405 | const char **get_pathspec(const char *prefix, const char **pathspec) | |
406 | { | |
87323bda NTND |
407 | struct pathspec ps; |
408 | parse_pathspec(&ps, | |
5c6933d2 NTND |
409 | PATHSPEC_ALL_MAGIC & |
410 | ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL), | |
fc12261f NTND |
411 | PATHSPEC_PREFER_CWD, |
412 | prefix, pathspec); | |
b3920bbd | 413 | return ps._raw; |
64acde94 | 414 | } |
e4d92cdc NTND |
415 | |
416 | void copy_pathspec(struct pathspec *dst, const struct pathspec *src) | |
417 | { | |
418 | *dst = *src; | |
419 | dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr); | |
420 | memcpy(dst->items, src->items, | |
421 | sizeof(struct pathspec_item) * dst->nr); | |
422 | } | |
9a087274 NTND |
423 | |
424 | void free_pathspec(struct pathspec *pathspec) | |
425 | { | |
426 | free(pathspec->items); | |
427 | pathspec->items = NULL; | |
428 | } |