]> git.ipfire.org Git - thirdparty/git.git/blame - pathspec.c
tag doc: reword --[no-]merged to talk about commits, not tips
[thirdparty/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[] = {
4f1bf4d2
BW
70 { PATHSPEC_FROMTOP, '/', "top" },
71 { PATHSPEC_LITERAL, '\0', "literal" },
72 { PATHSPEC_GLOB, '\0', "glob" },
73 { PATHSPEC_ICASE, '\0', "icase" },
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
42ebeb9d
LT
227 /* Special case alias for '!' */
228 if (ch == '^') {
229 *magic |= PATHSPEC_EXCLUDE;
230 continue;
231 }
232
b4bebdce
BW
233 if (!is_pathspec_magic(ch))
234 break;
235
236 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
237 if (pathspec_magic[i].mnemonic == ch) {
238 *magic |= pathspec_magic[i].bit;
239 break;
240 }
241 }
242
243 if (ARRAY_SIZE(pathspec_magic) <= i)
244 die(_("Unimplemented pathspec magic '%c' in '%s'"),
245 ch, elem);
246 }
247
248 if (*pos == ':')
249 pos++;
250
251 return pos;
252}
253
1b6112c5
BW
254static const char *parse_element_magic(unsigned *magic, int *prefix_len,
255 const char *elem)
256{
257 if (elem[0] != ':' || get_literal_global())
258 return elem; /* nothing to do */
259 else if (elem[1] == '(')
260 /* longhand */
261 return parse_long_magic(magic, prefix_len, elem);
262 else
263 /* shorthand */
264 return parse_short_magic(magic, elem);
265}
266
5590215b
BW
267static void strip_submodule_slash_cheap(struct pathspec_item *item)
268{
269 if (item->len >= 1 && item->match[item->len - 1] == '/') {
270 int i = cache_name_pos(item->match, item->len - 1);
271
272 if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
273 item->len--;
274 item->match[item->len] = '\0';
275 }
276 }
277}
278
279static void strip_submodule_slash_expensive(struct pathspec_item *item)
280{
281 int i;
282
283 for (i = 0; i < active_nr; i++) {
284 struct cache_entry *ce = active_cache[i];
285 int ce_len = ce_namelen(ce);
286
287 if (!S_ISGITLINK(ce->ce_mode))
288 continue;
289
290 if (item->len <= ce_len || item->match[ce_len] != '/' ||
291 memcmp(ce->name, item->match, ce_len))
292 continue;
293
294 if (item->len == ce_len + 1) {
295 /* strip trailing slash */
296 item->len--;
297 item->match[item->len] = '\0';
298 } else {
299 die(_("Pathspec '%s' is in submodule '%.*s'"),
300 item->original, ce_len, ce->name);
301 }
302 }
303}
304
2d81c48f
SB
305static void die_inside_submodule_path(struct pathspec_item *item)
306{
307 int i;
308
309 for (i = 0; i < active_nr; i++) {
310 struct cache_entry *ce = active_cache[i];
311 int ce_len = ce_namelen(ce);
312
313 if (!S_ISGITLINK(ce->ce_mode))
314 continue;
315
316 if (item->len < ce_len ||
317 !(item->match[ce_len] == '/' || item->match[ce_len] == '\0') ||
318 memcmp(ce->name, item->match, ce_len))
319 continue;
320
321 die(_("Pathspec '%s' is in submodule '%.*s'"),
322 item->original, ce_len, ce->name);
323 }
324}
325
64acde94 326/*
27ec4282 327 * Perform the initialization of a pathspec_item based on a pathspec element.
9d67b61f 328 */
27ec4282
BW
329static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
330 const char *prefix, int prefixlen,
331 const char *elt)
9d67b61f 332{
db7e8598 333 unsigned magic = 0, element_magic = 0;
5d8f084a 334 const char *copyfrom = elt;
87323bda 335 char *match;
5590215b 336 int pathspec_prefix = -1;
64acde94 337
db7e8598 338 /* PATHSPEC_LITERAL_PATH ignores magic */
1b6112c5 339 if (flags & PATHSPEC_LITERAL_PATH) {
db7e8598 340 magic = PATHSPEC_LITERAL;
1b6112c5
BW
341 } else {
342 copyfrom = parse_element_magic(&element_magic,
343 &pathspec_prefix,
344 elt);
345 magic |= element_magic;
db7e8598 346 magic |= get_global_magic(element_magic);
1b6112c5 347 }
87323bda 348
27ec4282
BW
349 item->magic = magic;
350
233c3e6c
NTND
351 if (pathspec_prefix >= 0 &&
352 (prefixlen || (prefix && *prefix)))
353 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
354
bd30c2e4
NTND
355 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
356 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
357
4f1bf4d2 358 /* Create match string which will be used for pathspec matching */
233c3e6c
NTND
359 if (pathspec_prefix >= 0) {
360 match = xstrdup(copyfrom);
361 prefixlen = pathspec_prefix;
362 } else if (magic & PATHSPEC_FROMTOP) {
87323bda 363 match = xstrdup(copyfrom);
645a29c4
NTND
364 prefixlen = 0;
365 } else {
4f1bf4d2
BW
366 match = prefix_path_gently(prefix, prefixlen,
367 &prefixlen, copyfrom);
645a29c4
NTND
368 if (!match)
369 die(_("%s: '%s' is outside repository"), elt, copyfrom);
370 }
4f1bf4d2 371
34305f77 372 item->match = match;
4f1bf4d2
BW
373 item->len = strlen(item->match);
374 item->prefix = prefixlen;
375
dad2586a
NTND
376 /*
377 * Prefix the pathspec (keep all magic) and assign to
378 * original. Useful for passing to another command.
379 */
5d8f084a 380 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
db7e8598 381 prefixlen && !get_literal_global()) {
dad2586a 382 struct strbuf sb = STRBUF_INIT;
5d8f084a
BW
383
384 /* Preserve the actual prefix length of each pattern */
385 prefix_magic(&sb, prefixlen, element_magic);
386
dad2586a
NTND
387 strbuf_addstr(&sb, match);
388 item->original = strbuf_detach(&sb, NULL);
8aee769f
BW
389 } else {
390 item->original = xstrdup(elt);
391 }
b69bb3fc 392
5590215b
BW
393 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
394 strip_submodule_slash_cheap(item);
b69bb3fc 395
87450244 396 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
5590215b 397 strip_submodule_slash_expensive(item);
87450244 398
4f1bf4d2 399 if (magic & PATHSPEC_LITERAL) {
87323bda 400 item->nowildcard_len = item->len;
4f1bf4d2 401 } else {
87323bda 402 item->nowildcard_len = simple_length(item->match);
645a29c4
NTND
403 if (item->nowildcard_len < prefixlen)
404 item->nowildcard_len = prefixlen;
405 }
4f1bf4d2 406
87323bda 407 item->flags = 0;
bd30c2e4
NTND
408 if (magic & PATHSPEC_GLOB) {
409 /*
410 * FIXME: should we enable ONESTAR in _GLOB for
411 * pattern "* * / * . c"?
412 */
413 } else {
414 if (item->nowildcard_len < item->len &&
415 item->match[item->nowildcard_len] == '*' &&
416 no_wildcard(item->match + item->nowildcard_len + 1))
417 item->flags |= PATHSPEC_ONESTAR;
9d67b61f 418 }
645a29c4
NTND
419
420 /* sanity checks, pathspec matchers assume these are sane */
2d81c48f
SB
421 if (item->nowildcard_len > item->len ||
422 item->prefix > item->len) {
423 /*
424 * This case can be triggered by the user pointing us to a
425 * pathspec inside a submodule, which is an input error.
426 * Detect that here and complain, but fallback in the
427 * non-submodule case to a BUG, as we have no idea what
428 * would trigger that.
429 */
430 die_inside_submodule_path(item);
431 die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
432 }
87323bda
NTND
433}
434
435static int pathspec_item_cmp(const void *a_, const void *b_)
436{
437 struct pathspec_item *a, *b;
438
439 a = (struct pathspec_item *)a_;
440 b = (struct pathspec_item *)b_;
441 return strcmp(a->match, b->match);
442}
443
444static void NORETURN unsupported_magic(const char *pattern,
2aee5849 445 unsigned magic)
87323bda
NTND
446{
447 struct strbuf sb = STRBUF_INIT;
93f3ddb2
BW
448 int i;
449 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
87323bda
NTND
450 const struct pathspec_magic *m = pathspec_magic + i;
451 if (!(magic & m->bit))
452 continue;
453 if (sb.len)
2aee5849
BW
454 strbuf_addstr(&sb, ", ");
455
456 if (m->mnemonic)
457 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
458 m->name, m->mnemonic);
87323bda
NTND
459 else
460 strbuf_addf(&sb, "'%s'", m->name);
87323bda
NTND
461 }
462 /*
463 * We may want to substitute "this command" with a command
464 * name. E.g. when add--interactive dies when running
465 * "checkout -p"
466 */
467 die(_("%s: pathspec magic not supported by this command: %s"),
468 pattern, sb.buf);
9d67b61f 469}
512aaf94
AS
470
471/*
87323bda
NTND
472 * Given command line arguments and a prefix, convert the input to
473 * pathspec. die() if any magic in magic_mask is used.
512aaf94 474 */
87323bda
NTND
475void parse_pathspec(struct pathspec *pathspec,
476 unsigned magic_mask, unsigned flags,
477 const char *prefix, const char **argv)
512aaf94 478{
87323bda
NTND
479 struct pathspec_item *item;
480 const char *entry = argv ? *argv : NULL;
d426430e 481 int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
87323bda
NTND
482
483 memset(pathspec, 0, sizeof(*pathspec));
484
6330a171
NTND
485 if (flags & PATHSPEC_MAXDEPTH_VALID)
486 pathspec->magic |= PATHSPEC_MAXDEPTH;
487
87323bda
NTND
488 /* No arguments, no prefix -> no pathspec */
489 if (!entry && !prefix)
490 return;
491
fc12261f
NTND
492 if ((flags & PATHSPEC_PREFER_CWD) &&
493 (flags & PATHSPEC_PREFER_FULL))
494 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
495
87323bda
NTND
496 /* No arguments with prefix -> prefix pathspec */
497 if (!entry) {
fc12261f
NTND
498 if (flags & PATHSPEC_PREFER_FULL)
499 return;
500
501 if (!(flags & PATHSPEC_PREFER_CWD))
502 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
503
51a60f5b 504 pathspec->items = item = xcalloc(1, sizeof(*item));
8aee769f
BW
505 item->match = xstrdup(prefix);
506 item->original = xstrdup(prefix);
87323bda 507 item->nowildcard_len = item->len = strlen(prefix);
645a29c4 508 item->prefix = item->len;
87323bda 509 pathspec->nr = 1;
87323bda 510 return;
512aaf94 511 }
87323bda
NTND
512
513 n = 0;
d426430e
EX
514 warn_empty_string = 1;
515 while (argv[n]) {
516 if (*argv[n] == '\0' && warn_empty_string) {
517 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
518 "please use . instead if you meant to match all paths"));
519 warn_empty_string = 0;
520 }
87323bda 521 n++;
d426430e 522 }
87323bda
NTND
523
524 pathspec->nr = n;
859b7f1d 525 ALLOC_ARRAY(pathspec->items, n + 1);
b32fa95f 526 item = pathspec->items;
87323bda
NTND
527 prefixlen = prefix ? strlen(prefix) : 0;
528
529 for (i = 0; i < n; i++) {
87323bda
NTND
530 entry = argv[i];
531
27ec4282 532 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
db7e8598 533
ef79b1f8
NTND
534 if (item[i].magic & PATHSPEC_EXCLUDE)
535 nr_exclude++;
87323bda 536 if (item[i].magic & magic_mask)
2aee5849 537 unsupported_magic(entry, item[i].magic & magic_mask);
87450244
NTND
538
539 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
540 has_symlink_leading_path(item[i].match, item[i].len)) {
541 die(_("pathspec '%s' is beyond a symbolic link"), entry);
542 }
543
87323bda
NTND
544 if (item[i].nowildcard_len < item[i].len)
545 pathspec->has_wildcard = 1;
546 pathspec->magic |= item[i].magic;
547 }
548
859b7f1d
LT
549 /*
550 * If everything is an exclude pattern, add one positive pattern
551 * that matches everyting. We allocated an extra one for this.
552 */
553 if (nr_exclude == n) {
554 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
555 init_pathspec_item(item + n, 0, prefix, plen, "");
556 pathspec->nr++;
557 }
931eab64
NTND
558
559 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
560 if (flags & PATHSPEC_KEEP_ORDER)
561 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
9ed0d8d6 562 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
931eab64 563 }
64acde94
NTND
564}
565
e4d92cdc
NTND
566void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
567{
8aee769f
BW
568 int i;
569
e4d92cdc 570 *dst = *src;
b32fa95f 571 ALLOC_ARRAY(dst->items, dst->nr);
45ccef87 572 COPY_ARRAY(dst->items, src->items, dst->nr);
8aee769f
BW
573
574 for (i = 0; i < dst->nr; i++) {
575 dst->items[i].match = xstrdup(src->items[i].match);
576 dst->items[i].original = xstrdup(src->items[i].original);
577 }
e4d92cdc 578}
9a087274 579
ed6e8038 580void clear_pathspec(struct pathspec *pathspec)
9a087274 581{
8aee769f
BW
582 int i;
583
584 for (i = 0; i < pathspec->nr; i++) {
585 free(pathspec->items[i].match);
586 free(pathspec->items[i].original);
587 }
9a087274
NTND
588 free(pathspec->items);
589 pathspec->items = NULL;
8aee769f 590 pathspec->nr = 0;
512aaf94 591}