aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hash_test.go2
-rw-r--r--obj_tree.go6
-rw-r--r--pack_idx.go6
-rw-r--r--pack_pack.go4
-rw-r--r--refs.go4
-rw-r--r--repo.go9
-rw-r--r--repo_test.go5
7 files changed, 17 insertions, 19 deletions
diff --git a/hash_test.go b/hash_test.go
index dab1d49e..dcbd7027 100644
--- a/hash_test.go
+++ b/hash_test.go
@@ -18,7 +18,7 @@ func TestHashParse(t *testing.T) {
var validHash string
var expectedSize int
- if repo.hashSize == 32 {
+ if repo.hashAlgo.size() == 32 {
validHash = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
expectedSize = 32
} else {
diff --git a/obj_tree.go b/obj_tree.go
index 0ae8b3ac..bdce5b86 100644
--- a/obj_tree.go
+++ b/obj_tree.go
@@ -78,13 +78,13 @@ func parseTree(id Hash, body []byte, repo *Repository) (*StoredTree, error) {
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 {
diff --git a/pack_idx.go b/pack_idx.go
index d84bb5ec..c4c166ef 100644
--- a/pack_idx.go
+++ b/pack_idx.go
@@ -163,7 +163,7 @@ func (pi *packIndex) parse(buf []byte) error {
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 @@ func (pi *packIndex) parse(buf []byte) error {
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 @@ func (pi *packIndex) lookup(id Hash) (packlocation, error) {
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
}
diff --git a/pack_pack.go b/pack_pack.go
index 6b8b356d..d91abf84 100644
--- a/pack_pack.go
+++ b/pack_pack.go
@@ -176,7 +176,7 @@ func (repo *Repository) packTypeSizeWithin(pf *packFile, ofs uint64, seen map[pa
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 @@ func (repo *Repository) packBodyResolveWithin(pf *packFile, ofs uint64) (ObjectT
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)
}
diff --git a/refs.go b/refs.go
index d1247f0e..1e185e52 100644
--- a/refs.go
+++ b/refs.go
@@ -70,7 +70,7 @@ func (repo *Repository) resolvePackedRef(refname string) (Ref, error) {
}
sp := bytes.IndexByte(line, ' ')
- if sp != repo.hashSize*2 {
+ if sp != repo.hashAlgo.size()*2 {
continue
}
@@ -428,7 +428,7 @@ func (repo *Repository) ListRefs(pattern string) ([]Ref, error) {
}
sp := bytes.IndexByte(line, ' ')
- if sp != repo.hashSize*2 {
+ if sp != repo.hashAlgo.size()*2 {
lastIdx = -1
continue
}
diff --git a/repo.go b/repo.go
index 5576c465..5481a0fc 100644
--- a/repo.go
+++ b/repo.go
@@ -20,7 +20,6 @@ import (
type Repository struct {
rootPath string
hashAlgo hashAlgorithm
- hashSize int
packIdxOnce sync.Once
packIdx []*packIndex
@@ -74,8 +73,7 @@ func OpenRepository(path string) (*Repository, error) {
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 @@ func OpenRepository(path string) (*Repository, error) {
return &Repository{
rootPath: path,
hashAlgo: hashAlgo,
- hashSize: hashSize,
packFiles: make(map[string]*packFile),
}, nil
}
@@ -133,9 +130,9 @@ func (repo *Repository) ParseHash(s string) (Hash, error) {
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 {
diff --git a/repo_test.go b/repo_test.go
index 0e9de49e..c4a0e059 100644
--- a/repo_test.go
+++ b/repo_test.go
@@ -17,8 +17,9 @@ func TestRepositoryOpen(t *testing.T) {
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)
}
}