Merge branch 'js/exec-path-coverity-workaround'
[git.git] / pathspec.c
blob7ababb315944d5536ec251b19937beef5def2538
1 #include "cache.h"
2 #include "dir.h"
3 #include "pathspec.h"
5 /*
6 * Finds which of the given pathspecs match items in the index.
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.
15 * If seen[] has not already been written to, it may make sense
16 * to use find_pathspecs_matching_against_index() instead.
18 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
19 char *seen)
21 int num_unmatched = 0, i;
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.
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);
41 * Finds which of the given pathspecs match items in the index.
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.
48 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
50 char *seen = xcalloc(pathspec->nr, 1);
51 add_pathspec_matches_against_index(pathspec, seen);
52 return seen;
56 * Magic pathspec
58 * Possible future magic semantics include stuff like:
60 * { PATHSPEC_RECURSIVE, '*', "recursive" },
61 * { PATHSPEC_REGEXP, '\0', "regexp" },
65 static 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" },
77 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
79 int i;
80 strbuf_addstr(sb, ":(");
81 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
82 if (magic & pathspec_magic[i].bit) {
83 if (sb->buf[sb->len - 1] != '(')
84 strbuf_addch(sb, ',');
85 strbuf_addstr(sb, pathspec_magic[i].name);
87 strbuf_addf(sb, ",prefix:%d)", prefixlen);
90 static inline int get_literal_global(void)
92 static int literal = -1;
94 if (literal < 0)
95 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
97 return literal;
100 static inline int get_glob_global(void)
102 static int glob = -1;
104 if (glob < 0)
105 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
107 return glob;
110 static inline int get_noglob_global(void)
112 static int noglob = -1;
114 if (noglob < 0)
115 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
117 return noglob;
120 static inline int get_icase_global(void)
122 static int icase = -1;
124 if (icase < 0)
125 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
127 return icase;
130 static int get_global_magic(int element_magic)
132 int global_magic = 0;
134 if (get_literal_global())
135 global_magic |= PATHSPEC_LITERAL;
137 /* --glob-pathspec is overridden by :(literal) */
138 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
139 global_magic |= PATHSPEC_GLOB;
141 if (get_glob_global() && get_noglob_global())
142 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
144 if (get_icase_global())
145 global_magic |= PATHSPEC_ICASE;
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"));
152 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
153 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
154 global_magic |= PATHSPEC_LITERAL;
156 return global_magic;
160 * Parse the pathspec element looking for long magic
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
166 static const char *parse_long_magic(unsigned *magic, int *prefix_len,
167 const char *elem)
169 const char *pos;
170 const char *nextat;
172 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
173 size_t len = strcspn(pos, ",)");
174 int i;
176 if (pos[len] == ',')
177 nextat = pos + len + 1; /* handle ',' */
178 else
179 nextat = pos + len; /* handle ')' and '\0' */
181 if (!len)
182 continue;
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;
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;
200 if (ARRAY_SIZE(pathspec_magic) <= i)
201 die(_("Invalid pathspec magic '%.*s' in '%s'"),
202 (int) len, pos, elem);
205 if (*pos != ')')
206 die(_("Missing ')' at the end of pathspec magic in '%s'"),
207 elem);
208 pos++;
210 return pos;
214 * Parse the pathspec element looking for short magic
216 * saves all magic in 'magic'
217 * returns the position in 'elem' after all magic has been parsed
219 static const char *parse_short_magic(unsigned *magic, const char *elem)
221 const char *pos;
223 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
224 char ch = *pos;
225 int i;
227 if (!is_pathspec_magic(ch))
228 break;
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;
237 if (ARRAY_SIZE(pathspec_magic) <= i)
238 die(_("Unimplemented pathspec magic '%c' in '%s'"),
239 ch, elem);
242 if (*pos == ':')
243 pos++;
245 return pos;
248 static const char *parse_element_magic(unsigned *magic, int *prefix_len,
249 const char *elem)
251 if (elem[0] != ':' || get_literal_global())
252 return elem; /* nothing to do */
253 else if (elem[1] == '(')
254 /* longhand */
255 return parse_long_magic(magic, prefix_len, elem);
256 else
257 /* shorthand */
258 return parse_short_magic(magic, elem);
261 static void strip_submodule_slash_cheap(struct pathspec_item *item)
263 if (item->len >= 1 && item->match[item->len - 1] == '/') {
264 int i = cache_name_pos(item->match, item->len - 1);
266 if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
267 item->len--;
268 item->match[item->len] = '\0';
273 static void strip_submodule_slash_expensive(struct pathspec_item *item)
275 int i;
277 for (i = 0; i < active_nr; i++) {
278 struct cache_entry *ce = active_cache[i];
279 int ce_len = ce_namelen(ce);
281 if (!S_ISGITLINK(ce->ce_mode))
282 continue;
284 if (item->len <= ce_len || item->match[ce_len] != '/' ||
285 memcmp(ce->name, item->match, ce_len))
286 continue;
288 if (item->len == ce_len + 1) {
289 /* strip trailing slash */
290 item->len--;
291 item->match[item->len] = '\0';
292 } else {
293 die(_("Pathspec '%s' is in submodule '%.*s'"),
294 item->original, ce_len, ce->name);
299 static void die_inside_submodule_path(struct pathspec_item *item)
301 int i;
303 for (i = 0; i < active_nr; i++) {
304 struct cache_entry *ce = active_cache[i];
305 int ce_len = ce_namelen(ce);
307 if (!S_ISGITLINK(ce->ce_mode))
308 continue;
310 if (item->len < ce_len ||
311 !(item->match[ce_len] == '/' || item->match[ce_len] == '\0') ||
312 memcmp(ce->name, item->match, ce_len))
313 continue;
315 die(_("Pathspec '%s' is in submodule '%.*s'"),
316 item->original, ce_len, ce->name);
321 * Perform the initialization of a pathspec_item based on a pathspec element.
323 static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
324 const char *prefix, int prefixlen,
325 const char *elt)
327 unsigned magic = 0, element_magic = 0;
328 const char *copyfrom = elt;
329 char *match;
330 int pathspec_prefix = -1;
332 /* PATHSPEC_LITERAL_PATH ignores magic */
333 if (flags & PATHSPEC_LITERAL_PATH) {
334 magic = PATHSPEC_LITERAL;
335 } else {
336 copyfrom = parse_element_magic(&element_magic,
337 &pathspec_prefix,
338 elt);
339 magic |= element_magic;
340 magic |= get_global_magic(element_magic);
343 item->magic = magic;
345 if (pathspec_prefix >= 0 &&
346 (prefixlen || (prefix && *prefix)))
347 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
349 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
350 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
352 /* Create match string which will be used for pathspec matching */
353 if (pathspec_prefix >= 0) {
354 match = xstrdup(copyfrom);
355 prefixlen = pathspec_prefix;
356 } else if (magic & PATHSPEC_FROMTOP) {
357 match = xstrdup(copyfrom);
358 prefixlen = 0;
359 } else {
360 match = prefix_path_gently(prefix, prefixlen,
361 &prefixlen, copyfrom);
362 if (!match)
363 die(_("%s: '%s' is outside repository"), elt, copyfrom);
366 item->match = match;
367 item->len = strlen(item->match);
368 item->prefix = prefixlen;
371 * Prefix the pathspec (keep all magic) and assign to
372 * original. Useful for passing to another command.
374 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
375 prefixlen && !get_literal_global()) {
376 struct strbuf sb = STRBUF_INIT;
378 /* Preserve the actual prefix length of each pattern */
379 prefix_magic(&sb, prefixlen, element_magic);
381 strbuf_addstr(&sb, match);
382 item->original = strbuf_detach(&sb, NULL);
383 } else {
384 item->original = xstrdup(elt);
387 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
388 strip_submodule_slash_cheap(item);
390 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
391 strip_submodule_slash_expensive(item);
393 if (magic & PATHSPEC_LITERAL) {
394 item->nowildcard_len = item->len;
395 } else {
396 item->nowildcard_len = simple_length(item->match);
397 if (item->nowildcard_len < prefixlen)
398 item->nowildcard_len = prefixlen;
401 item->flags = 0;
402 if (magic & PATHSPEC_GLOB) {
404 * FIXME: should we enable ONESTAR in _GLOB for
405 * pattern "* * / * . c"?
407 } else {
408 if (item->nowildcard_len < item->len &&
409 item->match[item->nowildcard_len] == '*' &&
410 no_wildcard(item->match + item->nowildcard_len + 1))
411 item->flags |= PATHSPEC_ONESTAR;
414 /* sanity checks, pathspec matchers assume these are sane */
415 if (item->nowildcard_len > item->len ||
416 item->prefix > item->len) {
418 * This case can be triggered by the user pointing us to a
419 * pathspec inside a submodule, which is an input error.
420 * Detect that here and complain, but fallback in the
421 * non-submodule case to a BUG, as we have no idea what
422 * would trigger that.
424 die_inside_submodule_path(item);
425 die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
429 static int pathspec_item_cmp(const void *a_, const void *b_)
431 struct pathspec_item *a, *b;
433 a = (struct pathspec_item *)a_;
434 b = (struct pathspec_item *)b_;
435 return strcmp(a->match, b->match);
438 static void NORETURN unsupported_magic(const char *pattern,
439 unsigned magic)
441 struct strbuf sb = STRBUF_INIT;
442 int i;
443 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
444 const struct pathspec_magic *m = pathspec_magic + i;
445 if (!(magic & m->bit))
446 continue;
447 if (sb.len)
448 strbuf_addstr(&sb, ", ");
450 if (m->mnemonic)
451 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
452 m->name, m->mnemonic);
453 else
454 strbuf_addf(&sb, "'%s'", m->name);
457 * We may want to substitute "this command" with a command
458 * name. E.g. when add--interactive dies when running
459 * "checkout -p"
461 die(_("%s: pathspec magic not supported by this command: %s"),
462 pattern, sb.buf);
466 * Given command line arguments and a prefix, convert the input to
467 * pathspec. die() if any magic in magic_mask is used.
469 void parse_pathspec(struct pathspec *pathspec,
470 unsigned magic_mask, unsigned flags,
471 const char *prefix, const char **argv)
473 struct pathspec_item *item;
474 const char *entry = argv ? *argv : NULL;
475 int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
477 memset(pathspec, 0, sizeof(*pathspec));
479 if (flags & PATHSPEC_MAXDEPTH_VALID)
480 pathspec->magic |= PATHSPEC_MAXDEPTH;
482 /* No arguments, no prefix -> no pathspec */
483 if (!entry && !prefix)
484 return;
486 if ((flags & PATHSPEC_PREFER_CWD) &&
487 (flags & PATHSPEC_PREFER_FULL))
488 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
490 /* No arguments with prefix -> prefix pathspec */
491 if (!entry) {
492 if (flags & PATHSPEC_PREFER_FULL)
493 return;
495 if (!(flags & PATHSPEC_PREFER_CWD))
496 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
498 pathspec->items = item = xcalloc(1, sizeof(*item));
499 item->match = xstrdup(prefix);
500 item->original = xstrdup(prefix);
501 item->nowildcard_len = item->len = strlen(prefix);
502 item->prefix = item->len;
503 pathspec->nr = 1;
504 return;
507 n = 0;
508 warn_empty_string = 1;
509 while (argv[n]) {
510 if (*argv[n] == '\0' && warn_empty_string) {
511 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
512 "please use . instead if you meant to match all paths"));
513 warn_empty_string = 0;
515 n++;
518 pathspec->nr = n;
519 ALLOC_ARRAY(pathspec->items, n);
520 item = pathspec->items;
521 prefixlen = prefix ? strlen(prefix) : 0;
523 for (i = 0; i < n; i++) {
524 entry = argv[i];
526 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
528 if (item[i].magic & PATHSPEC_EXCLUDE)
529 nr_exclude++;
530 if (item[i].magic & magic_mask)
531 unsupported_magic(entry, item[i].magic & magic_mask);
533 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
534 has_symlink_leading_path(item[i].match, item[i].len)) {
535 die(_("pathspec '%s' is beyond a symbolic link"), entry);
538 if (item[i].nowildcard_len < item[i].len)
539 pathspec->has_wildcard = 1;
540 pathspec->magic |= item[i].magic;
543 if (nr_exclude == n)
544 die(_("There is nothing to exclude from by :(exclude) patterns.\n"
545 "Perhaps you forgot to add either ':/' or '.' ?"));
548 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
549 if (flags & PATHSPEC_KEEP_ORDER)
550 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
551 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
555 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
557 int i;
559 *dst = *src;
560 ALLOC_ARRAY(dst->items, dst->nr);
561 COPY_ARRAY(dst->items, src->items, dst->nr);
563 for (i = 0; i < dst->nr; i++) {
564 dst->items[i].match = xstrdup(src->items[i].match);
565 dst->items[i].original = xstrdup(src->items[i].original);
569 void clear_pathspec(struct pathspec *pathspec)
571 int i;
573 for (i = 0; i < pathspec->nr; i++) {
574 free(pathspec->items[i].match);
575 free(pathspec->items[i].original);
577 free(pathspec->items);
578 pathspec->items = NULL;
579 pathspec->nr = 0;