summaryrefslogtreecommitdiffstats
path: root/pkg/gui/patch_options_panel.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-11-04 19:47:25 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-11-05 19:22:01 +1100
commitd5e443e8e3609fe38586aed942a3dae3343dbe47 (patch)
tree6d7465b9abd8df3ae903e6d95898054ac3a6d8b4 /pkg/gui/patch_options_panel.go
parenta3c84296bf2fbc8b132d5b2285eedba09813fbee (diff)
Support building and moving patches
WIP
Diffstat (limited to 'pkg/gui/patch_options_panel.go')
-rw-r--r--pkg/gui/patch_options_panel.go89
1 files changed, 89 insertions, 0 deletions
diff --git a/pkg/gui/patch_options_panel.go b/pkg/gui/patch_options_panel.go
new file mode 100644
index 000000000..14d13cd82
--- /dev/null
+++ b/pkg/gui/patch_options_panel.go
@@ -0,0 +1,89 @@
+package gui
+
+import (
+ "fmt"
+
+ "github.com/jesseduffield/gocui"
+)
+
+type patchMenuOption struct {
+ displayName string
+ function func() error
+}
+
+// GetDisplayStrings is a function.
+func (o *patchMenuOption) GetDisplayStrings(isFocused bool) []string {
+ return []string{o.displayName}
+}
+
+func (gui *Gui) handleCreatePatchOptionsMenu(g *gocui.Gui, v *gocui.View) error {
+ m := gui.State.PatchManager
+ if m == nil {
+ return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoPatchError"))
+ }
+
+ options := []*patchMenuOption{
+ {displayName: "discard patch", function: gui.handleDeletePatchFromCommit},
+ {displayName: "pull patch out into index", function: gui.handlePullPatchIntoWorkingTree},
+ {displayName: "save patch to file"},
+ {displayName: "clear patch", function: gui.handleClearPatch},
+ }
+
+ selectedCommit := gui.getSelectedCommit(gui.g)
+ if selectedCommit != nil && gui.State.PatchManager.CommitSha != selectedCommit.Sha {
+ options = append(options, &patchMenuOption{
+ displayName: fmt.Sprintf("move patch to selected commit (%s)", selectedCommit.Sha),
+ function: gui.handleMovePatchToSelectedCommit,
+ })
+ }
+
+ handleMenuPress := func(index int) error {
+ return options[index].function()
+ }
+
+ return gui.createMenu(gui.Tr.SLocalize("PatchOptionsTitle"), options, len(options), handleMenuPress)
+}
+
+func (gui *Gui) getPatchCommitIndex() int {
+ for index, commit := range gui.State.Commits {
+ if commit.Sha == gui.State.PatchManager.CommitSha {
+ return index
+ }
+ }
+ return -1
+}
+
+func (gui *Gui) handleDeletePatchFromCommit() error {
+ // TODO: deal with when we're already rebasing
+
+ return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
+ commitIndex := gui.getPatchCommitIndex()
+ err := gui.GitCommand.DeletePatchesFromCommit(gui.State.Commits, commitIndex, gui.State.PatchManager)
+ return gui.handleGenericMergeCommandResult(err)
+ })
+}
+
+func (gui *Gui) handleMovePatchToSelectedCommit() error {
+ // TODO: deal with when we're already rebasing
+
+ return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
+ commitIndex := gui.getPatchCommitIndex()
+ err := gui.GitCommand.MovePatchToSelectedCommit(gui.State.Commits, commitIndex, gui.State.Panels.Commits.SelectedLine, gui.State.PatchManager)
+ return gui.handleGenericMergeCommandResult(err)
+ })
+}
+
+func (gui *Gui) handlePullPatchIntoWorkingTree() error {
+ // TODO: deal with when we're already rebasing
+
+ return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
+ commitIndex := gui.getPatchCommitIndex()
+ err := gui.GitCommand.PullPatchIntoIndex(gui.State.Commits, commitIndex, gui.State.PatchManager)
+ return gui.handleGenericMergeCommandResult(err)
+ })
+}
+
+func (gui *Gui) handleClearPatch() error {
+ gui.State.PatchManager = nil
+ return gui.refreshCommitFilesView()
+}