4 == pack-*.pack files have the following format:
6 - A header appears at the beginning and consists of the following:
9 The signature is: {'P', 'A', 'C', 'K'}
11 4-byte version number (network byte order):
12 Git currently accepts version number 2 or 3 but
13 generates version 2 only.
15 4-byte number of objects contained in the pack (network byte order)
17 Observation: we cannot have more than 4G versions ;-) and
18 more than 4G objects in a pack.
20 - The header is followed by number of object entries, each of
21 which looks like this:
23 (undeltified representation)
24 n-byte type and length (3-bit type, (n-1)*7+4-bit length)
27 (deltified representation)
28 n-byte type and length (3-bit type, (n-1)*7+4-bit length)
29 20-byte base object name if OBJ_REF_DELTA or a negative relative
30 offset from the delta object's position in the pack if this
31 is an OBJ_OFS_DELTA object
34 Observation: length of each object is encoded in a variable
35 length format and is not constrained to 32-bit or anything.
37 - The trailer records 20-byte SHA-1 checksum of all of the above.
41 Valid object types are:
50 Type 5 is reserved for future expansion. Type 0 is invalid.
52 === Deltified representation
54 Conceptually there are only four object types: commit, tree, tag and
55 blob. However to save space, an object could be stored as a "delta" of
56 another "base" object. These representations are assigned new types
57 ofs-delta and ref-delta, which is only valid in a pack file.
59 Both ofs-delta and ref-delta store the "delta" to be applied to
60 another object (called 'base object') to reconstruct the object. The
61 difference between them is, ref-delta directly encodes 20-byte base
62 object name. If the base object is in the same pack, ofs-delta encodes
63 the offset of the base object in the pack instead.
65 The base object could also be deltified if it's in the same pack.
66 Ref-delta can also refer to an object outside the pack (i.e. the
67 so-called "thin pack"). When stored on disk however, the pack should
68 be self contained to avoid cyclic dependency.
70 The delta data is a sequence of instructions to reconstruct an object
71 from the base object. If the base object is deltified, it must be
72 converted to canonical form first. Each instruction appends more and
73 more data to the target object until it's complete. There are two
74 supported instructions so far: one for copy a byte range from the
75 source object and one for inserting new data embedded in the
78 Each instruction has variable length. Instruction type is determined
79 by the seventh bit of the first octet. The following diagrams follow
80 the convention in RFC 1951 (Deflate compressed data format).
82 ==== Instruction to copy from base object
84 +----------+---------+---------+---------+---------+-------+-------+-------+
85 | 1xxxxxxx | offset1 | offset2 | offset3 | offset4 | size1 | size2 | size3 |
86 +----------+---------+---------+---------+---------+-------+-------+-------+
88 This is the instruction format to copy a byte range from the source
89 object. It encodes the offset to copy from and the number of bytes to
90 copy. Offset and size are in little-endian order.
92 All offset and size bytes are optional. This is to reduce the
93 instruction size when encoding small offsets or sizes. The first seven
94 bits in the first octet determines which of the next seven octets is
95 present. If bit zero is set, offset1 is present. If bit one is set
96 offset2 is present and so on.
98 Note that a more compact instruction does not change offset and size
99 encoding. For example, if only offset2 is omitted like below, offset3
100 still contains bits 16-23. It does not become offset2 and contains
101 bits 8-15 even if it's right next to offset1.
103 +----------+---------+---------+
104 | 10000101 | offset1 | offset3 |
105 +----------+---------+---------+
107 In its most compact form, this instruction only takes up one byte
108 (0x80) with both offset and size omitted, which will have default
109 values zero. There is another exception: size zero is automatically
110 converted to 0x10000.
112 ==== Instruction to add new data
114 +----------+============+
116 +----------+============+
118 This is the instruction to construct target object without the base
119 object. The following data is appended to the target object. The first
120 seven bits of the first octet determines the size of data in
121 bytes. The size must be non-zero.
123 ==== Reserved instruction
125 +----------+============
127 +----------+============
129 This is the instruction reserved for future expansion.
131 == Original (version 1) pack-*.idx files have the following format:
133 - The header consists of 256 4-byte network byte order
134 integers. N-th entry of this table records the number of
135 objects in the corresponding pack, the first byte of whose
136 object name is less than or equal to N. This is called the
137 'first-level fan-out' table.
139 - The header is followed by sorted 24-byte entries, one entry
140 per object in the pack. Each entry is:
142 4-byte network byte order integer, recording where the
143 object is stored in the packfile as the offset from the
148 - The file is concluded with a trailer:
150 A copy of the 20-byte SHA-1 checksum at the end of
151 corresponding packfile.
153 20-byte SHA-1-checksum of all of the above.
157 -- +--------------------------------+
158 fanout | fanout[0] = 2 (for example) |-.
159 table +--------------------------------+ |
161 +--------------------------------+ |
163 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
164 | fanout[255] = total objects |---.
165 -- +--------------------------------+ | |
167 index | object name 00XXXXXXXXXXXXXXXX | | |
168 table +--------------------------------+ | |
170 | object name 00XXXXXXXXXXXXXXXX | | |
171 +--------------------------------+<+ |
173 | | object name 01XXXXXXXXXXXXXXXX | |
174 | +--------------------------------+ |
176 | | object name 01XXXXXXXXXXXXXXXX | |
177 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
179 | | object name FFXXXXXXXXXXXXXXXX | |
180 --| +--------------------------------+<--+
181 trailer | | packfile checksum |
182 | +--------------------------------+
183 | | idxfile checksum |
184 | +--------------------------------+
189 packed object header:
190 1-byte size extension bit (MSB)
193 n-byte sizeN (as long as MSB is set, each 7-bit)
194 size0..sizeN form 4+7+7+..+7 bit integer, size0
195 is the least significant part, and sizeN is the
196 most significant part.
198 If it is not DELTA, then deflated bytes (the size above
199 is the size before compression).
200 If it is REF_DELTA, then
201 20-byte base object name SHA-1 (the size above is the
202 size of the delta data that follows).
203 delta data, deflated.
204 If it is OFS_DELTA, then
205 n-byte offset (see below) interpreted as a negative
206 offset from the type-byte of the header of the
207 ofs-delta entry (the size above is the size of
208 the delta data that follows).
209 delta data, deflated.
212 n bytes with MSB set in all but the last one.
213 The offset is then the number constructed by
214 concatenating the lower 7 bit of each byte, and
215 for n >= 2 adding 2^7 + 2^14 + ... + 2^(7*(n-1))
220 == Version 2 pack-*.idx files support packs larger than 4 GiB, and
221 have some other reorganizations. They have the format:
223 - A 4-byte magic number '\377tOc' which is an unreasonable
226 - A 4-byte version number (= 2)
228 - A 256-entry fan-out table just like v1.
230 - A table of sorted 20-byte SHA-1 object names. These are
231 packed together without offset values to reduce the cache
232 footprint of the binary search for a specific object name.
234 - A table of 4-byte CRC32 values of the packed object data.
235 This is new in v2 so compressed data can be copied directly
236 from pack to pack during repacking without undetected
239 - A table of 4-byte offset values (in network byte order).
240 These are usually 31-bit pack file offsets, but large
241 offsets are encoded as an index into the next table with
244 - A table of 8-byte offset entries (empty for pack files less
245 than 2 GiB). Pack files are organized with heavily used
246 objects toward the front, so most object references should
247 not need to refer to this table.
249 - The same trailer as a v1 pack file:
251 A copy of the 20-byte SHA-1 checksum at the end of
252 corresponding packfile.
254 20-byte SHA-1-checksum of all of the above.