# [PATCH] rebase: find orig_head unambiguously

2 messages from 2013-04-23 to 2013-04-23. Participants: Phil Hord, Junio C Hamano.
Thread: https://gitlist.dev/t/33583

## Phil Hord, 2013-04-23 22:51

Subject: [PATCH] rebase: find orig_head unambiguously
Message-ID: <1366757474-29149-1-git-send-email-hordp@cisco.com>
URL: https://gitlist.dev/e/1366757474-29149-1-git-send-email-hordp%40cisco.com

```
When we 'git rebase $upstream', git uses 'rev-parse --verify
$current_branch' to find ORIG_HEAD.  But if $current_branch
is ambiguous, 'rev-parse --verify' emits a warning and returns
a SHA1 anyway.  When the wrong ambiguous choice is used,
git-rebase fails non-gracefully:  it emits a warning about
failing to lock $current_branch, an error about being unable to
checkout $current_branch again, and it might even decide the
rebase is a fast-forward when it is not.

In the 'rebase $upstream' case, we already know the unambiguous
spelling of $current_branch is "HEAD".  Fix git-rebase to find
$orig_head unambiguously.

Add a test in t3400-rebase.sh which creates an ambiguous branch
name and rebases it implicitly with 'git rebase $other'.

Signed-off-by: Phil Hord <hordp@cisco.com>
---
 git-rebase.sh     | 2 +-
 t/t3400-rebase.sh | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/git-rebase.sh b/git-rebase.sh
index b2f1c76..2c692c3 100755
--- a/git-rebase.sh
+++ b/git-rebase.sh
@@ -473,7 +473,7 @@ case "$#" in
 		head_name="detached HEAD"
 		branch_name=HEAD ;# detached
 	fi
-	orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
+	orig_head=$(git rev-parse --verify HEAD) || exit
 	;;
 *)
 	die "BUG: unexpected number of arguments left to parse"
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index f6cc102..b58fa1a 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -101,7 +101,14 @@ test_expect_success 'HEAD was detached during rebase' '
 	test $(git rev-parse HEAD@{1}) != $(git rev-parse my-topic-branch@{1})
 '
 
+test_expect_success 'rebase from ambiguous branch name' '
+	git checkout -b topic side &&
+	git rebase master
+'
+
 test_expect_success 'rebase after merge master' '
+	git checkout --detach refs/tags/topic &&
+	git branch -D topic &&
 	git reset --hard topic &&
 	git merge master &&
 	git rebase master &&
-- 
1.8.2.1.854.g46dda50.dirty

```

## Junio C Hamano, 2013-04-23 23:31

Subject: Re: [PATCH] rebase: find orig_head unambiguously
Message-ID: <7vobd4hlio.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vobd4hlio.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1366757474-29149-1-git-send-email-hordp@cisco.com>

```
Phil Hord <hordp@cisco.com> writes:

> When we 'git rebase $upstream', git uses 'rev-parse --verify
> $current_branch' to find ORIG_HEAD.  But if $current_branch
> is ambiguous, 'rev-parse --verify' emits a warning and returns
> a SHA1 anyway.  When the wrong ambiguous choice is used,
> git-rebase fails non-gracefully:  it emits a warning about
> failing to lock $current_branch, an error about being unable to
> checkout $current_branch again, and it might even decide the
> rebase is a fast-forward when it is not.
>
> In the 'rebase $upstream' case, we already know the unambiguous
> spelling of $current_branch is "HEAD".  Fix git-rebase to find
> $orig_head unambiguously.

Well explained; $current_branch that never appears as a variable in
the section of the code in question made me scratch my head while
reading it, though.

Will queue; thanks.

> Add a test in t3400-rebase.sh which creates an ambiguous branch
> name and rebases it implicitly with 'git rebase $other'.
>
> Signed-off-by: Phil Hord <hordp@cisco.com>
> ---
>  git-rebase.sh     | 2 +-
>  t/t3400-rebase.sh | 7 +++++++
>  2 files changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/git-rebase.sh b/git-rebase.sh
> index b2f1c76..2c692c3 100755
> --- a/git-rebase.sh
> +++ b/git-rebase.sh
> @@ -473,7 +473,7 @@ case "$#" in
>  		head_name="detached HEAD"
>  		branch_name=HEAD ;# detached
>  	fi
> -	orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
> +	orig_head=$(git rev-parse --verify HEAD) || exit
>  	;;
>  *)
>  	die "BUG: unexpected number of arguments left to parse"
> diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
> index f6cc102..b58fa1a 100755
> --- a/t/t3400-rebase.sh
> +++ b/t/t3400-rebase.sh
> @@ -101,7 +101,14 @@ test_expect_success 'HEAD was detached during rebase' '
>  	test $(git rev-parse HEAD@{1}) != $(git rev-parse my-topic-branch@{1})
>  '
>  
> +test_expect_success 'rebase from ambiguous branch name' '
> +	git checkout -b topic side &&
> +	git rebase master
> +'
> +
>  test_expect_success 'rebase after merge master' '
> +	git checkout --detach refs/tags/topic &&
> +	git branch -D topic &&
>  	git reset --hard topic &&
>  	git merge master &&
>  	git rebase master &&

```
