1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-26 05:37:18 +02:00
lazygit/pkg/gui/patch_options_panel.go
Jesse Duffield 1dd7307fde start moving commit panel handlers into controller
more

and more

move rebase commit refreshing into existing abstraction

and more

and more

WIP

and more

handling clicks

properly fix merge conflicts

update cheatsheet

lots more preparation to start moving things into controllers

WIP

better typing

expand on remotes controller

moving more code into controllers
2022-03-17 19:13:40 +11:00

199 lines
5.5 KiB
Go

package gui
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
"github.com/jesseduffield/lazygit/pkg/gui/popup"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
func (gui *Gui) handleCreatePatchOptionsMenu() error {
if !gui.git.Patch.PatchManager.Active() {
return gui.c.ErrorMsg(gui.c.Tr.NoPatchError)
}
menuItems := []*popup.MenuItem{
{
DisplayString: "reset patch",
OnPress: gui.handleResetPatch,
},
{
DisplayString: "apply patch",
OnPress: func() error { return gui.handleApplyPatch(false) },
},
{
DisplayString: "apply patch in reverse",
OnPress: func() error { return gui.handleApplyPatch(true) },
},
}
if gui.git.Patch.PatchManager.CanRebase && gui.git.Status.WorkingTreeState() == enums.REBASE_MODE_NONE {
menuItems = append(menuItems, []*popup.MenuItem{
{
DisplayString: fmt.Sprintf("remove patch from original commit (%s)", gui.git.Patch.PatchManager.To),
OnPress: gui.handleDeletePatchFromCommit,
},
{
DisplayString: "move patch out into index",
OnPress: gui.handleMovePatchIntoWorkingTree,
},
{
DisplayString: "move patch into new commit",
OnPress: gui.handlePullPatchIntoNewCommit,
},
}...)
if gui.currentContext().GetKey() == gui.State.Contexts.BranchCommits.GetKey() {
selectedCommit := gui.getSelectedLocalCommit()
if selectedCommit != nil && gui.git.Patch.PatchManager.To != selectedCommit.Sha {
// adding this option to index 1
menuItems = append(
menuItems[:1],
append(
[]*popup.MenuItem{
{
DisplayString: fmt.Sprintf("move patch to selected commit (%s)", selectedCommit.Sha),
OnPress: gui.handleMovePatchToSelectedCommit,
},
}, menuItems[1:]...,
)...,
)
}
}
}
return gui.c.Menu(popup.CreateMenuOptions{Title: gui.c.Tr.PatchOptionsTitle, Items: menuItems})
}
func (gui *Gui) getPatchCommitIndex() int {
for index, commit := range gui.State.Commits {
if commit.Sha == gui.git.Patch.PatchManager.To {
return index
}
}
return -1
}
func (gui *Gui) validateNormalWorkingTreeState() (bool, error) {
if gui.git.Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
return false, gui.c.ErrorMsg(gui.c.Tr.CantPatchWhileRebasingError)
}
return true, nil
}
func (gui *Gui) returnFocusFromLineByLinePanelIfNecessary() error {
if gui.State.MainContext == MAIN_PATCH_BUILDING_CONTEXT_KEY {
return gui.handleEscapePatchBuildingPanel()
}
return nil
}
func (gui *Gui) handleDeletePatchFromCommit() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
return err
}
return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error {
commitIndex := gui.getPatchCommitIndex()
gui.c.LogAction(gui.c.Tr.Actions.RemovePatchFromCommit)
err := gui.git.Patch.DeletePatchesFromCommit(gui.State.Commits, commitIndex)
return gui.checkMergeOrRebase(err)
})
}
func (gui *Gui) handleMovePatchToSelectedCommit() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
return err
}
return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error {
commitIndex := gui.getPatchCommitIndex()
gui.c.LogAction(gui.c.Tr.Actions.MovePatchToSelectedCommit)
err := gui.git.Patch.MovePatchToSelectedCommit(gui.State.Commits, commitIndex, gui.State.Panels.Commits.SelectedLineIdx)
return gui.checkMergeOrRebase(err)
})
}
func (gui *Gui) handleMovePatchIntoWorkingTree() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
return err
}
pull := func(stash bool) error {
return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error {
commitIndex := gui.getPatchCommitIndex()
gui.c.LogAction(gui.c.Tr.Actions.MovePatchIntoIndex)
err := gui.git.Patch.MovePatchIntoIndex(gui.State.Commits, commitIndex, stash)
return gui.checkMergeOrRebase(err)
})
}
if gui.workingTreeHelper.IsWorkingTreeDirty() {
return gui.c.Ask(popup.AskOpts{
Title: gui.c.Tr.MustStashTitle,
Prompt: gui.c.Tr.MustStashWarning,
HandleConfirm: func() error {
return pull(true)
},
})
} else {
return pull(false)
}
}
func (gui *Gui) handlePullPatchIntoNewCommit() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
return err
}
return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error {
commitIndex := gui.getPatchCommitIndex()
gui.c.LogAction(gui.c.Tr.Actions.MovePatchIntoNewCommit)
err := gui.git.Patch.PullPatchIntoNewCommit(gui.State.Commits, commitIndex)
return gui.checkMergeOrRebase(err)
})
}
func (gui *Gui) handleApplyPatch(reverse bool) error {
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
return err
}
action := gui.c.Tr.Actions.ApplyPatch
if reverse {
action = "Apply patch in reverse"
}
gui.c.LogAction(action)
if err := gui.git.Patch.PatchManager.ApplyPatches(reverse); err != nil {
return gui.c.Error(err)
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}
func (gui *Gui) handleResetPatch() error {
gui.git.Patch.PatchManager.Reset()
if gui.currentContextKeyIgnoringPopups() == MAIN_PATCH_BUILDING_CONTEXT_KEY {
if err := gui.c.PushContext(gui.State.Contexts.CommitFiles); err != nil {
return err
}
}
return gui.refreshCommitFilesView()
}