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>
Johannes Schindelin committed
Aug 26, 2022 at 09:39 UTC
0087d7dfbebf10f23394f32db3c283eb7ab03129
1 file changed
+29
-12
builtin/range-diff.c
+29
-12
@@ -40,6 +40,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
40
struct option *options;
41
int res = 0;
42
struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
43
+ struct object_id oid;
44
45
git_config(git_diff_ui_config, NULL);
46
@@ -56,24 +57,41 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
57
diffopt.use_color = 1;
58
59
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
+
73
strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
74
strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
75
} else if (argc == 2) {
76
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]);
84
66
- if (!is_range_diff_range(argv[1]))
67
- die(_("not a commit range: '%s'"), argv[1]);
85
+ strbuf_addstr(&range1, argv[0]);
86
strbuf_addstr(&range2, argv[1]);
87
} else if (argc == 1) {
88
const char *b = strstr(argv[0], "..."), *a = argv[0];
89
int a_len;
90
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]);
95
96
a_len = (int)(b - a);
97
if (!a_len) {
@@ -85,10 +103,9 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
103
b = "HEAD";
104
strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
105
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);
109
FREE_AND_NULL(options);
110
111
range_diff_opts.dual_color = simple_color < 1;