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