diff options
| author | 2026-03-25 14:49:17 +0000 | |
|---|---|---|
| committer | 2026-03-25 15:02:22 +0000 | |
| commit | 7847657e0820af98120031f719b8ede635ad8c07 (patch) | |
| tree | 8c4439c78f72f1382edc809b49be33115847b6e7 /object/tree/parse.go | |
| parent | object: Remove type.go (diff) | |
| signature | No signature | |
object: Split each object type into its own package v0.1.108
Diffstat (limited to 'object/tree/parse.go')
| -rw-r--r-- | object/tree/parse.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/object/tree/parse.go b/object/tree/parse.go new file mode 100644 index 00000000..10bef968 --- /dev/null +++ b/object/tree/parse.go @@ -0,0 +1,58 @@ +package tree + +import ( + "bytes" + "fmt" + "strconv" + + objectid "codeberg.org/lindenii/furgit/object/id" +) + +// Parse decodes a tree object body. +func Parse(body []byte, algo objectid.Algorithm) (*Tree, error) { + var entries []TreeEntry + + i := 0 + for i < len(body) { + space := bytes.IndexByte(body[i:], ' ') + if space < 0 { + return nil, fmt.Errorf("object: tree: missing mode terminator") + } + + modeBytes := body[i : i+space] + i += space + 1 + + nul := bytes.IndexByte(body[i:], 0) + if nul < 0 { + return nil, fmt.Errorf("object: tree: missing name terminator") + } + + nameBytes := body[i : i+nul] + i += nul + 1 + + idEnd := i + algo.Size() + if idEnd > len(body) { + return nil, fmt.Errorf("object: tree: truncated child object id") + } + + id, err := objectid.FromBytes(algo, body[i:idEnd]) + if err != nil { + return nil, err + } + + i = idEnd + + mode, err := strconv.ParseUint(string(modeBytes), 8, 32) + if err != nil { + return nil, fmt.Errorf("object: tree: parse mode: %w", err) + } + + entries = append(entries, TreeEntry{ + Mode: FileMode(mode), + Name: append([]byte(nil), nameBytes...), + ID: id, + }) + } + + return &Tree{Entries: entries}, nil +} |
