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

299 lines
7.9 KiB
Go
Raw Normal View History

package gui
import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
2021-03-31 13:39:55 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/patch"
2021-03-31 13:08:55 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
)
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 {
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()
}
func (gui *Gui) onCommitFileFocus() error {
gui.escapeLineByLinePanel()
return nil
}
2019-11-10 07:20:35 +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
}
2022-01-21 15:13:51 +02:00
to := gui.State.CommitFileTreeViewModel.GetParent()
from, reverse := gui.getFromAndReverseArgsForDiff(to)
2022-01-08 05:10:01 +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(),
})
}
func (gui *Gui) handleCheckoutCommitFile() error {
2021-03-31 13:08:55 +02:00
node := gui.getSelectedCommitFileNode()
if node == nil {
return nil
}
2019-03-11 00:53:46 +02:00
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.CheckoutFile)
2022-01-21 15:13:51 +02:00
if err := gui.Git.WorkingTree.CheckoutFile(gui.State.CommitFileTreeViewModel.GetParent(), node.GetPath()); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
2019-03-11 00:53:46 +02:00
}
2020-03-28 00:57:36 +02:00
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
2019-03-11 00:53:46 +02:00
}
func (gui *Gui) handleDiscardOldFileChange() error {
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
return err
}
2021-03-31 13:08:55 +02:00
fileName := gui.getSelectedCommitFileName()
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.DiscardFileChangesTitle,
prompt: gui.Tr.DiscardFileChangesPrompt,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
2020-10-04 02:00:48 +02:00
return gui.WithWaitingStatus(gui.Tr.RebasingStatus, func() error {
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.DiscardOldFileChange)
2022-01-08 05:10:01 +02:00
if err := gui.Git.Rebase.DiscardOldFileChanges(gui.State.Commits, gui.State.Panels.Commits.SelectedLineIdx, fileName); err != nil {
2020-08-15 08:36:39 +02:00
if err := gui.handleGenericMergeCommandResult(err); err != nil {
return err
}
}
2020-08-15 08:36:39 +02:00
return gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI})
})
},
})
}
func (gui *Gui) refreshCommitFilesView() error {
currentSideContext := gui.currentSideContext()
if currentSideContext.GetKey() == COMMIT_FILES_CONTEXT_KEY || currentSideContext.GetKey() == BRANCH_COMMITS_CONTEXT_KEY {
if err := gui.handleRefreshPatchBuildingPanel(-1); err != nil {
return err
}
}
to := gui.State.Panels.CommitFiles.refName
from, reverse := gui.getFromAndReverseArgsForDiff(to)
2022-01-08 05:10:01 +02:00
files, err := gui.Git.Loaders.CommitFiles.GetFilesInDiff(from, to, reverse)
if err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
}
2022-01-21 15:13:51 +02:00
gui.State.CommitFileTreeViewModel.SetParent(to)
gui.State.CommitFileTreeViewModel.SetFiles(files)
2021-04-03 06:56:11 +02:00
return gui.postRefreshUpdate(gui.State.Contexts.CommitFiles)
2020-08-19 10:41:57 +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
}
2021-03-31 13:08:55 +02:00
return gui.openFile(node.GetPath())
}
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 {
return gui.createErrorPanel(gui.Tr.ErrCannotEditDirectory)
}
2021-03-31 13:08:55 +02:00
return gui.editFile(node.GetPath())
2020-07-21 10:12:05 +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
}
toggleTheFile := func() error {
2022-01-08 05:10:01 +02:00
if !gui.Git.Patch.PatchManager.Active() {
2019-11-05 09:10:47 +02:00
if err := gui.startPatchManager(); err != nil {
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-21 15:13:51 +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-08 05:10:01 +02:00
return gui.Git.Patch.PatchManager.AddFileWhole(file.Name)
2021-03-31 13:39:55 +02:00
} else {
2022-01-08 05:10:01 +02:00
return gui.Git.Patch.PatchManager.RemoveFile(file.Name)
2021-03-31 13:39:55 +02:00
}
})
if err != nil {
return gui.surfaceError(err)
2020-08-22 08:46:19 +02:00
}
2022-01-08 05:10:01 +02:00
if gui.Git.Patch.PatchManager.IsEmpty() {
gui.Git.Patch.PatchManager.Reset()
}
2021-04-03 06:56:11 +02:00
return gui.postRefreshUpdate(gui.State.Contexts.CommitFiles)
}
2022-01-21 15:13:51 +02:00
if gui.Git.Patch.PatchManager.Active() && gui.Git.Patch.PatchManager.To != gui.State.CommitFileTreeViewModel.GetParent() {
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.DiscardPatch,
prompt: gui.Tr.DiscardPatchConfirm,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
2022-01-08 05:10:01 +02:00
gui.Git.Patch.PatchManager.Reset()
2020-08-15 08:36:39 +02:00
return toggleTheFile()
},
})
}
return toggleTheFile()
}
2019-11-05 09:10:47 +02:00
func (gui *Gui) startPatchManager() error {
canRebase := gui.State.Panels.CommitFiles.canRebase
to := gui.State.Panels.CommitFiles.refName
from, reverse := gui.getFromAndReverseArgsForDiff(to)
2022-01-08 05:10:01 +02:00
gui.Git.Patch.PatchManager.Start(from, to, reverse, canRebase)
return nil
}
func (gui *Gui) handleEnterCommitFile() error {
return gui.enterCommitFile(OnFocusOpts{ClickedViewName: "", ClickedViewLineIdx: -1})
2019-11-10 07:20:35 +02:00
}
func (gui *Gui) enterCommitFile(opts 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
}
2021-03-31 13:08:55 +02:00
if node.File == nil {
return gui.handleToggleCommitFileDirCollapsed()
}
enterTheFile := func() error {
2022-01-08 05:10:01 +02:00
if !gui.Git.Patch.PatchManager.Active() {
2019-11-05 09:10:47 +02:00
if err := gui.startPatchManager(); err != nil {
return err
}
}
return gui.pushContext(gui.State.Contexts.PatchBuilding, opts)
}
2022-01-21 15:13:51 +02:00
if gui.Git.Patch.PatchManager.Active() && gui.Git.Patch.PatchManager.To != gui.State.CommitFileTreeViewModel.GetParent() {
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2022-01-15 03:04:00 +02:00
title: gui.Tr.DiscardPatch,
prompt: gui.Tr.DiscardPatchConfirm,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
2022-01-08 05:10:01 +02:00
gui.Git.Patch.PatchManager.Reset()
return enterTheFile()
2020-08-15 08:36:39 +02:00
},
})
}
return enterTheFile()
}
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
2021-04-03 06:56:11 +02:00
if err := gui.postRefreshUpdate(gui.State.Contexts.CommitFiles); err != nil {
2021-03-31 13:08:55 +02:00
gui.Log.Error(err)
}
return nil
}
func (gui *Gui) switchToCommitFilesContext(refName string, canRebase bool, context Context, windowName string) 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
gui.State.Panels.CommitFiles.refName = refName
gui.State.Panels.CommitFiles.canRebase = canRebase
2021-04-03 06:56:11 +02:00
gui.State.Contexts.CommitFiles.SetParentContext(context)
gui.State.Contexts.CommitFiles.SetWindowName(windowName)
2020-08-21 11:53:45 +02:00
if err := gui.refreshCommitFilesView(); err != nil {
return err
}
2021-04-03 06:56:11 +02:00
return gui.pushContext(gui.State.Contexts.CommitFiles)
2020-08-21 11:53:45 +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()
// 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)
if found {
2021-04-03 06:56:11 +02:00
gui.State.Contexts.CommitFiles.GetPanelState().SetSelectedLineIdx(index)
}
}
2021-04-04 17:10:23 +02:00
if err := gui.State.Contexts.CommitFiles.HandleRender(); err != nil {
return err
}
if err := gui.State.Contexts.CommitFiles.HandleFocus(); err != nil {
return err
}
return nil
}