mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-25 12:24:47 +02:00
more mutex safety with staging panel
This commit is contained in:
parent
a9049b4a82
commit
bb081ca764
@ -1275,7 +1275,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
ViewName: "main",
|
ViewName: "main",
|
||||||
Contexts: []string{MAIN_PATCH_BUILDING_CONTEXT_KEY},
|
Contexts: []string{MAIN_PATCH_BUILDING_CONTEXT_KEY},
|
||||||
Key: gui.getKey(config.Universal.Select),
|
Key: gui.getKey(config.Universal.Select),
|
||||||
Handler: gui.handleToggleSelectionForPatch,
|
Handler: gui.wrappedHandler(gui.handleToggleSelectionForPatch),
|
||||||
Description: gui.Tr.ToggleSelectionForPatch,
|
Description: gui.Tr.ToggleSelectionForPatch,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -341,7 +341,7 @@ func (gui *Gui) handleToggleSelectHunk() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) escapeLineByLinePanel() {
|
func (gui *Gui) escapeLineByLinePanel() {
|
||||||
gui.withLBLActiveCheck(func(*lineByLinePanelState) error {
|
_ = gui.withLBLActiveCheck(func(*lineByLinePanelState) error {
|
||||||
gui.State.Panels.LineByLine = nil
|
gui.State.Panels.LineByLine = nil
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package gui
|
package gui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jesseduffield/gocui"
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -58,37 +57,40 @@ func (gui *Gui) refreshPatchBuildingPanel(selectedLineIdx int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleToggleSelectionForPatch(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleToggleSelectionForPatch() error {
|
||||||
state := gui.State.Panels.LineByLine
|
return gui.withLBLActiveCheck(func(state *lineByLinePanelState) error {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
toggleFunc := gui.GitCommand.PatchManager.AddFileLineRange
|
// add range of lines to those set for the file
|
||||||
filename := gui.getSelectedCommitFileName()
|
commitFile := gui.getSelectedCommitFile()
|
||||||
includedLineIndices, err := gui.GitCommand.PatchManager.GetFileIncLineIndices(filename)
|
if commitFile == nil {
|
||||||
if err != nil {
|
return nil
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
currentLineIsStaged := utils.IncludesInt(includedLineIndices, state.SelectedLineIdx)
|
if err := toggleFunc(commitFile.Name, state.FirstLineIdx, state.LastLineIdx); err != nil {
|
||||||
if currentLineIsStaged {
|
// might actually want to return an error here
|
||||||
toggleFunc = gui.GitCommand.PatchManager.RemoveFileLineRange
|
gui.Log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := gui.refreshCommitFilesView(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gui.refreshPatchBuildingPanel(-1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// add range of lines to those set for the file
|
|
||||||
commitFile := gui.getSelectedCommitFile()
|
|
||||||
if commitFile == nil {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
})
|
||||||
|
|
||||||
toggleFunc(commitFile.Name, state.FirstLineIdx, state.LastLineIdx)
|
|
||||||
|
|
||||||
if err := gui.refreshCommitFilesView(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := gui.refreshPatchBuildingPanel(-1); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleEscapePatchBuildingPanel() error {
|
func (gui *Gui) handleEscapePatchBuildingPanel() error {
|
||||||
|
@ -69,18 +69,18 @@ func (gui *Gui) refreshStagingPanel(forceSecondaryFocused bool, selectedLineIdx
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleTogglePanelClick(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleTogglePanelClick(g *gocui.Gui, v *gocui.View) error {
|
||||||
state := gui.State.Panels.LineByLine
|
return gui.withLBLActiveCheck(func(state *lineByLinePanelState) error {
|
||||||
|
state.SecondaryFocused = !state.SecondaryFocused
|
||||||
|
|
||||||
state.SecondaryFocused = !state.SecondaryFocused
|
return gui.refreshStagingPanel(false, v.SelectedLineIdx())
|
||||||
|
})
|
||||||
return gui.refreshStagingPanel(false, v.SelectedLineIdx())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleTogglePanel(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleTogglePanel(g *gocui.Gui, v *gocui.View) error {
|
||||||
state := gui.State.Panels.LineByLine
|
return gui.withLBLActiveCheck(func(state *lineByLinePanelState) error {
|
||||||
|
state.SecondaryFocused = !state.SecondaryFocused
|
||||||
state.SecondaryFocused = !state.SecondaryFocused
|
return gui.refreshStagingPanel(false, -1)
|
||||||
return gui.refreshStagingPanel(false, -1)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleStagingEscape() error {
|
func (gui *Gui) handleStagingEscape() error {
|
||||||
@ -90,74 +90,74 @@ func (gui *Gui) handleStagingEscape() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleToggleStagedSelection(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleToggleStagedSelection(g *gocui.Gui, v *gocui.View) error {
|
||||||
state := gui.State.Panels.LineByLine
|
return gui.withLBLActiveCheck(func(state *lineByLinePanelState) error {
|
||||||
|
return gui.applySelection(state.SecondaryFocused)
|
||||||
return gui.applySelection(state.SecondaryFocused)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleResetSelection(g *gocui.Gui, v *gocui.View) error {
|
func (gui *Gui) handleResetSelection(g *gocui.Gui, v *gocui.View) error {
|
||||||
state := gui.State.Panels.LineByLine
|
return gui.withLBLActiveCheck(func(state *lineByLinePanelState) error {
|
||||||
|
if state.SecondaryFocused {
|
||||||
|
// for backwards compatibility
|
||||||
|
return gui.applySelection(true)
|
||||||
|
}
|
||||||
|
|
||||||
if state.SecondaryFocused {
|
if !gui.Config.GetUserConfig().Gui.SkipUnstageLineWarning {
|
||||||
// for backwards compatibility
|
return gui.ask(askOpts{
|
||||||
return gui.applySelection(true)
|
title: gui.Tr.UnstageLinesTitle,
|
||||||
}
|
prompt: gui.Tr.UnstageLinesPrompt,
|
||||||
|
handlersManageFocus: true,
|
||||||
|
handleConfirm: func() error {
|
||||||
|
if err := gui.switchContext(gui.Contexts.Staging.Context); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if !gui.Config.GetUserConfig().Gui.SkipUnstageLineWarning {
|
return gui.applySelection(true)
|
||||||
return gui.ask(askOpts{
|
},
|
||||||
title: gui.Tr.UnstageLinesTitle,
|
handleClose: func() error {
|
||||||
prompt: gui.Tr.UnstageLinesPrompt,
|
return gui.switchContext(gui.Contexts.Staging.Context)
|
||||||
handlersManageFocus: true,
|
},
|
||||||
handleConfirm: func() error {
|
})
|
||||||
if err := gui.switchContext(gui.Contexts.Staging.Context); err != nil {
|
} else {
|
||||||
return err
|
return gui.applySelection(true)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
return gui.applySelection(true)
|
|
||||||
},
|
|
||||||
handleClose: func() error {
|
|
||||||
return gui.switchContext(gui.Contexts.Staging.Context)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return gui.applySelection(true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) applySelection(reverse bool) error {
|
func (gui *Gui) applySelection(reverse bool) error {
|
||||||
state := gui.State.Panels.LineByLine
|
return gui.withLBLActiveCheck(func(state *lineByLinePanelState) error {
|
||||||
|
file := gui.getSelectedFile()
|
||||||
|
if file == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
file := gui.getSelectedFile()
|
patch := patch.ModifiedPatchForRange(gui.Log, file.Name, state.Diff, state.FirstLineIdx, state.LastLineIdx, reverse, false)
|
||||||
if file == nil {
|
|
||||||
|
if patch == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// apply the patch then refresh this panel
|
||||||
|
// create a new temp file with the patch, then call git apply with that patch
|
||||||
|
applyFlags := []string{}
|
||||||
|
if !reverse || state.SecondaryFocused {
|
||||||
|
applyFlags = append(applyFlags, "cached")
|
||||||
|
}
|
||||||
|
err := gui.GitCommand.ApplyPatch(patch, applyFlags...)
|
||||||
|
if err != nil {
|
||||||
|
return gui.surfaceError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.SelectMode == RANGE {
|
||||||
|
state.SelectMode = LINE
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gui.refreshSidePanels(refreshOptions{scope: []int{FILES}}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := gui.refreshStagingPanel(false, -1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
})
|
||||||
|
|
||||||
patch := patch.ModifiedPatchForRange(gui.Log, file.Name, state.Diff, state.FirstLineIdx, state.LastLineIdx, reverse, false)
|
|
||||||
|
|
||||||
if patch == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// apply the patch then refresh this panel
|
|
||||||
// create a new temp file with the patch, then call git apply with that patch
|
|
||||||
applyFlags := []string{}
|
|
||||||
if !reverse || state.SecondaryFocused {
|
|
||||||
applyFlags = append(applyFlags, "cached")
|
|
||||||
}
|
|
||||||
err := gui.GitCommand.ApplyPatch(patch, applyFlags...)
|
|
||||||
if err != nil {
|
|
||||||
return gui.surfaceError(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if state.SelectMode == RANGE {
|
|
||||||
state.SelectMode = LINE
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := gui.refreshSidePanels(refreshOptions{scope: []int{FILES}}); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := gui.refreshStagingPanel(false, -1); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user