b8faa8f4653bfb04d4ec56969c1128ae0aee1262
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
16 * to use find_pathspecs_matching_against_index() instead.
17 */
18void add_pathspec_matches_against_index(const struct pathspec *pathspec,
19 char *seen)
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 */
29 for (i = 0; i < pathspec->nr; i++)
30 if (!seen[i])
31 num_unmatched++;
32 if (!num_unmatched)
33 return;
34 for (i = 0; i < active_nr; i++) {
35 const struct cache_entry *ce = active_cache[i];
36 ce_path_match(ce, pathspec, seen);
37 }
38}
39
40/*
41 * Finds which of the given pathspecs match items in the index.
42 *
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.
47 */
48char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
49{
50 char *seen = xcalloc(pathspec->nr, 1);
51 add_pathspec_matches_against_index(pathspec, seen);
52 return seen;
53}
54
55/*
56 * Magic pathspec
57 *
58 * Possible future magic semantics include stuff like:
59 *
60 * { PATHSPEC_RECURSIVE, '*', "recursive" },
61 * { PATHSPEC_REGEXP, '\0', "regexp" },
62 *
63 */
64
65static struct pathspec_magic {
66 unsigned bit;
67 char mnemonic; /* this cannot be ':'! */
68 const char *name;
69} pathspec_magic[] = {
70 { PATHSPEC_FROMTOP, '/', "top" },
71 { PATHSPEC_LITERAL, 0, "literal" },
72 { PATHSPEC_GLOB, '\0', "glob" },
73 { PATHSPEC_ICASE, '\0', "icase" },
74 { PATHSPEC_EXCLUDE, '!', "exclude" },
75};
76
77static void prefix_short_magic(struct strbuf *sb, int prefixlen,
78 unsigned short_magic)
79{
80 int i;
81 strbuf_addstr(sb, ":(");
82 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
83 if (short_magic & pathspec_magic[i].bit) {
84 if (sb->buf[sb->len - 1] != '(')
85 strbuf_addch(sb, ',');
86 strbuf_addstr(sb, pathspec_magic[i].name);
87 }
88 strbuf_addf(sb, ",prefix:%d)", prefixlen);
89}
90
91/*
92 * Take an element of a pathspec and check for magic signatures.
93 * Append the result to the prefix. Return the magic bitmap.
94 *
95 * For now, we only parse the syntax and throw out anything other than
96 * "top" magic.
97 *
98 * NEEDSWORK: This needs to be rewritten when we start migrating
99 * get_pathspec() users to use the "struct pathspec" interface. For
100 * example, a pathspec element may be marked as case-insensitive, but
101 * the prefix part must always match literally, and a single stupid
102 * string cannot express such a case.
103 */
104static unsigned prefix_pathspec(struct pathspec_item *item,
105 unsigned *p_short_magic,
106 unsigned flags,
107 const char *prefix, int prefixlen,
108 const char *elt)
109{
110 static int literal_global = -1;
111 static int glob_global = -1;
112 static int noglob_global = -1;
113 static int icase_global = -1;
114 unsigned magic = 0, short_magic = 0, global_magic = 0;
115 const char *copyfrom = elt, *long_magic_end = NULL;
116 char *match;
117 int i, pathspec_prefix = -1;
118
119 if (literal_global < 0)
120 literal_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
121 if (literal_global)
122 global_magic |= PATHSPEC_LITERAL;
123
124 if (glob_global < 0)
125 glob_global = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
126 if (glob_global)
127 global_magic |= PATHSPEC_GLOB;
128
129 if (noglob_global < 0)
130 noglob_global = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
131
132 if (glob_global && noglob_global)
133 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
134
135
136 if (icase_global < 0)
137 icase_global = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
138 if (icase_global)
139 global_magic |= PATHSPEC_ICASE;
140
141 if ((global_magic & PATHSPEC_LITERAL) &&
142 (global_magic & ~PATHSPEC_LITERAL))
143 die(_("global 'literal' pathspec setting is incompatible "
144 "with all other global pathspec settings"));
145
146 if (flags & PATHSPEC_LITERAL_PATH)
147 global_magic = 0;
148
149 if (elt[0] != ':' || literal_global ||
150 (flags & PATHSPEC_LITERAL_PATH)) {
151 ; /* nothing to do */
152 } else if (elt[1] == '(') {
153 /* longhand */
154 const char *nextat;
155 for (copyfrom = elt + 2;
156 *copyfrom && *copyfrom != ')';
157 copyfrom = nextat) {
158 size_t len = strcspn(copyfrom, ",)");
159 if (copyfrom[len] == ',')
160 nextat = copyfrom + len + 1;
161 else
162 /* handle ')' and '\0' */
163 nextat = copyfrom + len;
164 if (!len)
165 continue;
166 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
167 if (strlen(pathspec_magic[i].name) == len &&
168 !strncmp(pathspec_magic[i].name, copyfrom, len)) {
169 magic |= pathspec_magic[i].bit;
170 break;
171 }
172 if (starts_with(copyfrom, "prefix:")) {
173 char *endptr;
174 pathspec_prefix = strtol(copyfrom + 7,
175 &endptr, 10);
176 if (endptr - copyfrom != len)
177 die(_("invalid parameter for pathspec magic 'prefix'"));
178 /* "i" would be wrong, but it does not matter */
179 break;
180 }
181 }
182 if (ARRAY_SIZE(pathspec_magic) <= i)
183 die(_("Invalid pathspec magic '%.*s' in '%s'"),
184 (int) len, copyfrom, elt);
185 }
186 if (*copyfrom != ')')
187 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
188 long_magic_end = copyfrom;
189 copyfrom++;
190 } else {
191 /* shorthand */
192 for (copyfrom = elt + 1;
193 *copyfrom && *copyfrom != ':';
194 copyfrom++) {
195 char ch = *copyfrom;
196
197 if (!is_pathspec_magic(ch))
198 break;
199 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
200 if (pathspec_magic[i].mnemonic == ch) {
201 short_magic |= pathspec_magic[i].bit;
202 break;
203 }
204 if (ARRAY_SIZE(pathspec_magic) <= i)
205 die(_("Unimplemented pathspec magic '%c' in '%s'"),
206 ch, elt);
207 }
208 if (*copyfrom == ':')
209 copyfrom++;
210 }
211
212 magic |= short_magic;
213 *p_short_magic = short_magic;
214
215 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
216 if (noglob_global && !(magic & PATHSPEC_GLOB))
217 global_magic |= PATHSPEC_LITERAL;
218
219 /* --glob-pathspec is overridden by :(literal) */
220 if ((global_magic & PATHSPEC_GLOB) && (magic & PATHSPEC_LITERAL))
221 global_magic &= ~PATHSPEC_GLOB;
222
223 magic |= global_magic;
224
225 if (pathspec_prefix >= 0 &&
226 (prefixlen || (prefix && *prefix)))
227 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
228
229 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
230 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
231
232 if (pathspec_prefix >= 0) {
233 match = xstrdup(copyfrom);
234 prefixlen = pathspec_prefix;
235 } else if (magic & PATHSPEC_FROMTOP) {
236 match = xstrdup(copyfrom);
237 prefixlen = 0;
238 } else {
239 match = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom);
240 if (!match)
241 die(_("%s: '%s' is outside repository"), elt, copyfrom);
242 }
243 item->match = match;
244 /*
245 * Prefix the pathspec (keep all magic) and assign to
246 * original. Useful for passing to another command.
247 */
248 if (flags & PATHSPEC_PREFIX_ORIGIN) {
249 struct strbuf sb = STRBUF_INIT;
250 if (prefixlen && !literal_global) {
251 /* Preserve the actual prefix length of each pattern */
252 if (short_magic)
253 prefix_short_magic(&sb, prefixlen, short_magic);
254 else if (long_magic_end) {
255 strbuf_add(&sb, elt, long_magic_end - elt);
256 strbuf_addf(&sb, ",prefix:%d)", prefixlen);
257 } else
258 strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
259 }
260 strbuf_addstr(&sb, match);
261 item->original = strbuf_detach(&sb, NULL);
262 } else {
263 item->original = xstrdup(elt);
264 }
265 item->len = strlen(item->match);
266 item->prefix = prefixlen;
267
268 if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
269 (item->len >= 1 && item->match[item->len - 1] == '/') &&
270 (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
271 S_ISGITLINK(active_cache[i]->ce_mode)) {
272 item->len--;
273 match[item->len] = '\0';
274 }
275
276 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
277 for (i = 0; i < active_nr; i++) {
278 struct cache_entry *ce = active_cache[i];
279 int ce_len = ce_namelen(ce);
280
281 if (!S_ISGITLINK(ce->ce_mode))
282 continue;
283
284 if (item->len <= ce_len || match[ce_len] != '/' ||
285 memcmp(ce->name, match, ce_len))
286 continue;
287 if (item->len == ce_len + 1) {
288 /* strip trailing slash */
289 item->len--;
290 match[item->len] = '\0';
291 } else
292 die (_("Pathspec '%s' is in submodule '%.*s'"),
293 elt, ce_len, ce->name);
294 }
295
296 if (magic & PATHSPEC_LITERAL)
297 item->nowildcard_len = item->len;
298 else {
299 item->nowildcard_len = simple_length(item->match);
300 if (item->nowildcard_len < prefixlen)
301 item->nowildcard_len = prefixlen;
302 }
303 item->flags = 0;
304 if (magic & PATHSPEC_GLOB) {
305 /*
306 * FIXME: should we enable ONESTAR in _GLOB for
307 * pattern "* * / * . c"?
308 */
309 } else {
310 if (item->nowildcard_len < item->len &&
311 item->match[item->nowildcard_len] == '*' &&
312 no_wildcard(item->match + item->nowildcard_len + 1))
313 item->flags |= PATHSPEC_ONESTAR;
314 }
315
316 /* sanity checks, pathspec matchers assume these are sane */
317 assert(item->nowildcard_len <= item->len &&
318 item->prefix <= item->len);
319 return magic;
320}
321
322static int pathspec_item_cmp(const void *a_, const void *b_)
323{
324 struct pathspec_item *a, *b;
325
326 a = (struct pathspec_item *)a_;
327 b = (struct pathspec_item *)b_;
328 return strcmp(a->match, b->match);
329}
330
331static void NORETURN unsupported_magic(const char *pattern,
332 unsigned magic,
333 unsigned short_magic)
334{
335 struct strbuf sb = STRBUF_INIT;
336 int i, n;
337 for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
338 const struct pathspec_magic *m = pathspec_magic + i;
339 if (!(magic & m->bit))
340 continue;
341 if (sb.len)
342 strbuf_addch(&sb, ' ');
343 if (short_magic & m->bit)
344 strbuf_addf(&sb, "'%c'", m->mnemonic);
345 else
346 strbuf_addf(&sb, "'%s'", m->name);
347 n++;
348 }
349 /*
350 * We may want to substitute "this command" with a command
351 * name. E.g. when add--interactive dies when running
352 * "checkout -p"
353 */
354 die(_("%s: pathspec magic not supported by this command: %s"),
355 pattern, sb.buf);
356}
357
358/*
359 * Given command line arguments and a prefix, convert the input to
360 * pathspec. die() if any magic in magic_mask is used.
361 */
362void parse_pathspec(struct pathspec *pathspec,
363 unsigned magic_mask, unsigned flags,
364 const char *prefix, const char **argv)
365{
366 struct pathspec_item *item;
367 const char *entry = argv ? *argv : NULL;
368 int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
369
370 memset(pathspec, 0, sizeof(*pathspec));
371
372 if (flags & PATHSPEC_MAXDEPTH_VALID)
373 pathspec->magic |= PATHSPEC_MAXDEPTH;
374
375 /* No arguments, no prefix -> no pathspec */
376 if (!entry && !prefix)
377 return;
378
379 if ((flags & PATHSPEC_PREFER_CWD) &&
380 (flags & PATHSPEC_PREFER_FULL))
381 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
382
383 /* No arguments with prefix -> prefix pathspec */
384 if (!entry) {
385 if (flags & PATHSPEC_PREFER_FULL)
386 return;
387
388 if (!(flags & PATHSPEC_PREFER_CWD))
389 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
390
391 pathspec->items = item = xcalloc(1, sizeof(*item));
392 item->match = xstrdup(prefix);
393 item->original = xstrdup(prefix);
394 item->nowildcard_len = item->len = strlen(prefix);
395 item->prefix = item->len;
396 pathspec->nr = 1;
397 return;
398 }
399
400 n = 0;
401 warn_empty_string = 1;
402 while (argv[n]) {
403 if (*argv[n] == '\0' && warn_empty_string) {
404 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
405 "please use . instead if you meant to match all paths"));
406 warn_empty_string = 0;
407 }
408 n++;
409 }
410
411 pathspec->nr = n;
412 ALLOC_ARRAY(pathspec->items, n);
413 item = pathspec->items;
414 prefixlen = prefix ? strlen(prefix) : 0;
415
416 for (i = 0; i < n; i++) {
417 unsigned short_magic;
418 entry = argv[i];
419
420 item[i].magic = prefix_pathspec(item + i, &short_magic,
421 flags,
422 prefix, prefixlen, entry);
423 if ((flags & PATHSPEC_LITERAL_PATH) &&
424 !(magic_mask & PATHSPEC_LITERAL))
425 item[i].magic |= PATHSPEC_LITERAL;
426 if (item[i].magic & PATHSPEC_EXCLUDE)
427 nr_exclude++;
428 if (item[i].magic & magic_mask)
429 unsupported_magic(entry,
430 item[i].magic & magic_mask,
431 short_magic);
432
433 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
434 has_symlink_leading_path(item[i].match, item[i].len)) {
435 die(_("pathspec '%s' is beyond a symbolic link"), entry);
436 }
437
438 if (item[i].nowildcard_len < item[i].len)
439 pathspec->has_wildcard = 1;
440 pathspec->magic |= item[i].magic;
441 }
442
443 if (nr_exclude == n)
444 die(_("There is nothing to exclude from by :(exclude) patterns.\n"
445 "Perhaps you forgot to add either ':/' or '.' ?"));
446
447
448 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
449 if (flags & PATHSPEC_KEEP_ORDER)
450 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
451 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
452 }
453}
454
455void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
456{
457 int i;
458
459 *dst = *src;
460 ALLOC_ARRAY(dst->items, dst->nr);
461 COPY_ARRAY(dst->items, src->items, dst->nr);
462
463 for (i = 0; i < dst->nr; i++) {
464 dst->items[i].match = xstrdup(src->items[i].match);
465 dst->items[i].original = xstrdup(src->items[i].original);
466 }
467}
468
469void clear_pathspec(struct pathspec *pathspec)
470{
471 int i;
472
473 for (i = 0; i < pathspec->nr; i++) {
474 free(pathspec->items[i].match);
475 free(pathspec->items[i].original);
476 }
477 free(pathspec->items);
478 pathspec->items = NULL;
479 pathspec->nr = 0;
480}