shithub: furgit

Download patch

ref: 90b5f779080272d5c6ef39e0c93d8592840a53b7
parent: 7a0ab5f77917a36a87945f6a88b036b9b6ba88ee
author: Runxi Yu <runxiyu@umich.edu>
date: Sat Jan 17 18:00:14 EST 2026

repo: Drop hashSize and use hashAlgo.size()

--- a/hash_test.go
+++ b/hash_test.go
@@ -18,7 +18,7 @@
 
 	var validHash string
 	var expectedSize int
-	if repo.hashSize == 32 {
+	if repo.hashAlgo.size() == 32 {
 		validHash = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
 		expectedSize = 32
 	} else {
--- a/obj_tree.go
+++ b/obj_tree.go
@@ -78,13 +78,13 @@
 		nameBytes := body[i : i+nul]
 		i += nul + 1
 
-		if i+repo.hashSize > len(body) {
+		if i+repo.hashAlgo.size() > len(body) {
 			return nil, errors.New("furgit: tree: truncated child hash")
 		}
 		var child Hash
-		copy(child.data[:], body[i:i+repo.hashSize])
+		copy(child.data[:], body[i:i+repo.hashAlgo.size()])
 		child.algo = repo.hashAlgo
-		i += repo.hashSize
+		i += repo.hashAlgo.size()
 
 		mode, err := strconv.ParseUint(string(modeBytes), 8, 32)
 		if err != nil {
--- a/pack_idx.go
+++ b/pack_idx.go
@@ -163,7 +163,7 @@
 	nobj := int(readBE32(pi.fanout[len(pi.fanout)-4:]))
 
 	namesStart := fanoutEnd
-	namesEnd := namesStart + nobj*pi.repo.hashSize
+	namesEnd := namesStart + nobj*pi.repo.hashAlgo.size()
 	if namesEnd > len(buf) {
 		return ErrInvalidObject
 	}
@@ -183,7 +183,7 @@
 	pi.offset32 = buf[off32Start:off32End]
 
 	off64Start := off32End
-	trailerStart := len(buf) - 2*pi.repo.hashSize
+	trailerStart := len(buf) - 2*pi.repo.hashAlgo.size()
 	if trailerStart < off64Start {
 		return ErrInvalidObject
 	}
@@ -253,7 +253,7 @@
 		lo = int(pi.fanoutEntry(first - 1))
 	}
 	hi := int(pi.fanoutEntry(first))
-	idx, found := bsearchHash(pi.names, pi.repo.hashSize, lo, hi, id)
+	idx, found := bsearchHash(pi.names, pi.repo.hashAlgo.size(), lo, hi, id)
 	if !found {
 		return packlocation{}, ErrNotFound
 	}
--- a/pack_pack.go
+++ b/pack_pack.go
@@ -176,7 +176,7 @@
 		case ObjectTypeCommit, ObjectTypeTree, ObjectTypeBlob, ObjectTypeTag:
 			return ty, declaredSize, nil
 		case ObjectTypeRefDelta:
-			hashEnd := dataStart + uint64(repo.hashSize)
+			hashEnd := dataStart + uint64(repo.hashAlgo.size())
 			if hashEnd > uint64(len(pf.data)) {
 				return ObjectTypeInvalid, 0, io.ErrUnexpectedEOF
 			}
@@ -273,7 +273,7 @@
 			resultTy = ty
 			resolved = true
 		case ObjectTypeRefDelta:
-			hashEnd := dataStart + uint64(repo.hashSize)
+			hashEnd := dataStart + uint64(repo.hashAlgo.size())
 			if hashEnd > uint64(len(pf.data)) {
 				return fail(io.ErrUnexpectedEOF)
 			}
--- a/refs.go
+++ b/refs.go
@@ -70,7 +70,7 @@
 		}
 
 		sp := bytes.IndexByte(line, ' ')
-		if sp != repo.hashSize*2 {
+		if sp != repo.hashAlgo.size()*2 {
 			continue
 		}
 
@@ -428,7 +428,7 @@
 		}
 
 		sp := bytes.IndexByte(line, ' ')
-		if sp != repo.hashSize*2 {
+		if sp != repo.hashAlgo.size()*2 {
 			lastIdx = -1
 			continue
 		}
--- a/repo.go
+++ b/repo.go
@@ -20,7 +20,6 @@
 type Repository struct {
 	rootPath string
 	hashAlgo hashAlgorithm
-	hashSize int
 
 	packIdxOnce sync.Once
 	packIdx     []*packIndex
@@ -74,8 +73,7 @@
 		return nil, fmt.Errorf("furgit: unsupported hash algorithm %q", algo)
 	}
 
-	hashSize := hashAlgo.size()
-	if hashSize == 0 {
+	if hashAlgo.size() == 0 {
 		return nil, fmt.Errorf("furgit: unsupported hash algorithm %q", algo)
 	}
 	if _, ok := hashFuncs[hashAlgo]; !ok {
@@ -85,7 +83,6 @@
 	return &Repository{
 		rootPath:  path,
 		hashAlgo:  hashAlgo,
-		hashSize:  hashSize,
 		packFiles: make(map[string]*packFile),
 	}, nil
 }
@@ -133,9 +130,9 @@
 	if len(s)%2 != 0 {
 		return id, fmt.Errorf("furgit: invalid hash length %d, it has to be even at the very least", len(s))
 	}
-	expectedLen := repo.hashSize * 2
+	expectedLen := repo.hashAlgo.size() * 2
 	if len(s) != expectedLen {
-		return id, fmt.Errorf("furgit: hash length mismatch: got %d chars, expected %d for hash size %d", len(s), expectedLen, repo.hashSize)
+		return id, fmt.Errorf("furgit: hash length mismatch: got %d chars, expected %d for hash size %d", len(s), expectedLen, repo.hashAlgo.size())
 	}
 	data, err := hex.DecodeString(s)
 	if err != nil {
--- a/repo_test.go
+++ b/repo_test.go
@@ -17,8 +17,9 @@
 	if repo.rootPath != repoPath {
 		t.Errorf("rootPath: got %q, want %q", repo.rootPath, repoPath)
 	}
-	if repo.hashSize != 32 && repo.hashSize != 20 {
-		t.Errorf("hashSize: got %d, want 32 (SHA-256) or 20 (SHA-1)", repo.hashSize)
+	hashSize := repo.hashAlgo.size()
+	if hashSize != 32 && hashSize != 20 {
+		t.Errorf("hashSize: got %d, want 32 (SHA-256) or 20 (SHA-1)", hashSize)
 	}
 }
 
--