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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jun 1, 2023 at 13:38 UTC
8e32caaa783a3a6b1d94834ee6e2d21ac36ff39f
2 files changed
+20
-7
pathspec.c
+12
-7
@@ -525,24 +525,29 @@ static int pathspec_item_cmp(const void *a_, const void *b_)
525
return strcmp(a->match, b->match);
526
}
527
528
-static void NORETURN unsupported_magic(const char *pattern,
529
- unsigned magic)
528
+void pathspec_magic_names(unsigned magic, struct strbuf *out)
529
{
531
- struct strbuf sb = STRBUF_INIT;
530
int i;
531
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
532
const struct pathspec_magic *m = pathspec_magic + i;
533
if (!(magic & m->bit))
534
continue;
537
- if (sb.len)
538
- strbuf_addstr(&sb, ", ");
535
+ if (out->len)
536
+ strbuf_addstr(out, ", ");
537
538
if (m->mnemonic)
541
- strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
539
+ strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
540
m->name, m->mnemonic);
541
else
544
- strbuf_addf(&sb, "'%s'", m->name);
542
+ strbuf_addf(out, "'%s'", m->name);
543
}
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);
551
/*
552
* We may want to substitute "this command" with a command
553
* name. E.g. when "git add -p" or "git add -i" dies when running
pathspec.h
+8
@@ -130,6 +130,14 @@ void parse_pathspec_file(struct pathspec *pathspec,
130
void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
131
void clear_pathspec(struct pathspec *);
132
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
+
141
static inline int ps_strncmp(const struct pathspec_item *item,
142
const char *s1, const char *s2, size_t n)
143
{