aboutsummaryrefslogtreecommitdiff
path: root/object/store/packed/delta_build_chain.go
diff options
context:
space:
mode:
Diffstat (limited to 'object/store/packed/delta_build_chain.go')
-rw-r--r--object/store/packed/delta_build_chain.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/object/store/packed/delta_build_chain.go b/object/store/packed/delta_build_chain.go
new file mode 100644
index 00000000..59f83542
--- /dev/null
+++ b/object/store/packed/delta_build_chain.go
@@ -0,0 +1,66 @@
+package packed
+
+import (
+ "fmt"
+
+ packfmt "codeberg.org/lindenii/furgit/format/packfile"
+ objecttype "codeberg.org/lindenii/furgit/object/type"
+)
+
+// deltaBuildChain walks one object's chain and builds a reconstruction chain.
+func (store *Store) deltaBuildChain(start location) (deltaChain, error) {
+ visited := make(map[location]struct{})
+ current := start
+
+ var chain deltaChain
+
+ for {
+ if _, ok := visited[current]; ok {
+ return deltaChain{}, fmt.Errorf("objectstore/packed: delta cycle while resolving object")
+ }
+
+ visited[current] = struct{}{}
+
+ _, meta, err := store.entryMetaAt(current)
+ if err != nil {
+ return deltaChain{}, err
+ }
+
+ if packfmt.IsBaseObjectType(meta.ty) {
+ chain.baseLoc = current
+ chain.baseType = meta.ty
+
+ return chain, nil
+ }
+
+ switch meta.ty {
+ case objecttype.TypeRefDelta:
+ chain.deltas = append(chain.deltas, deltaNode{
+ loc: current,
+ dataOffset: meta.dataOffset,
+ })
+
+ next, err := store.lookup(meta.baseRefID)
+ if err != nil {
+ return deltaChain{}, err
+ }
+
+ current = next
+ case objecttype.TypeOfsDelta:
+ chain.deltas = append(chain.deltas, deltaNode{
+ loc: current,
+ dataOffset: meta.dataOffset,
+ })
+ current = location{
+ packName: current.packName,
+ offset: meta.baseOfs,
+ }
+ case objecttype.TypeCommit, objecttype.TypeTree, objecttype.TypeBlob, objecttype.TypeTag:
+ return deltaChain{}, fmt.Errorf("objectstore/packed: internal invariant violation for base type %d", meta.ty)
+ case objecttype.TypeInvalid, objecttype.TypeFuture:
+ return deltaChain{}, fmt.Errorf("objectstore/packed: unsupported pack type %d", meta.ty)
+ default:
+ return deltaChain{}, fmt.Errorf("objectstore/packed: unsupported pack type %d", meta.ty)
+ }
+ }
+}