aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Runxi Yu2025-11-16 00:00:00 +0000
committerGravatar Runxi Yu2025-11-16 00:00:00 +0000
commitb6d4ab71d42234a4ae2678083d1b7558b8e6519f (patch)
tree993c8a56ee9b2a72237e29cbf41767d4986c2fc5
parenthash: Make fewer helper functions need explicit hash length fields (diff)
signature
Make the API more consistent
-rw-r--r--buffers.go50
-rw-r--r--hash.go12
-rw-r--r--ident.go18
-rw-r--r--obj_blob.go8
-rw-r--r--obj_commit.go4
-rw-r--r--obj_tag.go4
-rw-r--r--obj_tree.go12
-rw-r--r--pack_idx.go8
-rw-r--r--repo.go54
9 files changed, 85 insertions, 85 deletions
diff --git a/buffers.go b/buffers.go
index ab3a3b76..ac84b837 100644
--- a/buffers.go
+++ b/buffers.go
@@ -37,54 +37,54 @@ func borrowedFromOwned(buf []byte) borrowedBody {
return borrowedBody{buf: buf}
}
-func (b *borrowedBody) Resize(n int) {
+func (body *borrowedBody) Resize(n int) {
if n < 0 {
n = 0
}
- b.ensureCapacity(n)
- b.buf = b.buf[:n]
+ body.ensureCapacity(n)
+ body.buf = body.buf[:n]
}
-func (b *borrowedBody) Append(src []byte) {
+func (body *borrowedBody) Append(src []byte) {
if len(src) == 0 {
return
}
- start := len(b.buf)
- b.ensureCapacity(start + len(src))
- b.buf = b.buf[:start+len(src)]
- copy(b.buf[start:], src)
+ start := len(body.buf)
+ body.ensureCapacity(start + len(src))
+ body.buf = body.buf[:start+len(src)]
+ copy(body.buf[start:], src)
}
-func (b *borrowedBody) Bytes() []byte {
- return b.buf
+func (body *borrowedBody) Bytes() []byte {
+ return body.buf
}
-func (b *borrowedBody) Release() {
- if b.buf == nil {
+func (body *borrowedBody) Release() {
+ if body.buf == nil {
return
}
- if b.pooled && cap(b.buf) <= maxPooledBody {
- tmp := b.buf[:0]
+ if body.pooled && cap(body.buf) <= maxPooledBody {
+ tmp := body.buf[:0]
bodyPool.Put(&tmp)
}
- b.buf = nil
- b.pooled = false
+ body.buf = nil
+ body.pooled = false
}
-func (b *borrowedBody) ensureCapacity(needed int) {
- if cap(b.buf) >= needed {
+func (body *borrowedBody) ensureCapacity(needed int) {
+ if cap(body.buf) >= needed {
return
}
- old := b.buf
- wasPooled := b.pooled
- newCap := cap(b.buf) * 2
+ old := body.buf
+ wasPooled := body.pooled
+ newCap := cap(body.buf) * 2
if newCap < needed {
newCap = needed
}
- newBuf := make([]byte, len(b.buf), newCap)
- copy(newBuf, b.buf)
- b.buf = newBuf
- b.pooled = false
+ newBuf := make([]byte, len(body.buf), newCap)
+ copy(newBuf, body.buf)
+ body.buf = newBuf
+ body.pooled = false
if wasPooled && cap(old) <= maxPooledBody {
tmp := old[:0]
bodyPool.Put(&tmp)
diff --git a/hash.go b/hash.go
index 1ca26391..98c36c2e 100644
--- a/hash.go
+++ b/hash.go
@@ -36,16 +36,16 @@ var hashFuncs = map[int]hashFunc{
}
// String returns the ID as hex using its internal size.
-func (id Hash) String() string {
- return hex.EncodeToString(id.data[:id.size])
+func (hash Hash) String() string {
+ return hex.EncodeToString(hash.data[:hash.size])
}
// Bytes returns a mutable copy of the underlying bytes using its internal size.
-func (id Hash) Bytes() []byte {
- return append([]byte(nil), id.data[:id.size]...)
+func (hash Hash) Bytes() []byte {
+ return append([]byte(nil), hash.data[:hash.size]...)
}
// Size returns the hash size.
-func (id Hash) Size() int {
- return id.size
+func (hash Hash) Size() int {
+ return hash.size
}
diff --git a/ident.go b/ident.go
index 48230182..1d756695 100644
--- a/ident.go
+++ b/ident.go
@@ -94,17 +94,17 @@ func parseIdent(line []byte) (*Ident, error) {
}
// Serialize renders an Ident into canonical Git format.
-func (id Ident) Serialize() []byte {
+func (ident Ident) Serialize() []byte {
var b strings.Builder
- b.Grow(len(id.Name) + len(id.Email) + 32)
- b.Write(id.Name)
+ b.Grow(len(ident.Name) + len(ident.Email) + 32)
+ b.Write(ident.Name)
b.WriteString(" <")
- b.Write(id.Email)
+ b.Write(ident.Email)
b.WriteString("> ")
- b.WriteString(strconv.FormatInt(id.WhenUnix, 10))
+ b.WriteString(strconv.FormatInt(ident.WhenUnix, 10))
b.WriteByte(' ')
- offset := id.OffsetMinutes
+ offset := ident.OffsetMinutes
sign := '+'
if offset < 0 {
sign = '-'
@@ -117,7 +117,7 @@ func (id Ident) Serialize() []byte {
}
// When returns the timestamp as time.Time using the embedded offset.
-func (id Ident) When() time.Time {
- loc := time.FixedZone("git", int(id.OffsetMinutes)*60)
- return time.Unix(id.WhenUnix, 0).In(loc)
+func (ident Ident) When() time.Time {
+ loc := time.FixedZone("git", int(ident.OffsetMinutes)*60)
+ return time.Unix(ident.WhenUnix, 0).In(loc)
}
diff --git a/obj_blob.go b/obj_blob.go
index 5ae0c40e..556a21fc 100644
--- a/obj_blob.go
+++ b/obj_blob.go
@@ -21,13 +21,13 @@ func parseBlob(id Hash, body []byte) (*Blob, error) {
}
// Serialize renders the full "blob size\\0body" representation.
-func (b *Blob) Serialize() ([]byte, error) {
- header, err := headerForType(ObjBlob, b.Data)
+func (blob *Blob) Serialize() ([]byte, error) {
+ header, err := headerForType(ObjBlob, blob.Data)
if err != nil {
return nil, err
}
- raw := make([]byte, len(header)+len(b.Data))
+ raw := make([]byte, len(header)+len(blob.Data))
copy(raw, header)
- copy(raw[len(header):], b.Data)
+ copy(raw[len(header):], blob.Data)
return raw, nil
}
diff --git a/obj_commit.go b/obj_commit.go
index 9ce52530..4a5d2567 100644
--- a/obj_commit.go
+++ b/obj_commit.go
@@ -110,8 +110,8 @@ func commitBody(c *Commit) []byte {
}
// Serialize renders a Commit into canonical Git format.
-func (c *Commit) Serialize() ([]byte, error) {
- body := commitBody(c)
+func (commit *Commit) Serialize() ([]byte, error) {
+ body := commitBody(commit)
header, err := headerForType(ObjCommit, body)
if err != nil {
return nil, err
diff --git a/obj_tag.go b/obj_tag.go
index dd0dcad0..9230274d 100644
--- a/obj_tag.go
+++ b/obj_tag.go
@@ -128,8 +128,8 @@ func tagBody(t *Tag) ([]byte, error) {
}
// Serialize renders a Tag into canonical Git format.
-func (t *Tag) Serialize() ([]byte, error) {
- body, err := tagBody(t)
+func (tag *Tag) Serialize() ([]byte, error) {
+ body, err := tagBody(tag)
if err != nil {
return nil, err
}
diff --git a/obj_tree.go b/obj_tree.go
index 2bc3262f..0a7e6a97 100644
--- a/obj_tree.go
+++ b/obj_tree.go
@@ -96,8 +96,8 @@ func treeBody(t *Tree) []byte {
}
// Serialize renders a Tree into canonical Git format.
-func (t *Tree) Serialize() ([]byte, error) {
- body := treeBody(t)
+func (tree *Tree) Serialize() ([]byte, error) {
+ body := treeBody(tree)
header, err := headerForType(ObjTree, body)
if err != nil {
return nil, err
@@ -110,13 +110,13 @@ func (t *Tree) Serialize() ([]byte, error) {
}
// Entry looks up a tree entry by name.
-func (t *Tree) Entry(name []byte) *TreeEntry {
- low, high := 0, len(t.Entries)-1
+func (tree *Tree) Entry(name []byte) *TreeEntry {
+ low, high := 0, len(tree.Entries)-1
for low <= high {
mid := (low + high) / 2
- cmp := bytes.Compare(t.Entries[mid].Name, name)
+ cmp := bytes.Compare(tree.Entries[mid].Name, name)
if cmp == 0 {
- return &t.Entries[mid]
+ return &tree.Entries[mid]
} else if cmp < 0 {
low = mid + 1
} else {
diff --git a/pack_idx.go b/pack_idx.go
index d313825f..7547777d 100644
--- a/pack_idx.go
+++ b/pack_idx.go
@@ -106,11 +106,11 @@ func (pi *packIndex) load() error {
return nil
}
-func (r *Repository) packIndexes() ([]*packIndex, error) {
- r.packIdxOnce.Do(func() {
- r.packIdx, r.packIdxErr = r.loadPackIndexes()
+func (repo *Repository) packIndexes() ([]*packIndex, error) {
+ repo.packIdxOnce.Do(func() {
+ repo.packIdx, repo.packIdxErr = repo.loadPackIndexes()
})
- return r.packIdx, r.packIdxErr
+ return repo.packIdx, repo.packIdxErr
}
func (repo *Repository) loadPackIndexes() ([]*packIndex, error) {
diff --git a/repo.go b/repo.go
index 8376d8af..250b989d 100644
--- a/repo.go
+++ b/repo.go
@@ -42,29 +42,29 @@ func OpenRepository(path string, hashSize int) (*Repository, error) {
return &Repository{rootPath: path, HashSize: hashSize}, nil
}
-func (r *Repository) Close() error {
+func (repo *Repository) Close() error {
var closeErr error
- r.closeOnce.Do(func() {
- r.packFiles.Range(func(keya any, pfa any) bool {
+ repo.closeOnce.Do(func() {
+ repo.packFiles.Range(func(keya any, pfa any) bool {
key := keya.(string)
pf := pfa.(*packFile)
err := pf.Close()
if err != nil && closeErr == nil {
closeErr = err
}
- r.packFiles.Delete(key)
+ repo.packFiles.Delete(key)
return true
})
- if len(r.packIdx) > 0 {
- for _, idx := range r.packIdx {
+ if len(repo.packIdx) > 0 {
+ for _, idx := range repo.packIdx {
err := idx.Close()
if err != nil && closeErr == nil {
closeErr = err
}
}
}
- if r.midx != nil {
- err := r.midx.Close()
+ if repo.midx != nil {
+ err := repo.midx.Close()
if err != nil && closeErr == nil {
closeErr = err
}
@@ -74,24 +74,24 @@ func (r *Repository) Close() error {
}
// Root returns the repository root path.
-func (r *Repository) Root() string {
- return r.rootPath
+func (repo *Repository) Root() string {
+ return repo.rootPath
}
// repoPath joins the root with a relative path.
-func (r *Repository) repoPath(rel string) string {
- return filepath.Join(r.rootPath, rel)
+func (repo *Repository) repoPath(rel string) string {
+ return filepath.Join(repo.rootPath, rel)
}
-func (r *Repository) packFile(rel string) (*packFile, error) {
- if pf, ok := r.packFiles.Load(rel); ok {
+func (repo *Repository) packFile(rel string) (*packFile, error) {
+ if pf, ok := repo.packFiles.Load(rel); ok {
return pf.(*packFile), nil
}
- pf, err := openPackFile(r.repoPath(rel), rel)
+ pf, err := openPackFile(repo.repoPath(rel), rel)
if err != nil {
return nil, err
}
- actual, loaded := r.packFiles.LoadOrStore(rel, pf)
+ actual, loaded := repo.packFiles.LoadOrStore(rel, pf)
if loaded {
_ = pf.Close()
return actual.(*packFile), nil
@@ -100,14 +100,14 @@ func (r *Repository) packFile(rel string) (*packFile, error) {
}
// ParseHash converts a hex string into a Hash, validating it matches the repository's hash size.
-func (r *Repository) ParseHash(s string) (Hash, error) {
+func (repo *Repository) ParseHash(s string) (Hash, error) {
var id Hash
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 := r.HashSize * 2
+ expectedLen := repo.HashSize * 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, r.HashSize)
+ return id, fmt.Errorf("furgit: hash length mismatch: got %d chars, expected %d for hash size %d", len(s), expectedLen, repo.HashSize)
}
data, err := hex.DecodeString(s)
if err != nil {
@@ -119,22 +119,22 @@ func (r *Repository) ParseHash(s string) (Hash, error) {
}
// computeRawHash computes a hash from raw data using the repository's hash algorithm.
-func (r *Repository) computeRawHash(data []byte) Hash {
- hashFunc := hashFuncs[r.HashSize]
+func (repo *Repository) computeRawHash(data []byte) Hash {
+ hashFunc := hashFuncs[repo.HashSize]
return hashFunc(data)
}
// verifyRawObject verifies a raw object against its expected hash.
-func (r *Repository) verifyRawObject(buf []byte, want Hash) bool {
- if want.size != r.HashSize {
+func (repo *Repository) verifyRawObject(buf []byte, want Hash) bool {
+ if want.size != repo.HashSize {
return false
}
- return r.computeRawHash(buf) == want
+ return repo.computeRawHash(buf) == want
}
// verifyTypedObject verifies a typed object against its expected hash.
-func (r *Repository) verifyTypedObject(ty ObjType, body []byte, want Hash) bool {
- if want.size != r.HashSize {
+func (repo *Repository) verifyTypedObject(ty ObjType, body []byte, want Hash) bool {
+ if want.size != repo.HashSize {
return false
}
header, err := headerForType(ty, body)
@@ -144,5 +144,5 @@ func (r *Repository) verifyTypedObject(ty ObjType, body []byte, want Hash) bool
raw := make([]byte, len(header)+len(body))
copy(raw, header)
copy(raw[len(header):], body)
- return r.computeRawHash(raw) == want
+ return repo.computeRawHash(raw) == want
}