Skip to content

Commit 359f0d7

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

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

range-diff.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pretty.h"
1212
#include "userdiff.h"
1313
#include "apply.h"
14+
#include "revision.h"
1415

1516
struct patch_util {
1617
/* For the search for an exact match */
@@ -567,5 +568,28 @@ int show_range_diff(const char *range1, const char *range2,
567568

568569
int is_range_diff_range(const char *arg)
569570
{
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;
571595
}

range-diff.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ int show_range_diff(const char *range1, const char *range2,
1818

1919
/*
2020
* 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.
2422
*/
2523
int is_range_diff_range(const char *arg);
2624

t/t3206-range-diff.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ test_expect_success 'simple A B C (unmodified)' '
150150
test_cmp expect actual
151151
'
152152

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+
153166
test_expect_success 'trivial reordering' '
154167
git range-diff --no-color master topic reordered >actual &&
155168
cat >expect <<-EOF &&

0 commit comments

Comments
 (0)