2018-05-26 07:44:44 +02:00
package main
import (
2018-07-21 07:51:18 +02:00
"fmt"
2018-06-01 15:23:31 +02:00
2018-07-21 07:51:18 +02:00
"github.com/jesseduffield/gocui"
2018-05-26 07:44:44 +02:00
)
func handleBranchPress ( g * gocui . Gui , v * gocui . View ) error {
2018-07-21 07:51:18 +02:00
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 )
2018-05-27 08:32:09 +02:00
}
func handleForceCheckout ( g * gocui . Gui , v * gocui . View ) error {
2018-07-21 07:51:18 +02:00
branch := getSelectedBranch ( v )
return createConfirmationPanel ( g , v , "Force Checkout Branch" , "Are you sure you want force checkout? You will lose all local changes (y/n)" , func ( g * gocui . Gui , v * gocui . View ) error {
if output , err := gitCheckout ( branch . Name , true ) ; err != nil {
createErrorPanel ( g , output )
}
return refreshSidePanels ( g )
} , nil )
2018-05-26 07:44:44 +02:00
}
2018-06-09 11:30:59 +02:00
func handleCheckoutByName ( g * gocui . Gui , v * gocui . View ) error {
2018-07-21 07:51:18 +02:00
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
2018-06-09 11:30:59 +02:00
}
2018-06-02 05:51:03 +02:00
func handleNewBranch ( g * gocui . Gui , v * gocui . View ) error {
2018-07-21 07:51:18 +02:00
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
2018-06-02 05:51:03 +02:00
}
2018-06-09 11:06:33 +02:00
func handleMerge ( g * gocui . Gui , v * gocui . View ) error {
2018-07-21 07:51:18 +02:00
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
2018-06-09 11:06:33 +02:00
}
2018-05-26 07:44:44 +02:00
func getSelectedBranch ( v * gocui . View ) Branch {
2018-07-21 07:51:18 +02:00
lineNumber := getItemPosition ( v )
return state . Branches [ lineNumber ]
2018-05-26 07:44:44 +02:00
}
2018-06-09 11:06:33 +02:00
func renderBranchesOptions ( g * gocui . Gui ) error {
2018-07-21 07:51:18 +02:00
return renderOptionsMap ( g , map [ string ] string {
"space" : "checkout" ,
"f" : "force checkout" ,
"m" : "merge" ,
"c" : "checkout by name" ,
"n" : "new branch" ,
} )
2018-06-09 11:06:33 +02:00
}
2018-06-02 00:35:49 +02:00
// may want to standardise how these select methods work
2018-05-26 07:44:44 +02:00
func handleBranchSelect ( g * gocui . Gui , v * gocui . View ) error {
2018-07-21 07:51:18 +02:00
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
2018-05-26 07:44:44 +02:00
}
2018-06-01 15:23:31 +02:00
// 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
2018-05-27 08:32:09 +02:00
func refreshBranches ( g * gocui . Gui ) error {
2018-07-21 07:51:18 +02:00
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
2018-05-26 07:44:44 +02:00
}