range-diff: consistently validate the arguments

This patch lets `range-diff` validate the arguments not only when
invoked with one or two arguments, but also in the code path where three
arguments are handled.

While at it, we now use `usage_msg_opt*()` consistently.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johannes Schindelin 2022-08-26 09:39:29 +00:00 committed by Junio C Hamano
parent edd6a31f46
commit 0087d7dfbe

View File

@ -40,6 +40,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
struct option *options; struct option *options;
int res = 0; int res = 0;
struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT; struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
struct object_id oid;
git_config(git_diff_ui_config, NULL); git_config(git_diff_ui_config, NULL);
@ -56,24 +57,41 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
diffopt.use_color = 1; diffopt.use_color = 1;
if (argc == 3) { if (argc == 3) {
if (get_oid_committish(argv[0], &oid))
usage_msg_optf(_("not a revision: '%s'"),
builtin_range_diff_usage, options,
argv[0]);
else if (get_oid_committish(argv[1], &oid))
usage_msg_optf(_("not a revision: '%s'"),
builtin_range_diff_usage, options,
argv[1]);
else if (get_oid_committish(argv[2], &oid))
usage_msg_optf(_("not a revision: '%s'"),
builtin_range_diff_usage, options,
argv[2]);
strbuf_addf(&range1, "%s..%s", argv[0], argv[1]); strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
strbuf_addf(&range2, "%s..%s", argv[0], argv[2]); strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
} else if (argc == 2) { } else if (argc == 2) {
if (!is_range_diff_range(argv[0])) if (!is_range_diff_range(argv[0]))
die(_("not a commit range: '%s'"), argv[0]); usage_msg_optf(_("not a commit range: '%s'"),
strbuf_addstr(&range1, argv[0]); builtin_range_diff_usage, options,
argv[0]);
else if (!is_range_diff_range(argv[1]))
usage_msg_optf(_("not a commit range: '%s'"),
builtin_range_diff_usage, options,
argv[1]);
if (!is_range_diff_range(argv[1])) strbuf_addstr(&range1, argv[0]);
die(_("not a commit range: '%s'"), argv[1]);
strbuf_addstr(&range2, argv[1]); strbuf_addstr(&range2, argv[1]);
} else if (argc == 1) { } else if (argc == 1) {
const char *b = strstr(argv[0], "..."), *a = argv[0]; const char *b = strstr(argv[0], "..."), *a = argv[0];
int a_len; int a_len;
if (!b) { if (!b)
error(_("single arg format must be symmetric range")); usage_msg_optf(_("not a symmetric range: '%s'"),
usage_with_options(builtin_range_diff_usage, options); builtin_range_diff_usage, options,
} argv[0]);
a_len = (int)(b - a); a_len = (int)(b - a);
if (!a_len) { if (!a_len) {
@ -85,10 +103,9 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
b = "HEAD"; b = "HEAD";
strbuf_addf(&range1, "%s..%.*s", b, a_len, a); strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
strbuf_addf(&range2, "%.*s..%s", a_len, a, b); strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
} else { } else
error(_("need two commit ranges")); usage_msg_opt(_("need two commit ranges"),
usage_with_options(builtin_range_diff_usage, options); builtin_range_diff_usage, options);
}
FREE_AND_NULL(options); FREE_AND_NULL(options);
range_diff_opts.dual_color = simple_color < 1; range_diff_opts.dual_color = simple_color < 1;