Skip to content

[bugfix] Fix parsing of key-value pairs according to the gitconfig #2911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/gitconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,15 @@ func parseConfig(in io.Reader, key, value string, cb parseFunc) []string {
lines = append(lines, fullLine)

line := strings.TrimSpace(fullLine)
// Handle full-line comments
if strings.HasPrefix(line, "#") {
continue
}
if strings.HasPrefix(line, ";") {
continue
}

// Handle section headers
if strings.HasPrefix(line, "[") {
s, subs, skip := parseSectionHeader(line)
if skip {
Expand All @@ -312,12 +315,16 @@ func parseConfig(in io.Reader, key, value string, cb parseFunc) []string {
// [core]
// sslVerify
// These are odd but we should still support them.
k, v, found := strings.Cut(line, " = ")
// Check https://git-scm.com/docs/git-config#_syntax for more details.
k, v, found := strings.Cut(line, "=")
if !found {
debug.V(3).Log("no valid KV-pair on line: %q", line)

continue
}
// Remove whitespace from key and value that might be around the '='
k = strings.TrimRight(k, " ")
v = strings.TrimLeft(v, " ")

fKey := section + "."
if subsection != "" {
Expand All @@ -331,6 +338,7 @@ func parseConfig(in io.Reader, key, value string, cb parseFunc) []string {
oValue := v
comment := ""

// Handle inline comments
if strings.ContainsAny(oValue, "#;") {
comment = " " + oValue[strings.IndexAny(oValue, "#;"):]
oValue = oValue[:strings.IndexAny(oValue, "#;")]
Expand Down
Loading