2018-08-14 11:05:26 +02:00
package gui
import (
"fmt"
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
2020-02-25 11:55:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
2019-11-13 13:14:57 +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
func ( gui * Gui ) getSelectedBranch ( ) * commands . Branch {
selectedLine := gui . State . Panels . Branches . SelectedLine
if selectedLine == - 1 {
return nil
}
return gui . State . Branches [ selectedLine ]
}
// may want to standardise how these select methods work
func ( gui * Gui ) handleBranchSelect ( g * gocui . Gui , v * gocui . View ) error {
2019-03-02 04:22:02 +02:00
if gui . popupPanelFocused ( ) {
return nil
}
2019-11-05 06:11:35 +02:00
gui . State . SplitMainPanel = false
2019-03-02 04:22:02 +02:00
if _ , err := gui . g . SetCurrentView ( v . Name ( ) ) ; err != nil {
return err
}
2019-11-10 07:20:35 +02:00
gui . getMainView ( ) . Title = "Log"
2018-12-06 13:18:17 +02:00
// This really shouldn't happen: there should always be a master branch
if len ( gui . State . Branches ) == 0 {
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
return gui . newStringTask ( "main" , gui . Tr . SLocalize ( "NoBranchesThisRepo" ) )
2018-12-06 13:18:17 +02:00
}
branch := gui . getSelectedBranch ( )
2020-03-09 02:34:10 +02:00
v . FocusPoint ( 0 , gui . State . Panels . Branches . SelectedLine )
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
if err := gui . RenderSelectedBranchUpstreamDifferences ( ) ; err != nil {
return err
}
cmd := gui . OSCommand . ExecutableFromString (
gui . GitCommand . GetBranchGraphCmdStr ( branch . Name ) ,
)
if err := gui . newCmdTask ( "main" , cmd ) ; err != nil {
gui . Log . Error ( err )
}
2018-12-06 13:18:17 +02:00
return nil
}
2018-12-07 09:52:31 +02:00
func ( gui * Gui ) RenderSelectedBranchUpstreamDifferences ( ) error {
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
return gui . newTask ( "branches" , func ( stop chan struct { } ) error {
branch := gui . getSelectedBranch ( )
branch . Pushables , branch . Pullables = gui . GitCommand . GetBranchUpstreamDifferenceCount ( branch . Name )
select {
case <- stop :
return nil
default :
}
2020-02-25 11:55:36 +02:00
branchesView := gui . getBranchesView ( )
displayStrings := presentation . GetBranchListDisplayStrings ( gui . State . Branches , gui . currentViewName ( ) == "branches" , gui . State . Panels . Branches . SelectedLine )
gui . renderDisplayStrings ( branchesView , displayStrings )
return nil
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
} )
2018-12-07 09:52:31 +02:00
}
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
func ( gui * Gui ) refreshBranches ( g * gocui . Gui ) error {
2019-11-13 14:18:31 +02:00
if err := gui . refreshRemotes ( ) ; err != nil {
return err
}
2019-11-18 00:38:36 +02:00
if err := gui . refreshTags ( ) ; err != nil {
return err
}
2018-12-06 13:18:17 +02:00
g . Update ( func ( g * gocui . Gui ) error {
2019-11-04 10:47:25 +02:00
builder , err := commands . NewBranchListBuilder ( gui . Log , gui . GitCommand )
2018-12-06 13:18:17 +02:00
if err != nil {
return err
}
gui . State . Branches = builder . Build ( )
2019-11-17 03:02:39 +02:00
// TODO: if we're in the remotes view and we've just deleted a remote we need to refresh accordingly
if gui . getBranchesView ( ) . Context == "local-branches" {
2020-02-10 09:56:48 +02:00
if err := gui . renderLocalBranchesWithSelection ( ) ; err != nil {
2019-11-17 03:02:39 +02:00
return err
}
2018-12-06 13:18:17 +02:00
}
return gui . refreshStatus ( g )
} )
return nil
}
2019-11-17 03:02:39 +02:00
func ( gui * Gui ) renderLocalBranchesWithSelection ( ) error {
branchesView := gui . getBranchesView ( )
gui . refreshSelectedLine ( & gui . State . Panels . Branches . SelectedLine , len ( gui . State . Branches ) )
2020-02-10 09:56:48 +02:00
if err := gui . RenderSelectedBranchUpstreamDifferences ( ) ; err != nil {
2019-11-17 03:02:39 +02:00
return err
}
2020-02-10 09:56:48 +02:00
if gui . g . CurrentView ( ) == branchesView {
2020-01-09 12:34:17 +02:00
if err := gui . handleBranchSelect ( gui . g , branchesView ) ; err != nil {
return err
}
2019-11-17 03:02:39 +02:00
}
return nil
}
2018-12-06 13:18:17 +02:00
// specific functions
2018-08-14 11:05:26 +02:00
func ( gui * Gui ) handleBranchPress ( g * gocui . Gui , v * gocui . View ) error {
2018-12-06 13:18:17 +02:00
if gui . State . Panels . Branches . SelectedLine == - 1 {
return nil
}
if gui . State . Panels . Branches . SelectedLine == 0 {
2018-08-16 07:16:32 +02:00
return gui . createErrorPanel ( g , gui . Tr . SLocalize ( "AlreadyCheckedOutBranch" ) )
2018-08-14 11:05:26 +02:00
}
2018-12-06 13:18:17 +02:00
branch := gui . getSelectedBranch ( )
2020-01-07 11:24:10 +02:00
return gui . handleCheckoutRef ( branch . Name )
2018-08-14 11:05:26 +02:00
}
2018-10-12 14:06:03 +02:00
func ( gui * Gui ) handleCreatePullRequestPress ( g * gocui . Gui , v * gocui . View ) 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 ( )
2018-10-12 14:06:03 +02:00
if err := pullRequest . Create ( branch ) ; err != nil {
return gui . createErrorPanel ( g , err . Error ( ) )
}
return nil
}
2018-12-02 15:58:18 +02:00
func ( gui * Gui ) handleGitFetch ( g * gocui . Gui , v * gocui . View ) error {
2019-02-16 03:03:22 +02:00
if err := gui . createLoaderPanel ( gui . g , v , gui . Tr . SLocalize ( "FetchWait" ) ) ; err != nil {
2018-12-07 15:56:29 +02:00
return err
}
go func ( ) {
unamePassOpend , err := gui . fetch ( g , v , true )
2018-12-10 09:22:52 +02:00
gui . HandleCredentialsPopup ( g , unamePassOpend , err )
2018-12-07 15:56:29 +02:00
} ( )
return nil
2018-12-02 15:58:18 +02:00
}
2018-08-14 11:05:26 +02:00
func ( gui * Gui ) handleForceCheckout ( g * gocui . Gui , v * gocui . View ) error {
2018-12-06 13:18:17 +02:00
branch := gui . getSelectedBranch ( )
2018-08-16 07:16:32 +02:00
message := gui . Tr . SLocalize ( "SureForceCheckout" )
title := gui . Tr . SLocalize ( "ForceCheckoutBranch" )
2019-11-05 06:19:43 +02:00
return gui . createConfirmationPanel ( g , v , true , title , message , func ( g * gocui . Gui , v * gocui . View ) error {
2018-08-14 11:05:26 +02:00
if err := gui . GitCommand . Checkout ( branch . Name , true ) ; err != nil {
2020-03-09 02:34:10 +02:00
_ = gui . createErrorPanel ( g , err . Error ( ) )
2018-08-14 11:05:26 +02:00
}
return gui . refreshSidePanels ( g )
} , nil )
}
2020-01-07 11:24:10 +02:00
func ( gui * Gui ) handleCheckoutRef ( ref string ) error {
if err := gui . GitCommand . Checkout ( ref , false ) ; err != nil {
2019-03-16 03:45:38 +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
2019-07-14 06:31:19 +02:00
if strings . Contains ( err . Error ( ) , "Please commit your changes or stash them before you switch branch" ) {
// offer to autostash changes
2019-11-05 06:19:43 +02:00
return gui . createConfirmationPanel ( gui . g , gui . getBranchesView ( ) , true , gui . Tr . SLocalize ( "AutoStashTitle" ) , gui . Tr . SLocalize ( "AutoStashPrompt" ) , func ( g * gocui . Gui , v * gocui . View ) error {
2020-01-07 11:24:10 +02:00
if err := gui . GitCommand . StashSave ( gui . Tr . SLocalize ( "StashPrefix" ) + ref ) ; err != nil {
2019-07-14 06:31:19 +02:00
return gui . createErrorPanel ( g , err . Error ( ) )
}
2020-01-07 11:24:10 +02:00
if err := gui . GitCommand . Checkout ( ref , false ) ; err != nil {
2019-07-14 06:31:19 +02:00
return gui . createErrorPanel ( g , err . Error ( ) )
}
// checkout successful so we select the new branch
gui . State . Panels . Branches . SelectedLine = 0
if err := gui . GitCommand . StashDo ( 0 , "pop" ) ; err != nil {
if err := gui . refreshSidePanels ( g ) ; err != nil {
return err
}
return gui . createErrorPanel ( g , err . Error ( ) )
2019-03-16 03:45:38 +02:00
}
2019-07-14 06:31:19 +02:00
return gui . refreshSidePanels ( g )
} , nil )
}
if err := gui . createErrorPanel ( gui . g , err . Error ( ) ) ; err != nil {
return err
}
2019-03-16 03:45:38 +02:00
}
gui . State . Panels . Branches . SelectedLine = 0
2020-01-07 11:24:10 +02:00
gui . State . Panels . Commits . SelectedLine = 0
2019-03-16 03:45:38 +02:00
return gui . refreshSidePanels ( gui . g )
}
2018-08-14 11:05:26 +02:00
func ( gui * Gui ) handleCheckoutByName ( g * gocui . Gui , v * gocui . View ) error {
2020-03-09 02:34:10 +02:00
return gui . createPromptPanel ( g , v , gui . Tr . SLocalize ( "BranchName" ) + ":" , "" , func ( g * gocui . Gui , v * gocui . View ) error {
2020-01-07 11:24:10 +02:00
return gui . handleCheckoutRef ( gui . trimmedContent ( v ) )
2018-08-14 11:05:26 +02:00
} )
}
2019-11-17 05:50:12 +02:00
func ( gui * Gui ) getCheckedOutBranch ( ) * commands . Branch {
if len ( gui . State . Branches ) == 0 {
return nil
}
return gui . State . Branches [ 0 ]
}
2018-08-14 11:05:26 +02:00
func ( gui * Gui ) handleNewBranch ( g * gocui . Gui , v * gocui . View ) error {
2020-03-08 04:19:38 +02:00
branch := gui . getSelectedBranch ( )
if branch == nil {
return nil
}
2018-08-14 22:29:17 +02:00
message := gui . Tr . TemplateLocalize (
"NewBranchNameBranchOff" ,
2018-08-16 11:31:03 +02:00
Teml {
2018-08-14 22:29:17 +02:00
"branchName" : branch . Name ,
} ,
)
2020-03-09 02:34:10 +02:00
return gui . createPromptPanel ( g , v , message , "" , func ( g * gocui . Gui , v * gocui . View ) error {
2020-03-08 04:19:38 +02:00
if err := gui . GitCommand . NewBranch ( gui . trimmedContent ( v ) , branch . Name ) ; err != nil {
2018-08-14 11:05:26 +02:00
return gui . createErrorPanel ( g , err . Error ( ) )
}
2020-03-09 02:34:10 +02:00
if err := gui . refreshSidePanels ( g ) ; err != nil {
return gui . createErrorPanel ( g , err . Error ( ) )
}
2018-08-14 11:05:26 +02:00
return gui . handleBranchSelect ( g , v )
} )
}
func ( gui * Gui ) handleDeleteBranch ( g * gocui . Gui , v * gocui . View ) error {
2018-08-21 11:32:17 +02:00
return gui . deleteBranch ( g , v , false )
2018-08-15 15:02:01 +02:00
}
func ( gui * Gui ) deleteBranch ( g * gocui . Gui , v * gocui . View , 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 {
2018-08-16 07:16:32 +02:00
return gui . createErrorPanel ( g , gui . Tr . SLocalize ( "CantDeleteCheckOutBranch" ) )
2018-08-14 11:05:26 +02:00
}
2018-10-06 13:34:33 +02:00
return gui . deleteNamedBranch ( g , v , selectedBranch , force )
}
func ( gui * Gui ) deleteNamedBranch ( g * gocui . Gui , v * gocui . View , selectedBranch * commands . Branch , force bool ) error {
2018-08-15 15:02:01 +02:00
title := gui . Tr . SLocalize ( "DeleteBranch" )
2018-12-06 09:26:05 +02:00
var messageID string
2018-08-21 12:06:42 +02:00
if force {
2018-12-06 09:26:05 +02:00
messageID = "ForceDeleteBranchMessage"
2018-08-21 12:06:42 +02:00
} else {
2018-12-06 09:26:05 +02:00
messageID = "DeleteBranchMessage"
2018-08-21 12:06:42 +02:00
}
2018-08-14 22:29:17 +02:00
message := gui . Tr . TemplateLocalize (
2018-12-06 09:26:05 +02:00
messageID ,
2018-08-16 11:31:03 +02:00
Teml {
2018-08-14 22:29:17 +02:00
"selectedBranchName" : selectedBranch . Name ,
} ,
)
2019-11-05 06:19:43 +02:00
return gui . createConfirmationPanel ( g , v , true , title , message , func ( g * gocui . Gui , v * gocui . View ) error {
2018-08-15 15:02:01 +02:00
if err := gui . GitCommand . DeleteBranch ( selectedBranch . Name , force ) ; err != nil {
2018-10-06 13:34:33 +02:00
errMessage := err . Error ( )
if ! force && strings . Contains ( errMessage , "is not fully merged" ) {
return gui . deleteNamedBranch ( g , v , selectedBranch , true )
}
2018-12-06 09:26:05 +02:00
return gui . createErrorPanel ( g , errMessage )
2018-08-14 11:05:26 +02:00
}
return gui . refreshSidePanels ( g )
} , nil )
}
2019-11-17 04:21:38 +02:00
func ( gui * Gui ) mergeBranchIntoCheckedOutBranch ( branchName string ) error {
if gui . GitCommand . IsHeadDetached ( ) {
return gui . createErrorPanel ( gui . g , "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 05:50:12 +02:00
checkedOutBranchName := gui . getCheckedOutBranch ( ) . Name
2019-11-17 04:21:38 +02:00
if checkedOutBranchName == branchName {
return gui . createErrorPanel ( gui . g , gui . Tr . SLocalize ( "CantMergeBranchIntoItself" ) )
2018-08-14 11:05:26 +02:00
}
2019-02-16 12:01:17 +02:00
prompt := gui . Tr . TemplateLocalize (
"ConfirmMerge" ,
Teml {
2019-11-17 04:21:38 +02:00
"checkedOutBranch" : checkedOutBranchName ,
"selectedBranch" : branchName ,
2019-02-16 12:01:17 +02:00
} ,
)
2019-11-17 04:21:38 +02:00
return gui . createConfirmationPanel ( gui . g , gui . getBranchesView ( ) , true , gui . Tr . SLocalize ( "MergingTitle" ) , prompt ,
2019-02-16 12:01:17 +02:00
func ( g * gocui . Gui , v * gocui . View ) error {
2019-11-17 04:21:38 +02:00
err := gui . GitCommand . Merge ( branchName )
2019-02-16 12:01:17 +02:00
return gui . handleGenericMergeCommandResult ( err )
} , nil )
}
2019-11-17 04:21:38 +02:00
func ( gui * Gui ) handleMerge ( g * gocui . Gui , v * gocui . View ) error {
selectedBranchName := gui . getSelectedBranch ( ) . Name
return gui . mergeBranchIntoCheckedOutBranch ( selectedBranchName )
}
2019-11-17 05:04:57 +02:00
func ( gui * Gui ) handleRebaseOntoLocalBranch ( g * gocui . Gui , v * gocui . View ) error {
selectedBranchName := gui . getSelectedBranch ( ) . Name
return gui . handleRebaseOntoBranch ( selectedBranchName )
}
func ( gui * Gui ) handleRebaseOntoBranch ( selectedBranchName string ) error {
2019-11-17 05:50:12 +02:00
checkedOutBranch := gui . getCheckedOutBranch ( ) . Name
2019-11-17 05:04:57 +02:00
if selectedBranchName == checkedOutBranch {
return gui . createErrorPanel ( gui . g , gui . Tr . SLocalize ( "CantRebaseOntoSelf" ) )
2018-08-14 11:05:26 +02:00
}
2019-02-16 12:01:17 +02:00
prompt := gui . Tr . TemplateLocalize (
"ConfirmRebase" ,
Teml {
"checkedOutBranch" : checkedOutBranch ,
2019-11-17 05:04:57 +02:00
"selectedBranch" : selectedBranchName ,
2019-02-16 12:01:17 +02:00
} ,
)
2019-11-17 05:04:57 +02:00
return gui . createConfirmationPanel ( gui . g , gui . getBranchesView ( ) , true , gui . Tr . SLocalize ( "RebasingTitle" ) , prompt ,
2019-02-16 12:01:17 +02:00
func ( g * gocui . Gui , v * gocui . View ) error {
2019-11-17 05:04:57 +02:00
err := gui . GitCommand . RebaseBranch ( selectedBranchName )
2019-02-16 12:01:17 +02:00
return gui . handleGenericMergeCommandResult ( err )
} , nil )
2018-08-14 11:05:26 +02:00
}
2018-12-07 09:52:31 +02:00
func ( gui * Gui ) handleFastForward ( g * gocui . Gui , v * gocui . View ) error {
branch := gui . getSelectedBranch ( )
if branch == nil {
return nil
}
if branch . Pushables == "" {
return nil
}
if branch . Pushables == "?" {
2019-02-16 12:01:17 +02:00
return gui . createErrorPanel ( gui . g , gui . Tr . SLocalize ( "FwdNoUpstream" ) )
2018-12-07 09:52:31 +02:00
}
if branch . Pushables != "0" {
2019-02-16 12:01:17 +02:00
return gui . createErrorPanel ( gui . g , gui . Tr . SLocalize ( "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 {
return gui . createErrorPanel ( gui . g , err . Error ( ) )
}
split := strings . Split ( upstream , "/" )
remoteName := split [ 0 ]
remoteBranchName := strings . Join ( split [ 1 : ] , "/" )
2018-12-07 09:52:31 +02:00
message := gui . Tr . TemplateLocalize (
"Fetching" ,
Teml {
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 ,
} ,
)
go func ( ) {
2019-02-16 03:03:22 +02:00
_ = gui . createLoaderPanel ( gui . g , v , message )
2019-11-17 05:28:38 +02:00
2020-02-18 11:53:46 +02:00
if gui . State . Panels . Branches . SelectedLine == 0 {
if err := gui . GitCommand . PullWithoutPasswordCheck ( "--ff-only" ) ; err != nil {
_ = gui . createErrorPanel ( gui . g , err . Error ( ) )
}
_ = gui . refreshSidePanels ( gui . g )
2018-12-07 09:52:31 +02:00
} else {
2020-02-18 11:53:46 +02:00
if err := gui . GitCommand . FastForward ( branch . Name , remoteName , remoteBranchName ) ; err != nil {
_ = gui . createErrorPanel ( gui . g , err . Error ( ) )
}
2018-12-07 09:52:31 +02:00
_ = gui . RenderSelectedBranchUpstreamDifferences ( )
}
2020-02-18 11:53:46 +02:00
_ = gui . closeConfirmationPrompt ( gui . g , true )
2018-12-07 09:52:31 +02:00
} ( )
return nil
}
2019-11-13 14:18:31 +02:00
func ( gui * Gui ) onBranchesTabClick ( tabIndex int ) error {
2019-11-18 00:38:36 +02:00
contexts := [ ] string { "local-branches" , "remotes" , "tags" }
2019-11-13 14:18:31 +02:00
branchesView := gui . getBranchesView ( )
branchesView . TabIndex = tabIndex
2019-11-16 08:35:59 +02:00
return gui . switchBranchesPanelContext ( contexts [ tabIndex ] )
}
func ( gui * Gui ) switchBranchesPanelContext ( context string ) error {
branchesView := gui . getBranchesView ( )
branchesView . Context = context
2020-03-09 02:34:10 +02:00
if err := gui . onSearchEscape ( ) ; err != nil {
return err
}
2019-11-16 08:35:59 +02:00
2019-11-17 04:25:28 +02:00
contextTabIndexMap := map [ string ] int {
"local-branches" : 0 ,
"remotes" : 1 ,
"remote-branches" : 1 ,
2019-11-18 00:38:36 +02:00
"tags" : 2 ,
2019-11-17 04:25:28 +02:00
}
branchesView . TabIndex = contextTabIndexMap [ context ]
2019-11-16 08:35:59 +02:00
switch context {
2019-11-16 05:00:27 +02:00
case "local-branches" :
2019-11-17 03:02:39 +02:00
return gui . renderLocalBranchesWithSelection ( )
2019-11-16 05:00:27 +02:00
case "remotes" :
2019-11-17 03:02:39 +02:00
return gui . renderRemotesWithSelection ( )
2019-11-16 08:35:59 +02:00
case "remote-branches" :
2019-11-17 03:02:39 +02:00
return gui . renderRemoteBranchesWithSelection ( )
2019-11-18 00:38:36 +02:00
case "tags" :
return gui . renderTagsWithSelection ( )
2019-11-13 14:18:31 +02:00
}
return nil
}
2019-11-16 07:20:05 +02:00
func ( gui * Gui ) handleNextBranchesTab ( g * gocui . Gui , v * gocui . View ) error {
return gui . onBranchesTabClick (
utils . ModuloWithWrap ( v . TabIndex + 1 , len ( v . Tabs ) ) ,
)
}
func ( gui * Gui ) handlePrevBranchesTab ( g * gocui . Gui , v * gocui . View ) error {
return gui . onBranchesTabClick (
utils . ModuloWithWrap ( v . TabIndex - 1 , len ( v . Tabs ) ) ,
)
}
2020-02-16 00:03:26 +02:00
func ( gui * Gui ) handleCreateResetToBranchMenu ( g * gocui . Gui , v * gocui . View ) error {
branch := gui . getSelectedBranch ( )
if branch == nil {
return nil
}
return gui . createResetMenu ( branch . Name )
}
2020-02-23 12:53:30 +02:00
func ( gui * Gui ) onBranchesPanelSearchSelect ( selectedLine int ) error {
branchesView := gui . getBranchesView ( )
switch branchesView . Context {
case "local-branches" :
gui . State . Panels . Branches . SelectedLine = selectedLine
return gui . handleBranchSelect ( gui . g , branchesView )
case "remotes" :
gui . State . Panels . Remotes . SelectedLine = selectedLine
return gui . handleRemoteSelect ( gui . g , branchesView )
case "remote-branches" :
gui . State . Panels . RemoteBranches . SelectedLine = selectedLine
return gui . handleRemoteBranchSelect ( gui . g , branchesView )
}
return nil
}