[PATCH] Add a bit of developer documentation to pull.h
[git.git] / pack-objects.c
blob8de7bd03444720b40fcad7c7102c989142b00c9c
1 #include "cache.h"
2 #include "object.h"
3 #include "delta.h"
5 static const char pack_usage[] = "git-pack-objects [--window=N] base-name < object-list";
7 enum object_type {
8 OBJ_NONE,
9 OBJ_COMMIT,
10 OBJ_TREE,
11 OBJ_BLOB,
12 OBJ_DELTA // NOTE! This is _not_ the same as a "delta" object in the filesystem
15 struct object_entry {
16 unsigned char sha1[20];
17 unsigned long size;
18 unsigned long offset;
19 unsigned int depth;
20 unsigned int flags;
21 enum object_type type;
22 unsigned long delta_size;
23 struct object_entry *delta;
26 static struct object_entry **sorted_by_sha, **sorted_by_type;
27 static struct object_entry *objects = NULL;
28 static int nr_objects = 0, nr_alloc = 0;
29 static int delta_window = 10;
30 static const char *base_name;
32 struct myfile {
33 int fd;
34 unsigned long chars;
35 unsigned char buffer[8192];
38 static FILE *create_file(const char *suffix)
40 static char filename[PATH_MAX];
41 unsigned len;
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");
49 static unsigned long fwrite_compressed(void *in, unsigned long size, FILE *f)
51 z_stream stream;
52 unsigned long maxsize;
53 void *out;
55 memset(&stream, 0, sizeof(stream));
56 deflateInit(&stream, Z_DEFAULT_COMPRESSION);
57 maxsize = deflateBound(&stream, size);
58 out = xmalloc(maxsize);
60 /* Compress it */
61 stream.next_in = in;
62 stream.avail_in = size;
64 stream.next_out = out;
65 stream.avail_out = maxsize;
67 while (deflate(&stream, Z_FINISH) == Z_OK)
68 /* nothing */;
69 deflateEnd(&stream);
71 size = stream.total_out;
72 fwrite(out, size, 1, f);
73 free(out);
74 return size;
77 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
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;
84 if (!otherbuf)
85 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
86 delta_buf = diff_delta(buf, size, otherbuf, othersize, &delta_size);
87 if (delta_size != entry->delta_size)
88 die("delta size changed");
89 free(buf);
90 free(otherbuf);
91 return delta_buf;
94 static unsigned long write_object(FILE *f, struct object_entry *entry)
96 unsigned long size;
97 char type[10];
98 void *buf = read_sha1_file(entry->sha1, type, &size);
99 char header[21];
100 unsigned hdrlen, datalen;
102 if (!buf)
103 die("unable to read %s", sha1_to_hex(entry->sha1));
104 if (size != entry->size)
105 die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
108 * The object header is a byte of 'type' followed by four bytes of
109 * length, except for deltas that has the 20 bytes of delta sha
110 * instead.
112 header[0] = ".CTB"[entry->type];
113 datalen = htonl(size);
114 memcpy(header+1, &datalen, 4);
115 hdrlen = 5;
116 if (entry->delta) {
117 header[0] = 'D';
118 memcpy(header+1, entry->delta, 20);
119 buf = delta_against(buf, size, entry);
120 size = entry->delta_size;
121 hdrlen = 21;
123 fwrite(header, hdrlen, 1, f);
124 datalen = fwrite_compressed(buf, size, f);
125 free(buf);
126 return hdrlen + datalen;
129 static void write_pack_file(void)
131 int i;
132 FILE *f = create_file("pack");
133 unsigned long offset = 0;
134 unsigned long mb;
136 for (i = 0; i < nr_objects; i++) {
137 struct object_entry *entry = objects + i;
138 entry->offset = offset;
139 offset += write_object(f, entry);
141 fclose(f);
142 mb = offset >> 20;
143 offset &= 0xfffff;
146 static void write_index_file(void)
148 int i;
149 FILE *f = create_file("idx");
150 struct object_entry **list = sorted_by_sha;
151 struct object_entry **last = list + nr_objects;
152 unsigned int array[256];
155 * Write the first-level table (the list is sorted,
156 * but we use a 256-entry lookup to be able to avoid
157 * having to do eight extra binary search iterations)
159 for (i = 0; i < 256; i++) {
160 struct object_entry **next = list;
161 while (next < last) {
162 struct object_entry *entry = *next;
163 if (entry->sha1[0] != i)
164 break;
165 next++;
167 array[i] = htonl(next - sorted_by_sha);
168 list = next;
170 fwrite(array, 256, sizeof(int), f);
173 * Write the actual SHA1 entries..
175 list = sorted_by_sha;
176 for (i = 0; i < nr_objects; i++) {
177 struct object_entry *entry = *list++;
178 unsigned int offset = htonl(entry->offset);
179 fwrite(&offset, 4, 1, f);
180 fwrite(entry->sha1, 20, 1, f);
182 fclose(f);
185 static void add_object_entry(unsigned char *sha1)
187 unsigned int idx = nr_objects;
188 struct object_entry *entry;
190 if (idx >= nr_alloc) {
191 unsigned int needed = (idx + 1024) * 3 / 2;
192 objects = xrealloc(objects, needed * sizeof(*entry));
193 nr_alloc = needed;
195 entry = objects + idx;
196 memset(entry, 0, sizeof(*entry));
197 memcpy(entry->sha1, sha1, 20);
198 nr_objects = idx+1;
201 static void check_object(struct object_entry *entry)
203 char buffer[128];
204 char type[10];
205 unsigned long mapsize;
206 z_stream stream;
207 void *map;
209 map = map_sha1_file(entry->sha1, &mapsize);
210 if (!map)
211 die("unable to map %s", sha1_to_hex(entry->sha1));
212 if (unpack_sha1_header(&stream, map, mapsize, buffer, sizeof(buffer)) < 0)
213 die("unable to unpack %s header", sha1_to_hex(entry->sha1));
214 munmap(map, mapsize);
215 if (parse_sha1_header(buffer, type, &entry->size) < 0)
216 die("unable to parse %s header", sha1_to_hex(entry->sha1));
217 if (!strcmp(type, "commit")) {
218 entry->type = OBJ_COMMIT;
219 } else if (!strcmp(type, "tree")) {
220 entry->type = OBJ_TREE;
221 } else if (!strcmp(type, "blob")) {
222 entry->type = OBJ_BLOB;
223 } else
224 die("unable to pack object %s of type %s", sha1_to_hex(entry->sha1), type);
227 static void get_object_details(void)
229 int i;
230 struct object_entry *entry = objects;
232 for (i = 0; i < nr_objects; i++)
233 check_object(entry++);
236 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
238 static entry_sort_t current_sort;
240 static int sort_comparator(const void *_a, const void *_b)
242 struct object_entry *a = *(struct object_entry **)_a;
243 struct object_entry *b = *(struct object_entry **)_b;
244 return current_sort(a,b);
247 static struct object_entry **create_sorted_list(entry_sort_t sort)
249 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
250 int i;
252 for (i = 0; i < nr_objects; i++)
253 list[i] = objects + i;
254 current_sort = sort;
255 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
256 return list;
259 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
261 return memcmp(a->sha1, b->sha1, 20);
264 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
266 if (a->type < b->type)
267 return -1;
268 if (a->type > b->type)
269 return 1;
270 if (a->size < b->size)
271 return -1;
272 if (a->size > b->size)
273 return 1;
274 return a < b ? -1 : (a > b);
277 struct unpacked {
278 struct object_entry *entry;
279 void *data;
283 * We search for deltas in a list sorted by type and by size, and
284 * walk the "old" chain backwards in the list, so if the type
285 * has changed or the size difference is too big, there's no point
286 * in even continuing the walk, since the other old objects are
287 * going to be even smaller or of a different type. So return -1
288 * once we determine that there's no point even trying.
290 static int try_delta(struct unpacked *cur, struct unpacked *old)
292 struct object_entry *cur_entry = cur->entry;
293 struct object_entry *old_entry = old->entry;
294 unsigned long size, oldsize, delta_size;
295 void *delta_buf;
297 /* Don't bother doing diffs between different types */
298 if (cur_entry->type != old_entry->type)
299 return -1;
301 /* Size is guaranteed to be larger than or equal to oldsize */
302 size = cur_entry->size;
303 oldsize = old_entry->size;
304 if (size - oldsize > oldsize / 4)
305 return -1;
308 * NOTE!
310 * We always delta from the bigger to the smaller, since that's
311 * more space-efficient (deletes don't have to say _what_ they
312 * delete).
314 delta_buf = diff_delta(cur->data, size, old->data, oldsize, &delta_size);
315 if (!delta_buf)
316 die("unable to create delta");
317 if (delta_size + 20 < size / 2) {
318 if (!cur_entry->delta || cur_entry->delta_size > delta_size) {
319 cur_entry->delta = old_entry;
320 cur_entry->delta_size = delta_size;
323 free(delta_buf);
326 static void find_deltas(struct object_entry **list, int window)
328 unsigned int i;
329 unsigned int array_size = window * sizeof(struct unpacked);
330 struct unpacked *array = xmalloc(array_size);
332 memset(array, 0, array_size);
333 for (i = 0; i < nr_objects; i++) {
334 unsigned int idx = i % window;
335 struct object_entry *entry = list[i];
336 struct unpacked *n = array + idx;
337 unsigned long size;
338 char type[10];
339 int j;
341 free(n->data);
342 n->entry = entry;
343 n->data = read_sha1_file(entry->sha1, type, &size);
344 if (size != entry->size)
345 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
346 for (j = 0; j < window; j++) {
347 unsigned int other_idx = idx - 1 - j;
348 struct unpacked *m;
349 if (other_idx < 0)
350 other_idx += window;
351 m = array + other_idx;
352 if (!m->entry)
353 break;
354 if (try_delta(n, m) < 0)
355 break;
360 int main(int argc, char **argv)
362 char line[128];
363 int i;
365 for (i = 1; i < argc; i++) {
366 const char *arg = argv[i];
368 if (*arg == '-') {
369 if (!strncmp("--window=", arg, 9)) {
370 char *end;
371 delta_window = strtoul(arg+9, &end, 0);
372 if (!arg[9] || *end)
373 usage(pack_usage);
374 continue;
376 usage(pack_usage);
378 if (base_name)
379 usage(pack_usage);
380 base_name = arg;
383 if (!base_name)
384 usage(pack_usage);
386 while (fgets(line, sizeof(line), stdin) != NULL) {
387 unsigned char sha1[20];
388 if (get_sha1_hex(line, sha1))
389 die("expected sha1, got garbage");
390 add_object_entry(sha1);
392 get_object_details();
394 printf("Packing %d objects\n", nr_objects);
396 sorted_by_sha = create_sorted_list(sha1_sort);
397 sorted_by_type = create_sorted_list(type_size_sort);
398 if (delta_window)
399 find_deltas(sorted_by_type, delta_window);
401 write_pack_file();
402 write_index_file();
403 return 0;