Merge branch 'jc/url-match'
[alt-git.git] / pathspec.c
blob4b32cc32cb5b0be495ac941c5e96d8aee8fd8ff9
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 match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, 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" },
77 * Take an element of a pathspec and check for magic signatures.
78 * Append the result to the prefix. Return the magic bitmap.
80 * For now, we only parse the syntax and throw out anything other than
81 * "top" magic.
83 * NEEDSWORK: This needs to be rewritten when we start migrating
84 * get_pathspec() users to use the "struct pathspec" interface. For
85 * example, a pathspec element may be marked as case-insensitive, but
86 * the prefix part must always match literally, and a single stupid
87 * string cannot express such a case.
89 static unsigned prefix_pathspec(struct pathspec_item *item,
90 unsigned *p_short_magic,
91 const char **raw, unsigned flags,
92 const char *prefix, int prefixlen,
93 const char *elt)
95 static int literal_global = -1;
96 static int glob_global = -1;
97 static int noglob_global = -1;
98 static int icase_global = -1;
99 unsigned magic = 0, short_magic = 0, global_magic = 0;
100 const char *copyfrom = elt, *long_magic_end = NULL;
101 char *match;
102 int i, pathspec_prefix = -1;
104 if (literal_global < 0)
105 literal_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
106 if (literal_global)
107 global_magic |= PATHSPEC_LITERAL;
109 if (glob_global < 0)
110 glob_global = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
111 if (glob_global)
112 global_magic |= PATHSPEC_GLOB;
114 if (noglob_global < 0)
115 noglob_global = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
117 if (glob_global && noglob_global)
118 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
121 if (icase_global < 0)
122 icase_global = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
123 if (icase_global)
124 global_magic |= PATHSPEC_ICASE;
126 if ((global_magic & PATHSPEC_LITERAL) &&
127 (global_magic & ~PATHSPEC_LITERAL))
128 die(_("global 'literal' pathspec setting is incompatible "
129 "with all other global pathspec settings"));
131 if (elt[0] != ':' || literal_global) {
132 ; /* nothing to do */
133 } else if (elt[1] == '(') {
134 /* longhand */
135 const char *nextat;
136 for (copyfrom = elt + 2;
137 *copyfrom && *copyfrom != ')';
138 copyfrom = nextat) {
139 size_t len = strcspn(copyfrom, ",)");
140 if (copyfrom[len] == ',')
141 nextat = copyfrom + len + 1;
142 else
143 /* handle ')' and '\0' */
144 nextat = copyfrom + len;
145 if (!len)
146 continue;
147 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
148 if (strlen(pathspec_magic[i].name) == len &&
149 !strncmp(pathspec_magic[i].name, copyfrom, len)) {
150 magic |= pathspec_magic[i].bit;
151 break;
153 if (!prefixcmp(copyfrom, "prefix:")) {
154 char *endptr;
155 pathspec_prefix = strtol(copyfrom + 7,
156 &endptr, 10);
157 if (endptr - copyfrom != len)
158 die(_("invalid parameter for pathspec magic 'prefix'"));
159 /* "i" would be wrong, but it does not matter */
160 break;
163 if (ARRAY_SIZE(pathspec_magic) <= i)
164 die(_("Invalid pathspec magic '%.*s' in '%s'"),
165 (int) len, copyfrom, elt);
167 if (*copyfrom != ')')
168 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
169 long_magic_end = copyfrom;
170 copyfrom++;
171 } else {
172 /* shorthand */
173 for (copyfrom = elt + 1;
174 *copyfrom && *copyfrom != ':';
175 copyfrom++) {
176 char ch = *copyfrom;
178 if (!is_pathspec_magic(ch))
179 break;
180 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
181 if (pathspec_magic[i].mnemonic == ch) {
182 short_magic |= pathspec_magic[i].bit;
183 break;
185 if (ARRAY_SIZE(pathspec_magic) <= i)
186 die(_("Unimplemented pathspec magic '%c' in '%s'"),
187 ch, elt);
189 if (*copyfrom == ':')
190 copyfrom++;
193 magic |= short_magic;
194 *p_short_magic = short_magic;
196 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specifed */
197 if (noglob_global && !(magic & PATHSPEC_GLOB))
198 global_magic |= PATHSPEC_LITERAL;
200 /* --glob-pathspec is overriden by :(literal) */
201 if ((global_magic & PATHSPEC_GLOB) && (magic & PATHSPEC_LITERAL))
202 global_magic &= ~PATHSPEC_GLOB;
204 magic |= global_magic;
206 if (pathspec_prefix >= 0 &&
207 (prefixlen || (prefix && *prefix)))
208 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
210 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
211 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
213 if (pathspec_prefix >= 0) {
214 match = xstrdup(copyfrom);
215 prefixlen = pathspec_prefix;
216 } else if (magic & PATHSPEC_FROMTOP) {
217 match = xstrdup(copyfrom);
218 prefixlen = 0;
219 } else {
220 match = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom);
221 if (!match)
222 die(_("%s: '%s' is outside repository"), elt, copyfrom);
224 *raw = item->match = match;
226 * Prefix the pathspec (keep all magic) and assign to
227 * original. Useful for passing to another command.
229 if (flags & PATHSPEC_PREFIX_ORIGIN) {
230 struct strbuf sb = STRBUF_INIT;
231 const char *start = elt;
232 if (prefixlen && !literal_global) {
233 /* Preserve the actual prefix length of each pattern */
234 if (long_magic_end) {
235 strbuf_add(&sb, start, long_magic_end - start);
236 strbuf_addf(&sb, ",prefix:%d", prefixlen);
237 start = long_magic_end;
238 } else {
239 if (*start == ':')
240 start++;
241 strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
244 strbuf_add(&sb, start, copyfrom - start);
245 strbuf_addstr(&sb, match);
246 item->original = strbuf_detach(&sb, NULL);
247 } else
248 item->original = elt;
249 item->len = strlen(item->match);
250 item->prefix = prefixlen;
252 if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
253 (item->len >= 1 && item->match[item->len - 1] == '/') &&
254 (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
255 S_ISGITLINK(active_cache[i]->ce_mode)) {
256 item->len--;
257 match[item->len] = '\0';
260 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
261 for (i = 0; i < active_nr; i++) {
262 struct cache_entry *ce = active_cache[i];
263 int ce_len = ce_namelen(ce);
265 if (!S_ISGITLINK(ce->ce_mode))
266 continue;
268 if (item->len <= ce_len || match[ce_len] != '/' ||
269 memcmp(ce->name, match, ce_len))
270 continue;
271 if (item->len == ce_len + 1) {
272 /* strip trailing slash */
273 item->len--;
274 match[item->len] = '\0';
275 } else
276 die (_("Pathspec '%s' is in submodule '%.*s'"),
277 elt, ce_len, ce->name);
280 if (magic & PATHSPEC_LITERAL)
281 item->nowildcard_len = item->len;
282 else {
283 item->nowildcard_len = simple_length(item->match);
284 if (item->nowildcard_len < prefixlen)
285 item->nowildcard_len = prefixlen;
287 item->flags = 0;
288 if (magic & PATHSPEC_GLOB) {
290 * FIXME: should we enable ONESTAR in _GLOB for
291 * pattern "* * / * . c"?
293 } else {
294 if (item->nowildcard_len < item->len &&
295 item->match[item->nowildcard_len] == '*' &&
296 no_wildcard(item->match + item->nowildcard_len + 1))
297 item->flags |= PATHSPEC_ONESTAR;
300 /* sanity checks, pathspec matchers assume these are sane */
301 assert(item->nowildcard_len <= item->len &&
302 item->prefix <= item->len);
303 return magic;
306 static int pathspec_item_cmp(const void *a_, const void *b_)
308 struct pathspec_item *a, *b;
310 a = (struct pathspec_item *)a_;
311 b = (struct pathspec_item *)b_;
312 return strcmp(a->match, b->match);
315 static void NORETURN unsupported_magic(const char *pattern,
316 unsigned magic,
317 unsigned short_magic)
319 struct strbuf sb = STRBUF_INIT;
320 int i, n;
321 for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
322 const struct pathspec_magic *m = pathspec_magic + i;
323 if (!(magic & m->bit))
324 continue;
325 if (sb.len)
326 strbuf_addstr(&sb, " ");
327 if (short_magic & m->bit)
328 strbuf_addf(&sb, "'%c'", m->mnemonic);
329 else
330 strbuf_addf(&sb, "'%s'", m->name);
331 n++;
334 * We may want to substitute "this command" with a command
335 * name. E.g. when add--interactive dies when running
336 * "checkout -p"
338 die(_("%s: pathspec magic not supported by this command: %s"),
339 pattern, sb.buf);
343 * Given command line arguments and a prefix, convert the input to
344 * pathspec. die() if any magic in magic_mask is used.
346 void parse_pathspec(struct pathspec *pathspec,
347 unsigned magic_mask, unsigned flags,
348 const char *prefix, const char **argv)
350 struct pathspec_item *item;
351 const char *entry = argv ? *argv : NULL;
352 int i, n, prefixlen;
354 memset(pathspec, 0, sizeof(*pathspec));
356 if (flags & PATHSPEC_MAXDEPTH_VALID)
357 pathspec->magic |= PATHSPEC_MAXDEPTH;
359 /* No arguments, no prefix -> no pathspec */
360 if (!entry && !prefix)
361 return;
363 if ((flags & PATHSPEC_PREFER_CWD) &&
364 (flags & PATHSPEC_PREFER_FULL))
365 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
367 /* No arguments with prefix -> prefix pathspec */
368 if (!entry) {
369 static const char *raw[2];
371 if (flags & PATHSPEC_PREFER_FULL)
372 return;
374 if (!(flags & PATHSPEC_PREFER_CWD))
375 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
377 pathspec->items = item = xmalloc(sizeof(*item));
378 memset(item, 0, sizeof(*item));
379 item->match = prefix;
380 item->original = prefix;
381 item->nowildcard_len = item->len = strlen(prefix);
382 item->prefix = item->len;
383 raw[0] = prefix;
384 raw[1] = NULL;
385 pathspec->nr = 1;
386 pathspec->_raw = raw;
387 return;
390 n = 0;
391 while (argv[n])
392 n++;
394 pathspec->nr = n;
395 pathspec->items = item = xmalloc(sizeof(*item) * n);
396 pathspec->_raw = argv;
397 prefixlen = prefix ? strlen(prefix) : 0;
399 for (i = 0; i < n; i++) {
400 unsigned short_magic;
401 entry = argv[i];
403 item[i].magic = prefix_pathspec(item + i, &short_magic,
404 argv + i, flags,
405 prefix, prefixlen, entry);
406 if (item[i].magic & magic_mask)
407 unsupported_magic(entry,
408 item[i].magic & magic_mask,
409 short_magic);
411 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
412 has_symlink_leading_path(item[i].match, item[i].len)) {
413 die(_("pathspec '%s' is beyond a symbolic link"), entry);
416 if (item[i].nowildcard_len < item[i].len)
417 pathspec->has_wildcard = 1;
418 pathspec->magic |= item[i].magic;
422 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
423 if (flags & PATHSPEC_KEEP_ORDER)
424 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
425 qsort(pathspec->items, pathspec->nr,
426 sizeof(struct pathspec_item), pathspec_item_cmp);
431 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
432 * based interface - see pathspec.c:parse_pathspec().
434 * Arguments:
435 * - prefix - a path relative to the root of the working tree
436 * - pathspec - a list of paths underneath the prefix path
438 * Iterates over pathspec, prepending each path with prefix,
439 * and return the resulting list.
441 * If pathspec is empty, return a singleton list containing prefix.
443 * If pathspec and prefix are both empty, return an empty list.
445 * This is typically used by built-in commands such as add.c, in order
446 * to normalize argv arguments provided to the built-in into a list of
447 * paths to process, all relative to the root of the working tree.
449 const char **get_pathspec(const char *prefix, const char **pathspec)
451 struct pathspec ps;
452 parse_pathspec(&ps,
453 PATHSPEC_ALL_MAGIC &
454 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
455 PATHSPEC_PREFER_CWD,
456 prefix, pathspec);
457 return ps._raw;
460 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
462 *dst = *src;
463 dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
464 memcpy(dst->items, src->items,
465 sizeof(struct pathspec_item) * dst->nr);
468 void free_pathspec(struct pathspec *pathspec)
470 free(pathspec->items);
471 pathspec->items = NULL;