NAME
gitformat-pack - Git pack formatSYNOPSIS
$GIT_DIR/objects/pack/pack- .{pack,idx} $GIT_DIR/objects/pack/pack-.rev $GIT_DIR/objects/pack/pack-*.mtimes $GIT_DIR/objects/pack/multi-pack-index
DESCRIPTION
The Git pack format is now Git stores most of its primary repository data. Over the lietime af a repository loose objects (if any) and smaller packs are consolidated into larger pack(s). See git-gc(1) and git-pack-objects(1).CHECKSUMS AND OBJECT IDS
In a repository using the traditional SHA-1, pack checksums, index checksums, and object IDs (object names) mentioned below are all computed using SHA-1. Similarly, in SHA-256 repositories, these values are computed using SHA-256.PACK-*.PACK FILES HAVE THE FOLLOWING FORMAT:
•A header appears at the beginning and
consists of the following:
4-byte signature: The signature is: {'P', 'A', 'C', 'K'}
4-byte version number (network byte order): Git currently accepts version number 2 or 3 but generates version 2 only.
4-byte number of objects contained in the pack (network byte order)
Observation: we cannot have more than 4G versions ;-) and more than 4G objects in a pack.
•The header is followed by number of
object entries, each of which looks like this:
(undeltified representation) n-byte type and length (3-bit type, (n-1)*7+4-bit length) compressed data
(deltified representation) n-byte type and length (3-bit type, (n-1)*7+4-bit length) base object name if OBJ_REF_DELTA or a negative relative offset from the delta object's position in the pack if this is an OBJ_OFS_DELTA object compressed delta data
Observation: length of each object is encoded in a variable length format and is not constrained to 32-bit or anything.
•The trailer records a pack checksum of
all of the above.
Object types
Valid object types are:•OBJ_COMMIT (1)
•OBJ_TREE (2)
•OBJ_BLOB (3)
•OBJ_TAG (4)
•OBJ_OFS_DELTA (6)
•OBJ_REF_DELTA (7)
Size encoding
This document uses the following "size encoding" of non-negative integers: From each byte, the seven least significant bits are used to form the resulting integer. As long as the most significant bit is 1, this process continues; the byte with MSB 0 provides the last seven bits. The seven-bit chunks are concatenated. Later values are more significant.Deltified representation
Conceptually there are only four object types: commit, tree, tag and blob. However to save space, an object could be stored as a "delta" of another "base" object. These representations are assigned new types ofs-delta and ref-delta, which is only valid in a pack file.+----------+---------+---------+---------+---------+-------+-------+-------+ | 1xxxxxxx | offset1 | offset2 | offset3 | offset4 | size1 | size2 | size3 | +----------+---------+---------+---------+---------+-------+-------+-------+
+----------+---------+---------+ | 10000101 | offset1 | offset3 | +----------+---------+---------+
+----------+============+ | 0xxxxxxx | data | +----------+============+
+----------+============ | 00000000 | +----------+============
ORIGINAL (VERSION 1) PACK-*.IDX FILES HAVE THE FOLLOWING FORMAT:
•The header consists of 256 4-byte
network byte order integers. N-th entry of this table records the number of
objects in the corresponding pack, the first byte of whose object name is less
than or equal to N. This is called the first-level fan-out table.
•The header is followed by sorted
24-byte entries, one entry per object in the pack. Each entry is:
4-byte network byte order integer, recording where the object is stored in the packfile as the offset from the beginning.
one object name of the appropriate size.
•The file is concluded with a trailer:
A copy of the pack checksum at the end of the corresponding packfile.
Index checksum of all of the above.
-- +--------------------------------+ fanout | fanout[0] = 2 (for example) |-. table +--------------------------------+ | | fanout[1] | | +--------------------------------+ | | fanout[2] | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | fanout[255] = total objects |---. -- +--------------------------------+ | | main | offset | | | index | object name 00XXXXXXXXXXXXXXXX | | | table +--------------------------------+ | | | offset | | | | object name 00XXXXXXXXXXXXXXXX | | | +--------------------------------+<+ | .-| offset | | | | object name 01XXXXXXXXXXXXXXXX | | | +--------------------------------+ | | | offset | | | | object name 01XXXXXXXXXXXXXXXX | | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | offset | | | | object name FFXXXXXXXXXXXXXXXX | | --| +--------------------------------+<--+ trailer | | packfile checksum | | +--------------------------------+ | | idxfile checksum | | +--------------------------------+ .-------. | Pack file entry: <+
packed object header: 1-byte size extension bit (MSB) type (next 3 bit) size0 (lower 4-bit) n-byte sizeN (as long as MSB is set, each 7-bit) size0..sizeN form 4+7+7+..+7 bit integer, size0 is the least significant part, and sizeN is the most significant part. packed object data: If it is not DELTA, then deflated bytes (the size above is the size before compression). If it is REF_DELTA, then base object name (the size above is the size of the delta data that follows). delta data, deflated. If it is OFS_DELTA, then n-byte offset (see below) interpreted as a negative offset from the type-byte of the header of the ofs-delta entry (the size above is the size of the delta data that follows). delta data, deflated.
offset encoding: n bytes with MSB set in all but the last one. The offset is then the number constructed by concatenating the lower 7 bit of each byte, and for n >= 2 adding 2^7 + 2^14 + ... + 2^(7*(n-1)) to the result.
VERSION 2 PACK-*.IDX FILES SUPPORT PACKS LARGER THAN 4 GIB, AND
have some other reorganizations. They have the format:
•A 4-byte magic number \377tOc
which is an unreasonable fanout[0] value.
•A 4-byte version number (= 2)
•A 256-entry fan-out table just like
v1.
•A table of sorted object names. These
are packed together without offset values to reduce the cache footprint of the
binary search for a specific object name.
•A table of 4-byte CRC32 values of the
packed object data. This is new in v2 so compressed data can be copied
directly from pack to pack during repacking without undetected data
corruption.
•A table of 4-byte offset values (in
network byte order). These are usually 31-bit pack file offsets, but large
offsets are encoded as an index into the next table with the msbit set.
•A table of 8-byte offset entries
(empty for pack files less than 2 GiB). Pack files are organized with heavily
used objects toward the front, so most object references should not need to
refer to this table.
•The same trailer as a v1 pack file:
A copy of the pack checksum at the end of corresponding packfile.
Index checksum of all of the above.
PACK-*.REV FILES HAVE THE FORMAT:
•A 4-byte magic number
0x52494458 ( RIDX).
•A 4-byte version identifier (=
1).
•A 4-byte hash function identifier (= 1
for SHA-1, 2 for SHA-256).
•A table of index positions (one per
packed object, num_objects in total, each a 4-byte unsigned integer in network
order), sorted by their corresponding offsets in the packfile.
•A trailer, containing a:
checksum of the corresponding packfile, and
a checksum of all of the above.
PACK-*.MTIMES FILES HAVE THE FORMAT:
All 4-byte numbers are in network byte order.•A 4-byte magic number
0x4d544d45 ( MTME).
•A 4-byte version identifier (=
1).
•A 4-byte hash function identifier (= 1
for SHA-1, 2 for SHA-256).
•A table of 4-byte unsigned integers.
The ith value is the modification time (mtime) of the ith object in the
corresponding pack by lexicographic (index) order. The mtimes count standard
epoch seconds.
•A trailer, containing a checksum of
the corresponding packfile, and a checksum of all of the above (each having
length according to the specified hash function).
MULTI-PACK-INDEX (MIDX) FILES HAVE THE FOLLOWING FORMAT:
The multi-pack-index files refer to multiple pack-files and loose objects.4-byte signature: The signature is: {'M', 'I', 'D', 'X'}
1-byte version number: Git only writes or recognizes version 1.
1-byte Object Id Version We infer the length of object IDs (OIDs) from this value: 1 => SHA-1 2 => SHA-256 If the hash type does not match the repository's hash algorithm, the multi-pack-index file should be ignored with a warning presented to the user.
1-byte number of "chunks"
1-byte number of base multi-pack-index files: This value is currently always zero.
4-byte number of pack files
(C + 1) * 12 bytes providing the chunk offsets: First 4 bytes describe chunk id. Value 0 is a terminating label. Other 8 bytes provide offset in current file for chunk to start. (Chunks are provided in file-order, so you can infer the length using the next chunk position if necessary.)
The CHUNK LOOKUP matches the table of contents from the chunk-based file format, see linkgit:gitformat-chunk[5].
The remaining data in the body is described one chunk at a time, and these chunks may be given in any order. Chunks are required unless otherwise specified.
Packfile Names (ID: {'P', 'N', 'A', 'M'}) Stores the packfile names as concatenated, null-terminated strings. Packfiles must be listed in lexicographic order for fast lookups by name. This is the only chunk not guaranteed to be a multiple of four bytes in length, so should be the last chunk for alignment reasons.
OID Fanout (ID: {'O', 'I', 'D', 'F'}) The ith entry, F[i], stores the number of OIDs with first byte at most i. Thus F[255] stores the total number of objects.
OID Lookup (ID: {'O', 'I', 'D', 'L'}) The OIDs for all objects in the MIDX are stored in lexicographic order in this chunk.
Object Offsets (ID: {'O', 'O', 'F', 'F'}) Stores two 4-byte values for every object. 1: The pack-int-id for the pack storing this object. 2: The offset within the pack. If all offsets are less than 2^32, then the large offset chunk will not exist and offsets are stored as in IDX v1. If there is at least one offset value larger than 2^32-1, then the large offset chunk must exist, and offsets larger than 2^31-1 must be stored in it instead. If the large offset chunk exists and the 31st bit is on, then removing that bit reveals the row in the large offsets containing the 8-byte offset of this object.
[Optional] Object Large Offsets (ID: {'L', 'O', 'F', 'F'}) 8-byte offsets into large packfiles.
[Optional] Bitmap pack order (ID: {'R', 'I', 'D', 'X'}) A list of MIDX positions (one per object in the MIDX, num_objects in total, each a 4-byte unsigned integer in network byte order), sorted according to their relative bitmap/pseudo-pack positions.
Index checksum of the above contents.
MULTI-PACK-INDEX REVERSE INDEXES
Similar to the pack-based reverse index, the multi-pack index can also be used to generate a reverse index.|a,0|a,1|...|a,9|b,0|b,1|...|b,14|c,0|c,1|...|c,19|
•If one of pack(o1) and
pack(o2) is preferred and the other is not, then the preferred one
sorts first.
(This is a detail that allows the MIDX bitmap to determine which pack should be
used by the pack-reuse mechanism, since it can ask the MIDX for the pack
containing the object at bit position 0).
•If pack(o1) ≠ pack(o2),
then sort the two objects in descending order based on the pack ID.
•Otherwise, pack(o1) = pack(o2),
and the objects are sorted in pack-order (i.e., o1 sorts ahead of
o2 exactly when offset(o1) < offset(o2)).
CRUFT PACKS
The cruft packs feature offer an alternative to Git’s traditional mechanism of removing unreachable objects. This document provides an overview of Git’s pruning mechanism, and how a cruft pack can be used instead to accomplish the same.Background
To remove unreachable objects from your repository, Git offers git repack -Ad (see git-repack(1)). Quoting from the documentation:[...] unreachable objects in a previous pack become loose, unpacked objects, instead of being left in the old pack. [...] loose unreachable objects will be pruned according to normal expiry rules with the next 'git gc' invocation.
Cruft packs
A cruft pack eliminates the need for storing unreachable objects in a loose state by including the per-object mtimes in a separate file alongside a single pack containing all loose objects. 1.Enumerate every object, marking any object
which is (a) not contained in a kept-pack, and (b) whose mtime is within the
grace period as a traversal tip.
2.Perform a reachability traversal based on
the tips gathered in the previous step, adding every object along the way to
the pack.
3.Write the pack out, along with a
.mtimes file that records the per-object timestamps.
Caution for mixed-version environments
Repositories that have cruft packs in them will continue to work with any older version of Git. Note, however, that previous versions of Git which do not understand the .mtimes file will use the cruft pack’s mtime as the mtime for all of the objects in it. In other words, do not expect older (pre-cruft pack) versions of Git to interpret or even read the contents of the .mtimes file.•An older version of Git running GC
explodes the contents of an existing cruft pack loose, using the cruft
pack’s mtime.
•A newer version running GC collects
those loose objects into a cruft pack, where the .mtime file reflects the
loose object’s actual mtimes, but the cruft pack mtime is
"now".
Alternatives
Notable alternatives to this design include:•The location of the per-object mtime
data, and
•Storing unreachable objects in
multiple cruft packs.
GIT
Part of the git(1) suite02/28/2023 | Git 2.39.2 |