rust/hg-core/src/operations/list_tracked_files.rs
author Simon Sapin <simon.sapin@octobus.net>
Wed, 27 Jan 2021 14:00:21 +0100
changeset 46440 776b97179c06
parent 46437 b274aa2f20fd
child 46443 43d63979a75e
permissions -rw-r--r--
rust: Remove DirstateParseError and ListDirstateTrackedFilesError Use HgError instead. Differential Revision: https://phab.mercurial-scm.org/D9894
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     1
// list_tracked_files.rs
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     2
//
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     3
// Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net>
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     4
//
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     7
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     8
use crate::dirstate::parsers::parse_dirstate;
46440
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
     9
use crate::errors::{HgError, IoResultExt};
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    10
use crate::repo::Repo;
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    11
use crate::revlog::changelog::Changelog;
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    12
use crate::revlog::manifest::{Manifest, ManifestEntry};
46433
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46431
diff changeset
    13
use crate::revlog::node::Node;
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    14
use crate::revlog::revlog::RevlogError;
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    15
use crate::utils::hg_path::HgPath;
46440
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    16
use crate::EntryState;
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    17
use rayon::prelude::*;
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    18
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    19
/// List files under Mercurial control in the working directory
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    20
/// by reading the dirstate
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    21
pub struct Dirstate {
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    22
    /// The `dirstate` content.
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    23
    content: Vec<u8>,
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    24
}
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    25
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    26
impl Dirstate {
46440
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    27
    pub fn new(repo: &Repo) -> Result<Self, HgError> {
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    28
        let content = repo
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    29
            .hg_vfs()
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    30
            .read("dirstate")
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    31
            // TODO: this will be more accurate when we use `HgError` in
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    32
            // `Vfs::read`.
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    33
            .for_file("dirstate".as_ref())?;
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    34
        Ok(Self { content })
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    35
    }
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    36
46440
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    37
    pub fn tracked_files(&self) -> Result<Vec<&HgPath>, HgError> {
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46437
diff changeset
    38
        let (_, entries, _) = parse_dirstate(&self.content)?;
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    39
        let mut files: Vec<&HgPath> = entries
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    40
            .into_iter()
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    41
            .filter_map(|(path, entry)| match entry.state {
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    42
                EntryState::Removed => None,
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    43
                _ => Some(path),
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    44
            })
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    45
            .collect();
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    46
        files.par_sort_unstable();
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    47
        Ok(files)
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    48
    }
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    49
}
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    50
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    51
/// List files under Mercurial control at a given revision.
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    52
pub fn list_rev_tracked_files(
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    53
    repo: &Repo,
46433
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46431
diff changeset
    54
    revset: &str,
46437
b274aa2f20fd rust: remove three enums that were identical to `RevlogError`
Simon Sapin <simon.sapin@octobus.net>
parents: 46435
diff changeset
    55
) -> Result<FilesForRev, RevlogError> {
46433
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46431
diff changeset
    56
    let rev = crate::revset::resolve_single(revset, repo)?;
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    57
    let changelog = Changelog::open(repo)?;
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    58
    let manifest = Manifest::open(repo)?;
46433
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46431
diff changeset
    59
    let changelog_entry = changelog.get_rev(rev)?;
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    60
    let manifest_node = Node::from_hex(&changelog_entry.manifest_node()?)
46437
b274aa2f20fd rust: remove three enums that were identical to `RevlogError`
Simon Sapin <simon.sapin@octobus.net>
parents: 46435
diff changeset
    61
        .map_err(|_| RevlogError::Corrupted)?;
46431
645ee7225fab rust: Make NodePrefix allocation-free and Copy, remove NodePrefixRef
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
    62
    let manifest_entry = manifest.get_node(manifest_node.into())?;
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    63
    Ok(FilesForRev(manifest_entry))
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    64
}
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    65
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    66
pub struct FilesForRev(ManifestEntry);
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    67
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    68
impl FilesForRev {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    69
    pub fn iter(&self) -> impl Iterator<Item = &HgPath> {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    70
        self.0.files()
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    71
    }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    72
}