From 4ca65d7064c5a3a662c537206c3e334f9daf17cd Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 21 Jan 2021 22:20:36 +0000 Subject: [PATCH 1/3] range-diff: refactor check for commit range Currently, when called with exactly two arguments, we test for a literal `..` in each of the two. However, `^!` is a perfectly valid commit range, equivalent to `^..` according to the `SPECIFYING RANGES` section of gitrevisions[7]. In preparation for allowing more sophisticated ways to specify commit ranges, let's refactor the conditional into its own function. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin/range-diff.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/builtin/range-diff.c b/builtin/range-diff.c index 24c4162f7446ce..551d3e689cb34a 100644 --- a/builtin/range-diff.c +++ b/builtin/range-diff.c @@ -11,6 +11,11 @@ N_("git range-diff [] "), NULL }; +static int is_range(const char *range) +{ + return !!strstr(range, ".."); +} + int cmd_range_diff(int argc, const char **argv, const char *prefix) { int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT; @@ -46,12 +51,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(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(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]); From 2781243c8cbf383c637fae59103c0ea9776810c1 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 21 Jan 2021 22:20:37 +0000 Subject: [PATCH 2/3] range-diff: handle commit ranges other than A..B In the `SPECIFYING RANGES` section of gitrevisions[7], two ways are described to specify commit ranges that `range-diff` does not yet accept: "^!" and "^-". Let's accept them. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin/range-diff.c | 21 ++++++++++++++++++++- t/t3206-range-diff.sh | 8 ++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/builtin/range-diff.c b/builtin/range-diff.c index 551d3e689cb34a..6097635c4326eb 100644 --- a/builtin/range-diff.c +++ b/builtin/range-diff.c @@ -13,7 +13,26 @@ NULL static int is_range(const char *range) { - return !!strstr(range, ".."); + size_t i; + char c; + + if (strstr(range, "..")) + return 1; + + i = strlen(range); + c = i ? range[--i] : 0; + if (c == '!') + i--; /* might be ...^! or ...^@ */ + else if (isdigit(c)) { + /* handle ...^- */ + while (i > 2 && isdigit(range[--i])) + ; /* keep trimming trailing digits */ + if (i < 2 || range[i--] != '-') + return 0; + } else + return 0; + + return i > 0 && range[i] == '^'; } int cmd_range_diff(int argc, const char **argv, const char *prefix) diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh index 6eb344be0312a4..e217cecac9ed27 100755 --- a/t/t3206-range-diff.sh +++ b/t/t3206-range-diff.sh @@ -150,6 +150,14 @@ test_expect_success 'simple A B C (unmodified)' ' test_cmp expect actual ' +test_expect_success 'A^! and A^- (unmodified)' ' + git range-diff --no-color topic^! unmodified^-1 >actual && + cat >expect <<-EOF && + 1: $(test_oid t4) = 1: $(test_oid u4) s/12/B/ + EOF + test_cmp expect actual +' + test_expect_success 'trivial reordering' ' git range-diff --no-color master topic reordered >actual && cat >expect <<-EOF && From 2b4a67f26852a1db95bc169c51daa71e079ca4f0 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 21 Jan 2021 22:20:38 +0000 Subject: [PATCH 3/3] range-diff(docs): explain how to specify commit ranges There are three forms, depending whether the user specifies one, two or three non-option arguments. We've never actually explained how this works in the manual, so let's explain it. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- Documentation/git-range-diff.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Documentation/git-range-diff.txt b/Documentation/git-range-diff.txt index 9701c1e5fdd5a0..76359baf26d3da 100644 --- a/Documentation/git-range-diff.txt +++ b/Documentation/git-range-diff.txt @@ -28,6 +28,19 @@ Finally, the list of matching commits is shown in the order of the second commit range, with unmatched commits being inserted just after all of their ancestors have been shown. +There are three ways to specify the commit ranges: + +- ` `: Either commit range can be of the form + `..`, `^!` or `^-`. See `SPECIFYING RANGES` + in linkgit:gitrevisions[7] for more details. + +- `...`. This resembles the symmetric ranges mentioned in + the `SPECIFYING RANGES` section of linkgit:gitrevisions[7], and is + equivalent to `.. ..` where `` is the + merge base as obtained via `git merge-base `. + +- ` `: This is equivalent to `.. + ..`. OPTIONS -------