Mercurial > public > mercurial-scm > hg-stable
annotate rust/chg/src/main.rs @ 44853:a347a329e48d
rust-chg: reimplement locator by using async/await and tokio-0.2
connect_spawned() is rewritten from scratch by using std::process. Before,
it would select completion of either connection or server process. New code
could be implemented as such, but it's much simpler to occasionally run
try_wait() to detect server death.
Differential Revision: https://phab.mercurial-scm.org/D8447
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 11 Apr 2020 00:47:32 +0900 |
parents | 5ac5c25ea97b |
children | 9fc9526e283a |
rev | line source |
---|---|
39968 | 1 // Copyright 2018 Yuya Nishihara <yuya@tcha.org> |
2 // | |
3 // This software may be used and distributed according to the terms of the | |
4 // GNU General Public License version 2 or any later version. | |
5 | |
44682
00ac60658654
rust-chg: collect server flags from command arguments
Yuya Nishihara <yuya@tcha.org>
parents:
44674
diff
changeset
|
6 use chg::locator::{self, Locator}; |
40121
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
7 use chg::procutil; |
43836
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
8 use chg::{ChgClientExt, ChgUiHandler}; |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
9 use futures::sync::oneshot; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
10 use std::env; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
11 use std::io; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
12 use std::process; |
40288
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
13 use std::time::Instant; |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
14 use tokio::prelude::*; |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
15 |
40288
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
16 struct DebugLogger { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
17 start: Instant, |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
18 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
19 |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
20 impl DebugLogger { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
21 pub fn new() -> DebugLogger { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
22 DebugLogger { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
23 start: Instant::now(), |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
24 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
25 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
26 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
27 |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
28 impl log::Log for DebugLogger { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
29 fn enabled(&self, metadata: &log::Metadata) -> bool { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
30 metadata.target().starts_with("chg::") |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
31 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
32 |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
33 fn log(&self, record: &log::Record) { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
34 if self.enabled(record.metadata()) { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
35 // just make the output looks similar to chg of C |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
36 let l = format!("{}", record.level()).to_lowercase(); |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
37 let t = self.start.elapsed(); |
43836
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
38 writeln!( |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
39 io::stderr(), |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
40 "chg: {}: {}.{:06} {}", |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
41 l, |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
42 t.as_secs(), |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
43 t.subsec_micros(), |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
44 record.args() |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
45 ) |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
46 .unwrap_or(()); |
40288
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
47 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
48 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
49 |
43836
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
50 fn flush(&self) {} |
40288
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
51 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
52 |
39968 | 53 fn main() { |
40288
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
54 if env::var_os("CHGDEBUG").is_some() { |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
55 log::set_boxed_logger(Box::new(DebugLogger::new())) |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
56 .expect("any logger should not be installed yet"); |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
57 log::set_max_level(log::LevelFilter::Debug); |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
58 } |
87c76e5f3427
rust-chg: install logger if $CHGDEBUG is set
Yuya Nishihara <yuya@tcha.org>
parents:
40286
diff
changeset
|
59 |
44672
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
43836
diff
changeset
|
60 // TODO: add loop detection by $CHGINTERNALMARK |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
43836
diff
changeset
|
61 |
44694
5ac5c25ea97b
rust-chg: move get_umask() call out of run() function
Yuya Nishihara <yuya@tcha.org>
parents:
44689
diff
changeset
|
62 let umask = unsafe { procutil::get_umask() }; // not thread safe |
5ac5c25ea97b
rust-chg: move get_umask() call out of run() function
Yuya Nishihara <yuya@tcha.org>
parents:
44689
diff
changeset
|
63 let code = run(umask).unwrap_or_else(|err| { |
40286
af52181f71ff
rust-chg: suppress panic while writing chg error to stderr
Yuya Nishihara <yuya@tcha.org>
parents:
40121
diff
changeset
|
64 writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(()); |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
65 255 |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
66 }); |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
67 process::exit(code); |
39968 | 68 } |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
69 |
44694
5ac5c25ea97b
rust-chg: move get_umask() call out of run() function
Yuya Nishihara <yuya@tcha.org>
parents:
44689
diff
changeset
|
70 fn run(umask: u32) -> io::Result<i32> { |
44682
00ac60658654
rust-chg: collect server flags from command arguments
Yuya Nishihara <yuya@tcha.org>
parents:
44674
diff
changeset
|
71 let mut loc = Locator::prepare_from_env()?; |
00ac60658654
rust-chg: collect server flags from command arguments
Yuya Nishihara <yuya@tcha.org>
parents:
44674
diff
changeset
|
72 loc.set_early_args(locator::collect_early_args(env::args_os().skip(1))); |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
73 let handler = ChgUiHandler::new(); |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
74 let (result_tx, result_rx) = oneshot::channel(); |
44672
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
43836
diff
changeset
|
75 let fut = loc |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
43836
diff
changeset
|
76 .connect() |
44674
0a2516efc463
rust-chg: move set_current_dir() to Locator
Yuya Nishihara <yuya@tcha.org>
parents:
44672
diff
changeset
|
77 .and_then(|(_, client)| client.attach_io(io::stdin(), io::stdout(), io::stderr())) |
44684
065048e66f32
rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents:
44682
diff
changeset
|
78 .and_then(move |client| client.set_umask(umask)) |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
79 .and_then(|client| { |
40121
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
80 let pid = client.server_spec().process_id.unwrap(); |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
81 let pgid = client.server_spec().process_group_id; |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
82 procutil::setup_signal_handler_once(pid, pgid)?; |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
83 Ok(client) |
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
84 }) |
43836
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
85 .and_then(|client| client.run_command_chg(handler, env::args_os().skip(1))) |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
86 .map(|(_client, _handler, code)| { |
40121
89742f1fa6cb
rust-chg: install signal handlers to forward signals to server
Yuya Nishihara <yuya@tcha.org>
parents:
39980
diff
changeset
|
87 procutil::restore_signal_handler_once()?; |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
88 Ok(code) |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
89 }) |
43836
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
90 .or_else(|err| Ok(Err(err))) // pass back error to caller |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
91 .map(|res| result_tx.send(res).unwrap()); |
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
92 tokio::run(fut); |
43836
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
93 result_rx.wait().unwrap_or(Err(io::Error::new( |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
94 io::ErrorKind::Other, |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
95 "no exit code set", |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40288
diff
changeset
|
96 ))) |
39980
6bdee4bc181a
rust-chg: add main program
Yuya Nishihara <yuya@tcha.org>
parents:
39968
diff
changeset
|
97 } |