Show More
@@ -1,429 +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, |
|
10 | pack_dirstate, parse_dirstate, | |
11 | utils::copy_into_array, |
|
11 | utils::copy_into_array, | |
12 | CopyMap, DirsIterable, DirsMultiset, DirstateEntry, DirstateError, |
|
12 | CopyMap, DirsIterable, DirsMultiset, DirstateEntry, DirstateError, | |
13 | DirstateMapError, DirstateParents, DirstateParseError, StateMap, |
|
13 | DirstateMapError, DirstateParents, DirstateParseError, StateMap, | |
14 | }; |
|
14 | }; | |
15 | use core::borrow::Borrow; |
|
15 | use core::borrow::Borrow; | |
16 | use std::collections::{HashMap, HashSet}; |
|
16 | use std::collections::{HashMap, HashSet}; | |
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_REVISION: [u8; 20] = [ |
|
23 | const NULL_REVISION: [u8; 20] = [0; 20]; | |
24 | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', |
|
|||
25 | b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', |
|
|||
26 | ]; |
|
|||
27 | const MTIME_UNSET: i32 = -1; |
|
24 | const MTIME_UNSET: i32 = -1; | |
28 | const SIZE_DIRTY: i32 = -2; |
|
25 | const SIZE_DIRTY: i32 = -2; | |
29 |
|
26 | |||
30 | #[derive(Default)] |
|
27 | #[derive(Default)] | |
31 | pub struct DirstateMap { |
|
28 | pub struct DirstateMap { | |
32 | state_map: StateMap, |
|
29 | state_map: StateMap, | |
33 | pub copy_map: CopyMap, |
|
30 | pub copy_map: CopyMap, | |
34 | file_fold_map: Option<FileFoldMap>, |
|
31 | file_fold_map: Option<FileFoldMap>, | |
35 | pub dirs: Option<DirsMultiset>, |
|
32 | pub dirs: Option<DirsMultiset>, | |
36 | pub all_dirs: Option<DirsMultiset>, |
|
33 | pub all_dirs: Option<DirsMultiset>, | |
37 | non_normal_set: HashSet<Vec<u8>>, |
|
34 | non_normal_set: HashSet<Vec<u8>>, | |
38 | other_parent_set: HashSet<Vec<u8>>, |
|
35 | other_parent_set: HashSet<Vec<u8>>, | |
39 | parents: Option<DirstateParents>, |
|
36 | parents: Option<DirstateParents>, | |
40 | dirty_parents: bool, |
|
37 | dirty_parents: bool, | |
41 | } |
|
38 | } | |
42 |
|
39 | |||
43 | /// Should only really be used in python interface code, for clarity |
|
40 | /// Should only really be used in python interface code, for clarity | |
44 | impl Deref for DirstateMap { |
|
41 | impl Deref for DirstateMap { | |
45 | type Target = StateMap; |
|
42 | type Target = StateMap; | |
46 |
|
43 | |||
47 | fn deref(&self) -> &Self::Target { |
|
44 | fn deref(&self) -> &Self::Target { | |
48 | &self.state_map |
|
45 | &self.state_map | |
49 | } |
|
46 | } | |
50 | } |
|
47 | } | |
51 |
|
48 | |||
52 | impl FromIterator<(Vec<u8>, DirstateEntry)> for DirstateMap { |
|
49 | impl FromIterator<(Vec<u8>, DirstateEntry)> for DirstateMap { | |
53 | fn from_iter<I: IntoIterator<Item = (Vec<u8>, DirstateEntry)>>( |
|
50 | fn from_iter<I: IntoIterator<Item = (Vec<u8>, DirstateEntry)>>( | |
54 | iter: I, |
|
51 | iter: I, | |
55 | ) -> Self { |
|
52 | ) -> Self { | |
56 | Self { |
|
53 | Self { | |
57 | state_map: iter.into_iter().collect(), |
|
54 | state_map: iter.into_iter().collect(), | |
58 | ..Self::default() |
|
55 | ..Self::default() | |
59 | } |
|
56 | } | |
60 | } |
|
57 | } | |
61 | } |
|
58 | } | |
62 |
|
59 | |||
63 | impl DirstateMap { |
|
60 | impl DirstateMap { | |
64 | pub fn new() -> Self { |
|
61 | pub fn new() -> Self { | |
65 | Self::default() |
|
62 | Self::default() | |
66 | } |
|
63 | } | |
67 |
|
64 | |||
68 | pub fn clear(&mut self) { |
|
65 | pub fn clear(&mut self) { | |
69 | self.state_map.clear(); |
|
66 | self.state_map.clear(); | |
70 | self.copy_map.clear(); |
|
67 | self.copy_map.clear(); | |
71 | self.file_fold_map = None; |
|
68 | self.file_fold_map = None; | |
72 | self.non_normal_set.clear(); |
|
69 | self.non_normal_set.clear(); | |
73 | self.other_parent_set.clear(); |
|
70 | self.other_parent_set.clear(); | |
74 | self.set_parents(DirstateParents { |
|
71 | self.set_parents(DirstateParents { | |
75 | p1: NULL_REVISION, |
|
72 | p1: NULL_REVISION, | |
76 | p2: NULL_REVISION, |
|
73 | p2: NULL_REVISION, | |
77 | }) |
|
74 | }) | |
78 | } |
|
75 | } | |
79 |
|
76 | |||
80 | /// Add a tracked file to the dirstate |
|
77 | /// Add a tracked file to the dirstate | |
81 | pub fn add_file( |
|
78 | pub fn add_file( | |
82 | &mut self, |
|
79 | &mut self, | |
83 | filename: &[u8], |
|
80 | filename: &[u8], | |
84 | old_state: EntryState, |
|
81 | old_state: EntryState, | |
85 | entry: DirstateEntry, |
|
82 | entry: DirstateEntry, | |
86 | ) { |
|
83 | ) { | |
87 | if old_state == EntryState::Unknown || old_state == EntryState::Removed |
|
84 | if old_state == EntryState::Unknown || old_state == EntryState::Removed | |
88 | { |
|
85 | { | |
89 | if let Some(ref mut dirs) = self.dirs { |
|
86 | if let Some(ref mut dirs) = self.dirs { | |
90 | dirs.add_path(filename) |
|
87 | dirs.add_path(filename) | |
91 | } |
|
88 | } | |
92 | } |
|
89 | } | |
93 | if old_state == EntryState::Unknown { |
|
90 | if old_state == EntryState::Unknown { | |
94 | if let Some(ref mut all_dirs) = self.all_dirs { |
|
91 | if let Some(ref mut all_dirs) = self.all_dirs { | |
95 | all_dirs.add_path(filename) |
|
92 | all_dirs.add_path(filename) | |
96 | } |
|
93 | } | |
97 | } |
|
94 | } | |
98 | self.state_map.insert(filename.to_owned(), entry.to_owned()); |
|
95 | self.state_map.insert(filename.to_owned(), entry.to_owned()); | |
99 |
|
96 | |||
100 | if entry.state != EntryState::Normal || entry.mtime == MTIME_UNSET { |
|
97 | if entry.state != EntryState::Normal || entry.mtime == MTIME_UNSET { | |
101 | self.non_normal_set.insert(filename.to_owned()); |
|
98 | self.non_normal_set.insert(filename.to_owned()); | |
102 | } |
|
99 | } | |
103 |
|
100 | |||
104 | if entry.size == SIZE_DIRTY { |
|
101 | if entry.size == SIZE_DIRTY { | |
105 | self.other_parent_set.insert(filename.to_owned()); |
|
102 | self.other_parent_set.insert(filename.to_owned()); | |
106 | } |
|
103 | } | |
107 | } |
|
104 | } | |
108 |
|
105 | |||
109 | /// Mark a file as removed in the dirstate. |
|
106 | /// Mark a file as removed in the dirstate. | |
110 | /// |
|
107 | /// | |
111 | /// The `size` parameter is used to store sentinel values that indicate |
|
108 | /// The `size` parameter is used to store sentinel values that indicate | |
112 | /// 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 | |
113 | /// to be more explicit about what that state is. |
|
110 | /// to be more explicit about what that state is. | |
114 | pub fn remove_file( |
|
111 | pub fn remove_file( | |
115 | &mut self, |
|
112 | &mut self, | |
116 | filename: &[u8], |
|
113 | filename: &[u8], | |
117 | old_state: EntryState, |
|
114 | old_state: EntryState, | |
118 | size: i32, |
|
115 | size: i32, | |
119 | ) -> Result<(), DirstateMapError> { |
|
116 | ) -> Result<(), DirstateMapError> { | |
120 | if old_state != EntryState::Unknown && old_state != EntryState::Removed |
|
117 | if old_state != EntryState::Unknown && old_state != EntryState::Removed | |
121 | { |
|
118 | { | |
122 | if let Some(ref mut dirs) = self.dirs { |
|
119 | if let Some(ref mut dirs) = self.dirs { | |
123 | dirs.delete_path(filename)?; |
|
120 | dirs.delete_path(filename)?; | |
124 | } |
|
121 | } | |
125 | } |
|
122 | } | |
126 | if old_state == EntryState::Unknown { |
|
123 | if old_state == EntryState::Unknown { | |
127 | if let Some(ref mut all_dirs) = self.all_dirs { |
|
124 | if let Some(ref mut all_dirs) = self.all_dirs { | |
128 | all_dirs.add_path(filename); |
|
125 | all_dirs.add_path(filename); | |
129 | } |
|
126 | } | |
130 | } |
|
127 | } | |
131 |
|
128 | |||
132 | 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 { | |
133 | file_fold_map.remove(&filename.to_ascii_uppercase()); |
|
130 | file_fold_map.remove(&filename.to_ascii_uppercase()); | |
134 | } |
|
131 | } | |
135 | self.state_map.insert( |
|
132 | self.state_map.insert( | |
136 | filename.to_owned(), |
|
133 | filename.to_owned(), | |
137 | DirstateEntry { |
|
134 | DirstateEntry { | |
138 | state: EntryState::Removed, |
|
135 | state: EntryState::Removed, | |
139 | mode: 0, |
|
136 | mode: 0, | |
140 | size, |
|
137 | size, | |
141 | mtime: 0, |
|
138 | mtime: 0, | |
142 | }, |
|
139 | }, | |
143 | ); |
|
140 | ); | |
144 | self.non_normal_set.insert(filename.to_owned()); |
|
141 | self.non_normal_set.insert(filename.to_owned()); | |
145 | Ok(()) |
|
142 | Ok(()) | |
146 | } |
|
143 | } | |
147 |
|
144 | |||
148 | /// Remove a file from the dirstate. |
|
145 | /// Remove a file from the dirstate. | |
149 | /// Returns `true` if the file was previously recorded. |
|
146 | /// Returns `true` if the file was previously recorded. | |
150 | pub fn drop_file( |
|
147 | pub fn drop_file( | |
151 | &mut self, |
|
148 | &mut self, | |
152 | filename: &[u8], |
|
149 | filename: &[u8], | |
153 | old_state: EntryState, |
|
150 | old_state: EntryState, | |
154 | ) -> Result<bool, DirstateMapError> { |
|
151 | ) -> Result<bool, DirstateMapError> { | |
155 | let exists = self.state_map.remove(filename).is_some(); |
|
152 | let exists = self.state_map.remove(filename).is_some(); | |
156 |
|
153 | |||
157 | if exists { |
|
154 | if exists { | |
158 | if old_state != EntryState::Removed { |
|
155 | if old_state != EntryState::Removed { | |
159 | if let Some(ref mut dirs) = self.dirs { |
|
156 | if let Some(ref mut dirs) = self.dirs { | |
160 | dirs.delete_path(filename)?; |
|
157 | dirs.delete_path(filename)?; | |
161 | } |
|
158 | } | |
162 | } |
|
159 | } | |
163 | if let Some(ref mut all_dirs) = self.all_dirs { |
|
160 | if let Some(ref mut all_dirs) = self.all_dirs { | |
164 | all_dirs.delete_path(filename)?; |
|
161 | all_dirs.delete_path(filename)?; | |
165 | } |
|
162 | } | |
166 | } |
|
163 | } | |
167 | 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 { | |
168 | file_fold_map.remove(&filename.to_ascii_uppercase()); |
|
165 | file_fold_map.remove(&filename.to_ascii_uppercase()); | |
169 | } |
|
166 | } | |
170 | self.non_normal_set.remove(filename); |
|
167 | self.non_normal_set.remove(filename); | |
171 |
|
168 | |||
172 | Ok(exists) |
|
169 | Ok(exists) | |
173 | } |
|
170 | } | |
174 |
|
171 | |||
175 | pub fn clear_ambiguous_times( |
|
172 | pub fn clear_ambiguous_times( | |
176 | &mut self, |
|
173 | &mut self, | |
177 | filenames: Vec<Vec<u8>>, |
|
174 | filenames: Vec<Vec<u8>>, | |
178 | now: i32, |
|
175 | now: i32, | |
179 | ) { |
|
176 | ) { | |
180 | for filename in filenames { |
|
177 | for filename in filenames { | |
181 | let mut changed = false; |
|
178 | let mut changed = false; | |
182 | self.state_map |
|
179 | self.state_map | |
183 | .entry(filename.to_owned()) |
|
180 | .entry(filename.to_owned()) | |
184 | .and_modify(|entry| { |
|
181 | .and_modify(|entry| { | |
185 | if entry.state == EntryState::Normal && entry.mtime == now |
|
182 | if entry.state == EntryState::Normal && entry.mtime == now | |
186 | { |
|
183 | { | |
187 | changed = true; |
|
184 | changed = true; | |
188 | *entry = DirstateEntry { |
|
185 | *entry = DirstateEntry { | |
189 | mtime: MTIME_UNSET, |
|
186 | mtime: MTIME_UNSET, | |
190 | ..*entry |
|
187 | ..*entry | |
191 | }; |
|
188 | }; | |
192 | } |
|
189 | } | |
193 | }); |
|
190 | }); | |
194 | if changed { |
|
191 | if changed { | |
195 | self.non_normal_set.insert(filename.to_owned()); |
|
192 | self.non_normal_set.insert(filename.to_owned()); | |
196 | } |
|
193 | } | |
197 | } |
|
194 | } | |
198 | } |
|
195 | } | |
199 |
|
196 | |||
200 | pub fn non_normal_other_parent_entries( |
|
197 | pub fn non_normal_other_parent_entries( | |
201 | &self, |
|
198 | &self, | |
202 | ) -> (HashSet<Vec<u8>>, HashSet<Vec<u8>>) { |
|
199 | ) -> (HashSet<Vec<u8>>, HashSet<Vec<u8>>) { | |
203 | let mut non_normal = HashSet::new(); |
|
200 | let mut non_normal = HashSet::new(); | |
204 | let mut other_parent = HashSet::new(); |
|
201 | let mut other_parent = HashSet::new(); | |
205 |
|
202 | |||
206 | for ( |
|
203 | for ( | |
207 | filename, |
|
204 | filename, | |
208 | DirstateEntry { |
|
205 | DirstateEntry { | |
209 | state, size, mtime, .. |
|
206 | state, size, mtime, .. | |
210 | }, |
|
207 | }, | |
211 | ) in self.state_map.iter() |
|
208 | ) in self.state_map.iter() | |
212 | { |
|
209 | { | |
213 | if *state != EntryState::Normal || *mtime == MTIME_UNSET { |
|
210 | if *state != EntryState::Normal || *mtime == MTIME_UNSET { | |
214 | non_normal.insert(filename.to_owned()); |
|
211 | non_normal.insert(filename.to_owned()); | |
215 | } |
|
212 | } | |
216 | if *state == EntryState::Normal && *size == SIZE_DIRTY { |
|
213 | if *state == EntryState::Normal && *size == SIZE_DIRTY { | |
217 | other_parent.insert(filename.to_owned()); |
|
214 | other_parent.insert(filename.to_owned()); | |
218 | } |
|
215 | } | |
219 | } |
|
216 | } | |
220 |
|
217 | |||
221 | (non_normal, other_parent) |
|
218 | (non_normal, other_parent) | |
222 | } |
|
219 | } | |
223 |
|
220 | |||
224 | /// 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 | |
225 | /// emulate a Python lazy property, but it is ugly and unidiomatic. |
|
222 | /// emulate a Python lazy property, but it is ugly and unidiomatic. | |
226 | /// 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 | |
227 | /// good idea. |
|
224 | /// good idea. | |
228 | pub fn set_all_dirs(&mut self) { |
|
225 | pub fn set_all_dirs(&mut self) { | |
229 | if self.all_dirs.is_none() { |
|
226 | if self.all_dirs.is_none() { | |
230 | self.all_dirs = Some(DirsMultiset::new( |
|
227 | self.all_dirs = Some(DirsMultiset::new( | |
231 | DirsIterable::Dirstate(&self.state_map), |
|
228 | DirsIterable::Dirstate(&self.state_map), | |
232 | None, |
|
229 | None, | |
233 | )); |
|
230 | )); | |
234 | } |
|
231 | } | |
235 | } |
|
232 | } | |
236 |
|
233 | |||
237 | pub fn set_dirs(&mut self) { |
|
234 | pub fn set_dirs(&mut self) { | |
238 | if self.dirs.is_none() { |
|
235 | if self.dirs.is_none() { | |
239 | self.dirs = Some(DirsMultiset::new( |
|
236 | self.dirs = Some(DirsMultiset::new( | |
240 | DirsIterable::Dirstate(&self.state_map), |
|
237 | DirsIterable::Dirstate(&self.state_map), | |
241 | Some(EntryState::Removed), |
|
238 | Some(EntryState::Removed), | |
242 | )); |
|
239 | )); | |
243 | } |
|
240 | } | |
244 | } |
|
241 | } | |
245 |
|
242 | |||
246 | pub fn has_tracked_dir(&mut self, directory: &[u8]) -> bool { |
|
243 | pub fn has_tracked_dir(&mut self, directory: &[u8]) -> bool { | |
247 | self.set_dirs(); |
|
244 | self.set_dirs(); | |
248 | self.dirs.as_ref().unwrap().contains(directory) |
|
245 | self.dirs.as_ref().unwrap().contains(directory) | |
249 | } |
|
246 | } | |
250 |
|
247 | |||
251 | pub fn has_dir(&mut self, directory: &[u8]) -> bool { |
|
248 | pub fn has_dir(&mut self, directory: &[u8]) -> bool { | |
252 | self.set_all_dirs(); |
|
249 | self.set_all_dirs(); | |
253 | self.all_dirs.as_ref().unwrap().contains(directory) |
|
250 | self.all_dirs.as_ref().unwrap().contains(directory) | |
254 | } |
|
251 | } | |
255 |
|
252 | |||
256 | pub fn parents( |
|
253 | pub fn parents( | |
257 | &mut self, |
|
254 | &mut self, | |
258 | file_contents: &[u8], |
|
255 | file_contents: &[u8], | |
259 | ) -> Result<DirstateParents, DirstateError> { |
|
256 | ) -> Result<DirstateParents, DirstateError> { | |
260 | if let Some(ref parents) = self.parents { |
|
257 | if let Some(ref parents) = self.parents { | |
261 | return Ok(parents.clone()); |
|
258 | return Ok(parents.clone()); | |
262 | } |
|
259 | } | |
263 | let parents; |
|
260 | let parents; | |
264 | if file_contents.len() == 40 { |
|
261 | if file_contents.len() == 40 { | |
265 | parents = DirstateParents { |
|
262 | parents = DirstateParents { | |
266 | p1: copy_into_array(&file_contents[..PARENT_SIZE]), |
|
263 | p1: copy_into_array(&file_contents[..PARENT_SIZE]), | |
267 | p2: copy_into_array( |
|
264 | p2: copy_into_array( | |
268 | &file_contents[PARENT_SIZE..PARENT_SIZE * 2], |
|
265 | &file_contents[PARENT_SIZE..PARENT_SIZE * 2], | |
269 | ), |
|
266 | ), | |
270 | }; |
|
267 | }; | |
271 | } else if file_contents.is_empty() { |
|
268 | } else if file_contents.is_empty() { | |
272 | parents = DirstateParents { |
|
269 | parents = DirstateParents { | |
273 | p1: NULL_REVISION, |
|
270 | p1: NULL_REVISION, | |
274 | p2: NULL_REVISION, |
|
271 | p2: NULL_REVISION, | |
275 | }; |
|
272 | }; | |
276 | } else { |
|
273 | } else { | |
277 | return Err(DirstateError::Parse(DirstateParseError::Damaged)); |
|
274 | return Err(DirstateError::Parse(DirstateParseError::Damaged)); | |
278 | } |
|
275 | } | |
279 |
|
276 | |||
280 | self.parents = Some(parents.to_owned()); |
|
277 | self.parents = Some(parents.to_owned()); | |
281 | Ok(parents.clone()) |
|
278 | Ok(parents.clone()) | |
282 | } |
|
279 | } | |
283 |
|
280 | |||
284 | pub fn set_parents(&mut self, parents: DirstateParents) { |
|
281 | pub fn set_parents(&mut self, parents: DirstateParents) { | |
285 | self.parents = Some(parents.clone()); |
|
282 | self.parents = Some(parents.clone()); | |
286 | self.dirty_parents = true; |
|
283 | self.dirty_parents = true; | |
287 | } |
|
284 | } | |
288 |
|
285 | |||
289 | pub fn read( |
|
286 | pub fn read( | |
290 | &mut self, |
|
287 | &mut self, | |
291 | file_contents: &[u8], |
|
288 | file_contents: &[u8], | |
292 | ) -> Result<Option<DirstateParents>, DirstateError> { |
|
289 | ) -> Result<Option<DirstateParents>, DirstateError> { | |
293 | if file_contents.is_empty() { |
|
290 | if file_contents.is_empty() { | |
294 | return Ok(None); |
|
291 | return Ok(None); | |
295 | } |
|
292 | } | |
296 |
|
293 | |||
297 | let parents = parse_dirstate( |
|
294 | let parents = parse_dirstate( | |
298 | &mut self.state_map, |
|
295 | &mut self.state_map, | |
299 | &mut self.copy_map, |
|
296 | &mut self.copy_map, | |
300 | file_contents, |
|
297 | file_contents, | |
301 | )?; |
|
298 | )?; | |
302 |
|
299 | |||
303 | if !self.dirty_parents { |
|
300 | if !self.dirty_parents { | |
304 | self.set_parents(parents.to_owned()); |
|
301 | self.set_parents(parents.to_owned()); | |
305 | } |
|
302 | } | |
306 |
|
303 | |||
307 | Ok(Some(parents)) |
|
304 | Ok(Some(parents)) | |
308 | } |
|
305 | } | |
309 |
|
306 | |||
310 | pub fn pack( |
|
307 | pub fn pack( | |
311 | &mut self, |
|
308 | &mut self, | |
312 | parents: DirstateParents, |
|
309 | parents: DirstateParents, | |
313 | now: Duration, |
|
310 | now: Duration, | |
314 | ) -> Result<Vec<u8>, DirstateError> { |
|
311 | ) -> Result<Vec<u8>, DirstateError> { | |
315 | let packed = |
|
312 | let packed = | |
316 | pack_dirstate(&mut self.state_map, &self.copy_map, parents, now)?; |
|
313 | pack_dirstate(&mut self.state_map, &self.copy_map, parents, now)?; | |
317 |
|
314 | |||
318 | self.dirty_parents = false; |
|
315 | self.dirty_parents = false; | |
319 |
|
316 | |||
320 | let result = self.non_normal_other_parent_entries(); |
|
317 | let result = self.non_normal_other_parent_entries(); | |
321 | self.non_normal_set = result.0; |
|
318 | self.non_normal_set = result.0; | |
322 | self.other_parent_set = result.1; |
|
319 | self.other_parent_set = result.1; | |
323 | Ok(packed) |
|
320 | Ok(packed) | |
324 | } |
|
321 | } | |
325 |
|
322 | |||
326 | pub fn build_file_fold_map(&mut self) -> FileFoldMap { |
|
323 | pub fn build_file_fold_map(&mut self) -> FileFoldMap { | |
327 | if let Some(ref file_fold_map) = self.file_fold_map { |
|
324 | if let Some(ref file_fold_map) = self.file_fold_map { | |
328 | return file_fold_map.to_owned(); |
|
325 | return file_fold_map.to_owned(); | |
329 | } |
|
326 | } | |
330 | let mut new_file_fold_map = FileFoldMap::new(); |
|
327 | let mut new_file_fold_map = FileFoldMap::new(); | |
331 | for (filename, DirstateEntry { state, .. }) in self.state_map.borrow() |
|
328 | for (filename, DirstateEntry { state, .. }) in self.state_map.borrow() | |
332 | { |
|
329 | { | |
333 | if *state == EntryState::Removed { |
|
330 | if *state == EntryState::Removed { | |
334 | new_file_fold_map.insert( |
|
331 | new_file_fold_map.insert( | |
335 | filename.to_ascii_uppercase().to_owned(), |
|
332 | filename.to_ascii_uppercase().to_owned(), | |
336 | filename.to_owned(), |
|
333 | filename.to_owned(), | |
337 | ); |
|
334 | ); | |
338 | } |
|
335 | } | |
339 | } |
|
336 | } | |
340 | self.file_fold_map = Some(new_file_fold_map); |
|
337 | self.file_fold_map = Some(new_file_fold_map); | |
341 | self.file_fold_map.to_owned().unwrap() |
|
338 | self.file_fold_map.to_owned().unwrap() | |
342 | } |
|
339 | } | |
343 | } |
|
340 | } | |
344 |
|
341 | |||
345 | #[cfg(test)] |
|
342 | #[cfg(test)] | |
346 | mod tests { |
|
343 | mod tests { | |
347 | use super::*; |
|
344 | use super::*; | |
348 |
|
345 | |||
349 | #[test] |
|
346 | #[test] | |
350 | fn test_dirs_multiset() { |
|
347 | fn test_dirs_multiset() { | |
351 | let mut map = DirstateMap::new(); |
|
348 | let mut map = DirstateMap::new(); | |
352 | assert!(map.dirs.is_none()); |
|
349 | assert!(map.dirs.is_none()); | |
353 | assert!(map.all_dirs.is_none()); |
|
350 | assert!(map.all_dirs.is_none()); | |
354 |
|
351 | |||
355 | assert_eq!(false, map.has_dir(b"nope")); |
|
352 | assert_eq!(false, map.has_dir(b"nope")); | |
356 | assert!(map.all_dirs.is_some()); |
|
353 | assert!(map.all_dirs.is_some()); | |
357 | assert!(map.dirs.is_none()); |
|
354 | assert!(map.dirs.is_none()); | |
358 |
|
355 | |||
359 | assert_eq!(false, map.has_tracked_dir(b"nope")); |
|
356 | assert_eq!(false, map.has_tracked_dir(b"nope")); | |
360 | assert!(map.dirs.is_some()); |
|
357 | assert!(map.dirs.is_some()); | |
361 | } |
|
358 | } | |
362 |
|
359 | |||
363 | #[test] |
|
360 | #[test] | |
364 | fn test_add_file() { |
|
361 | fn test_add_file() { | |
365 | let mut map = DirstateMap::new(); |
|
362 | let mut map = DirstateMap::new(); | |
366 |
|
363 | |||
367 | assert_eq!(0, map.len()); |
|
364 | assert_eq!(0, map.len()); | |
368 |
|
365 | |||
369 | map.add_file( |
|
366 | map.add_file( | |
370 | b"meh", |
|
367 | b"meh", | |
371 | EntryState::Normal, |
|
368 | EntryState::Normal, | |
372 | DirstateEntry { |
|
369 | DirstateEntry { | |
373 | state: EntryState::Normal, |
|
370 | state: EntryState::Normal, | |
374 | mode: 1337, |
|
371 | mode: 1337, | |
375 | mtime: 1337, |
|
372 | mtime: 1337, | |
376 | size: 1337, |
|
373 | size: 1337, | |
377 | }, |
|
374 | }, | |
378 | ); |
|
375 | ); | |
379 |
|
376 | |||
380 | assert_eq!(1, map.len()); |
|
377 | assert_eq!(1, map.len()); | |
381 | assert_eq!(0, map.non_normal_set.len()); |
|
378 | assert_eq!(0, map.non_normal_set.len()); | |
382 | assert_eq!(0, map.other_parent_set.len()); |
|
379 | assert_eq!(0, map.other_parent_set.len()); | |
383 | } |
|
380 | } | |
384 |
|
381 | |||
385 | #[test] |
|
382 | #[test] | |
386 | fn test_non_normal_other_parent_entries() { |
|
383 | fn test_non_normal_other_parent_entries() { | |
387 | let map: DirstateMap = [ |
|
384 | let map: DirstateMap = [ | |
388 | (b"f1", (EntryState::Removed, 1337, 1337, 1337)), |
|
385 | (b"f1", (EntryState::Removed, 1337, 1337, 1337)), | |
389 | (b"f2", (EntryState::Normal, 1337, 1337, -1)), |
|
386 | (b"f2", (EntryState::Normal, 1337, 1337, -1)), | |
390 | (b"f3", (EntryState::Normal, 1337, 1337, 1337)), |
|
387 | (b"f3", (EntryState::Normal, 1337, 1337, 1337)), | |
391 | (b"f4", (EntryState::Normal, 1337, -2, 1337)), |
|
388 | (b"f4", (EntryState::Normal, 1337, -2, 1337)), | |
392 | (b"f5", (EntryState::Added, 1337, 1337, 1337)), |
|
389 | (b"f5", (EntryState::Added, 1337, 1337, 1337)), | |
393 | (b"f6", (EntryState::Added, 1337, 1337, -1)), |
|
390 | (b"f6", (EntryState::Added, 1337, 1337, -1)), | |
394 | (b"f7", (EntryState::Merged, 1337, 1337, -1)), |
|
391 | (b"f7", (EntryState::Merged, 1337, 1337, -1)), | |
395 | (b"f8", (EntryState::Merged, 1337, 1337, 1337)), |
|
392 | (b"f8", (EntryState::Merged, 1337, 1337, 1337)), | |
396 | (b"f9", (EntryState::Merged, 1337, -2, 1337)), |
|
393 | (b"f9", (EntryState::Merged, 1337, -2, 1337)), | |
397 | (b"fa", (EntryState::Added, 1337, -2, 1337)), |
|
394 | (b"fa", (EntryState::Added, 1337, -2, 1337)), | |
398 | (b"fb", (EntryState::Removed, 1337, -2, 1337)), |
|
395 | (b"fb", (EntryState::Removed, 1337, -2, 1337)), | |
399 | ] |
|
396 | ] | |
400 | .iter() |
|
397 | .iter() | |
401 | .map(|(fname, (state, mode, size, mtime))| { |
|
398 | .map(|(fname, (state, mode, size, mtime))| { | |
402 | ( |
|
399 | ( | |
403 | fname.to_vec(), |
|
400 | fname.to_vec(), | |
404 | DirstateEntry { |
|
401 | DirstateEntry { | |
405 | state: *state, |
|
402 | state: *state, | |
406 | mode: *mode, |
|
403 | mode: *mode, | |
407 | size: *size, |
|
404 | size: *size, | |
408 | mtime: *mtime, |
|
405 | mtime: *mtime, | |
409 | }, |
|
406 | }, | |
410 | ) |
|
407 | ) | |
411 | }) |
|
408 | }) | |
412 | .collect(); |
|
409 | .collect(); | |
413 |
|
410 | |||
414 | let non_normal = [ |
|
411 | let non_normal = [ | |
415 | 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", | |
416 | ] |
|
413 | ] | |
417 | .iter() |
|
414 | .iter() | |
418 | .map(|x| x.to_vec()) |
|
415 | .map(|x| x.to_vec()) | |
419 | .collect(); |
|
416 | .collect(); | |
420 |
|
417 | |||
421 | let mut other_parent = HashSet::new(); |
|
418 | let mut other_parent = HashSet::new(); | |
422 | other_parent.insert(b"f4".to_vec()); |
|
419 | other_parent.insert(b"f4".to_vec()); | |
423 |
|
420 | |||
424 | assert_eq!( |
|
421 | assert_eq!( | |
425 | (non_normal, other_parent), |
|
422 | (non_normal, other_parent), | |
426 | map.non_normal_other_parent_entries() |
|
423 | map.non_normal_other_parent_entries() | |
427 | ); |
|
424 | ); | |
428 | } |
|
425 | } | |
429 | } |
|
426 | } |
General Comments 0
You need to be logged in to leave comments.
Login now