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