git-http-fetch: Remove size limit for objects/info/{packs,alternates}
[alt-git.git] / pack-objects.c
blob8a1ee746e083947343087f127f7fa6280a62b77c
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"
8 static const char pack_usage[] = "git-pack-objects [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
10 struct 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;
21 static unsigned char object_list_sha1[20];
22 static int non_empty = 0;
23 static int local = 0;
24 static int incremental = 0;
25 static struct object_entry **sorted_by_sha, **sorted_by_type;
26 static struct object_entry *objects = NULL;
27 static int nr_objects = 0, nr_alloc = 0;
28 static const char *base_name;
29 static unsigned char pack_file_sha1[20];
31 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
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;
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;
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"
56 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
58 int n = 1;
59 unsigned char c;
61 if (type < OBJ_COMMIT || type > OBJ_DELTA)
62 die("bad type %d", type);
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++;
72 *hdr = c;
73 return n;
76 static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
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;
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);
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.
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;
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;
107 datalen = sha1write_compressed(f, buf, size);
108 free(buf);
109 return hdrlen + datalen;
112 static unsigned long write_one(struct sha1file *f,
113 struct object_entry *e,
114 unsigned long offset)
116 if (e->offset)
117 /* offset starts from header size and cannot be zero
118 * if it is written already.
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;
129 static void write_pack_file(void)
131 int i;
132 struct sha1file *f;
133 unsigned long offset;
134 unsigned long mb;
135 struct pack_header hdr;
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);
149 sha1close(f, pack_file_sha1, 1);
150 mb = offset >> 20;
151 offset &= 0xfffff;
154 static void write_index_file(void)
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];
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).
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++;
175 array[i] = htonl(next - sorted_by_sha);
176 list = next;
178 sha1write(f, array, 256 * sizeof(int));
181 * Write the actual SHA1 entries..
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);
190 sha1write(f, pack_file_sha1, 20);
191 sha1close(f, NULL, 1);
194 static int add_object_entry(unsigned char *sha1, unsigned int hash)
196 unsigned int idx = nr_objects;
197 struct object_entry *entry;
199 if (incremental || local) {
200 struct packed_git *p;
202 for (p = packed_git; p; p = p->next) {
203 struct pack_entry e;
205 if (find_pack_entry_one(sha1, &e, p)) {
206 if (incremental)
207 return 0;
208 if (local && !p->pack_local)
209 return 0;
214 if (idx >= nr_alloc) {
215 unsigned int needed = (idx + 1024) * 3 / 2;
216 objects = xrealloc(objects, needed * sizeof(*entry));
217 nr_alloc = needed;
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;
227 static void check_object(struct object_entry *entry)
229 char type[20];
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);
244 else
245 die("unable to get type of object %s",
246 sha1_to_hex(entry->sha1));
249 static void get_object_details(void)
251 int i;
252 struct object_entry *entry = objects;
254 for (i = 0; i < nr_objects; i++)
255 check_object(entry++);
258 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
260 static entry_sort_t current_sort;
262 static int sort_comparator(const void *_a, const void *_b)
264 struct object_entry *a = *(struct object_entry **)_a;
265 struct object_entry *b = *(struct object_entry **)_b;
266 return current_sort(a,b);
269 static struct object_entry **create_sorted_list(entry_sort_t sort)
271 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
272 int i;
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;
281 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
283 return memcmp(a->sha1, b->sha1, 20);
286 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
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);
303 struct unpacked {
304 struct object_entry *entry;
305 void *data;
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.
316 static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
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;
324 /* Don't bother doing diffs between different types */
325 if (cur_entry->type != old_entry->type)
326 return -1;
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;
339 * NOTE!
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).
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;
361 static void find_deltas(struct object_entry **list, int window, int depth)
363 int i, idx;
364 unsigned int array_size = window * sizeof(struct unpacked);
365 struct unpacked *array = xmalloc(array_size);
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;
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;
394 idx++;
395 if (idx >= window)
396 idx = 0;
399 for (i = 0; i < window; ++i)
400 free(array[i].data);
401 free(array);
404 int main(int argc, char **argv)
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;
412 for (i = 1; i < argc; i++) {
413 const char *arg = argv[i];
415 if (*arg == '-') {
416 if (!strcmp("--non-empty", arg)) {
417 non_empty = 1;
418 continue;
420 if (!strcmp("--local", arg)) {
421 local = 1;
422 continue;
424 if (!strcmp("--incremental", arg)) {
425 incremental = 1;
426 continue;
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;
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;
442 if (!strcmp("--stdout", arg)) {
443 pack_to_stdout = 1;
444 continue;
446 usage(pack_usage);
448 if (base_name)
449 usage(pack_usage);
450 base_name = arg;
453 if (pack_to_stdout != !base_name)
454 usage(pack_usage);
456 prepare_packed_git();
457 while (fgets(line, sizeof(line), stdin) != NULL) {
458 unsigned int hash;
459 char *p;
460 unsigned char sha1[20];
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;
472 add_object_entry(sha1, hash);
474 if (non_empty && !nr_objects)
475 return 0;
476 get_object_details();
478 fprintf(stderr, "Packing %d objects\n", nr_objects);
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);
487 SHA1_Final(object_list_sha1, &ctx);
489 sorted_by_type = create_sorted_list(type_size_sort);
490 if (window && depth)
491 find_deltas(sorted_by_type, window+1, depth);
493 write_pack_file();
494 if (!pack_to_stdout) {
495 write_index_file();
496 puts(sha1_to_hex(object_list_sha1));
498 return 0;