range-diff/format-patch: refactor check for commit range
Currently, when called with exactly two arguments, `git range-diff` tests for a literal `..` in each of the two. Likewise, the argument provided via `--range-diff` to `git format-patch` is checked in the same manner. However, `<commit>^!` is a perfectly valid commit range, equivalent to `<commit>^..<commit>` according to the `SPECIFYING RANGES` section of gitrevisions[7]. In preparation for allowing more sophisticated ways to specify commit ranges, let's refactor the check into its own function. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jan 27, 2021 at 16:37 UTC
679b5916cdafdcfa9fb36c31dbf53d7e4aa0af0b
4 files changed
+19
-5
builtin/log.c
+1
-1
@@ -1680,7 +1680,7 @@ static void infer_range_diff_ranges(struct strbuf *r1,
1680
struct commit *head)
1681
{
1682
const char *head_oid = oid_to_hex(&head->object.oid);
1683
- int prev_is_range = !!strstr(prev, "..");
1683
+ int prev_is_range = is_range_diff_range(prev);
1684
1685
if (prev_is_range)
1686
strbuf_addstr(r1, prev);
builtin/range-diff.c
+5
-4
@@ -3,6 +3,7 @@
3
#include "parse-options.h"
4
#include "range-diff.h"
5
#include "config.h"
6
+#include "revision.h"
7
8
static const char * const builtin_range_diff_usage[] = {
9
N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
@@ -46,12 +47,12 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
47
diffopt.use_color = 1;
48
49
if (argc == 2) {
49
- if (!strstr(argv[0], ".."))
50
- die(_("no .. in range: '%s'"), argv[0]);
50
+ if (!is_range_diff_range(argv[0]))
51
+ die(_("not a commit range: '%s'"), argv[0]);
52
strbuf_addstr(&range1, argv[0]);
53
53
- if (!strstr(argv[1], ".."))
54
- die(_("no .. in range: '%s'"), argv[1]);
54
+ if (!is_range_diff_range(argv[1]))
55
+ die(_("not a commit range: '%s'"), argv[1]);
56
strbuf_addstr(&range2, argv[1]);
57
} else if (argc == 3) {
58
strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
range-diff.c
+5
@@ -564,3 +564,8 @@ int show_range_diff(const char *range1, const char *range2,
564
565
return res;
566
}
567
+
568
+int is_range_diff_range(const char *arg)
569
+{
570
+ return !!strstr(arg, "..");
571
+}
range-diff.h
+8
@@ -16,4 +16,12 @@ int show_range_diff(const char *range1, const char *range2,
16
const struct diff_options *diffopt,
17
const struct strvec *other_arg);
18
19
+/*
20
+ * Determine whether the given argument is usable as a range argument of `git
21
+ * range-diff`, e.g. A..B. Note that this only validates the format but does
22
+ * _not_ parse it, i.e. it does _not_ look up the specified commits in the
23
+ * local repository.
24
+ */
25
+int is_range_diff_range(const char *arg);
26
+
27
#endif