7 static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] base-name < object-list";
14 OBJ_DELTA // NOTE! This is _not_ the same as a "delta" object in the filesystem
18 unsigned char sha1[20];
23 enum object_type type;
24 unsigned long delta_size;
25 struct object_entry *delta;
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;
33 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
35 unsigned long othersize, delta_size;
37 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
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");
51 static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
55 void *buf = read_sha1_file(entry->sha1, type, &size);
57 unsigned hdrlen, datalen;
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);
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
69 header[0] = ".CTB"[entry->type];
73 memcpy(header+5, entry->delta, 20);
74 buf = delta_against(buf, size, entry);
75 size = entry->delta_size;
78 datalen = htonl(size);
79 memcpy(header+1, &datalen, 4);
80 sha1write(f, header, hdrlen);
81 datalen = sha1write_compressed(f, buf, size);
83 return hdrlen + datalen;
86 static void write_pack_file(void)
89 struct sha1file *f = sha1create("%s.%s", base_name, "pack");
90 unsigned long offset = 0;
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);
103 static void write_index_file(void)
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];
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)
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)
124 array[i] = htonl(next - sorted_by_sha);
127 sha1write(f, array, 256 * sizeof(int));
130 * Write the actual SHA1 entries..
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);
142 static void add_object_entry(unsigned char *sha1, unsigned int hash)
144 unsigned int idx = nr_objects;
145 struct object_entry *entry;
147 if (idx >= nr_alloc) {
148 unsigned int needed = (idx + 1024) * 3 / 2;
149 objects = xrealloc(objects, needed * sizeof(*entry));
152 entry = objects + idx;
153 memset(entry, 0, sizeof(*entry));
154 memcpy(entry->sha1, sha1, 20);
159 static void check_object(struct object_entry *entry)
163 unsigned long mapsize;
167 map = map_sha1_file(entry->sha1, &mapsize);
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;
182 die("unable to pack object %s of type %s", sha1_to_hex(entry->sha1), type);
185 static void get_object_details(void)
188 struct object_entry *entry = objects;
190 for (i = 0; i < nr_objects; i++)
191 check_object(entry++);
194 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
196 static entry_sort_t current_sort;
198 static int sort_comparator(const void *_a, const void *_b)
200 struct object_entry *a = *(struct object_entry **)_a;
201 struct object_entry *b = *(struct object_entry **)_b;
202 return current_sort(a,b);
205 static struct object_entry **create_sorted_list(entry_sort_t sort)
207 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
210 for (i = 0; i < nr_objects; i++)
211 list[i] = objects + i;
213 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
217 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
219 return memcmp(a->sha1, b->sha1, 20);
222 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
224 if (a->type < b->type)
226 if (a->type > b->type)
228 if (a->hash < b->hash)
230 if (a->hash > b->hash)
232 if (a->size < b->size)
234 if (a->size > b->size)
236 return a < b ? -1 : (a > b);
240 struct object_entry *entry;
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
252 static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
254 struct object_entry *cur_entry = cur->entry;
255 struct object_entry *old_entry = old->entry;
256 unsigned long size, oldsize, delta_size, sizediff;
260 /* Don't bother doing diffs between different types */
261 if (cur_entry->type != old_entry->type)
264 size = cur_entry->size;
267 oldsize = old_entry->size;
268 sizediff = oldsize > size ? oldsize - size : size - oldsize;
269 if (sizediff > size / 8)
271 if (old_entry->depth >= max_depth)
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
281 max_size = size / 2 - 20;
282 if (cur_entry->delta)
283 max_size = cur_entry->delta_size-1;
284 if (sizediff >= max_size)
286 delta_buf = diff_delta(old->data, oldsize,
287 cur->data, size, &delta_size, max_size);
290 cur_entry->delta = old_entry;
291 cur_entry->delta_size = delta_size;
292 cur_entry->depth = old_entry->depth + 1;
297 static void find_deltas(struct object_entry **list, int window, int depth)
300 unsigned int array_size = window * sizeof(struct unpacked);
301 struct unpacked *array = xmalloc(array_size);
303 memset(array, 0, array_size);
307 struct object_entry *entry = list[i];
308 struct unpacked *n = array + idx;
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);
320 unsigned int other_idx = idx + j;
322 if (other_idx >= window)
324 m = array + other_idx;
327 if (try_delta(n, m, depth) < 0)
336 int main(int argc, char **argv)
338 char line[PATH_MAX + 20];
339 int window = 10, depth = 10;
342 for (i = 1; i < argc; i++) {
343 const char *arg = argv[i];
346 if (!strncmp("--window=", arg, 9)) {
348 window = strtoul(arg+9, &end, 0);
353 if (!strncmp("--depth=", arg, 8)) {
355 depth = strtoul(arg+8, &end, 0);
370 while (fgets(line, sizeof(line), stdin) != NULL) {
373 unsigned char sha1[20];
375 if (get_sha1_hex(line, sha1))
376 die("expected sha1, got garbage");
380 unsigned char c = *p++;
383 hash = hash * 11 + c;
385 add_object_entry(sha1, hash);
387 get_object_details();
389 printf("Packing %d objects\n", nr_objects);
391 sorted_by_sha = create_sorted_list(sha1_sort);
392 sorted_by_type = create_sorted_list(type_size_sort);
394 find_deltas(sorted_by_type, window+1, depth);