7 static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] {--stdout | base-name} < object-list";
10 * The object type is a single-character shorthand:
18 unsigned char sha1[20];
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;
32 static unsigned char pack_file_sha1[20];
34 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
36 unsigned long othersize, delta_size;
38 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
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");
52 static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
56 void *buf = read_sha1_file(entry->sha1, type, &size);
58 unsigned hdrlen, datalen;
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);
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
70 header[0] = entry->type;
74 memcpy(header+5, entry->delta, 20);
75 buf = delta_against(buf, size, entry);
76 size = entry->delta_size;
79 datalen = htonl(size);
80 memcpy(header+1, &datalen, 4);
81 sha1write(f, header, hdrlen);
82 datalen = sha1write_compressed(f, buf, size);
84 return hdrlen + datalen;
87 static void write_pack_file(void)
91 unsigned long offset = 0;
95 f = sha1fd(1, "<stdout>");
97 f = sha1create("%s.%s", base_name, "pack");
98 for (i = 0; i < nr_objects; i++) {
99 struct object_entry *entry = objects + i;
100 entry->offset = offset;
101 offset += write_object(f, entry);
103 sha1close(f, pack_file_sha1, 1);
108 static void write_index_file(void)
111 struct sha1file *f = sha1create("%s.%s", base_name, "idx");
112 struct object_entry **list = sorted_by_sha;
113 struct object_entry **last = list + nr_objects;
114 unsigned int array[256];
117 * Write the first-level table (the list is sorted,
118 * but we use a 256-entry lookup to be able to avoid
119 * having to do eight extra binary search iterations).
121 for (i = 0; i < 256; i++) {
122 struct object_entry **next = list;
123 while (next < last) {
124 struct object_entry *entry = *next;
125 if (entry->sha1[0] != i)
129 array[i] = htonl(next - sorted_by_sha);
132 sha1write(f, array, 256 * sizeof(int));
135 * Write the actual SHA1 entries..
137 list = sorted_by_sha;
138 for (i = 0; i < nr_objects; i++) {
139 struct object_entry *entry = *list++;
140 unsigned int offset = htonl(entry->offset);
141 sha1write(f, &offset, 4);
142 sha1write(f, entry->sha1, 20);
144 sha1write(f, pack_file_sha1, 20);
145 sha1close(f, NULL, 1);
148 static void add_object_entry(unsigned char *sha1, unsigned int hash)
150 unsigned int idx = nr_objects;
151 struct object_entry *entry;
153 if (idx >= nr_alloc) {
154 unsigned int needed = (idx + 1024) * 3 / 2;
155 objects = xrealloc(objects, needed * sizeof(*entry));
158 entry = objects + idx;
159 memset(entry, 0, sizeof(*entry));
160 memcpy(entry->sha1, sha1, 20);
165 static void check_object(struct object_entry *entry)
169 if (!sha1_object_info(entry->sha1, type, &entry->size)) {
170 if (!strcmp(type, "commit")) {
172 } else if (!strcmp(type, "tree")) {
174 } else if (!strcmp(type, "blob")) {
176 } else if (!strcmp(type, "tag")) {
179 die("unable to pack object %s of type %s",
180 sha1_to_hex(entry->sha1), type);
183 die("unable to get type of object %s",
184 sha1_to_hex(entry->sha1));
187 static void get_object_details(void)
190 struct object_entry *entry = objects;
192 for (i = 0; i < nr_objects; i++)
193 check_object(entry++);
196 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
198 static entry_sort_t current_sort;
200 static int sort_comparator(const void *_a, const void *_b)
202 struct object_entry *a = *(struct object_entry **)_a;
203 struct object_entry *b = *(struct object_entry **)_b;
204 return current_sort(a,b);
207 static struct object_entry **create_sorted_list(entry_sort_t sort)
209 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
212 for (i = 0; i < nr_objects; i++)
213 list[i] = objects + i;
215 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
219 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
221 return memcmp(a->sha1, b->sha1, 20);
224 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
226 if (a->type < b->type)
228 if (a->type > b->type)
230 if (a->hash < b->hash)
232 if (a->hash > b->hash)
234 if (a->size < b->size)
236 if (a->size > b->size)
238 return a < b ? -1 : (a > b);
242 struct object_entry *entry;
247 * We search for deltas _backwards_ in a list sorted by type and
248 * by size, so that we see progressively smaller and smaller files.
249 * That's because we prefer deltas to be from the bigger file
250 * to the smaller - deletes are potentially cheaper, but perhaps
251 * more importantly, the bigger file is likely the more recent
254 static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
256 struct object_entry *cur_entry = cur->entry;
257 struct object_entry *old_entry = old->entry;
258 unsigned long size, oldsize, delta_size, sizediff;
262 /* Don't bother doing diffs between different types */
263 if (cur_entry->type != old_entry->type)
266 size = cur_entry->size;
269 oldsize = old_entry->size;
270 sizediff = oldsize > size ? oldsize - size : size - oldsize;
271 if (sizediff > size / 8)
273 if (old_entry->depth >= max_depth)
279 * We always delta from the bigger to the smaller, since that's
280 * more space-efficient (deletes don't have to say _what_ they
283 max_size = size / 2 - 20;
284 if (cur_entry->delta)
285 max_size = cur_entry->delta_size-1;
286 if (sizediff >= max_size)
288 delta_buf = diff_delta(old->data, oldsize,
289 cur->data, size, &delta_size, max_size);
292 cur_entry->delta = old_entry;
293 cur_entry->delta_size = delta_size;
294 cur_entry->depth = old_entry->depth + 1;
299 static void find_deltas(struct object_entry **list, int window, int depth)
302 unsigned int array_size = window * sizeof(struct unpacked);
303 struct unpacked *array = xmalloc(array_size);
305 memset(array, 0, array_size);
309 struct object_entry *entry = list[i];
310 struct unpacked *n = array + idx;
317 n->data = read_sha1_file(entry->sha1, type, &size);
318 if (size != entry->size)
319 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
322 unsigned int other_idx = idx + j;
324 if (other_idx >= window)
326 m = array + other_idx;
329 if (try_delta(n, m, depth) < 0)
338 int main(int argc, char **argv)
340 char line[PATH_MAX + 20];
341 int window = 10, depth = 10, pack_to_stdout = 0;
344 for (i = 1; i < argc; i++) {
345 const char *arg = argv[i];
348 if (!strncmp("--window=", arg, 9)) {
350 window = strtoul(arg+9, &end, 0);
355 if (!strncmp("--depth=", arg, 8)) {
357 depth = strtoul(arg+8, &end, 0);
362 if (!strcmp("--stdout", arg)) {
373 if (pack_to_stdout != !base_name)
376 while (fgets(line, sizeof(line), stdin) != NULL) {
379 unsigned char sha1[20];
381 if (get_sha1_hex(line, sha1))
382 die("expected sha1, got garbage");
386 unsigned char c = *p++;
389 hash = hash * 11 + c;
391 add_object_entry(sha1, hash);
393 get_object_details();
395 fprintf(stderr, "Packing %d objects\n", nr_objects);
397 sorted_by_sha = create_sorted_list(sha1_sort);
398 sorted_by_type = create_sorted_list(type_size_sort);
400 find_deltas(sorted_by_type, window+1, depth);