##// END OF EJS Templates
rhg: Propogate manifest parse errors instead of panicking...
rhg: Propogate manifest parse errors instead of panicking The Rust parser for the manifest file format is an iterator. Now every item from that iterator is a `Result`, which makes error handling required in multiple new places. This makes better recovery on errors possible, compare to a run time panic. Differential Revision: https://phab.mercurial-scm.org/D11771

File last commit:

r49165:10c32e1b default
r49165:10c32e1b default
Show More
cat.rs
119 lines | 3.3 KiB | application/rls-services+xml | RustLexer
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 // 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
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 use crate::repo::Repo;
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 use crate::revlog::revlog::RevlogError;
Simon Sapin
rust: use NodePrefix::from_hex instead of hex::decode directly...
r46647 use crate::revlog::Node;
Simon Sapin
rust: Add a Filelog struct that wraps Revlog...
r48775
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 use crate::utils::hg_path::HgPath;
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 use crate::errors::HgError;
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 use itertools::put_back;
use itertools::PutBack;
use std::cmp::Ordering;
Arseniy Alekseyev
rhg: faster hg cat when many files are requested...
r49038
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 pub struct CatOutput<'a> {
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 /// Whether any file in the manifest matched the paths given as CLI
/// arguments
pub found_any: bool,
/// The contents of matching files, in manifest order
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 pub results: Vec<(&'a HgPath, Vec<u8>)>,
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 /// Which of the CLI arguments did not match any manifest file
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 pub missing: Vec<&'a HgPath>,
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 /// The node ID that the given revset was resolved to
pub node: Node,
}
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 // Find an item in an iterator over a sorted collection.
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 fn find_item<'a, D, I: Iterator<Item = Result<(&'a HgPath, D), HgError>>>(
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 i: &mut PutBack<I>,
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 needle: &HgPath,
) -> Result<Option<D>, HgError> {
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 loop {
match i.next() {
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 None => return Ok(None),
Some(result) => {
let (path, value) = result?;
match needle.as_bytes().cmp(path.as_bytes()) {
Ordering::Less => {
i.put_back(Ok((path, value)));
return Ok(None);
}
Ordering::Greater => continue,
Ordering::Equal => return Ok(Some(value)),
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 }
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 }
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 }
}
}
fn find_files_in_manifest<
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 'manifest,
'query,
Data,
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 Manifest: Iterator<Item = Result<(&'manifest HgPath, Data), HgError>>,
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 Query: Iterator<Item = &'query HgPath>,
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 >(
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 manifest: Manifest,
query: Query,
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 ) -> Result<(Vec<(&'query HgPath, Data)>, Vec<&'query HgPath>), HgError> {
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 let mut manifest = put_back(manifest);
let mut res = vec![];
let mut missing = vec![];
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 for file in query {
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 match find_item(&mut manifest, file)? {
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 None => missing.push(file),
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 Some(item) => res.push((file, item)),
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 }
}
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 return Ok((res, missing));
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 }
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 /// Output the given revision of files
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 ///
/// * `root`: Repository root
/// * `rev`: The revision to cat the files from.
/// * `files`: The files to output.
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 pub fn cat<'a>(
Simon Sapin
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 repo: &Repo,
Simon Sapin
rhg: centralize parsing of `--rev` CLI arguments...
r47162 revset: &str,
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 mut files: Vec<&'a HgPath>,
) -> Result<CatOutput<'a>, RevlogError> {
Simon Sapin
rhg: centralize parsing of `--rev` CLI arguments...
r47162 let rev = crate::revset::resolve_single(revset, repo)?;
Simon Sapin
rhg: Reuse manifest when checking status of multiple ambiguous files...
r48778 let manifest = repo.manifest_for_rev(rev)?;
Simon Sapin
rust: Add Repo::manifest(revision)...
r48774 let node = *repo
.changelog()?
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 .node_from_rev(rev)
Simon Sapin
rust: Add Repo::manifest(revision)...
r48774 .expect("should succeed when repo.manifest did");
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 let mut results: Vec<(&'a HgPath, Vec<u8>)> = vec![];
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 let mut found_any = false;
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039
Arseniy Alekseyev
rhg: faster hg cat when many files are requested...
r49038 files.sort_unstable();
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 let (found, missing) = find_files_in_manifest(
manifest.files_with_nodes(),
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 files.into_iter().map(|f| f.as_ref()),
Simon Sapin
rhg: Propogate manifest parse errors instead of panicking...
r49165 )?;
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 for (file_path, node_bytes) in found {
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 found_any = true;
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 let file_log = repo.filelog(file_path)?;
Arseniy Alekseyev
rhg: stop manifest traversal when no more files are needed...
r49039 let file_node = Node::from_hex_for_repo(node_bytes)?;
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 results.push((
file_path,
file_log.data_for_node(file_node)?.into_data()?,
));
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 }
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 Ok(CatOutput {
found_any,
Arseniy Alekseyev
rhg: internally, return a structured representation from hg cat...
r49051 results,
Simon Sapin
rhg: `cat` command: print error messages for missing files...
r47478 missing,
node,
})
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 }