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