From db826b3c8795e47e3471ab76bc4bcb131892c224 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 16 Aug 2020 22:01:14 +1000 Subject: [PATCH] add keybinding to create new branch off of commit retain focus in commits panel surface prompt errors better description --- docs/keybindings/Keybindings_en.md | 1 + docs/keybindings/Keybindings_nl.md | 1 + docs/keybindings/Keybindings_pl.md | 1 + pkg/commands/commit.go | 6 ++++++ pkg/commands/git.go | 4 ++-- pkg/gui/commits_panel.go | 24 ++++++++++++++++++++++++ pkg/gui/confirmation_panel.go | 2 +- pkg/gui/keybindings.go | 8 ++++++++ pkg/i18n/english.go | 3 +++ 9 files changed, 47 insertions(+), 3 deletions(-) diff --git a/docs/keybindings/Keybindings_en.md b/docs/keybindings/Keybindings_en.md index 26fbd2b1f..e28142755 100644 --- a/docs/keybindings/Keybindings_en.md +++ b/docs/keybindings/Keybindings_en.md @@ -144,6 +144,7 @@ v: paste commits (cherry-pick) enter: view commit's files space: checkout commit + n: create new branch off of commit T: tag commit ctrl+r: reset cherry-picked (copied) commits selection ,: previous page diff --git a/docs/keybindings/Keybindings_nl.md b/docs/keybindings/Keybindings_nl.md index 227d8e028..567bf4d25 100644 --- a/docs/keybindings/Keybindings_nl.md +++ b/docs/keybindings/Keybindings_nl.md @@ -144,6 +144,7 @@ v: plak commits (cherry-pick) enter: bekijk gecommite bestanden space: checkout commit + n: create new branch off of commit T: tag commit ctrl+r: reset cherry-picked (gecopieerde) commits selectie ,: vorige pagina diff --git a/docs/keybindings/Keybindings_pl.md b/docs/keybindings/Keybindings_pl.md index 0026a1c51..bc288f107 100644 --- a/docs/keybindings/Keybindings_pl.md +++ b/docs/keybindings/Keybindings_pl.md @@ -144,6 +144,7 @@ v: paste commits (cherry-pick) enter: view commit's files space: checkout commit + n: create new branch off of commit T: tag commit ctrl+r: reset cherry-picked (copied) commits selection ,: previous page diff --git a/pkg/commands/commit.go b/pkg/commands/commit.go index ea4192b33..f8f7491b4 100644 --- a/pkg/commands/commit.go +++ b/pkg/commands/commit.go @@ -1,5 +1,7 @@ package commands +import "fmt" + // Commit : A git commit type Commit struct { Sha string @@ -18,3 +20,7 @@ func (c *Commit) ShortSha() string { } return c.Sha[:8] } + +func (c *Commit) NameWithSha() string { + return fmt.Sprintf("%s %s", c.Sha[:7], c.Name) +} diff --git a/pkg/commands/git.go b/pkg/commands/git.go index f4fb4a424..b52c989d7 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -401,8 +401,8 @@ func (c *GitCommand) ResetToCommit(sha string, strength string, options RunComma } // NewBranch create new branch -func (c *GitCommand) NewBranch(name string, baseBranch string) error { - return c.OSCommand.RunCommand("git checkout -b %s %s", name, baseBranch) +func (c *GitCommand) NewBranch(name string, base string) error { + return c.OSCommand.RunCommand("git checkout -b %s %s", name, base) } // CurrentBranchName get the current branch name and displayname. diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 728517b9b..d1f86d287 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -771,3 +771,27 @@ func (gui *Gui) handleClipboardCopyCommit(g *gocui.Gui, v *gocui.View) error { return gui.OSCommand.CopyToClipboard(commit.Sha) } + +func (gui *Gui) handleNewBranchOffCommit() error { + commit := gui.getSelectedCommit() + if commit == nil { + return nil + } + + message := gui.Tr.TemplateLocalize( + "NewBranchNameBranchOff", + Teml{ + "branchName": commit.NameWithSha(), + }, + ) + + return gui.prompt(gui.getCommitsView(), message, "", func(response string) error { + if err := gui.GitCommand.NewBranch(response, commit.Sha); err != nil { + return err + } + gui.State.Panels.Commits.SelectedLine = 0 + gui.State.Panels.Branches.SelectedLine = 0 + + return gui.refreshSidePanels(refreshOptions{mode: ASYNC}) + }) +} diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index e1aa29531..71fe20452 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -85,7 +85,7 @@ func (gui *Gui) wrappedPromptConfirmationFunction(function func(string) error, r if function != nil { if err := function(v.Buffer()); err != nil { - return err + return gui.surfaceError(err) } } diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 6e4afceec..e149dc66d 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -775,6 +775,14 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Handler: gui.handleCheckoutCommit, Description: gui.Tr.SLocalize("checkoutCommit"), }, + { + ViewName: "commits", + Contexts: []string{"branch-commits"}, + Key: gui.getKey("universal.new"), + Modifier: gocui.ModNone, + Handler: gui.wrappedHandler(gui.handleNewBranchOffCommit), + Description: gui.Tr.SLocalize("createNewBranchFromCommit"), + }, { ViewName: "commits", Contexts: []string{"branch-commits"}, diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 0815eeb73..71d0923c8 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -1167,6 +1167,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "UnstageLinesPrompt", Other: "Are you sure you want to delete the selected lines (git reset)? It is irreversible.\nTo disable this dialogue set the config key of 'gui.skipUnstageLineWarning' to true", + }, &i18n.Message{ + ID: "createNewBranchFromCommit", + Other: "create new branch off of commit", }, ) }