rename field "raw" to "_raw" in struct 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{
87323bda 94 unsigned magic = 0, short_magic = 0;
64acde94 95 const char *copyfrom = elt;
87323bda 96 char *match;
64acde94
NTND
97 int i;
98
99 if (elt[0] != ':') {
100 ; /* nothing to do */
101 } else if (elt[1] == '(') {
102 /* longhand */
103 const char *nextat;
104 for (copyfrom = elt + 2;
105 *copyfrom && *copyfrom != ')';
106 copyfrom = nextat) {
107 size_t len = strcspn(copyfrom, ",)");
108 if (copyfrom[len] == ',')
109 nextat = copyfrom + len + 1;
110 else
111 /* handle ')' and '\0' */
112 nextat = copyfrom + len;
113 if (!len)
114 continue;
115 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
116 if (strlen(pathspec_magic[i].name) == len &&
117 !strncmp(pathspec_magic[i].name, copyfrom, len)) {
118 magic |= pathspec_magic[i].bit;
119 break;
120 }
121 if (ARRAY_SIZE(pathspec_magic) <= i)
f01d9820 122 die(_("Invalid pathspec magic '%.*s' in '%s'"),
64acde94
NTND
123 (int) len, copyfrom, elt);
124 }
125 if (*copyfrom != ')')
f01d9820 126 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
64acde94
NTND
127 copyfrom++;
128 } else {
129 /* shorthand */
130 for (copyfrom = elt + 1;
131 *copyfrom && *copyfrom != ':';
132 copyfrom++) {
133 char ch = *copyfrom;
134
135 if (!is_pathspec_magic(ch))
136 break;
137 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
138 if (pathspec_magic[i].mnemonic == ch) {
87323bda 139 short_magic |= pathspec_magic[i].bit;
64acde94
NTND
140 break;
141 }
142 if (ARRAY_SIZE(pathspec_magic) <= i)
f01d9820 143 die(_("Unimplemented pathspec magic '%c' in '%s'"),
64acde94
NTND
144 ch, elt);
145 }
146 if (*copyfrom == ':')
147 copyfrom++;
148 }
149
87323bda
NTND
150 magic |= short_magic;
151 *p_short_magic = short_magic;
152
64acde94 153 if (magic & PATHSPEC_FROMTOP)
87323bda 154 match = xstrdup(copyfrom);
64acde94 155 else
87323bda
NTND
156 match = prefix_path(prefix, prefixlen, copyfrom);
157 *raw = item->match = match;
dad2586a
NTND
158 /*
159 * Prefix the pathspec (keep all magic) and assign to
160 * original. Useful for passing to another command.
161 */
162 if (flags & PATHSPEC_PREFIX_ORIGIN) {
163 struct strbuf sb = STRBUF_INIT;
164 strbuf_add(&sb, elt, copyfrom - elt);
165 strbuf_addstr(&sb, match);
166 item->original = strbuf_detach(&sb, NULL);
167 } else
168 item->original = elt;
87323bda 169 item->len = strlen(item->match);
b69bb3fc
NTND
170
171 if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
172 (item->len >= 1 && item->match[item->len - 1] == '/') &&
173 (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
174 S_ISGITLINK(active_cache[i]->ce_mode)) {
175 item->len--;
176 match[item->len] = '\0';
177 }
178
87450244
NTND
179 if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
180 for (i = 0; i < active_nr; i++) {
181 struct cache_entry *ce = active_cache[i];
182 int ce_len = ce_namelen(ce);
183
184 if (!S_ISGITLINK(ce->ce_mode))
185 continue;
186
187 if (item->len <= ce_len || match[ce_len] != '/' ||
188 memcmp(ce->name, match, ce_len))
189 continue;
190 if (item->len == ce_len + 1) {
191 /* strip trailing slash */
192 item->len--;
193 match[item->len] = '\0';
194 } else
195 die (_("Pathspec '%s' is in submodule '%.*s'"),
196 elt, ce_len, ce->name);
197 }
198
87323bda
NTND
199 if (limit_pathspec_to_literal())
200 item->nowildcard_len = item->len;
201 else
202 item->nowildcard_len = simple_length(item->match);
203 item->flags = 0;
204 if (item->nowildcard_len < item->len &&
205 item->match[item->nowildcard_len] == '*' &&
206 no_wildcard(item->match + item->nowildcard_len + 1))
207 item->flags |= PATHSPEC_ONESTAR;
208 return magic;
209}
210
211static int pathspec_item_cmp(const void *a_, const void *b_)
212{
213 struct pathspec_item *a, *b;
214
215 a = (struct pathspec_item *)a_;
216 b = (struct pathspec_item *)b_;
217 return strcmp(a->match, b->match);
218}
219
220static void NORETURN unsupported_magic(const char *pattern,
221 unsigned magic,
222 unsigned short_magic)
223{
224 struct strbuf sb = STRBUF_INIT;
225 int i, n;
226 for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
227 const struct pathspec_magic *m = pathspec_magic + i;
228 if (!(magic & m->bit))
229 continue;
230 if (sb.len)
231 strbuf_addstr(&sb, " ");
232 if (short_magic & m->bit)
233 strbuf_addf(&sb, "'%c'", m->mnemonic);
234 else
235 strbuf_addf(&sb, "'%s'", m->name);
236 n++;
237 }
238 /*
239 * We may want to substitute "this command" with a command
240 * name. E.g. when add--interactive dies when running
241 * "checkout -p"
242 */
243 die(_("%s: pathspec magic not supported by this command: %s"),
244 pattern, sb.buf);
245}
246
247/*
248 * Given command line arguments and a prefix, convert the input to
249 * pathspec. die() if any magic in magic_mask is used.
250 */
251void parse_pathspec(struct pathspec *pathspec,
252 unsigned magic_mask, unsigned flags,
253 const char *prefix, const char **argv)
254{
255 struct pathspec_item *item;
256 const char *entry = argv ? *argv : NULL;
257 int i, n, prefixlen;
258
259 memset(pathspec, 0, sizeof(*pathspec));
260
6330a171
NTND
261 if (flags & PATHSPEC_MAXDEPTH_VALID)
262 pathspec->magic |= PATHSPEC_MAXDEPTH;
263
87323bda
NTND
264 /* No arguments, no prefix -> no pathspec */
265 if (!entry && !prefix)
266 return;
267
fc12261f
NTND
268 if ((flags & PATHSPEC_PREFER_CWD) &&
269 (flags & PATHSPEC_PREFER_FULL))
270 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
271
87323bda
NTND
272 /* No arguments with prefix -> prefix pathspec */
273 if (!entry) {
274 static const char *raw[2];
275
fc12261f
NTND
276 if (flags & PATHSPEC_PREFER_FULL)
277 return;
278
279 if (!(flags & PATHSPEC_PREFER_CWD))
280 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
281
87323bda
NTND
282 pathspec->items = item = xmalloc(sizeof(*item));
283 memset(item, 0, sizeof(*item));
284 item->match = prefix;
d2ce1331 285 item->original = prefix;
87323bda
NTND
286 item->nowildcard_len = item->len = strlen(prefix);
287 raw[0] = prefix;
288 raw[1] = NULL;
289 pathspec->nr = 1;
b3920bbd 290 pathspec->_raw = raw;
87323bda
NTND
291 return;
292 }
293
294 n = 0;
295 while (argv[n])
296 n++;
297
298 pathspec->nr = n;
299 pathspec->items = item = xmalloc(sizeof(*item) * n);
b3920bbd 300 pathspec->_raw = argv;
87323bda
NTND
301 prefixlen = prefix ? strlen(prefix) : 0;
302
303 for (i = 0; i < n; i++) {
304 unsigned short_magic;
305 entry = argv[i];
306
307 item[i].magic = prefix_pathspec(item + i, &short_magic,
308 argv + i, flags,
309 prefix, prefixlen, entry);
310 if (item[i].magic & magic_mask)
311 unsupported_magic(entry,
312 item[i].magic & magic_mask,
313 short_magic);
87450244
NTND
314
315 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
316 has_symlink_leading_path(item[i].match, item[i].len)) {
317 die(_("pathspec '%s' is beyond a symbolic link"), entry);
318 }
319
87323bda
NTND
320 if (item[i].nowildcard_len < item[i].len)
321 pathspec->has_wildcard = 1;
322 pathspec->magic |= item[i].magic;
323 }
324
931eab64
NTND
325
326 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
327 if (flags & PATHSPEC_KEEP_ORDER)
328 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
6330a171
NTND
329 qsort(pathspec->items, pathspec->nr,
330 sizeof(struct pathspec_item), pathspec_item_cmp);
931eab64 331 }
64acde94
NTND
332}
333
334/*
335 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
87323bda 336 * based interface - see pathspec.c:parse_pathspec().
64acde94
NTND
337 *
338 * Arguments:
339 * - prefix - a path relative to the root of the working tree
340 * - pathspec - a list of paths underneath the prefix path
341 *
342 * Iterates over pathspec, prepending each path with prefix,
343 * and return the resulting list.
344 *
345 * If pathspec is empty, return a singleton list containing prefix.
346 *
347 * If pathspec and prefix are both empty, return an empty list.
348 *
349 * This is typically used by built-in commands such as add.c, in order
350 * to normalize argv arguments provided to the built-in into a list of
351 * paths to process, all relative to the root of the working tree.
352 */
353const char **get_pathspec(const char *prefix, const char **pathspec)
354{
87323bda
NTND
355 struct pathspec ps;
356 parse_pathspec(&ps,
357 PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
fc12261f
NTND
358 PATHSPEC_PREFER_CWD,
359 prefix, pathspec);
b3920bbd 360 return ps._raw;
64acde94 361}
e4d92cdc
NTND
362
363void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
364{
365 *dst = *src;
366 dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
367 memcpy(dst->items, src->items,
368 sizeof(struct pathspec_item) * dst->nr);
369}
9a087274
NTND
370
371void free_pathspec(struct pathspec *pathspec)
372{
373 free(pathspec->items);
374 pathspec->items = NULL;
375}