Add "--pretty=full" format that also shows committer.
[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"
5
d116a45a 6static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] base-name < object-list";
c323ac7d
LT
7
8enum object_type {
9 OBJ_NONE,
10 OBJ_COMMIT,
11 OBJ_TREE,
12 OBJ_BLOB,
13 OBJ_DELTA // NOTE! This is _not_ the same as a "delta" object in the filesystem
14};
15
16struct object_entry {
17 unsigned char sha1[20];
18 unsigned long size;
19 unsigned long offset;
20 unsigned int depth;
27225f2e 21 unsigned int hash;
c323ac7d
LT
22 enum object_type type;
23 unsigned long delta_size;
24 struct object_entry *delta;
25};
26
27static struct object_entry **sorted_by_sha, **sorted_by_type;
28static struct object_entry *objects = NULL;
29static int nr_objects = 0, nr_alloc = 0;
c323ac7d
LT
30static const char *base_name;
31
32struct myfile {
33 int fd;
34 unsigned long chars;
35 unsigned char buffer[8192];
36};
37
38static FILE *create_file(const char *suffix)
39{
40 static char filename[PATH_MAX];
41 unsigned len;
42
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");
47}
48
49static unsigned long fwrite_compressed(void *in, unsigned long size, FILE *f)
50{
51 z_stream stream;
52 unsigned long maxsize;
53 void *out;
54
55 memset(&stream, 0, sizeof(stream));
56 deflateInit(&stream, Z_DEFAULT_COMPRESSION);
57 maxsize = deflateBound(&stream, size);
58 out = xmalloc(maxsize);
59
60 /* Compress it */
61 stream.next_in = in;
62 stream.avail_in = size;
63
64 stream.next_out = out;
65 stream.avail_out = maxsize;
66
67 while (deflate(&stream, Z_FINISH) == Z_OK)
68 /* nothing */;
69 deflateEnd(&stream);
70
71 size = stream.total_out;
72 fwrite(out, size, 1, f);
73 free(out);
74 return size;
75}
76
77static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
78{
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;
83
84 if (!otherbuf)
85 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
8ee378a0
JH
86 delta_buf = diff_delta(otherbuf, othersize,
87 buf, size, &delta_size, ~0UL);
75c42d8c 88 if (!delta_buf || delta_size != entry->delta_size)
c323ac7d
LT
89 die("delta size changed");
90 free(buf);
91 free(otherbuf);
92 return delta_buf;
93}
94
95static unsigned long write_object(FILE *f, struct object_entry *entry)
96{
97 unsigned long size;
98 char type[10];
99 void *buf = read_sha1_file(entry->sha1, type, &size);
c4fb06c0 100 char header[25];
c323ac7d
LT
101 unsigned hdrlen, datalen;
102
103 if (!buf)
104 die("unable to read %s", sha1_to_hex(entry->sha1));
105 if (size != entry->size)
106 die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
107
108 /*
109 * The object header is a byte of 'type' followed by four bytes of
110 * length, except for deltas that has the 20 bytes of delta sha
111 * instead.
112 */
113 header[0] = ".CTB"[entry->type];
c323ac7d
LT
114 hdrlen = 5;
115 if (entry->delta) {
d38c3721 116 header[0] = 'D';
c4fb06c0 117 memcpy(header+5, entry->delta, 20);
c323ac7d
LT
118 buf = delta_against(buf, size, entry);
119 size = entry->delta_size;
c4fb06c0 120 hdrlen = 25;
c323ac7d 121 }
c4fb06c0
LT
122 datalen = htonl(size);
123 memcpy(header+1, &datalen, 4);
c323ac7d
LT
124 fwrite(header, hdrlen, 1, f);
125 datalen = fwrite_compressed(buf, size, f);
126 free(buf);
127 return hdrlen + datalen;
128}
129
130static void write_pack_file(void)
131{
132 int i;
133 FILE *f = create_file("pack");
134 unsigned long offset = 0;
135 unsigned long mb;
136
137 for (i = 0; i < nr_objects; i++) {
138 struct object_entry *entry = objects + i;
139 entry->offset = offset;
140 offset += write_object(f, entry);
141 }
142 fclose(f);
143 mb = offset >> 20;
144 offset &= 0xfffff;
145}
146
147static void write_index_file(void)
148{
149 int i;
150 FILE *f = create_file("idx");
151 struct object_entry **list = sorted_by_sha;
152 struct object_entry **last = list + nr_objects;
153 unsigned int array[256];
154
155 /*
156 * Write the first-level table (the list is sorted,
157 * but we use a 256-entry lookup to be able to avoid
158 * having to do eight extra binary search iterations)
159 */
160 for (i = 0; i < 256; i++) {
161 struct object_entry **next = list;
162 while (next < last) {
163 struct object_entry *entry = *next;
164 if (entry->sha1[0] != i)
165 break;
166 next++;
167 }
168 array[i] = htonl(next - sorted_by_sha);
169 list = next;
170 }
171 fwrite(array, 256, sizeof(int), f);
172
173 /*
174 * Write the actual SHA1 entries..
175 */
176 list = sorted_by_sha;
49397104 177 for (i = 0; i < nr_objects; i++) {
c323ac7d
LT
178 struct object_entry *entry = *list++;
179 unsigned int offset = htonl(entry->offset);
180 fwrite(&offset, 4, 1, f);
181 fwrite(entry->sha1, 20, 1, f);
182 }
183 fclose(f);
184}
185
27225f2e 186static void add_object_entry(unsigned char *sha1, unsigned int hash)
c323ac7d
LT
187{
188 unsigned int idx = nr_objects;
189 struct object_entry *entry;
190
191 if (idx >= nr_alloc) {
192 unsigned int needed = (idx + 1024) * 3 / 2;
193 objects = xrealloc(objects, needed * sizeof(*entry));
194 nr_alloc = needed;
195 }
196 entry = objects + idx;
197 memset(entry, 0, sizeof(*entry));
198 memcpy(entry->sha1, sha1, 20);
27225f2e 199 entry->hash = hash;
c323ac7d
LT
200 nr_objects = idx+1;
201}
202
203static void check_object(struct object_entry *entry)
204{
205 char buffer[128];
206 char type[10];
207 unsigned long mapsize;
208 z_stream stream;
209 void *map;
210
211 map = map_sha1_file(entry->sha1, &mapsize);
212 if (!map)
213 die("unable to map %s", sha1_to_hex(entry->sha1));
214 if (unpack_sha1_header(&stream, map, mapsize, buffer, sizeof(buffer)) < 0)
215 die("unable to unpack %s header", sha1_to_hex(entry->sha1));
216 munmap(map, mapsize);
217 if (parse_sha1_header(buffer, type, &entry->size) < 0)
218 die("unable to parse %s header", sha1_to_hex(entry->sha1));
219 if (!strcmp(type, "commit")) {
220 entry->type = OBJ_COMMIT;
221 } else if (!strcmp(type, "tree")) {
222 entry->type = OBJ_TREE;
223 } else if (!strcmp(type, "blob")) {
224 entry->type = OBJ_BLOB;
225 } else
226 die("unable to pack object %s of type %s", sha1_to_hex(entry->sha1), type);
227}
228
229static void get_object_details(void)
230{
231 int i;
232 struct object_entry *entry = objects;
233
234 for (i = 0; i < nr_objects; i++)
235 check_object(entry++);
236}
237
238typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
239
240static entry_sort_t current_sort;
241
242static int sort_comparator(const void *_a, const void *_b)
243{
244 struct object_entry *a = *(struct object_entry **)_a;
245 struct object_entry *b = *(struct object_entry **)_b;
246 return current_sort(a,b);
247}
248
249static struct object_entry **create_sorted_list(entry_sort_t sort)
250{
251 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
252 int i;
253
254 for (i = 0; i < nr_objects; i++)
255 list[i] = objects + i;
256 current_sort = sort;
257 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
258 return list;
259}
260
261static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
262{
263 return memcmp(a->sha1, b->sha1, 20);
264}
265
266static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
267{
268 if (a->type < b->type)
269 return -1;
270 if (a->type > b->type)
271 return 1;
27225f2e
LT
272 if (a->hash < b->hash)
273 return -1;
274 if (a->hash > b->hash)
275 return 1;
c323ac7d
LT
276 if (a->size < b->size)
277 return -1;
278 if (a->size > b->size)
279 return 1;
280 return a < b ? -1 : (a > b);
281}
282
283struct unpacked {
284 struct object_entry *entry;
285 void *data;
286};
287
288/*
521a4f4c
LT
289 * We search for deltas _backwards_ in a list sorted by type and
290 * by size, so that we see progressively smaller and smaller files.
291 * That's because we prefer deltas to be from the bigger file
292 * to the smaller - deletes are potentially cheaper, but perhaps
293 * more importantly, the bigger file is likely the more recent
294 * one.
c323ac7d 295 */
d116a45a 296static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
c323ac7d
LT
297{
298 struct object_entry *cur_entry = cur->entry;
299 struct object_entry *old_entry = old->entry;
521a4f4c 300 unsigned long size, oldsize, delta_size, sizediff;
75c42d8c 301 long max_size;
c323ac7d
LT
302 void *delta_buf;
303
304 /* Don't bother doing diffs between different types */
305 if (cur_entry->type != old_entry->type)
306 return -1;
307
c323ac7d 308 size = cur_entry->size;
75c42d8c
LT
309 if (size < 50)
310 return -1;
c323ac7d 311 oldsize = old_entry->size;
521a4f4c
LT
312 sizediff = oldsize > size ? oldsize - size : size - oldsize;
313 if (sizediff > size / 8)
c323ac7d 314 return -1;
d116a45a
LT
315 if (old_entry->depth >= max_depth)
316 return 0;
c323ac7d
LT
317
318 /*
319 * NOTE!
320 *
321 * We always delta from the bigger to the smaller, since that's
322 * more space-efficient (deletes don't have to say _what_ they
323 * delete).
324 */
75c42d8c
LT
325 max_size = size / 2 - 20;
326 if (cur_entry->delta)
327 max_size = cur_entry->delta_size-1;
27225f2e
LT
328 if (sizediff >= max_size)
329 return -1;
8ee378a0
JH
330 delta_buf = diff_delta(old->data, oldsize,
331 cur->data, size, &delta_size, max_size);
c323ac7d 332 if (!delta_buf)
75c42d8c
LT
333 return 0;
334 cur_entry->delta = old_entry;
335 cur_entry->delta_size = delta_size;
d116a45a 336 cur_entry->depth = old_entry->depth + 1;
c323ac7d 337 free(delta_buf);
eb41ab11 338 return 0;
c323ac7d
LT
339}
340
d116a45a 341static void find_deltas(struct object_entry **list, int window, int depth)
c323ac7d 342{
521a4f4c 343 int i, idx;
c323ac7d
LT
344 unsigned int array_size = window * sizeof(struct unpacked);
345 struct unpacked *array = xmalloc(array_size);
346
347 memset(array, 0, array_size);
521a4f4c
LT
348 i = nr_objects;
349 idx = 0;
350 while (--i >= 0) {
c323ac7d
LT
351 struct object_entry *entry = list[i];
352 struct unpacked *n = array + idx;
353 unsigned long size;
354 char type[10];
355 int j;
356
357 free(n->data);
358 n->entry = entry;
359 n->data = read_sha1_file(entry->sha1, type, &size);
360 if (size != entry->size)
361 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
78817c15
LT
362 j = window;
363 while (--j > 0) {
364 unsigned int other_idx = idx + j;
c323ac7d 365 struct unpacked *m;
78817c15
LT
366 if (other_idx >= window)
367 other_idx -= window;
c323ac7d
LT
368 m = array + other_idx;
369 if (!m->entry)
370 break;
d116a45a 371 if (try_delta(n, m, depth) < 0)
c323ac7d
LT
372 break;
373 }
521a4f4c
LT
374 idx++;
375 if (idx >= window)
376 idx = 0;
c323ac7d
LT
377 }
378}
379
380int main(int argc, char **argv)
381{
27225f2e 382 char line[PATH_MAX + 20];
d116a45a 383 int window = 10, depth = 10;
c323ac7d
LT
384 int i;
385
386 for (i = 1; i < argc; i++) {
387 const char *arg = argv[i];
388
389 if (*arg == '-') {
390 if (!strncmp("--window=", arg, 9)) {
391 char *end;
f846bbff 392 window = strtoul(arg+9, &end, 0);
c323ac7d
LT
393 if (!arg[9] || *end)
394 usage(pack_usage);
395 continue;
396 }
d116a45a
LT
397 if (!strncmp("--depth=", arg, 8)) {
398 char *end;
399 depth = strtoul(arg+8, &end, 0);
400 if (!arg[8] || *end)
401 usage(pack_usage);
402 continue;
403 }
c323ac7d
LT
404 usage(pack_usage);
405 }
406 if (base_name)
407 usage(pack_usage);
408 base_name = arg;
409 }
410
411 if (!base_name)
412 usage(pack_usage);
413
414 while (fgets(line, sizeof(line), stdin) != NULL) {
27225f2e
LT
415 unsigned int hash;
416 char *p;
c323ac7d 417 unsigned char sha1[20];
27225f2e 418
c323ac7d
LT
419 if (get_sha1_hex(line, sha1))
420 die("expected sha1, got garbage");
27225f2e
LT
421 hash = 0;
422 p = line+40;
423 while (*p) {
424 unsigned char c = *p++;
425 if (isspace(c))
426 continue;
427 hash = hash * 11 + c;
428 }
429 add_object_entry(sha1, hash);
c323ac7d
LT
430 }
431 get_object_details();
432
433 printf("Packing %d objects\n", nr_objects);
434
435 sorted_by_sha = create_sorted_list(sha1_sort);
436 sorted_by_type = create_sorted_list(type_size_sort);
d116a45a
LT
437 if (window && depth)
438 find_deltas(sorted_by_type, window+1, depth);
c323ac7d
LT
439
440 write_pack_file();
441 write_index_file();
442 return 0;
443}