##// END OF EJS Templates
rhg: refactor to pass argv down, instead of caling args_os()...
Arseniy Alekseyev -
r49943:86c49b00 default draft
parent child Browse files
Show More
@@ -5,6 +5,7 b' use format_bytes::format_bytes;'
5 5 use hg::errors::HgError;
6 6 use hg::repo::Repo;
7 7 use hg::utils::{files::get_bytes_from_os_str, shell_quote};
8 use std::ffi::OsString;
8 9
9 10 const ONE_MEBIBYTE: u64 = 1 << 20;
10 11
@@ -83,14 +84,21 b" impl<'a> Blackbox<'a> {"
83 84 })
84 85 }
85 86
86 pub fn log_command_start(&self) {
87 pub fn log_command_start<'arg>(
88 &self,
89 argv: impl Iterator<Item = &'arg OsString>,
90 ) {
87 91 if let Some(configured) = &self.configured {
88 let message = format_bytes!(b"(rust) {}", format_cli_args());
92 let message = format_bytes!(b"(rust) {}", format_cli_args(argv));
89 93 configured.log(&self.process_start_time.calendar_based, &message);
90 94 }
91 95 }
92 96
93 pub fn log_command_end(&self, exit_code: i32) {
97 pub fn log_command_end<'arg>(
98 &self,
99 argv: impl Iterator<Item = &'arg OsString>,
100 exit_code: i32,
101 ) {
94 102 if let Some(configured) = &self.configured {
95 103 let now = chrono::Local::now();
96 104 let duration = self
@@ -100,7 +108,7 b" impl<'a> Blackbox<'a> {"
100 108 .as_secs_f64();
101 109 let message = format_bytes!(
102 110 b"(rust) {} exited {} after {} seconds",
103 format_cli_args(),
111 format_cli_args(argv),
104 112 exit_code,
105 113 format_bytes::Utf8(format_args!("{:.03}", duration))
106 114 );
@@ -147,8 +155,9 b" impl ConfiguredBlackbox<'_> {"
147 155 }
148 156 }
149 157
150 fn format_cli_args() -> Vec<u8> {
151 let mut args = std::env::args_os();
158 fn format_cli_args<'a>(
159 mut args: impl Iterator<Item = &'a OsString>,
160 ) -> Vec<u8> {
152 161 let _ = args.next(); // Skip the first (or zeroth) arg, the name of the `rhg` executable
153 162 let mut args = args.map(|arg| shell_quote(&get_bytes_from_os_str(arg)));
154 163 let mut formatted = Vec::new();
@@ -25,6 +25,7 b' pub mod utils {'
25 25 }
26 26
27 27 fn main_with_result(
28 argv: Vec<OsString>,
28 29 process_start_time: &blackbox::ProcessStartTime,
29 30 ui: &ui::Ui,
30 31 repo: Result<&Repo, &NoRepoInCwdError>,
@@ -78,7 +79,7 b' fn main_with_result('
78 79 .version("0.0.1");
79 80 let app = add_subcommand_args(app);
80 81
81 let matches = app.clone().get_matches_safe()?;
82 let matches = app.clone().get_matches_from_safe(argv.iter())?;
82 83
83 84 let (subcommand_name, subcommand_matches) = matches.subcommand();
84 85
@@ -123,9 +124,9 b' fn main_with_result('
123 124 if config.is_extension_enabled(b"blackbox") {
124 125 let blackbox =
125 126 blackbox::Blackbox::new(&invocation, process_start_time)?;
126 blackbox.log_command_start();
127 blackbox.log_command_start(argv.iter());
127 128 let result = run(&invocation);
128 blackbox.log_command_end(exit_code(
129 blackbox.log_command_end(argv.iter(), exit_code(
129 130 &result,
130 131 // TODO: show a warning or combine with original error if
131 132 // `get_bool` returns an error
@@ -139,7 +140,7 b' fn main_with_result('
139 140 }
140 141 }
141 142
142 fn main() {
143 fn rhg_main(argv: Vec<OsString>) -> ! {
143 144 // Run this first, before we find out if the blackbox extension is even
144 145 // enabled, in order to include everything in-between in the duration
145 146 // measurements. Reading config files can be slow if they’re on NFS.
@@ -147,7 +148,7 b' fn main() {'
147 148
148 149 env_logger::init();
149 150
150 let early_args = EarlyArgs::parse(std::env::args_os());
151 let early_args = EarlyArgs::parse(&argv);
151 152
152 153 let initial_current_dir = early_args.cwd.map(|cwd| {
153 154 let cwd = get_path_from_bytes(&cwd);
@@ -158,6 +159,7 b' fn main() {'
158 159 })
159 160 .unwrap_or_else(|error| {
160 161 exit(
162 &argv,
161 163 &None,
162 164 &Ui::new_infallible(&Config::empty()),
163 165 OnUnsupported::Abort,
@@ -179,6 +181,7 b' fn main() {'
179 181 let on_unsupported = OnUnsupported::Abort;
180 182
181 183 exit(
184 &argv,
182 185 &initial_current_dir,
183 186 &Ui::new_infallible(&Config::empty()),
184 187 on_unsupported,
@@ -191,6 +194,7 b' fn main() {'
191 194 .load_cli_args(early_args.config, early_args.color)
192 195 .unwrap_or_else(|error| {
193 196 exit(
197 &argv,
194 198 &initial_current_dir,
195 199 &Ui::new_infallible(&non_repo_config),
196 200 OnUnsupported::from_config(&non_repo_config),
@@ -209,6 +213,7 b' fn main() {'
209 213 }
210 214 if SCHEME_RE.is_match(&repo_path_bytes) {
211 215 exit(
216 &argv,
212 217 &initial_current_dir,
213 218 &Ui::new_infallible(&non_repo_config),
214 219 OnUnsupported::from_config(&non_repo_config),
@@ -299,6 +304,7 b' fn main() {'
299 304 Err(NoRepoInCwdError { cwd: at })
300 305 }
301 306 Err(error) => exit(
307 &argv,
302 308 &initial_current_dir,
303 309 &Ui::new_infallible(&non_repo_config),
304 310 OnUnsupported::from_config(&non_repo_config),
@@ -318,6 +324,7 b' fn main() {'
318 324 };
319 325 let ui = Ui::new(&config).unwrap_or_else(|error| {
320 326 exit(
327 &argv,
321 328 &initial_current_dir,
322 329 &Ui::new_infallible(&config),
323 330 OnUnsupported::from_config(&config),
@@ -330,12 +337,14 b' fn main() {'
330 337 let on_unsupported = OnUnsupported::from_config(config);
331 338
332 339 let result = main_with_result(
340 argv.iter().map(|s| s.to_owned()).collect(),
333 341 &process_start_time,
334 342 &ui,
335 343 repo_result.as_ref(),
336 344 config,
337 345 );
338 346 exit(
347 &argv,
339 348 &initial_current_dir,
340 349 &ui,
341 350 on_unsupported,
@@ -348,6 +357,10 b' fn main() {'
348 357 )
349 358 }
350 359
360 fn main() -> ! {
361 rhg_main(std::env::args_os().collect())
362 }
363
351 364 fn exit_code(
352 365 result: &Result<(), CommandError>,
353 366 use_detailed_exit_code: bool,
@@ -374,7 +387,8 b' fn exit_code('
374 387 }
375 388 }
376 389
377 fn exit(
390 fn exit<'a>(
391 original_args: &'a [OsString],
378 392 initial_current_dir: &Option<PathBuf>,
379 393 ui: &Ui,
380 394 mut on_unsupported: OnUnsupported,
@@ -386,7 +400,7 b' fn exit('
386 400 Err(CommandError::UnsupportedFeature { message }),
387 401 ) = (&on_unsupported, &result)
388 402 {
389 let mut args = std::env::args_os();
403 let mut args = original_args.iter();
390 404 let executable = match executable {
391 405 None => {
392 406 exit_no_fallback(
@@ -546,7 +560,7 b' struct EarlyArgs {'
546 560 }
547 561
548 562 impl EarlyArgs {
549 fn parse(args: impl IntoIterator<Item = OsString>) -> Self {
563 fn parse<'a>(args: impl IntoIterator<Item = &'a OsString>) -> Self {
550 564 let mut args = args.into_iter().map(get_bytes_from_os_str);
551 565 let mut config = Vec::new();
552 566 let mut color = None;
General Comments 0
You need to be logged in to leave comments. Login now