aboutsummaryrefslogtreecommitdiff
path: root/format/commitgraph/read/position.go
blob: b2e1138bad552fa5aba160ad927c576eb6cd072c (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
package read

import (
	"fmt"

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

// Position identifies one commit record by layer and row index.
type Position struct {
	Graph uint32
	Index uint32
}

func (reader *Reader) globalToPosition(global uint32) (Position, error) {
	for i := range reader.layers {
		layer := &reader.layers[i]
		from := layer.globalFrom

		to := from + layer.numCommits
		if global >= from && global < to {
			graph, err := intconv.IntToUint32(i)
			if err != nil {
				return Position{}, err
			}

			return Position{
				Graph: graph,
				Index: global - from,
			}, nil
		}
	}

	return Position{}, &MalformedError{
		Path:   "commit-graph",
		Reason: fmt.Sprintf("parent global position out of range: %d", global),
	}
}