git-pack-objects: add "--stdout" flag to write the pack file to stdout
[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
d22b9290 7static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] {--stdout | base-name} < object-list";
c323ac7d 8
a69d0943
LT
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 */
c323ac7d
LT
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;
a69d0943 23 unsigned char type;
c323ac7d
LT
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 31static const char *base_name;
e1808845 32static unsigned char pack_file_sha1[20];
c323ac7d 33
c323ac7d
LT
34static 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));
8ee378a0
JH
43 delta_buf = diff_delta(otherbuf, othersize,
44 buf, size, &delta_size, ~0UL);
75c42d8c 45 if (!delta_buf || delta_size != entry->delta_size)
c323ac7d
LT
46 die("delta size changed");
47 free(buf);
48 free(otherbuf);
49 return delta_buf;
50}
51
c38138cd 52static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
c323ac7d
LT
53{
54 unsigned long size;
55 char type[10];
56 void *buf = read_sha1_file(entry->sha1, type, &size);
c4fb06c0 57 char header[25];
c323ac7d
LT
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 */
a69d0943 70 header[0] = entry->type;
c323ac7d
LT
71 hdrlen = 5;
72 if (entry->delta) {
d38c3721 73 header[0] = 'D';
c4fb06c0 74 memcpy(header+5, entry->delta, 20);
c323ac7d
LT
75 buf = delta_against(buf, size, entry);
76 size = entry->delta_size;
c4fb06c0 77 hdrlen = 25;
c323ac7d 78 }
c4fb06c0
LT
79 datalen = htonl(size);
80 memcpy(header+1, &datalen, 4);
c38138cd
LT
81 sha1write(f, header, hdrlen);
82 datalen = sha1write_compressed(f, buf, size);
c323ac7d
LT
83 free(buf);
84 return hdrlen + datalen;
85}
86
87static void write_pack_file(void)
88{
89 int i;
d22b9290 90 struct sha1file *f;
c323ac7d
LT
91 unsigned long offset = 0;
92 unsigned long mb;
93
d22b9290
LT
94 if (!base_name)
95 f = sha1fd(1, "<stdout>");
96 else
97 f = sha1create("%s.%s", base_name, "pack");
c323ac7d
LT
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);
102 }
e1808845 103 sha1close(f, pack_file_sha1, 1);
c323ac7d
LT
104 mb = offset >> 20;
105 offset &= 0xfffff;
106}
107
108static void write_index_file(void)
109{
110 int i;
c38138cd 111 struct sha1file *f = sha1create("%s.%s", base_name, "idx");
c323ac7d
LT
112 struct object_entry **list = sorted_by_sha;
113 struct object_entry **last = list + nr_objects;
114 unsigned int array[256];
115
116 /*
117 * Write the first-level table (the list is sorted,
118 * but we use a 256-entry lookup to be able to avoid
e1808845 119 * having to do eight extra binary search iterations).
c323ac7d
LT
120 */
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)
126 break;
127 next++;
128 }
129 array[i] = htonl(next - sorted_by_sha);
130 list = next;
131 }
c38138cd 132 sha1write(f, array, 256 * sizeof(int));
c323ac7d
LT
133
134 /*
135 * Write the actual SHA1 entries..
136 */
137 list = sorted_by_sha;
49397104 138 for (i = 0; i < nr_objects; i++) {
c323ac7d
LT
139 struct object_entry *entry = *list++;
140 unsigned int offset = htonl(entry->offset);
c38138cd
LT
141 sha1write(f, &offset, 4);
142 sha1write(f, entry->sha1, 20);
c323ac7d 143 }
e1808845
LT
144 sha1write(f, pack_file_sha1, 20);
145 sha1close(f, NULL, 1);
c323ac7d
LT
146}
147
27225f2e 148static void add_object_entry(unsigned char *sha1, unsigned int hash)
c323ac7d
LT
149{
150 unsigned int idx = nr_objects;
151 struct object_entry *entry;
152
153 if (idx >= nr_alloc) {
154 unsigned int needed = (idx + 1024) * 3 / 2;
155 objects = xrealloc(objects, needed * sizeof(*entry));
156 nr_alloc = needed;
157 }
158 entry = objects + idx;
159 memset(entry, 0, sizeof(*entry));
160 memcpy(entry->sha1, sha1, 20);
27225f2e 161 entry->hash = hash;
c323ac7d
LT
162 nr_objects = idx+1;
163}
164
165static void check_object(struct object_entry *entry)
166{
36e4d74a
JH
167 char type[20];
168
169 if (!sha1_object_info(entry->sha1, type, &entry->size)) {
170 if (!strcmp(type, "commit")) {
a69d0943 171 entry->type = 'C';
36e4d74a 172 } else if (!strcmp(type, "tree")) {
a69d0943 173 entry->type = 'T';
36e4d74a 174 } else if (!strcmp(type, "blob")) {
a69d0943
LT
175 entry->type = 'B';
176 } else if (!strcmp(type, "tag")) {
177 entry->type = 'G';
36e4d74a
JH
178 } else
179 die("unable to pack object %s of type %s",
180 sha1_to_hex(entry->sha1), type);
181 }
182 else
183 die("unable to get type of object %s",
184 sha1_to_hex(entry->sha1));
c323ac7d
LT
185}
186
187static void get_object_details(void)
188{
189 int i;
190 struct object_entry *entry = objects;
191
192 for (i = 0; i < nr_objects; i++)
193 check_object(entry++);
194}
195
196typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
197
198static entry_sort_t current_sort;
199
200static int sort_comparator(const void *_a, const void *_b)
201{
202 struct object_entry *a = *(struct object_entry **)_a;
203 struct object_entry *b = *(struct object_entry **)_b;
204 return current_sort(a,b);
205}
206
207static struct object_entry **create_sorted_list(entry_sort_t sort)
208{
209 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
210 int i;
211
212 for (i = 0; i < nr_objects; i++)
213 list[i] = objects + i;
214 current_sort = sort;
215 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
216 return list;
217}
218
219static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
220{
221 return memcmp(a->sha1, b->sha1, 20);
222}
223
224static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
225{
226 if (a->type < b->type)
227 return -1;
228 if (a->type > b->type)
229 return 1;
27225f2e
LT
230 if (a->hash < b->hash)
231 return -1;
232 if (a->hash > b->hash)
233 return 1;
c323ac7d
LT
234 if (a->size < b->size)
235 return -1;
236 if (a->size > b->size)
237 return 1;
238 return a < b ? -1 : (a > b);
239}
240
241struct unpacked {
242 struct object_entry *entry;
243 void *data;
244};
245
246/*
521a4f4c
LT
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
252 * one.
c323ac7d 253 */
d116a45a 254static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
c323ac7d
LT
255{
256 struct object_entry *cur_entry = cur->entry;
257 struct object_entry *old_entry = old->entry;
521a4f4c 258 unsigned long size, oldsize, delta_size, sizediff;
75c42d8c 259 long max_size;
c323ac7d
LT
260 void *delta_buf;
261
262 /* Don't bother doing diffs between different types */
263 if (cur_entry->type != old_entry->type)
264 return -1;
265
c323ac7d 266 size = cur_entry->size;
75c42d8c
LT
267 if (size < 50)
268 return -1;
c323ac7d 269 oldsize = old_entry->size;
521a4f4c
LT
270 sizediff = oldsize > size ? oldsize - size : size - oldsize;
271 if (sizediff > size / 8)
c323ac7d 272 return -1;
d116a45a
LT
273 if (old_entry->depth >= max_depth)
274 return 0;
c323ac7d
LT
275
276 /*
277 * NOTE!
278 *
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
281 * delete).
282 */
75c42d8c
LT
283 max_size = size / 2 - 20;
284 if (cur_entry->delta)
285 max_size = cur_entry->delta_size-1;
27225f2e
LT
286 if (sizediff >= max_size)
287 return -1;
8ee378a0
JH
288 delta_buf = diff_delta(old->data, oldsize,
289 cur->data, size, &delta_size, max_size);
c323ac7d 290 if (!delta_buf)
75c42d8c
LT
291 return 0;
292 cur_entry->delta = old_entry;
293 cur_entry->delta_size = delta_size;
d116a45a 294 cur_entry->depth = old_entry->depth + 1;
c323ac7d 295 free(delta_buf);
eb41ab11 296 return 0;
c323ac7d
LT
297}
298
d116a45a 299static void find_deltas(struct object_entry **list, int window, int depth)
c323ac7d 300{
521a4f4c 301 int i, idx;
c323ac7d
LT
302 unsigned int array_size = window * sizeof(struct unpacked);
303 struct unpacked *array = xmalloc(array_size);
304
305 memset(array, 0, array_size);
521a4f4c
LT
306 i = nr_objects;
307 idx = 0;
308 while (--i >= 0) {
c323ac7d
LT
309 struct object_entry *entry = list[i];
310 struct unpacked *n = array + idx;
311 unsigned long size;
312 char type[10];
313 int j;
314
315 free(n->data);
316 n->entry = entry;
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);
78817c15
LT
320 j = window;
321 while (--j > 0) {
322 unsigned int other_idx = idx + j;
c323ac7d 323 struct unpacked *m;
78817c15
LT
324 if (other_idx >= window)
325 other_idx -= window;
c323ac7d
LT
326 m = array + other_idx;
327 if (!m->entry)
328 break;
d116a45a 329 if (try_delta(n, m, depth) < 0)
c323ac7d
LT
330 break;
331 }
521a4f4c
LT
332 idx++;
333 if (idx >= window)
334 idx = 0;
c323ac7d
LT
335 }
336}
337
338int main(int argc, char **argv)
339{
27225f2e 340 char line[PATH_MAX + 20];
d22b9290 341 int window = 10, depth = 10, pack_to_stdout = 0;
c323ac7d
LT
342 int i;
343
344 for (i = 1; i < argc; i++) {
345 const char *arg = argv[i];
346
347 if (*arg == '-') {
348 if (!strncmp("--window=", arg, 9)) {
349 char *end;
f846bbff 350 window = strtoul(arg+9, &end, 0);
c323ac7d
LT
351 if (!arg[9] || *end)
352 usage(pack_usage);
353 continue;
354 }
d116a45a
LT
355 if (!strncmp("--depth=", arg, 8)) {
356 char *end;
357 depth = strtoul(arg+8, &end, 0);
358 if (!arg[8] || *end)
359 usage(pack_usage);
360 continue;
361 }
d22b9290
LT
362 if (!strcmp("--stdout", arg)) {
363 pack_to_stdout = 1;
364 continue;
365 }
c323ac7d
LT
366 usage(pack_usage);
367 }
368 if (base_name)
369 usage(pack_usage);
370 base_name = arg;
371 }
372
d22b9290 373 if (pack_to_stdout != !base_name)
c323ac7d
LT
374 usage(pack_usage);
375
376 while (fgets(line, sizeof(line), stdin) != NULL) {
27225f2e
LT
377 unsigned int hash;
378 char *p;
c323ac7d 379 unsigned char sha1[20];
27225f2e 380
c323ac7d
LT
381 if (get_sha1_hex(line, sha1))
382 die("expected sha1, got garbage");
27225f2e
LT
383 hash = 0;
384 p = line+40;
385 while (*p) {
386 unsigned char c = *p++;
387 if (isspace(c))
388 continue;
389 hash = hash * 11 + c;
390 }
391 add_object_entry(sha1, hash);
c323ac7d
LT
392 }
393 get_object_details();
394
d22b9290 395 fprintf(stderr, "Packing %d objects\n", nr_objects);
c323ac7d
LT
396
397 sorted_by_sha = create_sorted_list(sha1_sort);
398 sorted_by_type = create_sorted_list(type_size_sort);
d116a45a
LT
399 if (window && depth)
400 find_deltas(sorted_by_type, window+1, depth);
c323ac7d
LT
401
402 write_pack_file();
d22b9290
LT
403 if (!pack_to_stdout)
404 write_index_file();
c323ac7d
LT
405 return 0;
406}