git-pack-objects: write the pack files with a SHA1 csum
[git.git] / pack-objects.c
1 #include <ctype.h>
2 #include "cache.h"
3 #include "object.h"
4 #include "delta.h"
5 #include "csum-file.h"
6
7 static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] base-name < object-list";
8
9 enum object_type {
10         OBJ_NONE,
11         OBJ_COMMIT,
12         OBJ_TREE,
13         OBJ_BLOB,
14         OBJ_DELTA       // NOTE! This is _not_ the same as a "delta" object in the filesystem
15 };
16
17 struct object_entry {
18         unsigned char sha1[20];
19         unsigned long size;
20         unsigned long offset;
21         unsigned int depth;
22         unsigned int hash;
23         enum object_type type;
24         unsigned long delta_size;
25         struct object_entry *delta;
26 };
27
28 static struct object_entry **sorted_by_sha, **sorted_by_type;
29 static struct object_entry *objects = NULL;
30 static int nr_objects = 0, nr_alloc = 0;
31 static const char *base_name;
32
33 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
34 {
35         unsigned long othersize, delta_size;
36         char type[10];
37         void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
38         void *delta_buf;
39
40         if (!otherbuf)
41                 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
42         delta_buf = diff_delta(otherbuf, othersize,
43                                buf, size, &delta_size, ~0UL);
44         if (!delta_buf || delta_size != entry->delta_size)
45                 die("delta size changed");
46         free(buf);
47         free(otherbuf);
48         return delta_buf;
49 }
50
51 static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
52 {
53         unsigned long size;
54         char type[10];
55         void *buf = read_sha1_file(entry->sha1, type, &size);
56         char header[25];
57         unsigned hdrlen, datalen;
58
59         if (!buf)
60                 die("unable to read %s", sha1_to_hex(entry->sha1));
61         if (size != entry->size)
62                 die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
63
64         /*
65          * The object header is a byte of 'type' followed by four bytes of
66          * length, except for deltas that has the 20 bytes of delta sha
67          * instead.
68          */
69         header[0] = ".CTB"[entry->type];
70         hdrlen = 5;
71         if (entry->delta) {
72                 header[0] = 'D';
73                 memcpy(header+5, entry->delta, 20);
74                 buf = delta_against(buf, size, entry);
75                 size = entry->delta_size;
76                 hdrlen = 25;
77         }
78         datalen = htonl(size);
79         memcpy(header+1, &datalen, 4);
80         sha1write(f, header, hdrlen);
81         datalen = sha1write_compressed(f, buf, size);
82         free(buf);
83         return hdrlen + datalen;
84 }
85
86 static void write_pack_file(void)
87 {
88         int i;
89         struct sha1file *f = sha1create("%s.%s", base_name, "pack");
90         unsigned long offset = 0;
91         unsigned long mb;
92
93         for (i = 0; i < nr_objects; i++) {
94                 struct object_entry *entry = objects + i;
95                 entry->offset = offset;
96                 offset += write_object(f, entry);
97         }
98         sha1close(f);
99         mb = offset >> 20;
100         offset &= 0xfffff;
101 }
102
103 static void write_index_file(void)
104 {
105         int i;
106         struct sha1file *f = sha1create("%s.%s", base_name, "idx");
107         struct object_entry **list = sorted_by_sha;
108         struct object_entry **last = list + nr_objects;
109         unsigned int array[256];
110
111         /*
112          * Write the first-level table (the list is sorted,
113          * but we use a 256-entry lookup to be able to avoid
114          * having to do eight extra binary search iterations)
115          */
116         for (i = 0; i < 256; i++) {
117                 struct object_entry **next = list;
118                 while (next < last) {
119                         struct object_entry *entry = *next;
120                         if (entry->sha1[0] != i)
121                                 break;
122                         next++;
123                 }
124                 array[i] = htonl(next - sorted_by_sha);
125                 list = next;
126         }
127         sha1write(f, array, 256 * sizeof(int));
128
129         /*
130          * Write the actual SHA1 entries..
131          */
132         list = sorted_by_sha;
133         for (i = 0; i < nr_objects; i++) {
134                 struct object_entry *entry = *list++;
135                 unsigned int offset = htonl(entry->offset);
136                 sha1write(f, &offset, 4);
137                 sha1write(f, entry->sha1, 20);
138         }
139         sha1close(f);
140 }
141
142 static void add_object_entry(unsigned char *sha1, unsigned int hash)
143 {
144         unsigned int idx = nr_objects;
145         struct object_entry *entry;
146
147         if (idx >= nr_alloc) {
148                 unsigned int needed = (idx + 1024) * 3 / 2;
149                 objects = xrealloc(objects, needed * sizeof(*entry));
150                 nr_alloc = needed;
151         }
152         entry = objects + idx;
153         memset(entry, 0, sizeof(*entry));
154         memcpy(entry->sha1, sha1, 20);
155         entry->hash = hash;
156         nr_objects = idx+1;
157 }
158
159 static void check_object(struct object_entry *entry)
160 {
161         char buffer[128];
162         char type[10];
163         unsigned long mapsize;
164         z_stream stream;
165         void *map;
166
167         map = map_sha1_file(entry->sha1, &mapsize);
168         if (!map)
169                 die("unable to map %s", sha1_to_hex(entry->sha1));
170         if (unpack_sha1_header(&stream, map, mapsize, buffer, sizeof(buffer)) < 0)
171                 die("unable to unpack %s header", sha1_to_hex(entry->sha1));
172         munmap(map, mapsize);
173         if (parse_sha1_header(buffer, type, &entry->size) < 0)
174                 die("unable to parse %s header", sha1_to_hex(entry->sha1));
175         if (!strcmp(type, "commit")) {
176                 entry->type = OBJ_COMMIT;
177         } else if (!strcmp(type, "tree")) {
178                 entry->type = OBJ_TREE;
179         } else if (!strcmp(type, "blob")) {
180                 entry->type = OBJ_BLOB;
181         } else
182                 die("unable to pack object %s of type %s", sha1_to_hex(entry->sha1), type);
183 }
184
185 static void get_object_details(void)
186 {
187         int i;
188         struct object_entry *entry = objects;
189
190         for (i = 0; i < nr_objects; i++)
191                 check_object(entry++);
192 }
193
194 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
195
196 static entry_sort_t current_sort;
197
198 static int sort_comparator(const void *_a, const void *_b)
199 {
200         struct object_entry *a = *(struct object_entry **)_a;
201         struct object_entry *b = *(struct object_entry **)_b;
202         return current_sort(a,b);
203 }
204
205 static struct object_entry **create_sorted_list(entry_sort_t sort)
206 {
207         struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
208         int i;
209
210         for (i = 0; i < nr_objects; i++)
211                 list[i] = objects + i;
212         current_sort = sort;
213         qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
214         return list;
215 }
216
217 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
218 {
219         return memcmp(a->sha1, b->sha1, 20);
220 }
221
222 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
223 {
224         if (a->type < b->type)
225                 return -1;
226         if (a->type > b->type)
227                 return 1;
228         if (a->hash < b->hash)
229                 return -1;
230         if (a->hash > b->hash)
231                 return 1;
232         if (a->size < b->size)
233                 return -1;
234         if (a->size > b->size)
235                 return 1;
236         return a < b ? -1 : (a > b);
237 }
238
239 struct unpacked {
240         struct object_entry *entry;
241         void *data;
242 };
243
244 /*
245  * We search for deltas _backwards_ in a list sorted by type and
246  * by size, so that we see progressively smaller and smaller files.
247  * That's because we prefer deltas to be from the bigger file
248  * to the smaller - deletes are potentially cheaper, but perhaps
249  * more importantly, the bigger file is likely the more recent
250  * one.
251  */
252 static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
253 {
254         struct object_entry *cur_entry = cur->entry;
255         struct object_entry *old_entry = old->entry;
256         unsigned long size, oldsize, delta_size, sizediff;
257         long max_size;
258         void *delta_buf;
259
260         /* Don't bother doing diffs between different types */
261         if (cur_entry->type != old_entry->type)
262                 return -1;
263
264         size = cur_entry->size;
265         if (size < 50)
266                 return -1;
267         oldsize = old_entry->size;
268         sizediff = oldsize > size ? oldsize - size : size - oldsize;
269         if (sizediff > size / 8)
270                 return -1;
271         if (old_entry->depth >= max_depth)
272                 return 0;
273
274         /*
275          * NOTE!
276          *
277          * We always delta from the bigger to the smaller, since that's
278          * more space-efficient (deletes don't have to say _what_ they
279          * delete).
280          */
281         max_size = size / 2 - 20;
282         if (cur_entry->delta)
283                 max_size = cur_entry->delta_size-1;
284         if (sizediff >= max_size)
285                 return -1;
286         delta_buf = diff_delta(old->data, oldsize,
287                                cur->data, size, &delta_size, max_size);
288         if (!delta_buf)
289                 return 0;
290         cur_entry->delta = old_entry;
291         cur_entry->delta_size = delta_size;
292         cur_entry->depth = old_entry->depth + 1;
293         free(delta_buf);
294         return 0;
295 }
296
297 static void find_deltas(struct object_entry **list, int window, int depth)
298 {
299         int i, idx;
300         unsigned int array_size = window * sizeof(struct unpacked);
301         struct unpacked *array = xmalloc(array_size);
302
303         memset(array, 0, array_size);
304         i = nr_objects;
305         idx = 0;
306         while (--i >= 0) {
307                 struct object_entry *entry = list[i];
308                 struct unpacked *n = array + idx;
309                 unsigned long size;
310                 char type[10];
311                 int j;
312
313                 free(n->data);
314                 n->entry = entry;
315                 n->data = read_sha1_file(entry->sha1, type, &size);
316                 if (size != entry->size)
317                         die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
318                 j = window;
319                 while (--j > 0) {
320                         unsigned int other_idx = idx + j;
321                         struct unpacked *m;
322                         if (other_idx >= window)
323                                 other_idx -= window;
324                         m = array + other_idx;
325                         if (!m->entry)
326                                 break;
327                         if (try_delta(n, m, depth) < 0)
328                                 break;
329                 }
330                 idx++;
331                 if (idx >= window)
332                         idx = 0;
333         }
334 }
335
336 int main(int argc, char **argv)
337 {
338         char line[PATH_MAX + 20];
339         int window = 10, depth = 10;
340         int i;
341
342         for (i = 1; i < argc; i++) {
343                 const char *arg = argv[i];
344
345                 if (*arg == '-') {
346                         if (!strncmp("--window=", arg, 9)) {
347                                 char *end;
348                                 window = strtoul(arg+9, &end, 0);
349                                 if (!arg[9] || *end)
350                                         usage(pack_usage);
351                                 continue;
352                         }
353                         if (!strncmp("--depth=", arg, 8)) {
354                                 char *end;
355                                 depth = strtoul(arg+8, &end, 0);
356                                 if (!arg[8] || *end)
357                                         usage(pack_usage);
358                                 continue;
359                         }
360                         usage(pack_usage);
361                 }
362                 if (base_name)
363                         usage(pack_usage);
364                 base_name = arg;
365         }
366
367         if (!base_name)
368                 usage(pack_usage);
369
370         while (fgets(line, sizeof(line), stdin) != NULL) {
371                 unsigned int hash;
372                 char *p;
373                 unsigned char sha1[20];
374
375                 if (get_sha1_hex(line, sha1))
376                         die("expected sha1, got garbage");
377                 hash = 0;
378                 p = line+40;
379                 while (*p) {
380                         unsigned char c = *p++;
381                         if (isspace(c))
382                                 continue;
383                         hash = hash * 11 + c;
384                 }
385                 add_object_entry(sha1, hash);
386         }
387         get_object_details();
388
389         printf("Packing %d objects\n", nr_objects);
390
391         sorted_by_sha = create_sorted_list(sha1_sort);
392         sorted_by_type = create_sorted_list(type_size_sort);
393         if (window && depth)
394                 find_deltas(sorted_by_type, window+1, depth);
395
396         write_pack_file();
397         write_index_file();
398         return 0;
399 }