git.pack

index · git

Overview

Packfile generation on top of the git core reader: walk the object graph from a set of wanted refs and serialize a version 2 packfile.

The walk reads commits, trees and tags. A tree entry names a blob and its type, so a blob is never opened on the walk.

A fetch names the commits the client holds. The walk then leaves out what they reach, the way git rev-list --objects <wants> --not <common> does. It finds the commits the client lacks newest first. It stops once every commit left in its queue is one the client holds, and older than each commit it took as one the client lacks. The trees of the commits the client holds just below the new ones are read whole, and every tree and blob in them stays out of the pack. A one-file change then sends the commit, the trees on the file's path and the blob.

The pack copies what the repository's packs already hold. A packed plain entry goes out as its stored zlib stream. A packed delta goes out as a REF_DELTA on its base's id, the stored stream unchanged, when its base went out before it. The records go out in the order the packs hold them, loose objects first, and a pack holds a delta's base before the delta. A delta that only ever points back cannot form a cycle, whatever two packs hold. Every other object is read whole and deflated at level 6: a loose object, and a delta whose base stays out. No ofs-delta is written, so the client needs no capability. Copied bytes are not checked: the client's index-pack hashes every object it takes.

pack_stream hands the pack out a record at a time: the header, each entry, then the SHA-1 trailer. It holds one record and the list of records in memory, never the pack. build_pack joins the pieces.

The walk and the stream take an optional tick, a function called once every 32 objects they read or locate. A caller on an event loop gives the loop its turn there.

Functions

NameSignature
collect_objectscollect_objects(git_dir, wants, common, tick) -> objects, err
pack_streampack_stream(git_dir, objects, tick) -> stream, err
build_packbuild_pack(git_dir, objects) -> pack, err

collect_objects(git_dir, wants, common, tick) -> objects, err

Collect the objects reachable from the wanted ids that the common ids do not reach

Walks commits (parents + trees), trees (entries, skipping gitlinks), and annotated tags. Returns an array of { sha = hex, type = str }. A blob named by a tree, or by a tag's type line, is listed unread: build_pack finds a missing one.

common lists ids the client holds, each one present in the repository. The walk leaves out every commit they reach, and every tree and blob in the boundary: the commits the client holds that are parents of the commits it lacks. A common tag counts as the object it names, a common tree as everything in it. With no common, everything the wants reach is listed.

tick, when given, is called once every 32 objects the walk reads.

pack_stream(git_dir, objects, tick) -> stream, err

Open a version 2 packfile of the given objects, to be read a record at a time

Takes the records collect_objects returns, each id once, and locates every object before it returns: a missing one fails here, with no byte handed out. tick, when given, is called once every 32 objects it locates.

stream:next() returns the next piece of the pack: the 12-byte header, then one entry per call, then the 20-byte SHA-1 trailer, then nil. The entries go out in the order the repository holds them: loose objects first, then each pack's by offset. A packed entry's stored stream is copied, and a packed delta as a REF_DELTA when its base went out before it. Every other object is read and deflated. A fault on the way, such as an object gone since the stream opened, returns nil and the reason, at that call and every later one.

build_pack(git_dir, objects) -> pack, err

Build a version 2 packfile containing the given objects

Joins the pieces of pack_stream over the same objects into one string.