{"thread":{"id":"61930","subject":"[BUG REPORT] `--autostash` rebase + pre-rebase error ends in corrupt state","startedAt":"2024-08-09T19:29:15Z","lastAt":"2024-08-13T10:01:57Z","messageCount":2,"participants":["Brian Lyles","Phillip Wood"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"500563","messageId":"17ea26263cf57664.70b1dd9aae081c6e.203dcd72f6563036@zivdesk","threadId":"61930","inReplyTo":null,"subject":"[BUG REPORT] `--autostash` rebase + pre-rebase error ends in corrupt state","fromName":"Brian Lyles","fromEmail":"brianmlyles@gmail.com","sentAt":"2024-08-09T19:29:13Z","receivedAt":"2024-08-09T19:29:15Z","isPatch":false,"sender":{"key":"brianmlyles@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1123282?v=4"},"body":"If you perform a rebase using `--autostash`, and the pre-rebase hook\nexits with a non-zero exit code, your worktree is left in some sort of\ninvalid state. This is reproducible with the following script:\n\n    git init --initial-branch main\n    echo -e '#!/bin/sh\\nexit 1' > .git/hooks/pre-rebase\n    chmod +x .git/hooks/pre-rebase\n    \n    git commit --allow-empty -m \"initial commit\"\n    \n    git switch -c feature1 main\n    \n    touch a.txt\n    git add a.txt\n    git commit -m \"add a\"\n    \n    git switch -c feature2 main\n    \n    touch b.txt\n    git add b.txt\n    git commit -m \"add b\"\n    \n    echo \"content\" > b.txt\n    git rebase --autostash feature1\n\nAt this point, `git status` indicates that you are rebasing and\n`__git_ps1` also shows simply `(|REBASE)`. However, attempting to `git\nrebase --abort` or `git rebase --continue` gives \"warning: could not\nread '.git/rebase-merge/head-name': No such file or directory\", and does\nnot appear to change the worktree state.\n\nYou're able to recover from this by using `git rebase --quit` and then\nresetting to the commit you were on before attempting the rebase.\n\nThis issue only occurs if there is actually some modification to stash;\nthe presence of `--autostash` by itself does not trigger it.\n\nIf the above script is adjusted to make no uncommitted modifications,\nand rebase without using `--autostash`, then the rebase is simply\naborted as expected when the pre-rebase hook fails.\n\nI've reproduced this in the following OS/git version combinations:\n\n- Gentoo Linux, git v2.46.0\n- MacOS Sonoma 14.5, git v2.46.0\n- Windows 11, git 2.44.0.windows.1\n\n-- \nThank you,\nBrian Lyles\n"},{"id":"500763","messageId":"61ea41e7-d525-496b-aa2c-f605b99fc3d5@gmail.com","threadId":"61930","inReplyTo":"17ea26263cf57664.70b1dd9aae081c6e.203dcd72f6563036@zivdesk","subject":"Re: [BUG REPORT] `--autostash` rebase + pre-rebase error ends in corrupt state","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2024-08-13T10:01:53Z","receivedAt":"2024-08-13T10:01:57Z","isPatch":false,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Brian\n\nOn 09/08/2024 20:29, Brian Lyles wrote:\n> If you perform a rebase using `--autostash`, and the pre-rebase hook\n> exits with a non-zero exit code, your worktree is left in some sort of\n> invalid state. This is reproducible with the following script:\n\nThanks for reporting this. It looks like the rebase code was written on \nthe assumption that we only write the state files after successfully \nchecking out the commit we're rebasing onto. Unfortunately \"--autostash\" \nbreaks that assumption as it creates the autostash state file much \nearlier leading to an inconsistent state if the rebase cannot start. \nI'll post a fix later this week.\n\n> You're able to recover from this by using `git rebase --quit` and then\n> resetting to the commit you were on before attempting the rebase.\n\nI don't think you should need to reset HEAD as it should be untouched. \nYou can get the stashed changes back by running \"git stash --apply\" with \nthe oid from the \"Created autostash: ...\" line that rebase prints.\n\nBest Wishes\n\nPhillip\n"}]}