]> git.ipfire.org Git - thirdparty/git.git/blob - pathspec.c
pathspec: factor global magic into its own function
[thirdparty/git.git] / pathspec.c
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
16 * to use find_pathspecs_matching_against_index() instead.
17 */
18 void add_pathspec_matches_against_index(const struct pathspec *pathspec,
19 char *seen)
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 */
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 ce_path_match(ce, pathspec, seen);
37 }
38 }
39
40 /*
41 * Finds which of the given pathspecs match items in the index.
42 *
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.
47 */
48 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
49 {
50 char *seen = xcalloc(pathspec->nr, 1);
51 add_pathspec_matches_against_index(pathspec, seen);
52 return seen;
53 }
54
55 /*
56 * Magic pathspec
57 *
58 * Possible future magic semantics include stuff like:
59 *
60 * { PATHSPEC_RECURSIVE, '*', "recursive" },
61 * { PATHSPEC_REGEXP, '\0', "regexp" },
62 *
63 */
64
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" },
74 { PATHSPEC_EXCLUDE, '!', "exclude" },
75 };
76
77 static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
78 {
79 int i;
80 strbuf_addstr(sb, ":(");
81 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
82 if (magic & pathspec_magic[i].bit) {
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
90 static 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
100 static 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
110 static 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
120 static 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
130 static 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
159 /*
160 * Take an element of a pathspec and check for magic signatures.
161 * Append the result to the prefix. Return the magic bitmap.
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.
171 */
172 static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
173 const char *prefix, int prefixlen,
174 const char *elt)
175 {
176 unsigned magic = 0, element_magic = 0;
177 const char *copyfrom = elt;
178 char *match;
179 int i, pathspec_prefix = -1;
180
181 if (elt[0] != ':' || get_literal_global() ||
182 (flags & PATHSPEC_LITERAL_PATH)) {
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)
197 continue;
198 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
199 if (strlen(pathspec_magic[i].name) == len &&
200 !strncmp(pathspec_magic[i].name, copyfrom, len)) {
201 element_magic |= pathspec_magic[i].bit;
202 break;
203 }
204 if (starts_with(copyfrom, "prefix:")) {
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 }
214 if (ARRAY_SIZE(pathspec_magic) <= i)
215 die(_("Invalid pathspec magic '%.*s' in '%s'"),
216 (int) len, copyfrom, elt);
217 }
218 if (*copyfrom != ')')
219 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
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) {
232 element_magic |= pathspec_magic[i].bit;
233 break;
234 }
235 if (ARRAY_SIZE(pathspec_magic) <= i)
236 die(_("Unimplemented pathspec magic '%c' in '%s'"),
237 ch, elt);
238 }
239 if (*copyfrom == ':')
240 copyfrom++;
241 }
242
243 magic |= element_magic;
244
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);
250
251 if (pathspec_prefix >= 0 &&
252 (prefixlen || (prefix && *prefix)))
253 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
254
255 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
256 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
257
258 if (pathspec_prefix >= 0) {
259 match = xstrdup(copyfrom);
260 prefixlen = pathspec_prefix;
261 } else if (magic & PATHSPEC_FROMTOP) {
262 match = xstrdup(copyfrom);
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 }
269 item->match = match;
270 /*
271 * Prefix the pathspec (keep all magic) and assign to
272 * original. Useful for passing to another command.
273 */
274 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
275 prefixlen && !get_literal_global()) {
276 struct strbuf sb = STRBUF_INIT;
277
278 /* Preserve the actual prefix length of each pattern */
279 prefix_magic(&sb, prefixlen, element_magic);
280
281 strbuf_addstr(&sb, match);
282 item->original = strbuf_detach(&sb, NULL);
283 } else {
284 item->original = xstrdup(elt);
285 }
286 item->len = strlen(item->match);
287 item->prefix = prefixlen;
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
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
317 if (magic & PATHSPEC_LITERAL)
318 item->nowildcard_len = item->len;
319 else {
320 item->nowildcard_len = simple_length(item->match);
321 if (item->nowildcard_len < prefixlen)
322 item->nowildcard_len = prefixlen;
323 }
324 item->flags = 0;
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;
335 }
336
337 /* sanity checks, pathspec matchers assume these are sane */
338 assert(item->nowildcard_len <= item->len &&
339 item->prefix <= item->len);
340 return magic;
341 }
342
343 static 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
352 static void NORETURN unsupported_magic(const char *pattern,
353 unsigned magic)
354 {
355 struct strbuf sb = STRBUF_INIT;
356 int i;
357 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
358 const struct pathspec_magic *m = pathspec_magic + i;
359 if (!(magic & m->bit))
360 continue;
361 if (sb.len)
362 strbuf_addstr(&sb, ", ");
363
364 if (m->mnemonic)
365 strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
366 m->name, m->mnemonic);
367 else
368 strbuf_addf(&sb, "'%s'", m->name);
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);
377 }
378
379 /*
380 * Given command line arguments and a prefix, convert the input to
381 * pathspec. die() if any magic in magic_mask is used.
382 */
383 void parse_pathspec(struct pathspec *pathspec,
384 unsigned magic_mask, unsigned flags,
385 const char *prefix, const char **argv)
386 {
387 struct pathspec_item *item;
388 const char *entry = argv ? *argv : NULL;
389 int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
390
391 memset(pathspec, 0, sizeof(*pathspec));
392
393 if (flags & PATHSPEC_MAXDEPTH_VALID)
394 pathspec->magic |= PATHSPEC_MAXDEPTH;
395
396 /* No arguments, no prefix -> no pathspec */
397 if (!entry && !prefix)
398 return;
399
400 if ((flags & PATHSPEC_PREFER_CWD) &&
401 (flags & PATHSPEC_PREFER_FULL))
402 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
403
404 /* No arguments with prefix -> prefix pathspec */
405 if (!entry) {
406 if (flags & PATHSPEC_PREFER_FULL)
407 return;
408
409 if (!(flags & PATHSPEC_PREFER_CWD))
410 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
411
412 pathspec->items = item = xcalloc(1, sizeof(*item));
413 item->match = xstrdup(prefix);
414 item->original = xstrdup(prefix);
415 item->nowildcard_len = item->len = strlen(prefix);
416 item->prefix = item->len;
417 pathspec->nr = 1;
418 return;
419 }
420
421 n = 0;
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 }
429 n++;
430 }
431
432 pathspec->nr = n;
433 ALLOC_ARRAY(pathspec->items, n);
434 item = pathspec->items;
435 prefixlen = prefix ? strlen(prefix) : 0;
436
437 for (i = 0; i < n; i++) {
438 entry = argv[i];
439
440 item[i].magic = prefix_pathspec(item + i, flags,
441 prefix, prefixlen, entry);
442
443 if (item[i].magic & PATHSPEC_EXCLUDE)
444 nr_exclude++;
445 if (item[i].magic & magic_mask)
446 unsupported_magic(entry, item[i].magic & magic_mask);
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
453 if (item[i].nowildcard_len < item[i].len)
454 pathspec->has_wildcard = 1;
455 pathspec->magic |= item[i].magic;
456 }
457
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
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");
466 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
467 }
468 }
469
470 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
471 {
472 int i;
473
474 *dst = *src;
475 ALLOC_ARRAY(dst->items, dst->nr);
476 COPY_ARRAY(dst->items, src->items, dst->nr);
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 }
482 }
483
484 void clear_pathspec(struct pathspec *pathspec)
485 {
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 }
492 free(pathspec->items);
493 pathspec->items = NULL;
494 pathspec->nr = 0;
495 }