2019-11-05 14:21:19 +11:00
package gui
import (
2020-02-29 18:44:08 +11:00
"github.com/jesseduffield/lazygit/pkg/utils"
2019-11-05 14:21:19 +11:00
)
2020-08-22 18:29:09 +10:00
// getFromAndReverseArgsForDiff tells us the from and reverse args to be used in a diff command. If we're not in diff mode we'll end up with the equivalent of a `git show` i.e `git diff blah^..blah`.
func ( gui * Gui ) getFromAndReverseArgsForDiff ( to string ) ( string , bool ) {
from := to + "^"
reverse := false
if gui . State . Modes . Diffing . Active ( ) {
reverse = gui . State . Modes . Diffing . Reverse
from = gui . State . Modes . Diffing . Ref
}
return from , reverse
}
2020-10-08 08:01:04 +11:00
func ( gui * Gui ) refreshPatchBuildingPanel ( selectedLineIdx int , state * lBlPanelState ) error {
2020-08-21 20:31:21 +10:00
if ! gui . GitCommand . PatchManager . Active ( ) {
2020-08-15 17:23:16 +10:00
return gui . handleEscapePatchBuildingPanel ( )
2019-11-05 17:57:59 +11:00
}
2020-08-18 08:26:40 +10:00
gui . splitMainPanel ( true )
2019-11-05 14:21:19 +11:00
2019-11-10 16:20:35 +11:00
gui . getMainView ( ) . Title = "Patch"
gui . getSecondaryView ( ) . Title = "Custom Patch"
2019-11-05 14:21:19 +11:00
// get diff from commit file that's currently selected
2020-03-29 14:34:17 +11:00
commitFile := gui . getSelectedCommitFile ( )
2019-11-05 14:21:19 +11:00
if commitFile == nil {
2020-03-09 11:34:10 +11:00
return nil
2019-11-05 14:21:19 +11:00
}
2020-08-22 18:29:09 +10:00
to := commitFile . Parent
from , reverse := gui . getFromAndReverseArgsForDiff ( to )
diff , err := gui . GitCommand . ShowFileDiff ( from , to , reverse , commitFile . Name , true )
2019-11-05 14:21:19 +11:00
if err != nil {
return err
}
secondaryDiff := gui . GitCommand . PatchManager . RenderPatchForFile ( commitFile . Name , true , false , true )
if err != nil {
return err
}
2020-10-08 08:01:04 +11:00
empty , err := gui . refreshLineByLinePanel ( diff , secondaryDiff , false , selectedLineIdx , state )
2019-11-05 14:21:19 +11:00
if err != nil {
return err
}
if empty {
2020-08-15 17:23:16 +10:00
return gui . handleEscapePatchBuildingPanel ( )
2019-11-05 14:21:19 +11:00
}
return nil
}
2020-10-08 08:01:04 +11:00
func ( gui * Gui ) handleRefreshPatchBuildingPanel ( selectedLineIdx int ) error {
gui . Mutexes . LineByLinePanelMutex . Lock ( )
defer gui . Mutexes . LineByLinePanelMutex . Unlock ( )
return gui . refreshPatchBuildingPanel ( selectedLineIdx , gui . State . Panels . LineByLine )
}
2020-10-07 17:54:45 +11:00
func ( gui * Gui ) handleToggleSelectionForPatch ( ) error {
2020-10-08 08:01:04 +11:00
err := gui . withLBLActiveCheck ( func ( state * lBlPanelState ) error {
2020-10-07 17:54:45 +11:00
toggleFunc := gui . GitCommand . PatchManager . AddFileLineRange
filename := gui . getSelectedCommitFileName ( )
includedLineIndices , err := gui . GitCommand . PatchManager . GetFileIncLineIndices ( filename )
if err != nil {
return err
}
currentLineIsStaged := utils . IncludesInt ( includedLineIndices , state . SelectedLineIdx )
if currentLineIsStaged {
toggleFunc = gui . GitCommand . PatchManager . RemoveFileLineRange
}
2019-11-05 14:21:19 +11:00
2020-10-07 17:54:45 +11:00
// add range of lines to those set for the file
commitFile := gui . getSelectedCommitFile ( )
if commitFile == nil {
return nil
}
2019-11-05 14:21:19 +11:00
2020-10-07 17:54:45 +11:00
if err := toggleFunc ( commitFile . Name , state . FirstLineIdx , state . LastLineIdx ) ; err != nil {
// might actually want to return an error here
gui . Log . Error ( err )
}
2019-11-05 14:21:19 +11:00
2020-10-07 17:54:45 +11:00
return nil
} )
2020-10-08 08:01:04 +11:00
if err != nil {
return err
}
if err := gui . refreshCommitFilesView ( ) ; err != nil {
return err
}
return nil
2019-11-05 14:21:19 +11:00
}
2020-08-15 17:23:16 +10:00
func ( gui * Gui ) handleEscapePatchBuildingPanel ( ) error {
2020-10-02 07:56:14 +10:00
gui . escapeLineByLinePanel ( )
2019-11-05 14:21:19 +11:00
2019-11-10 15:24:20 +11:00
if gui . GitCommand . PatchManager . IsEmpty ( ) {
gui . GitCommand . PatchManager . Reset ( )
}
2020-08-21 21:08:06 +10:00
if gui . currentContext ( ) . GetKey ( ) == gui . Contexts . PatchBuilding . Context . GetKey ( ) {
return gui . switchContext ( gui . Contexts . CommitFiles . Context )
2020-08-23 13:49:05 +10:00
} else {
// need to re-focus in case the secondary view should now be hidden
return gui . currentContext ( ) . HandleFocus ( )
2020-08-21 21:08:06 +10:00
}
2019-11-05 14:21:19 +11:00
}
2019-11-05 15:11:35 +11:00
2020-08-18 22:02:35 +10:00
func ( gui * Gui ) secondaryPatchPanelUpdateOpts ( ) * viewUpdateOpts {
2020-08-21 20:31:21 +10:00
if gui . GitCommand . PatchManager . Active ( ) {
2020-08-18 22:02:35 +10:00
patch := gui . GitCommand . PatchManager . RenderAggregatedPatchColored ( false )
return & viewUpdateOpts {
title : "Custom Patch" ,
noWrap : true ,
highlight : true ,
task : gui . createRenderStringWithoutScrollTask ( patch ) ,
}
}
return nil
}