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