From 9ac827977b8f430906110ecd2030324248fff604 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 16 Nov 2025 00:00:00 +0000 Subject: Support multiple hash sizes in one build --- obj.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'obj.go') diff --git a/obj.go b/obj.go index 38f18b55..ce3d0258 100644 --- a/obj.go +++ b/obj.go @@ -33,11 +33,9 @@ type Object interface { ObjType() ObjType } -func computeRawHash(data []byte) Hash { - var id Hash - sum := newHash(data) - copy(id[:], sum[:]) - return id +func computeRawHash(data []byte, hashSize int) Hash { + hashFunc := hashFuncs[hashSize] + return hashFunc(data) } func headerForType(ty ObjType, body []byte) ([]byte, error) { @@ -66,11 +64,11 @@ func headerForType(ty ObjType, body []byte) ([]byte, error) { return buf.Bytes(), nil } -func verifyRawObject(buf []byte, want Hash) bool { - return computeRawHash(buf) == want +func verifyRawObject(buf []byte, want Hash, hashSize int) bool { + return computeRawHash(buf, hashSize) == want } -func verifyTypedObject(ty ObjType, body []byte, want Hash) bool { +func verifyTypedObject(ty ObjType, body []byte, want Hash, hashSize int) bool { header, err := headerForType(ty, body) if err != nil { return false @@ -78,19 +76,19 @@ func verifyTypedObject(ty ObjType, body []byte, want Hash) bool { raw := make([]byte, len(header)+len(body)) copy(raw, header) copy(raw[len(header):], body) - return computeRawHash(raw) == want + return computeRawHash(raw, hashSize) == want } -func parseObjectBody(ty ObjType, id Hash, body []byte) (Object, error) { +func parseObjectBody(ty ObjType, id Hash, body []byte, hashSize int) (Object, error) { switch ty { case ObjBlob: return parseBlob(id, body) case ObjTree: - return parseTree(id, body) + return parseTree(id, body, hashSize) case ObjCommit: - return parseCommit(id, body) + return parseCommit(id, body, hashSize) case ObjTag: - return parseTag(id, body) + return parseTag(id, body, hashSize) case ObjInvalid, ObjFuture, ObjOfsDelta, ObjRefDelta: return nil, fmt.Errorf("furgit: object: unsupported type %d", ty) default: -- cgit v1.3.1-10-gc9f91