rust/hg-core/src/dirstate/parsers.rs
author Simon Sapin <simon.sapin@octobus.net>
Tue, 16 Feb 2021 13:08:37 +0100
changeset 46601 755c31a1caf9
parent 46595 98a455a62699
child 46890 441024b279a6
permissions -rw-r--r--
rhg: Add support for the blackbox extension Only `command` and `commandfinish` events are logged. The `dirty`, `logsource`, `track` and `ignore` configuration items are not supported yet. To indicate commands executed without Python, a `(rust) ` prefix is added in corresponding log messages. Differential Revision: https://phab.mercurial-scm.org/D10012
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// This software may be used and distributed according to the terms of the
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
// GNU General Public License version 2 or any later version.
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
46439
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
     6
use crate::errors::HgError;
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
     7
use crate::utils::hg_path::HgPath;
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents: 42424
diff changeset
     8
use crate::{
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
     9
    dirstate::{CopyMap, EntryState, RawEntry, StateMap},
46440
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46439
diff changeset
    10
    DirstateEntry, DirstateParents,
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents: 42424
diff changeset
    11
};
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    12
use byteorder::{BigEndian, WriteBytesExt};
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    13
use bytes_cast::BytesCast;
44541
d880805d5442 hg-core: add function timing information
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
    14
use micro_timer::timed;
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
    15
use std::convert::{TryFrom, TryInto};
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    16
use std::time::Duration;
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    17
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    18
/// Parents are stored in the dirstate as byte hashes.
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    19
pub const PARENT_SIZE: usize = 20;
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
/// Dirstate entries have a static part of 8 + 32 + 32 + 32 + 32 bits.
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
const MIN_ENTRY_SIZE: usize = 17;
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
45357
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
    23
type ParseResult<'a> = (
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    24
    &'a DirstateParents,
45357
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
    25
    Vec<(&'a HgPath, DirstateEntry)>,
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
    26
    Vec<(&'a HgPath, &'a HgPath)>,
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
    27
);
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    28
46601
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46595
diff changeset
    29
pub fn parse_dirstate_parents(
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46595
diff changeset
    30
    contents: &[u8],
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46595
diff changeset
    31
) -> Result<&DirstateParents, HgError> {
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46595
diff changeset
    32
    let (parents, _rest) = DirstateParents::from_bytes(contents)
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46595
diff changeset
    33
        .map_err(|_| HgError::corrupted("Too little data for dirstate."))?;
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46595
diff changeset
    34
    Ok(parents)
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46595
diff changeset
    35
}
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46595
diff changeset
    36
44541
d880805d5442 hg-core: add function timing information
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
    37
#[timed]
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    38
pub fn parse_dirstate(mut contents: &[u8]) -> Result<ParseResult, HgError> {
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    39
    let mut copies = Vec::new();
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    40
    let mut entries = Vec::new();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    42
    let (parents, rest) = DirstateParents::from_bytes(contents)
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    43
        .map_err(|_| HgError::corrupted("Too little data for dirstate."))?;
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    44
    contents = rest;
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    45
    while !contents.is_empty() {
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    46
        let (raw_entry, rest) = RawEntry::from_bytes(contents)
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    47
            .map_err(|_| HgError::corrupted("Overflow in dirstate."))?;
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    49
        let entry = DirstateEntry {
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    50
            state: EntryState::try_from(raw_entry.state)?,
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    51
            mode: raw_entry.mode.get(),
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    52
            mtime: raw_entry.mtime.get(),
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    53
            size: raw_entry.size.get(),
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    54
        };
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    55
        let (paths, rest) =
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    56
            u8::slice_from_bytes(rest, raw_entry.length.get() as usize)
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    57
                .map_err(|_| HgError::corrupted("Overflow in dirstate."))?;
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    59
        // `paths` is either a single path, or two paths separated by a NULL
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    60
        // byte
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    61
        let mut iter = paths.splitn(2, |&byte| byte == b'\0');
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    62
        let path = HgPath::new(
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    63
            iter.next().expect("splitn always yields at least one item"),
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    64
        );
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    65
        if let Some(copy_source) = iter.next() {
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    66
            copies.push((path, HgPath::new(copy_source)));
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
        }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    69
        entries.push((path, entry));
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
    70
        contents = rest;
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
    }
45357
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
    72
    Ok((parents, entries, copies))
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
}
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    75
/// `now` is the duration in seconds since the Unix epoch
45610
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
    76
#[cfg(not(feature = "dirstate-tree"))]
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
pub fn pack_dirstate(
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    78
    state_map: &mut StateMap,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    79
    copy_map: &CopyMap,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
    parents: DirstateParents,
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    81
    now: Duration,
46439
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
    82
) -> Result<Vec<u8>, HgError> {
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    83
    // TODO move away from i32 before 2038.
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    84
    let now: i32 = now.as_secs().try_into().expect("time overflow");
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    86
    let expected_size: usize = state_map
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    87
        .iter()
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    88
        .map(|(filename, _)| {
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    89
            let mut length = MIN_ENTRY_SIZE + filename.len();
42794
cc424cc16704 rust-dirstate: remove unneeded "ref"
Yuya Nishihara <yuya@tcha.org>
parents: 42749
diff changeset
    90
            if let Some(copy) = copy_map.get(filename) {
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    91
                length += copy.len() + 1;
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    92
            }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    93
            length
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
        })
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    95
        .sum();
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    96
    let expected_size = expected_size + PARENT_SIZE * 2;
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    97
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
    let mut packed = Vec::with_capacity(expected_size);
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
46595
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   100
    packed.extend(parents.p1.as_bytes());
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   101
    packed.extend(parents.p2.as_bytes());
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   102
45646
1efbc787334c rust-parsers: use in-place mutation instead of allocating a new `Vec`
Raphaël Gomès <rgomes@octobus.net>
parents: 45610
diff changeset
   103
    for (filename, entry) in state_map.iter_mut() {
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   104
        let new_filename = filename.to_owned();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   105
        let mut new_mtime: i32 = entry.mtime;
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   106
        if entry.state == EntryState::Normal && entry.mtime == now {
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   107
            // The file was last modified "simultaneously" with the current
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   108
            // write to dirstate (i.e. within the same second for file-
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   109
            // systems with a granularity of 1 sec). This commonly happens
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   110
            // for at least a couple of files on 'update'.
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   111
            // The user could change the file without changing its size
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   112
            // within the same second. Invalidate the file's mtime in
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   113
            // dirstate, forcing future 'status' calls to compare the
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   114
            // contents of the file if the size is the same. This prevents
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   115
            // mistakenly treating such files as clean.
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   116
            new_mtime = -1;
45646
1efbc787334c rust-parsers: use in-place mutation instead of allocating a new `Vec`
Raphaël Gomès <rgomes@octobus.net>
parents: 45610
diff changeset
   117
            *entry = DirstateEntry {
1efbc787334c rust-parsers: use in-place mutation instead of allocating a new `Vec`
Raphaël Gomès <rgomes@octobus.net>
parents: 45610
diff changeset
   118
                mtime: new_mtime,
1efbc787334c rust-parsers: use in-place mutation instead of allocating a new `Vec`
Raphaël Gomès <rgomes@octobus.net>
parents: 45610
diff changeset
   119
                ..*entry
1efbc787334c rust-parsers: use in-place mutation instead of allocating a new `Vec`
Raphaël Gomès <rgomes@octobus.net>
parents: 45610
diff changeset
   120
            };
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   121
        }
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   122
        let mut new_filename = new_filename.into_vec();
42794
cc424cc16704 rust-dirstate: remove unneeded "ref"
Yuya Nishihara <yuya@tcha.org>
parents: 42749
diff changeset
   123
        if let Some(copy) = copy_map.get(filename) {
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44541
diff changeset
   124
            new_filename.push(b'\0');
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   125
            new_filename.extend(copy.bytes());
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   126
        }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   127
46439
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   128
        // Unwrapping because `impl std::io::Write for Vec<u8>` never errors
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   129
        packed.write_u8(entry.state.into()).unwrap();
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   130
        packed.write_i32::<BigEndian>(entry.mode).unwrap();
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   131
        packed.write_i32::<BigEndian>(entry.size).unwrap();
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   132
        packed.write_i32::<BigEndian>(new_mtime).unwrap();
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   133
        packed
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   134
            .write_i32::<BigEndian>(new_filename.len() as i32)
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   135
            .unwrap();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   136
        packed.extend(new_filename)
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   137
    }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   138
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   139
    if packed.len() != expected_size {
46439
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   140
        return Err(HgError::CorruptedRepository(format!(
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   141
            "bad dirstate size: {} != {}",
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   142
            expected_size,
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   143
            packed.len()
68a15b5a7e58 rust: Replace DirstatePackError with HgError
Simon Sapin <simon.sapin@octobus.net>
parents: 45646
diff changeset
   144
        )));
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   145
    }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   146
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   147
    Ok(packed)
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   148
}
45610
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   149
/// `now` is the duration in seconds since the Unix epoch
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   150
#[cfg(feature = "dirstate-tree")]
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   151
pub fn pack_dirstate(
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   152
    state_map: &mut StateMap,
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   153
    copy_map: &CopyMap,
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   154
    parents: DirstateParents,
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   155
    now: Duration,
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   156
) -> Result<Vec<u8>, DirstatePackError> {
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   157
    // TODO move away from i32 before 2038.
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   158
    let now: i32 = now.as_secs().try_into().expect("time overflow");
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   159
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   160
    let expected_size: usize = state_map
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   161
        .iter()
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   162
        .map(|(filename, _)| {
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   163
            let mut length = MIN_ENTRY_SIZE + filename.len();
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   164
            if let Some(copy) = copy_map.get(&filename) {
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   165
                length += copy.len() + 1;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   166
            }
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   167
            length
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   168
        })
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   169
        .sum();
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   170
    let expected_size = expected_size + PARENT_SIZE * 2;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   171
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   172
    let mut packed = Vec::with_capacity(expected_size);
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   173
    let mut new_state_map = vec![];
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   174
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   175
    packed.extend(&parents.p1);
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   176
    packed.extend(&parents.p2);
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   177
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   178
    for (filename, entry) in state_map.iter() {
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   179
        let new_filename = filename.to_owned();
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   180
        let mut new_mtime: i32 = entry.mtime;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   181
        if entry.state == EntryState::Normal && entry.mtime == now {
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   182
            // The file was last modified "simultaneously" with the current
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   183
            // write to dirstate (i.e. within the same second for file-
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   184
            // systems with a granularity of 1 sec). This commonly happens
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   185
            // for at least a couple of files on 'update'.
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   186
            // The user could change the file without changing its size
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   187
            // within the same second. Invalidate the file's mtime in
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   188
            // dirstate, forcing future 'status' calls to compare the
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   189
            // contents of the file if the size is the same. This prevents
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   190
            // mistakenly treating such files as clean.
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   191
            new_mtime = -1;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   192
            new_state_map.push((
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   193
                filename.to_owned(),
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   194
                DirstateEntry {
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   195
                    mtime: new_mtime,
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   196
                    ..entry
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   197
                },
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   198
            ));
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   199
        }
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   200
        let mut new_filename = new_filename.into_vec();
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   201
        if let Some(copy) = copy_map.get(&filename) {
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   202
            new_filename.push(b'\0');
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   203
            new_filename.extend(copy.bytes());
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   204
        }
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   205
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   206
        packed.write_u8(entry.state.into())?;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   207
        packed.write_i32::<BigEndian>(entry.mode)?;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   208
        packed.write_i32::<BigEndian>(entry.size)?;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   209
        packed.write_i32::<BigEndian>(new_mtime)?;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   210
        packed.write_i32::<BigEndian>(new_filename.len() as i32)?;
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   211
        packed.extend(new_filename)
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   212
    }
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   213
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   214
    if packed.len() != expected_size {
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   215
        return Err(DirstatePackError::BadSize(expected_size, packed.len()));
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   216
    }
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   217
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   218
    state_map.extend(new_state_map);
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   219
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   220
    Ok(packed)
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   221
}
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   222
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   223
#[cfg(test)]
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   224
mod tests {
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   225
    use super::*;
43826
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 42957
diff changeset
   226
    use crate::{utils::hg_path::HgPathBuf, FastHashMap};
45610
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   227
    use pretty_assertions::assert_eq;
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   228
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   229
    #[test]
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   230
    fn test_pack_dirstate_empty() {
45610
496537c9c1b4 rust: start plugging the dirstate tree behind a feature gate
Raphaël Gomès <rgomes@octobus.net>
parents: 45357
diff changeset
   231
        let mut state_map = StateMap::default();
43826
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 42957
diff changeset
   232
        let copymap = FastHashMap::default();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   233
        let parents = DirstateParents {
46595
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   234
            p1: b"12345678910111213141".into(),
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   235
            p2: b"00000000000000000000".into(),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   236
        };
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   237
        let now = Duration::new(15000000, 0);
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   238
        let expected = b"1234567891011121314100000000000000000000".to_vec();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   239
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   240
        assert_eq!(
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   241
            expected,
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   242
            pack_dirstate(&mut state_map, &copymap, parents, now).unwrap()
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   243
        );
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   244
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   245
        assert!(state_map.is_empty())
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   246
    }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   247
    #[test]
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   248
    fn test_pack_dirstate_one_entry() {
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   249
        let expected_state_map: StateMap = [(
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   250
            HgPathBuf::from_bytes(b"f1"),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   251
            DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   252
                state: EntryState::Normal,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   253
                mode: 0o644,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   254
                size: 0,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   255
                mtime: 791231220,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   256
            },
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   257
        )]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   258
        .iter()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   259
        .cloned()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   260
        .collect();
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   261
        let mut state_map = expected_state_map.clone();
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   262
43826
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 42957
diff changeset
   263
        let copymap = FastHashMap::default();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   264
        let parents = DirstateParents {
46595
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   265
            p1: b"12345678910111213141".into(),
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   266
            p2: b"00000000000000000000".into(),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   267
        };
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   268
        let now = Duration::new(15000000, 0);
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   269
        let expected = [
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   270
            49, 50, 51, 52, 53, 54, 55, 56, 57, 49, 48, 49, 49, 49, 50, 49,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   271
            51, 49, 52, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   272
            48, 48, 48, 48, 48, 48, 48, 48, 110, 0, 0, 1, 164, 0, 0, 0, 0, 47,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   273
            41, 58, 244, 0, 0, 0, 2, 102, 49,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   274
        ]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   275
        .to_vec();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   276
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   277
        assert_eq!(
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   278
            expected,
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   279
            pack_dirstate(&mut state_map, &copymap, parents, now).unwrap()
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   280
        );
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   281
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   282
        assert_eq!(expected_state_map, state_map);
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   283
    }
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   284
    #[test]
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   285
    fn test_pack_dirstate_one_entry_with_copy() {
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   286
        let expected_state_map: StateMap = [(
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   287
            HgPathBuf::from_bytes(b"f1"),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   288
            DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   289
                state: EntryState::Normal,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   290
                mode: 0o644,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   291
                size: 0,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   292
                mtime: 791231220,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   293
            },
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   294
        )]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   295
        .iter()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   296
        .cloned()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   297
        .collect();
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   298
        let mut state_map = expected_state_map.clone();
43826
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 42957
diff changeset
   299
        let mut copymap = FastHashMap::default();
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   300
        copymap.insert(
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   301
            HgPathBuf::from_bytes(b"f1"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   302
            HgPathBuf::from_bytes(b"copyname"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   303
        );
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   304
        let parents = DirstateParents {
46595
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   305
            p1: b"12345678910111213141".into(),
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   306
            p2: b"00000000000000000000".into(),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   307
        };
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   308
        let now = Duration::new(15000000, 0);
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   309
        let expected = [
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   310
            49, 50, 51, 52, 53, 54, 55, 56, 57, 49, 48, 49, 49, 49, 50, 49,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   311
            51, 49, 52, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   312
            48, 48, 48, 48, 48, 48, 48, 48, 110, 0, 0, 1, 164, 0, 0, 0, 0, 47,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   313
            41, 58, 244, 0, 0, 0, 11, 102, 49, 0, 99, 111, 112, 121, 110, 97,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   314
            109, 101,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   315
        ]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   316
        .to_vec();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   317
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   318
        assert_eq!(
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   319
            expected,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   320
            pack_dirstate(&mut state_map, &copymap, parents, now).unwrap()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   321
        );
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   322
        assert_eq!(expected_state_map, state_map);
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   323
    }
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   324
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   325
    #[test]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   326
    fn test_parse_pack_one_entry_with_copy() {
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   327
        let mut state_map: StateMap = [(
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   328
            HgPathBuf::from_bytes(b"f1"),
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   329
            DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   330
                state: EntryState::Normal,
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   331
                mode: 0o644,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   332
                size: 0,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   333
                mtime: 791231220,
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   334
            },
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   335
        )]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   336
        .iter()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   337
        .cloned()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   338
        .collect();
43826
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 42957
diff changeset
   339
        let mut copymap = FastHashMap::default();
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   340
        copymap.insert(
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   341
            HgPathBuf::from_bytes(b"f1"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   342
            HgPathBuf::from_bytes(b"copyname"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   343
        );
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   344
        let parents = DirstateParents {
46595
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   345
            p1: b"12345678910111213141".into(),
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   346
            p2: b"00000000000000000000".into(),
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   347
        };
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   348
        let now = Duration::new(15000000, 0);
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   349
        let result =
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   350
            pack_dirstate(&mut state_map, &copymap, parents.clone(), now)
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   351
                .unwrap();
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   352
45357
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   353
        let (new_parents, entries, copies) =
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   354
            parse_dirstate(result.as_slice()).unwrap();
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   355
        let new_state_map: StateMap = entries
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   356
            .into_iter()
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   357
            .map(|(path, entry)| (path.to_owned(), entry))
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   358
            .collect();
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   359
        let new_copy_map: CopyMap = copies
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   360
            .into_iter()
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   361
            .map(|(path, copy)| (path.to_owned(), copy.to_owned()))
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   362
            .collect();
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   363
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   364
        assert_eq!(
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
   365
            (&parents, state_map, copymap),
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   366
            (new_parents, new_state_map, new_copy_map)
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   367
        )
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   368
    }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   369
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   370
    #[test]
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   371
    fn test_parse_pack_multiple_entries_with_copy() {
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   372
        let mut state_map: StateMap = [
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   373
            (
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   374
                HgPathBuf::from_bytes(b"f1"),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   375
                DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   376
                    state: EntryState::Normal,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   377
                    mode: 0o644,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   378
                    size: 0,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   379
                    mtime: 791231220,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   380
                },
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   381
            ),
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   382
            (
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   383
                HgPathBuf::from_bytes(b"f2"),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   384
                DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   385
                    state: EntryState::Merged,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   386
                    mode: 0o777,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   387
                    size: 1000,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   388
                    mtime: 791231220,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   389
                },
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   390
            ),
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   391
            (
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   392
                HgPathBuf::from_bytes(b"f3"),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   393
                DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   394
                    state: EntryState::Removed,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   395
                    mode: 0o644,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   396
                    size: 234553,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   397
                    mtime: 791231220,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   398
                },
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   399
            ),
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   400
            (
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   401
                HgPathBuf::from_bytes(b"f4\xF6"),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   402
                DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   403
                    state: EntryState::Added,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   404
                    mode: 0o644,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   405
                    size: -1,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   406
                    mtime: -1,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   407
                },
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   408
            ),
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   409
        ]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   410
        .iter()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   411
        .cloned()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   412
        .collect();
43826
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 42957
diff changeset
   413
        let mut copymap = FastHashMap::default();
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   414
        copymap.insert(
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   415
            HgPathBuf::from_bytes(b"f1"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   416
            HgPathBuf::from_bytes(b"copyname"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   417
        );
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   418
        copymap.insert(
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   419
            HgPathBuf::from_bytes(b"f4\xF6"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   420
            HgPathBuf::from_bytes(b"copyname2"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   421
        );
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   422
        let parents = DirstateParents {
46595
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   423
            p1: b"12345678910111213141".into(),
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   424
            p2: b"00000000000000000000".into(),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   425
        };
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   426
        let now = Duration::new(15000000, 0);
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   427
        let result =
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   428
            pack_dirstate(&mut state_map, &copymap, parents.clone(), now)
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   429
                .unwrap();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   430
45357
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   431
        let (new_parents, entries, copies) =
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   432
            parse_dirstate(result.as_slice()).unwrap();
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   433
        let new_state_map: StateMap = entries
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   434
            .into_iter()
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   435
            .map(|(path, entry)| (path.to_owned(), entry))
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   436
            .collect();
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   437
        let new_copy_map: CopyMap = copies
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   438
            .into_iter()
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   439
            .map(|(path, copy)| (path.to_owned(), copy.to_owned()))
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   440
            .collect();
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   441
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   442
        assert_eq!(
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
   443
            (&parents, state_map, copymap),
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   444
            (new_parents, new_state_map, new_copy_map)
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   445
        )
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   446
    }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   447
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   448
    #[test]
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   449
    /// https://www.mercurial-scm.org/repo/hg/rev/af3f26b6bba4
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   450
    fn test_parse_pack_one_entry_with_copy_and_time_conflict() {
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   451
        let mut state_map: StateMap = [(
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   452
            HgPathBuf::from_bytes(b"f1"),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   453
            DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   454
                state: EntryState::Normal,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   455
                mode: 0o644,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   456
                size: 0,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   457
                mtime: 15000000,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   458
            },
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   459
        )]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   460
        .iter()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   461
        .cloned()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   462
        .collect();
43826
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 42957
diff changeset
   463
        let mut copymap = FastHashMap::default();
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   464
        copymap.insert(
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   465
            HgPathBuf::from_bytes(b"f1"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   466
            HgPathBuf::from_bytes(b"copyname"),
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   467
        );
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   468
        let parents = DirstateParents {
46595
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   469
            p1: b"12345678910111213141".into(),
98a455a62699 rust: Make `DirstateParents`’s fields typed `Node`s
Simon Sapin <simon.sapin@octobus.net>
parents: 46594
diff changeset
   470
            p2: b"00000000000000000000".into(),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   471
        };
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   472
        let now = Duration::new(15000000, 0);
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   473
        let result =
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   474
            pack_dirstate(&mut state_map, &copymap, parents.clone(), now)
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   475
                .unwrap();
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   476
45357
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   477
        let (new_parents, entries, copies) =
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   478
            parse_dirstate(result.as_slice()).unwrap();
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   479
        let new_state_map: StateMap = entries
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   480
            .into_iter()
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   481
            .map(|(path, entry)| (path.to_owned(), entry))
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   482
            .collect();
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   483
        let new_copy_map: CopyMap = copies
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   484
            .into_iter()
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   485
            .map(|(path, copy)| (path.to_owned(), copy.to_owned()))
27424779c5b8 hg-core: make parse_dirstate return references rather than update hashmaps
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
   486
            .collect();
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   487
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   488
        assert_eq!(
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   489
            (
46594
f88e8ae0aa8f rust: Rewrite dirstate parsing usin the `bytes-cast` crate
Simon Sapin <simon.sapin@octobus.net>
parents: 46440
diff changeset
   490
                &parents,
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   491
                [(
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42799
diff changeset
   492
                    HgPathBuf::from_bytes(b"f1"),
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   493
                    DirstateEntry {
42749
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
   494
                        state: EntryState::Normal,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   495
                        mode: 0o644,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   496
                        size: 0,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   497
                        mtime: -1
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   498
                    }
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   499
                )]
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   500
                .iter()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   501
                .cloned()
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   502
                .collect::<StateMap>(),
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   503
                copymap,
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   504
            ),
42748
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   505
            (new_parents, new_state_map, new_copy_map)
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   506
        )
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   507
    }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   508
}