da045662ac16883ed28a3c783b4d7c6d10a130cc
[git.git] / fast-import.c
1 /*
2 Format of STDIN stream:
3
4   stream ::= cmd*;
5
6   cmd ::= new_blob
7         | new_commit
8         | new_tag
9         | reset_branch
10         | checkpoint
11         | progress
12         ;
13
14   new_blob ::= 'blob' lf
15     mark?
16     file_content;
17   file_content ::= data;
18
19   new_commit ::= 'commit' sp ref_str lf
20     mark?
21     ('author' sp name '<' email '>' when lf)?
22     'committer' sp name '<' email '>' when lf
23     commit_msg
24     ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
25     ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
26     file_change*
27     lf?;
28   commit_msg ::= data;
29
30   file_change ::= file_clr
31     | file_del
32     | file_rnm
33     | file_cpy
34     | file_obm
35     | file_inm;
36   file_clr ::= 'deleteall' lf;
37   file_del ::= 'D' sp path_str lf;
38   file_rnm ::= 'R' sp path_str sp path_str lf;
39   file_cpy ::= 'C' sp path_str sp path_str lf;
40   file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
41   file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
42     data;
43
44   new_tag ::= 'tag' sp tag_str lf
45     'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
46     'tagger' sp name '<' email '>' when lf
47     tag_msg;
48   tag_msg ::= data;
49
50   reset_branch ::= 'reset' sp ref_str lf
51     ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
52     lf?;
53
54   checkpoint ::= 'checkpoint' lf
55     lf?;
56
57   progress ::= 'progress' sp not_lf* lf
58     lf?;
59
60      # note: the first idnum in a stream should be 1 and subsequent
61      # idnums should not have gaps between values as this will cause
62      # the stream parser to reserve space for the gapped values.  An
63      # idnum can be updated in the future to a new object by issuing
64      # a new mark directive with the old idnum.
65      #
66   mark ::= 'mark' sp idnum lf;
67   data ::= (delimited_data | exact_data)
68     lf?;
69
70     # note: delim may be any string but must not contain lf.
71     # data_line may contain any data but must not be exactly
72     # delim.
73   delimited_data ::= 'data' sp '<<' delim lf
74     (data_line lf)*
75     delim lf;
76
77      # note: declen indicates the length of binary_data in bytes.
78      # declen does not include the lf preceeding the binary data.
79      #
80   exact_data ::= 'data' sp declen lf
81     binary_data;
82
83      # note: quoted strings are C-style quoting supporting \c for
84      # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
85      # is the signed byte value in octal.  Note that the only
86      # characters which must actually be escaped to protect the
87      # stream formatting is: \, " and LF.  Otherwise these values
88      # are UTF8.
89      #
90   ref_str     ::= ref;
91   sha1exp_str ::= sha1exp;
92   tag_str     ::= tag;
93   path_str    ::= path    | '"' quoted(path)    '"' ;
94   mode        ::= '100644' | '644'
95                 | '100755' | '755'
96                 | '120000'
97                 ;
98
99   declen ::= # unsigned 32 bit value, ascii base10 notation;
100   bigint ::= # unsigned integer value, ascii base10 notation;
101   binary_data ::= # file content, not interpreted;
102
103   when         ::= raw_when | rfc2822_when;
104   raw_when     ::= ts sp tz;
105   rfc2822_when ::= # Valid RFC 2822 date and time;
106
107   sp ::= # ASCII space character;
108   lf ::= # ASCII newline (LF) character;
109
110      # note: a colon (':') must precede the numerical value assigned to
111      # an idnum.  This is to distinguish it from a ref or tag name as
112      # GIT does not permit ':' in ref or tag strings.
113      #
114   idnum   ::= ':' bigint;
115   path    ::= # GIT style file path, e.g. "a/b/c";
116   ref     ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
117   tag     ::= # GIT tag name, e.g. "FIREFOX_1_5";
118   sha1exp ::= # Any valid GIT SHA1 expression;
119   hexsha1 ::= # SHA1 in hexadecimal format;
120
121      # note: name and email are UTF8 strings, however name must not
122      # contain '<' or lf and email must not contain any of the
123      # following: '<', '>', lf.
124      #
125   name  ::= # valid GIT author/committer name;
126   email ::= # valid GIT author/committer email;
127   ts    ::= # time since the epoch in seconds, ascii base10 notation;
128   tz    ::= # GIT style timezone;
129
130      # note: comments may appear anywhere in the input, except
131      # within a data command.  Any form of the data command
132      # always escapes the related input from comment processing.
133      #
134      # In case it is not clear, the '#' that starts the comment
135      # must be the first character on that the line (an lf have
136      # preceeded it).
137      #
138   comment ::= '#' not_lf* lf;
139   not_lf  ::= # Any byte that is not ASCII newline (LF);
140 */
141
142 #include "builtin.h"
143 #include "cache.h"
144 #include "object.h"
145 #include "blob.h"
146 #include "tree.h"
147 #include "commit.h"
148 #include "delta.h"
149 #include "pack.h"
150 #include "refs.h"
151 #include "csum-file.h"
152 #include "quote.h"
153
154 #define PACK_ID_BITS 16
155 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
156
157 struct object_entry
158 {
159         struct object_entry *next;
160         uint32_t offset;
161         unsigned type : TYPE_BITS;
162         unsigned pack_id : PACK_ID_BITS;
163         unsigned char sha1[20];
164 };
165
166 struct object_entry_pool
167 {
168         struct object_entry_pool *next_pool;
169         struct object_entry *next_free;
170         struct object_entry *end;
171         struct object_entry entries[FLEX_ARRAY]; /* more */
172 };
173
174 struct mark_set
175 {
176         union {
177                 struct object_entry *marked[1024];
178                 struct mark_set *sets[1024];
179         } data;
180         unsigned int shift;
181 };
182
183 struct last_object
184 {
185         void *data;
186         unsigned long len;
187         uint32_t offset;
188         unsigned int depth;
189         unsigned no_free:1;
190 };
191
192 struct mem_pool
193 {
194         struct mem_pool *next_pool;
195         char *next_free;
196         char *end;
197         char space[FLEX_ARRAY]; /* more */
198 };
199
200 struct atom_str
201 {
202         struct atom_str *next_atom;
203         unsigned short str_len;
204         char str_dat[FLEX_ARRAY]; /* more */
205 };
206
207 struct tree_content;
208 struct tree_entry
209 {
210         struct tree_content *tree;
211         struct atom_str* name;
212         struct tree_entry_ms
213         {
214                 uint16_t mode;
215                 unsigned char sha1[20];
216         } versions[2];
217 };
218
219 struct tree_content
220 {
221         unsigned int entry_capacity; /* must match avail_tree_content */
222         unsigned int entry_count;
223         unsigned int delta_depth;
224         struct tree_entry *entries[FLEX_ARRAY]; /* more */
225 };
226
227 struct avail_tree_content
228 {
229         unsigned int entry_capacity; /* must match tree_content */
230         struct avail_tree_content *next_avail;
231 };
232
233 struct branch
234 {
235         struct branch *table_next_branch;
236         struct branch *active_next_branch;
237         const char *name;
238         struct tree_entry branch_tree;
239         uintmax_t last_commit;
240         unsigned active : 1;
241         unsigned pack_id : PACK_ID_BITS;
242         unsigned char sha1[20];
243 };
244
245 struct tag
246 {
247         struct tag *next_tag;
248         const char *name;
249         unsigned int pack_id;
250         unsigned char sha1[20];
251 };
252
253 struct dbuf
254 {
255         void *buffer;
256         size_t capacity;
257 };
258
259 struct hash_list
260 {
261         struct hash_list *next;
262         unsigned char sha1[20];
263 };
264
265 typedef enum {
266         WHENSPEC_RAW = 1,
267         WHENSPEC_RFC2822,
268         WHENSPEC_NOW,
269 } whenspec_type;
270
271 struct recent_command
272 {
273         struct recent_command *prev;
274         struct recent_command *next;
275         char *buf;
276 };
277
278 /* Configured limits on output */
279 static unsigned long max_depth = 10;
280 static off_t max_packsize = (1LL << 32) - 1;
281 static int force_update;
282
283 /* Stats and misc. counters */
284 static uintmax_t alloc_count;
285 static uintmax_t marks_set_count;
286 static uintmax_t object_count_by_type[1 << TYPE_BITS];
287 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
288 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
289 static unsigned long object_count;
290 static unsigned long branch_count;
291 static unsigned long branch_load_count;
292 static int failure;
293 static FILE *pack_edges;
294
295 /* Memory pools */
296 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
297 static size_t total_allocd;
298 static struct mem_pool *mem_pool;
299
300 /* Atom management */
301 static unsigned int atom_table_sz = 4451;
302 static unsigned int atom_cnt;
303 static struct atom_str **atom_table;
304
305 /* The .pack file being generated */
306 static unsigned int pack_id;
307 static struct packed_git *pack_data;
308 static struct packed_git **all_packs;
309 static unsigned long pack_size;
310
311 /* Table of objects we've written. */
312 static unsigned int object_entry_alloc = 5000;
313 static struct object_entry_pool *blocks;
314 static struct object_entry *object_table[1 << 16];
315 static struct mark_set *marks;
316 static const char* mark_file;
317
318 /* Our last blob */
319 static struct last_object last_blob;
320
321 /* Tree management */
322 static unsigned int tree_entry_alloc = 1000;
323 static void *avail_tree_entry;
324 static unsigned int avail_tree_table_sz = 100;
325 static struct avail_tree_content **avail_tree_table;
326 static struct dbuf old_tree;
327 static struct dbuf new_tree;
328
329 /* Branch data */
330 static unsigned long max_active_branches = 5;
331 static unsigned long cur_active_branches;
332 static unsigned long branch_table_sz = 1039;
333 static struct branch **branch_table;
334 static struct branch *active_branches;
335
336 /* Tag data */
337 static struct tag *first_tag;
338 static struct tag *last_tag;
339
340 /* Input stream parsing */
341 static whenspec_type whenspec = WHENSPEC_RAW;
342 static struct strbuf command_buf = STRBUF_INIT;
343 static int unread_command_buf;
344 static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
345 static struct recent_command *cmd_tail = &cmd_hist;
346 static struct recent_command *rc_free;
347 static unsigned int cmd_save = 100;
348 static uintmax_t next_mark;
349 static struct dbuf new_data;
350
351 static void write_branch_report(FILE *rpt, struct branch *b)
352 {
353         fprintf(rpt, "%s:\n", b->name);
354
355         fprintf(rpt, "  status      :");
356         if (b->active)
357                 fputs(" active", rpt);
358         if (b->branch_tree.tree)
359                 fputs(" loaded", rpt);
360         if (is_null_sha1(b->branch_tree.versions[1].sha1))
361                 fputs(" dirty", rpt);
362         fputc('\n', rpt);
363
364         fprintf(rpt, "  tip commit  : %s\n", sha1_to_hex(b->sha1));
365         fprintf(rpt, "  old tree    : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1));
366         fprintf(rpt, "  cur tree    : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1));
367         fprintf(rpt, "  commit clock: %" PRIuMAX "\n", b->last_commit);
368
369         fputs("  last pack   : ", rpt);
370         if (b->pack_id < MAX_PACK_ID)
371                 fprintf(rpt, "%u", b->pack_id);
372         fputc('\n', rpt);
373
374         fputc('\n', rpt);
375 }
376
377 static void write_crash_report(const char *err)
378 {
379         char *loc = git_path("fast_import_crash_%d", getpid());
380         FILE *rpt = fopen(loc, "w");
381         struct branch *b;
382         unsigned long lu;
383         struct recent_command *rc;
384
385         if (!rpt) {
386                 error("can't write crash report %s: %s", loc, strerror(errno));
387                 return;
388         }
389
390         fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
391
392         fprintf(rpt, "fast-import crash report:\n");
393         fprintf(rpt, "    fast-import process: %d\n", getpid());
394         fprintf(rpt, "    parent process     : %d\n", getppid());
395         fprintf(rpt, "    at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
396         fputc('\n', rpt);
397
398         fputs("fatal: ", rpt);
399         fputs(err, rpt);
400         fputc('\n', rpt);
401
402         fputc('\n', rpt);
403         fputs("Most Recent Commands Before Crash\n", rpt);
404         fputs("---------------------------------\n", rpt);
405         for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
406                 if (rc->next == &cmd_hist)
407                         fputs("* ", rpt);
408                 else
409                         fputs("  ", rpt);
410                 fputs(rc->buf, rpt);
411                 fputc('\n', rpt);
412         }
413
414         fputc('\n', rpt);
415         fputs("Active Branch LRU\n", rpt);
416         fputs("-----------------\n", rpt);
417         fprintf(rpt, "    active_branches = %lu cur, %lu max\n",
418                 cur_active_branches,
419                 max_active_branches);
420         fputc('\n', rpt);
421         fputs("  pos  clock name\n", rpt);
422         fputs("  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
423         for (b = active_branches, lu = 0; b; b = b->active_next_branch)
424                 fprintf(rpt, "  %2lu) %6" PRIuMAX" %s\n",
425                         ++lu, b->last_commit, b->name);
426
427         fputc('\n', rpt);
428         fputs("Inactive Branches\n", rpt);
429         fputs("-----------------\n", rpt);
430         for (lu = 0; lu < branch_table_sz; lu++) {
431                 for (b = branch_table[lu]; b; b = b->table_next_branch)
432                         write_branch_report(rpt, b);
433         }
434
435         fputc('\n', rpt);
436         fputs("-------------------\n", rpt);
437         fputs("END OF CRASH REPORT\n", rpt);
438         fclose(rpt);
439 }
440
441 static NORETURN void die_nicely(const char *err, va_list params)
442 {
443         static int zombie;
444         char message[2 * PATH_MAX];
445
446         vsnprintf(message, sizeof(message), err, params);
447         fputs("fatal: ", stderr);
448         fputs(message, stderr);
449         fputc('\n', stderr);
450
451         if (!zombie) {
452                 zombie = 1;
453                 write_crash_report(message);
454         }
455         exit(128);
456 }
457
458 static void alloc_objects(unsigned int cnt)
459 {
460         struct object_entry_pool *b;
461
462         b = xmalloc(sizeof(struct object_entry_pool)
463                 + cnt * sizeof(struct object_entry));
464         b->next_pool = blocks;
465         b->next_free = b->entries;
466         b->end = b->entries + cnt;
467         blocks = b;
468         alloc_count += cnt;
469 }
470
471 static struct object_entry *new_object(unsigned char *sha1)
472 {
473         struct object_entry *e;
474
475         if (blocks->next_free == blocks->end)
476                 alloc_objects(object_entry_alloc);
477
478         e = blocks->next_free++;
479         hashcpy(e->sha1, sha1);
480         return e;
481 }
482
483 static struct object_entry *find_object(unsigned char *sha1)
484 {
485         unsigned int h = sha1[0] << 8 | sha1[1];
486         struct object_entry *e;
487         for (e = object_table[h]; e; e = e->next)
488                 if (!hashcmp(sha1, e->sha1))
489                         return e;
490         return NULL;
491 }
492
493 static struct object_entry *insert_object(unsigned char *sha1)
494 {
495         unsigned int h = sha1[0] << 8 | sha1[1];
496         struct object_entry *e = object_table[h];
497         struct object_entry *p = NULL;
498
499         while (e) {
500                 if (!hashcmp(sha1, e->sha1))
501                         return e;
502                 p = e;
503                 e = e->next;
504         }
505
506         e = new_object(sha1);
507         e->next = NULL;
508         e->offset = 0;
509         if (p)
510                 p->next = e;
511         else
512                 object_table[h] = e;
513         return e;
514 }
515
516 static unsigned int hc_str(const char *s, size_t len)
517 {
518         unsigned int r = 0;
519         while (len-- > 0)
520                 r = r * 31 + *s++;
521         return r;
522 }
523
524 static void *pool_alloc(size_t len)
525 {
526         struct mem_pool *p;
527         void *r;
528
529         for (p = mem_pool; p; p = p->next_pool)
530                 if ((p->end - p->next_free >= len))
531                         break;
532
533         if (!p) {
534                 if (len >= (mem_pool_alloc/2)) {
535                         total_allocd += len;
536                         return xmalloc(len);
537                 }
538                 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
539                 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
540                 p->next_pool = mem_pool;
541                 p->next_free = p->space;
542                 p->end = p->next_free + mem_pool_alloc;
543                 mem_pool = p;
544         }
545
546         r = p->next_free;
547         /* round out to a pointer alignment */
548         if (len & (sizeof(void*) - 1))
549                 len += sizeof(void*) - (len & (sizeof(void*) - 1));
550         p->next_free += len;
551         return r;
552 }
553
554 static void *pool_calloc(size_t count, size_t size)
555 {
556         size_t len = count * size;
557         void *r = pool_alloc(len);
558         memset(r, 0, len);
559         return r;
560 }
561
562 static char *pool_strdup(const char *s)
563 {
564         char *r = pool_alloc(strlen(s) + 1);
565         strcpy(r, s);
566         return r;
567 }
568
569 static void size_dbuf(struct dbuf *b, size_t maxlen)
570 {
571         if (b->buffer) {
572                 if (b->capacity >= maxlen)
573                         return;
574                 free(b->buffer);
575         }
576         b->capacity = ((maxlen / 1024) + 1) * 1024;
577         b->buffer = xmalloc(b->capacity);
578 }
579
580 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
581 {
582         struct mark_set *s = marks;
583         while ((idnum >> s->shift) >= 1024) {
584                 s = pool_calloc(1, sizeof(struct mark_set));
585                 s->shift = marks->shift + 10;
586                 s->data.sets[0] = marks;
587                 marks = s;
588         }
589         while (s->shift) {
590                 uintmax_t i = idnum >> s->shift;
591                 idnum -= i << s->shift;
592                 if (!s->data.sets[i]) {
593                         s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
594                         s->data.sets[i]->shift = s->shift - 10;
595                 }
596                 s = s->data.sets[i];
597         }
598         if (!s->data.marked[idnum])
599                 marks_set_count++;
600         s->data.marked[idnum] = oe;
601 }
602
603 static struct object_entry *find_mark(uintmax_t idnum)
604 {
605         uintmax_t orig_idnum = idnum;
606         struct mark_set *s = marks;
607         struct object_entry *oe = NULL;
608         if ((idnum >> s->shift) < 1024) {
609                 while (s && s->shift) {
610                         uintmax_t i = idnum >> s->shift;
611                         idnum -= i << s->shift;
612                         s = s->data.sets[i];
613                 }
614                 if (s)
615                         oe = s->data.marked[idnum];
616         }
617         if (!oe)
618                 die("mark :%" PRIuMAX " not declared", orig_idnum);
619         return oe;
620 }
621
622 static struct atom_str *to_atom(const char *s, unsigned short len)
623 {
624         unsigned int hc = hc_str(s, len) % atom_table_sz;
625         struct atom_str *c;
626
627         for (c = atom_table[hc]; c; c = c->next_atom)
628                 if (c->str_len == len && !strncmp(s, c->str_dat, len))
629                         return c;
630
631         c = pool_alloc(sizeof(struct atom_str) + len + 1);
632         c->str_len = len;
633         strncpy(c->str_dat, s, len);
634         c->str_dat[len] = 0;
635         c->next_atom = atom_table[hc];
636         atom_table[hc] = c;
637         atom_cnt++;
638         return c;
639 }
640
641 static struct branch *lookup_branch(const char *name)
642 {
643         unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
644         struct branch *b;
645
646         for (b = branch_table[hc]; b; b = b->table_next_branch)
647                 if (!strcmp(name, b->name))
648                         return b;
649         return NULL;
650 }
651
652 static struct branch *new_branch(const char *name)
653 {
654         unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
655         struct branch* b = lookup_branch(name);
656
657         if (b)
658                 die("Invalid attempt to create duplicate branch: %s", name);
659         switch (check_ref_format(name)) {
660         case  0: break; /* its valid */
661         case -2: break; /* valid, but too few '/', allow anyway */
662         default:
663                 die("Branch name doesn't conform to GIT standards: %s", name);
664         }
665
666         b = pool_calloc(1, sizeof(struct branch));
667         b->name = pool_strdup(name);
668         b->table_next_branch = branch_table[hc];
669         b->branch_tree.versions[0].mode = S_IFDIR;
670         b->branch_tree.versions[1].mode = S_IFDIR;
671         b->active = 0;
672         b->pack_id = MAX_PACK_ID;
673         branch_table[hc] = b;
674         branch_count++;
675         return b;
676 }
677
678 static unsigned int hc_entries(unsigned int cnt)
679 {
680         cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
681         return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
682 }
683
684 static struct tree_content *new_tree_content(unsigned int cnt)
685 {
686         struct avail_tree_content *f, *l = NULL;
687         struct tree_content *t;
688         unsigned int hc = hc_entries(cnt);
689
690         for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
691                 if (f->entry_capacity >= cnt)
692                         break;
693
694         if (f) {
695                 if (l)
696                         l->next_avail = f->next_avail;
697                 else
698                         avail_tree_table[hc] = f->next_avail;
699         } else {
700                 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
701                 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
702                 f->entry_capacity = cnt;
703         }
704
705         t = (struct tree_content*)f;
706         t->entry_count = 0;
707         t->delta_depth = 0;
708         return t;
709 }
710
711 static void release_tree_entry(struct tree_entry *e);
712 static void release_tree_content(struct tree_content *t)
713 {
714         struct avail_tree_content *f = (struct avail_tree_content*)t;
715         unsigned int hc = hc_entries(f->entry_capacity);
716         f->next_avail = avail_tree_table[hc];
717         avail_tree_table[hc] = f;
718 }
719
720 static void release_tree_content_recursive(struct tree_content *t)
721 {
722         unsigned int i;
723         for (i = 0; i < t->entry_count; i++)
724                 release_tree_entry(t->entries[i]);
725         release_tree_content(t);
726 }
727
728 static struct tree_content *grow_tree_content(
729         struct tree_content *t,
730         int amt)
731 {
732         struct tree_content *r = new_tree_content(t->entry_count + amt);
733         r->entry_count = t->entry_count;
734         r->delta_depth = t->delta_depth;
735         memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
736         release_tree_content(t);
737         return r;
738 }
739
740 static struct tree_entry *new_tree_entry(void)
741 {
742         struct tree_entry *e;
743
744         if (!avail_tree_entry) {
745                 unsigned int n = tree_entry_alloc;
746                 total_allocd += n * sizeof(struct tree_entry);
747                 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
748                 while (n-- > 1) {
749                         *((void**)e) = e + 1;
750                         e++;
751                 }
752                 *((void**)e) = NULL;
753         }
754
755         e = avail_tree_entry;
756         avail_tree_entry = *((void**)e);
757         return e;
758 }
759
760 static void release_tree_entry(struct tree_entry *e)
761 {
762         if (e->tree)
763                 release_tree_content_recursive(e->tree);
764         *((void**)e) = avail_tree_entry;
765         avail_tree_entry = e;
766 }
767
768 static struct tree_content *dup_tree_content(struct tree_content *s)
769 {
770         struct tree_content *d;
771         struct tree_entry *a, *b;
772         unsigned int i;
773
774         if (!s)
775                 return NULL;
776         d = new_tree_content(s->entry_count);
777         for (i = 0; i < s->entry_count; i++) {
778                 a = s->entries[i];
779                 b = new_tree_entry();
780                 memcpy(b, a, sizeof(*a));
781                 if (a->tree && is_null_sha1(b->versions[1].sha1))
782                         b->tree = dup_tree_content(a->tree);
783                 else
784                         b->tree = NULL;
785                 d->entries[i] = b;
786         }
787         d->entry_count = s->entry_count;
788         d->delta_depth = s->delta_depth;
789
790         return d;
791 }
792
793 static void start_packfile(void)
794 {
795         static char tmpfile[PATH_MAX];
796         struct packed_git *p;
797         struct pack_header hdr;
798         int pack_fd;
799
800         snprintf(tmpfile, sizeof(tmpfile),
801                 "%s/tmp_pack_XXXXXX", get_object_directory());
802         pack_fd = xmkstemp(tmpfile);
803         p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
804         strcpy(p->pack_name, tmpfile);
805         p->pack_fd = pack_fd;
806
807         hdr.hdr_signature = htonl(PACK_SIGNATURE);
808         hdr.hdr_version = htonl(2);
809         hdr.hdr_entries = 0;
810         write_or_die(p->pack_fd, &hdr, sizeof(hdr));
811
812         pack_data = p;
813         pack_size = sizeof(hdr);
814         object_count = 0;
815
816         all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
817         all_packs[pack_id] = p;
818 }
819
820 static int oecmp (const void *a_, const void *b_)
821 {
822         struct object_entry *a = *((struct object_entry**)a_);
823         struct object_entry *b = *((struct object_entry**)b_);
824         return hashcmp(a->sha1, b->sha1);
825 }
826
827 static char *create_index(void)
828 {
829         static char tmpfile[PATH_MAX];
830         SHA_CTX ctx;
831         struct sha1file *f;
832         struct object_entry **idx, **c, **last, *e;
833         struct object_entry_pool *o;
834         uint32_t array[256];
835         int i, idx_fd;
836
837         /* Build the sorted table of object IDs. */
838         idx = xmalloc(object_count * sizeof(struct object_entry*));
839         c = idx;
840         for (o = blocks; o; o = o->next_pool)
841                 for (e = o->next_free; e-- != o->entries;)
842                         if (pack_id == e->pack_id)
843                                 *c++ = e;
844         last = idx + object_count;
845         if (c != last)
846                 die("internal consistency error creating the index");
847         qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
848
849         /* Generate the fan-out array. */
850         c = idx;
851         for (i = 0; i < 256; i++) {
852                 struct object_entry **next = c;;
853                 while (next < last) {
854                         if ((*next)->sha1[0] != i)
855                                 break;
856                         next++;
857                 }
858                 array[i] = htonl(next - idx);
859                 c = next;
860         }
861
862         snprintf(tmpfile, sizeof(tmpfile),
863                 "%s/tmp_idx_XXXXXX", get_object_directory());
864         idx_fd = xmkstemp(tmpfile);
865         f = sha1fd(idx_fd, tmpfile);
866         sha1write(f, array, 256 * sizeof(int));
867         SHA1_Init(&ctx);
868         for (c = idx; c != last; c++) {
869                 uint32_t offset = htonl((*c)->offset);
870                 sha1write(f, &offset, 4);
871                 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
872                 SHA1_Update(&ctx, (*c)->sha1, 20);
873         }
874         sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
875         sha1close(f, NULL, 1);
876         free(idx);
877         SHA1_Final(pack_data->sha1, &ctx);
878         return tmpfile;
879 }
880
881 static char *keep_pack(char *curr_index_name)
882 {
883         static char name[PATH_MAX];
884         static const char *keep_msg = "fast-import";
885         int keep_fd;
886
887         chmod(pack_data->pack_name, 0444);
888         chmod(curr_index_name, 0444);
889
890         snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
891                  get_object_directory(), sha1_to_hex(pack_data->sha1));
892         keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
893         if (keep_fd < 0)
894                 die("cannot create keep file");
895         write(keep_fd, keep_msg, strlen(keep_msg));
896         close(keep_fd);
897
898         snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
899                  get_object_directory(), sha1_to_hex(pack_data->sha1));
900         if (move_temp_to_file(pack_data->pack_name, name))
901                 die("cannot store pack file");
902
903         snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
904                  get_object_directory(), sha1_to_hex(pack_data->sha1));
905         if (move_temp_to_file(curr_index_name, name))
906                 die("cannot store index file");
907         return name;
908 }
909
910 static void unkeep_all_packs(void)
911 {
912         static char name[PATH_MAX];
913         int k;
914
915         for (k = 0; k < pack_id; k++) {
916                 struct packed_git *p = all_packs[k];
917                 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
918                          get_object_directory(), sha1_to_hex(p->sha1));
919                 unlink(name);
920         }
921 }
922
923 static void end_packfile(void)
924 {
925         struct packed_git *old_p = pack_data, *new_p;
926
927         if (object_count) {
928                 char *idx_name;
929                 int i;
930                 struct branch *b;
931                 struct tag *t;
932
933                 fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
934                                     pack_data->pack_name, object_count);
935                 close(pack_data->pack_fd);
936                 idx_name = keep_pack(create_index());
937
938                 /* Register the packfile with core git's machinary. */
939                 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
940                 if (!new_p)
941                         die("core git rejected index %s", idx_name);
942                 new_p->windows = old_p->windows;
943                 all_packs[pack_id] = new_p;
944                 install_packed_git(new_p);
945
946                 /* Print the boundary */
947                 if (pack_edges) {
948                         fprintf(pack_edges, "%s:", new_p->pack_name);
949                         for (i = 0; i < branch_table_sz; i++) {
950                                 for (b = branch_table[i]; b; b = b->table_next_branch) {
951                                         if (b->pack_id == pack_id)
952                                                 fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
953                                 }
954                         }
955                         for (t = first_tag; t; t = t->next_tag) {
956                                 if (t->pack_id == pack_id)
957                                         fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
958                         }
959                         fputc('\n', pack_edges);
960                         fflush(pack_edges);
961                 }
962
963                 pack_id++;
964         }
965         else
966                 unlink(old_p->pack_name);
967         free(old_p);
968
969         /* We can't carry a delta across packfiles. */
970         free(last_blob.data);
971         last_blob.data = NULL;
972         last_blob.len = 0;
973         last_blob.offset = 0;
974         last_blob.depth = 0;
975 }
976
977 static void cycle_packfile(void)
978 {
979         end_packfile();
980         start_packfile();
981 }
982
983 static size_t encode_header(
984         enum object_type type,
985         size_t size,
986         unsigned char *hdr)
987 {
988         int n = 1;
989         unsigned char c;
990
991         if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
992                 die("bad type %d", type);
993
994         c = (type << 4) | (size & 15);
995         size >>= 4;
996         while (size) {
997                 *hdr++ = c | 0x80;
998                 c = size & 0x7f;
999                 size >>= 7;
1000                 n++;
1001         }
1002         *hdr = c;
1003         return n;
1004 }
1005
1006 static int store_object(
1007         enum object_type type,
1008         void *dat,
1009         size_t datlen,
1010         struct last_object *last,
1011         unsigned char *sha1out,
1012         uintmax_t mark)
1013 {
1014         void *out, *delta;
1015         struct object_entry *e;
1016         unsigned char hdr[96];
1017         unsigned char sha1[20];
1018         unsigned long hdrlen, deltalen;
1019         SHA_CTX c;
1020         z_stream s;
1021
1022         hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
1023                 (unsigned long)datlen) + 1;
1024         SHA1_Init(&c);
1025         SHA1_Update(&c, hdr, hdrlen);
1026         SHA1_Update(&c, dat, datlen);
1027         SHA1_Final(sha1, &c);
1028         if (sha1out)
1029                 hashcpy(sha1out, sha1);
1030
1031         e = insert_object(sha1);
1032         if (mark)
1033                 insert_mark(mark, e);
1034         if (e->offset) {
1035                 duplicate_count_by_type[type]++;
1036                 return 1;
1037         } else if (find_sha1_pack(sha1, packed_git)) {
1038                 e->type = type;
1039                 e->pack_id = MAX_PACK_ID;
1040                 e->offset = 1; /* just not zero! */
1041                 duplicate_count_by_type[type]++;
1042                 return 1;
1043         }
1044
1045         if (last && last->data && last->depth < max_depth) {
1046                 delta = diff_delta(last->data, last->len,
1047                         dat, datlen,
1048                         &deltalen, 0);
1049                 if (delta && deltalen >= datlen) {
1050                         free(delta);
1051                         delta = NULL;
1052                 }
1053         } else
1054                 delta = NULL;
1055
1056         memset(&s, 0, sizeof(s));
1057         deflateInit(&s, zlib_compression_level);
1058         if (delta) {
1059                 s.next_in = delta;
1060                 s.avail_in = deltalen;
1061         } else {
1062                 s.next_in = dat;
1063                 s.avail_in = datlen;
1064         }
1065         s.avail_out = deflateBound(&s, s.avail_in);
1066         s.next_out = out = xmalloc(s.avail_out);
1067         while (deflate(&s, Z_FINISH) == Z_OK)
1068                 /* nothing */;
1069         deflateEnd(&s);
1070
1071         /* Determine if we should auto-checkpoint. */
1072         if ((pack_size + 60 + s.total_out) > max_packsize
1073                 || (pack_size + 60 + s.total_out) < pack_size) {
1074
1075                 /* This new object needs to *not* have the current pack_id. */
1076                 e->pack_id = pack_id + 1;
1077                 cycle_packfile();
1078
1079                 /* We cannot carry a delta into the new pack. */
1080                 if (delta) {
1081                         free(delta);
1082                         delta = NULL;
1083
1084                         memset(&s, 0, sizeof(s));
1085                         deflateInit(&s, zlib_compression_level);
1086                         s.next_in = dat;
1087                         s.avail_in = datlen;
1088                         s.avail_out = deflateBound(&s, s.avail_in);
1089                         s.next_out = out = xrealloc(out, s.avail_out);
1090                         while (deflate(&s, Z_FINISH) == Z_OK)
1091                                 /* nothing */;
1092                         deflateEnd(&s);
1093                 }
1094         }
1095
1096         e->type = type;
1097         e->pack_id = pack_id;
1098         e->offset = pack_size;
1099         object_count++;
1100         object_count_by_type[type]++;
1101
1102         if (delta) {
1103                 unsigned long ofs = e->offset - last->offset;
1104                 unsigned pos = sizeof(hdr) - 1;
1105
1106                 delta_count_by_type[type]++;
1107                 last->depth++;
1108
1109                 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
1110                 write_or_die(pack_data->pack_fd, hdr, hdrlen);
1111                 pack_size += hdrlen;
1112
1113                 hdr[pos] = ofs & 127;
1114                 while (ofs >>= 7)
1115                         hdr[--pos] = 128 | (--ofs & 127);
1116                 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
1117                 pack_size += sizeof(hdr) - pos;
1118         } else {
1119                 if (last)
1120                         last->depth = 0;
1121                 hdrlen = encode_header(type, datlen, hdr);
1122                 write_or_die(pack_data->pack_fd, hdr, hdrlen);
1123                 pack_size += hdrlen;
1124         }
1125
1126         write_or_die(pack_data->pack_fd, out, s.total_out);
1127         pack_size += s.total_out;
1128
1129         free(out);
1130         free(delta);
1131         if (last) {
1132                 if (!last->no_free)
1133                         free(last->data);
1134                 last->data = dat;
1135                 last->offset = e->offset;
1136                 last->len = datlen;
1137         }
1138         return 0;
1139 }
1140
1141 static void *gfi_unpack_entry(
1142         struct object_entry *oe,
1143         unsigned long *sizep)
1144 {
1145         enum object_type type;
1146         struct packed_git *p = all_packs[oe->pack_id];
1147         if (p == pack_data)
1148                 p->pack_size = pack_size + 20;
1149         return unpack_entry(p, oe->offset, &type, sizep);
1150 }
1151
1152 static const char *get_mode(const char *str, uint16_t *modep)
1153 {
1154         unsigned char c;
1155         uint16_t mode = 0;
1156
1157         while ((c = *str++) != ' ') {
1158                 if (c < '0' || c > '7')
1159                         return NULL;
1160                 mode = (mode << 3) + (c - '0');
1161         }
1162         *modep = mode;
1163         return str;
1164 }
1165
1166 static void load_tree(struct tree_entry *root)
1167 {
1168         unsigned char* sha1 = root->versions[1].sha1;
1169         struct object_entry *myoe;
1170         struct tree_content *t;
1171         unsigned long size;
1172         char *buf;
1173         const char *c;
1174
1175         root->tree = t = new_tree_content(8);
1176         if (is_null_sha1(sha1))
1177                 return;
1178
1179         myoe = find_object(sha1);
1180         if (myoe && myoe->pack_id != MAX_PACK_ID) {
1181                 if (myoe->type != OBJ_TREE)
1182                         die("Not a tree: %s", sha1_to_hex(sha1));
1183                 t->delta_depth = 0;
1184                 buf = gfi_unpack_entry(myoe, &size);
1185         } else {
1186                 enum object_type type;
1187                 buf = read_sha1_file(sha1, &type, &size);
1188                 if (!buf || type != OBJ_TREE)
1189                         die("Can't load tree %s", sha1_to_hex(sha1));
1190         }
1191
1192         c = buf;
1193         while (c != (buf + size)) {
1194                 struct tree_entry *e = new_tree_entry();
1195
1196                 if (t->entry_count == t->entry_capacity)
1197                         root->tree = t = grow_tree_content(t, t->entry_count);
1198                 t->entries[t->entry_count++] = e;
1199
1200                 e->tree = NULL;
1201                 c = get_mode(c, &e->versions[1].mode);
1202                 if (!c)
1203                         die("Corrupt mode in %s", sha1_to_hex(sha1));
1204                 e->versions[0].mode = e->versions[1].mode;
1205                 e->name = to_atom(c, strlen(c));
1206                 c += e->name->str_len + 1;
1207                 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1208                 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1209                 c += 20;
1210         }
1211         free(buf);
1212 }
1213
1214 static int tecmp0 (const void *_a, const void *_b)
1215 {
1216         struct tree_entry *a = *((struct tree_entry**)_a);
1217         struct tree_entry *b = *((struct tree_entry**)_b);
1218         return base_name_compare(
1219                 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1220                 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1221 }
1222
1223 static int tecmp1 (const void *_a, const void *_b)
1224 {
1225         struct tree_entry *a = *((struct tree_entry**)_a);
1226         struct tree_entry *b = *((struct tree_entry**)_b);
1227         return base_name_compare(
1228                 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1229                 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1230 }
1231
1232 static void mktree(struct tree_content *t,
1233         int v,
1234         unsigned long *szp,
1235         struct dbuf *b)
1236 {
1237         size_t maxlen = 0;
1238         unsigned int i;
1239         char *c;
1240
1241         if (!v)
1242                 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1243         else
1244                 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1245
1246         for (i = 0; i < t->entry_count; i++) {
1247                 if (t->entries[i]->versions[v].mode)
1248                         maxlen += t->entries[i]->name->str_len + 34;
1249         }
1250
1251         size_dbuf(b, maxlen);
1252         c = b->buffer;
1253         for (i = 0; i < t->entry_count; i++) {
1254                 struct tree_entry *e = t->entries[i];
1255                 if (!e->versions[v].mode)
1256                         continue;
1257                 c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1258                 *c++ = ' ';
1259                 strcpy(c, e->name->str_dat);
1260                 c += e->name->str_len + 1;
1261                 hashcpy((unsigned char*)c, e->versions[v].sha1);
1262                 c += 20;
1263         }
1264         *szp = c - (char*)b->buffer;
1265 }
1266
1267 static void store_tree(struct tree_entry *root)
1268 {
1269         struct tree_content *t = root->tree;
1270         unsigned int i, j, del;
1271         unsigned long new_len;
1272         struct last_object lo;
1273         struct object_entry *le;
1274
1275         if (!is_null_sha1(root->versions[1].sha1))
1276                 return;
1277
1278         for (i = 0; i < t->entry_count; i++) {
1279                 if (t->entries[i]->tree)
1280                         store_tree(t->entries[i]);
1281         }
1282
1283         le = find_object(root->versions[0].sha1);
1284         if (!S_ISDIR(root->versions[0].mode)
1285                 || !le
1286                 || le->pack_id != pack_id) {
1287                 lo.data = NULL;
1288                 lo.depth = 0;
1289                 lo.no_free = 0;
1290         } else {
1291                 mktree(t, 0, &lo.len, &old_tree);
1292                 lo.data = old_tree.buffer;
1293                 lo.offset = le->offset;
1294                 lo.depth = t->delta_depth;
1295                 lo.no_free = 1;
1296         }
1297
1298         mktree(t, 1, &new_len, &new_tree);
1299         store_object(OBJ_TREE, new_tree.buffer, new_len,
1300                 &lo, root->versions[1].sha1, 0);
1301
1302         t->delta_depth = lo.depth;
1303         for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1304                 struct tree_entry *e = t->entries[i];
1305                 if (e->versions[1].mode) {
1306                         e->versions[0].mode = e->versions[1].mode;
1307                         hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1308                         t->entries[j++] = e;
1309                 } else {
1310                         release_tree_entry(e);
1311                         del++;
1312                 }
1313         }
1314         t->entry_count -= del;
1315 }
1316
1317 static int tree_content_set(
1318         struct tree_entry *root,
1319         const char *p,
1320         const unsigned char *sha1,
1321         const uint16_t mode,
1322         struct tree_content *subtree)
1323 {
1324         struct tree_content *t = root->tree;
1325         const char *slash1;
1326         unsigned int i, n;
1327         struct tree_entry *e;
1328
1329         slash1 = strchr(p, '/');
1330         if (slash1)
1331                 n = slash1 - p;
1332         else
1333                 n = strlen(p);
1334         if (!n)
1335                 die("Empty path component found in input");
1336         if (!slash1 && !S_ISDIR(mode) && subtree)
1337                 die("Non-directories cannot have subtrees");
1338
1339         for (i = 0; i < t->entry_count; i++) {
1340                 e = t->entries[i];
1341                 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1342                         if (!slash1) {
1343                                 if (!S_ISDIR(mode)
1344                                                 && e->versions[1].mode == mode
1345                                                 && !hashcmp(e->versions[1].sha1, sha1))
1346                                         return 0;
1347                                 e->versions[1].mode = mode;
1348                                 hashcpy(e->versions[1].sha1, sha1);
1349                                 if (e->tree)
1350                                         release_tree_content_recursive(e->tree);
1351                                 e->tree = subtree;
1352                                 hashclr(root->versions[1].sha1);
1353                                 return 1;
1354                         }
1355                         if (!S_ISDIR(e->versions[1].mode)) {
1356                                 e->tree = new_tree_content(8);
1357                                 e->versions[1].mode = S_IFDIR;
1358                         }
1359                         if (!e->tree)
1360                                 load_tree(e);
1361                         if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
1362                                 hashclr(root->versions[1].sha1);
1363                                 return 1;
1364                         }
1365                         return 0;
1366                 }
1367         }
1368
1369         if (t->entry_count == t->entry_capacity)
1370                 root->tree = t = grow_tree_content(t, t->entry_count);
1371         e = new_tree_entry();
1372         e->name = to_atom(p, n);
1373         e->versions[0].mode = 0;
1374         hashclr(e->versions[0].sha1);
1375         t->entries[t->entry_count++] = e;
1376         if (slash1) {
1377                 e->tree = new_tree_content(8);
1378                 e->versions[1].mode = S_IFDIR;
1379                 tree_content_set(e, slash1 + 1, sha1, mode, subtree);
1380         } else {
1381                 e->tree = subtree;
1382                 e->versions[1].mode = mode;
1383                 hashcpy(e->versions[1].sha1, sha1);
1384         }
1385         hashclr(root->versions[1].sha1);
1386         return 1;
1387 }
1388
1389 static int tree_content_remove(
1390         struct tree_entry *root,
1391         const char *p,
1392         struct tree_entry *backup_leaf)
1393 {
1394         struct tree_content *t = root->tree;
1395         const char *slash1;
1396         unsigned int i, n;
1397         struct tree_entry *e;
1398
1399         slash1 = strchr(p, '/');
1400         if (slash1)
1401                 n = slash1 - p;
1402         else
1403                 n = strlen(p);
1404
1405         for (i = 0; i < t->entry_count; i++) {
1406                 e = t->entries[i];
1407                 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1408                         if (!slash1 || !S_ISDIR(e->versions[1].mode))
1409                                 goto del_entry;
1410                         if (!e->tree)
1411                                 load_tree(e);
1412                         if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
1413                                 for (n = 0; n < e->tree->entry_count; n++) {
1414                                         if (e->tree->entries[n]->versions[1].mode) {
1415                                                 hashclr(root->versions[1].sha1);
1416                                                 return 1;
1417                                         }
1418                                 }
1419                                 backup_leaf = NULL;
1420                                 goto del_entry;
1421                         }
1422                         return 0;
1423                 }
1424         }
1425         return 0;
1426
1427 del_entry:
1428         if (backup_leaf)
1429                 memcpy(backup_leaf, e, sizeof(*backup_leaf));
1430         else if (e->tree)
1431                 release_tree_content_recursive(e->tree);
1432         e->tree = NULL;
1433         e->versions[1].mode = 0;
1434         hashclr(e->versions[1].sha1);
1435         hashclr(root->versions[1].sha1);
1436         return 1;
1437 }
1438
1439 static int tree_content_get(
1440         struct tree_entry *root,
1441         const char *p,
1442         struct tree_entry *leaf)
1443 {
1444         struct tree_content *t = root->tree;
1445         const char *slash1;
1446         unsigned int i, n;
1447         struct tree_entry *e;
1448
1449         slash1 = strchr(p, '/');
1450         if (slash1)
1451                 n = slash1 - p;
1452         else
1453                 n = strlen(p);
1454
1455         for (i = 0; i < t->entry_count; i++) {
1456                 e = t->entries[i];
1457                 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1458                         if (!slash1) {
1459                                 memcpy(leaf, e, sizeof(*leaf));
1460                                 if (e->tree && is_null_sha1(e->versions[1].sha1))
1461                                         leaf->tree = dup_tree_content(e->tree);
1462                                 else
1463                                         leaf->tree = NULL;
1464                                 return 1;
1465                         }
1466                         if (!S_ISDIR(e->versions[1].mode))
1467                                 return 0;
1468                         if (!e->tree)
1469                                 load_tree(e);
1470                         return tree_content_get(e, slash1 + 1, leaf);
1471                 }
1472         }
1473         return 0;
1474 }
1475
1476 static int update_branch(struct branch *b)
1477 {
1478         static const char *msg = "fast-import";
1479         struct ref_lock *lock;
1480         unsigned char old_sha1[20];
1481
1482         if (read_ref(b->name, old_sha1))
1483                 hashclr(old_sha1);
1484         lock = lock_any_ref_for_update(b->name, old_sha1, 0);
1485         if (!lock)
1486                 return error("Unable to lock %s", b->name);
1487         if (!force_update && !is_null_sha1(old_sha1)) {
1488                 struct commit *old_cmit, *new_cmit;
1489
1490                 old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1491                 new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1492                 if (!old_cmit || !new_cmit) {
1493                         unlock_ref(lock);
1494                         return error("Branch %s is missing commits.", b->name);
1495                 }
1496
1497                 if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
1498                         unlock_ref(lock);
1499                         warning("Not updating %s"
1500                                 " (new tip %s does not contain %s)",
1501                                 b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1502                         return -1;
1503                 }
1504         }
1505         if (write_ref_sha1(lock, b->sha1, msg) < 0)
1506                 return error("Unable to update %s", b->name);
1507         return 0;
1508 }
1509
1510 static void dump_branches(void)
1511 {
1512         unsigned int i;
1513         struct branch *b;
1514
1515         for (i = 0; i < branch_table_sz; i++) {
1516                 for (b = branch_table[i]; b; b = b->table_next_branch)
1517                         failure |= update_branch(b);
1518         }
1519 }
1520
1521 static void dump_tags(void)
1522 {
1523         static const char *msg = "fast-import";
1524         struct tag *t;
1525         struct ref_lock *lock;
1526         char ref_name[PATH_MAX];
1527
1528         for (t = first_tag; t; t = t->next_tag) {
1529                 sprintf(ref_name, "tags/%s", t->name);
1530                 lock = lock_ref_sha1(ref_name, NULL);
1531                 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1532                         failure |= error("Unable to update %s", ref_name);
1533         }
1534 }
1535
1536 static void dump_marks_helper(FILE *f,
1537         uintmax_t base,
1538         struct mark_set *m)
1539 {
1540         uintmax_t k;
1541         if (m->shift) {
1542                 for (k = 0; k < 1024; k++) {
1543                         if (m->data.sets[k])
1544                                 dump_marks_helper(f, (base + k) << m->shift,
1545                                         m->data.sets[k]);
1546                 }
1547         } else {
1548                 for (k = 0; k < 1024; k++) {
1549                         if (m->data.marked[k])
1550                                 fprintf(f, ":%" PRIuMAX " %s\n", base + k,
1551                                         sha1_to_hex(m->data.marked[k]->sha1));
1552                 }
1553         }
1554 }
1555
1556 static void dump_marks(void)
1557 {
1558         static struct lock_file mark_lock;
1559         int mark_fd;
1560         FILE *f;
1561
1562         if (!mark_file)
1563                 return;
1564
1565         mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
1566         if (mark_fd < 0) {
1567                 failure |= error("Unable to write marks file %s: %s",
1568                         mark_file, strerror(errno));
1569                 return;
1570         }
1571
1572         f = fdopen(mark_fd, "w");
1573         if (!f) {
1574                 rollback_lock_file(&mark_lock);
1575                 failure |= error("Unable to write marks file %s: %s",
1576                         mark_file, strerror(errno));
1577                 return;
1578         }
1579
1580         dump_marks_helper(f, 0, marks);
1581         fclose(f);
1582         if (commit_lock_file(&mark_lock))
1583                 failure |= error("Unable to write marks file %s: %s",
1584                         mark_file, strerror(errno));
1585 }
1586
1587 static int read_next_command(void)
1588 {
1589         static int stdin_eof = 0;
1590
1591         if (stdin_eof) {
1592                 unread_command_buf = 0;
1593                 return EOF;
1594         }
1595
1596         do {
1597                 if (unread_command_buf) {
1598                         unread_command_buf = 0;
1599                 } else {
1600                         struct recent_command *rc;
1601
1602                         strbuf_detach(&command_buf);
1603                         stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
1604                         if (stdin_eof)
1605                                 return EOF;
1606
1607                         rc = rc_free;
1608                         if (rc)
1609                                 rc_free = rc->next;
1610                         else {
1611                                 rc = cmd_hist.next;
1612                                 cmd_hist.next = rc->next;
1613                                 cmd_hist.next->prev = &cmd_hist;
1614                                 free(rc->buf);
1615                         }
1616
1617                         rc->buf = command_buf.buf;
1618                         rc->prev = cmd_tail;
1619                         rc->next = cmd_hist.prev;
1620                         rc->prev->next = rc;
1621                         cmd_tail = rc;
1622                 }
1623         } while (command_buf.buf[0] == '#');
1624
1625         return 0;
1626 }
1627
1628 static void skip_optional_lf(void)
1629 {
1630         int term_char = fgetc(stdin);
1631         if (term_char != '\n' && term_char != EOF)
1632                 ungetc(term_char, stdin);
1633 }
1634
1635 static void cmd_mark(void)
1636 {
1637         if (!prefixcmp(command_buf.buf, "mark :")) {
1638                 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1639                 read_next_command();
1640         }
1641         else
1642                 next_mark = 0;
1643 }
1644
1645 static void *cmd_data (size_t *size)
1646 {
1647         struct strbuf buffer;
1648
1649         strbuf_init(&buffer, 0);
1650         if (prefixcmp(command_buf.buf, "data "))
1651                 die("Expected 'data n' command, found: %s", command_buf.buf);
1652
1653         if (!prefixcmp(command_buf.buf + 5, "<<")) {
1654                 char *term = xstrdup(command_buf.buf + 5 + 2);
1655                 size_t term_len = command_buf.len - 5 - 2;
1656
1657                 for (;;) {
1658                         if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
1659                                 die("EOF in data (terminator '%s' not found)", term);
1660                         if (term_len == command_buf.len
1661                                 && !strcmp(term, command_buf.buf))
1662                                 break;
1663                         strbuf_addbuf(&buffer, &command_buf);
1664                         strbuf_addch(&buffer, '\n');
1665                 }
1666                 free(term);
1667         }
1668         else {
1669                 size_t n = 0, length;
1670
1671                 length = strtoul(command_buf.buf + 5, NULL, 10);
1672
1673                 while (n < length) {
1674                         size_t s = strbuf_fread(&buffer, length - n, stdin);
1675                         if (!s && feof(stdin))
1676                                 die("EOF in data (%lu bytes remaining)",
1677                                         (unsigned long)(length - n));
1678                         n += s;
1679                 }
1680         }
1681
1682         skip_optional_lf();
1683         *size = buffer.len;
1684         return strbuf_detach(&buffer);
1685 }
1686
1687 static int validate_raw_date(const char *src, char *result, int maxlen)
1688 {
1689         const char *orig_src = src;
1690         char *endp, sign;
1691
1692         strtoul(src, &endp, 10);
1693         if (endp == src || *endp != ' ')
1694                 return -1;
1695
1696         src = endp + 1;
1697         if (*src != '-' && *src != '+')
1698                 return -1;
1699         sign = *src;
1700
1701         strtoul(src + 1, &endp, 10);
1702         if (endp == src || *endp || (endp - orig_src) >= maxlen)
1703                 return -1;
1704
1705         strcpy(result, orig_src);
1706         return 0;
1707 }
1708
1709 static char *parse_ident(const char *buf)
1710 {
1711         const char *gt;
1712         size_t name_len;
1713         char *ident;
1714
1715         gt = strrchr(buf, '>');
1716         if (!gt)
1717                 die("Missing > in ident string: %s", buf);
1718         gt++;
1719         if (*gt != ' ')
1720                 die("Missing space after > in ident string: %s", buf);
1721         gt++;
1722         name_len = gt - buf;
1723         ident = xmalloc(name_len + 24);
1724         strncpy(ident, buf, name_len);
1725
1726         switch (whenspec) {
1727         case WHENSPEC_RAW:
1728                 if (validate_raw_date(gt, ident + name_len, 24) < 0)
1729                         die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1730                 break;
1731         case WHENSPEC_RFC2822:
1732                 if (parse_date(gt, ident + name_len, 24) < 0)
1733                         die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1734                 break;
1735         case WHENSPEC_NOW:
1736                 if (strcmp("now", gt))
1737                         die("Date in ident must be 'now': %s", buf);
1738                 datestamp(ident + name_len, 24);
1739                 break;
1740         }
1741
1742         return ident;
1743 }
1744
1745 static void cmd_new_blob(void)
1746 {
1747         size_t l;
1748         void *d;
1749
1750         read_next_command();
1751         cmd_mark();
1752         d = cmd_data(&l);
1753
1754         if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1755                 free(d);
1756 }
1757
1758 static void unload_one_branch(void)
1759 {
1760         while (cur_active_branches
1761                 && cur_active_branches >= max_active_branches) {
1762                 uintmax_t min_commit = ULONG_MAX;
1763                 struct branch *e, *l = NULL, *p = NULL;
1764
1765                 for (e = active_branches; e; e = e->active_next_branch) {
1766                         if (e->last_commit < min_commit) {
1767                                 p = l;
1768                                 min_commit = e->last_commit;
1769                         }
1770                         l = e;
1771                 }
1772
1773                 if (p) {
1774                         e = p->active_next_branch;
1775                         p->active_next_branch = e->active_next_branch;
1776                 } else {
1777                         e = active_branches;
1778                         active_branches = e->active_next_branch;
1779                 }
1780                 e->active = 0;
1781                 e->active_next_branch = NULL;
1782                 if (e->branch_tree.tree) {
1783                         release_tree_content_recursive(e->branch_tree.tree);
1784                         e->branch_tree.tree = NULL;
1785                 }
1786                 cur_active_branches--;
1787         }
1788 }
1789
1790 static void load_branch(struct branch *b)
1791 {
1792         load_tree(&b->branch_tree);
1793         if (!b->active) {
1794                 b->active = 1;
1795                 b->active_next_branch = active_branches;
1796                 active_branches = b;
1797                 cur_active_branches++;
1798                 branch_load_count++;
1799         }
1800 }
1801
1802 static void file_change_m(struct branch *b)
1803 {
1804         const char *p = command_buf.buf + 2;
1805         char *p_uq;
1806         const char *endp;
1807         struct object_entry *oe = oe;
1808         unsigned char sha1[20];
1809         uint16_t mode, inline_data = 0;
1810
1811         p = get_mode(p, &mode);
1812         if (!p)
1813                 die("Corrupt mode: %s", command_buf.buf);
1814         switch (mode) {
1815         case S_IFREG | 0644:
1816         case S_IFREG | 0755:
1817         case S_IFLNK:
1818         case 0644:
1819         case 0755:
1820                 /* ok */
1821                 break;
1822         default:
1823                 die("Corrupt mode: %s", command_buf.buf);
1824         }
1825
1826         if (*p == ':') {
1827                 char *x;
1828                 oe = find_mark(strtoumax(p + 1, &x, 10));
1829                 hashcpy(sha1, oe->sha1);
1830                 p = x;
1831         } else if (!prefixcmp(p, "inline")) {
1832                 inline_data = 1;
1833                 p += 6;
1834         } else {
1835                 if (get_sha1_hex(p, sha1))
1836                         die("Invalid SHA1: %s", command_buf.buf);
1837                 oe = find_object(sha1);
1838                 p += 40;
1839         }
1840         if (*p++ != ' ')
1841                 die("Missing space after SHA1: %s", command_buf.buf);
1842
1843         p_uq = unquote_c_style(p, &endp);
1844         if (p_uq) {
1845                 if (*endp)
1846                         die("Garbage after path in: %s", command_buf.buf);
1847                 p = p_uq;
1848         }
1849
1850         if (inline_data) {
1851                 size_t l;
1852                 void *d;
1853                 if (!p_uq)
1854                         p = p_uq = xstrdup(p);
1855                 read_next_command();
1856                 d = cmd_data(&l);
1857                 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1858                         free(d);
1859         } else if (oe) {
1860                 if (oe->type != OBJ_BLOB)
1861                         die("Not a blob (actually a %s): %s",
1862                                 command_buf.buf, typename(oe->type));
1863         } else {
1864                 enum object_type type = sha1_object_info(sha1, NULL);
1865                 if (type < 0)
1866                         die("Blob not found: %s", command_buf.buf);
1867                 if (type != OBJ_BLOB)
1868                         die("Not a blob (actually a %s): %s",
1869                             typename(type), command_buf.buf);
1870         }
1871
1872         tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
1873         free(p_uq);
1874 }
1875
1876 static void file_change_d(struct branch *b)
1877 {
1878         const char *p = command_buf.buf + 2;
1879         char *p_uq;
1880         const char *endp;
1881
1882         p_uq = unquote_c_style(p, &endp);
1883         if (p_uq) {
1884                 if (*endp)
1885                         die("Garbage after path in: %s", command_buf.buf);
1886                 p = p_uq;
1887         }
1888         tree_content_remove(&b->branch_tree, p, NULL);
1889         free(p_uq);
1890 }
1891
1892 static void file_change_cr(struct branch *b, int rename)
1893 {
1894         const char *s, *d;
1895         char *s_uq, *d_uq;
1896         const char *endp;
1897         struct tree_entry leaf;
1898
1899         s = command_buf.buf + 2;
1900         s_uq = unquote_c_style(s, &endp);
1901         if (s_uq) {
1902                 if (*endp != ' ')
1903                         die("Missing space after source: %s", command_buf.buf);
1904         }
1905         else {
1906                 endp = strchr(s, ' ');
1907                 if (!endp)
1908                         die("Missing space after source: %s", command_buf.buf);
1909                 s_uq = xmalloc(endp - s + 1);
1910                 memcpy(s_uq, s, endp - s);
1911                 s_uq[endp - s] = 0;
1912         }
1913         s = s_uq;
1914
1915         endp++;
1916         if (!*endp)
1917                 die("Missing dest: %s", command_buf.buf);
1918
1919         d = endp;
1920         d_uq = unquote_c_style(d, &endp);
1921         if (d_uq) {
1922                 if (*endp)
1923                         die("Garbage after dest in: %s", command_buf.buf);
1924                 d = d_uq;
1925         }
1926
1927         memset(&leaf, 0, sizeof(leaf));
1928         if (rename)
1929                 tree_content_remove(&b->branch_tree, s, &leaf);
1930         else
1931                 tree_content_get(&b->branch_tree, s, &leaf);
1932         if (!leaf.versions[1].mode)
1933                 die("Path %s not in branch", s);
1934         tree_content_set(&b->branch_tree, d,
1935                 leaf.versions[1].sha1,
1936                 leaf.versions[1].mode,
1937                 leaf.tree);
1938
1939         free(s_uq);
1940         free(d_uq);
1941 }
1942
1943 static void file_change_deleteall(struct branch *b)
1944 {
1945         release_tree_content_recursive(b->branch_tree.tree);
1946         hashclr(b->branch_tree.versions[0].sha1);
1947         hashclr(b->branch_tree.versions[1].sha1);
1948         load_tree(&b->branch_tree);
1949 }
1950
1951 static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
1952 {
1953         if (!buf || size < 46)
1954                 die("Not a valid commit: %s", sha1_to_hex(b->sha1));
1955         if (memcmp("tree ", buf, 5)
1956                 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1957                 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1958         hashcpy(b->branch_tree.versions[0].sha1,
1959                 b->branch_tree.versions[1].sha1);
1960 }
1961
1962 static void cmd_from_existing(struct branch *b)
1963 {
1964         if (is_null_sha1(b->sha1)) {
1965                 hashclr(b->branch_tree.versions[0].sha1);
1966                 hashclr(b->branch_tree.versions[1].sha1);
1967         } else {
1968                 unsigned long size;
1969                 char *buf;
1970
1971                 buf = read_object_with_reference(b->sha1,
1972                         commit_type, &size, b->sha1);
1973                 cmd_from_commit(b, buf, size);
1974                 free(buf);
1975         }
1976 }
1977
1978 static int cmd_from(struct branch *b)
1979 {
1980         const char *from;
1981         struct branch *s;
1982
1983         if (prefixcmp(command_buf.buf, "from "))
1984                 return 0;
1985
1986         if (b->branch_tree.tree) {
1987                 release_tree_content_recursive(b->branch_tree.tree);
1988                 b->branch_tree.tree = NULL;
1989         }
1990
1991         from = strchr(command_buf.buf, ' ') + 1;
1992         s = lookup_branch(from);
1993         if (b == s)
1994                 die("Can't create a branch from itself: %s", b->name);
1995         else if (s) {
1996                 unsigned char *t = s->branch_tree.versions[1].sha1;
1997                 hashcpy(b->sha1, s->sha1);
1998                 hashcpy(b->branch_tree.versions[0].sha1, t);
1999                 hashcpy(b->branch_tree.versions[1].sha1, t);
2000         } else if (*from == ':') {
2001                 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
2002                 struct object_entry *oe = find_mark(idnum);
2003                 if (oe->type != OBJ_COMMIT)
2004                         die("Mark :%" PRIuMAX " not a commit", idnum);
2005                 hashcpy(b->sha1, oe->sha1);
2006                 if (oe->pack_id != MAX_PACK_ID) {
2007                         unsigned long size;
2008                         char *buf = gfi_unpack_entry(oe, &size);
2009                         cmd_from_commit(b, buf, size);
2010                         free(buf);
2011                 } else
2012                         cmd_from_existing(b);
2013         } else if (!get_sha1(from, b->sha1))
2014                 cmd_from_existing(b);
2015         else
2016                 die("Invalid ref name or SHA1 expression: %s", from);
2017
2018         read_next_command();
2019         return 1;
2020 }
2021
2022 static struct hash_list *cmd_merge(unsigned int *count)
2023 {
2024         struct hash_list *list = NULL, *n, *e = e;
2025         const char *from;
2026         struct branch *s;
2027
2028         *count = 0;
2029         while (!prefixcmp(command_buf.buf, "merge ")) {
2030                 from = strchr(command_buf.buf, ' ') + 1;
2031                 n = xmalloc(sizeof(*n));
2032                 s = lookup_branch(from);
2033                 if (s)
2034                         hashcpy(n->sha1, s->sha1);
2035                 else if (*from == ':') {
2036                         uintmax_t idnum = strtoumax(from + 1, NULL, 10);
2037                         struct object_entry *oe = find_mark(idnum);
2038                         if (oe->type != OBJ_COMMIT)
2039                                 die("Mark :%" PRIuMAX " not a commit", idnum);
2040                         hashcpy(n->sha1, oe->sha1);
2041                 } else if (!get_sha1(from, n->sha1)) {
2042                         unsigned long size;
2043                         char *buf = read_object_with_reference(n->sha1,
2044                                 commit_type, &size, n->sha1);
2045                         if (!buf || size < 46)
2046                                 die("Not a valid commit: %s", from);
2047                         free(buf);
2048                 } else
2049                         die("Invalid ref name or SHA1 expression: %s", from);
2050
2051                 n->next = NULL;
2052                 if (list)
2053                         e->next = n;
2054                 else
2055                         list = n;
2056                 e = n;
2057                 (*count)++;
2058                 read_next_command();
2059         }
2060         return list;
2061 }
2062
2063 static void cmd_new_commit(void)
2064 {
2065         struct branch *b;
2066         void *msg;
2067         size_t msglen;
2068         char *sp;
2069         char *author = NULL;
2070         char *committer = NULL;
2071         struct hash_list *merge_list = NULL;
2072         unsigned int merge_count;
2073
2074         /* Obtain the branch name from the rest of our command */
2075         sp = strchr(command_buf.buf, ' ') + 1;
2076         b = lookup_branch(sp);
2077         if (!b)
2078                 b = new_branch(sp);
2079
2080         read_next_command();
2081         cmd_mark();
2082         if (!prefixcmp(command_buf.buf, "author ")) {
2083                 author = parse_ident(command_buf.buf + 7);
2084                 read_next_command();
2085         }
2086         if (!prefixcmp(command_buf.buf, "committer ")) {
2087                 committer = parse_ident(command_buf.buf + 10);
2088                 read_next_command();
2089         }
2090         if (!committer)
2091                 die("Expected committer but didn't get one");
2092         msg = cmd_data(&msglen);
2093         read_next_command();
2094         cmd_from(b);
2095         merge_list = cmd_merge(&merge_count);
2096
2097         /* ensure the branch is active/loaded */
2098         if (!b->branch_tree.tree || !max_active_branches) {
2099                 unload_one_branch();
2100                 load_branch(b);
2101         }
2102
2103         /* file_change* */
2104         while (command_buf.len > 0) {
2105                 if (!prefixcmp(command_buf.buf, "M "))
2106                         file_change_m(b);
2107                 else if (!prefixcmp(command_buf.buf, "D "))
2108                         file_change_d(b);
2109                 else if (!prefixcmp(command_buf.buf, "R "))
2110                         file_change_cr(b, 1);
2111                 else if (!prefixcmp(command_buf.buf, "C "))
2112                         file_change_cr(b, 0);
2113                 else if (!strcmp("deleteall", command_buf.buf))
2114                         file_change_deleteall(b);
2115                 else {
2116                         unread_command_buf = 1;
2117                         break;
2118                 }
2119                 if (read_next_command() == EOF)
2120                         break;
2121         }
2122
2123         /* build the tree and the commit */
2124         store_tree(&b->branch_tree);
2125         hashcpy(b->branch_tree.versions[0].sha1,
2126                 b->branch_tree.versions[1].sha1);
2127         size_dbuf(&new_data, 114 + msglen
2128                 + merge_count * 49
2129                 + (author
2130                         ? strlen(author) + strlen(committer)
2131                         : 2 * strlen(committer)));
2132         sp = new_data.buffer;
2133         sp += sprintf(sp, "tree %s\n",
2134                 sha1_to_hex(b->branch_tree.versions[1].sha1));
2135         if (!is_null_sha1(b->sha1))
2136                 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
2137         while (merge_list) {
2138                 struct hash_list *next = merge_list->next;
2139                 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
2140                 free(merge_list);
2141                 merge_list = next;
2142         }
2143         sp += sprintf(sp, "author %s\n", author ? author : committer);
2144         sp += sprintf(sp, "committer %s\n", committer);
2145         *sp++ = '\n';
2146         memcpy(sp, msg, msglen);
2147         sp += msglen;
2148         free(author);
2149         free(committer);
2150         free(msg);
2151
2152         if (!store_object(OBJ_COMMIT,
2153                 new_data.buffer, sp - (char*)new_data.buffer,
2154                 NULL, b->sha1, next_mark))
2155                 b->pack_id = pack_id;
2156         b->last_commit = object_count_by_type[OBJ_COMMIT];
2157 }
2158
2159 static void cmd_new_tag(void)
2160 {
2161         char *sp;
2162         const char *from;
2163         char *tagger;
2164         struct branch *s;
2165         void *msg;
2166         size_t msglen;
2167         struct tag *t;
2168         uintmax_t from_mark = 0;
2169         unsigned char sha1[20];
2170
2171         /* Obtain the new tag name from the rest of our command */
2172         sp = strchr(command_buf.buf, ' ') + 1;
2173         t = pool_alloc(sizeof(struct tag));
2174         t->next_tag = NULL;
2175         t->name = pool_strdup(sp);
2176         if (last_tag)
2177                 last_tag->next_tag = t;
2178         else
2179                 first_tag = t;
2180         last_tag = t;
2181         read_next_command();
2182
2183         /* from ... */
2184         if (prefixcmp(command_buf.buf, "from "))
2185                 die("Expected from command, got %s", command_buf.buf);
2186         from = strchr(command_buf.buf, ' ') + 1;
2187         s = lookup_branch(from);
2188         if (s) {
2189                 hashcpy(sha1, s->sha1);
2190         } else if (*from == ':') {
2191                 struct object_entry *oe;
2192                 from_mark = strtoumax(from + 1, NULL, 10);
2193                 oe = find_mark(from_mark);
2194                 if (oe->type != OBJ_COMMIT)
2195                         die("Mark :%" PRIuMAX " not a commit", from_mark);
2196                 hashcpy(sha1, oe->sha1);
2197         } else if (!get_sha1(from, sha1)) {
2198                 unsigned long size;
2199                 char *buf;
2200
2201                 buf = read_object_with_reference(sha1,
2202                         commit_type, &size, sha1);
2203                 if (!buf || size < 46)
2204                         die("Not a valid commit: %s", from);
2205                 free(buf);
2206         } else
2207                 die("Invalid ref name or SHA1 expression: %s", from);
2208         read_next_command();
2209
2210         /* tagger ... */
2211         if (prefixcmp(command_buf.buf, "tagger "))
2212                 die("Expected tagger command, got %s", command_buf.buf);
2213         tagger = parse_ident(command_buf.buf + 7);
2214
2215         /* tag payload/message */
2216         read_next_command();
2217         msg = cmd_data(&msglen);
2218
2219         /* build the tag object */
2220         size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
2221         sp = new_data.buffer;
2222         sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
2223         sp += sprintf(sp, "type %s\n", commit_type);
2224         sp += sprintf(sp, "tag %s\n", t->name);
2225         sp += sprintf(sp, "tagger %s\n", tagger);
2226         *sp++ = '\n';
2227         memcpy(sp, msg, msglen);
2228         sp += msglen;
2229         free(tagger);
2230         free(msg);
2231
2232         if (store_object(OBJ_TAG, new_data.buffer,
2233                 sp - (char*)new_data.buffer,
2234                 NULL, t->sha1, 0))
2235                 t->pack_id = MAX_PACK_ID;
2236         else
2237                 t->pack_id = pack_id;
2238 }
2239
2240 static void cmd_reset_branch(void)
2241 {
2242         struct branch *b;
2243         char *sp;
2244
2245         /* Obtain the branch name from the rest of our command */
2246         sp = strchr(command_buf.buf, ' ') + 1;
2247         b = lookup_branch(sp);
2248         if (b) {
2249                 hashclr(b->sha1);
2250                 hashclr(b->branch_tree.versions[0].sha1);
2251                 hashclr(b->branch_tree.versions[1].sha1);
2252                 if (b->branch_tree.tree) {
2253                         release_tree_content_recursive(b->branch_tree.tree);
2254                         b->branch_tree.tree = NULL;
2255                 }
2256         }
2257         else
2258                 b = new_branch(sp);
2259         read_next_command();
2260         if (!cmd_from(b) && command_buf.len > 0)
2261                 unread_command_buf = 1;
2262 }
2263
2264 static void cmd_checkpoint(void)
2265 {
2266         if (object_count) {
2267                 cycle_packfile();
2268                 dump_branches();
2269                 dump_tags();
2270                 dump_marks();
2271         }
2272         skip_optional_lf();
2273 }
2274
2275 static void cmd_progress(void)
2276 {
2277         fwrite(command_buf.buf, 1, command_buf.len, stdout);
2278         fputc('\n', stdout);
2279         fflush(stdout);
2280         skip_optional_lf();
2281 }
2282
2283 static void import_marks(const char *input_file)
2284 {
2285         char line[512];
2286         FILE *f = fopen(input_file, "r");
2287         if (!f)
2288                 die("cannot read %s: %s", input_file, strerror(errno));
2289         while (fgets(line, sizeof(line), f)) {
2290                 uintmax_t mark;
2291                 char *end;
2292                 unsigned char sha1[20];
2293                 struct object_entry *e;
2294
2295                 end = strchr(line, '\n');
2296                 if (line[0] != ':' || !end)
2297                         die("corrupt mark line: %s", line);
2298                 *end = 0;
2299                 mark = strtoumax(line + 1, &end, 10);
2300                 if (!mark || end == line + 1
2301                         || *end != ' ' || get_sha1(end + 1, sha1))
2302                         die("corrupt mark line: %s", line);
2303                 e = find_object(sha1);
2304                 if (!e) {
2305                         enum object_type type = sha1_object_info(sha1, NULL);
2306                         if (type < 0)
2307                                 die("object not found: %s", sha1_to_hex(sha1));
2308                         e = insert_object(sha1);
2309                         e->type = type;
2310                         e->pack_id = MAX_PACK_ID;
2311                         e->offset = 1; /* just not zero! */
2312                 }
2313                 insert_mark(mark, e);
2314         }
2315         fclose(f);
2316 }
2317
2318 static const char fast_import_usage[] =
2319 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2320
2321 int main(int argc, const char **argv)
2322 {
2323         unsigned int i, show_stats = 1;
2324
2325         git_config(git_default_config);
2326         alloc_objects(object_entry_alloc);
2327         strbuf_init(&command_buf, 0);
2328         atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2329         branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2330         avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2331         marks = pool_calloc(1, sizeof(struct mark_set));
2332
2333         for (i = 1; i < argc; i++) {
2334                 const char *a = argv[i];
2335
2336                 if (*a != '-' || !strcmp(a, "--"))
2337                         break;
2338                 else if (!prefixcmp(a, "--date-format=")) {
2339                         const char *fmt = a + 14;
2340                         if (!strcmp(fmt, "raw"))
2341                                 whenspec = WHENSPEC_RAW;
2342                         else if (!strcmp(fmt, "rfc2822"))
2343                                 whenspec = WHENSPEC_RFC2822;
2344                         else if (!strcmp(fmt, "now"))
2345                                 whenspec = WHENSPEC_NOW;
2346                         else
2347                                 die("unknown --date-format argument %s", fmt);
2348                 }
2349                 else if (!prefixcmp(a, "--max-pack-size="))
2350                         max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
2351                 else if (!prefixcmp(a, "--depth="))
2352                         max_depth = strtoul(a + 8, NULL, 0);
2353                 else if (!prefixcmp(a, "--active-branches="))
2354                         max_active_branches = strtoul(a + 18, NULL, 0);
2355                 else if (!prefixcmp(a, "--import-marks="))
2356                         import_marks(a + 15);
2357                 else if (!prefixcmp(a, "--export-marks="))
2358                         mark_file = a + 15;
2359                 else if (!prefixcmp(a, "--export-pack-edges=")) {
2360                         if (pack_edges)
2361                                 fclose(pack_edges);
2362                         pack_edges = fopen(a + 20, "a");
2363                         if (!pack_edges)
2364                                 die("Cannot open %s: %s", a + 20, strerror(errno));
2365                 } else if (!strcmp(a, "--force"))
2366                         force_update = 1;
2367                 else if (!strcmp(a, "--quiet"))
2368                         show_stats = 0;
2369                 else if (!strcmp(a, "--stats"))
2370                         show_stats = 1;
2371                 else
2372                         die("unknown option %s", a);
2373         }
2374         if (i != argc)
2375                 usage(fast_import_usage);
2376
2377         rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
2378         for (i = 0; i < (cmd_save - 1); i++)
2379                 rc_free[i].next = &rc_free[i + 1];
2380         rc_free[cmd_save - 1].next = NULL;
2381
2382         prepare_packed_git();
2383         start_packfile();
2384         set_die_routine(die_nicely);
2385         while (read_next_command() != EOF) {
2386                 if (!strcmp("blob", command_buf.buf))
2387                         cmd_new_blob();
2388                 else if (!prefixcmp(command_buf.buf, "commit "))
2389                         cmd_new_commit();
2390                 else if (!prefixcmp(command_buf.buf, "tag "))
2391                         cmd_new_tag();
2392                 else if (!prefixcmp(command_buf.buf, "reset "))
2393                         cmd_reset_branch();
2394                 else if (!strcmp("checkpoint", command_buf.buf))
2395                         cmd_checkpoint();
2396                 else if (!prefixcmp(command_buf.buf, "progress "))
2397                         cmd_progress();
2398                 else
2399                         die("Unsupported command: %s", command_buf.buf);
2400         }
2401         end_packfile();
2402
2403         dump_branches();
2404         dump_tags();
2405         unkeep_all_packs();
2406         dump_marks();
2407
2408         if (pack_edges)
2409                 fclose(pack_edges);
2410
2411         if (show_stats) {
2412                 uintmax_t total_count = 0, duplicate_count = 0;
2413                 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
2414                         total_count += object_count_by_type[i];
2415                 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
2416                         duplicate_count += duplicate_count_by_type[i];
2417
2418                 fprintf(stderr, "%s statistics:\n", argv[0]);
2419                 fprintf(stderr, "---------------------------------------------------------------------\n");
2420                 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
2421                 fprintf(stderr, "Total objects:   %10" PRIuMAX " (%10" PRIuMAX " duplicates                  )\n", total_count, duplicate_count);
2422                 fprintf(stderr, "      blobs  :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
2423                 fprintf(stderr, "      trees  :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
2424                 fprintf(stderr, "      commits:   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
2425                 fprintf(stderr, "      tags   :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
2426                 fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
2427                 fprintf(stderr, "      marks:     %10" PRIuMAX " (%10" PRIuMAX " unique    )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
2428                 fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
2429                 fprintf(stderr, "Memory total:    %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
2430                 fprintf(stderr, "       pools:    %10lu KiB\n", (unsigned long)(total_allocd/1024));
2431                 fprintf(stderr, "     objects:    %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
2432                 fprintf(stderr, "---------------------------------------------------------------------\n");
2433                 pack_report();
2434                 fprintf(stderr, "---------------------------------------------------------------------\n");
2435                 fprintf(stderr, "\n");
2436         }
2437
2438         return failure ? 1 : 0;
2439 }