1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-17 01:42:45 +02:00

support general git config calls

This commit is contained in:
Jesse Duffield
2022-01-07 20:17:23 +11:00
parent 610e503296
commit e8229f0ee0
5 changed files with 56 additions and 15 deletions

View File

@ -95,5 +95,5 @@ func (self *ConfigCommands) Branches() (map[string]*config.Branch, error) {
} }
func (self *ConfigCommands) GetGitFlowPrefixes() string { func (self *ConfigCommands) GetGitFlowPrefixes() string {
return self.gitConfig.Get("--local --get-regexp gitflow.prefix.") return self.gitConfig.GetGeneral("--local --get-regexp gitflow.prefix")
} }

View File

@ -29,7 +29,7 @@ func NewFlowCommands(
} }
func (self *FlowCommands) GitFlowEnabled() bool { func (self *FlowCommands) GitFlowEnabled() bool {
return self.config.GetGitFlowPrefixes() == "" return self.config.GetGitFlowPrefixes() != ""
} }
func (self *FlowCommands) FinishCmdObj(branchName string) (oscommands.ICmdObj, error) { func (self *FlowCommands) FinishCmdObj(branchName string) (oscommands.ICmdObj, error) {

View File

@ -1,31 +1,36 @@
package git_config package git_config
import ( import (
"os/exec"
"strings" "strings"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type IGitConfig interface { type IGitConfig interface {
// this is for when you want to pass 'mykey' (it calls `git config --get --null mykey` under the hood)
Get(string) string Get(string) string
// 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
GetBool(string) bool GetBool(string) bool
} }
type CachedGitConfig struct { type CachedGitConfig struct {
cache map[string]string cache map[string]string
getKey func(string) (string, error) runGitConfigCmd func(*exec.Cmd) (string, error)
log *logrus.Entry log *logrus.Entry
} }
func NewStdCachedGitConfig(log *logrus.Entry) *CachedGitConfig { func NewStdCachedGitConfig(log *logrus.Entry) *CachedGitConfig {
return NewCachedGitConfig(getGitConfigValue, log) return NewCachedGitConfig(runGitConfigCmd, log)
} }
func NewCachedGitConfig(getKey func(string) (string, error), log *logrus.Entry) *CachedGitConfig { func NewCachedGitConfig(runGitConfigCmd func(*exec.Cmd) (string, error), log *logrus.Entry) *CachedGitConfig {
return &CachedGitConfig{ return &CachedGitConfig{
cache: make(map[string]string), cache: make(map[string]string),
getKey: getKey, runGitConfigCmd: runGitConfigCmd,
log: log, log: log,
} }
} }
@ -40,8 +45,30 @@ func (self *CachedGitConfig) Get(key string) string {
return value return value
} }
func (self *CachedGitConfig) GetGeneral(args string) string {
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)
}
func (self *CachedGitConfig) getAux(key string) string { func (self *CachedGitConfig) getAux(key string) string {
value, err := self.getKey(key) cmd := getGitConfigCmd(key)
value, err := self.runGitConfigCmd(cmd)
if err != nil { if err != nil {
self.log.Debugf("Error getting git config value for key: " + key + ". Error: " + err.Error()) self.log.Debugf("Error getting git config value for key: " + key + ". Error: " + err.Error())
return "" return ""

View File

@ -17,6 +17,13 @@ func (self *FakeGitConfig) Get(key string) string {
return self.mockResponses[key] return self.mockResponses[key]
} }
func (self *FakeGitConfig) GetGeneral(args string) string {
if self.mockResponses == nil {
return ""
}
return self.mockResponses[args]
}
func (self *FakeGitConfig) GetBool(key string) bool { func (self *FakeGitConfig) GetBool(key string) bool {
return isTruthy(self.Get(key)) return isTruthy(self.Get(key))
} }

View File

@ -35,11 +35,8 @@ import (
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
func getGitConfigValue(key string) (string, error) { func runGitConfigCmd(cmd *exec.Cmd) (string, error) {
// allowing caller to say that key is '--local mykey' so that they can add extra flags.
gitArgs := append([]string{"config", "--get", "--null"}, strings.Split(key, " ")...)
var stdout bytes.Buffer var stdout bytes.Buffer
cmd := secureexec.Command("git", gitArgs...)
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = ioutil.Discard cmd.Stderr = ioutil.Discard
@ -55,3 +52,13 @@ func getGitConfigValue(key string) (string, error) {
return strings.TrimRight(stdout.String(), "\000"), nil return strings.TrimRight(stdout.String(), "\000"), nil
} }
func getGitConfigCmd(key string) *exec.Cmd {
gitArgs := []string{"config", "--get", "--null", key}
return secureexec.Command("git", gitArgs...)
}
func getGitConfigGeneralCmd(args string) *exec.Cmd {
gitArgs := append([]string{"config"}, strings.Split(args, " ")...)
return secureexec.Command("git", gitArgs...)
}