aboutsummaryrefslogtreecommitdiff
path: root/object
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-04-02 08:54:27 +0000
committerGravatar Runxi Yu2026-04-02 08:54:27 +0000
commit6d799b169180a1de8b78875e0288776a6ad0d4e2 (patch)
treebce84e2f6cf9e09ce184a16a0690f89ae9da1d2f /object
parentinternal/intconv: Actually it's fine for these to be in one file probably (diff)
signatureNo signature
object/header: Add
Diffstat (limited to 'object')
-rw-r--r--object/header/append.go29
-rw-r--r--object/header/doc.go3
-rw-r--r--object/header/encode.go8
-rw-r--r--object/header/parse.go40
4 files changed, 80 insertions, 0 deletions
diff --git a/object/header/append.go b/object/header/append.go
new file mode 100644
index 00000000..5b062e05
--- /dev/null
+++ b/object/header/append.go
@@ -0,0 +1,29 @@
+package header
+
+import (
+ "strconv"
+
+ "codeberg.org/lindenii/furgit/object/typ"
+)
+
+// Append appends a canonical loose-object header ("type size\x00") to dst.
+func Append(dst []byte, ty typ.Type, size int64) ([]byte, bool) {
+ if size < 0 {
+ return nil, false
+ }
+
+ tyName, ok := ty.Name()
+ 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
+}
diff --git a/object/header/doc.go b/object/header/doc.go
new file mode 100644
index 00000000..e9003a8f
--- /dev/null
+++ b/object/header/doc.go
@@ -0,0 +1,3 @@
+// Package header parses and serializes loose-object headers
+// ("type size\x00").
+package header
diff --git a/object/header/encode.go b/object/header/encode.go
new file mode 100644
index 00000000..4cd8599c
--- /dev/null
+++ b/object/header/encode.go
@@ -0,0 +1,8 @@
+package header
+
+import "codeberg.org/lindenii/furgit/object/typ"
+
+// Encode returns a canonical loose-object header ("type size\x00").
+func Encode(ty typ.Type, size int64) ([]byte, bool) {
+ return Append(nil, ty, size)
+}
diff --git a/object/header/parse.go b/object/header/parse.go
new file mode 100644
index 00000000..77deef38
--- /dev/null
+++ b/object/header/parse.go
@@ -0,0 +1,40 @@
+package header
+
+import (
+ "bytes"
+ "strconv"
+
+ "codeberg.org/lindenii/furgit/object/typ"
+)
+
+// Parse parses a canonical loose-object header ("type size\x00").
+func Parse(data []byte) (ty typ.Type, size int64, consumed int, ok bool) {
+ space := bytes.IndexByte(data, ' ')
+ if space <= 0 {
+ return typ.TypeInvalid, 0, 0, false
+ }
+
+ nulRel := bytes.IndexByte(data[space+1:], 0)
+ if nulRel < 0 {
+ return typ.TypeInvalid, 0, 0, false
+ }
+
+ nul := space + 1 + nulRel
+
+ ty, ok = typ.Parse(string(data[:space]))
+ if !ok {
+ return typ.TypeInvalid, 0, 0, false
+ }
+
+ sizeBytes := data[space+1 : nul]
+ if len(sizeBytes) == 0 {
+ return typ.TypeInvalid, 0, 0, false
+ }
+
+ size, err := strconv.ParseInt(string(sizeBytes), 10, 64)
+ if err != nil || size < 0 {
+ return typ.TypeInvalid, 0, 0, false
+ }
+
+ return ty, size, nul + 1, true
+}