Skip to content

Commit e36091a

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c2a3fd commit e36091a

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

builtin/ls-files.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,13 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
266266
struct strbuf sb = STRBUF_INIT;
267267

268268
while (strbuf_expand_step(&sb, &format)) {
269-
const char *end;
270269
size_t len;
271270
struct stat st;
272271

273272
if (skip_prefix(format, "%", &format))
274273
strbuf_addch(&sb, '%');
275274
else if ((len = strbuf_expand_literal(&sb, format)))
276275
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);
283276
else if (skip_prefix(format, "(objectmode)", &format))
284277
strbuf_addf(&sb, "%06o", ce->ce_mode);
285278
else if (skip_prefix(format, "(objectname)", &format))
@@ -308,8 +301,7 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
308301
else if (skip_prefix(format, "(path)", &format))
309302
write_name_to_buf(&sb, fullname);
310303
else
311-
die(_("bad ls-files format: %%%.*s"),
312-
(int)(end - format + 1), format);
304+
strbuf_expand_bad_format(format, "ls-files");
313305
}
314306
strbuf_addch(&sb, line_terminator);
315307
fwrite(sb.buf, sb.len, 1, stdout);

builtin/ls-tree.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,12 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
100100
return 0;
101101

102102
while (strbuf_expand_step(&sb, &format)) {
103-
const char *end;
104103
size_t len;
105104

106105
if (skip_prefix(format, "%", &format))
107106
strbuf_addch(&sb, '%');
108107
else if ((len = strbuf_expand_literal(&sb, format)))
109108
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);
116109
else if (skip_prefix(format, "(objectmode)", &format))
117110
strbuf_addf(&sb, "%06o", mode);
118111
else if (skip_prefix(format, "(objecttype)", &format))
@@ -135,8 +128,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
135128
strbuf_setlen(base, baselen);
136129
strbuf_release(&sbuf);
137130
} else
138-
die(_("bad ls-tree format: %%%.*s"),
139-
(int)(end - format + 1), format);
131+
strbuf_expand_bad_format(format, "ls-tree");
140132
}
141133
strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
142134
fwrite(sb.buf, sb.len, 1, stdout);

strbuf.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,26 @@ size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder)
442442
return 0;
443443
}
444444

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+
445465
void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
446466
{
447467
size_t i, len = src->len;

strbuf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder);
337337
*/
338338
int strbuf_expand_step(struct strbuf *sb, const char **formatp);
339339

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+
340345
/**
341346
* Append the contents of one strbuf to another, quoting any
342347
* percent signs ("%") into double-percents ("%%") in the

0 commit comments

Comments
 (0)