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