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