factor out strbuf_expand_bad_format()
Extract a function for reporting placeholders that are not enclosed in a parenthesis or are unknown. This reduces the number of strings to translate and improves consistency across commands. Call it at the end of the if/else chain, after exhausting all accepted possibilities. 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:19 UTC
e36091aa1d67cedba02ea9de9245f0ff14a52f15
4 files changed
+27
-18
builtin/ls-files.c
+1
-9
@@ -266,7 +266,6 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
266
struct strbuf sb = STRBUF_INIT;
267
268
while (strbuf_expand_step(&sb, &format)) {
269
- const char *end;
269
size_t len;
270
struct stat st;
271
@@ -274,12 +273,6 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
273
strbuf_addch(&sb, '%');
274
else if ((len = strbuf_expand_literal(&sb, format)))
275
format += len;
277
- else if (*format != '(')
278
- die(_("bad ls-files format: element '%s' "
279
- "does not start with '('"), format);
280
- else if (!(end = strchr(format + 1, ')')))
281
- die(_("bad ls-files format: element '%s' "
282
- "does not end in ')'"), format);
276
else if (skip_prefix(format, "(objectmode)", &format))
277
strbuf_addf(&sb, "%06o", ce->ce_mode);
278
else if (skip_prefix(format, "(objectname)", &format))
@@ -308,8 +301,7 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
301
else if (skip_prefix(format, "(path)", &format))
302
write_name_to_buf(&sb, fullname);
303
else
311
- die(_("bad ls-files format: %%%.*s"),
312
- (int)(end - format + 1), format);
304
+ strbuf_expand_bad_format(format, "ls-files");
305
}
306
strbuf_addch(&sb, line_terminator);
307
fwrite(sb.buf, sb.len, 1, stdout);
builtin/ls-tree.c
+1
-9
@@ -100,19 +100,12 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
100
return 0;
101
102
while (strbuf_expand_step(&sb, &format)) {
103
- const char *end;
103
size_t len;
104
105
if (skip_prefix(format, "%", &format))
106
strbuf_addch(&sb, '%');
107
else if ((len = strbuf_expand_literal(&sb, format)))
108
format += len;
110
- else if (*format != '(')
111
- die(_("bad ls-tree format: element '%s' "
112
- "does not start with '('"), format);
113
- else if (!(end = strchr(format + 1, ')')))
114
- die(_("bad ls-tree format: element '%s' "
115
- "does not end in ')'"), format);
109
else if (skip_prefix(format, "(objectmode)", &format))
110
strbuf_addf(&sb, "%06o", mode);
111
else if (skip_prefix(format, "(objecttype)", &format))
@@ -135,8 +128,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
128
strbuf_setlen(base, baselen);
129
strbuf_release(&sbuf);
130
} else
138
- die(_("bad ls-tree format: %%%.*s"),
139
- (int)(end - format + 1), format);
131
+ strbuf_expand_bad_format(format, "ls-tree");
132
}
133
strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
134
fwrite(sb.buf, sb.len, 1, stdout);
strbuf.c
+20
@@ -442,6 +442,26 @@ size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder)
442
return 0;
443
}
444
445
+void strbuf_expand_bad_format(const char *format, const char *command)
446
+{
447
+ const char *end;
448
+
449
+ if (*format != '(')
450
+ /* TRANSLATORS: The first %s is a command like "ls-tree". */
451
+ die(_("bad %s format: element '%s' does not start with '('"),
452
+ command, format);
453
+
454
+ end = strchr(format + 1, ')');
455
+ if (!end)
456
+ /* TRANSLATORS: The first %s is a command like "ls-tree". */
457
+ die(_("bad %s format: element '%s' does not end in ')'"),
458
+ command, format);
459
+
460
+ /* TRANSLATORS: %s is a command like "ls-tree". */
461
+ die(_("bad %s format: %%%.*s"),
462
+ command, (int)(end - format + 1), format);
463
+}
464
+
465
void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
466
{
467
size_t i, len = src->len;
strbuf.h
+5
@@ -337,6 +337,11 @@ size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder);
337
*/
338
int strbuf_expand_step(struct strbuf *sb, const char **formatp);
339
340
+/**
341
+ * Used with `strbuf_expand_step` to report unknown placeholders.
342
+ */
343
+void strbuf_expand_bad_format(const char *format, const char *command);
344
+
345
/**
346
* Append the contents of one strbuf to another, quoting any
347
* percent signs ("%") into double-percents ("%%") in the