cat-file: use strbuf_expand_bad_format()

Report unknown format elements and missing closing parentheses with consistent and translated messages by calling strbuf_expand_bad_format() at the very end of the combined if/else chain of expand_format() and expand_atom(). Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 24, 2024 at 12:21 UTC 7c43bdf07b63ce86572bec1db9d19a160c8de8cd
1 file changed +8 -8
builtin/cat-file.c
+8 -8
@@ -310,8 +310,8 @@ static int is_atom(const char *atom, const char *s, int slen)
310 return alen == slen && !memcmp(atom, s, alen);
311 }
312
313 -static void expand_atom(struct strbuf *sb, const char *atom, int len,
314 - struct expand_data *data)
313 +static int expand_atom(struct strbuf *sb, const char *atom, int len,
314 + struct expand_data *data)
315 {
316 if (is_atom("objectname", atom, len)) {
317 if (!data->mark_query)
@@ -343,7 +343,8 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
343 strbuf_addstr(sb,
344 oid_to_hex(&data->delta_base_oid));
345 } else
346 - die("unknown format element: %.*s", len, atom);
346 + return 0;
347 + return 1;
348 }
349
350 static void expand_format(struct strbuf *sb, const char *start,
@@ -354,12 +355,11 @@ static void expand_format(struct strbuf *sb, const char *start,
355
356 if (skip_prefix(start, "%", &start) || *start != '(')
357 strbuf_addch(sb, '%');
357 - else if (!(end = strchr(start + 1, ')')))
358 - die("format element '%s' does not end in ')'", start);
359 - else {
360 - expand_atom(sb, start + 1, end - start - 1, data);
358 + else if ((end = strchr(start + 1, ')')) &&
359 + expand_atom(sb, start + 1, end - start - 1, data))
360 start = end + 1;
362 - }
361 + else
362 + strbuf_expand_bad_format(start, "cat-file");
363 }
364 }
365