annotate rust/rhg/src/main.rs @ 46742:6cd9f53aaed8

rhg: Fall back to Python on --repository with an URL A low-hanging fruit to improve on this would be to properly parse and handle `file:` URLs. But other Python-based hg supports some other URL schemes for features that rhg does not support yet. Differential Revision: https://phab.mercurial-scm.org/D10101
author Simon Sapin <simon.sapin@octobus.net>
date Wed, 03 Mar 2021 19:02:06 +0100
parents 93e9f448273c
children 3d692e724d06
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45536
b1cea0dc9db0 rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
1 extern crate log;
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
2 use crate::ui::Ui;
45051
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
3 use clap::App;
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
4 use clap::AppSettings;
46555
d8730ff51d5a rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46554
diff changeset
5 use clap::Arg;
45535
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45451
diff changeset
6 use clap::ArgMatches;
46513
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45938
diff changeset
7 use format_bytes::format_bytes;
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
8 use hg::config::Config;
46632
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
9 use hg::repo::{Repo, RepoError};
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
10 use hg::utils::files::{get_bytes_from_os_str, get_path_from_bytes};
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
11 use hg::utils::SliceExt;
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
12 use std::ffi::OsString;
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
13 use std::path::PathBuf;
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
14 use std::process::Command;
45051
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
15
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
16 mod blackbox;
45002
bacf6c7ef01b rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45001
diff changeset
17 mod error;
45001
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
18 mod exitcode;
45050
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45002
diff changeset
19 mod ui;
45535
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45451
diff changeset
20 use error::CommandError;
45001
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
21
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
22 fn main_with_result(
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
23 process_start_time: &blackbox::ProcessStartTime,
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
24 ui: &ui::Ui,
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
25 repo: Result<&Repo, &NoRepoInCwdError>,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
26 config: &Config,
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
27 ) -> Result<(), CommandError> {
45535
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45451
diff changeset
28 let app = App::new("rhg")
46647
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
29 .global_setting(AppSettings::AllowInvalidUtf8)
45051
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
30 .setting(AppSettings::SubcommandRequired)
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
31 .setting(AppSettings::VersionlessSubcommands)
46647
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
32 .arg(
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
33 Arg::with_name("repository")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
34 .help("repository root directory")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
35 .short("-R")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
36 .long("--repository")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
37 .value_name("REPO")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
38 .takes_value(true)
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
39 // Both ok: `hg -R ./foo log` or `hg log -R ./foo`
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
40 .global(true),
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
41 )
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
42 .arg(
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
43 Arg::with_name("config")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
44 .help("set/override config option (use 'section.name=value')")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
45 .long("--config")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
46 .value_name("CONFIG")
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
47 .takes_value(true)
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
48 .global(true)
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
49 // Ok: `--config section.key1=val --config section.key2=val2`
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
50 .multiple(true)
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
51 // Not ok: `--config section.key1=val section.key2=val2`
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
52 .number_of_values(1),
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
53 )
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
54 .version("0.0.1");
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
55 let app = add_subcommand_args(app);
45051
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
56
46630
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46557
diff changeset
57 let matches = app.clone().get_matches_safe()?;
46555
d8730ff51d5a rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46554
diff changeset
58
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
59 let (subcommand_name, subcommand_matches) = matches.subcommand();
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
60 let run = subcommand_run_fn(subcommand_name)
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
61 .expect("unknown subcommand name from clap despite AppSettings::SubcommandRequired");
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
62 let subcommand_args = subcommand_matches
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
63 .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired");
45051
18f8d3b31baa rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45050
diff changeset
64
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
65 let invocation = CliInvocation {
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
66 ui,
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
67 subcommand_args,
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
68 config,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
69 repo,
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
70 };
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
71 let blackbox = blackbox::Blackbox::new(&invocation, process_start_time)?;
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
72 blackbox.log_command_start();
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
73 let result = run(&invocation);
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
74 blackbox.log_command_end(exit_code(&result));
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
75 result
46630
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46557
diff changeset
76 }
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
77
46630
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46557
diff changeset
78 fn main() {
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
79 // Run this first, before we find out if the blackbox extension is even
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
80 // enabled, in order to include everything in-between in the duration
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
81 // measurements. Reading config files can be slow if they’re on NFS.
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
82 let process_start_time = blackbox::ProcessStartTime::now();
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
83
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
84 env_logger::init();
46632
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
85 let ui = ui::Ui::new();
46630
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46557
diff changeset
86
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
87 let early_args = EarlyArgs::parse(std::env::args_os());
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
88 let non_repo_config =
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
89 Config::load(early_args.config).unwrap_or_else(|error| {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
90 // Normally this is decided based on config, but we don’t have that
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
91 // available. As of this writing config loading never returns an
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
92 // "unsupported" error but that is not enforced by the type system.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
93 let on_unsupported = OnUnsupported::Abort;
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
94
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
95 exit(&ui, on_unsupported, Err(error.into()))
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
96 });
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
97
46742
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
98 if let Some(repo_path_bytes) = &early_args.repo {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
99 lazy_static::lazy_static! {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
100 static ref SCHEME_RE: regex::bytes::Regex =
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
101 // Same as `_matchscheme` in `mercurial/util.py`
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
102 regex::bytes::Regex::new("^[a-zA-Z0-9+.\\-]+:").unwrap();
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
103 }
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
104 if SCHEME_RE.is_match(&repo_path_bytes) {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
105 exit(
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
106 &ui,
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
107 OnUnsupported::from_config(&non_repo_config),
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
108 Err(CommandError::UnsupportedFeature {
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
109 message: format_bytes!(
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
110 b"URL-like --repository {}",
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
111 repo_path_bytes
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
112 ),
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
113 }),
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
114 )
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
115 }
6cd9f53aaed8 rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents: 46706
diff changeset
116 }
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
117 let repo_path = early_args.repo.as_deref().map(get_path_from_bytes);
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
118 let repo_result = match Repo::find(&non_repo_config, repo_path) {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
119 Ok(repo) => Ok(repo),
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
120 Err(RepoError::NotFound { at }) if repo_path.is_none() => {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
121 // Not finding a repo is not fatal yet, if `-R` was not given
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
122 Err(NoRepoInCwdError { cwd: at })
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
123 }
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
124 Err(error) => exit(
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
125 &ui,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
126 OnUnsupported::from_config(&non_repo_config),
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
127 Err(error.into()),
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
128 ),
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
129 };
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
130
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
131 let config = if let Ok(repo) = &repo_result {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
132 repo.config()
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
133 } else {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
134 &non_repo_config
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
135 };
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
136
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
137 let result = main_with_result(
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
138 &process_start_time,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
139 &ui,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
140 repo_result.as_ref(),
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
141 config,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
142 );
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
143 exit(&ui, OnUnsupported::from_config(config), result)
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
144 }
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
145
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
146 fn exit_code(result: &Result<(), CommandError>) -> i32 {
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
147 match result {
46630
21d3b40b4c0e rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46557
diff changeset
148 Ok(()) => exitcode::OK,
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
149 Err(CommandError::Abort { .. }) => exitcode::ABORT,
46513
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45938
diff changeset
150
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45938
diff changeset
151 // Exit with a specific code and no error message to let a potential
ca3f73cc3cf4 rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents: 45938
diff changeset
152 // wrapper script fallback to Python-based Mercurial.
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
153 Err(CommandError::UnsupportedFeature { .. }) => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
154 exitcode::UNIMPLEMENTED
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
155 }
46640
755c31a1caf9 rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents: 46635
diff changeset
156 }
45001
cf04f62d1579 rhg: add rhg crate
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
157 }
45535
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45451
diff changeset
158
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
159 fn exit(
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
160 ui: &Ui,
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
161 mut on_unsupported: OnUnsupported,
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
162 result: Result<(), CommandError>,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
163 ) -> ! {
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
164 if let (
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
165 OnUnsupported::Fallback { executable },
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
166 Err(CommandError::UnsupportedFeature { .. }),
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
167 ) = (&on_unsupported, &result)
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
168 {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
169 let mut args = std::env::args_os();
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
170 let executable_path = get_path_from_bytes(&executable);
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
171 let this_executable = args.next().expect("exepcted argv[0] to exist");
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
172 if executable_path == &PathBuf::from(this_executable) {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
173 // Avoid spawning infinitely many processes until resource
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
174 // exhaustion.
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
175 let _ = ui.write_stderr(&format_bytes!(
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
176 b"Blocking recursive fallback. The 'rhg.fallback-executable = {}' config \
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
177 points to `rhg` itself.\n",
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
178 executable
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
179 ));
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
180 on_unsupported = OnUnsupported::Abort
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
181 } else {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
182 // `args` is now `argv[1..]` since we’ve already consumed `argv[0]`
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
183 let result = Command::new(executable_path).args(args).status();
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
184 match result {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
185 Ok(status) => std::process::exit(
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
186 status.code().unwrap_or(exitcode::ABORT),
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
187 ),
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
188 Err(error) => {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
189 let _ = ui.write_stderr(&format_bytes!(
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
190 b"tried to fall back to a '{}' sub-process but got error {}\n",
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
191 executable, format_bytes::Utf8(error)
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
192 ));
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
193 on_unsupported = OnUnsupported::Abort
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
194 }
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
195 }
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
196 }
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
197 }
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
198 match &result {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
199 Ok(_) => {}
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
200 Err(CommandError::Abort { message }) => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
201 if !message.is_empty() {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
202 // Ignore errors when writing to stderr, we’re already exiting
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
203 // with failure code so there’s not much more we can do.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
204 let _ =
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
205 ui.write_stderr(&format_bytes!(b"abort: {}\n", message));
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
206 }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
207 }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
208 Err(CommandError::UnsupportedFeature { message }) => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
209 match on_unsupported {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
210 OnUnsupported::Abort => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
211 let _ = ui.write_stderr(&format_bytes!(
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
212 b"unsupported feature: {}\n",
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
213 message
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
214 ));
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
215 }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
216 OnUnsupported::AbortSilent => {}
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
217 OnUnsupported::Fallback { .. } => unreachable!(),
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
218 }
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
219 }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
220 }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
221 std::process::exit(exit_code(&result))
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
222 }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
223
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
224 macro_rules! subcommands {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
225 ($( $command: ident )+) => {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
226 mod commands {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
227 $(
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
228 pub mod $command;
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
229 )+
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
230 }
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
231
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
232 fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
233 app
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
234 $(
46647
4e4c70401028 rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46640
diff changeset
235 .subcommand(commands::$command::args())
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
236 )+
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
237 }
46543
a6e4e4650bac rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents: 46513
diff changeset
238
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
239 pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>;
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
240
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
241 fn subcommand_run_fn(name: &str) -> Option<RunFn> {
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
242 match name {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
243 $(
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
244 stringify!($command) => Some(commands::$command::run),
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
245 )+
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
246 _ => None,
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
247 }
45548
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45543
diff changeset
248 }
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
249 };
45535
f17caf8f3fef rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45451
diff changeset
250 }
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
251
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
252 subcommands! {
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
253 cat
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
254 debugdata
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
255 debugrequirements
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
256 files
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
257 root
46557
a25033eb43b5 rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents: 46556
diff changeset
258 config
46554
95d37db31479 rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents: 46553
diff changeset
259 }
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
260 pub struct CliInvocation<'a> {
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
261 ui: &'a Ui,
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
262 subcommand_args: &'a ArgMatches<'a>,
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
263 config: &'a Config,
46632
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
264 /// References inside `Result` is a bit peculiar but allow
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
265 /// `invocation.repo?` to work out with `&CliInvocation` since this
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
266 /// `Result` type is `Copy`.
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
267 repo: Result<&'a Repo, &'a NoRepoInCwdError>,
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
268 }
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
269
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
270 struct NoRepoInCwdError {
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
271 cwd: PathBuf,
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46630
diff changeset
272 }
46632
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
273
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
274 /// CLI arguments to be parsed "early" in order to be able to read
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
275 /// configuration before using Clap. Ideally we would also use Clap for this,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
276 /// see <https://github.com/clap-rs/clap/discussions/2366>.
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
277 ///
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
278 /// These arguments are still declared when we do use Clap later, so that Clap
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
279 /// does not return an error for their presence.
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
280 struct EarlyArgs {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
281 /// Values of all `--config` arguments. (Possibly none)
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
282 config: Vec<Vec<u8>>,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
283 /// Value of the `-R` or `--repository` argument, if any.
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
284 repo: Option<Vec<u8>>,
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
285 }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
286
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
287 impl EarlyArgs {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
288 fn parse(args: impl IntoIterator<Item = OsString>) -> Self {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
289 let mut args = args.into_iter().map(get_bytes_from_os_str);
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
290 let mut config = Vec::new();
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
291 let mut repo = None;
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
292 // Use `while let` instead of `for` so that we can also call
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
293 // `args.next()` inside the loop.
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
294 while let Some(arg) = args.next() {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
295 if arg == b"--config" {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
296 if let Some(value) = args.next() {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
297 config.push(value)
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
298 }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
299 } else if let Some(value) = arg.drop_prefix(b"--config=") {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
300 config.push(value.to_owned())
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
301 }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
302
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
303 if arg == b"--repository" || arg == b"-R" {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
304 if let Some(value) = args.next() {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
305 repo = Some(value)
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
306 }
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
307 } else if let Some(value) = arg.drop_prefix(b"--repository=") {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
308 repo = Some(value.to_owned())
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
309 } else if let Some(value) = arg.drop_prefix(b"-R") {
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
310 repo = Some(value.to_owned())
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
311 }
46632
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
312 }
46704
7284b524b441 rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents: 46647
diff changeset
313 Self { config, repo }
46632
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
314 }
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46631
diff changeset
315 }
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
316
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
317 /// What to do when encountering some unsupported feature.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
318 ///
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
319 /// See `HgError::UnsupportedFeature` and `CommandError::UnsupportedFeature`.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
320 enum OnUnsupported {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
321 /// Print an error message describing what feature is not supported,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
322 /// and exit with code 252.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
323 Abort,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
324 /// Silently exit with code 252.
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
325 AbortSilent,
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
326 /// Try running a Python implementation
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
327 Fallback { executable: Vec<u8> },
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
328 }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
329
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
330 impl OnUnsupported {
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
331 const DEFAULT: Self = OnUnsupported::Abort;
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
332 const DEFAULT_FALLBACK_EXECUTABLE: &'static [u8] = b"hg";
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
333
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
334 fn from_config(config: &Config) -> Self {
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
335 match config
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
336 .get(b"rhg", b"on-unsupported")
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
337 .map(|value| value.to_ascii_lowercase())
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
338 .as_deref()
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
339 {
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
340 Some(b"abort") => OnUnsupported::Abort,
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
341 Some(b"abort-silent") => OnUnsupported::AbortSilent,
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
342 Some(b"fallback") => OnUnsupported::Fallback {
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
343 executable: config
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
344 .get(b"rhg", b"fallback-executable")
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
345 .unwrap_or(Self::DEFAULT_FALLBACK_EXECUTABLE)
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
346 .to_owned(),
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
347 },
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
348 None => Self::DEFAULT,
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
349 Some(_) => {
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
350 // TODO: warn about unknown config value
46706
93e9f448273c rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents: 46705
diff changeset
351 Self::DEFAULT
46705
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
352 }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
353 }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
354 }
33f2d56acc73 rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents: 46704
diff changeset
355 }