parse_pathspec: add PATHSPEC_PREFER_{CWD,FULL} flags
We have two ways of dealing with empty pathspec: 1. limit it to current prefix 2. match the entire working directory Some commands go with #1, some #2. get_pathspec() and parse_pathspec() only support #1. Make parse_pathspec() reject empty pathspec by default. #1 and #2 can be specified via new flags. This makes it more expressive about default behavior at command level. 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
fc12261fea778cabd08dec707169cb88ecdbc647
2 files changed
+16
-1
pathspec.c
+12
-1
@@ -271,10 +271,20 @@ void parse_pathspec(struct pathspec *pathspec,
271
if (!entry && !prefix)
272
return;
273
274
+ if ((flags & PATHSPEC_PREFER_CWD) &&
275
+ (flags & PATHSPEC_PREFER_FULL))
276
+ die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
277
+
278
/* No arguments with prefix -> prefix pathspec */
279
if (!entry) {
280
static const char *raw[2];
281
282
+ if (flags & PATHSPEC_PREFER_FULL)
283
+ return;
284
+
285
+ if (!(flags & PATHSPEC_PREFER_CWD))
286
+ die("BUG: PATHSPEC_PREFER_CWD requires arguments");
287
+
288
pathspec->items = item = xmalloc(sizeof(*item));
289
memset(item, 0, sizeof(*item));
290
item->match = prefix;
@@ -340,7 +350,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
350
struct pathspec ps;
351
parse_pathspec(&ps,
352
PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
343
- 0, prefix, pathspec);
353
+ PATHSPEC_PREFER_CWD,
354
+ prefix, pathspec);
355
return ps.raw;
356
}
357
pathspec.h
+4
@@ -24,6 +24,10 @@ struct pathspec {
24
} *items;
25
};
26
27
+/* parse_pathspec flags */
28
+#define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */
29
+#define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */
30
+
31
extern int init_pathspec(struct pathspec *, const char **);
32
extern void parse_pathspec(struct pathspec *pathspec,
33
unsigned magic_mask,