Skip to content

Commit

Permalink
[gitconfig] Fix splitting of Key-Value pairs (#2482)
Browse files Browse the repository at this point in the history
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 <dominik.schulz@gauner.org>

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz authored Dec 18, 2022
1 parent f90204c commit 0658d53
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
17 changes: 11 additions & 6 deletions pkg/gitconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,27 @@ 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
}

fKey := section + "."
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, "#;") {
Expand Down
25 changes: 24 additions & 1 deletion pkg/gitconfig/gitconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -308,14 +310,35 @@ 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))

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) {
Expand Down

0 comments on commit 0658d53

Please sign in to comment.