aboutsummaryrefslogtreecommitdiff
path: root/commitgraph/read/edges.go
blob: 33a6c9fc72b3e18dc301c783263da1a0e705302d (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package read

import (
	"encoding/binary"

	"codeberg.org/lindenii/furgit/commitgraph"
	"codeberg.org/lindenii/furgit/internal/intconv"
)

func (reader *Reader) decodeExtraEdgeList(layer *layer, edgeStart uint32) ([]Position, error) {
	if len(layer.chunkExtraEdges) == 0 {
		return nil, &MalformedError{Path: layer.path, Reason: "missing EDGE chunk"}
	}

	out := make([]Position, 0)

	cur := edgeStart
	for {
		off64 := uint64(cur) * 4

		off, err := intconv.Uint64ToInt(off64)
		if err != nil {
			return nil, err
		}

		if off+4 > len(layer.chunkExtraEdges) {
			return nil, &MalformedError{Path: layer.path, Reason: "EDGE index out of range"}
		}

		word := binary.BigEndian.Uint32(layer.chunkExtraEdges[off : off+4])
		parentGlobal := word & commitgraph.ParentLastMask

		parentPos, err := reader.globalToPosition(parentGlobal)
		if err != nil {
			return nil, err
		}

		out = append(out, parentPos)

		if word&commitgraph.ParentExtraMask != 0 {
			break
		}

		cur++
	}

	return out, nil
}