main.rs
715 lines
| 23.4 KiB
| application/rls-services+xml
|
RustLexer
|
r46101 | extern crate log; | ||
|
r49159 | use crate::error::CommandError; | ||
|
r47334 | use crate::ui::Ui; | ||
|
r45593 | use clap::App; | ||
use clap::AppSettings; | ||||
|
r47253 | use clap::Arg; | ||
|
r46100 | use clap::ArgMatches; | ||
|
r47467 | use format_bytes::{format_bytes, join}; | ||
|
r48196 | use hg::config::{Config, ConfigSource}; | ||
|
r48199 | use hg::exit_codes; | ||
|
r47335 | use hg::repo::{Repo, RepoError}; | ||
|
r47423 | use hg::utils::files::{get_bytes_from_os_str, get_path_from_bytes}; | ||
use hg::utils::SliceExt; | ||||
|
r49270 | use std::collections::HashSet; | ||
|
r47423 | use std::ffi::OsString; | ||
use std::path::PathBuf; | ||||
|
r47425 | use std::process::Command; | ||
|
r45593 | |||
|
r47343 | mod blackbox; | ||
|
r49584 | mod color; | ||
|
r45515 | mod error; | ||
|
r45592 | mod ui; | ||
|
r48988 | pub mod utils { | ||
pub mod path_utils; | ||||
} | ||||
|
r45503 | |||
|
r47343 | fn main_with_result( | ||
|
r47423 | process_start_time: &blackbox::ProcessStartTime, | ||
|
r47343 | ui: &ui::Ui, | ||
|
r47423 | repo: Result<&Repo, &NoRepoInCwdError>, | ||
config: &Config, | ||||
|
r47343 | ) -> Result<(), CommandError> { | ||
|
r49341 | check_unsupported(config, repo, ui)?; | ||
|
r47467 | |||
|
r46100 | let app = App::new("rhg") | ||
|
r47351 | .global_setting(AppSettings::AllowInvalidUtf8) | ||
|
r47480 | .global_setting(AppSettings::DisableVersion) | ||
|
r45593 | .setting(AppSettings::SubcommandRequired) | ||
.setting(AppSettings::VersionlessSubcommands) | ||||
|
r47351 | .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), | ||||
) | ||||
|
r47470 | .arg( | ||
Arg::with_name("cwd") | ||||
.help("change working directory") | ||||
.long("--cwd") | ||||
.value_name("DIR") | ||||
.takes_value(true) | ||||
.global(true), | ||||
) | ||||
|
r49583 | .arg( | ||
Arg::with_name("color") | ||||
.help("when to colorize (boolean, always, auto, never, or debug)") | ||||
.long("--color") | ||||
.value_name("TYPE") | ||||
.takes_value(true) | ||||
.global(true), | ||||
) | ||||
|
r47252 | .version("0.0.1"); | ||
let app = add_subcommand_args(app); | ||||
|
r45593 | |||
|
r47333 | let matches = app.clone().get_matches_safe()?; | ||
|
r47253 | |||
|
r47252 | let (subcommand_name, subcommand_matches) = matches.subcommand(); | ||
|
r48889 | |||
|
r48890 | // Mercurial allows users to define "defaults" for commands, fallback | ||
// if a default is detected for the current command | ||||
let defaults = config.get_str(b"defaults", subcommand_name.as_bytes()); | ||||
if defaults?.is_some() { | ||||
let msg = "`defaults` config set"; | ||||
return Err(CommandError::unsupported(msg)); | ||||
} | ||||
|
r48889 | for prefix in ["pre", "post", "fail"].iter() { | ||
// Mercurial allows users to define generic hooks for commands, | ||||
// fallback if any are detected | ||||
let item = format!("{}-{}", prefix, subcommand_name); | ||||
let hook_for_command = config.get_str(b"hooks", item.as_bytes())?; | ||||
if hook_for_command.is_some() { | ||||
let msg = format!("{}-{} hook defined", prefix, subcommand_name); | ||||
return Err(CommandError::unsupported(msg)); | ||||
} | ||||
} | ||||
|
r47252 | let run = subcommand_run_fn(subcommand_name) | ||
.expect("unknown subcommand name from clap despite AppSettings::SubcommandRequired"); | ||||
|
r47334 | let subcommand_args = subcommand_matches | ||
|
r47252 | .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired"); | ||
|
r45593 | |||
|
r47343 | let invocation = CliInvocation { | ||
|
r47334 | ui, | ||
subcommand_args, | ||||
|
r47423 | config, | ||
repo, | ||||
|
r47343 | }; | ||
|
r48891 | |||
if let Ok(repo) = repo { | ||||
// We don't support subrepos, fallback if the subrepos file is present | ||||
if repo.working_directory_vfs().join(".hgsub").exists() { | ||||
let msg = "subrepos (.hgsub is present)"; | ||||
return Err(CommandError::unsupported(msg)); | ||||
} | ||||
} | ||||
|
r49243 | if config.is_extension_enabled(b"blackbox") { | ||
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 | ||||
} else { | ||||
run(&invocation) | ||||
} | ||||
|
r47333 | } | ||
|
r47252 | |||
|
r47333 | fn main() { | ||
|
r47343 | // 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(); | ||||
|
r47423 | env_logger::init(); | ||
|
r47333 | |||
|
r47423 | let early_args = EarlyArgs::parse(std::env::args_os()); | ||
|
r47470 | |||
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, | ||||
|
r49582 | &Ui::new_infallible(&Config::empty()), | ||
|
r47470 | OnUnsupported::Abort, | ||
Err(CommandError::abort(format!( | ||||
"abort: {}: '{}'", | ||||
error, | ||||
cwd.display() | ||||
))), | ||||
|
r47576 | false, | ||
|
r47470 | ) | ||
}) | ||||
}); | ||||
|
r48198 | let mut non_repo_config = | ||
Config::load_non_repo().unwrap_or_else(|error| { | ||||
|
r47424 | // 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; | ||||
|
r47576 | exit( | ||
&initial_current_dir, | ||||
|
r49582 | &Ui::new_infallible(&Config::empty()), | ||
|
r47576 | on_unsupported, | ||
Err(error.into()), | ||||
false, | ||||
) | ||||
|
r47424 | }); | ||
|
r47423 | |||
|
r48198 | non_repo_config | ||
|
r49583 | .load_cli_args(early_args.config, early_args.color) | ||
|
r48198 | .unwrap_or_else(|error| { | ||
exit( | ||||
&initial_current_dir, | ||||
|
r49582 | &Ui::new_infallible(&non_repo_config), | ||
|
r49176 | OnUnsupported::from_config(&non_repo_config), | ||
|
r48198 | Err(error.into()), | ||
non_repo_config | ||||
.get_bool(b"ui", b"detailed-exit-code") | ||||
.unwrap_or(false), | ||||
) | ||||
}); | ||||
|
r47463 | 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( | ||||
|
r47470 | &initial_current_dir, | ||
|
r49582 | &Ui::new_infallible(&non_repo_config), | ||
|
r49176 | OnUnsupported::from_config(&non_repo_config), | ||
|
r47463 | Err(CommandError::UnsupportedFeature { | ||
message: format_bytes!( | ||||
b"URL-like --repository {}", | ||||
repo_path_bytes | ||||
), | ||||
}), | ||||
|
r47576 | // 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), | ||||
|
r47463 | ) | ||
} | ||||
} | ||||
|
r48196 | let repo_arg = early_args.repo.unwrap_or(Vec::new()); | ||
let repo_path: Option<PathBuf> = { | ||||
if repo_arg.is_empty() { | ||||
None | ||||
} else { | ||||
let local_config = { | ||||
if std::env::var_os("HGRCSKIPREPO").is_none() { | ||||
|
r48197 | // TODO: handle errors from find_repo_root | ||
if let Ok(current_dir_path) = Repo::find_repo_root() { | ||||
|
r48196 | 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()) | ||||
{ | ||||
|
r47423 | 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 }) | ||||
|
r47343 | } | ||
|
r47424 | Err(error) => exit( | ||
|
r47470 | &initial_current_dir, | ||
|
r49582 | &Ui::new_infallible(&non_repo_config), | ||
|
r49176 | OnUnsupported::from_config(&non_repo_config), | ||
|
r47424 | Err(error.into()), | ||
|
r47576 | // 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), | ||||
|
r47424 | ), | ||
|
r47423 | }; | ||
let config = if let Ok(repo) = &repo_result { | ||||
repo.config() | ||||
} else { | ||||
&non_repo_config | ||||
}; | ||||
|
r49582 | let ui = Ui::new(&config).unwrap_or_else(|error| { | ||
exit( | ||||
&initial_current_dir, | ||||
&Ui::new_infallible(&config), | ||||
OnUnsupported::from_config(&config), | ||||
Err(error.into()), | ||||
config | ||||
.get_bool(b"ui", b"detailed-exit-code") | ||||
.unwrap_or(false), | ||||
) | ||||
}); | ||||
|
r49176 | let on_unsupported = OnUnsupported::from_config(config); | ||
|
r47423 | |||
let result = main_with_result( | ||||
&process_start_time, | ||||
&ui, | ||||
repo_result.as_ref(), | ||||
config, | ||||
); | ||||
|
r47576 | 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), | ||||
) | ||||
|
r47343 | } | ||
|
r47576 | fn exit_code( | ||
result: &Result<(), CommandError>, | ||||
use_detailed_exit_code: bool, | ||||
) -> i32 { | ||||
|
r47343 | match result { | ||
|
r48199 | Ok(()) => exit_codes::OK, | ||
|
r47576 | Err(CommandError::Abort { | ||
message: _, | ||||
detailed_exit_code, | ||||
}) => { | ||||
if use_detailed_exit_code { | ||||
*detailed_exit_code | ||||
} else { | ||||
|
r48199 | exit_codes::ABORT | ||
|
r47576 | } | ||
} | ||||
|
r48199 | Err(CommandError::Unsuccessful) => exit_codes::UNSUCCESSFUL, | ||
|
r47174 | |||
// Exit with a specific code and no error message to let a potential | ||||
// wrapper script fallback to Python-based Mercurial. | ||||
|
r47424 | Err(CommandError::UnsupportedFeature { .. }) => { | ||
|
r48199 | exit_codes::UNIMPLEMENTED | ||
|
r47424 | } | ||
|
r47343 | } | ||
|
r45503 | } | ||
|
r46100 | |||
|
r47424 | fn exit( | ||
|
r47470 | initial_current_dir: &Option<PathBuf>, | ||
|
r47424 | ui: &Ui, | ||
|
r47425 | mut on_unsupported: OnUnsupported, | ||
|
r47424 | result: Result<(), CommandError>, | ||
|
r47576 | use_detailed_exit_code: bool, | ||
|
r47424 | ) -> ! { | ||
|
r47425 | if let ( | ||
OnUnsupported::Fallback { executable }, | ||||
Err(CommandError::UnsupportedFeature { .. }), | ||||
) = (&on_unsupported, &result) | ||||
{ | ||||
let mut args = std::env::args_os(); | ||||
|
r49176 | let executable = match executable { | ||
None => { | ||||
exit_no_fallback( | ||||
ui, | ||||
OnUnsupported::Abort, | ||||
Err(CommandError::abort( | ||||
"abort: 'rhg.on-unsupported=fallback' without \ | ||||
'rhg.fallback-executable' set.", | ||||
)), | ||||
false, | ||||
); | ||||
} | ||||
Some(executable) => executable, | ||||
}; | ||||
|
r47425 | 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 { | ||||
|
r49176 | // `args` is now `argv[1..]` since we’ve already consumed | ||
// `argv[0]` | ||||
|
r47470 | 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(); | ||||
|
r47425 | match result { | ||
Ok(status) => std::process::exit( | ||||
|
r48199 | status.code().unwrap_or(exit_codes::ABORT), | ||
|
r47425 | ), | ||
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 | ||||
} | ||||
} | ||||
} | ||||
} | ||||
|
r47576 | exit_no_fallback(ui, on_unsupported, result, use_detailed_exit_code) | ||
|
r47482 | } | ||
fn exit_no_fallback( | ||||
ui: &Ui, | ||||
on_unsupported: OnUnsupported, | ||||
result: Result<(), CommandError>, | ||||
|
r47576 | use_detailed_exit_code: bool, | ||
|
r47482 | ) -> ! { | ||
|
r47424 | match &result { | ||
Ok(_) => {} | ||||
|
r47478 | Err(CommandError::Unsuccessful) => {} | ||
|
r47576 | Err(CommandError::Abort { | ||
message, | ||||
detailed_exit_code: _, | ||||
}) => { | ||||
|
r47424 | 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. | ||||
|
r47465 | let _ = ui.write_stderr(&format_bytes!(b"{}\n", message)); | ||
|
r47424 | } | ||
} | ||||
Err(CommandError::UnsupportedFeature { message }) => { | ||||
match on_unsupported { | ||||
OnUnsupported::Abort => { | ||||
let _ = ui.write_stderr(&format_bytes!( | ||||
b"unsupported feature: {}\n", | ||||
message | ||||
)); | ||||
} | ||||
OnUnsupported::AbortSilent => {} | ||||
|
r47425 | OnUnsupported::Fallback { .. } => unreachable!(), | ||
|
r47424 | } | ||
|
r47423 | } | ||
} | ||||
|
r47576 | std::process::exit(exit_code(&result, use_detailed_exit_code)) | ||
|
r47423 | } | ||
|
r47252 | macro_rules! subcommands { | ||
($( $command: ident )+) => { | ||||
mod commands { | ||||
$( | ||||
pub mod $command; | ||||
)+ | ||||
} | ||||
fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { | ||||
app | ||||
$( | ||||
|
r47351 | .subcommand(commands::$command::args()) | ||
|
r47252 | )+ | ||
} | ||||
|
r47213 | |||
|
r47334 | pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>; | ||
fn subcommand_run_fn(name: &str) -> Option<RunFn> { | ||||
|
r47252 | match name { | ||
$( | ||||
stringify!($command) => Some(commands::$command::run), | ||||
)+ | ||||
_ => None, | ||||
} | ||||
|
r46113 | } | ||
|
r47252 | }; | ||
|
r46100 | } | ||
|
r47252 | |||
subcommands! { | ||||
cat | ||||
debugdata | ||||
debugrequirements | ||||
|
r49178 | debugignorerhg | ||
|
r47252 | files | ||
root | ||||
|
r47255 | config | ||
|
r47578 | status | ||
|
r47252 | } | ||
|
r47578 | |||
|
r47334 | pub struct CliInvocation<'a> { | ||
ui: &'a Ui, | ||||
subcommand_args: &'a ArgMatches<'a>, | ||||
|
r47423 | config: &'a Config, | ||
|
r47335 | /// 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, | ||||
|
r47334 | } | ||
|
r47335 | |||
|
r47423 | /// 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 <https://github.com/clap-rs/clap/discussions/2366>. | ||||
/// | ||||
/// 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<Vec<u8>>, | ||||
|
r49583 | /// Value of all the `--color` argument, if any. | ||
color: Option<Vec<u8>>, | ||||
|
r47423 | /// Value of the `-R` or `--repository` argument, if any. | ||
repo: Option<Vec<u8>>, | ||||
|
r47470 | /// Value of the `--cwd` argument, if any. | ||
cwd: Option<Vec<u8>>, | ||||
|
r47423 | } | ||
impl EarlyArgs { | ||||
fn parse(args: impl IntoIterator<Item = OsString>) -> Self { | ||||
let mut args = args.into_iter().map(get_bytes_from_os_str); | ||||
let mut config = Vec::new(); | ||||
|
r49583 | let mut color = None; | ||
|
r47423 | let mut repo = None; | ||
|
r47470 | let mut cwd = None; | ||
|
r47423 | // 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()) | ||||
} | ||||
|
r49583 | if arg == b"--color" { | ||
if let Some(value) = args.next() { | ||||
color = Some(value) | ||||
} | ||||
} else if let Some(value) = arg.drop_prefix(b"--color=") { | ||||
color = Some(value.to_owned()) | ||||
} | ||||
|
r47470 | 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()) | ||||
} | ||||
|
r47423 | 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()) | ||||
} | ||||
|
r47335 | } | ||
|
r49583 | Self { | ||
config, | ||||
color, | ||||
repo, | ||||
cwd, | ||||
} | ||||
|
r47335 | } | ||
} | ||||
|
r47424 | |||
/// 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, | ||||
|
r47425 | /// Try running a Python implementation | ||
|
r49176 | Fallback { executable: Option<Vec<u8>> }, | ||
|
r47424 | } | ||
impl OnUnsupported { | ||||
|
r47425 | const DEFAULT: Self = OnUnsupported::Abort; | ||
|
r49176 | fn from_config(config: &Config) -> Self { | ||
|
r47425 | match config | ||
.get(b"rhg", b"on-unsupported") | ||||
.map(|value| value.to_ascii_lowercase()) | ||||
.as_deref() | ||||
{ | ||||
|
r47424 | Some(b"abort") => OnUnsupported::Abort, | ||
Some(b"abort-silent") => OnUnsupported::AbortSilent, | ||||
|
r47425 | Some(b"fallback") => OnUnsupported::Fallback { | ||
executable: config | ||||
.get(b"rhg", b"fallback-executable") | ||||
|
r49176 | .map(|x| x.to_owned()), | ||
|
r47425 | }, | ||
None => Self::DEFAULT, | ||||
|
r47424 | Some(_) => { | ||
// TODO: warn about unknown config value | ||||
|
r47425 | Self::DEFAULT | ||
|
r47424 | } | ||
} | ||||
} | ||||
} | ||||
|
r47467 | |||
|
r49270 | /// The `*` extension is an edge-case for config sub-options that apply to all | ||
/// extensions. For now, only `:required` exists, but that may change in the | ||||
/// future. | ||||
|
r49238 | const SUPPORTED_EXTENSIONS: &[&[u8]] = | ||
|
r49270 | &[b"blackbox", b"share", b"sparse", b"narrow", b"*"]; | ||
|
r47467 | |||
fn check_extensions(config: &Config) -> Result<(), CommandError> { | ||||
|
r49270 | let enabled: HashSet<&[u8]> = config | ||
.get_section_keys(b"extensions") | ||||
.into_iter() | ||||
.map(|extension| { | ||||
// Ignore extension suboptions. Only `required` exists for now. | ||||
// `rhg` either supports an extension or doesn't, so it doesn't | ||||
// make sense to consider the loading of an extension. | ||||
extension.split_2(b':').unwrap_or((extension, b"")).0 | ||||
}) | ||||
.collect(); | ||||
|
r47467 | |||
let mut unsupported = enabled; | ||||
for supported in SUPPORTED_EXTENSIONS { | ||||
unsupported.remove(supported); | ||||
} | ||||
|
r48763 | if let Some(ignored_list) = config.get_list(b"rhg", b"ignored-extensions") | ||
|
r47468 | { | ||
for ignored in ignored_list { | ||||
|
r48763 | unsupported.remove(ignored.as_slice()); | ||
|
r47468 | } | ||
} | ||||
|
r47467 | if unsupported.is_empty() { | ||
Ok(()) | ||||
} else { | ||||
Err(CommandError::UnsupportedFeature { | ||||
message: format_bytes!( | ||||
|
r47468 | b"extensions: {} (consider adding them to 'rhg.ignored-extensions' config)", | ||
|
r47467 | join(unsupported, b", ") | ||
), | ||||
}) | ||||
} | ||||
} | ||||
|
r49159 | |||
|
r49163 | fn check_unsupported( | ||
config: &Config, | ||||
|
r49341 | repo: Result<&Repo, &NoRepoInCwdError>, | ||
|
r49163 | ui: &ui::Ui, | ||
) -> Result<(), CommandError> { | ||||
|
r49159 | check_extensions(config)?; | ||
if std::env::var_os("HG_PENDING").is_some() { | ||||
// TODO: only if the value is `== repo.working_directory`? | ||||
// What about relative v.s. absolute paths? | ||||
Err(CommandError::unsupported("$HG_PENDING"))? | ||||
} | ||||
|
r49341 | if let Ok(repo) = repo { | ||
if repo.has_subrepos()? { | ||||
Err(CommandError::unsupported("sub-repositories"))? | ||||
} | ||||
} | ||||
|
r49162 | if config.has_non_empty_section(b"encode") { | ||
Err(CommandError::unsupported("[encode] config"))? | ||||
} | ||||
if config.has_non_empty_section(b"decode") { | ||||
Err(CommandError::unsupported("[decode] config"))? | ||||
} | ||||
|
r49163 | if let Some(color) = config.get(b"ui", b"color") { | ||
|
r49580 | if (color == b"always" || color == b"debug") | ||
&& !ui.plain(Some("color")) | ||||
{ | ||||
|
r49163 | Err(CommandError::unsupported("colored output"))? | ||
} | ||||
} | ||||
|
r49159 | Ok(()) | ||
} | ||||