list_tracked_files.rs
72 lines
| 2.2 KiB
| application/rls-services+xml
|
RustLexer
|
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. | ||||
use crate::dirstate::parsers::parse_dirstate; | ||||
|
r47169 | use crate::errors::{HgError, IoResultExt}; | ||
|
r46782 | use crate::repo::Repo; | ||
|
r46107 | use crate::revlog::changelog::Changelog; | ||
use crate::revlog::manifest::{Manifest, ManifestEntry}; | ||||
|
r47162 | use crate::revlog::node::Node; | ||
|
r46107 | use crate::revlog::revlog::RevlogError; | ||
|
r45918 | use crate::utils::hg_path::HgPath; | ||
|
r47169 | use crate::EntryState; | ||
|
r45918 | use rayon::prelude::*; | ||
/// List files under Mercurial control in the working directory | ||||
/// by reading the dirstate | ||||
|
r46751 | pub struct Dirstate { | ||
|
r46106 | /// The `dirstate` content. | ||
|
r45918 | content: Vec<u8>, | ||
} | ||||
|
r46751 | impl Dirstate { | ||
|
r47169 | pub fn new(repo: &Repo) -> Result<Self, HgError> { | ||
let content = repo | ||||
.hg_vfs() | ||||
.read("dirstate") | ||||
// TODO: this will be more accurate when we use `HgError` in | ||||
// `Vfs::read`. | ||||
.for_file("dirstate".as_ref())?; | ||||
|
r46106 | Ok(Self { content }) | ||
} | ||||
|
r47169 | pub fn tracked_files(&self) -> Result<Vec<&HgPath>, HgError> { | ||
let (_, entries, _) = parse_dirstate(&self.content)?; | ||||
|
r45918 | let mut files: Vec<&HgPath> = entries | ||
.into_iter() | ||||
.filter_map(|(path, entry)| match entry.state { | ||||
EntryState::Removed => None, | ||||
_ => Some(path), | ||||
}) | ||||
.collect(); | ||||
files.par_sort_unstable(); | ||||
Ok(files) | ||||
} | ||||
} | ||||
|
r46107 | |||
/// List files under Mercurial control at a given revision. | ||||
|
r46751 | pub fn list_rev_tracked_files( | ||
|
r46782 | repo: &Repo, | ||
|
r47162 | revset: &str, | ||
|
r47166 | ) -> Result<FilesForRev, RevlogError> { | ||
|
r47162 | let rev = crate::revset::resolve_single(revset, repo)?; | ||
|
r46782 | let changelog = Changelog::open(repo)?; | ||
let manifest = Manifest::open(repo)?; | ||||
|
r47162 | let changelog_entry = changelog.get_rev(rev)?; | ||
|
r46751 | let manifest_node = Node::from_hex(&changelog_entry.manifest_node()?) | ||
|
r47166 | .map_err(|_| RevlogError::Corrupted)?; | ||
|
r47160 | let manifest_entry = manifest.get_node(manifest_node.into())?; | ||
|
r46751 | Ok(FilesForRev(manifest_entry)) | ||
|
r46107 | } | ||
|
r46751 | pub struct FilesForRev(ManifestEntry); | ||
|
r46107 | |||
|
r46751 | impl FilesForRev { | ||
pub fn iter(&self) -> impl Iterator<Item = &HgPath> { | ||||
self.0.files() | ||||
|
r46107 | } | ||
} | ||||