Differences between revisions 8 and 9
Revision 8 as of 2011-06-13 15:07:25
Size: 4468
Editor: IdanKamara
Comment:
Revision 9 as of 2011-06-13 15:15:37
Size: 4617
Editor: IdanKamara
Comment:
Deletions are marked like this. Additions are marked like this.
Line 35: Line 35:
For now the server cannot be queried for the local encoding. If the client wishes to set it, it should start the server with the proper HGENCODING.

Command Server

A server that allows communication with Mercurial's API over a pipe.

1. Protocol

All communication with the server is done on stdin/stdout. The byte order used by the server is big-endian.

Data sent from the server is channel based, meaning a (channel [character], length [unsigned int]) pair is sent before the actual data. For example:

o
1234
<data: 1234 bytes>

that is 1234 bytes sent on channel 'o', with the data following.

When starting the server, it will send a new-line separated list of capabilities (on the 'o' channel), in this format:

capabilities: capability1\n
capability2\n
...

At the most basic level, the server will support the 'runcommand' capability.

1.1. Encoding

Strings are encoded in the local encoding. For now the server cannot be queried for the local encoding. If the client wishes to set it, it should start the server with the proper HGENCODING.

1.2. Channels

There are currently 5 channels:

  • o - Output channel. Most of the communication happens on this channel. When running commands, output Mercurial writes to stdout is written to this channel.
  • e - Error channel. When running commands, this correlates to stderr.
  • i - Input channel. The length field here can either be 0, telling the client to send all input, or some positive number telling the client to send at most <length> bytes.

  • l - Line based input channel. The client should send a single line of input (trimmed if length is not 0). This channel is used when Mercurial interacts with the user or when iterating over stdin.

Input should be sent on stdin in the following format:

length
data

length = 0 sent by the client is interpreted as EOF by the server.

  • d - Debug channel. Used when the server is started with logging to '-'.

1.3. Capabilities

The server is running on an endless loop (until stdin is closed) waiting for commands. A command request looks like this:

commandname\n
<command specific request>
  • runcommand - Run the command specified by a list of \0-terminated strings. An unsigned int indicating the length of the arguments should be sent before the list. Example:

runcommand\n
8
log\0
-l\0
5

Which corresponds to running 'hg log -l 5'.

The server responds with input/output generated by Mercurial on the matching channels. When the command returns, the server writes the following to the 'o' channel:

\0
<return code>

1.4. Examples

1.4.1. runcommand

Complete example of a client running 'hg summary', right after starting the server:

(text in the server column is <channel>: <length>, where length is really 4 byte unsigned ints, not plain text like below)

server

client

notes

connected, waiting for capabilities

o: 24
capabilities: runcommand

waiting for a command

runcommand\n
7
summary

client talks to server on stdin

starts running command

o: 27
parent: 14571:17c0cb1045e5

o: 3
tip

o: 1
\n

o: 53
paper, coal: display diffstat on the changeset page\n

o: 16
branch: default\n

o: 16
commit: (clean)\n

o: 18
update: (current)\n

o: 5
\0 0

server finished running command, writes \0+returncode to the client

closes server stdin

client disconnects

server exits

client waits for server to exit

And another one with activity on the input channels too by running 'import -':

(starting after client read capabilities)

server

client

notes

waiting for a command

runcommand\n
8
import\0
-

client talks to server on stdin

starts running command

o: 26
applying patch from stdin\n

l: 0

server tells client to send it a line

21
# HG changeset patch\n

client responds with <length><line>

l: 0

server processes line, asks for another one

...this goes on until the client has no more input

l: 0

0

it responds with length=0

o: 5
\0 0

server finished running command, writes \0+returncode to the client

closes server stdin

client disconnects

server exits

client waits for server to exit


CategoryDeveloper

CommandServer (last edited 2022-12-23 22:42:51 by gavenkoa)