ref-filter: factor out "%(foo) does not take arguments" errors
Many atom parsers give the same error message, differing only in the name of the atom. If we use "%s does not take arguments", that should make life easier for translators, as they only need to translate one string. And in doing so, we can easily pull it into a helper function to make sure they are all using the exact same string. I've added a basic test here for %(HEAD), just to make sure this code is exercised at all in the test suite. We could cover each such atom, but the effort-to-reward ratio of trying to maintain an exhaustive list doesn't seem worth it. Signed-off-by: Jeff King <peff@peff.net> Acked-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Dec 14, 2022 at 11:19 UTC
a33d0fae76ab95e88d383793cac41934920296ba
2 files changed
+17
-5
ref-filter.c
+11
-5
@@ -228,6 +228,12 @@ static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
228
return ret;
229
}
230
231
+static int err_no_arg(struct strbuf *sb, const char *name)
232
+{
233
+ strbuf_addf(sb, _("%%(%s) does not take arguments"), name);
234
+ return -1;
235
+}
236
+
237
static int color_atom_parser(struct ref_format *format, struct used_atom *atom,
238
const char *color_value, struct strbuf *err)
239
{
@@ -317,7 +323,7 @@ static int objecttype_atom_parser(struct ref_format *format, struct used_atom *a
323
const char *arg, struct strbuf *err)
324
{
325
if (arg)
320
- return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments"));
326
+ return err_no_arg(err, "objecttype");
327
if (*atom->name == '*')
328
oi_deref.info.typep = &oi_deref.type;
329
else
@@ -349,7 +355,7 @@ static int deltabase_atom_parser(struct ref_format *format, struct used_atom *at
355
const char *arg, struct strbuf *err)
356
{
357
if (arg)
352
- return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments"));
358
+ return err_no_arg(err, "deltabase");
359
if (*atom->name == '*')
360
oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid;
361
else
@@ -361,7 +367,7 @@ static int body_atom_parser(struct ref_format *format, struct used_atom *atom,
367
const char *arg, struct strbuf *err)
368
{
369
if (arg)
364
- return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments"));
370
+ return err_no_arg(err, "body");
371
atom->u.contents.option = C_BODY_DEP;
372
return 0;
373
}
@@ -565,7 +571,7 @@ static int rest_atom_parser(struct ref_format *format, struct used_atom *atom,
571
const char *arg, struct strbuf *err)
572
{
573
if (arg)
568
- return strbuf_addf_ret(err, -1, _("%%(rest) does not take arguments"));
574
+ return err_no_arg(err, "rest");
575
format->use_rest = 1;
576
return 0;
577
}
@@ -574,7 +580,7 @@ static int head_atom_parser(struct ref_format *format, struct used_atom *atom,
580
const char *arg, struct strbuf *err)
581
{
582
if (arg)
577
- return strbuf_addf_ret(err, -1, _("%%(HEAD) does not take arguments"));
583
+ return err_no_arg(err, "HEAD");
584
atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
585
return 0;
586
}
t/t6300-for-each-ref.sh
+6
@@ -1242,6 +1242,12 @@ test_expect_success 'basic atom: rest must fail' '
1242
test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1243
'
1244
1245
+test_expect_success 'HEAD atom does not take arguments' '
1246
+ test_must_fail git for-each-ref --format="%(HEAD:foo)" 2>err &&
1247
+ echo "fatal: %(HEAD) does not take arguments" >expect &&
1248
+ test_cmp expect err
1249
+'
1250
+
1251
test_expect_success 'trailer parsing not fooled by --- line' '
1252
git commit --allow-empty -F - <<-\EOF &&
1253
this is the subject