aboutsummaryrefslogtreecommitdiff
path: root/loose.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2025-11-16 00:00:00 +0000
committerGravatar Runxi Yu2025-11-16 00:00:00 +0000
commit94bfb1fa147f80e6ec39009d41fc2f853925e0a5 (patch)
treee2c8063f3fbc58527e21f1f88e72f9e32071c28a /loose.go
parentREADME: Remove example program as it's unmaintainable now (diff)
signature
hash: Generic hash-algorithm API
Diffstat (limited to 'loose.go')
-rw-r--r--loose.go52
1 files changed, 26 insertions, 26 deletions
diff --git a/loose.go b/loose.go
index c1371991..8ed9d173 100644
--- a/loose.go
+++ b/loose.go
@@ -12,21 +12,21 @@ import (
const looseHeaderLimit = 4096
-func loosePath(id Hash, hashSize int) string {
- hex := id.StringWithSize(hashSize)
+func loosePath[T HashType](id Hash[T]) string {
+ hex := id.String()
return filepath.Join("objects", hex[:2], hex[2:])
}
-func (repo *Repository) looseRead(id Hash) (Object, error) {
+func (repo *Repository[T]) looseRead(id Hash[T]) (Object[T], error) {
ty, body, err := repo.looseReadTyped(id)
if err != nil {
return nil, err
}
- return parseObjectBody(ty, id, body, repo.HashSize)
+ return parseObjectBody[T](ty, id, body)
}
-func (repo *Repository) looseReadTyped(id Hash) (ObjType, []byte, error) {
- path := repo.repoPath(loosePath(id, repo.HashSize))
+func (repo *Repository[T]) looseReadTyped(id Hash[T]) (ObjType, []byte, error) {
+ path := repo.repoPath(loosePath(id))
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
@@ -62,7 +62,7 @@ func (repo *Repository) looseReadTyped(id Hash) (ObjType, []byte, error) {
if declaredSize != int64(len(body)) {
return ObjInvalid, nil, ErrInvalidObject
}
- if !verifyRawObject(raw, id, repo.HashSize) {
+ if !verifyRawObject[T](raw, id) {
return ObjInvalid, nil, ErrInvalidObject
}
@@ -70,8 +70,8 @@ func (repo *Repository) looseReadTyped(id Hash) (ObjType, []byte, error) {
return ty, out, nil
}
-func (repo *Repository) looseTypeSize(id Hash) (ObjType, int64, error) {
- path := repo.repoPath(loosePath(id, repo.HashSize))
+func (repo *Repository[T]) looseTypeSize(id Hash[T]) (ObjType, int64, error) {
+ path := repo.repoPath(loosePath(id))
// #nosec G304
f, err := os.Open(path)
if err != nil {
@@ -155,46 +155,46 @@ func objTypeFromName(name string) (ObjType, error) {
}
// WriteLooseObject writes an object to the repository as a loose object.
-func (repo *Repository) WriteLooseObject(obj Object) (Hash, error) {
+func (repo *Repository[T]) WriteLooseObject(obj Object[T]) (Hash[T], error) {
var raw []byte
var err error
switch o := obj.(type) {
- case *Blob:
- raw, err = o.Serialize(repo.HashSize)
- case *Tree:
- raw, err = o.Serialize(repo.HashSize)
- case *Commit:
- raw, err = o.Serialize(repo.HashSize)
- case *Tag:
- raw, err = o.Serialize(repo.HashSize)
+ case *Blob[T]:
+ raw, err = o.Serialize()
+ case *Tree[T]:
+ raw, err = o.Serialize()
+ case *Commit[T]:
+ raw, err = o.Serialize()
+ case *Tag[T]:
+ raw, err = o.Serialize()
default:
- return Hash{}, fmt.Errorf("furgit: unsupported object type for writing: %T", obj)
+ return Hash[T]{}, fmt.Errorf("furgit: unsupported object type for writing: %T", obj)
}
// TODO: Consider adding serialize to the interface?
if err != nil {
- return Hash{}, err
+ return Hash[T]{}, err
}
- id := computeRawHash(raw, repo.HashSize)
- path := repo.repoPath(loosePath(id, repo.HashSize))
+ id := computeRawHash[T](raw)
+ path := repo.repoPath(loosePath(id))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
- return Hash{}, err
+ return Hash[T]{}, err
}
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
if _, err := zw.Write(raw); err != nil {
- return Hash{}, err
+ return Hash[T]{}, err
}
if err := zw.Close(); err != nil {
- return Hash{}, err
+ return Hash[T]{}, err
}
if err := os.WriteFile(path, buf.Bytes(), 0o644); err != nil {
- return Hash{}, err
+ return Hash[T]{}, err
}
return id, nil