2018-05-27 08:32:09 +02:00
package main
import (
2018-06-01 15:23:31 +02:00
"errors"
2018-05-27 08:32:09 +02:00
"github.com/fatih/color"
2018-06-05 10:47:31 +02:00
"github.com/jesseduffield/gocui"
2018-05-27 08:32:09 +02:00
)
2018-06-01 15:23:31 +02:00
var (
// ErrNoCommits : When no commits are found for the branch
ErrNoCommits = errors . New ( "No commits for this branch" )
)
2018-05-27 08:32:09 +02:00
func refreshCommits ( g * gocui . Gui ) error {
g . Update ( func ( * gocui . Gui ) error {
2018-06-02 05:51:03 +02:00
state . Commits = getCommits ( )
2018-05-27 08:32:09 +02:00
v , err := g . View ( "commits" )
if err != nil {
panic ( err )
}
v . Clear ( )
2018-06-01 15:23:31 +02:00
red := color . New ( color . FgRed )
2018-05-27 08:32:09 +02:00
yellow := color . New ( color . FgYellow )
white := color . New ( color . FgWhite )
2018-06-01 15:23:31 +02:00
shaColor := white
2018-05-27 08:32:09 +02:00
for _ , commit := range state . Commits {
2018-06-01 15:23:31 +02:00
if commit . Pushed {
shaColor = red
} else {
shaColor = yellow
}
shaColor . Fprint ( v , commit . Sha + " " )
2018-05-27 08:32:09 +02:00
white . Fprintln ( v , commit . Name )
}
2018-06-02 05:51:03 +02:00
refreshStatus ( g )
2018-05-27 08:32:09 +02:00
return nil
} )
return nil
}
2018-06-02 05:51:03 +02:00
func handleResetToCommit ( g * gocui . Gui , commitView * gocui . View ) error {
return createConfirmationPanel ( g , commitView , "Reset To Commit" , "Are you sure you want to reset to this commit? (y/n)" , func ( g * gocui . Gui , v * gocui . View ) error {
commit , err := getSelectedCommit ( g )
devLog ( commit )
if err != nil {
panic ( err )
}
if output , err := gitResetToCommit ( commit . Sha ) ; err != nil {
2018-06-05 10:49:10 +02:00
return createErrorPanel ( g , output )
2018-06-02 05:51:03 +02:00
}
if err := refreshCommits ( g ) ; err != nil {
panic ( err )
}
if err := refreshFiles ( g ) ; err != nil {
panic ( err )
}
resetOrigin ( commitView )
return handleCommitSelect ( g , nil )
} , nil )
}
2018-05-27 08:32:09 +02:00
func handleCommitSelect ( g * gocui . Gui , v * gocui . View ) error {
2018-06-02 05:51:03 +02:00
renderString ( g , "options" , "s: squash down, r: rename, g: reset to this commit" )
commit , err := getSelectedCommit ( g )
2018-06-01 15:23:31 +02:00
if err != nil {
if err != ErrNoCommits {
return err
}
return renderString ( g , "main" , "No commits for this branch" )
}
2018-05-27 08:32:09 +02:00
commitText := gitShow ( commit . Sha )
return renderString ( g , "main" , commitText )
}
func handleCommitSquashDown ( g * gocui . Gui , v * gocui . View ) error {
if getItemPosition ( v ) != 0 {
2018-06-05 10:49:10 +02:00
return createErrorPanel ( g , "Can only squash topmost commit" )
2018-05-27 08:32:09 +02:00
}
2018-06-02 05:51:03 +02:00
if len ( state . Commits ) == 1 {
2018-06-05 10:49:10 +02:00
return createErrorPanel ( g , "You have no commits to squash with" )
2018-06-02 05:51:03 +02:00
}
commit , err := getSelectedCommit ( g )
2018-06-01 15:23:31 +02:00
if err != nil {
return err
}
2018-05-27 08:32:09 +02:00
if output , err := gitSquashPreviousTwoCommits ( commit . Name ) ; err != nil {
2018-06-05 10:49:10 +02:00
return createErrorPanel ( g , output )
2018-05-27 08:32:09 +02:00
}
if err := refreshCommits ( g ) ; err != nil {
panic ( err )
}
2018-06-02 05:51:03 +02:00
refreshStatus ( g )
2018-05-27 08:32:09 +02:00
return handleCommitSelect ( g , v )
}
func handleRenameCommit ( g * gocui . Gui , v * gocui . View ) error {
if getItemPosition ( v ) != 0 {
2018-06-05 10:49:10 +02:00
return createErrorPanel ( g , "Can only rename topmost commit" )
2018-05-27 08:32:09 +02:00
}
createPromptPanel ( g , v , "Rename Commit" , func ( g * gocui . Gui , v * gocui . View ) error {
if output , err := gitRenameCommit ( v . Buffer ( ) ) ; err != nil {
2018-06-05 10:49:10 +02:00
return createErrorPanel ( g , output )
2018-05-27 08:32:09 +02:00
}
if err := refreshCommits ( g ) ; err != nil {
panic ( err )
}
return handleCommitSelect ( g , v )
} )
return nil
}
2018-06-02 05:51:03 +02:00
func getSelectedCommit ( g * gocui . Gui ) ( Commit , error ) {
v , err := g . View ( "commits" )
if err != nil {
panic ( err )
}
2018-05-27 08:32:09 +02:00
if len ( state . Commits ) == 0 {
2018-06-01 15:23:31 +02:00
return Commit { } , ErrNoCommits
2018-05-27 08:32:09 +02:00
}
2018-06-01 15:23:31 +02:00
lineNumber := getItemPosition ( v )
2018-06-02 05:51:03 +02:00
if lineNumber > len ( state . Commits ) - 1 {
colorLog ( color . FgRed , "potential error in getSelected Commit (mismatched ui and state)" , state . Commits , lineNumber )
return state . Commits [ len ( state . Commits ) - 1 ] , nil
}
2018-06-01 15:23:31 +02:00
return state . Commits [ lineNumber ] , nil
2018-05-27 08:32:09 +02:00
}