##// END OF EJS Templates
hooks: introduce a `:run-with-plain` option for hooks...
hooks: introduce a `:run-with-plain` option for hooks This option control if HGPLAIN should be set or not for the hooks. This is the first step to give user some control of the HGPLAIN setting for they hooks. Some hooks (eg: consistency checking) deserve to be run with HGPLAIN, some other (eg: user set visual helper) might need to respect the user config and setting. So both usage are valid and we need to restore the ability to run -without- HGPLAIN that got lost in Mercurial 5.7. This does not offer a way to restore the pre-5.7 behavior yet (respect whatever HGPLAIN setting from the shell), this will be dealt with in the next changeset. The option name is a bit verbose because implementing this highlighs the need for another option: `:run-if-plain`. That would make it possible for some hooks to be easily disabled if HG PLAIN is set. However such option would be a new feature, not something introduced to mitigate a behavior change introduced in 5.7, so the `:run-if-plain` option belong to the default branch and is not part of this series. Differential Revision: https://phab.mercurial-scm.org/D9981

File last commit:

r47172:43d63979 default
r47242:7289eac7 stable
Show More
cat.rs
135 lines | 4.6 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.
use std::convert::From;
Simon Sapin
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 use std::path::PathBuf;
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112
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::changelog::Changelog;
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 use crate::revlog::manifest::Manifest;
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 use crate::revlog::path_encode::path_encode;
use crate::revlog::revlog::Revlog;
use crate::revlog::revlog::RevlogError;
Simon Sapin
rust: use NodePrefix::from_hex instead of hex::decode directly...
r46647 use crate::revlog::Node;
use crate::revlog::NodePrefix;
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 use crate::revlog::Revision;
Antoine cezar
hg-core: fix path encoding usage...
r46408 use crate::utils::files::get_path_from_bytes;
use crate::utils::hg_path::{HgPath, HgPathBuf};
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112
Antoine cezar
rhg: strip copied files metadata from `cat` output...
r46406 const METADATA_DELIMITER: [u8; 2] = [b'\x01', b'\n'];
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 /// Kind of error encountered by `CatRev`
#[derive(Debug)]
pub enum CatRevErrorKind {
/// Error when reading a `revlog` file.
IoError(std::io::Error),
/// The revision has not been found.
InvalidRevision,
Simon Sapin
rhg: allow specifying a changeset ID prefix...
r46646 /// Found more than one revision whose ID match the requested prefix
AmbiguousPrefix,
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 /// A `revlog` file is corrupted.
CorruptedRevlog,
/// The `revlog` format version is not supported.
UnsuportedRevlogVersion(u16),
/// The `revlog` data format is not supported.
UnknowRevlogDataFormat(u8),
}
/// A `CatRev` error
#[derive(Debug)]
pub struct CatRevError {
/// Kind of error encountered by `CatRev`
pub kind: CatRevErrorKind,
}
impl From<CatRevErrorKind> for CatRevError {
fn from(kind: CatRevErrorKind) -> Self {
CatRevError { kind }
}
}
impl From<RevlogError> for CatRevError {
fn from(err: RevlogError) -> Self {
match err {
RevlogError::IoError(err) => CatRevErrorKind::IoError(err),
RevlogError::UnsuportedVersion(version) => {
CatRevErrorKind::UnsuportedRevlogVersion(version)
}
RevlogError::InvalidRevision => CatRevErrorKind::InvalidRevision,
Simon Sapin
rhg: allow specifying a changeset ID prefix...
r46646 RevlogError::AmbiguousPrefix => CatRevErrorKind::AmbiguousPrefix,
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 RevlogError::Corrupted => CatRevErrorKind::CorruptedRevlog,
RevlogError::UnknowDataFormat(format) => {
CatRevErrorKind::UnknowRevlogDataFormat(format)
}
}
.into()
}
}
/// List files under Mercurial control at a given revision.
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.
pub fn cat(
Simon Sapin
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 repo: &Repo,
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 rev: &str,
files: &[HgPathBuf],
) -> Result<Vec<u8>, CatRevError> {
Simon Sapin
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 let changelog = Changelog::open(repo)?;
let manifest = Manifest::open(repo)?;
Simon Sapin
rust: replace most "operation" structs with functions...
r46751
let changelog_entry = match rev.parse::<Revision>() {
Ok(rev) => changelog.get_rev(rev)?,
_ => {
let changelog_node = NodePrefix::from_hex(&rev)
.map_err(|_| CatRevErrorKind::InvalidRevision)?;
changelog.get_node(changelog_node.borrow())?
}
};
let manifest_node = Node::from_hex(&changelog_entry.manifest_node()?)
.map_err(|_| CatRevErrorKind::CorruptedRevlog)?;
let manifest_entry = manifest.get_node((&manifest_node).into())?;
let mut bytes = vec![];
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 for (manifest_file, node_bytes) in manifest_entry.files_with_nodes() {
for cat_file in files.iter() {
if cat_file.as_bytes() == manifest_file.as_bytes() {
Simon Sapin
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 let index_path = store_path(manifest_file, b".i");
let data_path = store_path(manifest_file, b".d");
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112
Simon Sapin
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 let file_log =
Revlog::open(repo, &index_path, Some(&data_path))?;
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 let file_node = Node::from_hex(node_bytes)
.map_err(|_| CatRevErrorKind::CorruptedRevlog)?;
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);
}
}
}
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 }
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 Ok(bytes)
Antoine Cezar
hg-core: add a `CatRev` operation...
r46112 }
Antoine cezar
hg-core: fix path encoding usage...
r46408
Simon Sapin
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 fn store_path(hg_path: &HgPath, suffix: &[u8]) -> PathBuf {
Antoine cezar
hg-core: fix path encoding usage...
r46408 let encoded_bytes =
path_encode(&[b"data/", hg_path.as_bytes(), suffix].concat());
Simon Sapin
rust: introduce Repo and Vfs types for filesystem abstraction...
r46782 get_path_from_bytes(&encoded_bytes).into()
Antoine cezar
hg-core: fix path encoding usage...
r46408 }