2019-03-09 16:42:10 +02:00
package gui
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
)
2020-08-21 12:50:54 +02:00
const (
// these are the possible ref types for refs that you can view files of.
// for local commits, we're allowed to build a patch and do things involving rebasing
// with that patch
REF_TYPE_LOCAL_COMMIT = iota
// for other kinds of commits like reflog commits, we can't do anything rebasey
REF_TYPE_OTHER_COMMIT
// for stash entries we can't do anything rebasey, and the command for
// obtaining the files is slightly different
REF_TYPE_STASH
)
2020-03-29 05:34:17 +02:00
func ( gui * Gui ) getSelectedCommitFile ( ) * commands . CommitFile {
2020-08-20 00:53:10 +02:00
selectedLine := gui . State . Panels . CommitFiles . SelectedLineIdx
2019-03-09 16:42:10 +02:00
if selectedLine == - 1 {
return nil
}
return gui . State . CommitFiles [ selectedLine ]
}
2020-08-15 09:01:43 +02:00
func ( gui * Gui ) handleCommitFileSelect ( ) error {
2020-08-19 14:27:31 +02:00
gui . handleEscapeLineByLinePanel ( )
2019-11-10 07:20:35 +02:00
2020-03-29 05:34:17 +02:00
commitFile := gui . getSelectedCommitFile ( )
2019-03-09 16:42:10 +02:00
if commitFile == nil {
2020-08-18 14:02:35 +02:00
// TODO: consider making it so that we can also render strings to our own view through some common interface, or just render this to the main view for consistency
2020-08-15 08:36:39 +02:00
gui . renderString ( "commitFiles" , gui . Tr . SLocalize ( "NoCommiteFiles" ) )
2020-03-09 02:34:10 +02:00
return nil
2019-03-09 16:42:10 +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
cmd := gui . OSCommand . ExecutableFromString (
2020-08-21 12:10:25 +02:00
gui . GitCommand . ShowCommitFileCmdStr ( commitFile . Parent , commitFile . Name , false ) ,
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-18 14:02:35 +02:00
task := gui . createRunPtyTask ( cmd )
return gui . refreshMain ( refreshMainOpts {
main : & viewUpdateOpts {
title : "Patch" ,
task : task ,
} ,
secondary : gui . secondaryPatchPanelUpdateOpts ( ) ,
} )
2019-03-09 16:42:10 +02:00
}
2019-03-11 00:53:46 +02:00
func ( gui * Gui ) handleCheckoutCommitFile ( g * gocui . Gui , v * gocui . View ) error {
2020-08-20 00:53:10 +02:00
file := gui . State . CommitFiles [ gui . State . Panels . CommitFiles . SelectedLineIdx ]
2019-03-11 00:53:46 +02:00
2020-08-21 12:10:25 +02:00
if err := gui . GitCommand . CheckoutFile ( file . Parent , file . Name ) ; err != nil {
2020-03-28 02:47:54 +02:00
return gui . surfaceError ( err )
2019-03-11 00:53:46 +02:00
}
2020-03-28 00:57:36 +02:00
return gui . refreshSidePanels ( refreshOptions { mode : ASYNC } )
2019-03-11 00:53:46 +02:00
}
2019-03-11 04:04:08 +02:00
func ( gui * Gui ) handleDiscardOldFileChange ( g * gocui . Gui , v * gocui . View ) error {
2019-11-05 02:53:01 +02:00
if ok , err := gui . validateNormalWorkingTreeState ( ) ; ! ok {
return err
}
2020-08-20 00:53:10 +02:00
fileName := gui . State . CommitFiles [ gui . State . Panels . CommitFiles . SelectedLineIdx ] . Name
2019-03-11 04:04:08 +02:00
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-08-15 08:36:39 +02:00
returnToView : v ,
returnFocusOnClose : true ,
title : gui . Tr . SLocalize ( "DiscardFileChangesTitle" ) ,
prompt : gui . Tr . SLocalize ( "DiscardFileChangesPrompt" ) ,
handleConfirm : func ( ) error {
return gui . WithWaitingStatus ( gui . Tr . SLocalize ( "RebasingStatus" ) , func ( ) error {
2020-08-20 00:53:10 +02:00
if err := gui . GitCommand . DiscardOldFileChanges ( gui . State . Commits , gui . State . Panels . Commits . SelectedLineIdx , fileName ) ; err != nil {
2020-08-15 08:36:39 +02:00
if err := gui . handleGenericMergeCommandResult ( err ) ; err != nil {
return err
}
2019-03-11 04:04:08 +02:00
}
2020-08-15 08:36:39 +02:00
return gui . refreshSidePanels ( refreshOptions { mode : BLOCK_UI } )
} )
} ,
} )
2019-03-11 04:04:08 +02:00
}
func ( gui * Gui ) refreshCommitFilesView ( ) error {
2019-11-10 07:20:35 +02:00
if err := gui . refreshPatchBuildingPanel ( - 1 ) ; err != nil {
2019-11-05 08:57:59 +02:00
return err
}
2020-08-21 12:50:54 +02:00
isStash := gui . State . Panels . CommitFiles . refType == REF_TYPE_STASH
2020-08-22 05:03:20 +02:00
refName := gui . State . Panels . CommitFiles . refName
diffing := gui . State . Modes . Diffing
var files [ ] * commands . CommitFile
var err error
if diffing . Active ( ) {
from := diffing . Ref
to := refName
if diffing . Reverse {
from , to = to , from
}
files , err = gui . GitCommand . GetFilesInDiff ( from , to , refName , gui . GitCommand . PatchManager )
} else {
files , err = gui . GitCommand . GetFilesInRef ( refName , isStash , gui . GitCommand . PatchManager )
}
2019-03-11 04:04:08 +02:00
if err != nil {
2020-03-28 02:47:54 +02:00
return gui . surfaceError ( err )
2019-03-11 04:04:08 +02:00
}
gui . State . CommitFiles = files
2020-08-21 01:12:45 +02:00
return gui . postRefreshUpdate ( gui . Contexts . CommitFiles . Context )
2020-08-19 10:41:57 +02:00
}
2019-03-15 04:29:27 +02:00
func ( gui * Gui ) handleOpenOldCommitFile ( g * gocui . Gui , v * gocui . View ) error {
2020-03-29 05:34:17 +02:00
file := gui . getSelectedCommitFile ( )
2020-07-21 10:12:05 +02:00
if file == nil {
return nil
}
2019-03-15 04:29:27 +02:00
return gui . openFile ( file . Name )
}
2019-11-04 10:47:25 +02:00
2020-07-21 10:12:05 +02:00
func ( gui * Gui ) handleEditCommitFile ( g * gocui . Gui , v * gocui . View ) error {
file := gui . getSelectedCommitFile ( )
if file == nil {
return nil
}
return gui . editFile ( file . Name )
}
2019-11-04 10:47:25 +02:00
func ( gui * Gui ) handleToggleFileForPatch ( g * gocui . Gui , v * gocui . View ) error {
2019-11-05 02:53:01 +02:00
if ok , err := gui . validateNormalWorkingTreeState ( ) ; ! ok {
return err
}
2020-03-29 05:34:17 +02:00
commitFile := gui . getSelectedCommitFile ( )
2019-11-04 10:47:25 +02:00
if commitFile == nil {
2020-08-15 08:36:39 +02:00
gui . renderString ( "commitFiles" , gui . Tr . SLocalize ( "NoCommiteFiles" ) )
2020-03-09 02:34:10 +02:00
return nil
2019-11-04 10:47:25 +02:00
}
toggleTheFile := func ( ) error {
2020-08-21 12:31:21 +02:00
if ! gui . GitCommand . PatchManager . Active ( ) {
2019-11-05 09:10:47 +02:00
if err := gui . startPatchManager ( ) ; err != nil {
2019-11-04 10:47:25 +02:00
return err
}
}
2019-11-05 02:55:04 +02:00
gui . GitCommand . PatchManager . ToggleFileWhole ( commitFile . Name )
2019-11-04 10:47:25 +02:00
2020-08-22 01:22:22 +02:00
if gui . GitCommand . PatchManager . IsEmpty ( ) {
gui . GitCommand . PatchManager . Reset ( )
}
2019-11-04 10:47:25 +02:00
return gui . refreshCommitFilesView ( )
}
2020-08-21 12:31:21 +02:00
if gui . GitCommand . PatchManager . Active ( ) && gui . GitCommand . PatchManager . Parent != commitFile . Parent {
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-08-15 08:36:39 +02:00
returnToView : v ,
returnFocusOnClose : true ,
title : gui . Tr . SLocalize ( "DiscardPatch" ) ,
prompt : gui . Tr . SLocalize ( "DiscardPatchConfirm" ) ,
handleConfirm : func ( ) error {
gui . GitCommand . PatchManager . Reset ( )
return toggleTheFile ( )
} ,
} )
2019-11-04 10:47:25 +02:00
}
return toggleTheFile ( )
}
2019-11-05 09:10:47 +02:00
func ( gui * Gui ) startPatchManager ( ) error {
2019-11-04 10:47:25 +02:00
diffMap := map [ string ] string { }
2020-08-21 12:31:21 +02:00
// TODO: only load these files as we need to
2019-11-04 10:47:25 +02:00
for _ , commitFile := range gui . State . CommitFiles {
2020-08-21 12:10:25 +02:00
commitText , err := gui . GitCommand . ShowCommitFile ( commitFile . Parent , commitFile . Name , true )
2019-11-04 10:47:25 +02:00
if err != nil {
return err
}
diffMap [ commitFile . Name ] = commitText
}
2020-08-21 12:50:54 +02:00
canRebase := gui . State . Panels . CommitFiles . refType == REF_TYPE_LOCAL_COMMIT
gui . GitCommand . PatchManager . Start ( gui . State . Panels . CommitFiles . refName , diffMap , canRebase )
2019-11-04 10:47:25 +02:00
return nil
}
func ( gui * Gui ) handleEnterCommitFile ( g * gocui . Gui , v * gocui . View ) error {
2019-11-10 07:20:35 +02:00
return gui . enterCommitFile ( - 1 )
}
func ( gui * Gui ) enterCommitFile ( selectedLineIdx int ) error {
2019-11-05 02:53:01 +02:00
if ok , err := gui . validateNormalWorkingTreeState ( ) ; ! ok {
return err
}
2020-03-29 05:34:17 +02:00
commitFile := gui . getSelectedCommitFile ( )
2019-11-04 10:47:25 +02:00
if commitFile == nil {
2020-08-15 08:36:39 +02:00
gui . renderString ( "commitFiles" , gui . Tr . SLocalize ( "NoCommiteFiles" ) )
2020-03-09 02:34:10 +02:00
return nil
2019-11-04 10:47:25 +02:00
}
2019-11-10 07:20:35 +02:00
enterTheFile := func ( selectedLineIdx int ) error {
2020-08-21 12:31:21 +02:00
if ! gui . GitCommand . PatchManager . Active ( ) {
2019-11-05 09:10:47 +02:00
if err := gui . startPatchManager ( ) ; err != nil {
2019-11-04 10:47:25 +02:00
return err
}
}
2020-08-16 05:58:29 +02:00
if err := gui . switchContext ( gui . Contexts . PatchBuilding . Context ) ; err != nil {
2019-11-04 10:47:25 +02:00
return err
}
2019-11-10 07:20:35 +02:00
return gui . refreshPatchBuildingPanel ( selectedLineIdx )
2019-11-04 10:47:25 +02:00
}
2020-08-21 12:31:21 +02:00
if gui . GitCommand . PatchManager . Active ( ) && gui . GitCommand . PatchManager . Parent != commitFile . Parent {
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-08-15 08:36:39 +02:00
returnToView : gui . getCommitFilesView ( ) ,
returnFocusOnClose : false ,
title : gui . Tr . SLocalize ( "DiscardPatch" ) ,
prompt : gui . Tr . SLocalize ( "DiscardPatchConfirm" ) ,
handleConfirm : func ( ) error {
gui . GitCommand . PatchManager . Reset ( )
return enterTheFile ( selectedLineIdx )
} ,
handleClose : func ( ) error {
2020-08-21 01:12:45 +02:00
return gui . switchContext ( gui . Contexts . CommitFiles . Context )
2020-08-15 08:36:39 +02:00
} ,
2020-03-28 03:52:45 +02:00
} )
2019-11-04 10:47:25 +02:00
}
2019-11-10 07:20:35 +02:00
return enterTheFile ( selectedLineIdx )
2019-11-04 10:47:25 +02:00
}
2020-08-21 11:53:45 +02:00
2020-08-21 12:50:54 +02:00
func ( gui * Gui ) switchToCommitFilesContext ( refName string , refType int , context Context , windowName string ) error {
2020-08-21 11:53:45 +02:00
// sometimes the commitFiles view is already shown in another window, so we need to ensure that window
// no longer considers the commitFiles view as its main view.
2020-08-21 12:08:42 +02:00
gui . resetWindowForView ( "commitFiles" )
2020-08-21 11:53:45 +02:00
gui . State . Panels . CommitFiles . SelectedLineIdx = 0
gui . State . Panels . CommitFiles . refName = refName
2020-08-21 12:50:54 +02:00
gui . State . Panels . CommitFiles . refType = refType
2020-08-21 11:53:45 +02:00
gui . Contexts . CommitFiles . Context . SetParentContext ( context )
gui . Contexts . CommitFiles . Context . SetWindowName ( windowName )
if err := gui . refreshCommitFilesView ( ) ; err != nil {
return err
}
return gui . switchContext ( gui . Contexts . CommitFiles . Context )
}