git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Bug: 'git stash --staged' crashes with binary files

From
Jeff King <peff@peff.net>
Date
Oct 11, 2023, 18:17 UTC
Message-ID
<20231011181733.GA514244@coredump.intra.peff.net>
In-Reply-To
<CAMyDDM3DFyru6zph4qqf_QoaOeezvYkT7SmwinCfJNnFsnuRjQ@mail.gmail.com>
On Thu, Jun 01, 2023 at 10:26:13PM +0100, Adam Johnson wrote:
Show 19 quoted lines
> Stage a binary file and run 'git stash --staged'. The stash is created but
> the command fails to remove changes from the index:
> 
> $ echo -n "\0" >file
> 
> $ git add file
> 
> $ git stash --staged
> Saved working directory and index state WIP on main: e7911b6 Whatever
> error: cannot apply binary patch to 'file' without full index line
> error: file: patch does not apply
> Cannot remove worktree changes
> 
> $ git status
> ...
> Changes to be committed:
>         new file:   file
> 
> The "remove from the index" step seems not to be binary-compatible.

This seems like a bug. It looks like stash does pass "--binary" in some cases, but not all. So it's probably a matter of finding the right invocation and adding it.

> The below patch adds a reproducing test.

I think finding the bug and writing the test is probably 75% of the work. :)

Show 10 quoted lines
> diff --git t/t3903-stash.sh t/t3903-stash.sh
> index 376cc8f4ab..5e3ea64f38 100755
> --- t/t3903-stash.sh
> +++ t/t3903-stash.sh
> @@ -392,6 +392,13 @@ setup_stash()
>      git stash pop &&
>      test bar,bar4 = $(cat file),$(cat file2)
>  '
> +test_expect_success 'stash --staged with binary file' '
> +    echo -n "\0" >file &&
Unfortunately "echo -n" isn't portable. But you can use:
  printf "\0" >file
instead.
Show 5 quoted lines
> +    git add file &&
> +    git stash --staged &&
> +    git stash pop &&
> +    test "\0" = $(cat file)
> +'

I doubt this "test" will work, as the shell won't interpolate that into a NUL (and anyway, I think having NULs in shell variables isn't portable). You could perhaps do:

  printf "\0" >expect &&
  test_cmp expect file
-Peff
Previous: Adam JohnsonNext: rsbecker@nexbridge.com
Message 2 of 3 in “Bug: 'git stash --staged' crashes with binary files”
  1. Adam JohnsonJun 1, 2023
  2. Jeff KingOct 11, 2023
  3. rsbecker@nexbridge.comOct 11, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.