aboutsummaryrefslogtreecommitdiff
path: root/commitquery/graph_pos.go
blob: b1d27129544cd409948d65a437dae69da50de9f1 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package commitquery

import commitgraphread "codeberg.org/lindenii/furgit/commitgraph/read"

// resolveGraphPos resolves one commit-graph position to one internal query node.
func (query *Query) resolveGraphPos(pos commitgraphread.Position) (nodeIndex, error) {
	idx, ok := query.byGraphPos[pos]
	if ok {
		err := query.ensureLoaded(idx)
		if err != nil {
			return 0, err
		}

		return idx, nil
	}

	commit, err := query.graph.CommitAt(pos)
	if err != nil {
		return 0, err
	}

	idx, ok = query.byOID[commit.OID]
	if !ok {
		idx = query.newNode(commit.OID)
		query.byOID[commit.OID] = idx
	}

	query.byGraphPos[pos] = idx
	query.nodes[idx].graphPos = pos
	query.nodes[idx].hasGraphPos = true

	err = query.loadCommitAtGraphPos(idx, pos)
	if err != nil {
		delete(query.byGraphPos, pos)

		return 0, err
	}

	return idx, nil
}

// loadByGraphPos populates one node from a commit-graph position.
func (query *Query) loadByGraphPos(idx nodeIndex) error {
	pos := query.nodes[idx].graphPos

	return query.loadCommitAtGraphPos(idx, pos)
}

func (query *Query) loadCommitAtGraphPos(idx nodeIndex, pos commitgraphread.Position) error {
	commit, err := query.graph.CommitAt(pos)
	if err != nil {
		return err
	}

	parents := make([]parentRef, 0, 2+len(commit.ExtraParents))

	if commit.Parent1.Valid {
		parentOID, err := query.graph.OIDAt(commit.Parent1.Pos)
		if err != nil {
			return err
		}

		parents = append(parents, parentRef{
			ID:          parentOID,
			GraphPos:    commit.Parent1.Pos,
			HasGraphPos: true,
		})
	}

	if commit.Parent2.Valid {
		parentOID, err := query.graph.OIDAt(commit.Parent2.Pos)
		if err != nil {
			return err
		}

		parents = append(parents, parentRef{
			ID:          parentOID,
			GraphPos:    commit.Parent2.Pos,
			HasGraphPos: true,
		})
	}

	for _, parentPos := range commit.ExtraParents {
		parentOID, err := query.graph.OIDAt(parentPos)
		if err != nil {
			return err
		}

		parents = append(parents, parentRef{
			ID:          parentOID,
			GraphPos:    parentPos,
			HasGraphPos: true,
		})
	}

	data := commitData{
		ID:            commit.OID,
		Parents:       parents,
		CommitTime:    commit.CommitTimeUnix,
		Generation:    commit.GenerationV2,
		HasGeneration: commit.GenerationV2 != 0,
		GraphPos:      pos,
		HasGraphPos:   true,
	}

	return query.populateNode(idx, data)
}