1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/patch_options_panel.go

197 lines
5.4 KiB
Go
Raw Normal View History

package gui
import (
"fmt"
2021-12-30 04:35:10 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
)
func (gui *Gui) handleCreatePatchOptionsMenu() error {
2022-01-08 05:10:01 +02:00
if !gui.Git.Patch.PatchManager.Active() {
2020-10-04 02:00:48 +02:00
return gui.createErrorPanel(gui.Tr.NoPatchError)
}
2020-02-14 14:32:52 +02:00
menuItems := []*menuItem{
{
displayString: "reset patch",
onPress: gui.handleResetPatch,
},
2020-03-26 12:00:08 +02:00
{
displayString: "apply patch",
2020-03-29 23:49:20 +02:00
onPress: func() error { return gui.handleApplyPatch(false) },
},
{
displayString: "apply patch in reverse",
onPress: func() error { return gui.handleApplyPatch(true) },
2020-03-26 12:00:08 +02:00
},
}
2022-01-08 05:10:01 +02:00
if gui.Git.Patch.PatchManager.CanRebase && gui.Git.Status.WorkingTreeState() == enums.REBASE_MODE_NONE {
menuItems = append(menuItems, []*menuItem{
{
2022-01-08 05:10:01 +02:00
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,
},
}...)
2021-04-03 06:56:11 +02:00
if gui.currentContext().GetKey() == gui.State.Contexts.BranchCommits.GetKey() {
selectedCommit := gui.getSelectedLocalCommit()
2022-01-08 05:10:01 +02:00
if selectedCommit != nil && gui.Git.Patch.PatchManager.To != selectedCommit.Sha {
2020-08-21 12:54:09 +02:00
// adding this option to index 1
menuItems = append(
menuItems[:1],
append(
[]*menuItem{
{
displayString: fmt.Sprintf("move patch to selected commit (%s)", selectedCommit.Sha),
onPress: gui.handleMovePatchToSelectedCommit,
},
}, menuItems[1:]...,
)...,
)
}
2020-08-21 12:51:39 +02:00
}
}
2020-10-04 02:00:48 +02:00
return gui.createMenu(gui.Tr.PatchOptionsTitle, menuItems, createMenuOptions{showCancel: true})
}
func (gui *Gui) getPatchCommitIndex() int {
for index, commit := range gui.State.Commits {
2022-01-08 05:10:01 +02:00
if commit.Sha == gui.Git.Patch.PatchManager.To {
return index
}
}
return -1
}
func (gui *Gui) validateNormalWorkingTreeState() (bool, error) {
2022-01-08 05:10:01 +02:00
if gui.Git.Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
2020-10-04 02:00:48 +02:00
return false, gui.createErrorPanel(gui.Tr.CantPatchWhileRebasingError)
}
return true, nil
}
func (gui *Gui) returnFocusFromLineByLinePanelIfNecessary() error {
2020-08-23 02:13:56 +02:00
if gui.State.MainContext == MAIN_PATCH_BUILDING_CONTEXT_KEY {
2020-08-15 09:23:16 +02:00
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
}
2020-10-04 02:00:48 +02:00
return gui.WithWaitingStatus(gui.Tr.RebasingStatus, func() error {
commitIndex := gui.getPatchCommitIndex()
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.RemovePatchFromCommit)
2022-01-08 05:10:01 +02:00
err := gui.Git.Patch.DeletePatchesFromCommit(gui.State.Commits, commitIndex)
return gui.handleGenericMergeCommandResult(err)
})
}
func (gui *Gui) handleMovePatchToSelectedCommit() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
return err
}
2020-10-04 02:00:48 +02:00
return gui.WithWaitingStatus(gui.Tr.RebasingStatus, func() error {
commitIndex := gui.getPatchCommitIndex()
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.MovePatchToSelectedCommit)
2022-01-08 05:10:01 +02:00
err := gui.Git.Patch.MovePatchToSelectedCommit(gui.State.Commits, commitIndex, gui.State.Panels.Commits.SelectedLineIdx)
return gui.handleGenericMergeCommandResult(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 {
2020-10-04 02:00:48 +02:00
return gui.WithWaitingStatus(gui.Tr.RebasingStatus, func() error {
commitIndex := gui.getPatchCommitIndex()
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.MovePatchIntoIndex)
2022-01-08 05:10:01 +02:00
err := gui.Git.Patch.MovePatchIntoIndex(gui.State.Commits, commitIndex, stash)
return gui.handleGenericMergeCommandResult(err)
})
}
if len(gui.trackedFiles()) > 0 {
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.MustStashTitle,
prompt: gui.Tr.MustStashWarning,
2020-08-15 08:36:39 +02:00
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
}
2020-10-04 02:00:48 +02:00
return gui.WithWaitingStatus(gui.Tr.RebasingStatus, func() error {
commitIndex := gui.getPatchCommitIndex()
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.MovePatchIntoNewCommit)
2022-01-08 05:10:01 +02:00
err := gui.Git.Patch.PullPatchIntoNewCommit(gui.State.Commits, commitIndex)
return gui.handleGenericMergeCommandResult(err)
})
}
2020-03-29 23:49:20 +02:00
func (gui *Gui) handleApplyPatch(reverse bool) error {
2020-03-26 12:00:08 +02:00
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
return err
}
2022-01-05 03:01:59 +02:00
action := gui.Tr.Actions.ApplyPatch
if reverse {
2022-01-05 03:01:59 +02:00
action = "Apply patch in reverse"
}
2022-01-05 03:01:59 +02:00
gui.logAction(action)
2022-01-08 05:10:01 +02:00
if err := gui.Git.Patch.PatchManager.ApplyPatches(reverse); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
2020-03-26 12:00:08 +02:00
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
2020-03-26 12:00:08 +02:00
}
2019-11-05 09:10:47 +02:00
func (gui *Gui) handleResetPatch() error {
2022-01-08 05:10:01 +02:00
gui.Git.Patch.PatchManager.Reset()
2020-08-23 03:23:32 +02:00
if gui.currentContextKeyIgnoringPopups() == MAIN_PATCH_BUILDING_CONTEXT_KEY {
2021-04-03 06:56:11 +02:00
if err := gui.pushContext(gui.State.Contexts.CommitFiles); err != nil {
2020-08-23 03:23:32 +02:00
return err
}
}
return gui.refreshCommitFilesView()
}