diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index 149114d5b..e70092891 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -39,10 +39,6 @@ func (gui *Gui) handleCommitFileSelect() error { }) } -func (gui *Gui) handleSwitchToCommitsPanel(g *gocui.Gui, v *gocui.View) error { - return gui.switchContext(gui.Contexts.BranchCommits.Context) -} - func (gui *Gui) handleCheckoutCommitFile(g *gocui.Gui, v *gocui.View) error { file := gui.State.CommitFiles[gui.State.Panels.CommitFiles.SelectedLineIdx] @@ -84,18 +80,13 @@ func (gui *Gui) refreshCommitFilesView() error { return err } - commit := gui.getSelectedCommit() - if commit == nil { - return nil - } - - files, err := gui.GitCommand.GetCommitFiles(commit.Sha, gui.GitCommand.PatchManager) + files, err := gui.GitCommand.GetCommitFiles(gui.State.Panels.CommitFiles.refName, gui.GitCommand.PatchManager) if err != nil { return gui.surfaceError(err) } gui.State.CommitFiles = files - return gui.postRefreshUpdate(gui.Contexts.BranchCommits.Files.Context) + return gui.postRefreshUpdate(gui.Contexts.CommitFiles.Context) } func (gui *Gui) handleOpenOldCommitFile(g *gocui.Gui, v *gocui.View) error { @@ -213,7 +204,7 @@ func (gui *Gui) enterCommitFile(selectedLineIdx int) error { return enterTheFile(selectedLineIdx) }, handleClose: func() error { - return gui.switchContext(gui.Contexts.BranchCommits.Files.Context) + return gui.switchContext(gui.Contexts.CommitFiles.Context) }, }) } diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 66600f3ec..148a0e460 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -499,12 +499,20 @@ func (gui *Gui) HandlePasteCommits(g *gocui.Gui, v *gocui.View) error { }) } -func (gui *Gui) handleSwitchToCommitFilesPanel() error { +func (gui *Gui) handleViewCommitFiles() error { + commit := gui.getSelectedCommit() + if commit == nil { + return nil + } + + gui.State.Panels.CommitFiles.refName = commit.Sha + gui.Contexts.CommitFiles.Context.SetParentContext(gui.Contexts.BranchCommits.Context) + if err := gui.refreshCommitFilesView(); err != nil { return err } - return gui.switchContext(gui.Contexts.BranchCommits.Files.Context) + return gui.switchContext(gui.Contexts.CommitFiles.Context) } func (gui *Gui) hasCommit(commits []*commands.Commit, target string) (int, bool) { diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 9cbd22276..9fa166571 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -43,6 +43,8 @@ type Context interface { GetViewName() string GetKey() string GetSelectedItemId() string + SetParentContext(Context) + GetParentContext() Context } type BasicContext struct { @@ -54,6 +56,14 @@ type BasicContext struct { ViewName string } +func (c BasicContext) SetParentContext(Context) { + panic("can't set parent context on basic context") +} + +func (c BasicContext) GetParentContext() Context { + panic("can't get parent context on basic context") +} + // TODO: think about whether we need this on the Context interface or if it should just be on the ListContext struct func (c BasicContext) GetSelectedItemId() string { return "" @@ -98,11 +108,6 @@ type RemotesContextNode struct { Branches SimpleContextNode } -type CommitsContextNode struct { - Context Context - Files SimpleContextNode -} - type ContextTree struct { Status SimpleContextNode Files SimpleContextNode @@ -110,7 +115,8 @@ type ContextTree struct { Branches SimpleContextNode Remotes RemotesContextNode Tags SimpleContextNode - BranchCommits CommitsContextNode + BranchCommits SimpleContextNode + CommitFiles SimpleContextNode ReflogCommits SimpleContextNode Stash SimpleContextNode Normal SimpleContextNode @@ -132,7 +138,7 @@ func (gui *Gui) allContexts() []Context { gui.Contexts.Remotes.Branches.Context, gui.Contexts.Tags.Context, gui.Contexts.BranchCommits.Context, - gui.Contexts.BranchCommits.Files.Context, + gui.Contexts.CommitFiles.Context, gui.Contexts.ReflogCommits.Context, gui.Contexts.Stash.Context, gui.Contexts.Menu.Context, @@ -168,11 +174,11 @@ func (gui *Gui) contextTree() ContextTree { Context: gui.remoteBranchesListContext(), }, }, - BranchCommits: CommitsContextNode{ + BranchCommits: SimpleContextNode{ Context: gui.branchCommitsListContext(), - Files: SimpleContextNode{ - Context: gui.commitFilesListContext(), - }, + }, + CommitFiles: SimpleContextNode{ + Context: gui.commitFilesListContext(), }, ReflogCommits: SimpleContextNode{ Context: gui.reflogCommitsListContext(), @@ -271,7 +277,7 @@ func (gui *Gui) initialViewContextMap() map[string]Context { "files": gui.Contexts.Files.Context, "branches": gui.Contexts.Branches.Context, "commits": gui.Contexts.BranchCommits.Context, - "commitFiles": gui.Contexts.BranchCommits.Files.Context, + "commitFiles": gui.Contexts.CommitFiles.Context, "stash": gui.Contexts.Stash.Context, "menu": gui.Contexts.Menu.Context, "confirmation": gui.Contexts.Confirmation.Context, @@ -474,12 +480,22 @@ func (gui *Gui) renderContextStack() string { } func (gui *Gui) currentContextKey() string { - // on startup the stack can be empty so we'll return an empty string in that case - if len(gui.State.ContextStack) == 0 { + currentContext := gui.currentContext() + + if currentContext == nil { return "" } - return gui.State.ContextStack[len(gui.State.ContextStack)-1].GetKey() + return currentContext.GetKey() +} + +func (gui *Gui) currentContext() Context { + // on startup the stack can be empty so we'll return an empty string in that case + if len(gui.State.ContextStack) == 0 { + return nil + } + + return gui.State.ContextStack[len(gui.State.ContextStack)-1] } func (gui *Gui) setInitialViewContexts() { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 4a0e1097e..e0ab0c724 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -184,6 +184,10 @@ type menuPanelState struct { type commitFilesPanelState struct { listPanelState + + // this is the SHA of the commit or the stash index of the stash. + // Not sure if ref is actually the right word here + refName string } type panelStates struct { @@ -291,7 +295,7 @@ func (gui *Gui) resetState() { Tags: &tagsPanelState{listPanelState{SelectedLineIdx: -1}}, Commits: &commitPanelState{listPanelState: listPanelState{SelectedLineIdx: -1}, LimitCommits: true}, ReflogCommits: &reflogCommitPanelState{listPanelState{SelectedLineIdx: 0}}, // TODO: might need to make -1 - CommitFiles: &commitFilesPanelState{listPanelState{SelectedLineIdx: -1}}, + CommitFiles: &commitFilesPanelState{listPanelState: listPanelState{SelectedLineIdx: -1}, refName: ""}, Stash: &stashPanelState{listPanelState{SelectedLineIdx: -1}}, Menu: &menuPanelState{listPanelState: listPanelState{SelectedLineIdx: 0}, OnPress: nil}, Merging: &mergingPanelState{ diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index e5df56c42..3ce94c631 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -765,7 +765,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { ViewName: "commits", Contexts: []string{BRANCH_COMMITS_CONTEXT_KEY}, Key: gui.getKey("universal.goInto"), - Handler: gui.wrappedHandler(gui.handleSwitchToCommitFilesPanel), + Handler: gui.wrappedHandler(gui.handleViewCommitFiles), Description: gui.Tr.SLocalize("viewCommitFiles"), }, { @@ -797,6 +797,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Handler: gui.handleResetCherryPick, Description: gui.Tr.SLocalize("resetCherryPick"), }, + { + ViewName: "commits", + Contexts: []string{REFLOG_COMMITS_CONTEXT_KEY}, + Key: gui.getKey("universal.goInto"), + Handler: gui.wrappedHandler(gui.handleViewReflogCommitFiles), + Description: gui.Tr.SLocalize("viewCommitFiles"), + }, { ViewName: "commits", Contexts: []string{REFLOG_COMMITS_CONTEXT_KEY}, @@ -865,12 +872,6 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Modifier: gocui.ModNone, Handler: gui.handleInfoClick, }, - { - ViewName: "commitFiles", - Key: gui.getKey("universal.return"), - Handler: gui.handleSwitchToCommitsPanel, - Description: gui.Tr.SLocalize("goBack"), - }, { ViewName: "commitFiles", Key: gui.getKey("commitFiles.checkoutCommitFile"), diff --git a/pkg/gui/list_context.go b/pkg/gui/list_context.go index 5e37e6c86..f70c202b7 100644 --- a/pkg/gui/list_context.go +++ b/pkg/gui/list_context.go @@ -19,12 +19,21 @@ type ListContext struct { Gui *Gui RendersToMainView bool Kind int + ParentContext Context } type ListItem interface { ID() string } +func (lc *ListContext) SetParentContext(c Context) { + lc.ParentContext = c +} + +func (lc *ListContext) GetParentContext() Context { + return lc.ParentContext +} + func (lc *ListContext) GetSelectedItem() ListItem { items := lc.GetItems() @@ -317,7 +326,7 @@ func (gui *Gui) branchCommitsListContext() *ListContext { GetItemsLength: func() int { return len(gui.State.Commits) }, GetPanelState: func() IListPanelState { return gui.State.Panels.Commits }, OnFocus: gui.handleCommitSelect, - OnClickSelectedItem: gui.handleSwitchToCommitFilesPanel, + OnClickSelectedItem: gui.handleViewCommitFiles, Gui: gui, RendersToMainView: true, Kind: SIDE_CONTEXT, diff --git a/pkg/gui/patch_building_panel.go b/pkg/gui/patch_building_panel.go index a2b872f36..0df46fe8f 100644 --- a/pkg/gui/patch_building_panel.go +++ b/pkg/gui/patch_building_panel.go @@ -82,7 +82,7 @@ func (gui *Gui) handleEscapePatchBuildingPanel() error { gui.GitCommand.PatchManager.Reset() } - return gui.switchContext(gui.Contexts.BranchCommits.Files.Context) + return gui.switchContext(gui.Contexts.CommitFiles.Context) } func (gui *Gui) refreshSecondaryPatchPanel() error { diff --git a/pkg/gui/quitting.go b/pkg/gui/quitting.go index b39f3bcb8..107538a39 100644 --- a/pkg/gui/quitting.go +++ b/pkg/gui/quitting.go @@ -42,6 +42,12 @@ func (gui *Gui) handleTopLevelReturn(g *gocui.Gui, v *gocui.View) error { return gui.exitFilterMode() } + currentContext := gui.currentContext() + if currentContext != nil && currentContext.GetParentContext() != nil { + // TODO: think about whether this should be marked as a return rather than adding to the stack + gui.switchContext(currentContext.GetParentContext()) + } + if gui.Config.GetUserConfig().GetBool("quitOnTopLevelReturn") { return gui.handleQuit() } diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index 1ccb8e4ac..d14557599 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -112,3 +112,19 @@ func (gui *Gui) handleCreateReflogResetMenu(g *gocui.Gui, v *gocui.View) error { return gui.createResetMenu(commit.Sha) } + +func (gui *Gui) handleViewReflogCommitFiles() error { + commit := gui.getSelectedReflogCommit() + if commit == nil { + return nil + } + + gui.State.Panels.CommitFiles.refName = commit.Sha + gui.Contexts.CommitFiles.Context.SetParentContext(gui.Contexts.ReflogCommits.Context) + + if err := gui.refreshCommitFilesView(); err != nil { + return err + } + + return gui.switchContext(gui.Contexts.CommitFiles.Context) +}