diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-04-02 19:53:16 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-05-06 22:44:38 +1000 |
commit | 5a0d0bb299eb2e4d693d3ecab506cc154e97678a (patch) | |
tree | 8c916afb93e7b5cae75b56c3c1431efa452dc29f /pkg | |
parent | 2746b1bd38fa2668490a8db2369dec5146fbaec6 (diff) |
support resetting to a commit in either soft, hard, or mixed mode
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/git.go | 4 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 4 | ||||
-rw-r--r-- | pkg/gui/commits_panel.go | 51 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 2 | ||||
-rw-r--r-- | pkg/i18n/dutch.go | 3 | ||||
-rw-r--r-- | pkg/i18n/english.go | 3 | ||||
-rw-r--r-- | pkg/i18n/polish.go | 3 |
7 files changed, 64 insertions, 6 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 64f3cd4b1..159c88cca 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -277,8 +277,8 @@ func (c *GitCommand) Fetch(unamePassQuestion func(string) string, canAskForCrede } // ResetToCommit reset to commit -func (c *GitCommand) ResetToCommit(sha string) error { - return c.OSCommand.RunCommand(fmt.Sprintf("git reset %s", sha)) +func (c *GitCommand) ResetToCommit(sha string, strength string) error { + return c.OSCommand.RunCommand(fmt.Sprintf("git reset --%s %s", strength, sha)) } // NewBranch create new branch diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 3358be054..0d59c30b7 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -616,12 +616,12 @@ func TestGitCommandResetToCommit(t *testing.T) { gitCmd := NewDummyGitCommand() gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"reset", "78976bc"}, args) + assert.EqualValues(t, []string{"reset", "--hard", "78976bc"}, args) return exec.Command("echo") } - assert.NoError(t, gitCmd.ResetToCommit("78976bc")) + assert.NoError(t, gitCmd.ResetToCommit("78976bc", "hard")) } // TestGitCommandNewBranch is a function. diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 98817bf59..c35b4adee 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" + "github.com/fatih/color" "github.com/go-errors/errors" "github.com/jesseduffield/gocui" @@ -124,7 +125,8 @@ func (gui *Gui) handleResetToCommit(g *gocui.Gui, commitView *gocui.View) error if commit == nil { panic(errors.New(gui.Tr.SLocalize("NoCommitsThisBranch"))) } - if err := gui.GitCommand.ResetToCommit(commit.Sha); err != nil { + + if err := gui.GitCommand.ResetToCommit(commit.Sha, "mixed"); err != nil { return gui.createErrorPanel(g, err.Error()) } if err := gui.refreshCommits(g); err != nil { @@ -554,3 +556,50 @@ func (gui *Gui) handleSquashAllAboveFixupCommits(g *gocui.Gui, v *gocui.View) er }) }, nil) } + +type resetOption struct { + description string + command string +} + +// GetDisplayStrings is a function. +func (r *resetOption) GetDisplayStrings(isFocused bool) []string { + return []string{r.description, color.New(color.FgRed).Sprint(r.command)} +} + +func (gui *Gui) handleCreateCommitResetMenu(g *gocui.Gui, v *gocui.View) error { + commit := gui.getSelectedCommit(g) + if commit == nil { + return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoCommitsThisBranch")) + } + + strengths := []string{"soft", "mixed", "hard"} + options := make([]*resetOption, len(strengths)) + for i, strength := range strengths { + options[i] = &resetOption{ + description: fmt.Sprintf("%s reset", strength), + command: fmt.Sprintf("reset --%s %s", strength, commit.Sha), + } + } + + handleMenuPress := func(index int) error { + if err := gui.GitCommand.ResetToCommit(commit.Sha, strengths[index]); err != nil { + return err + } + + if err := gui.refreshCommits(g); err != nil { + return err + } + if err := gui.refreshFiles(); err != nil { + return err + } + if err := gui.resetOrigin(gui.getCommitsView()); err != nil { + return err + } + + gui.State.Panels.Commits.SelectedLine = 0 + return gui.handleCommitSelect(g, gui.getCommitsView()) + } + + return gui.createMenu(fmt.Sprintf("%s %s", gui.Tr.SLocalize("resetTo"), commit.Sha), options, len(options), handleMenuPress) +} diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 6bb32ea59..fd4ed7625 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -327,7 +327,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { ViewName: "commits", Key: 'g', Modifier: gocui.ModNone, - Handler: gui.handleResetToCommit, + Handler: gui.handleCreateCommitResetMenu, Description: gui.Tr.SLocalize("resetToThisCommit"), }, { ViewName: "commits", diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index 489a5d955..97b49e76d 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -733,6 +733,9 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "SkipHookPrefixNotConfigured", Other: "Je hebt nog niet een commit bericht voorvoegsel ingesteld voor het overslaan van hooks. Set `git.skipHookPrefix = 'WIP'` in je config", + }, &i18n.Message{ + ID: "resetTo", + Other: `reset to`, }, ) } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index b6c8215d8..291f114e8 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -756,6 +756,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "SkipHookPrefixNotConfigured", Other: "You have not configured a commit message prefix for skipping hooks. Set `git.skipHookPrefix = 'WIP'` in your config", + }, &i18n.Message{ + ID: "resetTo", + Other: `reset to`, }, ) } diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index 09ad7624f..8229dc5df 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -716,6 +716,9 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "SkipHookPrefixNotConfigured", Other: "You have not configured a commit message prefix for skipping hooks. Set `git.skipHookPrefix = 'WIP'` in your config", + }, &i18n.Message{ + ID: "resetTo", + Other: `reset to`, }, ) } |