2022-02-22 11:13:11 +02:00
|
|
|
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
|
2023-03-23 09:47:29 +02:00
|
|
|
c *ControllerCommon
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.IController = &CommitFilesController{}
|
|
|
|
|
|
|
|
func NewCommitFilesController(
|
2023-03-23 09:47:29 +02:00
|
|
|
common *ControllerCommon,
|
2022-02-22 11:13:11 +02:00
|
|
|
) *CommitFilesController {
|
|
|
|
return &CommitFilesController{
|
2023-03-23 09:47:29 +02:00
|
|
|
baseController: baseController{},
|
|
|
|
c: common,
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
|
|
bindings := []*types.Binding{
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.CommitFiles.CheckoutCommitFile),
|
2022-02-22 12:21:35 +02:00
|
|
|
Handler: self.checkSelected(self.checkout),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.CheckoutCommitFile,
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.Remove),
|
2022-02-22 12:21:35 +02:00
|
|
|
Handler: self.checkSelected(self.discard),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.DiscardOldFileChange,
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.OpenFile),
|
2022-02-22 12:21:35 +02:00
|
|
|
Handler: self.checkSelected(self.open),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.OpenFile,
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.Edit),
|
2022-02-22 12:21:35 +02:00
|
|
|
Handler: self.checkSelected(self.edit),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.EditFile,
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.Select),
|
2022-02-22 12:21:35 +02:00
|
|
|
Handler: self.checkSelected(self.toggleForPatch),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.ToggleAddToPatch,
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
2022-03-23 14:15:54 +02:00
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Files.ToggleStagedAll),
|
|
|
|
Handler: self.checkSelected(self.toggleAllForPatch),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.ToggleAllInPatch,
|
2022-03-23 14:15:54 +02:00
|
|
|
},
|
2022-02-22 11:13:11 +02:00
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.GoInto),
|
2022-02-22 12:21:35 +02:00
|
|
|
Handler: self.checkSelected(self.enter),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.EnterFile,
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Files.ToggleTreeView),
|
2022-02-22 12:21:35 +02:00
|
|
|
Handler: self.toggleTreeView,
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.ToggleTreeView,
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFilesController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
|
|
|
|
return []*gocui.ViewMouseBinding{
|
|
|
|
{
|
2022-06-13 03:01:26 +02:00
|
|
|
ViewName: "patchBuilding",
|
2022-02-27 02:42:22 +02:00
|
|
|
Key: gocui.MouseLeft,
|
|
|
|
Handler: self.onClickMain,
|
2022-06-13 03:01:26 +02:00
|
|
|
FocusedView: self.context().GetViewName(),
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFilesController) checkSelected(callback func(*filetree.CommitFileNode) error) func() error {
|
|
|
|
return func() error {
|
2022-03-19 00:31:52 +02:00
|
|
|
selected := self.context().GetSelected()
|
2022-02-22 11:13:11 +02:00
|
|
|
if selected == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(selected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFilesController) Context() types.Context {
|
|
|
|
return self.context()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFilesController) context() *context.CommitFilesContext {
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.Contexts().CommitFiles
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 06:36:25 +02:00
|
|
|
func (self *CommitFilesController) GetOnRenderToMain() func() error {
|
|
|
|
return func() error {
|
|
|
|
node := self.context().GetSelected()
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ref := self.context().GetRef()
|
|
|
|
to := ref.RefName()
|
|
|
|
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
|
|
|
|
|
|
|
|
cmdObj := self.c.Git().WorkingTree.ShowFileDiffCmdObj(
|
2023-07-26 11:29:43 +02:00
|
|
|
from, to, reverse, node.GetPath(), false, self.c.GetAppState().IgnoreWhitespaceInDiffView,
|
2023-04-15 06:36:25 +02:00
|
|
|
)
|
|
|
|
task := types.NewRunPtyTask(cmdObj.GetCmd())
|
|
|
|
|
|
|
|
pair := self.c.MainViewPairs().Normal
|
|
|
|
if node.File != nil {
|
|
|
|
pair = self.c.MainViewPairs().PatchBuilding
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.RenderToMainViews(types.RefreshMainOpts{
|
|
|
|
Pair: pair,
|
|
|
|
Main: &types.ViewUpdateOpts{
|
2023-05-18 15:29:37 +02:00
|
|
|
Title: self.c.Tr.Patch,
|
|
|
|
SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
|
|
|
|
Task: task,
|
2023-04-15 06:36:25 +02:00
|
|
|
},
|
|
|
|
Secondary: secondaryPatchPanelUpdateOpts(self.c),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:13:11 +02:00
|
|
|
func (self *CommitFilesController) onClickMain(opts gocui.ViewMouseBindingOpts) error {
|
2022-03-19 00:31:52 +02:00
|
|
|
node := self.context().GetSelected()
|
2022-02-22 11:13:11 +02:00
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-13 03:01:26 +02:00
|
|
|
return self.enterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "main", ClickedViewLineIdx: opts.Y})
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 12:21:35 +02:00
|
|
|
func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error {
|
2022-02-22 11:13:11 +02:00
|
|
|
self.c.LogAction(self.c.Tr.Actions.CheckoutFile)
|
2023-03-23 04:04:57 +02:00
|
|
|
if err := self.c.Git().WorkingTree.CheckoutFile(self.context().GetRef().RefName(), node.GetPath()); err != nil {
|
2022-02-22 11:13:11 +02:00
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:21:35 +02:00
|
|
|
func (self *CommitFilesController) discard(node *filetree.CommitFileNode) error {
|
2023-06-06 08:30:44 +02:00
|
|
|
parentContext, ok := self.c.CurrentContext().GetParentContext()
|
|
|
|
if !ok || parentContext.GetKey() != context.LOCAL_COMMITS_CONTEXT_KEY {
|
|
|
|
return self.c.ErrorMsg(self.c.Tr.CanOnlyDiscardFromLocalCommits)
|
|
|
|
}
|
|
|
|
|
2023-03-12 17:52:02 +02:00
|
|
|
if node.File == nil {
|
|
|
|
return self.c.ErrorMsg(self.c.Tr.DiscardNotSupportedForDirectory)
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:47:29 +02:00
|
|
|
if ok, err := self.c.Helpers().PatchBuilding.ValidateNormalWorkingTreeState(); !ok {
|
2022-02-22 11:13:11 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-12 17:45:46 +02:00
|
|
|
prompt := self.c.Tr.DiscardFileChangesPrompt
|
|
|
|
if node.File.Added() {
|
|
|
|
prompt = self.c.Tr.DiscardAddedFileChangesPrompt
|
|
|
|
} else if node.File.Deleted() {
|
|
|
|
prompt = self.c.Tr.DiscardDeletedFileChangesPrompt
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-02-22 11:13:11 +02:00
|
|
|
Title: self.c.Tr.DiscardFileChangesTitle,
|
2023-03-12 17:45:46 +02:00
|
|
|
Prompt: prompt,
|
2022-02-22 11:13:11 +02:00
|
|
|
HandleConfirm: func() error {
|
2023-07-09 13:09:52 +02:00
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error {
|
2022-02-22 11:13:11 +02:00
|
|
|
self.c.LogAction(self.c.Tr.Actions.DiscardOldFileChange)
|
2023-03-23 04:04:57 +02:00
|
|
|
if err := self.c.Git().Rebase.DiscardOldFileChanges(self.c.Model().Commits, self.c.Contexts().LocalCommits.GetSelectedLineIdx(), node.GetPath()); err != nil {
|
2023-03-23 09:47:29 +02:00
|
|
|
if err := self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err); err != nil {
|
2022-02-22 11:13:11 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:21:35 +02:00
|
|
|
func (self *CommitFilesController) open(node *filetree.CommitFileNode) error {
|
2023-03-23 09:47:29 +02:00
|
|
|
return self.c.Helpers().Files.OpenFile(node.GetPath())
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 12:21:35 +02:00
|
|
|
func (self *CommitFilesController) edit(node *filetree.CommitFileNode) error {
|
2022-02-22 11:13:11 +02:00
|
|
|
if node.File == nil {
|
|
|
|
return self.c.ErrorMsg(self.c.Tr.ErrCannotEditDirectory)
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:47:29 +02:00
|
|
|
return self.c.Helpers().Files.EditFile(node.GetPath())
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 12:21:35 +02:00
|
|
|
func (self *CommitFilesController) toggleForPatch(node *filetree.CommitFileNode) error {
|
2022-03-23 14:15:54 +02:00
|
|
|
toggle := func() error {
|
2023-07-09 13:09:52 +02:00
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.UpdatingPatch, func(gocui.Task) error {
|
2023-03-23 04:04:57 +02:00
|
|
|
if !self.c.Git().Patch.PatchBuilder.Active() {
|
2023-03-19 07:09:03 +02:00
|
|
|
if err := self.startPatchBuilder(); err != nil {
|
2022-03-23 14:15:54 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 14:15:54 +02:00
|
|
|
// if there is any file that hasn't been fully added we'll fully add everything,
|
|
|
|
// otherwise we'll remove everything
|
2022-07-31 11:10:16 +02:00
|
|
|
adding := node.SomeFile(func(file *models.CommitFile) bool {
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.Git().Patch.PatchBuilder.GetFileStatus(file.Name, self.context().GetRef().RefName()) != patch.WHOLE
|
2022-03-23 14:15:54 +02:00
|
|
|
})
|
2022-02-22 11:13:11 +02:00
|
|
|
|
2022-03-23 14:15:54 +02:00
|
|
|
err := node.ForEachFile(func(file *models.CommitFile) error {
|
|
|
|
if adding {
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.Git().Patch.PatchBuilder.AddFileWhole(file.Name)
|
2022-03-23 14:15:54 +02:00
|
|
|
} else {
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.Git().Patch.PatchBuilder.RemoveFile(file.Name)
|
2022-03-23 14:15:54 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
if self.c.Git().Patch.PatchBuilder.IsEmpty() {
|
|
|
|
self.c.Git().Patch.PatchBuilder.Reset()
|
2022-03-23 14:15:54 +02:00
|
|
|
}
|
2022-02-22 11:13:11 +02:00
|
|
|
|
2022-03-23 14:15:54 +02:00
|
|
|
return self.c.PostRefreshUpdate(self.context())
|
|
|
|
})
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.To != self.context().GetRef().RefName() {
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-02-22 11:13:11 +02:00
|
|
|
Title: self.c.Tr.DiscardPatch,
|
|
|
|
Prompt: self.c.Tr.DiscardPatchConfirm,
|
|
|
|
HandleConfirm: func() error {
|
2023-03-23 04:04:57 +02:00
|
|
|
self.c.Git().Patch.PatchBuilder.Reset()
|
2022-03-23 14:15:54 +02:00
|
|
|
return toggle()
|
2022-02-22 11:13:11 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-23 14:15:54 +02:00
|
|
|
return toggle()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFilesController) toggleAllForPatch(_ *filetree.CommitFileNode) error {
|
2022-07-31 11:10:16 +02:00
|
|
|
root := self.context().CommitFileTreeViewModel.GetRoot()
|
2022-03-23 14:15:54 +02:00
|
|
|
return self.toggleForPatch(root)
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2023-03-19 07:09:03 +02:00
|
|
|
func (self *CommitFilesController) startPatchBuilder() error {
|
2022-02-22 11:13:11 +02:00
|
|
|
commitFilesContext := self.context()
|
|
|
|
|
|
|
|
canRebase := commitFilesContext.GetCanRebase()
|
2022-03-26 15:18:08 +02:00
|
|
|
ref := commitFilesContext.GetRef()
|
|
|
|
to := ref.RefName()
|
2023-03-23 04:04:57 +02:00
|
|
|
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
|
2022-02-22 11:13:11 +02:00
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
self.c.Git().Patch.PatchBuilder.Start(from, to, reverse, canRebase)
|
2022-02-22 11:13:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:21:35 +02:00
|
|
|
func (self *CommitFilesController) enter(node *filetree.CommitFileNode) error {
|
2022-06-13 03:01:26 +02:00
|
|
|
return self.enterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "", ClickedViewLineIdx: -1})
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode, opts types.OnFocusOpts) error {
|
|
|
|
if node.File == nil {
|
|
|
|
return self.handleToggleCommitFileDirCollapsed(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
enterTheFile := func() error {
|
2023-03-23 04:04:57 +02:00
|
|
|
if !self.c.Git().Patch.PatchBuilder.Active() {
|
2023-03-19 07:09:03 +02:00
|
|
|
if err := self.startPatchBuilder(); err != nil {
|
2022-02-22 11:13:11 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.PushContext(self.c.Contexts().CustomPatchBuilder, opts)
|
2022-02-22 11:13:11 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.To != self.context().GetRef().RefName() {
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-02-22 11:13:11 +02:00
|
|
|
Title: self.c.Tr.DiscardPatch,
|
|
|
|
Prompt: self.c.Tr.DiscardPatchConfirm,
|
|
|
|
HandleConfirm: func() error {
|
2023-03-23 04:04:57 +02:00
|
|
|
self.c.Git().Patch.PatchBuilder.Reset()
|
2022-02-22 11:13:11 +02:00
|
|
|
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
|
2022-02-22 12:21:35 +02:00
|
|
|
func (self *CommitFilesController) toggleTreeView() error {
|
2022-02-22 11:13:11 +02:00
|
|
|
self.context().CommitFileTreeViewModel.ToggleShowTree()
|
|
|
|
|
|
|
|
return self.c.PostRefreshUpdate(self.context())
|
|
|
|
}
|