rust/hg-core/src/operations/list_tracked_files.rs
author Simon Sapin <simon.sapin@octobus.net>
Mon, 13 Sep 2021 13:01:25 +0200
changeset 47985 d44740725b95
parent 47682 78f7f0d490ee
child 47987 21d25e9ee58e
permissions -rw-r--r--
rust: Rename Manifest to Manifestlog, ManifestEntry to Manifest This appears to match the terminology used in Python code and on https://www.mercurial-scm.org/wiki/Manifest Differential Revision: https://phab.mercurial-scm.org/D11404
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45379
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
47379
d2fb8b4adcc3 rhg: Remove some intermediate Vecs in `rhg files`
Simon Sapin <simon.sapin@octobus.net>
parents: 46511
diff changeset
     8
use crate::dirstate::parsers::parse_dirstate_entries;
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47380
diff changeset
     9
use crate::dirstate_tree::on_disk::{for_each_tracked_path, read_docket};
46511
43d63979a75e rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents: 46508
diff changeset
    10
use crate::errors::HgError;
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46136
diff changeset
    11
use crate::repo::Repo;
45542
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    12
use crate::revlog::changelog::Changelog;
47985
d44740725b95 rust: Rename Manifest to Manifestlog, ManifestEntry to Manifest
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    13
use crate::revlog::manifest::{Manifest, Manifestlog};
46501
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
    14
use crate::revlog::node::Node;
45542
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    15
use crate::revlog::revlog::RevlogError;
45379
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    16
use crate::utils::hg_path::HgPath;
47380
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    17
use crate::DirstateError;
45379
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    18
use rayon::prelude::*;
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    19
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    20
/// 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
    21
/// by reading the dirstate
46136
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    22
pub struct Dirstate {
45541
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45447
diff changeset
    23
    /// The `dirstate` content.
45379
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    24
    content: Vec<u8>,
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    25
    v2_metadata: Option<Vec<u8>>,
45379
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    26
}
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    27
46136
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    28
impl Dirstate {
46508
776b97179c06 rust: Remove DirstateParseError and ListDirstateTrackedFilesError
Simon Sapin <simon.sapin@octobus.net>
parents: 46505
diff changeset
    29
    pub fn new(repo: &Repo) -> Result<Self, HgError> {
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47380
diff changeset
    30
        let mut content = repo.hg_vfs().read("dirstate")?;
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    31
        let v2_metadata = if repo.has_dirstate_v2() {
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47380
diff changeset
    32
            let docket = read_docket(&content)?;
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    33
            let meta = docket.tree_metadata().to_vec();
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47380
diff changeset
    34
            content = repo.hg_vfs().read(docket.data_filename())?;
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    35
            Some(meta)
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    36
        } else {
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    37
            None
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    38
        };
47380
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    39
        Ok(Self {
47674
ff97e793ed36 dirstate-v2: Introduce a docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47380
diff changeset
    40
            content,
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    41
            v2_metadata,
47380
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    42
        })
45541
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45447
diff changeset
    43
    }
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45447
diff changeset
    44
47380
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    45
    pub fn tracked_files(&self) -> Result<Vec<&HgPath>, DirstateError> {
47379
d2fb8b4adcc3 rhg: Remove some intermediate Vecs in `rhg files`
Simon Sapin <simon.sapin@octobus.net>
parents: 46511
diff changeset
    46
        let mut files = Vec::new();
47380
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    47
        if !self.content.is_empty() {
47682
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    48
            if let Some(meta) = &self.v2_metadata {
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    49
                for_each_tracked_path(&self.content, meta, |path| {
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    50
                    files.push(path)
78f7f0d490ee dirstate-v2: Move fixed-size tree metadata into the docket file
Simon Sapin <simon.sapin@octobus.net>
parents: 47674
diff changeset
    51
                })?
47380
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    52
            } else {
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    53
                let _parents = parse_dirstate_entries(
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    54
                    &self.content,
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    55
                    |path, entry, _copy_source| {
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    56
                        if entry.state.is_tracked() {
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    57
                            files.push(path)
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    58
                        }
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    59
                        Ok(())
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    60
                    },
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    61
                )?;
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    62
            }
bd88b6bfd8da rhg: Add support for dirstate-v2
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
    63
        }
45379
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    64
        files.par_sort_unstable();
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    65
        Ok(files)
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    66
    }
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    67
}
45542
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    68
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    69
/// List files under Mercurial control at a given revision.
46136
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    70
pub fn list_rev_tracked_files(
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46136
diff changeset
    71
    repo: &Repo,
46501
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
    72
    revset: &str,
46505
b274aa2f20fd rust: remove three enums that were identical to `RevlogError`
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
    73
) -> Result<FilesForRev, RevlogError> {
46501
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
    74
    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: 46136
diff changeset
    75
    let changelog = Changelog::open(repo)?;
47985
d44740725b95 rust: Rename Manifest to Manifestlog, ManifestEntry to Manifest
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    76
    let manifest = Manifestlog::open(repo)?;
46501
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46499
diff changeset
    77
    let changelog_entry = changelog.get_rev(rev)?;
46511
43d63979a75e rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents: 46508
diff changeset
    78
    let manifest_node =
43d63979a75e rust: use HgError in RevlogError and Vfs
Simon Sapin <simon.sapin@octobus.net>
parents: 46508
diff changeset
    79
        Node::from_hex_for_repo(&changelog_entry.manifest_node()?)?;
46499
645ee7225fab rust: Make NodePrefix allocation-free and Copy, remove NodePrefixRef
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
    80
    let manifest_entry = manifest.get_node(manifest_node.into())?;
46136
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    81
    Ok(FilesForRev(manifest_entry))
45542
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    82
}
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    83
47985
d44740725b95 rust: Rename Manifest to Manifestlog, ManifestEntry to Manifest
Simon Sapin <simon.sapin@octobus.net>
parents: 47682
diff changeset
    84
pub struct FilesForRev(Manifest);
45542
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    85
46136
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    86
impl FilesForRev {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    87
    pub fn iter(&self) -> impl Iterator<Item = &HgPath> {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    88
        self.0.files()
45542
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    89
    }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45541
diff changeset
    90
}