From 61f0801bd366e959437676ae72b2d9fe98c4b8af Mon Sep 17 00:00:00 2001 From: Kristijan Husak Date: Wed, 12 Sep 2018 15:20:35 +0200 Subject: [PATCH] Add ammend commit action. --- pkg/commands/git.go | 8 ++++++-- pkg/gui/commit_message_panel.go | 3 ++- pkg/gui/files_panel.go | 27 +++++++++++++++++++++++++++ pkg/gui/keybindings.go | 7 +++++++ pkg/i18n/dutch.go | 3 +++ pkg/i18n/english.go | 3 +++ pkg/i18n/polish.go | 3 +++ 7 files changed, 51 insertions(+), 3 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 5744fa6aa..a6f9e2f9c 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -299,8 +299,12 @@ func (c *GitCommand) usingGpg() bool { } // Commit commits to git -func (c *GitCommand) Commit(message string) (*exec.Cmd, error) { - command := fmt.Sprintf("git commit -m %s", c.OSCommand.Quote(message)) +func (c *GitCommand) Commit(message string, amend bool) (*exec.Cmd, error) { + amendParam := "" + if amend { + amendParam = "--amend " + } + command := fmt.Sprintf("git commit %s-m %s", amendParam, c.OSCommand.Quote(message)) if c.usingGpg() { return c.OSCommand.PrepareSubProcess(c.OSCommand.Platform.shell, c.OSCommand.Platform.shellArg, command), nil } diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go index e23c47da0..2c3d086c7 100644 --- a/pkg/gui/commit_message_panel.go +++ b/pkg/gui/commit_message_panel.go @@ -12,7 +12,8 @@ func (gui *Gui) handleCommitConfirm(g *gocui.Gui, v *gocui.View) error { if message == "" { return gui.createErrorPanel(g, gui.Tr.SLocalize("CommitWithoutMessageErr")) } - sub, err := gui.GitCommand.Commit(message) + amendCommit := v.Title == gui.Tr.SLocalize("AmendLastCommit") + sub, err := gui.GitCommand.Commit(message, amendCommit) if err != nil { // TODO need to find a way to send through this error if err != gui.Errors.ErrSubProcess { diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index bfcc938bb..235cb12d8 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -198,7 +198,14 @@ func (gui *Gui) handleCommitPress(g *gocui.Gui, filesView *gocui.View) error { if len(gui.stagedFiles()) == 0 && !gui.State.HasMergeConflicts { return gui.createErrorPanel(g, gui.Tr.SLocalize("NoStagedFilesToCommit")) } + commitMessageView := gui.getCommitMessageView(g) + if commitMessageView.Title == gui.Tr.SLocalize("AmendLastCommit") { + commitMessageView.Clear() + commitMessageView.SetCursor(0, 0) + } + + commitMessageView.Title = gui.Tr.SLocalize("CommitMessage") g.Update(func(g *gocui.Gui) error { g.SetViewOnTop("commitMessage") gui.switchFocus(g, filesView, commitMessageView) @@ -208,6 +215,26 @@ func (gui *Gui) handleCommitPress(g *gocui.Gui, filesView *gocui.View) error { return nil } +func (gui *Gui) handleAmendCommitPress(g *gocui.Gui, filesView *gocui.View) error { + if len(gui.stagedFiles()) == 0 && !gui.State.HasMergeConflicts { + return gui.createErrorPanel(g, gui.Tr.SLocalize("NoStagedFilesToCommit")) + } + commitMessageView := gui.getCommitMessageView(g) + commitMessageView.Clear() + commitMessageView.Title = gui.Tr.SLocalize("AmendLastCommit") + + g.Update(func(g *gocui.Gui) error { + g.SetViewOnTop("commitMessage") + gui.switchFocus(g, filesView, commitMessageView) + lastCommitName := gui.State.Commits[0].Name + commitMessageView.Write([]byte(lastCommitName)) + commitMessageView.SetCursor(len(lastCommitName), 0) + gui.RenderCommitLength() + return nil + }) + return nil +} + // handleCommitEditorPress - handle when the user wants to commit changes via // their editor rather than via the popup panel func (gui *Gui) handleCommitEditorPress(g *gocui.Gui, filesView *gocui.View) error { diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index c8ed7d3c8..3eb8785d2 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -1,6 +1,7 @@ package gui import "github.com/jesseduffield/gocui" +import "strings" // Binding - a keybinding mapping a key and modifier to a handler. The keypress // is only handled if the given view has focus, or handled globally if the view @@ -98,6 +99,12 @@ func (gui *Gui) GetKeybindings() []Binding { Modifier: gocui.ModNone, Handler: gui.handleCommitPress, Description: gui.Tr.SLocalize("CommitChanges"), + }, { + ViewName: "files", + Key: 'M', + Modifier: gocui.ModNone, + Handler: gui.handleAmendCommitPress, + Description: strings.ToLower(gui.Tr.SLocalize("AmendLastCommit")), }, { ViewName: "files", Key: 'C', diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index cd1282de2..ef10625d5 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -34,6 +34,9 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CommitChanges", Other: "Commit Veranderingen", + }, &i18n.Message{ + ID: "AmendLastCommit", + Other: "Wijzig laatste commit", }, &i18n.Message{ ID: "CommitChangesWithEditor", Other: "commit Veranderingen met de git editor", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 4f009c075..796e6db62 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -42,6 +42,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CommitChanges", Other: "commit changes", + }, &i18n.Message{ + ID: "AmendLastCommit", + Other: "Amend last commit", }, &i18n.Message{ ID: "CommitChangesWithEditor", Other: "commit changes using git editor", diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index c64ab87b8..d4f204a3f 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -32,6 +32,9 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CommitChanges", Other: "commituj zmiany", + }, &i18n.Message{ + ID: "AmendLastCommit", + Other: "Zmień ostatnie zatwierdzenie", }, &i18n.Message{ ID: "CommitChangesWithEditor", Other: "commituj zmiany używając edytora z gita",