From 0658d530b795d0f0228add745ca5f614941e0169 Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Sun, 18 Dec 2022 16:50:50 +0100 Subject: [PATCH] [gitconfig] Fix splitting of Key-Value pairs (#2482) Previously we were ignoring anything after the second equal sign. This was invalid. Fixes #2479 RELEASE_NOTES=[BUGFIX] [gitconfig] Properly parse Key-Value pairs with multiple equal signs. Signed-off-by: Dominik Schulz Signed-off-by: Dominik Schulz --- pkg/gitconfig/config.go | 17 +++++++++++------ pkg/gitconfig/gitconfig_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/pkg/gitconfig/config.go b/pkg/gitconfig/config.go index c91edcf4d2..f5e5d0caad 100644 --- a/pkg/gitconfig/config.go +++ b/pkg/gitconfig/config.go @@ -256,9 +256,14 @@ func parseConfig(in io.Reader, key, value string, cb parseFunc) []string { continue } - kvp := strings.Split(line, "=") - trim(kvp) - if len(kvp) < 2 { + // TODO(gitconfig) This will skip over valid entries like this one: + // [core] + // sslVerify + // These are odd but we should still support them. + k, v, found := strings.Cut(line, " = ") + if !found { + debug.Log("no valid KV-pair on line: %q", line) + continue } @@ -266,12 +271,12 @@ func parseConfig(in io.Reader, key, value string, cb parseFunc) []string { if subsection != "" { fKey += subsection + "." } - fKey += kvp[0] + fKey += k if key == "" { - wKey = kvp[0] + wKey = k } - oValue := kvp[1] + oValue := v comment := "" if strings.ContainsAny(oValue, "#;") { diff --git a/pkg/gitconfig/gitconfig_test.go b/pkg/gitconfig/gitconfig_test.go index b70ab31a86..93604bd3ec 100644 --- a/pkg/gitconfig/gitconfig_test.go +++ b/pkg/gitconfig/gitconfig_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "golang.org/x/exp/maps" ) // https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-config.html#EXAMPLES @@ -168,6 +169,7 @@ var configSampleComplex = ` autocrlf = input protectHFS = true protectNTFS = true + sshCommand = ssh -oControlMaster=auto -oControlPersist=600 -oControlPath=/tmp/.ssh-%C [receive] fsckObjects = true @@ -308,7 +310,7 @@ func TestGopass(t *testing.T) { t.Logf("Vars:\n%+v\n", c.global.vars) } -func TestParse(t *testing.T) { +func TestParseSimple(t *testing.T) { t.Parallel() c := ParseConfig(strings.NewReader(configSampleDocs)) @@ -316,6 +318,27 @@ func TestParse(t *testing.T) { for k, v := range c.vars { t.Logf("%s => %s\n", k, v) } + + want := map[string]string{ + "core.filemode": "false", + "diff.external": "/usr/local/bin/diff-wrapper", + "diff.renames": "true", + "core.gitproxy": "default-proxy", + // TODO(gitconfig): "http.sslVerify": "", // not supported, yet + "http.https://weak.example.com.sslVerify": "false", + "http.https://weak.example.com.cookieFile": "/tmp/cookie.txt", + } + + assert.Equal(t, want, c.vars) +} + +func TestParseComplex(t *testing.T) { + t.Parallel() + + c := ParseConfig(strings.NewReader(configSampleComplex)) + + assert.Contains(t, maps.Keys(c.vars), "core.sshCommand") + assert.Equal(t, "ssh -oControlMaster=auto -oControlPersist=600 -oControlPath=/tmp/.ssh-%C", c.vars["core.sshCommand"]) } func TestGitBinary(t *testing.T) {