OSDN Git Service

rev-parse: fix parent shorthands with --symbolic
authorJeff King <peff@peff.net>
Wed, 16 Nov 2016 08:46:26 +0000 (00:46 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 16 Nov 2016 19:12:15 +0000 (11:12 -0800)
The try_parent_shorthands() function shows each parent via
show_rev(). We pass the correct parent sha1, but our "name"
parameter still points at the original refname. So asking
for a regular rev-parse works fine (it prints the sha1s),
but asking for the symbolic name gives nonsense like:

    $ git rev-parse --symbolic HEAD^-1
    HEAD
    ^HEAD

which is always an empty set of commits. Asking for "^!" is
likewise broken, with the added bonus that its prints ^HEAD
for _each_ parent. And "^@" just prints HEAD repeatedly.

Arguably it would be correct to just pass NULL as the name
here, and always get the parent expressed as a sha1. The
"--symbolic" documentaton claims only "as close to the
original input as possible", and we certainly fallback to
sha1s where necessary. But it's pretty easy to generate a
symbolic name on the fly from the original.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
t/t6101-rev-parse-parents.sh

index 4da1f1d..c352f2f 100644 (file)
@@ -342,11 +342,16 @@ static int try_parent_shorthands(const char *arg)
        for (parents = commit->parents, parent_number = 1;
             parents;
             parents = parents->next, parent_number++) {
+               char *name = NULL;
+
                if (exclude_parent && parent_number != exclude_parent)
                        continue;
 
+               if (symbolic)
+                       name = xstrfmt("%s^%d", arg, parent_number);
                show_rev(include_parents ? NORMAL : REVERSED,
-                        parents->item->object.oid.hash, arg);
+                        parents->item->object.oid.hash, name);
+               free(name);
        }
 
        *dotdot = '^';
index 64a9850..8c61798 100755 (executable)
@@ -83,12 +83,24 @@ test_expect_success 'final^1^@ = final^1^1 final^1^2' '
        test_cmp expect actual
 '
 
+test_expect_success 'symbolic final^1^@ = final^1^1 final^1^2' '
+       git rev-parse --symbolic final^1^1 final^1^2 >expect &&
+       git rev-parse --symbolic final^1^@ >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'final^1^! = final^1 ^final^1^1 ^final^1^2' '
        git rev-parse final^1 ^final^1^1 ^final^1^2 >expect &&
        git rev-parse final^1^! >actual &&
        test_cmp expect actual
 '
 
+test_expect_success 'symbolic final^1^! = final^1 ^final^1^1 ^final^1^2' '
+       git rev-parse --symbolic final^1 ^final^1^1 ^final^1^2 >expect &&
+       git rev-parse --symbolic final^1^! >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'large graft octopus' '
        test_cmp_rev_output b31 "git rev-parse --verify b1^30"
 '
@@ -143,6 +155,12 @@ test_expect_success 'rev-parse merge^-2 = merge^2..merge' '
        test_cmp expect actual
 '
 
+test_expect_success 'symbolic merge^-1 = merge^1..merge' '
+       git rev-parse --symbolic merge^1..merge >expect &&
+       git rev-parse --symbolic merge^-1 >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'rev-parse merge^-0 (invalid parent)' '
        test_must_fail git rev-parse merge^-0
 '