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