repack: s/write_bitmap/&s/ in code
[git/mingw/j6t.git] / pathspec.c
blobad1a9f5b28c5119275fbcf9a53e62651190de137
1 #include "cache.h"
2 #include "dir.h"
3 #include "pathspec.h"
5 /*
6 * Finds which of the given pathspecs match items in the index.
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.
15 * If seen[] has not already been written to, it may make sense
16 * to use find_pathspecs_matching_against_index() instead.
18 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
19 char *seen)
21 int num_unmatched = 0, i;
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.
29 for (i = 0; i < pathspec->nr; i++)
30 if (!seen[i])
31 num_unmatched++;
32 if (!num_unmatched)
33 return;
34 for (i = 0; i < active_nr; i++) {
35 const struct cache_entry *ce = active_cache[i];
36 match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, seen);
41 * Finds which of the given pathspecs match items in the index.
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.
48 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
50 char *seen = xcalloc(pathspec->nr, 1);
51 add_pathspec_matches_against_index(pathspec, seen);
52 return seen;
56 * Magic pathspec
58 * Possible future magic semantics include stuff like:
60 * { PATHSPEC_RECURSIVE, '*', "recursive" },
61 * { PATHSPEC_REGEXP, '\0', "regexp" },
65 static struct pathspec_magic {
66 unsigned bit;
67 char mnemonic; /* this cannot be ':'! */
68 const char *name;
69 } pathspec_magic[] = {
70 { PATHSPEC_FROMTOP, '/', "top" },
71 { PATHSPEC_LITERAL, 0, "literal" },
72 { PATHSPEC_GLOB, '\0', "glob" },
73 { PATHSPEC_ICASE, '\0', "icase" },
77 * Take an element of a pathspec and check for magic signatures.
78 * Append the result to the prefix. Return the magic bitmap.
80 * For now, we only parse the syntax and throw out anything other than
81 * "top" magic.
83 * NEEDSWORK: This needs to be rewritten when we start migrating
84 * get_pathspec() users to use the "struct pathspec" interface. For
85 * example, a pathspec element may be marked as case-insensitive, but
86 * the prefix part must always match literally, and a single stupid
87 * string cannot express such a case.
89 static unsigned prefix_pathspec(struct pathspec_item *item,
90 unsigned *p_short_magic,
91 const char **raw, unsigned flags,
92 const char *prefix, int prefixlen,
93 const char *elt)
95 static int literal_global = -1;
96 static int glob_global = -1;
97 static int noglob_global = -1;
98 static int icase_global = -1;
99 unsigned magic = 0, short_magic = 0, global_magic = 0;
100 const char *copyfrom = elt, *long_magic_end = NULL;
101 char *match;
102 int i, pathspec_prefix = -1;
104 if (literal_global < 0)
105 literal_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
106 if (literal_global)
107 global_magic |= PATHSPEC_LITERAL;
109 if (glob_global < 0)
110 glob_global = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
111 if (glob_global)
112 global_magic |= PATHSPEC_GLOB;
114 if (noglob_global < 0)
115 noglob_global = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
117 if (glob_global && noglob_global)
118 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
121 if (icase_global < 0)
122 icase_global = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
123 if (icase_global)
124 global_magic |= PATHSPEC_ICASE;
126 if ((global_magic & PATHSPEC_LITERAL) &&
127 (global_magic & ~PATHSPEC_LITERAL))
128 die(_("global 'literal' pathspec setting is incompatible "
129 "with all other global pathspec settings"));
131 if (elt[0] != ':' || literal_global) {
132 ; /* nothing to do */
133 } else if (elt[1] == '(') {
134 /* longhand */
135 const char *nextat;
136 for (copyfrom = elt + 2;
137 *copyfrom && *copyfrom != ')';
138 copyfrom = nextat) {
139 size_t len = strcspn(copyfrom, ",)");
140 if (copyfrom[len] == ',')
141 nextat = copyfrom + len + 1;
142 else
143 /* handle ')' and '\0' */
144 nextat = copyfrom + len;
145 if (!len)
146 continue;
147 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
148 if (strlen(pathspec_magic[i].name) == len &&
149 !strncmp(pathspec_magic[i].name, copyfrom, len)) {
150 magic |= pathspec_magic[i].bit;
151 break;
153 if (!prefixcmp(copyfrom, "prefix:")) {
154 char *endptr;
155 pathspec_prefix = strtol(copyfrom + 7,
156 &endptr, 10);
157 if (endptr - copyfrom != len)
158 die(_("invalid parameter for pathspec magic 'prefix'"));
159 /* "i" would be wrong, but it does not matter */
160 break;
163 if (ARRAY_SIZE(pathspec_magic) <= i)
164 die(_("Invalid pathspec magic '%.*s' in '%s'"),
165 (int) len, copyfrom, elt);
167 if (*copyfrom != ')')
168 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
169 long_magic_end = copyfrom;
170 copyfrom++;
171 } else {
172 /* shorthand */
173 for (copyfrom = elt + 1;
174 *copyfrom && *copyfrom != ':';
175 copyfrom++) {
176 char ch = *copyfrom;
178 if (!is_pathspec_magic(ch))
179 break;
180 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
181 if (pathspec_magic[i].mnemonic == ch) {
182 short_magic |= pathspec_magic[i].bit;
183 break;
185 if (ARRAY_SIZE(pathspec_magic) <= i)
186 die(_("Unimplemented pathspec magic '%c' in '%s'"),
187 ch, elt);
189 if (*copyfrom == ':')
190 copyfrom++;
193 magic |= short_magic;
194 *p_short_magic = short_magic;
196 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specifed */
197 if (noglob_global && !(magic & PATHSPEC_GLOB))
198 global_magic |= PATHSPEC_LITERAL;
200 /* --glob-pathspec is overriden by :(literal) */
201 if ((global_magic & PATHSPEC_GLOB) && (magic & PATHSPEC_LITERAL))
202 global_magic &= ~PATHSPEC_GLOB;
204 magic |= global_magic;
206 if (pathspec_prefix >= 0 &&
207 (prefixlen || (prefix && *prefix)))
208 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
210 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
211 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
213 if (pathspec_prefix >= 0) {
214 match = xstrdup(copyfrom);
215 prefixlen = pathspec_prefix;
216 } else if (magic & PATHSPEC_FROMTOP) {
217 match = xstrdup(copyfrom);
218 prefixlen = 0;
219 } else {
220 match = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom);
221 if (!match)
222 die(_("%s: '%s' is outside repository"), elt, copyfrom);
224 *raw = item->match = match;
226 * Prefix the pathspec (keep all magic) and assign to
227 * original. Useful for passing to another command.
229 if (flags & PATHSPEC_PREFIX_ORIGIN) {
230 struct strbuf sb = STRBUF_INIT;
231 const char *start = elt;
232 if (prefixlen && !literal_global) {
233 /* Preserve the actual prefix length of each pattern */
234 if (short_magic)
235 die("BUG: prefixing on short magic is not supported");
236 else if (long_magic_end) {
237 strbuf_add(&sb, start, long_magic_end - start);
238 strbuf_addf(&sb, ",prefix:%d", prefixlen);
239 start = long_magic_end;
240 } else {
241 if (*start == ':')
242 start++;
243 strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
246 strbuf_add(&sb, start, copyfrom - start);
247 strbuf_addstr(&sb, match);
248 item->original = strbuf_detach(&sb, NULL);
249 } else
250 item->original = elt;
251 item->len = strlen(item->match);
252 item->prefix = prefixlen;
254 if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
255 (item->len >= 1 && item->match[item->len - 1] == '/') &&
256 (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
257 S_ISGITLINK(active_cache[i]->ce_mode)) {
258 item->len--;
259 match[item->len] = '\0';
262 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
263 for (i = 0; i < active_nr; i++) {
264 struct cache_entry *ce = active_cache[i];
265 int ce_len = ce_namelen(ce);
267 if (!S_ISGITLINK(ce->ce_mode))
268 continue;
270 if (item->len <= ce_len || match[ce_len] != '/' ||
271 memcmp(ce->name, match, ce_len))
272 continue;
273 if (item->len == ce_len + 1) {
274 /* strip trailing slash */
275 item->len--;
276 match[item->len] = '\0';
277 } else
278 die (_("Pathspec '%s' is in submodule '%.*s'"),
279 elt, ce_len, ce->name);
282 if (magic & PATHSPEC_LITERAL)
283 item->nowildcard_len = item->len;
284 else {
285 item->nowildcard_len = simple_length(item->match);
286 if (item->nowildcard_len < prefixlen)
287 item->nowildcard_len = prefixlen;
289 item->flags = 0;
290 if (magic & PATHSPEC_GLOB) {
292 * FIXME: should we enable ONESTAR in _GLOB for
293 * pattern "* * / * . c"?
295 } else {
296 if (item->nowildcard_len < item->len &&
297 item->match[item->nowildcard_len] == '*' &&
298 no_wildcard(item->match + item->nowildcard_len + 1))
299 item->flags |= PATHSPEC_ONESTAR;
302 /* sanity checks, pathspec matchers assume these are sane */
303 assert(item->nowildcard_len <= item->len &&
304 item->prefix <= item->len);
305 return magic;
308 static int pathspec_item_cmp(const void *a_, const void *b_)
310 struct pathspec_item *a, *b;
312 a = (struct pathspec_item *)a_;
313 b = (struct pathspec_item *)b_;
314 return strcmp(a->match, b->match);
317 static void NORETURN unsupported_magic(const char *pattern,
318 unsigned magic,
319 unsigned short_magic)
321 struct strbuf sb = STRBUF_INIT;
322 int i, n;
323 for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
324 const struct pathspec_magic *m = pathspec_magic + i;
325 if (!(magic & m->bit))
326 continue;
327 if (sb.len)
328 strbuf_addstr(&sb, " ");
329 if (short_magic & m->bit)
330 strbuf_addf(&sb, "'%c'", m->mnemonic);
331 else
332 strbuf_addf(&sb, "'%s'", m->name);
333 n++;
336 * We may want to substitute "this command" with a command
337 * name. E.g. when add--interactive dies when running
338 * "checkout -p"
340 die(_("%s: pathspec magic not supported by this command: %s"),
341 pattern, sb.buf);
345 * Given command line arguments and a prefix, convert the input to
346 * pathspec. die() if any magic in magic_mask is used.
348 void parse_pathspec(struct pathspec *pathspec,
349 unsigned magic_mask, unsigned flags,
350 const char *prefix, const char **argv)
352 struct pathspec_item *item;
353 const char *entry = argv ? *argv : NULL;
354 int i, n, prefixlen;
356 memset(pathspec, 0, sizeof(*pathspec));
358 if (flags & PATHSPEC_MAXDEPTH_VALID)
359 pathspec->magic |= PATHSPEC_MAXDEPTH;
361 /* No arguments, no prefix -> no pathspec */
362 if (!entry && !prefix)
363 return;
365 if ((flags & PATHSPEC_PREFER_CWD) &&
366 (flags & PATHSPEC_PREFER_FULL))
367 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
369 /* No arguments with prefix -> prefix pathspec */
370 if (!entry) {
371 static const char *raw[2];
373 if (flags & PATHSPEC_PREFER_FULL)
374 return;
376 if (!(flags & PATHSPEC_PREFER_CWD))
377 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
379 pathspec->items = item = xmalloc(sizeof(*item));
380 memset(item, 0, sizeof(*item));
381 item->match = prefix;
382 item->original = prefix;
383 item->nowildcard_len = item->len = strlen(prefix);
384 item->prefix = item->len;
385 raw[0] = prefix;
386 raw[1] = NULL;
387 pathspec->nr = 1;
388 pathspec->_raw = raw;
389 return;
392 n = 0;
393 while (argv[n])
394 n++;
396 pathspec->nr = n;
397 pathspec->items = item = xmalloc(sizeof(*item) * n);
398 pathspec->_raw = argv;
399 prefixlen = prefix ? strlen(prefix) : 0;
401 for (i = 0; i < n; i++) {
402 unsigned short_magic;
403 entry = argv[i];
405 item[i].magic = prefix_pathspec(item + i, &short_magic,
406 argv + i, flags,
407 prefix, prefixlen, entry);
408 if (item[i].magic & magic_mask)
409 unsupported_magic(entry,
410 item[i].magic & magic_mask,
411 short_magic);
413 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
414 has_symlink_leading_path(item[i].match, item[i].len)) {
415 die(_("pathspec '%s' is beyond a symbolic link"), entry);
418 if (item[i].nowildcard_len < item[i].len)
419 pathspec->has_wildcard = 1;
420 pathspec->magic |= item[i].magic;
424 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
425 if (flags & PATHSPEC_KEEP_ORDER)
426 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
427 qsort(pathspec->items, pathspec->nr,
428 sizeof(struct pathspec_item), pathspec_item_cmp);
433 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
434 * based interface - see pathspec.c:parse_pathspec().
436 * Arguments:
437 * - prefix - a path relative to the root of the working tree
438 * - pathspec - a list of paths underneath the prefix path
440 * Iterates over pathspec, prepending each path with prefix,
441 * and return the resulting list.
443 * If pathspec is empty, return a singleton list containing prefix.
445 * If pathspec and prefix are both empty, return an empty list.
447 * This is typically used by built-in commands such as add.c, in order
448 * to normalize argv arguments provided to the built-in into a list of
449 * paths to process, all relative to the root of the working tree.
451 const char **get_pathspec(const char *prefix, const char **pathspec)
453 struct pathspec ps;
454 parse_pathspec(&ps,
455 PATHSPEC_ALL_MAGIC &
456 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
457 PATHSPEC_PREFER_CWD,
458 prefix, pathspec);
459 return ps._raw;
462 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
464 *dst = *src;
465 dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
466 memcpy(dst->items, src->items,
467 sizeof(struct pathspec_item) * dst->nr);
470 void free_pathspec(struct pathspec *pathspec)
472 free(pathspec->items);
473 pathspec->items = NULL;