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