# [PATCH v2 0/3] Fix rebase autostash

9 messages from 2018-10-22 to 2018-10-23. Participants: Johannes Schindelin via GitGitGadget, Eric Sunshine, SZEDER Gábor, Junio C Hamano, Johannes Schindelin, Alban Gruin.
Thread: https://gitlist.dev/t/49641

## Johannes Schindelin via GitGitGadget, 2018-10-22 22:15

Subject: [PATCH v2 0/3] Fix rebase autostash
Message-ID: <pull.52.v2.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.52.v2.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.52.git.gitgitgadget@gmail.com>

```
Gábor reported in 
https://public-inbox.org/git/20181019124625.GB30222@szeder.dev/ that 
t5520-pull.sh fails from time to time, and Alban root-caused this to a bug
in the built-in rebase.

This patch series fixes that, and while at it also fixes an oversight of
yours truly when helping Pratik with his GSoC project, and it also adds a
change on top that makes really, really certain that git stash apply 
interprets the OID passed to it correctly (as opposed to an insanely large
number for the stash reflog).

Please note that I based these patches on top of next (they might be most
appropriately applied on top of rebase-in-c-6-final, though).

(Sorry for the v2, v1 did not send due to an UTF-8 character in the Cc:
list, a bug that still needs to be fixed in GitGitGadget.)

Johannes Schindelin (3):
  rebase (autostash): avoid duplicate call to state_dir_path()
  rebase (autostash): store the full OID in <state-dir>/autostash
  rebase (autostash): use an explicit OID to apply the stash

 builtin/rebase.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)


base-commit: 500967bb5e73b67708a64a4360867cf2407ba880
Published-As: https://github.com/gitgitgadget/git/releases/tags/pr-52%2Fdscho%2Ffix-rebase-autostash-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-52/dscho/fix-rebase-autostash-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/52

Range-diff vs v1:

 1:  88241ad32 = 1:  88241ad32 rebase (autostash): avoid duplicate call to state_dir_path()
 2:  86107a6d0 = 2:  86107a6d0 rebase (autostash): store the full OID in <state-dir>/autostash
 3:  d3b47a4c1 = 3:  07140a71d rebase (autostash): use an explicit OID to apply the stash

-- 
gitgitgadget

```

## Johannes Schindelin via GitGitGadget, 2018-10-22 22:15

Subject: [PATCH v2 3/3] rebase (autostash): use an explicit OID to apply the stash
Message-ID: <07140a71dd9ed3f709970f0ce5eb6aa014417b25.1540246499.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/07140a71dd9ed3f709970f0ce5eb6aa014417b25.1540246499.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.52.v2.git.gitgitgadget@gmail.com>

```
From: Johannes Schindelin <johannes.schindelin@gmx.de>

When `git stash apply <argument>` sees an argument that consists only of
digits, it tries to be smart and interpret it as `stash@{<number>}`.

Unfortunately, an all-digit hash (which is unlikely but still possible)
is therefore misinterpreted as `stash@{<n>}` reflog.

To prevent that from happening, let's append `^0` after the stash hash,
to make sure that it is interpreted as an OID rather than as a number.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index 418624837..30d58118c 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -253,6 +253,8 @@ static int apply_autostash(struct rebase_options *opts)
 
 	if (read_one(path, &autostash))
 		return error(_("Could not read '%s'"), path);
+	/* Ensure that the hash is not mistake for a number */
+	strbuf_addstr(&autostash, "^0");
 	argv_array_pushl(&stash_apply.args,
 			 "stash", "apply", autostash.buf, NULL);
 	stash_apply.git_cmd = 1;
-- 
gitgitgadget

```

## Johannes Schindelin via GitGitGadget, 2018-10-22 22:15

Subject: [PATCH v2 2/3] rebase (autostash): store the full OID in <state-dir>/autostash
Message-ID: <86107a6d0a044e3338e6f70313917066d200a1f4.1540246499.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/86107a6d0a044e3338e6f70313917066d200a1f4.1540246499.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.52.v2.git.gitgitgadget@gmail.com>

```
From: Johannes Schindelin <johannes.schindelin@gmx.de>

It was reported by Gábor Szeder and analyzed by Alban Gruin that the
built-in rebase stores only abbreviated stash hashes in the `autostash`
file.

This is problematic e.g. in t5520-pull.sh, where the abbreviated hash is
so short that it sometimes consists only of digits, which are
subsequently mistaken ("DWIMmed") for numbers by `git stash apply`.

Let's align the behavior of the built-in rebase with the scripted rebase
and store the full stash hash instead. That makes it a lot less likely
that it consists only of digits.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index d21504d9f..418624837 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -1375,7 +1375,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 			if (safe_create_leading_directories_const(autostash))
 				die(_("Could not create directory for '%s'"),
 				    options.state_dir);
-			write_file(autostash, "%s", buf.buf);
+			write_file(autostash, "%s", oid_to_hex(&oid));
 			printf(_("Created autostash: %s\n"), buf.buf);
 			if (reset_head(&head->object.oid, "reset --hard",
 				       NULL, 0, NULL, NULL) < 0)
-- 
gitgitgadget


```

## Johannes Schindelin via GitGitGadget, 2018-10-22 22:15

Subject: [PATCH v2 1/3] rebase (autostash): avoid duplicate call to state_dir_path()
Message-ID: <88241ad327ac42b94046a7e7ca208c84fd9f4578.1540246499.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/88241ad327ac42b94046a7e7ca208c84fd9f4578.1540246499.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.52.v2.git.gitgitgadget@gmail.com>

```
From: Johannes Schindelin <johannes.schindelin@gmx.de>

We already called that function at this point, and stored the result in
the `path` variable. We might just as well use it ;-)

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index 313a8263d..d21504d9f 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -251,7 +251,7 @@ static int apply_autostash(struct rebase_options *opts)
 	if (!file_exists(path))
 		return 0;
 
-	if (read_one(state_dir_path("autostash", opts), &autostash))
+	if (read_one(path, &autostash))
 		return error(_("Could not read '%s'"), path);
 	argv_array_pushl(&stash_apply.args,
 			 "stash", "apply", autostash.buf, NULL);
-- 
gitgitgadget


```

## Eric Sunshine, 2018-10-22 22:25

Subject: Re: [PATCH v2 3/3] rebase (autostash): use an explicit OID to apply the stash
Message-ID: <CAPig+cSgRttNJTScnYaE6G2kgpFzwYMnroH=bCKtc0a2Y5CiBQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcSgRttNJTScnYaE6G2kgpFzwYMnroH%3DbCKtc0a2Y5CiBQ%40mail.gmail.com
In-Reply-To: <07140a71dd9ed3f709970f0ce5eb6aa014417b25.1540246499.git.gitgitgadget@gmail.com>

```
On Mon, Oct 22, 2018 at 6:15 PM Johannes Schindelin via GitGitGadget
<gitgitgadget@gmail.com> wrote:
> When `git stash apply <argument>` sees an argument that consists only of
> digits, it tries to be smart and interpret it as `stash@{<number>}`.
>
> Unfortunately, an all-digit hash (which is unlikely but still possible)
> is therefore misinterpreted as `stash@{<n>}` reflog.
>
> To prevent that from happening, let's append `^0` after the stash hash,
> to make sure that it is interpreted as an OID rather than as a number.
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
> diff --git a/builtin/rebase.c b/builtin/rebase.c
> @@ -253,6 +253,8 @@ static int apply_autostash(struct rebase_options *opts)
>
>         if (read_one(path, &autostash))
>                 return error(_("Could not read '%s'"), path);
> +       /* Ensure that the hash is not mistake for a number */

s/mistake/mistaken/

```

## SZEDER Gábor, 2018-10-22 22:32

Subject: Re: [PATCH v2 3/3] rebase (autostash): use an explicit OID to apply the stash
Message-ID: <20181022223256.GI30222@szeder.dev>
URL: https://gitlist.dev/e/20181022223256.GI30222%40szeder.dev
In-Reply-To: <07140a71dd9ed3f709970f0ce5eb6aa014417b25.1540246499.git.gitgitgadget@gmail.com>

```
On Mon, Oct 22, 2018 at 03:15:05PM -0700, Johannes Schindelin via GitGitGadget wrote:
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
> 
> When `git stash apply <argument>` sees an argument that consists only of
> digits, it tries to be smart and interpret it as `stash@{<number>}`.
> 
> Unfortunately, an all-digit hash (which is unlikely but still possible)
> is therefore misinterpreted as `stash@{<n>}` reflog.
> 
> To prevent that from happening, let's append `^0` after the stash hash,
> to make sure that it is interpreted as an OID rather than as a number.

Oh, this is clever.

FWIW, all patches look good to me, barring the typo below.

> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>  builtin/rebase.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/builtin/rebase.c b/builtin/rebase.c
> index 418624837..30d58118c 100644
> --- a/builtin/rebase.c
> +++ b/builtin/rebase.c
> @@ -253,6 +253,8 @@ static int apply_autostash(struct rebase_options *opts)
>  
>  	if (read_one(path, &autostash))
>  		return error(_("Could not read '%s'"), path);
> +	/* Ensure that the hash is not mistake for a number */

s/mistake/mistaken/

> +	strbuf_addstr(&autostash, "^0");
>  	argv_array_pushl(&stash_apply.args,
>  			 "stash", "apply", autostash.buf, NULL);
>  	stash_apply.git_cmd = 1;
> -- 
> gitgitgadget

```

## Junio C Hamano, 2018-10-23 04:13

Subject: Re: [PATCH v2 3/3] rebase (autostash): use an explicit OID to apply the stash
Message-ID: <xmqqzhv5s3lf.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqzhv5s3lf.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20181022223256.GI30222@szeder.dev>

```
SZEDER Gábor <szeder.dev@gmail.com> writes:

>> To prevent that from happening, let's append `^0` after the stash hash,
>> to make sure that it is interpreted as an OID rather than as a number.
>
> Oh, this is clever.

Yeah, we can do this as we know we'd be dealing with a commit-ish.
If we made a mistake to use a tree object to represent a stash, this
trick wouldn't have been possible ;-)

> FWIW, all patches look good to me, barring the typo below.
> ...
>> +	/* Ensure that the hash is not mistake for a number */
>
> s/mistake/mistaken/

Will squash this in and add your reviewed-by before queuing.

Thanks, both.

```

## Johannes Schindelin, 2018-10-23 09:34

Subject: Re: [PATCH v2 3/3] rebase (autostash): use an explicit OID to apply the stash
Message-ID: <nycvar.QRO.7.76.6.1810231134020.4546@tvgsbejvaqbjf.bet>
URL: https://gitlist.dev/e/nycvar.QRO.7.76.6.1810231134020.4546%40tvgsbejvaqbjf.bet
In-Reply-To: <xmqqzhv5s3lf.fsf@gitster-ct.c.googlers.com>

```
Hi,

On Tue, 23 Oct 2018, Junio C Hamano wrote:

> SZEDER Gábor <szeder.dev@gmail.com> writes:
> 
> >> To prevent that from happening, let's append `^0` after the stash hash,
> >> to make sure that it is interpreted as an OID rather than as a number.
> >
> > Oh, this is clever.
> 
> Yeah, we can do this as we know we'd be dealing with a commit-ish.
> If we made a mistake to use a tree object to represent a stash, this
> trick wouldn't have been possible ;-)
> 
> > FWIW, all patches look good to me, barring the typo below.
> > ...
> >> +	/* Ensure that the hash is not mistake for a number */
> >
> > s/mistake/mistaken/
> 
> Will squash this in and add your reviewed-by before queuing.
> 
> Thanks, both.

Thank you all!
Dscho
```

## Alban Gruin, 2018-10-23 17:07

Subject: Re: [PATCH v2 3/3] rebase (autostash): use an explicit OID to apply the stash
Message-ID: <9dab8e05-6520-0dc1-e08f-07236e65f25f@gmail.com>
URL: https://gitlist.dev/e/9dab8e05-6520-0dc1-e08f-07236e65f25f%40gmail.com
In-Reply-To: <07140a71dd9ed3f709970f0ce5eb6aa014417b25.1540246499.git.gitgitgadget@gmail.com>

```
Hi Johannes,

this looks good to me, too!

Cheers,
Alban


```
