git-pack-objects: write the pack files with a SHA1 csum
[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"
c38138cd 5#include "csum-file.h"
c323ac7d 6
d116a45a 7static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] base-name < object-list";
c323ac7d
LT
8
9enum 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
17struct object_entry {
18 unsigned char sha1[20];
19 unsigned long size;
20 unsigned long offset;
21 unsigned int depth;
27225f2e 22 unsigned int hash;
c323ac7d
LT
23 enum object_type type;
24 unsigned long delta_size;
25 struct object_entry *delta;
26};
27
28static struct object_entry **sorted_by_sha, **sorted_by_type;
29static struct object_entry *objects = NULL;
30static int nr_objects = 0, nr_alloc = 0;
c323ac7d
LT
31static const char *base_name;
32
c323ac7d
LT
33static 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));
8ee378a0
JH
42 delta_buf = diff_delta(otherbuf, othersize,
43 buf, size, &delta_size, ~0UL);
75c42d8c 44 if (!delta_buf || delta_size != entry->delta_size)
c323ac7d
LT
45 die("delta size changed");
46 free(buf);
47 free(otherbuf);
48 return delta_buf;
49}
50
c38138cd 51static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
c323ac7d
LT
52{
53 unsigned long size;
54 char type[10];
55 void *buf = read_sha1_file(entry->sha1, type, &size);
c4fb06c0 56 char header[25];
c323ac7d
LT
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];
c323ac7d
LT
70 hdrlen = 5;
71 if (entry->delta) {
d38c3721 72 header[0] = 'D';
c4fb06c0 73 memcpy(header+5, entry->delta, 20);
c323ac7d
LT
74 buf = delta_against(buf, size, entry);
75 size = entry->delta_size;
c4fb06c0 76 hdrlen = 25;
c323ac7d 77 }
c4fb06c0
LT
78 datalen = htonl(size);
79 memcpy(header+1, &datalen, 4);
c38138cd
LT
80 sha1write(f, header, hdrlen);
81 datalen = sha1write_compressed(f, buf, size);
c323ac7d
LT
82 free(buf);
83 return hdrlen + datalen;
84}
85
86static void write_pack_file(void)
87{
88 int i;
c38138cd 89 struct sha1file *f = sha1create("%s.%s", base_name, "pack");
c323ac7d
LT
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 }
c38138cd 98 sha1close(f);
c323ac7d
LT
99 mb = offset >> 20;
100 offset &= 0xfffff;
101}
102
103static void write_index_file(void)
104{
105 int i;
c38138cd 106 struct sha1file *f = sha1create("%s.%s", base_name, "idx");
c323ac7d
LT
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 }
c38138cd 127 sha1write(f, array, 256 * sizeof(int));
c323ac7d
LT
128
129 /*
130 * Write the actual SHA1 entries..
131 */
132 list = sorted_by_sha;
49397104 133 for (i = 0; i < nr_objects; i++) {
c323ac7d
LT
134 struct object_entry *entry = *list++;
135 unsigned int offset = htonl(entry->offset);
c38138cd
LT
136 sha1write(f, &offset, 4);
137 sha1write(f, entry->sha1, 20);
c323ac7d 138 }
c38138cd 139 sha1close(f);
c323ac7d
LT
140}
141
27225f2e 142static void add_object_entry(unsigned char *sha1, unsigned int hash)
c323ac7d
LT
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);
27225f2e 155 entry->hash = hash;
c323ac7d
LT
156 nr_objects = idx+1;
157}
158
159static 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
185static 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
194typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
195
196static entry_sort_t current_sort;
197
198static 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
205static 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
217static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
218{
219 return memcmp(a->sha1, b->sha1, 20);
220}
221
222static 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;
27225f2e
LT
228 if (a->hash < b->hash)
229 return -1;
230 if (a->hash > b->hash)
231 return 1;
c323ac7d
LT
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
239struct unpacked {
240 struct object_entry *entry;
241 void *data;
242};
243
244/*
521a4f4c
LT
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.
c323ac7d 251 */
d116a45a 252static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
c323ac7d
LT
253{
254 struct object_entry *cur_entry = cur->entry;
255 struct object_entry *old_entry = old->entry;
521a4f4c 256 unsigned long size, oldsize, delta_size, sizediff;
75c42d8c 257 long max_size;
c323ac7d
LT
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
c323ac7d 264 size = cur_entry->size;
75c42d8c
LT
265 if (size < 50)
266 return -1;
c323ac7d 267 oldsize = old_entry->size;
521a4f4c
LT
268 sizediff = oldsize > size ? oldsize - size : size - oldsize;
269 if (sizediff > size / 8)
c323ac7d 270 return -1;
d116a45a
LT
271 if (old_entry->depth >= max_depth)
272 return 0;
c323ac7d
LT
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 */
75c42d8c
LT
281 max_size = size / 2 - 20;
282 if (cur_entry->delta)
283 max_size = cur_entry->delta_size-1;
27225f2e
LT
284 if (sizediff >= max_size)
285 return -1;
8ee378a0
JH
286 delta_buf = diff_delta(old->data, oldsize,
287 cur->data, size, &delta_size, max_size);
c323ac7d 288 if (!delta_buf)
75c42d8c
LT
289 return 0;
290 cur_entry->delta = old_entry;
291 cur_entry->delta_size = delta_size;
d116a45a 292 cur_entry->depth = old_entry->depth + 1;
c323ac7d 293 free(delta_buf);
eb41ab11 294 return 0;
c323ac7d
LT
295}
296
d116a45a 297static void find_deltas(struct object_entry **list, int window, int depth)
c323ac7d 298{
521a4f4c 299 int i, idx;
c323ac7d
LT
300 unsigned int array_size = window * sizeof(struct unpacked);
301 struct unpacked *array = xmalloc(array_size);
302
303 memset(array, 0, array_size);
521a4f4c
LT
304 i = nr_objects;
305 idx = 0;
306 while (--i >= 0) {
c323ac7d
LT
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);
78817c15
LT
318 j = window;
319 while (--j > 0) {
320 unsigned int other_idx = idx + j;
c323ac7d 321 struct unpacked *m;
78817c15
LT
322 if (other_idx >= window)
323 other_idx -= window;
c323ac7d
LT
324 m = array + other_idx;
325 if (!m->entry)
326 break;
d116a45a 327 if (try_delta(n, m, depth) < 0)
c323ac7d
LT
328 break;
329 }
521a4f4c
LT
330 idx++;
331 if (idx >= window)
332 idx = 0;
c323ac7d
LT
333 }
334}
335
336int main(int argc, char **argv)
337{
27225f2e 338 char line[PATH_MAX + 20];
d116a45a 339 int window = 10, depth = 10;
c323ac7d
LT
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;
f846bbff 348 window = strtoul(arg+9, &end, 0);
c323ac7d
LT
349 if (!arg[9] || *end)
350 usage(pack_usage);
351 continue;
352 }
d116a45a
LT
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 }
c323ac7d
LT
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) {
27225f2e
LT
371 unsigned int hash;
372 char *p;
c323ac7d 373 unsigned char sha1[20];
27225f2e 374
c323ac7d
LT
375 if (get_sha1_hex(line, sha1))
376 die("expected sha1, got garbage");
27225f2e
LT
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);
c323ac7d
LT
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);
d116a45a
LT
393 if (window && depth)
394 find_deltas(sorted_by_type, window+1, depth);
c323ac7d
LT
395
396 write_pack_file();
397 write_index_file();
398 return 0;
399}