# [PATCH] Use reflog in 'pull --rebase . foo'

6 messages from 2010-11-12 to 2010-11-13. Participants: Martin von Zweigbergk, Andreas Schwab, Santi Béjar.
Thread: https://gitlist.dev/t/25734

## Martin von Zweigbergk, 2010-11-12 19:38

Subject: [PATCH] Use reflog in 'pull --rebase . foo'
Message-ID: <1289590708-11064-1-git-send-email-martin.von.zweigbergk@gmail.com>
URL: https://gitlist.dev/e/1289590708-11064-1-git-send-email-martin.von.zweigbergk%40gmail.com

```
Since c85c792 (pull --rebase: be cleverer with rebased upstream
branches, 2008-01-26), "git pull --rebase" has used the reflog to try to
rebase from the old upstream onto the new upstream.

Make this work if the local repository is explicitly passed on the
command line as in 'git pull --rebase . foo'.
---
I removed some seemingly unnecessary variables. I couldn't find any uses
of them in the callers' code either, so it should be safe...

I apparently introduced a call to sed as well. I think I remember seeing
some guideslines about the use of sed in Git, but I can't remember what.
I couldn't find anything in the CodingGuidelines either. Is the code
below ok?

 git-parse-remote.sh |   24 ++++++++++--------------
 t/t5520-pull.sh     |    7 +++++++
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/git-parse-remote.sh b/git-parse-remote.sh
index 5f47b18..0565edd 100644
--- a/git-parse-remote.sh
+++ b/git-parse-remote.sh
@@ -63,33 +63,29 @@ get_default_remote () {
 get_remote_merge_branch () {
 	case "$#" in
 	0|1)
-	    origin="$1"
-	    default=$(get_default_remote)
-	    test -z "$origin" && origin=$default
 	    curr_branch=$(git symbolic-ref -q HEAD)
-	    [ "$origin" = "$default" ] &&
+	    test -z "$1" || test "$1" = $(get_default_remote) &&
 	    echo $(git for-each-ref --format='%(upstream)' $curr_branch)
 	    ;;
 	*)
 	    repo=$1
 	    shift
-	    ref=$1
 	    # FIXME: It should return the tracking branch
 	    #        Currently only works with the default mapping
-	    case "$ref" in
-	    +*)
-		ref=$(expr "z$ref" : 'z+\(.*\)')
-		;;
-	    esac
-	    expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
-	    remote=$(expr "z$ref" : 'z\([^:]*\):')
+	    remote=$(echo "$1" | sed -e 's|+\?\([^:]*\):\?|\1|g')
 	    case "$remote" in
 	    '' | HEAD ) remote=HEAD ;;
 	    heads/*) remote=${remote#heads/} ;;
 	    refs/heads/*) remote=${remote#refs/heads/} ;;
 	    refs/* | tags/* | remotes/* ) remote=
 	    esac
-
-	    [ -n "$remote" ] && echo "refs/remotes/$repo/$remote"
+	    [ -n "$remote" ] && case "$repo" in
+		.)
+		    echo "refs/heads/$remote"
+		    ;;
+		*)
+		    echo "refs/remotes/$repo/$remote"
+		    ;;
+	    esac
 	esac
 }
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
index 0b489f5..0470a81 100755
--- a/t/t5520-pull.sh
+++ b/t/t5520-pull.sh
@@ -222,4 +222,11 @@ test_expect_success 'git pull --rebase does not reapply old patches' '
 	)
 '
 
+test_expect_success 'git pull --rebase against local branch' '
+	git checkout -b copy2 to-rebase-orig &&
+	git pull --rebase . to-rebase &&
+	test "conflicting modification" = "$(cat file)" &&
+	test file = "$(cat file2)"
+'
+
 test_done
-- 
1.7.3.2.167.ga361b

```

## Andreas Schwab, 2010-11-13 08:58

Subject: Re: [PATCH] Use reflog in 'pull --rebase . foo'
Message-ID: <m2pqu98urp.fsf@whitebox.home>
URL: https://gitlist.dev/e/m2pqu98urp.fsf%40whitebox.home
In-Reply-To: <1289590708-11064-1-git-send-email-martin.von.zweigbergk@gmail.com>

```
Martin von Zweigbergk <martin.von.zweigbergk@gmail.com> writes:

> +	    remote=$(echo "$1" | sed -e 's|+\?\([^:]*\):\?|\1|g')

\? is not portable.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Martin von Zweigbergk, 2010-11-13 12:09

Subject: Re: [PATCH] Use reflog in 'pull --rebase . foo'
Message-ID: <AANLkTiniwpk7ao=M4=5vuahczk6FscQYJE1rZx=wuBk_@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTiniwpk7ao%3DM4%3D5vuahczk6FscQYJE1rZx%3DwuBk_%40mail.gmail.com
In-Reply-To: <m2pqu98urp.fsf@whitebox.home>

```
I see, maybe just dropping the middle part of the patch would be better.
I think it looks like I have lost a '.*' in there as well (test cases
pass though, so I'm not sure). Anyway, better be safe than sorry. The
following seems better.

diff --git a/git-parse-remote.sh b/git-parse-remote.sh
index 5f47b18..2e1661d 100644
--- a/git-parse-remote.sh
+++ b/git-parse-remote.sh
@@ -63,11 +63,8 @@ get_default_remote () {
 get_remote_merge_branch () {
 	case "$#" in
 	0|1)
-	    origin="$1"
-	    default=$(get_default_remote)
-	    test -z "$origin" && origin=$default
 	    curr_branch=$(git symbolic-ref -q HEAD)
-	    [ "$origin" = "$default" ] &&
+	    test -z "$1" || test "$1" = $(get_default_remote) &&
 	    echo $(git for-each-ref --format='%(upstream)' $curr_branch)
 	    ;;
 	*)
@@ -89,7 +86,13 @@ get_remote_merge_branch () {
 	    refs/heads/*) remote=${remote#refs/heads/} ;;
 	    refs/* | tags/* | remotes/* ) remote=
 	    esac
-
-	    [ -n "$remote" ] && echo "refs/remotes/$repo/$remote"
+	    [ -n "$remote" ] && case "$repo" in
+		.)
+		    echo "refs/heads/$remote"
+		    ;;
+		*)
+		    echo "refs/remotes/$repo/$remote"
+		    ;;
+	    esac
 	esac
 }
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
index 0b489f5..0470a81 100755
--- a/t/t5520-pull.sh
+++ b/t/t5520-pull.sh
@@ -222,4 +222,11 @@ test_expect_success 'git pull --rebase does not
reapply old patches' '
 	)
 '

+test_expect_success 'git pull --rebase against local branch' '
+	git checkout -b copy2 to-rebase-orig &&
+	git pull --rebase . to-rebase &&
+	test "conflicting modification" = "$(cat file)" &&
+	test file = "$(cat file2)"
+'
+
 test_done
-- 
1.7.3.2.167.ga361b

```

## Santi Béjar, 2010-11-13 17:20

Subject: Re: [PATCH] Use reflog in 'pull --rebase . foo'
Message-ID: <AANLkTikmJVj9BM=K2oSYJvPV27Uzn_9SQhsLc9EQm8zQ@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTikmJVj9BM%3DK2oSYJvPV27Uzn_9SQhsLc9EQm8zQ%40mail.gmail.com
In-Reply-To: <AANLkTiniwpk7ao=M4=5vuahczk6FscQYJE1rZx=wuBk_@mail.gmail.com>

```
On Sat, Nov 13, 2010 at 1:09 PM, Martin von Zweigbergk
<martin.von.zweigbergk@gmail.com> wrote:
> diff --git a/git-parse-remote.sh b/git-parse-remote.sh
> index 5f47b18..2e1661d 100644
> --- a/git-parse-remote.sh
> +++ b/git-parse-remote.sh
> @@ -63,11 +63,8 @@ get_default_remote () {
>  get_remote_merge_branch () {
>        case "$#" in
>        0|1)
> -           origin="$1"
> -           default=$(get_default_remote)
> -           test -z "$origin" && origin=$default
>            curr_branch=$(git symbolic-ref -q HEAD)
> -           [ "$origin" = "$default" ] &&
> +           test -z "$1" || test "$1" = $(get_default_remote) &&
>            echo $(git for-each-ref --format='%(upstream)' $curr_branch)
>            ;;
>        *)

They are not equivalent, the last line (echo $(git for-each-ref...))
is always executed, not only when ask for the default remote. When
$origin != $default the last line does not return the correct answer.
It should return nothing, it is not well defined. Or maybe it should
return the branch pointed by $origin/HEAD (I cannot test right now
what 'git pull $remote-not-the-default' merges).

And it has nothing to do with letting 'pull --rebase . for" work.

> @@ -89,7 +86,13 @@ get_remote_merge_branch () {
>            refs/heads/*) remote=${remote#refs/heads/} ;;
>            refs/* | tags/* | remotes/* ) remote=
>            esac
> -
> -           [ -n "$remote" ] && echo "refs/remotes/$repo/$remote"
> +           [ -n "$remote" ] && case "$repo" in
> +               .)
> +                   echo "refs/heads/$remote"
> +                   ;;
> +               *)
> +                   echo "refs/remotes/$repo/$remote"
> +                   ;;
> +           esac
>        esac
>  }
> diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
> index 0b489f5..0470a81 100755
> --- a/t/t5520-pull.sh
> +++ b/t/t5520-pull.sh
> @@ -222,4 +222,11 @@ test_expect_success 'git pull --rebase does not
> reapply old patches' '
>        )
>  '
>
> +test_expect_success 'git pull --rebase against local branch' '
> +       git checkout -b copy2 to-rebase-orig &&
> +       git pull --rebase . to-rebase &&
> +       test "conflicting modification" = "$(cat file)" &&
> +       test file = "$(cat file2)"
> +'
> +
>  test_done

For the rest I think they are OK, and make sense.

HTH,
Santi

```

## Martin von Zweigbergk, 2010-11-13 17:52

Subject: Re: [PATCH] Use reflog in 'pull --rebase . foo'
Message-ID: <AANLkTineaFVawXbJKHKv39bwPco7JHcmpUwXtZhscaFs@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTineaFVawXbJKHKv39bwPco7JHcmpUwXtZhscaFs%40mail.gmail.com
In-Reply-To: <AANLkTikmJVj9BM=K2oSYJvPV27Uzn_9SQhsLc9EQm8zQ@mail.gmail.com>

```
On Sat, Nov 13, 2010 at 12:20 PM, Santi Béjar <santi@agolina.net> wrote:
> On Sat, Nov 13, 2010 at 1:09 PM, Martin von Zweigbergk
> <martin.von.zweigbergk@gmail.com> wrote:
>> diff --git a/git-parse-remote.sh b/git-parse-remote.sh
>> index 5f47b18..2e1661d 100644
>> --- a/git-parse-remote.sh
>> +++ b/git-parse-remote.sh
>> @@ -63,11 +63,8 @@ get_default_remote () {
>>  get_remote_merge_branch () {
>>        case "$#" in
>>        0|1)
>> -           origin="$1"
>> -           default=$(get_default_remote)
>> -           test -z "$origin" && origin=$default
>>            curr_branch=$(git symbolic-ref -q HEAD)
>> -           [ "$origin" = "$default" ] &&
>> +           test -z "$1" || test "$1" = $(get_default_remote) &&
>>            echo $(git for-each-ref --format='%(upstream)' $curr_branch)
>>            ;;
>>        *)
>
> They are not equivalent, the last line (echo $(git for-each-ref...))
> is always executed, not only when ask for the default remote. When
> $origin != $default the last line does not return the correct answer.
> It should return nothing, it is not well defined. Or maybe it should
> return the branch pointed by $origin/HEAD (I cannot test right now
> what 'git pull $remote-not-the-default' merges).
>
> And it has nothing to do with letting 'pull --rebase . for" work.

You are right, of course. I think I was modifying the code to try to
understand how it behaved and it should not have been part of the patch.
Sorry about that.

Junio, will you just exclude that hunk?

```

## Santi Béjar, 2010-11-13 23:10

Subject: Re: [PATCH] Use reflog in 'pull --rebase . foo'
Message-ID: <AANLkTi=QQZUjYtvRRiunrsCXMOX9+85K_nZXwsrT30bM@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTi%3DQQZUjYtvRRiunrsCXMOX9%2B85K_nZXwsrT30bM%40mail.gmail.com
In-Reply-To: <AANLkTineaFVawXbJKHKv39bwPco7JHcmpUwXtZhscaFs@mail.gmail.com>

```
On Sat, Nov 13, 2010 at 6:52 PM, Martin von Zweigbergk
<martin.von.zweigbergk@gmail.com> wrote:
> On Sat, Nov 13, 2010 at 12:20 PM, Santi Béjar <santi@agolina.net> wrote:
>> On Sat, Nov 13, 2010 at 1:09 PM, Martin von Zweigbergk
>> <martin.von.zweigbergk@gmail.com> wrote:
>>> diff --git a/git-parse-remote.sh b/git-parse-remote.sh
>>> index 5f47b18..2e1661d 100644
>>> --- a/git-parse-remote.sh
>>> +++ b/git-parse-remote.sh
>>> @@ -63,11 +63,8 @@ get_default_remote () {
>>>  get_remote_merge_branch () {
>>>        case "$#" in
>>>        0|1)
>>> -           origin="$1"
>>> -           default=$(get_default_remote)
>>> -           test -z "$origin" && origin=$default
>>>            curr_branch=$(git symbolic-ref -q HEAD)
>>> -           [ "$origin" = "$default" ] &&
>>> +           test -z "$1" || test "$1" = $(get_default_remote) &&
>>>            echo $(git for-each-ref --format='%(upstream)' $curr_branch)
>>>            ;;
>>>        *)
>>
>> They are not equivalent, the last line (echo $(git for-each-ref...))
>> is always executed, not only when ask for the default remote. When
>> $origin != $default the last line does not return the correct answer.
>> It should return nothing, it is not well defined. Or maybe it should
>> return the branch pointed by $origin/HEAD (I cannot test right now
>> what 'git pull $remote-not-the-default' merges).

After thinking (and testing) this again, I see now that I was wrong,
the behavior is identical. But I find it more difficult to follow, but
maybe it is because I wrote the original.

>>
>> And it has nothing to do with letting 'pull --rebase . for" work.
>
> You are right, of course. I think I was modifying the code to try to
> understand how it behaved and it should not have been part of the patch.
> Sorry about that.
>
> Junio, will you just exclude that hunk?

Better send it again, and add my

Ack-by: Santi Béjar <santi@agolina.net>

Santi

```
