1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/commands/git_config/cached_git_config.go

96 lines
2.4 KiB
Go
Raw Normal View History

2021-10-23 00:52:19 +02:00
package git_config
import (
2022-01-07 11:17:23 +02:00
"os/exec"
2021-10-23 00:52:19 +02:00
"strings"
"sync"
2021-10-23 00:52:19 +02:00
"github.com/sirupsen/logrus"
)
type IGitConfig interface {
2022-01-07 11:17:23 +02:00
// this is for when you want to pass 'mykey' (it calls `git config --get --null mykey` under the hood)
2021-10-23 00:52:19 +02:00
Get(string) string
2022-01-07 11:17:23 +02:00
// this is for when you want to pass '--local --get-regexp mykey'
GetGeneral(string) string
// this is for when you want to pass 'mykey' and check if the result is truthy
2021-10-23 00:52:19 +02:00
GetBool(string) bool
}
type CachedGitConfig struct {
2022-01-07 11:17:23 +02:00
cache map[string]string
runGitConfigCmd func(*exec.Cmd) (string, error)
log *logrus.Entry
mutex sync.Mutex
2021-10-23 00:52:19 +02:00
}
func NewStdCachedGitConfig(log *logrus.Entry) *CachedGitConfig {
2022-01-07 11:17:23 +02:00
return NewCachedGitConfig(runGitConfigCmd, log)
2021-10-23 00:52:19 +02:00
}
2022-01-07 11:17:23 +02:00
func NewCachedGitConfig(runGitConfigCmd func(*exec.Cmd) (string, error), log *logrus.Entry) *CachedGitConfig {
2021-10-23 00:52:19 +02:00
return &CachedGitConfig{
2022-01-07 11:17:23 +02:00
cache: make(map[string]string),
runGitConfigCmd: runGitConfigCmd,
log: log,
mutex: sync.Mutex{},
2021-10-23 00:52:19 +02:00
}
}
func (self *CachedGitConfig) Get(key string) string {
self.mutex.Lock()
defer self.mutex.Unlock()
2021-10-23 00:52:19 +02:00
if value, ok := self.cache[key]; ok {
self.log.Debugf("using cache for key " + key)
return value
}
value := self.getAux(key)
self.cache[key] = value
return value
}
2022-01-07 11:17:23 +02:00
func (self *CachedGitConfig) GetGeneral(args string) string {
self.mutex.Lock()
defer self.mutex.Unlock()
2022-01-07 11:17:23 +02:00
if value, ok := self.cache[args]; ok {
self.log.Debugf("using cache for args " + args)
return value
}
value := self.getGeneralAux(args)
self.cache[args] = value
return value
}
func (self *CachedGitConfig) getGeneralAux(args string) string {
cmd := getGitConfigGeneralCmd(args)
value, err := self.runGitConfigCmd(cmd)
if err != nil {
self.log.Debugf("Error getting git config value for args: " + args + ". Error: " + err.Error())
return ""
}
return strings.TrimSpace(value)
}
2021-10-23 00:52:19 +02:00
func (self *CachedGitConfig) getAux(key string) string {
2022-01-07 11:17:23 +02:00
cmd := getGitConfigCmd(key)
value, err := self.runGitConfigCmd(cmd)
2021-10-23 00:52:19 +02:00
if err != nil {
self.log.Debugf("Error getting git config value for key: " + key + ". Error: " + err.Error())
return ""
}
return strings.TrimSpace(value)
}
func (self *CachedGitConfig) GetBool(key string) bool {
return isTruthy(self.Get(key))
}
func isTruthy(value string) bool {
lcValue := strings.ToLower(value)
return lcValue == "true" || lcValue == "1" || lcValue == "yes" || lcValue == "on"
}