]> git.ipfire.org Git - thirdparty/git.git/blame - fast-import.c
Correct max_packsize default in fast-import.
[thirdparty/git.git] / fast-import.c
CommitLineData
463acbe1
SP
1/*
2Format of STDIN stream:
3
4 stream ::= cmd*;
5
6 cmd ::= new_blob
c44cdc7e 7 | new_commit
463acbe1 8 | new_tag
5fced8dc 9 | reset_branch
463acbe1
SP
10 ;
11
c44cdc7e
SP
12 new_blob ::= 'blob' lf
13 mark?
14 file_content;
15 file_content ::= data;
463acbe1 16
c44cdc7e 17 new_commit ::= 'commit' sp ref_str lf
00e2b884
SP
18 mark?
19 ('author' sp name '<' email '>' ts tz lf)?
20 'committer' sp name '<' email '>' ts tz lf
21 commit_msg
02f3389d 22 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
62b6f483 23 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
c44cdc7e
SP
24 file_change*
25 lf;
26 commit_msg ::= data;
463acbe1 27
c44cdc7e
SP
28 file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
29 | 'D' sp path_str lf
463acbe1 30 ;
c44cdc7e
SP
31 mode ::= '644' | '755';
32
33 new_tag ::= 'tag' sp tag_str lf
34 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
35 'tagger' sp name '<' email '>' ts tz lf
36 tag_msg;
37 tag_msg ::= data;
38
9938ffc5
SP
39 reset_branch ::= 'reset' sp ref_str lf
40 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
41 lf;
5fced8dc 42
7bfe6e26
SP
43 checkpoint ::= 'checkpoint' lf
44 lf;
45
c44cdc7e
SP
46 # note: the first idnum in a stream should be 1 and subsequent
47 # idnums should not have gaps between values as this will cause
48 # the stream parser to reserve space for the gapped values. An
49 # idnum can be updated in the future to a new object by issuing
50 # a new mark directive with the old idnum.
51 #
52 mark ::= 'mark' sp idnum lf;
53
54 # note: declen indicates the length of binary_data in bytes.
55 # declen does not include the lf preceeding or trailing the
56 # binary data.
57 #
58 data ::= 'data' sp declen lf
59 binary_data
60 lf;
61
62 # note: quoted strings are C-style quoting supporting \c for
63 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
64 # is the signed byte value in octal. Note that the only
65 # characters which must actually be escaped to protect the
66 # stream formatting is: \, " and LF. Otherwise these values
67 # are UTF8.
68 #
69 ref_str ::= ref | '"' quoted(ref) '"' ;
70 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
71 tag_str ::= tag | '"' quoted(tag) '"' ;
72 path_str ::= path | '"' quoted(path) '"' ;
73
74 declen ::= # unsigned 32 bit value, ascii base10 notation;
463acbe1 75 binary_data ::= # file content, not interpreted;
c44cdc7e 76
463acbe1
SP
77 sp ::= # ASCII space character;
78 lf ::= # ASCII newline (LF) character;
c44cdc7e
SP
79
80 # note: a colon (':') must precede the numerical value assigned to
81 # an idnum. This is to distinguish it from a ref or tag name as
82 # GIT does not permit ':' in ref or tag strings.
83 #
84 idnum ::= ':' declen;
85 path ::= # GIT style file path, e.g. "a/b/c";
86 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
87 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
463acbe1
SP
88 sha1exp ::= # Any valid GIT SHA1 expression;
89 hexsha1 ::= # SHA1 in hexadecimal format;
c44cdc7e
SP
90
91 # note: name and email are UTF8 strings, however name must not
92 # contain '<' or lf and email must not contain any of the
93 # following: '<', '>', lf.
94 #
95 name ::= # valid GIT author/committer name;
463acbe1 96 email ::= # valid GIT author/committer email;
c44cdc7e
SP
97 ts ::= # time since the epoch in seconds, ascii base10 notation;
98 tz ::= # GIT style timezone;
463acbe1
SP
99*/
100
db5e523f
SP
101#include "builtin.h"
102#include "cache.h"
103#include "object.h"
104#include "blob.h"
463acbe1 105#include "tree.h"
db5e523f
SP
106#include "delta.h"
107#include "pack.h"
463acbe1 108#include "refs.h"
db5e523f 109#include "csum-file.h"
c44cdc7e
SP
110#include "strbuf.h"
111#include "quote.h"
db5e523f 112
27d6d290
SP
113struct object_entry
114{
115 struct object_entry *next;
116 unsigned long offset;
03842d8e 117 unsigned type : TYPE_BITS;
7bfe6e26 118 unsigned pack_id : 16;
27d6d290
SP
119 unsigned char sha1[20];
120};
121
463acbe1 122struct object_entry_pool
27d6d290 123{
463acbe1 124 struct object_entry_pool *next_pool;
27d6d290
SP
125 struct object_entry *next_free;
126 struct object_entry *end;
ac47a738 127 struct object_entry entries[FLEX_ARRAY]; /* more */
27d6d290
SP
128};
129
d8397168
SP
130struct mark_set
131{
132 int shift;
133 union {
134 struct object_entry *marked[1024];
135 struct mark_set *sets[1024];
136 } data;
137};
138
ac47a738
SP
139struct last_object
140{
141 void *data;
4cabf858 142 unsigned long len;
d489bc14 143 unsigned long offset;
6bb5b329 144 unsigned int depth;
d489bc14 145 unsigned no_free:1;
ac47a738
SP
146};
147
463acbe1
SP
148struct mem_pool
149{
150 struct mem_pool *next_pool;
151 char *next_free;
152 char *end;
153 char space[FLEX_ARRAY]; /* more */
154};
155
156struct atom_str
157{
158 struct atom_str *next_atom;
159 int str_len;
160 char str_dat[FLEX_ARRAY]; /* more */
161};
162
163struct tree_content;
6bb5b329
SP
164struct tree_entry
165{
463acbe1
SP
166 struct tree_content *tree;
167 struct atom_str* name;
4cabf858
SP
168 struct tree_entry_ms
169 {
170 unsigned int mode;
171 unsigned char sha1[20];
172 } versions[2];
6bb5b329
SP
173};
174
463acbe1 175struct tree_content
6bb5b329 176{
463acbe1
SP
177 unsigned int entry_capacity; /* must match avail_tree_content */
178 unsigned int entry_count;
4cabf858 179 unsigned int delta_depth;
463acbe1
SP
180 struct tree_entry *entries[FLEX_ARRAY]; /* more */
181};
182
183struct avail_tree_content
184{
185 unsigned int entry_capacity; /* must match tree_content */
186 struct avail_tree_content *next_avail;
6bb5b329
SP
187};
188
189struct branch
190{
463acbe1
SP
191 struct branch *table_next_branch;
192 struct branch *active_next_branch;
6bb5b329 193 const char *name;
463acbe1
SP
194 unsigned long last_commit;
195 struct tree_entry branch_tree;
196 unsigned char sha1[20];
6bb5b329
SP
197};
198
72303d44
SP
199struct tag
200{
201 struct tag *next_tag;
202 const char *name;
203 unsigned char sha1[20];
204};
205
e2eb469d
SP
206struct dbuf
207{
208 void *buffer;
209 size_t capacity;
210};
211
62b6f483
SP
212struct hash_list
213{
214 struct hash_list *next;
215 unsigned char sha1[20];
216};
463acbe1 217
0ea9f045 218/* Configured limits on output */
d5c57b28 219static unsigned long max_depth = 10;
eec11c24 220static unsigned long max_packsize = (1LL << 32) - 1;
0ea9f045
SP
221static uintmax_t max_objects = -1;
222
223/* Stats and misc. counters */
224static uintmax_t alloc_count;
225static uintmax_t object_count;
226static uintmax_t marks_set_count;
227static uintmax_t object_count_by_type[1 << TYPE_BITS];
228static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
229static uintmax_t delta_count_by_type[1 << TYPE_BITS];
6bb5b329 230static unsigned long branch_count;
d6c7eb2c 231static unsigned long branch_load_count;
ac47a738 232
463acbe1
SP
233/* Memory pools */
234static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
235static size_t total_allocd;
236static struct mem_pool *mem_pool;
237
c44cdc7e 238/* Atom management */
463acbe1
SP
239static unsigned int atom_table_sz = 4451;
240static unsigned int atom_cnt;
241static struct atom_str **atom_table;
242
243/* The .pack file being generated */
7bfe6e26 244static unsigned int pack_id;
d489bc14 245static struct packed_git *pack_data;
7bfe6e26 246static struct packed_git **all_packs;
41e5257f 247static unsigned long pack_size;
ac47a738
SP
248
249/* Table of objects we've written. */
4cabf858 250static unsigned int object_entry_alloc = 5000;
463acbe1
SP
251static struct object_entry_pool *blocks;
252static struct object_entry *object_table[1 << 16];
d8397168 253static struct mark_set *marks;
a6a1a831 254static const char* mark_file;
ac47a738
SP
255
256/* Our last blob */
463acbe1
SP
257static struct last_object last_blob;
258
259/* Tree management */
260static unsigned int tree_entry_alloc = 1000;
261static void *avail_tree_entry;
262static unsigned int avail_tree_table_sz = 100;
263static struct avail_tree_content **avail_tree_table;
e2eb469d
SP
264static struct dbuf old_tree;
265static struct dbuf new_tree;
8bcce301 266
6bb5b329 267/* Branch data */
d5c57b28
SP
268static unsigned long max_active_branches = 5;
269static unsigned long cur_active_branches;
270static unsigned long branch_table_sz = 1039;
463acbe1
SP
271static struct branch **branch_table;
272static struct branch *active_branches;
273
72303d44
SP
274/* Tag data */
275static struct tag *first_tag;
276static struct tag *last_tag;
277
c44cdc7e
SP
278/* Input stream parsing */
279static struct strbuf command_buf;
0ea9f045 280static uintmax_t next_mark;
243f801d 281static struct dbuf new_data;
264244a0 282static FILE* branch_log;
c44cdc7e 283
6bb5b329 284
03842d8e 285static void alloc_objects(unsigned int cnt)
8bcce301 286{
463acbe1 287 struct object_entry_pool *b;
27d6d290 288
463acbe1 289 b = xmalloc(sizeof(struct object_entry_pool)
27d6d290 290 + cnt * sizeof(struct object_entry));
463acbe1 291 b->next_pool = blocks;
27d6d290
SP
292 b->next_free = b->entries;
293 b->end = b->entries + cnt;
294 blocks = b;
295 alloc_count += cnt;
296}
8bcce301 297
27d6d290 298static struct object_entry* new_object(unsigned char *sha1)
8bcce301 299{
27d6d290 300 struct object_entry *e;
8bcce301 301
27d6d290 302 if (blocks->next_free == blocks->end)
463acbe1 303 alloc_objects(object_entry_alloc);
8bcce301 304
27d6d290 305 e = blocks->next_free++;
445b8599 306 hashcpy(e->sha1, sha1);
27d6d290 307 return e;
8bcce301
SP
308}
309
463acbe1
SP
310static struct object_entry* find_object(unsigned char *sha1)
311{
312 unsigned int h = sha1[0] << 8 | sha1[1];
313 struct object_entry *e;
314 for (e = object_table[h]; e; e = e->next)
445b8599 315 if (!hashcmp(sha1, e->sha1))
463acbe1
SP
316 return e;
317 return NULL;
318}
319
8bcce301
SP
320static struct object_entry* insert_object(unsigned char *sha1)
321{
322 unsigned int h = sha1[0] << 8 | sha1[1];
27d6d290 323 struct object_entry *e = object_table[h];
463acbe1 324 struct object_entry *p = NULL;
8bcce301
SP
325
326 while (e) {
445b8599 327 if (!hashcmp(sha1, e->sha1))
8bcce301
SP
328 return e;
329 p = e;
330 e = e->next;
331 }
332
333 e = new_object(sha1);
463acbe1 334 e->next = NULL;
8bcce301
SP
335 e->offset = 0;
336 if (p)
337 p->next = e;
338 else
27d6d290 339 object_table[h] = e;
8bcce301
SP
340 return e;
341}
db5e523f 342
463acbe1
SP
343static unsigned int hc_str(const char *s, size_t len)
344{
345 unsigned int r = 0;
346 while (len-- > 0)
347 r = r * 31 + *s++;
348 return r;
349}
350
351static void* pool_alloc(size_t len)
352{
353 struct mem_pool *p;
354 void *r;
355
356 for (p = mem_pool; p; p = p->next_pool)
357 if ((p->end - p->next_free >= len))
358 break;
359
360 if (!p) {
361 if (len >= (mem_pool_alloc/2)) {
362 total_allocd += len;
363 return xmalloc(len);
364 }
365 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
366 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
367 p->next_pool = mem_pool;
368 p->next_free = p->space;
369 p->end = p->next_free + mem_pool_alloc;
370 mem_pool = p;
371 }
372
373 r = p->next_free;
8d8928b0
SP
374 /* round out to a pointer alignment */
375 if (len & (sizeof(void*) - 1))
376 len += sizeof(void*) - (len & (sizeof(void*) - 1));
463acbe1
SP
377 p->next_free += len;
378 return r;
379}
380
381static void* pool_calloc(size_t count, size_t size)
382{
383 size_t len = count * size;
384 void *r = pool_alloc(len);
385 memset(r, 0, len);
386 return r;
387}
388
389static char* pool_strdup(const char *s)
390{
391 char *r = pool_alloc(strlen(s) + 1);
392 strcpy(r, s);
393 return r;
394}
395
243f801d
SP
396static void size_dbuf(struct dbuf *b, size_t maxlen)
397{
398 if (b->buffer) {
399 if (b->capacity >= maxlen)
400 return;
401 free(b->buffer);
402 }
403 b->capacity = ((maxlen / 1024) + 1) * 1024;
404 b->buffer = xmalloc(b->capacity);
405}
406
0ea9f045 407static void insert_mark(uintmax_t idnum, struct object_entry *oe)
d8397168
SP
408{
409 struct mark_set *s = marks;
410 while ((idnum >> s->shift) >= 1024) {
411 s = pool_calloc(1, sizeof(struct mark_set));
412 s->shift = marks->shift + 10;
413 s->data.sets[0] = marks;
414 marks = s;
415 }
416 while (s->shift) {
0ea9f045 417 uintmax_t i = idnum >> s->shift;
d8397168
SP
418 idnum -= i << s->shift;
419 if (!s->data.sets[i]) {
420 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
421 s->data.sets[i]->shift = s->shift - 10;
422 }
423 s = s->data.sets[i];
424 }
425 if (!s->data.marked[idnum])
426 marks_set_count++;
427 s->data.marked[idnum] = oe;
428}
429
0ea9f045 430static struct object_entry* find_mark(uintmax_t idnum)
d8397168 431{
0ea9f045 432 uintmax_t orig_idnum = idnum;
d8397168
SP
433 struct mark_set *s = marks;
434 struct object_entry *oe = NULL;
435 if ((idnum >> s->shift) < 1024) {
436 while (s && s->shift) {
0ea9f045 437 uintmax_t i = idnum >> s->shift;
d8397168
SP
438 idnum -= i << s->shift;
439 s = s->data.sets[i];
440 }
441 if (s)
442 oe = s->data.marked[idnum];
443 }
444 if (!oe)
0ea9f045 445 die("mark :%ju not declared", orig_idnum);
d8397168
SP
446 return oe;
447}
448
463acbe1
SP
449static struct atom_str* to_atom(const char *s, size_t len)
450{
451 unsigned int hc = hc_str(s, len) % atom_table_sz;
452 struct atom_str *c;
453
454 for (c = atom_table[hc]; c; c = c->next_atom)
455 if (c->str_len == len && !strncmp(s, c->str_dat, len))
456 return c;
457
458 c = pool_alloc(sizeof(struct atom_str) + len + 1);
459 c->str_len = len;
460 strncpy(c->str_dat, s, len);
461 c->str_dat[len] = 0;
462 c->next_atom = atom_table[hc];
463 atom_table[hc] = c;
464 atom_cnt++;
465 return c;
466}
467
468static struct branch* lookup_branch(const char *name)
469{
470 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
471 struct branch *b;
472
473 for (b = branch_table[hc]; b; b = b->table_next_branch)
474 if (!strcmp(name, b->name))
475 return b;
476 return NULL;
477}
478
479static struct branch* new_branch(const char *name)
480{
481 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
482 struct branch* b = lookup_branch(name);
483
484 if (b)
485 die("Invalid attempt to create duplicate branch: %s", name);
c44cdc7e
SP
486 if (check_ref_format(name))
487 die("Branch name doesn't conform to GIT standards: %s", name);
463acbe1
SP
488
489 b = pool_calloc(1, sizeof(struct branch));
490 b->name = pool_strdup(name);
491 b->table_next_branch = branch_table[hc];
8a8c55ea
SP
492 b->branch_tree.versions[0].mode = S_IFDIR;
493 b->branch_tree.versions[1].mode = S_IFDIR;
463acbe1
SP
494 branch_table[hc] = b;
495 branch_count++;
496 return b;
497}
498
499static unsigned int hc_entries(unsigned int cnt)
500{
501 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
502 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
503}
504
505static struct tree_content* new_tree_content(unsigned int cnt)
506{
507 struct avail_tree_content *f, *l = NULL;
508 struct tree_content *t;
509 unsigned int hc = hc_entries(cnt);
510
511 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
512 if (f->entry_capacity >= cnt)
513 break;
514
515 if (f) {
516 if (l)
517 l->next_avail = f->next_avail;
518 else
519 avail_tree_table[hc] = f->next_avail;
520 } else {
521 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
522 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
523 f->entry_capacity = cnt;
524 }
525
526 t = (struct tree_content*)f;
527 t->entry_count = 0;
4cabf858 528 t->delta_depth = 0;
463acbe1
SP
529 return t;
530}
531
532static void release_tree_entry(struct tree_entry *e);
533static void release_tree_content(struct tree_content *t)
534{
535 struct avail_tree_content *f = (struct avail_tree_content*)t;
536 unsigned int hc = hc_entries(f->entry_capacity);
afde8dd9
SP
537 f->next_avail = avail_tree_table[hc];
538 avail_tree_table[hc] = f;
539}
540
541static void release_tree_content_recursive(struct tree_content *t)
542{
463acbe1
SP
543 unsigned int i;
544 for (i = 0; i < t->entry_count; i++)
545 release_tree_entry(t->entries[i]);
afde8dd9 546 release_tree_content(t);
463acbe1
SP
547}
548
549static struct tree_content* grow_tree_content(
550 struct tree_content *t,
551 int amt)
552{
553 struct tree_content *r = new_tree_content(t->entry_count + amt);
554 r->entry_count = t->entry_count;
4cabf858 555 r->delta_depth = t->delta_depth;
463acbe1
SP
556 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
557 release_tree_content(t);
558 return r;
559}
560
561static struct tree_entry* new_tree_entry()
562{
563 struct tree_entry *e;
564
565 if (!avail_tree_entry) {
566 unsigned int n = tree_entry_alloc;
8435a9cb 567 total_allocd += n * sizeof(struct tree_entry);
463acbe1 568 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
2eb26d84 569 while (n-- > 1) {
463acbe1
SP
570 *((void**)e) = e + 1;
571 e++;
572 }
35ef237c 573 *((void**)e) = NULL;
463acbe1
SP
574 }
575
576 e = avail_tree_entry;
577 avail_tree_entry = *((void**)e);
578 return e;
579}
580
581static void release_tree_entry(struct tree_entry *e)
582{
583 if (e->tree)
afde8dd9 584 release_tree_content_recursive(e->tree);
463acbe1
SP
585 *((void**)e) = avail_tree_entry;
586 avail_tree_entry = e;
587}
588
f70b6534
SP
589static void start_packfile()
590{
8455e484 591 static char tmpfile[PATH_MAX];
7bfe6e26 592 struct packed_git *p;
f70b6534 593 struct pack_header hdr;
0fcbcae7 594 int pack_fd;
f70b6534 595
8455e484
SP
596 snprintf(tmpfile, sizeof(tmpfile),
597 "%s/pack_XXXXXX", get_object_directory());
598 pack_fd = mkstemp(tmpfile);
f70b6534 599 if (pack_fd < 0)
8455e484
SP
600 die("Can't create %s: %s", tmpfile, strerror(errno));
601 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
602 strcpy(p->pack_name, tmpfile);
7bfe6e26 603 p->pack_fd = pack_fd;
f70b6534
SP
604
605 hdr.hdr_signature = htonl(PACK_SIGNATURE);
606 hdr.hdr_version = htonl(2);
607 hdr.hdr_entries = 0;
0fcbcae7 608 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
7bfe6e26
SP
609
610 pack_data = p;
f70b6534
SP
611 pack_size = sizeof(hdr);
612 object_count = 0;
7bfe6e26
SP
613
614 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
615 all_packs[pack_id] = p;
f70b6534
SP
616}
617
618static void fixup_header_footer()
619{
0fcbcae7 620 int pack_fd = pack_data->pack_fd;
f70b6534
SP
621 SHA_CTX c;
622 char hdr[8];
623 unsigned long cnt;
624 char *buf;
625
626 if (lseek(pack_fd, 0, SEEK_SET) != 0)
627 die("Failed seeking to start: %s", strerror(errno));
628
629 SHA1_Init(&c);
6cf09261
SP
630 if (read_in_full(pack_fd, hdr, 8) != 8)
631 die("Unable to reread header of %s", pack_data->pack_name);
f70b6534
SP
632 SHA1_Update(&c, hdr, 8);
633
634 cnt = htonl(object_count);
635 SHA1_Update(&c, &cnt, 4);
636 write_or_die(pack_fd, &cnt, 4);
637
638 buf = xmalloc(128 * 1024);
639 for (;;) {
640 size_t n = xread(pack_fd, buf, 128 * 1024);
641 if (n <= 0)
642 break;
643 SHA1_Update(&c, buf, n);
644 }
645 free(buf);
646
09543c96
SP
647 SHA1_Final(pack_data->sha1, &c);
648 write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
12801587 649 close(pack_fd);
f70b6534
SP
650}
651
652static int oecmp (const void *a_, const void *b_)
653{
654 struct object_entry *a = *((struct object_entry**)a_);
655 struct object_entry *b = *((struct object_entry**)b_);
656 return hashcmp(a->sha1, b->sha1);
657}
658
8455e484 659static char* create_index()
f70b6534 660{
8455e484
SP
661 static char tmpfile[PATH_MAX];
662 SHA_CTX ctx;
f70b6534
SP
663 struct sha1file *f;
664 struct object_entry **idx, **c, **last, *e;
665 struct object_entry_pool *o;
666 unsigned int array[256];
8455e484 667 int i, idx_fd;
f70b6534
SP
668
669 /* Build the sorted table of object IDs. */
670 idx = xmalloc(object_count * sizeof(struct object_entry*));
671 c = idx;
672 for (o = blocks; o; o = o->next_pool)
d9ee53ce
SP
673 for (e = o->next_free; e-- != o->entries;)
674 if (pack_id == e->pack_id)
675 *c++ = e;
f70b6534 676 last = idx + object_count;
2fce1f3c
SP
677 if (c != last)
678 die("internal consistency error creating the index");
f70b6534
SP
679 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
680
681 /* Generate the fan-out array. */
682 c = idx;
683 for (i = 0; i < 256; i++) {
684 struct object_entry **next = c;;
685 while (next < last) {
686 if ((*next)->sha1[0] != i)
687 break;
688 next++;
689 }
690 array[i] = htonl(next - idx);
691 c = next;
692 }
693
8455e484
SP
694 snprintf(tmpfile, sizeof(tmpfile),
695 "%s/index_XXXXXX", get_object_directory());
696 idx_fd = mkstemp(tmpfile);
697 if (idx_fd < 0)
698 die("Can't create %s: %s", tmpfile, strerror(errno));
699 f = sha1fd(idx_fd, tmpfile);
f70b6534 700 sha1write(f, array, 256 * sizeof(int));
8455e484 701 SHA1_Init(&ctx);
f70b6534
SP
702 for (c = idx; c != last; c++) {
703 unsigned int offset = htonl((*c)->offset);
704 sha1write(f, &offset, 4);
705 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
8455e484 706 SHA1_Update(&ctx, (*c)->sha1, 20);
f70b6534 707 }
09543c96 708 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
8455e484 709 sha1close(f, NULL, 1);
f70b6534 710 free(idx);
8455e484
SP
711 SHA1_Final(pack_data->sha1, &ctx);
712 return tmpfile;
713}
714
715static char* keep_pack(char *curr_index_name)
716{
717 static char name[PATH_MAX];
718 static char *keep_msg = "fast-import";
719 int keep_fd;
720
721 chmod(pack_data->pack_name, 0444);
722 chmod(curr_index_name, 0444);
723
724 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
725 get_object_directory(), sha1_to_hex(pack_data->sha1));
726 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
727 if (keep_fd < 0)
728 die("cannot create keep file");
729 write(keep_fd, keep_msg, strlen(keep_msg));
730 close(keep_fd);
731
732 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
733 get_object_directory(), sha1_to_hex(pack_data->sha1));
734 if (move_temp_to_file(pack_data->pack_name, name))
735 die("cannot store pack file");
736 printf("%s\n", name);
737
738 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
739 get_object_directory(), sha1_to_hex(pack_data->sha1));
740 if (move_temp_to_file(curr_index_name, name))
741 die("cannot store index file");
742 return name;
743}
744
745static void unkeep_all_packs()
746{
747 static char name[PATH_MAX];
748 int k;
749
750 for (k = 0; k < pack_id; k++) {
751 struct packed_git *p = all_packs[k];
752 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
753 get_object_directory(), sha1_to_hex(p->sha1));
754 unlink(name);
755 }
f70b6534
SP
756}
757
758static void end_packfile()
759{
7bfe6e26
SP
760 struct packed_git *old_p = pack_data, *new_p;
761
3e005baf 762 if (object_count) {
8455e484
SP
763 char *idx_name;
764
3e005baf 765 fixup_header_footer();
8455e484 766 idx_name = keep_pack(create_index());
3e005baf
SP
767
768 /* Register the packfile with core git's machinary. */
769 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
770 if (!new_p)
771 die("core git rejected index %s", idx_name);
772 new_p->windows = old_p->windows;
3e005baf
SP
773 all_packs[pack_id++] = new_p;
774 install_packed_git(new_p);
775 }
12801587 776 else
3e005baf 777 unlink(old_p->pack_name);
7bfe6e26 778 free(old_p);
7bfe6e26
SP
779
780 /* We can't carry a delta across packfiles. */
781 free(last_blob.data);
782 last_blob.data = NULL;
783 last_blob.len = 0;
784 last_blob.offset = 0;
785 last_blob.depth = 0;
f70b6534
SP
786}
787
d9ee53ce
SP
788static void checkpoint()
789{
790 end_packfile();
791 start_packfile();
792}
793
c44cdc7e 794static size_t encode_header(
ac47a738 795 enum object_type type,
c44cdc7e 796 size_t size,
ac47a738 797 unsigned char *hdr)
db5e523f
SP
798{
799 int n = 1;
800 unsigned char c;
801
1fcdd62a 802 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
db5e523f
SP
803 die("bad type %d", type);
804
805 c = (type << 4) | (size & 15);
806 size >>= 4;
807 while (size) {
808 *hdr++ = c | 0x80;
809 c = size & 0x7f;
810 size >>= 7;
811 n++;
812 }
813 *hdr = c;
814 return n;
815}
816
ac47a738
SP
817static int store_object(
818 enum object_type type,
819 void *dat,
c44cdc7e 820 size_t datlen,
6bb5b329 821 struct last_object *last,
d8397168 822 unsigned char *sha1out,
0ea9f045 823 uintmax_t mark)
db5e523f 824{
db5e523f 825 void *out, *delta;
ac47a738
SP
826 struct object_entry *e;
827 unsigned char hdr[96];
828 unsigned char sha1[20];
db5e523f 829 unsigned long hdrlen, deltalen;
ac47a738
SP
830 SHA_CTX c;
831 z_stream s;
832
833 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
834 SHA1_Init(&c);
835 SHA1_Update(&c, hdr, hdrlen);
836 SHA1_Update(&c, dat, datlen);
837 SHA1_Final(sha1, &c);
6bb5b329 838 if (sha1out)
445b8599 839 hashcpy(sha1out, sha1);
ac47a738
SP
840
841 e = insert_object(sha1);
d8397168
SP
842 if (mark)
843 insert_mark(mark, e);
ac47a738 844 if (e->offset) {
6143f064 845 duplicate_count_by_type[type]++;
463acbe1 846 return 1;
ac47a738 847 }
db5e523f 848
d9ee53ce 849 if (last && last->data && last->depth < max_depth) {
ac47a738 850 delta = diff_delta(last->data, last->len,
db5e523f
SP
851 dat, datlen,
852 &deltalen, 0);
d9ee53ce
SP
853 if (delta && deltalen >= datlen) {
854 free(delta);
855 delta = NULL;
856 }
857 } else
858 delta = NULL;
db5e523f
SP
859
860 memset(&s, 0, sizeof(s));
861 deflateInit(&s, zlib_compression_level);
d9ee53ce
SP
862 if (delta) {
863 s.next_in = delta;
864 s.avail_in = deltalen;
865 } else {
866 s.next_in = dat;
867 s.avail_in = datlen;
868 }
869 s.avail_out = deflateBound(&s, s.avail_in);
870 s.next_out = out = xmalloc(s.avail_out);
871 while (deflate(&s, Z_FINISH) == Z_OK)
872 /* nothing */;
873 deflateEnd(&s);
874
875 /* Determine if we should auto-checkpoint. */
876 if ((object_count + 1) > max_objects
877 || (object_count + 1) < object_count
878 || (pack_size + 60 + s.total_out) > max_packsize
879 || (pack_size + 60 + s.total_out) < pack_size) {
880
881 /* This new object needs to *not* have the current pack_id. */
882 e->pack_id = pack_id + 1;
883 checkpoint();
884
885 /* We cannot carry a delta into the new pack. */
886 if (delta) {
887 free(delta);
888 delta = NULL;
5d6f3ef6
SP
889
890 memset(&s, 0, sizeof(s));
891 deflateInit(&s, zlib_compression_level);
892 s.next_in = dat;
893 s.avail_in = datlen;
894 s.avail_out = deflateBound(&s, s.avail_in);
895 s.next_out = out = xrealloc(out, s.avail_out);
896 while (deflate(&s, Z_FINISH) == Z_OK)
897 /* nothing */;
898 deflateEnd(&s);
d9ee53ce 899 }
d9ee53ce
SP
900 }
901
902 e->type = type;
903 e->pack_id = pack_id;
904 e->offset = pack_size;
905 object_count++;
906 object_count_by_type[type]++;
db5e523f
SP
907
908 if (delta) {
d489bc14
SP
909 unsigned long ofs = e->offset - last->offset;
910 unsigned pos = sizeof(hdr) - 1;
911
4cabf858 912 delta_count_by_type[type]++;
ac47a738 913 last->depth++;
d489bc14
SP
914
915 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
0fcbcae7 916 write_or_die(pack_data->pack_fd, hdr, hdrlen);
d489bc14
SP
917 pack_size += hdrlen;
918
919 hdr[pos] = ofs & 127;
920 while (ofs >>= 7)
921 hdr[--pos] = 128 | (--ofs & 127);
0fcbcae7 922 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
d489bc14 923 pack_size += sizeof(hdr) - pos;
db5e523f 924 } else {
463acbe1
SP
925 if (last)
926 last->depth = 0;
ac47a738 927 hdrlen = encode_header(type, datlen, hdr);
0fcbcae7 928 write_or_die(pack_data->pack_fd, hdr, hdrlen);
41e5257f 929 pack_size += hdrlen;
db5e523f
SP
930 }
931
0fcbcae7 932 write_or_die(pack_data->pack_fd, out, s.total_out);
41e5257f 933 pack_size += s.total_out;
db5e523f
SP
934
935 free(out);
936 if (delta)
937 free(delta);
463acbe1 938 if (last) {
e2eb469d 939 if (last->data && !last->no_free)
463acbe1
SP
940 free(last->data);
941 last->data = dat;
d489bc14 942 last->offset = e->offset;
463acbe1 943 last->len = datlen;
463acbe1
SP
944 }
945 return 0;
946}
947
7bfe6e26
SP
948static void *gfi_unpack_entry(
949 struct object_entry *oe,
950 unsigned long *sizep)
41e5257f 951{
7bfe6e26
SP
952 static char type[20];
953 struct packed_git *p = all_packs[oe->pack_id];
954 if (p == pack_data)
955 p->pack_size = pack_size + 20;
956 return unpack_entry(p, oe->offset, type, sizep);
41e5257f
SP
957}
958
463acbe1
SP
959static const char *get_mode(const char *str, unsigned int *modep)
960{
961 unsigned char c;
962 unsigned int mode = 0;
963
964 while ((c = *str++) != ' ') {
965 if (c < '0' || c > '7')
966 return NULL;
967 mode = (mode << 3) + (c - '0');
968 }
969 *modep = mode;
970 return str;
971}
972
973static void load_tree(struct tree_entry *root)
974{
4cabf858 975 unsigned char* sha1 = root->versions[1].sha1;
463acbe1
SP
976 struct object_entry *myoe;
977 struct tree_content *t;
978 unsigned long size;
979 char *buf;
980 const char *c;
463acbe1
SP
981
982 root->tree = t = new_tree_content(8);
4cabf858 983 if (is_null_sha1(sha1))
463acbe1
SP
984 return;
985
4cabf858 986 myoe = find_object(sha1);
463acbe1 987 if (myoe) {
41e5257f 988 if (myoe->type != OBJ_TREE)
4cabf858 989 die("Not a tree: %s", sha1_to_hex(sha1));
d489bc14 990 t->delta_depth = 0;
7bfe6e26 991 buf = gfi_unpack_entry(myoe, &size);
463acbe1 992 } else {
41e5257f 993 char type[20];
4cabf858 994 buf = read_sha1_file(sha1, type, &size);
00e2b884 995 if (!buf || strcmp(type, tree_type))
4cabf858 996 die("Can't load tree %s", sha1_to_hex(sha1));
463acbe1
SP
997 }
998
999 c = buf;
1000 while (c != (buf + size)) {
1001 struct tree_entry *e = new_tree_entry();
1002
1003 if (t->entry_count == t->entry_capacity)
1004 root->tree = t = grow_tree_content(t, 8);
1005 t->entries[t->entry_count++] = e;
1006
1007 e->tree = NULL;
4cabf858 1008 c = get_mode(c, &e->versions[1].mode);
463acbe1 1009 if (!c)
4cabf858
SP
1010 die("Corrupt mode in %s", sha1_to_hex(sha1));
1011 e->versions[0].mode = e->versions[1].mode;
463acbe1
SP
1012 e->name = to_atom(c, strlen(c));
1013 c += e->name->str_len + 1;
4cabf858
SP
1014 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1015 hashcpy(e->versions[1].sha1, (unsigned char*)c);
463acbe1
SP
1016 c += 20;
1017 }
1018 free(buf);
1019}
1020
4cabf858 1021static int tecmp0 (const void *_a, const void *_b)
463acbe1
SP
1022{
1023 struct tree_entry *a = *((struct tree_entry**)_a);
1024 struct tree_entry *b = *((struct tree_entry**)_b);
1025 return base_name_compare(
4cabf858
SP
1026 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1027 b->name->str_dat, b->name->str_len, b->versions[0].mode);
463acbe1
SP
1028}
1029
4cabf858 1030static int tecmp1 (const void *_a, const void *_b)
463acbe1 1031{
4cabf858
SP
1032 struct tree_entry *a = *((struct tree_entry**)_a);
1033 struct tree_entry *b = *((struct tree_entry**)_b);
1034 return base_name_compare(
1035 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1036 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1037}
1038
e2eb469d
SP
1039static void mktree(struct tree_content *t,
1040 int v,
1041 unsigned long *szp,
1042 struct dbuf *b)
4cabf858
SP
1043{
1044 size_t maxlen = 0;
463acbe1 1045 unsigned int i;
e2eb469d 1046 char *c;
463acbe1 1047
4cabf858
SP
1048 if (!v)
1049 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1050 else
1051 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
463acbe1 1052
463acbe1 1053 for (i = 0; i < t->entry_count; i++) {
4cabf858
SP
1054 if (t->entries[i]->versions[v].mode)
1055 maxlen += t->entries[i]->name->str_len + 34;
463acbe1
SP
1056 }
1057
243f801d 1058 size_dbuf(b, maxlen);
e2eb469d 1059 c = b->buffer;
463acbe1
SP
1060 for (i = 0; i < t->entry_count; i++) {
1061 struct tree_entry *e = t->entries[i];
4cabf858
SP
1062 if (!e->versions[v].mode)
1063 continue;
1064 c += sprintf(c, "%o", e->versions[v].mode);
463acbe1
SP
1065 *c++ = ' ';
1066 strcpy(c, e->name->str_dat);
1067 c += e->name->str_len + 1;
4cabf858 1068 hashcpy((unsigned char*)c, e->versions[v].sha1);
463acbe1
SP
1069 c += 20;
1070 }
e2eb469d 1071 *szp = c - (char*)b->buffer;
4cabf858
SP
1072}
1073
1074static void store_tree(struct tree_entry *root)
1075{
1076 struct tree_content *t = root->tree;
1077 unsigned int i, j, del;
e2eb469d 1078 unsigned long new_len;
4cabf858 1079 struct last_object lo;
d489bc14 1080 struct object_entry *le;
4cabf858
SP
1081
1082 if (!is_null_sha1(root->versions[1].sha1))
1083 return;
1084
1085 for (i = 0; i < t->entry_count; i++) {
1086 if (t->entries[i]->tree)
1087 store_tree(t->entries[i]);
1088 }
1089
d489bc14 1090 le = find_object(root->versions[0].sha1);
7bfe6e26
SP
1091 if (!S_ISDIR(root->versions[0].mode)
1092 || !le
1093 || le->pack_id != pack_id) {
4cabf858
SP
1094 lo.data = NULL;
1095 lo.depth = 0;
1096 } else {
e2eb469d
SP
1097 mktree(t, 0, &lo.len, &old_tree);
1098 lo.data = old_tree.buffer;
d489bc14 1099 lo.offset = le->offset;
4cabf858 1100 lo.depth = t->delta_depth;
e2eb469d 1101 lo.no_free = 1;
4cabf858 1102 }
4cabf858 1103
8a8c55ea 1104 mktree(t, 1, &new_len, &new_tree);
e2eb469d 1105 store_object(OBJ_TREE, new_tree.buffer, new_len,
4cabf858 1106 &lo, root->versions[1].sha1, 0);
4cabf858
SP
1107
1108 t->delta_depth = lo.depth;
4cabf858
SP
1109 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1110 struct tree_entry *e = t->entries[i];
1111 if (e->versions[1].mode) {
1112 e->versions[0].mode = e->versions[1].mode;
1113 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1114 t->entries[j++] = e;
1115 } else {
1116 release_tree_entry(e);
1117 del++;
1118 }
1119 }
1120 t->entry_count -= del;
463acbe1
SP
1121}
1122
1123static int tree_content_set(
1124 struct tree_entry *root,
1125 const char *p,
1126 const unsigned char *sha1,
1127 const unsigned int mode)
1128{
1129 struct tree_content *t = root->tree;
1130 const char *slash1;
1131 unsigned int i, n;
1132 struct tree_entry *e;
1133
1134 slash1 = strchr(p, '/');
1135 if (slash1)
1136 n = slash1 - p;
1137 else
1138 n = strlen(p);
1139
1140 for (i = 0; i < t->entry_count; i++) {
1141 e = t->entries[i];
1142 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1143 if (!slash1) {
4cabf858
SP
1144 if (e->versions[1].mode == mode
1145 && !hashcmp(e->versions[1].sha1, sha1))
463acbe1 1146 return 0;
4cabf858
SP
1147 e->versions[1].mode = mode;
1148 hashcpy(e->versions[1].sha1, sha1);
463acbe1 1149 if (e->tree) {
afde8dd9 1150 release_tree_content_recursive(e->tree);
463acbe1
SP
1151 e->tree = NULL;
1152 }
4cabf858 1153 hashclr(root->versions[1].sha1);
463acbe1
SP
1154 return 1;
1155 }
4cabf858 1156 if (!S_ISDIR(e->versions[1].mode)) {
463acbe1 1157 e->tree = new_tree_content(8);
4cabf858 1158 e->versions[1].mode = S_IFDIR;
463acbe1
SP
1159 }
1160 if (!e->tree)
1161 load_tree(e);
1162 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
4cabf858 1163 hashclr(root->versions[1].sha1);
463acbe1
SP
1164 return 1;
1165 }
1166 return 0;
1167 }
1168 }
1169
1170 if (t->entry_count == t->entry_capacity)
1171 root->tree = t = grow_tree_content(t, 8);
1172 e = new_tree_entry();
1173 e->name = to_atom(p, n);
4cabf858
SP
1174 e->versions[0].mode = 0;
1175 hashclr(e->versions[0].sha1);
463acbe1
SP
1176 t->entries[t->entry_count++] = e;
1177 if (slash1) {
1178 e->tree = new_tree_content(8);
4cabf858 1179 e->versions[1].mode = S_IFDIR;
463acbe1
SP
1180 tree_content_set(e, slash1 + 1, sha1, mode);
1181 } else {
1182 e->tree = NULL;
4cabf858
SP
1183 e->versions[1].mode = mode;
1184 hashcpy(e->versions[1].sha1, sha1);
463acbe1 1185 }
4cabf858 1186 hashclr(root->versions[1].sha1);
463acbe1
SP
1187 return 1;
1188}
1189
1190static int tree_content_remove(struct tree_entry *root, const char *p)
1191{
1192 struct tree_content *t = root->tree;
1193 const char *slash1;
1194 unsigned int i, n;
1195 struct tree_entry *e;
1196
1197 slash1 = strchr(p, '/');
1198 if (slash1)
1199 n = slash1 - p;
1200 else
1201 n = strlen(p);
1202
1203 for (i = 0; i < t->entry_count; i++) {
1204 e = t->entries[i];
1205 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
4cabf858 1206 if (!slash1 || !S_ISDIR(e->versions[1].mode))
463acbe1
SP
1207 goto del_entry;
1208 if (!e->tree)
1209 load_tree(e);
1210 if (tree_content_remove(e, slash1 + 1)) {
b54d6422
SP
1211 for (n = 0; n < e->tree->entry_count; n++) {
1212 if (e->tree->entries[n]->versions[1].mode) {
1213 hashclr(root->versions[1].sha1);
1214 return 1;
1215 }
1216 }
1217 goto del_entry;
463acbe1
SP
1218 }
1219 return 0;
1220 }
1221 }
1222 return 0;
1223
1224del_entry:
4cabf858
SP
1225 if (e->tree) {
1226 release_tree_content_recursive(e->tree);
1227 e->tree = NULL;
1228 }
1229 e->versions[1].mode = 0;
1230 hashclr(e->versions[1].sha1);
1231 hashclr(root->versions[1].sha1);
ac47a738 1232 return 1;
db5e523f
SP
1233}
1234
463acbe1
SP
1235static void dump_branches()
1236{
1237 static const char *msg = "fast-import";
1238 unsigned int i;
1239 struct branch *b;
1240 struct ref_lock *lock;
1241
1242 for (i = 0; i < branch_table_sz; i++) {
1243 for (b = branch_table[i]; b; b = b->table_next_branch) {
1fcdd62a 1244 lock = lock_any_ref_for_update(b->name, NULL);
463acbe1
SP
1245 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1246 die("Can't write %s", b->name);
1247 }
1248 }
1249}
1250
72303d44
SP
1251static void dump_tags()
1252{
1253 static const char *msg = "fast-import";
1254 struct tag *t;
1255 struct ref_lock *lock;
1256 char path[PATH_MAX];
1257
1258 for (t = first_tag; t; t = t->next_tag) {
1259 sprintf(path, "refs/tags/%s", t->name);
1fcdd62a 1260 lock = lock_any_ref_for_update(path, NULL);
72303d44
SP
1261 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1262 die("Can't write %s", path);
1263 }
1264}
1265
a6a1a831 1266static void dump_marks_helper(FILE *f,
0ea9f045 1267 uintmax_t base,
a6a1a831
SP
1268 struct mark_set *m)
1269{
0ea9f045 1270 uintmax_t k;
a6a1a831
SP
1271 if (m->shift) {
1272 for (k = 0; k < 1024; k++) {
1273 if (m->data.sets[k])
1274 dump_marks_helper(f, (base + k) << m->shift,
1275 m->data.sets[k]);
1276 }
1277 } else {
1278 for (k = 0; k < 1024; k++) {
1279 if (m->data.marked[k])
0ea9f045 1280 fprintf(f, ":%ju %s\n", base + k,
a6a1a831
SP
1281 sha1_to_hex(m->data.marked[k]->sha1));
1282 }
1283 }
1284}
1285
1286static void dump_marks()
1287{
1288 if (mark_file)
1289 {
1290 FILE *f = fopen(mark_file, "w");
1291 dump_marks_helper(f, 0, marks);
1292 fclose(f);
1293 }
1294}
1295
c44cdc7e
SP
1296static void read_next_command()
1297{
1298 read_line(&command_buf, stdin, '\n');
1299}
1300
1301static void cmd_mark()
1302{
1303 if (!strncmp("mark :", command_buf.buf, 6)) {
0ea9f045 1304 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
c44cdc7e
SP
1305 read_next_command();
1306 }
1307 else
d8397168 1308 next_mark = 0;
c44cdc7e
SP
1309}
1310
1311static void* cmd_data (size_t *size)
1312{
1313 size_t n = 0;
1314 void *buffer;
1315 size_t length;
1316
1317 if (strncmp("data ", command_buf.buf, 5))
1318 die("Expected 'data n' command, found: %s", command_buf.buf);
1319
1320 length = strtoul(command_buf.buf + 5, NULL, 10);
1321 buffer = xmalloc(length);
1322
1323 while (n < length) {
1324 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1325 if (!s && feof(stdin))
1326 die("EOF in data (%lu bytes remaining)", length - n);
1327 n += s;
1328 }
1329
1330 if (fgetc(stdin) != '\n')
1331 die("An lf did not trail the binary data as expected.");
1332
1333 *size = length;
1334 return buffer;
1335}
1336
463acbe1 1337static void cmd_new_blob()
6143f064 1338{
d8397168
SP
1339 size_t l;
1340 void *d;
c44cdc7e
SP
1341
1342 read_next_command();
1343 cmd_mark();
d8397168 1344 d = cmd_data(&l);
6143f064 1345
d8397168
SP
1346 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1347 free(d);
6143f064
SP
1348}
1349
463acbe1 1350static void unload_one_branch()
6bb5b329 1351{
41e5257f
SP
1352 while (cur_active_branches
1353 && cur_active_branches >= max_active_branches) {
463acbe1
SP
1354 unsigned long min_commit = ULONG_MAX;
1355 struct branch *e, *l = NULL, *p = NULL;
1356
1357 for (e = active_branches; e; e = e->active_next_branch) {
1358 if (e->last_commit < min_commit) {
1359 p = l;
1360 min_commit = e->last_commit;
1361 }
1362 l = e;
1363 }
1364
1365 if (p) {
1366 e = p->active_next_branch;
1367 p->active_next_branch = e->active_next_branch;
1368 } else {
1369 e = active_branches;
1370 active_branches = e->active_next_branch;
1371 }
1372 e->active_next_branch = NULL;
1373 if (e->branch_tree.tree) {
afde8dd9 1374 release_tree_content_recursive(e->branch_tree.tree);
463acbe1
SP
1375 e->branch_tree.tree = NULL;
1376 }
1377 cur_active_branches--;
6bb5b329 1378 }
6bb5b329
SP
1379}
1380
463acbe1 1381static void load_branch(struct branch *b)
6bb5b329 1382{
463acbe1
SP
1383 load_tree(&b->branch_tree);
1384 b->active_next_branch = active_branches;
1385 active_branches = b;
1386 cur_active_branches++;
d6c7eb2c 1387 branch_load_count++;
6bb5b329
SP
1388}
1389
463acbe1 1390static void file_change_m(struct branch *b)
6bb5b329 1391{
c44cdc7e
SP
1392 const char *p = command_buf.buf + 2;
1393 char *p_uq;
1394 const char *endp;
7111feed 1395 struct object_entry *oe;
463acbe1 1396 unsigned char sha1[20];
c44cdc7e 1397 unsigned int mode;
7111feed 1398 char type[20];
6bb5b329 1399
c44cdc7e
SP
1400 p = get_mode(p, &mode);
1401 if (!p)
1402 die("Corrupt mode: %s", command_buf.buf);
1403 switch (mode) {
1404 case S_IFREG | 0644:
1405 case S_IFREG | 0755:
ace4a9d1 1406 case S_IFLNK:
c44cdc7e
SP
1407 case 0644:
1408 case 0755:
1409 /* ok */
1410 break;
1411 default:
1412 die("Corrupt mode: %s", command_buf.buf);
1413 }
1414
d8397168
SP
1415 if (*p == ':') {
1416 char *x;
0ea9f045 1417 oe = find_mark(strtoumax(p + 1, &x, 10));
cacbdd0a 1418 hashcpy(sha1, oe->sha1);
d8397168
SP
1419 p = x;
1420 } else {
1421 if (get_sha1_hex(p, sha1))
1422 die("Invalid SHA1: %s", command_buf.buf);
1423 oe = find_object(sha1);
1424 p += 40;
1425 }
c44cdc7e
SP
1426 if (*p++ != ' ')
1427 die("Missing space after SHA1: %s", command_buf.buf);
1428
1429 p_uq = unquote_c_style(p, &endp);
1430 if (p_uq) {
1431 if (*endp)
1432 die("Garbage after path in: %s", command_buf.buf);
1433 p = p_uq;
1434 }
6bb5b329 1435
7111feed
SP
1436 if (oe) {
1437 if (oe->type != OBJ_BLOB)
c44cdc7e
SP
1438 die("Not a blob (actually a %s): %s",
1439 command_buf.buf, type_names[oe->type]);
7111feed
SP
1440 } else {
1441 if (sha1_object_info(sha1, type, NULL))
c44cdc7e 1442 die("Blob not found: %s", command_buf.buf);
7111feed 1443 if (strcmp(blob_type, type))
c44cdc7e
SP
1444 die("Not a blob (actually a %s): %s",
1445 command_buf.buf, type);
7111feed 1446 }
6bb5b329 1447
c44cdc7e
SP
1448 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1449
1450 if (p_uq)
1451 free(p_uq);
463acbe1 1452}
6bb5b329 1453
463acbe1
SP
1454static void file_change_d(struct branch *b)
1455{
c44cdc7e
SP
1456 const char *p = command_buf.buf + 2;
1457 char *p_uq;
1458 const char *endp;
1459
1460 p_uq = unquote_c_style(p, &endp);
1461 if (p_uq) {
1462 if (*endp)
1463 die("Garbage after path in: %s", command_buf.buf);
1464 p = p_uq;
1465 }
1466 tree_content_remove(&b->branch_tree, p);
1467 if (p_uq)
1468 free(p_uq);
6bb5b329
SP
1469}
1470
00e2b884
SP
1471static void cmd_from(struct branch *b)
1472{
1473 const char *from, *endp;
1474 char *str_uq;
1475 struct branch *s;
1476
1477 if (strncmp("from ", command_buf.buf, 5))
1478 return;
1479
1480 if (b->last_commit)
1481 die("Can't reinitailize branch %s", b->name);
1482
1483 from = strchr(command_buf.buf, ' ') + 1;
1484 str_uq = unquote_c_style(from, &endp);
1485 if (str_uq) {
1486 if (*endp)
1487 die("Garbage after string in: %s", command_buf.buf);
1488 from = str_uq;
1489 }
1490
1491 s = lookup_branch(from);
1492 if (b == s)
1493 die("Can't create a branch from itself: %s", b->name);
1494 else if (s) {
4cabf858 1495 unsigned char *t = s->branch_tree.versions[1].sha1;
445b8599 1496 hashcpy(b->sha1, s->sha1);
4cabf858
SP
1497 hashcpy(b->branch_tree.versions[0].sha1, t);
1498 hashcpy(b->branch_tree.versions[1].sha1, t);
00e2b884 1499 } else if (*from == ':') {
0ea9f045 1500 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
00e2b884
SP
1501 struct object_entry *oe = find_mark(idnum);
1502 unsigned long size;
1503 char *buf;
1504 if (oe->type != OBJ_COMMIT)
0ea9f045 1505 die("Mark :%ju not a commit", idnum);
445b8599 1506 hashcpy(b->sha1, oe->sha1);
7bfe6e26 1507 buf = gfi_unpack_entry(oe, &size);
00e2b884
SP
1508 if (!buf || size < 46)
1509 die("Not a valid commit: %s", from);
1510 if (memcmp("tree ", buf, 5)
4cabf858 1511 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
00e2b884
SP
1512 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1513 free(buf);
4cabf858
SP
1514 hashcpy(b->branch_tree.versions[0].sha1,
1515 b->branch_tree.versions[1].sha1);
00e2b884 1516 } else if (!get_sha1(from, b->sha1)) {
4cabf858
SP
1517 if (is_null_sha1(b->sha1)) {
1518 hashclr(b->branch_tree.versions[0].sha1);
1519 hashclr(b->branch_tree.versions[1].sha1);
1520 } else {
00e2b884
SP
1521 unsigned long size;
1522 char *buf;
1523
1524 buf = read_object_with_reference(b->sha1,
1525 type_names[OBJ_COMMIT], &size, b->sha1);
1526 if (!buf || size < 46)
1527 die("Not a valid commit: %s", from);
1528 if (memcmp("tree ", buf, 5)
4cabf858 1529 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
00e2b884
SP
1530 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1531 free(buf);
4cabf858
SP
1532 hashcpy(b->branch_tree.versions[0].sha1,
1533 b->branch_tree.versions[1].sha1);
00e2b884
SP
1534 }
1535 } else
1536 die("Invalid ref name or SHA1 expression: %s", from);
1537
1538 read_next_command();
1539}
1540
62b6f483
SP
1541static struct hash_list* cmd_merge(unsigned int *count)
1542{
1543 struct hash_list *list = NULL, *n, *e;
1544 const char *from, *endp;
1545 char *str_uq;
1546 struct branch *s;
1547
1548 *count = 0;
1549 while (!strncmp("merge ", command_buf.buf, 6)) {
1550 from = strchr(command_buf.buf, ' ') + 1;
1551 str_uq = unquote_c_style(from, &endp);
1552 if (str_uq) {
1553 if (*endp)
1554 die("Garbage after string in: %s", command_buf.buf);
1555 from = str_uq;
1556 }
1557
1558 n = xmalloc(sizeof(*n));
1559 s = lookup_branch(from);
1560 if (s)
1561 hashcpy(n->sha1, s->sha1);
1562 else if (*from == ':') {
0ea9f045 1563 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
62b6f483
SP
1564 struct object_entry *oe = find_mark(idnum);
1565 if (oe->type != OBJ_COMMIT)
0ea9f045 1566 die("Mark :%ju not a commit", idnum);
62b6f483
SP
1567 hashcpy(n->sha1, oe->sha1);
1568 } else if (get_sha1(from, n->sha1))
1569 die("Invalid ref name or SHA1 expression: %s", from);
1570
1571 n->next = NULL;
1572 if (list)
1573 e->next = n;
1574 else
1575 list = n;
1576 e = n;
1577 *count++;
1578 read_next_command();
1579 }
1580 return list;
1581}
1582
463acbe1 1583static void cmd_new_commit()
6bb5b329 1584{
c44cdc7e
SP
1585 struct branch *b;
1586 void *msg;
1587 size_t msglen;
1588 char *str_uq;
1589 const char *endp;
1590 char *sp;
1591 char *author = NULL;
1592 char *committer = NULL;
62b6f483
SP
1593 struct hash_list *merge_list = NULL;
1594 unsigned int merge_count;
c44cdc7e
SP
1595
1596 /* Obtain the branch name from the rest of our command */
1597 sp = strchr(command_buf.buf, ' ') + 1;
1598 str_uq = unquote_c_style(sp, &endp);
1599 if (str_uq) {
1600 if (*endp)
1601 die("Garbage after ref in: %s", command_buf.buf);
1602 sp = str_uq;
1603 }
1604 b = lookup_branch(sp);
463acbe1 1605 if (!b)
00e2b884 1606 b = new_branch(sp);
c44cdc7e
SP
1607 if (str_uq)
1608 free(str_uq);
1609
1610 read_next_command();
1611 cmd_mark();
1612 if (!strncmp("author ", command_buf.buf, 7)) {
1613 author = strdup(command_buf.buf);
1614 read_next_command();
1615 }
1616 if (!strncmp("committer ", command_buf.buf, 10)) {
1617 committer = strdup(command_buf.buf);
1618 read_next_command();
1619 }
1620 if (!committer)
1621 die("Expected committer but didn't get one");
1622 msg = cmd_data(&msglen);
02f3389d
SP
1623 read_next_command();
1624 cmd_from(b);
62b6f483 1625 merge_list = cmd_merge(&merge_count);
c44cdc7e
SP
1626
1627 /* ensure the branch is active/loaded */
41e5257f 1628 if (!b->branch_tree.tree || !max_active_branches) {
463acbe1
SP
1629 unload_one_branch();
1630 load_branch(b);
1631 }
6bb5b329 1632
463acbe1
SP
1633 /* file_change* */
1634 for (;;) {
c44cdc7e 1635 if (1 == command_buf.len)
463acbe1 1636 break;
c44cdc7e 1637 else if (!strncmp("M ", command_buf.buf, 2))
463acbe1 1638 file_change_m(b);
c44cdc7e 1639 else if (!strncmp("D ", command_buf.buf, 2))
463acbe1
SP
1640 file_change_d(b);
1641 else
c44cdc7e 1642 die("Unsupported file_change: %s", command_buf.buf);
02f3389d 1643 read_next_command();
6bb5b329 1644 }
6bb5b329 1645
c44cdc7e 1646 /* build the tree and the commit */
463acbe1 1647 store_tree(&b->branch_tree);
8a8c55ea
SP
1648 hashcpy(b->branch_tree.versions[0].sha1,
1649 b->branch_tree.versions[1].sha1);
243f801d 1650 size_dbuf(&new_data, 97 + msglen
62b6f483 1651 + merge_count * 49
c44cdc7e
SP
1652 + (author
1653 ? strlen(author) + strlen(committer)
1654 : 2 * strlen(committer)));
243f801d 1655 sp = new_data.buffer;
4cabf858
SP
1656 sp += sprintf(sp, "tree %s\n",
1657 sha1_to_hex(b->branch_tree.versions[1].sha1));
445b8599 1658 if (!is_null_sha1(b->sha1))
c44cdc7e 1659 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
62b6f483
SP
1660 while (merge_list) {
1661 struct hash_list *next = merge_list->next;
1662 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1663 free(merge_list);
1664 merge_list = next;
1665 }
c44cdc7e
SP
1666 if (author)
1667 sp += sprintf(sp, "%s\n", author);
1668 else
1669 sp += sprintf(sp, "author %s\n", committer + 10);
1670 sp += sprintf(sp, "%s\n\n", committer);
1671 memcpy(sp, msg, msglen);
1672 sp += msglen;
1673 if (author)
1674 free(author);
1675 free(committer);
1676 free(msg);
1677
243f801d
SP
1678 store_object(OBJ_COMMIT,
1679 new_data.buffer, sp - (char*)new_data.buffer,
1680 NULL, b->sha1, next_mark);
463acbe1 1681 b->last_commit = object_count_by_type[OBJ_COMMIT];
264244a0
SP
1682
1683 if (branch_log) {
1684 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1685 fprintf(branch_log, "commit ");
1686 if (need_dq) {
1687 fputc('"', branch_log);
1688 quote_c_style(b->name, NULL, branch_log, 0);
1689 fputc('"', branch_log);
1690 } else
1691 fprintf(branch_log, "%s", b->name);
0ea9f045 1692 fprintf(branch_log," :%ju %s\n",next_mark,sha1_to_hex(b->sha1));
264244a0 1693 }
6bb5b329
SP
1694}
1695
72303d44
SP
1696static void cmd_new_tag()
1697{
1698 char *str_uq;
1699 const char *endp;
1700 char *sp;
1701 const char *from;
1702 char *tagger;
1703 struct branch *s;
1704 void *msg;
1705 size_t msglen;
72303d44 1706 struct tag *t;
0ea9f045 1707 uintmax_t from_mark = 0;
72303d44
SP
1708 unsigned char sha1[20];
1709
1710 /* Obtain the new tag name from the rest of our command */
1711 sp = strchr(command_buf.buf, ' ') + 1;
1712 str_uq = unquote_c_style(sp, &endp);
1713 if (str_uq) {
1714 if (*endp)
1715 die("Garbage after tag name in: %s", command_buf.buf);
1716 sp = str_uq;
1717 }
1718 t = pool_alloc(sizeof(struct tag));
1719 t->next_tag = NULL;
1720 t->name = pool_strdup(sp);
1721 if (last_tag)
1722 last_tag->next_tag = t;
1723 else
1724 first_tag = t;
1725 last_tag = t;
1726 if (str_uq)
1727 free(str_uq);
1728 read_next_command();
1729
1730 /* from ... */
1731 if (strncmp("from ", command_buf.buf, 5))
1732 die("Expected from command, got %s", command_buf.buf);
1733
1734 from = strchr(command_buf.buf, ' ') + 1;
1735 str_uq = unquote_c_style(from, &endp);
1736 if (str_uq) {
1737 if (*endp)
1738 die("Garbage after string in: %s", command_buf.buf);
1739 from = str_uq;
1740 }
1741
1742 s = lookup_branch(from);
1743 if (s) {
445b8599 1744 hashcpy(sha1, s->sha1);
72303d44 1745 } else if (*from == ':') {
0ea9f045 1746 from_mark = strtoumax(from + 1, NULL, 10);
264244a0 1747 struct object_entry *oe = find_mark(from_mark);
72303d44 1748 if (oe->type != OBJ_COMMIT)
0ea9f045 1749 die("Mark :%ju not a commit", from_mark);
445b8599 1750 hashcpy(sha1, oe->sha1);
72303d44
SP
1751 } else if (!get_sha1(from, sha1)) {
1752 unsigned long size;
1753 char *buf;
1754
1755 buf = read_object_with_reference(sha1,
1756 type_names[OBJ_COMMIT], &size, sha1);
1757 if (!buf || size < 46)
1758 die("Not a valid commit: %s", from);
1759 free(buf);
1760 } else
1761 die("Invalid ref name or SHA1 expression: %s", from);
1762
1763 if (str_uq)
1764 free(str_uq);
1765 read_next_command();
1766
1767 /* tagger ... */
1768 if (strncmp("tagger ", command_buf.buf, 7))
1769 die("Expected tagger command, got %s", command_buf.buf);
1770 tagger = strdup(command_buf.buf);
1771
1772 /* tag payload/message */
1773 read_next_command();
1774 msg = cmd_data(&msglen);
1775
1776 /* build the tag object */
243f801d
SP
1777 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1778 sp = new_data.buffer;
72303d44
SP
1779 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1780 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1781 sp += sprintf(sp, "tag %s\n", t->name);
1782 sp += sprintf(sp, "%s\n\n", tagger);
1783 memcpy(sp, msg, msglen);
1784 sp += msglen;
1785 free(tagger);
1786 free(msg);
1787
243f801d
SP
1788 store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1789 NULL, t->sha1, 0);
264244a0
SP
1790
1791 if (branch_log) {
1792 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1793 fprintf(branch_log, "tag ");
1794 if (need_dq) {
1795 fputc('"', branch_log);
1796 quote_c_style(t->name, NULL, branch_log, 0);
1797 fputc('"', branch_log);
1798 } else
1799 fprintf(branch_log, "%s", t->name);
0ea9f045 1800 fprintf(branch_log," :%ju %s\n",from_mark,sha1_to_hex(t->sha1));
264244a0 1801 }
72303d44
SP
1802}
1803
5fced8dc
SP
1804static void cmd_reset_branch()
1805{
1806 struct branch *b;
1807 char *str_uq;
1808 const char *endp;
1809 char *sp;
1810
1811 /* Obtain the branch name from the rest of our command */
1812 sp = strchr(command_buf.buf, ' ') + 1;
1813 str_uq = unquote_c_style(sp, &endp);
1814 if (str_uq) {
1815 if (*endp)
1816 die("Garbage after ref in: %s", command_buf.buf);
1817 sp = str_uq;
1818 }
1819 b = lookup_branch(sp);
1820 if (b) {
1821 b->last_commit = 0;
1822 if (b->branch_tree.tree) {
1823 release_tree_content_recursive(b->branch_tree.tree);
1824 b->branch_tree.tree = NULL;
1825 }
1826 }
9938ffc5
SP
1827 else
1828 b = new_branch(sp);
5fced8dc
SP
1829 if (str_uq)
1830 free(str_uq);
9938ffc5
SP
1831 read_next_command();
1832 cmd_from(b);
5fced8dc
SP
1833}
1834
7bfe6e26
SP
1835static void cmd_checkpoint()
1836{
d9ee53ce
SP
1837 if (object_count)
1838 checkpoint();
7bfe6e26
SP
1839 read_next_command();
1840}
1841
d5c57b28 1842static const char fast_import_usage[] =
8455e484 1843"git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
d5c57b28 1844
8bcce301
SP
1845int main(int argc, const char **argv)
1846{
d5c57b28 1847 int i;
0ea9f045
SP
1848 uintmax_t est_obj_cnt = object_entry_alloc;
1849 uintmax_t duplicate_count;
8bcce301 1850
463acbe1
SP
1851 setup_ident();
1852 git_config(git_default_config);
1853
d5c57b28
SP
1854 for (i = 1; i < argc; i++) {
1855 const char *a = argv[i];
1856
1857 if (*a != '-' || !strcmp(a, "--"))
1858 break;
1859 else if (!strncmp(a, "--objects=", 10))
0ea9f045 1860 est_obj_cnt = strtoumax(a + 10, NULL, 0);
d9ee53ce 1861 else if (!strncmp(a, "--max-objects-per-pack=", 23))
0ea9f045 1862 max_objects = strtoumax(a + 23, NULL, 0);
d9ee53ce 1863 else if (!strncmp(a, "--max-pack-size=", 16))
0ea9f045 1864 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
d5c57b28
SP
1865 else if (!strncmp(a, "--depth=", 8))
1866 max_depth = strtoul(a + 8, NULL, 0);
1867 else if (!strncmp(a, "--active-branches=", 18))
1868 max_active_branches = strtoul(a + 18, NULL, 0);
a6a1a831
SP
1869 else if (!strncmp(a, "--export-marks=", 15))
1870 mark_file = a + 15;
264244a0
SP
1871 else if (!strncmp(a, "--branch-log=", 13)) {
1872 branch_log = fopen(a + 13, "w");
1873 if (!branch_log)
1874 die("Can't create %s: %s", a + 13, strerror(errno));
1875 }
d5c57b28
SP
1876 else
1877 die("unknown option %s", a);
1878 }
8455e484 1879 if (i != argc)
d5c57b28 1880 usage(fast_import_usage);
d5c57b28 1881
27d6d290 1882 alloc_objects(est_obj_cnt);
c44cdc7e 1883 strbuf_init(&command_buf);
463acbe1
SP
1884
1885 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1886 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1887 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
d8397168 1888 marks = pool_calloc(1, sizeof(struct mark_set));
463acbe1 1889
f70b6534 1890 start_packfile();
db5e523f 1891 for (;;) {
c44cdc7e
SP
1892 read_next_command();
1893 if (command_buf.eof)
db5e523f 1894 break;
c44cdc7e
SP
1895 else if (!strcmp("blob", command_buf.buf))
1896 cmd_new_blob();
c44cdc7e
SP
1897 else if (!strncmp("commit ", command_buf.buf, 7))
1898 cmd_new_commit();
72303d44
SP
1899 else if (!strncmp("tag ", command_buf.buf, 4))
1900 cmd_new_tag();
5fced8dc
SP
1901 else if (!strncmp("reset ", command_buf.buf, 6))
1902 cmd_reset_branch();
7bfe6e26
SP
1903 else if (!strcmp("checkpoint", command_buf.buf))
1904 cmd_checkpoint();
c44cdc7e
SP
1905 else
1906 die("Unsupported command: %s", command_buf.buf);
db5e523f 1907 }
f70b6534 1908 end_packfile();
c44cdc7e 1909
463acbe1 1910 dump_branches();
72303d44 1911 dump_tags();
8455e484 1912 unkeep_all_packs();
a6a1a831 1913 dump_marks();
08d7e892
SP
1914 if (branch_log)
1915 fclose(branch_log);
8bcce301 1916
0ea9f045 1917 duplicate_count = 0;
80144727
SP
1918 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
1919 duplicate_count += duplicate_count_by_type[i];
1920
6143f064 1921 fprintf(stderr, "%s statistics:\n", argv[0]);
8a8c55ea 1922 fprintf(stderr, "---------------------------------------------------------------------\n");
0ea9f045
SP
1923 fprintf(stderr, "Alloc'd objects: %10ju (%10ju overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1924 fprintf(stderr, "Total objects: %10ju (%10ju duplicates )\n", object_count, duplicate_count);
1925 fprintf(stderr, " blobs : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
1926 fprintf(stderr, " trees : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
1927 fprintf(stderr, " commits: %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
1928 fprintf(stderr, " tags : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
d6c7eb2c 1929 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
0ea9f045 1930 fprintf(stderr, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
d6c7eb2c 1931 fprintf(stderr, " atoms: %10u\n", atom_cnt);
0ea9f045 1932 fprintf(stderr, "Memory total: %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
7111feed 1933 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
0ea9f045 1934 fprintf(stderr, " objects: %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
8a8c55ea 1935 fprintf(stderr, "---------------------------------------------------------------------\n");
7bfe6e26
SP
1936 pack_report();
1937 fprintf(stderr, "---------------------------------------------------------------------\n");
6143f064 1938 fprintf(stderr, "\n");
db5e523f
SP
1939
1940 return 0;
1941}