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