annotate rust/chg/src/runcommand.rs @ 49886:becd16690cbe

debug: add a config to abort update early This is useful to benchmark the parts of [hg update] that come before the parallel worker.
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Fri, 06 Jan 2023 16:01:31 +0000
parents 426294d06ddc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39975
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
1 // Copyright 2018 Yuya Nishihara <yuya@tcha.org>
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
2 //
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
3 // This software may be used and distributed according to the terms of the
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
4 // GNU General Public License version 2 or any later version.
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
5
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
6 //! Functions to run Mercurial command in cHg-aware command server.
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
7
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
8 use bytes::Bytes;
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
9 use std::io;
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
10 use std::os::unix::io::AsRawFd;
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
11 use tokio_hglib::codec::ChannelMessage;
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
12 use tokio_hglib::{Connection, Protocol};
39975
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
13
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
14 use crate::attachio;
44689
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
15 use crate::message::{self, CommandType};
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
16 use crate::uihandler::SystemHandler;
39975
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
17
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
18 /// Runs the given Mercurial command in cHg-aware command server, and
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
19 /// fetches the result code.
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
20 ///
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
21 /// This is a subset of tokio-hglib's `run_command()` with the additional
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
22 /// SystemRequest support.
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
23 pub async fn run_command(
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
24 proto: &mut Protocol<impl Connection + AsRawFd>,
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
25 handler: &mut impl SystemHandler,
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
26 packed_args: impl Into<Bytes>,
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
27 ) -> io::Result<i32> {
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
28 proto
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
29 .send_command_with_args("runcommand", packed_args)
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
30 .await?;
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
31 loop {
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
32 match proto.fetch_response().await? {
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
33 ChannelMessage::Data(b'r', data) => {
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
34 return message::parse_result_code(data);
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
35 }
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
36 ChannelMessage::Data(..) => {
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
37 // just ignores data sent to optional channel
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
38 }
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44751
diff changeset
39 ChannelMessage::InputRequest(..)
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44751
diff changeset
40 | ChannelMessage::LineRequest(..) => {
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
41 return Err(io::Error::new(
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
42 io::ErrorKind::InvalidData,
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
43 "unsupported request",
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
44 ));
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
45 }
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
46 ChannelMessage::SystemRequest(data) => {
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
47 let (cmd_type, cmd_spec) = message::parse_command_spec(data)?;
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
48 match cmd_type {
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
49 CommandType::Pager => {
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
50 // server spins new command loop while pager request is
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
51 // in progress, which can be terminated by "" command.
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
52 let pin = handler.spawn_pager(&cmd_spec).await?;
45620
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44751
diff changeset
53 attachio::attach_io(proto, &io::stdin(), &pin, &pin)
426294d06ddc rust: move rustfmt.toml to repo root so it can be used by `hg fix`
Martin von Zweigbergk <martinvonz@google.com>
parents: 44751
diff changeset
54 .await?;
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
55 proto.send_command("").await?; // terminator
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
56 }
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
57 CommandType::System => {
44751
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
58 let code = handler.run_system(&cmd_spec).await?;
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
59 let data = message::pack_result_code(code);
94cace4b80ea rust-chg: reimplement run_command operation as async function
Yuya Nishihara <yuya@tcha.org>
parents: 44695
diff changeset
60 proto.send_data(data).await?;
44695
f87804825df5 rust-chg: indent process_message() to prepare mass rewrite to futures-0.3
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
61 }
39975
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
62 }
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
63 }
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
64 }
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
65 }
571d8eb39095 rust-chg: add state machine to handle "runcommand" request with cHg extension
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
66 }