[PATCH] denser delta header encoding
[git/git.git] / pack-objects.c
CommitLineData
27225f2e 1#include <ctype.h>
c323ac7d
LT
2#include "cache.h"
3#include "object.h"
4#include "delta.h"
a733cb60 5#include "pack.h"
c38138cd 6#include "csum-file.h"
c323ac7d 7
d22b9290 8static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] {--stdout | base-name} < object-list";
c323ac7d 9
c323ac7d
LT
10struct object_entry {
11 unsigned char sha1[20];
12 unsigned long size;
13 unsigned long offset;
14 unsigned int depth;
27225f2e 15 unsigned int hash;
a733cb60 16 enum object_type type;
c323ac7d
LT
17 unsigned long delta_size;
18 struct object_entry *delta;
19};
20
21static struct object_entry **sorted_by_sha, **sorted_by_type;
22static struct object_entry *objects = NULL;
23static int nr_objects = 0, nr_alloc = 0;
c323ac7d 24static const char *base_name;
e1808845 25static unsigned char pack_file_sha1[20];
c323ac7d 26
c323ac7d
LT
27static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
28{
29 unsigned long othersize, delta_size;
30 char type[10];
31 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
32 void *delta_buf;
33
34 if (!otherbuf)
35 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
8ee378a0
JH
36 delta_buf = diff_delta(otherbuf, othersize,
37 buf, size, &delta_size, ~0UL);
75c42d8c 38 if (!delta_buf || delta_size != entry->delta_size)
c323ac7d
LT
39 die("delta size changed");
40 free(buf);
41 free(otherbuf);
42 return delta_buf;
43}
44
a733cb60
LT
45/*
46 * The per-object header is a pretty dense thing, which is
47 * - first byte: low four bits are "size", then three bits of "type",
48 * and the high bit is "size continues".
49 * - each byte afterwards: low seven bits are size continuation,
50 * with the high bit being "size continues"
51 */
52static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
53{
54 int n = 1, i;
55 unsigned char c;
56
57 if (type < OBJ_COMMIT || type > OBJ_DELTA)
58 die("bad type %d", type);
59
60 /*
61 * Shift the size up by 7 bits at a time,
62 * until you get bits in the "high four".
63 * That will be our beginning. We'll have
64 * four size bits in 28..31, then groups
65 * of seven in 21..27, 14..20, 7..13 and
66 * finally 0..6.
67 */
68 if (size) {
69 n = 5;
70 while (!(size & 0xfe000000)) {
71 size <<= 7;
72 n--;
73 }
74 }
75 c = (type << 4) | (size >> 28);
76 for (i = 1; i < n; i++) {
77 *hdr++ = c | 0x80;
78 c = (size >> 21) & 0x7f;
79 size <<= 7;
80 }
81 *hdr = c;
82 return n;
83}
84
c38138cd 85static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
c323ac7d
LT
86{
87 unsigned long size;
88 char type[10];
89 void *buf = read_sha1_file(entry->sha1, type, &size);
a733cb60 90 unsigned char header[10];
c323ac7d 91 unsigned hdrlen, datalen;
a733cb60 92 enum object_type obj_type;
c323ac7d
LT
93
94 if (!buf)
95 die("unable to read %s", sha1_to_hex(entry->sha1));
96 if (size != entry->size)
97 die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
98
99 /*
e1ddc976
JH
100 * The object header is a byte of 'type' followed by zero or
101 * more bytes of length. For deltas, the 20 bytes of delta sha1
102 * follows that.
c323ac7d 103 */
a733cb60 104 obj_type = entry->type;
c323ac7d 105 if (entry->delta) {
c323ac7d
LT
106 buf = delta_against(buf, size, entry);
107 size = entry->delta_size;
a733cb60 108 obj_type = OBJ_DELTA;
c323ac7d 109 }
a733cb60 110 hdrlen = encode_header(obj_type, size, header);
c38138cd 111 sha1write(f, header, hdrlen);
a733cb60
LT
112 if (entry->delta) {
113 sha1write(f, entry->delta, 20);
114 hdrlen += 20;
115 }
c38138cd 116 datalen = sha1write_compressed(f, buf, size);
c323ac7d
LT
117 free(buf);
118 return hdrlen + datalen;
119}
120
9d5ab962
JH
121static unsigned long write_one(struct sha1file *f,
122 struct object_entry *e,
123 unsigned long offset)
124{
125 if (e->offset)
126 /* offset starts from header size and cannot be zero
127 * if it is written already.
128 */
129 return offset;
130 e->offset = offset;
131 offset += write_object(f, e);
132 /* if we are delitified, write out its base object. */
133 if (e->delta)
134 offset = write_one(f, e->delta, offset);
135 return offset;
136}
137
c323ac7d
LT
138static void write_pack_file(void)
139{
140 int i;
d22b9290 141 struct sha1file *f;
a733cb60 142 unsigned long offset;
c323ac7d 143 unsigned long mb;
a733cb60 144 struct pack_header hdr;
c323ac7d 145
d22b9290
LT
146 if (!base_name)
147 f = sha1fd(1, "<stdout>");
148 else
149 f = sha1create("%s.%s", base_name, "pack");
a733cb60
LT
150 hdr.hdr_signature = htonl(PACK_SIGNATURE);
151 hdr.hdr_version = htonl(1);
152 hdr.hdr_entries = htonl(nr_objects);
153 sha1write(f, &hdr, sizeof(hdr));
154 offset = sizeof(hdr);
9d5ab962
JH
155 for (i = 0; i < nr_objects; i++)
156 offset = write_one(f, objects + i, offset);
157
e1808845 158 sha1close(f, pack_file_sha1, 1);
c323ac7d
LT
159 mb = offset >> 20;
160 offset &= 0xfffff;
161}
162
163static void write_index_file(void)
164{
165 int i;
c38138cd 166 struct sha1file *f = sha1create("%s.%s", base_name, "idx");
c323ac7d
LT
167 struct object_entry **list = sorted_by_sha;
168 struct object_entry **last = list + nr_objects;
169 unsigned int array[256];
170
171 /*
172 * Write the first-level table (the list is sorted,
173 * but we use a 256-entry lookup to be able to avoid
e1808845 174 * having to do eight extra binary search iterations).
c323ac7d
LT
175 */
176 for (i = 0; i < 256; i++) {
177 struct object_entry **next = list;
178 while (next < last) {
179 struct object_entry *entry = *next;
180 if (entry->sha1[0] != i)
181 break;
182 next++;
183 }
184 array[i] = htonl(next - sorted_by_sha);
185 list = next;
186 }
c38138cd 187 sha1write(f, array, 256 * sizeof(int));
c323ac7d
LT
188
189 /*
190 * Write the actual SHA1 entries..
191 */
192 list = sorted_by_sha;
49397104 193 for (i = 0; i < nr_objects; i++) {
c323ac7d
LT
194 struct object_entry *entry = *list++;
195 unsigned int offset = htonl(entry->offset);
c38138cd
LT
196 sha1write(f, &offset, 4);
197 sha1write(f, entry->sha1, 20);
c323ac7d 198 }
e1808845
LT
199 sha1write(f, pack_file_sha1, 20);
200 sha1close(f, NULL, 1);
c323ac7d
LT
201}
202
27225f2e 203static void add_object_entry(unsigned char *sha1, unsigned int hash)
c323ac7d
LT
204{
205 unsigned int idx = nr_objects;
206 struct object_entry *entry;
207
208 if (idx >= nr_alloc) {
209 unsigned int needed = (idx + 1024) * 3 / 2;
210 objects = xrealloc(objects, needed * sizeof(*entry));
211 nr_alloc = needed;
212 }
213 entry = objects + idx;
214 memset(entry, 0, sizeof(*entry));
215 memcpy(entry->sha1, sha1, 20);
27225f2e 216 entry->hash = hash;
c323ac7d
LT
217 nr_objects = idx+1;
218}
219
220static void check_object(struct object_entry *entry)
221{
36e4d74a
JH
222 char type[20];
223
224 if (!sha1_object_info(entry->sha1, type, &entry->size)) {
225 if (!strcmp(type, "commit")) {
a733cb60 226 entry->type = OBJ_COMMIT;
36e4d74a 227 } else if (!strcmp(type, "tree")) {
a733cb60 228 entry->type = OBJ_TREE;
36e4d74a 229 } else if (!strcmp(type, "blob")) {
a733cb60 230 entry->type = OBJ_BLOB;
a69d0943 231 } else if (!strcmp(type, "tag")) {
a733cb60 232 entry->type = OBJ_TAG;
36e4d74a
JH
233 } else
234 die("unable to pack object %s of type %s",
235 sha1_to_hex(entry->sha1), type);
236 }
237 else
238 die("unable to get type of object %s",
239 sha1_to_hex(entry->sha1));
c323ac7d
LT
240}
241
242static void get_object_details(void)
243{
244 int i;
245 struct object_entry *entry = objects;
246
247 for (i = 0; i < nr_objects; i++)
248 check_object(entry++);
249}
250
251typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
252
253static entry_sort_t current_sort;
254
255static int sort_comparator(const void *_a, const void *_b)
256{
257 struct object_entry *a = *(struct object_entry **)_a;
258 struct object_entry *b = *(struct object_entry **)_b;
259 return current_sort(a,b);
260}
261
262static struct object_entry **create_sorted_list(entry_sort_t sort)
263{
264 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
265 int i;
266
267 for (i = 0; i < nr_objects; i++)
268 list[i] = objects + i;
269 current_sort = sort;
270 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
271 return list;
272}
273
274static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
275{
276 return memcmp(a->sha1, b->sha1, 20);
277}
278
279static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
280{
281 if (a->type < b->type)
282 return -1;
283 if (a->type > b->type)
284 return 1;
27225f2e
LT
285 if (a->hash < b->hash)
286 return -1;
287 if (a->hash > b->hash)
288 return 1;
c323ac7d
LT
289 if (a->size < b->size)
290 return -1;
291 if (a->size > b->size)
292 return 1;
293 return a < b ? -1 : (a > b);
294}
295
296struct unpacked {
297 struct object_entry *entry;
298 void *data;
299};
300
301/*
521a4f4c
LT
302 * We search for deltas _backwards_ in a list sorted by type and
303 * by size, so that we see progressively smaller and smaller files.
304 * That's because we prefer deltas to be from the bigger file
305 * to the smaller - deletes are potentially cheaper, but perhaps
306 * more importantly, the bigger file is likely the more recent
307 * one.
c323ac7d 308 */
d116a45a 309static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
c323ac7d
LT
310{
311 struct object_entry *cur_entry = cur->entry;
312 struct object_entry *old_entry = old->entry;
521a4f4c 313 unsigned long size, oldsize, delta_size, sizediff;
75c42d8c 314 long max_size;
c323ac7d
LT
315 void *delta_buf;
316
317 /* Don't bother doing diffs between different types */
318 if (cur_entry->type != old_entry->type)
319 return -1;
320
c323ac7d 321 size = cur_entry->size;
75c42d8c
LT
322 if (size < 50)
323 return -1;
c323ac7d 324 oldsize = old_entry->size;
521a4f4c
LT
325 sizediff = oldsize > size ? oldsize - size : size - oldsize;
326 if (sizediff > size / 8)
c323ac7d 327 return -1;
d116a45a
LT
328 if (old_entry->depth >= max_depth)
329 return 0;
c323ac7d
LT
330
331 /*
332 * NOTE!
333 *
334 * We always delta from the bigger to the smaller, since that's
335 * more space-efficient (deletes don't have to say _what_ they
336 * delete).
337 */
75c42d8c
LT
338 max_size = size / 2 - 20;
339 if (cur_entry->delta)
340 max_size = cur_entry->delta_size-1;
27225f2e
LT
341 if (sizediff >= max_size)
342 return -1;
8ee378a0
JH
343 delta_buf = diff_delta(old->data, oldsize,
344 cur->data, size, &delta_size, max_size);
c323ac7d 345 if (!delta_buf)
75c42d8c
LT
346 return 0;
347 cur_entry->delta = old_entry;
348 cur_entry->delta_size = delta_size;
d116a45a 349 cur_entry->depth = old_entry->depth + 1;
c323ac7d 350 free(delta_buf);
eb41ab11 351 return 0;
c323ac7d
LT
352}
353
d116a45a 354static void find_deltas(struct object_entry **list, int window, int depth)
c323ac7d 355{
521a4f4c 356 int i, idx;
c323ac7d
LT
357 unsigned int array_size = window * sizeof(struct unpacked);
358 struct unpacked *array = xmalloc(array_size);
359
360 memset(array, 0, array_size);
521a4f4c
LT
361 i = nr_objects;
362 idx = 0;
363 while (--i >= 0) {
c323ac7d
LT
364 struct object_entry *entry = list[i];
365 struct unpacked *n = array + idx;
366 unsigned long size;
367 char type[10];
368 int j;
369
370 free(n->data);
371 n->entry = entry;
372 n->data = read_sha1_file(entry->sha1, type, &size);
373 if (size != entry->size)
374 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
78817c15
LT
375 j = window;
376 while (--j > 0) {
377 unsigned int other_idx = idx + j;
c323ac7d 378 struct unpacked *m;
78817c15
LT
379 if (other_idx >= window)
380 other_idx -= window;
c323ac7d
LT
381 m = array + other_idx;
382 if (!m->entry)
383 break;
d116a45a 384 if (try_delta(n, m, depth) < 0)
c323ac7d
LT
385 break;
386 }
521a4f4c
LT
387 idx++;
388 if (idx >= window)
389 idx = 0;
c323ac7d
LT
390 }
391}
392
393int main(int argc, char **argv)
394{
27225f2e 395 char line[PATH_MAX + 20];
d22b9290 396 int window = 10, depth = 10, pack_to_stdout = 0;
c323ac7d
LT
397 int i;
398
399 for (i = 1; i < argc; i++) {
400 const char *arg = argv[i];
401
402 if (*arg == '-') {
403 if (!strncmp("--window=", arg, 9)) {
404 char *end;
f846bbff 405 window = strtoul(arg+9, &end, 0);
c323ac7d
LT
406 if (!arg[9] || *end)
407 usage(pack_usage);
408 continue;
409 }
d116a45a
LT
410 if (!strncmp("--depth=", arg, 8)) {
411 char *end;
412 depth = strtoul(arg+8, &end, 0);
413 if (!arg[8] || *end)
414 usage(pack_usage);
415 continue;
416 }
d22b9290
LT
417 if (!strcmp("--stdout", arg)) {
418 pack_to_stdout = 1;
419 continue;
420 }
c323ac7d
LT
421 usage(pack_usage);
422 }
423 if (base_name)
424 usage(pack_usage);
425 base_name = arg;
426 }
427
d22b9290 428 if (pack_to_stdout != !base_name)
c323ac7d
LT
429 usage(pack_usage);
430
431 while (fgets(line, sizeof(line), stdin) != NULL) {
27225f2e
LT
432 unsigned int hash;
433 char *p;
c323ac7d 434 unsigned char sha1[20];
27225f2e 435
c323ac7d
LT
436 if (get_sha1_hex(line, sha1))
437 die("expected sha1, got garbage");
27225f2e
LT
438 hash = 0;
439 p = line+40;
440 while (*p) {
441 unsigned char c = *p++;
442 if (isspace(c))
443 continue;
444 hash = hash * 11 + c;
445 }
446 add_object_entry(sha1, hash);
c323ac7d
LT
447 }
448 get_object_details();
449
d22b9290 450 fprintf(stderr, "Packing %d objects\n", nr_objects);
c323ac7d
LT
451
452 sorted_by_sha = create_sorted_list(sha1_sort);
453 sorted_by_type = create_sorted_list(type_size_sort);
d116a45a
LT
454 if (window && depth)
455 find_deltas(sorted_by_type, window+1, depth);
c323ac7d
LT
456
457 write_pack_file();
d22b9290
LT
458 if (!pack_to_stdout)
459 write_index_file();
c323ac7d
LT
460 return 0;
461}