# [Bug report] Git gets stuck in rebase mode

2 messages from 2020-12-30 to 2020-12-31. Participants: LeSeulArtichaut, René Scharfe.
Thread: https://gitlist.dev/t/54907

## LeSeulArtichaut, 2020-12-30 21:41

Subject: [Bug report] Git gets stuck in rebase mode
Message-ID: <ECF77241-0631-45D6-8B79-4250EE777A5D@gmail.com>
URL: https://gitlist.dev/e/ECF77241-0631-45D6-8B79-4250EE777A5D%40gmail.com

```
What did you do before the bug happened? (Steps to reproduce your issue)

I tried to rebase a branch, say `master`, onto a commit that does not exist in my local repository, say `37b52f3b750b11b9ef60db118f737a66af3bf242`:
`git rebase master 37b52f3b750b11b9ef60db118f737a66af3bf242`

- The branch has to be valid (e.g. `git rebase foo 37b52f3b750b11b9ef60db118f737a66af3bf242` doesn’t reproduce the bug)
- The commit SHA has to be a full SHA (e.g. `git rebase master 37b52f3b750b11b9ef60db118f737a66af3bf2 #42` doesn’t reproduce the bug)

Repro "from scratch":
ˋˋˋzsh
mkdir git-bug
cd git-bug
git init
touch foo.txt
git add foo.txt
git commit -m 'Initial commit'
git rebase master 37b52f3b750b11b9ef60db118f737a66af3bf242
ˋˋˋ

What did you expect to happen? (Expected behavior)
Git either successfully applies the rebase or gives an error.

What happened instead? (Actual behavior)
ˋˋˋ
error: Could not read 37b52f3b750b11b9ef60db118f737a66af3bf242
fatal: Invalid symmetric difference expression b91ebd8a08983ed5c27a3946821be02a39f99b3c…37b52f3b750b11b9ef60db118f737a66af3bf242
ˋˋˋ

Git then instead gets « stuck » in rebase mode (i.e. a `.git/rebase-merge` directory exists), with the following visible effects:
 - `git status` kind of gets confused:
ˋˋˋbash
$ git status
On branch master
git-rebase-todo is missing.
No commands done.
No commands remaining.
You are currently editing a commit during a rebase.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working tree clean
ˋˋˋ
 - powerlevel10k displays a `rebase-i` state
 - `git rebase --continue` and `git rebase --abort` fail:
ˋˋˋbash
$ git rebase --continue
error: could not open '.git/rebase-merge/git-rebase-todo': No such file or directory
$ git rebase --abort
fatal: unable to read tree 37b52f3b750b11b9ef60db118f737a66af3bf242
ˋˋˋ

This disappears by deleting the `.git/rebase-merge` directory.

What's different between what you expected and what actually happened?
I didn’t expect Git to stay stuck in rebase. After the failure in the `rebase` command, the `.git/rebase-merge` directory should be deleted.


[System Info]
git version:
git version 2.30.0
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Darwin 19.6.0 Darwin Kernel Version 19.6.0: Tue Nov 10 00:10:30 PST 2020; root:xnu-6153.141.10~1/RELEASE_X86_64 x86_64
compiler info: clang: 12.0.0 (clang-1200.0.32.28)
libc info: no libc information available
$SHELL (typically, interactive shell): /bin/zsh


[Enabled Hooks]

```

## René Scharfe, 2020-12-31 23:55

Subject: [PATCH] rebase: verify commit parameter
Message-ID: <49e25873-90e0-8ae2-7a40-01bec3f99e5f@web.de>
URL: https://gitlist.dev/e/49e25873-90e0-8ae2-7a40-01bec3f99e5f%40web.de
In-Reply-To: <ECF77241-0631-45D6-8B79-4250EE777A5D@gmail.com>

```
If the user specifies a base commit to switch to, check if it actually
references a commit right away to avoid getting confused later on when
it turns out to be an invalid object.

Reported-by: LeSeulArtichaut <leseulartichaut@gmail.com>
Signed-off-by: René Scharfe <l.s.r@web.de>
---
git-legacy-rebase.sh also accepted non-commits, if I read the code
correctly.

 builtin/rebase.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index 19c7b377aa..840dbd7eb7 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -1917,7 +1917,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 			die_if_checked_out(buf.buf, 1);
 			options.head_name = xstrdup(buf.buf);
 		/* If not is it a valid ref (branch or commit)? */
-		} else if (!get_oid(branch_name, &options.orig_head))
+		} else if (!get_oid(branch_name, &options.orig_head) &&
+			   lookup_commit_reference(the_repository,
+						   &options.orig_head))
 			options.head_name = NULL;
 		else
 			die(_("fatal: no such branch/commit '%s'"),
--
2.30.0

```
