Mercurial > hg
annotate rust/rhg/src/main.rs @ 46748:bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
When `rhg.on-unsupported` is configured to `fallback` and an unsupported
feature is encountered, the previous default was to look for an `hg`
executable in `$PATH`.
This default was fragile since it was easy to end up accidentally using
an older version of Mercurial installed system-wide,
when a local (perhaps patched) installation was intended.
Instead, it is now an error to have `rhg.on-unsupported=fallback`
without also configuring an explicit path or the fallback executable.
Differential Revision: https://phab.mercurial-scm.org/D10189
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Fri, 12 Mar 2021 21:44:07 +0100 |
parents | eb14264b98e8 |
children | 821929d59e01 |
rev | line source |
---|---|
45530
b1cea0dc9db0
rhg: Add debug timing
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45529
diff
changeset
|
1 extern crate log; |
46592
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
2 use crate::ui::Ui; |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
3 use clap::App; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
4 use clap::AppSettings; |
46503
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46502
diff
changeset
|
5 use clap::Arg; |
45529
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
6 use clap::ArgMatches; |
46733
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
7 use format_bytes::{format_bytes, join}; |
46592
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
8 use hg::config::Config; |
46593
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
9 use hg::repo::{Repo, RepoError}; |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
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:
46608
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:
46608
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:
46608
diff
changeset
|
13 use std::path::PathBuf; |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
14 use std::process::Command; |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
15 |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
16 mod blackbox; |
44982
bacf6c7ef01b
rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44981
diff
changeset
|
17 mod error; |
44981 | 18 mod exitcode; |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
19 mod ui; |
45529
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
20 use error::CommandError; |
44981 | 21 |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
22 fn main_with_result( |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
23 process_start_time: &blackbox::ProcessStartTime, |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
24 ui: &ui::Ui, |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
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:
46608
diff
changeset
|
26 config: &Config, |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
27 ) -> Result<(), CommandError> { |
46733
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
28 check_extensions(config)?; |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
29 |
45529
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
30 let app = App::new("rhg") |
46608
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
31 .global_setting(AppSettings::AllowInvalidUtf8) |
46746
eb14264b98e8
rhg: Fall back to Python for --version
Simon Sapin <simon.sapin@octobus.net>
parents:
46744
diff
changeset
|
32 .global_setting(AppSettings::DisableVersion) |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
33 .setting(AppSettings::SubcommandRequired) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
34 .setting(AppSettings::VersionlessSubcommands) |
46608
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
35 .arg( |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
36 Arg::with_name("repository") |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
37 .help("repository root directory") |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
38 .short("-R") |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
39 .long("--repository") |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
40 .value_name("REPO") |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
41 .takes_value(true) |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
42 // 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:
46601
diff
changeset
|
43 .global(true), |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
44 ) |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
45 .arg( |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
46 Arg::with_name("config") |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
47 .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:
46601
diff
changeset
|
48 .long("--config") |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
49 .value_name("CONFIG") |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
50 .takes_value(true) |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
51 .global(true) |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
52 // 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:
46601
diff
changeset
|
53 .multiple(true) |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
54 // 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:
46601
diff
changeset
|
55 .number_of_values(1), |
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
56 ) |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
57 .arg( |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
58 Arg::with_name("cwd") |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
59 .help("change working directory") |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
60 .long("--cwd") |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
61 .value_name("DIR") |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
62 .takes_value(true) |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
63 .global(true), |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
64 ) |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
65 .version("0.0.1"); |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
66 let app = add_subcommand_args(app); |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
67 |
46591
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46505
diff
changeset
|
68 let matches = app.clone().get_matches_safe()?; |
46503
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46502
diff
changeset
|
69 |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
70 let (subcommand_name, subcommand_matches) = matches.subcommand(); |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
71 let run = subcommand_run_fn(subcommand_name) |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
72 .expect("unknown subcommand name from clap despite AppSettings::SubcommandRequired"); |
46592
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
73 let subcommand_args = subcommand_matches |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
74 .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired"); |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
75 |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
76 let invocation = CliInvocation { |
46592
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
77 ui, |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
78 subcommand_args, |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
79 config, |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
80 repo, |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
81 }; |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
82 let blackbox = blackbox::Blackbox::new(&invocation, process_start_time)?; |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
83 blackbox.log_command_start(); |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
84 let result = run(&invocation); |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
85 blackbox.log_command_end(exit_code(&result)); |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
86 result |
46591
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46505
diff
changeset
|
87 } |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
88 |
46591
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46505
diff
changeset
|
89 fn main() { |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
90 // 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:
46596
diff
changeset
|
91 // 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:
46596
diff
changeset
|
92 // 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:
46596
diff
changeset
|
93 let process_start_time = blackbox::ProcessStartTime::now(); |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
94 |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
95 env_logger::init(); |
46593
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
96 let ui = ui::Ui::new(); |
46591
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46505
diff
changeset
|
97 |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
98 let early_args = EarlyArgs::parse(std::env::args_os()); |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
99 |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
100 let initial_current_dir = early_args.cwd.map(|cwd| { |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
101 let cwd = get_path_from_bytes(&cwd); |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
102 std::env::current_dir() |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
103 .and_then(|initial| { |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
104 std::env::set_current_dir(cwd)?; |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
105 Ok(initial) |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
106 }) |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
107 .unwrap_or_else(|error| { |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
108 exit( |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
109 &None, |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
110 &ui, |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
111 OnUnsupported::Abort, |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
112 Err(CommandError::abort(format!( |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
113 "abort: {}: '{}'", |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
114 error, |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
115 cwd.display() |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
116 ))), |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
117 ) |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
118 }) |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
119 }); |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
120 |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
121 let non_repo_config = |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
122 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:
46665
diff
changeset
|
123 // 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:
46665
diff
changeset
|
124 // 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:
46665
diff
changeset
|
125 // "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:
46665
diff
changeset
|
126 let on_unsupported = OnUnsupported::Abort; |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
127 |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
128 exit(&initial_current_dir, &ui, on_unsupported, Err(error.into())) |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
129 }); |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
130 |
46729
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
131 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:
46667
diff
changeset
|
132 lazy_static::lazy_static! { |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
133 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:
46667
diff
changeset
|
134 // 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:
46667
diff
changeset
|
135 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:
46667
diff
changeset
|
136 } |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
137 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:
46667
diff
changeset
|
138 exit( |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
139 &initial_current_dir, |
46729
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
140 &ui, |
46748
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
141 OnUnsupported::from_config(&ui, &non_repo_config), |
46729
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
142 Err(CommandError::UnsupportedFeature { |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
143 message: format_bytes!( |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
144 b"URL-like --repository {}", |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
145 repo_path_bytes |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
146 ), |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
147 }), |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
148 ) |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
149 } |
6cd9f53aaed8
rhg: Fall back to Python on --repository with an URL
Simon Sapin <simon.sapin@octobus.net>
parents:
46667
diff
changeset
|
150 } |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
151 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:
46608
diff
changeset
|
152 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:
46608
diff
changeset
|
153 Ok(repo) => Ok(repo), |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
154 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:
46608
diff
changeset
|
155 // 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:
46608
diff
changeset
|
156 Err(NoRepoInCwdError { cwd: at }) |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
157 } |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
158 Err(error) => exit( |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
159 &initial_current_dir, |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
160 &ui, |
46748
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
161 OnUnsupported::from_config(&ui, &non_repo_config), |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
162 Err(error.into()), |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
163 ), |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
164 }; |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
165 |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
166 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:
46608
diff
changeset
|
167 repo.config() |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
168 } else { |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
169 &non_repo_config |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
170 }; |
46748
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
171 let on_unsupported = OnUnsupported::from_config(&ui, config); |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
172 |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
173 let result = main_with_result( |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
174 &process_start_time, |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
175 &ui, |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
176 repo_result.as_ref(), |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
177 config, |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
178 ); |
46748
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
179 exit(&initial_current_dir, &ui, on_unsupported, result) |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
180 } |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
181 |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
182 fn exit_code(result: &Result<(), CommandError>) -> i32 { |
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
183 match result { |
46591
21d3b40b4c0e
rhg: Remove error message on unsupported CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46505
diff
changeset
|
184 Ok(()) => exitcode::OK, |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
185 Err(CommandError::Abort { .. }) => exitcode::ABORT, |
46744
b1f2c2b336ec
rhg: `cat` command: print error messages for missing files
Simon Sapin <simon.sapin@octobus.net>
parents:
46736
diff
changeset
|
186 Err(CommandError::Unsuccessful) => exitcode::UNSUCCESSFUL, |
46445
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45923
diff
changeset
|
187 |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
45923
diff
changeset
|
188 // 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:
45923
diff
changeset
|
189 // wrapper script fallback to Python-based Mercurial. |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
190 Err(CommandError::UnsupportedFeature { .. }) => { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
191 exitcode::UNIMPLEMENTED |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
192 } |
46601
755c31a1caf9
rhg: Add support for the blackbox extension
Simon Sapin <simon.sapin@octobus.net>
parents:
46596
diff
changeset
|
193 } |
44981 | 194 } |
45529
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
195 |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
196 fn exit( |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
197 initial_current_dir: &Option<PathBuf>, |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
198 ui: &Ui, |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
199 mut on_unsupported: OnUnsupported, |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
200 result: Result<(), CommandError>, |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
201 ) -> ! { |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
202 if let ( |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
203 OnUnsupported::Fallback { executable }, |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
204 Err(CommandError::UnsupportedFeature { .. }), |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
205 ) = (&on_unsupported, &result) |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
206 { |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
207 let mut args = std::env::args_os(); |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
208 let executable_path = get_path_from_bytes(&executable); |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
209 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:
46666
diff
changeset
|
210 if executable_path == &PathBuf::from(this_executable) { |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
211 // Avoid spawning infinitely many processes until resource |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
212 // exhaustion. |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
213 let _ = ui.write_stderr(&format_bytes!( |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
214 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:
46666
diff
changeset
|
215 points to `rhg` itself.\n", |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
216 executable |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
217 )); |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
218 on_unsupported = OnUnsupported::Abort |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
219 } else { |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
220 // `args` is now `argv[1..]` since we’ve already consumed `argv[0]` |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
221 let mut command = Command::new(executable_path); |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
222 command.args(args); |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
223 if let Some(initial) = initial_current_dir { |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
224 command.current_dir(initial); |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
225 } |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
226 let result = command.status(); |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
227 match result { |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
228 Ok(status) => std::process::exit( |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
229 status.code().unwrap_or(exitcode::ABORT), |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
230 ), |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
231 Err(error) => { |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
232 let _ = ui.write_stderr(&format_bytes!( |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
233 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:
46666
diff
changeset
|
234 executable, format_bytes::Utf8(error) |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
235 )); |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
236 on_unsupported = OnUnsupported::Abort |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
237 } |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
238 } |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
239 } |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
240 } |
46748
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
241 exit_no_fallback(ui, on_unsupported, result) |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
242 } |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
243 |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
244 fn exit_no_fallback( |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
245 ui: &Ui, |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
246 on_unsupported: OnUnsupported, |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
247 result: Result<(), CommandError>, |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
248 ) -> ! { |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
249 match &result { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
250 Ok(_) => {} |
46744
b1f2c2b336ec
rhg: `cat` command: print error messages for missing files
Simon Sapin <simon.sapin@octobus.net>
parents:
46736
diff
changeset
|
251 Err(CommandError::Unsuccessful) => {} |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
252 Err(CommandError::Abort { message }) => { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
253 if !message.is_empty() { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
254 // 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:
46665
diff
changeset
|
255 // with failure code so there’s not much more we can do. |
46731
3d692e724d06
rhg: Align config file parse error formatting with Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46729
diff
changeset
|
256 let _ = ui.write_stderr(&format_bytes!(b"{}\n", message)); |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
257 } |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
258 } |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
259 Err(CommandError::UnsupportedFeature { message }) => { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
260 match on_unsupported { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
261 OnUnsupported::Abort => { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
262 let _ = ui.write_stderr(&format_bytes!( |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
263 b"unsupported feature: {}\n", |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
264 message |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
265 )); |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
266 } |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
267 OnUnsupported::AbortSilent => {} |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
268 OnUnsupported::Fallback { .. } => unreachable!(), |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
269 } |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
270 } |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
271 } |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
272 std::process::exit(exit_code(&result)) |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
273 } |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
274 |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
275 macro_rules! subcommands { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
276 ($( $command: ident )+) => { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
277 mod commands { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
278 $( |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
279 pub mod $command; |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
280 )+ |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
281 } |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
282 |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
283 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:
46501
diff
changeset
|
284 app |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
285 $( |
46608
4e4c70401028
rhg: Use clap’s support for global CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46601
diff
changeset
|
286 .subcommand(commands::$command::args()) |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
287 )+ |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
288 } |
46484
a6e4e4650bac
rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents:
46445
diff
changeset
|
289 |
46592
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
290 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:
46591
diff
changeset
|
291 |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
292 fn subcommand_run_fn(name: &str) -> Option<RunFn> { |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
293 match name { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
294 $( |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
295 stringify!($command) => Some(commands::$command::run), |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
296 )+ |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
297 _ => None, |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
298 } |
45542
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45537
diff
changeset
|
299 } |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
300 }; |
45529
f17caf8f3fef
rhg: add a limited `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
301 } |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
302 |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
303 subcommands! { |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
304 cat |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
305 debugdata |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
306 debugrequirements |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
307 files |
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
308 root |
46505
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46504
diff
changeset
|
309 config |
46502
95d37db31479
rhg: Replace subcommand boilerplate with a macro
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
310 } |
46592
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
311 pub struct CliInvocation<'a> { |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
312 ui: &'a Ui, |
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
313 subcommand_args: &'a ArgMatches<'a>, |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
314 config: &'a Config, |
46593
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
315 /// References inside `Result` is a bit peculiar but allow |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
316 /// `invocation.repo?` to work out with `&CliInvocation` since this |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
317 /// `Result` type is `Copy`. |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
318 repo: Result<&'a Repo, &'a NoRepoInCwdError>, |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
319 } |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
320 |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
321 struct NoRepoInCwdError { |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
322 cwd: PathBuf, |
46592
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46591
diff
changeset
|
323 } |
46593
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
324 |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
325 /// 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:
46608
diff
changeset
|
326 /// 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:
46608
diff
changeset
|
327 /// 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:
46608
diff
changeset
|
328 /// |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
329 /// 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:
46608
diff
changeset
|
330 /// 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:
46608
diff
changeset
|
331 struct EarlyArgs { |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
332 /// 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:
46608
diff
changeset
|
333 config: Vec<Vec<u8>>, |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
334 /// 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:
46608
diff
changeset
|
335 repo: Option<Vec<u8>>, |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
336 /// Value of the `--cwd` argument, if any. |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
337 cwd: Option<Vec<u8>>, |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
338 } |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
339 |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
340 impl EarlyArgs { |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
341 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:
46608
diff
changeset
|
342 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:
46608
diff
changeset
|
343 let mut config = Vec::new(); |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
344 let mut repo = None; |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
345 let mut cwd = None; |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
346 // 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:
46608
diff
changeset
|
347 // `args.next()` inside the loop. |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
348 while let Some(arg) = args.next() { |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
349 if arg == b"--config" { |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
350 if let Some(value) = args.next() { |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
351 config.push(value) |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
352 } |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
353 } 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:
46608
diff
changeset
|
354 config.push(value.to_owned()) |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
355 } |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
356 |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
357 if arg == b"--cwd" { |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
358 if let Some(value) = args.next() { |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
359 cwd = Some(value) |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
360 } |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
361 } else if let Some(value) = arg.drop_prefix(b"--cwd=") { |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
362 cwd = Some(value.to_owned()) |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
363 } |
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
364 |
46665
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
365 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:
46608
diff
changeset
|
366 if let Some(value) = args.next() { |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
367 repo = Some(value) |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
368 } |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
369 } 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:
46608
diff
changeset
|
370 repo = Some(value.to_owned()) |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
371 } 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:
46608
diff
changeset
|
372 repo = Some(value.to_owned()) |
7284b524b441
rhg: Make configuration available as early as possible in main()
Simon Sapin <simon.sapin@octobus.net>
parents:
46608
diff
changeset
|
373 } |
46593
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
374 } |
46736
2255e7eb39e5
rhg: Add support for --cwd
Simon Sapin <simon.sapin@octobus.net>
parents:
46734
diff
changeset
|
375 Self { config, repo, cwd } |
46593
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
376 } |
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46592
diff
changeset
|
377 } |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
378 |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
379 /// What to do when encountering some unsupported feature. |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
380 /// |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
381 /// See `HgError::UnsupportedFeature` and `CommandError::UnsupportedFeature`. |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
382 enum OnUnsupported { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
383 /// 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:
46665
diff
changeset
|
384 /// and exit with code 252. |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
385 Abort, |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
386 /// Silently exit with code 252. |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
387 AbortSilent, |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
388 /// Try running a Python implementation |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
389 Fallback { executable: Vec<u8> }, |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
390 } |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
391 |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
392 impl OnUnsupported { |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
393 const DEFAULT: Self = OnUnsupported::Abort; |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
394 |
46748
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
395 fn from_config(ui: &Ui, config: &Config) -> Self { |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
396 match config |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
397 .get(b"rhg", b"on-unsupported") |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
398 .map(|value| value.to_ascii_lowercase()) |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
399 .as_deref() |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
400 { |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
401 Some(b"abort") => OnUnsupported::Abort, |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
402 Some(b"abort-silent") => OnUnsupported::AbortSilent, |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
403 Some(b"fallback") => OnUnsupported::Fallback { |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
404 executable: config |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
405 .get(b"rhg", b"fallback-executable") |
46748
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
406 .unwrap_or_else(|| { |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
407 exit_no_fallback( |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
408 ui, |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
409 Self::Abort, |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
410 Err(CommandError::abort( |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
411 "abort: 'rhg.on-unsupported=fallback' without \ |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
412 'rhg.fallback-executable' set." |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
413 )), |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
414 ) |
bde90e9b4507
rhg: Remove `rhg.fallback-executable=hg` default configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46746
diff
changeset
|
415 }) |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
416 .to_owned(), |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
417 }, |
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
418 None => Self::DEFAULT, |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
419 Some(_) => { |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
420 // TODO: warn about unknown config value |
46667
93e9f448273c
rhg: Add support for automatic fallback to Python
Simon Sapin <simon.sapin@octobus.net>
parents:
46666
diff
changeset
|
421 Self::DEFAULT |
46666
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
422 } |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
423 } |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
424 } |
33f2d56acc73
rhg: Add a `rhg.on-unsupported` configuration key
Simon Sapin <simon.sapin@octobus.net>
parents:
46665
diff
changeset
|
425 } |
46733
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
426 |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
427 const SUPPORTED_EXTENSIONS: &[&[u8]] = &[b"blackbox", b"share"]; |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
428 |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
429 fn check_extensions(config: &Config) -> Result<(), CommandError> { |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
430 let enabled = config.get_section_keys(b"extensions"); |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
431 |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
432 let mut unsupported = enabled; |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
433 for supported in SUPPORTED_EXTENSIONS { |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
434 unsupported.remove(supported); |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
435 } |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
436 |
46734
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
437 if let Some(ignored_list) = |
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
438 config.get_simple_list(b"rhg", b"ignored-extensions") |
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
439 { |
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
440 for ignored in ignored_list { |
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
441 unsupported.remove(ignored); |
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
442 } |
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
443 } |
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
444 |
46733
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
445 if unsupported.is_empty() { |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
446 Ok(()) |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
447 } else { |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
448 Err(CommandError::UnsupportedFeature { |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
449 message: format_bytes!( |
46734
1a036d33bc18
rhg: Add an allow-list of ignored extensions
Simon Sapin <simon.sapin@octobus.net>
parents:
46733
diff
changeset
|
450 b"extensions: {} (consider adding them to 'rhg.ignored-extensions' config)", |
46733
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
451 join(unsupported, b", ") |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
452 ), |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
453 }) |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
454 } |
1bac7764ceef
rhg: Fall back to Python if unsupported extensions are enabled
Simon Sapin <simon.sapin@octobus.net>
parents:
46731
diff
changeset
|
455 } |