From df73a4c6f1b58075316ba7449fbfb127b9fbb79d Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 29 Mar 2026 14:42:13 +0000 Subject: commitquery: Reorganize --- commitquery/query_is_ancestor.go | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 commitquery/query_is_ancestor.go (limited to 'commitquery/query_is_ancestor.go') diff --git a/commitquery/query_is_ancestor.go b/commitquery/query_is_ancestor.go new file mode 100644 index 00000000..c21892c8 --- /dev/null +++ b/commitquery/query_is_ancestor.go @@ -0,0 +1,49 @@ +package commitquery + +import objectid "codeberg.org/lindenii/furgit/object/id" + +// IsAncestor reports whether ancestor is reachable from descendant through +// commit parent edges. +// +// Both inputs are peeled through annotated tags before commit traversal. +func (query *query) IsAncestor(ancestor, descendant objectid.ObjectID) (bool, error) { + ancestorIdx, err := query.resolveCommitish(ancestor) + if err != nil { + return false, err + } + + descendantIdx, err := query.resolveCommitish(descendant) + if err != nil { + return false, err + } + + return query.isAncestor(ancestorIdx, descendantIdx) +} + +// isAncestor answers one ancestry query between two resolved internal nodes. +func (query *query) isAncestor(ancestor, descendant nodeIndex) (bool, error) { + if ancestor == descendant { + return true, nil + } + + ancestorGeneration := query.effectiveGeneration(ancestor) + descendantGeneration := query.effectiveGeneration(descendant) + + if ancestorGeneration != generationInfinity && + descendantGeneration != generationInfinity && + ancestorGeneration > descendantGeneration { + return false, nil + } + + minGeneration := uint64(0) + if ancestorGeneration != generationInfinity { + minGeneration = ancestorGeneration + } + + err := query.paintDownToCommon(ancestor, []nodeIndex{descendant}, minGeneration) + if err != nil { + return false, err + } + + return query.hasAnyMarks(ancestor, markRight), nil +} -- cgit v1.3.1-10-gc9f91