Skip to content

Commit 4cc302e

Browse files
committed
Merge branch 'rs/strbuf-expand-bad-format'
Code clean-up. * rs/strbuf-expand-bad-format: cat-file: use strbuf_expand_bad_format() factor out strbuf_expand_bad_format()
2 parents f046355 + 7c43bdf commit 4cc302e

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

builtin/cat-file.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ static int is_atom(const char *atom, const char *s, int slen)
314314
return alen == slen && !memcmp(atom, s, alen);
315315
}
316316

317-
static void expand_atom(struct strbuf *sb, const char *atom, int len,
318-
struct expand_data *data)
317+
static int expand_atom(struct strbuf *sb, const char *atom, int len,
318+
struct expand_data *data)
319319
{
320320
if (is_atom("objectname", atom, len)) {
321321
if (!data->mark_query)
@@ -347,7 +347,8 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
347347
strbuf_addstr(sb,
348348
oid_to_hex(&data->delta_base_oid));
349349
} else
350-
die("unknown format element: %.*s", len, atom);
350+
return 0;
351+
return 1;
351352
}
352353

353354
static void expand_format(struct strbuf *sb, const char *start,
@@ -358,12 +359,11 @@ static void expand_format(struct strbuf *sb, const char *start,
358359

359360
if (skip_prefix(start, "%", &start) || *start != '(')
360361
strbuf_addch(sb, '%');
361-
else if (!(end = strchr(start + 1, ')')))
362-
die("format element '%s' does not end in ')'", start);
363-
else {
364-
expand_atom(sb, start + 1, end - start - 1, data);
362+
else if ((end = strchr(start + 1, ')')) &&
363+
expand_atom(sb, start + 1, end - start - 1, data))
365364
start = end + 1;
366-
}
365+
else
366+
strbuf_expand_bad_format(start, "cat-file");
367367
}
368368
}
369369

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)