Skip to content

Commit c17a0e3

Browse files
committed
Fix panic when git config contains a key without a value
1 parent cf98d83 commit c17a0e3

2 files changed

Lines changed: 69 additions & 10 deletions

File tree

‎main.go‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -838,16 +838,7 @@ func main() {
838838
log.V(0).Info("unexpected stderr reading git config", "stdout", stdout, "stderr", stderr)
839839
os.Exit(1)
840840
} else {
841-
cfgs := strings.Split(stdout, string(rune(0)))
842-
kvs := funcr.PseudoStruct{} // like a map but ordered
843-
for _, cfg := range cfgs {
844-
if cfg == "" {
845-
continue
846-
}
847-
parts := strings.SplitN(cfg, "\n", 2) // any additional newlines are part of the value
848-
kvs = append(kvs, parts[0], parts[1])
849-
}
850-
log.V(0).Info("git config", "configs", kvs)
841+
log.V(0).Info("git config", "configs", parseGitConfigList(stdout))
851842
}
852843

853844
// The scope of the initialization context ends here, so we call cancel to release resources associated with it.
@@ -2324,6 +2315,26 @@ type keyVal struct {
23242315
val string
23252316
}
23262317

2318+
// parseGitConfigList parses the output of `git config list -z`. A key with no
2319+
// value (e.g. a bare `key` line, which git treats as boolean true) is printed
2320+
// without a newline, and is reported with a nil value.
2321+
func parseGitConfigList(stdout string) funcr.PseudoStruct {
2322+
cfgs := strings.Split(stdout, string(rune(0)))
2323+
kvs := funcr.PseudoStruct{} // like a map but ordered
2324+
for _, cfg := range cfgs {
2325+
if cfg == "" {
2326+
continue
2327+
}
2328+
parts := strings.SplitN(cfg, "\n", 2) // any additional newlines are part of the value
2329+
if len(parts) == 1 {
2330+
kvs = append(kvs, parts[0], nil)
2331+
continue
2332+
}
2333+
kvs = append(kvs, parts[0], parts[1])
2334+
}
2335+
return kvs
2336+
}
2337+
23272338
func parseGitConfigs(configsFlag string) ([]keyVal, error) {
23282339
// Use a channel as a FIFO. We don't expect the input strings to be very
23292340
// large, so this simple model should suffice.

‎main_test.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27+
"github.com/go-logr/logr/funcr"
2728
"go.uber.org/goleak"
2829
)
2930

@@ -248,6 +249,53 @@ func TestParseGitConfigs(t *testing.T) {
248249
}
249250
}
250251

252+
func TestParseGitConfigList(t *testing.T) {
253+
cases := []struct {
254+
name string
255+
input string
256+
expect funcr.PseudoStruct
257+
}{{
258+
name: "empty",
259+
input: "",
260+
expect: funcr.PseudoStruct{},
261+
}, {
262+
name: "one-pair",
263+
input: "k\nv\x00",
264+
expect: funcr.PseudoStruct{"k", "v"},
265+
}, {
266+
name: "no-trailing-nul",
267+
input: "k\nv",
268+
expect: funcr.PseudoStruct{"k", "v"},
269+
}, {
270+
name: "empty-value",
271+
input: "k\n\x00",
272+
expect: funcr.PseudoStruct{"k", ""},
273+
}, {
274+
name: "multi-line-value",
275+
input: "k\nline1\nline2\x00",
276+
expect: funcr.PseudoStruct{"k", "line1\nline2"},
277+
}, {
278+
// `git config list -z` prints a key with no value (e.g. a bare `key`
279+
// line, which git treats as boolean true) without a newline.
280+
name: "no-value",
281+
input: "k\x00",
282+
expect: funcr.PseudoStruct{"k", nil},
283+
}, {
284+
name: "multiple",
285+
input: "core.bare\nfalse\x00demo.bare\x00demo.empty\n\x00demo.multi\nline1\nline2\x00",
286+
expect: funcr.PseudoStruct{"core.bare", "false", "demo.bare", nil, "demo.empty", "", "demo.multi", "line1\nline2"},
287+
}}
288+
289+
for _, tc := range cases {
290+
t.Run(tc.name, func(t *testing.T) {
291+
kvs := parseGitConfigList(tc.input)
292+
if !reflect.DeepEqual(kvs, tc.expect) {
293+
t.Errorf("bad result:\n\texpected: %#v\n\t got: %#v", tc.expect, kvs)
294+
}
295+
})
296+
}
297+
}
298+
251299
func TestDirIsEmpty(t *testing.T) {
252300
root := absPath(t.TempDir())
253301

0 commit comments

Comments
 (0)