Code

1c74b90c84f1c100cbf593a0e7bcd1fdafe40feb
[git.git] / fast-import.c
1 /*
2 Format of STDIN stream:
4   stream ::= cmd*;
6   cmd ::= new_blob
7         | new_branch
8         | new_commit
9         | new_tag
10         ;
12   new_blob ::= 'blob' lf
13         mark?
14     file_content;
15   file_content ::= data;
17   new_branch ::= 'branch' sp ref_str lf
18     ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
19     lf;
21   new_commit ::= 'commit' sp ref_str lf
22         mark?
23         ('author' sp name '<' email '>' ts tz lf)?
24         'committer' sp name '<' email '>' ts tz lf
25         commit_msg
26     file_change*
27     lf;
28   commit_msg ::= data;
30   file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
31                 | 'D' sp path_str lf
32                 ;
33   mode ::= '644' | '755';
35   new_tag ::= 'tag' sp tag_str lf
36     'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
37         'tagger' sp name '<' email '>' ts tz lf
38     tag_msg;
39   tag_msg ::= data;
41      # note: the first idnum in a stream should be 1 and subsequent
42      # idnums should not have gaps between values as this will cause
43      # the stream parser to reserve space for the gapped values.  An
44          # idnum can be updated in the future to a new object by issuing
45      # a new mark directive with the old idnum.
46          #
47   mark ::= 'mark' sp idnum lf;
49      # note: declen indicates the length of binary_data in bytes.
50      # declen does not include the lf preceeding or trailing the
51      # binary data.
52      #
53   data ::= 'data' sp declen lf
54     binary_data
55         lf;
57      # note: quoted strings are C-style quoting supporting \c for
58      # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
59          # is the signed byte value in octal.  Note that the only
60      # characters which must actually be escaped to protect the
61      # stream formatting is: \, " and LF.  Otherwise these values
62          # are UTF8.
63      #
64   ref_str     ::= ref     | '"' quoted(ref)     '"' ;
65   sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
66   tag_str     ::= tag     | '"' quoted(tag)     '"' ;
67   path_str    ::= path    | '"' quoted(path)    '"' ;
69   declen ::= # unsigned 32 bit value, ascii base10 notation;
70   binary_data ::= # file content, not interpreted;
72   sp ::= # ASCII space character;
73   lf ::= # ASCII newline (LF) character;
75      # note: a colon (':') must precede the numerical value assigned to
76          # an idnum.  This is to distinguish it from a ref or tag name as
77      # GIT does not permit ':' in ref or tag strings.
78          #
79   idnum   ::= ':' declen;
80   path    ::= # GIT style file path, e.g. "a/b/c";
81   ref     ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
82   tag     ::= # GIT tag name, e.g. "FIREFOX_1_5";
83   sha1exp ::= # Any valid GIT SHA1 expression;
84   hexsha1 ::= # SHA1 in hexadecimal format;
86      # note: name and email are UTF8 strings, however name must not
87          # contain '<' or lf and email must not contain any of the
88      # following: '<', '>', lf.
89          #
90   name  ::= # valid GIT author/committer name;
91   email ::= # valid GIT author/committer email;
92   ts    ::= # time since the epoch in seconds, ascii base10 notation;
93   tz    ::= # GIT style timezone;
94 */
96 #include "builtin.h"
97 #include "cache.h"
98 #include "object.h"
99 #include "blob.h"
100 #include "tree.h"
101 #include "delta.h"
102 #include "pack.h"
103 #include "refs.h"
104 #include "csum-file.h"
105 #include "strbuf.h"
106 #include "quote.h"
108 struct object_entry
110         struct object_entry *next;
111         enum object_type type;
112         unsigned long offset;
113         unsigned char sha1[20];
114 };
116 struct object_entry_pool
118         struct object_entry_pool *next_pool;
119         struct object_entry *next_free;
120         struct object_entry *end;
121         struct object_entry entries[FLEX_ARRAY]; /* more */
122 };
124 struct mark_set
126         int shift;
127         union {
128                 struct object_entry *marked[1024];
129                 struct mark_set *sets[1024];
130         } data;
131 };
133 struct last_object
135         void *data;
136         unsigned int len;
137         unsigned int depth;
138         unsigned char sha1[20];
139 };
141 struct mem_pool
143         struct mem_pool *next_pool;
144         char *next_free;
145         char *end;
146         char space[FLEX_ARRAY]; /* more */
147 };
149 struct atom_str
151         struct atom_str *next_atom;
152         int str_len;
153         char str_dat[FLEX_ARRAY]; /* more */
154 };
156 struct tree_content;
157 struct tree_entry
159         struct tree_content *tree;
160         struct atom_str* name;
161         unsigned int mode;
162         unsigned char sha1[20];
163 };
165 struct tree_content
167         unsigned int entry_capacity; /* must match avail_tree_content */
168         unsigned int entry_count;
169         struct tree_entry *entries[FLEX_ARRAY]; /* more */
170 };
172 struct avail_tree_content
174         unsigned int entry_capacity; /* must match tree_content */
175         struct avail_tree_content *next_avail;
176 };
178 struct branch
180         struct branch *table_next_branch;
181         struct branch *active_next_branch;
182         const char *name;
183         unsigned long last_commit;
184         struct tree_entry branch_tree;
185         unsigned char sha1[20];
186 };
188 struct tag
190         struct tag *next_tag;
191         const char *name;
192         unsigned char sha1[20];
193 };
196 /* Stats and misc. counters */
197 static unsigned long max_depth = 10;
198 static unsigned long alloc_count;
199 static unsigned long branch_count;
200 static unsigned long branch_load_count;
201 static unsigned long remap_count;
202 static unsigned long object_count;
203 static unsigned long duplicate_count;
204 static unsigned long marks_set_count;
205 static unsigned long object_count_by_type[9];
206 static unsigned long duplicate_count_by_type[9];
208 /* Memory pools */
209 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
210 static size_t total_allocd;
211 static struct mem_pool *mem_pool;
213 /* Atom management */
214 static unsigned int atom_table_sz = 4451;
215 static unsigned int atom_cnt;
216 static struct atom_str **atom_table;
218 /* The .pack file being generated */
219 static int pack_fd;
220 static unsigned long pack_size;
221 static unsigned char pack_sha1[20];
222 static void* pack_base;
223 static size_t pack_mlen;
225 /* Table of objects we've written. */
226 static unsigned int object_entry_alloc = 1000;
227 static struct object_entry_pool *blocks;
228 static struct object_entry *object_table[1 << 16];
229 static struct mark_set *marks;
231 /* Our last blob */
232 static struct last_object last_blob;
234 /* Tree management */
235 static unsigned int tree_entry_alloc = 1000;
236 static void *avail_tree_entry;
237 static unsigned int avail_tree_table_sz = 100;
238 static struct avail_tree_content **avail_tree_table;
240 /* Branch data */
241 static unsigned long max_active_branches = 5;
242 static unsigned long cur_active_branches;
243 static unsigned long branch_table_sz = 1039;
244 static struct branch **branch_table;
245 static struct branch *active_branches;
247 /* Tag data */
248 static struct tag *first_tag;
249 static struct tag *last_tag;
251 /* Input stream parsing */
252 static struct strbuf command_buf;
253 static unsigned long next_mark;
256 static void alloc_objects(int cnt)
258         struct object_entry_pool *b;
260         b = xmalloc(sizeof(struct object_entry_pool)
261                 + cnt * sizeof(struct object_entry));
262         b->next_pool = blocks;
263         b->next_free = b->entries;
264         b->end = b->entries + cnt;
265         blocks = b;
266         alloc_count += cnt;
269 static struct object_entry* new_object(unsigned char *sha1)
271         struct object_entry *e;
273         if (blocks->next_free == blocks->end)
274                 alloc_objects(object_entry_alloc);
276         e = blocks->next_free++;
277         memcpy(e->sha1, sha1, sizeof(e->sha1));
278         return e;
281 static struct object_entry* find_object(unsigned char *sha1)
283         unsigned int h = sha1[0] << 8 | sha1[1];
284         struct object_entry *e;
285         for (e = object_table[h]; e; e = e->next)
286                 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
287                         return e;
288         return NULL;
291 static struct object_entry* insert_object(unsigned char *sha1)
293         unsigned int h = sha1[0] << 8 | sha1[1];
294         struct object_entry *e = object_table[h];
295         struct object_entry *p = NULL;
297         while (e) {
298                 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
299                         return e;
300                 p = e;
301                 e = e->next;
302         }
304         e = new_object(sha1);
305         e->next = NULL;
306         e->offset = 0;
307         if (p)
308                 p->next = e;
309         else
310                 object_table[h] = e;
311         return e;
314 static unsigned int hc_str(const char *s, size_t len)
316         unsigned int r = 0;
317         while (len-- > 0)
318                 r = r * 31 + *s++;
319         return r;
322 static void* pool_alloc(size_t len)
324         struct mem_pool *p;
325         void *r;
327         for (p = mem_pool; p; p = p->next_pool)
328                 if ((p->end - p->next_free >= len))
329                         break;
331         if (!p) {
332                 if (len >= (mem_pool_alloc/2)) {
333                         total_allocd += len;
334                         return xmalloc(len);
335                 }
336                 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
337                 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
338                 p->next_pool = mem_pool;
339                 p->next_free = p->space;
340                 p->end = p->next_free + mem_pool_alloc;
341                 mem_pool = p;
342         }
344         r = p->next_free;
345         p->next_free += len;
346         return r;
349 static void* pool_calloc(size_t count, size_t size)
351         size_t len = count * size;
352         void *r = pool_alloc(len);
353         memset(r, 0, len);
354         return r;
357 static char* pool_strdup(const char *s)
359         char *r = pool_alloc(strlen(s) + 1);
360         strcpy(r, s);
361         return r;
364 static void insert_mark(unsigned long idnum, struct object_entry *oe)
366         struct mark_set *s = marks;
367         while ((idnum >> s->shift) >= 1024) {
368                 s = pool_calloc(1, sizeof(struct mark_set));
369                 s->shift = marks->shift + 10;
370                 s->data.sets[0] = marks;
371                 marks = s;
372         }
373         while (s->shift) {
374                 unsigned long i = idnum >> s->shift;
375                 idnum -= i << s->shift;
376                 if (!s->data.sets[i]) {
377                         s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
378                         s->data.sets[i]->shift = s->shift - 10;
379                 }
380                 s = s->data.sets[i];
381         }
382         if (!s->data.marked[idnum])
383                 marks_set_count++;
384         s->data.marked[idnum] = oe;
387 static struct object_entry* find_mark(unsigned long idnum)
389         unsigned long orig_idnum = idnum;
390         struct mark_set *s = marks;
391         struct object_entry *oe = NULL;
392         if ((idnum >> s->shift) < 1024) {
393                 while (s && s->shift) {
394                         unsigned long i = idnum >> s->shift;
395                         idnum -= i << s->shift;
396                         s = s->data.sets[i];
397                 }
398                 if (s)
399                         oe = s->data.marked[idnum];
400         }
401         if (!oe)
402                 die("mark :%lu not declared", orig_idnum);
403         return oe;
406 static struct atom_str* to_atom(const char *s, size_t len)
408         unsigned int hc = hc_str(s, len) % atom_table_sz;
409         struct atom_str *c;
411         for (c = atom_table[hc]; c; c = c->next_atom)
412                 if (c->str_len == len && !strncmp(s, c->str_dat, len))
413                         return c;
415         c = pool_alloc(sizeof(struct atom_str) + len + 1);
416         c->str_len = len;
417         strncpy(c->str_dat, s, len);
418         c->str_dat[len] = 0;
419         c->next_atom = atom_table[hc];
420         atom_table[hc] = c;
421         atom_cnt++;
422         return c;
425 static struct branch* lookup_branch(const char *name)
427         unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
428         struct branch *b;
430         for (b = branch_table[hc]; b; b = b->table_next_branch)
431                 if (!strcmp(name, b->name))
432                         return b;
433         return NULL;
436 static struct branch* new_branch(const char *name)
438         unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
439         struct branch* b = lookup_branch(name);
441         if (b)
442                 die("Invalid attempt to create duplicate branch: %s", name);
443         if (check_ref_format(name))
444                 die("Branch name doesn't conform to GIT standards: %s", name);
446         b = pool_calloc(1, sizeof(struct branch));
447         b->name = pool_strdup(name);
448         b->table_next_branch = branch_table[hc];
449         branch_table[hc] = b;
450         branch_count++;
451         return b;
454 static unsigned int hc_entries(unsigned int cnt)
456         cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
457         return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
460 static struct tree_content* new_tree_content(unsigned int cnt)
462         struct avail_tree_content *f, *l = NULL;
463         struct tree_content *t;
464         unsigned int hc = hc_entries(cnt);
466         for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
467                 if (f->entry_capacity >= cnt)
468                         break;
470         if (f) {
471                 if (l)
472                         l->next_avail = f->next_avail;
473                 else
474                         avail_tree_table[hc] = f->next_avail;
475         } else {
476                 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
477                 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
478                 f->entry_capacity = cnt;
479         }
481         t = (struct tree_content*)f;
482         t->entry_count = 0;
483         return t;
486 static void release_tree_entry(struct tree_entry *e);
487 static void release_tree_content(struct tree_content *t)
489         struct avail_tree_content *f = (struct avail_tree_content*)t;
490         unsigned int hc = hc_entries(f->entry_capacity);
491         f->next_avail = avail_tree_table[hc];
492         avail_tree_table[hc] = f;
495 static void release_tree_content_recursive(struct tree_content *t)
497         unsigned int i;
498         for (i = 0; i < t->entry_count; i++)
499                 release_tree_entry(t->entries[i]);
500         release_tree_content(t);
503 static struct tree_content* grow_tree_content(
504         struct tree_content *t,
505         int amt)
507         struct tree_content *r = new_tree_content(t->entry_count + amt);
508         r->entry_count = t->entry_count;
509         memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
510         release_tree_content(t);
511         return r;
514 static struct tree_entry* new_tree_entry()
516         struct tree_entry *e;
518         if (!avail_tree_entry) {
519                 unsigned int n = tree_entry_alloc;
520                 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
521                 while (n--) {
522                         *((void**)e) = e + 1;
523                         e++;
524                 }
525         }
527         e = avail_tree_entry;
528         avail_tree_entry = *((void**)e);
529         return e;
532 static void release_tree_entry(struct tree_entry *e)
534         if (e->tree)
535                 release_tree_content_recursive(e->tree);
536         *((void**)e) = avail_tree_entry;
537         avail_tree_entry = e;
540 static void yread(int fd, void *buffer, size_t length)
542         ssize_t ret = 0;
543         while (ret < length) {
544                 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
545                 if (!size)
546                         die("Read from descriptor %i: end of stream", fd);
547                 if (size < 0)
548                         die("Read from descriptor %i: %s", fd, strerror(errno));
549                 ret += size;
550         }
553 static void ywrite(int fd, void *buffer, size_t length)
555         ssize_t ret = 0;
556         while (ret < length) {
557                 ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret);
558                 if (!size)
559                         die("Write to descriptor %i: end of file", fd);
560                 if (size < 0)
561                         die("Write to descriptor %i: %s", fd, strerror(errno));
562                 ret += size;
563         }
566 static size_t encode_header(
567         enum object_type type,
568         size_t size,
569         unsigned char *hdr)
571         int n = 1;
572         unsigned char c;
574         if (type < OBJ_COMMIT || type > OBJ_DELTA)
575                 die("bad type %d", type);
577         c = (type << 4) | (size & 15);
578         size >>= 4;
579         while (size) {
580                 *hdr++ = c | 0x80;
581                 c = size & 0x7f;
582                 size >>= 7;
583                 n++;
584         }
585         *hdr = c;
586         return n;
589 static int store_object(
590         enum object_type type,
591         void *dat,
592         size_t datlen,
593         struct last_object *last,
594         unsigned char *sha1out,
595         unsigned long mark)
597         void *out, *delta;
598         struct object_entry *e;
599         unsigned char hdr[96];
600         unsigned char sha1[20];
601         unsigned long hdrlen, deltalen;
602         SHA_CTX c;
603         z_stream s;
605         hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
606         SHA1_Init(&c);
607         SHA1_Update(&c, hdr, hdrlen);
608         SHA1_Update(&c, dat, datlen);
609         SHA1_Final(sha1, &c);
610         if (sha1out)
611                 memcpy(sha1out, sha1, sizeof(sha1));
613         e = insert_object(sha1);
614         if (mark)
615                 insert_mark(mark, e);
616         if (e->offset) {
617                 duplicate_count++;
618                 duplicate_count_by_type[type]++;
619                 return 1;
620         }
621         e->type = type;
622         e->offset = pack_size;
623         object_count++;
624         object_count_by_type[type]++;
626         if (last && last->data && last->depth < max_depth)
627                 delta = diff_delta(last->data, last->len,
628                         dat, datlen,
629                         &deltalen, 0);
630         else
631                 delta = 0;
633         memset(&s, 0, sizeof(s));
634         deflateInit(&s, zlib_compression_level);
636         if (delta) {
637                 last->depth++;
638                 s.next_in = delta;
639                 s.avail_in = deltalen;
640                 hdrlen = encode_header(OBJ_DELTA, deltalen, hdr);
641                 ywrite(pack_fd, hdr, hdrlen);
642                 ywrite(pack_fd, last->sha1, sizeof(sha1));
643                 pack_size += hdrlen + sizeof(sha1);
644         } else {
645                 if (last)
646                         last->depth = 0;
647                 s.next_in = dat;
648                 s.avail_in = datlen;
649                 hdrlen = encode_header(type, datlen, hdr);
650                 ywrite(pack_fd, hdr, hdrlen);
651                 pack_size += hdrlen;
652         }
654         s.avail_out = deflateBound(&s, s.avail_in);
655         s.next_out = out = xmalloc(s.avail_out);
656         while (deflate(&s, Z_FINISH) == Z_OK)
657                 /* nothing */;
658         deflateEnd(&s);
660         ywrite(pack_fd, out, s.total_out);
661         pack_size += s.total_out;
663         free(out);
664         if (delta)
665                 free(delta);
666         if (last) {
667                 if (last->data)
668                         free(last->data);
669                 last->data = dat;
670                 last->len = datlen;
671                 memcpy(last->sha1, sha1, sizeof(sha1));
672         }
673         return 0;
676 static void* map_pack(unsigned long offset)
678         if (offset >= pack_size)
679                 die("object offset outside of pack file");
680         if (offset >= pack_mlen) {
681                 if (pack_base)
682                         munmap(pack_base, pack_mlen);
683                 /* round out how much we map to 16 MB units */
684                 pack_mlen = pack_size;
685                 if (pack_mlen & ((1 << 24) - 1))
686                         pack_mlen = ((pack_mlen >> 24) + 1) << 24;
687                 pack_base = mmap(NULL,pack_mlen,PROT_READ,MAP_SHARED,pack_fd,0);
688                 if (pack_base == MAP_FAILED)
689                         die("Failed to map generated pack: %s", strerror(errno));
690                 remap_count++;
691         }
692         return (char*)pack_base + offset;
695 static unsigned long unpack_object_header(unsigned long offset,
696         enum object_type *type,
697         unsigned long *sizep)
699         unsigned shift;
700         unsigned char c;
701         unsigned long size;
703         c = *(unsigned char*)map_pack(offset++);
704         *type = (c >> 4) & 7;
705         size = c & 15;
706         shift = 4;
707         while (c & 0x80) {
708                 c = *(unsigned char*)map_pack(offset++);
709                 size += (c & 0x7f) << shift;
710                 shift += 7;
711         }
712         *sizep = size;
713         return offset;
716 static void *unpack_non_delta_entry(unsigned long o, unsigned long sz)
718         z_stream stream;
719         unsigned char *result;
721         result = xmalloc(sz + 1);
722         result[sz] = 0;
724         memset(&stream, 0, sizeof(stream));
725         stream.next_in = map_pack(o);
726         stream.avail_in = pack_mlen - o;
727         stream.next_out = result;
728         stream.avail_out = sz;
730         inflateInit(&stream);
731         for (;;) {
732                 int st = inflate(&stream, Z_FINISH);
733                 if (st == Z_STREAM_END)
734                         break;
735                 if (st == Z_OK) {
736                         o = stream.next_in - (unsigned char*)pack_base;
737                         stream.next_in = map_pack(o);
738                         stream.avail_in = pack_mlen - o;
739                         continue;
740                 }
741                 die("Error from zlib during inflate.");
742         }
743         inflateEnd(&stream);
744         if (stream.total_out != sz)
745                 die("Error after inflate: sizes mismatch");
746         return result;
749 static void *unpack_entry(unsigned long offset, unsigned long *sizep);
751 static void *unpack_delta_entry(unsigned long offset,
752         unsigned long delta_size,
753         unsigned long *sizep)
755         struct object_entry *base_oe;
756         unsigned char *base_sha1;
757         void *delta_data, *base, *result;
758         unsigned long base_size, result_size;
760         base_sha1 = (unsigned char*)map_pack(offset + 20) - 20;
761         base_oe = find_object(base_sha1);
762         if (!base_oe)
763                 die("I'm broken; I can't find a base I know must be here.");
764         base = unpack_entry(base_oe->offset, &base_size);
765         delta_data = unpack_non_delta_entry(offset + 20, delta_size);
766         result = patch_delta(base, base_size,
767                              delta_data, delta_size,
768                              &result_size);
769         if (!result)
770                 die("failed to apply delta");
771         free(delta_data);
772         free(base);
773         *sizep = result_size;
774         return result;
777 static void *unpack_entry(unsigned long offset, unsigned long *sizep)
779         unsigned long size;
780         enum object_type kind;
782         offset = unpack_object_header(offset, &kind, &size);
783         switch (kind) {
784         case OBJ_DELTA:
785                 return unpack_delta_entry(offset, size, sizep);
786         case OBJ_COMMIT:
787         case OBJ_TREE:
788         case OBJ_BLOB:
789         case OBJ_TAG:
790                 *sizep = size;
791                 return unpack_non_delta_entry(offset, size);
792         default:
793                 die("I created an object I can't read!");
794         }
797 static const char *get_mode(const char *str, unsigned int *modep)
799         unsigned char c;
800         unsigned int mode = 0;
802         while ((c = *str++) != ' ') {
803                 if (c < '0' || c > '7')
804                         return NULL;
805                 mode = (mode << 3) + (c - '0');
806         }
807         *modep = mode;
808         return str;
811 static void load_tree(struct tree_entry *root)
813         struct object_entry *myoe;
814         struct tree_content *t;
815         unsigned long size;
816         char *buf;
817         const char *c;
819         root->tree = t = new_tree_content(8);
820         if (!memcmp(root->sha1, null_sha1, 20))
821                 return;
823         myoe = find_object(root->sha1);
824         if (myoe) {
825                 if (myoe->type != OBJ_TREE)
826                         die("Not a tree: %s", sha1_to_hex(root->sha1));
827                 buf = unpack_entry(myoe->offset, &size);
828         } else {
829                 char type[20];
830                 buf = read_sha1_file(root->sha1, type, &size);
831                 if (!buf || !strcmp(type, tree_type))
832                         die("Can't load tree %s", sha1_to_hex(root->sha1));
833         }
835         c = buf;
836         while (c != (buf + size)) {
837                 struct tree_entry *e = new_tree_entry();
839                 if (t->entry_count == t->entry_capacity)
840                         root->tree = t = grow_tree_content(t, 8);
841                 t->entries[t->entry_count++] = e;
843                 e->tree = NULL;
844                 c = get_mode(c, &e->mode);
845                 if (!c)
846                         die("Corrupt mode in %s", sha1_to_hex(root->sha1));
847                 e->name = to_atom(c, strlen(c));
848                 c += e->name->str_len + 1;
849                 memcpy(e->sha1, c, sizeof(e->sha1));
850                 c += 20;
851         }
852         free(buf);
855 static int tecmp (const void *_a, const void *_b)
857         struct tree_entry *a = *((struct tree_entry**)_a);
858         struct tree_entry *b = *((struct tree_entry**)_b);
859         return base_name_compare(
860                 a->name->str_dat, a->name->str_len, a->mode,
861                 b->name->str_dat, b->name->str_len, b->mode);
864 static void store_tree(struct tree_entry *root)
866         struct tree_content *t = root->tree;
867         unsigned int i;
868         size_t maxlen;
869         char *buf, *c;
871         if (memcmp(root->sha1, null_sha1, 20))
872                 return;
874         maxlen = 0;
875         for (i = 0; i < t->entry_count; i++) {
876                 maxlen += t->entries[i]->name->str_len + 34;
877                 if (t->entries[i]->tree)
878                         store_tree(t->entries[i]);
879         }
881         qsort(t->entries, t->entry_count, sizeof(t->entries[0]), tecmp);
882         buf = c = xmalloc(maxlen);
883         for (i = 0; i < t->entry_count; i++) {
884                 struct tree_entry *e = t->entries[i];
885                 c += sprintf(c, "%o", e->mode);
886                 *c++ = ' ';
887                 strcpy(c, e->name->str_dat);
888                 c += e->name->str_len + 1;
889                 memcpy(c, e->sha1, 20);
890                 c += 20;
891         }
892         store_object(OBJ_TREE, buf, c - buf, NULL, root->sha1, 0);
893         free(buf);
896 static int tree_content_set(
897         struct tree_entry *root,
898         const char *p,
899         const unsigned char *sha1,
900         const unsigned int mode)
902         struct tree_content *t = root->tree;
903         const char *slash1;
904         unsigned int i, n;
905         struct tree_entry *e;
907         slash1 = strchr(p, '/');
908         if (slash1)
909                 n = slash1 - p;
910         else
911                 n = strlen(p);
913         for (i = 0; i < t->entry_count; i++) {
914                 e = t->entries[i];
915                 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
916                         if (!slash1) {
917                                 if (e->mode == mode && !memcmp(e->sha1, sha1, 20))
918                                         return 0;
919                                 e->mode = mode;
920                                 memcpy(e->sha1, sha1, 20);
921                                 if (e->tree) {
922                                         release_tree_content_recursive(e->tree);
923                                         e->tree = NULL;
924                                 }
925                                 memcpy(root->sha1, null_sha1, 20);
926                                 return 1;
927                         }
928                         if (!S_ISDIR(e->mode)) {
929                                 e->tree = new_tree_content(8);
930                                 e->mode = S_IFDIR;
931                         }
932                         if (!e->tree)
933                                 load_tree(e);
934                         if (tree_content_set(e, slash1 + 1, sha1, mode)) {
935                                 memcpy(root->sha1, null_sha1, 20);
936                                 return 1;
937                         }
938                         return 0;
939                 }
940         }
942         if (t->entry_count == t->entry_capacity)
943                 root->tree = t = grow_tree_content(t, 8);
944         e = new_tree_entry();
945         e->name = to_atom(p, n);
946         t->entries[t->entry_count++] = e;
947         if (slash1) {
948                 e->tree = new_tree_content(8);
949                 e->mode = S_IFDIR;
950                 tree_content_set(e, slash1 + 1, sha1, mode);
951         } else {
952                 e->tree = NULL;
953                 e->mode = mode;
954                 memcpy(e->sha1, sha1, 20);
955         }
956         memcpy(root->sha1, null_sha1, 20);
957         return 1;
960 static int tree_content_remove(struct tree_entry *root, const char *p)
962         struct tree_content *t = root->tree;
963         const char *slash1;
964         unsigned int i, n;
965         struct tree_entry *e;
967         slash1 = strchr(p, '/');
968         if (slash1)
969                 n = slash1 - p;
970         else
971                 n = strlen(p);
973         for (i = 0; i < t->entry_count; i++) {
974                 e = t->entries[i];
975                 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
976                         if (!slash1 || !S_ISDIR(e->mode))
977                                 goto del_entry;
978                         if (!e->tree)
979                                 load_tree(e);
980                         if (tree_content_remove(e, slash1 + 1)) {
981                                 if (!e->tree->entry_count)
982                                         goto del_entry;
983                                 memcpy(root->sha1, null_sha1, 20);
984                                 return 1;
985                         }
986                         return 0;
987                 }
988         }
989         return 0;
991 del_entry:
992         for (i++; i < t->entry_count; i++)
993                 t->entries[i-1] = t->entries[i];
994         t->entry_count--;
995         release_tree_entry(e);
996         memcpy(root->sha1, null_sha1, 20);
997         return 1;
1000 static void init_pack_header()
1002         struct pack_header hdr;
1004         hdr.hdr_signature = htonl(PACK_SIGNATURE);
1005         hdr.hdr_version = htonl(2);
1006         hdr.hdr_entries = 0;
1008         ywrite(pack_fd, &hdr, sizeof(hdr));
1009         pack_size = sizeof(hdr);
1012 static void fixup_header_footer()
1014         SHA_CTX c;
1015         char hdr[8];
1016         unsigned long cnt;
1017         char *buf;
1018         size_t n;
1020         if (lseek(pack_fd, 0, SEEK_SET) != 0)
1021                 die("Failed seeking to start: %s", strerror(errno));
1023         SHA1_Init(&c);
1024         yread(pack_fd, hdr, 8);
1025         SHA1_Update(&c, hdr, 8);
1027         cnt = htonl(object_count);
1028         SHA1_Update(&c, &cnt, 4);
1029         ywrite(pack_fd, &cnt, 4);
1031         buf = xmalloc(128 * 1024);
1032         for (;;) {
1033                 n = xread(pack_fd, buf, 128 * 1024);
1034                 if (n <= 0)
1035                         break;
1036                 SHA1_Update(&c, buf, n);
1037         }
1038         free(buf);
1040         SHA1_Final(pack_sha1, &c);
1041         ywrite(pack_fd, pack_sha1, sizeof(pack_sha1));
1044 static int oecmp (const void *_a, const void *_b)
1046         struct object_entry *a = *((struct object_entry**)_a);
1047         struct object_entry *b = *((struct object_entry**)_b);
1048         return memcmp(a->sha1, b->sha1, sizeof(a->sha1));
1051 static void write_index(const char *idx_name)
1053         struct sha1file *f;
1054         struct object_entry **idx, **c, **last;
1055         struct object_entry *e;
1056         struct object_entry_pool *o;
1057         unsigned int array[256];
1058         int i;
1060         /* Build the sorted table of object IDs. */
1061         idx = xmalloc(object_count * sizeof(struct object_entry*));
1062         c = idx;
1063         for (o = blocks; o; o = o->next_pool)
1064                 for (e = o->entries; e != o->next_free; e++)
1065                         *c++ = e;
1066         last = idx + object_count;
1067         qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
1069         /* Generate the fan-out array. */
1070         c = idx;
1071         for (i = 0; i < 256; i++) {
1072                 struct object_entry **next = c;;
1073                 while (next < last) {
1074                         if ((*next)->sha1[0] != i)
1075                                 break;
1076                         next++;
1077                 }
1078                 array[i] = htonl(next - idx);
1079                 c = next;
1080         }
1082         f = sha1create("%s", idx_name);
1083         sha1write(f, array, 256 * sizeof(int));
1084         for (c = idx; c != last; c++) {
1085                 unsigned int offset = htonl((*c)->offset);
1086                 sha1write(f, &offset, 4);
1087                 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
1088         }
1089         sha1write(f, pack_sha1, sizeof(pack_sha1));
1090         sha1close(f, NULL, 1);
1091         free(idx);
1094 static void dump_branches()
1096         static const char *msg = "fast-import";
1097         unsigned int i;
1098         struct branch *b;
1099         struct ref_lock *lock;
1101         for (i = 0; i < branch_table_sz; i++) {
1102                 for (b = branch_table[i]; b; b = b->table_next_branch) {
1103                         lock = lock_any_ref_for_update(b->name, NULL, 0);
1104                         if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1105                                 die("Can't write %s", b->name);
1106                 }
1107         }
1110 static void dump_tags()
1112         static const char *msg = "fast-import";
1113         struct tag *t;
1114         struct ref_lock *lock;
1115         char path[PATH_MAX];
1117         for (t = first_tag; t; t = t->next_tag) {
1118                 sprintf(path, "refs/tags/%s", t->name);
1119                 lock = lock_any_ref_for_update(path, NULL, 0);
1120                 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1121                         die("Can't write %s", path);
1122         }
1125 static void read_next_command()
1127         read_line(&command_buf, stdin, '\n');
1130 static void cmd_mark()
1132         if (!strncmp("mark :", command_buf.buf, 6)) {
1133                 next_mark = strtoul(command_buf.buf + 6, NULL, 10);
1134                 read_next_command();
1135         }
1136         else
1137                 next_mark = 0;
1140 static void* cmd_data (size_t *size)
1142         size_t n = 0;
1143         void *buffer;
1144         size_t length;
1146         if (strncmp("data ", command_buf.buf, 5))
1147                 die("Expected 'data n' command, found: %s", command_buf.buf);
1149         length = strtoul(command_buf.buf + 5, NULL, 10);
1150         buffer = xmalloc(length);
1152         while (n < length) {
1153                 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1154                 if (!s && feof(stdin))
1155                         die("EOF in data (%lu bytes remaining)", length - n);
1156                 n += s;
1157         }
1159         if (fgetc(stdin) != '\n')
1160                 die("An lf did not trail the binary data as expected.");
1162         *size = length;
1163         return buffer;
1166 static void cmd_new_blob()
1168         size_t l;
1169         void *d;
1171         read_next_command();
1172         cmd_mark();
1173         d = cmd_data(&l);
1175         if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1176                 free(d);
1179 static void unload_one_branch()
1181         while (cur_active_branches
1182                 && cur_active_branches >= max_active_branches) {
1183                 unsigned long min_commit = ULONG_MAX;
1184                 struct branch *e, *l = NULL, *p = NULL;
1186                 for (e = active_branches; e; e = e->active_next_branch) {
1187                         if (e->last_commit < min_commit) {
1188                                 p = l;
1189                                 min_commit = e->last_commit;
1190                         }
1191                         l = e;
1192                 }
1194                 if (p) {
1195                         e = p->active_next_branch;
1196                         p->active_next_branch = e->active_next_branch;
1197                 } else {
1198                         e = active_branches;
1199                         active_branches = e->active_next_branch;
1200                 }
1201                 e->active_next_branch = NULL;
1202                 if (e->branch_tree.tree) {
1203                         release_tree_content_recursive(e->branch_tree.tree);
1204                         e->branch_tree.tree = NULL;
1205                 }
1206                 cur_active_branches--;
1207         }
1210 static void load_branch(struct branch *b)
1212         load_tree(&b->branch_tree);
1213         b->active_next_branch = active_branches;
1214         active_branches = b;
1215         cur_active_branches++;
1216         branch_load_count++;
1219 static void file_change_m(struct branch *b)
1221         const char *p = command_buf.buf + 2;
1222         char *p_uq;
1223         const char *endp;
1224         struct object_entry *oe;
1225         unsigned char sha1[20];
1226         unsigned int mode;
1227         char type[20];
1229         p = get_mode(p, &mode);
1230         if (!p)
1231                 die("Corrupt mode: %s", command_buf.buf);
1232         switch (mode) {
1233         case S_IFREG | 0644:
1234         case S_IFREG | 0755:
1235         case S_IFLNK:
1236         case 0644:
1237         case 0755:
1238                 /* ok */
1239                 break;
1240         default:
1241                 die("Corrupt mode: %s", command_buf.buf);
1242         }
1244         if (*p == ':') {
1245                 char *x;
1246                 oe = find_mark(strtoul(p + 1, &x, 10));
1247                 p = x;
1248         } else {
1249                 if (get_sha1_hex(p, sha1))
1250                         die("Invalid SHA1: %s", command_buf.buf);
1251                 oe = find_object(sha1);
1252                 p += 40;
1253         }
1254         if (*p++ != ' ')
1255                 die("Missing space after SHA1: %s", command_buf.buf);
1257         p_uq = unquote_c_style(p, &endp);
1258         if (p_uq) {
1259                 if (*endp)
1260                         die("Garbage after path in: %s", command_buf.buf);
1261                 p = p_uq;
1262         }
1264         if (oe) {
1265                 if (oe->type != OBJ_BLOB)
1266                         die("Not a blob (actually a %s): %s",
1267                                 command_buf.buf, type_names[oe->type]);
1268         } else {
1269                 if (sha1_object_info(sha1, type, NULL))
1270                         die("Blob not found: %s", command_buf.buf);
1271                 if (strcmp(blob_type, type))
1272                         die("Not a blob (actually a %s): %s",
1273                                 command_buf.buf, type);
1274         }
1276         tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1278         if (p_uq)
1279                 free(p_uq);
1282 static void file_change_d(struct branch *b)
1284         const char *p = command_buf.buf + 2;
1285         char *p_uq;
1286         const char *endp;
1288         p_uq = unquote_c_style(p, &endp);
1289         if (p_uq) {
1290                 if (*endp)
1291                         die("Garbage after path in: %s", command_buf.buf);
1292                 p = p_uq;
1293         }
1294         tree_content_remove(&b->branch_tree, p);
1295         if (p_uq)
1296                 free(p_uq);
1299 static void cmd_new_commit()
1301         struct branch *b;
1302         void *msg;
1303         size_t msglen;
1304         char *str_uq;
1305         const char *endp;
1306         char *sp;
1307         char *author = NULL;
1308         char *committer = NULL;
1309         char *body;
1311         /* Obtain the branch name from the rest of our command */
1312         sp = strchr(command_buf.buf, ' ') + 1;
1313         str_uq = unquote_c_style(sp, &endp);
1314         if (str_uq) {
1315                 if (*endp)
1316                         die("Garbage after ref in: %s", command_buf.buf);
1317                 sp = str_uq;
1318         }
1319         b = lookup_branch(sp);
1320         if (!b)
1321                 die("Branch not declared: %s", sp);
1322         if (str_uq)
1323                 free(str_uq);
1325         read_next_command();
1326         cmd_mark();
1327         if (!strncmp("author ", command_buf.buf, 7)) {
1328                 author = strdup(command_buf.buf);
1329                 read_next_command();
1330         }
1331         if (!strncmp("committer ", command_buf.buf, 10)) {
1332                 committer = strdup(command_buf.buf);
1333                 read_next_command();
1334         }
1335         if (!committer)
1336                 die("Expected committer but didn't get one");
1337         msg = cmd_data(&msglen);
1339         /* ensure the branch is active/loaded */
1340         if (!b->branch_tree.tree || !max_active_branches) {
1341                 unload_one_branch();
1342                 load_branch(b);
1343         }
1345         /* file_change* */
1346         for (;;) {
1347                 read_next_command();
1348                 if (1 == command_buf.len)
1349                         break;
1350                 else if (!strncmp("M ", command_buf.buf, 2))
1351                         file_change_m(b);
1352                 else if (!strncmp("D ", command_buf.buf, 2))
1353                         file_change_d(b);
1354                 else
1355                         die("Unsupported file_change: %s", command_buf.buf);
1356         }
1358         /* build the tree and the commit */
1359         store_tree(&b->branch_tree);
1360         body = xmalloc(97 + msglen
1361                 + (author
1362                         ? strlen(author) + strlen(committer)
1363                         : 2 * strlen(committer)));
1364         sp = body;
1365         sp += sprintf(sp, "tree %s\n", sha1_to_hex(b->branch_tree.sha1));
1366         if (memcmp(b->sha1, null_sha1, 20))
1367                 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1368         if (author)
1369                 sp += sprintf(sp, "%s\n", author);
1370         else
1371                 sp += sprintf(sp, "author %s\n", committer + 10);
1372         sp += sprintf(sp, "%s\n\n", committer);
1373         memcpy(sp, msg, msglen);
1374         sp += msglen;
1375         if (author)
1376                 free(author);
1377         free(committer);
1378         free(msg);
1380         store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1, next_mark);
1381         free(body);
1382         b->last_commit = object_count_by_type[OBJ_COMMIT];
1385 static void cmd_new_branch()
1387         struct branch *b;
1388         char *str_uq;
1389         const char *endp;
1390         char *sp;
1392         /* Obtain the new branch name from the rest of our command */
1393         sp = strchr(command_buf.buf, ' ') + 1;
1394         str_uq = unquote_c_style(sp, &endp);
1395         if (str_uq) {
1396                 if (*endp)
1397                         die("Garbage after ref in: %s", command_buf.buf);
1398                 sp = str_uq;
1399         }
1400         b = new_branch(sp);
1401         if (str_uq)
1402                 free(str_uq);
1403         read_next_command();
1405         /* from ... */
1406         if (!strncmp("from ", command_buf.buf, 5)) {
1407                 const char *from;
1408                 struct branch *s;
1410                 from = strchr(command_buf.buf, ' ') + 1;
1411                 str_uq = unquote_c_style(from, &endp);
1412                 if (str_uq) {
1413                         if (*endp)
1414                                 die("Garbage after string in: %s", command_buf.buf);
1415                         from = str_uq;
1416                 }
1418                 s = lookup_branch(from);
1419                 if (b == s)
1420                         die("Can't create a branch from itself: %s", b->name);
1421                 else if (s) {
1422                         memcpy(b->sha1, s->sha1, 20);
1423                         memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
1424                 } else if (*from == ':') {
1425                         unsigned long idnum = strtoul(from + 1, NULL, 10);
1426                         struct object_entry *oe = find_mark(idnum);
1427                         unsigned long size;
1428                         char *buf;
1429                         if (oe->type != OBJ_COMMIT)
1430                                 die("Mark :%lu not a commit", idnum);
1431                         memcpy(b->sha1, oe->sha1, 20);
1432                         buf = unpack_entry(oe->offset, &size);
1433                         if (!buf || size < 46)
1434                                 die("Not a valid commit: %s", from);
1435                         if (memcmp("tree ", buf, 5)
1436                                 || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1437                                 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1438                         free(buf);
1439                 } else if (!get_sha1(from, b->sha1)) {
1440                         if (!memcmp(b->sha1, null_sha1, 20))
1441                                 memcpy(b->branch_tree.sha1, null_sha1, 20);
1442                         else {
1443                                 unsigned long size;
1444                                 char *buf;
1446                                 buf = read_object_with_reference(b->sha1,
1447                                         type_names[OBJ_COMMIT], &size, b->sha1);
1448                                 if (!buf || size < 46)
1449                                         die("Not a valid commit: %s", from);
1450                                 if (memcmp("tree ", buf, 5)
1451                                         || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1452                                         die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1453                                 free(buf);
1454                         }
1455                 } else
1456                         die("Invalid ref name or SHA1 expression: %s", from);
1458                 if (str_uq)
1459                         free(str_uq);
1460                 read_next_command();
1461         } else {
1462                 memcpy(b->sha1, null_sha1, 20);
1463                 memcpy(b->branch_tree.sha1, null_sha1, 20);
1464         }
1466         if (command_buf.eof || command_buf.len > 1)
1467                 die("An lf did not terminate the branch command as expected.");
1470 static void cmd_new_tag()
1472         char *str_uq;
1473         const char *endp;
1474         char *sp;
1475         const char *from;
1476         char *tagger;
1477         struct branch *s;
1478         void *msg;
1479         size_t msglen;
1480         char *body;
1481         struct tag *t;
1482         unsigned char sha1[20];
1484         /* Obtain the new tag name from the rest of our command */
1485         sp = strchr(command_buf.buf, ' ') + 1;
1486         str_uq = unquote_c_style(sp, &endp);
1487         if (str_uq) {
1488                 if (*endp)
1489                         die("Garbage after tag name in: %s", command_buf.buf);
1490                 sp = str_uq;
1491         }
1492         t = pool_alloc(sizeof(struct tag));
1493         t->next_tag = NULL;
1494         t->name = pool_strdup(sp);
1495         if (last_tag)
1496                 last_tag->next_tag = t;
1497         else
1498                 first_tag = t;
1499         last_tag = t;
1500         if (str_uq)
1501                 free(str_uq);
1502         read_next_command();
1504         /* from ... */
1505         if (strncmp("from ", command_buf.buf, 5))
1506                 die("Expected from command, got %s", command_buf.buf);
1508         from = strchr(command_buf.buf, ' ') + 1;
1509         str_uq = unquote_c_style(from, &endp);
1510         if (str_uq) {
1511                 if (*endp)
1512                         die("Garbage after string in: %s", command_buf.buf);
1513                 from = str_uq;
1514         }
1516         s = lookup_branch(from);
1517         if (s) {
1518                 memcpy(sha1, s->sha1, 20);
1519         } else if (*from == ':') {
1520                 unsigned long idnum = strtoul(from + 1, NULL, 10);
1521                 struct object_entry *oe = find_mark(idnum);
1522                 if (oe->type != OBJ_COMMIT)
1523                         die("Mark :%lu not a commit", idnum);
1524                 memcpy(sha1, oe->sha1, 20);
1525         } else if (!get_sha1(from, sha1)) {
1526                 unsigned long size;
1527                 char *buf;
1529                 buf = read_object_with_reference(sha1,
1530                         type_names[OBJ_COMMIT], &size, sha1);
1531                 if (!buf || size < 46)
1532                         die("Not a valid commit: %s", from);
1533                 free(buf);
1534         } else
1535                 die("Invalid ref name or SHA1 expression: %s", from);
1537         if (str_uq)
1538                 free(str_uq);
1539         read_next_command();
1541         /* tagger ... */
1542         if (strncmp("tagger ", command_buf.buf, 7))
1543                 die("Expected tagger command, got %s", command_buf.buf);
1544         tagger = strdup(command_buf.buf);
1546         /* tag payload/message */
1547         read_next_command();
1548         msg = cmd_data(&msglen);
1550         /* build the tag object */
1551         body = xmalloc(67 + strlen(t->name) + strlen(tagger) + msglen);
1552         sp = body;
1553         sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1554         sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1555         sp += sprintf(sp, "tag %s\n", t->name);
1556         sp += sprintf(sp, "%s\n\n", tagger);
1557         memcpy(sp, msg, msglen);
1558         sp += msglen;
1559         free(tagger);
1560         free(msg);
1562         store_object(OBJ_TAG, body, sp - body, NULL, t->sha1, 0);
1563         free(body);
1566 static const char fast_import_usage[] =
1567 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] temp.pack";
1569 int main(int argc, const char **argv)
1571         const char *base_name;
1572         int i;
1573         unsigned long est_obj_cnt = 1000;
1574         char *pack_name;
1575         char *idx_name;
1576         struct stat sb;
1578         setup_ident();
1579         git_config(git_default_config);
1581         for (i = 1; i < argc; i++) {
1582                 const char *a = argv[i];
1584                 if (*a != '-' || !strcmp(a, "--"))
1585                         break;
1586                 else if (!strncmp(a, "--objects=", 10))
1587                         est_obj_cnt = strtoul(a + 10, NULL, 0);
1588                 else if (!strncmp(a, "--depth=", 8))
1589                         max_depth = strtoul(a + 8, NULL, 0);
1590                 else if (!strncmp(a, "--active-branches=", 18))
1591                         max_active_branches = strtoul(a + 18, NULL, 0);
1592                 else
1593                         die("unknown option %s", a);
1594         }
1595         if ((i+1) != argc)
1596                 usage(fast_import_usage);
1597         base_name = argv[i];
1599         pack_name = xmalloc(strlen(base_name) + 6);
1600         sprintf(pack_name, "%s.pack", base_name);
1601         idx_name = xmalloc(strlen(base_name) + 5);
1602         sprintf(idx_name, "%s.idx", base_name);
1604         pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
1605         if (pack_fd < 0)
1606                 die("Can't create %s: %s", pack_name, strerror(errno));
1608         init_pack_header();
1609         alloc_objects(est_obj_cnt);
1610         strbuf_init(&command_buf);
1612         atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1613         branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1614         avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1615         marks = pool_calloc(1, sizeof(struct mark_set));
1617         for (;;) {
1618                 read_next_command();
1619                 if (command_buf.eof)
1620                         break;
1621                 else if (!strcmp("blob", command_buf.buf))
1622                         cmd_new_blob();
1623                 else if (!strncmp("branch ", command_buf.buf, 7))
1624                         cmd_new_branch();
1625                 else if (!strncmp("commit ", command_buf.buf, 7))
1626                         cmd_new_commit();
1627                 else if (!strncmp("tag ", command_buf.buf, 4))
1628                         cmd_new_tag();
1629                 else
1630                         die("Unsupported command: %s", command_buf.buf);
1631         }
1633         fixup_header_footer();
1634         close(pack_fd);
1635         write_index(idx_name);
1636         dump_branches();
1637         dump_tags();
1639         fprintf(stderr, "%s statistics:\n", argv[0]);
1640         fprintf(stderr, "---------------------------------------------------\n");
1641         fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow  )\n", alloc_count, alloc_count - est_obj_cnt);
1642         fprintf(stderr, "Total objects:   %10lu (%10lu duplicates)\n", object_count, duplicate_count);
1643         fprintf(stderr, "      blobs  :   %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB]);
1644         fprintf(stderr, "      trees  :   %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]);
1645         fprintf(stderr, "      commits:   %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]);
1646         fprintf(stderr, "      tags   :   %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
1647         fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
1648         fprintf(stderr, "      marks:     %10u (%10lu unique    )\n", (1 << marks->shift) * 1024, marks_set_count);
1649         fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
1650         fprintf(stderr, "Memory total:    %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1651         fprintf(stderr, "       pools:    %10lu KiB\n", total_allocd/1024);
1652         fprintf(stderr, "     objects:    %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1653         fprintf(stderr, "Pack remaps:     %10lu\n", remap_count);
1654         fprintf(stderr, "---------------------------------------------------\n");
1656         stat(pack_name, &sb);
1657         fprintf(stderr, "Pack size:       %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1658         stat(idx_name, &sb);
1659         fprintf(stderr, "Index size:      %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1661         fprintf(stderr, "\n");
1663         return 0;