parse_pathspec: support stripping submodule trailing slashes
[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 */
4b78d7bc
AS
18void add_pathspec_matches_against_index(const char **pathspec,
19 char *seen, int specs)
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 */
29 for (i = 0; i < specs; i++)
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];
36 match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
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 */
4b78d7bc 48char *find_pathspecs_matching_against_index(const char **pathspec)
6f525e71
AS
49{
50 char *seen;
51 int i;
52
53 for (i = 0; pathspec[i]; i++)
54 ; /* just counting */
55 seen = xcalloc(i, 1);
4b78d7bc 56 add_pathspec_matches_against_index(pathspec, seen, i);
6f525e71
AS
57 return seen;
58}
9d67b61f
AS
59
60/*
61 * Check the index to see whether path refers to a submodule, or
62 * something inside a submodule. If the former, returns the path with
63 * any trailing slash stripped. If the latter, dies with an error
64 * message.
65 */
66const char *check_path_for_gitlink(const char *path)
67{
68 int i, path_len = strlen(path);
69 for (i = 0; i < active_nr; i++) {
70 struct cache_entry *ce = active_cache[i];
71 if (S_ISGITLINK(ce->ce_mode)) {
72 int ce_len = ce_namelen(ce);
73 if (path_len <= ce_len || path[ce_len] != '/' ||
74 memcmp(ce->name, path, ce_len))
75 /* path does not refer to this
76 * submodule or anything inside it */
77 continue;
78 if (path_len == ce_len + 1) {
79 /* path refers to submodule;
80 * strip trailing slash */
81 return xstrndup(ce->name, ce_len);
82 } else {
83 die (_("Path '%s' is in submodule '%.*s'"),
84 path, ce_len, ce->name);
85 }
86 }
87 }
88 return path;
89}
512aaf94
AS
90
91/*
92 * Dies if the given path refers to a file inside a symlinked
93 * directory in the index.
94 */
95void die_if_path_beyond_symlink(const char *path, const char *prefix)
96{
97 if (has_symlink_leading_path(path, strlen(path))) {
98 int len = prefix ? strlen(prefix) : 0;
99 die(_("'%s' is beyond a symbolic link"), path + len);
100 }
101}
64acde94
NTND
102
103/*
104 * Magic pathspec
105 *
64acde94
NTND
106 * Possible future magic semantics include stuff like:
107 *
108 * { PATHSPEC_NOGLOB, '!', "noglob" },
109 * { PATHSPEC_ICASE, '\0', "icase" },
110 * { PATHSPEC_RECURSIVE, '*', "recursive" },
111 * { PATHSPEC_REGEXP, '\0', "regexp" },
112 *
113 */
64acde94
NTND
114
115static struct pathspec_magic {
116 unsigned bit;
117 char mnemonic; /* this cannot be ':'! */
118 const char *name;
119} pathspec_magic[] = {
120 { PATHSPEC_FROMTOP, '/', "top" },
121};
122
123/*
124 * Take an element of a pathspec and check for magic signatures.
87323bda 125 * Append the result to the prefix. Return the magic bitmap.
64acde94
NTND
126 *
127 * For now, we only parse the syntax and throw out anything other than
128 * "top" magic.
129 *
130 * NEEDSWORK: This needs to be rewritten when we start migrating
131 * get_pathspec() users to use the "struct pathspec" interface. For
132 * example, a pathspec element may be marked as case-insensitive, but
133 * the prefix part must always match literally, and a single stupid
134 * string cannot express such a case.
135 */
87323bda
NTND
136static unsigned prefix_pathspec(struct pathspec_item *item,
137 unsigned *p_short_magic,
138 const char **raw, unsigned flags,
139 const char *prefix, int prefixlen,
140 const char *elt)
64acde94 141{
87323bda 142 unsigned magic = 0, short_magic = 0;
64acde94 143 const char *copyfrom = elt;
87323bda 144 char *match;
64acde94
NTND
145 int i;
146
147 if (elt[0] != ':') {
148 ; /* nothing to do */
149 } else if (elt[1] == '(') {
150 /* longhand */
151 const char *nextat;
152 for (copyfrom = elt + 2;
153 *copyfrom && *copyfrom != ')';
154 copyfrom = nextat) {
155 size_t len = strcspn(copyfrom, ",)");
156 if (copyfrom[len] == ',')
157 nextat = copyfrom + len + 1;
158 else
159 /* handle ')' and '\0' */
160 nextat = copyfrom + len;
161 if (!len)
162 continue;
163 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
164 if (strlen(pathspec_magic[i].name) == len &&
165 !strncmp(pathspec_magic[i].name, copyfrom, len)) {
166 magic |= pathspec_magic[i].bit;
167 break;
168 }
169 if (ARRAY_SIZE(pathspec_magic) <= i)
f01d9820 170 die(_("Invalid pathspec magic '%.*s' in '%s'"),
64acde94
NTND
171 (int) len, copyfrom, elt);
172 }
173 if (*copyfrom != ')')
f01d9820 174 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
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;
200
64acde94 201 if (magic & PATHSPEC_FROMTOP)
87323bda 202 match = xstrdup(copyfrom);
64acde94 203 else
87323bda
NTND
204 match = prefix_path(prefix, prefixlen, copyfrom);
205 *raw = item->match = match;
d2ce1331 206 item->original = elt;
87323bda 207 item->len = strlen(item->match);
b69bb3fc
NTND
208
209 if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
210 (item->len >= 1 && item->match[item->len - 1] == '/') &&
211 (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
212 S_ISGITLINK(active_cache[i]->ce_mode)) {
213 item->len--;
214 match[item->len] = '\0';
215 }
216
87323bda
NTND
217 if (limit_pathspec_to_literal())
218 item->nowildcard_len = item->len;
219 else
220 item->nowildcard_len = simple_length(item->match);
221 item->flags = 0;
222 if (item->nowildcard_len < item->len &&
223 item->match[item->nowildcard_len] == '*' &&
224 no_wildcard(item->match + item->nowildcard_len + 1))
225 item->flags |= PATHSPEC_ONESTAR;
226 return magic;
227}
228
229static int pathspec_item_cmp(const void *a_, const void *b_)
230{
231 struct pathspec_item *a, *b;
232
233 a = (struct pathspec_item *)a_;
234 b = (struct pathspec_item *)b_;
235 return strcmp(a->match, b->match);
236}
237
238static void NORETURN unsupported_magic(const char *pattern,
239 unsigned magic,
240 unsigned short_magic)
241{
242 struct strbuf sb = STRBUF_INIT;
243 int i, n;
244 for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
245 const struct pathspec_magic *m = pathspec_magic + i;
246 if (!(magic & m->bit))
247 continue;
248 if (sb.len)
249 strbuf_addstr(&sb, " ");
250 if (short_magic & m->bit)
251 strbuf_addf(&sb, "'%c'", m->mnemonic);
252 else
253 strbuf_addf(&sb, "'%s'", m->name);
254 n++;
255 }
256 /*
257 * We may want to substitute "this command" with a command
258 * name. E.g. when add--interactive dies when running
259 * "checkout -p"
260 */
261 die(_("%s: pathspec magic not supported by this command: %s"),
262 pattern, sb.buf);
263}
264
265/*
266 * Given command line arguments and a prefix, convert the input to
267 * pathspec. die() if any magic in magic_mask is used.
268 */
269void parse_pathspec(struct pathspec *pathspec,
270 unsigned magic_mask, unsigned flags,
271 const char *prefix, const char **argv)
272{
273 struct pathspec_item *item;
274 const char *entry = argv ? *argv : NULL;
275 int i, n, prefixlen;
276
277 memset(pathspec, 0, sizeof(*pathspec));
278
6330a171
NTND
279 if (flags & PATHSPEC_MAXDEPTH_VALID)
280 pathspec->magic |= PATHSPEC_MAXDEPTH;
281
87323bda
NTND
282 /* No arguments, no prefix -> no pathspec */
283 if (!entry && !prefix)
284 return;
285
fc12261f
NTND
286 if ((flags & PATHSPEC_PREFER_CWD) &&
287 (flags & PATHSPEC_PREFER_FULL))
288 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
289
87323bda
NTND
290 /* No arguments with prefix -> prefix pathspec */
291 if (!entry) {
292 static const char *raw[2];
293
fc12261f
NTND
294 if (flags & PATHSPEC_PREFER_FULL)
295 return;
296
297 if (!(flags & PATHSPEC_PREFER_CWD))
298 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
299
87323bda
NTND
300 pathspec->items = item = xmalloc(sizeof(*item));
301 memset(item, 0, sizeof(*item));
302 item->match = prefix;
d2ce1331 303 item->original = prefix;
87323bda
NTND
304 item->nowildcard_len = item->len = strlen(prefix);
305 raw[0] = prefix;
306 raw[1] = NULL;
307 pathspec->nr = 1;
308 pathspec->raw = raw;
309 return;
310 }
311
312 n = 0;
313 while (argv[n])
314 n++;
315
316 pathspec->nr = n;
317 pathspec->items = item = xmalloc(sizeof(*item) * n);
318 pathspec->raw = argv;
319 prefixlen = prefix ? strlen(prefix) : 0;
320
321 for (i = 0; i < n; i++) {
322 unsigned short_magic;
323 entry = argv[i];
324
325 item[i].magic = prefix_pathspec(item + i, &short_magic,
326 argv + i, flags,
327 prefix, prefixlen, entry);
328 if (item[i].magic & magic_mask)
329 unsupported_magic(entry,
330 item[i].magic & magic_mask,
331 short_magic);
332 if (item[i].nowildcard_len < item[i].len)
333 pathspec->has_wildcard = 1;
334 pathspec->magic |= item[i].magic;
335 }
336
6330a171
NTND
337 if (pathspec->magic & PATHSPEC_MAXDEPTH)
338 qsort(pathspec->items, pathspec->nr,
339 sizeof(struct pathspec_item), pathspec_item_cmp);
64acde94
NTND
340}
341
342/*
343 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
87323bda 344 * based interface - see pathspec.c:parse_pathspec().
64acde94
NTND
345 *
346 * Arguments:
347 * - prefix - a path relative to the root of the working tree
348 * - pathspec - a list of paths underneath the prefix path
349 *
350 * Iterates over pathspec, prepending each path with prefix,
351 * and return the resulting list.
352 *
353 * If pathspec is empty, return a singleton list containing prefix.
354 *
355 * If pathspec and prefix are both empty, return an empty list.
356 *
357 * This is typically used by built-in commands such as add.c, in order
358 * to normalize argv arguments provided to the built-in into a list of
359 * paths to process, all relative to the root of the working tree.
360 */
361const char **get_pathspec(const char *prefix, const char **pathspec)
362{
87323bda
NTND
363 struct pathspec ps;
364 parse_pathspec(&ps,
365 PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
fc12261f
NTND
366 PATHSPEC_PREFER_CWD,
367 prefix, pathspec);
87323bda 368 return ps.raw;
64acde94 369}
e4d92cdc
NTND
370
371void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
372{
373 *dst = *src;
374 dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
375 memcpy(dst->items, src->items,
376 sizeof(struct pathspec_item) * dst->nr);
377}