connected: verify promisor-ness of partial clone
[git.git] / pathspec.c
blob128f27fcb7ae8c88b0f7143943ea8a7171a83c5d
1 #include "cache.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "pathspec.h"
5 #include "attr.h"
6 #include "argv-array.h"
7 #include "quote.h"
9 /*
10 * Finds which of the given pathspecs match items in the index.
12 * For each pathspec, sets the corresponding entry in the seen[] array
13 * (which should be specs items long, i.e. the same size as pathspec)
14 * to the nature of the "closest" (i.e. most specific) match found for
15 * that pathspec in the index, if it was a closer type of match than
16 * the existing entry. As an optimization, matching is skipped
17 * altogether if seen[] already only contains non-zero entries.
19 * If seen[] has not already been written to, it may make sense
20 * to use find_pathspecs_matching_against_index() instead.
22 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
23 const struct index_state *istate,
24 char *seen)
26 int num_unmatched = 0, i;
29 * Since we are walking the index as if we were walking the directory,
30 * we have to mark the matched pathspec as seen; otherwise we will
31 * mistakenly think that the user gave a pathspec that did not match
32 * anything.
34 for (i = 0; i < pathspec->nr; i++)
35 if (!seen[i])
36 num_unmatched++;
37 if (!num_unmatched)
38 return;
39 for (i = 0; i < istate->cache_nr; i++) {
40 const struct cache_entry *ce = istate->cache[i];
41 ce_path_match(istate, ce, pathspec, seen);
46 * Finds which of the given pathspecs match items in the index.
48 * This is a one-shot wrapper around add_pathspec_matches_against_index()
49 * which allocates, populates, and returns a seen[] array indicating the
50 * nature of the "closest" (i.e. most specific) matches which each of the
51 * given pathspecs achieves against all items in the index.
53 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
54 const struct index_state *istate)
56 char *seen = xcalloc(pathspec->nr, 1);
57 add_pathspec_matches_against_index(pathspec, istate, seen);
58 return seen;
62 * Magic pathspec
64 * Possible future magic semantics include stuff like:
66 * { PATHSPEC_RECURSIVE, '*', "recursive" },
67 * { PATHSPEC_REGEXP, '\0', "regexp" },
71 static struct pathspec_magic {
72 unsigned bit;
73 char mnemonic; /* this cannot be ':'! */
74 const char *name;
75 } pathspec_magic[] = {
76 { PATHSPEC_FROMTOP, '/', "top" },
77 { PATHSPEC_LITERAL, '\0', "literal" },
78 { PATHSPEC_GLOB, '\0', "glob" },
79 { PATHSPEC_ICASE, '\0', "icase" },
80 { PATHSPEC_EXCLUDE, '!', "exclude" },
81 { PATHSPEC_ATTR, '\0', "attr" },
84 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
86 int i;
87 strbuf_addstr(sb, ":(");
88 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
89 if (magic & pathspec_magic[i].bit) {
90 if (sb->buf[sb->len - 1] != '(')
91 strbuf_addch(sb, ',');
92 strbuf_addstr(sb, pathspec_magic[i].name);
94 strbuf_addf(sb, ",prefix:%d)", prefixlen);
97 static size_t strcspn_escaped(const char *s, const char *stop)
99 const char *i;
101 for (i = s; *i; i++) {
102 /* skip the escaped character */
103 if (i[0] == '\\' && i[1]) {
104 i++;
105 continue;
108 if (strchr(stop, *i))
109 break;
111 return i - s;
114 static inline int invalid_value_char(const char ch)
116 if (isalnum(ch) || strchr(",-_", ch))
117 return 0;
118 return -1;
121 static char *attr_value_unescape(const char *value)
123 const char *src;
124 char *dst, *ret;
126 ret = xmallocz(strlen(value));
127 for (src = value, dst = ret; *src; src++, dst++) {
128 if (*src == '\\') {
129 if (!src[1])
130 die(_("Escape character '\\' not allowed as "
131 "last character in attr value"));
132 src++;
134 if (invalid_value_char(*src))
135 die("cannot use '%c' for value matching", *src);
136 *dst = *src;
138 *dst = '\0';
139 return ret;
142 static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
144 struct string_list_item *si;
145 struct string_list list = STRING_LIST_INIT_DUP;
147 if (item->attr_check || item->attr_match)
148 die(_("Only one 'attr:' specification is allowed."));
150 if (!value || !*value)
151 die(_("attr spec must not be empty"));
153 string_list_split(&list, value, ' ', -1);
154 string_list_remove_empty_items(&list, 0);
156 item->attr_check = attr_check_alloc();
157 item->attr_match = xcalloc(list.nr, sizeof(struct attr_match));
159 for_each_string_list_item(si, &list) {
160 size_t attr_len;
161 char *attr_name;
162 const struct git_attr *a;
164 int j = item->attr_match_nr++;
165 const char *attr = si->string;
166 struct attr_match *am = &item->attr_match[j];
168 switch (*attr) {
169 case '!':
170 am->match_mode = MATCH_UNSPECIFIED;
171 attr++;
172 attr_len = strlen(attr);
173 break;
174 case '-':
175 am->match_mode = MATCH_UNSET;
176 attr++;
177 attr_len = strlen(attr);
178 break;
179 default:
180 attr_len = strcspn(attr, "=");
181 if (attr[attr_len] != '=')
182 am->match_mode = MATCH_SET;
183 else {
184 const char *v = &attr[attr_len + 1];
185 am->match_mode = MATCH_VALUE;
186 am->value = attr_value_unescape(v);
188 break;
191 attr_name = xmemdupz(attr, attr_len);
192 a = git_attr(attr_name);
193 if (!a)
194 die(_("invalid attribute name %s"), attr_name);
196 attr_check_append(item->attr_check, a);
198 free(attr_name);
201 if (item->attr_check->nr != item->attr_match_nr)
202 BUG("should have same number of entries");
204 string_list_clear(&list, 0);
207 static inline int get_literal_global(void)
209 static int literal = -1;
211 if (literal < 0)
212 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
214 return literal;
217 static inline int get_glob_global(void)
219 static int glob = -1;
221 if (glob < 0)
222 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
224 return glob;
227 static inline int get_noglob_global(void)
229 static int noglob = -1;
231 if (noglob < 0)
232 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
234 return noglob;
237 static inline int get_icase_global(void)
239 static int icase = -1;
241 if (icase < 0)
242 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
244 return icase;
247 static int get_global_magic(int element_magic)
249 int global_magic = 0;
251 if (get_literal_global())
252 global_magic |= PATHSPEC_LITERAL;
254 /* --glob-pathspec is overridden by :(literal) */
255 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
256 global_magic |= PATHSPEC_GLOB;
258 if (get_glob_global() && get_noglob_global())
259 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
261 if (get_icase_global())
262 global_magic |= PATHSPEC_ICASE;
264 if ((global_magic & PATHSPEC_LITERAL) &&
265 (global_magic & ~PATHSPEC_LITERAL))
266 die(_("global 'literal' pathspec setting is incompatible "
267 "with all other global pathspec settings"));
269 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
270 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
271 global_magic |= PATHSPEC_LITERAL;
273 return global_magic;
277 * Parse the pathspec element looking for long magic
279 * saves all magic in 'magic'
280 * if prefix magic is used, save the prefix length in 'prefix_len'
281 * returns the position in 'elem' after all magic has been parsed
283 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
284 struct pathspec_item *item,
285 const char *elem)
287 const char *pos;
288 const char *nextat;
290 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
291 size_t len = strcspn_escaped(pos, ",)");
292 int i;
294 if (pos[len] == ',')
295 nextat = pos + len + 1; /* handle ',' */
296 else
297 nextat = pos + len; /* handle ')' and '\0' */
299 if (!len)
300 continue;
302 if (starts_with(pos, "prefix:")) {
303 char *endptr;
304 *prefix_len = strtol(pos + 7, &endptr, 10);
305 if (endptr - pos != len)
306 die(_("invalid parameter for pathspec magic 'prefix'"));
307 continue;
310 if (starts_with(pos, "attr:")) {
311 char *attr_body = xmemdupz(pos + 5, len - 5);
312 parse_pathspec_attr_match(item, attr_body);
313 *magic |= PATHSPEC_ATTR;
314 free(attr_body);
315 continue;
318 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
319 if (strlen(pathspec_magic[i].name) == len &&
320 !strncmp(pathspec_magic[i].name, pos, len)) {
321 *magic |= pathspec_magic[i].bit;
322 break;
326 if (ARRAY_SIZE(pathspec_magic) <= i)
327 die(_("Invalid pathspec magic '%.*s' in '%s'"),
328 (int) len, pos, elem);
331 if (*pos != ')')
332 die(_("Missing ')' at the end of pathspec magic in '%s'"),
333 elem);
334 pos++;
336 return pos;
340 * Parse the pathspec element looking for short magic
342 * saves all magic in 'magic'
343 * returns the position in 'elem' after all magic has been parsed
345 static const char *parse_short_magic(unsigned *magic, const char *elem)
347 const char *pos;
349 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
350 char ch = *pos;
351 int i;
353 /* Special case alias for '!' */
354 if (ch == '^') {
355 *magic |= PATHSPEC_EXCLUDE;
356 continue;
359 if (!is_pathspec_magic(ch))
360 break;
362 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
363 if (pathspec_magic[i].mnemonic == ch) {
364 *magic |= pathspec_magic[i].bit;
365 break;
369 if (ARRAY_SIZE(pathspec_magic) <= i)
370 die(_("Unimplemented pathspec magic '%c' in '%s'"),
371 ch, elem);
374 if (*pos == ':')
375 pos++;
377 return pos;
380 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
381 struct pathspec_item *item,
382 const char *elem)
384 if (elem[0] != ':' || get_literal_global())
385 return elem; /* nothing to do */
386 else if (elem[1] == '(')
387 /* longhand */
388 return parse_long_magic(magic, prefix_len, item, elem);
389 else
390 /* shorthand */
391 return parse_short_magic(magic, elem);
395 * Perform the initialization of a pathspec_item based on a pathspec element.
397 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
398 const char *prefix, int prefixlen,
399 const char *elt)
401 unsigned magic = 0, element_magic = 0;
402 const char *copyfrom = elt;
403 char *match;
404 int pathspec_prefix = -1;
406 item->attr_check = NULL;
407 item->attr_match = NULL;
408 item->attr_match_nr = 0;
410 /* PATHSPEC_LITERAL_PATH ignores magic */
411 if (flags & PATHSPEC_LITERAL_PATH) {
412 magic = PATHSPEC_LITERAL;
413 } else {
414 copyfrom = parse_element_magic(&element_magic,
415 &pathspec_prefix,
416 item,
417 elt);
418 magic |= element_magic;
419 magic |= get_global_magic(element_magic);
422 item->magic = magic;
424 if (pathspec_prefix >= 0 &&
425 (prefixlen || (prefix && *prefix)))
426 BUG("'prefix' magic is supposed to be used at worktree's root");
428 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
429 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
431 /* Create match string which will be used for pathspec matching */
432 if (pathspec_prefix >= 0) {
433 match = xstrdup(copyfrom);
434 prefixlen = pathspec_prefix;
435 } else if (magic & PATHSPEC_FROMTOP) {
436 match = xstrdup(copyfrom);
437 prefixlen = 0;
438 } else {
439 match = prefix_path_gently(prefix, prefixlen,
440 &prefixlen, copyfrom);
441 if (!match)
442 die(_("%s: '%s' is outside repository"), elt, copyfrom);
445 item->match = match;
446 item->len = strlen(item->match);
447 item->prefix = prefixlen;
450 * Prefix the pathspec (keep all magic) and assign to
451 * original. Useful for passing to another command.
453 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
454 !get_literal_global()) {
455 struct strbuf sb = STRBUF_INIT;
457 /* Preserve the actual prefix length of each pattern */
458 prefix_magic(&sb, prefixlen, element_magic);
460 strbuf_addstr(&sb, match);
461 item->original = strbuf_detach(&sb, NULL);
462 } else {
463 item->original = xstrdup(elt);
466 if (magic & PATHSPEC_LITERAL) {
467 item->nowildcard_len = item->len;
468 } else {
469 item->nowildcard_len = simple_length(item->match);
470 if (item->nowildcard_len < prefixlen)
471 item->nowildcard_len = prefixlen;
474 item->flags = 0;
475 if (magic & PATHSPEC_GLOB) {
477 * FIXME: should we enable ONESTAR in _GLOB for
478 * pattern "* * / * . c"?
480 } else {
481 if (item->nowildcard_len < item->len &&
482 item->match[item->nowildcard_len] == '*' &&
483 no_wildcard(item->match + item->nowildcard_len + 1))
484 item->flags |= PATHSPEC_ONESTAR;
487 /* sanity checks, pathspec matchers assume these are sane */
488 if (item->nowildcard_len > item->len ||
489 item->prefix > item->len) {
490 BUG("error initializing pathspec_item");
494 static int pathspec_item_cmp(const void *a_, const void *b_)
496 struct pathspec_item *a, *b;
498 a = (struct pathspec_item *)a_;
499 b = (struct pathspec_item *)b_;
500 return strcmp(a->match, b->match);
503 static void NORETURN unsupported_magic(const char *pattern,
504 unsigned magic)
506 struct strbuf sb = STRBUF_INIT;
507 int i;
508 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
509 const struct pathspec_magic *m = pathspec_magic + i;
510 if (!(magic & m->bit))
511 continue;
512 if (sb.len)
513 strbuf_addstr(&sb, ", ");
515 if (m->mnemonic)
516 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
517 m->name, m->mnemonic);
518 else
519 strbuf_addf(&sb, "'%s'", m->name);
522 * We may want to substitute "this command" with a command
523 * name. E.g. when add--interactive dies when running
524 * "checkout -p"
526 die(_("%s: pathspec magic not supported by this command: %s"),
527 pattern, sb.buf);
530 void parse_pathspec(struct pathspec *pathspec,
531 unsigned magic_mask, unsigned flags,
532 const char *prefix, const char **argv)
534 struct pathspec_item *item;
535 const char *entry = argv ? *argv : NULL;
536 int i, n, prefixlen, nr_exclude = 0;
538 memset(pathspec, 0, sizeof(*pathspec));
540 if (flags & PATHSPEC_MAXDEPTH_VALID)
541 pathspec->magic |= PATHSPEC_MAXDEPTH;
543 /* No arguments, no prefix -> no pathspec */
544 if (!entry && !prefix)
545 return;
547 if ((flags & PATHSPEC_PREFER_CWD) &&
548 (flags & PATHSPEC_PREFER_FULL))
549 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
551 /* No arguments with prefix -> prefix pathspec */
552 if (!entry) {
553 if (flags & PATHSPEC_PREFER_FULL)
554 return;
556 if (!(flags & PATHSPEC_PREFER_CWD))
557 BUG("PATHSPEC_PREFER_CWD requires arguments");
559 pathspec->items = item = xcalloc(1, sizeof(*item));
560 item->match = xstrdup(prefix);
561 item->original = xstrdup(prefix);
562 item->nowildcard_len = item->len = strlen(prefix);
563 item->prefix = item->len;
564 pathspec->nr = 1;
565 return;
568 n = 0;
569 while (argv[n]) {
570 if (*argv[n] == '\0')
571 die("empty string is not a valid pathspec. "
572 "please use . instead if you meant to match all paths");
573 n++;
576 pathspec->nr = n;
577 ALLOC_ARRAY(pathspec->items, n + 1);
578 item = pathspec->items;
579 prefixlen = prefix ? strlen(prefix) : 0;
581 for (i = 0; i < n; i++) {
582 entry = argv[i];
584 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
586 if (item[i].magic & PATHSPEC_EXCLUDE)
587 nr_exclude++;
588 if (item[i].magic & magic_mask)
589 unsupported_magic(entry, item[i].magic & magic_mask);
591 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
592 has_symlink_leading_path(item[i].match, item[i].len)) {
593 die(_("pathspec '%s' is beyond a symbolic link"), entry);
596 if (item[i].nowildcard_len < item[i].len)
597 pathspec->has_wildcard = 1;
598 pathspec->magic |= item[i].magic;
602 * If everything is an exclude pattern, add one positive pattern
603 * that matches everything. We allocated an extra one for this.
605 if (nr_exclude == n) {
606 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
607 init_pathspec_item(item + n, 0, prefix, plen, "");
608 pathspec->nr++;
611 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
612 if (flags & PATHSPEC_KEEP_ORDER)
613 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
614 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
618 void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
619 unsigned flags, const char *prefix,
620 const char *file, int nul_term_line)
622 struct argv_array parsed_file = ARGV_ARRAY_INIT;
623 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
624 strbuf_getline;
625 struct strbuf buf = STRBUF_INIT;
626 struct strbuf unquoted = STRBUF_INIT;
627 FILE *in;
629 if (!strcmp(file, "-"))
630 in = stdin;
631 else
632 in = xfopen(file, "r");
634 while (getline_fn(&buf, in) != EOF) {
635 if (!nul_term_line && buf.buf[0] == '"') {
636 strbuf_reset(&unquoted);
637 if (unquote_c_style(&unquoted, buf.buf, NULL))
638 die(_("line is badly quoted: %s"), buf.buf);
639 strbuf_swap(&buf, &unquoted);
641 argv_array_push(&parsed_file, buf.buf);
642 strbuf_reset(&buf);
645 strbuf_release(&unquoted);
646 strbuf_release(&buf);
647 if (in != stdin)
648 fclose(in);
650 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.argv);
651 argv_array_clear(&parsed_file);
654 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
656 int i, j;
658 *dst = *src;
659 ALLOC_ARRAY(dst->items, dst->nr);
660 COPY_ARRAY(dst->items, src->items, dst->nr);
662 for (i = 0; i < dst->nr; i++) {
663 struct pathspec_item *d = &dst->items[i];
664 struct pathspec_item *s = &src->items[i];
666 d->match = xstrdup(s->match);
667 d->original = xstrdup(s->original);
669 ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
670 COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
671 for (j = 0; j < d->attr_match_nr; j++) {
672 const char *value = s->attr_match[j].value;
673 d->attr_match[j].value = xstrdup_or_null(value);
676 d->attr_check = attr_check_dup(s->attr_check);
680 void clear_pathspec(struct pathspec *pathspec)
682 int i, j;
684 for (i = 0; i < pathspec->nr; i++) {
685 free(pathspec->items[i].match);
686 free(pathspec->items[i].original);
688 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
689 free(pathspec->items[i].attr_match[j].value);
690 free(pathspec->items[i].attr_match);
692 if (pathspec->items[i].attr_check)
693 attr_check_free(pathspec->items[i].attr_check);
696 FREE_AND_NULL(pathspec->items);
697 pathspec->nr = 0;
700 int match_pathspec_attrs(const struct index_state *istate,
701 const char *name, int namelen,
702 const struct pathspec_item *item)
704 int i;
705 char *to_free = NULL;
707 if (name[namelen])
708 name = to_free = xmemdupz(name, namelen);
710 git_check_attr(istate, name, item->attr_check);
712 free(to_free);
714 for (i = 0; i < item->attr_match_nr; i++) {
715 const char *value;
716 int matched;
717 enum attr_match_mode match_mode;
719 value = item->attr_check->items[i].value;
720 match_mode = item->attr_match[i].match_mode;
722 if (ATTR_TRUE(value))
723 matched = (match_mode == MATCH_SET);
724 else if (ATTR_FALSE(value))
725 matched = (match_mode == MATCH_UNSET);
726 else if (ATTR_UNSET(value))
727 matched = (match_mode == MATCH_UNSPECIFIED);
728 else
729 matched = (match_mode == MATCH_VALUE &&
730 !strcmp(item->attr_match[i].value, value));
731 if (!matched)
732 return 0;
735 return 1;