http.git

index · http

Overview

Read-only git smart-HTTP frontend (protocol v0, git-upload-pack only).

Serves git clone and git fetch over HTTP on top of the git core reader and git.pack packfile generation. Provides serve which builds a handler function for http.server, plus the underlying protocol pieces (info_refs, upload_pack_stream, upload_pack, pkt-line helpers) for direct use and testing.

A fetch negotiates the way git upload-pack does over stateless RPC. The server advertises multi_ack_detailed and no-done. Each POST carries the wants, then the client's have lines. A have counts as common when the repository holds the object, reachable or not, as git's own server rules. The answer is ACK <id> common for each common have. Once every want reaches a common commit, it is ACK <id> ready, and NAK closes the round. A client that asked for no-done gets a final ACK <id> and the pack in the same answer. Any other client sends one more POST that ends in done. Its answer is the final ACK, or NAK when nothing was common, then the pack. The pack holds what the wants reach and the common haves do not (see git.pack). A have the repository does not hold is ignored.

Plain ACK mode, git's default without multi_ack, is answered for a client that asks for neither multi_ack mode, but it is not advertised. git's own client cannot use it over HTTP: it drops the ACKed have from the POST that carries done, so the server sees no have at all.

Shallow (--depth) and partial (--filter) requests are rejected with an ERR pkt. thin-pack is not offered: a delta whose base the client holds goes out whole.

The pack copies the stored streams and deltas of the repository's packs, and deflates loose objects (see git.pack). serve streams it as it is built: the walk runs first, giving the event loop a turn every 32 objects, then each side-band pkt goes out once its bytes are ready. No more than one pkt and one pack record are held at a time. No progress goes out on band 2.

Functions

NameSignature
encode_pktencode_pkt(data) -> pkt
decode_pktsdecode_pkts(body) -> pkts, err
info_refsinfo_refs(git_dir) -> body
parse_upload_pack_requestparse_upload_pack_request(body) -> req, err
upload_pack_streamupload_pack_stream(git_dir, body, tick) -> response
upload_packupload_pack(git_dir, body) -> response
serveserve(repos) -> handle, err

encode_pkt(data) -> pkt

Encode a pkt-line

decode_pkts(body) -> pkts, err

Decode a pkt-line stream into an array of { kind = "line"|"flush", data } entries

info_refs(git_dir) -> body

Build the info/refs smart advertisement for service=git-upload-pack

parse_upload_pack_request(body) -> req, err

Parse an upload-pack request body (want/have/done pkt-lines)

Returns { wants = {sha, ...}, haves = {sha, ...}, done = bool, side_band = bool, side_band_64k = bool, multi_ack_detailed = bool, no_done = bool }, the haves in the order sent. The flags come from the capabilities on the want lines. Shallow and filter requests yield an error.

upload_pack_stream(git_dir, body, tick) -> response

Answer an upload-pack POST body, the pack as a producer for http.server

Stateless-RPC negotiation, as the module header lays it out. A round without done gets its ACK lines and NAK, and the pack too when the client asked for no-done and the server is ready. A round with done gets its ACK lines, the final ACK or NAK, then the pack of what the wants reach and the common haves do not.

A round with no pack, a malformed request, and a walk that fails come back as a string: errors as ERR pkts, or a band-3 message with side-band. A round with a pack comes back as a producer, the table http.server streams (see http.server.new). Its first piece holds the ACK and NAK pkts. Then each piece is one side-band pkt of 65,515 bytes of pack (995 under side-band), or 64 KB or more of raw pack without side-band, then the flush with side-band.

The walk runs before this returns. tick, when given, is called every 32 objects the walk reads, and every 32 the pack locates; serve passes one that lets the event loop run. A fault after the first piece, such as an object gone mid-pack, ends a side-band answer with the band-3 message and a flush. A raw answer has no channel for it: next raises.

upload_pack(git_dir, body) -> response

Answer an upload-pack POST body with an upload-pack response

The whole answer of upload_pack_stream as one string: its pieces joined. A fault mid-pack ends the string as the stream ends with side-band. Without side-band, the pieces sent are followed by an ERR pkt.

serve(repos) -> handle, err

Build an http.server handler serving the given repositories read-only

repos maps URL prefixes to repository paths, e.g. { ["/lilush"] = "/srv/git/lilush.git" }. Each path must be a git directory (bare repository) or a worktree containing .git. URLs may carry an optional .git suffix (clone http://host/lilush.git works).

The returned function matches the http.server handler contract: handle(method, query, args, headers, body, ctx). An upload-pack answer that carries a pack comes back as a producer (see upload_pack_stream), and its walk gives the loop a turn every 32 objects.