glossary-content.txt: rephrase magic signature part
[git/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];
84b8b5d1 36 match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, 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[] = {
70 { PATHSPEC_FROMTOP, '/', "top" },
5c6933d2 71 { PATHSPEC_LITERAL, 0, "literal" },
bd30c2e4 72 { PATHSPEC_GLOB, '\0', "glob" },
93d93537 73 { PATHSPEC_ICASE, '\0', "icase" },
64acde94
NTND
74};
75
76/*
77 * Take an element of a pathspec and check for magic signatures.
87323bda 78 * Append the result to the prefix. Return the magic bitmap.
64acde94
NTND
79 *
80 * For now, we only parse the syntax and throw out anything other than
81 * "top" magic.
82 *
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.
9d67b61f 88 */
87323bda
NTND
89static 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)
9d67b61f 94{
341003e7 95 static int literal_global = -1;
bd30c2e4
NTND
96 static int glob_global = -1;
97 static int noglob_global = -1;
93d93537 98 static int icase_global = -1;
5c6933d2 99 unsigned magic = 0, short_magic = 0, global_magic = 0;
233c3e6c 100 const char *copyfrom = elt, *long_magic_end = NULL;
87323bda 101 char *match;
233c3e6c 102 int i, pathspec_prefix = -1;
64acde94 103
341003e7
NTND
104 if (literal_global < 0)
105 literal_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
5c6933d2
NTND
106 if (literal_global)
107 global_magic |= PATHSPEC_LITERAL;
341003e7 108
bd30c2e4
NTND
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;
113
114 if (noglob_global < 0)
115 noglob_global = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
116
117 if (glob_global && noglob_global)
118 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
119
93d93537
NTND
120
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;
125
bd30c2e4
NTND
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"));
130
4a2d5ae2
NTND
131 if (flags & PATHSPEC_LITERAL_PATH)
132 global_magic = 0;
133
134 if (elt[0] != ':' || literal_global ||
135 (flags & PATHSPEC_LITERAL_PATH)) {
64acde94
NTND
136 ; /* nothing to do */
137 } else if (elt[1] == '(') {
138 /* longhand */
139 const char *nextat;
140 for (copyfrom = elt + 2;
141 *copyfrom && *copyfrom != ')';
142 copyfrom = nextat) {
143 size_t len = strcspn(copyfrom, ",)");
144 if (copyfrom[len] == ',')
145 nextat = copyfrom + len + 1;
146 else
147 /* handle ')' and '\0' */
148 nextat = copyfrom + len;
149 if (!len)
9d67b61f 150 continue;
233c3e6c 151 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
64acde94
NTND
152 if (strlen(pathspec_magic[i].name) == len &&
153 !strncmp(pathspec_magic[i].name, copyfrom, len)) {
154 magic |= pathspec_magic[i].bit;
155 break;
156 }
233c3e6c
NTND
157 if (!prefixcmp(copyfrom, "prefix:")) {
158 char *endptr;
159 pathspec_prefix = strtol(copyfrom + 7,
160 &endptr, 10);
161 if (endptr - copyfrom != len)
162 die(_("invalid parameter for pathspec magic 'prefix'"));
163 /* "i" would be wrong, but it does not matter */
164 break;
165 }
166 }
64acde94 167 if (ARRAY_SIZE(pathspec_magic) <= i)
f01d9820 168 die(_("Invalid pathspec magic '%.*s' in '%s'"),
64acde94
NTND
169 (int) len, copyfrom, elt);
170 }
171 if (*copyfrom != ')')
f01d9820 172 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
233c3e6c 173 long_magic_end = copyfrom;
64acde94
NTND
174 copyfrom++;
175 } else {
176 /* shorthand */
177 for (copyfrom = elt + 1;
178 *copyfrom && *copyfrom != ':';
179 copyfrom++) {
180 char ch = *copyfrom;
181
182 if (!is_pathspec_magic(ch))
183 break;
184 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
185 if (pathspec_magic[i].mnemonic == ch) {
87323bda 186 short_magic |= pathspec_magic[i].bit;
64acde94
NTND
187 break;
188 }
189 if (ARRAY_SIZE(pathspec_magic) <= i)
f01d9820 190 die(_("Unimplemented pathspec magic '%c' in '%s'"),
64acde94
NTND
191 ch, elt);
192 }
193 if (*copyfrom == ':')
194 copyfrom++;
195 }
196
87323bda
NTND
197 magic |= short_magic;
198 *p_short_magic = short_magic;
bd30c2e4 199
382d20e3 200 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
bd30c2e4
NTND
201 if (noglob_global && !(magic & PATHSPEC_GLOB))
202 global_magic |= PATHSPEC_LITERAL;
203
382d20e3 204 /* --glob-pathspec is overridden by :(literal) */
bd30c2e4
NTND
205 if ((global_magic & PATHSPEC_GLOB) && (magic & PATHSPEC_LITERAL))
206 global_magic &= ~PATHSPEC_GLOB;
207
5c6933d2 208 magic |= global_magic;
87323bda 209
233c3e6c
NTND
210 if (pathspec_prefix >= 0 &&
211 (prefixlen || (prefix && *prefix)))
212 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
213
bd30c2e4
NTND
214 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
215 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
216
233c3e6c
NTND
217 if (pathspec_prefix >= 0) {
218 match = xstrdup(copyfrom);
219 prefixlen = pathspec_prefix;
220 } else if (magic & PATHSPEC_FROMTOP) {
87323bda 221 match = xstrdup(copyfrom);
645a29c4
NTND
222 prefixlen = 0;
223 } else {
224 match = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom);
225 if (!match)
226 die(_("%s: '%s' is outside repository"), elt, copyfrom);
227 }
87323bda 228 *raw = item->match = match;
dad2586a
NTND
229 /*
230 * Prefix the pathspec (keep all magic) and assign to
231 * original. Useful for passing to another command.
232 */
233 if (flags & PATHSPEC_PREFIX_ORIGIN) {
234 struct strbuf sb = STRBUF_INIT;
233c3e6c 235 const char *start = elt;
341003e7 236 if (prefixlen && !literal_global) {
233c3e6c 237 /* Preserve the actual prefix length of each pattern */
bc341c8b
NTND
238 if (short_magic)
239 die("BUG: prefixing on short magic is not supported");
240 else if (long_magic_end) {
233c3e6c
NTND
241 strbuf_add(&sb, start, long_magic_end - start);
242 strbuf_addf(&sb, ",prefix:%d", prefixlen);
243 start = long_magic_end;
9d67b61f 244 } else {
233c3e6c
NTND
245 if (*start == ':')
246 start++;
247 strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
9d67b61f
AS
248 }
249 }
233c3e6c 250 strbuf_add(&sb, start, copyfrom - start);
dad2586a
NTND
251 strbuf_addstr(&sb, match);
252 item->original = strbuf_detach(&sb, NULL);
253 } else
254 item->original = elt;
87323bda 255 item->len = strlen(item->match);
645a29c4 256 item->prefix = prefixlen;
b69bb3fc
NTND
257
258 if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
259 (item->len >= 1 && item->match[item->len - 1] == '/') &&
260 (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
261 S_ISGITLINK(active_cache[i]->ce_mode)) {
262 item->len--;
263 match[item->len] = '\0';
264 }
265
87450244
NTND
266 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
267 for (i = 0; i < active_nr; i++) {
268 struct cache_entry *ce = active_cache[i];
269 int ce_len = ce_namelen(ce);
270
271 if (!S_ISGITLINK(ce->ce_mode))
272 continue;
273
274 if (item->len <= ce_len || match[ce_len] != '/' ||
275 memcmp(ce->name, match, ce_len))
276 continue;
277 if (item->len == ce_len + 1) {
278 /* strip trailing slash */
279 item->len--;
280 match[item->len] = '\0';
281 } else
282 die (_("Pathspec '%s' is in submodule '%.*s'"),
283 elt, ce_len, ce->name);
284 }
285
5c6933d2 286 if (magic & PATHSPEC_LITERAL)
87323bda 287 item->nowildcard_len = item->len;
645a29c4 288 else {
87323bda 289 item->nowildcard_len = simple_length(item->match);
645a29c4
NTND
290 if (item->nowildcard_len < prefixlen)
291 item->nowildcard_len = prefixlen;
292 }
87323bda 293 item->flags = 0;
bd30c2e4
NTND
294 if (magic & PATHSPEC_GLOB) {
295 /*
296 * FIXME: should we enable ONESTAR in _GLOB for
297 * pattern "* * / * . c"?
298 */
299 } else {
300 if (item->nowildcard_len < item->len &&
301 item->match[item->nowildcard_len] == '*' &&
302 no_wildcard(item->match + item->nowildcard_len + 1))
303 item->flags |= PATHSPEC_ONESTAR;
9d67b61f 304 }
645a29c4
NTND
305
306 /* sanity checks, pathspec matchers assume these are sane */
307 assert(item->nowildcard_len <= item->len &&
308 item->prefix <= item->len);
87323bda
NTND
309 return magic;
310}
311
312static int pathspec_item_cmp(const void *a_, const void *b_)
313{
314 struct pathspec_item *a, *b;
315
316 a = (struct pathspec_item *)a_;
317 b = (struct pathspec_item *)b_;
318 return strcmp(a->match, b->match);
319}
320
321static void NORETURN unsupported_magic(const char *pattern,
322 unsigned magic,
323 unsigned short_magic)
324{
325 struct strbuf sb = STRBUF_INIT;
326 int i, n;
327 for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
328 const struct pathspec_magic *m = pathspec_magic + i;
329 if (!(magic & m->bit))
330 continue;
331 if (sb.len)
332 strbuf_addstr(&sb, " ");
333 if (short_magic & m->bit)
334 strbuf_addf(&sb, "'%c'", m->mnemonic);
335 else
336 strbuf_addf(&sb, "'%s'", m->name);
337 n++;
338 }
339 /*
340 * We may want to substitute "this command" with a command
341 * name. E.g. when add--interactive dies when running
342 * "checkout -p"
343 */
344 die(_("%s: pathspec magic not supported by this command: %s"),
345 pattern, sb.buf);
9d67b61f 346}
512aaf94
AS
347
348/*
87323bda
NTND
349 * Given command line arguments and a prefix, convert the input to
350 * pathspec. die() if any magic in magic_mask is used.
512aaf94 351 */
87323bda
NTND
352void parse_pathspec(struct pathspec *pathspec,
353 unsigned magic_mask, unsigned flags,
354 const char *prefix, const char **argv)
512aaf94 355{
87323bda
NTND
356 struct pathspec_item *item;
357 const char *entry = argv ? *argv : NULL;
358 int i, n, prefixlen;
359
360 memset(pathspec, 0, sizeof(*pathspec));
361
6330a171
NTND
362 if (flags & PATHSPEC_MAXDEPTH_VALID)
363 pathspec->magic |= PATHSPEC_MAXDEPTH;
364
87323bda
NTND
365 /* No arguments, no prefix -> no pathspec */
366 if (!entry && !prefix)
367 return;
368
fc12261f
NTND
369 if ((flags & PATHSPEC_PREFER_CWD) &&
370 (flags & PATHSPEC_PREFER_FULL))
371 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
372
87323bda
NTND
373 /* No arguments with prefix -> prefix pathspec */
374 if (!entry) {
375 static const char *raw[2];
376
fc12261f
NTND
377 if (flags & PATHSPEC_PREFER_FULL)
378 return;
379
380 if (!(flags & PATHSPEC_PREFER_CWD))
381 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
382
87323bda
NTND
383 pathspec->items = item = xmalloc(sizeof(*item));
384 memset(item, 0, sizeof(*item));
385 item->match = prefix;
d2ce1331 386 item->original = prefix;
87323bda 387 item->nowildcard_len = item->len = strlen(prefix);
645a29c4 388 item->prefix = item->len;
87323bda
NTND
389 raw[0] = prefix;
390 raw[1] = NULL;
391 pathspec->nr = 1;
b3920bbd 392 pathspec->_raw = raw;
87323bda 393 return;
512aaf94 394 }
87323bda
NTND
395
396 n = 0;
397 while (argv[n])
398 n++;
399
400 pathspec->nr = n;
401 pathspec->items = item = xmalloc(sizeof(*item) * n);
b3920bbd 402 pathspec->_raw = argv;
87323bda
NTND
403 prefixlen = prefix ? strlen(prefix) : 0;
404
405 for (i = 0; i < n; i++) {
406 unsigned short_magic;
407 entry = argv[i];
408
409 item[i].magic = prefix_pathspec(item + i, &short_magic,
410 argv + i, flags,
411 prefix, prefixlen, entry);
4a2d5ae2
NTND
412 if ((flags & PATHSPEC_LITERAL_PATH) &&
413 !(magic_mask & PATHSPEC_LITERAL))
414 item[i].magic |= PATHSPEC_LITERAL;
87323bda
NTND
415 if (item[i].magic & magic_mask)
416 unsupported_magic(entry,
417 item[i].magic & magic_mask,
418 short_magic);
87450244
NTND
419
420 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
421 has_symlink_leading_path(item[i].match, item[i].len)) {
422 die(_("pathspec '%s' is beyond a symbolic link"), entry);
423 }
424
87323bda
NTND
425 if (item[i].nowildcard_len < item[i].len)
426 pathspec->has_wildcard = 1;
427 pathspec->magic |= item[i].magic;
428 }
429
931eab64
NTND
430
431 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
432 if (flags & PATHSPEC_KEEP_ORDER)
433 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
6330a171
NTND
434 qsort(pathspec->items, pathspec->nr,
435 sizeof(struct pathspec_item), pathspec_item_cmp);
931eab64 436 }
64acde94
NTND
437}
438
439/*
440 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
87323bda 441 * based interface - see pathspec.c:parse_pathspec().
64acde94
NTND
442 *
443 * Arguments:
444 * - prefix - a path relative to the root of the working tree
445 * - pathspec - a list of paths underneath the prefix path
446 *
447 * Iterates over pathspec, prepending each path with prefix,
448 * and return the resulting list.
449 *
450 * If pathspec is empty, return a singleton list containing prefix.
451 *
452 * If pathspec and prefix are both empty, return an empty list.
453 *
454 * This is typically used by built-in commands such as add.c, in order
455 * to normalize argv arguments provided to the built-in into a list of
456 * paths to process, all relative to the root of the working tree.
457 */
458const char **get_pathspec(const char *prefix, const char **pathspec)
459{
87323bda
NTND
460 struct pathspec ps;
461 parse_pathspec(&ps,
5c6933d2
NTND
462 PATHSPEC_ALL_MAGIC &
463 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
fc12261f
NTND
464 PATHSPEC_PREFER_CWD,
465 prefix, pathspec);
b3920bbd 466 return ps._raw;
64acde94 467}
e4d92cdc
NTND
468
469void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
470{
471 *dst = *src;
472 dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
473 memcpy(dst->items, src->items,
474 sizeof(struct pathspec_item) * dst->nr);
475}
9a087274
NTND
476
477void free_pathspec(struct pathspec *pathspec)
478{
479 free(pathspec->items);
480 pathspec->items = NULL;
512aaf94 481}