check return value of verify_ref_format()

Users of the ref-filter code must call verify_ref_format() before formatting any refs, but most ignore its return value. This means we may print an error on a syntactically bogus pattern, but keep going anyway. In most cases this results in a fatal error when we actually try to format a ref. But if you have no refs to show at all, then the behavior is confusing: git prints the error from verify_ref_format(), then exits with code 0 without showing any output. Let's instead abort immediately if we know we have a bogus format. We'll output the usage information if we have it handy (just like the existing call in cmd_for_each_ref() does), and otherwise just die(). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 13, 2017 at 10:56 UTC 2eda0102beb85b97bd43708176d81feabaf161e8
3 files changed +10 -5
builtin/branch.c
+3 -1
@@ -409,7 +409,9 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
409
410 if (!format)
411 format = to_free = build_format(filter, maxwidth, remote_prefix);
412 - verify_ref_format(format);
412 +
413 + if (verify_ref_format(format))
414 + die(_("unable to parse format string"));
415
416 ref_array_sort(sorting, &array);
417
builtin/tag.c
+4 -3
@@ -53,7 +53,8 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, con
53 format = "%(refname:lstrip=2)";
54 }
55
56 - verify_ref_format(format);
56 + if (verify_ref_format(format))
57 + die(_("unable to parse format string"));
58 filter->with_commit_tag_algo = 1;
59 filter_refs(&array, filter, FILTER_REFS_TAGS);
60 ref_array_sort(sorting, &array);
@@ -501,8 +502,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
502 if (cmdmode == 'd')
503 return for_each_tag_name(argv, delete_tag, NULL);
504 if (cmdmode == 'v') {
504 - if (format)
505 - verify_ref_format(format);
505 + if (format && verify_ref_format(format))
506 + usage_with_options(git_tag_usage, options);
507 return for_each_tag_name(argv, verify_tag, format);
508 }
509
builtin/verify-tag.c
+3 -1
@@ -51,7 +51,9 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix)
51 flags |= GPG_VERIFY_VERBOSE;
52
53 if (fmt_pretty) {
54 - verify_ref_format(fmt_pretty);
54 + if (verify_ref_format(fmt_pretty))
55 + usage_with_options(verify_tag_usage,
56 + verify_tag_options);
57 flags |= GPG_VERIFY_OMIT_STATUS;
58 }
59