Skip to content

Commit 679b591

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66e871b commit 679b591

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ static void infer_range_diff_ranges(struct strbuf *r1,
16801680
struct commit *head)
16811681
{
16821682
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);
16841684

16851685
if (prev_is_range)
16861686
strbuf_addstr(r1, prev);

builtin/range-diff.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "parse-options.h"
44
#include "range-diff.h"
55
#include "config.h"
6+
#include "revision.h"
67

78
static const char * const builtin_range_diff_usage[] = {
89
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)
4647
diffopt.use_color = 1;
4748

4849
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]);
5152
strbuf_addstr(&range1, argv[0]);
5253

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]);
5556
strbuf_addstr(&range2, argv[1]);
5657
} else if (argc == 3) {
5758
strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);

range-diff.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,8 @@ int show_range_diff(const char *range1, const char *range2,
564564

565565
return res;
566566
}
567+
568+
int is_range_diff_range(const char *arg)
569+
{
570+
return !!strstr(arg, "..");
571+
}

range-diff.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ int show_range_diff(const char *range1, const char *range2,
1616
const struct diff_options *diffopt,
1717
const struct strvec *other_arg);
1818

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+
1927
#endif

0 commit comments

Comments
 (0)