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