Mercurial > public > mercurial-scm > hg-stable
diff rust/hg-core/src/operations/cat.rs @ 48249:027ebad952ac
rhg: internally, return a structured representation from hg cat
The purpose of this change is to make it possible to support limited templating in `hg cat`, so we could print separators between files etc.
The templating itself is not implemented yet, so this functionality is unused in `rhg cat`.
However, in our fork of hg we're implementing a slightly different command `hg jscat` which makes use of this.
So accepting this change will let us minimize the size of the patch we're maintaining on our side.
Differential Revision: https://phab.mercurial-scm.org/D11679
author | Arseniy Alekseyev <aalekseyev@janestreet.com> |
---|---|
date | Fri, 15 Oct 2021 14:05:20 +0100 |
parents | 1837663ac216 |
children | 10c32e1b892a |
line wrap: on
line diff
--- a/rust/hg-core/src/operations/cat.rs Thu Oct 14 19:02:08 2021 +0100 +++ b/rust/hg-core/src/operations/cat.rs Fri Oct 15 14:05:20 2021 +0100 @@ -10,20 +10,19 @@ use crate::revlog::Node; use crate::utils::hg_path::HgPath; -use crate::utils::hg_path::HgPathBuf; use itertools::put_back; use itertools::PutBack; use std::cmp::Ordering; -pub struct CatOutput { +pub struct CatOutput<'a> { /// 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 - pub concatenated: Vec<u8>, + pub results: Vec<(&'a HgPath, Vec<u8>)>, /// Which of the CLI arguments did not match any manifest file - pub missing: Vec<HgPathBuf>, + pub missing: Vec<&'a HgPath>, /// The node ID that the given revset was resolved to pub node: Node, } @@ -32,7 +31,7 @@ fn find_item<'a, 'b, 'c, D, I: Iterator<Item = (&'a HgPath, D)>>( i: &mut PutBack<I>, needle: &'b HgPath, -) -> Option<I::Item> { +) -> Option<D> { loop { match i.next() { None => return None, @@ -42,30 +41,30 @@ return None; } Ordering::Greater => continue, - Ordering::Equal => return Some(val), + Ordering::Equal => return Some(val.1), }, } } } fn find_files_in_manifest< - 'a, - 'b, - D, - I: Iterator<Item = (&'a HgPath, D)>, - J: Iterator<Item = &'b HgPath>, + 'manifest, + 'query, + Data, + Manifest: Iterator<Item = (&'manifest HgPath, Data)>, + Query: Iterator<Item = &'query HgPath>, >( - manifest: I, - files: J, -) -> (Vec<(&'a HgPath, D)>, Vec<&'b HgPath>) { + manifest: Manifest, + query: Query, +) -> (Vec<(&'query HgPath, Data)>, Vec<&'query HgPath>) { let mut manifest = put_back(manifest); let mut res = vec![]; let mut missing = vec![]; - for file in files { + for file in query { match find_item(&mut manifest, file) { None => missing.push(file), - Some(item) => res.push(item), + Some(item) => res.push((file, item)), } } return (res, missing); @@ -79,36 +78,37 @@ pub fn cat<'a>( repo: &Repo, revset: &str, - mut files: Vec<HgPathBuf>, -) -> Result<CatOutput, RevlogError> { + mut files: Vec<&'a HgPath>, +) -> Result<CatOutput<'a>, RevlogError> { let rev = crate::revset::resolve_single(revset, repo)?; let manifest = repo.manifest_for_rev(rev)?; let node = *repo .changelog()? .node_from_rev(rev) .expect("should succeed when repo.manifest did"); - let mut bytes: Vec<u8> = vec![]; + let mut results: Vec<(&'a HgPath, Vec<u8>)> = vec![]; let mut found_any = false; files.sort_unstable(); let (found, missing) = find_files_in_manifest( manifest.files_with_nodes(), - files.iter().map(|f| f.as_ref()), + files.into_iter().map(|f| f.as_ref()), ); - for (manifest_file, node_bytes) in found { + for (file_path, node_bytes) in found { found_any = true; - let file_log = repo.filelog(manifest_file)?; + let file_log = repo.filelog(file_path)?; let file_node = Node::from_hex_for_repo(node_bytes)?; - bytes.extend(file_log.data_for_node(file_node)?.data()?); + results.push(( + file_path, + file_log.data_for_node(file_node)?.into_data()?, + )); } - let missing: Vec<HgPathBuf> = - missing.iter().map(|file| (*file).to_owned()).collect(); Ok(CatOutput { found_any, - concatenated: bytes, + results, missing, node, })