summaryrefslogtreecommitdiff
path: root/Documentation/technical/protocol-v2.txt
blob: bbb8b14d27f1e1caa4e3cb0ca49b20f86a12cf38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
 Git Wire Protocol, Version 2
==============================

This document presents a specification for a version 2 of Git's wire
protocol.  Protocol v2 will improve upon v1 in the following ways:

  * Instead of multiple service names, multiple commands will be
    supported by a single service
  * Easily extendable as capabilities are moved into their own section
    of the protocol, no longer being hidden behind a NUL byte and
    limited by the size of a pkt-line
  * Separate out other information hidden behind NUL bytes (e.g. agent
    string as a capability and symrefs can be requested using 'ls-refs')
  * Reference advertisement will be omitted unless explicitly requested
  * ls-refs command to explicitly request some refs
  * Designed with http and stateless-rpc in mind.  With clear flush
    semantics the http remote helper can simply act as a proxy

In protocol v2 communication is command oriented.  When first contacting a
server a list of capabilities will advertised.  Some of these capabilities
will be commands which a client can request be executed.  Once a command
has completed, a client can reuse the connection and request that other
commands be executed.

 Packet-Line Framing
---------------------

All communication is done using packet-line framing, just as in v1.  See
`Documentation/technical/pack-protocol.txt` and
`Documentation/technical/protocol-common.txt` for more information.

In protocol v2 these special packets will have the following semantics:

  * '0000' Flush Packet (flush-pkt) - indicates the end of a message
  * '0001' Delimiter Packet (delim-pkt) - separates sections of a message

 Initial Client Request
------------------------

In general a client can request to speak protocol v2 by sending
`version=2` through the respective side-channel for the transport being
used which inevitably sets `GIT_PROTOCOL`.  More information can be
found in `pack-protocol.txt` and `http-protocol.txt`.  In all cases the
response from the server is the capability advertisement.

 Git Transport
~~~~~~~~~~~~~~~

When using the git:// transport, you can request to use protocol v2 by
sending "version=2" as an extra parameter:

   003egit-upload-pack /project.git\0host=myserver.com\0\0version=2\0

 SSH and File Transport
~~~~~~~~~~~~~~~~~~~~~~~~

When using either the ssh:// or file:// transport, the GIT_PROTOCOL
environment variable must be set explicitly to include "version=2".

 HTTP Transport
~~~~~~~~~~~~~~~~

When using the http:// or https:// transport a client makes a "smart"
info/refs request as described in `http-protocol.txt` and requests that
v2 be used by supplying "version=2" in the `Git-Protocol` header.

   C: Git-Protocol: version=2
   C:
   C: GET $GIT_URL/info/refs?service=git-upload-pack HTTP/1.0

A v2 server would reply:

   S: 200 OK
   S: <Some headers>
   S: ...
   S:
   S: 000eversion 2\n
   S: <capability-advertisement>

Subsequent requests are then made directly to the service
`$GIT_URL/git-upload-pack`. (This works the same for git-receive-pack).

 Capability Advertisement
--------------------------

A server which decides to communicate (based on a request from a client)
using protocol version 2, notifies the client by sending a version string
in its initial response followed by an advertisement of its capabilities.
Each capability is a key with an optional value.  Clients must ignore all
unknown keys.  Semantics of unknown values are left to the definition of
each key.  Some capabilities will describe commands which can be requested
to be executed by the client.

    capability-advertisement = protocol-version
			       capability-list
			       flush-pkt

    protocol-version = PKT-LINE("version 2" LF)
    capability-list = *capability
    capability = PKT-LINE(key[=value] LF)

    key = 1*(ALPHA | DIGIT | "-_")
    value = 1*(ALPHA | DIGIT | " -_.,?\/{}[]()<>!@#$%^&*+=:;")

 Command Request
-----------------

After receiving the capability advertisement, a client can then issue a
request to select the command it wants with any particular capabilities
or arguments.  There is then an optional section where the client can
provide any command specific parameters or queries.  Only a single
command can be requested at a time.

    request = empty-request | command-request
    empty-request = flush-pkt
    command-request = command
		      capability-list
		      [command-args]
		      flush-pkt
    command = PKT-LINE("command=" key LF)
    command-args = delim-pkt
		   *command-specific-arg

    command-specific-args are packet line framed arguments defined by
    each individual command.

The server will then check to ensure that the client's request is
comprised of a valid command as well as valid capabilities which were
advertised.  If the request is valid the server will then execute the
command.  A server MUST wait till it has received the client's entire
request before issuing a response.  The format of the response is
determined by the command being executed, but in all cases a flush-pkt
indicates the end of the response.

When a command has finished, and the client has received the entire
response from the server, a client can either request that another
command be executed or can terminate the connection.  A client may
optionally send an empty request consisting of just a flush-pkt to
indicate that no more requests will be made.

 Capabilities
--------------

There are two different types of capabilities: normal capabilities,
which can be used to to convey information or alter the behavior of a
request, and commands, which are the core actions that a client wants to
perform (fetch, push, etc).

Protocol version 2 is stateless by default.  This means that all commands
must only last a single round and be stateless from the perspective of the
server side, unless the client has requested a capability indicating that
state should be maintained by the server.  Clients MUST NOT require state
management on the server side in order to function correctly.  This
permits simple round-robin load-balancing on the server side, without
needing to worry about state management.

 agent
~~~~~~~

The server can advertise the `agent` capability with a value `X` (in the
form `agent=X`) to notify the client that the server is running version
`X`.  The client may optionally send its own agent string by including
the `agent` capability with a value `Y` (in the form `agent=Y`) in its
request to the server (but it MUST NOT do so if the server did not
advertise the agent capability). The `X` and `Y` strings may contain any
printable ASCII characters except space (i.e., the byte range 32 < x <
127), and are typically of the form "package/version" (e.g.,
"git/1.8.3.1"). The agent strings are purely informative for statistics
and debugging purposes, and MUST NOT be used to programmatically assume
the presence or absence of particular features.

 ls-refs
~~~~~~~~~

`ls-refs` is the command used to request a reference advertisement in v2.
Unlike the current reference advertisement, ls-refs takes in arguments
which can be used to limit the refs sent from the server.

Additional features not supported in the base command will be advertised
as the value of the command in the capability advertisement in the form
of a space separated list of features: "<command>=<feature 1> <feature 2>"

ls-refs takes in the following arguments:

    symrefs
	In addition to the object pointed by it, show the underlying ref
	pointed by it when showing a symbolic ref.
    peel
	Show peeled tags.
    ref-prefix <prefix>
	When specified, only references having a prefix matching one of
	the provided prefixes are displayed.

The output of ls-refs is as follows:

    output = *ref
	     flush-pkt
    ref = PKT-LINE(obj-id SP refname *(SP ref-attribute) LF)
    ref-attribute = (symref | peeled)
    symref = "symref-target:" symref-target
    peeled = "peeled:" obj-id