aboutsummaryrefslogtreecommitdiff
path: root/internal/commitquery/generation.go
blob: c5edcd9f77a1c9a59849df3868b0ba4e76f91017 (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
package commitquery

import (
	"math"

	"codeberg.org/lindenii/furgit/objectid"
)

// EffectiveGeneration returns one node's generation value.
func (ctx *Context) EffectiveGeneration(idx NodeIndex) uint64 {
	if !ctx.nodes[idx].hasGeneration {
		return generationInfinity
	}

	return ctx.nodes[idx].generation
}

const (
	generationInfinity = uint64(math.MaxUint64)
)

func compareByGeneration(ctx *Context) func(NodeIndex, NodeIndex) int {
	return func(left, right NodeIndex) int {
		leftGeneration := ctx.EffectiveGeneration(left)
		rightGeneration := ctx.EffectiveGeneration(right)

		switch {
		case leftGeneration < rightGeneration:
			return -1
		case leftGeneration > rightGeneration:
			return 1
		}

		switch {
		case ctx.nodes[left].commitTime < ctx.nodes[right].commitTime:
			return -1
		case ctx.nodes[left].commitTime > ctx.nodes[right].commitTime:
			return 1
		}

		return objectid.Compare(ctx.nodes[left].id, ctx.nodes[right].id)
	}
}