add parse_pathspec() that converts cmdline args to struct pathspec

Currently to fill a struct pathspec, we do: const char **paths; paths = get_pathspec(prefix, argv); ... init_pathspec(&pathspec, paths); "paths" can only carry bare strings, which loses information from command line arguments such as pathspec magic or the prefix part's length for each argument. parse_pathspec() is introduced to combine the two calls into one. The plan is gradually replace all get_pathspec() and init_pathspec() with parse_pathspec(). get_pathspec() now becomes a thin wrapper of parse_pathspec(). parse_pathspec() allows the caller to reject the pathspec magics that it does not support. When a new pathspec magic is introduced, we can enable it per command after making sure that all underlying code has no problem with the new magic. "flags" parameter is currently unused. But it would allow callers to pass certain instructions to parse_pathspec, for example forcing literal pathspec when no magic is used. With the introduction of parse_pathspec, there are now two functions that can initialize struct pathspec: init_pathspec and parse_pathspec. Any semantic changes in struct pathspec must be reflected in both functions. init_pathspec() will be phased out in favor of parse_pathspec(). Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jul 14, 2013 at 15:35 UTC 87323bdace47c3d464a783f48b16617720e8eeee
5 files changed +163 -41
Documentation/technical/api-setup.txt
+18 -1
@@ -8,6 +8,23 @@ Talk about
8 * is_inside_git_dir()
9 * is_inside_work_tree()
10 * setup_work_tree()
11 -* get_pathspec()
11
12 (Dscho)
13 +
14 +Pathspec
15 +--------
16 +
17 +See glossary-context.txt for the syntax of pathspec. In memory, a
18 +pathspec set is represented by "struct pathspec" and is prepared by
19 +parse_pathspec(). This function takes several arguments:
20 +
21 +- magic_mask specifies what features that are NOT supported by the
22 + following code. If a user attempts to use such a feature,
23 + parse_pathspec() can reject it early.
24 +
25 +- flags specifies other things that the caller wants parse_pathspec to
26 + perform.
27 +
28 +- prefix and args come from cmd_* functions
29 +
30 +get_pathspec() is obsolete and should never be used in new code.
dir.c
+2 -2
@@ -381,7 +381,7 @@ int match_pathspec_depth(const struct pathspec *ps,
381 /*
382 * Return the length of the "simple" part of a path match limiter.
383 */
384 -static int simple_length(const char *match)
384 +int simple_length(const char *match)
385 {
386 int len = -1;
387
@@ -393,7 +393,7 @@ static int simple_length(const char *match)
393 }
394 }
395
396 -static int no_wildcard(const char *string)
396 +int no_wildcard(const char *string)
397 {
398 return string[simple_length(string)] == '\0';
399 }
dir.h
+2
@@ -128,6 +128,8 @@ struct dir_struct {
128 #define MATCHED_RECURSIVELY 1
129 #define MATCHED_FNMATCH 2
130 #define MATCHED_EXACTLY 3
131 +extern int simple_length(const char *match);
132 +extern int no_wildcard(const char *string);
133 extern char *common_prefix(const char **pathspec);
134 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
135 extern int match_pathspec_depth(const struct pathspec *pathspec,
pathspec.c
+130 -38
@@ -103,10 +103,6 @@ void die_if_path_beyond_symlink(const char *path, const char *prefix)
103 /*
104 * Magic pathspec
105 *
106 - * NEEDSWORK: These need to be moved to dir.h or even to a new
107 - * pathspec.h when we restructure get_pathspec() users to use the
108 - * "struct pathspec" interface.
109 - *
106 * Possible future magic semantics include stuff like:
107 *
108 * { PATHSPEC_NOGLOB, '!', "noglob" },
@@ -115,7 +111,6 @@ void die_if_path_beyond_symlink(const char *path, const char *prefix)
111 * { PATHSPEC_REGEXP, '\0', "regexp" },
112 *
113 */
118 -#define PATHSPEC_FROMTOP (1<<0)
114
115 static struct pathspec_magic {
116 unsigned bit;
@@ -127,7 +122,7 @@ static struct pathspec_magic {
122
123 /*
124 * Take an element of a pathspec and check for magic signatures.
130 - * Append the result to the prefix.
125 + * Append the result to the prefix. Return the magic bitmap.
126 *
127 * For now, we only parse the syntax and throw out anything other than
128 * "top" magic.
@@ -138,10 +133,15 @@ static struct pathspec_magic {
133 * the prefix part must always match literally, and a single stupid
134 * string cannot express such a case.
135 */
141 -static const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)
136 +static 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)
141 {
143 - unsigned magic = 0;
142 + unsigned magic = 0, short_magic = 0;
143 const char *copyfrom = elt;
144 + char *match;
145 int i;
146
147 if (elt[0] != ':') {
@@ -184,7 +184,7 @@ static const char *prefix_pathspec(const char *prefix, int prefixlen, const char
184 break;
185 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
186 if (pathspec_magic[i].mnemonic == ch) {
187 - magic |= pathspec_magic[i].bit;
187 + short_magic |= pathspec_magic[i].bit;
188 break;
189 }
190 if (ARRAY_SIZE(pathspec_magic) <= i)
@@ -195,15 +195,128 @@ static const char *prefix_pathspec(const char *prefix, int prefixlen, const char
195 copyfrom++;
196 }
197
198 + magic |= short_magic;
199 + *p_short_magic = short_magic;
200 +
201 if (magic & PATHSPEC_FROMTOP)
199 - return xstrdup(copyfrom);
202 + match = xstrdup(copyfrom);
203 else
201 - return prefix_path(prefix, prefixlen, copyfrom);
204 + match = prefix_path(prefix, prefixlen, copyfrom);
205 + *raw = item->match = match;
206 + item->len = strlen(item->match);
207 + if (limit_pathspec_to_literal())
208 + item->nowildcard_len = item->len;
209 + else
210 + item->nowildcard_len = simple_length(item->match);
211 + item->flags = 0;
212 + if (item->nowildcard_len < item->len &&
213 + item->match[item->nowildcard_len] == '*' &&
214 + no_wildcard(item->match + item->nowildcard_len + 1))
215 + item->flags |= PATHSPEC_ONESTAR;
216 + return magic;
217 +}
218 +
219 +static int pathspec_item_cmp(const void *a_, const void *b_)
220 +{
221 + struct pathspec_item *a, *b;
222 +
223 + a = (struct pathspec_item *)a_;
224 + b = (struct pathspec_item *)b_;
225 + return strcmp(a->match, b->match);
226 +}
227 +
228 +static void NORETURN unsupported_magic(const char *pattern,
229 + unsigned magic,
230 + unsigned short_magic)
231 +{
232 + struct strbuf sb = STRBUF_INIT;
233 + int i, n;
234 + for (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
235 + const struct pathspec_magic *m = pathspec_magic + i;
236 + if (!(magic & m->bit))
237 + continue;
238 + if (sb.len)
239 + strbuf_addstr(&sb, " ");
240 + if (short_magic & m->bit)
241 + strbuf_addf(&sb, "'%c'", m->mnemonic);
242 + else
243 + strbuf_addf(&sb, "'%s'", m->name);
244 + n++;
245 + }
246 + /*
247 + * We may want to substitute "this command" with a command
248 + * name. E.g. when add--interactive dies when running
249 + * "checkout -p"
250 + */
251 + die(_("%s: pathspec magic not supported by this command: %s"),
252 + pattern, sb.buf);
253 +}
254 +
255 +/*
256 + * Given command line arguments and a prefix, convert the input to
257 + * pathspec. die() if any magic in magic_mask is used.
258 + */
259 +void parse_pathspec(struct pathspec *pathspec,
260 + unsigned magic_mask, unsigned flags,
261 + const char *prefix, const char **argv)
262 +{
263 + struct pathspec_item *item;
264 + const char *entry = argv ? *argv : NULL;
265 + int i, n, prefixlen;
266 +
267 + memset(pathspec, 0, sizeof(*pathspec));
268 +
269 + /* No arguments, no prefix -> no pathspec */
270 + if (!entry && !prefix)
271 + return;
272 +
273 + /* No arguments with prefix -> prefix pathspec */
274 + if (!entry) {
275 + static const char *raw[2];
276 +
277 + pathspec->items = item = xmalloc(sizeof(*item));
278 + memset(item, 0, sizeof(*item));
279 + item->match = prefix;
280 + item->nowildcard_len = item->len = strlen(prefix);
281 + raw[0] = prefix;
282 + raw[1] = NULL;
283 + pathspec->nr = 1;
284 + pathspec->raw = raw;
285 + return;
286 + }
287 +
288 + n = 0;
289 + while (argv[n])
290 + n++;
291 +
292 + pathspec->nr = n;
293 + pathspec->items = item = xmalloc(sizeof(*item) * n);
294 + pathspec->raw = argv;
295 + prefixlen = prefix ? strlen(prefix) : 0;
296 +
297 + for (i = 0; i < n; i++) {
298 + unsigned short_magic;
299 + entry = argv[i];
300 +
301 + item[i].magic = prefix_pathspec(item + i, &short_magic,
302 + argv + i, flags,
303 + prefix, prefixlen, entry);
304 + if (item[i].magic & magic_mask)
305 + unsupported_magic(entry,
306 + item[i].magic & magic_mask,
307 + short_magic);
308 + if (item[i].nowildcard_len < item[i].len)
309 + pathspec->has_wildcard = 1;
310 + pathspec->magic |= item[i].magic;
311 + }
312 +
313 + qsort(pathspec->items, pathspec->nr,
314 + sizeof(struct pathspec_item), pathspec_item_cmp);
315 }
316
317 /*
318 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
206 - * based interface - see pathspec_magic above.
319 + * based interface - see pathspec.c:parse_pathspec().
320 *
321 * Arguments:
322 * - prefix - a path relative to the root of the working tree
@@ -222,32 +335,11 @@ static const char *prefix_pathspec(const char *prefix, int prefixlen, const char
335 */
336 const char **get_pathspec(const char *prefix, const char **pathspec)
337 {
225 - const char *entry = *pathspec;
226 - const char **src, **dst;
227 - int prefixlen;
228 -
229 - if (!prefix && !entry)
230 - return NULL;
231 -
232 - if (!entry) {
233 - static const char *spec[2];
234 - spec[0] = prefix;
235 - spec[1] = NULL;
236 - return spec;
237 - }
238 -
239 - /* Otherwise we have to re-write the entries.. */
240 - src = pathspec;
241 - dst = pathspec;
242 - prefixlen = prefix ? strlen(prefix) : 0;
243 - while (*src) {
244 - *(dst++) = prefix_pathspec(prefix, prefixlen, *src);
245 - src++;
246 - }
247 - *dst = NULL;
248 - if (!*pathspec)
249 - return NULL;
250 - return pathspec;
338 + struct pathspec ps;
339 + parse_pathspec(&ps,
340 + PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
341 + 0, prefix, pathspec);
342 + return ps.raw;
343 }
344
345 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
pathspec.h
+11
@@ -1,6 +1,10 @@
1 #ifndef PATHSPEC_H
2 #define PATHSPEC_H
3
4 +/* Pathspec magic */
5 +#define PATHSPEC_FROMTOP (1<<0)
6 +#define PATHSPEC_ALL_MAGIC PATHSPEC_FROMTOP
7 +
8 #define PATHSPEC_ONESTAR 1 /* the pathspec pattern sastisfies GFNM_ONESTAR */
9
10 struct pathspec {
@@ -8,9 +12,11 @@ struct pathspec {
12 int nr;
13 unsigned int has_wildcard:1;
14 unsigned int recursive:1;
15 + unsigned magic;
16 int max_depth;
17 struct pathspec_item {
18 const char *match;
19 + unsigned magic;
20 int len;
21 int nowildcard_len;
22 int flags;
@@ -18,6 +24,11 @@ struct pathspec {
24 };
25
26 extern int init_pathspec(struct pathspec *, const char **);
27 +extern void parse_pathspec(struct pathspec *pathspec,
28 + unsigned magic_mask,
29 + unsigned flags,
30 + const char *prefix,
31 + const char **args);
32 extern void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
33 extern void free_pathspec(struct pathspec *);
34