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, |
|
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_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.clone()); | |
259 | } |
|
259 | } | |
260 | let parents; |
|
260 | let parents; | |
261 |
if file_contents.len() == |
|
261 | if file_contents.len() == PARENT_SIZE * 2 { | |
262 | parents = DirstateParents { |
|
262 | parents = DirstateParents { | |
263 | p1: copy_into_array(&file_contents[..PARENT_SIZE]), |
|
263 | p1: copy_into_array(&file_contents[..PARENT_SIZE]), | |
264 | p2: copy_into_array( |
|
264 | p2: copy_into_array( | |
265 | &file_contents[PARENT_SIZE..PARENT_SIZE * 2], |
|
265 | &file_contents[PARENT_SIZE..PARENT_SIZE * 2], | |
266 | ), |
|
266 | ), | |
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.to_owned()); | |
278 | Ok(parents.clone()) |
|
278 | Ok(parents.clone()) | |
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.to_owned()); | |
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.to_owned(); | |
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.to_owned().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 | } |
General Comments 0
You need to be logged in to leave comments.
Login now