Mercurial > public > mercurial-scm > hg-stable
diff rust/hg-core/src/operations/cat.rs @ 47989:4d2a5ca060e3
rust: Add a Filelog struct that wraps Revlog
Some filelog-specific logic is moved from code `rhg cat` into this struct
where it can better be reused.
Additionally, a missing end delimiter for metadata causes an error
to be returned instead of being silently ignored.
Differential Revision: https://phab.mercurial-scm.org/D11408
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 13 Sep 2021 15:42:39 +0200 |
parents | cfb6e6699b25 |
children | 796206e74b10 |
line wrap: on
line diff
--- a/rust/hg-core/src/operations/cat.rs Mon Sep 13 13:45:10 2021 +0200 +++ b/rust/hg-core/src/operations/cat.rs Mon Sep 13 15:42:39 2021 +0200 @@ -5,15 +5,11 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. -use std::path::PathBuf; - use crate::repo::Repo; -use crate::revlog::path_encode::path_encode; -use crate::revlog::revlog::Revlog; use crate::revlog::revlog::RevlogError; use crate::revlog::Node; -use crate::utils::files::get_path_from_bytes; -use crate::utils::hg_path::{HgPath, HgPathBuf}; + +use crate::utils::hg_path::HgPathBuf; pub struct CatOutput { /// Whether any file in the manifest matched the paths given as CLI @@ -27,8 +23,6 @@ pub node: Node, } -const METADATA_DELIMITER: [u8; 2] = [b'\x01', b'\n']; - /// Output the given revision of files /// /// * `root`: Repository root @@ -54,26 +48,10 @@ if cat_file.as_bytes() == manifest_file.as_bytes() { *is_matched = true; found_any = true; - let index_path = store_path(manifest_file, b".i"); - let data_path = store_path(manifest_file, b".d"); - - let file_log = - Revlog::open(repo, &index_path, Some(&data_path))?; + let file_log = repo.filelog(manifest_file)?; let file_node = Node::from_hex_for_repo(node_bytes)?; - let file_rev = file_log.get_node_rev(file_node.into())?; - let data = file_log.get_rev_data(file_rev)?; - if data.starts_with(&METADATA_DELIMITER) { - let end_delimiter_position = data - [METADATA_DELIMITER.len()..] - .windows(METADATA_DELIMITER.len()) - .position(|bytes| bytes == METADATA_DELIMITER); - if let Some(position) = end_delimiter_position { - let offset = METADATA_DELIMITER.len() * 2; - bytes.extend(data[position + offset..].iter()); - } - } else { - bytes.extend(data); - } + let entry = file_log.get_node(file_node)?; + bytes.extend(entry.data()?) } } } @@ -91,9 +69,3 @@ node, }) } - -fn store_path(hg_path: &HgPath, suffix: &[u8]) -> PathBuf { - let encoded_bytes = - path_encode(&[b"data/", hg_path.as_bytes(), suffix].concat()); - get_path_from_bytes(&encoded_bytes).into() -}