2019-11-04 10:47:25 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (gui *Gui) handleCreatePatchOptionsMenu(g *gocui.Gui, v *gocui.View) error {
|
2019-11-10 06:24:20 +02:00
|
|
|
if !gui.GitCommand.PatchManager.CommitSelected() {
|
2020-03-28 02:47:54 +02:00
|
|
|
return gui.createErrorPanel(gui.Tr.SLocalize("NoPatchError"))
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 14:32:52 +02:00
|
|
|
menuItems := []*menuItem{
|
|
|
|
{
|
|
|
|
displayString: fmt.Sprintf("remove patch from original commit (%s)", gui.GitCommand.PatchManager.CommitSha),
|
|
|
|
onPress: gui.handleDeletePatchFromCommit,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayString: "pull patch out into index",
|
|
|
|
onPress: gui.handlePullPatchIntoWorkingTree,
|
|
|
|
},
|
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
|
|
|
},
|
2020-02-14 14:32:52 +02:00
|
|
|
{
|
|
|
|
displayString: "reset patch",
|
|
|
|
onPress: gui.handleResetPatch,
|
|
|
|
},
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 05:34:17 +02:00
|
|
|
selectedCommit := gui.getSelectedCommit()
|
2019-11-05 02:55:04 +02:00
|
|
|
if selectedCommit != nil && gui.GitCommand.PatchManager.CommitSha != selectedCommit.Sha {
|
2019-11-05 03:42:19 +02:00
|
|
|
// adding this option to index 1
|
2020-02-14 14:32:52 +02:00
|
|
|
menuItems = append(
|
|
|
|
menuItems[:1],
|
2019-11-05 03:42:19 +02:00
|
|
|
append(
|
2020-02-14 14:32:52 +02:00
|
|
|
[]*menuItem{
|
2019-11-05 03:42:19 +02:00
|
|
|
{
|
2020-02-14 14:32:52 +02:00
|
|
|
displayString: fmt.Sprintf("move patch to selected commit (%s)", selectedCommit.Sha),
|
|
|
|
onPress: gui.handleMovePatchToSelectedCommit,
|
2019-11-05 03:42:19 +02:00
|
|
|
},
|
2020-02-14 14:32:52 +02:00
|
|
|
}, menuItems[1:]...,
|
2019-11-05 03:42:19 +02:00
|
|
|
)...,
|
|
|
|
)
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 14:39:02 +02:00
|
|
|
return gui.createMenu(gui.Tr.SLocalize("PatchOptionsTitle"), menuItems, createMenuOptions{showCancel: true})
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) getPatchCommitIndex() int {
|
|
|
|
for index, commit := range gui.State.Commits {
|
2019-11-05 02:55:04 +02:00
|
|
|
if commit.Sha == gui.GitCommand.PatchManager.CommitSha {
|
2019-11-04 10:47:25 +02:00
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-11-05 08:57:59 +02:00
|
|
|
func (gui *Gui) validateNormalWorkingTreeState() (bool, error) {
|
2020-03-28 07:28:35 +02:00
|
|
|
if gui.GitCommand.WorkingTreeState() != "normal" {
|
|
|
|
return false, gui.createErrorPanel(gui.Tr.SLocalize("CantPatchWhileRebasingError"))
|
|
|
|
}
|
2020-03-28 03:08:13 +02:00
|
|
|
if gui.GitCommand.WorkingTreeState() != "normal" {
|
2020-03-28 02:47:54 +02:00
|
|
|
return false, gui.createErrorPanel(gui.Tr.SLocalize("CantPatchWhileRebasingError"))
|
2019-11-05 08:57:59 +02:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) returnFocusFromLineByLinePanelIfNecessary() error {
|
2019-11-16 03:41:04 +02:00
|
|
|
if gui.State.MainContext == "patch-building" {
|
2019-11-05 08:57:59 +02:00
|
|
|
return gui.handleEscapePatchBuildingPanel(gui.g, nil)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:47:25 +02:00
|
|
|
func (gui *Gui) handleDeletePatchFromCommit() error {
|
2019-11-05 02:53:01 +02:00
|
|
|
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-04 10:47:25 +02:00
|
|
|
|
2019-11-05 08:57:59 +02:00
|
|
|
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:47:25 +02:00
|
|
|
return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
|
|
|
|
commitIndex := gui.getPatchCommitIndex()
|
2019-11-05 02:55:04 +02:00
|
|
|
err := gui.GitCommand.DeletePatchesFromCommit(gui.State.Commits, commitIndex, gui.GitCommand.PatchManager)
|
2019-11-04 10:47:25 +02:00
|
|
|
return gui.handleGenericMergeCommandResult(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleMovePatchToSelectedCommit() error {
|
2019-11-05 02:53:01 +02:00
|
|
|
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-04 10:47:25 +02:00
|
|
|
|
2019-11-05 08:57:59 +02:00
|
|
|
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:47:25 +02:00
|
|
|
return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
|
|
|
|
commitIndex := gui.getPatchCommitIndex()
|
2019-11-05 02:55:04 +02:00
|
|
|
err := gui.GitCommand.MovePatchToSelectedCommit(gui.State.Commits, commitIndex, gui.State.Panels.Commits.SelectedLine, gui.GitCommand.PatchManager)
|
2019-11-04 10:47:25 +02:00
|
|
|
return gui.handleGenericMergeCommandResult(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handlePullPatchIntoWorkingTree() error {
|
2019-11-05 02:53:01 +02:00
|
|
|
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-04 10:47:25 +02:00
|
|
|
|
2019-11-05 08:57:59 +02:00
|
|
|
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-28 03:43:31 +02:00
|
|
|
pull := func(stash bool) error {
|
|
|
|
return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
|
|
|
|
commitIndex := gui.getPatchCommitIndex()
|
|
|
|
err := gui.GitCommand.PullPatchIntoIndex(gui.State.Commits, commitIndex, gui.GitCommand.PatchManager, stash)
|
|
|
|
return gui.handleGenericMergeCommandResult(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(gui.trackedFiles()) > 0 {
|
|
|
|
return gui.createConfirmationPanel(gui.g, gui.g.CurrentView(), true, gui.Tr.SLocalize("MustStashTitle"), gui.Tr.SLocalize("MustStashWarning"), func(*gocui.Gui, *gocui.View) error {
|
|
|
|
return pull(true)
|
|
|
|
}, nil)
|
|
|
|
} else {
|
|
|
|
return pull(false)
|
|
|
|
}
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-29 23:49:20 +02:00
|
|
|
if err := gui.GitCommand.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
|
|
|
}
|
2020-03-27 10:12:15 +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 {
|
|
|
|
gui.GitCommand.PatchManager.Reset()
|
2019-11-04 10:47:25 +02:00
|
|
|
return gui.refreshCommitFilesView()
|
|
|
|
}
|