1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-03 00:57:52 +02:00

Instantiate mutexes by value (#4632)

- **PR Description**

Here's a minor cleanup: instantiate mutexes by value so that they don't
have to be initialized explicitly. It is always preferable for structs
to have a valid zero value, and this is one small step in that
direction.

I read this recommendation in the [Uber style
guide](https://github.com/uber-go/guide/blob/master/style.md#zero-value-mutexes-are-valid),
and it makes a lot of sense to me. I find most of the rest of this style
guide to be a very good read, too.
This commit is contained in:
Stefan Haller
2025-06-19 18:42:56 +02:00
committed by GitHub
8 changed files with 21 additions and 40 deletions

View File

@ -21,7 +21,7 @@ type MainBranches struct {
previousMainBranches []string previousMainBranches []string
cmd oscommands.ICmdObjBuilder cmd oscommands.ICmdObjBuilder
mutex *deadlock.Mutex mutex deadlock.Mutex
} }
func NewMainBranches( func NewMainBranches(
@ -32,7 +32,6 @@ func NewMainBranches(
c: cmn, c: cmn,
existingMainBranches: nil, existingMainBranches: nil,
cmd: cmd, cmd: cmd,
mutex: &deadlock.Mutex{},
} }
} }

View File

@ -16,14 +16,13 @@ type FilteredList[T any] struct {
getFilterFields func(T) []string getFilterFields func(T) []string
filter string filter string
mutex *deadlock.Mutex mutex deadlock.Mutex
} }
func NewFilteredList[T any](getList func() []T, getFilterFields func(T) []string) *FilteredList[T] { func NewFilteredList[T any](getList func() []T, getFilterFields func(T) []string) *FilteredList[T] {
return &FilteredList[T]{ return &FilteredList[T]{
getList: getList, getList: getList,
getFilterFields: getFilterFields, getFilterFields: getFilterFields,
mutex: &deadlock.Mutex{},
} }
} }

View File

@ -12,7 +12,7 @@ type MergeConflictsContext struct {
types.Context types.Context
viewModel *ConflictsViewModel viewModel *ConflictsViewModel
c *ContextCommon c *ContextCommon
mutex *deadlock.Mutex mutex deadlock.Mutex
} }
type ConflictsViewModel struct { type ConflictsViewModel struct {
@ -33,7 +33,6 @@ func NewMergeConflictsContext(
return &MergeConflictsContext{ return &MergeConflictsContext{
viewModel: viewModel, viewModel: viewModel,
mutex: &deadlock.Mutex{},
Context: NewSimpleContext( Context: NewSimpleContext(
NewBaseContext(NewBaseContextOpts{ NewBaseContext(NewBaseContextOpts{
Kind: types.MAIN_CONTEXT, Kind: types.MAIN_CONTEXT,
@ -57,7 +56,7 @@ func (self *MergeConflictsContext) SetState(state *mergeconflicts.State) {
} }
func (self *MergeConflictsContext) GetMutex() *deadlock.Mutex { func (self *MergeConflictsContext) GetMutex() *deadlock.Mutex {
return self.mutex return &self.mutex
} }
func (self *MergeConflictsContext) SetUserScrolling(isScrolling bool) { func (self *MergeConflictsContext) SetUserScrolling(isScrolling bool) {

View File

@ -15,7 +15,7 @@ type PatchExplorerContext struct {
viewTrait *ViewTrait viewTrait *ViewTrait
getIncludedLineIndices func() []int getIncludedLineIndices func() []int
c *ContextCommon c *ContextCommon
mutex *deadlock.Mutex mutex deadlock.Mutex
} }
var ( var (
@ -36,7 +36,6 @@ func NewPatchExplorerContext(
state: nil, state: nil,
viewTrait: NewViewTrait(view), viewTrait: NewViewTrait(view),
c: c, c: c,
mutex: &deadlock.Mutex{},
getIncludedLineIndices: getIncludedLineIndices, getIncludedLineIndices: getIncludedLineIndices,
SimpleContext: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ SimpleContext: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
View: view, View: view,
@ -137,7 +136,7 @@ func (self *PatchExplorerContext) NavigateTo(selectedLineIdx int) {
} }
func (self *PatchExplorerContext) GetMutex() *deadlock.Mutex { func (self *PatchExplorerContext) GetMutex() *deadlock.Mutex {
return self.mutex return &self.mutex
} }
func (self *PatchExplorerContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition { func (self *PatchExplorerContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {

View File

@ -15,7 +15,7 @@ type InlineStatusHelper struct {
windowHelper *WindowHelper windowHelper *WindowHelper
contextsWithInlineStatus map[types.ContextKey]*inlineStatusInfo contextsWithInlineStatus map[types.ContextKey]*inlineStatusInfo
mutex *deadlock.Mutex mutex deadlock.Mutex
} }
func NewInlineStatusHelper(c *HelperCommon, windowHelper *WindowHelper) *InlineStatusHelper { func NewInlineStatusHelper(c *HelperCommon, windowHelper *WindowHelper) *InlineStatusHelper {
@ -23,7 +23,6 @@ func NewInlineStatusHelper(c *HelperCommon, windowHelper *WindowHelper) *InlineS
c: c, c: c,
windowHelper: windowHelper, windowHelper: windowHelper,
contextsWithInlineStatus: make(map[types.ContextKey]*inlineStatusInfo), contextsWithInlineStatus: make(map[types.ContextKey]*inlineStatusInfo),
mutex: &deadlock.Mutex{},
} }
} }

View File

@ -118,7 +118,7 @@ type Gui struct {
// is being pushed). At the moment the rule is to use an item operation when // is being pushed). At the moment the rule is to use an item operation when
// we need to talk to the remote. // we need to talk to the remote.
itemOperations map[string]types.ItemOperation itemOperations map[string]types.ItemOperation
itemOperationsMutex *deadlock.Mutex itemOperationsMutex deadlock.Mutex
PrevLayout PrevLayout PrevLayout PrevLayout
@ -681,22 +681,10 @@ func NewGui(
// real value after loading the user config: // real value after loading the user config:
ShowExtrasWindow: true, ShowExtrasWindow: true,
Mutexes: types.Mutexes{
RefreshingFilesMutex: &deadlock.Mutex{},
RefreshingBranchesMutex: &deadlock.Mutex{},
RefreshingStatusMutex: &deadlock.Mutex{},
LocalCommitsMutex: &deadlock.Mutex{},
SubCommitsMutex: &deadlock.Mutex{},
AuthorsMutex: &deadlock.Mutex{},
SubprocessMutex: &deadlock.Mutex{},
PopupMutex: &deadlock.Mutex{},
PtyMutex: &deadlock.Mutex{},
},
InitialDir: initialDir, InitialDir: initialDir,
afterLayoutFuncs: make(chan func() error, 1000), afterLayoutFuncs: make(chan func() error, 1000),
itemOperations: make(map[string]types.ItemOperation), itemOperations: make(map[string]types.ItemOperation),
itemOperationsMutex: &deadlock.Mutex{},
} }
gui.PopupHandler = popup.NewPopupHandler( gui.PopupHandler = popup.NewPopupHandler(

View File

@ -100,8 +100,8 @@ func (self *guiCommon) Model() *types.Model {
return self.gui.State.Model return self.gui.State.Model
} }
func (self *guiCommon) Mutexes() types.Mutexes { func (self *guiCommon) Mutexes() *types.Mutexes {
return self.gui.Mutexes return &self.gui.Mutexes
} }
func (self *guiCommon) GocuiGui() *gocui.Gui { func (self *guiCommon) GocuiGui() *gocui.Gui {

View File

@ -94,7 +94,7 @@ type IGuiCommon interface {
Modes() *Modes Modes() *Modes
Mutexes() Mutexes Mutexes() *Mutexes
State() IStateAccessor State() IStateAccessor
@ -313,18 +313,16 @@ type Model struct {
HashPool *utils.StringPool HashPool *utils.StringPool
} }
// if you add a new mutex here be sure to instantiate it. We're using pointers to
// mutexes so that we can pass the mutexes to controllers.
type Mutexes struct { type Mutexes struct {
RefreshingFilesMutex *deadlock.Mutex RefreshingFilesMutex deadlock.Mutex
RefreshingBranchesMutex *deadlock.Mutex RefreshingBranchesMutex deadlock.Mutex
RefreshingStatusMutex *deadlock.Mutex RefreshingStatusMutex deadlock.Mutex
LocalCommitsMutex *deadlock.Mutex LocalCommitsMutex deadlock.Mutex
SubCommitsMutex *deadlock.Mutex SubCommitsMutex deadlock.Mutex
AuthorsMutex *deadlock.Mutex AuthorsMutex deadlock.Mutex
SubprocessMutex *deadlock.Mutex SubprocessMutex deadlock.Mutex
PopupMutex *deadlock.Mutex PopupMutex deadlock.Mutex
PtyMutex *deadlock.Mutex PtyMutex deadlock.Mutex
} }
// A long-running operation associated with an item. For example, we'll show // A long-running operation associated with an item. For example, we'll show