1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-11-27 22:38:09 +02:00

Use mutex on cached git config

This fixes a race condition caused by a concurrent map read and write
This commit is contained in:
Jesse Duffield
2023-07-09 20:39:06 +10:00
parent 14ecc15e71
commit 8964cedf27

View File

@@ -3,6 +3,7 @@ package git_config
import ( import (
"os/exec" "os/exec"
"strings" "strings"
"sync"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -20,6 +21,7 @@ type CachedGitConfig struct {
cache map[string]string cache map[string]string
runGitConfigCmd func(*exec.Cmd) (string, error) runGitConfigCmd func(*exec.Cmd) (string, error)
log *logrus.Entry log *logrus.Entry
mutex sync.Mutex
} }
func NewStdCachedGitConfig(log *logrus.Entry) *CachedGitConfig { func NewStdCachedGitConfig(log *logrus.Entry) *CachedGitConfig {
@@ -31,10 +33,14 @@ func NewCachedGitConfig(runGitConfigCmd func(*exec.Cmd) (string, error), log *lo
cache: make(map[string]string), cache: make(map[string]string),
runGitConfigCmd: runGitConfigCmd, runGitConfigCmd: runGitConfigCmd,
log: log, log: log,
mutex: sync.Mutex{},
} }
} }
func (self *CachedGitConfig) Get(key string) string { func (self *CachedGitConfig) Get(key string) string {
self.mutex.Lock()
defer self.mutex.Unlock()
if value, ok := self.cache[key]; ok { if value, ok := self.cache[key]; ok {
self.log.Debugf("using cache for key " + key) self.log.Debugf("using cache for key " + key)
return value return value
@@ -46,6 +52,9 @@ func (self *CachedGitConfig) Get(key string) string {
} }
func (self *CachedGitConfig) GetGeneral(args string) string { func (self *CachedGitConfig) GetGeneral(args string) string {
self.mutex.Lock()
defer self.mutex.Unlock()
if value, ok := self.cache[args]; ok { if value, ok := self.cache[args]; ok {
self.log.Debugf("using cache for args " + args) self.log.Debugf("using cache for args " + args)
return value return value