2022-02-06 06:54:26 +02:00
package helpers
2022-01-16 05:46:53 +02:00
import (
"fmt"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
2022-02-06 06:54:26 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
2022-01-30 11:03:08 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-01-16 05:46:53 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
2022-01-30 11:03:08 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2022-01-16 05:46:53 +02:00
)
2022-01-31 13:11:34 +02:00
type IRefsHelper interface {
CheckoutRef ( ref string , options types . CheckoutRefOptions ) error
2022-02-06 06:54:26 +02:00
GetCheckedOutRef ( ) * models . Branch
2022-01-31 13:11:34 +02:00
CreateGitResetMenu ( ref string ) error
ResetToRef ( ref string , strength string , envVars [ ] string ) error
NewBranch ( from string , fromDescription string , suggestedBranchname string ) error
}
2022-01-16 05:46:53 +02:00
2022-01-31 13:11:34 +02:00
type RefsHelper struct {
2022-02-06 06:54:26 +02:00
c * types . HelperCommon
2022-02-06 05:37:16 +02:00
git * commands . GitCommand
contexts * context . ContextTree
2022-02-06 06:54:26 +02:00
model * types . Model
2022-01-16 05:46:53 +02:00
}
2022-01-30 01:23:39 +02:00
func NewRefsHelper (
2022-02-06 06:54:26 +02:00
c * types . HelperCommon ,
2022-01-16 05:46:53 +02:00
git * commands . GitCommand ,
2022-01-31 13:20:28 +02:00
contexts * context . ContextTree ,
2022-02-06 06:54:26 +02:00
model * types . Model ,
2022-01-30 01:23:39 +02:00
) * RefsHelper {
return & RefsHelper {
2022-02-06 05:37:16 +02:00
c : c ,
git : git ,
contexts : contexts ,
2022-02-06 06:54:26 +02:00
model : model ,
2022-01-16 05:46:53 +02:00
}
}
2022-01-31 13:11:34 +02:00
var _ IRefsHelper = & RefsHelper { }
2022-01-16 05:46:53 +02:00
2022-01-30 01:23:39 +02:00
func ( self * RefsHelper ) CheckoutRef ( ref string , options types . CheckoutRefOptions ) error {
2022-01-16 05:46:53 +02:00
waitingStatus := options . WaitingStatus
if waitingStatus == "" {
waitingStatus = self . c . Tr . CheckingOutStatus
}
cmdOptions := git_commands . CheckoutOptions { Force : false , EnvVars : options . EnvVars }
onSuccess := func ( ) {
2022-02-06 05:37:16 +02:00
self . contexts . Branches . SetSelectedLineIdx ( 0 )
self . contexts . ReflogCommits . SetSelectedLineIdx ( 0 )
2022-02-13 08:01:53 +02:00
self . contexts . LocalCommits . SetSelectedLineIdx ( 0 )
2022-01-16 05:46:53 +02:00
// loading a heap of commits is slow so we limit them whenever doing a reset
2022-02-13 08:01:53 +02:00
self . contexts . LocalCommits . SetLimitCommits ( true )
2022-01-16 05:46:53 +02:00
}
return self . c . WithWaitingStatus ( waitingStatus , func ( ) error {
if err := self . git . Branch . Checkout ( ref , cmdOptions ) ; err != nil {
// note, this will only work for english-language git commands. If we force git to use english, and the error isn't this one, then the user will receive an english command they may not understand. I'm not sure what the best solution to this is. Running the command once in english and a second time in the native language is one option
if options . OnRefNotFound != nil && strings . Contains ( err . Error ( ) , "did not match any file(s) known to git" ) {
return options . OnRefNotFound ( ref )
}
if strings . Contains ( err . Error ( ) , "Please commit your changes or stash them before you switch branch" ) {
// offer to autostash changes
2022-01-29 10:09:20 +02:00
return self . c . Ask ( types . AskOpts {
2022-01-16 05:46:53 +02:00
Title : self . c . Tr . AutoStashTitle ,
Prompt : self . c . Tr . AutoStashPrompt ,
HandleConfirm : func ( ) error {
if err := self . git . Stash . Save ( self . c . Tr . StashPrefix + ref ) ; err != nil {
return self . c . Error ( err )
}
if err := self . git . Branch . Checkout ( ref , cmdOptions ) ; err != nil {
return self . c . Error ( err )
}
onSuccess ( )
if err := self . git . Stash . Pop ( 0 ) ; err != nil {
if err := self . c . Refresh ( types . RefreshOptions { Mode : types . BLOCK_UI } ) ; err != nil {
return err
}
return self . c . Error ( err )
}
return self . c . Refresh ( types . RefreshOptions { Mode : types . BLOCK_UI } )
} ,
} )
}
if err := self . c . Error ( err ) ; err != nil {
return err
}
}
onSuccess ( )
return self . c . Refresh ( types . RefreshOptions { Mode : types . BLOCK_UI } )
} )
}
2022-02-06 06:54:26 +02:00
func ( self * RefsHelper ) GetCheckedOutRef ( ) * models . Branch {
if len ( self . model . Branches ) == 0 {
return nil
}
return self . model . Branches [ 0 ]
}
2022-01-30 01:23:39 +02:00
func ( self * RefsHelper ) ResetToRef ( ref string , strength string , envVars [ ] string ) error {
2022-01-16 05:46:53 +02:00
if err := self . git . Commit . ResetToCommit ( ref , strength , envVars ) ; err != nil {
return self . c . Error ( err )
}
2022-02-13 08:01:53 +02:00
self . contexts . LocalCommits . SetSelectedLineIdx ( 0 )
2022-02-06 05:37:16 +02:00
self . contexts . ReflogCommits . SetSelectedLineIdx ( 0 )
2022-01-16 05:46:53 +02:00
// loading a heap of commits is slow so we limit them whenever doing a reset
2022-02-13 08:01:53 +02:00
self . contexts . LocalCommits . SetLimitCommits ( true )
2022-01-16 05:46:53 +02:00
2022-02-13 08:01:53 +02:00
if err := self . c . PushContext ( self . contexts . LocalCommits ) ; err != nil {
2022-01-16 05:46:53 +02:00
return err
}
if err := self . c . Refresh ( types . RefreshOptions { Scope : [ ] types . RefreshableView { types . FILES , types . BRANCHES , types . REFLOG , types . COMMITS } } ) ; err != nil {
return err
}
return nil
}
2022-01-30 01:23:39 +02:00
func ( self * RefsHelper ) CreateGitResetMenu ( ref string ) error {
2022-01-16 05:46:53 +02:00
strengths := [ ] string { "soft" , "mixed" , "hard" }
2022-01-29 10:09:20 +02:00
menuItems := make ( [ ] * types . MenuItem , len ( strengths ) )
2022-01-16 05:46:53 +02:00
for i , strength := range strengths {
strength := strength
2022-01-29 10:09:20 +02:00
menuItems [ i ] = & types . MenuItem {
2022-01-16 05:46:53 +02:00
DisplayStrings : [ ] string {
fmt . Sprintf ( "%s reset" , strength ) ,
style . FgRed . Sprintf ( "reset --%s %s" , strength , ref ) ,
} ,
OnPress : func ( ) error {
self . c . LogAction ( "Reset" )
return self . ResetToRef ( ref , strength , [ ] string { } )
} ,
}
}
2022-01-29 10:09:20 +02:00
return self . c . Menu ( types . CreateMenuOptions {
2022-01-16 05:46:53 +02:00
Title : fmt . Sprintf ( "%s %s" , self . c . Tr . LcResetTo , ref ) ,
Items : menuItems ,
} )
}
2022-01-30 11:03:08 +02:00
func ( self * RefsHelper ) NewBranch ( from string , fromFormattedName string , suggestedBranchName string ) error {
message := utils . ResolvePlaceholderString (
self . c . Tr . NewBranchNameBranchOff ,
map [ string ] string {
"branchName" : fromFormattedName ,
} ,
)
return self . c . Prompt ( types . PromptOpts {
Title : message ,
InitialContent : suggestedBranchName ,
HandleConfirm : func ( response string ) error {
self . c . LogAction ( self . c . Tr . Actions . CreateBranch )
if err := self . git . Branch . New ( sanitizedBranchName ( response ) , from ) ; err != nil {
return err
}
2022-01-31 13:20:28 +02:00
if self . c . CurrentContext ( ) != self . contexts . Branches {
if err := self . c . PushContext ( self . contexts . Branches ) ; err != nil {
2022-01-30 11:03:08 +02:00
return err
}
}
2022-02-13 08:01:53 +02:00
self . contexts . LocalCommits . SetSelectedLineIdx ( 0 )
2022-02-06 05:37:16 +02:00
self . contexts . Branches . SetSelectedLineIdx ( 0 )
2022-01-30 11:03:08 +02:00
return self . c . Refresh ( types . RefreshOptions { Mode : types . ASYNC } )
} ,
} )
}
2022-01-31 13:11:34 +02:00
// sanitizedBranchName will remove all spaces in favor of a dash "-" to meet
// git's branch naming requirement.
func sanitizedBranchName ( input string ) string {
return strings . Replace ( input , " " , "-" , - 1 )
}