rust/hg-cpython/src/dirstate/non_normal_entries.rs
author Pierre-Yves David <pierre-yves.david@octobus.net>
Mon, 19 Jul 2021 07:23:55 +0200
changeset 47692 e5fb14a07866
parent 47335 ed1583a845d2
permissions -rw-r--r--
dirstate-map: move most of `dirstate.update_file` logic in the dsmap A new `reset_state` method is introduced to deal with most of that logic. This move things one layer lower, but the ultimate goal is to deal with most of this at the DirstateItem level. This reveal various imperfection with the data passed to update_file by `mergestate.recordupdates`, however this is orthogonal to this patch and should be dealt with at a higher level. Differential Revision: https://phab.mercurial-scm.org/D11134
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// non_normal_other_parent_entries.rs
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2020 Raphaël Gomès <rgomes@octobus.net>
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
//
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
use cpython::{
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
     9
    exc::NotImplementedError, CompareOp, ObjectProtocol, PyBytes, PyClone,
47094
e061a1df32a8 dirstate-tree: Abstract "non-normal" and "other parent" sets
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
    10
    PyErr, PyObject, PyResult, PyString, Python, PythonObject, ToPyObject,
e061a1df32a8 dirstate-tree: Abstract "non-normal" and "other parent" sets
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
    11
    UnsafePyLeaked,
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
};
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    14
use crate::dirstate::dirstate_map::v2_error;
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    15
use crate::dirstate::DirstateMap;
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    16
use hg::dirstate_tree::on_disk::DirstateV2ParseError;
47124
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47094
diff changeset
    17
use hg::utils::hg_path::HgPath;
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    18
use std::cell::RefCell;
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
py_class!(pub class NonNormalEntries |py| {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
    data dmap: DirstateMap;
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
    def __contains__(&self, key: PyObject) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
        self.dmap(py).non_normal_entries_contains(py, key)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
    def remove(&self, key: PyObject) -> PyResult<PyObject> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
        self.dmap(py).non_normal_entries_remove(py, key)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
    }
47692
e5fb14a07866 dirstate-map: move most of `dirstate.update_file` logic in the dsmap
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47335
diff changeset
    29
    def add(&self, key: PyObject) -> PyResult<PyObject> {
e5fb14a07866 dirstate-map: move most of `dirstate.update_file` logic in the dsmap
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47335
diff changeset
    30
        self.dmap(py).non_normal_entries_add(py, key)
e5fb14a07866 dirstate-map: move most of `dirstate.update_file` logic in the dsmap
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47335
diff changeset
    31
    }
e5fb14a07866 dirstate-map: move most of `dirstate.update_file` logic in the dsmap
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47335
diff changeset
    32
    def discard(&self, key: PyObject) -> PyResult<PyObject> {
e5fb14a07866 dirstate-map: move most of `dirstate.update_file` logic in the dsmap
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47335
diff changeset
    33
        self.dmap(py).non_normal_entries_discard(py, key)
e5fb14a07866 dirstate-map: move most of `dirstate.update_file` logic in the dsmap
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47335
diff changeset
    34
    }
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
    def __richcmp__(&self, other: PyObject, op: CompareOp) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
        match op {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
            CompareOp::Eq => self.is_equal_to(py, other),
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    38
            CompareOp::Ne => Ok(!self.is_equal_to(py, other)?),
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    39
            _ => Err(PyErr::new::<NotImplementedError, _>(py, ""))
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    40
        }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
    def __repr__(&self) -> PyResult<PyString> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
        self.dmap(py).non_normal_entries_display(py)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
    }
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    45
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    46
    def __iter__(&self) -> PyResult<NonNormalEntriesIterator> {
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    47
        self.dmap(py).non_normal_entries_iter(py)
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    48
    }
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
});
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
impl NonNormalEntries {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
    pub fn from_inner(py: Python, dm: DirstateMap) -> PyResult<Self> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
        Self::create_instance(py, dm)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
    fn is_equal_to(&self, py: Python, other: PyObject) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
        for item in other.iter(py)? {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
            if !self.dmap(py).non_normal_entries_contains(py, item?)? {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
                return Ok(false);
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
            }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
        }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
        Ok(true)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
    }
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    64
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    65
    fn translate_key(
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    66
        py: Python,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    67
        key: Result<&HgPath, DirstateV2ParseError>,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    68
    ) -> PyResult<Option<PyBytes>> {
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    69
        let key = key.map_err(|e| v2_error(py, e))?;
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44416
diff changeset
    70
        Ok(Some(PyBytes::new(py, key.as_bytes())))
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    71
    }
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
}
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    73
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    74
type NonNormalEntriesIter<'a> = Box<
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    75
    dyn Iterator<Item = Result<&'a HgPath, DirstateV2ParseError>> + Send + 'a,
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47124
diff changeset
    76
>;
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    77
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    78
py_shared_iterator!(
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    79
    NonNormalEntriesIterator,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    80
    UnsafePyLeaked<NonNormalEntriesIter<'static>>,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    81
    NonNormalEntries::translate_key,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    82
    Option<PyBytes>
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    83
);