Repository: jesseduffield/lazygit (language: go)

Issue:
Crash moving patch into new commit (index out of range)
**Describe the bug**
When I try to do Patch Options such as "Move patch into new commit with nothing selected in Custom Patch I get a crash. 

**To Reproduce**
Steps to reproduce the behavior:

This will occur in any repo:
* enter lazygit
* go to Commits view 
* enter patch view for some file (enter, enter)
* Control-P to "View custom patch options"
* select something like `i` "Move patch into new commit" 
* observe crash:



**Expected behavior**
If you repeat these steps at the commit view instead of patch view, you get an error (without crashing):
`No patch created yet. To start building a patch, use 'space' on a commit file or enter to add specific lines`. Something like this would be useful. 

**Screenshots**

```
panic: runtime error: index out of range [-1]

goroutine 157 [running]:
github.com/jesseduffield/lazygit/pkg/commands/git_commands.(*RebaseCommands).BeginInteractiveRebaseForCommit(0x14000150300, {0x14000324280, 0x4, 0x140001128f0?}, 0xffffffffffffffff, 0x0)
        github.com/jesseduffield/lazygit/pkg/commands/git_commands/rebase.go:292 +0x278
github.com/jesseduffield/lazygit/pkg/commands/git_commands.(*PatchCommands).MovePatchIntoIndex(0x140002fe330, {0x14000324280?, 0x4, 0x10546a9ac?}, 0xffffffffffffffff, 0x0)
        github.com/jesseduffield/lazygit/pkg/commands/git_commands/patch.go:228 +0xcc
github.com/jesseduffield/lazygit/pkg/gui/controllers.(*CustomPatchOptionsMenuAction).handleMovePatchIntoWorkingTree.func1.1({0x14000030550, 0x14000068900})
        github.com/jesseduffield/lazygit/pkg/gui/controllers/custom_patch_options_menu_action.go:159 +0xe0
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*AppStatusHelper).WithWaitingStatus.func1.1()
        github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers/app_status_helper.go:35 +0x54
github.com/jesseduffield/lazygit/pkg/gui/status.(*StatusManager).WithWaitingStatus(0x1400038e330, {0x105522c4d, 0x8}, 0x140003eaf20)
        github.com/jesseduffield/lazygit/pkg/gui/status/status_manager.go:44 +0x1d8
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*AppStatusHelper).WithWaitingStatus.func1({0x1057e4f70, 0x14000324820})
        github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers/app_status_helper.go:32 +0x94
github.com/jesseduffield/gocui.(*Gui).onWorkerAux(0x0?, 0x0?, {0x1057e4f70?, 0x14000324820?})
        github.com/jesseduffield/gocui@v0.3.1-0.20230723014157-03e858e46144/gui.go:673 +0x6c
github.com/jesseduffield/gocui.(*Gui).OnWorker.func1()
        github.com/jesseduffield/gocui@v0.3.1-0.20230723014157-03e858e46144/gui.go:660 +0x34
created by github.com/jesseduffield/gocui.(*Gui).OnWorker
        github.com/jesseduffield/gocui@v0.3.1-0.20230723014157-03e858e46144/gui.go:659 +0x9c
```

**Version info:**
`commit=, build date=, build source=homebrew, version=0.39.4, os=darwin, arch=arm64, git version=2.39.2 (Apple Git-143)`
`git version 2.39.2 (Apple Git-143)`

**Additional context**
N/A

**Note:** please try updating to the latest version or [manually building](https://github.com/jesseduffield/lazygit/#manual) the latest `master` to see if the issue still occurs.

<!--
If you want to try and debug this issue yourself, you can run `lazygit --debug` in one terminal panel and `lazygit --logs` in another to view the logs.
-->



Discussion / hints:
Might have been a recent regression caused by https://github.com/jesseduffield/lazygit/pull/2800. @alexpopov did you build lazygit from source?
> Might have been a recent regression caused by #2800. @alexpopov did you build lazygit from source?

No, I just realized: I got the error from some older build, decided to update in case it fixed it; and did a `brew upgrade` to get this latest version. 

That said, the earlier build I had used may also have been quite recent. 

Fix patch:
diff --git a/pkg/gui/controllers/custom_patch_options_menu_action.go b/pkg/gui/controllers/custom_patch_options_menu_action.go
index 3adedfc1b98..a8feed168f3 100644
--- a/pkg/gui/controllers/custom_patch_options_menu_action.go
+++ b/pkg/gui/controllers/custom_patch_options_menu_action.go
@@ -18,6 +18,10 @@ func (self *CustomPatchOptionsMenuAction) Call() error {
 		return self.c.ErrorMsg(self.c.Tr.NoPatchError)
 	}
 
+	if self.c.Git().Patch.PatchBuilder.IsEmpty() {
+		return self.c.ErrorMsg(self.c.Tr.EmptyPatchError)
+	}
+
 	menuItems := []*types.MenuItem{
 		{
 			Label:   self.c.Tr.ResetPatch,
diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go
index 97522da54be..735ac3cf40c 100644
--- a/pkg/i18n/english.go
+++ b/pkg/i18n/english.go
@@ -323,6 +323,7 @@ type TranslationSet struct {
 	ViewPatchOptions                    string
 	PatchOptionsTitle                   string
 	NoPatchError                        string
+	EmptyPatchError                     string
 	EnterFile                           string
 	ExitCustomPatchBuilder              string
 	EnterUpstream                       string
@@ -1017,6 +1018,7 @@ func EnglishTranslationSet() TranslationSet {
 		ViewPatchOptions:                    "View custom patch options",
 		PatchOptionsTitle:                   "Patch options",
 		NoPatchError:                        "No patch created yet. To start building a patch, use 'space' on a commit file or enter to add specific lines",
+		EmptyPatchError:                     "Patch is still empty. Add some files or lines to your patch first.",
 		EnterFile:                           "Enter file to add selected lines to the patch (or toggle directory collapsed)",
 		ExitCustomPatchBuilder:              `Exit custom patch builder`,
 		EnterUpstream:                       `Enter upstream as '<remote> <branchname>'`,


Test patch: