rust/hg-core/src/dirstate_tree/on_disk.rs
author Simon Sapin <simon.sapin@octobus.net>
Fri, 16 Jul 2021 16:19:31 +0200
changeset 47684 852262e2e7d9
parent 47682 78f7f0d490ee
child 47966 681851d6409b
permissions -rw-r--r--
dirstate-v2: Reserve a few bytes of space for future extensions See doc-comment Differential Revision: https://phab.mercurial-scm.org/D11101
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
     1
//! The "version 2" disk representation of the dirstate
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
     2
//!
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
     3
//! # File format
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
     4
//!
47676
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
     5
//! In dirstate-v2 format, the `.hg/dirstate` file is a "docket that starts
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
     6
//! with a fixed-sized header whose layout is defined by the `DocketHeader`
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
     7
//! struct, followed by the data file identifier.
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
     8
//!
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
     9
//! A separate `.hg/dirstate.{uuid}.d` file contains most of the data. That
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
    10
//! file may be longer than the size given in the docket, but not shorter. Only
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
    11
//! the start of the data file up to the given size is considered. The
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
    12
//! fixed-size "root" of the dirstate tree whose layout is defined by the
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
    13
//! `Root` struct is found at the end of that slice of data.
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
    14
//!
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
    15
//! Its `root_nodes` field contains the slice (offset and length) to
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    16
//! the nodes representing the files and directories at the root of the
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    17
//! repository. Each node is also fixed-size, defined by the `Node` struct.
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    18
//! Nodes in turn contain slices to variable-size paths, and to their own child
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    19
//! nodes (if any) for nested files and directories.
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    20
47333
69530e5d4fe5 dirstate-tree: Add `NodeRef` and `ChildNodesRef` enums
Simon Sapin <simon.sapin@octobus.net>
parents: 47331
diff changeset
    21
use crate::dirstate_tree::dirstate_map::{self, DirstateMap, NodeRef};
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    22
use crate::dirstate_tree::path_with_basename::WithBasename;
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    23
use crate::errors::HgError;
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    24
use crate::utils::hg_path::HgPath;
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    25
use crate::DirstateEntry;
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    26
use crate::DirstateError;
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    27
use crate::DirstateParents;
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
    28
use crate::EntryState;
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
    29
use bytes_cast::unaligned::{I32Be, I64Be, U16Be, U32Be};
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    30
use bytes_cast::BytesCast;
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    31
use format_bytes::format_bytes;
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    32
use std::borrow::Cow;
47675
48aec076b8fb dirstate-v2: Enforce data size read from the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    33
use std::convert::{TryFrom, TryInto};
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
    34
use std::time::{Duration, SystemTime, UNIX_EPOCH};
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    35
47280
1766130fe9ba dirstate-v2: Change the on-disk format when the requirement is enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    36
/// Added at the start of `.hg/dirstate` when the "v2" format is used.
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    37
/// This a redundant sanity check more than an actual "magic number" since
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    38
/// `.hg/requires` already governs which format should be used.
47280
1766130fe9ba dirstate-v2: Change the on-disk format when the requirement is enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
diff changeset
    39
pub const V2_FORMAT_MARKER: &[u8; 12] = b"dirstate-v2\n";
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    40
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    41
/// Keep space for 256-bit hashes
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    42
const STORED_NODE_ID_BYTES: usize = 32;
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    43
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    44
/// … even though only 160 bits are used for now, with SHA-1
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    45
const USED_NODE_ID_BYTES: usize = 20;
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    46
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
    47
pub(super) const IGNORE_PATTERNS_HASH_LEN: usize = 20;
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
    48
pub(super) type IgnorePatternsHash = [u8; IGNORE_PATTERNS_HASH_LEN];
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
    49
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    50
/// Must match the constant of the same name in
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    51
/// `mercurial/dirstateutils/docket.py`
47684
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    52
const TREE_METADATA_SIZE: usize = 44;
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    53
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    54
/// Make sure that size-affecting changes are made knowingly
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    55
#[allow(unused)]
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    56
fn static_assert_size_of() {
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    57
    let _ = std::mem::transmute::<TreeMetadata, [u8; TREE_METADATA_SIZE]>;
47684
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    58
    let _ = std::mem::transmute::<DocketHeader, [u8; TREE_METADATA_SIZE + 81]>;
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    59
    let _ = std::mem::transmute::<Node, [u8; 43]>;
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    60
}
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    61
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    62
// Must match `HEADER` in `mercurial/dirstateutils/docket.py`
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    63
#[derive(BytesCast)]
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    64
#[repr(C)]
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    65
struct DocketHeader {
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    66
    marker: [u8; V2_FORMAT_MARKER.len()],
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    67
    parent_1: [u8; STORED_NODE_ID_BYTES],
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    68
    parent_2: [u8; STORED_NODE_ID_BYTES],
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
    69
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
    70
    /// Counted in bytes
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    71
    data_size: Size,
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
    72
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    73
    metadata: TreeMetadata,
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    74
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    75
    uuid_size: u8,
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    76
}
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    77
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    78
pub struct Docket<'on_disk> {
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    79
    header: &'on_disk DocketHeader,
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    80
    uuid: &'on_disk [u8],
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    81
}
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
    82
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    83
#[derive(BytesCast)]
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    84
#[repr(C)]
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
    85
struct TreeMetadata {
47676
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
    86
    root_nodes: ChildNodes,
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    87
    nodes_with_entry_count: Size,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
    88
    nodes_with_copy_source_count: Size,
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
    89
47681
d94118365ec5 dirstate-v2: Add heuristic for when to create a new data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47680
diff changeset
    90
    /// How many bytes of this data file are not used anymore
d94118365ec5 dirstate-v2: Add heuristic for when to create a new data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47680
diff changeset
    91
    unreachable_bytes: Size,
d94118365ec5 dirstate-v2: Add heuristic for when to create a new data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47680
diff changeset
    92
47684
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    93
    /// Current version always sets these bytes to zero when creating or
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    94
    /// updating a dirstate. Future versions could assign some bits to signal
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    95
    /// for example "the version that last wrote/updated this dirstate did so
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    96
    /// in such and such way that can be relied on by versions that know to."
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    97
    unused: [u8; 4],
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    98
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
    99
    /// If non-zero, a hash of ignore files that were used for some previous
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   100
    /// run of the `status` algorithm.
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   101
    ///
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   102
    /// We define:
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   103
    ///
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   104
    /// * "Root" ignore files are `.hgignore` at the root of the repository if
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   105
    ///   it exists, and files from `ui.ignore.*` config. This set of files is
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   106
    ///   then sorted by the string representation of their path.
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   107
    /// * The "expanded contents" of an ignore files is the byte string made
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   108
    ///   by concatenating its contents with the "expanded contents" of other
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   109
    ///   files included with `include:` or `subinclude:` files, in inclusion
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   110
    ///   order. This definition is recursive, as included files can
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   111
    ///   themselves include more files.
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   112
    ///
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   113
    /// This hash is defined as the SHA-1 of the concatenation (in sorted
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   114
    /// order) of the "expanded contents" of each "root" ignore file.
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   115
    /// (Note that computing this does not require actually concatenating byte
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   116
    /// strings into contiguous memory, instead SHA-1 hashing can be done
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   117
    /// incrementally.)
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   118
    ignore_patterns_hash: IgnorePatternsHash,
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   119
}
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   120
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   121
#[derive(BytesCast)]
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   122
#[repr(C)]
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   123
pub(super) struct Node {
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   124
    full_path: PathSlice,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   125
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   126
    /// In bytes from `self.full_path.start`
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   127
    base_name_start: PathSize,
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   128
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   129
    copy_source: OptPathSlice,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   130
    children: ChildNodes,
47478
ca8121d26732 dirstate-tree: Keep a counter of descendant nodes that have an entry
Simon Sapin <simon.sapin@octobus.net>
parents: 47476
diff changeset
   131
    pub(super) descendants_with_entry_count: Size,
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   132
    pub(super) tracked_descendants_count: Size,
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   133
47491
8851acad5906 rust: Document the DirstateMapMethods trait
Simon Sapin <simon.sapin@octobus.net>
parents: 47478
diff changeset
   134
    /// Depending on the value of `state`:
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   135
    ///
47349
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   136
    /// * A null byte: `data` is not used.
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   137
    ///
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   138
    /// * A `n`, `a`, `r`, or `m` ASCII byte: `state` and `data` together
47349
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   139
    ///   represent a dirstate entry like in the v1 format.
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   140
    ///
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   141
    /// * A `d` ASCII byte: the bytes of `data` should instead be interpreted
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   142
    ///   as the `Timestamp` for the mtime of a cached directory.
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   143
    ///
47349
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   144
    ///   The presence of this state means that at some point, this path in
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   145
    ///   the working directory was observed:
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   146
    ///
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   147
    ///   - To be a directory
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   148
    ///   - With the modification time as given by `Timestamp`
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   149
    ///   - That timestamp was already strictly in the past when observed,
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   150
    ///     meaning that later changes cannot happen in the same clock tick
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   151
    ///     and must cause a different modification time (unless the system
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   152
    ///     clock jumps back and we get unlucky, which is not impossible but
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   153
    ///     but deemed unlikely enough).
47475
94e38822d395 status: Extend read_dir caching to directories with ignored files
Simon Sapin <simon.sapin@octobus.net>
parents: 47409
diff changeset
   154
    ///   - All direct children of this directory (as returned by
94e38822d395 status: Extend read_dir caching to directories with ignored files
Simon Sapin <simon.sapin@octobus.net>
parents: 47409
diff changeset
   155
    ///     `std::fs::read_dir`) either have a corresponding dirstate node, or
94e38822d395 status: Extend read_dir caching to directories with ignored files
Simon Sapin <simon.sapin@octobus.net>
parents: 47409
diff changeset
   156
    ///     are ignored by ignore patterns whose hash is in
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   157
    ///     `TreeMetadata::ignore_patterns_hash`.
47349
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   158
    ///
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   159
    ///   This means that if `std::fs::symlink_metadata` later reports the
47475
94e38822d395 status: Extend read_dir caching to directories with ignored files
Simon Sapin <simon.sapin@octobus.net>
parents: 47409
diff changeset
   160
    ///   same modification time and ignored patterns haven’t changed, a run
94e38822d395 status: Extend read_dir caching to directories with ignored files
Simon Sapin <simon.sapin@octobus.net>
parents: 47409
diff changeset
   161
    ///   of status that is not listing ignored   files can skip calling
94e38822d395 status: Extend read_dir caching to directories with ignored files
Simon Sapin <simon.sapin@octobus.net>
parents: 47409
diff changeset
   162
    ///   `std::fs::read_dir` again for this directory,   iterate child
94e38822d395 status: Extend read_dir caching to directories with ignored files
Simon Sapin <simon.sapin@octobus.net>
parents: 47409
diff changeset
   163
    ///   dirstate nodes instead.
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   164
    state: u8,
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   165
    data: Entry,
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   166
}
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   167
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   168
#[derive(BytesCast, Copy, Clone)]
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   169
#[repr(C)]
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   170
struct Entry {
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   171
    mode: I32Be,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   172
    mtime: I32Be,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   173
    size: I32Be,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   174
}
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   175
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   176
/// Duration since the Unix epoch
47349
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   177
#[derive(BytesCast, Copy, Clone, PartialEq)]
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   178
#[repr(C)]
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   179
pub(super) struct Timestamp {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   180
    seconds: I64Be,
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   181
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   182
    /// In `0 .. 1_000_000_000`.
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   183
    ///
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   184
    /// This timestamp is later or earlier than `(seconds, 0)` by this many
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   185
    /// nanoseconds, if `seconds` is non-negative or negative, respectively.
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   186
    nanoseconds: U32Be,
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   187
}
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   188
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   189
/// Counted in bytes from the start of the file
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   190
///
47476
f23eafb036af dirstate-v2: Use 32-bit integers instead of 64-bit for offsets
Simon Sapin <simon.sapin@octobus.net>
parents: 47475
diff changeset
   191
/// NOTE: not supporting `.hg/dirstate` files larger than 4 GiB.
f23eafb036af dirstate-v2: Use 32-bit integers instead of 64-bit for offsets
Simon Sapin <simon.sapin@octobus.net>
parents: 47475
diff changeset
   192
type Offset = U32Be;
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   193
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   194
/// Counted in number of items
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   195
///
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   196
/// NOTE: we choose not to support counting more than 4 billion nodes anywhere.
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   197
type Size = U32Be;
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   198
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   199
/// Counted in bytes
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   200
///
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   201
/// NOTE: we choose not to support file names/paths longer than 64 KiB.
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   202
type PathSize = U16Be;
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   203
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   204
/// A contiguous sequence of `len` times `Node`, representing the child nodes
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   205
/// of either some other node or of the repository root.
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   206
///
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   207
/// Always sorted by ascending `full_path`, to allow binary search.
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   208
/// Since nodes with the same parent nodes also have the same parent path,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   209
/// only the `base_name`s need to be compared during binary search.
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   210
#[derive(BytesCast, Copy, Clone)]
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   211
#[repr(C)]
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   212
struct ChildNodes {
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   213
    start: Offset,
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   214
    len: Size,
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   215
}
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   216
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   217
/// A `HgPath` of `len` bytes
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   218
#[derive(BytesCast, Copy, Clone)]
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   219
#[repr(C)]
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   220
struct PathSlice {
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   221
    start: Offset,
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   222
    len: PathSize,
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   223
}
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   224
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   225
/// Either nothing if `start == 0`, or a `HgPath` of `len` bytes
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   226
type OptPathSlice = PathSlice;
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   227
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   228
/// Unexpected file format found in `.hg/dirstate` with the "v2" format.
47335
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47334
diff changeset
   229
///
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47334
diff changeset
   230
/// This should only happen if Mercurial is buggy or a repository is corrupted.
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47334
diff changeset
   231
#[derive(Debug)]
ed1583a845d2 dirstate-v2: Make more APIs fallible, returning Result
Simon Sapin <simon.sapin@octobus.net>
parents: 47334
diff changeset
   232
pub struct DirstateV2ParseError;
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   233
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   234
impl From<DirstateV2ParseError> for HgError {
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   235
    fn from(_: DirstateV2ParseError) -> Self {
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   236
        HgError::corrupted("dirstate-v2 parse error")
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   237
    }
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   238
}
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   239
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   240
impl From<DirstateV2ParseError> for crate::DirstateError {
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   241
    fn from(error: DirstateV2ParseError) -> Self {
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   242
        HgError::from(error).into()
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   243
    }
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   244
}
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   245
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   246
impl<'on_disk> Docket<'on_disk> {
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   247
    pub fn parents(&self) -> DirstateParents {
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   248
        use crate::Node;
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   249
        let p1 = Node::try_from(&self.header.parent_1[..USED_NODE_ID_BYTES])
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   250
            .unwrap()
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   251
            .clone();
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   252
        let p2 = Node::try_from(&self.header.parent_2[..USED_NODE_ID_BYTES])
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   253
            .unwrap()
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   254
            .clone();
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   255
        DirstateParents { p1, p2 }
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   256
    }
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   257
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   258
    pub fn tree_metadata(&self) -> &[u8] {
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   259
        self.header.metadata.as_bytes()
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   260
    }
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   261
47675
48aec076b8fb dirstate-v2: Enforce data size read from the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
   262
    pub fn data_size(&self) -> usize {
48aec076b8fb dirstate-v2: Enforce data size read from the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
   263
        // This `unwrap` could only panic on a 16-bit CPU
48aec076b8fb dirstate-v2: Enforce data size read from the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
   264
        self.header.data_size.get().try_into().unwrap()
48aec076b8fb dirstate-v2: Enforce data size read from the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
   265
    }
48aec076b8fb dirstate-v2: Enforce data size read from the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
   266
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   267
    pub fn data_filename(&self) -> String {
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   268
        String::from_utf8(format_bytes!(b"dirstate.{}.d", self.uuid)).unwrap()
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   269
    }
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   270
}
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   271
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   272
pub fn read_docket(
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   273
    on_disk: &[u8],
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   274
) -> Result<Docket<'_>, DirstateV2ParseError> {
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   275
    let (header, uuid) =
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   276
        DocketHeader::from_bytes(on_disk).map_err(|_| DirstateV2ParseError)?;
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   277
    let uuid_size = header.uuid_size as usize;
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   278
    if header.marker == *V2_FORMAT_MARKER && uuid.len() == uuid_size {
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   279
        Ok(Docket { header, uuid })
47374
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   280
    } else {
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   281
        Err(DirstateV2ParseError)
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   282
    }
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   283
}
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   284
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   285
pub(super) fn read<'on_disk>(
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   286
    on_disk: &'on_disk [u8],
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   287
    metadata: &[u8],
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   288
) -> Result<DirstateMap<'on_disk>, DirstateV2ParseError> {
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   289
    if on_disk.is_empty() {
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   290
        return Ok(DirstateMap::empty(on_disk));
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   291
    }
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   292
    let (meta, _) = TreeMetadata::from_bytes(metadata)
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   293
        .map_err(|_| DirstateV2ParseError)?;
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   294
    let dirstate_map = DirstateMap {
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   295
        on_disk,
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   296
        root: dirstate_map::ChildNodes::OnDisk(read_nodes(
47374
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   297
            on_disk,
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   298
            meta.root_nodes,
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   299
        )?),
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   300
        nodes_with_entry_count: meta.nodes_with_entry_count.get(),
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   301
        nodes_with_copy_source_count: meta.nodes_with_copy_source_count.get(),
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   302
        ignore_patterns_hash: meta.ignore_patterns_hash,
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   303
        unreachable_bytes: meta.unreachable_bytes.get(),
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   304
    };
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47491
diff changeset
   305
    Ok(dirstate_map)
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   306
}
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   307
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   308
impl Node {
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   309
    pub(super) fn full_path<'on_disk>(
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   310
        &self,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   311
        on_disk: &'on_disk [u8],
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   312
    ) -> Result<&'on_disk HgPath, DirstateV2ParseError> {
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   313
        read_hg_path(on_disk, self.full_path)
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   314
    }
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   315
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   316
    pub(super) fn base_name_start<'on_disk>(
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   317
        &self,
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   318
    ) -> Result<usize, DirstateV2ParseError> {
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   319
        let start = self.base_name_start.get();
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   320
        if start < self.full_path.len.get() {
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   321
            let start = usize::try_from(start)
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   322
                // u32 -> usize, could only panic on a 16-bit CPU
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   323
                .expect("dirstate-v2 base_name_start out of bounds");
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   324
            Ok(start)
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   325
        } else {
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   326
            Err(DirstateV2ParseError)
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   327
        }
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   328
    }
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   329
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   330
    pub(super) fn base_name<'on_disk>(
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   331
        &self,
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   332
        on_disk: &'on_disk [u8],
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   333
    ) -> Result<&'on_disk HgPath, DirstateV2ParseError> {
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   334
        let full_path = self.full_path(on_disk)?;
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   335
        let base_name_start = self.base_name_start()?;
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   336
        Ok(HgPath::new(&full_path.as_bytes()[base_name_start..]))
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   337
    }
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   338
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   339
    pub(super) fn path<'on_disk>(
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   340
        &self,
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   341
        on_disk: &'on_disk [u8],
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   342
    ) -> Result<dirstate_map::NodeKey<'on_disk>, DirstateV2ParseError> {
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   343
        Ok(WithBasename::from_raw_parts(
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   344
            Cow::Borrowed(self.full_path(on_disk)?),
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   345
            self.base_name_start()?,
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   346
        ))
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   347
    }
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   348
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   349
    pub(super) fn has_copy_source<'on_disk>(&self) -> bool {
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   350
        self.copy_source.start.get() != 0
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   351
    }
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   352
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   353
    pub(super) fn copy_source<'on_disk>(
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   354
        &self,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   355
        on_disk: &'on_disk [u8],
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   356
    ) -> Result<Option<&'on_disk HgPath>, DirstateV2ParseError> {
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   357
        Ok(if self.has_copy_source() {
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   358
            Some(read_hg_path(on_disk, self.copy_source)?)
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   359
        } else {
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   360
            None
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   361
        })
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   362
    }
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   363
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   364
    pub(super) fn node_data(
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   365
        &self,
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   366
    ) -> Result<dirstate_map::NodeData, DirstateV2ParseError> {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   367
        let entry = |state| {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   368
            dirstate_map::NodeData::Entry(self.entry_with_given_state(state))
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   369
        };
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   370
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   371
        match self.state {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   372
            b'\0' => Ok(dirstate_map::NodeData::None),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   373
            b'd' => Ok(dirstate_map::NodeData::CachedDirectory {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   374
                mtime: *self.data.as_timestamp(),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   375
            }),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   376
            b'n' => Ok(entry(EntryState::Normal)),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   377
            b'a' => Ok(entry(EntryState::Added)),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   378
            b'r' => Ok(entry(EntryState::Removed)),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   379
            b'm' => Ok(entry(EntryState::Merged)),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   380
            _ => Err(DirstateV2ParseError),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   381
        }
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   382
    }
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   383
47349
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   384
    pub(super) fn cached_directory_mtime(&self) -> Option<&Timestamp> {
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   385
        if self.state == b'd' {
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   386
            Some(self.data.as_timestamp())
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   387
        } else {
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   388
            None
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   389
        }
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   390
    }
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   391
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   392
    pub(super) fn state(
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   393
        &self,
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   394
    ) -> Result<Option<EntryState>, DirstateV2ParseError> {
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   395
        match self.state {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   396
            b'\0' | b'd' => Ok(None),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   397
            b'n' => Ok(Some(EntryState::Normal)),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   398
            b'a' => Ok(Some(EntryState::Added)),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   399
            b'r' => Ok(Some(EntryState::Removed)),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   400
            b'm' => Ok(Some(EntryState::Merged)),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   401
            _ => Err(DirstateV2ParseError),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   402
        }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   403
    }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   404
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   405
    fn entry_with_given_state(&self, state: EntryState) -> DirstateEntry {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   406
        DirstateEntry {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   407
            state,
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   408
            mode: self.data.mode.get(),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   409
            mtime: self.data.mtime.get(),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   410
            size: self.data.size.get(),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   411
        }
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   412
    }
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   413
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   414
    pub(super) fn entry(
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   415
        &self,
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   416
    ) -> Result<Option<DirstateEntry>, DirstateV2ParseError> {
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   417
        Ok(self
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   418
            .state()?
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   419
            .map(|state| self.entry_with_given_state(state)))
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   420
    }
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   421
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   422
    pub(super) fn children<'on_disk>(
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   423
        &self,
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   424
        on_disk: &'on_disk [u8],
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   425
    ) -> Result<&'on_disk [Node], DirstateV2ParseError> {
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   426
        read_nodes(on_disk, self.children)
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   427
    }
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   428
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   429
    pub(super) fn to_in_memory_node<'on_disk>(
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   430
        &self,
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   431
        on_disk: &'on_disk [u8],
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   432
    ) -> Result<dirstate_map::Node<'on_disk>, DirstateV2ParseError> {
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   433
        Ok(dirstate_map::Node {
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   434
            children: dirstate_map::ChildNodes::OnDisk(
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   435
                self.children(on_disk)?,
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   436
            ),
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   437
            copy_source: self.copy_source(on_disk)?.map(Cow::Borrowed),
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   438
            data: self.node_data()?,
47478
ca8121d26732 dirstate-tree: Keep a counter of descendant nodes that have an entry
Simon Sapin <simon.sapin@octobus.net>
parents: 47476
diff changeset
   439
            descendants_with_entry_count: self
ca8121d26732 dirstate-tree: Keep a counter of descendant nodes that have an entry
Simon Sapin <simon.sapin@octobus.net>
parents: 47476
diff changeset
   440
                .descendants_with_entry_count
ca8121d26732 dirstate-tree: Keep a counter of descendant nodes that have an entry
Simon Sapin <simon.sapin@octobus.net>
parents: 47476
diff changeset
   441
                .get(),
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   442
            tracked_descendants_count: self.tracked_descendants_count.get(),
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   443
        })
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   444
    }
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   445
}
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   446
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   447
impl Entry {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   448
    fn from_timestamp(timestamp: Timestamp) -> Self {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   449
        // Safety: both types implement the `ByteCast` trait, so we could
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   450
        // safely use `as_bytes` and `from_bytes` to do this conversion. Using
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   451
        // `transmute` instead makes the compiler check that the two types
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   452
        // have the same size, which eliminates the error case of
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   453
        // `from_bytes`.
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   454
        unsafe { std::mem::transmute::<Timestamp, Entry>(timestamp) }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   455
    }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   456
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   457
    fn as_timestamp(&self) -> &Timestamp {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   458
        // Safety: same as above in `from_timestamp`
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   459
        unsafe { &*(self as *const Entry as *const Timestamp) }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   460
    }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   461
}
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   462
47351
3b9914b28133 dirstate-v2: Add --dirs to debugdirstate command
Simon Sapin <simon.sapin@octobus.net>
parents: 47349
diff changeset
   463
impl Timestamp {
3b9914b28133 dirstate-v2: Add --dirs to debugdirstate command
Simon Sapin <simon.sapin@octobus.net>
parents: 47349
diff changeset
   464
    pub fn seconds(&self) -> i64 {
3b9914b28133 dirstate-v2: Add --dirs to debugdirstate command
Simon Sapin <simon.sapin@octobus.net>
parents: 47349
diff changeset
   465
        self.seconds.get()
3b9914b28133 dirstate-v2: Add --dirs to debugdirstate command
Simon Sapin <simon.sapin@octobus.net>
parents: 47349
diff changeset
   466
    }
3b9914b28133 dirstate-v2: Add --dirs to debugdirstate command
Simon Sapin <simon.sapin@octobus.net>
parents: 47349
diff changeset
   467
}
3b9914b28133 dirstate-v2: Add --dirs to debugdirstate command
Simon Sapin <simon.sapin@octobus.net>
parents: 47349
diff changeset
   468
47349
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   469
impl From<SystemTime> for Timestamp {
7138c863d0a1 dirstate-v2: Skip readdir in status based on directory mtime
Simon Sapin <simon.sapin@octobus.net>
parents: 47348
diff changeset
   470
    fn from(system_time: SystemTime) -> Self {
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   471
        let (secs, nanos) = match system_time.duration_since(UNIX_EPOCH) {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   472
            Ok(duration) => {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   473
                (duration.as_secs() as i64, duration.subsec_nanos())
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   474
            }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   475
            Err(error) => {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   476
                let negative = error.duration();
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   477
                (-(negative.as_secs() as i64), negative.subsec_nanos())
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   478
            }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   479
        };
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   480
        Timestamp {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   481
            seconds: secs.into(),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   482
            nanoseconds: nanos.into(),
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   483
        }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   484
    }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   485
}
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   486
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   487
impl From<&'_ Timestamp> for SystemTime {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   488
    fn from(timestamp: &'_ Timestamp) -> Self {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   489
        let secs = timestamp.seconds.get();
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   490
        let nanos = timestamp.nanoseconds.get();
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   491
        if secs >= 0 {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   492
            UNIX_EPOCH + Duration::new(secs as u64, nanos)
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   493
        } else {
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   494
            UNIX_EPOCH - Duration::new((-secs) as u64, nanos)
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   495
        }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   496
    }
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   497
}
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   498
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   499
fn read_hg_path(
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   500
    on_disk: &[u8],
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   501
    slice: PathSlice,
47337
0654b3b3d2b5 dirstate-v2: Parse the dirstate lazily, with copy-on-write nodes
Simon Sapin <simon.sapin@octobus.net>
parents: 47336
diff changeset
   502
) -> Result<&HgPath, DirstateV2ParseError> {
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   503
    read_slice(on_disk, slice.start, slice.len.get()).map(HgPath::new)
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   504
}
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   505
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   506
fn read_nodes(
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   507
    on_disk: &[u8],
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   508
    slice: ChildNodes,
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   509
) -> Result<&[Node], DirstateV2ParseError> {
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   510
    read_slice(on_disk, slice.start, slice.len.get())
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   511
}
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   512
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   513
fn read_slice<T, Len>(
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   514
    on_disk: &[u8],
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   515
    start: Offset,
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   516
    len: Len,
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   517
) -> Result<&[T], DirstateV2ParseError>
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   518
where
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   519
    T: BytesCast,
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   520
    Len: TryInto<usize>,
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   521
{
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   522
    // Either `usize::MAX` would result in "out of bounds" error since a single
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   523
    // `&[u8]` cannot occupy the entire addess space.
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   524
    let start = start.get().try_into().unwrap_or(std::usize::MAX);
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   525
    let len = len.try_into().unwrap_or(std::usize::MAX);
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   526
    on_disk
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   527
        .get(start..)
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   528
        .and_then(|bytes| T::slice_from_bytes(bytes, len).ok())
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   529
        .map(|(slice, _rest)| slice)
47334
18b3060fe598 dirstate-v2: Add a zero-size error type for dirstate v2 parse errors
Simon Sapin <simon.sapin@octobus.net>
parents: 47333
diff changeset
   530
        .ok_or_else(|| DirstateV2ParseError)
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   531
}
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   532
47374
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   533
pub(crate) fn for_each_tracked_path<'on_disk>(
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   534
    on_disk: &'on_disk [u8],
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   535
    metadata: &[u8],
47374
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   536
    mut f: impl FnMut(&'on_disk HgPath),
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   537
) -> Result<(), DirstateV2ParseError> {
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   538
    let (meta, _) = TreeMetadata::from_bytes(metadata)
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   539
        .map_err(|_| DirstateV2ParseError)?;
47374
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   540
    fn recur<'on_disk>(
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   541
        on_disk: &'on_disk [u8],
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   542
        nodes: ChildNodes,
47374
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   543
        f: &mut impl FnMut(&'on_disk HgPath),
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   544
    ) -> Result<(), DirstateV2ParseError> {
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   545
        for node in read_nodes(on_disk, nodes)? {
47374
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   546
            if let Some(state) = node.state()? {
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   547
                if state.is_tracked() {
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   548
                    f(node.full_path(on_disk)?)
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   549
                }
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   550
            }
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   551
            recur(on_disk, node.children, f)?
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   552
        }
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   553
        Ok(())
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   554
    }
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   555
    recur(on_disk, meta.root_nodes, &mut f)
47374
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   556
}
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47351
diff changeset
   557
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   558
/// Returns new data and metadata, together with whether that data should be
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   559
/// appended to the existing data file whose content is at
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   560
/// `dirstate_map.on_disk` (true), instead of written to a new data file
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   561
/// (false).
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   562
pub(super) fn write(
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   563
    dirstate_map: &mut DirstateMap,
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   564
    can_append: bool,
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   565
) -> Result<(Vec<u8>, Vec<u8>, bool), DirstateError> {
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   566
    let append = can_append && dirstate_map.write_should_append();
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   567
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   568
    // This ignores the space for paths, and for nodes without an entry.
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   569
    // TODO: better estimate? Skip the `Vec` and write to a file directly?
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   570
    let size_guess = std::mem::size_of::<Node>()
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   571
        * dirstate_map.nodes_with_entry_count as usize;
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   572
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   573
    let mut writer = Writer {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   574
        dirstate_map,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   575
        append,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   576
        out: Vec::with_capacity(size_guess),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   577
    };
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   578
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   579
    let root_nodes = writer.write_nodes(dirstate_map.root.as_ref())?;
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   580
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   581
    let meta = TreeMetadata {
47676
096ee2e260a3 dirstate-v2: Rename Header to Root, move it to the end of the data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47675
diff changeset
   582
        root_nodes,
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   583
        nodes_with_entry_count: dirstate_map.nodes_with_entry_count.into(),
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   584
        nodes_with_copy_source_count: dirstate_map
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   585
            .nodes_with_copy_source_count
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   586
            .into(),
47681
d94118365ec5 dirstate-v2: Add heuristic for when to create a new data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47680
diff changeset
   587
        unreachable_bytes: dirstate_map.unreachable_bytes.into(),
47684
852262e2e7d9 dirstate-v2: Reserve a few bytes of space for future extensions
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
   588
        unused: [0; 4],
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47374
diff changeset
   589
        ignore_patterns_hash: dirstate_map.ignore_patterns_hash,
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   590
    };
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47681
diff changeset
   591
    Ok((writer.out, meta.as_bytes().to_vec(), append))
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   592
}
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   593
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   594
struct Writer<'dmap, 'on_disk> {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   595
    dirstate_map: &'dmap DirstateMap<'on_disk>,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   596
    append: bool,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   597
    out: Vec<u8>,
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   598
}
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   599
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   600
impl Writer<'_, '_> {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   601
    fn write_nodes(
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   602
        &mut self,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   603
        nodes: dirstate_map::ChildNodesRef,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   604
    ) -> Result<ChildNodes, DirstateError> {
47679
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   605
        // Reuse already-written nodes if possible
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   606
        if self.append {
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   607
            if let dirstate_map::ChildNodesRef::OnDisk(nodes_slice) = nodes {
47680
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   608
                let start = self.on_disk_offset_of(nodes_slice).expect(
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   609
                    "dirstate-v2 OnDisk nodes not found within on_disk",
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   610
                );
47679
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   611
                let len = child_nodes_len_from_usize(nodes_slice.len());
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   612
                return Ok(ChildNodes { start, len });
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   613
            }
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   614
        }
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   615
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   616
        // `dirstate_map::ChildNodes::InMemory` contains a `HashMap` which has
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   617
        // undefined iteration order. Sort to enable binary search in the
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   618
        // written file.
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   619
        let nodes = nodes.sorted();
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   620
        let nodes_len = nodes.len();
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   621
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   622
        // First accumulate serialized nodes in a `Vec`
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   623
        let mut on_disk_nodes = Vec::with_capacity(nodes_len);
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   624
        for node in nodes {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   625
            let children =
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   626
                self.write_nodes(node.children(self.dirstate_map.on_disk)?)?;
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   627
            let full_path = node.full_path(self.dirstate_map.on_disk)?;
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   628
            let full_path = self.write_path(full_path.as_bytes());
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   629
            let copy_source = if let Some(source) =
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   630
                node.copy_source(self.dirstate_map.on_disk)?
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   631
            {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   632
                self.write_path(source.as_bytes())
47336
8d0260d0dbc9 dirstate-v2: Make the dirstate bytes buffer available in more places
Simon Sapin <simon.sapin@octobus.net>
parents: 47335
diff changeset
   633
            } else {
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   634
                PathSlice {
47336
8d0260d0dbc9 dirstate-v2: Make the dirstate bytes buffer available in more places
Simon Sapin <simon.sapin@octobus.net>
parents: 47335
diff changeset
   635
                    start: 0.into(),
8d0260d0dbc9 dirstate-v2: Make the dirstate bytes buffer available in more places
Simon Sapin <simon.sapin@octobus.net>
parents: 47335
diff changeset
   636
                    len: 0.into(),
8d0260d0dbc9 dirstate-v2: Make the dirstate bytes buffer available in more places
Simon Sapin <simon.sapin@octobus.net>
parents: 47335
diff changeset
   637
                }
8d0260d0dbc9 dirstate-v2: Make the dirstate bytes buffer available in more places
Simon Sapin <simon.sapin@octobus.net>
parents: 47335
diff changeset
   638
            };
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   639
            on_disk_nodes.push(match node {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   640
                NodeRef::InMemory(path, node) => {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   641
                    let (state, data) = match &node.data {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   642
                        dirstate_map::NodeData::Entry(entry) => (
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   643
                            entry.state.into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   644
                            Entry {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   645
                                mode: entry.mode.into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   646
                                mtime: entry.mtime.into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   647
                                size: entry.size.into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   648
                            },
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   649
                        ),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   650
                        dirstate_map::NodeData::CachedDirectory { mtime } => {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   651
                            (b'd', Entry::from_timestamp(*mtime))
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   652
                        }
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   653
                        dirstate_map::NodeData::None => (
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   654
                            b'\0',
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   655
                            Entry {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   656
                                mode: 0.into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   657
                                mtime: 0.into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   658
                                size: 0.into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   659
                            },
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   660
                        ),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   661
                    };
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   662
                    Node {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   663
                        children,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   664
                        copy_source,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   665
                        full_path,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   666
                        base_name_start: u16::try_from(path.base_name_start())
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   667
                            // Could only panic for paths over 64 KiB
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   668
                            .expect("dirstate-v2 path length overflow")
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   669
                            .into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   670
                        descendants_with_entry_count: node
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   671
                            .descendants_with_entry_count
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   672
                            .into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   673
                        tracked_descendants_count: node
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   674
                            .tracked_descendants_count
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   675
                            .into(),
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   676
                        state,
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   677
                        data,
47333
69530e5d4fe5 dirstate-tree: Add `NodeRef` and `ChildNodesRef` enums
Simon Sapin <simon.sapin@octobus.net>
parents: 47331
diff changeset
   678
                    }
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   679
                }
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   680
                NodeRef::OnDisk(node) => Node {
47348
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   681
                    children,
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   682
                    copy_source,
a4de570e61fa dirstate-v2: Allow tree nodes without an entry to store a timestamp
Simon Sapin <simon.sapin@octobus.net>
parents: 47337
diff changeset
   683
                    full_path,
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   684
                    ..*node
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   685
                },
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   686
            })
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   687
        }
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   688
        // … so we can write them contiguously, after writing everything else
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   689
        // they refer to.
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   690
        let start = self.current_offset();
47679
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   691
        let len = child_nodes_len_from_usize(nodes_len);
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   692
        self.out.extend(on_disk_nodes.as_bytes());
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   693
        Ok(ChildNodes { start, len })
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   694
    }
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   695
47680
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   696
    /// If the given slice of items is within `on_disk`, returns its offset
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   697
    /// from the start of `on_disk`.
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   698
    fn on_disk_offset_of<T>(&self, slice: &[T]) -> Option<Offset>
47679
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   699
    where
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   700
        T: BytesCast,
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   701
    {
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   702
        fn address_range(slice: &[u8]) -> std::ops::RangeInclusive<usize> {
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   703
            let start = slice.as_ptr() as usize;
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   704
            let end = start + slice.len();
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   705
            start..=end
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   706
        }
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   707
        let slice_addresses = address_range(slice.as_bytes());
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   708
        let on_disk_addresses = address_range(self.dirstate_map.on_disk);
47680
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   709
        if on_disk_addresses.contains(slice_addresses.start())
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   710
            && on_disk_addresses.contains(slice_addresses.end())
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   711
        {
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   712
            let offset = slice_addresses.start() - on_disk_addresses.start();
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   713
            Some(offset_from_usize(offset))
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   714
        } else {
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   715
            None
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   716
        }
47679
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   717
    }
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   718
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   719
    fn current_offset(&mut self) -> Offset {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   720
        let mut offset = self.out.len();
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   721
        if self.append {
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   722
            offset += self.dirstate_map.on_disk.len()
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   723
        }
47679
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   724
        offset_from_usize(offset)
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   725
    }
47677
da1c0cd68d53 dirstate-v2: shrink on-disk path lengths to 16-bits
Simon Sapin <simon.sapin@octobus.net>
parents: 47676
diff changeset
   726
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   727
    fn write_path(&mut self, slice: &[u8]) -> PathSlice {
47680
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   728
        let len = path_len_from_usize(slice.len());
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   729
        // Reuse an already-written path if possible
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   730
        if self.append {
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   731
            if let Some(start) = self.on_disk_offset_of(slice) {
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   732
                return PathSlice { start, len };
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   733
            }
a8b0f29dc0d7 dirstate-v2: Reuse existing paths when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47679
diff changeset
   734
        }
47678
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   735
        let start = self.current_offset();
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   736
        self.out.extend(slice.as_bytes());
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   737
        PathSlice { start, len }
065e61628980 dirstate-v2: Support appending to the same data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47677
diff changeset
   738
    }
47283
2a9ddc8094c7 dirstate-v2: Change the on-disk format to be tree-shaped
Simon Sapin <simon.sapin@octobus.net>
parents: 47280
diff changeset
   739
}
47679
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   740
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   741
fn offset_from_usize(x: usize) -> Offset {
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   742
    u32::try_from(x)
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   743
        // Could only panic for a dirstate file larger than 4 GiB
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   744
        .expect("dirstate-v2 offset overflow")
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   745
        .into()
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   746
}
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   747
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   748
fn child_nodes_len_from_usize(x: usize) -> Size {
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   749
    u32::try_from(x)
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   750
        // Could only panic with over 4 billion nodes
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   751
        .expect("dirstate-v2 slice length overflow")
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   752
        .into()
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   753
}
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   754
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   755
fn path_len_from_usize(x: usize) -> PathSize {
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   756
    u16::try_from(x)
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   757
        // Could only panic for paths over 64 KiB
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   758
        .expect("dirstate-v2 path length overflow")
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   759
        .into()
731286dc5321 dirstate-v2: Reuse existing nodes when appending to a data file
Simon Sapin <simon.sapin@octobus.net>
parents: 47678
diff changeset
   760
}