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

213 lines
5.8 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"
2022-01-28 11:44:36 +02:00
"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)
}
2022-01-29 10:09:20 +02:00
menuItems := []*types.MenuItem{
2020-02-14 14:32:52 +02:00
{
Label: "reset patch",
OnPress: gui.helpers.PatchBuilding.Reset,
Key: 'c',
},
2020-03-26 12:00:08 +02:00
{
Label: "apply patch",
OnPress: func() error { return gui.handleApplyPatch(false) },
Key: 'a',
2020-03-29 23:49:20 +02:00
},
{
Label: "apply patch in reverse",
OnPress: func() error { return gui.handleApplyPatch(true) },
Key: 'r',
2020-03-26 12:00:08 +02:00
},
{
Label: "Copy patch to clipboard",
OnPress: func() error { return gui.copyPatchToClipbaord() },
2023-01-18 02:20:47 +02:00
Key: 'y',
},
}
if gui.git.Patch.PatchManager.CanRebase && gui.git.Status.WorkingTreeState() == enums.REBASE_MODE_NONE {
2022-01-29 10:09:20 +02:00
menuItems = append(menuItems, []*types.MenuItem{
{
Label: fmt.Sprintf("remove patch from original commit (%s)", gui.git.Patch.PatchManager.To),
OnPress: gui.handleDeletePatchFromCommit,
Key: 'd',
},
{
Label: "move patch out into index",
OnPress: gui.handleMovePatchIntoWorkingTree,
Key: 'i',
},
{
Label: "move patch into new commit",
OnPress: gui.handlePullPatchIntoNewCommit,
Key: 'n',
},
}...)
2022-02-13 08:01:53 +02:00
if gui.currentContext().GetKey() == gui.State.Contexts.LocalCommits.GetKey() {
selectedCommit := gui.getSelectedLocalCommit()
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(
2022-01-29 10:09:20 +02:00
[]*types.MenuItem{
2020-08-21 12:54:09 +02:00
{
Label: fmt.Sprintf("move patch to selected commit (%s)", selectedCommit.Sha),
OnPress: gui.handleMovePatchToSelectedCommit,
Key: 'm',
2020-08-21 12:54:09 +02:00
},
}, menuItems[1:]...,
)...,
)
}
2020-08-21 12:51:39 +02:00
}
}
2022-01-29 10:09:20 +02:00
return gui.c.Menu(types.CreateMenuOptions{Title: gui.c.Tr.PatchOptionsTitle, Items: menuItems})
}
func (gui *Gui) getPatchCommitIndex() int {
2022-01-31 13:11:34 +02:00
for index, commit := range gui.State.Model.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) returnFocusFromPatchExplorerIfNecessary() error {
if gui.currentContext().GetKey() == gui.State.Contexts.CustomPatchBuilder.GetKey() {
return gui.helpers.PatchBuilding.Escape()
}
return nil
}
func (gui *Gui) handleDeletePatchFromCommit() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromPatchExplorerIfNecessary(); 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)
2022-01-31 13:11:34 +02:00
err := gui.git.Patch.DeletePatchesFromCommit(gui.State.Model.Commits, commitIndex)
2022-02-06 06:54:26 +02:00
return gui.helpers.MergeAndRebase.CheckMergeOrRebase(err)
})
}
func (gui *Gui) handleMovePatchToSelectedCommit() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromPatchExplorerIfNecessary(); 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)
2022-02-13 08:01:53 +02:00
err := gui.git.Patch.MovePatchToSelectedCommit(gui.State.Model.Commits, commitIndex, gui.State.Contexts.LocalCommits.GetSelectedLineIdx())
2022-02-06 06:54:26 +02:00
return gui.helpers.MergeAndRebase.CheckMergeOrRebase(err)
})
}
func (gui *Gui) handleMovePatchIntoWorkingTree() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromPatchExplorerIfNecessary(); 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)
2022-01-31 13:11:34 +02:00
err := gui.git.Patch.MovePatchIntoIndex(gui.State.Model.Commits, commitIndex, stash)
2022-02-06 06:54:26 +02:00
return gui.helpers.MergeAndRebase.CheckMergeOrRebase(err)
})
}
2022-01-31 13:11:34 +02:00
if gui.helpers.WorkingTree.IsWorkingTreeDirty() {
return gui.c.Confirm(types.ConfirmOpts{
Title: gui.c.Tr.MustStashTitle,
Prompt: gui.c.Tr.MustStashWarning,
2022-01-28 11:44:36 +02:00
HandleConfirm: func() error {
2020-08-15 08:36:39 +02:00
return pull(true)
},
})
} else {
return pull(false)
}
}
func (gui *Gui) handlePullPatchIntoNewCommit() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
if err := gui.returnFocusFromPatchExplorerIfNecessary(); 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)
2022-01-31 13:11:34 +02:00
err := gui.git.Patch.PullPatchIntoNewCommit(gui.State.Model.Commits, commitIndex)
2022-02-06 06:54:26 +02:00
return gui.helpers.MergeAndRebase.CheckMergeOrRebase(err)
})
}
2020-03-29 23:49:20 +02:00
func (gui *Gui) handleApplyPatch(reverse bool) error {
if err := gui.returnFocusFromPatchExplorerIfNecessary(); err != nil {
2020-03-26 12:00:08 +02:00
return err
}
action := gui.c.Tr.Actions.ApplyPatch
if reverse {
2022-01-05 03:01:59 +02:00
action = "Apply patch in reverse"
}
gui.c.LogAction(action)
if err := gui.git.Patch.PatchManager.ApplyPatches(reverse); err != nil {
return gui.c.Error(err)
2020-03-26 12:00:08 +02:00
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
2020-03-26 12:00:08 +02:00
}
func (gui *Gui) copyPatchToClipbaord() error {
patch := gui.git.Patch.PatchManager.RenderAggregatedPatchColored(true)
gui.c.LogAction(gui.c.Tr.Actions.CopyPatchToClipboard)
if err := gui.os.CopyToClipboard(patch); err != nil {
return gui.c.Error(err)
}
2023-01-18 02:20:47 +02:00
gui.c.Toast(gui.c.Tr.Actions.CopyPatchToClipboard)
return nil
}