diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-07 20:17:23 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-09 14:09:53 +1100 |
commit | e8229f0ee0eafb2d94674a091cb7a8776468e9c1 (patch) | |
tree | fd4ff9b117eafc5187a86f4945a5fa9e2038f32d /pkg | |
parent | 610e503296b36ef1d0551c957a6c593fae4cd60c (diff) |
support general git config calls
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/config.go | 2 | ||||
-rw-r--r-- | pkg/commands/flow.go | 2 | ||||
-rw-r--r-- | pkg/commands/git_config/cached_git_config.go | 45 | ||||
-rw-r--r-- | pkg/commands/git_config/fake_git_config.go | 7 | ||||
-rw-r--r-- | pkg/commands/git_config/get_key.go | 15 |
5 files changed, 56 insertions, 15 deletions
diff --git a/pkg/commands/config.go b/pkg/commands/config.go index c5f56c7b4..5d296ddb8 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/config.go @@ -95,5 +95,5 @@ func (self *ConfigCommands) Branches() (map[string]*config.Branch, error) { } func (self *ConfigCommands) GetGitFlowPrefixes() string { - return self.gitConfig.Get("--local --get-regexp gitflow.prefix.") + return self.gitConfig.GetGeneral("--local --get-regexp gitflow.prefix") } diff --git a/pkg/commands/flow.go b/pkg/commands/flow.go index 9506a7e85..0e0186f96 100644 --- a/pkg/commands/flow.go +++ b/pkg/commands/flow.go @@ -29,7 +29,7 @@ func NewFlowCommands( } func (self *FlowCommands) GitFlowEnabled() bool { - return self.config.GetGitFlowPrefixes() == "" + return self.config.GetGitFlowPrefixes() != "" } func (self *FlowCommands) FinishCmdObj(branchName string) (oscommands.ICmdObj, error) { diff --git a/pkg/commands/git_config/cached_git_config.go b/pkg/commands/git_config/cached_git_config.go index e2b83bad8..fe3bc1eca 100644 --- a/pkg/commands/git_config/cached_git_config.go +++ b/pkg/commands/git_config/cached_git_config.go @@ -1,31 +1,36 @@ package git_config import ( + "os/exec" "strings" "github.com/sirupsen/logrus" ) 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 + // 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 } type CachedGitConfig struct { - cache map[string]string - getKey func(string) (string, error) - log *logrus.Entry + cache map[string]string + runGitConfigCmd func(*exec.Cmd) (string, error) + log *logrus.Entry } 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{ - cache: make(map[string]string), - getKey: getKey, - log: log, + cache: make(map[string]string), + runGitConfigCmd: runGitConfigCmd, + log: log, } } @@ -40,8 +45,30 @@ func (self *CachedGitConfig) Get(key string) string { 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 { - value, err := self.getKey(key) + cmd := getGitConfigCmd(key) + value, err := self.runGitConfigCmd(cmd) if err != nil { self.log.Debugf("Error getting git config value for key: " + key + ". Error: " + err.Error()) return "" diff --git a/pkg/commands/git_config/fake_git_config.go b/pkg/commands/git_config/fake_git_config.go index f010efd8c..78ba62c54 100644 --- a/pkg/commands/git_config/fake_git_config.go +++ b/pkg/commands/git_config/fake_git_config.go @@ -17,6 +17,13 @@ func (self *FakeGitConfig) Get(key string) string { 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 { return isTruthy(self.Get(key)) } diff --git a/pkg/commands/git_config/get_key.go b/pkg/commands/git_config/get_key.go index 4ad5ae63e..bd6f59248 100644 --- a/pkg/commands/git_config/get_key.go +++ b/pkg/commands/git_config/get_key.go @@ -35,11 +35,8 @@ import ( // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -func getGitConfigValue(key string) (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, " ")...) +func runGitConfigCmd(cmd *exec.Cmd) (string, error) { var stdout bytes.Buffer - cmd := secureexec.Command("git", gitArgs...) cmd.Stdout = &stdout cmd.Stderr = ioutil.Discard @@ -55,3 +52,13 @@ func getGitConfigValue(key string) (string, error) { 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...) +} |