git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] cat-file: use strbuf_expand_bad_format()

From
René Scharfe <l.s.r@web.de>
Date
Mar 24, 2024, 08:04 UTC
Message-ID
<62c7ec20-4c1f-4a17-9deb-30699215251a@web.de>
In-Reply-To
<27cdcde7-74bc-4ee8-bc84-9a6046292cae@web.de>

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>
---
 builtin/cat-file.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index bbf851138e..fadf2da2f0 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -310,8 +310,8 @@ static int is_atom(const char *atom, const char *s, int slen)
 	return alen == slen && !memcmp(atom, s, alen);
 }

-static void expand_atom(struct strbuf *sb, const char *atom, int len,
-			struct expand_data *data)
+static int expand_atom(struct strbuf *sb, const char *atom, int len,
+		       struct expand_data *data)
 {
 	if (is_atom("objectname", atom, len)) {
 		if (!data->mark_query)
@@ -343,7 +343,8 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
 			strbuf_addstr(sb,
 				      oid_to_hex(&data->delta_base_oid));
 	} else
-		die("unknown format element: %.*s", len, atom);
+		return 0;
+	return 1;
 }

 static void expand_format(struct strbuf *sb, const char *start,
@@ -354,12 +355,11 @@ static void expand_format(struct strbuf *sb, const char *start,

 		if (skip_prefix(start, "%", &start) || *start != '(')
 			strbuf_addch(sb, '%');
-		else if (!(end = strchr(start + 1, ')')))
-			die("format element '%s' does not end in ')'", start);
-		else {
-			expand_atom(sb, start + 1, end - start - 1, data);
+		else if ((end = strchr(start + 1, ')')) &&
+			 expand_atom(sb, start + 1, end - start - 1, data))
 			start = end + 1;
-		}
+		else
+			strbuf_expand_bad_format(start, "cat-file");
 	}
 }

--
2.44.0
Previous: René ScharfeNext: Chris Torek
Message 2 of 7 in “factor out strbuf_expand_bad_format()”
  1. 1/2 factor out strbuf_expand_bad_format()René Scharfe, Mar 24, 2024
  2. 2/2 cat-file: use strbuf_expand_bad_format()René Scharfe, Mar 24, 2024
  3. Chris TorekMar 24, 2024
  4. René ScharfeMar 24, 2024
  5. 1/2 factor out strbuf_expand_bad_format()René Scharfe, Mar 24, 2024
  6. Junio C HamanoMar 24, 2024
  7. 2/2 cat-file: use strbuf_expand_bad_format()René Scharfe, Mar 24, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.