1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-19 22:33:16 +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 { func (gui *Gui) handleNewBranchOffCurrentItem() error {
context := gui.currentSideContext() context := gui.currentSideContext()
item := context.GetSelectedItem() item, ok := context.GetSelectedItem()
if item == nil { if !ok {
return nil return nil
} }

View File

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

View File

@ -45,8 +45,6 @@ type Context interface {
GetWindowName() string GetWindowName() string
SetWindowName(string) SetWindowName(string)
GetKey() string GetKey() string
GetSelectedItemId() string
GetSelectedItem() ListItem
SetParentContext(Context) SetParentContext(Context)
GetParentContext() Context GetParentContext() Context
} }
@ -64,10 +62,6 @@ func (c BasicContext) SetWindowName(windowName string) {
panic("can't set window name on basic context") panic("can't set window name on basic context")
} }
func (c BasicContext) GetSelectedItem() ListItem {
return nil
}
func (c BasicContext) GetWindowName() string { func (c BasicContext) GetWindowName() string {
// TODO: fix this up // TODO: fix this up
return c.GetViewName() return c.GetViewName()
@ -81,11 +75,6 @@ func (c BasicContext) GetParentContext() Context {
panic("can't get parent context on basic 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 { func (c BasicContext) HandleRender() error {
if c.OnRender != nil { if c.OnRender != nil {
return c.OnRender() return c.OnRender()
@ -534,7 +523,12 @@ func (gui *Gui) currentSideContext() *ListContext {
context := stack[len(stack)-1-i] context := stack[len(stack)-1-i]
if context.GetKind() == SIDE_CONTEXT { 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 return view
} }
func (gui *Gui) getSideContextSelectedItem() ListItem { func (gui *Gui) getSideContextSelectedItemId() string {
currentSideContext := gui.currentSideContext() currentSideContext := gui.currentSideContext()
if currentSideContext == nil { 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 { if context == nil {
return nil return nil
} }
item := context.GetSelectedItem() item, ok := context.GetSelectedItem()
if item == nil { if !ok {
return nil return nil
} }
return []string{item.ID()} return []string{item.ID()}
@ -79,11 +79,7 @@ func (gui *Gui) currentDiffTerminal() string {
func (gui *Gui) currentlySelectedFilename() string { func (gui *Gui) currentlySelectedFilename() string {
switch gui.currentContextKey() { switch gui.currentContextKey() {
case FILES_CONTEXT_KEY, COMMIT_FILES_CONTEXT_KEY: case FILES_CONTEXT_KEY, COMMIT_FILES_CONTEXT_KEY:
item := gui.getSideContextSelectedItem() return gui.getSideContextSelectedItemId()
if item == nil {
return ""
}
return item.ID()
default: default:
return "" return ""
} }

View File

@ -181,11 +181,11 @@ func (gui *Gui) fetch(canPromptForCredentials bool) (err error) {
func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error { func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
// important to note that this assumes we've selected an item in a side context // 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 nil
} }
return gui.OSCommand.CopyToClipboard(item.ID()) return gui.OSCommand.CopyToClipboard(itemId)
} }

View File

@ -21,7 +21,9 @@ type ListContext struct {
OnFocus func() error OnFocus func() error
OnFocusLost func() error OnFocusLost func() error
OnClickSelectedItem func() error OnClickSelectedItem func() error
SelectedItem func() ListItem
// 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 GetPanelState func() IListPanelState
Gui *Gui Gui *Gui
@ -40,7 +42,7 @@ type ListItem interface {
Description() string Description() string
} }
func (lc *ListContext) GetSelectedItem() ListItem { func (lc *ListContext) GetSelectedItem() (ListItem, bool) {
return lc.SelectedItem() return lc.SelectedItem()
} }
@ -72,9 +74,9 @@ func (lc *ListContext) GetParentContext() Context {
} }
func (lc *ListContext) GetSelectedItemId() string { func (lc *ListContext) GetSelectedItemId() string {
item := lc.SelectedItem() item, ok := lc.SelectedItem()
if item == nil { if !ok {
return "" return ""
} }
@ -278,7 +280,10 @@ func (gui *Gui) filesListContext() *ListContext {
return presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Modes.Diffing.Ref) return presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_NOTHING, Contains: CONTAINS_NOTHING,
SelectedItem: func() ListItem { return gui.getSelectedFile() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedFile()
return item, item != nil
},
} }
} }
@ -296,7 +301,10 @@ func (gui *Gui) branchesListContext() *ListContext {
return presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Modes.Diffing.Ref) return presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_COMMITS, Contains: CONTAINS_COMMITS,
SelectedItem: func() ListItem { return gui.getSelectedBranch() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedBranch()
return item, item != nil
},
} }
} }
@ -315,7 +323,10 @@ func (gui *Gui) remotesListContext() *ListContext {
return presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Modes.Diffing.Ref) return presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_BRANCHES, Contains: CONTAINS_BRANCHES,
SelectedItem: func() ListItem { return gui.getSelectedRemote() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedRemote()
return item, item != nil
},
} }
} }
@ -333,7 +344,10 @@ func (gui *Gui) remoteBranchesListContext() *ListContext {
return presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Modes.Diffing.Ref) return presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_COMMITS, Contains: CONTAINS_COMMITS,
SelectedItem: func() ListItem { return gui.getSelectedRemoteBranch() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedRemoteBranch()
return item, item != nil
},
} }
} }
@ -351,7 +365,10 @@ func (gui *Gui) tagsListContext() *ListContext {
return presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Modes.Diffing.Ref) return presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_COMMITS, Contains: CONTAINS_COMMITS,
SelectedItem: func() ListItem { return gui.getSelectedTag() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedTag()
return item, item != nil
},
} }
} }
@ -370,7 +387,10 @@ func (gui *Gui) branchCommitsListContext() *ListContext {
return presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref) return presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_FILES, Contains: CONTAINS_FILES,
SelectedItem: func() ListItem { return gui.getSelectedLocalCommit() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedLocalCommit()
return item, item != nil
},
} }
} }
@ -388,7 +408,10 @@ func (gui *Gui) reflogCommitsListContext() *ListContext {
return presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref) return presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_FILES, Contains: CONTAINS_FILES,
SelectedItem: func() ListItem { return gui.getSelectedReflogCommit() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedReflogCommit()
return item, item != nil
},
} }
} }
@ -407,7 +430,10 @@ func (gui *Gui) subCommitsListContext() *ListContext {
return presentation.GetCommitListDisplayStrings(gui.State.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref) return presentation.GetCommitListDisplayStrings(gui.State.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_COMMITS, Contains: CONTAINS_COMMITS,
SelectedItem: func() ListItem { return gui.getSelectedSubCommit() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedSubCommit()
return item, item != nil
},
} }
} }
@ -425,7 +451,10 @@ func (gui *Gui) stashListContext() *ListContext {
return presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Modes.Diffing.Ref) return presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_FILES, Contains: CONTAINS_FILES,
SelectedItem: func() ListItem { return gui.getSelectedStashEntry() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedStashEntry()
return item, item != nil
},
} }
} }
@ -444,7 +473,10 @@ func (gui *Gui) commitFilesListContext() *ListContext {
return presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Modes.Diffing.Ref) return presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Modes.Diffing.Ref)
}, },
Contains: CONTAINS_NOTHING, Contains: CONTAINS_NOTHING,
SelectedItem: func() ListItem { return gui.getSelectedCommitFile() }, SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedCommitFile()
return item, item != nil
},
} }
} }