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