##// END OF EJS Templates
dirstate: Remove the Rust abstraction DirstateMapMethods...
Simon Sapin -
r48883:3d0a9c6e default
parent child Browse files
Show More
@@ -1,7 +1,5 b''
1 1 pub mod dirstate_map;
2 pub mod dispatch;
3 2 pub mod on_disk;
4 3 pub mod owning;
5 mod owning_dispatch;
6 4 pub mod path_with_basename;
7 5 pub mod status;
@@ -1,1168 +1,1193 b''
1 1 use bytes_cast::BytesCast;
2 2 use micro_timer::timed;
3 3 use std::borrow::Cow;
4 4 use std::convert::TryInto;
5 5 use std::path::PathBuf;
6 6
7 7 use super::on_disk;
8 8 use super::on_disk::DirstateV2ParseError;
9 use super::owning::OwningDirstateMap;
9 10 use super::path_with_basename::WithBasename;
10 11 use crate::dirstate::parsers::pack_entry;
11 12 use crate::dirstate::parsers::packed_entry_size;
12 13 use crate::dirstate::parsers::parse_dirstate_entries;
13 14 use crate::dirstate::parsers::Timestamp;
14 15 use crate::dirstate::CopyMapIter;
15 16 use crate::dirstate::StateMapIter;
16 17 use crate::dirstate::SIZE_FROM_OTHER_PARENT;
17 18 use crate::dirstate::SIZE_NON_NORMAL;
18 19 use crate::matchers::Matcher;
19 20 use crate::utils::hg_path::{HgPath, HgPathBuf};
20 21 use crate::DirstateEntry;
21 22 use crate::DirstateError;
22 23 use crate::DirstateParents;
23 24 use crate::DirstateStatus;
24 25 use crate::EntryState;
25 26 use crate::FastHashMap;
26 27 use crate::PatternFileWarning;
27 28 use crate::StatusError;
28 29 use crate::StatusOptions;
29 30
30 31 /// Append to an existing data file if the amount of unreachable data (not used
31 32 /// anymore) is less than this fraction of the total amount of existing data.
32 33 const ACCEPTABLE_UNREACHABLE_BYTES_RATIO: f32 = 0.5;
33 34
34 35 pub struct DirstateMap<'on_disk> {
35 36 /// Contents of the `.hg/dirstate` file
36 37 pub(super) on_disk: &'on_disk [u8],
37 38
38 39 pub(super) root: ChildNodes<'on_disk>,
39 40
40 41 /// Number of nodes anywhere in the tree that have `.entry.is_some()`.
41 42 pub(super) nodes_with_entry_count: u32,
42 43
43 44 /// Number of nodes anywhere in the tree that have
44 45 /// `.copy_source.is_some()`.
45 46 pub(super) nodes_with_copy_source_count: u32,
46 47
47 48 /// See on_disk::Header
48 49 pub(super) ignore_patterns_hash: on_disk::IgnorePatternsHash,
49 50
50 51 /// How many bytes of `on_disk` are not used anymore
51 52 pub(super) unreachable_bytes: u32,
52 53 }
53 54
54 55 /// Using a plain `HgPathBuf` of the full path from the repository root as a
55 56 /// map key would also work: all paths in a given map have the same parent
56 57 /// path, so comparing full paths gives the same result as comparing base
57 58 /// names. However `HashMap` would waste time always re-hashing the same
58 59 /// string prefix.
59 60 pub(super) type NodeKey<'on_disk> = WithBasename<Cow<'on_disk, HgPath>>;
60 61
61 62 /// Similar to `&'tree Cow<'on_disk, HgPath>`, but can also be returned
62 63 /// for on-disk nodes that don’t actually have a `Cow` to borrow.
63 64 pub(super) enum BorrowedPath<'tree, 'on_disk> {
64 65 InMemory(&'tree HgPathBuf),
65 66 OnDisk(&'on_disk HgPath),
66 67 }
67 68
68 69 pub(super) enum ChildNodes<'on_disk> {
69 70 InMemory(FastHashMap<NodeKey<'on_disk>, Node<'on_disk>>),
70 71 OnDisk(&'on_disk [on_disk::Node]),
71 72 }
72 73
73 74 pub(super) enum ChildNodesRef<'tree, 'on_disk> {
74 75 InMemory(&'tree FastHashMap<NodeKey<'on_disk>, Node<'on_disk>>),
75 76 OnDisk(&'on_disk [on_disk::Node]),
76 77 }
77 78
78 79 pub(super) enum NodeRef<'tree, 'on_disk> {
79 80 InMemory(&'tree NodeKey<'on_disk>, &'tree Node<'on_disk>),
80 81 OnDisk(&'on_disk on_disk::Node),
81 82 }
82 83
83 84 impl<'tree, 'on_disk> BorrowedPath<'tree, 'on_disk> {
84 85 pub fn detach_from_tree(&self) -> Cow<'on_disk, HgPath> {
85 86 match *self {
86 87 BorrowedPath::InMemory(in_memory) => Cow::Owned(in_memory.clone()),
87 88 BorrowedPath::OnDisk(on_disk) => Cow::Borrowed(on_disk),
88 89 }
89 90 }
90 91 }
91 92
92 93 impl<'tree, 'on_disk> std::ops::Deref for BorrowedPath<'tree, 'on_disk> {
93 94 type Target = HgPath;
94 95
95 96 fn deref(&self) -> &HgPath {
96 97 match *self {
97 98 BorrowedPath::InMemory(in_memory) => in_memory,
98 99 BorrowedPath::OnDisk(on_disk) => on_disk,
99 100 }
100 101 }
101 102 }
102 103
103 104 impl Default for ChildNodes<'_> {
104 105 fn default() -> Self {
105 106 ChildNodes::InMemory(Default::default())
106 107 }
107 108 }
108 109
109 110 impl<'on_disk> ChildNodes<'on_disk> {
110 111 pub(super) fn as_ref<'tree>(
111 112 &'tree self,
112 113 ) -> ChildNodesRef<'tree, 'on_disk> {
113 114 match self {
114 115 ChildNodes::InMemory(nodes) => ChildNodesRef::InMemory(nodes),
115 116 ChildNodes::OnDisk(nodes) => ChildNodesRef::OnDisk(nodes),
116 117 }
117 118 }
118 119
119 120 pub(super) fn is_empty(&self) -> bool {
120 121 match self {
121 122 ChildNodes::InMemory(nodes) => nodes.is_empty(),
122 123 ChildNodes::OnDisk(nodes) => nodes.is_empty(),
123 124 }
124 125 }
125 126
126 127 fn make_mut(
127 128 &mut self,
128 129 on_disk: &'on_disk [u8],
129 130 unreachable_bytes: &mut u32,
130 131 ) -> Result<
131 132 &mut FastHashMap<NodeKey<'on_disk>, Node<'on_disk>>,
132 133 DirstateV2ParseError,
133 134 > {
134 135 match self {
135 136 ChildNodes::InMemory(nodes) => Ok(nodes),
136 137 ChildNodes::OnDisk(nodes) => {
137 138 *unreachable_bytes +=
138 139 std::mem::size_of_val::<[on_disk::Node]>(nodes) as u32;
139 140 let nodes = nodes
140 141 .iter()
141 142 .map(|node| {
142 143 Ok((
143 144 node.path(on_disk)?,
144 145 node.to_in_memory_node(on_disk)?,
145 146 ))
146 147 })
147 148 .collect::<Result<_, _>>()?;
148 149 *self = ChildNodes::InMemory(nodes);
149 150 match self {
150 151 ChildNodes::InMemory(nodes) => Ok(nodes),
151 152 ChildNodes::OnDisk(_) => unreachable!(),
152 153 }
153 154 }
154 155 }
155 156 }
156 157 }
157 158
158 159 impl<'tree, 'on_disk> ChildNodesRef<'tree, 'on_disk> {
159 160 pub(super) fn get(
160 161 &self,
161 162 base_name: &HgPath,
162 163 on_disk: &'on_disk [u8],
163 164 ) -> Result<Option<NodeRef<'tree, 'on_disk>>, DirstateV2ParseError> {
164 165 match self {
165 166 ChildNodesRef::InMemory(nodes) => Ok(nodes
166 167 .get_key_value(base_name)
167 168 .map(|(k, v)| NodeRef::InMemory(k, v))),
168 169 ChildNodesRef::OnDisk(nodes) => {
169 170 let mut parse_result = Ok(());
170 171 let search_result = nodes.binary_search_by(|node| {
171 172 match node.base_name(on_disk) {
172 173 Ok(node_base_name) => node_base_name.cmp(base_name),
173 174 Err(e) => {
174 175 parse_result = Err(e);
175 176 // Dummy comparison result, `search_result` won’t
176 177 // be used since `parse_result` is an error
177 178 std::cmp::Ordering::Equal
178 179 }
179 180 }
180 181 });
181 182 parse_result.map(|()| {
182 183 search_result.ok().map(|i| NodeRef::OnDisk(&nodes[i]))
183 184 })
184 185 }
185 186 }
186 187 }
187 188
188 189 /// Iterate in undefined order
189 190 pub(super) fn iter(
190 191 &self,
191 192 ) -> impl Iterator<Item = NodeRef<'tree, 'on_disk>> {
192 193 match self {
193 194 ChildNodesRef::InMemory(nodes) => itertools::Either::Left(
194 195 nodes.iter().map(|(k, v)| NodeRef::InMemory(k, v)),
195 196 ),
196 197 ChildNodesRef::OnDisk(nodes) => {
197 198 itertools::Either::Right(nodes.iter().map(NodeRef::OnDisk))
198 199 }
199 200 }
200 201 }
201 202
202 203 /// Iterate in parallel in undefined order
203 204 pub(super) fn par_iter(
204 205 &self,
205 206 ) -> impl rayon::iter::ParallelIterator<Item = NodeRef<'tree, 'on_disk>>
206 207 {
207 208 use rayon::prelude::*;
208 209 match self {
209 210 ChildNodesRef::InMemory(nodes) => rayon::iter::Either::Left(
210 211 nodes.par_iter().map(|(k, v)| NodeRef::InMemory(k, v)),
211 212 ),
212 213 ChildNodesRef::OnDisk(nodes) => rayon::iter::Either::Right(
213 214 nodes.par_iter().map(NodeRef::OnDisk),
214 215 ),
215 216 }
216 217 }
217 218
218 219 pub(super) fn sorted(&self) -> Vec<NodeRef<'tree, 'on_disk>> {
219 220 match self {
220 221 ChildNodesRef::InMemory(nodes) => {
221 222 let mut vec: Vec<_> = nodes
222 223 .iter()
223 224 .map(|(k, v)| NodeRef::InMemory(k, v))
224 225 .collect();
225 226 fn sort_key<'a>(node: &'a NodeRef) -> &'a HgPath {
226 227 match node {
227 228 NodeRef::InMemory(path, _node) => path.base_name(),
228 229 NodeRef::OnDisk(_) => unreachable!(),
229 230 }
230 231 }
231 232 // `sort_unstable_by_key` doesn’t allow keys borrowing from the
232 233 // value: https://github.com/rust-lang/rust/issues/34162
233 234 vec.sort_unstable_by(|a, b| sort_key(a).cmp(sort_key(b)));
234 235 vec
235 236 }
236 237 ChildNodesRef::OnDisk(nodes) => {
237 238 // Nodes on disk are already sorted
238 239 nodes.iter().map(NodeRef::OnDisk).collect()
239 240 }
240 241 }
241 242 }
242 243 }
243 244
244 245 impl<'tree, 'on_disk> NodeRef<'tree, 'on_disk> {
245 246 pub(super) fn full_path(
246 247 &self,
247 248 on_disk: &'on_disk [u8],
248 249 ) -> Result<&'tree HgPath, DirstateV2ParseError> {
249 250 match self {
250 251 NodeRef::InMemory(path, _node) => Ok(path.full_path()),
251 252 NodeRef::OnDisk(node) => node.full_path(on_disk),
252 253 }
253 254 }
254 255
255 256 /// Returns a `BorrowedPath`, which can be turned into a `Cow<'on_disk,
256 257 /// HgPath>` detached from `'tree`
257 258 pub(super) fn full_path_borrowed(
258 259 &self,
259 260 on_disk: &'on_disk [u8],
260 261 ) -> Result<BorrowedPath<'tree, 'on_disk>, DirstateV2ParseError> {
261 262 match self {
262 263 NodeRef::InMemory(path, _node) => match path.full_path() {
263 264 Cow::Borrowed(on_disk) => Ok(BorrowedPath::OnDisk(on_disk)),
264 265 Cow::Owned(in_memory) => Ok(BorrowedPath::InMemory(in_memory)),
265 266 },
266 267 NodeRef::OnDisk(node) => {
267 268 Ok(BorrowedPath::OnDisk(node.full_path(on_disk)?))
268 269 }
269 270 }
270 271 }
271 272
272 273 pub(super) fn base_name(
273 274 &self,
274 275 on_disk: &'on_disk [u8],
275 276 ) -> Result<&'tree HgPath, DirstateV2ParseError> {
276 277 match self {
277 278 NodeRef::InMemory(path, _node) => Ok(path.base_name()),
278 279 NodeRef::OnDisk(node) => node.base_name(on_disk),
279 280 }
280 281 }
281 282
282 283 pub(super) fn children(
283 284 &self,
284 285 on_disk: &'on_disk [u8],
285 286 ) -> Result<ChildNodesRef<'tree, 'on_disk>, DirstateV2ParseError> {
286 287 match self {
287 288 NodeRef::InMemory(_path, node) => Ok(node.children.as_ref()),
288 289 NodeRef::OnDisk(node) => {
289 290 Ok(ChildNodesRef::OnDisk(node.children(on_disk)?))
290 291 }
291 292 }
292 293 }
293 294
294 295 pub(super) fn has_copy_source(&self) -> bool {
295 296 match self {
296 297 NodeRef::InMemory(_path, node) => node.copy_source.is_some(),
297 298 NodeRef::OnDisk(node) => node.has_copy_source(),
298 299 }
299 300 }
300 301
301 302 pub(super) fn copy_source(
302 303 &self,
303 304 on_disk: &'on_disk [u8],
304 305 ) -> Result<Option<&'tree HgPath>, DirstateV2ParseError> {
305 306 match self {
306 307 NodeRef::InMemory(_path, node) => {
307 308 Ok(node.copy_source.as_ref().map(|s| &**s))
308 309 }
309 310 NodeRef::OnDisk(node) => node.copy_source(on_disk),
310 311 }
311 312 }
312 313
313 314 pub(super) fn entry(
314 315 &self,
315 316 ) -> Result<Option<DirstateEntry>, DirstateV2ParseError> {
316 317 match self {
317 318 NodeRef::InMemory(_path, node) => {
318 319 Ok(node.data.as_entry().copied())
319 320 }
320 321 NodeRef::OnDisk(node) => node.entry(),
321 322 }
322 323 }
323 324
324 325 pub(super) fn state(
325 326 &self,
326 327 ) -> Result<Option<EntryState>, DirstateV2ParseError> {
327 328 match self {
328 329 NodeRef::InMemory(_path, node) => {
329 330 Ok(node.data.as_entry().map(|entry| entry.state()))
330 331 }
331 332 NodeRef::OnDisk(node) => node.state(),
332 333 }
333 334 }
334 335
335 336 pub(super) fn cached_directory_mtime(
336 337 &self,
337 338 ) -> Option<&'tree on_disk::Timestamp> {
338 339 match self {
339 340 NodeRef::InMemory(_path, node) => match &node.data {
340 341 NodeData::CachedDirectory { mtime } => Some(mtime),
341 342 _ => None,
342 343 },
343 344 NodeRef::OnDisk(node) => node.cached_directory_mtime(),
344 345 }
345 346 }
346 347
347 348 pub(super) fn descendants_with_entry_count(&self) -> u32 {
348 349 match self {
349 350 NodeRef::InMemory(_path, node) => {
350 351 node.descendants_with_entry_count
351 352 }
352 353 NodeRef::OnDisk(node) => node.descendants_with_entry_count.get(),
353 354 }
354 355 }
355 356
356 357 pub(super) fn tracked_descendants_count(&self) -> u32 {
357 358 match self {
358 359 NodeRef::InMemory(_path, node) => node.tracked_descendants_count,
359 360 NodeRef::OnDisk(node) => node.tracked_descendants_count.get(),
360 361 }
361 362 }
362 363 }
363 364
364 365 /// Represents a file or a directory
365 366 #[derive(Default)]
366 367 pub(super) struct Node<'on_disk> {
367 368 pub(super) data: NodeData,
368 369
369 370 pub(super) copy_source: Option<Cow<'on_disk, HgPath>>,
370 371
371 372 pub(super) children: ChildNodes<'on_disk>,
372 373
373 374 /// How many (non-inclusive) descendants of this node have an entry.
374 375 pub(super) descendants_with_entry_count: u32,
375 376
376 377 /// How many (non-inclusive) descendants of this node have an entry whose
377 378 /// state is "tracked".
378 379 pub(super) tracked_descendants_count: u32,
379 380 }
380 381
381 382 pub(super) enum NodeData {
382 383 Entry(DirstateEntry),
383 384 CachedDirectory { mtime: on_disk::Timestamp },
384 385 None,
385 386 }
386 387
387 388 impl Default for NodeData {
388 389 fn default() -> Self {
389 390 NodeData::None
390 391 }
391 392 }
392 393
393 394 impl NodeData {
394 395 fn has_entry(&self) -> bool {
395 396 match self {
396 397 NodeData::Entry(_) => true,
397 398 _ => false,
398 399 }
399 400 }
400 401
401 402 fn as_entry(&self) -> Option<&DirstateEntry> {
402 403 match self {
403 404 NodeData::Entry(entry) => Some(entry),
404 405 _ => None,
405 406 }
406 407 }
407 408 }
408 409
409 410 impl<'on_disk> DirstateMap<'on_disk> {
410 411 pub(super) fn empty(on_disk: &'on_disk [u8]) -> Self {
411 412 Self {
412 413 on_disk,
413 414 root: ChildNodes::default(),
414 415 nodes_with_entry_count: 0,
415 416 nodes_with_copy_source_count: 0,
416 417 ignore_patterns_hash: [0; on_disk::IGNORE_PATTERNS_HASH_LEN],
417 418 unreachable_bytes: 0,
418 419 }
419 420 }
420 421
421 422 #[timed]
422 423 pub fn new_v2(
423 424 on_disk: &'on_disk [u8],
424 425 data_size: usize,
425 426 metadata: &[u8],
426 427 ) -> Result<Self, DirstateError> {
427 428 if let Some(data) = on_disk.get(..data_size) {
428 429 Ok(on_disk::read(data, metadata)?)
429 430 } else {
430 431 Err(DirstateV2ParseError.into())
431 432 }
432 433 }
433 434
434 435 #[timed]
435 436 pub fn new_v1(
436 437 on_disk: &'on_disk [u8],
437 438 ) -> Result<(Self, Option<DirstateParents>), DirstateError> {
438 439 let mut map = Self::empty(on_disk);
439 440 if map.on_disk.is_empty() {
440 441 return Ok((map, None));
441 442 }
442 443
443 444 let parents = parse_dirstate_entries(
444 445 map.on_disk,
445 446 |path, entry, copy_source| {
446 447 let tracked = entry.state().is_tracked();
447 448 let node = Self::get_or_insert_node(
448 449 map.on_disk,
449 450 &mut map.unreachable_bytes,
450 451 &mut map.root,
451 452 path,
452 453 WithBasename::to_cow_borrowed,
453 454 |ancestor| {
454 455 if tracked {
455 456 ancestor.tracked_descendants_count += 1
456 457 }
457 458 ancestor.descendants_with_entry_count += 1
458 459 },
459 460 )?;
460 461 assert!(
461 462 !node.data.has_entry(),
462 463 "duplicate dirstate entry in read"
463 464 );
464 465 assert!(
465 466 node.copy_source.is_none(),
466 467 "duplicate dirstate entry in read"
467 468 );
468 469 node.data = NodeData::Entry(*entry);
469 470 node.copy_source = copy_source.map(Cow::Borrowed);
470 471 map.nodes_with_entry_count += 1;
471 472 if copy_source.is_some() {
472 473 map.nodes_with_copy_source_count += 1
473 474 }
474 475 Ok(())
475 476 },
476 477 )?;
477 478 let parents = Some(parents.clone());
478 479
479 480 Ok((map, parents))
480 481 }
481 482
482 483 /// Assuming dirstate-v2 format, returns whether the next write should
483 484 /// append to the existing data file that contains `self.on_disk` (true),
484 485 /// or create a new data file from scratch (false).
485 486 pub(super) fn write_should_append(&self) -> bool {
486 487 let ratio = self.unreachable_bytes as f32 / self.on_disk.len() as f32;
487 488 ratio < ACCEPTABLE_UNREACHABLE_BYTES_RATIO
488 489 }
489 490
490 491 fn get_node<'tree>(
491 492 &'tree self,
492 493 path: &HgPath,
493 494 ) -> Result<Option<NodeRef<'tree, 'on_disk>>, DirstateV2ParseError> {
494 495 let mut children = self.root.as_ref();
495 496 let mut components = path.components();
496 497 let mut component =
497 498 components.next().expect("expected at least one components");
498 499 loop {
499 500 if let Some(child) = children.get(component, self.on_disk)? {
500 501 if let Some(next_component) = components.next() {
501 502 component = next_component;
502 503 children = child.children(self.on_disk)?;
503 504 } else {
504 505 return Ok(Some(child));
505 506 }
506 507 } else {
507 508 return Ok(None);
508 509 }
509 510 }
510 511 }
511 512
512 513 /// Returns a mutable reference to the node at `path` if it exists
513 514 ///
514 515 /// This takes `root` instead of `&mut self` so that callers can mutate
515 516 /// other fields while the returned borrow is still valid
516 517 fn get_node_mut<'tree>(
517 518 on_disk: &'on_disk [u8],
518 519 unreachable_bytes: &mut u32,
519 520 root: &'tree mut ChildNodes<'on_disk>,
520 521 path: &HgPath,
521 522 ) -> Result<Option<&'tree mut Node<'on_disk>>, DirstateV2ParseError> {
522 523 let mut children = root;
523 524 let mut components = path.components();
524 525 let mut component =
525 526 components.next().expect("expected at least one components");
526 527 loop {
527 528 if let Some(child) = children
528 529 .make_mut(on_disk, unreachable_bytes)?
529 530 .get_mut(component)
530 531 {
531 532 if let Some(next_component) = components.next() {
532 533 component = next_component;
533 534 children = &mut child.children;
534 535 } else {
535 536 return Ok(Some(child));
536 537 }
537 538 } else {
538 539 return Ok(None);
539 540 }
540 541 }
541 542 }
542 543
543 544 pub(super) fn get_or_insert<'tree, 'path>(
544 545 &'tree mut self,
545 546 path: &HgPath,
546 547 ) -> Result<&'tree mut Node<'on_disk>, DirstateV2ParseError> {
547 548 Self::get_or_insert_node(
548 549 self.on_disk,
549 550 &mut self.unreachable_bytes,
550 551 &mut self.root,
551 552 path,
552 553 WithBasename::to_cow_owned,
553 554 |_| {},
554 555 )
555 556 }
556 557
557 558 fn get_or_insert_node<'tree, 'path>(
558 559 on_disk: &'on_disk [u8],
559 560 unreachable_bytes: &mut u32,
560 561 root: &'tree mut ChildNodes<'on_disk>,
561 562 path: &'path HgPath,
562 563 to_cow: impl Fn(
563 564 WithBasename<&'path HgPath>,
564 565 ) -> WithBasename<Cow<'on_disk, HgPath>>,
565 566 mut each_ancestor: impl FnMut(&mut Node),
566 567 ) -> Result<&'tree mut Node<'on_disk>, DirstateV2ParseError> {
567 568 let mut child_nodes = root;
568 569 let mut inclusive_ancestor_paths =
569 570 WithBasename::inclusive_ancestors_of(path);
570 571 let mut ancestor_path = inclusive_ancestor_paths
571 572 .next()
572 573 .expect("expected at least one inclusive ancestor");
573 574 loop {
574 575 // TODO: can we avoid allocating an owned key in cases where the
575 576 // map already contains that key, without introducing double
576 577 // lookup?
577 578 let child_node = child_nodes
578 579 .make_mut(on_disk, unreachable_bytes)?
579 580 .entry(to_cow(ancestor_path))
580 581 .or_default();
581 582 if let Some(next) = inclusive_ancestor_paths.next() {
582 583 each_ancestor(child_node);
583 584 ancestor_path = next;
584 585 child_nodes = &mut child_node.children;
585 586 } else {
586 587 return Ok(child_node);
587 588 }
588 589 }
589 590 }
590 591
591 592 fn add_or_remove_file(
592 593 &mut self,
593 594 path: &HgPath,
594 595 old_state: Option<EntryState>,
595 596 new_entry: DirstateEntry,
596 597 ) -> Result<(), DirstateV2ParseError> {
597 598 let had_entry = old_state.is_some();
598 599 let was_tracked = old_state.map_or(false, |s| s.is_tracked());
599 600 let tracked_count_increment =
600 601 match (was_tracked, new_entry.state().is_tracked()) {
601 602 (false, true) => 1,
602 603 (true, false) => -1,
603 604 _ => 0,
604 605 };
605 606
606 607 let node = Self::get_or_insert_node(
607 608 self.on_disk,
608 609 &mut self.unreachable_bytes,
609 610 &mut self.root,
610 611 path,
611 612 WithBasename::to_cow_owned,
612 613 |ancestor| {
613 614 if !had_entry {
614 615 ancestor.descendants_with_entry_count += 1;
615 616 }
616 617
617 618 // We can’t use `+= increment` because the counter is unsigned,
618 619 // and we want debug builds to detect accidental underflow
619 620 // through zero
620 621 match tracked_count_increment {
621 622 1 => ancestor.tracked_descendants_count += 1,
622 623 -1 => ancestor.tracked_descendants_count -= 1,
623 624 _ => {}
624 625 }
625 626 },
626 627 )?;
627 628 if !had_entry {
628 629 self.nodes_with_entry_count += 1
629 630 }
630 631 node.data = NodeData::Entry(new_entry);
631 632 Ok(())
632 633 }
633 634
634 635 fn iter_nodes<'tree>(
635 636 &'tree self,
636 637 ) -> impl Iterator<
637 638 Item = Result<NodeRef<'tree, 'on_disk>, DirstateV2ParseError>,
638 639 > + 'tree {
639 640 // Depth first tree traversal.
640 641 //
641 642 // If we could afford internal iteration and recursion,
642 643 // this would look like:
643 644 //
644 645 // ```
645 646 // fn traverse_children(
646 647 // children: &ChildNodes,
647 648 // each: &mut impl FnMut(&Node),
648 649 // ) {
649 650 // for child in children.values() {
650 651 // traverse_children(&child.children, each);
651 652 // each(child);
652 653 // }
653 654 // }
654 655 // ```
655 656 //
656 657 // However we want an external iterator and therefore can’t use the
657 658 // call stack. Use an explicit stack instead:
658 659 let mut stack = Vec::new();
659 660 let mut iter = self.root.as_ref().iter();
660 661 std::iter::from_fn(move || {
661 662 while let Some(child_node) = iter.next() {
662 663 let children = match child_node.children(self.on_disk) {
663 664 Ok(children) => children,
664 665 Err(error) => return Some(Err(error)),
665 666 };
666 667 // Pseudo-recursion
667 668 let new_iter = children.iter();
668 669 let old_iter = std::mem::replace(&mut iter, new_iter);
669 670 stack.push((child_node, old_iter));
670 671 }
671 672 // Found the end of a `children.iter()` iterator.
672 673 if let Some((child_node, next_iter)) = stack.pop() {
673 674 // "Return" from pseudo-recursion by restoring state from the
674 675 // explicit stack
675 676 iter = next_iter;
676 677
677 678 Some(Ok(child_node))
678 679 } else {
679 680 // Reached the bottom of the stack, we’re done
680 681 None
681 682 }
682 683 })
683 684 }
684 685
685 686 fn clear_known_ambiguous_mtimes(
686 687 &mut self,
687 688 paths: &[impl AsRef<HgPath>],
688 689 ) -> Result<(), DirstateV2ParseError> {
689 690 for path in paths {
690 691 if let Some(node) = Self::get_node_mut(
691 692 self.on_disk,
692 693 &mut self.unreachable_bytes,
693 694 &mut self.root,
694 695 path.as_ref(),
695 696 )? {
696 697 if let NodeData::Entry(entry) = &mut node.data {
697 698 entry.clear_mtime();
698 699 }
699 700 }
700 701 }
701 702 Ok(())
702 703 }
703 704
704 705 fn count_dropped_path(unreachable_bytes: &mut u32, path: &Cow<HgPath>) {
705 706 if let Cow::Borrowed(path) = path {
706 707 *unreachable_bytes += path.len() as u32
707 708 }
708 709 }
709 710 }
710 711
711 712 /// Like `Iterator::filter_map`, but over a fallible iterator of `Result`s.
712 713 ///
713 714 /// The callback is only called for incoming `Ok` values. Errors are passed
714 715 /// through as-is. In order to let it use the `?` operator the callback is
715 716 /// expected to return a `Result` of `Option`, instead of an `Option` of
716 717 /// `Result`.
717 718 fn filter_map_results<'a, I, F, A, B, E>(
718 719 iter: I,
719 720 f: F,
720 721 ) -> impl Iterator<Item = Result<B, E>> + 'a
721 722 where
722 723 I: Iterator<Item = Result<A, E>> + 'a,
723 724 F: Fn(A) -> Result<Option<B>, E> + 'a,
724 725 {
725 726 iter.filter_map(move |result| match result {
726 727 Ok(node) => f(node).transpose(),
727 728 Err(e) => Some(Err(e)),
728 729 })
729 730 }
730 731
731 impl<'on_disk> super::dispatch::DirstateMapMethods for DirstateMap<'on_disk> {
732 fn clear(&mut self) {
733 self.root = Default::default();
734 self.nodes_with_entry_count = 0;
735 self.nodes_with_copy_source_count = 0;
732 impl OwningDirstateMap {
733 pub fn clear(&mut self) {
734 let map = self.get_map_mut();
735 map.root = Default::default();
736 map.nodes_with_entry_count = 0;
737 map.nodes_with_copy_source_count = 0;
736 738 }
737 739
738 fn set_entry(
740 pub fn set_entry(
739 741 &mut self,
740 742 filename: &HgPath,
741 743 entry: DirstateEntry,
742 744 ) -> Result<(), DirstateV2ParseError> {
743 self.get_or_insert(&filename)?.data = NodeData::Entry(entry);
745 let map = self.get_map_mut();
746 map.get_or_insert(&filename)?.data = NodeData::Entry(entry);
744 747 Ok(())
745 748 }
746 749
747 fn add_file(
750 pub fn add_file(
748 751 &mut self,
749 752 filename: &HgPath,
750 753 entry: DirstateEntry,
751 754 ) -> Result<(), DirstateError> {
752 755 let old_state = self.get(filename)?.map(|e| e.state());
753 Ok(self.add_or_remove_file(filename, old_state, entry)?)
756 let map = self.get_map_mut();
757 Ok(map.add_or_remove_file(filename, old_state, entry)?)
754 758 }
755 759
756 fn remove_file(
760 pub fn remove_file(
757 761 &mut self,
758 762 filename: &HgPath,
759 763 in_merge: bool,
760 764 ) -> Result<(), DirstateError> {
761 765 let old_entry_opt = self.get(filename)?;
762 766 let old_state = old_entry_opt.map(|e| e.state());
763 767 let mut size = 0;
764 768 if in_merge {
765 769 // XXX we should not be able to have 'm' state and 'FROM_P2' if not
766 770 // during a merge. So I (marmoute) am not sure we need the
767 771 // conditionnal at all. Adding double checking this with assert
768 772 // would be nice.
769 773 if let Some(old_entry) = old_entry_opt {
770 774 // backup the previous state
771 775 if old_entry.state() == EntryState::Merged {
772 776 size = SIZE_NON_NORMAL;
773 777 } else if old_entry.state() == EntryState::Normal
774 778 && old_entry.size() == SIZE_FROM_OTHER_PARENT
775 779 {
776 780 // other parent
777 781 size = SIZE_FROM_OTHER_PARENT;
778 782 }
779 783 }
780 784 }
781 785 if size == 0 {
782 786 self.copy_map_remove(filename)?;
783 787 }
788 let map = self.get_map_mut();
784 789 let entry = DirstateEntry::new_removed(size);
785 Ok(self.add_or_remove_file(filename, old_state, entry)?)
790 Ok(map.add_or_remove_file(filename, old_state, entry)?)
786 791 }
787 792
788 fn drop_entry_and_copy_source(
793 pub fn drop_entry_and_copy_source(
789 794 &mut self,
790 795 filename: &HgPath,
791 796 ) -> Result<(), DirstateError> {
792 797 let was_tracked = self
793 798 .get(filename)?
794 799 .map_or(false, |e| e.state().is_tracked());
800 let map = self.get_map_mut();
795 801 struct Dropped {
796 802 was_tracked: bool,
797 803 had_entry: bool,
798 804 had_copy_source: bool,
799 805 }
800 806
801 807 /// If this returns `Ok(Some((dropped, removed)))`, then
802 808 ///
803 809 /// * `dropped` is about the leaf node that was at `filename`
804 810 /// * `removed` is whether this particular level of recursion just
805 811 /// removed a node in `nodes`.
806 812 fn recur<'on_disk>(
807 813 on_disk: &'on_disk [u8],
808 814 unreachable_bytes: &mut u32,
809 815 nodes: &mut ChildNodes<'on_disk>,
810 816 path: &HgPath,
811 817 ) -> Result<Option<(Dropped, bool)>, DirstateV2ParseError> {
812 818 let (first_path_component, rest_of_path) =
813 819 path.split_first_component();
814 820 let nodes = nodes.make_mut(on_disk, unreachable_bytes)?;
815 821 let node = if let Some(node) = nodes.get_mut(first_path_component)
816 822 {
817 823 node
818 824 } else {
819 825 return Ok(None);
820 826 };
821 827 let dropped;
822 828 if let Some(rest) = rest_of_path {
823 829 if let Some((d, removed)) = recur(
824 830 on_disk,
825 831 unreachable_bytes,
826 832 &mut node.children,
827 833 rest,
828 834 )? {
829 835 dropped = d;
830 836 if dropped.had_entry {
831 837 node.descendants_with_entry_count -= 1;
832 838 }
833 839 if dropped.was_tracked {
834 840 node.tracked_descendants_count -= 1;
835 841 }
836 842
837 843 // Directory caches must be invalidated when removing a
838 844 // child node
839 845 if removed {
840 846 if let NodeData::CachedDirectory { .. } = &node.data {
841 847 node.data = NodeData::None
842 848 }
843 849 }
844 850 } else {
845 851 return Ok(None);
846 852 }
847 853 } else {
848 854 let had_entry = node.data.has_entry();
849 855 if had_entry {
850 856 node.data = NodeData::None
851 857 }
852 858 if let Some(source) = &node.copy_source {
853 859 DirstateMap::count_dropped_path(unreachable_bytes, source);
854 860 node.copy_source = None
855 861 }
856 862 dropped = Dropped {
857 863 was_tracked: node
858 864 .data
859 865 .as_entry()
860 866 .map_or(false, |entry| entry.state().is_tracked()),
861 867 had_entry,
862 868 had_copy_source: node.copy_source.take().is_some(),
863 869 };
864 870 }
865 871 // After recursion, for both leaf (rest_of_path is None) nodes and
866 872 // parent nodes, remove a node if it just became empty.
867 873 let remove = !node.data.has_entry()
868 874 && node.copy_source.is_none()
869 875 && node.children.is_empty();
870 876 if remove {
871 877 let (key, _) =
872 878 nodes.remove_entry(first_path_component).unwrap();
873 879 DirstateMap::count_dropped_path(
874 880 unreachable_bytes,
875 881 key.full_path(),
876 882 )
877 883 }
878 884 Ok(Some((dropped, remove)))
879 885 }
880 886
881 887 if let Some((dropped, _removed)) = recur(
882 self.on_disk,
883 &mut self.unreachable_bytes,
884 &mut self.root,
888 map.on_disk,
889 &mut map.unreachable_bytes,
890 &mut map.root,
885 891 filename,
886 892 )? {
887 893 if dropped.had_entry {
888 self.nodes_with_entry_count -= 1
894 map.nodes_with_entry_count -= 1
889 895 }
890 896 if dropped.had_copy_source {
891 self.nodes_with_copy_source_count -= 1
897 map.nodes_with_copy_source_count -= 1
892 898 }
893 899 } else {
894 900 debug_assert!(!was_tracked);
895 901 }
896 902 Ok(())
897 903 }
898 904
899 fn has_tracked_dir(
905 pub fn has_tracked_dir(
900 906 &mut self,
901 907 directory: &HgPath,
902 908 ) -> Result<bool, DirstateError> {
903 if let Some(node) = self.get_node(directory)? {
909 let map = self.get_map_mut();
910 if let Some(node) = map.get_node(directory)? {
904 911 // A node without a `DirstateEntry` was created to hold child
905 912 // nodes, and is therefore a directory.
906 913 let state = node.state()?;
907 914 Ok(state.is_none() && node.tracked_descendants_count() > 0)
908 915 } else {
909 916 Ok(false)
910 917 }
911 918 }
912 919
913 fn has_dir(&mut self, directory: &HgPath) -> Result<bool, DirstateError> {
914 if let Some(node) = self.get_node(directory)? {
920 pub fn has_dir(
921 &mut self,
922 directory: &HgPath,
923 ) -> Result<bool, DirstateError> {
924 let map = self.get_map_mut();
925 if let Some(node) = map.get_node(directory)? {
915 926 // A node without a `DirstateEntry` was created to hold child
916 927 // nodes, and is therefore a directory.
917 928 let state = node.state()?;
918 929 Ok(state.is_none() && node.descendants_with_entry_count() > 0)
919 930 } else {
920 931 Ok(false)
921 932 }
922 933 }
923 934
924 935 #[timed]
925 fn pack_v1(
936 pub fn pack_v1(
926 937 &mut self,
927 938 parents: DirstateParents,
928 939 now: Timestamp,
929 940 ) -> Result<Vec<u8>, DirstateError> {
941 let map = self.get_map_mut();
930 942 let now: i32 = now.0.try_into().expect("time overflow");
931 943 let mut ambiguous_mtimes = Vec::new();
932 944 // Optizimation (to be measured?): pre-compute size to avoid `Vec`
933 945 // reallocations
934 946 let mut size = parents.as_bytes().len();
935 for node in self.iter_nodes() {
947 for node in map.iter_nodes() {
936 948 let node = node?;
937 949 if let Some(entry) = node.entry()? {
938 950 size += packed_entry_size(
939 node.full_path(self.on_disk)?,
940 node.copy_source(self.on_disk)?,
951 node.full_path(map.on_disk)?,
952 node.copy_source(map.on_disk)?,
941 953 );
942 954 if entry.mtime_is_ambiguous(now) {
943 955 ambiguous_mtimes.push(
944 node.full_path_borrowed(self.on_disk)?
956 node.full_path_borrowed(map.on_disk)?
945 957 .detach_from_tree(),
946 958 )
947 959 }
948 960 }
949 961 }
950 self.clear_known_ambiguous_mtimes(&ambiguous_mtimes)?;
962 map.clear_known_ambiguous_mtimes(&ambiguous_mtimes)?;
951 963
952 964 let mut packed = Vec::with_capacity(size);
953 965 packed.extend(parents.as_bytes());
954 966
955 for node in self.iter_nodes() {
967 for node in map.iter_nodes() {
956 968 let node = node?;
957 969 if let Some(entry) = node.entry()? {
958 970 pack_entry(
959 node.full_path(self.on_disk)?,
971 node.full_path(map.on_disk)?,
960 972 &entry,
961 node.copy_source(self.on_disk)?,
973 node.copy_source(map.on_disk)?,
962 974 &mut packed,
963 975 );
964 976 }
965 977 }
966 978 Ok(packed)
967 979 }
968 980
969 981 /// Returns new data and metadata together with whether that data should be
970 982 /// appended to the existing data file whose content is at
971 /// `self.on_disk` (true), instead of written to a new data file
983 /// `map.on_disk` (true), instead of written to a new data file
972 984 /// (false).
973 985 #[timed]
974 fn pack_v2(
986 pub fn pack_v2(
975 987 &mut self,
976 988 now: Timestamp,
977 989 can_append: bool,
978 990 ) -> Result<(Vec<u8>, Vec<u8>, bool), DirstateError> {
991 let map = self.get_map_mut();
979 992 // TODO: how do we want to handle this in 2038?
980 993 let now: i32 = now.0.try_into().expect("time overflow");
981 994 let mut paths = Vec::new();
982 for node in self.iter_nodes() {
995 for node in map.iter_nodes() {
983 996 let node = node?;
984 997 if let Some(entry) = node.entry()? {
985 998 if entry.mtime_is_ambiguous(now) {
986 999 paths.push(
987 node.full_path_borrowed(self.on_disk)?
1000 node.full_path_borrowed(map.on_disk)?
988 1001 .detach_from_tree(),
989 1002 )
990 1003 }
991 1004 }
992 1005 }
993 1006 // Borrow of `self` ends here since we collect cloned paths
994 1007
995 self.clear_known_ambiguous_mtimes(&paths)?;
1008 map.clear_known_ambiguous_mtimes(&paths)?;
996 1009
997 on_disk::write(self, can_append)
1010 on_disk::write(map, can_append)
998 1011 }
999 1012
1000 fn status<'a>(
1013 pub fn status<'a>(
1001 1014 &'a mut self,
1002 1015 matcher: &'a (dyn Matcher + Sync),
1003 1016 root_dir: PathBuf,
1004 1017 ignore_files: Vec<PathBuf>,
1005 1018 options: StatusOptions,
1006 1019 ) -> Result<(DirstateStatus<'a>, Vec<PatternFileWarning>), StatusError>
1007 1020 {
1008 super::status::status(self, matcher, root_dir, ignore_files, options)
1021 let map = self.get_map_mut();
1022 super::status::status(map, matcher, root_dir, ignore_files, options)
1009 1023 }
1010 1024
1011 fn copy_map_len(&self) -> usize {
1012 self.nodes_with_copy_source_count as usize
1025 pub fn copy_map_len(&self) -> usize {
1026 let map = self.get_map();
1027 map.nodes_with_copy_source_count as usize
1013 1028 }
1014 1029
1015 fn copy_map_iter(&self) -> CopyMapIter<'_> {
1016 Box::new(filter_map_results(self.iter_nodes(), move |node| {
1017 Ok(if let Some(source) = node.copy_source(self.on_disk)? {
1018 Some((node.full_path(self.on_disk)?, source))
1030 pub fn copy_map_iter(&self) -> CopyMapIter<'_> {
1031 let map = self.get_map();
1032 Box::new(filter_map_results(map.iter_nodes(), move |node| {
1033 Ok(if let Some(source) = node.copy_source(map.on_disk)? {
1034 Some((node.full_path(map.on_disk)?, source))
1019 1035 } else {
1020 1036 None
1021 1037 })
1022 1038 }))
1023 1039 }
1024 1040
1025 fn copy_map_contains_key(
1041 pub fn copy_map_contains_key(
1026 1042 &self,
1027 1043 key: &HgPath,
1028 1044 ) -> Result<bool, DirstateV2ParseError> {
1029 Ok(if let Some(node) = self.get_node(key)? {
1045 let map = self.get_map();
1046 Ok(if let Some(node) = map.get_node(key)? {
1030 1047 node.has_copy_source()
1031 1048 } else {
1032 1049 false
1033 1050 })
1034 1051 }
1035 1052
1036 fn copy_map_get(
1053 pub fn copy_map_get(
1037 1054 &self,
1038 1055 key: &HgPath,
1039 1056 ) -> Result<Option<&HgPath>, DirstateV2ParseError> {
1040 if let Some(node) = self.get_node(key)? {
1041 if let Some(source) = node.copy_source(self.on_disk)? {
1057 let map = self.get_map();
1058 if let Some(node) = map.get_node(key)? {
1059 if let Some(source) = node.copy_source(map.on_disk)? {
1042 1060 return Ok(Some(source));
1043 1061 }
1044 1062 }
1045 1063 Ok(None)
1046 1064 }
1047 1065
1048 fn copy_map_remove(
1066 pub fn copy_map_remove(
1049 1067 &mut self,
1050 1068 key: &HgPath,
1051 1069 ) -> Result<Option<HgPathBuf>, DirstateV2ParseError> {
1052 let count = &mut self.nodes_with_copy_source_count;
1053 let unreachable_bytes = &mut self.unreachable_bytes;
1054 Ok(Self::get_node_mut(
1055 self.on_disk,
1070 let map = self.get_map_mut();
1071 let count = &mut map.nodes_with_copy_source_count;
1072 let unreachable_bytes = &mut map.unreachable_bytes;
1073 Ok(DirstateMap::get_node_mut(
1074 map.on_disk,
1056 1075 unreachable_bytes,
1057 &mut self.root,
1076 &mut map.root,
1058 1077 key,
1059 1078 )?
1060 1079 .and_then(|node| {
1061 1080 if let Some(source) = &node.copy_source {
1062 1081 *count -= 1;
1063 Self::count_dropped_path(unreachable_bytes, source);
1082 DirstateMap::count_dropped_path(unreachable_bytes, source);
1064 1083 }
1065 1084 node.copy_source.take().map(Cow::into_owned)
1066 1085 }))
1067 1086 }
1068 1087
1069 fn copy_map_insert(
1088 pub fn copy_map_insert(
1070 1089 &mut self,
1071 1090 key: HgPathBuf,
1072 1091 value: HgPathBuf,
1073 1092 ) -> Result<Option<HgPathBuf>, DirstateV2ParseError> {
1074 let node = Self::get_or_insert_node(
1075 self.on_disk,
1076 &mut self.unreachable_bytes,
1077 &mut self.root,
1093 let map = self.get_map_mut();
1094 let node = DirstateMap::get_or_insert_node(
1095 map.on_disk,
1096 &mut map.unreachable_bytes,
1097 &mut map.root,
1078 1098 &key,
1079 1099 WithBasename::to_cow_owned,
1080 1100 |_ancestor| {},
1081 1101 )?;
1082 1102 if node.copy_source.is_none() {
1083 self.nodes_with_copy_source_count += 1
1103 map.nodes_with_copy_source_count += 1
1084 1104 }
1085 1105 Ok(node.copy_source.replace(value.into()).map(Cow::into_owned))
1086 1106 }
1087 1107
1088 fn len(&self) -> usize {
1089 self.nodes_with_entry_count as usize
1108 pub fn len(&self) -> usize {
1109 let map = self.get_map();
1110 map.nodes_with_entry_count as usize
1090 1111 }
1091 1112
1092 fn contains_key(
1113 pub fn contains_key(
1093 1114 &self,
1094 1115 key: &HgPath,
1095 1116 ) -> Result<bool, DirstateV2ParseError> {
1096 1117 Ok(self.get(key)?.is_some())
1097 1118 }
1098 1119
1099 fn get(
1120 pub fn get(
1100 1121 &self,
1101 1122 key: &HgPath,
1102 1123 ) -> Result<Option<DirstateEntry>, DirstateV2ParseError> {
1103 Ok(if let Some(node) = self.get_node(key)? {
1124 let map = self.get_map();
1125 Ok(if let Some(node) = map.get_node(key)? {
1104 1126 node.entry()?
1105 1127 } else {
1106 1128 None
1107 1129 })
1108 1130 }
1109 1131
1110 fn iter(&self) -> StateMapIter<'_> {
1111 Box::new(filter_map_results(self.iter_nodes(), move |node| {
1132 pub fn iter(&self) -> StateMapIter<'_> {
1133 let map = self.get_map();
1134 Box::new(filter_map_results(map.iter_nodes(), move |node| {
1112 1135 Ok(if let Some(entry) = node.entry()? {
1113 Some((node.full_path(self.on_disk)?, entry))
1136 Some((node.full_path(map.on_disk)?, entry))
1114 1137 } else {
1115 1138 None
1116 1139 })
1117 1140 }))
1118 1141 }
1119 1142
1120 fn iter_tracked_dirs(
1143 pub fn iter_tracked_dirs(
1121 1144 &mut self,
1122 1145 ) -> Result<
1123 1146 Box<
1124 1147 dyn Iterator<Item = Result<&HgPath, DirstateV2ParseError>>
1125 1148 + Send
1126 1149 + '_,
1127 1150 >,
1128 1151 DirstateError,
1129 1152 > {
1130 let on_disk = self.on_disk;
1153 let map = self.get_map_mut();
1154 let on_disk = map.on_disk;
1131 1155 Ok(Box::new(filter_map_results(
1132 self.iter_nodes(),
1156 map.iter_nodes(),
1133 1157 move |node| {
1134 1158 Ok(if node.tracked_descendants_count() > 0 {
1135 1159 Some(node.full_path(on_disk)?)
1136 1160 } else {
1137 1161 None
1138 1162 })
1139 1163 },
1140 1164 )))
1141 1165 }
1142 1166
1143 fn debug_iter(
1167 pub fn debug_iter(
1144 1168 &self,
1145 1169 all: bool,
1146 1170 ) -> Box<
1147 1171 dyn Iterator<
1148 1172 Item = Result<
1149 1173 (&HgPath, (u8, i32, i32, i32)),
1150 1174 DirstateV2ParseError,
1151 1175 >,
1152 1176 > + Send
1153 1177 + '_,
1154 1178 > {
1155 Box::new(filter_map_results(self.iter_nodes(), move |node| {
1179 let map = self.get_map();
1180 Box::new(filter_map_results(map.iter_nodes(), move |node| {
1156 1181 let debug_tuple = if let Some(entry) = node.entry()? {
1157 1182 entry.debug_tuple()
1158 1183 } else if !all {
1159 1184 return Ok(None);
1160 1185 } else if let Some(mtime) = node.cached_directory_mtime() {
1161 1186 (b' ', 0, -1, mtime.seconds() as i32)
1162 1187 } else {
1163 1188 (b' ', 0, -1, -1)
1164 1189 };
1165 Ok(Some((node.full_path(self.on_disk)?, debug_tuple)))
1190 Ok(Some((node.full_path(map.on_disk)?, debug_tuple)))
1166 1191 }))
1167 1192 }
1168 1193 }
@@ -1,105 +1,105 b''
1 1 use super::dirstate_map::DirstateMap;
2 2 use stable_deref_trait::StableDeref;
3 3 use std::ops::Deref;
4 4
5 5 /// Keep a `DirstateMap<'on_disk>` next to the `on_disk` buffer that it
6 6 /// borrows.
7 7 ///
8 8 /// This is similar to [`OwningRef`] which is more limited because it
9 9 /// represents exactly one `&T` reference next to the value it borrows, as
10 10 /// opposed to a struct that may contain an arbitrary number of references in
11 11 /// arbitrarily-nested data structures.
12 12 ///
13 13 /// [`OwningRef`]: https://docs.rs/owning_ref/0.4.1/owning_ref/struct.OwningRef.html
14 14 pub struct OwningDirstateMap {
15 15 /// Owned handle to a bytes buffer with a stable address.
16 16 ///
17 17 /// See <https://docs.rs/owning_ref/0.4.1/owning_ref/trait.StableAddress.html>.
18 18 on_disk: Box<dyn Deref<Target = [u8]> + Send>,
19 19
20 20 /// Pointer for `Box<DirstateMap<'on_disk>>`, typed-erased because the
21 21 /// language cannot represent a lifetime referencing a sibling field.
22 22 /// This is not quite a self-referencial struct (moving this struct is not
23 23 /// a problem as it doesn’t change the address of the bytes buffer owned
24 24 /// by `PyBytes`) but touches similar borrow-checker limitations.
25 25 ptr: *mut (),
26 26 }
27 27
28 28 impl OwningDirstateMap {
29 29 pub fn new_empty<OnDisk>(on_disk: OnDisk) -> Self
30 30 where
31 31 OnDisk: Deref<Target = [u8]> + StableDeref + Send + 'static,
32 32 {
33 33 let on_disk = Box::new(on_disk);
34 34 let bytes: &'_ [u8] = &on_disk;
35 35 let map = DirstateMap::empty(bytes);
36 36
37 37 // Like in `bytes` above, this `'_` lifetime parameter borrows from
38 38 // the bytes buffer owned by `on_disk`.
39 39 let ptr: *mut DirstateMap<'_> = Box::into_raw(Box::new(map));
40 40
41 41 // Erase the pointed type entirely in order to erase the lifetime.
42 42 let ptr: *mut () = ptr.cast();
43 43
44 44 Self { on_disk, ptr }
45 45 }
46 46
47 pub fn get_mut_pair<'a>(
47 pub fn get_pair_mut<'a>(
48 48 &'a mut self,
49 49 ) -> (&'a [u8], &'a mut DirstateMap<'a>) {
50 50 // SAFETY: We cast the type-erased pointer back to the same type it had
51 51 // in `new`, except with a different lifetime parameter. This time we
52 52 // connect the lifetime to that of `self`. This cast is valid because
53 53 // `self` owns the same `PyBytes` whose buffer `DirstateMap`
54 54 // references. That buffer has a stable memory address because the byte
55 55 // string value of a `PyBytes` is immutable.
56 56 let ptr: *mut DirstateMap<'a> = self.ptr.cast();
57 57 // SAFETY: we dereference that pointer, connecting the lifetime of the
58 58 // new `&mut` to that of `self`. This is valid because the
59 59 // raw pointer is to a boxed value, and `self` owns that box.
60 60 (&self.on_disk, unsafe { &mut *ptr })
61 61 }
62 62
63 pub fn get_mut<'a>(&'a mut self) -> &'a mut DirstateMap<'a> {
64 self.get_mut_pair().1
63 pub fn get_map_mut<'a>(&'a mut self) -> &'a mut DirstateMap<'a> {
64 self.get_pair_mut().1
65 65 }
66 66
67 pub fn get<'a>(&'a self) -> &'a DirstateMap<'a> {
67 pub fn get_map<'a>(&'a self) -> &'a DirstateMap<'a> {
68 68 // SAFETY: same reasoning as in `get_mut` above.
69 69 let ptr: *mut DirstateMap<'a> = self.ptr.cast();
70 70 unsafe { &*ptr }
71 71 }
72 72
73 73 pub fn on_disk<'a>(&'a self) -> &'a [u8] {
74 74 &self.on_disk
75 75 }
76 76 }
77 77
78 78 impl Drop for OwningDirstateMap {
79 79 fn drop(&mut self) {
80 80 // Silence a "field is never read" warning, and demonstrate that this
81 81 // value is still alive.
82 82 let _ = &self.on_disk;
83 83 // SAFETY: this cast is the same as in `get_mut`, and is valid for the
84 84 // same reason. `self.on_disk` still exists at this point, drop glue
85 85 // will drop it implicitly after this `drop` method returns.
86 86 let ptr: *mut DirstateMap<'_> = self.ptr.cast();
87 87 // SAFETY: `Box::from_raw` takes ownership of the box away from `self`.
88 88 // This is fine because drop glue does nothig for `*mut ()` and we’re
89 89 // in `drop`, so `get` and `get_mut` cannot be called again.
90 90 unsafe { drop(Box::from_raw(ptr)) }
91 91 }
92 92 }
93 93
94 94 fn _static_assert_is_send<T: Send>() {}
95 95
96 96 fn _static_assert_fields_are_send() {
97 97 _static_assert_is_send::<Box<DirstateMap<'_>>>();
98 98 }
99 99
100 100 // SAFETY: we don’t get this impl implicitly because `*mut (): !Send` because
101 101 // thread-safety of raw pointers is unknown in the general case. However this
102 102 // particular raw pointer represents a `Box<DirstateMap<'on_disk>>` that we
103 103 // own. Since that `Box` is `Send` as shown in above, it is sound to mark
104 104 // this struct as `Send` too.
105 105 unsafe impl Send for OwningDirstateMap {}
@@ -1,409 +1,409 b''
1 1 use crate::changelog::Changelog;
2 2 use crate::config::{Config, ConfigError, ConfigParseError};
3 3 use crate::dirstate::DirstateParents;
4 4 use crate::dirstate_tree::dirstate_map::DirstateMap;
5 5 use crate::dirstate_tree::owning::OwningDirstateMap;
6 6 use crate::errors::HgError;
7 7 use crate::errors::HgResultExt;
8 8 use crate::exit_codes;
9 9 use crate::manifest::{Manifest, Manifestlog};
10 10 use crate::revlog::filelog::Filelog;
11 11 use crate::revlog::revlog::RevlogError;
12 12 use crate::utils::files::get_path_from_bytes;
13 13 use crate::utils::hg_path::HgPath;
14 14 use crate::utils::SliceExt;
15 15 use crate::vfs::{is_dir, is_file, Vfs};
16 16 use crate::{requirements, NodePrefix};
17 17 use crate::{DirstateError, Revision};
18 18 use std::cell::{Cell, Ref, RefCell, RefMut};
19 19 use std::collections::HashSet;
20 20 use std::path::{Path, PathBuf};
21 21
22 22 /// A repository on disk
23 23 pub struct Repo {
24 24 working_directory: PathBuf,
25 25 dot_hg: PathBuf,
26 26 store: PathBuf,
27 27 requirements: HashSet<String>,
28 28 config: Config,
29 29 // None means not known/initialized yet
30 30 dirstate_parents: Cell<Option<DirstateParents>>,
31 31 dirstate_map: LazyCell<OwningDirstateMap, DirstateError>,
32 32 changelog: LazyCell<Changelog, HgError>,
33 33 manifestlog: LazyCell<Manifestlog, HgError>,
34 34 }
35 35
36 36 #[derive(Debug, derive_more::From)]
37 37 pub enum RepoError {
38 38 NotFound {
39 39 at: PathBuf,
40 40 },
41 41 #[from]
42 42 ConfigParseError(ConfigParseError),
43 43 #[from]
44 44 Other(HgError),
45 45 }
46 46
47 47 impl From<ConfigError> for RepoError {
48 48 fn from(error: ConfigError) -> Self {
49 49 match error {
50 50 ConfigError::Parse(error) => error.into(),
51 51 ConfigError::Other(error) => error.into(),
52 52 }
53 53 }
54 54 }
55 55
56 56 impl Repo {
57 57 /// tries to find nearest repository root in current working directory or
58 58 /// its ancestors
59 59 pub fn find_repo_root() -> Result<PathBuf, RepoError> {
60 60 let current_directory = crate::utils::current_dir()?;
61 61 // ancestors() is inclusive: it first yields `current_directory`
62 62 // as-is.
63 63 for ancestor in current_directory.ancestors() {
64 64 if is_dir(ancestor.join(".hg"))? {
65 65 return Ok(ancestor.to_path_buf());
66 66 }
67 67 }
68 68 return Err(RepoError::NotFound {
69 69 at: current_directory,
70 70 });
71 71 }
72 72
73 73 /// Find a repository, either at the given path (which must contain a `.hg`
74 74 /// sub-directory) or by searching the current directory and its
75 75 /// ancestors.
76 76 ///
77 77 /// A method with two very different "modes" like this usually a code smell
78 78 /// to make two methods instead, but in this case an `Option` is what rhg
79 79 /// sub-commands get from Clap for the `-R` / `--repository` CLI argument.
80 80 /// Having two methods would just move that `if` to almost all callers.
81 81 pub fn find(
82 82 config: &Config,
83 83 explicit_path: Option<PathBuf>,
84 84 ) -> Result<Self, RepoError> {
85 85 if let Some(root) = explicit_path {
86 86 if is_dir(root.join(".hg"))? {
87 87 Self::new_at_path(root.to_owned(), config)
88 88 } else if is_file(&root)? {
89 89 Err(HgError::unsupported("bundle repository").into())
90 90 } else {
91 91 Err(RepoError::NotFound {
92 92 at: root.to_owned(),
93 93 })
94 94 }
95 95 } else {
96 96 let root = Self::find_repo_root()?;
97 97 Self::new_at_path(root, config)
98 98 }
99 99 }
100 100
101 101 /// To be called after checking that `.hg` is a sub-directory
102 102 fn new_at_path(
103 103 working_directory: PathBuf,
104 104 config: &Config,
105 105 ) -> Result<Self, RepoError> {
106 106 let dot_hg = working_directory.join(".hg");
107 107
108 108 let mut repo_config_files = Vec::new();
109 109 repo_config_files.push(dot_hg.join("hgrc"));
110 110 repo_config_files.push(dot_hg.join("hgrc-not-shared"));
111 111
112 112 let hg_vfs = Vfs { base: &dot_hg };
113 113 let mut reqs = requirements::load_if_exists(hg_vfs)?;
114 114 let relative =
115 115 reqs.contains(requirements::RELATIVE_SHARED_REQUIREMENT);
116 116 let shared =
117 117 reqs.contains(requirements::SHARED_REQUIREMENT) || relative;
118 118
119 119 // From `mercurial/localrepo.py`:
120 120 //
121 121 // if .hg/requires contains the sharesafe requirement, it means
122 122 // there exists a `.hg/store/requires` too and we should read it
123 123 // NOTE: presence of SHARESAFE_REQUIREMENT imply that store requirement
124 124 // is present. We never write SHARESAFE_REQUIREMENT for a repo if store
125 125 // is not present, refer checkrequirementscompat() for that
126 126 //
127 127 // However, if SHARESAFE_REQUIREMENT is not present, it means that the
128 128 // repository was shared the old way. We check the share source
129 129 // .hg/requires for SHARESAFE_REQUIREMENT to detect whether the
130 130 // current repository needs to be reshared
131 131 let share_safe = reqs.contains(requirements::SHARESAFE_REQUIREMENT);
132 132
133 133 let store_path;
134 134 if !shared {
135 135 store_path = dot_hg.join("store");
136 136 } else {
137 137 let bytes = hg_vfs.read("sharedpath")?;
138 138 let mut shared_path =
139 139 get_path_from_bytes(bytes.trim_end_matches(|b| b == b'\n'))
140 140 .to_owned();
141 141 if relative {
142 142 shared_path = dot_hg.join(shared_path)
143 143 }
144 144 if !is_dir(&shared_path)? {
145 145 return Err(HgError::corrupted(format!(
146 146 ".hg/sharedpath points to nonexistent directory {}",
147 147 shared_path.display()
148 148 ))
149 149 .into());
150 150 }
151 151
152 152 store_path = shared_path.join("store");
153 153
154 154 let source_is_share_safe =
155 155 requirements::load(Vfs { base: &shared_path })?
156 156 .contains(requirements::SHARESAFE_REQUIREMENT);
157 157
158 158 if share_safe && !source_is_share_safe {
159 159 return Err(match config
160 160 .get(b"share", b"safe-mismatch.source-not-safe")
161 161 {
162 162 Some(b"abort") | None => HgError::abort(
163 163 "abort: share source does not support share-safe requirement\n\
164 164 (see `hg help config.format.use-share-safe` for more information)",
165 165 exit_codes::ABORT,
166 166 ),
167 167 _ => HgError::unsupported("share-safe downgrade"),
168 168 }
169 169 .into());
170 170 } else if source_is_share_safe && !share_safe {
171 171 return Err(
172 172 match config.get(b"share", b"safe-mismatch.source-safe") {
173 173 Some(b"abort") | None => HgError::abort(
174 174 "abort: version mismatch: source uses share-safe \
175 175 functionality while the current share does not\n\
176 176 (see `hg help config.format.use-share-safe` for more information)",
177 177 exit_codes::ABORT,
178 178 ),
179 179 _ => HgError::unsupported("share-safe upgrade"),
180 180 }
181 181 .into(),
182 182 );
183 183 }
184 184
185 185 if share_safe {
186 186 repo_config_files.insert(0, shared_path.join("hgrc"))
187 187 }
188 188 }
189 189 if share_safe {
190 190 reqs.extend(requirements::load(Vfs { base: &store_path })?);
191 191 }
192 192
193 193 let repo_config = if std::env::var_os("HGRCSKIPREPO").is_none() {
194 194 config.combine_with_repo(&repo_config_files)?
195 195 } else {
196 196 config.clone()
197 197 };
198 198
199 199 let repo = Self {
200 200 requirements: reqs,
201 201 working_directory,
202 202 store: store_path,
203 203 dot_hg,
204 204 config: repo_config,
205 205 dirstate_parents: Cell::new(None),
206 206 dirstate_map: LazyCell::new(Self::new_dirstate_map),
207 207 changelog: LazyCell::new(Changelog::open),
208 208 manifestlog: LazyCell::new(Manifestlog::open),
209 209 };
210 210
211 211 requirements::check(&repo)?;
212 212
213 213 Ok(repo)
214 214 }
215 215
216 216 pub fn working_directory_path(&self) -> &Path {
217 217 &self.working_directory
218 218 }
219 219
220 220 pub fn requirements(&self) -> &HashSet<String> {
221 221 &self.requirements
222 222 }
223 223
224 224 pub fn config(&self) -> &Config {
225 225 &self.config
226 226 }
227 227
228 228 /// For accessing repository files (in `.hg`), except for the store
229 229 /// (`.hg/store`).
230 230 pub fn hg_vfs(&self) -> Vfs<'_> {
231 231 Vfs { base: &self.dot_hg }
232 232 }
233 233
234 234 /// For accessing repository store files (in `.hg/store`)
235 235 pub fn store_vfs(&self) -> Vfs<'_> {
236 236 Vfs { base: &self.store }
237 237 }
238 238
239 239 /// For accessing the working copy
240 240 pub fn working_directory_vfs(&self) -> Vfs<'_> {
241 241 Vfs {
242 242 base: &self.working_directory,
243 243 }
244 244 }
245 245
246 246 pub fn has_dirstate_v2(&self) -> bool {
247 247 self.requirements
248 248 .contains(requirements::DIRSTATE_V2_REQUIREMENT)
249 249 }
250 250
251 251 fn dirstate_file_contents(&self) -> Result<Vec<u8>, HgError> {
252 252 Ok(self
253 253 .hg_vfs()
254 254 .read("dirstate")
255 255 .io_not_found_as_none()?
256 256 .unwrap_or(Vec::new()))
257 257 }
258 258
259 259 pub fn dirstate_parents(&self) -> Result<DirstateParents, HgError> {
260 260 if let Some(parents) = self.dirstate_parents.get() {
261 261 return Ok(parents);
262 262 }
263 263 let dirstate = self.dirstate_file_contents()?;
264 264 let parents = if dirstate.is_empty() {
265 265 DirstateParents::NULL
266 266 } else if self.has_dirstate_v2() {
267 267 crate::dirstate_tree::on_disk::read_docket(&dirstate)?.parents()
268 268 } else {
269 269 crate::dirstate::parsers::parse_dirstate_parents(&dirstate)?
270 270 .clone()
271 271 };
272 272 self.dirstate_parents.set(Some(parents));
273 273 Ok(parents)
274 274 }
275 275
276 276 fn new_dirstate_map(&self) -> Result<OwningDirstateMap, DirstateError> {
277 277 let dirstate_file_contents = self.dirstate_file_contents()?;
278 278 if dirstate_file_contents.is_empty() {
279 279 self.dirstate_parents.set(Some(DirstateParents::NULL));
280 280 Ok(OwningDirstateMap::new_empty(Vec::new()))
281 281 } else if self.has_dirstate_v2() {
282 282 let docket = crate::dirstate_tree::on_disk::read_docket(
283 283 &dirstate_file_contents,
284 284 )?;
285 285 self.dirstate_parents.set(Some(docket.parents()));
286 286 let data_size = docket.data_size();
287 287 let metadata = docket.tree_metadata();
288 288 let mut map = if let Some(data_mmap) = self
289 289 .hg_vfs()
290 290 .mmap_open(docket.data_filename())
291 291 .io_not_found_as_none()?
292 292 {
293 293 OwningDirstateMap::new_empty(data_mmap)
294 294 } else {
295 295 OwningDirstateMap::new_empty(Vec::new())
296 296 };
297 let (on_disk, placeholder) = map.get_mut_pair();
297 let (on_disk, placeholder) = map.get_pair_mut();
298 298 *placeholder = DirstateMap::new_v2(on_disk, data_size, metadata)?;
299 299 Ok(map)
300 300 } else {
301 301 let mut map = OwningDirstateMap::new_empty(dirstate_file_contents);
302 let (on_disk, placeholder) = map.get_mut_pair();
302 let (on_disk, placeholder) = map.get_pair_mut();
303 303 let (inner, parents) = DirstateMap::new_v1(on_disk)?;
304 304 self.dirstate_parents
305 305 .set(Some(parents.unwrap_or(DirstateParents::NULL)));
306 306 *placeholder = inner;
307 307 Ok(map)
308 308 }
309 309 }
310 310
311 311 pub fn dirstate_map(
312 312 &self,
313 313 ) -> Result<Ref<OwningDirstateMap>, DirstateError> {
314 314 self.dirstate_map.get_or_init(self)
315 315 }
316 316
317 317 pub fn dirstate_map_mut(
318 318 &self,
319 319 ) -> Result<RefMut<OwningDirstateMap>, DirstateError> {
320 320 self.dirstate_map.get_mut_or_init(self)
321 321 }
322 322
323 323 pub fn changelog(&self) -> Result<Ref<Changelog>, HgError> {
324 324 self.changelog.get_or_init(self)
325 325 }
326 326
327 327 pub fn changelog_mut(&self) -> Result<RefMut<Changelog>, HgError> {
328 328 self.changelog.get_mut_or_init(self)
329 329 }
330 330
331 331 pub fn manifestlog(&self) -> Result<Ref<Manifestlog>, HgError> {
332 332 self.manifestlog.get_or_init(self)
333 333 }
334 334
335 335 pub fn manifestlog_mut(&self) -> Result<RefMut<Manifestlog>, HgError> {
336 336 self.manifestlog.get_mut_or_init(self)
337 337 }
338 338
339 339 /// Returns the manifest of the *changeset* with the given node ID
340 340 pub fn manifest_for_node(
341 341 &self,
342 342 node: impl Into<NodePrefix>,
343 343 ) -> Result<Manifest, RevlogError> {
344 344 self.manifestlog()?.data_for_node(
345 345 self.changelog()?
346 346 .data_for_node(node.into())?
347 347 .manifest_node()?
348 348 .into(),
349 349 )
350 350 }
351 351
352 352 /// Returns the manifest of the *changeset* with the given revision number
353 353 pub fn manifest_for_rev(
354 354 &self,
355 355 revision: Revision,
356 356 ) -> Result<Manifest, RevlogError> {
357 357 self.manifestlog()?.data_for_node(
358 358 self.changelog()?
359 359 .data_for_rev(revision)?
360 360 .manifest_node()?
361 361 .into(),
362 362 )
363 363 }
364 364
365 365 pub fn filelog(&self, path: &HgPath) -> Result<Filelog, HgError> {
366 366 Filelog::open(self, path)
367 367 }
368 368 }
369 369
370 370 /// Lazily-initialized component of `Repo` with interior mutability
371 371 ///
372 372 /// This differs from `OnceCell` in that the value can still be "deinitialized"
373 373 /// later by setting its inner `Option` to `None`.
374 374 struct LazyCell<T, E> {
375 375 value: RefCell<Option<T>>,
376 376 // `Fn`s that don’t capture environment are zero-size, so this box does
377 377 // not allocate:
378 378 init: Box<dyn Fn(&Repo) -> Result<T, E>>,
379 379 }
380 380
381 381 impl<T, E> LazyCell<T, E> {
382 382 fn new(init: impl Fn(&Repo) -> Result<T, E> + 'static) -> Self {
383 383 Self {
384 384 value: RefCell::new(None),
385 385 init: Box::new(init),
386 386 }
387 387 }
388 388
389 389 fn get_or_init(&self, repo: &Repo) -> Result<Ref<T>, E> {
390 390 let mut borrowed = self.value.borrow();
391 391 if borrowed.is_none() {
392 392 drop(borrowed);
393 393 // Only use `borrow_mut` if it is really needed to avoid panic in
394 394 // case there is another outstanding borrow but mutation is not
395 395 // needed.
396 396 *self.value.borrow_mut() = Some((self.init)(repo)?);
397 397 borrowed = self.value.borrow()
398 398 }
399 399 Ok(Ref::map(borrowed, |option| option.as_ref().unwrap()))
400 400 }
401 401
402 402 pub fn get_mut_or_init(&self, repo: &Repo) -> Result<RefMut<T>, E> {
403 403 let mut borrowed = self.value.borrow_mut();
404 404 if borrowed.is_none() {
405 405 *borrowed = Some((self.init)(repo)?);
406 406 }
407 407 Ok(RefMut::map(borrowed, |option| option.as_mut().unwrap()))
408 408 }
409 409 }
@@ -1,505 +1,504 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 //! Bindings for the `hg::dirstate::dirstate_map` file provided by the
9 9 //! `hg-core` package.
10 10
11 11 use std::cell::{RefCell, RefMut};
12 12 use std::convert::TryInto;
13 13
14 14 use cpython::{
15 15 exc, PyBool, PyBytes, PyClone, PyDict, PyErr, PyList, PyNone, PyObject,
16 16 PyResult, Python, PythonObject, ToPyObject, UnsafePyLeaked,
17 17 };
18 18
19 19 use crate::{
20 20 dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator},
21 21 dirstate::item::DirstateItem,
22 22 pybytes_deref::PyBytesDeref,
23 23 };
24 24 use hg::{
25 25 dirstate::parsers::Timestamp,
26 26 dirstate::StateMapIter,
27 27 dirstate_tree::dirstate_map::DirstateMap as TreeDirstateMap,
28 dirstate_tree::dispatch::DirstateMapMethods,
29 28 dirstate_tree::on_disk::DirstateV2ParseError,
30 29 dirstate_tree::owning::OwningDirstateMap,
31 30 revlog::Node,
32 31 utils::files::normalize_case,
33 32 utils::hg_path::{HgPath, HgPathBuf},
34 33 DirstateEntry, DirstateError, DirstateParents, EntryState,
35 34 };
36 35
37 36 // TODO
38 37 // This object needs to share references to multiple members of its Rust
39 38 // inner struct, namely `copy_map`, `dirs` and `all_dirs`.
40 39 // Right now `CopyMap` is done, but it needs to have an explicit reference
41 40 // to `RustDirstateMap` which itself needs to have an encapsulation for
42 41 // every method in `CopyMap` (copymapcopy, etc.).
43 42 // This is ugly and hard to maintain.
44 43 // The same logic applies to `dirs` and `all_dirs`, however the `Dirs`
45 44 // `py_class!` is already implemented and does not mention
46 45 // `RustDirstateMap`, rightfully so.
47 46 // All attributes also have to have a separate refcount data attribute for
48 47 // leaks, with all methods that go along for reference sharing.
49 48 py_class!(pub class DirstateMap |py| {
50 @shared data inner: Box<dyn DirstateMapMethods + Send>;
49 @shared data inner: OwningDirstateMap;
51 50
52 51 /// Returns a `(dirstate_map, parents)` tuple
53 52 @staticmethod
54 53 def new_v1(
55 54 on_disk: PyBytes,
56 55 ) -> PyResult<PyObject> {
57 56 let on_disk = PyBytesDeref::new(py, on_disk);
58 57 let mut map = OwningDirstateMap::new_empty(on_disk);
59 let (on_disk, map_placeholder) = map.get_mut_pair();
58 let (on_disk, map_placeholder) = map.get_pair_mut();
60 59
61 60 let (actual_map, parents) = TreeDirstateMap::new_v1(on_disk)
62 61 .map_err(|e| dirstate_error(py, e))?;
63 62 *map_placeholder = actual_map;
64 let map = Self::create_instance(py, Box::new(map))?;
63 let map = Self::create_instance(py, map)?;
65 64 let parents = parents.map(|p| {
66 65 let p1 = PyBytes::new(py, p.p1.as_bytes());
67 66 let p2 = PyBytes::new(py, p.p2.as_bytes());
68 67 (p1, p2)
69 68 });
70 69 Ok((map, parents).to_py_object(py).into_object())
71 70 }
72 71
73 72 /// Returns a DirstateMap
74 73 @staticmethod
75 74 def new_v2(
76 75 on_disk: PyBytes,
77 76 data_size: usize,
78 77 tree_metadata: PyBytes,
79 78 ) -> PyResult<PyObject> {
80 79 let dirstate_error = |e: DirstateError| {
81 80 PyErr::new::<exc::OSError, _>(py, format!("Dirstate error: {:?}", e))
82 81 };
83 82 let on_disk = PyBytesDeref::new(py, on_disk);
84 83 let mut map = OwningDirstateMap::new_empty(on_disk);
85 let (on_disk, map_placeholder) = map.get_mut_pair();
84 let (on_disk, map_placeholder) = map.get_pair_mut();
86 85 *map_placeholder = TreeDirstateMap::new_v2(
87 86 on_disk, data_size, tree_metadata.data(py),
88 87 ).map_err(dirstate_error)?;
89 let map = Self::create_instance(py, Box::new(map))?;
88 let map = Self::create_instance(py, map)?;
90 89 Ok(map.into_object())
91 90 }
92 91
93 92 def clear(&self) -> PyResult<PyObject> {
94 93 self.inner(py).borrow_mut().clear();
95 94 Ok(py.None())
96 95 }
97 96
98 97 def get(
99 98 &self,
100 99 key: PyObject,
101 100 default: Option<PyObject> = None
102 101 ) -> PyResult<Option<PyObject>> {
103 102 let key = key.extract::<PyBytes>(py)?;
104 103 match self
105 104 .inner(py)
106 105 .borrow()
107 106 .get(HgPath::new(key.data(py)))
108 107 .map_err(|e| v2_error(py, e))?
109 108 {
110 109 Some(entry) => {
111 110 Ok(Some(DirstateItem::new_as_pyobject(py, entry)?))
112 111 },
113 112 None => Ok(default)
114 113 }
115 114 }
116 115
117 116 def set_dirstate_item(
118 117 &self,
119 118 path: PyObject,
120 119 item: DirstateItem
121 120 ) -> PyResult<PyObject> {
122 121 let f = path.extract::<PyBytes>(py)?;
123 122 let filename = HgPath::new(f.data(py));
124 123 self.inner(py)
125 124 .borrow_mut()
126 125 .set_entry(filename, item.get_entry(py))
127 126 .map_err(|e| v2_error(py, e))?;
128 127 Ok(py.None())
129 128 }
130 129
131 130 def addfile(
132 131 &self,
133 132 f: PyBytes,
134 133 item: DirstateItem,
135 134 ) -> PyResult<PyNone> {
136 135 let filename = HgPath::new(f.data(py));
137 136 let entry = item.get_entry(py);
138 137 self.inner(py)
139 138 .borrow_mut()
140 139 .add_file(filename, entry)
141 140 .map_err(|e |dirstate_error(py, e))?;
142 141 Ok(PyNone)
143 142 }
144 143
145 144 def removefile(
146 145 &self,
147 146 f: PyObject,
148 147 in_merge: PyObject
149 148 ) -> PyResult<PyObject> {
150 149 self.inner(py).borrow_mut()
151 150 .remove_file(
152 151 HgPath::new(f.extract::<PyBytes>(py)?.data(py)),
153 152 in_merge.extract::<PyBool>(py)?.is_true(),
154 153 )
155 154 .or_else(|_| {
156 155 Err(PyErr::new::<exc::OSError, _>(
157 156 py,
158 157 "Dirstate error".to_string(),
159 158 ))
160 159 })?;
161 160 Ok(py.None())
162 161 }
163 162
164 163 def drop_item_and_copy_source(
165 164 &self,
166 165 f: PyBytes,
167 166 ) -> PyResult<PyNone> {
168 167 self.inner(py)
169 168 .borrow_mut()
170 169 .drop_entry_and_copy_source(HgPath::new(f.data(py)))
171 170 .map_err(|e |dirstate_error(py, e))?;
172 171 Ok(PyNone)
173 172 }
174 173
175 174 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> {
176 175 let d = d.extract::<PyBytes>(py)?;
177 176 Ok(self.inner(py).borrow_mut()
178 177 .has_tracked_dir(HgPath::new(d.data(py)))
179 178 .map_err(|e| {
180 179 PyErr::new::<exc::ValueError, _>(py, e.to_string())
181 180 })?
182 181 .to_py_object(py))
183 182 }
184 183
185 184 def hasdir(&self, d: PyObject) -> PyResult<PyBool> {
186 185 let d = d.extract::<PyBytes>(py)?;
187 186 Ok(self.inner(py).borrow_mut()
188 187 .has_dir(HgPath::new(d.data(py)))
189 188 .map_err(|e| {
190 189 PyErr::new::<exc::ValueError, _>(py, e.to_string())
191 190 })?
192 191 .to_py_object(py))
193 192 }
194 193
195 194 def write_v1(
196 195 &self,
197 196 p1: PyObject,
198 197 p2: PyObject,
199 198 now: PyObject
200 199 ) -> PyResult<PyBytes> {
201 200 let now = Timestamp(now.extract(py)?);
202 201
203 202 let mut inner = self.inner(py).borrow_mut();
204 203 let parents = DirstateParents {
205 204 p1: extract_node_id(py, &p1)?,
206 205 p2: extract_node_id(py, &p2)?,
207 206 };
208 207 let result = inner.pack_v1(parents, now);
209 208 match result {
210 209 Ok(packed) => Ok(PyBytes::new(py, &packed)),
211 210 Err(_) => Err(PyErr::new::<exc::OSError, _>(
212 211 py,
213 212 "Dirstate error".to_string(),
214 213 )),
215 214 }
216 215 }
217 216
218 217 /// Returns new data together with whether that data should be appended to
219 218 /// the existing data file whose content is at `self.on_disk` (True),
220 219 /// instead of written to a new data file (False).
221 220 def write_v2(
222 221 &self,
223 222 now: PyObject,
224 223 can_append: bool,
225 224 ) -> PyResult<PyObject> {
226 225 let now = Timestamp(now.extract(py)?);
227 226
228 227 let mut inner = self.inner(py).borrow_mut();
229 228 let result = inner.pack_v2(now, can_append);
230 229 match result {
231 230 Ok((packed, tree_metadata, append)) => {
232 231 let packed = PyBytes::new(py, &packed);
233 232 let tree_metadata = PyBytes::new(py, &tree_metadata);
234 233 let tuple = (packed, tree_metadata, append);
235 234 Ok(tuple.to_py_object(py).into_object())
236 235 },
237 236 Err(_) => Err(PyErr::new::<exc::OSError, _>(
238 237 py,
239 238 "Dirstate error".to_string(),
240 239 )),
241 240 }
242 241 }
243 242
244 243 def filefoldmapasdict(&self) -> PyResult<PyDict> {
245 244 let dict = PyDict::new(py);
246 245 for item in self.inner(py).borrow_mut().iter() {
247 246 let (path, entry) = item.map_err(|e| v2_error(py, e))?;
248 247 if entry.state() != EntryState::Removed {
249 248 let key = normalize_case(path);
250 249 let value = path;
251 250 dict.set_item(
252 251 py,
253 252 PyBytes::new(py, key.as_bytes()).into_object(),
254 253 PyBytes::new(py, value.as_bytes()).into_object(),
255 254 )?;
256 255 }
257 256 }
258 257 Ok(dict)
259 258 }
260 259
261 260 def __len__(&self) -> PyResult<usize> {
262 261 Ok(self.inner(py).borrow().len())
263 262 }
264 263
265 264 def __contains__(&self, key: PyObject) -> PyResult<bool> {
266 265 let key = key.extract::<PyBytes>(py)?;
267 266 self.inner(py)
268 267 .borrow()
269 268 .contains_key(HgPath::new(key.data(py)))
270 269 .map_err(|e| v2_error(py, e))
271 270 }
272 271
273 272 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> {
274 273 let key = key.extract::<PyBytes>(py)?;
275 274 let key = HgPath::new(key.data(py));
276 275 match self
277 276 .inner(py)
278 277 .borrow()
279 278 .get(key)
280 279 .map_err(|e| v2_error(py, e))?
281 280 {
282 281 Some(entry) => {
283 282 Ok(DirstateItem::new_as_pyobject(py, entry)?)
284 283 },
285 284 None => Err(PyErr::new::<exc::KeyError, _>(
286 285 py,
287 286 String::from_utf8_lossy(key.as_bytes()),
288 287 )),
289 288 }
290 289 }
291 290
292 291 def keys(&self) -> PyResult<DirstateMapKeysIterator> {
293 292 let leaked_ref = self.inner(py).leak_immutable();
294 293 DirstateMapKeysIterator::from_inner(
295 294 py,
296 295 unsafe { leaked_ref.map(py, |o| o.iter()) },
297 296 )
298 297 }
299 298
300 299 def items(&self) -> PyResult<DirstateMapItemsIterator> {
301 300 let leaked_ref = self.inner(py).leak_immutable();
302 301 DirstateMapItemsIterator::from_inner(
303 302 py,
304 303 unsafe { leaked_ref.map(py, |o| o.iter()) },
305 304 )
306 305 }
307 306
308 307 def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
309 308 let leaked_ref = self.inner(py).leak_immutable();
310 309 DirstateMapKeysIterator::from_inner(
311 310 py,
312 311 unsafe { leaked_ref.map(py, |o| o.iter()) },
313 312 )
314 313 }
315 314
316 315 // TODO all copymap* methods, see docstring above
317 316 def copymapcopy(&self) -> PyResult<PyDict> {
318 317 let dict = PyDict::new(py);
319 318 for item in self.inner(py).borrow().copy_map_iter() {
320 319 let (key, value) = item.map_err(|e| v2_error(py, e))?;
321 320 dict.set_item(
322 321 py,
323 322 PyBytes::new(py, key.as_bytes()),
324 323 PyBytes::new(py, value.as_bytes()),
325 324 )?;
326 325 }
327 326 Ok(dict)
328 327 }
329 328
330 329 def copymapgetitem(&self, key: PyObject) -> PyResult<PyBytes> {
331 330 let key = key.extract::<PyBytes>(py)?;
332 331 match self
333 332 .inner(py)
334 333 .borrow()
335 334 .copy_map_get(HgPath::new(key.data(py)))
336 335 .map_err(|e| v2_error(py, e))?
337 336 {
338 337 Some(copy) => Ok(PyBytes::new(py, copy.as_bytes())),
339 338 None => Err(PyErr::new::<exc::KeyError, _>(
340 339 py,
341 340 String::from_utf8_lossy(key.data(py)),
342 341 )),
343 342 }
344 343 }
345 344 def copymap(&self) -> PyResult<CopyMap> {
346 345 CopyMap::from_inner(py, self.clone_ref(py))
347 346 }
348 347
349 348 def copymaplen(&self) -> PyResult<usize> {
350 349 Ok(self.inner(py).borrow().copy_map_len())
351 350 }
352 351 def copymapcontains(&self, key: PyObject) -> PyResult<bool> {
353 352 let key = key.extract::<PyBytes>(py)?;
354 353 self.inner(py)
355 354 .borrow()
356 355 .copy_map_contains_key(HgPath::new(key.data(py)))
357 356 .map_err(|e| v2_error(py, e))
358 357 }
359 358 def copymapget(
360 359 &self,
361 360 key: PyObject,
362 361 default: Option<PyObject>
363 362 ) -> PyResult<Option<PyObject>> {
364 363 let key = key.extract::<PyBytes>(py)?;
365 364 match self
366 365 .inner(py)
367 366 .borrow()
368 367 .copy_map_get(HgPath::new(key.data(py)))
369 368 .map_err(|e| v2_error(py, e))?
370 369 {
371 370 Some(copy) => Ok(Some(
372 371 PyBytes::new(py, copy.as_bytes()).into_object(),
373 372 )),
374 373 None => Ok(default),
375 374 }
376 375 }
377 376 def copymapsetitem(
378 377 &self,
379 378 key: PyObject,
380 379 value: PyObject
381 380 ) -> PyResult<PyObject> {
382 381 let key = key.extract::<PyBytes>(py)?;
383 382 let value = value.extract::<PyBytes>(py)?;
384 383 self.inner(py)
385 384 .borrow_mut()
386 385 .copy_map_insert(
387 386 HgPathBuf::from_bytes(key.data(py)),
388 387 HgPathBuf::from_bytes(value.data(py)),
389 388 )
390 389 .map_err(|e| v2_error(py, e))?;
391 390 Ok(py.None())
392 391 }
393 392 def copymappop(
394 393 &self,
395 394 key: PyObject,
396 395 default: Option<PyObject>
397 396 ) -> PyResult<Option<PyObject>> {
398 397 let key = key.extract::<PyBytes>(py)?;
399 398 match self
400 399 .inner(py)
401 400 .borrow_mut()
402 401 .copy_map_remove(HgPath::new(key.data(py)))
403 402 .map_err(|e| v2_error(py, e))?
404 403 {
405 404 Some(_) => Ok(None),
406 405 None => Ok(default),
407 406 }
408 407 }
409 408
410 409 def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
411 410 let leaked_ref = self.inner(py).leak_immutable();
412 411 CopyMapKeysIterator::from_inner(
413 412 py,
414 413 unsafe { leaked_ref.map(py, |o| o.copy_map_iter()) },
415 414 )
416 415 }
417 416
418 417 def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
419 418 let leaked_ref = self.inner(py).leak_immutable();
420 419 CopyMapItemsIterator::from_inner(
421 420 py,
422 421 unsafe { leaked_ref.map(py, |o| o.copy_map_iter()) },
423 422 )
424 423 }
425 424
426 425 def tracked_dirs(&self) -> PyResult<PyList> {
427 426 let dirs = PyList::new(py, &[]);
428 427 for path in self.inner(py).borrow_mut().iter_tracked_dirs()
429 428 .map_err(|e |dirstate_error(py, e))?
430 429 {
431 430 let path = path.map_err(|e| v2_error(py, e))?;
432 431 let path = PyBytes::new(py, path.as_bytes());
433 432 dirs.append(py, path.into_object())
434 433 }
435 434 Ok(dirs)
436 435 }
437 436
438 437 def debug_iter(&self, all: bool) -> PyResult<PyList> {
439 438 let dirs = PyList::new(py, &[]);
440 439 for item in self.inner(py).borrow().debug_iter(all) {
441 440 let (path, (state, mode, size, mtime)) =
442 441 item.map_err(|e| v2_error(py, e))?;
443 442 let path = PyBytes::new(py, path.as_bytes());
444 443 let item = (path, state, mode, size, mtime);
445 444 dirs.append(py, item.to_py_object(py).into_object())
446 445 }
447 446 Ok(dirs)
448 447 }
449 448 });
450 449
451 450 impl DirstateMap {
452 451 pub fn get_inner_mut<'a>(
453 452 &'a self,
454 453 py: Python<'a>,
455 ) -> RefMut<'a, Box<dyn DirstateMapMethods + Send>> {
454 ) -> RefMut<'a, OwningDirstateMap> {
456 455 self.inner(py).borrow_mut()
457 456 }
458 457 fn translate_key(
459 458 py: Python,
460 459 res: Result<(&HgPath, DirstateEntry), DirstateV2ParseError>,
461 460 ) -> PyResult<Option<PyBytes>> {
462 461 let (f, _entry) = res.map_err(|e| v2_error(py, e))?;
463 462 Ok(Some(PyBytes::new(py, f.as_bytes())))
464 463 }
465 464 fn translate_key_value(
466 465 py: Python,
467 466 res: Result<(&HgPath, DirstateEntry), DirstateV2ParseError>,
468 467 ) -> PyResult<Option<(PyBytes, PyObject)>> {
469 468 let (f, entry) = res.map_err(|e| v2_error(py, e))?;
470 469 Ok(Some((
471 470 PyBytes::new(py, f.as_bytes()),
472 471 DirstateItem::new_as_pyobject(py, entry)?,
473 472 )))
474 473 }
475 474 }
476 475
477 476 py_shared_iterator!(
478 477 DirstateMapKeysIterator,
479 478 UnsafePyLeaked<StateMapIter<'static>>,
480 479 DirstateMap::translate_key,
481 480 Option<PyBytes>
482 481 );
483 482
484 483 py_shared_iterator!(
485 484 DirstateMapItemsIterator,
486 485 UnsafePyLeaked<StateMapIter<'static>>,
487 486 DirstateMap::translate_key_value,
488 487 Option<(PyBytes, PyObject)>
489 488 );
490 489
491 490 fn extract_node_id(py: Python, obj: &PyObject) -> PyResult<Node> {
492 491 let bytes = obj.extract::<PyBytes>(py)?;
493 492 match bytes.data(py).try_into() {
494 493 Ok(s) => Ok(s),
495 494 Err(e) => Err(PyErr::new::<exc::ValueError, _>(py, e.to_string())),
496 495 }
497 496 }
498 497
499 498 pub(super) fn v2_error(py: Python<'_>, _: DirstateV2ParseError) -> PyErr {
500 499 PyErr::new::<exc::ValueError, _>(py, "corrupted dirstate-v2")
501 500 }
502 501
503 502 fn dirstate_error(py: Python<'_>, e: DirstateError) -> PyErr {
504 503 PyErr::new::<exc::OSError, _>(py, format!("Dirstate error: {:?}", e))
505 504 }
@@ -1,281 +1,280 b''
1 1 // status.rs
2 2 //
3 3 // Copyright 2020, Georges Racinet <georges.racinets@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::error::CommandError;
9 9 use crate::ui::Ui;
10 10 use clap::{Arg, SubCommand};
11 11 use hg;
12 use hg::dirstate_tree::dispatch::DirstateMapMethods;
13 12 use hg::errors::HgError;
14 13 use hg::manifest::Manifest;
15 14 use hg::matchers::AlwaysMatcher;
16 15 use hg::repo::Repo;
17 16 use hg::utils::hg_path::{hg_path_to_os_string, HgPath};
18 17 use hg::{HgPathCow, StatusOptions};
19 18 use log::{info, warn};
20 19
21 20 pub const HELP_TEXT: &str = "
22 21 Show changed files in the working directory
23 22
24 23 This is a pure Rust version of `hg status`.
25 24
26 25 Some options might be missing, check the list below.
27 26 ";
28 27
29 28 pub fn args() -> clap::App<'static, 'static> {
30 29 SubCommand::with_name("status")
31 30 .alias("st")
32 31 .about(HELP_TEXT)
33 32 .arg(
34 33 Arg::with_name("all")
35 34 .help("show status of all files")
36 35 .short("-A")
37 36 .long("--all"),
38 37 )
39 38 .arg(
40 39 Arg::with_name("modified")
41 40 .help("show only modified files")
42 41 .short("-m")
43 42 .long("--modified"),
44 43 )
45 44 .arg(
46 45 Arg::with_name("added")
47 46 .help("show only added files")
48 47 .short("-a")
49 48 .long("--added"),
50 49 )
51 50 .arg(
52 51 Arg::with_name("removed")
53 52 .help("show only removed files")
54 53 .short("-r")
55 54 .long("--removed"),
56 55 )
57 56 .arg(
58 57 Arg::with_name("clean")
59 58 .help("show only clean files")
60 59 .short("-c")
61 60 .long("--clean"),
62 61 )
63 62 .arg(
64 63 Arg::with_name("deleted")
65 64 .help("show only deleted files")
66 65 .short("-d")
67 66 .long("--deleted"),
68 67 )
69 68 .arg(
70 69 Arg::with_name("unknown")
71 70 .help("show only unknown (not tracked) files")
72 71 .short("-u")
73 72 .long("--unknown"),
74 73 )
75 74 .arg(
76 75 Arg::with_name("ignored")
77 76 .help("show only ignored files")
78 77 .short("-i")
79 78 .long("--ignored"),
80 79 )
81 80 }
82 81
83 82 /// Pure data type allowing the caller to specify file states to display
84 83 #[derive(Copy, Clone, Debug)]
85 84 pub struct DisplayStates {
86 85 pub modified: bool,
87 86 pub added: bool,
88 87 pub removed: bool,
89 88 pub clean: bool,
90 89 pub deleted: bool,
91 90 pub unknown: bool,
92 91 pub ignored: bool,
93 92 }
94 93
95 94 pub const DEFAULT_DISPLAY_STATES: DisplayStates = DisplayStates {
96 95 modified: true,
97 96 added: true,
98 97 removed: true,
99 98 clean: false,
100 99 deleted: true,
101 100 unknown: true,
102 101 ignored: false,
103 102 };
104 103
105 104 pub const ALL_DISPLAY_STATES: DisplayStates = DisplayStates {
106 105 modified: true,
107 106 added: true,
108 107 removed: true,
109 108 clean: true,
110 109 deleted: true,
111 110 unknown: true,
112 111 ignored: true,
113 112 };
114 113
115 114 impl DisplayStates {
116 115 pub fn is_empty(&self) -> bool {
117 116 !(self.modified
118 117 || self.added
119 118 || self.removed
120 119 || self.clean
121 120 || self.deleted
122 121 || self.unknown
123 122 || self.ignored)
124 123 }
125 124 }
126 125
127 126 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
128 127 let status_enabled_default = false;
129 128 let status_enabled = invocation.config.get_option(b"rhg", b"status")?;
130 129 if !status_enabled.unwrap_or(status_enabled_default) {
131 130 return Err(CommandError::unsupported(
132 131 "status is experimental in rhg (enable it with 'rhg.status = true' \
133 132 or enable fallback with 'rhg.on-unsupported = fallback')"
134 133 ));
135 134 }
136 135
137 136 let ui = invocation.ui;
138 137 let args = invocation.subcommand_args;
139 138 let display_states = if args.is_present("all") {
140 139 // TODO when implementing `--quiet`: it excludes clean files
141 140 // from `--all`
142 141 ALL_DISPLAY_STATES
143 142 } else {
144 143 let requested = DisplayStates {
145 144 modified: args.is_present("modified"),
146 145 added: args.is_present("added"),
147 146 removed: args.is_present("removed"),
148 147 clean: args.is_present("clean"),
149 148 deleted: args.is_present("deleted"),
150 149 unknown: args.is_present("unknown"),
151 150 ignored: args.is_present("ignored"),
152 151 };
153 152 if requested.is_empty() {
154 153 DEFAULT_DISPLAY_STATES
155 154 } else {
156 155 requested
157 156 }
158 157 };
159 158
160 159 let repo = invocation.repo?;
161 160 let mut dmap = repo.dirstate_map_mut()?;
162 161
163 162 let options = StatusOptions {
164 163 // TODO should be provided by the dirstate parsing and
165 164 // hence be stored on dmap. Using a value that assumes we aren't
166 165 // below the time resolution granularity of the FS and the
167 166 // dirstate.
168 167 last_normal_time: 0,
169 168 // we're currently supporting file systems with exec flags only
170 169 // anyway
171 170 check_exec: true,
172 171 list_clean: display_states.clean,
173 172 list_unknown: display_states.unknown,
174 173 list_ignored: display_states.ignored,
175 174 collect_traversed_dirs: false,
176 175 };
177 176 let ignore_file = repo.working_directory_vfs().join(".hgignore"); // TODO hardcoded
178 177 let (mut ds_status, pattern_warnings) = dmap.status(
179 178 &AlwaysMatcher,
180 179 repo.working_directory_path().to_owned(),
181 180 vec![ignore_file],
182 181 options,
183 182 )?;
184 183 if !pattern_warnings.is_empty() {
185 184 warn!("Pattern warnings: {:?}", &pattern_warnings);
186 185 }
187 186
188 187 if !ds_status.bad.is_empty() {
189 188 warn!("Bad matches {:?}", &(ds_status.bad))
190 189 }
191 190 if !ds_status.unsure.is_empty() {
192 191 info!(
193 192 "Files to be rechecked by retrieval from filelog: {:?}",
194 193 &ds_status.unsure
195 194 );
196 195 }
197 196 if !ds_status.unsure.is_empty()
198 197 && (display_states.modified || display_states.clean)
199 198 {
200 199 let p1 = repo.dirstate_parents()?.p1;
201 200 let manifest = repo.manifest_for_node(p1).map_err(|e| {
202 201 CommandError::from((e, &*format!("{:x}", p1.short())))
203 202 })?;
204 203 for to_check in ds_status.unsure {
205 204 if cat_file_is_modified(repo, &manifest, &to_check)? {
206 205 if display_states.modified {
207 206 ds_status.modified.push(to_check);
208 207 }
209 208 } else {
210 209 if display_states.clean {
211 210 ds_status.clean.push(to_check);
212 211 }
213 212 }
214 213 }
215 214 }
216 215 if display_states.modified {
217 216 display_status_paths(ui, &mut ds_status.modified, b"M")?;
218 217 }
219 218 if display_states.added {
220 219 display_status_paths(ui, &mut ds_status.added, b"A")?;
221 220 }
222 221 if display_states.removed {
223 222 display_status_paths(ui, &mut ds_status.removed, b"R")?;
224 223 }
225 224 if display_states.deleted {
226 225 display_status_paths(ui, &mut ds_status.deleted, b"!")?;
227 226 }
228 227 if display_states.unknown {
229 228 display_status_paths(ui, &mut ds_status.unknown, b"?")?;
230 229 }
231 230 if display_states.ignored {
232 231 display_status_paths(ui, &mut ds_status.ignored, b"I")?;
233 232 }
234 233 if display_states.clean {
235 234 display_status_paths(ui, &mut ds_status.clean, b"C")?;
236 235 }
237 236 Ok(())
238 237 }
239 238
240 239 // Probably more elegant to use a Deref or Borrow trait rather than
241 240 // harcode HgPathBuf, but probably not really useful at this point
242 241 fn display_status_paths(
243 242 ui: &Ui,
244 243 paths: &mut [HgPathCow],
245 244 status_prefix: &[u8],
246 245 ) -> Result<(), CommandError> {
247 246 paths.sort_unstable();
248 247 for path in paths {
249 248 // Same TODO as in commands::root
250 249 let bytes: &[u8] = path.as_bytes();
251 250 // TODO optim, probably lots of unneeded copies here, especially
252 251 // if out stream is buffered
253 252 ui.write_stdout(&[status_prefix, b" ", bytes, b"\n"].concat())?;
254 253 }
255 254 Ok(())
256 255 }
257 256
258 257 /// Check if a file is modified by comparing actual repo store and file system.
259 258 ///
260 259 /// This meant to be used for those that the dirstate cannot resolve, due
261 260 /// to time resolution limits.
262 261 ///
263 262 /// TODO: detect permission bits and similar metadata modifications
264 263 fn cat_file_is_modified(
265 264 repo: &Repo,
266 265 manifest: &Manifest,
267 266 hg_path: &HgPath,
268 267 ) -> Result<bool, HgError> {
269 268 let file_node = manifest
270 269 .find_file(hg_path)?
271 270 .expect("ambgious file not in p1");
272 271 let filelog = repo.filelog(hg_path)?;
273 272 let filelog_entry = filelog.data_for_node(file_node).map_err(|_| {
274 273 HgError::corrupted("filelog missing node from manifest")
275 274 })?;
276 275 let contents_in_p1 = filelog_entry.data()?;
277 276
278 277 let fs_path = hg_path_to_os_string(hg_path).expect("HgPath conversion");
279 278 let fs_contents = repo.working_directory_vfs().read(fs_path)?;
280 279 return Ok(contents_in_p1 == &*fs_contents);
281 280 }
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now