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