aboutsummaryrefslogtreecommitdiff
path: root/ancestor/ancestor.go
blob: ad90bd33393ec727f9e76463596674711b96c6a3 (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
// Package ancestor answers commit ancestry queries.
package ancestor

import (
	commitgraphread "codeberg.org/lindenii/furgit/format/commitgraph/read"
	"codeberg.org/lindenii/furgit/internal/commitquery"
	"codeberg.org/lindenii/furgit/internal/peel"
	"codeberg.org/lindenii/furgit/objectid"
	"codeberg.org/lindenii/furgit/objectstore"
)

// Is reports whether ancestor is reachable from descendant through commit
// parent edges.
//
// Both inputs are peeled through annotated tags before commit traversal.
func Is(
	store objectstore.Store,
	graph *commitgraphread.Reader,
	ancestor objectid.ObjectID,
	descendant objectid.ObjectID,
) (bool, error) {
	ancestorCommit, err := peel.ToCommit(store, ancestor)
	if err != nil {
		return false, err
	}

	descendantCommit, err := peel.ToCommit(store, descendant)
	if err != nil {
		return false, err
	}

	ctx := commitquery.NewContext(store, graph)

	ancestorIdx, err := ctx.ResolveOID(ancestorCommit)
	if err != nil {
		return false, err
	}

	descendantIdx, err := ctx.ResolveOID(descendantCommit)
	if err != nil {
		return false, err
	}

	return commitquery.IsAncestor(ctx, ancestorIdx, descendantIdx)
}