diff options
| author | 2026-03-06 08:05:51 +0800 | |
|---|---|---|
| committer | 2026-03-06 10:00:35 +0800 | |
| commit | e15054a4f93fc54806e84aa7036e60168e78e823 (patch) | |
| tree | b576dcb1d3368324e7ca73ca0fe79dd8865c5524 /format/commitgraph/parents.go | |
| parent | internal/intconv: Add Uint32ToUint8 (diff) | |
| signature | No signature | |
format/commitgraph: Add initial commit-graph support
Diffstat (limited to 'format/commitgraph/parents.go')
| -rw-r--r-- | format/commitgraph/parents.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/format/commitgraph/parents.go b/format/commitgraph/parents.go new file mode 100644 index 00000000..df6d33ff --- /dev/null +++ b/format/commitgraph/parents.go @@ -0,0 +1,65 @@ +package commitgraph + +// ParentRef references one parent position. +type ParentRef struct { + Valid bool + Pos Position +} + +func (reader *Reader) decodeParents(layer *layer, p1, p2 uint32) (ParentRef, ParentRef, []Position, error) { + parent1, err := reader.decodeSingleParent(p1) + if err != nil { + return ParentRef{}, ParentRef{}, nil, err + } + + if p2 == parentNone { + return parent1, ParentRef{}, nil, nil + } + + if p2&parentExtraMask == 0 { + parent2, err := reader.decodeSingleParent(p2) + if err != nil { + return ParentRef{}, ParentRef{}, nil, err + } + + return parent1, parent2, nil, nil + } + + edgeStart := p2 & parentLastMask + + parents, err := reader.decodeExtraEdgeList(layer, edgeStart) + if err != nil { + return ParentRef{}, ParentRef{}, nil, err + } + + if len(parents) == 0 { + return ParentRef{}, ParentRef{}, nil, &ErrMalformed{Path: layer.path, Reason: "empty EDGE list"} + } + + parent2 := ParentRef{Valid: true, Pos: parents[0]} + if len(parents) == 1 { + return parent1, parent2, nil, nil + } + + return parent1, parent2, parents[1:], nil +} + +func (reader *Reader) decodeSingleParent(raw uint32) (ParentRef, error) { + if raw == parentNone { + return ParentRef{}, nil + } + + if raw&parentExtraMask != 0 { + return ParentRef{}, &ErrMalformed{ + Path: "commit-graph", + Reason: "unexpected EDGE marker in single-parent slot", + } + } + + pos, err := reader.globalToPosition(raw) + if err != nil { + return ParentRef{}, err + } + + return ParentRef{Valid: true, Pos: pos}, nil +} |
