Skip to content

Commit 0087d7d

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edd6a31 commit 0087d7d

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

builtin/range-diff.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
4040
struct option *options;
4141
int res = 0;
4242
struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
43+
struct object_id oid;
4344

4445
git_config(git_diff_ui_config, NULL);
4546

@@ -56,24 +57,41 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
5657
diffopt.use_color = 1;
5758

5859
if (argc == 3) {
60+
if (get_oid_committish(argv[0], &oid))
61+
usage_msg_optf(_("not a revision: '%s'"),
62+
builtin_range_diff_usage, options,
63+
argv[0]);
64+
else if (get_oid_committish(argv[1], &oid))
65+
usage_msg_optf(_("not a revision: '%s'"),
66+
builtin_range_diff_usage, options,
67+
argv[1]);
68+
else if (get_oid_committish(argv[2], &oid))
69+
usage_msg_optf(_("not a revision: '%s'"),
70+
builtin_range_diff_usage, options,
71+
argv[2]);
72+
5973
strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
6074
strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
6175
} else if (argc == 2) {
6276
if (!is_range_diff_range(argv[0]))
63-
die(_("not a commit range: '%s'"), argv[0]);
64-
strbuf_addstr(&range1, argv[0]);
77+
usage_msg_optf(_("not a commit range: '%s'"),
78+
builtin_range_diff_usage, options,
79+
argv[0]);
80+
else if (!is_range_diff_range(argv[1]))
81+
usage_msg_optf(_("not a commit range: '%s'"),
82+
builtin_range_diff_usage, options,
83+
argv[1]);
6584

66-
if (!is_range_diff_range(argv[1]))
67-
die(_("not a commit range: '%s'"), argv[1]);
85+
strbuf_addstr(&range1, argv[0]);
6886
strbuf_addstr(&range2, argv[1]);
6987
} else if (argc == 1) {
7088
const char *b = strstr(argv[0], "..."), *a = argv[0];
7189
int a_len;
7290

73-
if (!b) {
74-
error(_("single arg format must be symmetric range"));
75-
usage_with_options(builtin_range_diff_usage, options);
76-
}
91+
if (!b)
92+
usage_msg_optf(_("not a symmetric range: '%s'"),
93+
builtin_range_diff_usage, options,
94+
argv[0]);
7795

7896
a_len = (int)(b - a);
7997
if (!a_len) {
@@ -85,10 +103,9 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
85103
b = "HEAD";
86104
strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
87105
strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
88-
} else {
89-
error(_("need two commit ranges"));
90-
usage_with_options(builtin_range_diff_usage, options);
91-
}
106+
} else
107+
usage_msg_opt(_("need two commit ranges"),
108+
builtin_range_diff_usage, options);
92109
FREE_AND_NULL(options);
93110

94111
range_diff_opts.dual_color = simple_color < 1;

0 commit comments

Comments
 (0)