2018-08-14 11:05:26 +02:00
package gui
import (
"fmt"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands"
2020-09-29 12:28:39 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
2021-04-10 09:31:23 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2020-11-28 11:01:45 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
2020-10-04 02:00:48 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-14 11:05:26 +02:00
)
2018-12-06 13:18:17 +02:00
// list panel functions
2020-09-29 10:34:01 +02:00
func ( gui * Gui ) getSelectedBranch ( ) * models . Branch {
2020-08-18 01:03:52 +02:00
if len ( gui . State . Branches ) == 0 {
return nil
}
2020-08-20 00:53:10 +02:00
selectedLine := gui . State . Panels . Branches . SelectedLineIdx
2018-12-06 13:18:17 +02:00
if selectedLine == - 1 {
return nil
}
return gui . State . Branches [ selectedLine ]
}
2020-08-15 08:48:35 +02:00
func ( gui * Gui ) handleBranchSelect ( ) error {
2020-08-18 14:02:35 +02:00
var task updateTask
2018-12-06 13:18:17 +02:00
branch := gui . getSelectedBranch ( )
2020-08-18 01:03:52 +02:00
if branch == nil {
2021-04-04 15:51:59 +02:00
task = NewRenderStringTask ( gui . Tr . NoBranchesThisRepo )
2020-08-18 01:03:52 +02:00
} else {
cmd := gui . OSCommand . ExecutableFromString (
gui . GitCommand . GetBranchGraphCmdStr ( branch . Name ) ,
)
allow fast flicking through any list panel
Up till now our approach to rendering things like file diffs, branch logs, and
commit patches, has been to run a command on the command line, wait for it to
complete, take its output as a string, and then write that string to the main
view (or secondary view e.g. when showing both staged and unstaged changes of a
file).
This has caused various issues. For once, if you are flicking through a list of
files and an untracked file is particularly large, not only will this require
lazygit to load that whole file into memory (or more accurately it's equally
large diff), it also will slow down the UI thread while loading that file, and
if the user continued down the list, the original command might eventually
resolve and replace whatever the diff is for the newly selected file.
Following what we've done in lazydocker, I've added a tasks package for when you
need something done but you want it to cancel as soon as something newer comes
up. Given this typically involves running a command to display to a view, I've
added a viewBufferManagerMap struct to the Gui struct which allows you to define
these tasks on a per-view basis.
viewBufferManagers can run files and directly write the output to their view,
meaning we no longer need to use so much memory.
In the tasks package there is a helper method called NewCmdTask which takes a
command, an initial amount of lines to read, and then runs that command, reads
that number of lines, and allows for a readLines channel to tell it to read more
lines. We read more lines when we scroll or resize the window.
There is an adapter for the tasks package in a file called tasks_adapter which
wraps the functions from the tasks package in gui-specific stuff like clearing
the main view before starting the next task that wants to write to the main
view.
I've removed some small features as part of this work, namely the little headers
that were at the top of the main view for some situations. For example, we no
longer show the upstream of a selected branch. I want to re-introduce this in
the future, but I didn't want to make this tasks system too complicated, and in
order to facilitate a header section in the main view we'd need to have a task
that gets the upstream for the current branch, writes it to the header, then
tells another task to write the branch log to the main view, but without
clearing inbetween. So it would get messy. I'm thinking instead of having a
separate 'header' view atop the main view to render that kind of thing (which
can happen in another PR)
I've also simplified the 'git show' to just call 'git show' and not do anything
fancy when it comes to merge commits.
I considered using this tasks approach whenever we write to a view. The only
thing is that the renderString method currently resets the origin of a view and
I don't want to lose that. So I've left some in there that I consider harmless,
but we should probably be just using tasks now for all rendering, even if it's
just strings we can instantly make.
2020-01-11 05:54:59 +02:00
2021-04-04 15:51:59 +02:00
task = NewRunPtyTask ( cmd )
2020-03-29 05:34:17 +02:00
}
2020-08-23 01:46:28 +02:00
return gui . refreshMainViews ( refreshMainOpts {
2020-08-18 14:02:35 +02:00
main : & viewUpdateOpts {
title : "Log" ,
task : task ,
} ,
} )
2018-12-06 13:18:17 +02:00
}
// gui.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
2020-03-26 14:20:12 +02:00
func ( gui * Gui ) refreshBranches ( ) {
2020-03-29 02:06:46 +02:00
reflogCommits := gui . State . FilteredReflogCommits
2020-08-22 03:44:03 +02:00
if gui . State . Modes . Filtering . Active ( ) {
2020-03-29 01:52:01 +02:00
// in filter mode we filter our reflog commits to just those containing the path
// however we need all the reflog entries to populate the recencies of our branches
// which allows us to order them correctly. So if we're filtering we'll just
// manually load all the reflog commits here
var err error
reflogCommits , _ , err = gui . GitCommand . GetReflogCommits ( nil , "" )
if err != nil {
gui . Log . Error ( err )
}
}
builder , err := commands . NewBranchListBuilder ( gui . Log , gui . GitCommand , reflogCommits )
2020-03-26 14:20:12 +02:00
if err != nil {
2020-03-28 02:47:54 +02:00
_ = gui . surfaceError ( err )
2019-11-18 00:38:36 +02:00
}
2020-03-26 14:20:12 +02:00
gui . State . Branches = builder . Build ( )
2019-11-18 00:38:36 +02:00
2021-04-03 06:56:11 +02:00
if err := gui . postRefreshUpdate ( gui . State . Contexts . Branches ) ; err != nil {
2020-08-19 10:41:57 +02:00
gui . Log . Error ( err )
2020-03-26 14:20:12 +02:00
}
2020-03-28 02:22:11 +02:00
gui . refreshStatus ( )
2018-12-06 13:18:17 +02:00
}
// specific functions
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleBranchPress ( ) error {
2020-08-20 00:53:10 +02:00
if gui . State . Panels . Branches . SelectedLineIdx == - 1 {
2018-12-06 13:18:17 +02:00
return nil
}
2020-08-20 00:53:10 +02:00
if gui . State . Panels . Branches . SelectedLineIdx == 0 {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . AlreadyCheckedOutBranch )
2018-08-14 11:05:26 +02:00
}
2018-12-06 13:18:17 +02:00
branch := gui . getSelectedBranch ( )
2021-04-11 11:35:42 +02:00
return gui . handleCheckoutRef ( branch . Name , handleCheckoutRefOptions { span : gui . Tr . Spans . CheckoutBranch } )
2018-08-14 11:05:26 +02:00
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleCreatePullRequestPress ( ) error {
2018-10-15 11:00:19 +02:00
pullRequest := commands . NewPullRequest ( gui . GitCommand )
2018-10-12 14:06:03 +02:00
2018-12-06 13:18:17 +02:00
branch := gui . getSelectedBranch ( )
2021-04-10 09:31:23 +02:00
url , err := pullRequest . Create ( branch )
if err != nil {
2020-03-28 02:47:54 +02:00
return gui . surfaceError ( err )
2018-10-12 14:06:03 +02:00
}
2021-04-10 09:31:23 +02:00
gui . OnRunCommand ( oscommands . NewCmdLogEntry ( fmt . Sprintf ( "Creating pull request at URL: %s" , url ) , "Create pull request" , false ) )
2018-10-12 14:06:03 +02:00
return nil
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleCopyPullRequestURLPress ( ) error {
2020-11-10 21:57:50 +02:00
pullRequest := commands . NewPullRequest ( gui . GitCommand )
branch := gui . getSelectedBranch ( )
2021-04-10 09:31:23 +02:00
url , err := pullRequest . CopyURL ( branch )
if err != nil {
2020-11-10 21:57:50 +02:00
return gui . surfaceError ( err )
}
2021-04-10 09:31:23 +02:00
gui . OnRunCommand ( oscommands . NewCmdLogEntry ( fmt . Sprintf ( "Copying to clipboard: '%s'" , url ) , "Copy URL" , false ) )
2020-11-10 21:57:50 +02:00
2020-11-21 08:15:43 +02:00
gui . raiseToast ( gui . Tr . PullRequestURLCopiedToClipboard )
2020-11-18 00:08:32 +02:00
return nil
2020-11-10 21:57:50 +02:00
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleGitFetch ( ) error {
2020-11-16 11:38:26 +02:00
if err := gui . createLoaderPanel ( gui . Tr . FetchWait ) ; err != nil {
2018-12-07 15:56:29 +02:00
return err
}
2020-10-07 12:19:38 +02:00
go utils . Safe ( func ( ) {
2021-04-10 09:31:23 +02:00
err := gui . fetch ( true , "Fetch" )
2020-08-11 13:41:32 +02:00
gui . handleCredentialsPopup ( err )
2020-08-11 13:29:18 +02:00
_ = gui . refreshSidePanels ( refreshOptions { mode : ASYNC } )
2020-10-07 12:19:38 +02:00
} )
2018-12-07 15:56:29 +02:00
return nil
2018-12-02 15:58:18 +02:00
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleForceCheckout ( ) error {
2018-12-06 13:18:17 +02:00
branch := gui . getSelectedBranch ( )
2020-10-04 02:00:48 +02:00
message := gui . Tr . SureForceCheckout
title := gui . Tr . ForceCheckoutBranch
2020-08-15 08:36:39 +02:00
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-08-23 04:20:49 +02:00
title : title ,
prompt : message ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2021-04-11 11:35:42 +02:00
if err := gui . GitCommand . WithSpan ( gui . Tr . Spans . ForceCheckoutBranch ) . Checkout ( branch . Name , commands . CheckoutOptions { Force : true } ) ; err != nil {
2020-08-15 08:36:39 +02:00
_ = gui . surfaceError ( err )
}
return gui . refreshSidePanels ( refreshOptions { mode : ASYNC } )
} ,
} )
2018-08-14 11:05:26 +02:00
}
2020-03-21 03:40:56 +02:00
type handleCheckoutRefOptions struct {
2020-03-21 09:27:20 +02:00
WaitingStatus string
EnvVars [ ] string
2020-07-17 01:02:25 +02:00
onRefNotFound func ( ref string ) error
2021-04-10 08:25:45 +02:00
span string
2020-03-21 03:40:56 +02:00
}
func ( gui * Gui ) handleCheckoutRef ( ref string , options handleCheckoutRefOptions ) error {
2020-03-21 09:27:20 +02:00
waitingStatus := options . WaitingStatus
2020-03-21 03:40:56 +02:00
if waitingStatus == "" {
2020-10-04 02:00:48 +02:00
waitingStatus = gui . Tr . CheckingOutStatus
2020-03-21 03:40:56 +02:00
}
2020-03-21 09:27:20 +02:00
cmdOptions := commands . CheckoutOptions { Force : false , EnvVars : options . EnvVars }
2020-03-26 12:12:12 +02:00
onSuccess := func ( ) {
2020-08-20 00:53:10 +02:00
gui . State . Panels . Branches . SelectedLineIdx = 0
gui . State . Panels . Commits . SelectedLineIdx = 0
2020-03-26 12:12:12 +02:00
// loading a heap of commits is slow so we limit them whenever doing a reset
gui . State . Panels . Commits . LimitCommits = true
}
2021-04-10 08:25:45 +02:00
gitCommand := gui . GitCommand . WithSpan ( options . span )
2020-03-21 03:49:12 +02:00
return gui . WithWaitingStatus ( waitingStatus , func ( ) error {
2021-04-10 08:25:45 +02:00
if err := gitCommand . Checkout ( ref , cmdOptions ) ; err != nil {
2020-03-21 03:49:12 +02:00
// 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
2020-07-17 01:02:25 +02:00
if options . onRefNotFound != nil && strings . Contains ( err . Error ( ) , "did not match any file(s) known to git" ) {
return options . onRefNotFound ( ref )
}
2020-03-21 03:49:12 +02:00
if strings . Contains ( err . Error ( ) , "Please commit your changes or stash them before you switch branch" ) {
// offer to autostash changes
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-08-23 04:20:49 +02:00
2020-10-04 02:00:48 +02:00
title : gui . Tr . AutoStashTitle ,
prompt : gui . Tr . AutoStashPrompt ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2021-04-10 08:25:45 +02:00
if err := gitCommand . StashSave ( gui . Tr . StashPrefix + ref ) ; err != nil {
2020-08-15 08:36:39 +02:00
return gui . surfaceError ( err )
2020-03-21 03:49:12 +02:00
}
2021-04-10 08:25:45 +02:00
if err := gitCommand . Checkout ( ref , cmdOptions ) ; err != nil {
2020-08-15 08:36:39 +02:00
return gui . surfaceError ( err )
}
onSuccess ( )
2021-04-10 08:25:45 +02:00
if err := gitCommand . StashDo ( 0 , "pop" ) ; err != nil {
2020-08-15 08:36:39 +02:00
if err := gui . refreshSidePanels ( refreshOptions { mode : BLOCK_UI } ) ; err != nil {
return err
}
return gui . surfaceError ( err )
}
return gui . refreshSidePanels ( refreshOptions { mode : BLOCK_UI } )
} ,
} )
2020-03-21 03:49:12 +02:00
}
2020-03-28 02:47:54 +02:00
if err := gui . surfaceError ( err ) ; err != nil {
2020-03-21 03:49:12 +02:00
return err
}
2019-07-14 06:31:19 +02:00
}
2020-03-26 12:12:12 +02:00
onSuccess ( )
2020-03-27 10:12:15 +02:00
return gui . refreshSidePanels ( refreshOptions { mode : BLOCK_UI } )
2020-03-21 03:49:12 +02:00
} )
2019-03-16 03:45:38 +02:00
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleCheckoutByName ( ) error {
2020-11-28 04:35:58 +02:00
return gui . prompt ( promptOpts {
2020-11-28 11:01:45 +02:00
title : gui . Tr . BranchName + ":" ,
findSuggestionsFunc : gui . findBranchNameSuggestions ,
2020-11-28 04:35:58 +02:00
handleConfirm : func ( response string ) error {
return gui . handleCheckoutRef ( response , handleCheckoutRefOptions {
2021-04-10 09:31:23 +02:00
span : "Checkout branch" ,
2020-11-28 04:35:58 +02:00
onRefNotFound : func ( ref string ) error {
return gui . ask ( askOpts {
title : gui . Tr . BranchNotFoundTitle ,
prompt : fmt . Sprintf ( "%s %s%s" , gui . Tr . BranchNotFoundPrompt , ref , "?" ) ,
handleConfirm : func ( ) error {
return gui . createNewBranchWithName ( ref )
} ,
} )
} ,
} )
} } ,
)
2018-08-14 11:05:26 +02:00
}
2020-09-29 10:34:01 +02:00
func ( gui * Gui ) getCheckedOutBranch ( ) * models . Branch {
2019-11-17 05:50:12 +02:00
if len ( gui . State . Branches ) == 0 {
return nil
}
return gui . State . Branches [ 0 ]
}
2020-07-17 01:02:25 +02:00
func ( gui * Gui ) createNewBranchWithName ( newBranchName string ) error {
branch := gui . getSelectedBranch ( )
if branch == nil {
return nil
}
if err := gui . GitCommand . NewBranch ( newBranchName , branch . Name ) ; err != nil {
return gui . surfaceError ( err )
}
2020-08-23 05:44:11 +02:00
2020-08-20 00:53:10 +02:00
gui . State . Panels . Branches . SelectedLineIdx = 0
2020-07-17 01:02:25 +02:00
return gui . refreshSidePanels ( refreshOptions { mode : ASYNC } )
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleDeleteBranch ( ) error {
2020-08-15 09:23:16 +02:00
return gui . deleteBranch ( false )
2018-08-15 15:02:01 +02:00
}
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) deleteBranch ( force bool ) error {
2018-12-06 13:18:17 +02:00
selectedBranch := gui . getSelectedBranch ( )
if selectedBranch == nil {
return nil
}
2019-11-17 05:50:12 +02:00
checkedOutBranch := gui . getCheckedOutBranch ( )
2018-08-14 11:05:26 +02:00
if checkedOutBranch . Name == selectedBranch . Name {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . CantDeleteCheckOutBranch )
2018-08-14 11:05:26 +02:00
}
2020-08-15 09:23:16 +02:00
return gui . deleteNamedBranch ( selectedBranch , force )
2018-10-06 13:34:33 +02:00
}
2020-09-29 10:34:01 +02:00
func ( gui * Gui ) deleteNamedBranch ( selectedBranch * models . Branch , force bool ) error {
2020-10-04 02:00:48 +02:00
title := gui . Tr . DeleteBranch
var templateStr string
2018-08-21 12:06:42 +02:00
if force {
2020-10-04 02:00:48 +02:00
templateStr = gui . Tr . ForceDeleteBranchMessage
2018-08-21 12:06:42 +02:00
} else {
2020-10-04 02:00:48 +02:00
templateStr = gui . Tr . DeleteBranchMessage
2018-08-21 12:06:42 +02:00
}
2020-10-04 02:00:48 +02:00
message := utils . ResolvePlaceholderString (
templateStr ,
map [ string ] string {
2018-08-14 22:29:17 +02:00
"selectedBranchName" : selectedBranch . Name ,
} ,
)
2020-08-15 08:36:39 +02:00
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-08-23 04:20:49 +02:00
title : title ,
prompt : message ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2021-04-11 11:35:42 +02:00
if err := gui . GitCommand . WithSpan ( gui . Tr . Spans . DeleteBranch ) . DeleteBranch ( selectedBranch . Name , force ) ; err != nil {
2020-08-15 08:36:39 +02:00
errMessage := err . Error ( )
if ! force && strings . Contains ( errMessage , "is not fully merged" ) {
2020-08-15 09:23:16 +02:00
return gui . deleteNamedBranch ( selectedBranch , true )
2020-08-15 08:36:39 +02:00
}
return gui . createErrorPanel ( errMessage )
2018-10-06 13:34:33 +02:00
}
2021-03-31 14:55:06 +02:00
return gui . refreshSidePanels ( refreshOptions { mode : ASYNC , scope : [ ] RefreshableView { BRANCHES } } )
2020-08-15 08:36:39 +02:00
} ,
} )
2018-08-14 11:05:26 +02:00
}
2019-11-17 04:21:38 +02:00
func ( gui * Gui ) mergeBranchIntoCheckedOutBranch ( branchName string ) error {
2020-03-29 01:11:15 +02:00
if ok , err := gui . validateNotInFilterMode ( ) ; err != nil || ! ok {
2020-03-28 07:28:35 +02:00
return err
}
2019-11-17 04:21:38 +02:00
if gui . GitCommand . IsHeadDetached ( ) {
2020-03-28 02:47:54 +02:00
return gui . createErrorPanel ( "Cannot merge branch in detached head state. You might have checked out a commit directly or a remote branch, in which case you should checkout the local branch you want to be on" )
2019-11-17 04:21:38 +02:00
}
2019-11-17 05:50:12 +02:00
checkedOutBranchName := gui . getCheckedOutBranch ( ) . Name
2019-11-17 04:21:38 +02:00
if checkedOutBranchName == branchName {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . CantMergeBranchIntoItself )
2018-08-14 11:05:26 +02:00
}
2020-10-04 02:00:48 +02:00
prompt := utils . ResolvePlaceholderString (
gui . Tr . ConfirmMerge ,
map [ string ] string {
2019-11-17 04:21:38 +02:00
"checkedOutBranch" : checkedOutBranchName ,
"selectedBranch" : branchName ,
2019-02-16 12:01:17 +02:00
} ,
)
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-10-04 02:00:48 +02:00
title : gui . Tr . MergingTitle ,
2020-08-23 04:20:49 +02:00
prompt : prompt ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2021-04-11 11:35:42 +02:00
err := gui . GitCommand . WithSpan ( gui . Tr . Spans . Merge ) . Merge ( branchName , commands . MergeOpts { } )
2019-02-16 12:01:17 +02:00
return gui . handleGenericMergeCommandResult ( err )
2020-08-15 08:36:39 +02:00
} ,
} )
2019-02-16 12:01:17 +02:00
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleMerge ( ) error {
2020-03-29 01:11:15 +02:00
if ok , err := gui . validateNotInFilterMode ( ) ; err != nil || ! ok {
2020-03-28 07:28:35 +02:00
return err
}
2019-11-17 04:21:38 +02:00
selectedBranchName := gui . getSelectedBranch ( ) . Name
return gui . mergeBranchIntoCheckedOutBranch ( selectedBranchName )
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleRebaseOntoLocalBranch ( ) error {
2019-11-17 05:04:57 +02:00
selectedBranchName := gui . getSelectedBranch ( ) . Name
return gui . handleRebaseOntoBranch ( selectedBranchName )
}
func ( gui * Gui ) handleRebaseOntoBranch ( selectedBranchName string ) error {
2020-03-29 01:11:15 +02:00
if ok , err := gui . validateNotInFilterMode ( ) ; err != nil || ! ok {
2020-03-28 07:28:35 +02:00
return err
}
2019-11-17 05:50:12 +02:00
checkedOutBranch := gui . getCheckedOutBranch ( ) . Name
2019-11-17 05:04:57 +02:00
if selectedBranchName == checkedOutBranch {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . CantRebaseOntoSelf )
2018-08-14 11:05:26 +02:00
}
2020-10-04 02:00:48 +02:00
prompt := utils . ResolvePlaceholderString (
gui . Tr . ConfirmRebase ,
map [ string ] string {
2019-02-16 12:01:17 +02:00
"checkedOutBranch" : checkedOutBranch ,
2019-11-17 05:04:57 +02:00
"selectedBranch" : selectedBranchName ,
2019-02-16 12:01:17 +02:00
} ,
)
2020-08-15 08:36:39 +02:00
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-10-04 02:00:48 +02:00
title : gui . Tr . RebasingTitle ,
2020-08-23 04:20:49 +02:00
prompt : prompt ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2021-04-11 11:35:42 +02:00
err := gui . GitCommand . WithSpan ( gui . Tr . Spans . RebaseBranch ) . RebaseBranch ( selectedBranchName )
2019-02-16 12:01:17 +02:00
return gui . handleGenericMergeCommandResult ( err )
2020-08-15 08:36:39 +02:00
} ,
} )
2018-08-14 11:05:26 +02:00
}
2018-12-07 09:52:31 +02:00
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleFastForward ( ) error {
2018-12-07 09:52:31 +02:00
branch := gui . getSelectedBranch ( )
2021-06-05 07:56:50 +02:00
if branch == nil || ! branch . IsRealBranch ( ) {
2018-12-07 09:52:31 +02:00
return nil
}
2021-06-05 07:56:50 +02:00
if ! branch . IsTrackingRemote ( ) {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . FwdNoUpstream )
2018-12-07 09:52:31 +02:00
}
2021-06-05 07:56:50 +02:00
if branch . HasCommitsToPush ( ) {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . FwdCommitsToPush )
2018-12-07 09:52:31 +02:00
}
2019-11-17 05:28:38 +02:00
upstream , err := gui . GitCommand . GetUpstreamForBranch ( branch . Name )
if err != nil {
2020-03-28 02:47:54 +02:00
return gui . surfaceError ( err )
2019-11-17 05:28:38 +02:00
}
2021-04-11 11:35:42 +02:00
span := gui . Tr . Spans . FastForwardBranch
2021-04-10 09:31:23 +02:00
2019-11-17 05:28:38 +02:00
split := strings . Split ( upstream , "/" )
remoteName := split [ 0 ]
remoteBranchName := strings . Join ( split [ 1 : ] , "/" )
2020-10-04 02:00:48 +02:00
message := utils . ResolvePlaceholderString (
gui . Tr . Fetching ,
map [ string ] string {
2019-11-17 05:28:38 +02:00
"from" : fmt . Sprintf ( "%s/%s" , remoteName , remoteBranchName ) ,
2018-12-07 09:52:31 +02:00
"to" : branch . Name ,
} ,
)
2020-10-07 12:19:38 +02:00
go utils . Safe ( func ( ) {
2020-11-16 11:38:26 +02:00
_ = gui . createLoaderPanel ( message )
2019-11-17 05:28:38 +02:00
2020-08-20 00:53:10 +02:00
if gui . State . Panels . Branches . SelectedLineIdx == 0 {
2021-04-10 09:31:23 +02:00
_ = gui . pullWithMode ( "ff-only" , PullFilesOptions { span : span } )
2018-12-07 09:52:31 +02:00
} else {
2021-04-10 09:31:23 +02:00
err := gui . GitCommand . WithSpan ( span ) . FastForward ( branch . Name , remoteName , remoteBranchName , gui . promptUserForCredential )
2020-08-11 13:52:43 +02:00
gui . handleCredentialsPopup ( err )
2021-03-31 14:55:06 +02:00
_ = gui . refreshSidePanels ( refreshOptions { mode : ASYNC , scope : [ ] RefreshableView { BRANCHES } } )
2018-12-07 09:52:31 +02:00
}
2020-10-07 12:19:38 +02:00
} )
2018-12-07 09:52:31 +02:00
return nil
}
2019-11-13 14:18:31 +02:00
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleCreateResetToBranchMenu ( ) error {
2020-02-16 00:03:26 +02:00
branch := gui . getSelectedBranch ( )
if branch == nil {
return nil
}
return gui . createResetMenu ( branch . Name )
}
2020-02-23 12:53:30 +02:00
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleRenameBranch ( ) error {
2020-03-17 12:22:07 +02:00
branch := gui . getSelectedBranch ( )
2021-06-05 07:56:50 +02:00
if branch == nil || ! branch . IsRealBranch ( ) {
2020-03-17 12:22:07 +02:00
return nil
}
promptForNewName := func ( ) error {
2020-11-28 04:35:58 +02:00
return gui . prompt ( promptOpts {
2021-02-09 17:50:24 +02:00
title : gui . Tr . NewBranchNamePrompt + " " + branch . Name + ":" ,
2021-02-08 19:39:50 +02:00
initialContent : branch . Name ,
2020-11-28 04:35:58 +02:00
handleConfirm : func ( newBranchName string ) error {
2021-04-11 11:35:42 +02:00
if err := gui . GitCommand . WithSpan ( gui . Tr . Spans . RenameBranch ) . RenameBranch ( branch . Name , newBranchName ) ; err != nil {
2020-11-28 04:35:58 +02:00
return gui . surfaceError ( err )
}
2021-04-06 07:13:40 +02:00
// need to find where the branch is now so that we can re-select it. That means we need to refetch the branches synchronously and then find our branch
gui . refreshBranches ( )
// now that we've got our stuff again we need to find that branch and reselect it.
for i , newBranch := range gui . State . Branches {
if newBranch . Name == newBranchName {
gui . State . Panels . Branches . SetSelectedLineIdx ( i )
if err := gui . State . Contexts . Branches . HandleRender ( ) ; err != nil {
return err
}
}
2020-11-28 04:35:58 +02:00
}
2020-03-17 12:22:07 +02:00
2021-04-06 07:13:40 +02:00
return nil
2020-11-28 04:35:58 +02:00
} ,
2020-03-17 12:22:07 +02:00
} )
}
// I could do an explicit check here for whether the branch is tracking a remote branch
// but if we've selected it we'll already know that via Pullables and Pullables.
// Bit of a hack but I'm lazy.
2021-06-05 07:56:50 +02:00
if ! branch . IsTrackingRemote ( ) {
2020-03-17 12:22:07 +02:00
return promptForNewName ( )
}
2020-08-15 08:36:39 +02:00
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-10-04 02:00:48 +02:00
title : gui . Tr . LcRenameBranch ,
prompt : gui . Tr . RenameBranchWarning ,
2020-08-23 04:20:49 +02:00
handleConfirm : promptForNewName ,
2020-08-15 08:36:39 +02:00
} )
2020-03-17 12:22:07 +02:00
}
2020-09-29 10:34:01 +02:00
func ( gui * Gui ) currentBranch ( ) * models . Branch {
2020-03-26 12:12:12 +02:00
if len ( gui . State . Branches ) == 0 {
return nil
}
2020-03-17 12:22:07 +02:00
return gui . State . Branches [ 0 ]
}
2020-04-15 12:30:24 +02:00
2020-08-22 02:17:09 +02:00
func ( gui * Gui ) handleNewBranchOffCurrentItem ( ) error {
2021-04-04 15:51:59 +02:00
context := gui . currentSideListContext ( )
2020-08-22 02:17:09 +02:00
2020-08-22 07:56:30 +02:00
item , ok := context . GetSelectedItem ( )
if ! ok {
2020-08-22 02:17:09 +02:00
return nil
}
2020-10-04 02:00:48 +02:00
message := utils . ResolvePlaceholderString (
gui . Tr . NewBranchNameBranchOff ,
map [ string ] string {
2020-08-22 02:17:09 +02:00
"branchName" : item . Description ( ) ,
} ,
)
2020-08-23 09:14:32 +02:00
prefilledName := ""
if context . GetKey ( ) == REMOTE_BRANCHES_CONTEXT_KEY {
2021-05-29 22:11:52 +02:00
// will set to the remote's branch name without the remote name
prefilledName = strings . SplitAfterN ( item . ID ( ) , "/" , 2 ) [ 1 ]
2020-08-23 09:14:32 +02:00
}
2020-08-22 02:17:09 +02:00
2020-11-28 04:35:58 +02:00
return gui . prompt ( promptOpts {
title : message ,
initialContent : prefilledName ,
handleConfirm : func ( response string ) error {
2021-04-11 11:35:42 +02:00
if err := gui . GitCommand . WithSpan ( gui . Tr . Spans . CreateBranch ) . NewBranch ( sanitizedBranchName ( response ) , item . ID ( ) ) ; err != nil {
2020-08-22 02:17:09 +02:00
return err
}
2020-11-28 04:35:58 +02:00
// if we're currently in the branch commits context then the selected commit
// is about to go to the top of the list
if context . GetKey ( ) == BRANCH_COMMITS_CONTEXT_KEY {
context . GetPanelState ( ) . SetSelectedLineIdx ( 0 )
}
2020-08-23 05:44:11 +02:00
2021-04-03 06:56:11 +02:00
if context . GetKey ( ) != gui . State . Contexts . Branches . GetKey ( ) {
if err := gui . pushContext ( gui . State . Contexts . Branches ) ; err != nil {
2020-11-28 04:35:58 +02:00
return err
}
}
gui . State . Panels . Branches . SelectedLineIdx = 0
return gui . refreshSidePanels ( refreshOptions { mode : ASYNC } )
} ,
2020-08-22 02:17:09 +02:00
} )
}
2020-11-28 11:01:45 +02:00
func ( gui * Gui ) getBranchNames ( ) [ ] string {
result := make ( [ ] string , len ( gui . State . Branches ) )
for i , branch := range gui . State . Branches {
result [ i ] = branch . Name
}
return result
}
func ( gui * Gui ) findBranchNameSuggestions ( input string ) [ ] * types . Suggestion {
branchNames := gui . getBranchNames ( )
2021-02-09 00:09:02 +02:00
matchingBranchNames := utils . FuzzySearch ( sanitizedBranchName ( input ) , branchNames )
2020-11-28 11:01:45 +02:00
suggestions := make ( [ ] * types . Suggestion , len ( matchingBranchNames ) )
for i , branchName := range matchingBranchNames {
suggestions [ i ] = & types . Suggestion {
Value : branchName ,
Label : utils . ColoredString ( branchName , presentation . GetBranchColor ( branchName ) ) ,
}
}
return suggestions
}
2021-02-09 00:09:02 +02:00
// sanitizedBranchName will remove all spaces in favor of a dash "-" to meet
// git's branch naming requirement.
func sanitizedBranchName ( input string ) string {
2021-02-24 01:09:05 +02:00
return strings . Replace ( input , " " , "-" , - 1 )
2021-02-09 00:09:02 +02:00
}