aboutsummaryrefslogtreecommitdiff
path: root/obj_tree.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2025-11-16 00:00:00 +0000
committerGravatar Runxi Yu2025-11-16 00:00:00 +0000
commit5cfbd8863dfb7c6af92497d9a5eb6eb63a6bd589 (patch)
tree42a871a72388bb6d40c479fbaa6eedde1cddc42e /obj_tree.go
parenthash: Generic hash-algorithm API (diff)
signature
Revert "hash: Generic hash-algorithm API"
This reverts commit 94bfb1fa147f80e6ec39009d41fc2f853925e0a5. Generics actually kinda suck for these purposes... once you look at it from the user's perspective.
Diffstat (limited to 'obj_tree.go')
-rw-r--r--obj_tree.go36
1 files changed, 17 insertions, 19 deletions
diff --git a/obj_tree.go b/obj_tree.go
index c025dfa3..55a27a08 100644
--- a/obj_tree.go
+++ b/obj_tree.go
@@ -8,27 +8,26 @@ import (
)
// Tree represents a Git tree object.
-type Tree[T HashType] struct {
- Hash Hash[T]
- Entries []TreeEntry[T]
+type Tree struct {
+ Hash Hash
+ Entries []TreeEntry
}
// TreeEntry represents a single entry in a Git tree.
-type TreeEntry[T HashType] struct {
+type TreeEntry struct {
Mode uint32
Name []byte
- ID Hash[T]
+ ID Hash
}
// ObjType allows Tree to satisfy the Object interface.
-func (*Tree[T]) ObjType() ObjType {
+func (*Tree) ObjType() ObjType {
return ObjTree
}
// parseTree decodes a tree body.
-func parseTree[T HashType](id Hash[T], body []byte) (*Tree[T], error) {
- var entries []TreeEntry[T]
- hashSize := hashLen[T]()
+func parseTree(id Hash, body []byte, hashSize int) (*Tree, error) {
+ var entries []TreeEntry
i := 0
for i < len(body) {
space := bytes.IndexByte(body[i:], ' ')
@@ -48,8 +47,8 @@ func parseTree[T HashType](id Hash[T], body []byte) (*Tree[T], error) {
if i+hashSize > len(body) {
return nil, errors.New("furgit: tree: truncated child hash")
}
- var child Hash[T]
- copy(child.Slice(), body[i:i+hashSize])
+ var child Hash
+ copy(child[:], body[i:i+hashSize])
i += hashSize
mode, err := strconv.ParseUint(string(modeBytes), 8, 32)
@@ -57,7 +56,7 @@ func parseTree[T HashType](id Hash[T], body []byte) (*Tree[T], error) {
return nil, fmt.Errorf("furgit: tree: parse mode: %w", err)
}
- entry := TreeEntry[T]{
+ entry := TreeEntry{
Mode: uint32(mode),
Name: append([]byte(nil), nameBytes...),
ID: child,
@@ -65,15 +64,14 @@ func parseTree[T HashType](id Hash[T], body []byte) (*Tree[T], error) {
entries = append(entries, entry)
}
- return &Tree[T]{
+ return &Tree{
Hash: id,
Entries: entries,
}, nil
}
// treeBody builds the entry list for a tree without the Git header.
-func treeBody[T HashType](t *Tree[T]) []byte {
- hashSize := hashLen[T]()
+func treeBody(t *Tree, hashSize int) []byte {
var bodyLen int
for _, e := range t.Entries {
mode := strconv.FormatUint(uint64(e.Mode), 8)
@@ -90,15 +88,15 @@ func treeBody[T HashType](t *Tree[T]) []byte {
pos += copy(body[pos:], e.Name)
body[pos] = 0
pos++
- pos += copy(body[pos:], e.ID.Slice()[:hashSize])
+ pos += copy(body[pos:], e.ID[:hashSize])
}
return body
}
// Serialize renders a Tree into canonical Git format.
-func (t *Tree[T]) Serialize() ([]byte, error) {
- body := treeBody(t)
+func (t *Tree) Serialize(hashSize int) ([]byte, error) {
+ body := treeBody(t, hashSize)
header, err := headerForType(ObjTree, body)
if err != nil {
return nil, err
@@ -111,7 +109,7 @@ func (t *Tree[T]) Serialize() ([]byte, error) {
}
// Entry looks up a tree entry by name.
-func (t *Tree[T]) Entry(name []byte) *TreeEntry[T] {
+func (t *Tree) Entry(name []byte) *TreeEntry {
low, high := 0, len(t.Entries)-1
for low <= high {
mid := (low + high) / 2