Skip to content

Commit fc12261

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d2ce133 commit fc12261

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pathspec.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,20 @@ void parse_pathspec(struct pathspec *pathspec,
271271
if (!entry && !prefix)
272272
return;
273273

274+
if ((flags & PATHSPEC_PREFER_CWD) &&
275+
(flags & PATHSPEC_PREFER_FULL))
276+
die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
277+
274278
/* No arguments with prefix -> prefix pathspec */
275279
if (!entry) {
276280
static const char *raw[2];
277281

282+
if (flags & PATHSPEC_PREFER_FULL)
283+
return;
284+
285+
if (!(flags & PATHSPEC_PREFER_CWD))
286+
die("BUG: PATHSPEC_PREFER_CWD requires arguments");
287+
278288
pathspec->items = item = xmalloc(sizeof(*item));
279289
memset(item, 0, sizeof(*item));
280290
item->match = prefix;
@@ -340,7 +350,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
340350
struct pathspec ps;
341351
parse_pathspec(&ps,
342352
PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
343-
0, prefix, pathspec);
353+
PATHSPEC_PREFER_CWD,
354+
prefix, pathspec);
344355
return ps.raw;
345356
}
346357

pathspec.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ struct pathspec {
2424
} *items;
2525
};
2626

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+
2731
extern int init_pathspec(struct pathspec *, const char **);
2832
extern void parse_pathspec(struct pathspec *pathspec,
2933
unsigned magic_mask,

0 commit comments

Comments
 (0)