1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-25 12:24:47 +02:00

allowing commit files to be viewed in reflog as well

This commit is contained in:
Jesse Duffield 2020-08-21 09:12:45 +10:00
parent 48f1adad49
commit ddf25e14af
9 changed files with 90 additions and 39 deletions

View File

@ -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)
},
})
}

View File

@ -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) {

View File

@ -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() {

View File

@ -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{

View File

@ -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"),

View File

@ -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,

View File

@ -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 {

View File

@ -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()
}

View File

@ -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)
}