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