]> asedeno.scripts.mit.edu Git - git.git/blob - fast-import.c
Include recent command history in fast-import crash reports
[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 "strbuf.h"
153 #include "quote.h"
154
155 #define PACK_ID_BITS 16
156 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
157
158 struct object_entry
159 {
160         struct object_entry *next;
161         uint32_t offset;
162         unsigned type : TYPE_BITS;
163         unsigned pack_id : PACK_ID_BITS;
164         unsigned char sha1[20];
165 };
166
167 struct object_entry_pool
168 {
169         struct object_entry_pool *next_pool;
170         struct object_entry *next_free;
171         struct object_entry *end;
172         struct object_entry entries[FLEX_ARRAY]; /* more */
173 };
174
175 struct mark_set
176 {
177         union {
178                 struct object_entry *marked[1024];
179                 struct mark_set *sets[1024];
180         } data;
181         unsigned int shift;
182 };
183
184 struct last_object
185 {
186         void *data;
187         unsigned long len;
188         uint32_t offset;
189         unsigned int depth;
190         unsigned no_free:1;
191 };
192
193 struct mem_pool
194 {
195         struct mem_pool *next_pool;
196         char *next_free;
197         char *end;
198         char space[FLEX_ARRAY]; /* more */
199 };
200
201 struct atom_str
202 {
203         struct atom_str *next_atom;
204         unsigned short str_len;
205         char str_dat[FLEX_ARRAY]; /* more */
206 };
207
208 struct tree_content;
209 struct tree_entry
210 {
211         struct tree_content *tree;
212         struct atom_str* name;
213         struct tree_entry_ms
214         {
215                 uint16_t mode;
216                 unsigned char sha1[20];
217         } versions[2];
218 };
219
220 struct tree_content
221 {
222         unsigned int entry_capacity; /* must match avail_tree_content */
223         unsigned int entry_count;
224         unsigned int delta_depth;
225         struct tree_entry *entries[FLEX_ARRAY]; /* more */
226 };
227
228 struct avail_tree_content
229 {
230         unsigned int entry_capacity; /* must match tree_content */
231         struct avail_tree_content *next_avail;
232 };
233
234 struct branch
235 {
236         struct branch *table_next_branch;
237         struct branch *active_next_branch;
238         const char *name;
239         struct tree_entry branch_tree;
240         uintmax_t last_commit;
241         unsigned active : 1;
242         unsigned pack_id : PACK_ID_BITS;
243         unsigned char sha1[20];
244 };
245
246 struct tag
247 {
248         struct tag *next_tag;
249         const char *name;
250         unsigned int pack_id;
251         unsigned char sha1[20];
252 };
253
254 struct dbuf
255 {
256         void *buffer;
257         size_t capacity;
258 };
259
260 struct hash_list
261 {
262         struct hash_list *next;
263         unsigned char sha1[20];
264 };
265
266 typedef enum {
267         WHENSPEC_RAW = 1,
268         WHENSPEC_RFC2822,
269         WHENSPEC_NOW,
270 } whenspec_type;
271
272 struct recent_command
273 {
274         struct recent_command *prev;
275         struct recent_command *next;
276         char *buf;
277 };
278
279 /* Configured limits on output */
280 static unsigned long max_depth = 10;
281 static off_t max_packsize = (1LL << 32) - 1;
282 static int force_update;
283
284 /* Stats and misc. counters */
285 static uintmax_t alloc_count;
286 static uintmax_t marks_set_count;
287 static uintmax_t object_count_by_type[1 << TYPE_BITS];
288 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
289 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
290 static unsigned long object_count;
291 static unsigned long branch_count;
292 static unsigned long branch_load_count;
293 static int failure;
294 static FILE *pack_edges;
295
296 /* Memory pools */
297 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
298 static size_t total_allocd;
299 static struct mem_pool *mem_pool;
300
301 /* Atom management */
302 static unsigned int atom_table_sz = 4451;
303 static unsigned int atom_cnt;
304 static struct atom_str **atom_table;
305
306 /* The .pack file being generated */
307 static unsigned int pack_id;
308 static struct packed_git *pack_data;
309 static struct packed_git **all_packs;
310 static unsigned long pack_size;
311
312 /* Table of objects we've written. */
313 static unsigned int object_entry_alloc = 5000;
314 static struct object_entry_pool *blocks;
315 static struct object_entry *object_table[1 << 16];
316 static struct mark_set *marks;
317 static const char* mark_file;
318
319 /* Our last blob */
320 static struct last_object last_blob;
321
322 /* Tree management */
323 static unsigned int tree_entry_alloc = 1000;
324 static void *avail_tree_entry;
325 static unsigned int avail_tree_table_sz = 100;
326 static struct avail_tree_content **avail_tree_table;
327 static struct dbuf old_tree;
328 static struct dbuf new_tree;
329
330 /* Branch data */
331 static unsigned long max_active_branches = 5;
332 static unsigned long cur_active_branches;
333 static unsigned long branch_table_sz = 1039;
334 static struct branch **branch_table;
335 static struct branch *active_branches;
336
337 /* Tag data */
338 static struct tag *first_tag;
339 static struct tag *last_tag;
340
341 /* Input stream parsing */
342 static whenspec_type whenspec = WHENSPEC_RAW;
343 static struct strbuf command_buf;
344 static int unread_command_buf;
345 static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
346 static struct recent_command *cmd_tail = &cmd_hist;
347 static struct recent_command *rc_free;
348 static unsigned int cmd_save = 100;
349 static uintmax_t next_mark;
350 static struct dbuf new_data;
351
352 static void write_branch_report(FILE *rpt, struct branch *b)
353 {
354         fprintf(rpt, "%s:\n", b->name);
355
356         fprintf(rpt, "  status      :");
357         if (b->active)
358                 fputs(" active", rpt);
359         if (b->branch_tree.tree)
360                 fputs(" loaded", rpt);
361         if (is_null_sha1(b->branch_tree.versions[1].sha1))
362                 fputs(" dirty", rpt);
363         fputc('\n', rpt);
364
365         fprintf(rpt, "  tip commit  : %s\n", sha1_to_hex(b->sha1));
366         fprintf(rpt, "  old tree    : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1));
367         fprintf(rpt, "  cur tree    : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1));
368         fprintf(rpt, "  commit clock: %" PRIuMAX "\n", b->last_commit);
369
370         fputs("  last pack   : ", rpt);
371         if (b->pack_id < MAX_PACK_ID)
372                 fprintf(rpt, "%u", b->pack_id);
373         fputc('\n', rpt);
374
375         fputc('\n', rpt);
376 }
377
378 static void write_crash_report(const char *err, va_list params)
379 {
380         char *loc = git_path("fast_import_crash_%d", getpid());
381         FILE *rpt = fopen(loc, "w");
382         struct branch *b;
383         unsigned long lu;
384         struct recent_command *rc;
385
386         if (!rpt) {
387                 error("can't write crash report %s: %s", loc, strerror(errno));
388                 return;
389         }
390
391         fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
392
393         fprintf(rpt, "fast-import crash report:\n");
394         fprintf(rpt, "    fast-import process: %d\n", getpid());
395         fprintf(rpt, "    parent process     : %d\n", getppid());
396         fprintf(rpt, "    at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
397         fputc('\n', rpt);
398
399         fputs("fatal: ", rpt);
400         vfprintf(rpt, err, params);
401         fputc('\n', rpt);
402
403         fputc('\n', rpt);
404         fputs("Most Recent Commands Before Crash\n", rpt);
405         fputs("---------------------------------\n", rpt);
406         for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
407                 if (rc->next == &cmd_hist)
408                         fputs("* ", rpt);
409                 else
410                         fputs("  ", rpt);
411                 fputs(rc->buf, rpt);
412                 fputc('\n', rpt);
413         }
414
415         fputc('\n', rpt);
416         fputs("Active Branch LRU\n", rpt);
417         fputs("-----------------\n", rpt);
418         fprintf(rpt, "    active_branches = %lu cur, %lu max\n",
419                 cur_active_branches,
420                 max_active_branches);
421         fputc('\n', rpt);
422         fputs("  pos  clock name\n", rpt);
423         fputs("  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
424         for (b = active_branches, lu = 0; b; b = b->active_next_branch)
425                 fprintf(rpt, "  %2lu) %6" PRIuMAX" %s\n",
426                         ++lu, b->last_commit, b->name);
427
428         fputc('\n', rpt);
429         fputs("Inactive Branches\n", rpt);
430         fputs("-----------------\n", rpt);
431         for (lu = 0; lu < branch_table_sz; lu++) {
432                 for (b = branch_table[lu]; b; b = b->table_next_branch)
433                         write_branch_report(rpt, b);
434         }
435
436         fputc('\n', rpt);
437         fputs("-------------------\n", rpt);
438         fputs("END OF CRASH REPORT\n", rpt);
439         fclose(rpt);
440 }
441
442 static NORETURN void die_nicely(const char *err, va_list params)
443 {
444         static int zombie;
445
446         fputs("fatal: ", stderr);
447         vfprintf(stderr, err, params);
448         fputc('\n', stderr);
449
450         if (!zombie) {
451                 zombie = 1;
452                 write_crash_report(err, params);
453         }
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 void read_next_command(void)
1588 {
1589         do {
1590                 if (unread_command_buf) {
1591                         unread_command_buf = 0;
1592                         if (command_buf.eof)
1593                                 return;
1594                 } else {
1595                         struct recent_command *rc;
1596
1597                         command_buf.buf = NULL;
1598                         read_line(&command_buf, stdin, '\n');
1599                         if (command_buf.eof)
1600                                 return;
1601
1602                         rc = rc_free;
1603                         if (rc)
1604                                 rc_free = rc->next;
1605                         else {
1606                                 rc = cmd_hist.next;
1607                                 cmd_hist.next = rc->next;
1608                                 cmd_hist.next->prev = &cmd_hist;
1609                                 free(rc->buf);
1610                         }
1611
1612                         rc->buf = command_buf.buf;
1613                         rc->prev = cmd_tail;
1614                         rc->next = cmd_hist.prev;
1615                         rc->prev->next = rc;
1616                         cmd_tail = rc;
1617                 }
1618         } while (command_buf.buf[0] == '#');
1619 }
1620
1621 static void skip_optional_lf()
1622 {
1623         int term_char = fgetc(stdin);
1624         if (term_char != '\n' && term_char != EOF)
1625                 ungetc(term_char, stdin);
1626 }
1627
1628 static void cmd_mark(void)
1629 {
1630         if (!prefixcmp(command_buf.buf, "mark :")) {
1631                 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1632                 read_next_command();
1633         }
1634         else
1635                 next_mark = 0;
1636 }
1637
1638 static void *cmd_data (size_t *size)
1639 {
1640         size_t length;
1641         char *buffer;
1642
1643         if (prefixcmp(command_buf.buf, "data "))
1644                 die("Expected 'data n' command, found: %s", command_buf.buf);
1645
1646         if (!prefixcmp(command_buf.buf + 5, "<<")) {
1647                 char *term = xstrdup(command_buf.buf + 5 + 2);
1648                 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1649                 length = 0;
1650                 buffer = xmalloc(sz);
1651                 command_buf.buf = NULL;
1652                 for (;;) {
1653                         read_line(&command_buf, stdin, '\n');
1654                         if (command_buf.eof)
1655                                 die("EOF in data (terminator '%s' not found)", term);
1656                         if (term_len == command_buf.len
1657                                 && !strcmp(term, command_buf.buf))
1658                                 break;
1659                         ALLOC_GROW(buffer, length + command_buf.len, sz);
1660                         memcpy(buffer + length,
1661                                 command_buf.buf,
1662                                 command_buf.len - 1);
1663                         length += command_buf.len - 1;
1664                         buffer[length++] = '\n';
1665                 }
1666                 free(term);
1667         }
1668         else {
1669                 size_t n = 0;
1670                 length = strtoul(command_buf.buf + 5, NULL, 10);
1671                 buffer = xmalloc(length);
1672                 while (n < length) {
1673                         size_t s = fread(buffer + n, 1, length - n, stdin);
1674                         if (!s && feof(stdin))
1675                                 die("EOF in data (%lu bytes remaining)",
1676                                         (unsigned long)(length - n));
1677                         n += s;
1678                 }
1679         }
1680
1681         skip_optional_lf();
1682         *size = length;
1683         return buffer;
1684 }
1685
1686 static int validate_raw_date(const char *src, char *result, int maxlen)
1687 {
1688         const char *orig_src = src;
1689         char *endp, sign;
1690
1691         strtoul(src, &endp, 10);
1692         if (endp == src || *endp != ' ')
1693                 return -1;
1694
1695         src = endp + 1;
1696         if (*src != '-' && *src != '+')
1697                 return -1;
1698         sign = *src;
1699
1700         strtoul(src + 1, &endp, 10);
1701         if (endp == src || *endp || (endp - orig_src) >= maxlen)
1702                 return -1;
1703
1704         strcpy(result, orig_src);
1705         return 0;
1706 }
1707
1708 static char *parse_ident(const char *buf)
1709 {
1710         const char *gt;
1711         size_t name_len;
1712         char *ident;
1713
1714         gt = strrchr(buf, '>');
1715         if (!gt)
1716                 die("Missing > in ident string: %s", buf);
1717         gt++;
1718         if (*gt != ' ')
1719                 die("Missing space after > in ident string: %s", buf);
1720         gt++;
1721         name_len = gt - buf;
1722         ident = xmalloc(name_len + 24);
1723         strncpy(ident, buf, name_len);
1724
1725         switch (whenspec) {
1726         case WHENSPEC_RAW:
1727                 if (validate_raw_date(gt, ident + name_len, 24) < 0)
1728                         die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1729                 break;
1730         case WHENSPEC_RFC2822:
1731                 if (parse_date(gt, ident + name_len, 24) < 0)
1732                         die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1733                 break;
1734         case WHENSPEC_NOW:
1735                 if (strcmp("now", gt))
1736                         die("Date in ident must be 'now': %s", buf);
1737                 datestamp(ident + name_len, 24);
1738                 break;
1739         }
1740
1741         return ident;
1742 }
1743
1744 static void cmd_new_blob(void)
1745 {
1746         size_t l;
1747         void *d;
1748
1749         read_next_command();
1750         cmd_mark();
1751         d = cmd_data(&l);
1752
1753         if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1754                 free(d);
1755 }
1756
1757 static void unload_one_branch(void)
1758 {
1759         while (cur_active_branches
1760                 && cur_active_branches >= max_active_branches) {
1761                 uintmax_t min_commit = ULONG_MAX;
1762                 struct branch *e, *l = NULL, *p = NULL;
1763
1764                 for (e = active_branches; e; e = e->active_next_branch) {
1765                         if (e->last_commit < min_commit) {
1766                                 p = l;
1767                                 min_commit = e->last_commit;
1768                         }
1769                         l = e;
1770                 }
1771
1772                 if (p) {
1773                         e = p->active_next_branch;
1774                         p->active_next_branch = e->active_next_branch;
1775                 } else {
1776                         e = active_branches;
1777                         active_branches = e->active_next_branch;
1778                 }
1779                 e->active = 0;
1780                 e->active_next_branch = NULL;
1781                 if (e->branch_tree.tree) {
1782                         release_tree_content_recursive(e->branch_tree.tree);
1783                         e->branch_tree.tree = NULL;
1784                 }
1785                 cur_active_branches--;
1786         }
1787 }
1788
1789 static void load_branch(struct branch *b)
1790 {
1791         load_tree(&b->branch_tree);
1792         if (!b->active) {
1793                 b->active = 1;
1794                 b->active_next_branch = active_branches;
1795                 active_branches = b;
1796                 cur_active_branches++;
1797                 branch_load_count++;
1798         }
1799 }
1800
1801 static void file_change_m(struct branch *b)
1802 {
1803         const char *p = command_buf.buf + 2;
1804         char *p_uq;
1805         const char *endp;
1806         struct object_entry *oe = oe;
1807         unsigned char sha1[20];
1808         uint16_t mode, inline_data = 0;
1809
1810         p = get_mode(p, &mode);
1811         if (!p)
1812                 die("Corrupt mode: %s", command_buf.buf);
1813         switch (mode) {
1814         case S_IFREG | 0644:
1815         case S_IFREG | 0755:
1816         case S_IFLNK:
1817         case 0644:
1818         case 0755:
1819                 /* ok */
1820                 break;
1821         default:
1822                 die("Corrupt mode: %s", command_buf.buf);
1823         }
1824
1825         if (*p == ':') {
1826                 char *x;
1827                 oe = find_mark(strtoumax(p + 1, &x, 10));
1828                 hashcpy(sha1, oe->sha1);
1829                 p = x;
1830         } else if (!prefixcmp(p, "inline")) {
1831                 inline_data = 1;
1832                 p += 6;
1833         } else {
1834                 if (get_sha1_hex(p, sha1))
1835                         die("Invalid SHA1: %s", command_buf.buf);
1836                 oe = find_object(sha1);
1837                 p += 40;
1838         }
1839         if (*p++ != ' ')
1840                 die("Missing space after SHA1: %s", command_buf.buf);
1841
1842         p_uq = unquote_c_style(p, &endp);
1843         if (p_uq) {
1844                 if (*endp)
1845                         die("Garbage after path in: %s", command_buf.buf);
1846                 p = p_uq;
1847         }
1848
1849         if (inline_data) {
1850                 size_t l;
1851                 void *d;
1852                 if (!p_uq)
1853                         p = p_uq = xstrdup(p);
1854                 read_next_command();
1855                 d = cmd_data(&l);
1856                 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1857                         free(d);
1858         } else if (oe) {
1859                 if (oe->type != OBJ_BLOB)
1860                         die("Not a blob (actually a %s): %s",
1861                                 command_buf.buf, typename(oe->type));
1862         } else {
1863                 enum object_type type = sha1_object_info(sha1, NULL);
1864                 if (type < 0)
1865                         die("Blob not found: %s", command_buf.buf);
1866                 if (type != OBJ_BLOB)
1867                         die("Not a blob (actually a %s): %s",
1868                             typename(type), command_buf.buf);
1869         }
1870
1871         tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
1872         free(p_uq);
1873 }
1874
1875 static void file_change_d(struct branch *b)
1876 {
1877         const char *p = command_buf.buf + 2;
1878         char *p_uq;
1879         const char *endp;
1880
1881         p_uq = unquote_c_style(p, &endp);
1882         if (p_uq) {
1883                 if (*endp)
1884                         die("Garbage after path in: %s", command_buf.buf);
1885                 p = p_uq;
1886         }
1887         tree_content_remove(&b->branch_tree, p, NULL);
1888         free(p_uq);
1889 }
1890
1891 static void file_change_cr(struct branch *b, int rename)
1892 {
1893         const char *s, *d;
1894         char *s_uq, *d_uq;
1895         const char *endp;
1896         struct tree_entry leaf;
1897
1898         s = command_buf.buf + 2;
1899         s_uq = unquote_c_style(s, &endp);
1900         if (s_uq) {
1901                 if (*endp != ' ')
1902                         die("Missing space after source: %s", command_buf.buf);
1903         }
1904         else {
1905                 endp = strchr(s, ' ');
1906                 if (!endp)
1907                         die("Missing space after source: %s", command_buf.buf);
1908                 s_uq = xmalloc(endp - s + 1);
1909                 memcpy(s_uq, s, endp - s);
1910                 s_uq[endp - s] = 0;
1911         }
1912         s = s_uq;
1913
1914         endp++;
1915         if (!*endp)
1916                 die("Missing dest: %s", command_buf.buf);
1917
1918         d = endp;
1919         d_uq = unquote_c_style(d, &endp);
1920         if (d_uq) {
1921                 if (*endp)
1922                         die("Garbage after dest in: %s", command_buf.buf);
1923                 d = d_uq;
1924         }
1925
1926         memset(&leaf, 0, sizeof(leaf));
1927         if (rename)
1928                 tree_content_remove(&b->branch_tree, s, &leaf);
1929         else
1930                 tree_content_get(&b->branch_tree, s, &leaf);
1931         if (!leaf.versions[1].mode)
1932                 die("Path %s not in branch", s);
1933         tree_content_set(&b->branch_tree, d,
1934                 leaf.versions[1].sha1,
1935                 leaf.versions[1].mode,
1936                 leaf.tree);
1937
1938         free(s_uq);
1939         free(d_uq);
1940 }
1941
1942 static void file_change_deleteall(struct branch *b)
1943 {
1944         release_tree_content_recursive(b->branch_tree.tree);
1945         hashclr(b->branch_tree.versions[0].sha1);
1946         hashclr(b->branch_tree.versions[1].sha1);
1947         load_tree(&b->branch_tree);
1948 }
1949
1950 static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
1951 {
1952         if (!buf || size < 46)
1953                 die("Not a valid commit: %s", sha1_to_hex(b->sha1));
1954         if (memcmp("tree ", buf, 5)
1955                 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1956                 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1957         hashcpy(b->branch_tree.versions[0].sha1,
1958                 b->branch_tree.versions[1].sha1);
1959 }
1960
1961 static void cmd_from_existing(struct branch *b)
1962 {
1963         if (is_null_sha1(b->sha1)) {
1964                 hashclr(b->branch_tree.versions[0].sha1);
1965                 hashclr(b->branch_tree.versions[1].sha1);
1966         } else {
1967                 unsigned long size;
1968                 char *buf;
1969
1970                 buf = read_object_with_reference(b->sha1,
1971                         commit_type, &size, b->sha1);
1972                 cmd_from_commit(b, buf, size);
1973                 free(buf);
1974         }
1975 }
1976
1977 static int cmd_from(struct branch *b)
1978 {
1979         const char *from;
1980         struct branch *s;
1981
1982         if (prefixcmp(command_buf.buf, "from "))
1983                 return 0;
1984
1985         if (b->branch_tree.tree) {
1986                 release_tree_content_recursive(b->branch_tree.tree);
1987                 b->branch_tree.tree = NULL;
1988         }
1989
1990         from = strchr(command_buf.buf, ' ') + 1;
1991         s = lookup_branch(from);
1992         if (b == s)
1993                 die("Can't create a branch from itself: %s", b->name);
1994         else if (s) {
1995                 unsigned char *t = s->branch_tree.versions[1].sha1;
1996                 hashcpy(b->sha1, s->sha1);
1997                 hashcpy(b->branch_tree.versions[0].sha1, t);
1998                 hashcpy(b->branch_tree.versions[1].sha1, t);
1999         } else if (*from == ':') {
2000                 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
2001                 struct object_entry *oe = find_mark(idnum);
2002                 if (oe->type != OBJ_COMMIT)
2003                         die("Mark :%" PRIuMAX " not a commit", idnum);
2004                 hashcpy(b->sha1, oe->sha1);
2005                 if (oe->pack_id != MAX_PACK_ID) {
2006                         unsigned long size;
2007                         char *buf = gfi_unpack_entry(oe, &size);
2008                         cmd_from_commit(b, buf, size);
2009                         free(buf);
2010                 } else
2011                         cmd_from_existing(b);
2012         } else if (!get_sha1(from, b->sha1))
2013                 cmd_from_existing(b);
2014         else
2015                 die("Invalid ref name or SHA1 expression: %s", from);
2016
2017         read_next_command();
2018         return 1;
2019 }
2020
2021 static struct hash_list *cmd_merge(unsigned int *count)
2022 {
2023         struct hash_list *list = NULL, *n, *e = e;
2024         const char *from;
2025         struct branch *s;
2026
2027         *count = 0;
2028         while (!prefixcmp(command_buf.buf, "merge ")) {
2029                 from = strchr(command_buf.buf, ' ') + 1;
2030                 n = xmalloc(sizeof(*n));
2031                 s = lookup_branch(from);
2032                 if (s)
2033                         hashcpy(n->sha1, s->sha1);
2034                 else if (*from == ':') {
2035                         uintmax_t idnum = strtoumax(from + 1, NULL, 10);
2036                         struct object_entry *oe = find_mark(idnum);
2037                         if (oe->type != OBJ_COMMIT)
2038                                 die("Mark :%" PRIuMAX " not a commit", idnum);
2039                         hashcpy(n->sha1, oe->sha1);
2040                 } else if (!get_sha1(from, n->sha1)) {
2041                         unsigned long size;
2042                         char *buf = read_object_with_reference(n->sha1,
2043                                 commit_type, &size, n->sha1);
2044                         if (!buf || size < 46)
2045                                 die("Not a valid commit: %s", from);
2046                         free(buf);
2047                 } else
2048                         die("Invalid ref name or SHA1 expression: %s", from);
2049
2050                 n->next = NULL;
2051                 if (list)
2052                         e->next = n;
2053                 else
2054                         list = n;
2055                 e = n;
2056                 (*count)++;
2057                 read_next_command();
2058         }
2059         return list;
2060 }
2061
2062 static void cmd_new_commit(void)
2063 {
2064         struct branch *b;
2065         void *msg;
2066         size_t msglen;
2067         char *sp;
2068         char *author = NULL;
2069         char *committer = NULL;
2070         struct hash_list *merge_list = NULL;
2071         unsigned int merge_count;
2072
2073         /* Obtain the branch name from the rest of our command */
2074         sp = strchr(command_buf.buf, ' ') + 1;
2075         b = lookup_branch(sp);
2076         if (!b)
2077                 b = new_branch(sp);
2078
2079         read_next_command();
2080         cmd_mark();
2081         if (!prefixcmp(command_buf.buf, "author ")) {
2082                 author = parse_ident(command_buf.buf + 7);
2083                 read_next_command();
2084         }
2085         if (!prefixcmp(command_buf.buf, "committer ")) {
2086                 committer = parse_ident(command_buf.buf + 10);
2087                 read_next_command();
2088         }
2089         if (!committer)
2090                 die("Expected committer but didn't get one");
2091         msg = cmd_data(&msglen);
2092         read_next_command();
2093         cmd_from(b);
2094         merge_list = cmd_merge(&merge_count);
2095
2096         /* ensure the branch is active/loaded */
2097         if (!b->branch_tree.tree || !max_active_branches) {
2098                 unload_one_branch();
2099                 load_branch(b);
2100         }
2101
2102         /* file_change* */
2103         while (!command_buf.eof && command_buf.len > 1) {
2104                 if (!prefixcmp(command_buf.buf, "M "))
2105                         file_change_m(b);
2106                 else if (!prefixcmp(command_buf.buf, "D "))
2107                         file_change_d(b);
2108                 else if (!prefixcmp(command_buf.buf, "R "))
2109                         file_change_cr(b, 1);
2110                 else if (!prefixcmp(command_buf.buf, "C "))
2111                         file_change_cr(b, 0);
2112                 else if (!strcmp("deleteall", command_buf.buf))
2113                         file_change_deleteall(b);
2114                 else {
2115                         unread_command_buf = 1;
2116                         break;
2117                 }
2118                 read_next_command();
2119         }
2120
2121         /* build the tree and the commit */
2122         store_tree(&b->branch_tree);
2123         hashcpy(b->branch_tree.versions[0].sha1,
2124                 b->branch_tree.versions[1].sha1);
2125         size_dbuf(&new_data, 114 + msglen
2126                 + merge_count * 49
2127                 + (author
2128                         ? strlen(author) + strlen(committer)
2129                         : 2 * strlen(committer)));
2130         sp = new_data.buffer;
2131         sp += sprintf(sp, "tree %s\n",
2132                 sha1_to_hex(b->branch_tree.versions[1].sha1));
2133         if (!is_null_sha1(b->sha1))
2134                 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
2135         while (merge_list) {
2136                 struct hash_list *next = merge_list->next;
2137                 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
2138                 free(merge_list);
2139                 merge_list = next;
2140         }
2141         sp += sprintf(sp, "author %s\n", author ? author : committer);
2142         sp += sprintf(sp, "committer %s\n", committer);
2143         *sp++ = '\n';
2144         memcpy(sp, msg, msglen);
2145         sp += msglen;
2146         free(author);
2147         free(committer);
2148         free(msg);
2149
2150         if (!store_object(OBJ_COMMIT,
2151                 new_data.buffer, sp - (char*)new_data.buffer,
2152                 NULL, b->sha1, next_mark))
2153                 b->pack_id = pack_id;
2154         b->last_commit = object_count_by_type[OBJ_COMMIT];
2155 }
2156
2157 static void cmd_new_tag(void)
2158 {
2159         char *sp;
2160         const char *from;
2161         char *tagger;
2162         struct branch *s;
2163         void *msg;
2164         size_t msglen;
2165         struct tag *t;
2166         uintmax_t from_mark = 0;
2167         unsigned char sha1[20];
2168
2169         /* Obtain the new tag name from the rest of our command */
2170         sp = strchr(command_buf.buf, ' ') + 1;
2171         t = pool_alloc(sizeof(struct tag));
2172         t->next_tag = NULL;
2173         t->name = pool_strdup(sp);
2174         if (last_tag)
2175                 last_tag->next_tag = t;
2176         else
2177                 first_tag = t;
2178         last_tag = t;
2179         read_next_command();
2180
2181         /* from ... */
2182         if (prefixcmp(command_buf.buf, "from "))
2183                 die("Expected from command, got %s", command_buf.buf);
2184         from = strchr(command_buf.buf, ' ') + 1;
2185         s = lookup_branch(from);
2186         if (s) {
2187                 hashcpy(sha1, s->sha1);
2188         } else if (*from == ':') {
2189                 struct object_entry *oe;
2190                 from_mark = strtoumax(from + 1, NULL, 10);
2191                 oe = find_mark(from_mark);
2192                 if (oe->type != OBJ_COMMIT)
2193                         die("Mark :%" PRIuMAX " not a commit", from_mark);
2194                 hashcpy(sha1, oe->sha1);
2195         } else if (!get_sha1(from, sha1)) {
2196                 unsigned long size;
2197                 char *buf;
2198
2199                 buf = read_object_with_reference(sha1,
2200                         commit_type, &size, sha1);
2201                 if (!buf || size < 46)
2202                         die("Not a valid commit: %s", from);
2203                 free(buf);
2204         } else
2205                 die("Invalid ref name or SHA1 expression: %s", from);
2206         read_next_command();
2207
2208         /* tagger ... */
2209         if (prefixcmp(command_buf.buf, "tagger "))
2210                 die("Expected tagger command, got %s", command_buf.buf);
2211         tagger = parse_ident(command_buf.buf + 7);
2212
2213         /* tag payload/message */
2214         read_next_command();
2215         msg = cmd_data(&msglen);
2216
2217         /* build the tag object */
2218         size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
2219         sp = new_data.buffer;
2220         sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
2221         sp += sprintf(sp, "type %s\n", commit_type);
2222         sp += sprintf(sp, "tag %s\n", t->name);
2223         sp += sprintf(sp, "tagger %s\n", tagger);
2224         *sp++ = '\n';
2225         memcpy(sp, msg, msglen);
2226         sp += msglen;
2227         free(tagger);
2228         free(msg);
2229
2230         if (store_object(OBJ_TAG, new_data.buffer,
2231                 sp - (char*)new_data.buffer,
2232                 NULL, t->sha1, 0))
2233                 t->pack_id = MAX_PACK_ID;
2234         else
2235                 t->pack_id = pack_id;
2236 }
2237
2238 static void cmd_reset_branch(void)
2239 {
2240         struct branch *b;
2241         char *sp;
2242
2243         /* Obtain the branch name from the rest of our command */
2244         sp = strchr(command_buf.buf, ' ') + 1;
2245         b = lookup_branch(sp);
2246         if (b) {
2247                 hashclr(b->sha1);
2248                 hashclr(b->branch_tree.versions[0].sha1);
2249                 hashclr(b->branch_tree.versions[1].sha1);
2250                 if (b->branch_tree.tree) {
2251                         release_tree_content_recursive(b->branch_tree.tree);
2252                         b->branch_tree.tree = NULL;
2253                 }
2254         }
2255         else
2256                 b = new_branch(sp);
2257         read_next_command();
2258         if (!cmd_from(b) && command_buf.len > 1)
2259                 unread_command_buf = 1;
2260 }
2261
2262 static void cmd_checkpoint(void)
2263 {
2264         if (object_count) {
2265                 cycle_packfile();
2266                 dump_branches();
2267                 dump_tags();
2268                 dump_marks();
2269         }
2270         skip_optional_lf();
2271 }
2272
2273 static void cmd_progress(void)
2274 {
2275         fwrite(command_buf.buf, 1, command_buf.len - 1, stdout);
2276         fputc('\n', stdout);
2277         fflush(stdout);
2278         skip_optional_lf();
2279 }
2280
2281 static void import_marks(const char *input_file)
2282 {
2283         char line[512];
2284         FILE *f = fopen(input_file, "r");
2285         if (!f)
2286                 die("cannot read %s: %s", input_file, strerror(errno));
2287         while (fgets(line, sizeof(line), f)) {
2288                 uintmax_t mark;
2289                 char *end;
2290                 unsigned char sha1[20];
2291                 struct object_entry *e;
2292
2293                 end = strchr(line, '\n');
2294                 if (line[0] != ':' || !end)
2295                         die("corrupt mark line: %s", line);
2296                 *end = 0;
2297                 mark = strtoumax(line + 1, &end, 10);
2298                 if (!mark || end == line + 1
2299                         || *end != ' ' || get_sha1(end + 1, sha1))
2300                         die("corrupt mark line: %s", line);
2301                 e = find_object(sha1);
2302                 if (!e) {
2303                         enum object_type type = sha1_object_info(sha1, NULL);
2304                         if (type < 0)
2305                                 die("object not found: %s", sha1_to_hex(sha1));
2306                         e = insert_object(sha1);
2307                         e->type = type;
2308                         e->pack_id = MAX_PACK_ID;
2309                         e->offset = 1; /* just not zero! */
2310                 }
2311                 insert_mark(mark, e);
2312         }
2313         fclose(f);
2314 }
2315
2316 static const char fast_import_usage[] =
2317 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2318
2319 int main(int argc, const char **argv)
2320 {
2321         unsigned int i, show_stats = 1;
2322
2323         git_config(git_default_config);
2324         alloc_objects(object_entry_alloc);
2325         strbuf_init(&command_buf);
2326         atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2327         branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2328         avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2329         marks = pool_calloc(1, sizeof(struct mark_set));
2330
2331         for (i = 1; i < argc; i++) {
2332                 const char *a = argv[i];
2333
2334                 if (*a != '-' || !strcmp(a, "--"))
2335                         break;
2336                 else if (!prefixcmp(a, "--date-format=")) {
2337                         const char *fmt = a + 14;
2338                         if (!strcmp(fmt, "raw"))
2339                                 whenspec = WHENSPEC_RAW;
2340                         else if (!strcmp(fmt, "rfc2822"))
2341                                 whenspec = WHENSPEC_RFC2822;
2342                         else if (!strcmp(fmt, "now"))
2343                                 whenspec = WHENSPEC_NOW;
2344                         else
2345                                 die("unknown --date-format argument %s", fmt);
2346                 }
2347                 else if (!prefixcmp(a, "--max-pack-size="))
2348                         max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
2349                 else if (!prefixcmp(a, "--depth="))
2350                         max_depth = strtoul(a + 8, NULL, 0);
2351                 else if (!prefixcmp(a, "--active-branches="))
2352                         max_active_branches = strtoul(a + 18, NULL, 0);
2353                 else if (!prefixcmp(a, "--import-marks="))
2354                         import_marks(a + 15);
2355                 else if (!prefixcmp(a, "--export-marks="))
2356                         mark_file = a + 15;
2357                 else if (!prefixcmp(a, "--export-pack-edges=")) {
2358                         if (pack_edges)
2359                                 fclose(pack_edges);
2360                         pack_edges = fopen(a + 20, "a");
2361                         if (!pack_edges)
2362                                 die("Cannot open %s: %s", a + 20, strerror(errno));
2363                 } else if (!strcmp(a, "--force"))
2364                         force_update = 1;
2365                 else if (!strcmp(a, "--quiet"))
2366                         show_stats = 0;
2367                 else if (!strcmp(a, "--stats"))
2368                         show_stats = 1;
2369                 else
2370                         die("unknown option %s", a);
2371         }
2372         if (i != argc)
2373                 usage(fast_import_usage);
2374
2375         rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
2376         for (i = 0; i < (cmd_save - 1); i++)
2377                 rc_free[i].next = &rc_free[i + 1];
2378         rc_free[cmd_save - 1].next = NULL;
2379
2380         prepare_packed_git();
2381         start_packfile();
2382         set_die_routine(die_nicely);
2383         for (;;) {
2384                 read_next_command();
2385                 if (command_buf.eof)
2386                         break;
2387                 else if (!strcmp("blob", command_buf.buf))
2388                         cmd_new_blob();
2389                 else if (!prefixcmp(command_buf.buf, "commit "))
2390                         cmd_new_commit();
2391                 else if (!prefixcmp(command_buf.buf, "tag "))
2392                         cmd_new_tag();
2393                 else if (!prefixcmp(command_buf.buf, "reset "))
2394                         cmd_reset_branch();
2395                 else if (!strcmp("checkpoint", command_buf.buf))
2396                         cmd_checkpoint();
2397                 else if (!prefixcmp(command_buf.buf, "progress "))
2398                         cmd_progress();
2399                 else
2400                         die("Unsupported command: %s", command_buf.buf);
2401         }
2402         end_packfile();
2403
2404         dump_branches();
2405         dump_tags();
2406         unkeep_all_packs();
2407         dump_marks();
2408
2409         if (pack_edges)
2410                 fclose(pack_edges);
2411
2412         if (show_stats) {
2413                 uintmax_t total_count = 0, duplicate_count = 0;
2414                 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
2415                         total_count += object_count_by_type[i];
2416                 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
2417                         duplicate_count += duplicate_count_by_type[i];
2418
2419                 fprintf(stderr, "%s statistics:\n", argv[0]);
2420                 fprintf(stderr, "---------------------------------------------------------------------\n");
2421                 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
2422                 fprintf(stderr, "Total objects:   %10" PRIuMAX " (%10" PRIuMAX " duplicates                  )\n", total_count, duplicate_count);
2423                 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]);
2424                 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]);
2425                 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]);
2426                 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]);
2427                 fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
2428                 fprintf(stderr, "      marks:     %10" PRIuMAX " (%10" PRIuMAX " unique    )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
2429                 fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
2430                 fprintf(stderr, "Memory total:    %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
2431                 fprintf(stderr, "       pools:    %10lu KiB\n", (unsigned long)(total_allocd/1024));
2432                 fprintf(stderr, "     objects:    %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
2433                 fprintf(stderr, "---------------------------------------------------------------------\n");
2434                 pack_report();
2435                 fprintf(stderr, "---------------------------------------------------------------------\n");
2436                 fprintf(stderr, "\n");
2437         }
2438
2439         return failure ? 1 : 0;
2440 }