list_tracked_files.rs
84 lines
| 2.6 KiB
| application/rls-services+xml
|
RustLexer
Antoine Cezar
|
r45918 | // list_tracked_files.rs | ||
// | ||||
// Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net> | ||||
// | ||||
// This software may be used and distributed according to the terms of the | ||||
// GNU General Public License version 2 or any later version. | ||||
Simon Sapin
|
r48164 | use crate::dirstate::parsers::parse_dirstate_entries; | ||
Simon Sapin
|
r48474 | use crate::dirstate_tree::on_disk::{for_each_tracked_path, read_docket}; | ||
Simon Sapin
|
r47172 | use crate::errors::HgError; | ||
Simon Sapin
|
r46782 | use crate::repo::Repo; | ||
Antoine Cezar
|
r46107 | use crate::revlog::changelog::Changelog; | ||
use crate::revlog::manifest::{Manifest, ManifestEntry}; | ||||
Simon Sapin
|
r47162 | use crate::revlog::node::Node; | ||
Antoine Cezar
|
r46107 | use crate::revlog::revlog::RevlogError; | ||
Antoine Cezar
|
r45918 | use crate::utils::hg_path::HgPath; | ||
Simon Sapin
|
r48165 | use crate::DirstateError; | ||
Antoine Cezar
|
r45918 | use rayon::prelude::*; | ||
/// List files under Mercurial control in the working directory | ||||
/// by reading the dirstate | ||||
Simon Sapin
|
r46751 | pub struct Dirstate { | ||
Antoine Cezar
|
r46106 | /// The `dirstate` content. | ||
Antoine Cezar
|
r45918 | content: Vec<u8>, | ||
Simon Sapin
|
r48165 | dirstate_v2: bool, | ||
Antoine Cezar
|
r45918 | } | ||
Simon Sapin
|
r46751 | impl Dirstate { | ||
Simon Sapin
|
r47169 | pub fn new(repo: &Repo) -> Result<Self, HgError> { | ||
Simon Sapin
|
r48474 | let mut content = repo.hg_vfs().read("dirstate")?; | ||
if repo.has_dirstate_v2() { | ||||
let docket = read_docket(&content)?; | ||||
content = repo.hg_vfs().read(docket.data_filename())?; | ||||
} | ||||
Simon Sapin
|
r48165 | Ok(Self { | ||
Simon Sapin
|
r48474 | content, | ||
Simon Sapin
|
r48165 | dirstate_v2: repo.has_dirstate_v2(), | ||
}) | ||||
Antoine Cezar
|
r46106 | } | ||
Simon Sapin
|
r48165 | pub fn tracked_files(&self) -> Result<Vec<&HgPath>, DirstateError> { | ||
Simon Sapin
|
r48164 | let mut files = Vec::new(); | ||
Simon Sapin
|
r48165 | if !self.content.is_empty() { | ||
if self.dirstate_v2 { | ||||
for_each_tracked_path(&self.content, |path| files.push(path))? | ||||
} else { | ||||
let _parents = parse_dirstate_entries( | ||||
&self.content, | ||||
|path, entry, _copy_source| { | ||||
if entry.state.is_tracked() { | ||||
files.push(path) | ||||
} | ||||
Ok(()) | ||||
}, | ||||
)?; | ||||
} | ||||
} | ||||
Antoine Cezar
|
r45918 | files.par_sort_unstable(); | ||
Ok(files) | ||||
} | ||||
} | ||||
Antoine Cezar
|
r46107 | |||
/// List files under Mercurial control at a given revision. | ||||
Simon Sapin
|
r46751 | pub fn list_rev_tracked_files( | ||
Simon Sapin
|
r46782 | repo: &Repo, | ||
Simon Sapin
|
r47162 | revset: &str, | ||
Simon Sapin
|
r47166 | ) -> Result<FilesForRev, RevlogError> { | ||
Simon Sapin
|
r47162 | let rev = crate::revset::resolve_single(revset, repo)?; | ||
Simon Sapin
|
r46782 | let changelog = Changelog::open(repo)?; | ||
let manifest = Manifest::open(repo)?; | ||||
Simon Sapin
|
r47162 | let changelog_entry = changelog.get_rev(rev)?; | ||
Simon Sapin
|
r47172 | let manifest_node = | ||
Node::from_hex_for_repo(&changelog_entry.manifest_node()?)?; | ||||
Simon Sapin
|
r47160 | let manifest_entry = manifest.get_node(manifest_node.into())?; | ||
Simon Sapin
|
r46751 | Ok(FilesForRev(manifest_entry)) | ||
Antoine Cezar
|
r46107 | } | ||
Simon Sapin
|
r46751 | pub struct FilesForRev(ManifestEntry); | ||
Antoine Cezar
|
r46107 | |||
Simon Sapin
|
r46751 | impl FilesForRev { | ||
pub fn iter(&self) -> impl Iterator<Item = &HgPath> { | ||||
self.0.files() | ||||
Antoine Cezar
|
r46107 | } | ||
} | ||||