# HG changeset patch # User pacien # Date 1645456686 -3600 # Node ID 8b8054b8e5a7df019784cdcd6213dc65fc5a2a2b # Parent 4346be456875dd350093fee56fa39db65cb14a86 rust: expose rank computation function to python Differential Revision: https://phab.mercurial-scm.org/D12211 diff -r 4346be456875 -r 8b8054b8e5a7 rust/hg-cpython/src/dagops.rs --- a/rust/hg-cpython/src/dagops.rs Mon Feb 21 18:06:02 2022 +0100 +++ b/rust/hg-cpython/src/dagops.rs Mon Feb 21 16:18:06 2022 +0100 @@ -14,6 +14,8 @@ use hg::dagops; use hg::Revision; use std::collections::HashSet; +use vcsgraph::ancestors::node_rank; +use vcsgraph::graph::{Parents, Rank}; use crate::revlog::pyindex_to_graph; @@ -31,6 +33,18 @@ Ok(as_set) } +/// Computes the rank, i.e. the number of ancestors including itself, +/// of a node represented by its parents. +pub fn rank( + py: Python, + index: PyObject, + p1r: Revision, + p2r: Revision, +) -> PyResult { + node_rank(&pyindex_to_graph(py, index)?, &Parents([p1r, p2r])) + .map_err(|e| GraphError::pynew_from_vcsgraph(py, e)) +} + /// Create the module, with `__package__` given from parent pub fn init_module(py: Python, package: &str) -> PyResult { let dotted_name = &format!("{}.dagop", package); @@ -42,6 +56,11 @@ "headrevs", py_fn!(py, headrevs(index: PyObject, revs: PyObject)), )?; + m.add( + py, + "rank", + py_fn!(py, rank(index: PyObject, p1r: Revision, p2r: Revision)), + )?; let sys = PyModule::import(py, "sys")?; let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?;