]> git.ipfire.org Git - thirdparty/git.git/commitdiff
range-diff/format-patch: refactor check for commit range
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Wed, 27 Jan 2021 16:37:22 +0000 (16:37 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Jan 2021 06:01:49 +0000 (22:01 -0800)
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>
builtin/log.c
builtin/range-diff.c
range-diff.c
range-diff.h

index f23ccdbec3263dffe87666baed28c7f0cdb76047..91466c059c79cbab32d294cf01cbc59e3df13e50 100644 (file)
@@ -1680,7 +1680,7 @@ static void infer_range_diff_ranges(struct strbuf *r1,
                                    struct commit *head)
 {
        const char *head_oid = oid_to_hex(&head->object.oid);
-       int prev_is_range = !!strstr(prev, "..");
+       int prev_is_range = is_range_diff_range(prev);
 
        if (prev_is_range)
                strbuf_addstr(r1, prev);
index 24c4162f7446ce89ff2fe9866bb0dc678785bed4..5b1f6326322f853a4a26a2cb2f2962d6f91ad67a 100644 (file)
@@ -3,6 +3,7 @@
 #include "parse-options.h"
 #include "range-diff.h"
 #include "config.h"
+#include "revision.h"
 
 static const char * const builtin_range_diff_usage[] = {
 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)
                diffopt.use_color = 1;
 
        if (argc == 2) {
-               if (!strstr(argv[0], ".."))
-                       die(_("no .. in range: '%s'"), argv[0]);
+               if (!is_range_diff_range(argv[0]))
+                       die(_("not a commit range: '%s'"), argv[0]);
                strbuf_addstr(&range1, argv[0]);
 
-               if (!strstr(argv[1], ".."))
-                       die(_("no .. in range: '%s'"), argv[1]);
+               if (!is_range_diff_range(argv[1]))
+                       die(_("not a commit range: '%s'"), argv[1]);
                strbuf_addstr(&range2, argv[1]);
        } else if (argc == 3) {
                strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
index b9950f10c8c486b16a2b916b0898d88c8d82fb59..9b93e08e8407477dff3b47859515a182afcfa7c7 100644 (file)
@@ -564,3 +564,8 @@ int show_range_diff(const char *range1, const char *range2,
 
        return res;
 }
+
+int is_range_diff_range(const char *arg)
+{
+       return !!strstr(arg, "..");
+}
index 583ced2e8e749003b9a22fe2496d0db49f5ba7f9..c17dbc2e75a8364c04fdeb3654255b1f35a8c5d2 100644 (file)
@@ -16,4 +16,12 @@ int show_range_diff(const char *range1, const char *range2,
                    const struct diff_options *diffopt,
                    const struct strvec *other_arg);
 
+/*
+ * Determine whether the given argument is usable as a range argument of `git
+ * range-diff`, e.g. A..B. Note that this only validates the format but does
+ * _not_ parse it, i.e. it does _not_ look up the specified commits in the
+ * local repository.
+ */
+int is_range_diff_range(const char *arg);
+
 #endif