Fast implementation of Git in pure Go
codeberg.org/lindenii/furgit
git
go
1package commitquery
2
3import (
4 "math"
5
6 objectid "codeberg.org/lindenii/furgit/object/id"
7)
8
9// effectiveGeneration returns one node's generation value.
10func (query *query) effectiveGeneration(idx nodeIndex) uint64 {
11 if !query.nodes[idx].hasGeneration {
12 return generationInfinity
13 }
14
15 return query.nodes[idx].generation
16}
17
18// generationInfinity sorts nodes without a known generation last.
19const (
20 generationInfinity = uint64(math.MaxUint64)
21)
22
23// compareByGeneration builds one comparator ordered by generation first.
24func (query *query) compareByGeneration() func(nodeIndex, nodeIndex) int {
25 return func(left, right nodeIndex) int {
26 leftGeneration := query.effectiveGeneration(left)
27 rightGeneration := query.effectiveGeneration(right)
28
29 switch {
30 case leftGeneration < rightGeneration:
31 return -1
32 case leftGeneration > rightGeneration:
33 return 1
34 }
35
36 switch {
37 case query.nodes[left].commitTime < query.nodes[right].commitTime:
38 return -1
39 case query.nodes[left].commitTime > query.nodes[right].commitTime:
40 return 1
41 }
42
43 return objectid.Compare(query.nodes[left].id, query.nodes[right].id)
44 }
45}