The ref-sharing mechanism has improved, but its ergonomics still left a bit
to be desired, as expected.
Details
Details
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
The ref-sharing mechanism has improved, but its ergonomics still left a bit
to be desired, as expected.
Lint Skipped |
Unit Tests Skipped |
Path | Packages | |||
---|---|---|---|---|
M | rust/hg-cpython/src/dirstate.rs (24 lines) | |||
M | rust/hg-cpython/src/dirstate/dirstate_map.rs (8 lines) | |||
A | M | rust/hg-cpython/src/dirstate/status.rs (82 lines) |
Commit | Parents | Author | Summary | Date |
---|---|---|---|---|
Raphaël Gomès | Oct 8 2019, 2:45 AM |
// dirstate.rs | // dirstate.rs | ||||
// | // | ||||
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net> | // Copyright 2019 Raphaël Gomès <rgomes@octobus.net> | ||||
// | // | ||||
// This software may be used and distributed according to the terms of the | // This software may be used and distributed according to the terms of the | ||||
// GNU General Public License version 2 or any later version. | // GNU General Public License version 2 or any later version. | ||||
//! Bindings for the `hg::dirstate` module provided by the | //! Bindings for the `hg::dirstate` module provided by the | ||||
//! `hg-core` package. | //! `hg-core` package. | ||||
//! | //! | ||||
//! From Python, this will be seen as `mercurial.rustext.dirstate` | //! From Python, this will be seen as `mercurial.rustext.dirstate` | ||||
mod copymap; | mod copymap; | ||||
mod dirs_multiset; | mod dirs_multiset; | ||||
mod dirstate_map; | 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::{ | use cpython::{ | ||||
exc, PyBytes, PyDict, PyErr, PyModule, PyObject, PyResult, PySequence, | exc, PyBytes, PyDict, PyErr, PyList, PyModule, PyObject, PyResult, | ||||
Python, | PySequence, Python, | ||||
}; | }; | ||||
use hg::{ | use hg::{ | ||||
utils::hg_path::HgPathBuf, DirstateEntry, DirstateParseError, EntryState, | utils::hg_path::HgPathBuf, DirstateEntry, DirstateParseError, EntryState, | ||||
StateMap, | StateMap, | ||||
}; | }; | ||||
use libc::{c_char, c_int}; | use libc::{c_char, c_int}; | ||||
#[cfg(feature = "python27")] | #[cfg(feature = "python27")] | ||||
use python27_sys::PyCapsule_Import; | use python27_sys::PyCapsule_Import; | ||||
let dotted_name = &format!("{}.dirstate", package); | let dotted_name = &format!("{}.dirstate", package); | ||||
let m = PyModule::new(py, dotted_name)?; | let m = PyModule::new(py, dotted_name)?; | ||||
m.add(py, "__package__", package)?; | m.add(py, "__package__", package)?; | ||||
m.add(py, "__doc__", "Dirstate - Rust implementation")?; | m.add(py, "__doc__", "Dirstate - Rust implementation")?; | ||||
m.add_class::<Dirs>(py)?; | m.add_class::<Dirs>(py)?; | ||||
m.add_class::<DirstateMap>(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 = PyModule::import(py, "sys")?; | ||||
let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?; | let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?; | ||||
sys_modules.set_item(py, dotted_name, &m)?; | sys_modules.set_item(py, dotted_name, &m)?; | ||||
Ok(m) | Ok(m) | ||||
} | } |
exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject, | exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject, | ||||
PyResult, PyTuple, Python, PythonObject, ToPyObject, | PyResult, PyTuple, Python, PythonObject, ToPyObject, | ||||
}; | }; | ||||
use libc::c_char; | use libc::c_char; | ||||
use crate::{ | use crate::{ | ||||
dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator}, | dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator}, | ||||
dirstate::{decapsule_make_dirstate_tuple, dirs_multiset::Dirs}, | dirstate::{decapsule_make_dirstate_tuple, dirs_multiset::Dirs}, | ||||
ref_sharing::{PyLeakedRef, PySharedRefCell}, | ref_sharing::{PyLeakedRef, PyRefMut, PySharedRefCell}, | ||||
}; | }; | ||||
use hg::{ | use hg::{ | ||||
utils::hg_path::{HgPath, HgPathBuf}, | utils::hg_path::{HgPath, HgPathBuf}, | ||||
DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap, | DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap, | ||||
DirstateParents, DirstateParseError, EntryState, StateMapIter, | DirstateParents, DirstateParseError, EntryState, StateMapIter, | ||||
PARENT_SIZE, | PARENT_SIZE, | ||||
}; | }; | ||||
leak_handle, | leak_handle, | ||||
leaked_ref.copy_map.iter(), | leaked_ref.copy_map.iter(), | ||||
) | ) | ||||
} | } | ||||
}); | }); | ||||
impl DirstateMap { | impl DirstateMap { | ||||
pub fn get_inner_mut<'a>( | |||||
&'a self, | |||||
py: Python<'a>, | |||||
) -> PyResult<PyRefMut<'a, RustDirstateMap>> { | |||||
self.inner_shared(py).borrow_mut() | |||||
} | |||||
fn translate_key( | fn translate_key( | ||||
py: Python, | py: Python, | ||||
res: (&HgPathBuf, &DirstateEntry), | res: (&HgPathBuf, &DirstateEntry), | ||||
) -> PyResult<Option<PyBytes>> { | ) -> PyResult<Option<PyBytes>> { | ||||
Ok(Some(PyBytes::new(py, res.0.as_ref()))) | Ok(Some(PyBytes::new(py, res.0.as_ref()))) | ||||
} | } | ||||
fn translate_key_value( | fn translate_key_value( | ||||
py: Python, | py: Python, |
// 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 mut dmap = dmap.get_inner_mut(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( | |||||
&mut 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)) | |||||
} |