Added branch load counter to fast-import.
[git/git.git] / fast-import.c
CommitLineData
463acbe1
SP
1/*
2Format of STDIN stream:
3
4 stream ::= cmd*;
5
6 cmd ::= new_blob
463acbe1 7 | new_branch
c44cdc7e 8 | new_commit
463acbe1
SP
9 | new_tag
10 ;
11
c44cdc7e
SP
12 new_blob ::= 'blob' lf
13 mark?
14 file_content;
15 file_content ::= data;
463acbe1 16
c44cdc7e
SP
17 new_branch ::= 'branch' sp ref_str lf
18 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
19 lf;
463acbe1 20
c44cdc7e
SP
21 new_commit ::= 'commit' sp ref_str lf
22 mark?
23 ('author' sp name '<' email '>' ts tz lf)?
24 'committer' sp name '<' email '>' ts tz lf
25 commit_msg
26 file_change*
27 lf;
28 commit_msg ::= data;
463acbe1 29
c44cdc7e
SP
30 file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
31 | 'D' sp path_str lf
463acbe1 32 ;
c44cdc7e
SP
33 mode ::= '644' | '755';
34
35 new_tag ::= 'tag' sp tag_str lf
36 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
37 'tagger' sp name '<' email '>' ts tz lf
38 tag_msg;
39 tag_msg ::= data;
40
41 # note: the first idnum in a stream should be 1 and subsequent
42 # idnums should not have gaps between values as this will cause
43 # the stream parser to reserve space for the gapped values. An
44 # idnum can be updated in the future to a new object by issuing
45 # a new mark directive with the old idnum.
46 #
47 mark ::= 'mark' sp idnum lf;
48
49 # note: declen indicates the length of binary_data in bytes.
50 # declen does not include the lf preceeding or trailing the
51 # binary data.
52 #
53 data ::= 'data' sp declen lf
54 binary_data
55 lf;
56
57 # note: quoted strings are C-style quoting supporting \c for
58 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
59 # is the signed byte value in octal. Note that the only
60 # characters which must actually be escaped to protect the
61 # stream formatting is: \, " and LF. Otherwise these values
62 # are UTF8.
63 #
64 ref_str ::= ref | '"' quoted(ref) '"' ;
65 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
66 tag_str ::= tag | '"' quoted(tag) '"' ;
67 path_str ::= path | '"' quoted(path) '"' ;
68
69 declen ::= # unsigned 32 bit value, ascii base10 notation;
463acbe1 70 binary_data ::= # file content, not interpreted;
c44cdc7e 71
463acbe1
SP
72 sp ::= # ASCII space character;
73 lf ::= # ASCII newline (LF) character;
c44cdc7e
SP
74
75 # note: a colon (':') must precede the numerical value assigned to
76 # an idnum. This is to distinguish it from a ref or tag name as
77 # GIT does not permit ':' in ref or tag strings.
78 #
79 idnum ::= ':' declen;
80 path ::= # GIT style file path, e.g. "a/b/c";
81 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
82 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
463acbe1
SP
83 sha1exp ::= # Any valid GIT SHA1 expression;
84 hexsha1 ::= # SHA1 in hexadecimal format;
c44cdc7e
SP
85
86 # note: name and email are UTF8 strings, however name must not
87 # contain '<' or lf and email must not contain any of the
88 # following: '<', '>', lf.
89 #
90 name ::= # valid GIT author/committer name;
463acbe1 91 email ::= # valid GIT author/committer email;
c44cdc7e
SP
92 ts ::= # time since the epoch in seconds, ascii base10 notation;
93 tz ::= # GIT style timezone;
463acbe1
SP
94*/
95
db5e523f
SP
96#include "builtin.h"
97#include "cache.h"
98#include "object.h"
99#include "blob.h"
463acbe1 100#include "tree.h"
db5e523f
SP
101#include "delta.h"
102#include "pack.h"
463acbe1 103#include "refs.h"
db5e523f 104#include "csum-file.h"
c44cdc7e
SP
105#include "strbuf.h"
106#include "quote.h"
db5e523f 107
27d6d290
SP
108struct object_entry
109{
110 struct object_entry *next;
7111feed 111 enum object_type type;
27d6d290
SP
112 unsigned long offset;
113 unsigned char sha1[20];
114};
115
463acbe1 116struct object_entry_pool
27d6d290 117{
463acbe1 118 struct object_entry_pool *next_pool;
27d6d290
SP
119 struct object_entry *next_free;
120 struct object_entry *end;
ac47a738 121 struct object_entry entries[FLEX_ARRAY]; /* more */
27d6d290
SP
122};
123
d8397168
SP
124struct mark_set
125{
126 int shift;
127 union {
128 struct object_entry *marked[1024];
129 struct mark_set *sets[1024];
130 } data;
131};
132
ac47a738
SP
133struct last_object
134{
135 void *data;
6bb5b329
SP
136 unsigned int len;
137 unsigned int depth;
ac47a738
SP
138 unsigned char sha1[20];
139};
140
463acbe1
SP
141struct mem_pool
142{
143 struct mem_pool *next_pool;
144 char *next_free;
145 char *end;
146 char space[FLEX_ARRAY]; /* more */
147};
148
149struct atom_str
150{
151 struct atom_str *next_atom;
152 int str_len;
153 char str_dat[FLEX_ARRAY]; /* more */
154};
155
156struct tree_content;
6bb5b329
SP
157struct tree_entry
158{
463acbe1
SP
159 struct tree_content *tree;
160 struct atom_str* name;
161 unsigned int mode;
6bb5b329 162 unsigned char sha1[20];
6bb5b329
SP
163};
164
463acbe1 165struct tree_content
6bb5b329 166{
463acbe1
SP
167 unsigned int entry_capacity; /* must match avail_tree_content */
168 unsigned int entry_count;
169 struct tree_entry *entries[FLEX_ARRAY]; /* more */
170};
171
172struct avail_tree_content
173{
174 unsigned int entry_capacity; /* must match tree_content */
175 struct avail_tree_content *next_avail;
6bb5b329
SP
176};
177
178struct branch
179{
463acbe1
SP
180 struct branch *table_next_branch;
181 struct branch *active_next_branch;
6bb5b329 182 const char *name;
463acbe1
SP
183 unsigned long last_commit;
184 struct tree_entry branch_tree;
185 unsigned char sha1[20];
6bb5b329
SP
186};
187
463acbe1
SP
188
189/* Stats and misc. counters */
d5c57b28 190static unsigned long max_depth = 10;
27d6d290 191static unsigned long alloc_count;
6bb5b329 192static unsigned long branch_count;
d6c7eb2c 193static unsigned long branch_load_count;
db5e523f 194static unsigned long object_count;
8bcce301 195static unsigned long duplicate_count;
d8397168 196static unsigned long marks_set_count;
6143f064
SP
197static unsigned long object_count_by_type[9];
198static unsigned long duplicate_count_by_type[9];
ac47a738 199
463acbe1
SP
200/* Memory pools */
201static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
202static size_t total_allocd;
203static struct mem_pool *mem_pool;
204
c44cdc7e 205/* Atom management */
463acbe1
SP
206static unsigned int atom_table_sz = 4451;
207static unsigned int atom_cnt;
208static struct atom_str **atom_table;
209
210/* The .pack file being generated */
ac47a738
SP
211static int pack_fd;
212static unsigned long pack_offset;
213static unsigned char pack_sha1[20];
214
215/* Table of objects we've written. */
463acbe1
SP
216static unsigned int object_entry_alloc = 1000;
217static struct object_entry_pool *blocks;
218static struct object_entry *object_table[1 << 16];
d8397168 219static struct mark_set *marks;
ac47a738
SP
220
221/* Our last blob */
463acbe1
SP
222static struct last_object last_blob;
223
224/* Tree management */
225static unsigned int tree_entry_alloc = 1000;
226static void *avail_tree_entry;
227static unsigned int avail_tree_table_sz = 100;
228static struct avail_tree_content **avail_tree_table;
8bcce301 229
6bb5b329 230/* Branch data */
d5c57b28
SP
231static unsigned long max_active_branches = 5;
232static unsigned long cur_active_branches;
233static unsigned long branch_table_sz = 1039;
463acbe1
SP
234static struct branch **branch_table;
235static struct branch *active_branches;
236
c44cdc7e
SP
237/* Input stream parsing */
238static struct strbuf command_buf;
d8397168 239static unsigned long next_mark;
c44cdc7e 240
6bb5b329 241
27d6d290 242static void alloc_objects(int cnt)
8bcce301 243{
463acbe1 244 struct object_entry_pool *b;
27d6d290 245
463acbe1 246 b = xmalloc(sizeof(struct object_entry_pool)
27d6d290 247 + cnt * sizeof(struct object_entry));
463acbe1 248 b->next_pool = blocks;
27d6d290
SP
249 b->next_free = b->entries;
250 b->end = b->entries + cnt;
251 blocks = b;
252 alloc_count += cnt;
253}
8bcce301 254
27d6d290 255static struct object_entry* new_object(unsigned char *sha1)
8bcce301 256{
27d6d290 257 struct object_entry *e;
8bcce301 258
27d6d290 259 if (blocks->next_free == blocks->end)
463acbe1 260 alloc_objects(object_entry_alloc);
8bcce301 261
27d6d290
SP
262 e = blocks->next_free++;
263 memcpy(e->sha1, sha1, sizeof(e->sha1));
264 return e;
8bcce301
SP
265}
266
463acbe1
SP
267static struct object_entry* find_object(unsigned char *sha1)
268{
269 unsigned int h = sha1[0] << 8 | sha1[1];
270 struct object_entry *e;
271 for (e = object_table[h]; e; e = e->next)
272 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
273 return e;
274 return NULL;
275}
276
8bcce301
SP
277static struct object_entry* insert_object(unsigned char *sha1)
278{
279 unsigned int h = sha1[0] << 8 | sha1[1];
27d6d290 280 struct object_entry *e = object_table[h];
463acbe1 281 struct object_entry *p = NULL;
8bcce301
SP
282
283 while (e) {
284 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
285 return e;
286 p = e;
287 e = e->next;
288 }
289
290 e = new_object(sha1);
463acbe1 291 e->next = NULL;
8bcce301
SP
292 e->offset = 0;
293 if (p)
294 p->next = e;
295 else
27d6d290 296 object_table[h] = e;
8bcce301
SP
297 return e;
298}
db5e523f 299
463acbe1
SP
300static unsigned int hc_str(const char *s, size_t len)
301{
302 unsigned int r = 0;
303 while (len-- > 0)
304 r = r * 31 + *s++;
305 return r;
306}
307
308static void* pool_alloc(size_t len)
309{
310 struct mem_pool *p;
311 void *r;
312
313 for (p = mem_pool; p; p = p->next_pool)
314 if ((p->end - p->next_free >= len))
315 break;
316
317 if (!p) {
318 if (len >= (mem_pool_alloc/2)) {
319 total_allocd += len;
320 return xmalloc(len);
321 }
322 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
323 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
324 p->next_pool = mem_pool;
325 p->next_free = p->space;
326 p->end = p->next_free + mem_pool_alloc;
327 mem_pool = p;
328 }
329
330 r = p->next_free;
331 p->next_free += len;
332 return r;
333}
334
335static void* pool_calloc(size_t count, size_t size)
336{
337 size_t len = count * size;
338 void *r = pool_alloc(len);
339 memset(r, 0, len);
340 return r;
341}
342
343static char* pool_strdup(const char *s)
344{
345 char *r = pool_alloc(strlen(s) + 1);
346 strcpy(r, s);
347 return r;
348}
349
d8397168
SP
350static void insert_mark(unsigned long idnum, struct object_entry *oe)
351{
352 struct mark_set *s = marks;
353 while ((idnum >> s->shift) >= 1024) {
354 s = pool_calloc(1, sizeof(struct mark_set));
355 s->shift = marks->shift + 10;
356 s->data.sets[0] = marks;
357 marks = s;
358 }
359 while (s->shift) {
360 unsigned long i = idnum >> s->shift;
361 idnum -= i << s->shift;
362 if (!s->data.sets[i]) {
363 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
364 s->data.sets[i]->shift = s->shift - 10;
365 }
366 s = s->data.sets[i];
367 }
368 if (!s->data.marked[idnum])
369 marks_set_count++;
370 s->data.marked[idnum] = oe;
371}
372
373static struct object_entry* find_mark(unsigned long idnum)
374{
375 unsigned long orig_idnum = idnum;
376 struct mark_set *s = marks;
377 struct object_entry *oe = NULL;
378 if ((idnum >> s->shift) < 1024) {
379 while (s && s->shift) {
380 unsigned long i = idnum >> s->shift;
381 idnum -= i << s->shift;
382 s = s->data.sets[i];
383 }
384 if (s)
385 oe = s->data.marked[idnum];
386 }
387 if (!oe)
388 die("mark :%lu not declared", orig_idnum);
389 return oe;
390}
391
463acbe1
SP
392static struct atom_str* to_atom(const char *s, size_t len)
393{
394 unsigned int hc = hc_str(s, len) % atom_table_sz;
395 struct atom_str *c;
396
397 for (c = atom_table[hc]; c; c = c->next_atom)
398 if (c->str_len == len && !strncmp(s, c->str_dat, len))
399 return c;
400
401 c = pool_alloc(sizeof(struct atom_str) + len + 1);
402 c->str_len = len;
403 strncpy(c->str_dat, s, len);
404 c->str_dat[len] = 0;
405 c->next_atom = atom_table[hc];
406 atom_table[hc] = c;
407 atom_cnt++;
408 return c;
409}
410
411static struct branch* lookup_branch(const char *name)
412{
413 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
414 struct branch *b;
415
416 for (b = branch_table[hc]; b; b = b->table_next_branch)
417 if (!strcmp(name, b->name))
418 return b;
419 return NULL;
420}
421
422static struct branch* new_branch(const char *name)
423{
424 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
425 struct branch* b = lookup_branch(name);
426
427 if (b)
428 die("Invalid attempt to create duplicate branch: %s", name);
c44cdc7e
SP
429 if (check_ref_format(name))
430 die("Branch name doesn't conform to GIT standards: %s", name);
463acbe1
SP
431
432 b = pool_calloc(1, sizeof(struct branch));
433 b->name = pool_strdup(name);
434 b->table_next_branch = branch_table[hc];
435 branch_table[hc] = b;
436 branch_count++;
437 return b;
438}
439
440static unsigned int hc_entries(unsigned int cnt)
441{
442 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
443 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
444}
445
446static struct tree_content* new_tree_content(unsigned int cnt)
447{
448 struct avail_tree_content *f, *l = NULL;
449 struct tree_content *t;
450 unsigned int hc = hc_entries(cnt);
451
452 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
453 if (f->entry_capacity >= cnt)
454 break;
455
456 if (f) {
457 if (l)
458 l->next_avail = f->next_avail;
459 else
460 avail_tree_table[hc] = f->next_avail;
461 } else {
462 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
463 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
464 f->entry_capacity = cnt;
465 }
466
467 t = (struct tree_content*)f;
468 t->entry_count = 0;
469 return t;
470}
471
472static void release_tree_entry(struct tree_entry *e);
473static void release_tree_content(struct tree_content *t)
474{
475 struct avail_tree_content *f = (struct avail_tree_content*)t;
476 unsigned int hc = hc_entries(f->entry_capacity);
afde8dd9
SP
477 f->next_avail = avail_tree_table[hc];
478 avail_tree_table[hc] = f;
479}
480
481static void release_tree_content_recursive(struct tree_content *t)
482{
463acbe1
SP
483 unsigned int i;
484 for (i = 0; i < t->entry_count; i++)
485 release_tree_entry(t->entries[i]);
afde8dd9 486 release_tree_content(t);
463acbe1
SP
487}
488
489static struct tree_content* grow_tree_content(
490 struct tree_content *t,
491 int amt)
492{
493 struct tree_content *r = new_tree_content(t->entry_count + amt);
494 r->entry_count = t->entry_count;
495 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
496 release_tree_content(t);
497 return r;
498}
499
500static struct tree_entry* new_tree_entry()
501{
502 struct tree_entry *e;
503
504 if (!avail_tree_entry) {
505 unsigned int n = tree_entry_alloc;
506 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
507 while (n--) {
508 *((void**)e) = e + 1;
509 e++;
510 }
511 }
512
513 e = avail_tree_entry;
514 avail_tree_entry = *((void**)e);
515 return e;
516}
517
518static void release_tree_entry(struct tree_entry *e)
519{
520 if (e->tree)
afde8dd9 521 release_tree_content_recursive(e->tree);
463acbe1
SP
522 *((void**)e) = avail_tree_entry;
523 avail_tree_entry = e;
524}
525
526static void yread(int fd, void *buffer, size_t length)
db5e523f
SP
527{
528 ssize_t ret = 0;
529 while (ret < length) {
530 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
463acbe1
SP
531 if (!size)
532 die("Read from descriptor %i: end of stream", fd);
533 if (size < 0)
534 die("Read from descriptor %i: %s", fd, strerror(errno));
535 ret += size;
536 }
537}
538
463acbe1 539static void ywrite(int fd, void *buffer, size_t length)
db5e523f
SP
540{
541 ssize_t ret = 0;
542 while (ret < length) {
543 ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret);
463acbe1
SP
544 if (!size)
545 die("Write to descriptor %i: end of file", fd);
546 if (size < 0)
547 die("Write to descriptor %i: %s", fd, strerror(errno));
db5e523f
SP
548 ret += size;
549 }
db5e523f
SP
550}
551
c44cdc7e 552static size_t encode_header(
ac47a738 553 enum object_type type,
c44cdc7e 554 size_t size,
ac47a738 555 unsigned char *hdr)
db5e523f
SP
556{
557 int n = 1;
558 unsigned char c;
559
560 if (type < OBJ_COMMIT || type > OBJ_DELTA)
561 die("bad type %d", type);
562
563 c = (type << 4) | (size & 15);
564 size >>= 4;
565 while (size) {
566 *hdr++ = c | 0x80;
567 c = size & 0x7f;
568 size >>= 7;
569 n++;
570 }
571 *hdr = c;
572 return n;
573}
574
ac47a738
SP
575static int store_object(
576 enum object_type type,
577 void *dat,
c44cdc7e 578 size_t datlen,
6bb5b329 579 struct last_object *last,
d8397168
SP
580 unsigned char *sha1out,
581 unsigned long mark)
db5e523f 582{
db5e523f 583 void *out, *delta;
ac47a738
SP
584 struct object_entry *e;
585 unsigned char hdr[96];
586 unsigned char sha1[20];
db5e523f 587 unsigned long hdrlen, deltalen;
ac47a738
SP
588 SHA_CTX c;
589 z_stream s;
590
591 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
592 SHA1_Init(&c);
593 SHA1_Update(&c, hdr, hdrlen);
594 SHA1_Update(&c, dat, datlen);
595 SHA1_Final(sha1, &c);
6bb5b329
SP
596 if (sha1out)
597 memcpy(sha1out, sha1, sizeof(sha1));
ac47a738
SP
598
599 e = insert_object(sha1);
d8397168
SP
600 if (mark)
601 insert_mark(mark, e);
ac47a738
SP
602 if (e->offset) {
603 duplicate_count++;
6143f064 604 duplicate_count_by_type[type]++;
463acbe1 605 return 1;
ac47a738 606 }
7111feed 607 e->type = type;
ac47a738
SP
608 e->offset = pack_offset;
609 object_count++;
6143f064 610 object_count_by_type[type]++;
db5e523f 611
463acbe1 612 if (last && last->data && last->depth < max_depth)
ac47a738 613 delta = diff_delta(last->data, last->len,
db5e523f
SP
614 dat, datlen,
615 &deltalen, 0);
ac47a738 616 else
db5e523f
SP
617 delta = 0;
618
619 memset(&s, 0, sizeof(s));
620 deflateInit(&s, zlib_compression_level);
621
622 if (delta) {
ac47a738 623 last->depth++;
db5e523f
SP
624 s.next_in = delta;
625 s.avail_in = deltalen;
626 hdrlen = encode_header(OBJ_DELTA, deltalen, hdr);
463acbe1
SP
627 ywrite(pack_fd, hdr, hdrlen);
628 ywrite(pack_fd, last->sha1, sizeof(sha1));
ac47a738 629 pack_offset += hdrlen + sizeof(sha1);
db5e523f 630 } else {
463acbe1
SP
631 if (last)
632 last->depth = 0;
db5e523f
SP
633 s.next_in = dat;
634 s.avail_in = datlen;
ac47a738 635 hdrlen = encode_header(type, datlen, hdr);
463acbe1 636 ywrite(pack_fd, hdr, hdrlen);
ac47a738 637 pack_offset += hdrlen;
db5e523f
SP
638 }
639
640 s.avail_out = deflateBound(&s, s.avail_in);
641 s.next_out = out = xmalloc(s.avail_out);
642 while (deflate(&s, Z_FINISH) == Z_OK)
643 /* nothing */;
644 deflateEnd(&s);
645
463acbe1 646 ywrite(pack_fd, out, s.total_out);
ac47a738 647 pack_offset += s.total_out;
db5e523f
SP
648
649 free(out);
650 if (delta)
651 free(delta);
463acbe1
SP
652 if (last) {
653 if (last->data)
654 free(last->data);
655 last->data = dat;
656 last->len = datlen;
657 memcpy(last->sha1, sha1, sizeof(sha1));
658 }
659 return 0;
660}
661
662static const char *get_mode(const char *str, unsigned int *modep)
663{
664 unsigned char c;
665 unsigned int mode = 0;
666
667 while ((c = *str++) != ' ') {
668 if (c < '0' || c > '7')
669 return NULL;
670 mode = (mode << 3) + (c - '0');
671 }
672 *modep = mode;
673 return str;
674}
675
676static void load_tree(struct tree_entry *root)
677{
678 struct object_entry *myoe;
679 struct tree_content *t;
680 unsigned long size;
681 char *buf;
682 const char *c;
683 char type[20];
684
685 root->tree = t = new_tree_content(8);
686 if (!memcmp(root->sha1, null_sha1, 20))
687 return;
688
689 myoe = find_object(root->sha1);
690 if (myoe) {
691 die("FIXME");
692 } else {
693 buf = read_sha1_file(root->sha1, type, &size);
694 if (!buf || strcmp(type, tree_type))
695 die("Can't load existing tree %s", sha1_to_hex(root->sha1));
696 }
697
698 c = buf;
699 while (c != (buf + size)) {
700 struct tree_entry *e = new_tree_entry();
701
702 if (t->entry_count == t->entry_capacity)
703 root->tree = t = grow_tree_content(t, 8);
704 t->entries[t->entry_count++] = e;
705
706 e->tree = NULL;
707 c = get_mode(c, &e->mode);
708 if (!c)
709 die("Corrupt mode in %s", sha1_to_hex(root->sha1));
710 e->name = to_atom(c, strlen(c));
711 c += e->name->str_len + 1;
712 memcpy(e->sha1, c, sizeof(e->sha1));
713 c += 20;
714 }
715 free(buf);
716}
717
718static int tecmp (const void *_a, const void *_b)
719{
720 struct tree_entry *a = *((struct tree_entry**)_a);
721 struct tree_entry *b = *((struct tree_entry**)_b);
722 return base_name_compare(
723 a->name->str_dat, a->name->str_len, a->mode,
724 b->name->str_dat, b->name->str_len, b->mode);
725}
726
727static void store_tree(struct tree_entry *root)
728{
729 struct tree_content *t = root->tree;
730 unsigned int i;
731 size_t maxlen;
732 char *buf, *c;
733
734 if (memcmp(root->sha1, null_sha1, 20))
735 return;
736
737 maxlen = 0;
738 for (i = 0; i < t->entry_count; i++) {
739 maxlen += t->entries[i]->name->str_len + 34;
740 if (t->entries[i]->tree)
741 store_tree(t->entries[i]);
742 }
743
744 qsort(t->entries, t->entry_count, sizeof(t->entries[0]), tecmp);
745 buf = c = xmalloc(maxlen);
746 for (i = 0; i < t->entry_count; i++) {
747 struct tree_entry *e = t->entries[i];
748 c += sprintf(c, "%o", e->mode);
749 *c++ = ' ';
750 strcpy(c, e->name->str_dat);
751 c += e->name->str_len + 1;
752 memcpy(c, e->sha1, 20);
753 c += 20;
754 }
d8397168 755 store_object(OBJ_TREE, buf, c - buf, NULL, root->sha1, 0);
463acbe1
SP
756 free(buf);
757}
758
759static int tree_content_set(
760 struct tree_entry *root,
761 const char *p,
762 const unsigned char *sha1,
763 const unsigned int mode)
764{
765 struct tree_content *t = root->tree;
766 const char *slash1;
767 unsigned int i, n;
768 struct tree_entry *e;
769
770 slash1 = strchr(p, '/');
771 if (slash1)
772 n = slash1 - p;
773 else
774 n = strlen(p);
775
776 for (i = 0; i < t->entry_count; i++) {
777 e = t->entries[i];
778 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
779 if (!slash1) {
780 if (e->mode == mode && !memcmp(e->sha1, sha1, 20))
781 return 0;
782 e->mode = mode;
783 memcpy(e->sha1, sha1, 20);
784 if (e->tree) {
afde8dd9 785 release_tree_content_recursive(e->tree);
463acbe1
SP
786 e->tree = NULL;
787 }
788 memcpy(root->sha1, null_sha1, 20);
789 return 1;
790 }
791 if (!S_ISDIR(e->mode)) {
792 e->tree = new_tree_content(8);
7111feed 793 e->mode = S_IFDIR;
463acbe1
SP
794 }
795 if (!e->tree)
796 load_tree(e);
797 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
798 memcpy(root->sha1, null_sha1, 20);
799 return 1;
800 }
801 return 0;
802 }
803 }
804
805 if (t->entry_count == t->entry_capacity)
806 root->tree = t = grow_tree_content(t, 8);
807 e = new_tree_entry();
808 e->name = to_atom(p, n);
809 t->entries[t->entry_count++] = e;
810 if (slash1) {
811 e->tree = new_tree_content(8);
7111feed 812 e->mode = S_IFDIR;
463acbe1
SP
813 tree_content_set(e, slash1 + 1, sha1, mode);
814 } else {
815 e->tree = NULL;
816 e->mode = mode;
817 memcpy(e->sha1, sha1, 20);
818 }
819 memcpy(root->sha1, null_sha1, 20);
820 return 1;
821}
822
823static int tree_content_remove(struct tree_entry *root, const char *p)
824{
825 struct tree_content *t = root->tree;
826 const char *slash1;
827 unsigned int i, n;
828 struct tree_entry *e;
829
830 slash1 = strchr(p, '/');
831 if (slash1)
832 n = slash1 - p;
833 else
834 n = strlen(p);
835
836 for (i = 0; i < t->entry_count; i++) {
837 e = t->entries[i];
838 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
839 if (!slash1 || !S_ISDIR(e->mode))
840 goto del_entry;
841 if (!e->tree)
842 load_tree(e);
843 if (tree_content_remove(e, slash1 + 1)) {
844 if (!e->tree->entry_count)
845 goto del_entry;
846 memcpy(root->sha1, null_sha1, 20);
847 return 1;
848 }
849 return 0;
850 }
851 }
852 return 0;
853
854del_entry:
855 for (i++; i < t->entry_count; i++)
856 t->entries[i-1] = t->entries[i];
857 t->entry_count--;
858 release_tree_entry(e);
859 memcpy(root->sha1, null_sha1, 20);
ac47a738 860 return 1;
db5e523f
SP
861}
862
8bcce301 863static void init_pack_header()
db5e523f 864{
c90be46a
SP
865 struct pack_header hdr;
866
867 hdr.hdr_signature = htonl(PACK_SIGNATURE);
868 hdr.hdr_version = htonl(2);
869 hdr.hdr_entries = 0;
870
871 ywrite(pack_fd, &hdr, sizeof(hdr));
872 pack_offset = sizeof(hdr);
db5e523f
SP
873}
874
8bcce301 875static void fixup_header_footer()
db5e523f
SP
876{
877 SHA_CTX c;
878 char hdr[8];
db5e523f
SP
879 unsigned long cnt;
880 char *buf;
881 size_t n;
882
ac47a738 883 if (lseek(pack_fd, 0, SEEK_SET) != 0)
db5e523f
SP
884 die("Failed seeking to start: %s", strerror(errno));
885
886 SHA1_Init(&c);
463acbe1 887 yread(pack_fd, hdr, 8);
db5e523f
SP
888 SHA1_Update(&c, hdr, 8);
889
db5e523f
SP
890 cnt = htonl(object_count);
891 SHA1_Update(&c, &cnt, 4);
463acbe1 892 ywrite(pack_fd, &cnt, 4);
db5e523f
SP
893
894 buf = xmalloc(128 * 1024);
895 for (;;) {
ac47a738 896 n = xread(pack_fd, buf, 128 * 1024);
db5e523f
SP
897 if (n <= 0)
898 break;
899 SHA1_Update(&c, buf, n);
900 }
901 free(buf);
902
ac47a738 903 SHA1_Final(pack_sha1, &c);
463acbe1 904 ywrite(pack_fd, pack_sha1, sizeof(pack_sha1));
db5e523f
SP
905}
906
8bcce301 907static int oecmp (const void *_a, const void *_b)
db5e523f 908{
8bcce301
SP
909 struct object_entry *a = *((struct object_entry**)_a);
910 struct object_entry *b = *((struct object_entry**)_b);
911 return memcmp(a->sha1, b->sha1, sizeof(a->sha1));
912}
913
914static void write_index(const char *idx_name)
915{
916 struct sha1file *f;
917 struct object_entry **idx, **c, **last;
918 struct object_entry *e;
463acbe1 919 struct object_entry_pool *o;
8bcce301
SP
920 unsigned int array[256];
921 int i;
922
923 /* Build the sorted table of object IDs. */
924 idx = xmalloc(object_count * sizeof(struct object_entry*));
925 c = idx;
463acbe1 926 for (o = blocks; o; o = o->next_pool)
27d6d290
SP
927 for (e = o->entries; e != o->next_free; e++)
928 *c++ = e;
8bcce301
SP
929 last = idx + object_count;
930 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
931
932 /* Generate the fan-out array. */
933 c = idx;
934 for (i = 0; i < 256; i++) {
935 struct object_entry **next = c;;
936 while (next < last) {
937 if ((*next)->sha1[0] != i)
938 break;
939 next++;
940 }
941 array[i] = htonl(next - idx);
942 c = next;
943 }
944
945 f = sha1create("%s", idx_name);
946 sha1write(f, array, 256 * sizeof(int));
947 for (c = idx; c != last; c++) {
948 unsigned int offset = htonl((*c)->offset);
949 sha1write(f, &offset, 4);
950 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
951 }
ac47a738 952 sha1write(f, pack_sha1, sizeof(pack_sha1));
8bcce301
SP
953 sha1close(f, NULL, 1);
954 free(idx);
955}
956
463acbe1
SP
957static void dump_branches()
958{
959 static const char *msg = "fast-import";
960 unsigned int i;
961 struct branch *b;
962 struct ref_lock *lock;
963
964 for (i = 0; i < branch_table_sz; i++) {
965 for (b = branch_table[i]; b; b = b->table_next_branch) {
966 lock = lock_any_ref_for_update(b->name, NULL, 0);
967 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
968 die("Can't write %s", b->name);
969 }
970 }
971}
972
c44cdc7e
SP
973static void read_next_command()
974{
975 read_line(&command_buf, stdin, '\n');
976}
977
978static void cmd_mark()
979{
980 if (!strncmp("mark :", command_buf.buf, 6)) {
d8397168 981 next_mark = strtoul(command_buf.buf + 6, NULL, 10);
c44cdc7e
SP
982 read_next_command();
983 }
984 else
d8397168 985 next_mark = 0;
c44cdc7e
SP
986}
987
988static void* cmd_data (size_t *size)
989{
990 size_t n = 0;
991 void *buffer;
992 size_t length;
993
994 if (strncmp("data ", command_buf.buf, 5))
995 die("Expected 'data n' command, found: %s", command_buf.buf);
996
997 length = strtoul(command_buf.buf + 5, NULL, 10);
998 buffer = xmalloc(length);
999
1000 while (n < length) {
1001 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1002 if (!s && feof(stdin))
1003 die("EOF in data (%lu bytes remaining)", length - n);
1004 n += s;
1005 }
1006
1007 if (fgetc(stdin) != '\n')
1008 die("An lf did not trail the binary data as expected.");
1009
1010 *size = length;
1011 return buffer;
1012}
1013
463acbe1 1014static void cmd_new_blob()
6143f064 1015{
d8397168
SP
1016 size_t l;
1017 void *d;
c44cdc7e
SP
1018
1019 read_next_command();
1020 cmd_mark();
d8397168 1021 d = cmd_data(&l);
6143f064 1022
d8397168
SP
1023 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1024 free(d);
6143f064
SP
1025}
1026
463acbe1 1027static void unload_one_branch()
6bb5b329 1028{
463acbe1
SP
1029 while (cur_active_branches >= max_active_branches) {
1030 unsigned long min_commit = ULONG_MAX;
1031 struct branch *e, *l = NULL, *p = NULL;
1032
1033 for (e = active_branches; e; e = e->active_next_branch) {
1034 if (e->last_commit < min_commit) {
1035 p = l;
1036 min_commit = e->last_commit;
1037 }
1038 l = e;
1039 }
1040
1041 if (p) {
1042 e = p->active_next_branch;
1043 p->active_next_branch = e->active_next_branch;
1044 } else {
1045 e = active_branches;
1046 active_branches = e->active_next_branch;
1047 }
1048 e->active_next_branch = NULL;
1049 if (e->branch_tree.tree) {
afde8dd9 1050 release_tree_content_recursive(e->branch_tree.tree);
463acbe1
SP
1051 e->branch_tree.tree = NULL;
1052 }
1053 cur_active_branches--;
6bb5b329 1054 }
6bb5b329
SP
1055}
1056
463acbe1 1057static void load_branch(struct branch *b)
6bb5b329 1058{
463acbe1
SP
1059 load_tree(&b->branch_tree);
1060 b->active_next_branch = active_branches;
1061 active_branches = b;
1062 cur_active_branches++;
d6c7eb2c 1063 branch_load_count++;
6bb5b329
SP
1064}
1065
463acbe1 1066static void file_change_m(struct branch *b)
6bb5b329 1067{
c44cdc7e
SP
1068 const char *p = command_buf.buf + 2;
1069 char *p_uq;
1070 const char *endp;
7111feed 1071 struct object_entry *oe;
463acbe1 1072 unsigned char sha1[20];
c44cdc7e 1073 unsigned int mode;
7111feed 1074 char type[20];
6bb5b329 1075
c44cdc7e
SP
1076 p = get_mode(p, &mode);
1077 if (!p)
1078 die("Corrupt mode: %s", command_buf.buf);
1079 switch (mode) {
1080 case S_IFREG | 0644:
1081 case S_IFREG | 0755:
ace4a9d1 1082 case S_IFLNK:
c44cdc7e
SP
1083 case 0644:
1084 case 0755:
1085 /* ok */
1086 break;
1087 default:
1088 die("Corrupt mode: %s", command_buf.buf);
1089 }
1090
d8397168
SP
1091 if (*p == ':') {
1092 char *x;
1093 oe = find_mark(strtoul(p + 1, &x, 10));
1094 p = x;
1095 } else {
1096 if (get_sha1_hex(p, sha1))
1097 die("Invalid SHA1: %s", command_buf.buf);
1098 oe = find_object(sha1);
1099 p += 40;
1100 }
c44cdc7e
SP
1101 if (*p++ != ' ')
1102 die("Missing space after SHA1: %s", command_buf.buf);
1103
1104 p_uq = unquote_c_style(p, &endp);
1105 if (p_uq) {
1106 if (*endp)
1107 die("Garbage after path in: %s", command_buf.buf);
1108 p = p_uq;
1109 }
6bb5b329 1110
7111feed
SP
1111 if (oe) {
1112 if (oe->type != OBJ_BLOB)
c44cdc7e
SP
1113 die("Not a blob (actually a %s): %s",
1114 command_buf.buf, type_names[oe->type]);
7111feed
SP
1115 } else {
1116 if (sha1_object_info(sha1, type, NULL))
c44cdc7e 1117 die("Blob not found: %s", command_buf.buf);
7111feed 1118 if (strcmp(blob_type, type))
c44cdc7e
SP
1119 die("Not a blob (actually a %s): %s",
1120 command_buf.buf, type);
7111feed 1121 }
6bb5b329 1122
c44cdc7e
SP
1123 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1124
1125 if (p_uq)
1126 free(p_uq);
463acbe1 1127}
6bb5b329 1128
463acbe1
SP
1129static void file_change_d(struct branch *b)
1130{
c44cdc7e
SP
1131 const char *p = command_buf.buf + 2;
1132 char *p_uq;
1133 const char *endp;
1134
1135 p_uq = unquote_c_style(p, &endp);
1136 if (p_uq) {
1137 if (*endp)
1138 die("Garbage after path in: %s", command_buf.buf);
1139 p = p_uq;
1140 }
1141 tree_content_remove(&b->branch_tree, p);
1142 if (p_uq)
1143 free(p_uq);
6bb5b329
SP
1144}
1145
463acbe1 1146static void cmd_new_commit()
6bb5b329 1147{
c44cdc7e
SP
1148 struct branch *b;
1149 void *msg;
1150 size_t msglen;
1151 char *str_uq;
1152 const char *endp;
1153 char *sp;
1154 char *author = NULL;
1155 char *committer = NULL;
1156 char *body;
1157
1158 /* Obtain the branch name from the rest of our command */
1159 sp = strchr(command_buf.buf, ' ') + 1;
1160 str_uq = unquote_c_style(sp, &endp);
1161 if (str_uq) {
1162 if (*endp)
1163 die("Garbage after ref in: %s", command_buf.buf);
1164 sp = str_uq;
1165 }
1166 b = lookup_branch(sp);
463acbe1 1167 if (!b)
c44cdc7e
SP
1168 die("Branch not declared: %s", sp);
1169 if (str_uq)
1170 free(str_uq);
1171
1172 read_next_command();
1173 cmd_mark();
1174 if (!strncmp("author ", command_buf.buf, 7)) {
1175 author = strdup(command_buf.buf);
1176 read_next_command();
1177 }
1178 if (!strncmp("committer ", command_buf.buf, 10)) {
1179 committer = strdup(command_buf.buf);
1180 read_next_command();
1181 }
1182 if (!committer)
1183 die("Expected committer but didn't get one");
1184 msg = cmd_data(&msglen);
1185
1186 /* ensure the branch is active/loaded */
463acbe1
SP
1187 if (!b->branch_tree.tree) {
1188 unload_one_branch();
1189 load_branch(b);
1190 }
6bb5b329 1191
463acbe1
SP
1192 /* file_change* */
1193 for (;;) {
c44cdc7e
SP
1194 read_next_command();
1195 if (1 == command_buf.len)
463acbe1 1196 break;
c44cdc7e 1197 else if (!strncmp("M ", command_buf.buf, 2))
463acbe1 1198 file_change_m(b);
c44cdc7e 1199 else if (!strncmp("D ", command_buf.buf, 2))
463acbe1
SP
1200 file_change_d(b);
1201 else
c44cdc7e 1202 die("Unsupported file_change: %s", command_buf.buf);
6bb5b329 1203 }
6bb5b329 1204
c44cdc7e 1205 /* build the tree and the commit */
463acbe1 1206 store_tree(&b->branch_tree);
c44cdc7e
SP
1207 body = xmalloc(97 + msglen
1208 + (author
1209 ? strlen(author) + strlen(committer)
1210 : 2 * strlen(committer)));
1211 sp = body;
1212 sp += sprintf(sp, "tree %s\n", sha1_to_hex(b->branch_tree.sha1));
1213 if (memcmp(b->sha1, null_sha1, 20))
1214 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1215 if (author)
1216 sp += sprintf(sp, "%s\n", author);
1217 else
1218 sp += sprintf(sp, "author %s\n", committer + 10);
1219 sp += sprintf(sp, "%s\n\n", committer);
1220 memcpy(sp, msg, msglen);
1221 sp += msglen;
1222 if (author)
1223 free(author);
1224 free(committer);
1225 free(msg);
1226
d8397168 1227 store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1, next_mark);
463acbe1
SP
1228 free(body);
1229 b->last_commit = object_count_by_type[OBJ_COMMIT];
6bb5b329
SP
1230}
1231
463acbe1 1232static void cmd_new_branch()
6bb5b329 1233{
c44cdc7e
SP
1234 struct branch *b;
1235 char *str_uq;
1236 const char *endp;
1237 char *sp;
1238
1239 /* Obtain the new branch name from the rest of our command */
1240 sp = strchr(command_buf.buf, ' ') + 1;
1241 str_uq = unquote_c_style(sp, &endp);
1242 if (str_uq) {
1243 if (*endp)
1244 die("Garbage after ref in: %s", command_buf.buf);
1245 sp = str_uq;
463acbe1 1246 }
c44cdc7e
SP
1247 b = new_branch(sp);
1248 if (str_uq)
1249 free(str_uq);
1250 read_next_command();
1251
1252 /* from ... */
1253 if (!strncmp("from ", command_buf.buf, 5)) {
1254 const char *from;
1255 struct branch *s;
1256
1257 from = strchr(command_buf.buf, ' ') + 1;
1258 str_uq = unquote_c_style(from, &endp);
1259 if (str_uq) {
1260 if (*endp)
1261 die("Garbage after string in: %s", command_buf.buf);
1262 from = str_uq;
463acbe1 1263 }
c44cdc7e
SP
1264
1265 s = lookup_branch(from);
1266 if (b == s)
1267 die("Can't create a branch from itself: %s", b->name);
1268 else if (s) {
1269 memcpy(b->sha1, s->sha1, 20);
1270 memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
d8397168
SP
1271 } else if (*from == ':') {
1272 unsigned long idnum = strtoul(from + 1, NULL, 10);
1273 struct object_entry *oe = find_mark(idnum);
1274 if (oe->type != OBJ_COMMIT)
1275 die("Mark :%lu not a commit", idnum);
1276 memcpy(b->sha1, oe->sha1, 20);
1277 memcpy(b->branch_tree.sha1, null_sha1, 20);
c44cdc7e
SP
1278 } else if (!get_sha1(from, b->sha1)) {
1279 if (!memcmp(b->sha1, null_sha1, 20))
1280 memcpy(b->branch_tree.sha1, null_sha1, 20);
1281 else {
1282 unsigned long size;
1283 char *buf;
1284
1285 buf = read_object_with_reference(b->sha1,
1286 type_names[OBJ_COMMIT], &size, b->sha1);
1287 if (!buf || size < 46)
1288 die("Not a valid commit: %s", from);
1289 if (memcmp("tree ", buf, 5)
1290 || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1291 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1292 free(buf);
1293 }
1294 } else
1295 die("Invalid ref name or SHA1 expression: %s", from);
1296
1297 if (str_uq)
1298 free(str_uq);
1299 read_next_command();
1300 } else {
1301 memcpy(b->sha1, null_sha1, 20);
1302 memcpy(b->branch_tree.sha1, null_sha1, 20);
1303 }
1304
1305 if (command_buf.eof || command_buf.len > 1)
1306 die("An lf did not terminate the branch command as expected.");
6bb5b329
SP
1307}
1308
d5c57b28
SP
1309static const char fast_import_usage[] =
1310"git-fast-import [--objects=n] [--depth=n] [--active-branches=n] temp.pack";
1311
8bcce301
SP
1312int main(int argc, const char **argv)
1313{
d5c57b28
SP
1314 const char *base_name;
1315 int i;
1316 unsigned long est_obj_cnt = 1000;
8bcce301
SP
1317 char *pack_name;
1318 char *idx_name;
6143f064 1319 struct stat sb;
8bcce301 1320
463acbe1
SP
1321 setup_ident();
1322 git_config(git_default_config);
1323
d5c57b28
SP
1324 for (i = 1; i < argc; i++) {
1325 const char *a = argv[i];
1326
1327 if (*a != '-' || !strcmp(a, "--"))
1328 break;
1329 else if (!strncmp(a, "--objects=", 10))
1330 est_obj_cnt = strtoul(a + 10, NULL, 0);
1331 else if (!strncmp(a, "--depth=", 8))
1332 max_depth = strtoul(a + 8, NULL, 0);
1333 else if (!strncmp(a, "--active-branches=", 18))
1334 max_active_branches = strtoul(a + 18, NULL, 0);
1335 else
1336 die("unknown option %s", a);
1337 }
1338 if ((i+1) != argc)
1339 usage(fast_import_usage);
1340 base_name = argv[i];
1341
8bcce301
SP
1342 pack_name = xmalloc(strlen(base_name) + 6);
1343 sprintf(pack_name, "%s.pack", base_name);
1344 idx_name = xmalloc(strlen(base_name) + 5);
1345 sprintf(idx_name, "%s.idx", base_name);
1346
ac47a738
SP
1347 pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
1348 if (pack_fd < 0)
6143f064 1349 die("Can't create %s: %s", pack_name, strerror(errno));
8bcce301 1350
c44cdc7e 1351 init_pack_header();
27d6d290 1352 alloc_objects(est_obj_cnt);
c44cdc7e 1353 strbuf_init(&command_buf);
463acbe1
SP
1354
1355 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1356 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1357 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
d8397168 1358 marks = pool_calloc(1, sizeof(struct mark_set));
463acbe1 1359
db5e523f 1360 for (;;) {
c44cdc7e
SP
1361 read_next_command();
1362 if (command_buf.eof)
db5e523f 1363 break;
c44cdc7e
SP
1364 else if (!strcmp("blob", command_buf.buf))
1365 cmd_new_blob();
1366 else if (!strncmp("branch ", command_buf.buf, 7))
1367 cmd_new_branch();
1368 else if (!strncmp("commit ", command_buf.buf, 7))
1369 cmd_new_commit();
1370 else
1371 die("Unsupported command: %s", command_buf.buf);
db5e523f 1372 }
c44cdc7e 1373
db5e523f 1374 fixup_header_footer();
ac47a738 1375 close(pack_fd);
8bcce301 1376 write_index(idx_name);
463acbe1 1377 dump_branches();
8bcce301 1378
6143f064
SP
1379 fprintf(stderr, "%s statistics:\n", argv[0]);
1380 fprintf(stderr, "---------------------------------------------------\n");
1381 fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1382 fprintf(stderr, "Total objects: %10lu (%10lu duplicates)\n", object_count, duplicate_count);
1383 fprintf(stderr, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB]);
1384 fprintf(stderr, " trees : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]);
1385 fprintf(stderr, " commits: %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]);
1386 fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
d6c7eb2c 1387 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
d8397168 1388 fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count);
d6c7eb2c 1389 fprintf(stderr, " atoms: %10u\n", atom_cnt);
7111feed
SP
1390 fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1391 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1392 fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
6143f064
SP
1393 fprintf(stderr, "---------------------------------------------------\n");
1394
1395 stat(pack_name, &sb);
1396 fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1397 stat(idx_name, &sb);
1398 fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1399
1400 fprintf(stderr, "\n");
db5e523f
SP
1401
1402 return 0;
1403}