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