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