2019-03-09 16:42:10 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2021-03-31 13:39:55 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/patch"
|
2022-01-29 10:15:46 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
2022-01-16 05:46:53 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/controllers"
|
2021-03-31 13:08:55 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
|
2022-01-28 11:44:36 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2019-03-09 16:42:10 +02:00
|
|
|
)
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (gui *Gui) getSelectedCommitFileNode() *filetree.CommitFileNode {
|
2020-08-20 00:53:10 +02:00
|
|
|
selectedLine := gui.State.Panels.CommitFiles.SelectedLineIdx
|
2022-01-21 15:13:51 +02:00
|
|
|
if selectedLine == -1 || selectedLine > gui.State.CommitFileTreeViewModel.GetItemsLength()-1 {
|
2019-03-09 16:42:10 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-21 15:13:51 +02:00
|
|
|
return gui.State.CommitFileTreeViewModel.GetItemAtIndex(selectedLine)
|
2021-03-31 13:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) getSelectedCommitFile() *models.CommitFile {
|
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return node.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) getSelectedCommitFilePath() string {
|
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return node.GetPath()
|
2019-03-09 16:42:10 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 03:48:49 +02:00
|
|
|
func (gui *Gui) onCommitFileFocus() error {
|
2020-10-01 23:56:14 +02:00
|
|
|
gui.escapeLineByLinePanel()
|
2021-11-21 03:48:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-10 07:20:35 +02:00
|
|
|
|
2021-11-21 03:48:49 +02:00
|
|
|
func (gui *Gui) commitFilesRenderToMain() error {
|
2021-03-31 13:08:55 +02:00
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
2020-03-09 02:34:10 +02:00
|
|
|
return nil
|
2019-03-09 16:42:10 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 15:13:51 +02:00
|
|
|
to := gui.State.CommitFileTreeViewModel.GetParent()
|
2020-08-22 10:29:09 +02:00
|
|
|
from, reverse := gui.getFromAndReverseArgsForDiff(to)
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
cmdObj := gui.git.WorkingTree.ShowFileDiffCmdObj(from, to, reverse, node.GetPath(), false)
|
2021-12-07 12:59:36 +02:00
|
|
|
task := NewRunPtyTask(cmdObj.GetCmd())
|
2020-08-18 14:02:35 +02:00
|
|
|
|
2020-08-23 01:46:28 +02:00
|
|
|
return gui.refreshMainViews(refreshMainOpts{
|
2020-08-18 14:02:35 +02:00
|
|
|
main: &viewUpdateOpts{
|
|
|
|
title: "Patch",
|
|
|
|
task: task,
|
|
|
|
},
|
|
|
|
secondary: gui.secondaryPatchPanelUpdateOpts(),
|
|
|
|
})
|
2019-03-09 16:42:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleCheckoutCommitFile() error {
|
2021-03-31 13:08:55 +02:00
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
2020-08-23 06:43:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-11 00:53:46 +02:00
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.c.LogAction(gui.c.Tr.Actions.CheckoutFile)
|
|
|
|
if err := gui.git.WorkingTree.CheckoutFile(gui.State.CommitFileTreeViewModel.GetParent(), node.GetPath()); err != nil {
|
|
|
|
return gui.c.Error(err)
|
2019-03-11 00:53:46 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
2019-03-11 00:53:46 +02:00
|
|
|
}
|
2019-03-11 04:04:08 +02:00
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleDiscardOldFileChange() error {
|
2019-11-05 02:53:01 +02:00
|
|
|
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-31 13:08:55 +02:00
|
|
|
fileName := gui.getSelectedCommitFileName()
|
2019-03-11 04:04:08 +02:00
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
return gui.c.Ask(types.AskOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: gui.c.Tr.DiscardFileChangesTitle,
|
|
|
|
Prompt: gui.c.Tr.DiscardFileChangesPrompt,
|
2022-01-28 11:44:36 +02:00
|
|
|
HandleConfirm: func() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
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.Commits, gui.State.Panels.Commits.SelectedLineIdx, fileName); err != nil {
|
|
|
|
if err := gui.checkMergeOrRebase(err); err != nil {
|
2020-08-15 08:36:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-03-11 04:04:08 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI})
|
2020-08-15 08:36:39 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
2019-03-11 04:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) refreshCommitFilesView() error {
|
2021-04-06 08:55:06 +02:00
|
|
|
currentSideContext := gui.currentSideContext()
|
2022-01-29 10:15:46 +02:00
|
|
|
if currentSideContext.GetKey() == context.COMMIT_FILES_CONTEXT_KEY || currentSideContext.GetKey() == context.BRANCH_COMMITS_CONTEXT_KEY {
|
2021-04-06 06:21:17 +02:00
|
|
|
if err := gui.handleRefreshPatchBuildingPanel(-1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-05 08:57:59 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 10:50:37 +02:00
|
|
|
to := gui.State.Panels.CommitFiles.refName
|
|
|
|
from, reverse := gui.getFromAndReverseArgsForDiff(to)
|
2020-08-22 05:03:20 +02:00
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
files, err := gui.git.Loaders.CommitFiles.GetFilesInDiff(from, to, reverse)
|
2019-03-11 04:04:08 +02:00
|
|
|
if err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.Error(err)
|
2019-03-11 04:04:08 +02:00
|
|
|
}
|
2022-01-21 15:13:51 +02:00
|
|
|
gui.State.CommitFileTreeViewModel.SetParent(to)
|
|
|
|
gui.State.CommitFileTreeViewModel.SetFiles(files)
|
2019-03-11 04:04:08 +02:00
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles)
|
2020-08-19 10:41:57 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleOpenOldCommitFile() error {
|
2021-03-31 13:08:55 +02:00
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
2020-07-21 10:12:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 01:23:39 +02:00
|
|
|
return gui.filesHelper.OpenFile(node.GetPath())
|
2019-03-15 04:29:27 +02:00
|
|
|
}
|
2019-11-04 10:47:25 +02:00
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleEditCommitFile() error {
|
2021-03-31 13:08:55 +02:00
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
2020-07-21 10:12:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:05:16 +02:00
|
|
|
if node.File == nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.ErrorMsg(gui.c.Tr.ErrCannotEditDirectory)
|
2021-03-31 14:05:16 +02:00
|
|
|
}
|
|
|
|
|
2022-01-30 01:23:39 +02:00
|
|
|
return gui.filesHelper.EditFile(node.GetPath())
|
2020-07-21 10:12:05 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleToggleFileForPatch() error {
|
2021-03-31 13:08:55 +02:00
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
2020-03-09 02:34:10 +02:00
|
|
|
return nil
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleTheFile := func() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
if !gui.git.Patch.PatchManager.Active() {
|
2019-11-05 09:10:47 +02:00
|
|
|
if err := gui.startPatchManager(); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 13:39:55 +02:00
|
|
|
// 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 {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.git.Patch.PatchManager.GetFileStatus(file.Name, gui.State.CommitFileTreeViewModel.GetParent()) != patch.WHOLE
|
2021-03-31 13:39:55 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
err := node.ForEachFile(func(file *models.CommitFile) error {
|
|
|
|
if adding {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.git.Patch.PatchManager.AddFileWhole(file.Name)
|
2021-03-31 13:39:55 +02:00
|
|
|
} else {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.git.Patch.PatchManager.RemoveFile(file.Name)
|
2021-03-31 13:39:55 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.Error(err)
|
2020-08-22 08:46:19 +02:00
|
|
|
}
|
2019-11-04 10:47:25 +02:00
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
if gui.git.Patch.PatchManager.IsEmpty() {
|
|
|
|
gui.git.Patch.PatchManager.Reset()
|
2020-08-22 01:22:22 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles)
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
if gui.git.Patch.PatchManager.Active() && gui.git.Patch.PatchManager.To != gui.State.CommitFileTreeViewModel.GetParent() {
|
2022-01-29 10:09:20 +02:00
|
|
|
return gui.c.Ask(types.AskOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: gui.c.Tr.DiscardPatch,
|
|
|
|
Prompt: gui.c.Tr.DiscardPatchConfirm,
|
2022-01-28 11:44:36 +02:00
|
|
|
HandleConfirm: func() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.git.Patch.PatchManager.Reset()
|
2020-08-15 08:36:39 +02:00
|
|
|
return toggleTheFile()
|
|
|
|
},
|
|
|
|
})
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return toggleTheFile()
|
|
|
|
}
|
|
|
|
|
2019-11-05 09:10:47 +02:00
|
|
|
func (gui *Gui) startPatchManager() error {
|
2020-08-22 10:50:37 +02:00
|
|
|
canRebase := gui.State.Panels.CommitFiles.canRebase
|
2020-08-22 10:29:09 +02:00
|
|
|
|
|
|
|
to := gui.State.Panels.CommitFiles.refName
|
|
|
|
from, reverse := gui.getFromAndReverseArgsForDiff(to)
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.git.Patch.PatchManager.Start(from, to, reverse, canRebase)
|
2019-11-04 10:47:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleEnterCommitFile() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.enterCommitFile(types.OnFocusOpts{ClickedViewName: "", ClickedViewLineIdx: -1})
|
2019-11-10 07:20:35 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
func (gui *Gui) enterCommitFile(opts types.OnFocusOpts) error {
|
2021-03-31 13:08:55 +02:00
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
2020-03-09 02:34:10 +02:00
|
|
|
return nil
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 13:08:55 +02:00
|
|
|
if node.File == nil {
|
|
|
|
return gui.handleToggleCommitFileDirCollapsed()
|
|
|
|
}
|
|
|
|
|
2021-11-21 03:48:49 +02:00
|
|
|
enterTheFile := func() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
if !gui.git.Patch.PatchManager.Active() {
|
2019-11-05 09:10:47 +02:00
|
|
|
if err := gui.startPatchManager(); err != nil {
|
2019-11-04 10:47:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.PushContext(gui.State.Contexts.PatchBuilding, opts)
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
if gui.git.Patch.PatchManager.Active() && gui.git.Patch.PatchManager.To != gui.State.CommitFileTreeViewModel.GetParent() {
|
2022-01-29 10:09:20 +02:00
|
|
|
return gui.c.Ask(types.AskOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: gui.c.Tr.DiscardPatch,
|
|
|
|
Prompt: gui.c.Tr.DiscardPatchConfirm,
|
2022-01-28 11:44:36 +02:00
|
|
|
HandleConfirm: func() error {
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.git.Patch.PatchManager.Reset()
|
2021-11-21 03:48:49 +02:00
|
|
|
return enterTheFile()
|
2020-08-15 08:36:39 +02:00
|
|
|
},
|
2020-03-28 03:52:45 +02:00
|
|
|
})
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 03:48:49 +02:00
|
|
|
return enterTheFile()
|
2019-11-04 10:47:25 +02:00
|
|
|
}
|
2020-08-21 11:53:45 +02:00
|
|
|
|
2021-03-31 13:08:55 +02:00
|
|
|
func (gui *Gui) handleToggleCommitFileDirCollapsed() error {
|
|
|
|
node := gui.getSelectedCommitFileNode()
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-21 15:13:51 +02:00
|
|
|
gui.State.CommitFileTreeViewModel.ToggleCollapsed(node.GetPath())
|
2021-03-31 13:08:55 +02:00
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
if err := gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles); err != nil {
|
|
|
|
gui.c.Log.Error(err)
|
2021-03-31 13:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
func (gui *Gui) SwitchToCommitFilesContext(opts controllers.SwitchToCommitFilesContextOpts) error {
|
2020-08-21 11:53:45 +02:00
|
|
|
// 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.
|
2021-04-04 17:10:23 +02:00
|
|
|
gui.resetWindowForView(gui.Views.CommitFiles)
|
2020-08-21 11:53:45 +02:00
|
|
|
|
|
|
|
gui.State.Panels.CommitFiles.SelectedLineIdx = 0
|
2022-01-16 05:46:53 +02:00
|
|
|
gui.State.Panels.CommitFiles.refName = opts.RefName
|
|
|
|
gui.State.Panels.CommitFiles.canRebase = opts.CanRebase
|
|
|
|
gui.State.Contexts.CommitFiles.SetParentContext(opts.Context)
|
|
|
|
gui.State.Contexts.CommitFiles.SetWindowName(opts.WindowName)
|
2020-08-21 11:53:45 +02:00
|
|
|
|
|
|
|
if err := gui.refreshCommitFilesView(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.PushContext(gui.State.Contexts.CommitFiles)
|
2020-08-21 11:53:45 +02:00
|
|
|
}
|
2021-03-31 14:20:36 +02:00
|
|
|
|
|
|
|
// NOTE: this is very similar to handleToggleFileTreeView, could be DRY'd with generics
|
|
|
|
func (gui *Gui) handleToggleCommitFileTreeView() error {
|
|
|
|
path := gui.getSelectedCommitFilePath()
|
|
|
|
|
2022-01-21 15:13:51 +02:00
|
|
|
gui.State.CommitFileTreeViewModel.ToggleShowTree()
|
2021-03-31 14:20:36 +02:00
|
|
|
|
|
|
|
// find that same node in the new format and move the cursor to it
|
|
|
|
if path != "" {
|
2022-01-21 15:13:51 +02:00
|
|
|
gui.State.CommitFileTreeViewModel.ExpandToPath(path)
|
|
|
|
index, found := gui.State.CommitFileTreeViewModel.GetIndexForPath(path)
|
2021-03-31 14:20:36 +02:00
|
|
|
if found {
|
2021-04-03 06:56:11 +02:00
|
|
|
gui.State.Contexts.CommitFiles.GetPanelState().SetSelectedLineIdx(index)
|
2021-03-31 14:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.PostRefreshUpdate(gui.State.Contexts.CommitFiles)
|
2021-03-31 14:20:36 +02:00
|
|
|
}
|