mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-12-04 10:34:55 +02:00
add commit files controller
This commit is contained in:
parent
85f2319897
commit
ecaff7fc6c
@ -2,10 +2,8 @@ package gui
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/patch"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/controllers"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
)
|
||||
|
||||
func (gui *Gui) getSelectedCommitFile() *models.CommitFile {
|
||||
@ -37,7 +35,7 @@ func (gui *Gui) commitFilesRenderToMain() error {
|
||||
}
|
||||
|
||||
to := gui.State.Contexts.CommitFiles.GetRefName()
|
||||
from, reverse := gui.getFromAndReverseArgsForDiff(to)
|
||||
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(to)
|
||||
|
||||
cmdObj := gui.git.WorkingTree.ShowFileDiffCmdObj(from, to, reverse, node.GetPath(), false)
|
||||
task := NewRunPtyTask(cmdObj.GetCmd())
|
||||
@ -57,212 +55,6 @@ func (gui *Gui) commitFilesRenderToMain() error {
|
||||
})
|
||||
}
|
||||
|
||||
func (gui *Gui) handleCheckoutCommitFile() error {
|
||||
node := gui.State.Contexts.CommitFiles.GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
gui.c.LogAction(gui.c.Tr.Actions.CheckoutFile)
|
||||
if err := gui.git.WorkingTree.CheckoutFile(gui.State.Contexts.CommitFiles.GetRefName(), node.GetPath()); err != nil {
|
||||
return gui.c.Error(err)
|
||||
}
|
||||
|
||||
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
||||
}
|
||||
|
||||
func (gui *Gui) handleDiscardOldFileChange() error {
|
||||
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
fileName := gui.getSelectedCommitFileName()
|
||||
|
||||
return gui.c.Ask(types.AskOpts{
|
||||
Title: gui.c.Tr.DiscardFileChangesTitle,
|
||||
Prompt: gui.c.Tr.DiscardFileChangesPrompt,
|
||||
HandleConfirm: func() error {
|
||||
return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error {
|
||||
gui.c.LogAction(gui.c.Tr.Actions.DiscardOldFileChange)
|
||||
if err := gui.git.Rebase.DiscardOldFileChanges(gui.State.Model.Commits, gui.State.Contexts.LocalCommits.GetSelectedLineIdx(), fileName); err != nil {
|
||||
if err := gui.helpers.MergeAndRebase.CheckMergeOrRebase(err); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return gui.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI})
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (gui *Gui) refreshCommitFilesView() error {
|
||||
currentSideContext := gui.currentSideContext()
|
||||
if currentSideContext.GetKey() == context.COMMIT_FILES_CONTEXT_KEY || currentSideContext.GetKey() == context.LOCAL_COMMITS_CONTEXT_KEY {
|
||||
if err := gui.handleRefreshPatchBuildingPanel(-1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
to := gui.State.Contexts.CommitFiles.GetRefName()
|
||||
from, reverse := gui.getFromAndReverseArgsForDiff(to)
|
||||
|
||||
files, err := gui.git.Loaders.CommitFiles.GetFilesInDiff(from, to, reverse)
|
||||
if err != nil {
|
||||
return gui.c.Error(err)
|
||||
}
|
||||
gui.State.Model.CommitFiles = files
|
||||
gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.SetTree()
|
||||
|
||||
return gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles)
|
||||
}
|
||||
|
||||
func (gui *Gui) handleOpenOldCommitFile() error {
|
||||
node := gui.State.Contexts.CommitFiles.GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return gui.helpers.Files.OpenFile(node.GetPath())
|
||||
}
|
||||
|
||||
func (gui *Gui) handleEditCommitFile() error {
|
||||
node := gui.State.Contexts.CommitFiles.GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if node.File == nil {
|
||||
return gui.c.ErrorMsg(gui.c.Tr.ErrCannotEditDirectory)
|
||||
}
|
||||
|
||||
return gui.helpers.Files.EditFile(node.GetPath())
|
||||
}
|
||||
|
||||
func (gui *Gui) handleToggleFileForPatch() error {
|
||||
node := gui.State.Contexts.CommitFiles.GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
toggleTheFile := func() error {
|
||||
if !gui.git.Patch.PatchManager.Active() {
|
||||
if err := gui.startPatchManager(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// if there is any file that hasn't been fully added we'll fully add everything,
|
||||
// otherwise we'll remove everything
|
||||
adding := node.AnyFile(func(file *models.CommitFile) bool {
|
||||
return gui.git.Patch.PatchManager.GetFileStatus(file.Name, gui.State.Contexts.CommitFiles.GetRefName()) != patch.WHOLE
|
||||
})
|
||||
|
||||
err := node.ForEachFile(func(file *models.CommitFile) error {
|
||||
if adding {
|
||||
return gui.git.Patch.PatchManager.AddFileWhole(file.Name)
|
||||
} else {
|
||||
return gui.git.Patch.PatchManager.RemoveFile(file.Name)
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return gui.c.Error(err)
|
||||
}
|
||||
|
||||
if gui.git.Patch.PatchManager.IsEmpty() {
|
||||
gui.git.Patch.PatchManager.Reset()
|
||||
}
|
||||
|
||||
return gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles)
|
||||
}
|
||||
|
||||
if gui.git.Patch.PatchManager.Active() && gui.git.Patch.PatchManager.To != gui.State.Contexts.CommitFiles.GetRefName() {
|
||||
return gui.c.Ask(types.AskOpts{
|
||||
Title: gui.c.Tr.DiscardPatch,
|
||||
Prompt: gui.c.Tr.DiscardPatchConfirm,
|
||||
HandleConfirm: func() error {
|
||||
gui.git.Patch.PatchManager.Reset()
|
||||
return toggleTheFile()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return toggleTheFile()
|
||||
}
|
||||
|
||||
func (gui *Gui) startPatchManager() error {
|
||||
commitFilesContext := gui.State.Contexts.CommitFiles
|
||||
|
||||
canRebase := commitFilesContext.GetCanRebase()
|
||||
to := commitFilesContext.GetRefName()
|
||||
|
||||
from, reverse := gui.getFromAndReverseArgsForDiff(to)
|
||||
|
||||
gui.git.Patch.PatchManager.Start(from, to, reverse, canRebase)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gui *Gui) handleEnterCommitFile() error {
|
||||
return gui.enterCommitFile(types.OnFocusOpts{ClickedViewName: "", ClickedViewLineIdx: -1})
|
||||
}
|
||||
|
||||
func (gui *Gui) enterCommitFile(opts types.OnFocusOpts) error {
|
||||
node := gui.State.Contexts.CommitFiles.GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if node.File == nil {
|
||||
return gui.handleToggleCommitFileDirCollapsed()
|
||||
}
|
||||
|
||||
enterTheFile := func() error {
|
||||
if !gui.git.Patch.PatchManager.Active() {
|
||||
if err := gui.startPatchManager(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return gui.c.PushContext(gui.State.Contexts.PatchBuilding, opts)
|
||||
}
|
||||
|
||||
if gui.git.Patch.PatchManager.Active() && gui.git.Patch.PatchManager.To != gui.State.Contexts.CommitFiles.GetRefName() {
|
||||
return gui.c.Ask(types.AskOpts{
|
||||
Title: gui.c.Tr.DiscardPatch,
|
||||
Prompt: gui.c.Tr.DiscardPatchConfirm,
|
||||
HandleConfirm: func() error {
|
||||
gui.git.Patch.PatchManager.Reset()
|
||||
return enterTheFile()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return enterTheFile()
|
||||
}
|
||||
|
||||
func (gui *Gui) handleToggleCommitFileDirCollapsed() error {
|
||||
node := gui.State.Contexts.CommitFiles.GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.ToggleCollapsed(node.GetPath())
|
||||
|
||||
if err := gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles); err != nil {
|
||||
gui.c.Log.Error(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NOTE: this is very similar to handleToggleFileTreeView, could be DRY'd with generics
|
||||
func (gui *Gui) handleToggleCommitFileTreeView() error {
|
||||
gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.ToggleShowTree()
|
||||
|
||||
return gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles)
|
||||
}
|
||||
|
||||
func (gui *Gui) SwitchToCommitFilesContext(opts controllers.SwitchToCommitFilesContextOpts) error {
|
||||
// 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.
|
||||
@ -280,3 +72,33 @@ func (gui *Gui) SwitchToCommitFilesContext(opts controllers.SwitchToCommitFilesC
|
||||
|
||||
return gui.c.PushContext(gui.State.Contexts.CommitFiles)
|
||||
}
|
||||
|
||||
func (gui *Gui) refreshCommitFilesView() error {
|
||||
currentSideContext := gui.currentSideContext()
|
||||
if currentSideContext.GetKey() == context.COMMIT_FILES_CONTEXT_KEY || currentSideContext.GetKey() == context.LOCAL_COMMITS_CONTEXT_KEY {
|
||||
if err := gui.handleRefreshPatchBuildingPanel(-1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
to := gui.State.Contexts.CommitFiles.GetRefName()
|
||||
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(to)
|
||||
|
||||
files, err := gui.git.Loaders.CommitFiles.GetFilesInDiff(from, to, reverse)
|
||||
if err != nil {
|
||||
return gui.c.Error(err)
|
||||
}
|
||||
gui.State.Model.CommitFiles = files
|
||||
gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.SetTree()
|
||||
|
||||
return gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles)
|
||||
}
|
||||
|
||||
func (gui *Gui) getSelectedCommitFileName() string {
|
||||
node := gui.State.Contexts.CommitFiles.GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return node.Path
|
||||
}
|
||||
|
259
pkg/gui/controllers/commits_files_controller.go
Normal file
259
pkg/gui/controllers/commits_files_controller.go
Normal file
@ -0,0 +1,259 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/gocui"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/patch"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
)
|
||||
|
||||
type CommitFilesController struct {
|
||||
baseController
|
||||
*controllerCommon
|
||||
}
|
||||
|
||||
var _ types.IController = &CommitFilesController{}
|
||||
|
||||
func NewCommitFilesController(
|
||||
common *controllerCommon,
|
||||
) *CommitFilesController {
|
||||
return &CommitFilesController{
|
||||
baseController: baseController{},
|
||||
controllerCommon: common,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
||||
bindings := []*types.Binding{
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.CommitFiles.CheckoutCommitFile),
|
||||
Handler: self.checkSelected(self.handleCheckoutCommitFile),
|
||||
Description: self.c.Tr.LcCheckoutCommitFile,
|
||||
},
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Universal.Remove),
|
||||
Handler: self.checkSelected(self.handleDiscardOldFileChange),
|
||||
Description: self.c.Tr.LcDiscardOldFileChange,
|
||||
},
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Universal.OpenFile),
|
||||
Handler: self.checkSelected(self.handleOpenOldCommitFile),
|
||||
Description: self.c.Tr.LcOpenFile,
|
||||
},
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Universal.Edit),
|
||||
Handler: self.checkSelected(self.handleEditCommitFile),
|
||||
Description: self.c.Tr.LcEditFile,
|
||||
},
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Universal.Select),
|
||||
Handler: self.checkSelected(self.handleToggleFileForPatch),
|
||||
Description: self.c.Tr.LcToggleAddToPatch,
|
||||
},
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Universal.GoInto),
|
||||
Handler: self.checkSelected(self.handleEnterCommitFile),
|
||||
Description: self.c.Tr.LcEnterFile,
|
||||
},
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Files.ToggleTreeView),
|
||||
Handler: self.handleToggleCommitFileTreeView,
|
||||
Description: self.c.Tr.LcToggleTreeView,
|
||||
},
|
||||
}
|
||||
|
||||
return bindings
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
|
||||
return []*gocui.ViewMouseBinding{
|
||||
{
|
||||
ViewName: "main",
|
||||
Key: gocui.MouseLeft,
|
||||
Handler: self.onClickMain,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) checkSelected(callback func(*filetree.CommitFileNode) error) func() error {
|
||||
return func() error {
|
||||
selected := self.context().GetSelectedFileNode()
|
||||
if selected == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return callback(selected)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) Context() types.Context {
|
||||
return self.context()
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) context() *context.CommitFilesContext {
|
||||
return self.contexts.CommitFiles
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) onClickMain(opts gocui.ViewMouseBindingOpts) error {
|
||||
clickedViewLineIdx := opts.Cy + opts.Oy
|
||||
node := self.context().GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
return self.enterCommitFile(node, types.OnFocusOpts{ClickedViewName: "main", ClickedViewLineIdx: clickedViewLineIdx})
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) handleCheckoutCommitFile(node *filetree.CommitFileNode) error {
|
||||
self.c.LogAction(self.c.Tr.Actions.CheckoutFile)
|
||||
if err := self.git.WorkingTree.CheckoutFile(self.context().GetRefName(), node.GetPath()); err != nil {
|
||||
return self.c.Error(err)
|
||||
}
|
||||
|
||||
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) handleDiscardOldFileChange(node *filetree.CommitFileNode) error {
|
||||
if ok, err := self.helpers.PatchBuilding.ValidateNormalWorkingTreeState(); !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
return self.c.Ask(types.AskOpts{
|
||||
Title: self.c.Tr.DiscardFileChangesTitle,
|
||||
Prompt: self.c.Tr.DiscardFileChangesPrompt,
|
||||
HandleConfirm: func() error {
|
||||
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func() error {
|
||||
self.c.LogAction(self.c.Tr.Actions.DiscardOldFileChange)
|
||||
if err := self.git.Rebase.DiscardOldFileChanges(self.model.Commits, self.contexts.LocalCommits.GetSelectedLineIdx(), node.GetPath()); err != nil {
|
||||
if err := self.helpers.MergeAndRebase.CheckMergeOrRebase(err); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return self.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI})
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) handleOpenOldCommitFile(node *filetree.CommitFileNode) error {
|
||||
return self.helpers.Files.OpenFile(node.GetPath())
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) handleEditCommitFile(node *filetree.CommitFileNode) error {
|
||||
if node.File == nil {
|
||||
return self.c.ErrorMsg(self.c.Tr.ErrCannotEditDirectory)
|
||||
}
|
||||
|
||||
return self.helpers.Files.EditFile(node.GetPath())
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) handleToggleFileForPatch(node *filetree.CommitFileNode) error {
|
||||
toggleTheFile := func() error {
|
||||
if !self.git.Patch.PatchManager.Active() {
|
||||
if err := self.startPatchManager(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// if there is any file that hasn't been fully added we'll fully add everything,
|
||||
// otherwise we'll remove everything
|
||||
adding := node.AnyFile(func(file *models.CommitFile) bool {
|
||||
return self.git.Patch.PatchManager.GetFileStatus(file.Name, self.context().GetRefName()) != patch.WHOLE
|
||||
})
|
||||
|
||||
err := node.ForEachFile(func(file *models.CommitFile) error {
|
||||
if adding {
|
||||
return self.git.Patch.PatchManager.AddFileWhole(file.Name)
|
||||
} else {
|
||||
return self.git.Patch.PatchManager.RemoveFile(file.Name)
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return self.c.Error(err)
|
||||
}
|
||||
|
||||
if self.git.Patch.PatchManager.IsEmpty() {
|
||||
self.git.Patch.PatchManager.Reset()
|
||||
}
|
||||
|
||||
return self.c.PostRefreshUpdate(self.context())
|
||||
}
|
||||
|
||||
if self.git.Patch.PatchManager.Active() && self.git.Patch.PatchManager.To != self.context().GetRefName() {
|
||||
return self.c.Ask(types.AskOpts{
|
||||
Title: self.c.Tr.DiscardPatch,
|
||||
Prompt: self.c.Tr.DiscardPatchConfirm,
|
||||
HandleConfirm: func() error {
|
||||
self.git.Patch.PatchManager.Reset()
|
||||
return toggleTheFile()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return toggleTheFile()
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) startPatchManager() error {
|
||||
commitFilesContext := self.context()
|
||||
|
||||
canRebase := commitFilesContext.GetCanRebase()
|
||||
to := commitFilesContext.GetRefName()
|
||||
|
||||
from, reverse := self.modes.Diffing.GetFromAndReverseArgsForDiff(to)
|
||||
|
||||
self.git.Patch.PatchManager.Start(from, to, reverse, canRebase)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) handleEnterCommitFile(node *filetree.CommitFileNode) error {
|
||||
return self.enterCommitFile(node, types.OnFocusOpts{ClickedViewName: "", ClickedViewLineIdx: -1})
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode, opts types.OnFocusOpts) error {
|
||||
if node.File == nil {
|
||||
return self.handleToggleCommitFileDirCollapsed(node)
|
||||
}
|
||||
|
||||
enterTheFile := func() error {
|
||||
if !self.git.Patch.PatchManager.Active() {
|
||||
if err := self.startPatchManager(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return self.c.PushContext(self.contexts.PatchBuilding, opts)
|
||||
}
|
||||
|
||||
if self.git.Patch.PatchManager.Active() && self.git.Patch.PatchManager.To != self.context().GetRefName() {
|
||||
return self.c.Ask(types.AskOpts{
|
||||
Title: self.c.Tr.DiscardPatch,
|
||||
Prompt: self.c.Tr.DiscardPatchConfirm,
|
||||
HandleConfirm: func() error {
|
||||
self.git.Patch.PatchManager.Reset()
|
||||
return enterTheFile()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return enterTheFile()
|
||||
}
|
||||
|
||||
func (self *CommitFilesController) handleToggleCommitFileDirCollapsed(node *filetree.CommitFileNode) error {
|
||||
self.context().CommitFileTreeViewModel.ToggleCollapsed(node.GetPath())
|
||||
|
||||
if err := self.c.PostRefreshUpdate(self.context()); err != nil {
|
||||
self.c.Log.Error(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NOTE: this is very similar to handleToggleFileTreeView, could be DRY'd with generics
|
||||
func (self *CommitFilesController) handleToggleCommitFileTreeView() error {
|
||||
self.context().CommitFileTreeViewModel.ToggleShowTree()
|
||||
|
||||
return self.c.PostRefreshUpdate(self.context())
|
||||
}
|
@ -10,6 +10,7 @@ type Helpers struct {
|
||||
MergeAndRebase *MergeAndRebaseHelper
|
||||
CherryPick *CherryPickHelper
|
||||
Host *HostHelper
|
||||
PatchBuilding *PatchBuildingHelper
|
||||
}
|
||||
|
||||
func NewStubHelpers() *Helpers {
|
||||
@ -23,5 +24,6 @@ func NewStubHelpers() *Helpers {
|
||||
MergeAndRebase: &MergeAndRebaseHelper{},
|
||||
CherryPick: &CherryPickHelper{},
|
||||
Host: &HostHelper{},
|
||||
PatchBuilding: &PatchBuildingHelper{},
|
||||
}
|
||||
}
|
||||
|
33
pkg/gui/controllers/helpers/patch_building_helper.go
Normal file
33
pkg/gui/controllers/helpers/patch_building_helper.go
Normal file
@ -0,0 +1,33 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
)
|
||||
|
||||
type IPatchBuildingHelper interface {
|
||||
ValidateNormalWorkingTreeState() (bool, error)
|
||||
}
|
||||
|
||||
type PatchBuildingHelper struct {
|
||||
c *types.HelperCommon
|
||||
git *commands.GitCommand
|
||||
}
|
||||
|
||||
func NewPatchBuildingHelper(
|
||||
c *types.HelperCommon,
|
||||
git *commands.GitCommand,
|
||||
) *PatchBuildingHelper {
|
||||
return &PatchBuildingHelper{
|
||||
c: c,
|
||||
git: git,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *PatchBuildingHelper) ValidateNormalWorkingTreeState() (bool, error) {
|
||||
if self.git.Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
|
||||
return false, self.c.ErrorMsg(self.c.Tr.CantPatchWhileRebasingError)
|
||||
}
|
||||
return true, nil
|
||||
}
|
@ -11,16 +11,14 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
SwitchToCommitFilesContextFn func(SwitchToCommitFilesContextOpts) error
|
||||
PullFilesFn func() error
|
||||
PullFilesFn func() error
|
||||
)
|
||||
|
||||
type LocalCommitsController struct {
|
||||
baseController
|
||||
*controllerCommon
|
||||
|
||||
pullFiles PullFilesFn
|
||||
switchToCommitFilesContext SwitchToCommitFilesContextFn
|
||||
pullFiles PullFilesFn
|
||||
}
|
||||
|
||||
var _ types.IController = &LocalCommitsController{}
|
||||
@ -28,13 +26,11 @@ var _ types.IController = &LocalCommitsController{}
|
||||
func NewLocalCommitsController(
|
||||
common *controllerCommon,
|
||||
pullFiles PullFilesFn,
|
||||
switchToCommitFilesContext SwitchToCommitFilesContextFn,
|
||||
) *LocalCommitsController {
|
||||
return &LocalCommitsController{
|
||||
baseController: baseController{},
|
||||
controllerCommon: common,
|
||||
pullFiles: pullFiles,
|
||||
switchToCommitFilesContext: switchToCommitFilesContext,
|
||||
baseController: baseController{},
|
||||
controllerCommon: common,
|
||||
pullFiles: pullFiles,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,20 +9,16 @@ import (
|
||||
type ReflogController struct {
|
||||
baseController
|
||||
*controllerCommon
|
||||
|
||||
switchToCommitFilesContext SwitchToCommitFilesContextFn
|
||||
}
|
||||
|
||||
var _ types.IController = &ReflogController{}
|
||||
|
||||
func NewReflogController(
|
||||
common *controllerCommon,
|
||||
switchToCommitFilesContext SwitchToCommitFilesContextFn,
|
||||
) *ReflogController {
|
||||
return &ReflogController{
|
||||
baseController: baseController{},
|
||||
controllerCommon: common,
|
||||
switchToCommitFilesContext: switchToCommitFilesContext,
|
||||
baseController: baseController{},
|
||||
controllerCommon: common,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,20 +9,16 @@ import (
|
||||
type SubCommitsController struct {
|
||||
baseController
|
||||
*controllerCommon
|
||||
|
||||
switchToCommitFilesContext SwitchToCommitFilesContextFn
|
||||
}
|
||||
|
||||
var _ types.IController = &SubCommitsController{}
|
||||
|
||||
func NewSubCommitsController(
|
||||
common *controllerCommon,
|
||||
switchToCommitFilesContext SwitchToCommitFilesContextFn,
|
||||
) *SubCommitsController {
|
||||
return &SubCommitsController{
|
||||
baseController: baseController{},
|
||||
controllerCommon: common,
|
||||
switchToCommitFilesContext: switchToCommitFilesContext,
|
||||
baseController: baseController{},
|
||||
controllerCommon: common,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -180,15 +180,6 @@ func (gui *Gui) handleRefresh() error {
|
||||
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
||||
}
|
||||
|
||||
func (gui *Gui) handleMouseDownMain() error {
|
||||
switch gui.currentSideContext() {
|
||||
case gui.State.Contexts.CommitFiles:
|
||||
return gui.enterCommitFile(types.OnFocusOpts{ClickedViewName: "main", ClickedViewLineIdx: gui.Views.Main.SelectedLineIdx()})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gui *Gui) backgroundFetch() (err error) {
|
||||
err = gui.git.Sync.Fetch(git_commands.FetchOptions{Background: true})
|
||||
|
||||
|
@ -497,6 +497,7 @@ func (gui *Gui) resetControllers() {
|
||||
rebaseHelper := helpers.NewMergeAndRebaseHelper(controllerCommon, gui.State.Contexts, gui.git, gui.takeOverMergeConflictScrolling, refsHelper)
|
||||
gui.helpers = &helpers.Helpers{
|
||||
Refs: refsHelper,
|
||||
PatchBuilding: helpers.NewPatchBuildingHelper(controllerCommon, gui.git),
|
||||
Bisect: helpers.NewBisectHelper(controllerCommon, gui.git),
|
||||
Suggestions: helpers.NewSuggestionsHelper(controllerCommon, model, gui.refreshSuggestions),
|
||||
Files: helpers.NewFilesHelper(controllerCommon, gui.git, osCommand),
|
||||
@ -534,8 +535,8 @@ func (gui *Gui) resetControllers() {
|
||||
|
||||
bisectController := controllers.NewBisectController(common)
|
||||
|
||||
reflogController := controllers.NewReflogController(common, gui.SwitchToCommitFilesContext)
|
||||
subCommitsController := controllers.NewSubCommitsController(common, gui.SwitchToCommitFilesContext)
|
||||
reflogController := controllers.NewReflogController(common)
|
||||
subCommitsController := controllers.NewSubCommitsController(common)
|
||||
|
||||
gui.Controllers = Controllers{
|
||||
Submodules: submodulesController,
|
||||
@ -548,12 +549,8 @@ func (gui *Gui) resetControllers() {
|
||||
func() string { return gui.State.failedCommitMessage },
|
||||
gui.switchToMerge,
|
||||
),
|
||||
Tags: controllers.NewTagsController(common),
|
||||
LocalCommits: controllers.NewLocalCommitsController(
|
||||
common,
|
||||
syncController.HandlePull,
|
||||
gui.SwitchToCommitFilesContext,
|
||||
),
|
||||
Tags: controllers.NewTagsController(common),
|
||||
LocalCommits: controllers.NewLocalCommitsController(common, syncController.HandlePull),
|
||||
Remotes: controllers.NewRemotesController(
|
||||
common,
|
||||
func(branches []*models.RemoteBranch) { gui.State.Model.RemoteBranches = branches },
|
||||
@ -567,6 +564,7 @@ func (gui *Gui) resetControllers() {
|
||||
gitFlowController := controllers.NewGitFlowController(common)
|
||||
filesRemoveController := controllers.NewFilesRemoveController(common)
|
||||
stashController := controllers.NewStashController(common)
|
||||
commitFilesController := controllers.NewCommitFilesController(common)
|
||||
|
||||
switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory(
|
||||
common,
|
||||
@ -602,6 +600,7 @@ func (gui *Gui) resetControllers() {
|
||||
controllers.AttachControllers(gui.State.Contexts.LocalCommits, gui.Controllers.LocalCommits, bisectController)
|
||||
controllers.AttachControllers(gui.State.Contexts.ReflogCommits, reflogController)
|
||||
controllers.AttachControllers(gui.State.Contexts.SubCommits, subCommitsController)
|
||||
controllers.AttachControllers(gui.State.Contexts.CommitFiles, commitFilesController)
|
||||
controllers.AttachControllers(gui.State.Contexts.Remotes, gui.Controllers.Remotes)
|
||||
controllers.AttachControllers(gui.State.Contexts.Stash, stashController)
|
||||
controllers.AttachControllers(gui.State.Contexts.Menu, gui.Controllers.Menu)
|
||||
|
@ -472,48 +472,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
|
||||
Handler: self.handleCopySelectedSideContextItemToClipboard,
|
||||
Description: self.c.Tr.LcCopyCommitFileNameToClipboard,
|
||||
},
|
||||
{
|
||||
ViewName: "commitFiles",
|
||||
Key: opts.GetKey(opts.Config.CommitFiles.CheckoutCommitFile),
|
||||
Handler: self.handleCheckoutCommitFile,
|
||||
Description: self.c.Tr.LcCheckoutCommitFile,
|
||||
},
|
||||
{
|
||||
ViewName: "commitFiles",
|
||||
Key: opts.GetKey(opts.Config.Universal.Remove),
|
||||
Handler: self.handleDiscardOldFileChange,
|
||||
Description: self.c.Tr.LcDiscardOldFileChange,
|
||||
},
|
||||
{
|
||||
ViewName: "commitFiles",
|
||||
Key: opts.GetKey(opts.Config.Universal.OpenFile),
|
||||
Handler: self.handleOpenOldCommitFile,
|
||||
Description: self.c.Tr.LcOpenFile,
|
||||
},
|
||||
{
|
||||
ViewName: "commitFiles",
|
||||
Key: opts.GetKey(opts.Config.Universal.Edit),
|
||||
Handler: self.handleEditCommitFile,
|
||||
Description: self.c.Tr.LcEditFile,
|
||||
},
|
||||
{
|
||||
ViewName: "commitFiles",
|
||||
Key: opts.GetKey(opts.Config.Universal.Select),
|
||||
Handler: self.handleToggleFileForPatch,
|
||||
Description: self.c.Tr.LcToggleAddToPatch,
|
||||
},
|
||||
{
|
||||
ViewName: "commitFiles",
|
||||
Key: opts.GetKey(opts.Config.Universal.GoInto),
|
||||
Handler: self.handleEnterCommitFile,
|
||||
Description: self.c.Tr.LcEnterFile,
|
||||
},
|
||||
{
|
||||
ViewName: "commitFiles",
|
||||
Key: opts.GetKey(opts.Config.Files.ToggleTreeView),
|
||||
Handler: self.handleToggleCommitFileTreeView,
|
||||
Description: self.c.Tr.LcToggleTreeView,
|
||||
},
|
||||
{
|
||||
ViewName: "",
|
||||
Key: opts.GetKey(opts.Config.Universal.FilteringMenu),
|
||||
@ -570,13 +528,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
|
||||
Description: self.c.Tr.ScrollUp,
|
||||
Alternative: "fn+down",
|
||||
},
|
||||
{
|
||||
ViewName: "main",
|
||||
Contexts: []string{string(context.MAIN_NORMAL_CONTEXT_KEY)},
|
||||
Key: gocui.MouseLeft,
|
||||
Modifier: gocui.ModNone,
|
||||
Handler: self.handleMouseDownMain,
|
||||
},
|
||||
{
|
||||
ViewName: "secondary",
|
||||
Contexts: []string{string(context.MAIN_STAGING_CONTEXT_KEY)},
|
||||
|
@ -120,15 +120,6 @@ func (gui *Gui) handleMouseDrag() error {
|
||||
})
|
||||
}
|
||||
|
||||
func (gui *Gui) getSelectedCommitFileName() string {
|
||||
node := gui.State.Contexts.CommitFiles.GetSelectedFileNode()
|
||||
if node == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return node.Path
|
||||
}
|
||||
|
||||
func (gui *Gui) refreshMainViewForLineByLine(state *LblPanelState) error {
|
||||
var includedLineIndices []int
|
||||
// I'd prefer not to have knowledge of contexts using this file but I'm not sure
|
||||
|
@ -10,6 +10,20 @@ func New() Diffing {
|
||||
return Diffing{}
|
||||
}
|
||||
|
||||
func (m *Diffing) Active() bool {
|
||||
return m.Ref != ""
|
||||
func (self *Diffing) Active() bool {
|
||||
return self.Ref != ""
|
||||
}
|
||||
|
||||
// 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 (self *Diffing) GetFromAndReverseArgsForDiff(to string) (string, bool) {
|
||||
from := to + "^"
|
||||
reverse := false
|
||||
|
||||
if self.Active() {
|
||||
reverse = self.Reverse
|
||||
from = self.Ref
|
||||
}
|
||||
|
||||
return from, reverse
|
||||
}
|
||||
|
@ -4,19 +4,6 @@ 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
|
||||
}
|
||||
|
||||
func (gui *Gui) refreshPatchBuildingPanel(selectedLineIdx int) error {
|
||||
if !gui.git.Patch.PatchManager.Active() {
|
||||
return gui.handleEscapePatchBuildingPanel()
|
||||
@ -32,7 +19,7 @@ func (gui *Gui) refreshPatchBuildingPanel(selectedLineIdx int) error {
|
||||
}
|
||||
|
||||
to := gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.GetRefName()
|
||||
from, reverse := gui.getFromAndReverseArgsForDiff(to)
|
||||
from, reverse := gui.State.Modes.Diffing.GetFromAndReverseArgsForDiff(to)
|
||||
diff, err := gui.git.WorkingTree.ShowFileDiff(from, to, reverse, node.GetPath(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user