Show More
@@ -1,129 +1,129 b'' | |||||
1 | // status.rs |
|
1 | // status.rs | |
2 | // |
|
2 | // | |
3 | // Copyright 2019, Raphaël Gomès <rgomes@octobus.net> |
|
3 | // Copyright 2019, Raphaël Gomès <rgomes@octobus.net> | |
4 | // |
|
4 | // | |
5 | // This software may be used and distributed according to the terms of the |
|
5 | // This software may be used and distributed according to the terms of the | |
6 | // GNU General Public License version 2 or any later version. |
|
6 | // GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | //! Bindings for the `hg::status` module provided by the |
|
8 | //! Bindings for the `hg::status` module provided by the | |
9 | //! `hg-core` crate. From Python, this will be seen as |
|
9 | //! `hg-core` crate. From Python, this will be seen as | |
10 | //! `rustext.dirstate.status`. |
|
10 | //! `rustext.dirstate.status`. | |
11 |
|
11 | |||
12 | use crate::dirstate::DirstateMap; |
|
12 | use crate::dirstate::DirstateMap; | |
13 | use cpython::exc::ValueError; |
|
13 | use cpython::exc::ValueError; | |
14 | use cpython::{ |
|
14 | use cpython::{ | |
15 | ObjectProtocol, PyBytes, PyErr, PyList, PyObject, PyResult, PyTuple, |
|
15 | ObjectProtocol, PyBytes, PyErr, PyList, PyObject, PyResult, PyTuple, | |
16 | Python, PythonObject, ToPyObject, |
|
16 | Python, PythonObject, ToPyObject, | |
17 | }; |
|
17 | }; | |
18 | use hg::utils::hg_path::HgPathBuf; |
|
18 | use hg::utils::hg_path::HgPathBuf; | |
19 | use hg::{ |
|
19 | use hg::{ | |
20 | matchers::{AlwaysMatcher, FileMatcher}, |
|
20 | matchers::{AlwaysMatcher, FileMatcher}, | |
21 | status, |
|
21 | status, | |
22 | utils::{files::get_path_from_bytes, hg_path::HgPath}, |
|
22 | utils::{files::get_path_from_bytes, hg_path::HgPath}, | |
23 | StatusResult, |
|
23 | StatusResult, | |
24 | }; |
|
24 | }; | |
25 | use std::borrow::Borrow; |
|
25 | use std::borrow::Borrow; | |
26 |
|
26 | |||
27 | /// This will be useless once trait impls for collection are added to `PyBytes` |
|
27 | /// This will be useless once trait impls for collection are added to `PyBytes` | |
28 | /// upstream. |
|
28 | /// upstream. | |
29 | fn collect_pybytes_list<P: AsRef<HgPath>>( |
|
29 | fn collect_pybytes_list<P: AsRef<HgPath>>( | |
30 | py: Python, |
|
30 | py: Python, | |
31 | collection: &[P], |
|
31 | collection: &[P], | |
32 | ) -> PyList { |
|
32 | ) -> PyList { | |
33 | let list = PyList::new(py, &[]); |
|
33 | let list = PyList::new(py, &[]); | |
34 |
|
34 | |||
35 | for (i, path) in collection.iter().enumerate() { |
|
35 | for (i, path) in collection.iter().enumerate() { | |
36 |
list.insert |
|
36 | list.insert( | |
37 | py, |
|
37 | py, | |
38 | i, |
|
38 | i, | |
39 | PyBytes::new(py, path.as_ref().as_bytes()).into_object(), |
|
39 | PyBytes::new(py, path.as_ref().as_bytes()).into_object(), | |
40 | ) |
|
40 | ) | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | list |
|
43 | list | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 | pub fn status_wrapper( |
|
46 | pub fn status_wrapper( | |
47 | py: Python, |
|
47 | py: Python, | |
48 | dmap: DirstateMap, |
|
48 | dmap: DirstateMap, | |
49 | matcher: PyObject, |
|
49 | matcher: PyObject, | |
50 | root_dir: PyObject, |
|
50 | root_dir: PyObject, | |
51 | list_clean: bool, |
|
51 | list_clean: bool, | |
52 | last_normal_time: i64, |
|
52 | last_normal_time: i64, | |
53 | check_exec: bool, |
|
53 | check_exec: bool, | |
54 | ) -> PyResult<(PyList, PyList, PyList, PyList, PyList, PyList, PyList)> { |
|
54 | ) -> PyResult<(PyList, PyList, PyList, PyList, PyList, PyList, PyList)> { | |
55 | let bytes = root_dir.extract::<PyBytes>(py)?; |
|
55 | let bytes = root_dir.extract::<PyBytes>(py)?; | |
56 | let root_dir = get_path_from_bytes(bytes.data(py)); |
|
56 | let root_dir = get_path_from_bytes(bytes.data(py)); | |
57 |
|
57 | |||
58 | let dmap: DirstateMap = dmap.to_py_object(py); |
|
58 | let dmap: DirstateMap = dmap.to_py_object(py); | |
59 | let dmap = dmap.get_inner(py); |
|
59 | let dmap = dmap.get_inner(py); | |
60 |
|
60 | |||
61 | match matcher.get_type(py).name(py).borrow() { |
|
61 | match matcher.get_type(py).name(py).borrow() { | |
62 | "alwaysmatcher" => { |
|
62 | "alwaysmatcher" => { | |
63 | let matcher = AlwaysMatcher; |
|
63 | let matcher = AlwaysMatcher; | |
64 | let (lookup, status_res) = status( |
|
64 | let (lookup, status_res) = status( | |
65 | &dmap, |
|
65 | &dmap, | |
66 | &matcher, |
|
66 | &matcher, | |
67 | &root_dir, |
|
67 | &root_dir, | |
68 | list_clean, |
|
68 | list_clean, | |
69 | last_normal_time, |
|
69 | last_normal_time, | |
70 | check_exec, |
|
70 | check_exec, | |
71 | ) |
|
71 | ) | |
72 | .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?; |
|
72 | .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?; | |
73 | build_response(lookup, status_res, py) |
|
73 | build_response(lookup, status_res, py) | |
74 | } |
|
74 | } | |
75 | "exactmatcher" => { |
|
75 | "exactmatcher" => { | |
76 | let files = matcher.call_method( |
|
76 | let files = matcher.call_method( | |
77 | py, |
|
77 | py, | |
78 | "files", |
|
78 | "files", | |
79 | PyTuple::new(py, &[]), |
|
79 | PyTuple::new(py, &[]), | |
80 | None, |
|
80 | None, | |
81 | )?; |
|
81 | )?; | |
82 | let files: PyList = files.cast_into(py)?; |
|
82 | let files: PyList = files.cast_into(py)?; | |
83 | let files: PyResult<Vec<HgPathBuf>> = files |
|
83 | let files: PyResult<Vec<HgPathBuf>> = files | |
84 | .iter(py) |
|
84 | .iter(py) | |
85 | .map(|f| { |
|
85 | .map(|f| { | |
86 | Ok(HgPathBuf::from_bytes( |
|
86 | Ok(HgPathBuf::from_bytes( | |
87 | f.extract::<PyBytes>(py)?.data(py), |
|
87 | f.extract::<PyBytes>(py)?.data(py), | |
88 | )) |
|
88 | )) | |
89 | }) |
|
89 | }) | |
90 | .collect(); |
|
90 | .collect(); | |
91 |
|
91 | |||
92 | let files = files?; |
|
92 | let files = files?; | |
93 | let matcher = FileMatcher::new(&files) |
|
93 | let matcher = FileMatcher::new(&files) | |
94 | .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?; |
|
94 | .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?; | |
95 | let (lookup, status_res) = status( |
|
95 | let (lookup, status_res) = status( | |
96 | &dmap, |
|
96 | &dmap, | |
97 | &matcher, |
|
97 | &matcher, | |
98 | &root_dir, |
|
98 | &root_dir, | |
99 | list_clean, |
|
99 | list_clean, | |
100 | last_normal_time, |
|
100 | last_normal_time, | |
101 | check_exec, |
|
101 | check_exec, | |
102 | ) |
|
102 | ) | |
103 | .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?; |
|
103 | .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?; | |
104 | build_response(lookup, status_res, py) |
|
104 | build_response(lookup, status_res, py) | |
105 | } |
|
105 | } | |
106 | e => { |
|
106 | e => { | |
107 | return Err(PyErr::new::<ValueError, _>( |
|
107 | return Err(PyErr::new::<ValueError, _>( | |
108 | py, |
|
108 | py, | |
109 | format!("Unsupported matcher {}", e), |
|
109 | format!("Unsupported matcher {}", e), | |
110 | )); |
|
110 | )); | |
111 | } |
|
111 | } | |
112 | } |
|
112 | } | |
113 | } |
|
113 | } | |
114 |
|
114 | |||
115 | fn build_response( |
|
115 | fn build_response( | |
116 | lookup: Vec<&HgPath>, |
|
116 | lookup: Vec<&HgPath>, | |
117 | status_res: StatusResult, |
|
117 | status_res: StatusResult, | |
118 | py: Python, |
|
118 | py: Python, | |
119 | ) -> PyResult<(PyList, PyList, PyList, PyList, PyList, PyList, PyList)> { |
|
119 | ) -> PyResult<(PyList, PyList, PyList, PyList, PyList, PyList, PyList)> { | |
120 | let modified = collect_pybytes_list(py, status_res.modified.as_ref()); |
|
120 | let modified = collect_pybytes_list(py, status_res.modified.as_ref()); | |
121 | let added = collect_pybytes_list(py, status_res.added.as_ref()); |
|
121 | let added = collect_pybytes_list(py, status_res.added.as_ref()); | |
122 | let removed = collect_pybytes_list(py, status_res.removed.as_ref()); |
|
122 | let removed = collect_pybytes_list(py, status_res.removed.as_ref()); | |
123 | let deleted = collect_pybytes_list(py, status_res.deleted.as_ref()); |
|
123 | let deleted = collect_pybytes_list(py, status_res.deleted.as_ref()); | |
124 | let clean = collect_pybytes_list(py, status_res.clean.as_ref()); |
|
124 | let clean = collect_pybytes_list(py, status_res.clean.as_ref()); | |
125 | let lookup = collect_pybytes_list(py, lookup.as_ref()); |
|
125 | let lookup = collect_pybytes_list(py, lookup.as_ref()); | |
126 | let unknown = PyList::new(py, &[]); |
|
126 | let unknown = PyList::new(py, &[]); | |
127 |
|
127 | |||
128 | Ok((lookup, modified, added, removed, deleted, unknown, clean)) |
|
128 | Ok((lookup, modified, added, removed, deleted, unknown, clean)) | |
129 | } |
|
129 | } |
General Comments 0
You need to be logged in to leave comments.
Login now