bc8bb954bfb7d9ba995e0f22f44361dcb24b9162
[git.git] / pack-objects.c
1 #include <ctype.h>
2 #include "cache.h"
3 #include "object.h"
4 #include "delta.h"
5
6 static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] base-name < object-list";
7
8 enum object_type {
9         OBJ_NONE,
10         OBJ_COMMIT,
11         OBJ_TREE,
12         OBJ_BLOB,
13         OBJ_DELTA       // NOTE! This is _not_ the same as a "delta" object in the filesystem
14 };
15
16 struct object_entry {
17         unsigned char sha1[20];
18         unsigned long size;
19         unsigned long offset;
20         unsigned int depth;
21         unsigned int hash;
22         enum object_type type;
23         unsigned long delta_size;
24         struct object_entry *delta;
25 };
26
27 static struct object_entry **sorted_by_sha, **sorted_by_type;
28 static struct object_entry *objects = NULL;
29 static int nr_objects = 0, nr_alloc = 0;
30 static const char *base_name;
31
32 struct myfile {
33         int fd;
34         unsigned long chars;
35         unsigned char buffer[8192];
36 };
37
38 static FILE *create_file(const char *suffix)
39 {
40         static char filename[PATH_MAX];
41         unsigned len;
42
43         len = snprintf(filename, PATH_MAX, "%s.%s", base_name, suffix);
44         if (len >= PATH_MAX)
45                 die("you wascally wabbit, you");
46         return fopen(filename, "w");
47 }
48
49 static unsigned long fwrite_compressed(void *in, unsigned long size, FILE *f)
50 {
51         z_stream stream;
52         unsigned long maxsize;
53         void *out;
54
55         memset(&stream, 0, sizeof(stream));
56         deflateInit(&stream, Z_DEFAULT_COMPRESSION);
57         maxsize = deflateBound(&stream, size);
58         out = xmalloc(maxsize);
59
60         /* Compress it */
61         stream.next_in = in;
62         stream.avail_in = size;
63
64         stream.next_out = out;
65         stream.avail_out = maxsize;
66
67         while (deflate(&stream, Z_FINISH) == Z_OK)
68                 /* nothing */;
69         deflateEnd(&stream);
70
71         size = stream.total_out;
72         fwrite(out, size, 1, f);
73         free(out);
74         return size;
75 }
76
77 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
78 {
79         unsigned long othersize, delta_size;
80         char type[10];
81         void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
82         void *delta_buf;
83
84         if (!otherbuf)
85                 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
86         delta_buf = diff_delta(otherbuf, othersize,
87                                buf, size, &delta_size, ~0UL);
88         if (!delta_buf || delta_size != entry->delta_size)
89                 die("delta size changed");
90         free(buf);
91         free(otherbuf);
92         return delta_buf;
93 }
94
95 static unsigned long write_object(FILE *f, struct object_entry *entry)
96 {
97         unsigned long size;
98         char type[10];
99         void *buf = read_sha1_file(entry->sha1, type, &size);
100         char header[25];
101         unsigned hdrlen, datalen;
102
103         if (!buf)
104                 die("unable to read %s", sha1_to_hex(entry->sha1));
105         if (size != entry->size)
106                 die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
107
108         /*
109          * The object header is a byte of 'type' followed by four bytes of
110          * length, except for deltas that has the 20 bytes of delta sha
111          * instead.
112          */
113         header[0] = ".CTB"[entry->type];
114         hdrlen = 5;
115         if (entry->delta) {
116                 header[0] = 'D';
117                 memcpy(header+5, entry->delta, 20);
118                 buf = delta_against(buf, size, entry);
119                 size = entry->delta_size;
120                 hdrlen = 25;
121         }
122         datalen = htonl(size);
123         memcpy(header+1, &datalen, 4);
124         fwrite(header, hdrlen, 1, f);
125         datalen = fwrite_compressed(buf, size, f);
126         free(buf);
127         return hdrlen + datalen;
128 }
129
130 static void write_pack_file(void)
131 {
132         int i;
133         FILE *f = create_file("pack");
134         unsigned long offset = 0;
135         unsigned long mb;
136
137         for (i = 0; i < nr_objects; i++) {
138                 struct object_entry *entry = objects + i;
139                 entry->offset = offset;
140                 offset += write_object(f, entry);
141         }
142         fclose(f);
143         mb = offset >> 20;
144         offset &= 0xfffff;
145 }
146
147 static void write_index_file(void)
148 {
149         int i;
150         FILE *f = create_file("idx");
151         struct object_entry **list = sorted_by_sha;
152         struct object_entry **last = list + nr_objects;
153         unsigned int array[256];
154
155         /*
156          * Write the first-level table (the list is sorted,
157          * but we use a 256-entry lookup to be able to avoid
158          * having to do eight extra binary search iterations)
159          */
160         for (i = 0; i < 256; i++) {
161                 struct object_entry **next = list;
162                 while (next < last) {
163                         struct object_entry *entry = *next;
164                         if (entry->sha1[0] != i)
165                                 break;
166                         next++;
167                 }
168                 array[i] = htonl(next - sorted_by_sha);
169                 list = next;
170         }
171         fwrite(array, 256, sizeof(int), f);
172
173         /*
174          * Write the actual SHA1 entries..
175          */
176         list = sorted_by_sha;
177         for (i = 0; i < nr_objects; i++) {
178                 struct object_entry *entry = *list++;
179                 unsigned int offset = htonl(entry->offset);
180                 fwrite(&offset, 4, 1, f);
181                 fwrite(entry->sha1, 20, 1, f);
182         }
183         fclose(f);
184 }
185
186 static void add_object_entry(unsigned char *sha1, unsigned int hash)
187 {
188         unsigned int idx = nr_objects;
189         struct object_entry *entry;
190
191         if (idx >= nr_alloc) {
192                 unsigned int needed = (idx + 1024) * 3 / 2;
193                 objects = xrealloc(objects, needed * sizeof(*entry));
194                 nr_alloc = needed;
195         }
196         entry = objects + idx;
197         memset(entry, 0, sizeof(*entry));
198         memcpy(entry->sha1, sha1, 20);
199         entry->hash = hash;
200         nr_objects = idx+1;
201 }
202
203 static void check_object(struct object_entry *entry)
204 {
205         char buffer[128];
206         char type[10];
207         unsigned long mapsize;
208         z_stream stream;
209         void *map;
210
211         map = map_sha1_file(entry->sha1, &mapsize);
212         if (!map)
213                 die("unable to map %s", sha1_to_hex(entry->sha1));
214         if (unpack_sha1_header(&stream, map, mapsize, buffer, sizeof(buffer)) < 0)
215                 die("unable to unpack %s header", sha1_to_hex(entry->sha1));
216         munmap(map, mapsize);
217         if (parse_sha1_header(buffer, type, &entry->size) < 0)
218                 die("unable to parse %s header", sha1_to_hex(entry->sha1));
219         if (!strcmp(type, "commit")) {
220                 entry->type = OBJ_COMMIT;
221         } else if (!strcmp(type, "tree")) {
222                 entry->type = OBJ_TREE;
223         } else if (!strcmp(type, "blob")) {
224                 entry->type = OBJ_BLOB;
225         } else
226                 die("unable to pack object %s of type %s", sha1_to_hex(entry->sha1), type);
227 }
228
229 static 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
238 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
239
240 static entry_sort_t current_sort;
241
242 static 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
249 static 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
261 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
262 {
263         return memcmp(a->sha1, b->sha1, 20);
264 }
265
266 static 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;
272         if (a->hash < b->hash)
273                 return -1;
274         if (a->hash > b->hash)
275                 return 1;
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
283 struct unpacked {
284         struct object_entry *entry;
285         void *data;
286 };
287
288 /*
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.
295  */
296 static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
297 {
298         struct object_entry *cur_entry = cur->entry;
299         struct object_entry *old_entry = old->entry;
300         unsigned long size, oldsize, delta_size, sizediff;
301         long max_size;
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
308         size = cur_entry->size;
309         if (size < 50)
310                 return -1;
311         oldsize = old_entry->size;
312         sizediff = oldsize > size ? oldsize - size : size - oldsize;
313         if (sizediff > size / 8)
314                 return -1;
315         if (old_entry->depth >= max_depth)
316                 return 0;
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          */
325         max_size = size / 2 - 20;
326         if (cur_entry->delta)
327                 max_size = cur_entry->delta_size-1;
328         if (sizediff >= max_size)
329                 return -1;
330         delta_buf = diff_delta(old->data, oldsize,
331                                cur->data, size, &delta_size, max_size);
332         if (!delta_buf)
333                 return 0;
334         cur_entry->delta = old_entry;
335         cur_entry->delta_size = delta_size;
336         cur_entry->depth = old_entry->depth + 1;
337         free(delta_buf);
338         return 0;
339 }
340
341 static void find_deltas(struct object_entry **list, int window, int depth)
342 {
343         int i, idx;
344         unsigned int array_size = window * sizeof(struct unpacked);
345         struct unpacked *array = xmalloc(array_size);
346
347         memset(array, 0, array_size);
348         i = nr_objects;
349         idx = 0;
350         while (--i >= 0) {
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);
362                 j = window;
363                 while (--j > 0) {
364                         unsigned int other_idx = idx + j;
365                         struct unpacked *m;
366                         if (other_idx >= window)
367                                 other_idx -= window;
368                         m = array + other_idx;
369                         if (!m->entry)
370                                 break;
371                         if (try_delta(n, m, depth) < 0)
372                                 break;
373                 }
374                 idx++;
375                 if (idx >= window)
376                         idx = 0;
377         }
378 }
379
380 int main(int argc, char **argv)
381 {
382         char line[PATH_MAX + 20];
383         int window = 10, depth = 10;
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;
392                                 window = strtoul(arg+9, &end, 0);
393                                 if (!arg[9] || *end)
394                                         usage(pack_usage);
395                                 continue;
396                         }
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                         }
404                         usage(pack_usage);
405                 }
406                 if (base_name)
407                         usage(pack_usage);
408                 base_name = arg;
409         }
410
411         if (!base_name)
412                 usage(pack_usage);
413
414         while (fgets(line, sizeof(line), stdin) != NULL) {
415                 unsigned int hash;
416                 char *p;
417                 unsigned char sha1[20];
418
419                 if (get_sha1_hex(line, sha1))
420                         die("expected sha1, got garbage");
421                 hash = 0;
422                 p = line+40;
423                 while (*p) {
424                         unsigned char c = *p++;
425                         if (isspace(c))
426                                 continue;
427                         hash = hash * 11 + c;
428                 }
429                 add_object_entry(sha1, hash);
430         }
431         get_object_details();
432
433         printf("Packing %d objects\n", nr_objects);
434
435         sorted_by_sha = create_sorted_list(sha1_sort);
436         sorted_by_type = create_sorted_list(type_size_sort);
437         if (window && depth)
438                 find_deltas(sorted_by_type, window+1, depth);
439
440         write_pack_file();
441         write_index_file();
442         return 0;
443 }