Skip to content

Commit 8e32caa

Browse files
peffgitster
authored andcommitted
pathspec: factor out magic-to-name function
When we have unsupported magic in a pathspec (because a command or code path does not support particular items), we list the unsupported ones in an error message. Let's factor out the code here that converts the bits back into their human-readable names, so that it can be used from other callers, which may want to provide more flexible error messages. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0d1bd1d commit 8e32caa

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

pathspec.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,24 +525,29 @@ static int pathspec_item_cmp(const void *a_, const void *b_)
525525
return strcmp(a->match, b->match);
526526
}
527527

528-
static void NORETURN unsupported_magic(const char *pattern,
529-
unsigned magic)
528+
void pathspec_magic_names(unsigned magic, struct strbuf *out)
530529
{
531-
struct strbuf sb = STRBUF_INIT;
532530
int i;
533531
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
534532
const struct pathspec_magic *m = pathspec_magic + i;
535533
if (!(magic & m->bit))
536534
continue;
537-
if (sb.len)
538-
strbuf_addstr(&sb, ", ");
535+
if (out->len)
536+
strbuf_addstr(out, ", ");
539537

540538
if (m->mnemonic)
541-
strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
539+
strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
542540
m->name, m->mnemonic);
543541
else
544-
strbuf_addf(&sb, "'%s'", m->name);
542+
strbuf_addf(out, "'%s'", m->name);
545543
}
544+
}
545+
546+
static void NORETURN unsupported_magic(const char *pattern,
547+
unsigned magic)
548+
{
549+
struct strbuf sb = STRBUF_INIT;
550+
pathspec_magic_names(magic, &sb);
546551
/*
547552
* We may want to substitute "this command" with a command
548553
* name. E.g. when "git add -p" or "git add -i" dies when running

pathspec.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ void parse_pathspec_file(struct pathspec *pathspec,
130130
void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
131131
void clear_pathspec(struct pathspec *);
132132

133+
/*
134+
* Add a human-readable string to "out" representing the PATHSPEC_* flags set
135+
* in "magic". The result is suitable for error messages, but not for
136+
* parsing as pathspec magic itself (you get 'icase' with quotes, not
137+
* :(icase)).
138+
*/
139+
void pathspec_magic_names(unsigned magic, struct strbuf *out);
140+
133141
static inline int ps_strncmp(const struct pathspec_item *item,
134142
const char *s1, const char *s2, size_t n)
135143
{

0 commit comments

Comments
 (0)