Merge branch 'sg/subtree-signed-commits' into pu
[git] / Documentation / technical / protocol-v2.txt
1  Git Wire Protocol, Version 2
2 ==============================
3
4 This document presents a specification for a version 2 of Git's wire
5 protocol.  Protocol v2 will improve upon v1 in the following ways:
6
7   * Instead of multiple service names, multiple commands will be
8     supported by a single service
9   * Easily extendable as capabilities are moved into their own section
10     of the protocol, no longer being hidden behind a NUL byte and
11     limited by the size of a pkt-line (as there will be a single
12     capability per pkt-line)
13   * Separate out other information hidden behind NUL bytes (e.g. agent
14     string as a capability and symrefs can be requested using 'ls-refs')
15   * Reference advertisement will be omitted unless explicitly requested
16   * ls-refs command to explicitly request some refs
17   * Designed with http and stateless-rpc in mind.  With clear flush
18     semantics the http remote helper can simply act as a proxy.
19
20  Detailed Design
21 =================
22
23 A client can request to speak protocol v2 by sending `version=2` in the
24 side-channel `GIT_PROTOCOL` in the initial request to the server.
25
26 In protocol v2 communication is command oriented.  When first contacting a
27 server a list of capabilities will advertised.  Some of these capabilities
28 will be commands which a client can request be executed.  Once a command
29 has completed, a client can reuse the connection and request that other
30 commands be executed.
31
32  Special Packets
33 -----------------
34
35 In protocol v2 these special packets will have the following semantics:
36
37   * '0000' Flush Packet (flush-pkt) - indicates the end of a message
38   * '0001' Delimiter Packet (delim-pkt) - separates sections of a message
39
40  Capability Advertisement
41 --------------------------
42
43 A server which decides to communicate (based on a request from a client)
44 using protocol version 2, notifies the client by sending a version string
45 in its initial response followed by an advertisement of its capabilities.
46 Each capability is a key with an optional value.  Clients must ignore all
47 unknown keys.  Semantics of unknown values are left to the definition of
48 each key.  Some capabilities will describe commands which can be requested
49 to be executed by the client.
50
51     capability-advertisement = protocol-version
52                                capability-list
53                                flush-pkt
54
55     protocol-version = PKT-LINE("version 2" LF)
56     capability-list = *capability
57     capability = PKT-LINE(key[=value] LF)
58
59     key = 1*CHAR
60     value = 1*CHAR
61     CHAR = 1*(ALPHA / DIGIT / "-" / "_")
62
63 A client then responds to select the command it wants with any particular
64 capabilities or arguments.  There is then an optional section where the
65 client can provide any command specific parameters or queries.
66
67     command-request = command
68                       capability-list
69                       (command-args)
70                       flush-pkt
71     command = PKT-LINE("command=" key LF)
72     command-args = delim-pkt
73                    *arg
74     arg = 1*CHAR
75
76 The server will then check to ensure that the client's request is
77 comprised of a valid command as well as valid capabilities which were
78 advertised.  If the request is valid the server will then execute the
79 command.
80
81 When a command has finished a client can either request that another
82 command be executed or can terminate the connection by sending an empty
83 request consisting of just a flush-pkt.
84
85  Capabilities
86 ~~~~~~~~~~~~~~
87
88 There are two different types of capabilities: normal capabilities,
89 which can be used to to convey information or alter the behavior of a
90 request, and command capabilities, which are the core actions that a
91 client wants to perform (fetch, push, etc).
92
93 All commands must only last a single round and be stateless from the
94 perspective of the server side.  All state MUST be retained and managed
95 by the client process.  This permits simple round-robin load-balancing
96 on the server side, without needing to worry about state management.
97
98 Clients MUST NOT require state management on the server side in order to
99 function correctly.
100
101  agent
102 -------
103
104 The server can advertise the `agent` capability with a value `X` (in the
105 form `agent=X`) to notify the client that the server is running version
106 `X`.  The client may optionally send its own agent string by including
107 the `agent` capability with a value `Y` (in the form `agent=Y`) in its
108 request to the server (but it MUST NOT do so if the server did not
109 advertise the agent capability). The `X` and `Y` strings may contain any
110 printable ASCII characters except space (i.e., the byte range 32 < x <
111 127), and are typically of the form "package/version" (e.g.,
112 "git/1.8.3.1"). The agent strings are purely informative for statistics
113 and debugging purposes, and MUST NOT be used to programmatically assume
114 the presence or absence of particular features.
115
116  ls-refs
117 ---------
118
119 `ls-refs` is the command used to request a reference advertisement in v2.
120 Unlike the current reference advertisement, ls-refs takes in parameters
121 which can be used to limit the refs sent from the server.
122
123 Additional features not supported in the base command will be advertised
124 as the value of the command in the capability advertisement in the form
125 of a space separated list of features, e.g.  "<command>=<feature 1>
126 <feature 2>".
127
128 ls-refs takes in the following parameters wrapped in packet-lines:
129
130     symrefs
131         In addition to the object pointed by it, show the underlying ref
132         pointed by it when showing a symbolic ref.
133     peel
134         Show peeled tags.
135     ref-pattern <pattern>
136         When specified, only references matching the one of the provided
137         patterns are displayed.
138
139 The output of ls-refs is as follows:
140
141     output = *ref
142              flush-pkt
143     ref = PKT-LINE(obj-id SP refname *(SP ref-attribute) LF)
144     ref-attribute = (symref | peeled)
145     symref = "symref-target:" symref-target
146     peeled = "peeled:" obj-id
147
148  fetch
149 -------
150
151 `fetch` is the command used to fetch a packfile in v2.  It can be looked
152 at as a modified version of the v1 fetch where the ref-advertisement is
153 stripped out (since the `ls-refs` command fills that role) and the
154 message format is tweaked to eliminate redundancies and permit easy
155 addition of future extensions.
156
157 Additional features not supported in the base command will be advertised
158 as the value of the command in the capability advertisement in the form
159 of a space separated list of features, e.g.  "<command>=<feature 1>
160 <feature 2>".
161
162 A `fetch` request can take the following parameters wrapped in
163 packet-lines:
164
165     want <oid>
166         Indicates to the server an object which the client wants to
167         retrieve.
168
169     have <oid>
170         Indicates to the server an object which the client has locally.
171         This allows the server to make a packfile which only contains
172         the objects that the client needs. Multiple 'have' lines can be
173         supplied.
174
175     done
176         Indicates to the server that negotiation should terminate (or
177         not even begin if performing a clone) and that the server should
178         use the information supplied in the request to construct the
179         packfile.
180
181     thin-pack
182         Request that a thin pack be sent, which is a pack with deltas
183         which reference base objects not contained within the pack (but
184         are known to exist at the receiving end). This can reduce the
185         network traffic significantly, but it requires the receiving end
186         to know how to "thicken" these packs by adding the missing bases
187         to the pack.
188
189     no-progress
190         Request that progress information that would normally be sent on
191         side-band channel 2, during the packfile transfer, should not be
192         sent.  However, the side-band channel 3 is still used for error
193         responses.
194
195     include-tag
196         Request that annotated tags should be sent if the objects they
197         point to are being sent.
198
199     ofs-delta
200         Indicate that the client understands PACKv2 with delta referring
201         to its base by position in pack rather than by an oid.  That is,
202         they can read OBJ_OFS_DELTA (ake type 6) in a packfile.
203
204     shallow <oid>
205         A client must notify the server of all objects for which it only
206         has shallow copies of (meaning that it doesn't have the parents
207         of a commit) by supplying a 'shallow <oid>' line for each such
208         object so that the serve is aware of the limitations of the
209         client's history.
210
211     deepen <depth>
212         Request that the fetch/clone should be shallow having a commit depth of
213         <depth> relative to the remote side.
214
215     deepen-relative
216         Requests that the semantics of the "deepen" command be changed
217         to indicate that the depth requested is relative to the clients
218         current shallow boundary, instead of relative to the remote
219         refs.
220
221     deepen-since <timestamp>
222         Requests that the shallow clone/fetch should be cut at a
223         specific time, instead of depth.  Internally it's equivalent of
224         doing "rev-list --max-age=<timestamp>". Cannot be used with
225         "deepen".
226
227     deepen-not <rev>
228         Requests that the shallow clone/fetch should be cut at a
229         specific revision specified by '<rev>', instead of a depth.
230         Internally it's equivalent of doing "rev-list --not <rev>".
231         Cannot be used with "deepen", but can be used with
232         "deepen-since".
233
234 The response of `fetch` is broken into a number of sections separated by
235 delimiter packets (0001), with each section beginning with its section
236 header.
237
238     output = *section
239     section = (acknowledgments | shallow-info | packfile)
240               (flush-pkt | delim-pkt)
241
242     acknowledgments = PKT-LINE("acknowledgments" LF)
243                       *(ready | nak | ack)
244     ready = PKT-LINE("ready" LF)
245     nak = PKT-LINE("NAK" LF)
246     ack = PKT-LINE("ACK" SP obj-id LF)
247
248     shallow-info = PKT-LINE("shallow-info" LF)
249                    *PKT-LINE((shallow | unshallow) LF)
250     shallow = "shallow" SP obj-id
251     unshallow = "unshallow" SP obj-id
252
253     packfile = PKT-LINE("packfile" LF)
254                [PACKFILE]
255
256 ----
257     acknowledgments section
258         * Always begins with the section header "acknowledgments"
259
260         * The server will respond with "NAK" if none of the object ids sent
261           as have lines were common.
262
263         * The server will respond with "ACK obj-id" for all of the
264           object ids sent as have lines which are common.
265
266         * A response cannot have both "ACK" lines as well as a "NAK"
267           line.
268
269         * The server will respond with a "ready" line indicating that
270           the server has found an acceptable common base and is ready to
271           make and send a packfile (which will be found in the packfile
272           section of the same response)
273
274         * If the client determines that it is finished with negotiations
275           by sending a "done" line, the acknowledgments sections can be
276           omitted from the server's response as an optimization.
277
278         * If the server has found a suitable cut point and has decided
279           to send a "ready" line, then the server can decide to (as an
280           optimization) omit any "ACK" lines it would have sent during
281           its response.  This is because the server will have already
282           determined the objects it plans to send to the client and no
283           further negotiation is needed.
284
285 ----
286     shallow-info section
287         If the client has requested a shallow fetch/clone, a shallow
288         client requests a fetch or the server is shallow then the
289         server's response may include a shallow-info section.  The
290         shallow-info section will be include if (due to one of the above
291         conditions) the server needs to inform the client of any shallow
292         boundaries or adjustments to the clients already existing
293         shallow boundaries.
294
295         * Always begins with the section header "shallow-info"
296
297         * If a positive depth is requested, the server will compute the
298           set of commits which are no deeper than the desired depth.
299
300         * The server sends a "shallow obj-id" line for each commit whose
301           parents will not be sent in the following packfile.
302
303         * The server sends an "unshallow obj-id" line for each commit
304           which the client has indicated is shallow, but is no longer
305           shallow as a result of the fetch (due to its parents being
306           sent in the following packfile).
307
308         * The server MUST NOT send any "unshallow" lines for anything
309           which the client has not indicated was shallow as a part of
310           its request.
311
312         * This section is only included if a packfile section is also
313           included in the response.
314
315 ----
316     packfile section
317         * Always begins with the section header "packfile"
318
319         * The transmission of the packfile begins immediately after the
320           section header
321
322         * The data transfer of the packfile is always multiplexed, using
323           the same semantics of the 'side-band-64k' capability from
324           protocol version 1.  This means that each packet, during the
325           packfile data stream, is made up of a leading 4-byte pkt-line
326           length (typical of the pkt-line format), followed by a 1-byte
327           stream code, followed by the actual data.
328
329           The stream code can be one of:
330                 1 - pack data
331                 2 - progress messages
332                 3 - fatal error message just before stream aborts
333
334         * This section is only included if the client has sent 'want'
335           lines in its request and either requested that no more
336           negotiation be done by sending 'done' or if the server has
337           decided it has found a sufficient cut point to produce a
338           packfile.