Merge branch 'tq/t1510'
[git/raj.git] / pathspec.c
blob82eb39cd679ffbce82abac691c6f4343335b9924
1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
2 #include "cache.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "pathspec.h"
6 #include "attr.h"
8 /*
9 * Finds which of the given pathspecs match items in the index.
11 * For each pathspec, sets the corresponding entry in the seen[] array
12 * (which should be specs items long, i.e. the same size as pathspec)
13 * to the nature of the "closest" (i.e. most specific) match found for
14 * that pathspec in the index, if it was a closer type of match than
15 * the existing entry. As an optimization, matching is skipped
16 * altogether if seen[] already only contains non-zero entries.
18 * If seen[] has not already been written to, it may make sense
19 * to use find_pathspecs_matching_against_index() instead.
21 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
22 const struct index_state *istate,
23 char *seen)
25 int num_unmatched = 0, i;
28 * Since we are walking the index as if we were walking the directory,
29 * we have to mark the matched pathspec as seen; otherwise we will
30 * mistakenly think that the user gave a pathspec that did not match
31 * anything.
33 for (i = 0; i < pathspec->nr; i++)
34 if (!seen[i])
35 num_unmatched++;
36 if (!num_unmatched)
37 return;
38 for (i = 0; i < istate->cache_nr; i++) {
39 const struct cache_entry *ce = istate->cache[i];
40 ce_path_match(ce, pathspec, seen);
45 * Finds which of the given pathspecs match items in the index.
47 * This is a one-shot wrapper around add_pathspec_matches_against_index()
48 * which allocates, populates, and returns a seen[] array indicating the
49 * nature of the "closest" (i.e. most specific) matches which each of the
50 * given pathspecs achieves against all items in the index.
52 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
53 const struct index_state *istate)
55 char *seen = xcalloc(pathspec->nr, 1);
56 add_pathspec_matches_against_index(pathspec, istate, seen);
57 return seen;
61 * Magic pathspec
63 * Possible future magic semantics include stuff like:
65 * { PATHSPEC_RECURSIVE, '*', "recursive" },
66 * { PATHSPEC_REGEXP, '\0', "regexp" },
70 static struct pathspec_magic {
71 unsigned bit;
72 char mnemonic; /* this cannot be ':'! */
73 const char *name;
74 } pathspec_magic[] = {
75 { PATHSPEC_FROMTOP, '/', "top" },
76 { PATHSPEC_LITERAL, '\0', "literal" },
77 { PATHSPEC_GLOB, '\0', "glob" },
78 { PATHSPEC_ICASE, '\0', "icase" },
79 { PATHSPEC_EXCLUDE, '!', "exclude" },
80 { PATHSPEC_ATTR, '\0', "attr" },
83 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
85 int i;
86 strbuf_addstr(sb, ":(");
87 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
88 if (magic & pathspec_magic[i].bit) {
89 if (sb->buf[sb->len - 1] != '(')
90 strbuf_addch(sb, ',');
91 strbuf_addstr(sb, pathspec_magic[i].name);
93 strbuf_addf(sb, ",prefix:%d)", prefixlen);
96 static size_t strcspn_escaped(const char *s, const char *stop)
98 const char *i;
100 for (i = s; *i; i++) {
101 /* skip the escaped character */
102 if (i[0] == '\\' && i[1]) {
103 i++;
104 continue;
107 if (strchr(stop, *i))
108 break;
110 return i - s;
113 static inline int invalid_value_char(const char ch)
115 if (isalnum(ch) || strchr(",-_", ch))
116 return 0;
117 return -1;
120 static char *attr_value_unescape(const char *value)
122 const char *src;
123 char *dst, *ret;
125 ret = xmallocz(strlen(value));
126 for (src = value, dst = ret; *src; src++, dst++) {
127 if (*src == '\\') {
128 if (!src[1])
129 die(_("Escape character '\\' not allowed as "
130 "last character in attr value"));
131 src++;
133 if (invalid_value_char(*src))
134 die("cannot use '%c' for value matching", *src);
135 *dst = *src;
137 *dst = '\0';
138 return ret;
141 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
143 struct string_list_item *si;
144 struct string_list list = STRING_LIST_INIT_DUP;
146 if (item->attr_check || item->attr_match)
147 die(_("Only one 'attr:' specification is allowed."));
149 if (!value || !*value)
150 die(_("attr spec must not be empty"));
152 string_list_split(&list, value, ' ', -1);
153 string_list_remove_empty_items(&list, 0);
155 item->attr_check = attr_check_alloc();
156 item->attr_match = xcalloc(list.nr, sizeof(struct attr_match));
158 for_each_string_list_item(si, &list) {
159 size_t attr_len;
160 char *attr_name;
161 const struct git_attr *a;
163 int j = item->attr_match_nr++;
164 const char *attr = si->string;
165 struct attr_match *am = &item->attr_match[j];
167 switch (*attr) {
168 case '!':
169 am->match_mode = MATCH_UNSPECIFIED;
170 attr++;
171 attr_len = strlen(attr);
172 break;
173 case '-':
174 am->match_mode = MATCH_UNSET;
175 attr++;
176 attr_len = strlen(attr);
177 break;
178 default:
179 attr_len = strcspn(attr, "=");
180 if (attr[attr_len] != '=')
181 am->match_mode = MATCH_SET;
182 else {
183 const char *v = &attr[attr_len + 1];
184 am->match_mode = MATCH_VALUE;
185 am->value = attr_value_unescape(v);
187 break;
190 attr_name = xmemdupz(attr, attr_len);
191 a = git_attr(attr_name);
192 if (!a)
193 die(_("invalid attribute name %s"), attr_name);
195 attr_check_append(item->attr_check, a);
197 free(attr_name);
200 if (item->attr_check->nr != item->attr_match_nr)
201 die("BUG: should have same number of entries");
203 string_list_clear(&list, 0);
206 static inline int get_literal_global(void)
208 static int literal = -1;
210 if (literal < 0)
211 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
213 return literal;
216 static inline int get_glob_global(void)
218 static int glob = -1;
220 if (glob < 0)
221 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
223 return glob;
226 static inline int get_noglob_global(void)
228 static int noglob = -1;
230 if (noglob < 0)
231 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
233 return noglob;
236 static inline int get_icase_global(void)
238 static int icase = -1;
240 if (icase < 0)
241 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
243 return icase;
246 static int get_global_magic(int element_magic)
248 int global_magic = 0;
250 if (get_literal_global())
251 global_magic |= PATHSPEC_LITERAL;
253 /* --glob-pathspec is overridden by :(literal) */
254 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
255 global_magic |= PATHSPEC_GLOB;
257 if (get_glob_global() && get_noglob_global())
258 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
260 if (get_icase_global())
261 global_magic |= PATHSPEC_ICASE;
263 if ((global_magic & PATHSPEC_LITERAL) &&
264 (global_magic & ~PATHSPEC_LITERAL))
265 die(_("global 'literal' pathspec setting is incompatible "
266 "with all other global pathspec settings"));
268 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
269 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
270 global_magic |= PATHSPEC_LITERAL;
272 return global_magic;
276 * Parse the pathspec element looking for long magic
278 * saves all magic in 'magic'
279 * if prefix magic is used, save the prefix length in 'prefix_len'
280 * returns the position in 'elem' after all magic has been parsed
282 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
283 struct pathspec_item *item,
284 const char *elem)
286 const char *pos;
287 const char *nextat;
289 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
290 size_t len = strcspn_escaped(pos, ",)");
291 int i;
293 if (pos[len] == ',')
294 nextat = pos + len + 1; /* handle ',' */
295 else
296 nextat = pos + len; /* handle ')' and '\0' */
298 if (!len)
299 continue;
301 if (starts_with(pos, "prefix:")) {
302 char *endptr;
303 *prefix_len = strtol(pos + 7, &endptr, 10);
304 if (endptr - pos != len)
305 die(_("invalid parameter for pathspec magic 'prefix'"));
306 continue;
309 if (starts_with(pos, "attr:")) {
310 char *attr_body = xmemdupz(pos + 5, len - 5);
311 parse_pathspec_attr_match(item, attr_body);
312 *magic |= PATHSPEC_ATTR;
313 free(attr_body);
314 continue;
317 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
318 if (strlen(pathspec_magic[i].name) == len &&
319 !strncmp(pathspec_magic[i].name, pos, len)) {
320 *magic |= pathspec_magic[i].bit;
321 break;
325 if (ARRAY_SIZE(pathspec_magic) <= i)
326 die(_("Invalid pathspec magic '%.*s' in '%s'"),
327 (int) len, pos, elem);
330 if (*pos != ')')
331 die(_("Missing ')' at the end of pathspec magic in '%s'"),
332 elem);
333 pos++;
335 return pos;
339 * Parse the pathspec element looking for short magic
341 * saves all magic in 'magic'
342 * returns the position in 'elem' after all magic has been parsed
344 static const char *parse_short_magic(unsigned *magic, const char *elem)
346 const char *pos;
348 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
349 char ch = *pos;
350 int i;
352 /* Special case alias for '!' */
353 if (ch == '^') {
354 *magic |= PATHSPEC_EXCLUDE;
355 continue;
358 if (!is_pathspec_magic(ch))
359 break;
361 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
362 if (pathspec_magic[i].mnemonic == ch) {
363 *magic |= pathspec_magic[i].bit;
364 break;
368 if (ARRAY_SIZE(pathspec_magic) <= i)
369 die(_("Unimplemented pathspec magic '%c' in '%s'"),
370 ch, elem);
373 if (*pos == ':')
374 pos++;
376 return pos;
379 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
380 struct pathspec_item *item,
381 const char *elem)
383 if (elem[0] != ':' || get_literal_global())
384 return elem; /* nothing to do */
385 else if (elem[1] == '(')
386 /* longhand */
387 return parse_long_magic(magic, prefix_len, item, elem);
388 else
389 /* shorthand */
390 return parse_short_magic(magic, elem);
394 * Perform the initialization of a pathspec_item based on a pathspec element.
396 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
397 const char *prefix, int prefixlen,
398 const char *elt)
400 unsigned magic = 0, element_magic = 0;
401 const char *copyfrom = elt;
402 char *match;
403 int pathspec_prefix = -1;
405 item->attr_check = NULL;
406 item->attr_match = NULL;
407 item->attr_match_nr = 0;
409 /* PATHSPEC_LITERAL_PATH ignores magic */
410 if (flags & PATHSPEC_LITERAL_PATH) {
411 magic = PATHSPEC_LITERAL;
412 } else {
413 copyfrom = parse_element_magic(&element_magic,
414 &pathspec_prefix,
415 item,
416 elt);
417 magic |= element_magic;
418 magic |= get_global_magic(element_magic);
421 item->magic = magic;
423 if (pathspec_prefix >= 0 &&
424 (prefixlen || (prefix && *prefix)))
425 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
427 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
428 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
430 /* Create match string which will be used for pathspec matching */
431 if (pathspec_prefix >= 0) {
432 match = xstrdup(copyfrom);
433 prefixlen = pathspec_prefix;
434 } else if (magic & PATHSPEC_FROMTOP) {
435 match = xstrdup(copyfrom);
436 prefixlen = 0;
437 } else {
438 match = prefix_path_gently(prefix, prefixlen,
439 &prefixlen, copyfrom);
440 if (!match)
441 die(_("%s: '%s' is outside repository"), elt, copyfrom);
444 item->match = match;
445 item->len = strlen(item->match);
446 item->prefix = prefixlen;
449 * Prefix the pathspec (keep all magic) and assign to
450 * original. Useful for passing to another command.
452 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
453 !get_literal_global()) {
454 struct strbuf sb = STRBUF_INIT;
456 /* Preserve the actual prefix length of each pattern */
457 prefix_magic(&sb, prefixlen, element_magic);
459 strbuf_addstr(&sb, match);
460 item->original = strbuf_detach(&sb, NULL);
461 } else {
462 item->original = xstrdup(elt);
465 if (magic & PATHSPEC_LITERAL) {
466 item->nowildcard_len = item->len;
467 } else {
468 item->nowildcard_len = simple_length(item->match);
469 if (item->nowildcard_len < prefixlen)
470 item->nowildcard_len = prefixlen;
473 item->flags = 0;
474 if (magic & PATHSPEC_GLOB) {
476 * FIXME: should we enable ONESTAR in _GLOB for
477 * pattern "* * / * . c"?
479 } else {
480 if (item->nowildcard_len < item->len &&
481 item->match[item->nowildcard_len] == '*' &&
482 no_wildcard(item->match + item->nowildcard_len + 1))
483 item->flags |= PATHSPEC_ONESTAR;
486 /* sanity checks, pathspec matchers assume these are sane */
487 if (item->nowildcard_len > item->len ||
488 item->prefix > item->len) {
489 die ("BUG: error initializing pathspec_item");
493 static int pathspec_item_cmp(const void *a_, const void *b_)
495 struct pathspec_item *a, *b;
497 a = (struct pathspec_item *)a_;
498 b = (struct pathspec_item *)b_;
499 return strcmp(a->match, b->match);
502 static void NORETURN unsupported_magic(const char *pattern,
503 unsigned magic)
505 struct strbuf sb = STRBUF_INIT;
506 int i;
507 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
508 const struct pathspec_magic *m = pathspec_magic + i;
509 if (!(magic & m->bit))
510 continue;
511 if (sb.len)
512 strbuf_addstr(&sb, ", ");
514 if (m->mnemonic)
515 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
516 m->name, m->mnemonic);
517 else
518 strbuf_addf(&sb, "'%s'", m->name);
521 * We may want to substitute "this command" with a command
522 * name. E.g. when add--interactive dies when running
523 * "checkout -p"
525 die(_("%s: pathspec magic not supported by this command: %s"),
526 pattern, sb.buf);
529 void parse_pathspec(struct pathspec *pathspec,
530 unsigned magic_mask, unsigned flags,
531 const char *prefix, const char **argv)
533 struct pathspec_item *item;
534 const char *entry = argv ? *argv : NULL;
535 int i, n, prefixlen, nr_exclude = 0;
537 memset(pathspec, 0, sizeof(*pathspec));
539 if (flags & PATHSPEC_MAXDEPTH_VALID)
540 pathspec->magic |= PATHSPEC_MAXDEPTH;
542 /* No arguments, no prefix -> no pathspec */
543 if (!entry && !prefix)
544 return;
546 if ((flags & PATHSPEC_PREFER_CWD) &&
547 (flags & PATHSPEC_PREFER_FULL))
548 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
550 /* No arguments with prefix -> prefix pathspec */
551 if (!entry) {
552 if (flags & PATHSPEC_PREFER_FULL)
553 return;
555 if (!(flags & PATHSPEC_PREFER_CWD))
556 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
558 pathspec->items = item = xcalloc(1, sizeof(*item));
559 item->match = xstrdup(prefix);
560 item->original = xstrdup(prefix);
561 item->nowildcard_len = item->len = strlen(prefix);
562 item->prefix = item->len;
563 pathspec->nr = 1;
564 return;
567 n = 0;
568 while (argv[n]) {
569 if (*argv[n] == '\0')
570 die("empty string is not a valid pathspec. "
571 "please use . instead if you meant to match all paths");
572 n++;
575 pathspec->nr = n;
576 ALLOC_ARRAY(pathspec->items, n + 1);
577 item = pathspec->items;
578 prefixlen = prefix ? strlen(prefix) : 0;
580 for (i = 0; i < n; i++) {
581 entry = argv[i];
583 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
585 if (item[i].magic & PATHSPEC_EXCLUDE)
586 nr_exclude++;
587 if (item[i].magic & magic_mask)
588 unsupported_magic(entry, item[i].magic & magic_mask);
590 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
591 has_symlink_leading_path(item[i].match, item[i].len)) {
592 die(_("pathspec '%s' is beyond a symbolic link"), entry);
595 if (item[i].nowildcard_len < item[i].len)
596 pathspec->has_wildcard = 1;
597 pathspec->magic |= item[i].magic;
601 * If everything is an exclude pattern, add one positive pattern
602 * that matches everything. We allocated an extra one for this.
604 if (nr_exclude == n) {
605 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
606 init_pathspec_item(item + n, 0, prefix, plen, "");
607 pathspec->nr++;
610 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
611 if (flags & PATHSPEC_KEEP_ORDER)
612 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
613 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
617 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
619 int i, j;
621 *dst = *src;
622 ALLOC_ARRAY(dst->items, dst->nr);
623 COPY_ARRAY(dst->items, src->items, dst->nr);
625 for (i = 0; i < dst->nr; i++) {
626 struct pathspec_item *d = &dst->items[i];
627 struct pathspec_item *s = &src->items[i];
629 d->match = xstrdup(s->match);
630 d->original = xstrdup(s->original);
632 ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
633 COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
634 for (j = 0; j < d->attr_match_nr; j++) {
635 const char *value = s->attr_match[j].value;
636 d->attr_match[j].value = xstrdup_or_null(value);
639 d->attr_check = attr_check_dup(s->attr_check);
643 void clear_pathspec(struct pathspec *pathspec)
645 int i, j;
647 for (i = 0; i < pathspec->nr; i++) {
648 free(pathspec->items[i].match);
649 free(pathspec->items[i].original);
651 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
652 free(pathspec->items[i].attr_match[j].value);
653 free(pathspec->items[i].attr_match);
655 if (pathspec->items[i].attr_check)
656 attr_check_free(pathspec->items[i].attr_check);
659 FREE_AND_NULL(pathspec->items);
660 pathspec->nr = 0;