1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +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 (
"os/exec"
"strings"
"sync"
"github.com/sirupsen/logrus"
)
@ -20,6 +21,7 @@ type CachedGitConfig struct {
cache map[string]string
runGitConfigCmd func(*exec.Cmd) (string, error)
log *logrus.Entry
mutex sync.Mutex
}
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),
runGitConfigCmd: runGitConfigCmd,
log: log,
mutex: sync.Mutex{},
}
}
func (self *CachedGitConfig) Get(key string) string {
self.mutex.Lock()
defer self.mutex.Unlock()
if value, ok := self.cache[key]; ok {
self.log.Debugf("using cache for key " + key)
return value
@ -46,6 +52,9 @@ func (self *CachedGitConfig) Get(key string) string {
}
func (self *CachedGitConfig) GetGeneral(args string) string {
self.mutex.Lock()
defer self.mutex.Unlock()
if value, ok := self.cache[args]; ok {
self.log.Debugf("using cache for args " + args)
return value