diff --git a/internal/git/git.go b/internal/git/git.go index 92c9229..25e7d2a 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -7,8 +7,6 @@ import ( "github.com/git-pkgs/manifests/internal/core" ) -const keyValueParts = 2 // key=value - func init() { core.Register("git", core.Manifest, &gitmodulesParser{}, core.ExactMatch(".gitmodules")) } @@ -27,11 +25,17 @@ type submodule struct { func (p *gitmodulesParser) Parse(filename string, content []byte) (*core.Result, error) { var deps []core.Dependency - lines := strings.Split(string(content), "\n") + s := string(content) var current *submodule - for _, line := range lines { - line = strings.TrimSpace(line) + for pos := 0; pos < len(s); { + start := pos + pos = lineEnd(s, start) + physical := strings.TrimSuffix(strings.TrimSuffix(s[start:pos], "\n"), "\r") + line := strings.TrimSpace(physical) + if strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") { + continue + } if match := submoduleHeaderRegex.FindStringSubmatch(line); match != nil { if current != nil { @@ -45,7 +49,13 @@ func (p *gitmodulesParser) Parse(filename string, content []byte) (*core.Result, continue } - key, val, ok := parseKeyValue(line) + eq := strings.IndexByte(physical, '=') + if eq < 0 { + continue + } + key := strings.ToLower(strings.TrimSpace(physical[:eq])) + val, n, ok := configValue(s[start+eq+1:]) + pos = start + eq + 1 + n if !ok { continue } @@ -89,10 +99,83 @@ func (s *submodule) dependency() core.Dependency { return dep } -func parseKeyValue(line string) (key, value string, ok bool) { - parts := strings.SplitN(line, "=", keyValueParts) - if len(parts) != keyValueParts { - return "", "", false +// lineEnd returns the index just past the newline at or after i, or len(s). +func lineEnd(s string, i int) int { + if j := strings.IndexByte(s[i:], '\n'); j >= 0 { + return i + j + 1 + } + return len(s) +} + +// configValue decodes the value at the start of s as git's parse_value does, in one pass; n is the bytes used through the newline that ends it, !ok means git rejects it. +func configValue(s string) (value string, n int, ok bool) { + var b strings.Builder + quote, comment := false, false + trim := 0 + for n < len(s) { + c := s[n] + n++ + if c == '\r' && n < len(s) && s[n] == '\n' { + c = '\n' + n++ + } + if c == '\n' { + break + } + if comment { + continue + } + if !quote && (c == ' ' || c == '\t' || c == '\r') { + if trim == 0 { + trim = b.Len() + } + if b.Len() > 0 { + b.WriteByte(c) + } + continue + } + if !quote && (c == ';' || c == '#') { + comment = true + continue + } + trim = 0 + switch c { + case '\\': + if n == len(s) { + continue + } + e := s[n] + n++ + if e == '\r' && n < len(s) && s[n] == '\n' { + e = '\n' + n++ + } + switch e { + case '\n': + continue + case 't': + b.WriteByte('\t') + case 'b': + b.WriteByte('\b') + case 'n': + b.WriteByte('\n') + case '\\', '"': + b.WriteByte(e) + default: + return "", lineEnd(s, n), false + } + case '"': + quote = !quote + default: + b.WriteByte(c) + } + } + if quote { + return "", n, false + } + value = b.String() + if trim > 0 { + value = value[:trim] } - return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), true + return value, n, true } diff --git a/internal/git/git_test.go b/internal/git/git_test.go index e114d05..ffeae13 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -3,6 +3,7 @@ package git import ( "os" "reflect" + "strings" "testing" "github.com/git-pkgs/manifests/internal/core" @@ -51,3 +52,127 @@ func TestGitmodulesWithoutURL(t *testing.T) { t.Errorf("got %+v, want %+v", res.Dependencies, want) } } + +func TestGitmodulesQuotedValues(t *testing.T) { + content := "[submodule \"lib/foo\"]\n" + + "\tpath = \"lib/foo\"\n" + + "\turl = \"https://github.com/example/foo.git\" ; mirror\n" + + "\tbranch = \"fix#1\" # pinned\n" + + "[submodule \"bar\"]\r\n" + + "\tpath = bar\r\n" + + "\turl = https://github.com/example/\\\r\n" + + "bar.git\r\n" + res, err := (&gitmodulesParser{}).Parse(".gitmodules", []byte(content)) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := []core.Dependency{ + {Name: "github.com/example/foo", Scope: core.Runtime, Direct: true, + Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/example/foo.git", Branch: "fix#1"}}, + {Name: "github.com/example/bar", Scope: core.Runtime, Direct: true, + Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/example/bar.git"}}, + } + if !reflect.DeepEqual(res.Dependencies, want) { + t.Errorf("got %+v, want %+v", res.Dependencies, want) + } +} + +func TestConfigValue(t *testing.T) { + tests := []struct { + in, want string + n int + ok bool + }{ + {`plain`, "plain", 5, true}, + {"plain\nnext", "plain", 6, true}, + {`"quoted value"`, "quoted value", 14, true}, + {`a "b c" d`, "a b c d", 9, true}, + {`trailing ; comment`, "trailing", 20, true}, + {`"kept " # comment`, "kept ", 19, true}, + {`esc\"aped\\ \t\n`, "esc\"aped\\ \t\n", 16, true}, + {"a\\\nb\nc", "ab", 5, true}, + {"x\\\r\ny\r\nnext", "xy", 7, true}, + {`ends with \`, "ends with ", 11, true}, + {"# comment \\\nnext", "", 12, true}, + {"a \v", "a \v", 3, true}, + {"a \f\r", "a \f", 4, true}, + {`"open`, "", 5, false}, + {"\"open\nnext", "", 6, false}, + {"bad\\q rest\nnext", "", 11, false}, + } + for _, tt := range tests { + got, n, ok := configValue(tt.in) + if got != tt.want || n != tt.n || ok != tt.ok { + t.Errorf("configValue(%q) = %q, %d, %v; want %q, %d, %v", tt.in, got, n, ok, tt.want, tt.n, tt.ok) + } + } +} + +func TestGitmodulesManyContinuationLines(t *testing.T) { + const lines = 5000 + content := "[submodule \"one\"]\n\tpath = one\n\turl = https://github.com/example/one.git\n\tbranch = " + + strings.Repeat("a\\\n", lines) + "\n[submodule \"two\"]\n\tpath = two\n" + res, err := (&gitmodulesParser{}).Parse(".gitmodules", []byte(content)) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := []core.Dependency{{Name: "github.com/example/one", Scope: core.Runtime, Direct: true, + Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/example/one.git", Branch: strings.Repeat("a", lines)}}, + {Name: "two", Scope: core.Runtime, Direct: true, NoPURL: true}} + if !reflect.DeepEqual(res.Dependencies, want) { + t.Errorf("got %d deps, want the branch joined from %d continuation lines and the submodule after it", len(res.Dependencies), lines) + } +} + +func TestGitmodulesKeysIgnoreCase(t *testing.T) { + content := "[submodule \"one\"]\n" + + "\tPath = one\n" + + "\tURL = https://github.com/example/one.git\n" + + "\tBranch = Main\n" + res, err := (&gitmodulesParser{}).Parse(".gitmodules", []byte(content)) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := []core.Dependency{{Name: "github.com/example/one", Scope: core.Runtime, Direct: true, + Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/example/one.git", Branch: "Main"}}} + if !reflect.DeepEqual(res.Dependencies, want) { + t.Errorf("got %+v, want %+v", res.Dependencies, want) + } +} + +func TestGitmodulesCommentLineEndingInBackslash(t *testing.T) { + content := "[submodule \"one\"]\n" + + "\tpath = one\n" + + "# example = \\\n" + + "[submodule \"two\"]\n" + + "\tpath = two\n" + + "\t; note = \\\n" + + "\turl = https://github.com/example/two.git\n" + res, err := (&gitmodulesParser{}).Parse(".gitmodules", []byte(content)) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := []core.Dependency{ + {Name: "one", Scope: core.Runtime, Direct: true, NoPURL: true}, + {Name: "github.com/example/two", Scope: core.Runtime, Direct: true, + Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/example/two.git"}}, + } + if !reflect.DeepEqual(res.Dependencies, want) { + t.Errorf("got %+v, want %+v", res.Dependencies, want) + } +} + +func TestGitmodulesBackslashBeforeTrailingSpace(t *testing.T) { + content := "[submodule \"one\"]\n" + + "\tpath = one\n" + + "\turl = https://github.com/example/one.git\\ \n" + + "\tbranch = main\n" + res, err := (&gitmodulesParser{}).Parse(".gitmodules", []byte(content)) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := []core.Dependency{{Name: "one", Scope: core.Runtime, Direct: true, NoPURL: true}} + if !reflect.DeepEqual(res.Dependencies, want) { + t.Errorf("got %+v, want %+v", res.Dependencies, want) + } +}