jesseduffield / jesseduffield/lazygit
Crash when using custom patch after switching branches
- Dominant language
- Go
- Stars
- 82.4k
- Forks
- 3k
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 19
Description
### Describe the bug
A custom patch started from the commits panel in one branch, then checking out some other branch, and then trying to use that custom patch can result in crashes/other incorrect behavior.
### To Reproduce
Steps to reproduce the behavior:
```bash
git init
git checkout -b a
touch a.txt
git add .
git commit -m "a"
git checkout -b b
touch b.txt
git add .
git commit -m "b"
lazygit
```
1. Navigate to the commits panel, select the `b` commit and start a custom patch by selecting the `b` file.
2. Navigate to the branches panel and check out `a`.
3. View custom patch options (``) -> Move patch out into index.
4. Crash:
```
panic: runtime error: index out of range [-1]
goroutine 784 [running]:
github.com/jesseduffield/lazygit/pkg/commands/git_commands.(*RebaseCommands).BeginInteractiveRebaseForCommit(0x400f70ee2b9e5?, {0xc00007e158?, 0x0?, 0x7ff700000000?}, 0x7ff70fb3a430?, 0x48?)
/home/runner/work/lazygit/lazygit/pkg/commands/git_commands/rebase.go:406 +0x185
github.com/jesseduffield/lazygit/pkg/commands/git_commands.(*PatchCommands).MovePatchIntoIndex(0xc0000d8870, {0xc00007e158, 0x1, 0xc0007d1de0?}, 0xffffffffffffffff, 0x0)
/home/runner/work/lazygit/lazygit/pkg/commands/git_commands/patch.go:227 +0x12b
github.com/jesseduffield/lazygit/pkg/gui/controllers.(*CustomPatchOptionsMenuAction).handleMovePatchIntoWorkingTree.func1.1({0x7ff70f8c3500?, 0xc0007d1e38?})
/home/runner/work/lazygit/lazygit/pkg/gui/controllers/custom_patch_options_menu_action.go:184 +0xca
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*AppStatusHelper).WithWaitingStatusImpl.func1(0xc000a3d5c0?)
/home/runner/work/lazygit/lazygit/pkg/gui/controllers/helpers/app_status_helper.go:69 +0x53
github.com/jesseduffield/lazygit/pkg/gui/status.(*StatusManager).WithWaitingStatus(0xc0001f9e00, {0x7ff70f95b622, 0x8}, 0xc0007e7660, 0xc0007d1ee0)
/home/runner/work/lazygit/lazygit/pkg/gui/status/status_manager.go:56 +0xd7
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*AppStatusHelper).WithWaitingStatusImpl(0xc000221008, {0x7ff70f95b622, 0x8}, 0xc0007e9860, {0x7ff70fb3d6a0, 0xc0008ce4c0})
/home/runner/work/lazygit/lazygit/pkg/gui/controllers/helpers/app_status_helper.go:68 +0xc7
github.com/jesseduffield/lazygit/pkg/gui.NewGui.func5.(*AppStatusHelper).WithWaitingStatus.1({0x7ff70fb3d6a0?, 0xc0008ce4c0?})
/home/runner/work/lazygit/lazygit/pkg/gui/controllers/helpers/app_status_helper.go:63 +0x39
github.com/jesseduffield/gocui.(*Gui).onWorkerAux(0xc00030e000, 0x7ff70f589536?, {0x7ff70fb3d6a0?, 0xc0008ce4c0?})
/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:721 +0x65
github.com/jesseduffield/gocui.(*Gui).OnWorker.func1()
/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:708 +0x2b
created by github.com/jesseduffield/gocui.(*Gui).OnWorker in goroutine 1
/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:707 +0x90
```
### Expected behavior
The rebase-related operations like "Move patch out into index" shouldn't even be available to select in this scenario. The `PatchBuilder` state including `CanRebase` is essentially snapshotted at the patch creation (true in this case because patch was started in commits view) but then not updated when the current branch changes.
### Version info:
```
commit=80223c7fbbd0b86631a988834cbe88e267bb0bd9, build date=2025-11-01T09:43:19Z, build source=binaryRelease, version=0.56.0, os=windows, arch=amd64, git version=2.46.0.windows.1
```
### Terminal info:
Windows Terminal
### Additional context
Can repro on master.
Contributor guide
Research direction
Reproduce the branch-switch workflow in the issue, then inspect PatchCommands.MovePatchIntoIndex in pkg/commands/git_commands/patch.go and RebaseCommands.BeginInteractiveRebaseForCommit in pkg/commands/git_commands/rebase.go. Trace how PatchBuilder and CanRebase are used by pkg/gui/controllers/custom_patch_options_menu_action.go after checkout. Done means the invalid rebase-related option is unavailable after switching branches and the workflow no longer crashes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- git, go
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 64/100