range-diff/format-patch: 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: "<commit>^!" and "<commit>^-<n>". Let's accept them, by parsing them via the revision machinery and looking for at least one interesting and one uninteresting revision in the resulting `pending` array. This also finally lets us reject arguments that _do_ contain `..` but are not actually ranges, e.g. `HEAD^{/do.. match this}`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Feb 5, 2021 at 14:44 UTC
359f0d754ab709c5a1ff3267bc117fb8559c62c2
3 files changed
+39
-4
range-diff.c
+25
-1
@@ -11,6 +11,7 @@
11
#include "pretty.h"
12
#include "userdiff.h"
13
#include "apply.h"
14
+#include "revision.h"
15
16
struct patch_util {
17
/* For the search for an exact match */
@@ -567,5 +568,28 @@ int show_range_diff(const char *range1, const char *range2,
568
569
int is_range_diff_range(const char *arg)
570
{
570
- return !!strstr(arg, "..");
571
+ char *copy = xstrdup(arg); /* setup_revisions() modifies it */
572
+ const char *argv[] = { "", copy, "--", NULL };
573
+ int i, positive = 0, negative = 0;
574
+ struct rev_info revs;
575
+
576
+ init_revisions(&revs, NULL);
577
+ if (setup_revisions(3, argv, &revs, NULL) == 1) {
578
+ for (i = 0; i < revs.pending.nr; i++)
579
+ if (revs.pending.objects[i].item->flags & UNINTERESTING)
580
+ negative++;
581
+ else
582
+ positive++;
583
+ for (i = 0; i < revs.pending.nr; i++) {
584
+ struct object *obj = revs.pending.objects[i].item;
585
+
586
+ if (obj->type == OBJ_COMMIT)
587
+ clear_commit_marks((struct commit *)obj,
588
+ ALL_REV_FLAGS);
589
+ }
590
+ }
591
+
592
+ free(copy);
593
+ object_array_clear(&revs.pending);
594
+ return negative > 0 && positive > 0;
595
}
range-diff.h
+1
-3
@@ -18,9 +18,7 @@ int show_range_diff(const char *range1, const char *range2,
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.
21
+ * range-diff`, e.g. A..B.
22
*/
23
int is_range_diff_range(const char *arg);
24
t/t3206-range-diff.sh
+13
@@ -150,6 +150,19 @@ test_expect_success 'simple A B C (unmodified)' '
150
test_cmp expect actual
151
'
152
153
+test_expect_success 'A^! and A^-<n> (unmodified)' '
154
+ git range-diff --no-color topic^! unmodified^-1 >actual &&
155
+ cat >expect <<-EOF &&
156
+ 1: $(test_oid t4) = 1: $(test_oid u4) s/12/B/
157
+ EOF
158
+ test_cmp expect actual
159
+'
160
+
161
+test_expect_success 'A^{/..} is not mistaken for a range' '
162
+ test_must_fail git range-diff topic^.. topic^{/..} 2>error &&
163
+ test_i18ngrep "not a commit range" error
164
+'
165
+
166
test_expect_success 'trivial reordering' '
167
git range-diff --no-color master topic reordered >actual &&
168
cat >expect <<-EOF &&