1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/patch_building_panel.go

134 lines
3.5 KiB
Go
Raw Normal View History

package gui
import (
"github.com/jesseduffield/lazygit/pkg/utils"
)
// 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-07 23:01:04 +02:00
func (gui *Gui) refreshPatchBuildingPanel(selectedLineIdx int, state *lBlPanelState) error {
2020-08-21 12:31:21 +02:00
if !gui.GitCommand.PatchManager.Active() {
2020-08-15 09:23:16 +02:00
return gui.handleEscapePatchBuildingPanel()
}
2020-08-18 00:26:40 +02:00
gui.splitMainPanel(true)
2019-11-10 07:20:35 +02:00
gui.getMainView().Title = "Patch"
gui.getSecondaryView().Title = "Custom Patch"
// get diff from commit file that's currently selected
2021-03-31 13:08:55 +02:00
node := gui.getSelectedCommitFileNode()
if node == nil {
2020-03-09 02:34:10 +02:00
return nil
}
2021-03-31 13:08:55 +02:00
to := gui.State.CommitFileChangeManager.GetParent()
from, reverse := gui.getFromAndReverseArgsForDiff(to)
2021-03-31 13:08:55 +02:00
diff, err := gui.GitCommand.ShowFileDiff(from, to, reverse, node.GetPath(), true)
if err != nil {
return err
}
2021-03-31 13:08:55 +02:00
secondaryDiff := gui.GitCommand.PatchManager.RenderPatchForFile(node.GetPath(), true, false, true)
if err != nil {
return err
}
2020-10-07 23:01:04 +02:00
empty, err := gui.refreshLineByLinePanel(diff, secondaryDiff, false, selectedLineIdx, state)
if err != nil {
return err
}
if empty {
2020-08-15 09:23:16 +02:00
return gui.handleEscapePatchBuildingPanel()
}
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()
return gui.refreshPatchBuildingPanel(selectedLineIdx, gui.State.Panels.LineByLine)
}
2020-10-07 08:54:45 +02:00
func (gui *Gui) handleToggleSelectionForPatch() error {
2020-10-07 23:01:04 +02:00
err := gui.withLBLActiveCheck(func(state *lBlPanelState) error {
2020-10-07 08:54:45 +02: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
}
2020-10-07 08:54:45 +02:00
// add range of lines to those set for the file
2021-03-31 13:08:55 +02:00
node := gui.getSelectedCommitFileNode()
if node == nil {
2020-10-07 08:54:45 +02:00
return nil
}
2021-03-31 13:08:55 +02:00
if err := toggleFunc(node.GetPath(), state.FirstLineIdx, state.LastLineIdx); err != nil {
2020-10-07 08:54:45 +02:00
// might actually want to return an error here
gui.Log.Error(err)
}
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
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleEscapePatchBuildingPanel() error {
gui.escapeLineByLinePanel()
if gui.GitCommand.PatchManager.IsEmpty() {
gui.GitCommand.PatchManager.Reset()
}
if gui.currentContext().GetKey() == gui.Contexts.PatchBuilding.Context.GetKey() {
return gui.pushContext(gui.Contexts.CommitFiles.Context)
} else {
// need to re-focus in case the secondary view should now be hidden
return gui.currentContext().HandleFocus()
}
}
2019-11-05 06:11:35 +02:00
2020-08-18 14:02:35 +02:00
func (gui *Gui) secondaryPatchPanelUpdateOpts() *viewUpdateOpts {
2020-08-21 12:31:21 +02:00
if gui.GitCommand.PatchManager.Active() {
2020-08-18 14:02:35 +02:00
patch := gui.GitCommand.PatchManager.RenderAggregatedPatchColored(false)
return &viewUpdateOpts{
title: "Custom Patch",
noWrap: true,
highlight: true,
task: gui.createRenderStringWithoutScrollTask(patch),
}
}
return nil
}