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