Mercurial > hg-stable
changeset 43273:478d0b1bf0c5
rust-dirstate-status: rust-cpython bindings for `dirstate.status`
The ref-sharing mechanism has improved, but its ergonomics still left a bit
to be desired, as expected.
Differential Revision: https://phab.mercurial-scm.org/D7059
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Tue, 08 Oct 2019 08:45:55 +0200 |
parents | 00222775d59b |
children | 733d4ffcd667 |
files | rust/hg-cpython/src/dirstate.rs rust/hg-cpython/src/dirstate/dirstate_map.rs rust/hg-cpython/src/dirstate/status.rs |
diffstat | 3 files changed, 110 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/hg-cpython/src/dirstate.rs Tue Oct 15 21:26:56 2019 +0200 +++ b/rust/hg-cpython/src/dirstate.rs Tue Oct 08 08:45:55 2019 +0200 @@ -12,10 +12,13 @@ mod copymap; mod dirs_multiset; mod dirstate_map; -use crate::dirstate::{dirs_multiset::Dirs, dirstate_map::DirstateMap}; +mod status; +use crate::dirstate::{ + dirs_multiset::Dirs, dirstate_map::DirstateMap, status::status_wrapper, +}; use cpython::{ - exc, PyBytes, PyDict, PyErr, PyModule, PyObject, PyResult, PySequence, - Python, + exc, PyBytes, PyDict, PyErr, PyList, PyModule, PyObject, PyResult, + PySequence, Python, }; use hg::{ utils::hg_path::HgPathBuf, DirstateEntry, DirstateParseError, EntryState, @@ -105,6 +108,21 @@ m.add_class::<Dirs>(py)?; m.add_class::<DirstateMap>(py)?; + m.add( + py, + "status", + py_fn!( + py, + status_wrapper( + dmap: DirstateMap, + root_dir: PyObject, + files: PyList, + list_clean: bool, + last_normal_time: i64, + check_exec: bool + ) + ), + )?; let sys = PyModule::import(py, "sys")?; let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?;
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs Tue Oct 15 21:26:56 2019 +0200 +++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs Tue Oct 08 08:45:55 2019 +0200 @@ -8,7 +8,7 @@ //! Bindings for the `hg::dirstate::dirstate_map` file provided by the //! `hg-core` package. -use std::cell::RefCell; +use std::cell::{Ref, RefCell}; use std::convert::TryInto; use std::time::Duration; @@ -465,6 +465,12 @@ }); impl DirstateMap { + pub fn get_inner<'a>( + &'a self, + py: Python<'a>, + ) -> Ref<'a, RustDirstateMap> { + self.inner_shared(py).borrow() + } fn translate_key( py: Python, res: (&HgPathBuf, &DirstateEntry),
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/hg-cpython/src/dirstate/status.rs Tue Oct 08 08:45:55 2019 +0200 @@ -0,0 +1,82 @@ +// status.rs +// +// Copyright 2019, Raphaël Gomès <rgomes@octobus.net> +// +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2 or any later version. + +//! Bindings for the `hg::status` module provided by the +//! `hg-core` crate. From Python, this will be seen as `rustext.dirstate.status`. +//! + +use crate::dirstate::DirstateMap; +use cpython::exc::ValueError; +use cpython::{ + PyBytes, PyErr, PyList, PyObject, PyResult, Python, PythonObject, + ToPyObject, +}; +use hg::utils::files::get_path_from_bytes; + +use hg::utils::hg_path::HgPath; +use hg::{status, utils::hg_path::HgPathBuf}; + +/// This will be useless once trait impls for collection are added to `PyBytes` +/// upstream. +fn collect_pybytes_list<P: AsRef<HgPath>>( + py: Python, + collection: &[P], +) -> PyList { + let list = PyList::new(py, &[]); + + for (i, path) in collection.iter().enumerate() { + list.insert_item( + py, + i, + PyBytes::new(py, path.as_ref().as_bytes()).into_object(), + ) + } + + list +} + +pub fn status_wrapper( + py: Python, + dmap: DirstateMap, + root_dir: PyObject, + files: PyList, + list_clean: bool, + last_normal_time: i64, + check_exec: bool, +) -> PyResult<(PyList, PyList, PyList, PyList, PyList, PyList, PyList)> { + let bytes = root_dir.extract::<PyBytes>(py)?; + let root_dir = get_path_from_bytes(bytes.data(py)); + + let dmap: DirstateMap = dmap.to_py_object(py); + let dmap = dmap.get_inner(py); + + let files: PyResult<Vec<HgPathBuf>> = files + .iter(py) + .map(|f| Ok(HgPathBuf::from_bytes(f.extract::<PyBytes>(py)?.data(py)))) + .collect(); + let files = files?; + + let (lookup, status_res) = status( + &dmap, + &root_dir, + &files, + list_clean, + last_normal_time, + check_exec, + ) + .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?; + + let modified = collect_pybytes_list(py, status_res.modified.as_ref()); + let added = collect_pybytes_list(py, status_res.added.as_ref()); + let removed = collect_pybytes_list(py, status_res.removed.as_ref()); + let deleted = collect_pybytes_list(py, status_res.deleted.as_ref()); + let clean = collect_pybytes_list(py, status_res.clean.as_ref()); + let lookup = collect_pybytes_list(py, lookup.as_ref()); + let unknown = PyList::new(py, &[]); + + Ok((lookup, modified, added, removed, deleted, unknown, clean)) +}