2018-08-14 11:05:26 +02:00
package gui
import (
// "io"
// "io/ioutil"
// "strings"
2018-09-12 10:23:25 +02:00
"fmt"
2020-04-15 17:27:42 +02:00
"regexp"
2018-08-14 11:05:26 +02:00
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
2020-09-29 12:28:39 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
2020-10-03 06:54:55 +02:00
"github.com/jesseduffield/lazygit/pkg/config"
2020-04-15 17:27:42 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2020-09-26 09:15:13 +02:00
"github.com/mgutz/str"
2018-08-14 11:05:26 +02:00
)
2018-12-06 13:18:17 +02:00
// list panel functions
2021-03-14 05:41:11 +02:00
func ( gui * Gui ) getSelectedStatusNode ( ) * models . StatusLineNode {
selectedLine := gui . State . Panels . Files . SelectedLineIdx
if selectedLine == - 1 {
return nil
}
2020-11-15 01:45:55 +02:00
2021-03-14 05:41:11 +02:00
return gui . State . StatusLineManager . GetItemAtIndex ( selectedLine )
}
2020-11-15 01:45:55 +02:00
2020-09-29 10:45:00 +02:00
func ( gui * Gui ) getSelectedFile ( ) * models . File {
2020-08-20 00:53:10 +02:00
selectedLine := gui . State . Panels . Files . SelectedLineIdx
2018-12-06 13:18:17 +02:00
if selectedLine == - 1 {
2020-08-17 11:24:22 +02:00
return nil
2018-12-06 13:18:17 +02:00
}
2020-11-15 01:45:55 +02:00
node := gui . State . StatusLineManager . GetItemAtIndex ( selectedLine )
if node == nil {
return nil
}
return node . File
2018-12-06 13:18:17 +02:00
}
2021-03-15 14:00:20 +02:00
func ( gui * Gui ) getSelectedPath ( ) string {
node := gui . getSelectedStatusNode ( )
if node == nil {
return ""
}
return node . GetPath ( )
}
2019-11-16 05:00:27 +02:00
func ( gui * Gui ) selectFile ( alreadySelected bool ) error {
2020-08-20 00:53:10 +02:00
gui . getFilesView ( ) . FocusPoint ( 0 , gui . State . Panels . Files . SelectedLineIdx )
2020-03-29 09:15:21 +02:00
2021-03-14 09:46:22 +02:00
node := gui . getSelectedStatusNode ( )
if node == nil {
2020-08-23 01:46:28 +02:00
return gui . refreshMainViews ( refreshMainOpts {
2020-08-18 14:02:35 +02:00
main : & viewUpdateOpts {
title : "" ,
2020-10-04 02:00:48 +02:00
task : gui . createRenderStringTask ( gui . Tr . NoChangedFiles ) ,
2020-08-18 14:02:35 +02:00
} ,
} )
2018-12-06 13:18:17 +02:00
}
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 ! alreadySelected {
2020-08-18 14:02:35 +02:00
// TODO: pull into update task interface
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 . resetOrigin ( gui . getMainView ( ) ) ; err != nil {
return err
}
if err := gui . resetOrigin ( gui . getSecondaryView ( ) ) ; err != nil {
return err
}
}
2021-03-14 09:46:22 +02:00
if node . File != nil && node . File . HasInlineMergeConflicts {
2020-03-29 05:34:17 +02:00
return gui . refreshMergePanel ( )
}
2021-03-14 09:46:22 +02:00
cmdStr := gui . GitCommand . WorktreeFileDiffCmdStr ( node , false , ! node . GetHasUnstagedChanges ( ) && node . GetHasStagedChanges ( ) )
2020-08-18 14:02:35 +02:00
cmd := gui . OSCommand . ExecutableFromString ( cmdStr )
refreshOpts := refreshMainOpts { main : & viewUpdateOpts {
2020-10-04 02:00:48 +02:00
title : gui . Tr . UnstagedChanges ,
2020-08-18 14:02:35 +02:00
task : gui . createRunPtyTask ( cmd ) ,
} }
2021-03-14 09:46:22 +02:00
if node . GetHasUnstagedChanges ( ) {
if node . GetHasStagedChanges ( ) {
cmdStr := gui . GitCommand . WorktreeFileDiffCmdStr ( node , false , true )
cmd := gui . OSCommand . ExecutableFromString ( cmdStr )
2019-10-30 11:23:25 +02:00
2021-03-14 09:46:22 +02:00
refreshOpts . secondary = & viewUpdateOpts {
title : gui . Tr . StagedChanges ,
task : gui . createRunPtyTask ( cmd ) ,
}
2020-08-18 14:02:35 +02:00
}
2021-03-14 09:46:22 +02:00
} else {
2020-10-04 02:00:48 +02:00
refreshOpts . main . title = gui . Tr . StagedChanges
2019-10-30 11:23:25 +02:00
}
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
2020-08-23 01:46:28 +02:00
return gui . refreshMainViews ( refreshOpts )
2018-12-06 13:18:17 +02:00
}
2020-09-30 00:27:23 +02:00
func ( gui * Gui ) refreshFilesAndSubmodules ( ) error {
2020-10-07 12:45:57 +02:00
gui . Mutexes . RefreshingFilesMutex . Lock ( )
2019-11-12 13:19:20 +02:00
gui . State . IsRefreshingFiles = true
defer func ( ) {
gui . State . IsRefreshingFiles = false
2020-10-07 12:45:57 +02:00
gui . Mutexes . RefreshingFilesMutex . Unlock ( )
2019-11-12 13:19:20 +02:00
} ( )
2021-03-15 14:00:20 +02:00
selectedPath := gui . getSelectedPath ( )
2019-01-15 11:12:31 +02:00
2018-12-08 07:54:54 +02:00
filesView := gui . getFilesView ( )
2019-05-06 14:29:35 +02:00
if filesView == nil {
// if the filesView hasn't been instantiated yet we just return
return nil
}
2020-09-28 01:14:32 +02:00
if err := gui . refreshStateSubmoduleConfigs ( ) ; err != nil {
return err
}
2019-03-02 04:22:02 +02:00
if err := gui . refreshStateFiles ( ) ; err != nil {
return err
}
2018-12-06 13:18:17 +02:00
gui . g . Update ( func ( g * gocui . Gui ) error {
2020-09-30 00:27:23 +02:00
if err := gui . postRefreshUpdate ( gui . Contexts . Submodules . Context ) ; err != nil {
gui . Log . Error ( err )
2020-08-19 10:41:57 +02:00
}
2018-12-06 13:18:17 +02:00
2020-09-30 00:27:23 +02:00
if gui . getFilesView ( ) . Context == FILES_CONTEXT_KEY {
// doing this a little custom (as opposed to using gui.postRefreshUpdate) because we handle selecting the file explicitly below
if err := gui . Contexts . Files . Context . HandleRender ( ) ; err != nil {
return err
}
}
if gui . currentContext ( ) . GetKey ( ) == FILES_CONTEXT_KEY || ( g . CurrentView ( ) == gui . getMainView ( ) && g . CurrentView ( ) . Context == MAIN_MERGING_CONTEXT_KEY ) {
2021-03-15 14:00:20 +02:00
newSelectedPath := gui . getSelectedPath ( )
alreadySelected := selectedPath != "" && newSelectedPath == selectedPath
2020-09-30 00:27:23 +02:00
if err := gui . selectFile ( alreadySelected ) ; err != nil {
return err
}
2018-12-06 13:18:17 +02:00
}
2020-09-30 00:27:23 +02:00
2018-12-06 13:18:17 +02:00
return nil
} )
return nil
}
// specific functions
2020-09-29 10:45:00 +02:00
func ( gui * Gui ) stagedFiles ( ) [ ] * models . File {
2020-11-15 01:45:55 +02:00
files := gui . State . StatusLineManager . GetAllFiles ( )
2020-09-29 10:45:00 +02:00
result := make ( [ ] * models . File , 0 )
2018-08-14 11:05:26 +02:00
for _ , file := range files {
if file . HasStagedChanges {
result = append ( result , file )
}
}
return result
}
2020-09-29 10:45:00 +02:00
func ( gui * Gui ) trackedFiles ( ) [ ] * models . File {
2020-11-15 01:45:55 +02:00
files := gui . State . StatusLineManager . GetAllFiles ( )
2020-09-29 10:45:00 +02:00
result := make ( [ ] * models . File , 0 , len ( files ) )
2018-08-14 11:05:26 +02:00
for _ , file := range files {
if file . Tracked {
result = append ( result , file )
}
}
return result
}
2020-11-16 11:38:26 +02:00
func ( gui * Gui ) stageSelectedFile ( ) error {
2020-08-16 09:45:12 +02:00
file := gui . getSelectedFile ( )
if file == nil {
return nil
2018-08-14 11:05:26 +02:00
}
2020-08-16 09:45:12 +02:00
2018-08-14 11:05:26 +02:00
return gui . GitCommand . StageFile ( file . Name )
}
2018-12-05 13:30:10 +02:00
func ( gui * Gui ) handleEnterFile ( g * gocui . Gui , v * gocui . View ) error {
2019-11-10 07:20:35 +02:00
return gui . enterFile ( false , - 1 )
}
func ( gui * Gui ) enterFile ( forceSecondaryFocused bool , selectedLineIdx int ) error {
2021-03-14 11:18:06 +02:00
node := gui . getSelectedStatusNode ( )
if node == nil {
2018-12-02 10:57:01 +02:00
return nil
}
2020-08-16 09:45:12 +02:00
2021-03-14 11:18:06 +02:00
if node . File == nil {
return gui . handleToggleDirCollapsed ( )
}
file := node . File
2020-09-30 00:27:23 +02:00
submoduleConfigs := gui . State . Submodules
2020-09-29 01:02:44 +02:00
if file . IsSubmodule ( submoduleConfigs ) {
submoduleConfig := file . SubmoduleConfig ( submoduleConfigs )
2020-09-30 00:27:23 +02:00
return gui . enterSubmodule ( submoduleConfig )
2020-09-29 01:02:44 +02:00
}
2019-03-03 06:55:19 +02:00
if file . HasInlineMergeConflicts {
2020-08-15 09:23:16 +02:00
return gui . handleSwitchToMerge ( )
2018-12-05 13:30:10 +02:00
}
2019-10-30 11:23:25 +02:00
if file . HasMergeConflicts {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . FileStagingRequirements )
2018-12-02 10:57:01 +02:00
}
2020-11-28 04:14:48 +02:00
_ = gui . pushContext ( gui . Contexts . Staging . Context )
2020-08-16 05:58:29 +02:00
2020-10-07 23:01:04 +02:00
return gui . handleRefreshStagingPanel ( forceSecondaryFocused , selectedLineIdx ) // TODO: check if this is broken, try moving into context code
2018-12-02 10:57:01 +02:00
}
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) handleFilePress ( ) error {
2021-03-14 05:41:11 +02:00
node := gui . getSelectedStatusNode ( )
if node == nil {
2020-08-16 09:45:12 +02:00
return nil
2018-08-14 11:05:26 +02:00
}
2021-03-14 05:41:11 +02:00
// need to stage or unstage depending on situation. If we have a merge conflict we can't do anything
if node . IsLeaf ( ) {
file := node . File
2018-08-14 11:05:26 +02:00
2021-03-14 05:41:11 +02:00
if file . HasInlineMergeConflicts {
return gui . handleSwitchToMerge ( )
}
if file . HasUnstagedChanges {
if err := gui . GitCommand . StageFile ( file . Name ) ; err != nil {
return gui . surfaceError ( err )
}
} else {
if err := gui . GitCommand . UnStageFile ( file . Name , file . Tracked ) ; err != nil {
return gui . surfaceError ( err )
}
2020-08-16 09:45:12 +02:00
}
2018-08-14 11:05:26 +02:00
} else {
2021-03-14 09:46:22 +02:00
if node . GetHasUnstagedChanges ( ) {
2021-03-14 05:41:11 +02:00
if err := gui . GitCommand . StageFile ( node . Path ) ; err != nil {
return gui . surfaceError ( err )
}
} else {
// pretty sure it doesn't matter that we're always passing true here
if err := gui . GitCommand . UnStageFile ( node . Path , true ) ; err != nil {
return gui . surfaceError ( err )
}
2020-08-16 09:45:12 +02:00
}
2018-08-14 11:05:26 +02:00
}
2020-03-28 00:57:36 +02:00
if err := gui . refreshSidePanels ( refreshOptions { scope : [ ] int { FILES } } ) ; err != nil {
2018-08-14 11:05:26 +02:00
return err
}
2019-11-16 05:00:27 +02:00
return gui . selectFile ( true )
2018-08-14 11:05:26 +02:00
}
2018-08-25 00:51:47 +02:00
func ( gui * Gui ) allFilesStaged ( ) bool {
2020-11-15 01:45:55 +02:00
for _ , file := range gui . State . StatusLineManager . GetAllFiles ( ) {
2018-08-25 00:51:47 +02:00
if file . HasUnstagedChanges {
return false
}
}
return true
}
2020-08-15 08:54:48 +02:00
func ( gui * Gui ) focusAndSelectFile ( ) error {
2019-12-08 12:23:51 +02:00
return gui . selectFile ( false )
}
2018-08-25 00:51:47 +02:00
func ( gui * Gui ) handleStageAll ( g * gocui . Gui , v * gocui . View ) error {
var err error
if gui . allFilesStaged ( ) {
err = gui . GitCommand . UnstageAll ( )
} else {
err = gui . GitCommand . StageAll ( )
}
if err != nil {
2020-03-28 02:47:54 +02:00
_ = gui . surfaceError ( err )
2018-08-25 00:51:47 +02:00
}
2020-03-28 00:57:36 +02:00
if err := gui . refreshSidePanels ( refreshOptions { scope : [ ] int { FILES } } ) ; err != nil {
2018-08-25 00:51:47 +02:00
return err
}
2019-12-08 12:23:51 +02:00
return gui . selectFile ( false )
2018-08-25 00:51:47 +02:00
}
2021-03-16 00:07:00 +02:00
func ( gui * Gui ) handleIgnoreFile ( ) error {
node := gui . getSelectedStatusNode ( )
if node == nil {
2020-08-16 09:45:12 +02:00
return nil
2018-08-14 11:05:26 +02:00
}
2021-03-16 00:07:00 +02:00
if node . GetPath ( ) == ".gitignore" {
2020-10-06 02:52:10 +02:00
return gui . createErrorPanel ( "Cannot ignore .gitignore" )
}
2020-02-03 17:59:46 +02:00
2021-03-16 00:07:00 +02:00
unstageFiles := func ( ) error {
return node . ForEachFile ( func ( file * models . File ) error {
if file . HasStagedChanges {
if err := gui . GitCommand . UnStageFile ( file . Name , file . Tracked ) ; err != nil {
return err
}
}
return nil
} )
}
if node . GetIsTracked ( ) {
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-10-04 02:00:48 +02:00
title : gui . Tr . IgnoreTracked ,
prompt : gui . Tr . IgnoreTrackedPrompt ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2021-03-16 00:07:00 +02:00
// not 100% sure if this is necessary but I'll assume it is
if err := unstageFiles ( ) ; err != nil {
2020-02-03 17:59:46 +02:00
return err
}
2021-03-16 00:07:00 +02:00
if err := gui . GitCommand . RemoveTrackedFiles ( node . GetPath ( ) ) ; err != nil {
return err
}
if err := gui . GitCommand . Ignore ( node . GetPath ( ) ) ; err != nil {
2020-02-03 17:59:46 +02:00
return err
}
2020-03-28 00:57:36 +02:00
return gui . refreshSidePanels ( refreshOptions { scope : [ ] int { FILES } } )
2020-08-15 08:36:39 +02:00
} ,
} )
2018-08-14 11:05:26 +02:00
}
2020-02-03 17:59:46 +02:00
2021-03-16 00:07:00 +02:00
if err := unstageFiles ( ) ; err != nil {
return err
}
if err := gui . GitCommand . Ignore ( node . GetPath ( ) ) ; err != nil {
2020-03-28 02:47:54 +02:00
return gui . surfaceError ( err )
2018-08-19 12:41:04 +02:00
}
2020-02-03 17:59:46 +02:00
2020-03-28 00:57:36 +02:00
return gui . refreshSidePanels ( refreshOptions { scope : [ ] int { FILES } } )
2018-08-14 11:05:26 +02:00
}
2019-04-13 05:56:31 +02:00
func ( gui * Gui ) handleWIPCommitPress ( g * gocui . Gui , filesView * gocui . View ) error {
2020-10-03 06:54:55 +02:00
skipHookPreifx := gui . Config . GetUserConfig ( ) . Git . SkipHookPrefix
2019-04-13 05:56:31 +02:00
if skipHookPreifx == "" {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . SkipHookPrefixNotConfigured )
2019-04-13 05:56:31 +02:00
}
2020-11-16 11:38:26 +02:00
_ = gui . renderStringSync ( "commitMessage" , skipHookPreifx )
2019-04-13 05:56:31 +02:00
if err := gui . getCommitMessageView ( ) . SetCursor ( len ( skipHookPreifx ) , 0 ) ; err != nil {
return err
}
2020-08-15 09:23:16 +02:00
return gui . handleCommitPress ( )
2019-04-13 05:56:31 +02:00
}
2020-10-03 06:54:55 +02:00
func ( gui * Gui ) commitPrefixConfigForRepo ( ) * config . CommitPrefixConfig {
cfg , ok := gui . Config . GetUserConfig ( ) . Git . CommitPrefixes [ utils . GetCurrentRepoName ( ) ]
if ! ok {
return nil
}
return & cfg
}
2020-11-23 10:14:15 +02:00
func ( gui * Gui ) prepareFilesForCommit ( ) error {
noStagedFiles := len ( gui . stagedFiles ( ) ) == 0
if noStagedFiles && gui . Config . GetUserConfig ( ) . Gui . SkipNoStagedFilesWarning {
2020-11-18 16:17:16 +02:00
err := gui . GitCommand . StageAll ( )
2020-11-23 10:14:15 +02:00
if err != nil {
return err
}
return gui . refreshFilesAndSubmodules ( )
2020-11-18 16:17:16 +02:00
}
2020-11-23 10:14:15 +02:00
return nil
2020-11-18 16:17:16 +02:00
}
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) handleCommitPress ( ) error {
2020-11-23 10:14:15 +02:00
if err := gui . prepareFilesForCommit ( ) ; err != nil {
return gui . surfaceError ( err )
}
if len ( gui . stagedFiles ( ) ) == 0 {
2020-11-16 11:38:26 +02:00
return gui . promptToStageAllAndRetry ( gui . handleCommitPress )
2018-08-14 11:05:26 +02:00
}
2020-07-17 00:39:45 +02:00
2018-12-08 07:54:54 +02:00
commitMessageView := gui . getCommitMessageView ( )
2020-10-03 06:54:55 +02:00
commitPrefixConfig := gui . commitPrefixConfigForRepo ( )
if commitPrefixConfig != nil {
prefixPattern := commitPrefixConfig . Pattern
prefixReplace := commitPrefixConfig . Replace
2020-04-20 10:42:24 +02:00
rgx , err := regexp . Compile ( prefixPattern )
if err != nil {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( fmt . Sprintf ( "%s: %s" , gui . Tr . LcCommitPrefixPatternError , err . Error ( ) ) )
2020-04-20 10:42:24 +02:00
}
2020-04-15 17:27:42 +02:00
prefix := rgx . ReplaceAllString ( gui . getCheckedOutBranch ( ) . Name , prefixReplace )
2020-08-15 08:36:39 +02:00
gui . renderString ( "commitMessage" , prefix )
2020-04-15 17:27:42 +02:00
if err := commitMessageView . SetCursor ( len ( prefix ) , 0 ) ; err != nil {
return err
}
}
2020-08-15 09:23:16 +02:00
gui . g . Update ( func ( g * gocui . Gui ) error {
2020-11-28 04:14:48 +02:00
if err := gui . pushContext ( gui . Contexts . CommitMessage . Context ) ; err != nil {
2020-03-09 02:34:10 +02:00
return err
}
2018-09-12 15:20:35 +02:00
gui . RenderCommitLength ( )
return nil
} )
return nil
}
2020-07-17 00:39:45 +02:00
func ( gui * Gui ) promptToStageAllAndRetry ( retry func ( ) error ) error {
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-10-04 02:00:48 +02:00
title : gui . Tr . NoFilesStagedTitle ,
prompt : gui . Tr . NoFilesStagedPrompt ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2020-07-17 00:39:45 +02:00
if err := gui . GitCommand . StageAll ( ) ; err != nil {
return gui . surfaceError ( err )
}
2020-09-30 00:27:23 +02:00
if err := gui . refreshFilesAndSubmodules ( ) ; err != nil {
2020-07-17 00:39:45 +02:00
return gui . surfaceError ( err )
}
return retry ( )
2020-08-15 08:36:39 +02:00
} ,
} )
2020-07-17 00:39:45 +02:00
}
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) handleAmendCommitPress ( ) error {
2020-07-17 00:39:45 +02:00
if len ( gui . stagedFiles ( ) ) == 0 {
2020-11-16 11:38:26 +02:00
return gui . promptToStageAllAndRetry ( gui . handleAmendCommitPress )
2018-09-12 15:20:35 +02:00
}
2020-07-17 00:39:45 +02:00
2018-10-08 21:19:45 +02:00
if len ( gui . State . Commits ) == 0 {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . NoCommitToAmend )
2018-10-08 21:19:45 +02:00
}
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-10-04 02:00:48 +02:00
title : strings . Title ( gui . Tr . AmendLastCommit ) ,
prompt : gui . Tr . SureToAmend ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2020-10-04 02:00:48 +02:00
return gui . WithWaitingStatus ( gui . Tr . AmendingStatus , func ( ) error {
2020-09-17 23:54:42 +02:00
ok , err := gui . runSyncOrAsyncCommand ( gui . GitCommand . AmendHead ( ) )
if err != nil {
return err
}
if ! ok {
return nil
}
2018-09-12 15:20:35 +02:00
2020-09-17 23:54:42 +02:00
return gui . refreshSidePanels ( refreshOptions { mode : ASYNC } )
} )
2020-08-15 08:36:39 +02:00
} ,
} )
2018-08-14 11:05:26 +02:00
}
// handleCommitEditorPress - handle when the user wants to commit changes via
// their editor rather than via the popup panel
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) handleCommitEditorPress ( ) error {
2020-07-17 00:39:45 +02:00
if len ( gui . stagedFiles ( ) ) == 0 {
2020-11-16 11:38:26 +02:00
return gui . promptToStageAllAndRetry ( gui . handleCommitEditorPress )
2018-08-14 11:05:26 +02:00
}
2020-07-17 00:39:45 +02:00
2020-09-26 09:15:13 +02:00
gui . PrepareSubProcess ( "git commit" )
2018-08-14 11:05:26 +02:00
return nil
}
// PrepareSubProcess - prepare a subprocess for execution and tell the gui to switch to it
2020-09-26 09:15:13 +02:00
func ( gui * Gui ) PrepareSubProcess ( command string ) {
splitCmd := str . ToArgv ( command )
gui . SubProcess = gui . OSCommand . PrepareSubProcess ( splitCmd [ 0 ] , splitCmd [ 1 : ] ... )
2020-08-15 09:23:16 +02:00
gui . g . Update ( func ( g * gocui . Gui ) error {
2018-08-16 11:31:03 +02:00
return gui . Errors . ErrSubProcess
2018-08-14 11:05:26 +02:00
} )
}
2018-08-31 10:43:54 +02:00
func ( gui * Gui ) editFile ( filename string ) error {
2020-11-24 23:52:00 +02:00
_ , err := gui . runSyncOrAsyncCommand ( gui . GitCommand . EditFile ( filename ) )
2019-03-23 03:16:55 +02:00
return err
2018-08-14 11:05:26 +02:00
}
func ( gui * Gui ) handleFileEdit ( g * gocui . Gui , v * gocui . View ) error {
2021-03-15 14:00:20 +02:00
node := gui . getSelectedStatusNode ( )
if node == nil {
2020-08-16 09:45:12 +02:00
return nil
2018-08-18 06:52:01 +02:00
}
2018-08-31 10:43:54 +02:00
2021-03-15 14:00:20 +02:00
if node . File == nil {
return gui . createErrorPanel ( gui . Tr . ErrCannotEditDirectory )
}
return gui . editFile ( node . GetPath ( ) )
2018-08-14 11:05:26 +02:00
}
func ( gui * Gui ) handleFileOpen ( g * gocui . Gui , v * gocui . View ) error {
2021-03-15 14:00:20 +02:00
node := gui . getSelectedStatusNode ( )
if node == nil {
2020-08-16 09:45:12 +02:00
return nil
2018-08-18 06:52:01 +02:00
}
2021-03-15 14:00:20 +02:00
return gui . openFile ( node . GetPath ( ) )
2018-08-14 11:05:26 +02:00
}
func ( gui * Gui ) handleRefreshFiles ( g * gocui . Gui , v * gocui . View ) error {
2020-03-28 00:57:36 +02:00
return gui . refreshSidePanels ( refreshOptions { scope : [ ] int { FILES } } )
2018-08-14 11:05:26 +02:00
}
2019-03-02 04:22:02 +02:00
func ( gui * Gui ) refreshStateFiles ( ) error {
2020-08-07 10:27:18 +02:00
// keep track of where the cursor is currently and the current file names
// when we refresh, go looking for a matching name
// move the cursor to there.
2020-08-16 09:45:12 +02:00
selectedFile := gui . getSelectedFile ( )
2020-08-25 00:34:02 +02:00
prevSelectedLineIdx := gui . State . Panels . Files . SelectedLineIdx
2020-08-07 10:27:18 +02:00
2018-08-14 11:05:26 +02:00
// get files to stage
2020-11-15 01:45:55 +02:00
noRenames := gui . State . StatusLineManager . TreeMode
files := gui . GitCommand . GetStatusFiles ( commands . GetStatusFileOptions { NoRenames : noRenames } )
gui . State . StatusLineManager . SetFiles (
gui . GitCommand . MergeStatusFiles ( gui . State . StatusLineManager . GetAllFiles ( ) , files , selectedFile ) ,
)
2019-11-12 13:19:20 +02:00
2020-01-08 12:02:01 +02:00
if err := gui . fileWatcher . addFilesToFileWatcher ( files ) ; err != nil {
2019-11-12 13:19:20 +02:00
return err
}
2020-08-07 10:27:18 +02:00
// let's try to find our file again and move the cursor to that
2020-08-25 00:34:02 +02:00
if selectedFile != nil {
2020-11-15 01:45:55 +02:00
for idx , node := range gui . State . StatusLineManager . GetAllItems ( ) {
// TODO: check that this works
selectedFileHasMoved := node . File != nil && node . File . Matches ( selectedFile ) && idx != prevSelectedLineIdx
2020-08-25 00:34:02 +02:00
if selectedFileHasMoved {
gui . State . Panels . Files . SelectedLineIdx = idx
break
}
2020-08-07 10:27:18 +02:00
}
}
2020-11-15 01:45:55 +02:00
gui . refreshSelectedLine ( gui . State . Panels . Files , gui . State . StatusLineManager . GetItemsLength ( ) )
2020-03-27 10:12:15 +02:00
return nil
2018-08-14 11:05:26 +02:00
}
2019-11-13 12:16:24 +02:00
func ( gui * Gui ) handlePullFiles ( g * gocui . Gui , v * gocui . View ) error {
2020-08-23 03:13:51 +02:00
if gui . popupPanelFocused ( ) {
return nil
}
2020-03-17 12:22:07 +02:00
currentBranch := gui . currentBranch ( )
2020-08-23 09:28:28 +02:00
if currentBranch == nil {
// need to wait for branches to refresh
return nil
}
// if we have no upstream branch we need to set that first
2020-03-17 12:22:07 +02:00
if currentBranch . Pullables == "?" {
2020-01-28 11:35:23 +02:00
// see if we have this branch in our config with an upstream
conf , err := gui . GitCommand . Repo . Config ( )
if err != nil {
2020-03-28 02:47:54 +02:00
return gui . surfaceError ( err )
2020-01-28 11:35:23 +02:00
}
for branchName , branch := range conf . Branches {
2020-03-17 12:22:07 +02:00
if branchName == currentBranch . Name {
2020-08-11 13:18:38 +02:00
return gui . pullFiles ( PullFilesOptions { RemoteName : branch . Remote , BranchName : branch . Name } )
2020-01-28 11:35:23 +02:00
}
}
2020-11-28 04:35:58 +02:00
return gui . prompt ( promptOpts {
title : gui . Tr . EnterUpstream ,
initialContent : "origin/" + currentBranch . Name ,
handleConfirm : func ( upstream string ) error {
if err := gui . GitCommand . SetUpstreamBranch ( upstream ) ; err != nil {
errorMessage := err . Error ( )
if strings . Contains ( errorMessage , "does not exist" ) {
errorMessage = fmt . Sprintf ( "upstream branch %s not found.\nIf you expect it to exist, you should fetch (with 'f').\nOtherwise, you should push (with 'shift+P')" , upstream )
}
return gui . createErrorPanel ( errorMessage )
2019-11-13 12:16:24 +02:00
}
2020-11-28 04:35:58 +02:00
return gui . pullFiles ( PullFilesOptions { } )
} ,
2019-11-13 12:16:24 +02:00
} )
}
2020-08-11 13:18:38 +02:00
return gui . pullFiles ( PullFilesOptions { } )
2019-11-13 12:16:24 +02:00
}
2020-08-11 13:18:38 +02:00
type PullFilesOptions struct {
RemoteName string
BranchName string
}
func ( gui * Gui ) pullFiles ( opts PullFilesOptions ) error {
2020-11-16 11:38:26 +02:00
if err := gui . createLoaderPanel ( gui . Tr . PullWait ) ; err != nil {
2018-11-02 10:54:54 +02:00
return err
}
2019-02-16 03:03:22 +02:00
2020-10-03 06:54:55 +02:00
mode := gui . Config . GetUserConfig ( ) . Git . Pull . Mode
2020-08-11 12:18:50 +02:00
2020-11-16 11:38:26 +02:00
go utils . Safe ( func ( ) { _ = gui . pullWithMode ( mode , opts ) } )
2019-11-13 12:16:24 +02:00
2018-08-14 11:05:26 +02:00
return nil
}
2020-08-11 13:38:59 +02:00
func ( gui * Gui ) pullWithMode ( mode string , opts PullFilesOptions ) error {
2020-10-07 12:45:57 +02:00
gui . Mutexes . FetchMutex . Lock ( )
defer gui . Mutexes . FetchMutex . Unlock ( )
2020-08-23 13:25:39 +02:00
2020-08-11 13:38:59 +02:00
err := gui . GitCommand . Fetch (
commands . FetchOptions {
PromptUserForCredential : gui . promptUserForCredential ,
RemoteName : opts . RemoteName ,
BranchName : opts . BranchName ,
} ,
)
2020-08-11 13:41:32 +02:00
gui . handleCredentialsPopup ( err )
2020-08-11 13:38:59 +02:00
if err != nil {
return gui . refreshSidePanels ( refreshOptions { mode : ASYNC } )
}
switch mode {
case "rebase" :
err := gui . GitCommand . RebaseBranch ( "FETCH_HEAD" )
return gui . handleGenericMergeCommandResult ( err )
case "merge" :
err := gui . GitCommand . Merge ( "FETCH_HEAD" , commands . MergeOpts { } )
return gui . handleGenericMergeCommandResult ( err )
case "ff-only" :
err := gui . GitCommand . Merge ( "FETCH_HEAD" , commands . MergeOpts { FastForwardOnly : true } )
return gui . handleGenericMergeCommandResult ( err )
default :
return gui . createErrorPanel ( fmt . Sprintf ( "git pull mode '%s' unrecognised" , mode ) )
}
}
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) pushWithForceFlag ( v * gocui . View , force bool , upstream string , args string ) error {
2020-11-16 11:38:26 +02:00
if err := gui . createLoaderPanel ( gui . Tr . PushWait ) ; err != nil {
2018-08-19 13:28:13 +02:00
return err
}
2020-10-07 12:19:38 +02:00
go utils . Safe ( func ( ) {
2019-11-17 05:50:12 +02:00
branchName := gui . getCheckedOutBranch ( ) . Name
2020-08-11 12:29:05 +02:00
err := gui . GitCommand . Push ( branchName , force , upstream , args , gui . promptUserForCredential )
2020-08-12 12:52:40 +02:00
if err != nil && ! force && strings . Contains ( err . Error ( ) , "Updates were rejected" ) {
2020-10-03 06:54:55 +02:00
forcePushDisabled := gui . Config . GetUserConfig ( ) . Git . DisableForcePushing
2020-08-30 04:23:08 +02:00
if forcePushDisabled {
2020-11-16 11:38:26 +02:00
_ = gui . createErrorPanel ( gui . Tr . UpdatesRejectedAndForcePushDisabled )
2020-08-30 04:23:08 +02:00
return
}
2020-11-16 11:38:26 +02:00
_ = gui . ask ( askOpts {
2020-10-04 02:00:48 +02:00
title : gui . Tr . ForcePush ,
prompt : gui . Tr . ForcePushPrompt ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2020-08-15 09:23:16 +02:00
return gui . pushWithForceFlag ( v , true , upstream , args )
2020-08-15 08:36:39 +02:00
} ,
} )
2020-08-12 12:52:40 +02:00
return
}
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-08-14 11:05:26 +02:00
return nil
}
2018-08-19 13:28:13 +02:00
func ( gui * Gui ) pushFiles ( g * gocui . Gui , v * gocui . View ) error {
2020-08-23 03:13:51 +02:00
if gui . popupPanelFocused ( ) {
return nil
}
2018-08-19 13:28:13 +02:00
// if we have pullables we'll ask if the user wants to force push
2020-03-17 12:22:07 +02:00
currentBranch := gui . currentBranch ( )
2019-11-11 14:22:09 +02:00
2020-03-17 12:22:07 +02:00
if currentBranch . Pullables == "?" {
2020-01-28 11:35:23 +02:00
// see if we have this branch in our config with an upstream
conf , err := gui . GitCommand . Repo . Config ( )
if err != nil {
2020-03-28 02:47:54 +02:00
return gui . surfaceError ( err )
2020-01-28 11:35:23 +02:00
}
for branchName , branch := range conf . Branches {
2020-03-17 12:22:07 +02:00
if branchName == currentBranch . Name {
2020-08-15 09:23:16 +02:00
return gui . pushWithForceFlag ( v , false , "" , fmt . Sprintf ( "%s %s" , branch . Remote , branchName ) )
2020-01-28 11:35:23 +02:00
}
}
2020-05-15 13:26:02 +02:00
if gui . GitCommand . PushToCurrent {
2020-08-15 09:23:16 +02:00
return gui . pushWithForceFlag ( v , false , "" , "--set-upstream" )
2020-05-15 13:26:02 +02:00
} else {
2020-11-28 04:35:58 +02:00
return gui . prompt ( promptOpts {
title : gui . Tr . EnterUpstream ,
initialContent : "origin " + currentBranch . Name ,
handleConfirm : func ( response string ) error {
return gui . pushWithForceFlag ( v , false , response , "" )
} ,
2020-05-15 13:26:02 +02:00
} )
}
2020-03-17 12:22:07 +02:00
} else if currentBranch . Pullables == "0" {
2020-08-15 09:23:16 +02:00
return gui . pushWithForceFlag ( v , false , "" , "" )
2018-08-19 13:28:13 +02:00
}
2020-08-15 08:36:39 +02:00
2020-10-03 06:54:55 +02:00
forcePushDisabled := gui . Config . GetUserConfig ( ) . Git . DisableForcePushing
2020-08-30 04:23:08 +02:00
if forcePushDisabled {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . ForcePushDisabled )
2020-08-30 04:23:08 +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 . ForcePush ,
prompt : gui . Tr . ForcePushPrompt ,
2020-08-15 08:36:39 +02:00
handleConfirm : func ( ) error {
2020-08-15 09:23:16 +02:00
return gui . pushWithForceFlag ( v , true , "" , "" )
2020-08-15 08:36:39 +02:00
} ,
} )
2018-08-19 13:28:13 +02:00
}
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) handleSwitchToMerge ( ) error {
2020-08-16 09:45:12 +02:00
file := gui . getSelectedFile ( )
if file == nil {
2018-08-14 11:05:26 +02:00
return nil
}
2020-08-16 09:45:12 +02:00
2019-03-03 06:55:19 +02:00
if ! file . HasInlineMergeConflicts {
2020-10-04 02:00:48 +02:00
return gui . createErrorPanel ( gui . Tr . FileNoMergeCons )
2018-08-14 11:05:26 +02:00
}
2020-08-23 03:08:51 +02:00
2020-11-28 04:14:48 +02:00
return gui . pushContext ( gui . Contexts . Merging . Context )
2018-08-14 11:05:26 +02:00
}
2018-08-23 21:05:09 +02:00
func ( gui * Gui ) openFile ( filename string ) error {
if err := gui . OSCommand . OpenFile ( filename ) ; err != nil {
2020-03-28 02:47:54 +02:00
return gui . surfaceError ( err )
2018-08-23 21:05:09 +02:00
}
return nil
}
2018-12-08 07:54:54 +02:00
func ( gui * Gui ) anyFilesWithMergeConflicts ( ) bool {
2020-11-15 01:45:55 +02:00
for _ , file := range gui . State . StatusLineManager . GetAllFiles ( ) {
2018-12-08 07:54:54 +02:00
if file . HasMergeConflicts {
return true
}
}
return false
}
2019-02-19 14:36:29 +02:00
2019-03-12 12:43:56 +02:00
func ( gui * Gui ) handleCustomCommand ( g * gocui . Gui , v * gocui . View ) error {
2020-11-28 04:35:58 +02:00
return gui . prompt ( promptOpts {
title : gui . Tr . CustomCommand ,
handleConfirm : func ( command string ) error {
gui . SubProcess = gui . OSCommand . RunCustomCommand ( command )
return gui . Errors . ErrSubProcess
} ,
2019-03-12 12:43:56 +02:00
} )
}
2019-05-30 14:45:56 +02:00
func ( gui * Gui ) handleCreateStashMenu ( g * gocui . Gui , v * gocui . View ) error {
2020-02-14 14:36:55 +02:00
menuItems := [ ] * menuItem {
2019-05-30 14:45:56 +02:00
{
2020-10-04 02:00:48 +02:00
displayString : gui . Tr . LcStashAllChanges ,
2020-02-14 14:36:55 +02:00
onPress : func ( ) error {
2019-05-30 14:45:56 +02:00
return gui . handleStashSave ( gui . GitCommand . StashSave )
} ,
} ,
{
2020-10-04 02:00:48 +02:00
displayString : gui . Tr . LcStashStagedChanges ,
2020-02-14 14:36:55 +02:00
onPress : func ( ) error {
2019-05-30 14:45:56 +02:00
return gui . handleStashSave ( gui . GitCommand . StashSaveStagedChanges )
} ,
} ,
}
2020-10-04 02:00:48 +02:00
return gui . createMenu ( gui . Tr . LcStashOptions , menuItems , createMenuOptions { showCancel : true } )
2019-05-30 14:45:56 +02:00
}
func ( gui * Gui ) handleStashChanges ( g * gocui . Gui , v * gocui . View ) error {
return gui . handleStashSave ( gui . GitCommand . StashSave )
}
2020-02-16 00:21:23 +02:00
func ( gui * Gui ) handleCreateResetToUpstreamMenu ( g * gocui . Gui , v * gocui . View ) error {
return gui . createResetMenu ( "@{upstream}" )
}
2021-03-14 11:18:06 +02:00
func ( gui * Gui ) handleToggleDirCollapsed ( ) error {
node := gui . getSelectedStatusNode ( )
if node == nil {
return nil
}
gui . State . StatusLineManager . ToggleCollapsed ( node )
if err := gui . postRefreshUpdate ( gui . Contexts . Files . Context ) ; err != nil {
gui . Log . Error ( err )
}
return nil
}