##// END OF EJS Templates
rust-dirstate: remove excessive clone() of parameter and return value...
Yuya Nishihara -
r43069:1a535313 default
parent child Browse files
Show More
@@ -1,426 +1,426 b''
1 // dirstate_map.rs
1 // dirstate_map.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 use crate::{
8 use crate::{
9 dirstate::{parsers::PARENT_SIZE, EntryState},
9 dirstate::{parsers::PARENT_SIZE, EntryState},
10 pack_dirstate, parse_dirstate, CopyMap, DirsIterable, DirsMultiset,
10 pack_dirstate, parse_dirstate, CopyMap, DirsIterable, DirsMultiset,
11 DirstateEntry, DirstateError, DirstateMapError, DirstateParents,
11 DirstateEntry, DirstateError, DirstateMapError, DirstateParents,
12 DirstateParseError, StateMap,
12 DirstateParseError, StateMap,
13 };
13 };
14 use core::borrow::Borrow;
14 use core::borrow::Borrow;
15 use std::collections::{HashMap, HashSet};
15 use std::collections::{HashMap, HashSet};
16 use std::convert::TryInto;
16 use std::convert::TryInto;
17 use std::iter::FromIterator;
17 use std::iter::FromIterator;
18 use std::ops::Deref;
18 use std::ops::Deref;
19 use std::time::Duration;
19 use std::time::Duration;
20
20
21 pub type FileFoldMap = HashMap<Vec<u8>, Vec<u8>>;
21 pub type FileFoldMap = HashMap<Vec<u8>, Vec<u8>>;
22
22
23 const NULL_ID: [u8; 20] = [0; 20];
23 const NULL_ID: [u8; 20] = [0; 20];
24 const MTIME_UNSET: i32 = -1;
24 const MTIME_UNSET: i32 = -1;
25 const SIZE_DIRTY: i32 = -2;
25 const SIZE_DIRTY: i32 = -2;
26
26
27 #[derive(Default)]
27 #[derive(Default)]
28 pub struct DirstateMap {
28 pub struct DirstateMap {
29 state_map: StateMap,
29 state_map: StateMap,
30 pub copy_map: CopyMap,
30 pub copy_map: CopyMap,
31 file_fold_map: Option<FileFoldMap>,
31 file_fold_map: Option<FileFoldMap>,
32 pub dirs: Option<DirsMultiset>,
32 pub dirs: Option<DirsMultiset>,
33 pub all_dirs: Option<DirsMultiset>,
33 pub all_dirs: Option<DirsMultiset>,
34 non_normal_set: HashSet<Vec<u8>>,
34 non_normal_set: HashSet<Vec<u8>>,
35 other_parent_set: HashSet<Vec<u8>>,
35 other_parent_set: HashSet<Vec<u8>>,
36 parents: Option<DirstateParents>,
36 parents: Option<DirstateParents>,
37 dirty_parents: bool,
37 dirty_parents: bool,
38 }
38 }
39
39
40 /// Should only really be used in python interface code, for clarity
40 /// Should only really be used in python interface code, for clarity
41 impl Deref for DirstateMap {
41 impl Deref for DirstateMap {
42 type Target = StateMap;
42 type Target = StateMap;
43
43
44 fn deref(&self) -> &Self::Target {
44 fn deref(&self) -> &Self::Target {
45 &self.state_map
45 &self.state_map
46 }
46 }
47 }
47 }
48
48
49 impl FromIterator<(Vec<u8>, DirstateEntry)> for DirstateMap {
49 impl FromIterator<(Vec<u8>, DirstateEntry)> for DirstateMap {
50 fn from_iter<I: IntoIterator<Item = (Vec<u8>, DirstateEntry)>>(
50 fn from_iter<I: IntoIterator<Item = (Vec<u8>, DirstateEntry)>>(
51 iter: I,
51 iter: I,
52 ) -> Self {
52 ) -> Self {
53 Self {
53 Self {
54 state_map: iter.into_iter().collect(),
54 state_map: iter.into_iter().collect(),
55 ..Self::default()
55 ..Self::default()
56 }
56 }
57 }
57 }
58 }
58 }
59
59
60 impl DirstateMap {
60 impl DirstateMap {
61 pub fn new() -> Self {
61 pub fn new() -> Self {
62 Self::default()
62 Self::default()
63 }
63 }
64
64
65 pub fn clear(&mut self) {
65 pub fn clear(&mut self) {
66 self.state_map.clear();
66 self.state_map.clear();
67 self.copy_map.clear();
67 self.copy_map.clear();
68 self.file_fold_map = None;
68 self.file_fold_map = None;
69 self.non_normal_set.clear();
69 self.non_normal_set.clear();
70 self.other_parent_set.clear();
70 self.other_parent_set.clear();
71 self.set_parents(DirstateParents {
71 self.set_parents(&DirstateParents {
72 p1: NULL_ID,
72 p1: NULL_ID,
73 p2: NULL_ID,
73 p2: NULL_ID,
74 })
74 })
75 }
75 }
76
76
77 /// Add a tracked file to the dirstate
77 /// Add a tracked file to the dirstate
78 pub fn add_file(
78 pub fn add_file(
79 &mut self,
79 &mut self,
80 filename: &[u8],
80 filename: &[u8],
81 old_state: EntryState,
81 old_state: EntryState,
82 entry: DirstateEntry,
82 entry: DirstateEntry,
83 ) {
83 ) {
84 if old_state == EntryState::Unknown || old_state == EntryState::Removed
84 if old_state == EntryState::Unknown || old_state == EntryState::Removed
85 {
85 {
86 if let Some(ref mut dirs) = self.dirs {
86 if let Some(ref mut dirs) = self.dirs {
87 dirs.add_path(filename)
87 dirs.add_path(filename)
88 }
88 }
89 }
89 }
90 if old_state == EntryState::Unknown {
90 if old_state == EntryState::Unknown {
91 if let Some(ref mut all_dirs) = self.all_dirs {
91 if let Some(ref mut all_dirs) = self.all_dirs {
92 all_dirs.add_path(filename)
92 all_dirs.add_path(filename)
93 }
93 }
94 }
94 }
95 self.state_map.insert(filename.to_owned(), entry.to_owned());
95 self.state_map.insert(filename.to_owned(), entry.to_owned());
96
96
97 if entry.state != EntryState::Normal || entry.mtime == MTIME_UNSET {
97 if entry.state != EntryState::Normal || entry.mtime == MTIME_UNSET {
98 self.non_normal_set.insert(filename.to_owned());
98 self.non_normal_set.insert(filename.to_owned());
99 }
99 }
100
100
101 if entry.size == SIZE_DIRTY {
101 if entry.size == SIZE_DIRTY {
102 self.other_parent_set.insert(filename.to_owned());
102 self.other_parent_set.insert(filename.to_owned());
103 }
103 }
104 }
104 }
105
105
106 /// Mark a file as removed in the dirstate.
106 /// Mark a file as removed in the dirstate.
107 ///
107 ///
108 /// The `size` parameter is used to store sentinel values that indicate
108 /// The `size` parameter is used to store sentinel values that indicate
109 /// the file's previous state. In the future, we should refactor this
109 /// the file's previous state. In the future, we should refactor this
110 /// to be more explicit about what that state is.
110 /// to be more explicit about what that state is.
111 pub fn remove_file(
111 pub fn remove_file(
112 &mut self,
112 &mut self,
113 filename: &[u8],
113 filename: &[u8],
114 old_state: EntryState,
114 old_state: EntryState,
115 size: i32,
115 size: i32,
116 ) -> Result<(), DirstateMapError> {
116 ) -> Result<(), DirstateMapError> {
117 if old_state != EntryState::Unknown && old_state != EntryState::Removed
117 if old_state != EntryState::Unknown && old_state != EntryState::Removed
118 {
118 {
119 if let Some(ref mut dirs) = self.dirs {
119 if let Some(ref mut dirs) = self.dirs {
120 dirs.delete_path(filename)?;
120 dirs.delete_path(filename)?;
121 }
121 }
122 }
122 }
123 if old_state == EntryState::Unknown {
123 if old_state == EntryState::Unknown {
124 if let Some(ref mut all_dirs) = self.all_dirs {
124 if let Some(ref mut all_dirs) = self.all_dirs {
125 all_dirs.add_path(filename);
125 all_dirs.add_path(filename);
126 }
126 }
127 }
127 }
128
128
129 if let Some(ref mut file_fold_map) = self.file_fold_map {
129 if let Some(ref mut file_fold_map) = self.file_fold_map {
130 file_fold_map.remove(&filename.to_ascii_uppercase());
130 file_fold_map.remove(&filename.to_ascii_uppercase());
131 }
131 }
132 self.state_map.insert(
132 self.state_map.insert(
133 filename.to_owned(),
133 filename.to_owned(),
134 DirstateEntry {
134 DirstateEntry {
135 state: EntryState::Removed,
135 state: EntryState::Removed,
136 mode: 0,
136 mode: 0,
137 size,
137 size,
138 mtime: 0,
138 mtime: 0,
139 },
139 },
140 );
140 );
141 self.non_normal_set.insert(filename.to_owned());
141 self.non_normal_set.insert(filename.to_owned());
142 Ok(())
142 Ok(())
143 }
143 }
144
144
145 /// Remove a file from the dirstate.
145 /// Remove a file from the dirstate.
146 /// Returns `true` if the file was previously recorded.
146 /// Returns `true` if the file was previously recorded.
147 pub fn drop_file(
147 pub fn drop_file(
148 &mut self,
148 &mut self,
149 filename: &[u8],
149 filename: &[u8],
150 old_state: EntryState,
150 old_state: EntryState,
151 ) -> Result<bool, DirstateMapError> {
151 ) -> Result<bool, DirstateMapError> {
152 let exists = self.state_map.remove(filename).is_some();
152 let exists = self.state_map.remove(filename).is_some();
153
153
154 if exists {
154 if exists {
155 if old_state != EntryState::Removed {
155 if old_state != EntryState::Removed {
156 if let Some(ref mut dirs) = self.dirs {
156 if let Some(ref mut dirs) = self.dirs {
157 dirs.delete_path(filename)?;
157 dirs.delete_path(filename)?;
158 }
158 }
159 }
159 }
160 if let Some(ref mut all_dirs) = self.all_dirs {
160 if let Some(ref mut all_dirs) = self.all_dirs {
161 all_dirs.delete_path(filename)?;
161 all_dirs.delete_path(filename)?;
162 }
162 }
163 }
163 }
164 if let Some(ref mut file_fold_map) = self.file_fold_map {
164 if let Some(ref mut file_fold_map) = self.file_fold_map {
165 file_fold_map.remove(&filename.to_ascii_uppercase());
165 file_fold_map.remove(&filename.to_ascii_uppercase());
166 }
166 }
167 self.non_normal_set.remove(filename);
167 self.non_normal_set.remove(filename);
168
168
169 Ok(exists)
169 Ok(exists)
170 }
170 }
171
171
172 pub fn clear_ambiguous_times(
172 pub fn clear_ambiguous_times(
173 &mut self,
173 &mut self,
174 filenames: Vec<Vec<u8>>,
174 filenames: Vec<Vec<u8>>,
175 now: i32,
175 now: i32,
176 ) {
176 ) {
177 for filename in filenames {
177 for filename in filenames {
178 let mut changed = false;
178 let mut changed = false;
179 self.state_map
179 self.state_map
180 .entry(filename.to_owned())
180 .entry(filename.to_owned())
181 .and_modify(|entry| {
181 .and_modify(|entry| {
182 if entry.state == EntryState::Normal && entry.mtime == now
182 if entry.state == EntryState::Normal && entry.mtime == now
183 {
183 {
184 changed = true;
184 changed = true;
185 *entry = DirstateEntry {
185 *entry = DirstateEntry {
186 mtime: MTIME_UNSET,
186 mtime: MTIME_UNSET,
187 ..*entry
187 ..*entry
188 };
188 };
189 }
189 }
190 });
190 });
191 if changed {
191 if changed {
192 self.non_normal_set.insert(filename.to_owned());
192 self.non_normal_set.insert(filename.to_owned());
193 }
193 }
194 }
194 }
195 }
195 }
196
196
197 pub fn non_normal_other_parent_entries(
197 pub fn non_normal_other_parent_entries(
198 &self,
198 &self,
199 ) -> (HashSet<Vec<u8>>, HashSet<Vec<u8>>) {
199 ) -> (HashSet<Vec<u8>>, HashSet<Vec<u8>>) {
200 let mut non_normal = HashSet::new();
200 let mut non_normal = HashSet::new();
201 let mut other_parent = HashSet::new();
201 let mut other_parent = HashSet::new();
202
202
203 for (
203 for (
204 filename,
204 filename,
205 DirstateEntry {
205 DirstateEntry {
206 state, size, mtime, ..
206 state, size, mtime, ..
207 },
207 },
208 ) in self.state_map.iter()
208 ) in self.state_map.iter()
209 {
209 {
210 if *state != EntryState::Normal || *mtime == MTIME_UNSET {
210 if *state != EntryState::Normal || *mtime == MTIME_UNSET {
211 non_normal.insert(filename.to_owned());
211 non_normal.insert(filename.to_owned());
212 }
212 }
213 if *state == EntryState::Normal && *size == SIZE_DIRTY {
213 if *state == EntryState::Normal && *size == SIZE_DIRTY {
214 other_parent.insert(filename.to_owned());
214 other_parent.insert(filename.to_owned());
215 }
215 }
216 }
216 }
217
217
218 (non_normal, other_parent)
218 (non_normal, other_parent)
219 }
219 }
220
220
221 /// Both of these setters and their uses appear to be the simplest way to
221 /// Both of these setters and their uses appear to be the simplest way to
222 /// emulate a Python lazy property, but it is ugly and unidiomatic.
222 /// emulate a Python lazy property, but it is ugly and unidiomatic.
223 /// TODO One day, rewriting this struct using the typestate might be a
223 /// TODO One day, rewriting this struct using the typestate might be a
224 /// good idea.
224 /// good idea.
225 pub fn set_all_dirs(&mut self) {
225 pub fn set_all_dirs(&mut self) {
226 if self.all_dirs.is_none() {
226 if self.all_dirs.is_none() {
227 self.all_dirs = Some(DirsMultiset::new(
227 self.all_dirs = Some(DirsMultiset::new(
228 DirsIterable::Dirstate(&self.state_map),
228 DirsIterable::Dirstate(&self.state_map),
229 None,
229 None,
230 ));
230 ));
231 }
231 }
232 }
232 }
233
233
234 pub fn set_dirs(&mut self) {
234 pub fn set_dirs(&mut self) {
235 if self.dirs.is_none() {
235 if self.dirs.is_none() {
236 self.dirs = Some(DirsMultiset::new(
236 self.dirs = Some(DirsMultiset::new(
237 DirsIterable::Dirstate(&self.state_map),
237 DirsIterable::Dirstate(&self.state_map),
238 Some(EntryState::Removed),
238 Some(EntryState::Removed),
239 ));
239 ));
240 }
240 }
241 }
241 }
242
242
243 pub fn has_tracked_dir(&mut self, directory: &[u8]) -> bool {
243 pub fn has_tracked_dir(&mut self, directory: &[u8]) -> bool {
244 self.set_dirs();
244 self.set_dirs();
245 self.dirs.as_ref().unwrap().contains(directory)
245 self.dirs.as_ref().unwrap().contains(directory)
246 }
246 }
247
247
248 pub fn has_dir(&mut self, directory: &[u8]) -> bool {
248 pub fn has_dir(&mut self, directory: &[u8]) -> bool {
249 self.set_all_dirs();
249 self.set_all_dirs();
250 self.all_dirs.as_ref().unwrap().contains(directory)
250 self.all_dirs.as_ref().unwrap().contains(directory)
251 }
251 }
252
252
253 pub fn parents(
253 pub fn parents(
254 &mut self,
254 &mut self,
255 file_contents: &[u8],
255 file_contents: &[u8],
256 ) -> Result<DirstateParents, DirstateError> {
256 ) -> Result<&DirstateParents, DirstateError> {
257 if let Some(ref parents) = self.parents {
257 if let Some(ref parents) = self.parents {
258 return Ok(parents.clone());
258 return Ok(parents);
259 }
259 }
260 let parents;
260 let parents;
261 if file_contents.len() == PARENT_SIZE * 2 {
261 if file_contents.len() == PARENT_SIZE * 2 {
262 parents = DirstateParents {
262 parents = DirstateParents {
263 p1: file_contents[..PARENT_SIZE].try_into().unwrap(),
263 p1: file_contents[..PARENT_SIZE].try_into().unwrap(),
264 p2: file_contents[PARENT_SIZE..PARENT_SIZE * 2]
264 p2: file_contents[PARENT_SIZE..PARENT_SIZE * 2]
265 .try_into()
265 .try_into()
266 .unwrap(),
266 .unwrap(),
267 };
267 };
268 } else if file_contents.is_empty() {
268 } else if file_contents.is_empty() {
269 parents = DirstateParents {
269 parents = DirstateParents {
270 p1: NULL_ID,
270 p1: NULL_ID,
271 p2: NULL_ID,
271 p2: NULL_ID,
272 };
272 };
273 } else {
273 } else {
274 return Err(DirstateError::Parse(DirstateParseError::Damaged));
274 return Err(DirstateError::Parse(DirstateParseError::Damaged));
275 }
275 }
276
276
277 self.parents = Some(parents.to_owned());
277 self.parents = Some(parents);
278 Ok(parents.clone())
278 Ok(self.parents.as_ref().unwrap())
279 }
279 }
280
280
281 pub fn set_parents(&mut self, parents: DirstateParents) {
281 pub fn set_parents(&mut self, parents: &DirstateParents) {
282 self.parents = Some(parents.clone());
282 self.parents = Some(parents.clone());
283 self.dirty_parents = true;
283 self.dirty_parents = true;
284 }
284 }
285
285
286 pub fn read(
286 pub fn read(
287 &mut self,
287 &mut self,
288 file_contents: &[u8],
288 file_contents: &[u8],
289 ) -> Result<Option<DirstateParents>, DirstateError> {
289 ) -> Result<Option<DirstateParents>, DirstateError> {
290 if file_contents.is_empty() {
290 if file_contents.is_empty() {
291 return Ok(None);
291 return Ok(None);
292 }
292 }
293
293
294 let parents = parse_dirstate(
294 let parents = parse_dirstate(
295 &mut self.state_map,
295 &mut self.state_map,
296 &mut self.copy_map,
296 &mut self.copy_map,
297 file_contents,
297 file_contents,
298 )?;
298 )?;
299
299
300 if !self.dirty_parents {
300 if !self.dirty_parents {
301 self.set_parents(parents.to_owned());
301 self.set_parents(&parents);
302 }
302 }
303
303
304 Ok(Some(parents))
304 Ok(Some(parents))
305 }
305 }
306
306
307 pub fn pack(
307 pub fn pack(
308 &mut self,
308 &mut self,
309 parents: DirstateParents,
309 parents: DirstateParents,
310 now: Duration,
310 now: Duration,
311 ) -> Result<Vec<u8>, DirstateError> {
311 ) -> Result<Vec<u8>, DirstateError> {
312 let packed =
312 let packed =
313 pack_dirstate(&mut self.state_map, &self.copy_map, parents, now)?;
313 pack_dirstate(&mut self.state_map, &self.copy_map, parents, now)?;
314
314
315 self.dirty_parents = false;
315 self.dirty_parents = false;
316
316
317 let result = self.non_normal_other_parent_entries();
317 let result = self.non_normal_other_parent_entries();
318 self.non_normal_set = result.0;
318 self.non_normal_set = result.0;
319 self.other_parent_set = result.1;
319 self.other_parent_set = result.1;
320 Ok(packed)
320 Ok(packed)
321 }
321 }
322
322
323 pub fn build_file_fold_map(&mut self) -> FileFoldMap {
323 pub fn build_file_fold_map(&mut self) -> &FileFoldMap {
324 if let Some(ref file_fold_map) = self.file_fold_map {
324 if let Some(ref file_fold_map) = self.file_fold_map {
325 return file_fold_map.to_owned();
325 return file_fold_map;
326 }
326 }
327 let mut new_file_fold_map = FileFoldMap::new();
327 let mut new_file_fold_map = FileFoldMap::new();
328 for (filename, DirstateEntry { state, .. }) in self.state_map.borrow()
328 for (filename, DirstateEntry { state, .. }) in self.state_map.borrow()
329 {
329 {
330 if *state == EntryState::Removed {
330 if *state == EntryState::Removed {
331 new_file_fold_map.insert(
331 new_file_fold_map.insert(
332 filename.to_ascii_uppercase().to_owned(),
332 filename.to_ascii_uppercase().to_owned(),
333 filename.to_owned(),
333 filename.to_owned(),
334 );
334 );
335 }
335 }
336 }
336 }
337 self.file_fold_map = Some(new_file_fold_map);
337 self.file_fold_map = Some(new_file_fold_map);
338 self.file_fold_map.to_owned().unwrap()
338 self.file_fold_map.as_ref().unwrap()
339 }
339 }
340 }
340 }
341
341
342 #[cfg(test)]
342 #[cfg(test)]
343 mod tests {
343 mod tests {
344 use super::*;
344 use super::*;
345
345
346 #[test]
346 #[test]
347 fn test_dirs_multiset() {
347 fn test_dirs_multiset() {
348 let mut map = DirstateMap::new();
348 let mut map = DirstateMap::new();
349 assert!(map.dirs.is_none());
349 assert!(map.dirs.is_none());
350 assert!(map.all_dirs.is_none());
350 assert!(map.all_dirs.is_none());
351
351
352 assert_eq!(false, map.has_dir(b"nope"));
352 assert_eq!(false, map.has_dir(b"nope"));
353 assert!(map.all_dirs.is_some());
353 assert!(map.all_dirs.is_some());
354 assert!(map.dirs.is_none());
354 assert!(map.dirs.is_none());
355
355
356 assert_eq!(false, map.has_tracked_dir(b"nope"));
356 assert_eq!(false, map.has_tracked_dir(b"nope"));
357 assert!(map.dirs.is_some());
357 assert!(map.dirs.is_some());
358 }
358 }
359
359
360 #[test]
360 #[test]
361 fn test_add_file() {
361 fn test_add_file() {
362 let mut map = DirstateMap::new();
362 let mut map = DirstateMap::new();
363
363
364 assert_eq!(0, map.len());
364 assert_eq!(0, map.len());
365
365
366 map.add_file(
366 map.add_file(
367 b"meh",
367 b"meh",
368 EntryState::Normal,
368 EntryState::Normal,
369 DirstateEntry {
369 DirstateEntry {
370 state: EntryState::Normal,
370 state: EntryState::Normal,
371 mode: 1337,
371 mode: 1337,
372 mtime: 1337,
372 mtime: 1337,
373 size: 1337,
373 size: 1337,
374 },
374 },
375 );
375 );
376
376
377 assert_eq!(1, map.len());
377 assert_eq!(1, map.len());
378 assert_eq!(0, map.non_normal_set.len());
378 assert_eq!(0, map.non_normal_set.len());
379 assert_eq!(0, map.other_parent_set.len());
379 assert_eq!(0, map.other_parent_set.len());
380 }
380 }
381
381
382 #[test]
382 #[test]
383 fn test_non_normal_other_parent_entries() {
383 fn test_non_normal_other_parent_entries() {
384 let map: DirstateMap = [
384 let map: DirstateMap = [
385 (b"f1", (EntryState::Removed, 1337, 1337, 1337)),
385 (b"f1", (EntryState::Removed, 1337, 1337, 1337)),
386 (b"f2", (EntryState::Normal, 1337, 1337, -1)),
386 (b"f2", (EntryState::Normal, 1337, 1337, -1)),
387 (b"f3", (EntryState::Normal, 1337, 1337, 1337)),
387 (b"f3", (EntryState::Normal, 1337, 1337, 1337)),
388 (b"f4", (EntryState::Normal, 1337, -2, 1337)),
388 (b"f4", (EntryState::Normal, 1337, -2, 1337)),
389 (b"f5", (EntryState::Added, 1337, 1337, 1337)),
389 (b"f5", (EntryState::Added, 1337, 1337, 1337)),
390 (b"f6", (EntryState::Added, 1337, 1337, -1)),
390 (b"f6", (EntryState::Added, 1337, 1337, -1)),
391 (b"f7", (EntryState::Merged, 1337, 1337, -1)),
391 (b"f7", (EntryState::Merged, 1337, 1337, -1)),
392 (b"f8", (EntryState::Merged, 1337, 1337, 1337)),
392 (b"f8", (EntryState::Merged, 1337, 1337, 1337)),
393 (b"f9", (EntryState::Merged, 1337, -2, 1337)),
393 (b"f9", (EntryState::Merged, 1337, -2, 1337)),
394 (b"fa", (EntryState::Added, 1337, -2, 1337)),
394 (b"fa", (EntryState::Added, 1337, -2, 1337)),
395 (b"fb", (EntryState::Removed, 1337, -2, 1337)),
395 (b"fb", (EntryState::Removed, 1337, -2, 1337)),
396 ]
396 ]
397 .iter()
397 .iter()
398 .map(|(fname, (state, mode, size, mtime))| {
398 .map(|(fname, (state, mode, size, mtime))| {
399 (
399 (
400 fname.to_vec(),
400 fname.to_vec(),
401 DirstateEntry {
401 DirstateEntry {
402 state: *state,
402 state: *state,
403 mode: *mode,
403 mode: *mode,
404 size: *size,
404 size: *size,
405 mtime: *mtime,
405 mtime: *mtime,
406 },
406 },
407 )
407 )
408 })
408 })
409 .collect();
409 .collect();
410
410
411 let non_normal = [
411 let non_normal = [
412 b"f1", b"f2", b"f5", b"f6", b"f7", b"f8", b"f9", b"fa", b"fb",
412 b"f1", b"f2", b"f5", b"f6", b"f7", b"f8", b"f9", b"fa", b"fb",
413 ]
413 ]
414 .iter()
414 .iter()
415 .map(|x| x.to_vec())
415 .map(|x| x.to_vec())
416 .collect();
416 .collect();
417
417
418 let mut other_parent = HashSet::new();
418 let mut other_parent = HashSet::new();
419 other_parent.insert(b"f4".to_vec());
419 other_parent.insert(b"f4".to_vec());
420
420
421 assert_eq!(
421 assert_eq!(
422 (non_normal, other_parent),
422 (non_normal, other_parent),
423 map.non_normal_other_parent_entries()
423 map.non_normal_other_parent_entries()
424 );
424 );
425 }
425 }
426 }
426 }
@@ -1,515 +1,515 b''
1 // dirstate_map.rs
1 // dirstate_map.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::dirstate::dirstate_map` file provided by the
8 //! Bindings for the `hg::dirstate::dirstate_map` file provided by the
9 //! `hg-core` package.
9 //! `hg-core` package.
10
10
11 use std::cell::RefCell;
11 use std::cell::RefCell;
12 use std::convert::TryInto;
12 use std::convert::TryInto;
13 use std::time::Duration;
13 use std::time::Duration;
14
14
15 use cpython::{
15 use cpython::{
16 exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject,
16 exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject,
17 PyResult, PyTuple, Python, PythonObject, ToPyObject,
17 PyResult, PyTuple, Python, PythonObject, ToPyObject,
18 };
18 };
19 use libc::c_char;
19 use libc::c_char;
20
20
21 use crate::{
21 use crate::{
22 dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator},
22 dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator},
23 dirstate::{decapsule_make_dirstate_tuple, dirs_multiset::Dirs},
23 dirstate::{decapsule_make_dirstate_tuple, dirs_multiset::Dirs},
24 ref_sharing::PySharedState,
24 ref_sharing::PySharedState,
25 };
25 };
26 use hg::{
26 use hg::{
27 DirsIterable, DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap,
27 DirsIterable, DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap,
28 DirstateParents, DirstateParseError, EntryState, PARENT_SIZE,
28 DirstateParents, DirstateParseError, EntryState, PARENT_SIZE,
29 };
29 };
30
30
31 // TODO
31 // TODO
32 // This object needs to share references to multiple members of its Rust
32 // This object needs to share references to multiple members of its Rust
33 // inner struct, namely `copy_map`, `dirs` and `all_dirs`.
33 // inner struct, namely `copy_map`, `dirs` and `all_dirs`.
34 // Right now `CopyMap` is done, but it needs to have an explicit reference
34 // Right now `CopyMap` is done, but it needs to have an explicit reference
35 // to `RustDirstateMap` which itself needs to have an encapsulation for
35 // to `RustDirstateMap` which itself needs to have an encapsulation for
36 // every method in `CopyMap` (copymapcopy, etc.).
36 // every method in `CopyMap` (copymapcopy, etc.).
37 // This is ugly and hard to maintain.
37 // This is ugly and hard to maintain.
38 // The same logic applies to `dirs` and `all_dirs`, however the `Dirs`
38 // The same logic applies to `dirs` and `all_dirs`, however the `Dirs`
39 // `py_class!` is already implemented and does not mention
39 // `py_class!` is already implemented and does not mention
40 // `RustDirstateMap`, rightfully so.
40 // `RustDirstateMap`, rightfully so.
41 // All attributes also have to have a separate refcount data attribute for
41 // All attributes also have to have a separate refcount data attribute for
42 // leaks, with all methods that go along for reference sharing.
42 // leaks, with all methods that go along for reference sharing.
43 py_class!(pub class DirstateMap |py| {
43 py_class!(pub class DirstateMap |py| {
44 data inner: RefCell<RustDirstateMap>;
44 data inner: RefCell<RustDirstateMap>;
45 data py_shared_state: PySharedState;
45 data py_shared_state: PySharedState;
46
46
47 def __new__(_cls, _root: PyObject) -> PyResult<Self> {
47 def __new__(_cls, _root: PyObject) -> PyResult<Self> {
48 let inner = RustDirstateMap::default();
48 let inner = RustDirstateMap::default();
49 Self::create_instance(
49 Self::create_instance(
50 py,
50 py,
51 RefCell::new(inner),
51 RefCell::new(inner),
52 PySharedState::default()
52 PySharedState::default()
53 )
53 )
54 }
54 }
55
55
56 def clear(&self) -> PyResult<PyObject> {
56 def clear(&self) -> PyResult<PyObject> {
57 self.borrow_mut(py)?.clear();
57 self.borrow_mut(py)?.clear();
58 Ok(py.None())
58 Ok(py.None())
59 }
59 }
60
60
61 def get(
61 def get(
62 &self,
62 &self,
63 key: PyObject,
63 key: PyObject,
64 default: Option<PyObject> = None
64 default: Option<PyObject> = None
65 ) -> PyResult<Option<PyObject>> {
65 ) -> PyResult<Option<PyObject>> {
66 let key = key.extract::<PyBytes>(py)?;
66 let key = key.extract::<PyBytes>(py)?;
67 match self.inner(py).borrow().get(key.data(py)) {
67 match self.inner(py).borrow().get(key.data(py)) {
68 Some(entry) => {
68 Some(entry) => {
69 // Explicitly go through u8 first, then cast to
69 // Explicitly go through u8 first, then cast to
70 // platform-specific `c_char`.
70 // platform-specific `c_char`.
71 let state: u8 = entry.state.into();
71 let state: u8 = entry.state.into();
72 Ok(Some(decapsule_make_dirstate_tuple(py)?(
72 Ok(Some(decapsule_make_dirstate_tuple(py)?(
73 state as c_char,
73 state as c_char,
74 entry.mode,
74 entry.mode,
75 entry.size,
75 entry.size,
76 entry.mtime,
76 entry.mtime,
77 )))
77 )))
78 },
78 },
79 None => Ok(default)
79 None => Ok(default)
80 }
80 }
81 }
81 }
82
82
83 def addfile(
83 def addfile(
84 &self,
84 &self,
85 f: PyObject,
85 f: PyObject,
86 oldstate: PyObject,
86 oldstate: PyObject,
87 state: PyObject,
87 state: PyObject,
88 mode: PyObject,
88 mode: PyObject,
89 size: PyObject,
89 size: PyObject,
90 mtime: PyObject
90 mtime: PyObject
91 ) -> PyResult<PyObject> {
91 ) -> PyResult<PyObject> {
92 self.borrow_mut(py)?.add_file(
92 self.borrow_mut(py)?.add_file(
93 f.extract::<PyBytes>(py)?.data(py),
93 f.extract::<PyBytes>(py)?.data(py),
94 oldstate.extract::<PyBytes>(py)?.data(py)[0]
94 oldstate.extract::<PyBytes>(py)?.data(py)[0]
95 .try_into()
95 .try_into()
96 .map_err(|e: DirstateParseError| {
96 .map_err(|e: DirstateParseError| {
97 PyErr::new::<exc::ValueError, _>(py, e.to_string())
97 PyErr::new::<exc::ValueError, _>(py, e.to_string())
98 })?,
98 })?,
99 DirstateEntry {
99 DirstateEntry {
100 state: state.extract::<PyBytes>(py)?.data(py)[0]
100 state: state.extract::<PyBytes>(py)?.data(py)[0]
101 .try_into()
101 .try_into()
102 .map_err(|e: DirstateParseError| {
102 .map_err(|e: DirstateParseError| {
103 PyErr::new::<exc::ValueError, _>(py, e.to_string())
103 PyErr::new::<exc::ValueError, _>(py, e.to_string())
104 })?,
104 })?,
105 mode: mode.extract(py)?,
105 mode: mode.extract(py)?,
106 size: size.extract(py)?,
106 size: size.extract(py)?,
107 mtime: mtime.extract(py)?,
107 mtime: mtime.extract(py)?,
108 },
108 },
109 );
109 );
110 Ok(py.None())
110 Ok(py.None())
111 }
111 }
112
112
113 def removefile(
113 def removefile(
114 &self,
114 &self,
115 f: PyObject,
115 f: PyObject,
116 oldstate: PyObject,
116 oldstate: PyObject,
117 size: PyObject
117 size: PyObject
118 ) -> PyResult<PyObject> {
118 ) -> PyResult<PyObject> {
119 self.borrow_mut(py)?
119 self.borrow_mut(py)?
120 .remove_file(
120 .remove_file(
121 f.extract::<PyBytes>(py)?.data(py),
121 f.extract::<PyBytes>(py)?.data(py),
122 oldstate.extract::<PyBytes>(py)?.data(py)[0]
122 oldstate.extract::<PyBytes>(py)?.data(py)[0]
123 .try_into()
123 .try_into()
124 .map_err(|e: DirstateParseError| {
124 .map_err(|e: DirstateParseError| {
125 PyErr::new::<exc::ValueError, _>(py, e.to_string())
125 PyErr::new::<exc::ValueError, _>(py, e.to_string())
126 })?,
126 })?,
127 size.extract(py)?,
127 size.extract(py)?,
128 )
128 )
129 .or_else(|_| {
129 .or_else(|_| {
130 Err(PyErr::new::<exc::OSError, _>(
130 Err(PyErr::new::<exc::OSError, _>(
131 py,
131 py,
132 "Dirstate error".to_string(),
132 "Dirstate error".to_string(),
133 ))
133 ))
134 })?;
134 })?;
135 Ok(py.None())
135 Ok(py.None())
136 }
136 }
137
137
138 def dropfile(
138 def dropfile(
139 &self,
139 &self,
140 f: PyObject,
140 f: PyObject,
141 oldstate: PyObject
141 oldstate: PyObject
142 ) -> PyResult<PyBool> {
142 ) -> PyResult<PyBool> {
143 self.borrow_mut(py)?
143 self.borrow_mut(py)?
144 .drop_file(
144 .drop_file(
145 f.extract::<PyBytes>(py)?.data(py),
145 f.extract::<PyBytes>(py)?.data(py),
146 oldstate.extract::<PyBytes>(py)?.data(py)[0]
146 oldstate.extract::<PyBytes>(py)?.data(py)[0]
147 .try_into()
147 .try_into()
148 .map_err(|e: DirstateParseError| {
148 .map_err(|e: DirstateParseError| {
149 PyErr::new::<exc::ValueError, _>(py, e.to_string())
149 PyErr::new::<exc::ValueError, _>(py, e.to_string())
150 })?,
150 })?,
151 )
151 )
152 .and_then(|b| Ok(b.to_py_object(py)))
152 .and_then(|b| Ok(b.to_py_object(py)))
153 .or_else(|_| {
153 .or_else(|_| {
154 Err(PyErr::new::<exc::OSError, _>(
154 Err(PyErr::new::<exc::OSError, _>(
155 py,
155 py,
156 "Dirstate error".to_string(),
156 "Dirstate error".to_string(),
157 ))
157 ))
158 })
158 })
159 }
159 }
160
160
161 def clearambiguoustimes(
161 def clearambiguoustimes(
162 &self,
162 &self,
163 files: PyObject,
163 files: PyObject,
164 now: PyObject
164 now: PyObject
165 ) -> PyResult<PyObject> {
165 ) -> PyResult<PyObject> {
166 let files: PyResult<Vec<Vec<u8>>> = files
166 let files: PyResult<Vec<Vec<u8>>> = files
167 .iter(py)?
167 .iter(py)?
168 .map(|filename| {
168 .map(|filename| {
169 Ok(filename?.extract::<PyBytes>(py)?.data(py).to_owned())
169 Ok(filename?.extract::<PyBytes>(py)?.data(py).to_owned())
170 })
170 })
171 .collect();
171 .collect();
172 self.inner(py)
172 self.inner(py)
173 .borrow_mut()
173 .borrow_mut()
174 .clear_ambiguous_times(files?, now.extract(py)?);
174 .clear_ambiguous_times(files?, now.extract(py)?);
175 Ok(py.None())
175 Ok(py.None())
176 }
176 }
177
177
178 // TODO share the reference
178 // TODO share the reference
179 def nonnormalentries(&self) -> PyResult<PyObject> {
179 def nonnormalentries(&self) -> PyResult<PyObject> {
180 let (non_normal, other_parent) =
180 let (non_normal, other_parent) =
181 self.inner(py).borrow().non_normal_other_parent_entries();
181 self.inner(py).borrow().non_normal_other_parent_entries();
182
182
183 let locals = PyDict::new(py);
183 let locals = PyDict::new(py);
184 locals.set_item(
184 locals.set_item(
185 py,
185 py,
186 "non_normal",
186 "non_normal",
187 non_normal
187 non_normal
188 .iter()
188 .iter()
189 .map(|v| PyBytes::new(py, &v))
189 .map(|v| PyBytes::new(py, &v))
190 .collect::<Vec<PyBytes>>()
190 .collect::<Vec<PyBytes>>()
191 .to_py_object(py),
191 .to_py_object(py),
192 )?;
192 )?;
193 locals.set_item(
193 locals.set_item(
194 py,
194 py,
195 "other_parent",
195 "other_parent",
196 other_parent
196 other_parent
197 .iter()
197 .iter()
198 .map(|v| PyBytes::new(py, &v))
198 .map(|v| PyBytes::new(py, &v))
199 .collect::<Vec<PyBytes>>()
199 .collect::<Vec<PyBytes>>()
200 .to_py_object(py),
200 .to_py_object(py),
201 )?;
201 )?;
202
202
203 py.eval("set(non_normal), set(other_parent)", None, Some(&locals))
203 py.eval("set(non_normal), set(other_parent)", None, Some(&locals))
204 }
204 }
205
205
206 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> {
206 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> {
207 let d = d.extract::<PyBytes>(py)?;
207 let d = d.extract::<PyBytes>(py)?;
208 Ok(self
208 Ok(self
209 .inner(py)
209 .inner(py)
210 .borrow_mut()
210 .borrow_mut()
211 .has_tracked_dir(d.data(py))
211 .has_tracked_dir(d.data(py))
212 .to_py_object(py))
212 .to_py_object(py))
213 }
213 }
214
214
215 def hasdir(&self, d: PyObject) -> PyResult<PyBool> {
215 def hasdir(&self, d: PyObject) -> PyResult<PyBool> {
216 let d = d.extract::<PyBytes>(py)?;
216 let d = d.extract::<PyBytes>(py)?;
217 Ok(self
217 Ok(self
218 .inner(py)
218 .inner(py)
219 .borrow_mut()
219 .borrow_mut()
220 .has_dir(d.data(py))
220 .has_dir(d.data(py))
221 .to_py_object(py))
221 .to_py_object(py))
222 }
222 }
223
223
224 def parents(&self, st: PyObject) -> PyResult<PyTuple> {
224 def parents(&self, st: PyObject) -> PyResult<PyTuple> {
225 self.inner(py)
225 self.inner(py)
226 .borrow_mut()
226 .borrow_mut()
227 .parents(st.extract::<PyBytes>(py)?.data(py))
227 .parents(st.extract::<PyBytes>(py)?.data(py))
228 .and_then(|d| {
228 .and_then(|d| {
229 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2))
229 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2))
230 .to_py_object(py))
230 .to_py_object(py))
231 })
231 })
232 .or_else(|_| {
232 .or_else(|_| {
233 Err(PyErr::new::<exc::OSError, _>(
233 Err(PyErr::new::<exc::OSError, _>(
234 py,
234 py,
235 "Dirstate error".to_string(),
235 "Dirstate error".to_string(),
236 ))
236 ))
237 })
237 })
238 }
238 }
239
239
240 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
240 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
241 let p1 = extract_node_id(py, &p1)?;
241 let p1 = extract_node_id(py, &p1)?;
242 let p2 = extract_node_id(py, &p2)?;
242 let p2 = extract_node_id(py, &p2)?;
243
243
244 self.inner(py)
244 self.inner(py)
245 .borrow_mut()
245 .borrow_mut()
246 .set_parents(DirstateParents { p1, p2 });
246 .set_parents(&DirstateParents { p1, p2 });
247 Ok(py.None())
247 Ok(py.None())
248 }
248 }
249
249
250 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> {
250 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> {
251 match self
251 match self
252 .inner(py)
252 .inner(py)
253 .borrow_mut()
253 .borrow_mut()
254 .read(st.extract::<PyBytes>(py)?.data(py))
254 .read(st.extract::<PyBytes>(py)?.data(py))
255 {
255 {
256 Ok(Some(parents)) => Ok(Some(
256 Ok(Some(parents)) => Ok(Some(
257 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2))
257 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2))
258 .to_py_object(py)
258 .to_py_object(py)
259 .into_object(),
259 .into_object(),
260 )),
260 )),
261 Ok(None) => Ok(Some(py.None())),
261 Ok(None) => Ok(Some(py.None())),
262 Err(_) => Err(PyErr::new::<exc::OSError, _>(
262 Err(_) => Err(PyErr::new::<exc::OSError, _>(
263 py,
263 py,
264 "Dirstate error".to_string(),
264 "Dirstate error".to_string(),
265 )),
265 )),
266 }
266 }
267 }
267 }
268 def write(
268 def write(
269 &self,
269 &self,
270 p1: PyObject,
270 p1: PyObject,
271 p2: PyObject,
271 p2: PyObject,
272 now: PyObject
272 now: PyObject
273 ) -> PyResult<PyBytes> {
273 ) -> PyResult<PyBytes> {
274 let now = Duration::new(now.extract(py)?, 0);
274 let now = Duration::new(now.extract(py)?, 0);
275 let parents = DirstateParents {
275 let parents = DirstateParents {
276 p1: extract_node_id(py, &p1)?,
276 p1: extract_node_id(py, &p1)?,
277 p2: extract_node_id(py, &p2)?,
277 p2: extract_node_id(py, &p2)?,
278 };
278 };
279
279
280 match self.borrow_mut(py)?.pack(parents, now) {
280 match self.borrow_mut(py)?.pack(parents, now) {
281 Ok(packed) => Ok(PyBytes::new(py, &packed)),
281 Ok(packed) => Ok(PyBytes::new(py, &packed)),
282 Err(_) => Err(PyErr::new::<exc::OSError, _>(
282 Err(_) => Err(PyErr::new::<exc::OSError, _>(
283 py,
283 py,
284 "Dirstate error".to_string(),
284 "Dirstate error".to_string(),
285 )),
285 )),
286 }
286 }
287 }
287 }
288
288
289 def filefoldmapasdict(&self) -> PyResult<PyDict> {
289 def filefoldmapasdict(&self) -> PyResult<PyDict> {
290 let dict = PyDict::new(py);
290 let dict = PyDict::new(py);
291 for (key, value) in
291 for (key, value) in
292 self.borrow_mut(py)?.build_file_fold_map().iter()
292 self.borrow_mut(py)?.build_file_fold_map().iter()
293 {
293 {
294 dict.set_item(py, key, value)?;
294 dict.set_item(py, key, value)?;
295 }
295 }
296 Ok(dict)
296 Ok(dict)
297 }
297 }
298
298
299 def __len__(&self) -> PyResult<usize> {
299 def __len__(&self) -> PyResult<usize> {
300 Ok(self.inner(py).borrow().len())
300 Ok(self.inner(py).borrow().len())
301 }
301 }
302
302
303 def __contains__(&self, key: PyObject) -> PyResult<bool> {
303 def __contains__(&self, key: PyObject) -> PyResult<bool> {
304 let key = key.extract::<PyBytes>(py)?;
304 let key = key.extract::<PyBytes>(py)?;
305 Ok(self.inner(py).borrow().contains_key(key.data(py)))
305 Ok(self.inner(py).borrow().contains_key(key.data(py)))
306 }
306 }
307
307
308 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> {
308 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> {
309 let key = key.extract::<PyBytes>(py)?;
309 let key = key.extract::<PyBytes>(py)?;
310 let key = key.data(py);
310 let key = key.data(py);
311 match self.inner(py).borrow().get(key) {
311 match self.inner(py).borrow().get(key) {
312 Some(entry) => {
312 Some(entry) => {
313 // Explicitly go through u8 first, then cast to
313 // Explicitly go through u8 first, then cast to
314 // platform-specific `c_char`.
314 // platform-specific `c_char`.
315 let state: u8 = entry.state.into();
315 let state: u8 = entry.state.into();
316 Ok(decapsule_make_dirstate_tuple(py)?(
316 Ok(decapsule_make_dirstate_tuple(py)?(
317 state as c_char,
317 state as c_char,
318 entry.mode,
318 entry.mode,
319 entry.size,
319 entry.size,
320 entry.mtime,
320 entry.mtime,
321 ))
321 ))
322 },
322 },
323 None => Err(PyErr::new::<exc::KeyError, _>(
323 None => Err(PyErr::new::<exc::KeyError, _>(
324 py,
324 py,
325 String::from_utf8_lossy(key),
325 String::from_utf8_lossy(key),
326 )),
326 )),
327 }
327 }
328 }
328 }
329
329
330 def keys(&self) -> PyResult<DirstateMapKeysIterator> {
330 def keys(&self) -> PyResult<DirstateMapKeysIterator> {
331 DirstateMapKeysIterator::from_inner(
331 DirstateMapKeysIterator::from_inner(
332 py,
332 py,
333 Some(DirstateMapLeakedRef::new(py, &self)),
333 Some(DirstateMapLeakedRef::new(py, &self)),
334 Box::new(self.leak_immutable(py)?.iter()),
334 Box::new(self.leak_immutable(py)?.iter()),
335 )
335 )
336 }
336 }
337
337
338 def items(&self) -> PyResult<DirstateMapItemsIterator> {
338 def items(&self) -> PyResult<DirstateMapItemsIterator> {
339 DirstateMapItemsIterator::from_inner(
339 DirstateMapItemsIterator::from_inner(
340 py,
340 py,
341 Some(DirstateMapLeakedRef::new(py, &self)),
341 Some(DirstateMapLeakedRef::new(py, &self)),
342 Box::new(self.leak_immutable(py)?.iter()),
342 Box::new(self.leak_immutable(py)?.iter()),
343 )
343 )
344 }
344 }
345
345
346 def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
346 def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
347 DirstateMapKeysIterator::from_inner(
347 DirstateMapKeysIterator::from_inner(
348 py,
348 py,
349 Some(DirstateMapLeakedRef::new(py, &self)),
349 Some(DirstateMapLeakedRef::new(py, &self)),
350 Box::new(self.leak_immutable(py)?.iter()),
350 Box::new(self.leak_immutable(py)?.iter()),
351 )
351 )
352 }
352 }
353
353
354 def getdirs(&self) -> PyResult<Dirs> {
354 def getdirs(&self) -> PyResult<Dirs> {
355 // TODO don't copy, share the reference
355 // TODO don't copy, share the reference
356 self.inner(py).borrow_mut().set_dirs();
356 self.inner(py).borrow_mut().set_dirs();
357 Dirs::from_inner(
357 Dirs::from_inner(
358 py,
358 py,
359 DirsMultiset::new(
359 DirsMultiset::new(
360 DirsIterable::Dirstate(&self.inner(py).borrow()),
360 DirsIterable::Dirstate(&self.inner(py).borrow()),
361 Some(EntryState::Removed),
361 Some(EntryState::Removed),
362 ),
362 ),
363 )
363 )
364 }
364 }
365 def getalldirs(&self) -> PyResult<Dirs> {
365 def getalldirs(&self) -> PyResult<Dirs> {
366 // TODO don't copy, share the reference
366 // TODO don't copy, share the reference
367 self.inner(py).borrow_mut().set_all_dirs();
367 self.inner(py).borrow_mut().set_all_dirs();
368 Dirs::from_inner(
368 Dirs::from_inner(
369 py,
369 py,
370 DirsMultiset::new(
370 DirsMultiset::new(
371 DirsIterable::Dirstate(&self.inner(py).borrow()),
371 DirsIterable::Dirstate(&self.inner(py).borrow()),
372 None,
372 None,
373 ),
373 ),
374 )
374 )
375 }
375 }
376
376
377 // TODO all copymap* methods, see docstring above
377 // TODO all copymap* methods, see docstring above
378 def copymapcopy(&self) -> PyResult<PyDict> {
378 def copymapcopy(&self) -> PyResult<PyDict> {
379 let dict = PyDict::new(py);
379 let dict = PyDict::new(py);
380 for (key, value) in self.inner(py).borrow().copy_map.iter() {
380 for (key, value) in self.inner(py).borrow().copy_map.iter() {
381 dict.set_item(py, PyBytes::new(py, key), PyBytes::new(py, value))?;
381 dict.set_item(py, PyBytes::new(py, key), PyBytes::new(py, value))?;
382 }
382 }
383 Ok(dict)
383 Ok(dict)
384 }
384 }
385
385
386 def copymapgetitem(&self, key: PyObject) -> PyResult<PyBytes> {
386 def copymapgetitem(&self, key: PyObject) -> PyResult<PyBytes> {
387 let key = key.extract::<PyBytes>(py)?;
387 let key = key.extract::<PyBytes>(py)?;
388 match self.inner(py).borrow().copy_map.get(key.data(py)) {
388 match self.inner(py).borrow().copy_map.get(key.data(py)) {
389 Some(copy) => Ok(PyBytes::new(py, copy)),
389 Some(copy) => Ok(PyBytes::new(py, copy)),
390 None => Err(PyErr::new::<exc::KeyError, _>(
390 None => Err(PyErr::new::<exc::KeyError, _>(
391 py,
391 py,
392 String::from_utf8_lossy(key.data(py)),
392 String::from_utf8_lossy(key.data(py)),
393 )),
393 )),
394 }
394 }
395 }
395 }
396 def copymap(&self) -> PyResult<CopyMap> {
396 def copymap(&self) -> PyResult<CopyMap> {
397 CopyMap::from_inner(py, self.clone_ref(py))
397 CopyMap::from_inner(py, self.clone_ref(py))
398 }
398 }
399
399
400 def copymaplen(&self) -> PyResult<usize> {
400 def copymaplen(&self) -> PyResult<usize> {
401 Ok(self.inner(py).borrow().copy_map.len())
401 Ok(self.inner(py).borrow().copy_map.len())
402 }
402 }
403 def copymapcontains(&self, key: PyObject) -> PyResult<bool> {
403 def copymapcontains(&self, key: PyObject) -> PyResult<bool> {
404 let key = key.extract::<PyBytes>(py)?;
404 let key = key.extract::<PyBytes>(py)?;
405 Ok(self.inner(py).borrow().copy_map.contains_key(key.data(py)))
405 Ok(self.inner(py).borrow().copy_map.contains_key(key.data(py)))
406 }
406 }
407 def copymapget(
407 def copymapget(
408 &self,
408 &self,
409 key: PyObject,
409 key: PyObject,
410 default: Option<PyObject>
410 default: Option<PyObject>
411 ) -> PyResult<Option<PyObject>> {
411 ) -> PyResult<Option<PyObject>> {
412 let key = key.extract::<PyBytes>(py)?;
412 let key = key.extract::<PyBytes>(py)?;
413 match self.inner(py).borrow().copy_map.get(key.data(py)) {
413 match self.inner(py).borrow().copy_map.get(key.data(py)) {
414 Some(copy) => Ok(Some(PyBytes::new(py, copy).into_object())),
414 Some(copy) => Ok(Some(PyBytes::new(py, copy).into_object())),
415 None => Ok(default),
415 None => Ok(default),
416 }
416 }
417 }
417 }
418 def copymapsetitem(
418 def copymapsetitem(
419 &self,
419 &self,
420 key: PyObject,
420 key: PyObject,
421 value: PyObject
421 value: PyObject
422 ) -> PyResult<PyObject> {
422 ) -> PyResult<PyObject> {
423 let key = key.extract::<PyBytes>(py)?;
423 let key = key.extract::<PyBytes>(py)?;
424 let value = value.extract::<PyBytes>(py)?;
424 let value = value.extract::<PyBytes>(py)?;
425 self.inner(py)
425 self.inner(py)
426 .borrow_mut()
426 .borrow_mut()
427 .copy_map
427 .copy_map
428 .insert(key.data(py).to_vec(), value.data(py).to_vec());
428 .insert(key.data(py).to_vec(), value.data(py).to_vec());
429 Ok(py.None())
429 Ok(py.None())
430 }
430 }
431 def copymappop(
431 def copymappop(
432 &self,
432 &self,
433 key: PyObject,
433 key: PyObject,
434 default: Option<PyObject>
434 default: Option<PyObject>
435 ) -> PyResult<Option<PyObject>> {
435 ) -> PyResult<Option<PyObject>> {
436 let key = key.extract::<PyBytes>(py)?;
436 let key = key.extract::<PyBytes>(py)?;
437 match self.inner(py).borrow_mut().copy_map.remove(key.data(py)) {
437 match self.inner(py).borrow_mut().copy_map.remove(key.data(py)) {
438 Some(_) => Ok(None),
438 Some(_) => Ok(None),
439 None => Ok(default),
439 None => Ok(default),
440 }
440 }
441 }
441 }
442
442
443 def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
443 def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
444 CopyMapKeysIterator::from_inner(
444 CopyMapKeysIterator::from_inner(
445 py,
445 py,
446 Some(DirstateMapLeakedRef::new(py, &self)),
446 Some(DirstateMapLeakedRef::new(py, &self)),
447 Box::new(self.leak_immutable(py)?.copy_map.iter()),
447 Box::new(self.leak_immutable(py)?.copy_map.iter()),
448 )
448 )
449 }
449 }
450
450
451 def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
451 def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
452 CopyMapItemsIterator::from_inner(
452 CopyMapItemsIterator::from_inner(
453 py,
453 py,
454 Some(DirstateMapLeakedRef::new(py, &self)),
454 Some(DirstateMapLeakedRef::new(py, &self)),
455 Box::new(self.leak_immutable(py)?.copy_map.iter()),
455 Box::new(self.leak_immutable(py)?.copy_map.iter()),
456 )
456 )
457 }
457 }
458
458
459 });
459 });
460
460
461 impl DirstateMap {
461 impl DirstateMap {
462 fn translate_key(
462 fn translate_key(
463 py: Python,
463 py: Python,
464 res: (&Vec<u8>, &DirstateEntry),
464 res: (&Vec<u8>, &DirstateEntry),
465 ) -> PyResult<Option<PyBytes>> {
465 ) -> PyResult<Option<PyBytes>> {
466 Ok(Some(PyBytes::new(py, res.0)))
466 Ok(Some(PyBytes::new(py, res.0)))
467 }
467 }
468 fn translate_key_value(
468 fn translate_key_value(
469 py: Python,
469 py: Python,
470 res: (&Vec<u8>, &DirstateEntry),
470 res: (&Vec<u8>, &DirstateEntry),
471 ) -> PyResult<Option<(PyBytes, PyObject)>> {
471 ) -> PyResult<Option<(PyBytes, PyObject)>> {
472 let (f, entry) = res;
472 let (f, entry) = res;
473
473
474 // Explicitly go through u8 first, then cast to
474 // Explicitly go through u8 first, then cast to
475 // platform-specific `c_char`.
475 // platform-specific `c_char`.
476 let state: u8 = entry.state.into();
476 let state: u8 = entry.state.into();
477 Ok(Some((
477 Ok(Some((
478 PyBytes::new(py, f),
478 PyBytes::new(py, f),
479 decapsule_make_dirstate_tuple(py)?(
479 decapsule_make_dirstate_tuple(py)?(
480 state as c_char,
480 state as c_char,
481 entry.mode,
481 entry.mode,
482 entry.size,
482 entry.size,
483 entry.mtime,
483 entry.mtime,
484 ),
484 ),
485 )))
485 )))
486 }
486 }
487 }
487 }
488
488
489 py_shared_ref!(DirstateMap, RustDirstateMap, inner, DirstateMapLeakedRef,);
489 py_shared_ref!(DirstateMap, RustDirstateMap, inner, DirstateMapLeakedRef,);
490
490
491 py_shared_mapping_iterator!(
491 py_shared_mapping_iterator!(
492 DirstateMapKeysIterator,
492 DirstateMapKeysIterator,
493 DirstateMapLeakedRef,
493 DirstateMapLeakedRef,
494 Vec<u8>,
494 Vec<u8>,
495 DirstateEntry,
495 DirstateEntry,
496 DirstateMap::translate_key,
496 DirstateMap::translate_key,
497 Option<PyBytes>
497 Option<PyBytes>
498 );
498 );
499
499
500 py_shared_mapping_iterator!(
500 py_shared_mapping_iterator!(
501 DirstateMapItemsIterator,
501 DirstateMapItemsIterator,
502 DirstateMapLeakedRef,
502 DirstateMapLeakedRef,
503 Vec<u8>,
503 Vec<u8>,
504 DirstateEntry,
504 DirstateEntry,
505 DirstateMap::translate_key_value,
505 DirstateMap::translate_key_value,
506 Option<(PyBytes, PyObject)>
506 Option<(PyBytes, PyObject)>
507 );
507 );
508
508
509 fn extract_node_id(py: Python, obj: &PyObject) -> PyResult<[u8; PARENT_SIZE]> {
509 fn extract_node_id(py: Python, obj: &PyObject) -> PyResult<[u8; PARENT_SIZE]> {
510 let bytes = obj.extract::<PyBytes>(py)?;
510 let bytes = obj.extract::<PyBytes>(py)?;
511 match bytes.data(py).try_into() {
511 match bytes.data(py).try_into() {
512 Ok(s) => Ok(s),
512 Ok(s) => Ok(s),
513 Err(e) => Err(PyErr::new::<exc::ValueError, _>(py, e.to_string())),
513 Err(e) => Err(PyErr::new::<exc::ValueError, _>(py, e.to_string())),
514 }
514 }
515 }
515 }
General Comments 0
You need to be logged in to leave comments. Login now