Code

8328e004bb2fad1c1a7a831fedf0ef2aab5c21f7
[git.git] / fast-import.c
1 /*
2 Format of STDIN stream:
4   stream ::= cmd*;
6   cmd ::= new_blob
7         | new_commit
8         | new_tag
9         ;
11   new_blob ::= 'blob' lf
12         mark?
13     file_content;
14   file_content ::= data;
16   new_commit ::= 'commit' sp ref_str lf
17     mark?
18     ('author' sp name '<' email '>' ts tz lf)?
19     'committer' sp name '<' email '>' ts tz lf
20     commit_msg
21     ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
22     file_change*
23     lf;
24   commit_msg ::= data;
26   file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
27                 | 'D' sp path_str lf
28                 ;
29   mode ::= '644' | '755';
31   new_tag ::= 'tag' sp tag_str lf
32     'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
33         'tagger' sp name '<' email '>' ts tz lf
34     tag_msg;
35   tag_msg ::= data;
37      # note: the first idnum in a stream should be 1 and subsequent
38      # idnums should not have gaps between values as this will cause
39      # the stream parser to reserve space for the gapped values.  An
40          # idnum can be updated in the future to a new object by issuing
41      # a new mark directive with the old idnum.
42          #
43   mark ::= 'mark' sp idnum lf;
45      # note: declen indicates the length of binary_data in bytes.
46      # declen does not include the lf preceeding or trailing the
47      # binary data.
48      #
49   data ::= 'data' sp declen lf
50     binary_data
51         lf;
53      # note: quoted strings are C-style quoting supporting \c for
54      # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
55          # is the signed byte value in octal.  Note that the only
56      # characters which must actually be escaped to protect the
57      # stream formatting is: \, " and LF.  Otherwise these values
58          # are UTF8.
59      #
60   ref_str     ::= ref     | '"' quoted(ref)     '"' ;
61   sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
62   tag_str     ::= tag     | '"' quoted(tag)     '"' ;
63   path_str    ::= path    | '"' quoted(path)    '"' ;
65   declen ::= # unsigned 32 bit value, ascii base10 notation;
66   binary_data ::= # file content, not interpreted;
68   sp ::= # ASCII space character;
69   lf ::= # ASCII newline (LF) character;
71      # note: a colon (':') must precede the numerical value assigned to
72          # an idnum.  This is to distinguish it from a ref or tag name as
73      # GIT does not permit ':' in ref or tag strings.
74          #
75   idnum   ::= ':' declen;
76   path    ::= # GIT style file path, e.g. "a/b/c";
77   ref     ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
78   tag     ::= # GIT tag name, e.g. "FIREFOX_1_5";
79   sha1exp ::= # Any valid GIT SHA1 expression;
80   hexsha1 ::= # SHA1 in hexadecimal format;
82      # note: name and email are UTF8 strings, however name must not
83          # contain '<' or lf and email must not contain any of the
84      # following: '<', '>', lf.
85          #
86   name  ::= # valid GIT author/committer name;
87   email ::= # valid GIT author/committer email;
88   ts    ::= # time since the epoch in seconds, ascii base10 notation;
89   tz    ::= # GIT style timezone;
90 */
92 #include "builtin.h"
93 #include "cache.h"
94 #include "object.h"
95 #include "blob.h"
96 #include "tree.h"
97 #include "delta.h"
98 #include "pack.h"
99 #include "refs.h"
100 #include "csum-file.h"
101 #include "strbuf.h"
102 #include "quote.h"
104 struct object_entry
106         struct object_entry *next;
107         enum object_type type;
108         unsigned long offset;
109         unsigned char sha1[20];
110 };
112 struct object_entry_pool
114         struct object_entry_pool *next_pool;
115         struct object_entry *next_free;
116         struct object_entry *end;
117         struct object_entry entries[FLEX_ARRAY]; /* more */
118 };
120 struct mark_set
122         int shift;
123         union {
124                 struct object_entry *marked[1024];
125                 struct mark_set *sets[1024];
126         } data;
127 };
129 struct last_object
131         void *data;
132         unsigned int len;
133         unsigned int depth;
134         unsigned char sha1[20];
135 };
137 struct mem_pool
139         struct mem_pool *next_pool;
140         char *next_free;
141         char *end;
142         char space[FLEX_ARRAY]; /* more */
143 };
145 struct atom_str
147         struct atom_str *next_atom;
148         int str_len;
149         char str_dat[FLEX_ARRAY]; /* more */
150 };
152 struct tree_content;
153 struct tree_entry
155         struct tree_content *tree;
156         struct atom_str* name;
157         unsigned int mode;
158         unsigned char sha1[20];
159 };
161 struct tree_content
163         unsigned int entry_capacity; /* must match avail_tree_content */
164         unsigned int entry_count;
165         struct tree_entry *entries[FLEX_ARRAY]; /* more */
166 };
168 struct avail_tree_content
170         unsigned int entry_capacity; /* must match tree_content */
171         struct avail_tree_content *next_avail;
172 };
174 struct branch
176         struct branch *table_next_branch;
177         struct branch *active_next_branch;
178         const char *name;
179         unsigned long last_commit;
180         struct tree_entry branch_tree;
181         unsigned char sha1[20];
182 };
184 struct tag
186         struct tag *next_tag;
187         const char *name;
188         unsigned char sha1[20];
189 };
192 /* Stats and misc. counters */
193 static unsigned long max_depth = 10;
194 static unsigned long alloc_count;
195 static unsigned long branch_count;
196 static unsigned long branch_load_count;
197 static unsigned long remap_count;
198 static unsigned long object_count;
199 static unsigned long duplicate_count;
200 static unsigned long marks_set_count;
201 static unsigned long object_count_by_type[9];
202 static unsigned long duplicate_count_by_type[9];
204 /* Memory pools */
205 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
206 static size_t total_allocd;
207 static struct mem_pool *mem_pool;
209 /* Atom management */
210 static unsigned int atom_table_sz = 4451;
211 static unsigned int atom_cnt;
212 static struct atom_str **atom_table;
214 /* The .pack file being generated */
215 static int pack_fd;
216 static unsigned long pack_size;
217 static unsigned char pack_sha1[20];
218 static void* pack_base;
219 static size_t pack_mlen;
221 /* Table of objects we've written. */
222 static unsigned int object_entry_alloc = 1000;
223 static struct object_entry_pool *blocks;
224 static struct object_entry *object_table[1 << 16];
225 static struct mark_set *marks;
226 static const char* mark_file;
228 /* Our last blob */
229 static struct last_object last_blob;
231 /* Tree management */
232 static unsigned int tree_entry_alloc = 1000;
233 static void *avail_tree_entry;
234 static unsigned int avail_tree_table_sz = 100;
235 static struct avail_tree_content **avail_tree_table;
237 /* Branch data */
238 static unsigned long max_active_branches = 5;
239 static unsigned long cur_active_branches;
240 static unsigned long branch_table_sz = 1039;
241 static struct branch **branch_table;
242 static struct branch *active_branches;
244 /* Tag data */
245 static struct tag *first_tag;
246 static struct tag *last_tag;
248 /* Input stream parsing */
249 static struct strbuf command_buf;
250 static unsigned long next_mark;
251 static FILE* branch_log;
254 static void alloc_objects(int cnt)
256         struct object_entry_pool *b;
258         b = xmalloc(sizeof(struct object_entry_pool)
259                 + cnt * sizeof(struct object_entry));
260         b->next_pool = blocks;
261         b->next_free = b->entries;
262         b->end = b->entries + cnt;
263         blocks = b;
264         alloc_count += cnt;
267 static struct object_entry* new_object(unsigned char *sha1)
269         struct object_entry *e;
271         if (blocks->next_free == blocks->end)
272                 alloc_objects(object_entry_alloc);
274         e = blocks->next_free++;
275         memcpy(e->sha1, sha1, sizeof(e->sha1));
276         return e;
279 static struct object_entry* find_object(unsigned char *sha1)
281         unsigned int h = sha1[0] << 8 | sha1[1];
282         struct object_entry *e;
283         for (e = object_table[h]; e; e = e->next)
284                 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
285                         return e;
286         return NULL;
289 static struct object_entry* insert_object(unsigned char *sha1)
291         unsigned int h = sha1[0] << 8 | sha1[1];
292         struct object_entry *e = object_table[h];
293         struct object_entry *p = NULL;
295         while (e) {
296                 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
297                         return e;
298                 p = e;
299                 e = e->next;
300         }
302         e = new_object(sha1);
303         e->next = NULL;
304         e->offset = 0;
305         if (p)
306                 p->next = e;
307         else
308                 object_table[h] = e;
309         return e;
312 static unsigned int hc_str(const char *s, size_t len)
314         unsigned int r = 0;
315         while (len-- > 0)
316                 r = r * 31 + *s++;
317         return r;
320 static void* pool_alloc(size_t len)
322         struct mem_pool *p;
323         void *r;
325         for (p = mem_pool; p; p = p->next_pool)
326                 if ((p->end - p->next_free >= len))
327                         break;
329         if (!p) {
330                 if (len >= (mem_pool_alloc/2)) {
331                         total_allocd += len;
332                         return xmalloc(len);
333                 }
334                 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
335                 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
336                 p->next_pool = mem_pool;
337                 p->next_free = p->space;
338                 p->end = p->next_free + mem_pool_alloc;
339                 mem_pool = p;
340         }
342         r = p->next_free;
343         /* round out to a pointer alignment */
344         if (len & (sizeof(void*) - 1))
345                 len += sizeof(void*) - (len & (sizeof(void*) - 1));
346         p->next_free += len;
347         return r;
350 static void* pool_calloc(size_t count, size_t size)
352         size_t len = count * size;
353         void *r = pool_alloc(len);
354         memset(r, 0, len);
355         return r;
358 static char* pool_strdup(const char *s)
360         char *r = pool_alloc(strlen(s) + 1);
361         strcpy(r, s);
362         return r;
365 static void insert_mark(unsigned long idnum, struct object_entry *oe)
367         struct mark_set *s = marks;
368         while ((idnum >> s->shift) >= 1024) {
369                 s = pool_calloc(1, sizeof(struct mark_set));
370                 s->shift = marks->shift + 10;
371                 s->data.sets[0] = marks;
372                 marks = s;
373         }
374         while (s->shift) {
375                 unsigned long i = idnum >> s->shift;
376                 idnum -= i << s->shift;
377                 if (!s->data.sets[i]) {
378                         s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
379                         s->data.sets[i]->shift = s->shift - 10;
380                 }
381                 s = s->data.sets[i];
382         }
383         if (!s->data.marked[idnum])
384                 marks_set_count++;
385         s->data.marked[idnum] = oe;
388 static struct object_entry* find_mark(unsigned long idnum)
390         unsigned long orig_idnum = idnum;
391         struct mark_set *s = marks;
392         struct object_entry *oe = NULL;
393         if ((idnum >> s->shift) < 1024) {
394                 while (s && s->shift) {
395                         unsigned long i = idnum >> s->shift;
396                         idnum -= i << s->shift;
397                         s = s->data.sets[i];
398                 }
399                 if (s)
400                         oe = s->data.marked[idnum];
401         }
402         if (!oe)
403                 die("mark :%lu not declared", orig_idnum);
404         return oe;
407 static struct atom_str* to_atom(const char *s, size_t len)
409         unsigned int hc = hc_str(s, len) % atom_table_sz;
410         struct atom_str *c;
412         for (c = atom_table[hc]; c; c = c->next_atom)
413                 if (c->str_len == len && !strncmp(s, c->str_dat, len))
414                         return c;
416         c = pool_alloc(sizeof(struct atom_str) + len + 1);
417         c->str_len = len;
418         strncpy(c->str_dat, s, len);
419         c->str_dat[len] = 0;
420         c->next_atom = atom_table[hc];
421         atom_table[hc] = c;
422         atom_cnt++;
423         return c;
426 static struct branch* lookup_branch(const char *name)
428         unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
429         struct branch *b;
431         for (b = branch_table[hc]; b; b = b->table_next_branch)
432                 if (!strcmp(name, b->name))
433                         return b;
434         return NULL;
437 static struct branch* new_branch(const char *name)
439         unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
440         struct branch* b = lookup_branch(name);
442         if (b)
443                 die("Invalid attempt to create duplicate branch: %s", name);
444         if (check_ref_format(name))
445                 die("Branch name doesn't conform to GIT standards: %s", name);
447         b = pool_calloc(1, sizeof(struct branch));
448         b->name = pool_strdup(name);
449         b->table_next_branch = branch_table[hc];
450         branch_table[hc] = b;
451         branch_count++;
452         return b;
455 static unsigned int hc_entries(unsigned int cnt)
457         cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
458         return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
461 static struct tree_content* new_tree_content(unsigned int cnt)
463         struct avail_tree_content *f, *l = NULL;
464         struct tree_content *t;
465         unsigned int hc = hc_entries(cnt);
467         for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
468                 if (f->entry_capacity >= cnt)
469                         break;
471         if (f) {
472                 if (l)
473                         l->next_avail = f->next_avail;
474                 else
475                         avail_tree_table[hc] = f->next_avail;
476         } else {
477                 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
478                 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
479                 f->entry_capacity = cnt;
480         }
482         t = (struct tree_content*)f;
483         t->entry_count = 0;
484         return t;
487 static void release_tree_entry(struct tree_entry *e);
488 static void release_tree_content(struct tree_content *t)
490         struct avail_tree_content *f = (struct avail_tree_content*)t;
491         unsigned int hc = hc_entries(f->entry_capacity);
492         f->next_avail = avail_tree_table[hc];
493         avail_tree_table[hc] = f;
496 static void release_tree_content_recursive(struct tree_content *t)
498         unsigned int i;
499         for (i = 0; i < t->entry_count; i++)
500                 release_tree_entry(t->entries[i]);
501         release_tree_content(t);
504 static struct tree_content* grow_tree_content(
505         struct tree_content *t,
506         int amt)
508         struct tree_content *r = new_tree_content(t->entry_count + amt);
509         r->entry_count = t->entry_count;
510         memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
511         release_tree_content(t);
512         return r;
515 static struct tree_entry* new_tree_entry()
517         struct tree_entry *e;
519         if (!avail_tree_entry) {
520                 unsigned int n = tree_entry_alloc;
521                 total_allocd += n * sizeof(struct tree_entry);
522                 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
523                 while (n--) {
524                         *((void**)e) = e + 1;
525                         e++;
526                 }
527         }
529         e = avail_tree_entry;
530         avail_tree_entry = *((void**)e);
531         return e;
534 static void release_tree_entry(struct tree_entry *e)
536         if (e->tree)
537                 release_tree_content_recursive(e->tree);
538         *((void**)e) = avail_tree_entry;
539         avail_tree_entry = e;
542 static void yread(int fd, void *buffer, size_t length)
544         ssize_t ret = 0;
545         while (ret < length) {
546                 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
547                 if (!size)
548                         die("Read from descriptor %i: end of stream", fd);
549                 if (size < 0)
550                         die("Read from descriptor %i: %s", fd, strerror(errno));
551                 ret += size;
552         }
555 static void ywrite(int fd, void *buffer, size_t length)
557         ssize_t ret = 0;
558         while (ret < length) {
559                 ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret);
560                 if (!size)
561                         die("Write to descriptor %i: end of file", fd);
562                 if (size < 0)
563                         die("Write to descriptor %i: %s", fd, strerror(errno));
564                 ret += size;
565         }
568 static size_t encode_header(
569         enum object_type type,
570         size_t size,
571         unsigned char *hdr)
573         int n = 1;
574         unsigned char c;
576         if (type < OBJ_COMMIT || type > OBJ_DELTA)
577                 die("bad type %d", type);
579         c = (type << 4) | (size & 15);
580         size >>= 4;
581         while (size) {
582                 *hdr++ = c | 0x80;
583                 c = size & 0x7f;
584                 size >>= 7;
585                 n++;
586         }
587         *hdr = c;
588         return n;
591 static int store_object(
592         enum object_type type,
593         void *dat,
594         size_t datlen,
595         struct last_object *last,
596         unsigned char *sha1out,
597         unsigned long mark)
599         void *out, *delta;
600         struct object_entry *e;
601         unsigned char hdr[96];
602         unsigned char sha1[20];
603         unsigned long hdrlen, deltalen;
604         SHA_CTX c;
605         z_stream s;
607         hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
608         SHA1_Init(&c);
609         SHA1_Update(&c, hdr, hdrlen);
610         SHA1_Update(&c, dat, datlen);
611         SHA1_Final(sha1, &c);
612         if (sha1out)
613                 memcpy(sha1out, sha1, sizeof(sha1));
615         e = insert_object(sha1);
616         if (mark)
617                 insert_mark(mark, e);
618         if (e->offset) {
619                 duplicate_count++;
620                 duplicate_count_by_type[type]++;
621                 return 1;
622         }
623         e->type = type;
624         e->offset = pack_size;
625         object_count++;
626         object_count_by_type[type]++;
628         if (last && last->data && last->depth < max_depth)
629                 delta = diff_delta(last->data, last->len,
630                         dat, datlen,
631                         &deltalen, 0);
632         else
633                 delta = 0;
635         memset(&s, 0, sizeof(s));
636         deflateInit(&s, zlib_compression_level);
638         if (delta) {
639                 last->depth++;
640                 s.next_in = delta;
641                 s.avail_in = deltalen;
642                 hdrlen = encode_header(OBJ_DELTA, deltalen, hdr);
643                 ywrite(pack_fd, hdr, hdrlen);
644                 ywrite(pack_fd, last->sha1, sizeof(sha1));
645                 pack_size += hdrlen + sizeof(sha1);
646         } else {
647                 if (last)
648                         last->depth = 0;
649                 s.next_in = dat;
650                 s.avail_in = datlen;
651                 hdrlen = encode_header(type, datlen, hdr);
652                 ywrite(pack_fd, hdr, hdrlen);
653                 pack_size += hdrlen;
654         }
656         s.avail_out = deflateBound(&s, s.avail_in);
657         s.next_out = out = xmalloc(s.avail_out);
658         while (deflate(&s, Z_FINISH) == Z_OK)
659                 /* nothing */;
660         deflateEnd(&s);
662         ywrite(pack_fd, out, s.total_out);
663         pack_size += s.total_out;
665         free(out);
666         if (delta)
667                 free(delta);
668         if (last) {
669                 if (last->data)
670                         free(last->data);
671                 last->data = dat;
672                 last->len = datlen;
673                 memcpy(last->sha1, sha1, sizeof(sha1));
674         }
675         return 0;
678 static void* map_pack(unsigned long offset)
680         if (offset >= pack_size)
681                 die("object offset outside of pack file");
682         if (offset >= pack_mlen) {
683                 if (pack_base)
684                         munmap(pack_base, pack_mlen);
685                 /* round out how much we map to 16 MB units */
686                 pack_mlen = pack_size;
687                 if (pack_mlen & ((1 << 24) - 1))
688                         pack_mlen = ((pack_mlen >> 24) + 1) << 24;
689                 pack_base = mmap(NULL,pack_mlen,PROT_READ,MAP_SHARED,pack_fd,0);
690                 if (pack_base == MAP_FAILED)
691                         die("Failed to map generated pack: %s", strerror(errno));
692                 remap_count++;
693         }
694         return (char*)pack_base + offset;
697 static unsigned long unpack_object_header(unsigned long offset,
698         enum object_type *type,
699         unsigned long *sizep)
701         unsigned shift;
702         unsigned char c;
703         unsigned long size;
705         c = *(unsigned char*)map_pack(offset++);
706         *type = (c >> 4) & 7;
707         size = c & 15;
708         shift = 4;
709         while (c & 0x80) {
710                 c = *(unsigned char*)map_pack(offset++);
711                 size += (c & 0x7f) << shift;
712                 shift += 7;
713         }
714         *sizep = size;
715         return offset;
718 static void *unpack_non_delta_entry(unsigned long o, unsigned long sz)
720         z_stream stream;
721         unsigned char *result;
723         result = xmalloc(sz + 1);
724         result[sz] = 0;
726         memset(&stream, 0, sizeof(stream));
727         stream.next_in = map_pack(o);
728         stream.avail_in = pack_mlen - o;
729         stream.next_out = result;
730         stream.avail_out = sz;
732         inflateInit(&stream);
733         for (;;) {
734                 int st = inflate(&stream, Z_FINISH);
735                 if (st == Z_STREAM_END)
736                         break;
737                 if (st == Z_OK) {
738                         o = stream.next_in - (unsigned char*)pack_base;
739                         stream.next_in = map_pack(o);
740                         stream.avail_in = pack_mlen - o;
741                         continue;
742                 }
743                 die("Error from zlib during inflate.");
744         }
745         inflateEnd(&stream);
746         if (stream.total_out != sz)
747                 die("Error after inflate: sizes mismatch");
748         return result;
751 static void *unpack_entry(unsigned long offset, unsigned long *sizep);
753 static void *unpack_delta_entry(unsigned long offset,
754         unsigned long delta_size,
755         unsigned long *sizep)
757         struct object_entry *base_oe;
758         unsigned char *base_sha1;
759         void *delta_data, *base, *result;
760         unsigned long base_size, result_size;
762         base_sha1 = (unsigned char*)map_pack(offset + 20) - 20;
763         base_oe = find_object(base_sha1);
764         if (!base_oe)
765                 die("I'm broken; I can't find a base I know must be here.");
766         base = unpack_entry(base_oe->offset, &base_size);
767         delta_data = unpack_non_delta_entry(offset + 20, delta_size);
768         result = patch_delta(base, base_size,
769                              delta_data, delta_size,
770                              &result_size);
771         if (!result)
772                 die("failed to apply delta");
773         free(delta_data);
774         free(base);
775         *sizep = result_size;
776         return result;
779 static void *unpack_entry(unsigned long offset, unsigned long *sizep)
781         unsigned long size;
782         enum object_type kind;
784         offset = unpack_object_header(offset, &kind, &size);
785         switch (kind) {
786         case OBJ_DELTA:
787                 return unpack_delta_entry(offset, size, sizep);
788         case OBJ_COMMIT:
789         case OBJ_TREE:
790         case OBJ_BLOB:
791         case OBJ_TAG:
792                 *sizep = size;
793                 return unpack_non_delta_entry(offset, size);
794         default:
795                 die("I created an object I can't read!");
796         }
799 static const char *get_mode(const char *str, unsigned int *modep)
801         unsigned char c;
802         unsigned int mode = 0;
804         while ((c = *str++) != ' ') {
805                 if (c < '0' || c > '7')
806                         return NULL;
807                 mode = (mode << 3) + (c - '0');
808         }
809         *modep = mode;
810         return str;
813 static void load_tree(struct tree_entry *root)
815         struct object_entry *myoe;
816         struct tree_content *t;
817         unsigned long size;
818         char *buf;
819         const char *c;
821         root->tree = t = new_tree_content(8);
822         if (!memcmp(root->sha1, null_sha1, 20))
823                 return;
825         myoe = find_object(root->sha1);
826         if (myoe) {
827                 if (myoe->type != OBJ_TREE)
828                         die("Not a tree: %s", sha1_to_hex(root->sha1));
829                 buf = unpack_entry(myoe->offset, &size);
830         } else {
831                 char type[20];
832                 buf = read_sha1_file(root->sha1, type, &size);
833                 if (!buf || strcmp(type, tree_type))
834                         die("Can't load tree %s", sha1_to_hex(root->sha1));
835         }
837         c = buf;
838         while (c != (buf + size)) {
839                 struct tree_entry *e = new_tree_entry();
841                 if (t->entry_count == t->entry_capacity)
842                         root->tree = t = grow_tree_content(t, 8);
843                 t->entries[t->entry_count++] = e;
845                 e->tree = NULL;
846                 c = get_mode(c, &e->mode);
847                 if (!c)
848                         die("Corrupt mode in %s", sha1_to_hex(root->sha1));
849                 e->name = to_atom(c, strlen(c));
850                 c += e->name->str_len + 1;
851                 memcpy(e->sha1, c, sizeof(e->sha1));
852                 c += 20;
853         }
854         free(buf);
857 static int tecmp (const void *_a, const void *_b)
859         struct tree_entry *a = *((struct tree_entry**)_a);
860         struct tree_entry *b = *((struct tree_entry**)_b);
861         return base_name_compare(
862                 a->name->str_dat, a->name->str_len, a->mode,
863                 b->name->str_dat, b->name->str_len, b->mode);
866 static void store_tree(struct tree_entry *root)
868         struct tree_content *t = root->tree;
869         unsigned int i;
870         size_t maxlen;
871         char *buf, *c;
873         if (memcmp(root->sha1, null_sha1, 20))
874                 return;
876         maxlen = 0;
877         for (i = 0; i < t->entry_count; i++) {
878                 maxlen += t->entries[i]->name->str_len + 34;
879                 if (t->entries[i]->tree)
880                         store_tree(t->entries[i]);
881         }
883         qsort(t->entries, t->entry_count, sizeof(t->entries[0]), tecmp);
884         buf = c = xmalloc(maxlen);
885         for (i = 0; i < t->entry_count; i++) {
886                 struct tree_entry *e = t->entries[i];
887                 c += sprintf(c, "%o", e->mode);
888                 *c++ = ' ';
889                 strcpy(c, e->name->str_dat);
890                 c += e->name->str_len + 1;
891                 memcpy(c, e->sha1, 20);
892                 c += 20;
893         }
894         store_object(OBJ_TREE, buf, c - buf, NULL, root->sha1, 0);
895         free(buf);
898 static int tree_content_set(
899         struct tree_entry *root,
900         const char *p,
901         const unsigned char *sha1,
902         const unsigned int mode)
904         struct tree_content *t = root->tree;
905         const char *slash1;
906         unsigned int i, n;
907         struct tree_entry *e;
909         slash1 = strchr(p, '/');
910         if (slash1)
911                 n = slash1 - p;
912         else
913                 n = strlen(p);
915         for (i = 0; i < t->entry_count; i++) {
916                 e = t->entries[i];
917                 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
918                         if (!slash1) {
919                                 if (e->mode == mode && !memcmp(e->sha1, sha1, 20))
920                                         return 0;
921                                 e->mode = mode;
922                                 memcpy(e->sha1, sha1, 20);
923                                 if (e->tree) {
924                                         release_tree_content_recursive(e->tree);
925                                         e->tree = NULL;
926                                 }
927                                 memcpy(root->sha1, null_sha1, 20);
928                                 return 1;
929                         }
930                         if (!S_ISDIR(e->mode)) {
931                                 e->tree = new_tree_content(8);
932                                 e->mode = S_IFDIR;
933                         }
934                         if (!e->tree)
935                                 load_tree(e);
936                         if (tree_content_set(e, slash1 + 1, sha1, mode)) {
937                                 memcpy(root->sha1, null_sha1, 20);
938                                 return 1;
939                         }
940                         return 0;
941                 }
942         }
944         if (t->entry_count == t->entry_capacity)
945                 root->tree = t = grow_tree_content(t, 8);
946         e = new_tree_entry();
947         e->name = to_atom(p, n);
948         t->entries[t->entry_count++] = e;
949         if (slash1) {
950                 e->tree = new_tree_content(8);
951                 e->mode = S_IFDIR;
952                 tree_content_set(e, slash1 + 1, sha1, mode);
953         } else {
954                 e->tree = NULL;
955                 e->mode = mode;
956                 memcpy(e->sha1, sha1, 20);
957         }
958         memcpy(root->sha1, null_sha1, 20);
959         return 1;
962 static int tree_content_remove(struct tree_entry *root, const char *p)
964         struct tree_content *t = root->tree;
965         const char *slash1;
966         unsigned int i, n;
967         struct tree_entry *e;
969         slash1 = strchr(p, '/');
970         if (slash1)
971                 n = slash1 - p;
972         else
973                 n = strlen(p);
975         for (i = 0; i < t->entry_count; i++) {
976                 e = t->entries[i];
977                 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
978                         if (!slash1 || !S_ISDIR(e->mode))
979                                 goto del_entry;
980                         if (!e->tree)
981                                 load_tree(e);
982                         if (tree_content_remove(e, slash1 + 1)) {
983                                 if (!e->tree->entry_count)
984                                         goto del_entry;
985                                 memcpy(root->sha1, null_sha1, 20);
986                                 return 1;
987                         }
988                         return 0;
989                 }
990         }
991         return 0;
993 del_entry:
994         for (i++; i < t->entry_count; i++)
995                 t->entries[i-1] = t->entries[i];
996         t->entry_count--;
997         release_tree_entry(e);
998         memcpy(root->sha1, null_sha1, 20);
999         return 1;
1002 static void init_pack_header()
1004         struct pack_header hdr;
1006         hdr.hdr_signature = htonl(PACK_SIGNATURE);
1007         hdr.hdr_version = htonl(2);
1008         hdr.hdr_entries = 0;
1010         ywrite(pack_fd, &hdr, sizeof(hdr));
1011         pack_size = sizeof(hdr);
1014 static void fixup_header_footer()
1016         SHA_CTX c;
1017         char hdr[8];
1018         unsigned long cnt;
1019         char *buf;
1020         size_t n;
1022         if (lseek(pack_fd, 0, SEEK_SET) != 0)
1023                 die("Failed seeking to start: %s", strerror(errno));
1025         SHA1_Init(&c);
1026         yread(pack_fd, hdr, 8);
1027         SHA1_Update(&c, hdr, 8);
1029         cnt = htonl(object_count);
1030         SHA1_Update(&c, &cnt, 4);
1031         ywrite(pack_fd, &cnt, 4);
1033         buf = xmalloc(128 * 1024);
1034         for (;;) {
1035                 n = xread(pack_fd, buf, 128 * 1024);
1036                 if (n <= 0)
1037                         break;
1038                 SHA1_Update(&c, buf, n);
1039         }
1040         free(buf);
1042         SHA1_Final(pack_sha1, &c);
1043         ywrite(pack_fd, pack_sha1, sizeof(pack_sha1));
1046 static int oecmp (const void *_a, const void *_b)
1048         struct object_entry *a = *((struct object_entry**)_a);
1049         struct object_entry *b = *((struct object_entry**)_b);
1050         return memcmp(a->sha1, b->sha1, sizeof(a->sha1));
1053 static void write_index(const char *idx_name)
1055         struct sha1file *f;
1056         struct object_entry **idx, **c, **last;
1057         struct object_entry *e;
1058         struct object_entry_pool *o;
1059         unsigned int array[256];
1060         int i;
1062         /* Build the sorted table of object IDs. */
1063         idx = xmalloc(object_count * sizeof(struct object_entry*));
1064         c = idx;
1065         for (o = blocks; o; o = o->next_pool)
1066                 for (e = o->entries; e != o->next_free; e++)
1067                         *c++ = e;
1068         last = idx + object_count;
1069         qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
1071         /* Generate the fan-out array. */
1072         c = idx;
1073         for (i = 0; i < 256; i++) {
1074                 struct object_entry **next = c;;
1075                 while (next < last) {
1076                         if ((*next)->sha1[0] != i)
1077                                 break;
1078                         next++;
1079                 }
1080                 array[i] = htonl(next - idx);
1081                 c = next;
1082         }
1084         f = sha1create("%s", idx_name);
1085         sha1write(f, array, 256 * sizeof(int));
1086         for (c = idx; c != last; c++) {
1087                 unsigned int offset = htonl((*c)->offset);
1088                 sha1write(f, &offset, 4);
1089                 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
1090         }
1091         sha1write(f, pack_sha1, sizeof(pack_sha1));
1092         sha1close(f, NULL, 1);
1093         free(idx);
1096 static void dump_branches()
1098         static const char *msg = "fast-import";
1099         unsigned int i;
1100         struct branch *b;
1101         struct ref_lock *lock;
1103         for (i = 0; i < branch_table_sz; i++) {
1104                 for (b = branch_table[i]; b; b = b->table_next_branch) {
1105                         lock = lock_any_ref_for_update(b->name, NULL, 0);
1106                         if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1107                                 die("Can't write %s", b->name);
1108                 }
1109         }
1112 static void dump_tags()
1114         static const char *msg = "fast-import";
1115         struct tag *t;
1116         struct ref_lock *lock;
1117         char path[PATH_MAX];
1119         for (t = first_tag; t; t = t->next_tag) {
1120                 sprintf(path, "refs/tags/%s", t->name);
1121                 lock = lock_any_ref_for_update(path, NULL, 0);
1122                 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1123                         die("Can't write %s", path);
1124         }
1127 static void dump_marks_helper(FILE *f,
1128         unsigned long base,
1129         struct mark_set *m)
1131         int k;
1132         if (m->shift) {
1133                 for (k = 0; k < 1024; k++) {
1134                         if (m->data.sets[k])
1135                                 dump_marks_helper(f, (base + k) << m->shift,
1136                                         m->data.sets[k]);
1137                 }
1138         } else {
1139                 for (k = 0; k < 1024; k++) {
1140                         if (m->data.marked[k])
1141                                 fprintf(f, ":%lu %s\n", base + k,
1142                                         sha1_to_hex(m->data.marked[k]->sha1));
1143                 }
1144         }
1147 static void dump_marks()
1149         if (mark_file)
1150         {
1151                 FILE *f = fopen(mark_file, "w");
1152                 dump_marks_helper(f, 0, marks);
1153                 fclose(f);
1154         }
1157 static void read_next_command()
1159         read_line(&command_buf, stdin, '\n');
1162 static void cmd_mark()
1164         if (!strncmp("mark :", command_buf.buf, 6)) {
1165                 next_mark = strtoul(command_buf.buf + 6, NULL, 10);
1166                 read_next_command();
1167         }
1168         else
1169                 next_mark = 0;
1172 static void* cmd_data (size_t *size)
1174         size_t n = 0;
1175         void *buffer;
1176         size_t length;
1178         if (strncmp("data ", command_buf.buf, 5))
1179                 die("Expected 'data n' command, found: %s", command_buf.buf);
1181         length = strtoul(command_buf.buf + 5, NULL, 10);
1182         buffer = xmalloc(length);
1184         while (n < length) {
1185                 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1186                 if (!s && feof(stdin))
1187                         die("EOF in data (%lu bytes remaining)", length - n);
1188                 n += s;
1189         }
1191         if (fgetc(stdin) != '\n')
1192                 die("An lf did not trail the binary data as expected.");
1194         *size = length;
1195         return buffer;
1198 static void cmd_new_blob()
1200         size_t l;
1201         void *d;
1203         read_next_command();
1204         cmd_mark();
1205         d = cmd_data(&l);
1207         if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1208                 free(d);
1211 static void unload_one_branch()
1213         while (cur_active_branches
1214                 && cur_active_branches >= max_active_branches) {
1215                 unsigned long min_commit = ULONG_MAX;
1216                 struct branch *e, *l = NULL, *p = NULL;
1218                 for (e = active_branches; e; e = e->active_next_branch) {
1219                         if (e->last_commit < min_commit) {
1220                                 p = l;
1221                                 min_commit = e->last_commit;
1222                         }
1223                         l = e;
1224                 }
1226                 if (p) {
1227                         e = p->active_next_branch;
1228                         p->active_next_branch = e->active_next_branch;
1229                 } else {
1230                         e = active_branches;
1231                         active_branches = e->active_next_branch;
1232                 }
1233                 e->active_next_branch = NULL;
1234                 if (e->branch_tree.tree) {
1235                         release_tree_content_recursive(e->branch_tree.tree);
1236                         e->branch_tree.tree = NULL;
1237                 }
1238                 cur_active_branches--;
1239         }
1242 static void load_branch(struct branch *b)
1244         load_tree(&b->branch_tree);
1245         b->active_next_branch = active_branches;
1246         active_branches = b;
1247         cur_active_branches++;
1248         branch_load_count++;
1251 static void file_change_m(struct branch *b)
1253         const char *p = command_buf.buf + 2;
1254         char *p_uq;
1255         const char *endp;
1256         struct object_entry *oe;
1257         unsigned char sha1[20];
1258         unsigned int mode;
1259         char type[20];
1261         p = get_mode(p, &mode);
1262         if (!p)
1263                 die("Corrupt mode: %s", command_buf.buf);
1264         switch (mode) {
1265         case S_IFREG | 0644:
1266         case S_IFREG | 0755:
1267         case S_IFLNK:
1268         case 0644:
1269         case 0755:
1270                 /* ok */
1271                 break;
1272         default:
1273                 die("Corrupt mode: %s", command_buf.buf);
1274         }
1276         if (*p == ':') {
1277                 char *x;
1278                 oe = find_mark(strtoul(p + 1, &x, 10));
1279                 p = x;
1280         } else {
1281                 if (get_sha1_hex(p, sha1))
1282                         die("Invalid SHA1: %s", command_buf.buf);
1283                 oe = find_object(sha1);
1284                 p += 40;
1285         }
1286         if (*p++ != ' ')
1287                 die("Missing space after SHA1: %s", command_buf.buf);
1289         p_uq = unquote_c_style(p, &endp);
1290         if (p_uq) {
1291                 if (*endp)
1292                         die("Garbage after path in: %s", command_buf.buf);
1293                 p = p_uq;
1294         }
1296         if (oe) {
1297                 if (oe->type != OBJ_BLOB)
1298                         die("Not a blob (actually a %s): %s",
1299                                 command_buf.buf, type_names[oe->type]);
1300         } else {
1301                 if (sha1_object_info(sha1, type, NULL))
1302                         die("Blob not found: %s", command_buf.buf);
1303                 if (strcmp(blob_type, type))
1304                         die("Not a blob (actually a %s): %s",
1305                                 command_buf.buf, type);
1306         }
1308         tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1310         if (p_uq)
1311                 free(p_uq);
1314 static void file_change_d(struct branch *b)
1316         const char *p = command_buf.buf + 2;
1317         char *p_uq;
1318         const char *endp;
1320         p_uq = unquote_c_style(p, &endp);
1321         if (p_uq) {
1322                 if (*endp)
1323                         die("Garbage after path in: %s", command_buf.buf);
1324                 p = p_uq;
1325         }
1326         tree_content_remove(&b->branch_tree, p);
1327         if (p_uq)
1328                 free(p_uq);
1331 static void cmd_from(struct branch *b)
1333         const char *from, *endp;
1334         char *str_uq;
1335         struct branch *s;
1337         if (strncmp("from ", command_buf.buf, 5))
1338                 return;
1340         if (b->last_commit)
1341                 die("Can't reinitailize branch %s", b->name);
1343         from = strchr(command_buf.buf, ' ') + 1;
1344         str_uq = unquote_c_style(from, &endp);
1345         if (str_uq) {
1346                 if (*endp)
1347                         die("Garbage after string in: %s", command_buf.buf);
1348                 from = str_uq;
1349         }
1351         s = lookup_branch(from);
1352         if (b == s)
1353                 die("Can't create a branch from itself: %s", b->name);
1354         else if (s) {
1355                 memcpy(b->sha1, s->sha1, 20);
1356                 memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
1357         } else if (*from == ':') {
1358                 unsigned long idnum = strtoul(from + 1, NULL, 10);
1359                 struct object_entry *oe = find_mark(idnum);
1360                 unsigned long size;
1361                 char *buf;
1362                 if (oe->type != OBJ_COMMIT)
1363                         die("Mark :%lu not a commit", idnum);
1364                 memcpy(b->sha1, oe->sha1, 20);
1365                 buf = unpack_entry(oe->offset, &size);
1366                 if (!buf || size < 46)
1367                         die("Not a valid commit: %s", from);
1368                 if (memcmp("tree ", buf, 5)
1369                         || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1370                         die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1371                 free(buf);
1372         } else if (!get_sha1(from, b->sha1)) {
1373                 if (!memcmp(b->sha1, null_sha1, 20))
1374                         memcpy(b->branch_tree.sha1, null_sha1, 20);
1375                 else {
1376                         unsigned long size;
1377                         char *buf;
1379                         buf = read_object_with_reference(b->sha1,
1380                                 type_names[OBJ_COMMIT], &size, b->sha1);
1381                         if (!buf || size < 46)
1382                                 die("Not a valid commit: %s", from);
1383                         if (memcmp("tree ", buf, 5)
1384                                 || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1385                                 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1386                         free(buf);
1387                 }
1388         } else
1389                 die("Invalid ref name or SHA1 expression: %s", from);
1391         read_next_command();
1394 static void cmd_new_commit()
1396         struct branch *b;
1397         void *msg;
1398         size_t msglen;
1399         char *str_uq;
1400         const char *endp;
1401         char *sp;
1402         char *author = NULL;
1403         char *committer = NULL;
1404         char *body;
1406         /* Obtain the branch name from the rest of our command */
1407         sp = strchr(command_buf.buf, ' ') + 1;
1408         str_uq = unquote_c_style(sp, &endp);
1409         if (str_uq) {
1410                 if (*endp)
1411                         die("Garbage after ref in: %s", command_buf.buf);
1412                 sp = str_uq;
1413         }
1414         b = lookup_branch(sp);
1415         if (!b)
1416                 b = new_branch(sp);
1417         if (str_uq)
1418                 free(str_uq);
1420         read_next_command();
1421         cmd_mark();
1422         if (!strncmp("author ", command_buf.buf, 7)) {
1423                 author = strdup(command_buf.buf);
1424                 read_next_command();
1425         }
1426         if (!strncmp("committer ", command_buf.buf, 10)) {
1427                 committer = strdup(command_buf.buf);
1428                 read_next_command();
1429         }
1430         if (!committer)
1431                 die("Expected committer but didn't get one");
1432         msg = cmd_data(&msglen);
1433         read_next_command();
1434         cmd_from(b);
1436         /* ensure the branch is active/loaded */
1437         if (!b->branch_tree.tree || !max_active_branches) {
1438                 unload_one_branch();
1439                 load_branch(b);
1440         }
1442         /* file_change* */
1443         for (;;) {
1444                 if (1 == command_buf.len)
1445                         break;
1446                 else if (!strncmp("M ", command_buf.buf, 2))
1447                         file_change_m(b);
1448                 else if (!strncmp("D ", command_buf.buf, 2))
1449                         file_change_d(b);
1450                 else
1451                         die("Unsupported file_change: %s", command_buf.buf);
1452                 read_next_command();
1453         }
1455         /* build the tree and the commit */
1456         store_tree(&b->branch_tree);
1457         body = xmalloc(97 + msglen
1458                 + (author
1459                         ? strlen(author) + strlen(committer)
1460                         : 2 * strlen(committer)));
1461         sp = body;
1462         sp += sprintf(sp, "tree %s\n", sha1_to_hex(b->branch_tree.sha1));
1463         if (memcmp(b->sha1, null_sha1, 20))
1464                 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1465         if (author)
1466                 sp += sprintf(sp, "%s\n", author);
1467         else
1468                 sp += sprintf(sp, "author %s\n", committer + 10);
1469         sp += sprintf(sp, "%s\n\n", committer);
1470         memcpy(sp, msg, msglen);
1471         sp += msglen;
1472         if (author)
1473                 free(author);
1474         free(committer);
1475         free(msg);
1477         store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1, next_mark);
1478         free(body);
1479         b->last_commit = object_count_by_type[OBJ_COMMIT];
1481         if (branch_log) {
1482                 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1483                 fprintf(branch_log, "commit ");
1484                 if (need_dq) {
1485                         fputc('"', branch_log);
1486                         quote_c_style(b->name, NULL, branch_log, 0);
1487                         fputc('"', branch_log);
1488                 } else
1489                         fprintf(branch_log, "%s", b->name);
1490                 fprintf(branch_log," :%lu %s\n",next_mark,sha1_to_hex(b->sha1));
1491         }
1494 static void cmd_new_tag()
1496         char *str_uq;
1497         const char *endp;
1498         char *sp;
1499         const char *from;
1500         char *tagger;
1501         struct branch *s;
1502         void *msg;
1503         size_t msglen;
1504         char *body;
1505         struct tag *t;
1506         unsigned long from_mark = 0;
1507         unsigned char sha1[20];
1509         /* Obtain the new tag name from the rest of our command */
1510         sp = strchr(command_buf.buf, ' ') + 1;
1511         str_uq = unquote_c_style(sp, &endp);
1512         if (str_uq) {
1513                 if (*endp)
1514                         die("Garbage after tag name in: %s", command_buf.buf);
1515                 sp = str_uq;
1516         }
1517         t = pool_alloc(sizeof(struct tag));
1518         t->next_tag = NULL;
1519         t->name = pool_strdup(sp);
1520         if (last_tag)
1521                 last_tag->next_tag = t;
1522         else
1523                 first_tag = t;
1524         last_tag = t;
1525         if (str_uq)
1526                 free(str_uq);
1527         read_next_command();
1529         /* from ... */
1530         if (strncmp("from ", command_buf.buf, 5))
1531                 die("Expected from command, got %s", command_buf.buf);
1533         from = strchr(command_buf.buf, ' ') + 1;
1534         str_uq = unquote_c_style(from, &endp);
1535         if (str_uq) {
1536                 if (*endp)
1537                         die("Garbage after string in: %s", command_buf.buf);
1538                 from = str_uq;
1539         }
1541         s = lookup_branch(from);
1542         if (s) {
1543                 memcpy(sha1, s->sha1, 20);
1544         } else if (*from == ':') {
1545                 from_mark = strtoul(from + 1, NULL, 10);
1546                 struct object_entry *oe = find_mark(from_mark);
1547                 if (oe->type != OBJ_COMMIT)
1548                         die("Mark :%lu not a commit", from_mark);
1549                 memcpy(sha1, oe->sha1, 20);
1550         } else if (!get_sha1(from, sha1)) {
1551                 unsigned long size;
1552                 char *buf;
1554                 buf = read_object_with_reference(sha1,
1555                         type_names[OBJ_COMMIT], &size, sha1);
1556                 if (!buf || size < 46)
1557                         die("Not a valid commit: %s", from);
1558                 free(buf);
1559         } else
1560                 die("Invalid ref name or SHA1 expression: %s", from);
1562         if (str_uq)
1563                 free(str_uq);
1564         read_next_command();
1566         /* tagger ... */
1567         if (strncmp("tagger ", command_buf.buf, 7))
1568                 die("Expected tagger command, got %s", command_buf.buf);
1569         tagger = strdup(command_buf.buf);
1571         /* tag payload/message */
1572         read_next_command();
1573         msg = cmd_data(&msglen);
1575         /* build the tag object */
1576         body = xmalloc(67 + strlen(t->name) + strlen(tagger) + msglen);
1577         sp = body;
1578         sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1579         sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1580         sp += sprintf(sp, "tag %s\n", t->name);
1581         sp += sprintf(sp, "%s\n\n", tagger);
1582         memcpy(sp, msg, msglen);
1583         sp += msglen;
1584         free(tagger);
1585         free(msg);
1587         store_object(OBJ_TAG, body, sp - body, NULL, t->sha1, 0);
1588         free(body);
1590         if (branch_log) {
1591                 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1592                 fprintf(branch_log, "tag ");
1593                 if (need_dq) {
1594                         fputc('"', branch_log);
1595                         quote_c_style(t->name, NULL, branch_log, 0);
1596                         fputc('"', branch_log);
1597                 } else
1598                         fprintf(branch_log, "%s", t->name);
1599                 fprintf(branch_log," :%lu %s\n",from_mark,sha1_to_hex(t->sha1));
1600         }
1603 static const char fast_import_usage[] =
1604 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1606 int main(int argc, const char **argv)
1608         const char *base_name;
1609         int i;
1610         unsigned long est_obj_cnt = 1000;
1611         char *pack_name;
1612         char *idx_name;
1613         struct stat sb;
1615         setup_ident();
1616         git_config(git_default_config);
1618         for (i = 1; i < argc; i++) {
1619                 const char *a = argv[i];
1621                 if (*a != '-' || !strcmp(a, "--"))
1622                         break;
1623                 else if (!strncmp(a, "--objects=", 10))
1624                         est_obj_cnt = strtoul(a + 10, NULL, 0);
1625                 else if (!strncmp(a, "--depth=", 8))
1626                         max_depth = strtoul(a + 8, NULL, 0);
1627                 else if (!strncmp(a, "--active-branches=", 18))
1628                         max_active_branches = strtoul(a + 18, NULL, 0);
1629                 else if (!strncmp(a, "--export-marks=", 15))
1630                         mark_file = a + 15;
1631                 else if (!strncmp(a, "--branch-log=", 13)) {
1632                         branch_log = fopen(a + 13, "w");
1633                         if (!branch_log)
1634                                 die("Can't create %s: %s", a + 13, strerror(errno));
1635                 }
1636                 else
1637                         die("unknown option %s", a);
1638         }
1639         if ((i+1) != argc)
1640                 usage(fast_import_usage);
1641         base_name = argv[i];
1643         pack_name = xmalloc(strlen(base_name) + 6);
1644         sprintf(pack_name, "%s.pack", base_name);
1645         idx_name = xmalloc(strlen(base_name) + 5);
1646         sprintf(idx_name, "%s.idx", base_name);
1648         pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
1649         if (pack_fd < 0)
1650                 die("Can't create %s: %s", pack_name, strerror(errno));
1652         init_pack_header();
1653         alloc_objects(est_obj_cnt);
1654         strbuf_init(&command_buf);
1656         atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1657         branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1658         avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1659         marks = pool_calloc(1, sizeof(struct mark_set));
1661         for (;;) {
1662                 read_next_command();
1663                 if (command_buf.eof)
1664                         break;
1665                 else if (!strcmp("blob", command_buf.buf))
1666                         cmd_new_blob();
1667                 else if (!strncmp("commit ", command_buf.buf, 7))
1668                         cmd_new_commit();
1669                 else if (!strncmp("tag ", command_buf.buf, 4))
1670                         cmd_new_tag();
1671                 else
1672                         die("Unsupported command: %s", command_buf.buf);
1673         }
1675         fixup_header_footer();
1676         close(pack_fd);
1677         write_index(idx_name);
1678         dump_branches();
1679         dump_tags();
1680         dump_marks();
1681         fclose(branch_log);
1683         fprintf(stderr, "%s statistics:\n", argv[0]);
1684         fprintf(stderr, "---------------------------------------------------\n");
1685         fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow  )\n", alloc_count, alloc_count - est_obj_cnt);
1686         fprintf(stderr, "Total objects:   %10lu (%10lu duplicates)\n", object_count, duplicate_count);
1687         fprintf(stderr, "      blobs  :   %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB]);
1688         fprintf(stderr, "      trees  :   %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]);
1689         fprintf(stderr, "      commits:   %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]);
1690         fprintf(stderr, "      tags   :   %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
1691         fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
1692         fprintf(stderr, "      marks:     %10u (%10lu unique    )\n", (1 << marks->shift) * 1024, marks_set_count);
1693         fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
1694         fprintf(stderr, "Memory total:    %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1695         fprintf(stderr, "       pools:    %10lu KiB\n", total_allocd/1024);
1696         fprintf(stderr, "     objects:    %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1697         fprintf(stderr, "Pack remaps:     %10lu\n", remap_count);
1698         fprintf(stderr, "---------------------------------------------------\n");
1700         stat(pack_name, &sb);
1701         fprintf(stderr, "Pack size:       %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1702         stat(idx_name, &sb);
1703         fprintf(stderr, "Index size:      %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1705         fprintf(stderr, "\n");
1707         return 0;