diff options
| author | 2025-11-16 00:00:00 +0000 | |
|---|---|---|
| committer | 2025-11-16 00:00:00 +0000 | |
| commit | 28a599b2b90fd6c657f0eda2387a77a34f31c2c3 (patch) | |
| tree | d08a7816c6d1cb92dab174e58eaf23b95d600ab6 | |
| parent | Make the API more consistent (diff) | |
| signature | ||
Add basic support for parsing configuration files
Now support for switching hash algorithms should be complete!
| -rw-r--r-- | config.go | 485 | ||||
| -rw-r--r-- | config_test.go | 365 | ||||
| -rw-r--r-- | repo.go | 41 | ||||
| -rw-r--r-- | repo_test.go | 47 |
4 files changed, 926 insertions, 12 deletions
diff --git a/config.go b/config.go new file mode 100644 index 00000000..b60a5f9d --- /dev/null +++ b/config.go @@ -0,0 +1,485 @@ +package furgit + +import ( + "bufio" + "bytes" + "errors" + "fmt" + "io" + "strings" + "unicode" +) + +// Config represents a parsed Git configuration. +type Config struct { + entries []ConfigEntry +} + +// ConfigEntry represents a single configuration key-value pair. +type ConfigEntry struct { + Section string + Subsection string + Key string + Value string +} + +// ParseConfig parses a Git configuration from a reader. +func ParseConfig(r io.Reader) (*Config, error) { + parser := &configParser{ + reader: bufio.NewReader(r), + lineNum: 1, + } + return parser.parse() +} + +// Get retrieves the first value for a given section, optional subsection, and key. +// Returns an empty string if not found. +func (c *Config) Get(section, subsection, key string) string { + section = strings.ToLower(section) + key = strings.ToLower(key) + for _, entry := range c.entries { + if strings.EqualFold(entry.Section, section) && + entry.Subsection == subsection && + strings.EqualFold(entry.Key, key) { + return entry.Value + } + } + return "" +} + +// GetAll retrieves all values for a given section, optional subsection, and key. +func (c *Config) GetAll(section, subsection, key string) []string { + section = strings.ToLower(section) + key = strings.ToLower(key) + var values []string + for _, entry := range c.entries { + if strings.EqualFold(entry.Section, section) && + entry.Subsection == subsection && + strings.EqualFold(entry.Key, key) { + values = append(values, entry.Value) + } + } + return values +} + +// Entries returns all configuration entries. +func (c *Config) Entries() []ConfigEntry { + result := make([]ConfigEntry, len(c.entries)) + copy(result, c.entries) + return result +} + +// configParser implements Git config file parsing using character-based reading. +type configParser struct { + reader *bufio.Reader + lineNum int + currentSection string + currentSubsec string + peeked rune + hasPeeked bool +} + +func (p *configParser) parse() (*Config, error) { + cfg := &Config{} + + if err := p.skipBOM(); err != nil { + return nil, err + } + + for { + ch, err := p.nextChar() + if err == io.EOF { + break + } + if err != nil { + return nil, err + } + + // Skip whitespace and newlines + if ch == '\n' || unicode.IsSpace(ch) { + continue + } + + // Comments + if ch == '#' || ch == ';' { + if err := p.skipToEOL(); err != nil && err != io.EOF { + return nil, err + } + continue + } + + // Section header + if ch == '[' { + if err := p.parseSection(); err != nil { + return nil, fmt.Errorf("furgit: config: line %d: %w", p.lineNum, err) + } + continue + } + + // Key-value pair + if unicode.IsLetter(ch) { + p.unreadChar(ch) + if err := p.parseKeyValue(cfg); err != nil { + return nil, fmt.Errorf("furgit: config: line %d: %w", p.lineNum, err) + } + continue + } + + return nil, fmt.Errorf("furgit: config: line %d: unexpected character %q", p.lineNum, ch) + } + + return cfg, nil +} + +func (p *configParser) nextChar() (rune, error) { + if p.hasPeeked { + p.hasPeeked = false + return p.peeked, nil + } + + ch, _, err := p.reader.ReadRune() + if err != nil { + return 0, err + } + + if ch == '\r' { + next, _, err := p.reader.ReadRune() + if err == nil && next == '\n' { + ch = '\n' + } else if err == nil { + // Weird but ok + _ = p.reader.UnreadRune() + } + } + + if ch == '\n' { + p.lineNum++ + } + + return ch, nil +} + +func (p *configParser) unreadChar(ch rune) { + p.peeked = ch + p.hasPeeked = true + if ch == '\n' && p.lineNum > 1 { + p.lineNum-- + } +} + +func (p *configParser) skipBOM() error { + first, _, err := p.reader.ReadRune() + if err == io.EOF { + return nil + } + if err != nil { + return err + } + if first != '\uFEFF' { + _ = p.reader.UnreadRune() + } + return nil +} + +func (p *configParser) skipToEOL() error { + for { + ch, err := p.nextChar() + if err != nil { + return err + } + if ch == '\n' { + return nil + } + } +} + +func (p *configParser) parseSection() error { + var name bytes.Buffer + + for { + ch, err := p.nextChar() + if err != nil { + return errors.New("unexpected EOF in section header") + } + + if ch == ']' { + section := name.String() + if !isValidSection(section) { + return fmt.Errorf("invalid section name: %q", section) + } + p.currentSection = strings.ToLower(section) + p.currentSubsec = "" + return nil + } + + if unicode.IsSpace(ch) { + return p.parseExtendedSection(&name) + } + + if !isKeyChar(ch) && ch != '.' { + return fmt.Errorf("invalid character in section name: %q", ch) + } + + name.WriteRune(unicode.ToLower(ch)) + } +} + +func (p *configParser) parseExtendedSection(sectionName *bytes.Buffer) error { + for { + ch, err := p.nextChar() + if err != nil { + return errors.New("unexpected EOF in section header") + } + if !unicode.IsSpace(ch) { + if ch != '"' { + return errors.New("expected quote after section name") + } + break + } + } + + var subsec bytes.Buffer + for { + ch, err := p.nextChar() + if err != nil { + return errors.New("unexpected EOF in subsection") + } + + if ch == '\n' { + return errors.New("newline in subsection") + } + + if ch == '"' { + break + } + + if ch == '\\' { + next, err := p.nextChar() + if err != nil { + return errors.New("unexpected EOF after backslash in subsection") + } + if next == '\n' { + return errors.New("newline after backslash in subsection") + } + subsec.WriteRune(next) + } else { + subsec.WriteRune(ch) + } + } + + ch, err := p.nextChar() + if err != nil { + return errors.New("unexpected EOF after subsection") + } + if ch != ']' { + return fmt.Errorf("expected ']' after subsection, got %q", ch) + } + + section := sectionName.String() + if !isValidSection(section) { + return fmt.Errorf("invalid section name: %q", section) + } + + p.currentSection = strings.ToLower(section) + p.currentSubsec = subsec.String() + return nil +} + +func (p *configParser) parseKeyValue(cfg *Config) error { + if p.currentSection == "" { + return errors.New("key-value pair before any section header") + } + + var key bytes.Buffer + for { + ch, err := p.nextChar() + if err != nil { + return errors.New("unexpected EOF reading key") + } + + if ch == '=' || ch == '\n' || unicode.IsSpace(ch) { + p.unreadChar(ch) + break + } + + if !isKeyChar(ch) { + return fmt.Errorf("invalid character in key: %q", ch) + } + + key.WriteRune(unicode.ToLower(ch)) + } + + keyStr := key.String() + if len(keyStr) == 0 { + return errors.New("empty key name") + } + if !unicode.IsLetter(rune(keyStr[0])) { + return errors.New("key must start with a letter") + } + + for { + ch, err := p.nextChar() + if err == io.EOF { + cfg.entries = append(cfg.entries, ConfigEntry{ + Section: p.currentSection, + Subsection: p.currentSubsec, + Key: keyStr, + Value: "true", + }) + return nil + } + if err != nil { + return err + } + + if ch == '\n' { + cfg.entries = append(cfg.entries, ConfigEntry{ + Section: p.currentSection, + Subsection: p.currentSubsec, + Key: keyStr, + Value: "true", + }) + return nil + } + + if ch == '#' || ch == ';' { + if err := p.skipToEOL(); err != nil && err != io.EOF { + return err + } + cfg.entries = append(cfg.entries, ConfigEntry{ + Section: p.currentSection, + Subsection: p.currentSubsec, + Key: keyStr, + Value: "true", + }) + return nil + } + + if ch == '=' { + break + } + + if !unicode.IsSpace(ch) { + return fmt.Errorf("unexpected character after key: %q", ch) + } + } + + value, err := p.parseValue() + if err != nil { + return err + } + + cfg.entries = append(cfg.entries, ConfigEntry{ + Section: p.currentSection, + Subsection: p.currentSubsec, + Key: keyStr, + Value: value, + }) + + return nil +} + +func (p *configParser) parseValue() (string, error) { + var value bytes.Buffer + var inQuote bool + var inComment bool + trimLen := 0 + + for { + ch, err := p.nextChar() + if err == io.EOF { + if inQuote { + return "", errors.New("unexpected EOF in quoted value") + } + if trimLen > 0 { + return value.String()[:trimLen], nil + } + return value.String(), nil + } + if err != nil { + return "", err + } + + if ch == '\n' { + if inQuote { + return "", errors.New("newline in quoted value") + } + if trimLen > 0 { + return value.String()[:trimLen], nil + } + return value.String(), nil + } + + if inComment { + continue + } + + if unicode.IsSpace(ch) && !inQuote { + if trimLen == 0 && value.Len() > 0 { + trimLen = value.Len() + } + if value.Len() > 0 { + value.WriteRune(ch) + } + continue + } + + if !inQuote && (ch == '#' || ch == ';') { + inComment = true + continue + } + + if trimLen > 0 { + trimLen = 0 + } + + if ch == '\\' { + next, err := p.nextChar() + if err == io.EOF { + return "", errors.New("unexpected EOF after backslash") + } + if err != nil { + return "", err + } + + switch next { + case '\n': + continue + case 'n': + value.WriteRune('\n') + case 't': + value.WriteRune('\t') + case 'b': + value.WriteRune('\b') + case '\\', '"': + value.WriteRune(next) + default: + return "", fmt.Errorf("invalid escape sequence: \\%c", next) + } + continue + } + + if ch == '"' { + inQuote = !inQuote + continue + } + + value.WriteRune(ch) + } +} + +func isValidSection(s string) bool { + if len(s) == 0 { + return false + } + for _, ch := range s { + if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) && ch != '-' && ch != '.' { + return false + } + } + return true +} + +func isKeyChar(ch rune) bool { + return unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '-' +} diff --git a/config_test.go b/config_test.go new file mode 100644 index 00000000..65a5c504 --- /dev/null +++ b/config_test.go @@ -0,0 +1,365 @@ +package furgit + +import ( + "strings" + "testing" +) + +func TestParseConfigSimple(t *testing.T) { + input := ` +[core] + repositoryformatversion = 0 + filemode = true + bare = false + +[user] + name = Alice Example + email = alice@example.com +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("core", "", "repositoryformatversion"); got != "0" { + t.Errorf("core.repositoryformatversion = %q, want %q", got, "0") + } + if got := cfg.Get("core", "", "filemode"); got != "true" { + t.Errorf("core.filemode = %q, want %q", got, "true") + } + if got := cfg.Get("user", "", "name"); got != "Alice Example" { + t.Errorf("user.name = %q, want %q", got, "Alice Example") + } + if got := cfg.Get("user", "", "email"); got != "alice@example.com" { + t.Errorf("user.email = %q, want %q", got, "alice@example.com") + } +} + +func TestParseConfigSubsection(t *testing.T) { + input := ` +[remote "origin"] + url = https://villosa.example.org/group1/group2//repos/repo + fetch = +refs/heads/*:refs/remotes/origin/* +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("remote", "origin", "url"); got != "https://villosa.example.org/group1/group2//repos/repo" { + t.Errorf("remote.origin.url = %q, want %q", got, "https://villosa.example.org/group1/group2//repos/repo") + } + if got := cfg.Get("remote", "origin", "fetch"); got != "+refs/heads/*:refs/remotes/origin/*" { + t.Errorf("remote.origin.fetch = %q, want %q", got, "+refs/heads/*:refs/remotes/origin/*") + } +} + +func TestParseConfigCaseInsensitive(t *testing.T) { + input := ` +[Core] + FileMode = true +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("core", "", "filemode"); got != "true" { + t.Errorf("core.filemode = %q, want %q", got, "true") + } + if got := cfg.Get("CORE", "", "FILEMODE"); got != "true" { + t.Errorf("CORE.FILEMODE = %q, want %q", got, "true") + } +} + +func TestParseConfigBooleanKeys(t *testing.T) { + input := ` +[core] + bare + ignorecase +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("core", "", "bare"); got != "true" { + t.Errorf("core.bare = %q, want %q", got, "true") + } + if got := cfg.Get("core", "", "ignorecase"); got != "true" { + t.Errorf("core.ignorecase = %q, want %q", got, "true") + } +} + +func TestParseConfigQuotedValues(t *testing.T) { + input := ` +[user] + name = "Bob Smith" + comment = "Has a \"quoted\" word" +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("user", "", "name"); got != "Bob Smith" { + t.Errorf("user.name = %q, want %q", got, "Bob Smith") + } + if got := cfg.Get("user", "", "comment"); got != `Has a "quoted" word` { + t.Errorf("user.comment = %q, want %q", got, `Has a "quoted" word`) + } +} + +func TestParseConfigEscapeSequences(t *testing.T) { + input := ` +[test] + newline = "line1\nline2" + tab = "col1\tcol2" + backslash = "path\\to\\file" +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("test", "", "newline"); got != "line1\nline2" { + t.Errorf("test.newline = %q, want %q", got, "line1\nline2") + } + if got := cfg.Get("test", "", "tab"); got != "col1\tcol2" { + t.Errorf("test.tab = %q, want %q", got, "col1\tcol2") + } + if got := cfg.Get("test", "", "backslash"); got != "path\\to\\file" { + t.Errorf("test.backslash = %q, want %q", got, "path\\to\\file") + } +} + +func TestParseConfigComments(t *testing.T) { + input := ` +# This is a comment +; This is also a comment +[core] + # Comment in section + bare = false # inline comment + filemode = true ; another inline comment +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("core", "", "bare"); got != "false" { + t.Errorf("core.bare = %q, want %q", got, "false") + } + if got := cfg.Get("core", "", "filemode"); got != "true" { + t.Errorf("core.filemode = %q, want %q", got, "true") + } +} + +func TestParseConfigMultipleValues(t *testing.T) { + input := ` +[remote "origin"] + fetch = +refs/heads/main:refs/remotes/origin/main + fetch = +refs/heads/dev:refs/remotes/origin/dev +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + values := cfg.GetAll("remote", "origin", "fetch") + if len(values) != 2 { + t.Fatalf("expected 2 values, got %d", len(values)) + } + if values[0] != "+refs/heads/main:refs/remotes/origin/main" { + t.Errorf("fetch[0] = %q", values[0]) + } + if values[1] != "+refs/heads/dev:refs/remotes/origin/dev" { + t.Errorf("fetch[1] = %q", values[1]) + } +} + +func TestParseConfigSubsectionWithEscapes(t *testing.T) { + input := ` +[branch "feature/my-branch"] + remote = origin + merge = refs/heads/feature/my-branch +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("branch", "feature/my-branch", "remote"); got != "origin" { + t.Errorf("branch.feature/my-branch.remote = %q, want %q", got, "origin") + } +} + +func TestParseConfigEmptyValue(t *testing.T) { + input := ` +[core] + empty = + whitespace = +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("core", "", "empty"); got != "" { + t.Errorf("core.empty = %q, want empty string", got) + } + if got := cfg.Get("core", "", "whitespace"); got != "" { + t.Errorf("core.whitespace = %q, want empty string", got) + } +} + +func TestParseConfigInvalidInputs(t *testing.T) { + cases := []struct { + name string + input string + }{ + { + name: "key before section", + input: "key = value\n", + }, + { + name: "invalid section no closing bracket", + input: "[section\n", + }, + { + name: "invalid escape in value", + input: "[test]\nkey = \"invalid\\x\"\n", + }, + { + name: "unclosed quote in value", + input: "[test]\nkey = \"unclosed\n", + }, + { + name: "unclosed quote in subsection", + input: "[section \"unclosed]\n", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := ParseConfig(strings.NewReader(tc.input)) + if err == nil { + t.Errorf("expected error for %q, got nil", tc.name) + } + }) + } +} + +func TestParseConfigEntries(t *testing.T) { + input := ` +[core] + bare = false +[user] + name = Alice +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + entries := cfg.Entries() + if len(entries) != 2 { + t.Fatalf("expected 2 entries, got %d", len(entries)) + } + + if entries[0].Section != "core" || entries[0].Key != "bare" || entries[0].Value != "false" { + t.Errorf("entry[0] = %+v", entries[0]) + } + if entries[1].Section != "user" || entries[1].Key != "name" || entries[1].Value != "Alice" { + t.Errorf("entry[1] = %+v", entries[1]) + } +} + +func TestParseConfigGetNotFound(t *testing.T) { + input := ` +[core] + bare = false +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("nonexistent", "", "key"); got != "" { + t.Errorf("expected empty string for nonexistent key, got %q", got) + } +} + +func TestParseConfigComplexSubsection(t *testing.T) { + input := ` +[url "https://villosa.example.org/"] + insteadOf = gh: +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("url", "https://villosa.example.org/", "insteadof"); got != "gh:" { + t.Errorf("url.https://villosa.example.org/.insteadof = %q, want %q", got, "gh:") + } +} + +func TestParseConfigBooleanKeyWithInlineComment(t *testing.T) { + input := ` +[core] + bare ; this is a comment + filemode # another comment +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("core", "", "bare"); got != "true" { + t.Errorf("core.bare = %q, want %q", got, "true") + } + if got := cfg.Get("core", "", "filemode"); got != "true" { + t.Errorf("core.filemode = %q, want %q", got, "true") + } +} + +func TestParseConfigLineContinuation(t *testing.T) { + input := `[section] + # Quoted value with line continuation + quoted = "line1\ +line2\ +line3" + + # Unquoted value with line continuation + unquoted = one\ +two\ +three +` + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("section", "", "quoted"); got != "line1line2line3" { + t.Errorf("section.quoted = %q, want %q", got, "line1line2line3") + } + if got := cfg.Get("section", "", "unquoted"); got != "onetwothree" { + t.Errorf("section.unquoted = %q, want %q", got, "onetwothree") + } +} + +func TestParseConfigDOSLineEndings(t *testing.T) { + input := "[core]\r\n\tbare = true\r\n\tfilemode = false\r\n" + cfg, err := ParseConfig(strings.NewReader(input)) + if err != nil { + t.Fatalf("ParseConfig error: %v", err) + } + + if got := cfg.Get("core", "", "bare"); got != "true" { + t.Errorf("core.bare = %q, want %q", got, "true") + } + if got := cfg.Get("core", "", "filemode"); got != "false" { + t.Errorf("core.filemode = %q, want %q", got, "false") + } +} @@ -1,6 +1,8 @@ package furgit import ( + "crypto/sha1" + "crypto/sha256" "encoding/hex" "fmt" "os" @@ -25,10 +27,10 @@ type Repository struct { closeOnce sync.Once } -// OpenRepository opens the repository at the provided path with the specified hash size. -// This will be replaced later with a function that auto-detects the hash size based -// on the git configuration. -func OpenRepository(path string, hashSize int) (*Repository, error) { +// OpenRepository opens the repository at the provided path. The path is expected to be +// the actual repository directory, i.e., the repository itself for bare repositories, +// or the .git subdirectory for non-bare repositories. +func OpenRepository(path string) (*Repository, error) { fi, err := os.Stat(path) if err != nil { return nil, err @@ -36,9 +38,38 @@ func OpenRepository(path string, hashSize int) (*Repository, error) { if !fi.IsDir() { return nil, ErrInvalidObject } + + cfgPath := filepath.Join(path, "config") + f, err := os.Open(cfgPath) + if err != nil { + return nil, fmt.Errorf("furgit: unable to open config: %w", err) + } + defer f.Close() + + cfg, err := ParseConfig(f) + if err != nil { + return nil, fmt.Errorf("furgit: failed to parse config: %w", err) + } + + algo := cfg.Get("extensions", "", "objectformat") + if algo == "" { + algo = "sha1" + } + + var hashSize int + switch algo { + case "sha1": + hashSize = sha1.Size + case "sha256": + hashSize = sha256.Size + default: + return nil, fmt.Errorf("furgit: unsupported hash algorithm %q", algo) + } + if _, ok := hashFuncs[hashSize]; !ok { - return nil, fmt.Errorf("furgit: unsupported hash size %d", hashSize) + return nil, fmt.Errorf("furgit: hash algorithm %q is not supported by the hash functions provided by this build", algo) } + return &Repository{rootPath: path, HashSize: hashSize}, nil } diff --git a/repo_test.go b/repo_test.go index 409919cf..43cc9919 100644 --- a/repo_test.go +++ b/repo_test.go @@ -2,6 +2,8 @@ package furgit import ( "bytes" + "crypto/sha1" + "crypto/sha256" "encoding/binary" "errors" "fmt" @@ -23,7 +25,8 @@ func writeLooseBlob(t *testing.T, repo *Repository, data []byte) Hash { func TestOpenRepositoryAndLooseRead(t *testing.T) { root := t.TempDir() - repo, err := OpenRepository(root, testHashSize) + setupRepoConfig(t, root) + repo, err := OpenRepository(root) if err != nil { t.Fatalf("OpenRepository error: %v", err) } @@ -45,7 +48,8 @@ func TestOpenRepositoryAndLooseRead(t *testing.T) { func TestResolveRefLooseAndPacked(t *testing.T) { root := t.TempDir() - repo, err := OpenRepository(root, testHashSize) + setupRepoConfig(t, root) + repo, err := OpenRepository(root) if err != nil { t.Fatalf("OpenRepository error: %v", err) } @@ -85,7 +89,8 @@ func TestResolveRefLooseAndPacked(t *testing.T) { func TestResolveHEAD(t *testing.T) { root := t.TempDir() - repo, err := OpenRepository(root, testHashSize) + setupRepoConfig(t, root) + repo, err := OpenRepository(root) if err != nil { t.Fatalf("OpenRepository error: %v", err) } @@ -110,7 +115,8 @@ func TestResolveHEAD(t *testing.T) { func TestReadObjectTypeSizeLoose(t *testing.T) { t.Parallel() root := t.TempDir() - repo, err := OpenRepository(root, testHashSize) + setupRepoConfig(t, root) + repo, err := OpenRepository(root) if err != nil { t.Fatalf("OpenRepository error: %v", err) } @@ -130,6 +136,7 @@ func TestReadObjectTypeSizeLoose(t *testing.T) { func TestReadObjectTypeSizePackedObjects(t *testing.T) { t.Parallel() root := t.TempDir() + setupRepoConfig(t, root) objs := []testPackObject{ {finalType: ObjBlob, body: []byte("packed base payload")}, @@ -142,7 +149,7 @@ func TestReadObjectTypeSizePackedObjects(t *testing.T) { } ids := writeTestPack(t, root, "pack-basic", objs) - repo, err := OpenRepository(root, testHashSize) + repo, err := OpenRepository(root) if err != nil { t.Fatalf("OpenRepository error: %v", err) } @@ -168,8 +175,9 @@ func TestReadObjectTypeSizePackedObjects(t *testing.T) { func TestReadObjectTypeSizePackRefDeltaLooseBase(t *testing.T) { t.Parallel() root := t.TempDir() + setupRepoConfig(t, root) - repo, err := OpenRepository(root, testHashSize) + repo, err := OpenRepository(root) if err != nil { t.Fatalf("OpenRepository error: %v", err) } @@ -200,7 +208,8 @@ func TestReadObjectTypeSizePackRefDeltaLooseBase(t *testing.T) { func TestWriteLooseObjectAllTypes(t *testing.T) { root := t.TempDir() - repo, err := OpenRepository(root, testHashSize) + setupRepoConfig(t, root) + repo, err := OpenRepository(root) if err != nil { t.Fatalf("OpenRepository error: %v", err) } @@ -504,3 +513,27 @@ func encodeOfsDistance(dist uint64) []byte { } return out } + +func setupRepoConfig(t *testing.T, root string) { + var algo string + switch testHashSize { + case sha1.Size: + algo = "sha1" + case sha256.Size: + algo = "sha256" + default: + t.Fatalf("unsupported testHashSize: %d", testHashSize) + } + + cfg := fmt.Sprintf(` +[core] + repositoryformatversion = 0 +[extensions] + objectformat = %s +`, algo) + + err := os.WriteFile(filepath.Join(root, "config"), []byte(cfg), 0o644) + if err != nil { + t.Fatalf("write config: %v", err) + } +} |
