ref-filter: factor out "unrecognized %(foo) arg" errors
Atom parsers that take arguments generally have a catch-all for "this arg is not recognized". Most of them use the same printf template, which is good, because it makes life easier for translators. Let's pull this template into a helper function, which makes the code in the parsers shorter and avoids any possibility of differences. As with the previous commit, we'll pick an arbitrary atom to make sure the test suite covers this code. 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:20 UTC
dda4fc1a849e2407c30b4619d64221c2b38bd570
2 files changed
+17
-5
ref-filter.c
+11
-5
@@ -234,6 +234,12 @@ static int err_no_arg(struct strbuf *sb, const char *name)
234
return -1;
235
}
236
237
+static int err_bad_arg(struct strbuf *sb, const char *name, const char *arg)
238
+{
239
+ strbuf_addf(sb, _("unrecognized %%(%s) argument: %s"), name, arg);
240
+ return -1;
241
+}
242
+
243
static int color_atom_parser(struct ref_format *format, struct used_atom *atom,
244
const char *color_value, struct strbuf *err)
245
{
@@ -347,7 +353,7 @@ static int objectsize_atom_parser(struct ref_format *format, struct used_atom *a
353
else
354
oi.info.disk_sizep = &oi.disk_size;
355
} else
350
- return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "objectsize", arg);
356
+ return err_bad_arg(err, "objectsize", arg);
357
return 0;
358
}
359
@@ -380,7 +386,7 @@ static int subject_atom_parser(struct ref_format *format, struct used_atom *atom
386
else if (!strcmp(arg, "sanitize"))
387
atom->u.contents.option = C_SUB_SANITIZE;
388
else
383
- return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "subject", arg);
389
+ return err_bad_arg(err, "subject", arg);
390
return 0;
391
}
392
@@ -434,7 +440,7 @@ static int contents_atom_parser(struct ref_format *format, struct used_atom *ato
440
if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
441
return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
442
} else
437
- return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "contents", arg);
443
+ return err_bad_arg(err, "contents", arg);
444
return 0;
445
}
446
@@ -446,7 +452,7 @@ static int raw_atom_parser(struct ref_format *format, struct used_atom *atom,
452
else if (!strcmp(arg, "size"))
453
atom->u.raw_data.option = RAW_LENGTH;
454
else
449
- return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "raw", arg);
455
+ return err_bad_arg(err, "raw", arg);
456
return 0;
457
}
458
@@ -563,7 +569,7 @@ static int if_atom_parser(struct ref_format *format, struct used_atom *atom,
569
} else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
570
atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
571
} else
566
- return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "if", arg);
572
+ return err_bad_arg(err, "if", arg);
573
return 0;
574
}
575
t/t6300-for-each-ref.sh
+6
@@ -1248,6 +1248,12 @@ test_expect_success 'HEAD atom does not take arguments' '
1248
test_cmp expect err
1249
'
1250
1251
+test_expect_success 'subject atom rejects unknown arguments' '
1252
+ test_must_fail git for-each-ref --format="%(subject:foo)" 2>err &&
1253
+ echo "fatal: unrecognized %(subject) argument: foo" >expect &&
1254
+ test_cmp expect err
1255
+'
1256
+
1257
test_expect_success 'trailer parsing not fooled by --- line' '
1258
git commit --allow-empty -F - <<-\EOF &&
1259
this is the subject