From 8822c409e24bc143d0fe68834cabcc80fadaf48d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 29 Mar 2020 11:06:46 +1100 Subject: split reflog commits into ReflogCommits and FilteredReflogCommits --- pkg/gui/branches_panel.go | 2 +- pkg/gui/gui.go | 31 +++++++++++++++++++------------ pkg/gui/layout.go | 2 +- pkg/gui/list_view.go | 2 +- pkg/gui/reflog_panel.go | 38 +++++++++++++++++++++++++++----------- pkg/gui/undoing.go | 2 +- 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 624b529e5..e2c09a4d8 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -54,7 +54,7 @@ func (gui *Gui) handleBranchSelect(g *gocui.Gui, v *gocui.View) error { // gui.refreshStatus is called at the end of this because that's when we can // be sure there is a state.Branches array to pick the current branch from func (gui *Gui) refreshBranches() { - reflogCommits := gui.State.ReflogCommits + reflogCommits := gui.State.FilteredReflogCommits if gui.inFilterMode() { // in filter mode we filter our reflog commits to just those containing the path // however we need all the reflog entries to populate the recencies of our branches diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 9e2130628..17ba0c00b 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -186,11 +186,17 @@ const ( ) type guiState struct { - Files []*commands.File - Branches []*commands.Branch - Commits []*commands.Commit - StashEntries []*commands.StashEntry - CommitFiles []*commands.CommitFile + Files []*commands.File + Branches []*commands.Branch + Commits []*commands.Commit + StashEntries []*commands.StashEntry + CommitFiles []*commands.CommitFile + // FilteredReflogCommits are the ones that appear in the reflog panel. + // when in filtering mode we only include the ones that match the given path + FilteredReflogCommits []*commands.Commit + // ReflogCommits are the ones used by the branches panel to obtain recency values + // if we're not in filtering mode, CommitFiles and FilteredReflogCommits will be + // one and the same ReflogCommits []*commands.Commit DiffEntries []*commands.Commit Remotes []*commands.Remote @@ -226,13 +232,14 @@ func (gui *Gui) resetState() { } gui.State = &guiState{ - Files: make([]*commands.File, 0), - PreviousView: "files", - Commits: make([]*commands.Commit, 0), - ReflogCommits: make([]*commands.Commit, 0), - CherryPickedCommits: make([]*commands.Commit, 0), - StashEntries: make([]*commands.StashEntry, 0), - DiffEntries: make([]*commands.Commit, 0), + Files: make([]*commands.File, 0), + PreviousView: "files", + Commits: make([]*commands.Commit, 0), + FilteredReflogCommits: make([]*commands.Commit, 0), + ReflogCommits: make([]*commands.Commit, 0), + CherryPickedCommits: make([]*commands.Commit, 0), + StashEntries: make([]*commands.StashEntry, 0), + DiffEntries: make([]*commands.Commit, 0), Panels: &panelStates{ Files: &filePanelState{SelectedLine: -1}, Branches: &branchPanelState{SelectedLine: 0}, diff --git a/pkg/gui/layout.go b/pkg/gui/layout.go index 0384fcd72..1e9046441 100644 --- a/pkg/gui/layout.go +++ b/pkg/gui/layout.go @@ -468,7 +468,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { {view: branchesView, context: "remotes", selectedLine: gui.State.Panels.Remotes.SelectedLine, lineCount: len(gui.State.Remotes)}, {view: branchesView, context: "remote-branches", selectedLine: gui.State.Panels.RemoteBranches.SelectedLine, lineCount: len(gui.State.Remotes)}, {view: commitsView, context: "branch-commits", selectedLine: gui.State.Panels.Commits.SelectedLine, lineCount: len(gui.State.Commits)}, - {view: commitsView, context: "reflog-commits", selectedLine: gui.State.Panels.ReflogCommits.SelectedLine, lineCount: len(gui.State.ReflogCommits)}, + {view: commitsView, context: "reflog-commits", selectedLine: gui.State.Panels.ReflogCommits.SelectedLine, lineCount: len(gui.State.FilteredReflogCommits)}, {view: stashView, context: "", selectedLine: gui.State.Panels.Stash.SelectedLine, lineCount: len(gui.State.StashEntries)}, } diff --git a/pkg/gui/list_view.go b/pkg/gui/list_view.go index 99039eb13..a8fab29d8 100644 --- a/pkg/gui/list_view.go +++ b/pkg/gui/list_view.go @@ -183,7 +183,7 @@ func (gui *Gui) getListViews() []*listView { { viewName: "commits", context: "reflog-commits", - getItemsLength: func() int { return len(gui.State.ReflogCommits) }, + getItemsLength: func() int { return len(gui.State.FilteredReflogCommits) }, getSelectedLineIdxPtr: func() *int { return &gui.State.Panels.ReflogCommits.SelectedLine }, handleFocus: gui.handleReflogCommitSelect, handleItemSelect: gui.handleReflogCommitSelect, diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index f8fa5e618..503ffde66 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -10,7 +10,7 @@ import ( func (gui *Gui) getSelectedReflogCommit() *commands.Commit { selectedLine := gui.State.Panels.ReflogCommits.SelectedLine - reflogComits := gui.State.ReflogCommits + reflogComits := gui.State.FilteredReflogCommits if selectedLine == -1 || len(reflogComits) == 0 { return nil } @@ -51,6 +51,9 @@ func (gui *Gui) handleReflogCommitSelect(g *gocui.Gui, v *gocui.View) error { // load entries that have been created since we last ran the call. This means // we need to be more careful with how we use this, and to ensure we're emptying // the reflogs array when changing contexts. +// This method also manages two things: ReflogCommits and FilteredReflogCommits. +// FilteredReflogCommits are rendered in the reflogs panel, and ReflogCommits +// are used by the branches panel to obtain recency values for sorting. func (gui *Gui) refreshReflogCommits() error { // pulling state into its own variable incase it gets swapped out for another state // and we get an out of bounds exception @@ -60,17 +63,30 @@ func (gui *Gui) refreshReflogCommits() error { lastReflogCommit = state.ReflogCommits[0] } - commits, onlyObtainedNewReflogCommits, err := gui.GitCommand.GetReflogCommits(lastReflogCommit, state.FilterPath) - if err != nil { - return gui.surfaceError(err) + refresh := func(stateCommits *[]*commands.Commit, filterPath string) error { + commits, onlyObtainedNewReflogCommits, err := gui.GitCommand.GetReflogCommits(lastReflogCommit, filterPath) + if err != nil { + return gui.surfaceError(err) + } + + if onlyObtainedNewReflogCommits { + *stateCommits = append(commits, *stateCommits...) + } else { + *stateCommits = commits + } + return nil } - if onlyObtainedNewReflogCommits { - state.ReflogCommits = append(commits, state.ReflogCommits...) + if err := refresh(&state.ReflogCommits, ""); err != nil { + return err + } + + if gui.inFilterMode() { + if err := refresh(&state.FilteredReflogCommits, state.FilterPath); err != nil { + return err + } } else { - // if we haven't found it we're probably in a new repo so we don't want to - // retain the old reflog commits - state.ReflogCommits = commits + state.FilteredReflogCommits = state.ReflogCommits } if gui.getCommitsView().Context == "reflog-commits" { @@ -83,8 +99,8 @@ func (gui *Gui) refreshReflogCommits() error { func (gui *Gui) renderReflogCommitsWithSelection() error { commitsView := gui.getCommitsView() - gui.refreshSelectedLine(&gui.State.Panels.ReflogCommits.SelectedLine, len(gui.State.ReflogCommits)) - displayStrings := presentation.GetReflogCommitListDisplayStrings(gui.State.ReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL) + gui.refreshSelectedLine(&gui.State.Panels.ReflogCommits.SelectedLine, len(gui.State.FilteredReflogCommits)) + displayStrings := presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL) gui.renderDisplayStrings(commitsView, displayStrings) if gui.g.CurrentView() == commitsView && commitsView.Context == "reflog-commits" { if err := gui.handleReflogCommitSelect(gui.g, commitsView); err != nil { diff --git a/pkg/gui/undoing.go b/pkg/gui/undoing.go index d34bfe91d..06057af37 100644 --- a/pkg/gui/undoing.go +++ b/pkg/gui/undoing.go @@ -37,7 +37,7 @@ type reflogAction struct { // Though we might support this later, hence the use of the CURRENT_REBASE action kind. func (gui *Gui) parseReflogForActions(onUserAction func(counter int, action reflogAction) (bool, error)) error { counter := 0 - reflogCommits := gui.State.ReflogCommits + reflogCommits := gui.State.FilteredReflogCommits rebaseFinishCommitSha := "" var action *reflogAction for reflogCommitIdx, reflogCommit := range reflogCommits { -- cgit v1.2.3