fetch-pack: perform a fetch using v2
[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 The response of `fetch` is broken into a number of sections separated by
205 delimiter packets (0001), with each section beginning with its section
206 header.
207
208     output = *section
209     section = (acknowledgments | packfile)
210               (flush-pkt | delim-pkt)
211
212     acknowledgments = PKT-LINE("acknowledgments" LF)
213                       *(ready | nak | ack)
214     ready = PKT-LINE("ready" LF)
215     nak = PKT-LINE("NAK" LF)
216     ack = PKT-LINE("ACK" SP obj-id LF)
217
218     packfile = PKT-LINE("packfile" LF)
219                [PACKFILE]
220
221 ----
222     acknowledgments section
223         * Always begins with the section header "acknowledgments"
224
225         * The server will respond with "NAK" if none of the object ids sent
226           as have lines were common.
227
228         * The server will respond with "ACK obj-id" for all of the
229           object ids sent as have lines which are common.
230
231         * A response cannot have both "ACK" lines as well as a "NAK"
232           line.
233
234         * The server will respond with a "ready" line indicating that
235           the server has found an acceptable common base and is ready to
236           make and send a packfile (which will be found in the packfile
237           section of the same response)
238
239         * If the client determines that it is finished with negotiations
240           by sending a "done" line, the acknowledgments sections can be
241           omitted from the server's response as an optimization.
242
243         * If the server has found a suitable cut point and has decided
244           to send a "ready" line, then the server can decide to (as an
245           optimization) omit any "ACK" lines it would have sent during
246           its response.  This is because the server will have already
247           determined the objects it plans to send to the client and no
248           further negotiation is needed.
249
250 ----
251     packfile section
252         * Always begins with the section header "packfile"
253
254         * The transmission of the packfile begins immediately after the
255           section header
256
257         * The data transfer of the packfile is always multiplexed, using
258           the same semantics of the 'side-band-64k' capability from
259           protocol version 1.  This means that each packet, during the
260           packfile data stream, is made up of a leading 4-byte pkt-line
261           length (typical of the pkt-line format), followed by a 1-byte
262           stream code, followed by the actual data.
263
264           The stream code can be one of:
265                 1 - pack data
266                 2 - progress messages
267                 3 - fatal error message just before stream aborts
268
269         * This section is only included if the client has sent 'want'
270           lines in its request and either requested that no more
271           negotiation be done by sending 'done' or if the server has
272           decided it has found a sufficient cut point to produce a
273           packfile.