2019-11-05 05:21:19 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2020-02-29 09:44:08 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2019-11-05 05:21:19 +02:00
|
|
|
)
|
|
|
|
|
2022-01-08 06:46:35 +02:00
|
|
|
func (gui *Gui) refreshPatchBuildingPanel(selectedLineIdx int) error {
|
2022-01-16 05:46:53 +02:00
|
|
|
if !gui.git.Patch.PatchManager.Active() {
|
2020-08-15 09:23:16 +02:00
|
|
|
return gui.handleEscapePatchBuildingPanel()
|
2019-11-05 08:57:59 +02:00
|
|
|
}
|
|
|
|
|
2021-04-04 15:51:59 +02:00
|
|
|
gui.Views.Main.Title = "Patch"
|
|
|
|
gui.Views.Secondary.Title = "Custom Patch"
|
2019-11-10 07:20:35 +02:00
|
|
|
|
2019-11-05 05:21:19 +02:00
|
|
|
// get diff from commit file that's currently selected
|
2022-03-19 00:31:52 +02:00
|
|
|
node := gui.State.Contexts.CommitFiles.GetSelected()
|
2021-03-31 13:08:55 +02:00
|
|
|
if node == nil {
|
2020-03-09 02:34:10 +02:00
|
|
|
return nil
|
2019-11-05 05:21:19 +02:00
|
|
|
}
|
|
|
|
|
2022-01-30 07:38:07 +02:00
|
|
|
to := gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.GetRefName()
|
2022-02-22 11:13:11 +02:00
|
|
|
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(to)
|
2022-01-16 05:46:53 +02:00
|
|
|
diff, err := gui.git.WorkingTree.ShowFileDiff(from, to, reverse, node.GetPath(), true)
|
2019-11-05 05:21:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
secondaryDiff := gui.git.Patch.PatchManager.RenderPatchForFile(node.GetPath(), true, false, true)
|
2019-11-05 05:21:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-18 08:30:34 +02:00
|
|
|
empty, err := gui.refreshLineByLinePanel(diff, secondaryDiff, false, selectedLineIdx)
|
2019-11-05 05:21:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if empty {
|
2020-08-15 09:23:16 +02:00
|
|
|
return gui.handleEscapePatchBuildingPanel()
|
2019-11-05 05:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-07 23:01:04 +02:00
|
|
|
func (gui *Gui) handleRefreshPatchBuildingPanel(selectedLineIdx int) error {
|
|
|
|
gui.Mutexes.LineByLinePanelMutex.Lock()
|
|
|
|
defer gui.Mutexes.LineByLinePanelMutex.Unlock()
|
|
|
|
|
2022-01-08 06:46:35 +02:00
|
|
|
return gui.refreshPatchBuildingPanel(selectedLineIdx)
|
2020-10-07 23:01:04 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 11:43:08 +02:00
|
|
|
func (gui *Gui) onPatchBuildingFocus(selectedLineIdx int) error {
|
|
|
|
gui.Mutexes.LineByLinePanelMutex.Lock()
|
|
|
|
defer gui.Mutexes.LineByLinePanelMutex.Unlock()
|
|
|
|
|
|
|
|
if gui.State.Panels.LineByLine == nil || selectedLineIdx != -1 {
|
|
|
|
return gui.refreshPatchBuildingPanel(selectedLineIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-07 08:54:45 +02:00
|
|
|
func (gui *Gui) handleToggleSelectionForPatch() error {
|
2021-04-18 08:30:34 +02:00
|
|
|
err := gui.withLBLActiveCheck(func(state *LblPanelState) error {
|
2022-01-16 05:46:53 +02:00
|
|
|
toggleFunc := gui.git.Patch.PatchManager.AddFileLineRange
|
2020-10-07 08:54:45 +02:00
|
|
|
filename := gui.getSelectedCommitFileName()
|
2022-01-16 05:46:53 +02:00
|
|
|
includedLineIndices, err := gui.git.Patch.PatchManager.GetFileIncLineIndices(filename)
|
2020-10-07 08:54:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-18 08:30:34 +02:00
|
|
|
currentLineIsStaged := utils.IncludesInt(includedLineIndices, state.GetSelectedLineIdx())
|
2020-10-07 08:54:45 +02:00
|
|
|
if currentLineIsStaged {
|
2022-01-16 05:46:53 +02:00
|
|
|
toggleFunc = gui.git.Patch.PatchManager.RemoveFileLineRange
|
2020-10-07 08:54:45 +02:00
|
|
|
}
|
2019-11-05 05:21:19 +02:00
|
|
|
|
2020-10-07 08:54:45 +02:00
|
|
|
// add range of lines to those set for the file
|
2022-03-19 00:31:52 +02:00
|
|
|
node := gui.State.Contexts.CommitFiles.GetSelected()
|
2021-03-31 13:08:55 +02:00
|
|
|
if node == nil {
|
2020-10-07 08:54:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-05 05:21:19 +02:00
|
|
|
|
2021-04-18 08:30:34 +02:00
|
|
|
firstLineIdx, lastLineIdx := state.SelectedRange()
|
|
|
|
|
|
|
|
if err := toggleFunc(node.GetPath(), firstLineIdx, lastLineIdx); err != nil {
|
2020-10-07 08:54:45 +02:00
|
|
|
// might actually want to return an error here
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.c.Log.Error(err)
|
2020-10-07 08:54:45 +02:00
|
|
|
}
|
2019-11-05 05:21:19 +02:00
|
|
|
|
2020-10-07 08:54:45 +02:00
|
|
|
return nil
|
|
|
|
})
|
2020-10-07 23:01:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gui.refreshCommitFilesView(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-11-05 05:21:19 +02:00
|
|
|
}
|
|
|
|
|
2020-08-15 09:23:16 +02:00
|
|
|
func (gui *Gui) handleEscapePatchBuildingPanel() error {
|
2020-10-01 23:56:14 +02:00
|
|
|
gui.escapeLineByLinePanel()
|
2019-11-05 05:21:19 +02:00
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
if gui.git.Patch.PatchManager.IsEmpty() {
|
|
|
|
gui.git.Patch.PatchManager.Reset()
|
2019-11-10 06:24:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-03 06:56:11 +02:00
|
|
|
if gui.currentContext().GetKey() == gui.State.Contexts.PatchBuilding.GetKey() {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.PushContext(gui.State.Contexts.CommitFiles)
|
2020-08-23 05:49:05 +02:00
|
|
|
} else {
|
|
|
|
// need to re-focus in case the secondary view should now be hidden
|
|
|
|
return gui.currentContext().HandleFocus()
|
2020-08-21 13:08:06 +02:00
|
|
|
}
|
2019-11-05 05:21:19 +02:00
|
|
|
}
|
2019-11-05 06:11:35 +02:00
|
|
|
|
2020-08-18 14:02:35 +02:00
|
|
|
func (gui *Gui) secondaryPatchPanelUpdateOpts() *viewUpdateOpts {
|
2022-01-16 05:46:53 +02:00
|
|
|
if gui.git.Patch.PatchManager.Active() {
|
|
|
|
patch := gui.git.Patch.PatchManager.RenderAggregatedPatchColored(false)
|
2020-08-18 14:02:35 +02:00
|
|
|
|
|
|
|
return &viewUpdateOpts{
|
|
|
|
title: "Custom Patch",
|
|
|
|
noWrap: true,
|
|
|
|
highlight: true,
|
2022-02-05 07:56:36 +02:00
|
|
|
context: gui.State.Contexts.PatchBuilding,
|
2021-04-04 15:51:59 +02:00
|
|
|
task: NewRenderStringWithoutScrollTask(patch),
|
2020-08-18 14:02:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|