summaryrefslogtreecommitdiffstats
path: root/branches_panel.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2018-07-21 16:06:38 +1000
committerJesse Duffield <jessedduffield@gmail.com>2018-07-21 16:06:38 +1000
commita47c889cbb385e93e895a257e12038a5827c45b8 (patch)
treeea05ee7849b9f6cfc9e699013b2a948ff3031566 /branches_panel.go
parentbcdfe242ac4050c4be22fc43791f3b98952b2f73 (diff)
parent61dcbb456d42fefc8aaac456e02c40bdef7de774 (diff)
merge with develop
Diffstat (limited to 'branches_panel.go')
-rw-r--r--branches_panel.go142
1 files changed, 73 insertions, 69 deletions
diff --git a/branches_panel.go b/branches_panel.go
index 12f2394a6..8b84ee908 100644
--- a/branches_panel.go
+++ b/branches_panel.go
@@ -1,17 +1,21 @@
package main
import (
- "fmt"
+ "fmt"
- "github.com/jesseduffield/gocui"
+ "github.com/jesseduffield/gocui"
)
func handleBranchPress(g *gocui.Gui, v *gocui.View) error {
- branch := getSelectedBranch(v)
- if output, err := gitCheckout(branch.Name, false); err != nil {
- createErrorPanel(g, output)
- }
- return refreshSidePanels(g)
+ index := getItemPosition(v)
+ if index == 0 {
+ return createErrorPanel(g, "You have already checked out this branch")
+ }
+ branch := getSelectedBranch(v)
+ if output, err := gitCheckout(branch.Name, false); err != nil {
+ createErrorPanel(g, output)
+ }
+ return refreshSidePanels(g)
}
func handleForceCheckout(g *gocui.Gui, v *gocui.View) error {
@@ -25,87 +29,87 @@ func handleForceCheckout(g *gocui.Gui, v *gocui.View) error {
}
func handleCheckoutByName(g *gocui.Gui, v *gocui.View) error {
- createPromptPanel(g, v, "Branch Name:", func(g *gocui.Gui, v *gocui.View) error {
- if output, err := gitCheckout(trimmedContent(v), false); err != nil {
- return createErrorPanel(g, output)
- }
- return refreshSidePanels(g)
- })
- return nil
+ createPromptPanel(g, v, "Branch Name:", func(g *gocui.Gui, v *gocui.View) error {
+ if output, err := gitCheckout(trimmedContent(v), false); err != nil {
+ return createErrorPanel(g, output)
+ }
+ return refreshSidePanels(g)
+ })
+ return nil
}
func handleNewBranch(g *gocui.Gui, v *gocui.View) error {
- branch := state.Branches[0]
- createPromptPanel(g, v, "New Branch Name (Branch is off of "+branch.Name+")", func(g *gocui.Gui, v *gocui.View) error {
- if output, err := gitNewBranch(trimmedContent(v)); err != nil {
- return createErrorPanel(g, output)
- }
- refreshSidePanels(g)
- return handleBranchSelect(g, v)
- })
- return nil
+ branch := state.Branches[0]
+ createPromptPanel(g, v, "New Branch Name (Branch is off of "+branch.Name+")", func(g *gocui.Gui, v *gocui.View) error {
+ if output, err := gitNewBranch(trimmedContent(v)); err != nil {
+ return createErrorPanel(g, output)
+ }
+ refreshSidePanels(g)
+ return handleBranchSelect(g, v)
+ })
+ return nil
}
func handleMerge(g *gocui.Gui, v *gocui.View) error {
- checkedOutBranch := state.Branches[0]
- selectedBranch := getSelectedBranch(v)
- defer refreshSidePanels(g)
- if checkedOutBranch.Name == selectedBranch.Name {
- return createErrorPanel(g, "You cannot merge a branch into itself")
- }
- if output, err := gitMerge(selectedBranch.Name); err != nil {
- return createErrorPanel(g, output)
- }
- return nil
+ checkedOutBranch := state.Branches[0]
+ selectedBranch := getSelectedBranch(v)
+ defer refreshSidePanels(g)
+ if checkedOutBranch.Name == selectedBranch.Name {
+ return createErrorPanel(g, "You cannot merge a branch into itself")
+ }
+ if output, err := gitMerge(selectedBranch.Name); err != nil {
+ return createErrorPanel(g, output)
+ }
+ return nil
}
func getSelectedBranch(v *gocui.View) Branch {
- lineNumber := getItemPosition(v)
- return state.Branches[lineNumber]
+ lineNumber := getItemPosition(v)
+ return state.Branches[lineNumber]
}
func renderBranchesOptions(g *gocui.Gui) error {
- return renderOptionsMap(g, map[string]string{
- "space": "checkout",
- "f": "force checkout",
- "m": "merge",
- "c": "checkout by name",
- "n": "checkout new branch",
- })
+ return renderOptionsMap(g, map[string]string{
+ "space": "checkout",
+ "f": "force checkout",
+ "m": "merge",
+ "c": "checkout by name",
+ "n": "new branch",
+ })
}
// may want to standardise how these select methods work
func handleBranchSelect(g *gocui.Gui, v *gocui.View) error {
- if err := renderBranchesOptions(g); err != nil {
- return err
- }
- // This really shouldn't happen: there should always be a master branch
- if len(state.Branches) == 0 {
- return renderString(g, "main", "No branches for this repo")
- }
- go func() {
- branch := getSelectedBranch(v)
- diff, _ := getBranchDiff(branch.Name, branch.BaseBranch)
- renderString(g, "main", diff)
- }()
- return nil
+ if err := renderBranchesOptions(g); err != nil {
+ return err
+ }
+ // This really shouldn't happen: there should always be a master branch
+ if len(state.Branches) == 0 {
+ return renderString(g, "main", "No branches for this repo")
+ }
+ go func() {
+ branch := getSelectedBranch(v)
+ diff, _ := getBranchDiff(branch.Name, branch.BaseBranch)
+ renderString(g, "main", diff)
+ }()
+ return nil
}
// refreshStatus is called at the end of this because that's when we can
// be sure there is a state.Branches array to pick the current branch from
func refreshBranches(g *gocui.Gui) error {
- g.Update(func(g *gocui.Gui) error {
- v, err := g.View("branches")
- if err != nil {
- panic(err)
- }
- state.Branches = getGitBranches()
- v.Clear()
- for _, branch := range state.Branches {
- fmt.Fprintln(v, branch.DisplayString)
- }
- resetOrigin(v)
- return refreshStatus(g)
- })
- return nil
+ g.Update(func(g *gocui.Gui) error {
+ v, err := g.View("branches")
+ if err != nil {
+ panic(err)
+ }
+ state.Branches = getGitBranches()
+ v.Clear()
+ for _, branch := range state.Branches {
+ fmt.Fprintln(v, branch.DisplayString)
+ }
+ resetOrigin(v)
+ return refreshStatus(g)
+ })
+ return nil
}