shithub: furgit

Download patch

ref: b3b2d5057a55baf88cbaaa31234edd3fe5d30e7f
parent: 977114819521563458c40a8d1aa35c72b23d5395
author: Runxi Yu <me@runxiyu.org>
date: Fri Feb 20 17:22:32 EST 2026

objectheader: Add loose-object header parsing and emitting code

--- /dev/null
+++ b/internal/objectheader/append.go
@@ -1,0 +1,27 @@
+package objectheader
+
+import (
+	"strconv"
+
+	"codeberg.org/lindenii/furgit/objecttype"
+)
+
+// Append appends a canonical loose-object header ("type size\\x00") to dst.
+func Append(dst []byte, ty objecttype.Type, size int64) ([]byte, bool) {
+	if size < 0 {
+		return nil, false
+	}
+	tyName, ok := objecttype.Name(ty)
+	if !ok {
+		return nil, false
+	}
+
+	sizeStr := strconv.FormatInt(size, 10)
+	out := make([]byte, 0, len(dst)+len(tyName)+len(sizeStr)+2)
+	out = append(out, dst...)
+	out = append(out, tyName...)
+	out = append(out, ' ')
+	out = append(out, sizeStr...)
+	out = append(out, 0)
+	return out, true
+}
--- /dev/null
+++ b/internal/objectheader/encode.go
@@ -1,0 +1,8 @@
+package objectheader
+
+import "codeberg.org/lindenii/furgit/objecttype"
+
+// Encode returns a canonical loose-object header ("type size\\x00").
+func Encode(ty objecttype.Type, size int64) ([]byte, bool) {
+	return Append(nil, ty, size)
+}
--- /dev/null
+++ b/internal/objectheader/parse.go
@@ -1,0 +1,40 @@
+package objectheader
+
+import (
+	"bytes"
+	"strconv"
+
+	"codeberg.org/lindenii/furgit/objecttype"
+)
+
+// Parse parses a canonical loose-object header ("type size\\x00").
+// It returns the parsed type, size, bytes consumed (including trailing NUL),
+// and whether parsing succeeded.
+func Parse(data []byte) (objecttype.Type, int64, int, bool) {
+	space := bytes.IndexByte(data, ' ')
+	if space <= 0 {
+		return objecttype.TypeInvalid, 0, 0, false
+	}
+
+	nulRel := bytes.IndexByte(data[space+1:], 0)
+	if nulRel < 0 {
+		return objecttype.TypeInvalid, 0, 0, false
+	}
+	nul := space + 1 + nulRel
+
+	ty, ok := objecttype.ParseName(string(data[:space]))
+	if !ok {
+		return objecttype.TypeInvalid, 0, 0, false
+	}
+
+	sizeBytes := data[space+1 : nul]
+	if len(sizeBytes) == 0 {
+		return objecttype.TypeInvalid, 0, 0, false
+	}
+	size, err := strconv.ParseInt(string(sizeBytes), 10, 64)
+	if err != nil || size < 0 {
+		return objecttype.TypeInvalid, 0, 0, false
+	}
+
+	return ty, size, nul + 1, true
+}
--