1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

deal with the fact that a nil wrapped in an interface is not equal to nil

This commit is contained in:
Jesse Duffield 2020-08-22 15:56:30 +10:00
parent e290710f67
commit 5874529f43
6 changed files with 91 additions and 64 deletions

View File

@ -463,8 +463,8 @@ func (gui *Gui) currentBranch() *commands.Branch {
func (gui *Gui) handleNewBranchOffCurrentItem() error {
context := gui.currentSideContext()
item := context.GetSelectedItem()
if item == nil {
item, ok := context.GetSelectedItem()
if !ok {
return nil
}

View File

@ -35,11 +35,12 @@ func (gui *Gui) handleCopyCommit() error {
return err
}
commit, ok := context.SelectedItem().(*commands.Commit)
item, ok := context.SelectedItem()
if !ok {
gui.Log.Error("type cast failed for handling copy commit")
return nil
}
if commit == nil {
commit, ok := item.(*commands.Commit)
if !ok {
return nil
}
@ -114,26 +115,24 @@ func (gui *Gui) handleCopyCommitRange() error {
return err
}
commit, ok := context.SelectedItem().(*commands.Commit)
if !ok {
gui.Log.Error("type cast failed for handling copy commit")
}
if commit == nil {
commitShaMap := gui.cherryPickedCommitShaMap()
commitsList := gui.commitsListForContext()
selectedLineIdx := context.GetPanelState().GetSelectedLineIdx()
if selectedLineIdx > len(commitsList)-1 {
return nil
}
commitShaMap := gui.cherryPickedCommitShaMap()
// find the last commit that is copied that's above our position
// if there are none, startIndex = 0
startIndex := 0
for index, commit := range gui.commitsListForContext()[0:context.GetPanelState().GetSelectedLineIdx()] {
for index, commit := range commitsList[0:selectedLineIdx] {
if commitShaMap[commit.Sha] {
startIndex = index
}
}
for index := startIndex; index <= context.GetPanelState().GetSelectedLineIdx(); index++ {
for index := startIndex; index <= selectedLineIdx; index++ {
gui.addCommitToCherryPickedCommits(index)
}

View File

@ -45,8 +45,6 @@ type Context interface {
GetWindowName() string
SetWindowName(string)
GetKey() string
GetSelectedItemId() string
GetSelectedItem() ListItem
SetParentContext(Context)
GetParentContext() Context
}
@ -64,10 +62,6 @@ func (c BasicContext) SetWindowName(windowName string) {
panic("can't set window name on basic context")
}
func (c BasicContext) GetSelectedItem() ListItem {
return nil
}
func (c BasicContext) GetWindowName() string {
// TODO: fix this up
return c.GetViewName()
@ -81,11 +75,6 @@ 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 ""
}
func (c BasicContext) HandleRender() error {
if c.OnRender != nil {
return c.OnRender()
@ -534,7 +523,12 @@ func (gui *Gui) currentSideContext() *ListContext {
context := stack[len(stack)-1-i]
if context.GetKind() == SIDE_CONTEXT {
return context.(*ListContext)
// not all side contexts are list contexts (e.g. the status panel)
listContext, ok := context.(*ListContext)
if !ok {
return nil
}
return listContext
}
}
@ -694,11 +688,17 @@ func (gui *Gui) getCurrentSideView() *gocui.View {
return view
}
func (gui *Gui) getSideContextSelectedItem() ListItem {
func (gui *Gui) getSideContextSelectedItemId() string {
currentSideContext := gui.currentSideContext()
if currentSideContext == nil {
return nil
return ""
}
return currentSideContext.GetSelectedItem()
item, ok := currentSideContext.GetSelectedItem()
if ok {
return item.ID()
}
return ""
}

View File

@ -60,8 +60,8 @@ func (gui *Gui) currentDiffTerminals() []string {
if context == nil {
return nil
}
item := context.GetSelectedItem()
if item == nil {
item, ok := context.GetSelectedItem()
if !ok {
return nil
}
return []string{item.ID()}
@ -79,11 +79,7 @@ func (gui *Gui) currentDiffTerminal() string {
func (gui *Gui) currentlySelectedFilename() string {
switch gui.currentContextKey() {
case FILES_CONTEXT_KEY, COMMIT_FILES_CONTEXT_KEY:
item := gui.getSideContextSelectedItem()
if item == nil {
return ""
}
return item.ID()
return gui.getSideContextSelectedItemId()
default:
return ""
}

View File

@ -181,11 +181,11 @@ func (gui *Gui) fetch(canPromptForCredentials bool) (err error) {
func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
// important to note that this assumes we've selected an item in a side context
item := gui.getSideContextSelectedItem()
itemId := gui.getSideContextSelectedItemId()
if item == nil {
if itemId == "" {
return nil
}
return gui.OSCommand.CopyToClipboard(item.ID())
return gui.OSCommand.CopyToClipboard(itemId)
}

View File

@ -21,8 +21,10 @@ type ListContext struct {
OnFocus func() error
OnFocusLost func() error
OnClickSelectedItem func() error
SelectedItem func() ListItem
GetPanelState func() IListPanelState
// have to return whether item is nil because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection)
SelectedItem func() (ListItem, bool)
GetPanelState func() IListPanelState
Gui *Gui
RendersToMainView bool
@ -40,7 +42,7 @@ type ListItem interface {
Description() string
}
func (lc *ListContext) GetSelectedItem() ListItem {
func (lc *ListContext) GetSelectedItem() (ListItem, bool) {
return lc.SelectedItem()
}
@ -72,9 +74,9 @@ func (lc *ListContext) GetParentContext() Context {
}
func (lc *ListContext) GetSelectedItemId() string {
item := lc.SelectedItem()
item, ok := lc.SelectedItem()
if item == nil {
if !ok {
return ""
}
@ -277,8 +279,11 @@ func (gui *Gui) filesListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_NOTHING,
SelectedItem: func() ListItem { return gui.getSelectedFile() },
Contains: CONTAINS_NOTHING,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedFile()
return item, item != nil
},
}
}
@ -295,8 +300,11 @@ func (gui *Gui) branchesListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_COMMITS,
SelectedItem: func() ListItem { return gui.getSelectedBranch() },
Contains: CONTAINS_COMMITS,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedBranch()
return item, item != nil
},
}
}
@ -314,8 +322,11 @@ func (gui *Gui) remotesListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_BRANCHES,
SelectedItem: func() ListItem { return gui.getSelectedRemote() },
Contains: CONTAINS_BRANCHES,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedRemote()
return item, item != nil
},
}
}
@ -332,8 +343,11 @@ func (gui *Gui) remoteBranchesListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_COMMITS,
SelectedItem: func() ListItem { return gui.getSelectedRemoteBranch() },
Contains: CONTAINS_COMMITS,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedRemoteBranch()
return item, item != nil
},
}
}
@ -350,8 +364,11 @@ func (gui *Gui) tagsListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_COMMITS,
SelectedItem: func() ListItem { return gui.getSelectedTag() },
Contains: CONTAINS_COMMITS,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedTag()
return item, item != nil
},
}
}
@ -369,8 +386,11 @@ func (gui *Gui) branchCommitsListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_FILES,
SelectedItem: func() ListItem { return gui.getSelectedLocalCommit() },
Contains: CONTAINS_FILES,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedLocalCommit()
return item, item != nil
},
}
}
@ -387,8 +407,11 @@ func (gui *Gui) reflogCommitsListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_FILES,
SelectedItem: func() ListItem { return gui.getSelectedReflogCommit() },
Contains: CONTAINS_FILES,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedReflogCommit()
return item, item != nil
},
}
}
@ -406,8 +429,11 @@ func (gui *Gui) subCommitsListContext() *ListContext {
gui.Log.Warn("getting display strings for sub commits")
return presentation.GetCommitListDisplayStrings(gui.State.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_COMMITS,
SelectedItem: func() ListItem { return gui.getSelectedSubCommit() },
Contains: CONTAINS_COMMITS,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedSubCommit()
return item, item != nil
},
}
}
@ -424,8 +450,11 @@ func (gui *Gui) stashListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_FILES,
SelectedItem: func() ListItem { return gui.getSelectedStashEntry() },
Contains: CONTAINS_FILES,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedStashEntry()
return item, item != nil
},
}
}
@ -443,8 +472,11 @@ func (gui *Gui) commitFilesListContext() *ListContext {
GetDisplayStrings: func() [][]string {
return presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Modes.Diffing.Ref)
},
Contains: CONTAINS_NOTHING,
SelectedItem: func() ListItem { return gui.getSelectedCommitFile() },
Contains: CONTAINS_NOTHING,
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedCommitFile()
return item, item != nil
},
}
}