From 1eb5d89f1d3882c4d765419edddc1a3d2c5eb372 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 8 Aug 2024 11:12:26 +0200 Subject: [PATCH] Remove bool return value of GetParentContext() The comments that I'm deleting here explain why we need the bool; however, in our case that's a theoretical issue. It would only arise if we ever were to pass a nil context to SetParentContext, which we never do. --- pkg/gui/context/parent_context_mgr.go | 7 ++----- pkg/gui/controllers/commits_files_controller.go | 4 ++-- pkg/gui/controllers/helpers/refresh_helper.go | 4 ++-- pkg/gui/controllers/quit_actions.go | 4 ++-- pkg/gui/types/context.go | 3 +-- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/pkg/gui/context/parent_context_mgr.go b/pkg/gui/context/parent_context_mgr.go index 50747a3a0..4f704e35e 100644 --- a/pkg/gui/context/parent_context_mgr.go +++ b/pkg/gui/context/parent_context_mgr.go @@ -4,17 +4,14 @@ import "github.com/jesseduffield/lazygit/pkg/gui/types" type ParentContextMgr struct { ParentContext types.Context - // we can't know on the calling end whether a Context is actually a nil value without reflection, so we're storing this flag here to tell us. There has got to be a better way around this - hasParent bool } var _ types.ParentContexter = (*ParentContextMgr)(nil) func (self *ParentContextMgr) SetParentContext(context types.Context) { self.ParentContext = context - self.hasParent = true } -func (self *ParentContextMgr) GetParentContext() (types.Context, bool) { - return self.ParentContext, self.hasParent +func (self *ParentContextMgr) GetParentContext() types.Context { + return self.ParentContext } diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 0cf08b554..2a9ba8271 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -178,8 +178,8 @@ func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error } func (self *CommitFilesController) discard(selectedNodes []*filetree.CommitFileNode) error { - parentContext, ok := self.c.Context().Current().GetParentContext() - if !ok || parentContext.GetKey() != context.LOCAL_COMMITS_CONTEXT_KEY { + parentContext := self.c.Context().Current().GetParentContext() + if parentContext == nil || parentContext.GetKey() != context.LOCAL_COMMITS_CONTEXT_KEY { return errors.New(self.c.Tr.CanOnlyDiscardFromLocalCommits) } diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index 21c20c646..4295c11ab 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -276,8 +276,8 @@ func (self *RefreshHelper) refreshReflogAndBranches(refreshWorktrees bool, keepB func (self *RefreshHelper) refreshCommitsAndCommitFiles() { _ = self.refreshCommitsWithLimit() - ctx, ok := self.c.Contexts().CommitFiles.GetParentContext() - if ok && ctx.GetKey() == context.LOCAL_COMMITS_CONTEXT_KEY { + ctx := self.c.Contexts().CommitFiles.GetParentContext() + if ctx != nil && ctx.GetKey() == context.LOCAL_COMMITS_CONTEXT_KEY { // This makes sense when we've e.g. just amended a commit, meaning we get a new commit hash at the same position. // However if we've just added a brand new commit, it pushes the list down by one and so we would end up // showing the contents of a different commit than the one we initially entered. diff --git a/pkg/gui/controllers/quit_actions.go b/pkg/gui/controllers/quit_actions.go index 78c7e4564..f7cd8e56f 100644 --- a/pkg/gui/controllers/quit_actions.go +++ b/pkg/gui/controllers/quit_actions.go @@ -71,8 +71,8 @@ func (self *QuitActions) Escape() error { } } - parentContext, hasParent := currentContext.GetParentContext() - if hasParent && currentContext != nil && parentContext != nil { + parentContext := currentContext.GetParentContext() + if parentContext != nil { // TODO: think about whether this should be marked as a return rather than adding to the stack return self.c.Context().Push(parentContext) } diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go index 27385bbbf..49a6e6e35 100644 --- a/pkg/gui/types/context.go +++ b/pkg/gui/types/context.go @@ -35,8 +35,7 @@ const ( type ParentContexter interface { SetParentContext(Context) - // we return a bool here to tell us whether or not the returned value just wraps a nil - GetParentContext() (Context, bool) + GetParentContext() Context } type NeedsRerenderOnWidthChangeLevel int