diff --git a/rust/hg-core/src/config/layer.rs b/rust/hg-core/src/config/layer.rs --- a/rust/hg-core/src/config/layer.rs +++ b/rust/hg-core/src/config/layer.rs @@ -1,319 +1,323 @@ // layer.rs // // Copyright 2020 // Valentin Gatien-Baron, // Raphaël Gomès // // 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::errors::HgError; +use crate::exit_codes::CONFIG_PARSE_ERROR_ABORT; use crate::utils::files::{get_bytes_from_path, get_path_from_bytes}; use format_bytes::{format_bytes, write_bytes, DisplayBytes}; use lazy_static::lazy_static; use regex::bytes::Regex; use std::collections::HashMap; use std::path::{Path, PathBuf}; lazy_static! { static ref SECTION_RE: Regex = make_regex(r"^\[([^\[]+)\]"); static ref ITEM_RE: Regex = make_regex(r"^([^=\s][^=]*?)\s*=\s*((.*\S)?)"); /// Continuation whitespace static ref CONT_RE: Regex = make_regex(r"^\s+(\S|\S.*\S)\s*$"); static ref EMPTY_RE: Regex = make_regex(r"^(;|#|\s*$)"); static ref COMMENT_RE: Regex = make_regex(r"^(;|#)"); /// A directive that allows for removing previous entries static ref UNSET_RE: Regex = make_regex(r"^%unset\s+(\S+)"); /// A directive that allows for including other config files static ref INCLUDE_RE: Regex = make_regex(r"^%include\s+(\S|\S.*\S)\s*$"); } /// All config values separated by layers of precedence. /// Each config source may be split in multiple layers if `%include` directives /// are used. /// TODO detail the general precedence #[derive(Clone)] pub struct ConfigLayer { /// Mapping of the sections to their items sections: HashMap, ConfigItem>, /// All sections (and their items/values) in a layer share the same origin pub origin: ConfigOrigin, /// Whether this layer comes from a trusted user or group pub trusted: bool, } impl ConfigLayer { pub fn new(origin: ConfigOrigin) -> Self { ConfigLayer { sections: HashMap::new(), trusted: true, // TODO check origin, } } /// Parse `--config` CLI arguments and return a layer if there’s any pub(crate) fn parse_cli_args( cli_config_args: impl IntoIterator>, ) -> Result, ConfigError> { fn parse_one(arg: &[u8]) -> Option<(Vec, Vec, Vec)> { use crate::utils::SliceExt; let (section_and_item, value) = arg.split_2(b'=')?; let (section, item) = section_and_item.trim().split_2(b'.')?; Some(( section.to_owned(), item.to_owned(), value.trim().to_owned(), )) } let mut layer = Self::new(ConfigOrigin::CommandLine); for arg in cli_config_args { let arg = arg.as_ref(); if let Some((section, item, value)) = parse_one(arg) { layer.add(section, item, value, None); } else { - Err(HgError::abort(format!( - "abort: malformed --config option: '{}' \ + Err(HgError::abort( + format!( + "abort: malformed --config option: '{}' \ (use --config section.name=value)", - String::from_utf8_lossy(arg), - )))? + String::from_utf8_lossy(arg), + ), + CONFIG_PARSE_ERROR_ABORT, + ))? } } if layer.sections.is_empty() { Ok(None) } else { Ok(Some(layer)) } } /// Returns whether this layer comes from `--config` CLI arguments pub(crate) fn is_from_command_line(&self) -> bool { if let ConfigOrigin::CommandLine = self.origin { true } else { false } } /// Add an entry to the config, overwriting the old one if already present. pub fn add( &mut self, section: Vec, item: Vec, value: Vec, line: Option, ) { self.sections .entry(section) .or_insert_with(|| HashMap::new()) .insert(item, ConfigValue { bytes: value, line }); } /// Returns the config value in `
.` if it exists pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&ConfigValue> { Some(self.sections.get(section)?.get(item)?) } /// Returns the keys defined in the given section pub fn iter_keys(&self, section: &[u8]) -> impl Iterator { self.sections .get(section) .into_iter() .flat_map(|section| section.keys().map(|vec| &**vec)) } pub fn is_empty(&self) -> bool { self.sections.is_empty() } /// Returns a `Vec` of layers in order of precedence (so, in read order), /// recursively parsing the `%include` directives if any. pub fn parse(src: &Path, data: &[u8]) -> Result, ConfigError> { let mut layers = vec![]; // Discard byte order mark if any let data = if data.starts_with(b"\xef\xbb\xbf") { &data[3..] } else { data }; // TODO check if it's trusted let mut current_layer = Self::new(ConfigOrigin::File(src.to_owned())); let mut lines_iter = data.split(|b| *b == b'\n').enumerate().peekable(); let mut section = b"".to_vec(); while let Some((index, bytes)) = lines_iter.next() { let line = Some(index + 1); if let Some(m) = INCLUDE_RE.captures(&bytes) { let filename_bytes = &m[1]; let filename_bytes = crate::utils::expand_vars(filename_bytes); // `Path::parent` only fails for the root directory, // which `src` can’t be since we’ve managed to open it as a // file. let dir = src .parent() .expect("Path::parent fail on a file we’ve read"); // `Path::join` with an absolute argument correctly ignores the // base path let filename = dir.join(&get_path_from_bytes(&filename_bytes)); match std::fs::read(&filename) { Ok(data) => { layers.push(current_layer); layers.extend(Self::parse(&filename, &data)?); current_layer = Self::new(ConfigOrigin::File(src.to_owned())); } Err(error) => { if error.kind() != std::io::ErrorKind::NotFound { return Err(ConfigParseError { origin: ConfigOrigin::File(src.to_owned()), line, message: format_bytes!( b"cannot include {} ({})", filename_bytes, format_bytes::Utf8(error) ), } .into()); } } } } else if let Some(_) = EMPTY_RE.captures(&bytes) { } else if let Some(m) = SECTION_RE.captures(&bytes) { section = m[1].to_vec(); } else if let Some(m) = ITEM_RE.captures(&bytes) { let item = m[1].to_vec(); let mut value = m[2].to_vec(); loop { match lines_iter.peek() { None => break, Some((_, v)) => { if let Some(_) = COMMENT_RE.captures(&v) { } else if let Some(_) = CONT_RE.captures(&v) { value.extend(b"\n"); value.extend(&m[1]); } else { break; } } }; lines_iter.next(); } current_layer.add(section.clone(), item, value, line); } else if let Some(m) = UNSET_RE.captures(&bytes) { if let Some(map) = current_layer.sections.get_mut(§ion) { map.remove(&m[1]); } } else { let message = if bytes.starts_with(b" ") { format_bytes!(b"unexpected leading whitespace: {}", bytes) } else { bytes.to_owned() }; return Err(ConfigParseError { origin: ConfigOrigin::File(src.to_owned()), line, message, } .into()); } } if !current_layer.is_empty() { layers.push(current_layer); } Ok(layers) } } impl DisplayBytes for ConfigLayer { fn display_bytes( &self, out: &mut dyn std::io::Write, ) -> std::io::Result<()> { let mut sections: Vec<_> = self.sections.iter().collect(); sections.sort_by(|e0, e1| e0.0.cmp(e1.0)); for (section, items) in sections.into_iter() { let mut items: Vec<_> = items.into_iter().collect(); items.sort_by(|e0, e1| e0.0.cmp(e1.0)); for (item, config_entry) in items { write_bytes!( out, b"{}.{}={} # {}\n", section, item, &config_entry.bytes, &self.origin, )? } } Ok(()) } } /// Mapping of section item to value. /// In the following: /// ```text /// [ui] /// paginate=no /// ``` /// "paginate" is the section item and "no" the value. pub type ConfigItem = HashMap, ConfigValue>; #[derive(Clone, Debug, PartialEq)] pub struct ConfigValue { /// The raw bytes of the value (be it from the CLI, env or from a file) pub bytes: Vec, /// Only present if the value comes from a file, 1-indexed. pub line: Option, } #[derive(Clone, Debug)] pub enum ConfigOrigin { /// From a configuration file File(PathBuf), /// From a `--config` CLI argument CommandLine, /// From environment variables like `$PAGER` or `$EDITOR` Environment(Vec), /* TODO cli * TODO defaults (configitems.py) * TODO extensions * TODO Python resources? * Others? */ } impl DisplayBytes for ConfigOrigin { fn display_bytes( &self, out: &mut dyn std::io::Write, ) -> std::io::Result<()> { match self { ConfigOrigin::File(p) => out.write_all(&get_bytes_from_path(p)), ConfigOrigin::CommandLine => out.write_all(b"--config"), ConfigOrigin::Environment(e) => write_bytes!(out, b"${}", e), } } } #[derive(Debug)] pub struct ConfigParseError { pub origin: ConfigOrigin, pub line: Option, pub message: Vec, } #[derive(Debug, derive_more::From)] pub enum ConfigError { Parse(ConfigParseError), Other(HgError), } fn make_regex(pattern: &'static str) -> Regex { Regex::new(pattern).expect("expected a valid regex") } diff --git a/rust/hg-core/src/errors.rs b/rust/hg-core/src/errors.rs --- a/rust/hg-core/src/errors.rs +++ b/rust/hg-core/src/errors.rs @@ -1,183 +1,194 @@ use crate::config::ConfigValueParseError; +use crate::exit_codes; use std::fmt; /// Common error cases that can happen in many different APIs #[derive(Debug, derive_more::From)] pub enum HgError { IoError { error: std::io::Error, context: IoErrorContext, }, /// A file under `.hg/` normally only written by Mercurial is not in the /// expected format. This indicates a bug in Mercurial, filesystem /// corruption, or hardware failure. /// /// The given string is a short explanation for users, not intended to be /// machine-readable. CorruptedRepository(String), /// The respository or requested operation involves a feature not /// supported by the Rust implementation. Falling back to the Python /// implementation may or may not work. /// /// The given string is a short explanation for users, not intended to be /// machine-readable. UnsupportedFeature(String), /// Operation cannot proceed for some other reason. /// - /// The given string is a short explanation for users, not intended to be + /// The message is a short explanation for users, not intended to be /// machine-readable. - Abort(String), + Abort { + message: String, + detailed_exit_code: exit_codes::ExitCode, + }, /// A configuration value is not in the expected syntax. /// /// These errors can happen in many places in the code because values are /// parsed lazily as the file-level parser does not know the expected type /// and syntax of each value. #[from] ConfigValueParseError(ConfigValueParseError), } /// Details about where an I/O error happened #[derive(Debug)] pub enum IoErrorContext { ReadingFile(std::path::PathBuf), WritingFile(std::path::PathBuf), RemovingFile(std::path::PathBuf), RenamingFile { from: std::path::PathBuf, to: std::path::PathBuf, }, /// `std::fs::canonicalize` CanonicalizingPath(std::path::PathBuf), /// `std::env::current_dir` CurrentDir, /// `std::env::current_exe` CurrentExe, } impl HgError { pub fn corrupted(explanation: impl Into) -> Self { // TODO: capture a backtrace here and keep it in the error value // to aid debugging? // https://doc.rust-lang.org/std/backtrace/struct.Backtrace.html HgError::CorruptedRepository(explanation.into()) } pub fn unsupported(explanation: impl Into) -> Self { HgError::UnsupportedFeature(explanation.into()) } - pub fn abort(explanation: impl Into) -> Self { - HgError::Abort(explanation.into()) + + pub fn abort( + explanation: impl Into, + exit_code: exit_codes::ExitCode, + ) -> Self { + HgError::Abort { + message: explanation.into(), + detailed_exit_code: exit_code, + } } } // TODO: use `DisplayBytes` instead to show non-Unicode filenames losslessly? impl fmt::Display for HgError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - HgError::Abort(explanation) => write!(f, "{}", explanation), + HgError::Abort { message, .. } => write!(f, "{}", message), HgError::IoError { error, context } => { write!(f, "abort: {}: {}", context, error) } HgError::CorruptedRepository(explanation) => { write!(f, "abort: {}", explanation) } HgError::UnsupportedFeature(explanation) => { write!(f, "unsupported feature: {}", explanation) } HgError::ConfigValueParseError(error) => error.fmt(f), } } } // TODO: use `DisplayBytes` instead to show non-Unicode filenames losslessly? impl fmt::Display for IoErrorContext { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { IoErrorContext::ReadingFile(path) => { write!(f, "when reading {}", path.display()) } IoErrorContext::WritingFile(path) => { write!(f, "when writing {}", path.display()) } IoErrorContext::RemovingFile(path) => { write!(f, "when removing {}", path.display()) } IoErrorContext::RenamingFile { from, to } => write!( f, "when renaming {} to {}", from.display(), to.display() ), IoErrorContext::CanonicalizingPath(path) => { write!(f, "when canonicalizing {}", path.display()) } IoErrorContext::CurrentDir => { write!(f, "error getting current working directory") } IoErrorContext::CurrentExe => { write!(f, "error getting current executable") } } } } pub trait IoResultExt { /// Annotate a possible I/O error as related to a reading a file at the /// given path. /// /// This allows printing something like “File not found when reading /// example.txt” instead of just “File not found”. /// /// Converts a `Result` with `std::io::Error` into one with `HgError`. fn when_reading_file(self, path: &std::path::Path) -> Result; fn with_context( self, context: impl FnOnce() -> IoErrorContext, ) -> Result; } impl IoResultExt for std::io::Result { fn when_reading_file(self, path: &std::path::Path) -> Result { self.with_context(|| IoErrorContext::ReadingFile(path.to_owned())) } fn with_context( self, context: impl FnOnce() -> IoErrorContext, ) -> Result { self.map_err(|error| HgError::IoError { error, context: context(), }) } } pub trait HgResultExt { /// Handle missing files separately from other I/O error cases. /// /// Wraps the `Ok` type in an `Option`: /// /// * `Ok(x)` becomes `Ok(Some(x))` /// * An I/O "not found" error becomes `Ok(None)` /// * Other errors are unchanged fn io_not_found_as_none(self) -> Result, HgError>; } impl HgResultExt for Result { fn io_not_found_as_none(self) -> Result, HgError> { match self { Ok(x) => Ok(Some(x)), Err(HgError::IoError { error, .. }) if error.kind() == std::io::ErrorKind::NotFound => { Ok(None) } Err(other_error) => Err(other_error), } } } diff --git a/rust/hg-core/src/exit_codes.rs b/rust/hg-core/src/exit_codes.rs new file mode 100644 --- /dev/null +++ b/rust/hg-core/src/exit_codes.rs @@ -0,0 +1,19 @@ +pub type ExitCode = i32; + +/// Successful exit +pub const OK: ExitCode = 0; + +/// Generic abort +pub const ABORT: ExitCode = 255; + +// Abort when there is a config related error +pub const CONFIG_ERROR_ABORT: ExitCode = 30; + +// Abort when there is an error while parsing config +pub const CONFIG_PARSE_ERROR_ABORT: ExitCode = 10; + +/// Generic something completed but did not succeed +pub const UNSUCCESSFUL: ExitCode = 1; + +/// Command or feature not implemented by rhg +pub const UNIMPLEMENTED: ExitCode = 252; diff --git a/rust/hg-core/src/lib.rs b/rust/hg-core/src/lib.rs --- a/rust/hg-core/src/lib.rs +++ b/rust/hg-core/src/lib.rs @@ -1,132 +1,133 @@ // Copyright 2018-2020 Georges Racinet // and Mercurial contributors // // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. mod ancestors; pub mod dagops; pub mod errors; pub use ancestors::{AncestorsIterator, LazyAncestors, MissingAncestors}; pub mod dirstate; pub mod dirstate_tree; pub mod discovery; +pub mod exit_codes; pub mod requirements; pub mod testing; // unconditionally built, for use from integration tests pub use dirstate::{ dirs_multiset::{DirsMultiset, DirsMultisetIter}, dirstate_map::DirstateMap, parsers::{pack_dirstate, parse_dirstate, PARENT_SIZE}, status::{ status, BadMatch, BadType, DirstateStatus, HgPathCow, StatusError, StatusOptions, }, CopyMap, CopyMapIter, DirstateEntry, DirstateParents, EntryState, StateMap, StateMapIter, }; pub mod copy_tracing; mod filepatterns; pub mod matchers; pub mod repo; pub mod revlog; pub use revlog::*; pub mod config; pub mod logging; pub mod operations; pub mod revset; pub mod utils; use crate::utils::hg_path::{HgPathBuf, HgPathError}; pub use filepatterns::{ parse_pattern_syntax, read_pattern_file, IgnorePattern, PatternFileWarning, PatternSyntax, }; use std::collections::HashMap; use std::fmt; use twox_hash::RandomXxHashBuilder64; /// This is a contract between the `micro-timer` crate and us, to expose /// the `log` crate as `crate::log`. use log; pub type LineNumber = usize; /// Rust's default hasher is too slow because it tries to prevent collision /// attacks. We are not concerned about those: if an ill-minded person has /// write access to your repository, you have other issues. pub type FastHashMap = HashMap; #[derive(Debug, PartialEq)] pub enum DirstateMapError { PathNotFound(HgPathBuf), EmptyPath, InvalidPath(HgPathError), } impl fmt::Display for DirstateMapError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { DirstateMapError::PathNotFound(_) => { f.write_str("expected a value, found none") } DirstateMapError::EmptyPath => { f.write_str("Overflow in dirstate.") } DirstateMapError::InvalidPath(path_error) => path_error.fmt(f), } } } #[derive(Debug, derive_more::From)] pub enum DirstateError { Map(DirstateMapError), Common(errors::HgError), } impl fmt::Display for DirstateError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { DirstateError::Map(error) => error.fmt(f), DirstateError::Common(error) => error.fmt(f), } } } #[derive(Debug, derive_more::From)] pub enum PatternError { #[from] Path(HgPathError), UnsupportedSyntax(String), UnsupportedSyntaxInFile(String, String, usize), TooLong(usize), #[from] IO(std::io::Error), /// Needed a pattern that can be turned into a regex but got one that /// can't. This should only happen through programmer error. NonRegexPattern(IgnorePattern), } impl fmt::Display for PatternError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { PatternError::UnsupportedSyntax(syntax) => { write!(f, "Unsupported syntax {}", syntax) } PatternError::UnsupportedSyntaxInFile(syntax, file_path, line) => { write!( f, "{}:{}: unsupported syntax {}", file_path, line, syntax ) } PatternError::TooLong(size) => { write!(f, "matcher pattern is too long ({} bytes)", size) } PatternError::IO(error) => error.fmt(f), PatternError::Path(error) => error.fmt(f), PatternError::NonRegexPattern(pattern) => { write!(f, "'{:?}' cannot be turned into a regex", pattern) } } } } diff --git a/rust/hg-core/src/repo.rs b/rust/hg-core/src/repo.rs --- a/rust/hg-core/src/repo.rs +++ b/rust/hg-core/src/repo.rs @@ -1,284 +1,287 @@ use crate::config::{Config, ConfigError, ConfigParseError}; use crate::errors::{HgError, IoErrorContext, IoResultExt}; +use crate::exit_codes; use crate::requirements; use crate::utils::files::get_path_from_bytes; use crate::utils::SliceExt; use memmap::{Mmap, MmapOptions}; use std::collections::HashSet; use std::path::{Path, PathBuf}; /// A repository on disk pub struct Repo { working_directory: PathBuf, dot_hg: PathBuf, store: PathBuf, requirements: HashSet, config: Config, } #[derive(Debug, derive_more::From)] pub enum RepoError { NotFound { at: PathBuf, }, #[from] ConfigParseError(ConfigParseError), #[from] Other(HgError), } impl From for RepoError { fn from(error: ConfigError) -> Self { match error { ConfigError::Parse(error) => error.into(), ConfigError::Other(error) => error.into(), } } } /// Filesystem access abstraction for the contents of a given "base" diretory #[derive(Clone, Copy)] pub struct Vfs<'a> { pub(crate) base: &'a Path, } impl Repo { /// tries to find nearest repository root in current working directory or /// its ancestors pub fn find_repo_root() -> Result { let current_directory = crate::utils::current_dir()?; // ancestors() is inclusive: it first yields `current_directory` // as-is. for ancestor in current_directory.ancestors() { if ancestor.join(".hg").is_dir() { return Ok(ancestor.to_path_buf()); } } return Err(RepoError::NotFound { at: current_directory, }); } /// Find a repository, either at the given path (which must contain a `.hg` /// sub-directory) or by searching the current directory and its /// ancestors. /// /// A method with two very different "modes" like this usually a code smell /// to make two methods instead, but in this case an `Option` is what rhg /// sub-commands get from Clap for the `-R` / `--repository` CLI argument. /// Having two methods would just move that `if` to almost all callers. pub fn find( config: &Config, explicit_path: Option, ) -> Result { if let Some(root) = explicit_path { if root.join(".hg").is_dir() { Self::new_at_path(root.to_owned(), config) } else if root.is_file() { Err(HgError::unsupported("bundle repository").into()) } else { Err(RepoError::NotFound { at: root.to_owned(), }) } } else { let root = Self::find_repo_root()?; Self::new_at_path(root, config) } } /// To be called after checking that `.hg` is a sub-directory fn new_at_path( working_directory: PathBuf, config: &Config, ) -> Result { let dot_hg = working_directory.join(".hg"); let mut repo_config_files = Vec::new(); repo_config_files.push(dot_hg.join("hgrc")); repo_config_files.push(dot_hg.join("hgrc-not-shared")); let hg_vfs = Vfs { base: &dot_hg }; let mut reqs = requirements::load_if_exists(hg_vfs)?; let relative = reqs.contains(requirements::RELATIVE_SHARED_REQUIREMENT); let shared = reqs.contains(requirements::SHARED_REQUIREMENT) || relative; // From `mercurial/localrepo.py`: // // if .hg/requires contains the sharesafe requirement, it means // there exists a `.hg/store/requires` too and we should read it // NOTE: presence of SHARESAFE_REQUIREMENT imply that store requirement // is present. We never write SHARESAFE_REQUIREMENT for a repo if store // is not present, refer checkrequirementscompat() for that // // However, if SHARESAFE_REQUIREMENT is not present, it means that the // repository was shared the old way. We check the share source // .hg/requires for SHARESAFE_REQUIREMENT to detect whether the // current repository needs to be reshared let share_safe = reqs.contains(requirements::SHARESAFE_REQUIREMENT); let store_path; if !shared { store_path = dot_hg.join("store"); } else { let bytes = hg_vfs.read("sharedpath")?; let mut shared_path = get_path_from_bytes(bytes.trim_end_newlines()).to_owned(); if relative { shared_path = dot_hg.join(shared_path) } if !shared_path.is_dir() { return Err(HgError::corrupted(format!( ".hg/sharedpath points to nonexistent directory {}", shared_path.display() )) .into()); } store_path = shared_path.join("store"); let source_is_share_safe = requirements::load(Vfs { base: &shared_path })? .contains(requirements::SHARESAFE_REQUIREMENT); if share_safe && !source_is_share_safe { return Err(match config .get(b"share", b"safe-mismatch.source-not-safe") { Some(b"abort") | None => HgError::abort( "abort: share source does not support share-safe requirement\n\ (see `hg help config.format.use-share-safe` for more information)", + exit_codes::ABORT, ), _ => HgError::unsupported("share-safe downgrade"), } .into()); } else if source_is_share_safe && !share_safe { return Err( match config.get(b"share", b"safe-mismatch.source-safe") { Some(b"abort") | None => HgError::abort( "abort: version mismatch: source uses share-safe \ functionality while the current share does not\n\ (see `hg help config.format.use-share-safe` for more information)", + exit_codes::ABORT, ), _ => HgError::unsupported("share-safe upgrade"), } .into(), ); } if share_safe { repo_config_files.insert(0, shared_path.join("hgrc")) } } if share_safe { reqs.extend(requirements::load(Vfs { base: &store_path })?); } let repo_config = if std::env::var_os("HGRCSKIPREPO").is_none() { config.combine_with_repo(&repo_config_files)? } else { config.clone() }; let repo = Self { requirements: reqs, working_directory, store: store_path, dot_hg, config: repo_config, }; requirements::check(&repo)?; Ok(repo) } pub fn working_directory_path(&self) -> &Path { &self.working_directory } pub fn requirements(&self) -> &HashSet { &self.requirements } pub fn config(&self) -> &Config { &self.config } /// For accessing repository files (in `.hg`), except for the store /// (`.hg/store`). pub fn hg_vfs(&self) -> Vfs<'_> { Vfs { base: &self.dot_hg } } /// For accessing repository store files (in `.hg/store`) pub fn store_vfs(&self) -> Vfs<'_> { Vfs { base: &self.store } } /// For accessing the working copy pub fn working_directory_vfs(&self) -> Vfs<'_> { Vfs { base: &self.working_directory, } } pub fn has_dirstate_v2(&self) -> bool { self.requirements .contains(requirements::DIRSTATE_V2_REQUIREMENT) } pub fn dirstate_parents( &self, ) -> Result { let dirstate = self.hg_vfs().mmap_open("dirstate")?; if dirstate.is_empty() { return Ok(crate::dirstate::DirstateParents::NULL); } let parents = if self.has_dirstate_v2() { crate::dirstate_tree::on_disk::parse_dirstate_parents(&dirstate)? } else { crate::dirstate::parsers::parse_dirstate_parents(&dirstate)? }; Ok(parents.clone()) } } impl Vfs<'_> { pub fn join(&self, relative_path: impl AsRef) -> PathBuf { self.base.join(relative_path) } pub fn read( &self, relative_path: impl AsRef, ) -> Result, HgError> { let path = self.join(relative_path); std::fs::read(&path).when_reading_file(&path) } pub fn mmap_open( &self, relative_path: impl AsRef, ) -> Result { let path = self.base.join(relative_path); let file = std::fs::File::open(&path).when_reading_file(&path)?; // TODO: what are the safety requirements here? let mmap = unsafe { MmapOptions::new().map(&file) } .when_reading_file(&path)?; Ok(mmap) } pub fn rename( &self, relative_from: impl AsRef, relative_to: impl AsRef, ) -> Result<(), HgError> { let from = self.join(relative_from); let to = self.join(relative_to); std::fs::rename(&from, &to) .with_context(|| IoErrorContext::RenamingFile { from, to }) } } diff --git a/rust/rhg/src/error.rs b/rust/rhg/src/error.rs --- a/rust/rhg/src/error.rs +++ b/rust/rhg/src/error.rs @@ -1,195 +1,195 @@ -use crate::exitcode; use crate::ui::utf8_to_local; use crate::ui::UiError; use crate::NoRepoInCwdError; use format_bytes::format_bytes; use hg::config::{ConfigError, ConfigParseError, ConfigValueParseError}; use hg::errors::HgError; +use hg::exit_codes; use hg::repo::RepoError; use hg::revlog::revlog::RevlogError; use hg::utils::files::get_bytes_from_path; use hg::{DirstateError, DirstateMapError, StatusError}; use std::convert::From; /// The kind of command error #[derive(Debug)] pub enum CommandError { /// Exit with an error message and "standard" failure exit code. Abort { message: Vec, - detailed_exit_code: exitcode::ExitCode, + detailed_exit_code: exit_codes::ExitCode, }, /// Exit with a failure exit code but no message. Unsuccessful, /// Encountered something (such as a CLI argument, repository layout, …) /// not supported by this version of `rhg`. Depending on configuration /// `rhg` may attempt to silently fall back to Python-based `hg`, which /// may or may not support this feature. UnsupportedFeature { message: Vec }, } impl CommandError { pub fn abort(message: impl AsRef) -> Self { - CommandError::abort_with_exit_code(message, exitcode::ABORT) + CommandError::abort_with_exit_code(message, exit_codes::ABORT) } pub fn abort_with_exit_code( message: impl AsRef, - detailed_exit_code: exitcode::ExitCode, + detailed_exit_code: exit_codes::ExitCode, ) -> Self { CommandError::Abort { // TODO: bytes-based (instead of Unicode-based) formatting // of error messages to handle non-UTF-8 filenames etc: // https://www.mercurial-scm.org/wiki/EncodingStrategy#Mixing_output message: utf8_to_local(message.as_ref()).into(), detailed_exit_code: detailed_exit_code, } } pub fn unsupported(message: impl AsRef) -> Self { CommandError::UnsupportedFeature { message: utf8_to_local(message.as_ref()).into(), } } } /// For now we don’t differenciate between invalid CLI args and valid for `hg` /// but not supported yet by `rhg`. impl From for CommandError { fn from(error: clap::Error) -> Self { CommandError::unsupported(error.to_string()) } } impl From for CommandError { fn from(error: HgError) -> Self { match error { HgError::UnsupportedFeature(message) => { CommandError::unsupported(message) } _ => CommandError::abort(error.to_string()), } } } impl From for CommandError { fn from(error: ConfigValueParseError) -> Self { CommandError::abort_with_exit_code( error.to_string(), - exitcode::CONFIG_ERROR_ABORT, + exit_codes::CONFIG_ERROR_ABORT, ) } } impl From for CommandError { fn from(_error: UiError) -> Self { // If we already failed writing to stdout or stderr, // writing an error message to stderr about it would be likely to fail // too. CommandError::abort("") } } impl From for CommandError { fn from(error: RepoError) -> Self { match error { RepoError::NotFound { at } => CommandError::Abort { message: format_bytes!( b"abort: repository {} not found", get_bytes_from_path(at) ), - detailed_exit_code: exitcode::ABORT, + detailed_exit_code: exit_codes::ABORT, }, RepoError::ConfigParseError(error) => error.into(), RepoError::Other(error) => error.into(), } } } impl<'a> From<&'a NoRepoInCwdError> for CommandError { fn from(error: &'a NoRepoInCwdError) -> Self { let NoRepoInCwdError { cwd } = error; CommandError::Abort { message: format_bytes!( b"abort: no repository found in '{}' (.hg not found)!", get_bytes_from_path(cwd) ), - detailed_exit_code: exitcode::ABORT, + detailed_exit_code: exit_codes::ABORT, } } } impl From for CommandError { fn from(error: ConfigError) -> Self { match error { ConfigError::Parse(error) => error.into(), ConfigError::Other(error) => error.into(), } } } impl From for CommandError { fn from(error: ConfigParseError) -> Self { let ConfigParseError { origin, line, message, } = error; let line_message = if let Some(line_number) = line { format_bytes!(b":{}", line_number.to_string().into_bytes()) } else { Vec::new() }; CommandError::Abort { message: format_bytes!( b"config error at {}{}: {}", origin, line_message, message ), - detailed_exit_code: exitcode::CONFIG_ERROR_ABORT, + detailed_exit_code: exit_codes::CONFIG_ERROR_ABORT, } } } impl From<(RevlogError, &str)> for CommandError { fn from((err, rev): (RevlogError, &str)) -> CommandError { match err { RevlogError::WDirUnsupported => CommandError::abort( "abort: working directory revision cannot be specified", ), RevlogError::InvalidRevision => CommandError::abort(format!( "abort: invalid revision identifier: {}", rev )), RevlogError::AmbiguousPrefix => CommandError::abort(format!( "abort: ambiguous revision identifier: {}", rev )), RevlogError::Other(error) => error.into(), } } } impl From for CommandError { fn from(error: StatusError) -> Self { CommandError::abort(format!("{}", error)) } } impl From for CommandError { fn from(error: DirstateMapError) -> Self { CommandError::abort(format!("{}", error)) } } impl From for CommandError { fn from(error: DirstateError) -> Self { match error { DirstateError::Common(error) => error.into(), DirstateError::Map(error) => error.into(), } } } diff --git a/rust/rhg/src/exitcode.rs b/rust/rhg/src/exitcode.rs deleted file mode 100644 --- a/rust/rhg/src/exitcode.rs +++ /dev/null @@ -1,16 +0,0 @@ -pub type ExitCode = i32; - -/// Successful exit -pub const OK: ExitCode = 0; - -/// Generic abort -pub const ABORT: ExitCode = 255; - -// Abort when there is a config related error -pub const CONFIG_ERROR_ABORT: ExitCode = 30; - -/// Generic something completed but did not succeed -pub const UNSUCCESSFUL: ExitCode = 1; - -/// Command or feature not implemented by rhg -pub const UNIMPLEMENTED: ExitCode = 252; diff --git a/rust/rhg/src/main.rs b/rust/rhg/src/main.rs --- a/rust/rhg/src/main.rs +++ b/rust/rhg/src/main.rs @@ -1,588 +1,588 @@ extern crate log; use crate::ui::Ui; use clap::App; use clap::AppSettings; use clap::Arg; use clap::ArgMatches; use format_bytes::{format_bytes, join}; use hg::config::{Config, ConfigSource}; +use hg::exit_codes; use hg::repo::{Repo, RepoError}; use hg::utils::files::{get_bytes_from_os_str, get_path_from_bytes}; use hg::utils::SliceExt; use std::ffi::OsString; use std::path::PathBuf; use std::process::Command; mod blackbox; mod error; -mod exitcode; mod ui; use error::CommandError; fn main_with_result( process_start_time: &blackbox::ProcessStartTime, ui: &ui::Ui, repo: Result<&Repo, &NoRepoInCwdError>, config: &Config, ) -> Result<(), CommandError> { check_extensions(config)?; let app = App::new("rhg") .global_setting(AppSettings::AllowInvalidUtf8) .global_setting(AppSettings::DisableVersion) .setting(AppSettings::SubcommandRequired) .setting(AppSettings::VersionlessSubcommands) .arg( Arg::with_name("repository") .help("repository root directory") .short("-R") .long("--repository") .value_name("REPO") .takes_value(true) // Both ok: `hg -R ./foo log` or `hg log -R ./foo` .global(true), ) .arg( Arg::with_name("config") .help("set/override config option (use 'section.name=value')") .long("--config") .value_name("CONFIG") .takes_value(true) .global(true) // Ok: `--config section.key1=val --config section.key2=val2` .multiple(true) // Not ok: `--config section.key1=val section.key2=val2` .number_of_values(1), ) .arg( Arg::with_name("cwd") .help("change working directory") .long("--cwd") .value_name("DIR") .takes_value(true) .global(true), ) .version("0.0.1"); let app = add_subcommand_args(app); let matches = app.clone().get_matches_safe()?; let (subcommand_name, subcommand_matches) = matches.subcommand(); let run = subcommand_run_fn(subcommand_name) .expect("unknown subcommand name from clap despite AppSettings::SubcommandRequired"); let subcommand_args = subcommand_matches .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired"); let invocation = CliInvocation { ui, subcommand_args, config, repo, }; let blackbox = blackbox::Blackbox::new(&invocation, process_start_time)?; blackbox.log_command_start(); let result = run(&invocation); blackbox.log_command_end(exit_code( &result, // TODO: show a warning or combine with original error if `get_bool` // returns an error config .get_bool(b"ui", b"detailed-exit-code") .unwrap_or(false), )); result } fn main() { // Run this first, before we find out if the blackbox extension is even // enabled, in order to include everything in-between in the duration // measurements. Reading config files can be slow if they’re on NFS. let process_start_time = blackbox::ProcessStartTime::now(); env_logger::init(); let ui = ui::Ui::new(); let early_args = EarlyArgs::parse(std::env::args_os()); let initial_current_dir = early_args.cwd.map(|cwd| { let cwd = get_path_from_bytes(&cwd); std::env::current_dir() .and_then(|initial| { std::env::set_current_dir(cwd)?; Ok(initial) }) .unwrap_or_else(|error| { exit( &None, &ui, OnUnsupported::Abort, Err(CommandError::abort(format!( "abort: {}: '{}'", error, cwd.display() ))), false, ) }) }); let mut non_repo_config = Config::load_non_repo().unwrap_or_else(|error| { // Normally this is decided based on config, but we don’t have that // available. As of this writing config loading never returns an // "unsupported" error but that is not enforced by the type system. let on_unsupported = OnUnsupported::Abort; exit( &initial_current_dir, &ui, on_unsupported, Err(error.into()), false, ) }); non_repo_config .load_cli_args_config(early_args.config) .unwrap_or_else(|error| { exit( &initial_current_dir, &ui, OnUnsupported::from_config(&ui, &non_repo_config), Err(error.into()), non_repo_config .get_bool(b"ui", b"detailed-exit-code") .unwrap_or(false), ) }); if let Some(repo_path_bytes) = &early_args.repo { lazy_static::lazy_static! { static ref SCHEME_RE: regex::bytes::Regex = // Same as `_matchscheme` in `mercurial/util.py` regex::bytes::Regex::new("^[a-zA-Z0-9+.\\-]+:").unwrap(); } if SCHEME_RE.is_match(&repo_path_bytes) { exit( &initial_current_dir, &ui, OnUnsupported::from_config(&ui, &non_repo_config), Err(CommandError::UnsupportedFeature { message: format_bytes!( b"URL-like --repository {}", repo_path_bytes ), }), // TODO: show a warning or combine with original error if // `get_bool` returns an error non_repo_config .get_bool(b"ui", b"detailed-exit-code") .unwrap_or(false), ) } } let repo_arg = early_args.repo.unwrap_or(Vec::new()); let repo_path: Option = { if repo_arg.is_empty() { None } else { let local_config = { if std::env::var_os("HGRCSKIPREPO").is_none() { // TODO: handle errors from find_repo_root if let Ok(current_dir_path) = Repo::find_repo_root() { let config_files = vec![ ConfigSource::AbsPath( current_dir_path.join(".hg/hgrc"), ), ConfigSource::AbsPath( current_dir_path.join(".hg/hgrc-not-shared"), ), ]; // TODO: handle errors from // `load_from_explicit_sources` Config::load_from_explicit_sources(config_files).ok() } else { None } } else { None } }; let non_repo_config_val = { let non_repo_val = non_repo_config.get(b"paths", &repo_arg); match &non_repo_val { Some(val) if val.len() > 0 => home::home_dir() .unwrap_or_else(|| PathBuf::from("~")) .join(get_path_from_bytes(val)) .canonicalize() // TODO: handle error and make it similar to python // implementation maybe? .ok(), _ => None, } }; let config_val = match &local_config { None => non_repo_config_val, Some(val) => { let local_config_val = val.get(b"paths", &repo_arg); match &local_config_val { Some(val) if val.len() > 0 => { // presence of a local_config assures that // current_dir // wont result in an Error let canpath = hg::utils::current_dir() .unwrap() .join(get_path_from_bytes(val)) .canonicalize(); canpath.ok().or(non_repo_config_val) } _ => non_repo_config_val, } } }; config_val.or(Some(get_path_from_bytes(&repo_arg).to_path_buf())) } }; let repo_result = match Repo::find(&non_repo_config, repo_path.to_owned()) { Ok(repo) => Ok(repo), Err(RepoError::NotFound { at }) if repo_path.is_none() => { // Not finding a repo is not fatal yet, if `-R` was not given Err(NoRepoInCwdError { cwd: at }) } Err(error) => exit( &initial_current_dir, &ui, OnUnsupported::from_config(&ui, &non_repo_config), Err(error.into()), // TODO: show a warning or combine with original error if // `get_bool` returns an error non_repo_config .get_bool(b"ui", b"detailed-exit-code") .unwrap_or(false), ), }; let config = if let Ok(repo) = &repo_result { repo.config() } else { &non_repo_config }; let on_unsupported = OnUnsupported::from_config(&ui, config); let result = main_with_result( &process_start_time, &ui, repo_result.as_ref(), config, ); exit( &initial_current_dir, &ui, on_unsupported, result, // TODO: show a warning or combine with original error if `get_bool` // returns an error config .get_bool(b"ui", b"detailed-exit-code") .unwrap_or(false), ) } fn exit_code( result: &Result<(), CommandError>, use_detailed_exit_code: bool, ) -> i32 { match result { - Ok(()) => exitcode::OK, + Ok(()) => exit_codes::OK, Err(CommandError::Abort { message: _, detailed_exit_code, }) => { if use_detailed_exit_code { *detailed_exit_code } else { - exitcode::ABORT + exit_codes::ABORT } } - Err(CommandError::Unsuccessful) => exitcode::UNSUCCESSFUL, + Err(CommandError::Unsuccessful) => exit_codes::UNSUCCESSFUL, // Exit with a specific code and no error message to let a potential // wrapper script fallback to Python-based Mercurial. Err(CommandError::UnsupportedFeature { .. }) => { - exitcode::UNIMPLEMENTED + exit_codes::UNIMPLEMENTED } } } fn exit( initial_current_dir: &Option, ui: &Ui, mut on_unsupported: OnUnsupported, result: Result<(), CommandError>, use_detailed_exit_code: bool, ) -> ! { if let ( OnUnsupported::Fallback { executable }, Err(CommandError::UnsupportedFeature { .. }), ) = (&on_unsupported, &result) { let mut args = std::env::args_os(); let executable_path = get_path_from_bytes(&executable); let this_executable = args.next().expect("exepcted argv[0] to exist"); if executable_path == &PathBuf::from(this_executable) { // Avoid spawning infinitely many processes until resource // exhaustion. let _ = ui.write_stderr(&format_bytes!( b"Blocking recursive fallback. The 'rhg.fallback-executable = {}' config \ points to `rhg` itself.\n", executable )); on_unsupported = OnUnsupported::Abort } else { // `args` is now `argv[1..]` since we’ve already consumed `argv[0]` let mut command = Command::new(executable_path); command.args(args); if let Some(initial) = initial_current_dir { command.current_dir(initial); } let result = command.status(); match result { Ok(status) => std::process::exit( - status.code().unwrap_or(exitcode::ABORT), + status.code().unwrap_or(exit_codes::ABORT), ), Err(error) => { let _ = ui.write_stderr(&format_bytes!( b"tried to fall back to a '{}' sub-process but got error {}\n", executable, format_bytes::Utf8(error) )); on_unsupported = OnUnsupported::Abort } } } } exit_no_fallback(ui, on_unsupported, result, use_detailed_exit_code) } fn exit_no_fallback( ui: &Ui, on_unsupported: OnUnsupported, result: Result<(), CommandError>, use_detailed_exit_code: bool, ) -> ! { match &result { Ok(_) => {} Err(CommandError::Unsuccessful) => {} Err(CommandError::Abort { message, detailed_exit_code: _, }) => { if !message.is_empty() { // Ignore errors when writing to stderr, we’re already exiting // with failure code so there’s not much more we can do. let _ = ui.write_stderr(&format_bytes!(b"{}\n", message)); } } Err(CommandError::UnsupportedFeature { message }) => { match on_unsupported { OnUnsupported::Abort => { let _ = ui.write_stderr(&format_bytes!( b"unsupported feature: {}\n", message )); } OnUnsupported::AbortSilent => {} OnUnsupported::Fallback { .. } => unreachable!(), } } } std::process::exit(exit_code(&result, use_detailed_exit_code)) } macro_rules! subcommands { ($( $command: ident )+) => { mod commands { $( pub mod $command; )+ } fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { app $( .subcommand(commands::$command::args()) )+ } pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>; fn subcommand_run_fn(name: &str) -> Option { match name { $( stringify!($command) => Some(commands::$command::run), )+ _ => None, } } }; } subcommands! { cat debugdata debugrequirements files root config status } pub struct CliInvocation<'a> { ui: &'a Ui, subcommand_args: &'a ArgMatches<'a>, config: &'a Config, /// References inside `Result` is a bit peculiar but allow /// `invocation.repo?` to work out with `&CliInvocation` since this /// `Result` type is `Copy`. repo: Result<&'a Repo, &'a NoRepoInCwdError>, } struct NoRepoInCwdError { cwd: PathBuf, } /// CLI arguments to be parsed "early" in order to be able to read /// configuration before using Clap. Ideally we would also use Clap for this, /// see . /// /// These arguments are still declared when we do use Clap later, so that Clap /// does not return an error for their presence. struct EarlyArgs { /// Values of all `--config` arguments. (Possibly none) config: Vec>, /// Value of the `-R` or `--repository` argument, if any. repo: Option>, /// Value of the `--cwd` argument, if any. cwd: Option>, } impl EarlyArgs { fn parse(args: impl IntoIterator) -> Self { let mut args = args.into_iter().map(get_bytes_from_os_str); let mut config = Vec::new(); let mut repo = None; let mut cwd = None; // Use `while let` instead of `for` so that we can also call // `args.next()` inside the loop. while let Some(arg) = args.next() { if arg == b"--config" { if let Some(value) = args.next() { config.push(value) } } else if let Some(value) = arg.drop_prefix(b"--config=") { config.push(value.to_owned()) } if arg == b"--cwd" { if let Some(value) = args.next() { cwd = Some(value) } } else if let Some(value) = arg.drop_prefix(b"--cwd=") { cwd = Some(value.to_owned()) } if arg == b"--repository" || arg == b"-R" { if let Some(value) = args.next() { repo = Some(value) } } else if let Some(value) = arg.drop_prefix(b"--repository=") { repo = Some(value.to_owned()) } else if let Some(value) = arg.drop_prefix(b"-R") { repo = Some(value.to_owned()) } } Self { config, repo, cwd } } } /// What to do when encountering some unsupported feature. /// /// See `HgError::UnsupportedFeature` and `CommandError::UnsupportedFeature`. enum OnUnsupported { /// Print an error message describing what feature is not supported, /// and exit with code 252. Abort, /// Silently exit with code 252. AbortSilent, /// Try running a Python implementation Fallback { executable: Vec }, } impl OnUnsupported { const DEFAULT: Self = OnUnsupported::Abort; fn from_config(ui: &Ui, config: &Config) -> Self { match config .get(b"rhg", b"on-unsupported") .map(|value| value.to_ascii_lowercase()) .as_deref() { Some(b"abort") => OnUnsupported::Abort, Some(b"abort-silent") => OnUnsupported::AbortSilent, Some(b"fallback") => OnUnsupported::Fallback { executable: config .get(b"rhg", b"fallback-executable") .unwrap_or_else(|| { exit_no_fallback( ui, Self::Abort, Err(CommandError::abort( "abort: 'rhg.on-unsupported=fallback' without \ 'rhg.fallback-executable' set." )), false, ) }) .to_owned(), }, None => Self::DEFAULT, Some(_) => { // TODO: warn about unknown config value Self::DEFAULT } } } } const SUPPORTED_EXTENSIONS: &[&[u8]] = &[b"blackbox", b"share"]; fn check_extensions(config: &Config) -> Result<(), CommandError> { let enabled = config.get_section_keys(b"extensions"); let mut unsupported = enabled; for supported in SUPPORTED_EXTENSIONS { unsupported.remove(supported); } if let Some(ignored_list) = config.get_simple_list(b"rhg", b"ignored-extensions") { for ignored in ignored_list { unsupported.remove(ignored); } } if unsupported.is_empty() { Ok(()) } else { Err(CommandError::UnsupportedFeature { message: format_bytes!( b"extensions: {} (consider adding them to 'rhg.ignored-extensions' config)", join(unsupported, b", ") ), }) } }