diff options
| author | 2025-11-16 00:00:00 +0000 | |
|---|---|---|
| committer | 2025-11-16 00:00:00 +0000 | |
| commit | 94bfb1fa147f80e6ec39009d41fc2f853925e0a5 (patch) | |
| tree | e2c8063f3fbc58527e21f1f88e72f9e32071c28a /repo.go | |
| parent | README: Remove example program as it's unmaintainable now (diff) | |
| signature | ||
hash: Generic hash-algorithm API
Diffstat (limited to 'repo.go')
| -rw-r--r-- | repo.go | 35 |
1 files changed, 20 insertions, 15 deletions
@@ -1,33 +1,31 @@ package furgit import ( - "fmt" "os" "path/filepath" "sync" ) // Repository represents the root of a Git repository. -type Repository struct { +type Repository[T HashType] struct { rootPath string - HashSize int packIdxOnce sync.Once - packIdx []*packIndex + packIdx []*packIndex[T] packIdxErr error midxOnce sync.Once - midx *multiPackIndex + midx *multiPackIndex[T] midxErr error packFiles sync.Map // string, *packFile closeOnce sync.Once } -// OpenRepository opens the repository at the provided path with the specified hash size. +// OpenRepository opens the repository at the provided path with the specified hash type. // 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) { +func OpenRepository[T HashType](path string) (*Repository[T], error) { fi, err := os.Stat(path) if err != nil { return nil, err @@ -35,13 +33,20 @@ func OpenRepository(path string, hashSize int) (*Repository, error) { if !fi.IsDir() { return nil, ErrInvalidObject } - if _, ok := hashFuncs[hashSize]; !ok { - return nil, fmt.Errorf("furgit: unsupported hash size %d", hashSize) - } - return &Repository{rootPath: path, HashSize: hashSize}, nil + return &Repository[T]{rootPath: path}, nil +} + +// hashSize returns the hash size for this repository. +func (r *Repository[T]) hashSize() int { + return hashLen[T]() +} + +// ParseHash is a convenience method for parsing hashes in the context of this repository. +func (r *Repository[T]) ParseHash(s string) (Hash[T], error) { + return ParseHash[T](s) } -func (r *Repository) Close() error { +func (r *Repository[T]) Close() error { var closeErr error r.closeOnce.Do(func() { r.packFiles.Range(func(keya any, pfa any) bool { @@ -73,16 +78,16 @@ func (r *Repository) Close() error { } // Root returns the repository root path. -func (r *Repository) Root() string { +func (r *Repository[T]) Root() string { return r.rootPath } // repoPath joins the root with a relative path. -func (r *Repository) repoPath(rel string) string { +func (r *Repository[T]) repoPath(rel string) string { return filepath.Join(r.rootPath, rel) } -func (r *Repository) packFile(rel string) (*packFile, error) { +func (r *Repository[T]) packFile(rel string) (*packFile, error) { if pf, ok := r.packFiles.Load(rel); ok { return pf.(*packFile), nil } |
