674 lines · 25.6 KB · Text
RawDownload
Packfile transfer protocols=========================== Git supports transferring data in packfiles over the ssh://, git://, http:// andfile:// transports.  There exist two sets of protocols, one for pushingdata from a client to a server and another for fetching data from aserver to a client.  The three transports (ssh, git, file) use the sameprotocol to transfer data. http is documented in http-protocol.txt. The processes invoked in the canonical Git implementation are 'upload-pack'on the server side and 'fetch-pack' on the client side for fetching data;then 'receive-pack' on the server and 'send-pack' on the client for pushingdata.  The protocol functions to have a server tell a client what iscurrently on the server, then for the two to negotiate the smallest amountof data to send in order to fully update one or the other. pkt-line Format--------------- The descriptions below build on the pkt-line format described inprotocol-common.txt. When the grammar indicate `PKT-LINE(...)`, unlessotherwise noted the usual pkt-line LF rules apply: the sender SHOULDinclude a LF, but the receiver MUST NOT complain if it is not present. An error packet is a special pkt-line that contains an error string. ----  error-line     =  PKT-LINE("ERR" SP explanation-text)---- Throughout the protocol, where `PKT-LINE(...)` is expected, an error packet MAYbe sent. Once this packet is sent by a client or a server, the data transferprocess defined in this protocol is terminated. Transports----------There are three transports over which the packfile protocol isinitiated.  The Git transport is a simple, unauthenticated server thattakes the command (almost always 'upload-pack', though Gitservers can be configured to be globally writable, in which 'receive-pack' initiation is also allowed) with which the client wishes tocommunicate and executes it and connects it to the requestingprocess. In the SSH transport, the client just runs the 'upload-pack'or 'receive-pack' process on the server over the SSH protocol and thencommunicates with that invoked process over the SSH connection. The file:// transport runs the 'upload-pack' or 'receive-pack'process locally and communicates with it over a pipe. Extra Parameters---------------- The protocol provides a mechanism in which clients can send additionalinformation in its first message to the server. These are called "ExtraParameters", and are supported by the Git, SSH, and HTTP protocols. Each Extra Parameter takes the form of `<key>=<value>` or `<key>`. Servers that receive any such Extra Parameters MUST ignore allunrecognized keys. Currently, the only Extra Parameter recognized is"version" with a value of '1' or '2'.  See protocol-v2.txt for moreinformation on protocol version 2. Git Transport------------- The Git transport starts off by sending the command and repositoryon the wire using the pkt-line format, followed by a NUL byte and ahostname parameter, terminated by a NUL byte.    0033git-upload-pack /project.git\0host=myserver.com\0 The transport may send Extra Parameters by adding an additional NULbyte, and then adding one or more NUL-terminated strings:    003egit-upload-pack /project.git\0host=myserver.com\0\0version=1\0 --   git-proto-request = request-command SP pathname NUL		       [ host-parameter NUL ] [ NUL extra-parameters ]   request-command   = "git-upload-pack" / "git-receive-pack" /		       "git-upload-archive"   ; case sensitive   pathname          = *( %x01-ff ) ; exclude NUL   host-parameter    = "host=" hostname [ ":" port ]   extra-parameters  = 1*extra-parameter   extra-parameter   = 1*( %x01-ff ) NUL-- host-parameter is used for thegit-daemon name based virtual hosting.  See --interpolated-pathoption to git daemon, with the %H/%CH format characters. Basically what the Git client is doing to connect to an 'upload-pack'process on the server side over the Git protocol is this:    $ echo -e -n \     "003agit-upload-pack /schacon/gitbook.git\0host=example.com\0" |     nc -v example.com 9418  SSH Transport------------- Initiating the upload-pack or receive-pack processes over SSH isexecuting the binary on the server via SSH remote execution.It is basically equivalent to running this:    $ ssh git.example.com "git-upload-pack '/project.git'" For a server to support Git pushing and pulling for a given user overSSH, that user needs to be able to execute one or both of thosecommands via the SSH shell that they are provided on login.  On somesystems, that shell access is limited to only being able to run thosetwo commands, or even just one of them. In an ssh:// format URI, it's absolute in the URI, so the '/' afterthe host name (or port number) is sent as an argument, which is thenread by the remote git-upload-pack exactly as is, so it's effectivelyan absolute path in the remote filesystem.        git clone ssh://user@example.com/project.git		    |		    v    ssh user@example.com "git-upload-pack '/project.git'" In a "user@host:path" format URI, its relative to the user's homedirectory, because the Git client will run:      git clone user@example.com:project.git		    |		    v  ssh user@example.com "git-upload-pack 'project.git'" The exception is if a '~' is used, in which casewe execute it without the leading '/'.       ssh://user@example.com/~alice/project.git,		     |		     v   ssh user@example.com "git-upload-pack '~alice/project.git'" Depending on the value of the `protocol.version` configuration variable,Git may attempt to send Extra Parameters as a colon-separated string inthe GIT_PROTOCOL environment variable. This is done only ifthe `ssh.variant` configuration variable indicates that the ssh commandsupports passing environment variables as an argument. A few things to remember here: - The "command name" is spelled with dash (e.g. git-upload-pack), but  this can be overridden by the client; - The repository path is always quoted with single quotes. Fetching Data From a Server--------------------------- When one Git repository wants to get data that a second repositoryhas, the first can 'fetch' from the second.  This operation determineswhat data the server has that the client does not then streams thatdata down to the client in packfile format.  Reference Discovery------------------- When the client initially connects the server will immediately respondwith a version number (if "version=1" is sent as an Extra Parameter),and a listing of each reference it has (all branches and tags) alongwith the object name that each reference currently points to.    $ echo -e -n "0045git-upload-pack /schacon/gitbook.git\0host=example.com\0\0version=1\0" |      nc -v example.com 9418   000eversion 1   00887217a7c7e582c46cec22a130adf4b9d7d950fba0 HEAD\0multi_ack thin-pack		side-band side-band-64k ofs-delta shallow no-progress include-tag   00441d3fcd5ced445d1abc402225c0b8a1299641f497 refs/heads/integration   003f7217a7c7e582c46cec22a130adf4b9d7d950fba0 refs/heads/master   003cb88d2441cac0977faf98efc80305012112238d9d refs/tags/v0.9   003c525128480b96c89e6418b1e40909bf6c5b2d580f refs/tags/v1.0   003fe92df48743b7bc7d26bcaabfddde0a1e20cae47c refs/tags/v1.0^{}   0000 The returned response is a pkt-line stream describing each ref andits current value.  The stream MUST be sorted by name according tothe C locale ordering. If HEAD is a valid ref, HEAD MUST appear as the first advertisedref.  If HEAD is not a valid ref, HEAD MUST NOT appear in theadvertisement list at all, but other refs may still appear. The stream MUST include capability declarations behind a NUL on thefirst ref. The peeled value of a ref (that is "ref^{}") MUST beimmediately after the ref itself, if presented. A conforming serverMUST peel the ref if it's an annotated tag. ----  advertised-refs  =  *1("version 1")		      (no-refs / list-of-refs)		      *shallow		      flush-pkt   no-refs          =  PKT-LINE(zero-id SP "capabilities^{}"		      NUL capability-list)   list-of-refs     =  first-ref *other-ref  first-ref        =  PKT-LINE(obj-id SP refname		      NUL capability-list)   other-ref        =  PKT-LINE(other-tip / other-peeled)  other-tip        =  obj-id SP refname  other-peeled     =  obj-id SP refname "^{}"   shallow          =  PKT-LINE("shallow" SP obj-id)   capability-list  =  capability *(SP capability)  capability       =  1*(LC_ALPHA / DIGIT / "-" / "_")  LC_ALPHA         =  %x61-7A---- Server and client MUST use lowercase for obj-id, both MUST treat obj-idas case-insensitive. See protocol-capabilities.txt for a list of allowed server capabilitiesand descriptions. Packfile Negotiation--------------------After reference and capabilities discovery, the client can decide toterminate the connection by sending a flush-pkt, telling the server it cannow gracefully terminate, and disconnect, when it does not need any packdata. This can happen with the ls-remote command, and also can happen whenthe client already is up to date. Otherwise, it enters the negotiation phase, where the client andserver determine what the minimal packfile necessary for transport is,by telling the server what objects it wants, its shallow objects(if any), and the maximum commit depth it wants (if any).  The clientwill also send a list of the capabilities it wants to be in effect,out of what the server said it could do with the first 'want' line. ----  upload-request    =  want-list		       *shallow-line		       *1depth-request		       [filter-request]		       flush-pkt   want-list         =  first-want		       *additional-want   shallow-line      =  PKT-LINE("shallow" SP obj-id)   depth-request     =  PKT-LINE("deepen" SP depth) /		       PKT-LINE("deepen-since" SP timestamp) /		       PKT-LINE("deepen-not" SP ref)   first-want        =  PKT-LINE("want" SP obj-id SP capability-list)  additional-want   =  PKT-LINE("want" SP obj-id)   depth             =  1*DIGIT   filter-request    =  PKT-LINE("filter" SP filter-spec)---- Clients MUST send all the obj-ids it wants from the referencediscovery phase as 'want' lines. Clients MUST send at least one'want' command in the request body. Clients MUST NOT mention anobj-id in a 'want' command which did not appear in the responseobtained through ref discovery. The client MUST write all obj-ids which it only has shallow copiesof (meaning that it does not have the parents of a commit) as'shallow' lines so that the server is aware of the limitations ofthe client's history. The client now sends the maximum commit history depth it wants forthis transaction, which is the number of commits it wants from thetip of the history, if any, as a 'deepen' line.  A depth of 0 is thesame as not making a depth request. The client does not want to receiveany commits beyond this depth, nor does it want objects needed only tocomplete those commits. Commits whose parents are not received as aresult are defined as shallow and marked as such in the server. Thisinformation is sent back to the client in the next step. The client can optionally request that pack-objects omit variousobjects from the packfile using one of several filtering techniques.These are intended for use with partial clone and partial fetchoperations. An object that does not meet a filter-spec value isomitted unless explicitly requested in a 'want' line. See `rev-list`for possible filter-spec values. Once all the 'want's and 'shallow's (and optional 'deepen') aretransferred, clients MUST send a flush-pkt, to tell the server sidethat it is done sending the list. Otherwise, if the client sent a positive depth request, the serverwill determine which commits will and will not be shallow andsend this information to the client. If the client did not requesta positive depth, this step is skipped. ----  shallow-update   =  *shallow-line		      *unshallow-line		      flush-pkt   shallow-line     =  PKT-LINE("shallow" SP obj-id)   unshallow-line   =  PKT-LINE("unshallow" SP obj-id)---- If the client has requested a positive depth, the server will computethe set of commits which are no deeper than the desired depth. The setof commits start at the client's wants. The server writes 'shallow' lines for eachcommit whose parents will not be sent as a result. The server writesan 'unshallow' line for each commit which the client has indicated isshallow, but is no longer shallow at the currently requested depth(that is, its parents will now be sent). The server MUST NOT markas unshallow anything which the client has not indicated was shallow. Now the client will send a list of the obj-ids it has using 'have'lines, so the server can make a packfile that only contains the objectsthat the client needs. In multi_ack mode, the canonical implementationwill send up to 32 of these at a time, then will send a flush-pkt. Thecanonical implementation will skip ahead and send the next 32 immediately,so that there is always a block of 32 "in-flight on the wire" at a time. ----  upload-haves      =  have-list		       compute-end   have-list         =  *have-line  have-line         =  PKT-LINE("have" SP obj-id)  compute-end       =  flush-pkt / PKT-LINE("done")---- If the server reads 'have' lines, it then will respond by ACKing anyof the obj-ids the client said it had that the server also has. Theserver will ACK obj-ids differently depending on which ack mode ischosen by the client. In multi_ack mode:   * the server will respond with 'ACK obj-id continue' for any common    commits.   * once the server has found an acceptable common base commit and is    ready to make a packfile, it will blindly ACK all 'have' obj-ids    back to the client.   * the server will then send a 'NAK' and then wait for another response    from the client - either a 'done' or another list of 'have' lines. In multi_ack_detailed mode:   * the server will differentiate the ACKs where it is signaling    that it is ready to send data with 'ACK obj-id ready' lines, and    signals the identified common commits with 'ACK obj-id common' lines. Without either multi_ack or multi_ack_detailed:  * upload-pack sends "ACK obj-id" on the first common object it finds.   After that it says nothing until the client gives it a "done".  * upload-pack sends "NAK" on a flush-pkt if no common object   has been found yet.  If one has been found, and thus an ACK   was already sent, it's silent on the flush-pkt. After the client has gotten enough ACK responses that it can determinethat the server has enough information to send an efficient packfile(in the canonical implementation, this is determined when it has receivedenough ACKs that it can color everything left in the --date-order queueas common with the server, or the --date-order queue is empty), or theclient determines that it wants to give up (in the canonical implementation,this is determined when the client sends 256 'have' lines without gettingany of them ACKed by the server - meaning there is nothing in common andthe server should just send all of its objects), then the client will senda 'done' command.  The 'done' command signals to the server that the clientis ready to receive its packfile data. However, the 256 limit *only* turns on in the canonical clientimplementation if we have received at least one "ACK %s continue"during a prior round.  This helps to ensure that at least one commonancestor is found before we give up entirely. Once the 'done' line is read from the client, the server will eithersend a final 'ACK obj-id' or it will send a 'NAK'. 'obj-id' is the objectname of the last commit determined to be common. The server only sendsACK after 'done' if there is at least one common base and multi_ack ormulti_ack_detailed is enabled. The server always sends NAK after 'done'if there is no common base found. Instead of 'ACK' or 'NAK', the server may send an error message (forexample, if it does not recognize an object in a 'want' line receivedfrom the client). Then the server will start sending its packfile data. ----  server-response = *ack_multi ack / nak  ack_multi       = PKT-LINE("ACK" SP obj-id ack_status)  ack_status      = "continue" / "common" / "ready"  ack             = PKT-LINE("ACK" SP obj-id)  nak             = PKT-LINE("NAK")---- A simple clone may look like this (with no 'have' lines): ----   C: 0054want 74730d410fcb6603ace96f1dc55ea6196122532d multi_ack \     side-band-64k ofs-delta\n   C: 0032want 7d1665144a3a975c05f1f43902ddaf084e784dbe\n   C: 0032want 5a3f6be755bbb7deae50065988cbfa1ffa9ab68a\n   C: 0032want 7e47fe2bd8d01d481f44d7af0531bd93d3b21c01\n   C: 0032want 74730d410fcb6603ace96f1dc55ea6196122532d\n   C: 0000   C: 0009done\n    S: 0008NAK\n   S: [PACKFILE]---- An incremental update (fetch) response might look like this: ----   C: 0054want 74730d410fcb6603ace96f1dc55ea6196122532d multi_ack \     side-band-64k ofs-delta\n   C: 0032want 7d1665144a3a975c05f1f43902ddaf084e784dbe\n   C: 0032want 5a3f6be755bbb7deae50065988cbfa1ffa9ab68a\n   C: 0000   C: 0032have 7e47fe2bd8d01d481f44d7af0531bd93d3b21c01\n   C: [30 more have lines]   C: 0032have 74730d410fcb6603ace96f1dc55ea6196122532d\n   C: 0000    S: 003aACK 7e47fe2bd8d01d481f44d7af0531bd93d3b21c01 continue\n   S: 003aACK 74730d410fcb6603ace96f1dc55ea6196122532d continue\n   S: 0008NAK\n    C: 0009done\n    S: 0031ACK 74730d410fcb6603ace96f1dc55ea6196122532d\n   S: [PACKFILE]----  Packfile Data------------- Now that the client and server have finished negotiation about whatthe minimal amount of data that needs to be sent to the client is, the serverwill construct and send the required data in packfile format. See pack-format.txt for what the packfile itself actually looks like. If 'side-band' or 'side-band-64k' capabilities have been specified bythe client, the server will send the packfile data multiplexed. Each packet starting with the packet-line length of the amount of datathat follows, followed by a single byte specifying the sideband thefollowing data is coming in on. In 'side-band' mode, it will send up to 999 data bytes plus 1 controlcode, for a total of up to 1000 bytes in a pkt-line.  In 'side-band-64k'mode it will send up to 65519 data bytes plus 1 control code, for atotal of up to 65520 bytes in a pkt-line. The sideband byte will be a '1', '2' or a '3'. Sideband '1' will containpackfile data, sideband '2' will be used for progress information that theclient will generally print to stderr and sideband '3' is used for errorinformation. If no 'side-band' capability was specified, the server will stream theentire packfile without multiplexing.  Pushing Data To a Server------------------------ Pushing data to a server will invoke the 'receive-pack' process on theserver, which will allow the client to tell it which references it shouldupdate and then send all the data the server will need for those newreferences to be complete.  Once all the data is received and validated,the server will then update its references to what the client specified. Authentication-------------- The protocol itself contains no authentication mechanisms.  That is to behandled by the transport, such as SSH, before the 'receive-pack' process isinvoked.  If 'receive-pack' is configured over the Git transport, thoserepositories will be writable by anyone who can access that port (9418) asthat transport is unauthenticated. Reference Discovery-------------------

Lines 1–500 of 674.