Patchwork D12211: rust: expose rank computation function to python

login
register
mail settings
Submitter phabricator
Date Feb. 21, 2022, 5:13 p.m.
Message ID <differential-rev-PHID-DREV-gwa23whr3nsdpc7dqsjv-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50546/
State New
Headers show

Comments

phabricator - Feb. 21, 2022, 5:13 p.m.
pacien created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D12211

AFFECTED FILES
  rust/hg-cpython/src/dagops.rs

CHANGE DETAILS




To: pacien, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/hg-cpython/src/dagops.rs b/rust/hg-cpython/src/dagops.rs
--- a/rust/hg-cpython/src/dagops.rs
+++ b/rust/hg-cpython/src/dagops.rs
@@ -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: i32,
+    p2r: i32,
+) -> PyResult<Rank> {
+    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<PyModule> {
     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: i32, p2r: i32)),
+    )?;
 
     let sys = PyModule::import(py, "sys")?;
     let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?;