Merge branch 'maint' of git://linux-nfs.org/~bfields/git into maint
[git/jrn.git] / fast-import.c
blob5e83296bf49f9b41b019185539cbd4ec1bf94513
1 /*
2 Format of STDIN stream:
4 stream ::= cmd*;
6 cmd ::= new_blob
7 | new_commit
8 | new_tag
9 | reset_branch
10 | checkpoint
11 | progress
14 new_blob ::= 'blob' lf
15 mark?
16 file_content;
17 file_content ::= data;
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;
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;
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;
50 reset_branch ::= 'reset' sp ref_str lf
51 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
52 lf?;
54 checkpoint ::= 'checkpoint' lf
55 lf?;
57 progress ::= 'progress' sp not_lf* lf
58 lf?;
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.
66 mark ::= 'mark' sp idnum lf;
67 data ::= (delimited_data | exact_data)
68 lf?;
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;
77 # note: declen indicates the length of binary_data in bytes.
78 # declen does not include the lf preceeding the binary data.
80 exact_data ::= 'data' sp declen lf
81 binary_data;
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.
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'
99 declen ::= # unsigned 32 bit value, ascii base10 notation;
100 bigint ::= # unsigned integer value, ascii base10 notation;
101 binary_data ::= # file content, not interpreted;
103 when ::= raw_when | rfc2822_when;
104 raw_when ::= ts sp tz;
105 rfc2822_when ::= # Valid RFC 2822 date and time;
107 sp ::= # ASCII space character;
108 lf ::= # ASCII newline (LF) character;
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.
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;
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.
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;
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.
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).
138 comment ::= '#' not_lf* lf;
139 not_lf ::= # Any byte that is not ASCII newline (LF);
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"
155 #define PACK_ID_BITS 16
156 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
157 #define DEPTH_BITS 13
158 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
160 struct object_entry
162 struct object_entry *next;
163 uint32_t offset;
164 uint32_t type : TYPE_BITS,
165 pack_id : PACK_ID_BITS,
166 depth : DEPTH_BITS;
167 unsigned char sha1[20];
170 struct object_entry_pool
172 struct object_entry_pool *next_pool;
173 struct object_entry *next_free;
174 struct object_entry *end;
175 struct object_entry entries[FLEX_ARRAY]; /* more */
178 struct mark_set
180 union {
181 struct object_entry *marked[1024];
182 struct mark_set *sets[1024];
183 } data;
184 unsigned int shift;
187 struct last_object
189 void *data;
190 unsigned long len;
191 uint32_t offset;
192 unsigned int depth;
193 unsigned no_free:1;
196 struct mem_pool
198 struct mem_pool *next_pool;
199 char *next_free;
200 char *end;
201 char space[FLEX_ARRAY]; /* more */
204 struct atom_str
206 struct atom_str *next_atom;
207 unsigned short str_len;
208 char str_dat[FLEX_ARRAY]; /* more */
211 struct tree_content;
212 struct tree_entry
214 struct tree_content *tree;
215 struct atom_str* name;
216 struct tree_entry_ms
218 uint16_t mode;
219 unsigned char sha1[20];
220 } versions[2];
223 struct tree_content
225 unsigned int entry_capacity; /* must match avail_tree_content */
226 unsigned int entry_count;
227 unsigned int delta_depth;
228 struct tree_entry *entries[FLEX_ARRAY]; /* more */
231 struct avail_tree_content
233 unsigned int entry_capacity; /* must match tree_content */
234 struct avail_tree_content *next_avail;
237 struct branch
239 struct branch *table_next_branch;
240 struct branch *active_next_branch;
241 const char *name;
242 struct tree_entry branch_tree;
243 uintmax_t last_commit;
244 unsigned active : 1;
245 unsigned pack_id : PACK_ID_BITS;
246 unsigned char sha1[20];
249 struct tag
251 struct tag *next_tag;
252 const char *name;
253 unsigned int pack_id;
254 unsigned char sha1[20];
257 struct dbuf
259 void *buffer;
260 size_t capacity;
263 struct hash_list
265 struct hash_list *next;
266 unsigned char sha1[20];
269 typedef enum {
270 WHENSPEC_RAW = 1,
271 WHENSPEC_RFC2822,
272 WHENSPEC_NOW,
273 } whenspec_type;
275 struct recent_command
277 struct recent_command *prev;
278 struct recent_command *next;
279 char *buf;
282 /* Configured limits on output */
283 static unsigned long max_depth = 10;
284 static off_t max_packsize = (1LL << 32) - 1;
285 static int force_update;
287 /* Stats and misc. counters */
288 static uintmax_t alloc_count;
289 static uintmax_t marks_set_count;
290 static uintmax_t object_count_by_type[1 << TYPE_BITS];
291 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
292 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
293 static unsigned long object_count;
294 static unsigned long branch_count;
295 static unsigned long branch_load_count;
296 static int failure;
297 static FILE *pack_edges;
299 /* Memory pools */
300 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
301 static size_t total_allocd;
302 static struct mem_pool *mem_pool;
304 /* Atom management */
305 static unsigned int atom_table_sz = 4451;
306 static unsigned int atom_cnt;
307 static struct atom_str **atom_table;
309 /* The .pack file being generated */
310 static unsigned int pack_id;
311 static struct packed_git *pack_data;
312 static struct packed_git **all_packs;
313 static unsigned long pack_size;
315 /* Table of objects we've written. */
316 static unsigned int object_entry_alloc = 5000;
317 static struct object_entry_pool *blocks;
318 static struct object_entry *object_table[1 << 16];
319 static struct mark_set *marks;
320 static const char* mark_file;
322 /* Our last blob */
323 static struct last_object last_blob;
325 /* Tree management */
326 static unsigned int tree_entry_alloc = 1000;
327 static void *avail_tree_entry;
328 static unsigned int avail_tree_table_sz = 100;
329 static struct avail_tree_content **avail_tree_table;
330 static struct dbuf old_tree;
331 static struct dbuf new_tree;
333 /* Branch data */
334 static unsigned long max_active_branches = 5;
335 static unsigned long cur_active_branches;
336 static unsigned long branch_table_sz = 1039;
337 static struct branch **branch_table;
338 static struct branch *active_branches;
340 /* Tag data */
341 static struct tag *first_tag;
342 static struct tag *last_tag;
344 /* Input stream parsing */
345 static whenspec_type whenspec = WHENSPEC_RAW;
346 static struct strbuf command_buf;
347 static int unread_command_buf;
348 static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
349 static struct recent_command *cmd_tail = &cmd_hist;
350 static struct recent_command *rc_free;
351 static unsigned int cmd_save = 100;
352 static uintmax_t next_mark;
353 static struct dbuf new_data;
355 static void write_branch_report(FILE *rpt, struct branch *b)
357 fprintf(rpt, "%s:\n", b->name);
359 fprintf(rpt, " status :");
360 if (b->active)
361 fputs(" active", rpt);
362 if (b->branch_tree.tree)
363 fputs(" loaded", rpt);
364 if (is_null_sha1(b->branch_tree.versions[1].sha1))
365 fputs(" dirty", rpt);
366 fputc('\n', rpt);
368 fprintf(rpt, " tip commit : %s\n", sha1_to_hex(b->sha1));
369 fprintf(rpt, " old tree : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1));
370 fprintf(rpt, " cur tree : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1));
371 fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit);
373 fputs(" last pack : ", rpt);
374 if (b->pack_id < MAX_PACK_ID)
375 fprintf(rpt, "%u", b->pack_id);
376 fputc('\n', rpt);
378 fputc('\n', rpt);
381 static void write_crash_report(const char *err)
383 char *loc = git_path("fast_import_crash_%d", getpid());
384 FILE *rpt = fopen(loc, "w");
385 struct branch *b;
386 unsigned long lu;
387 struct recent_command *rc;
389 if (!rpt) {
390 error("can't write crash report %s: %s", loc, strerror(errno));
391 return;
394 fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
396 fprintf(rpt, "fast-import crash report:\n");
397 fprintf(rpt, " fast-import process: %d\n", getpid());
398 fprintf(rpt, " parent process : %d\n", getppid());
399 fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
400 fputc('\n', rpt);
402 fputs("fatal: ", rpt);
403 fputs(err, rpt);
404 fputc('\n', rpt);
406 fputc('\n', rpt);
407 fputs("Most Recent Commands Before Crash\n", rpt);
408 fputs("---------------------------------\n", rpt);
409 for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
410 if (rc->next == &cmd_hist)
411 fputs("* ", rpt);
412 else
413 fputs(" ", rpt);
414 fputs(rc->buf, rpt);
415 fputc('\n', rpt);
418 fputc('\n', rpt);
419 fputs("Active Branch LRU\n", rpt);
420 fputs("-----------------\n", rpt);
421 fprintf(rpt, " active_branches = %lu cur, %lu max\n",
422 cur_active_branches,
423 max_active_branches);
424 fputc('\n', rpt);
425 fputs(" pos clock name\n", rpt);
426 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
427 for (b = active_branches, lu = 0; b; b = b->active_next_branch)
428 fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n",
429 ++lu, b->last_commit, b->name);
431 fputc('\n', rpt);
432 fputs("Inactive Branches\n", rpt);
433 fputs("-----------------\n", rpt);
434 for (lu = 0; lu < branch_table_sz; lu++) {
435 for (b = branch_table[lu]; b; b = b->table_next_branch)
436 write_branch_report(rpt, b);
439 fputc('\n', rpt);
440 fputs("-------------------\n", rpt);
441 fputs("END OF CRASH REPORT\n", rpt);
442 fclose(rpt);
445 static NORETURN void die_nicely(const char *err, va_list params)
447 static int zombie;
448 char message[2 * PATH_MAX];
450 vsnprintf(message, sizeof(message), err, params);
451 fputs("fatal: ", stderr);
452 fputs(message, stderr);
453 fputc('\n', stderr);
455 if (!zombie) {
456 zombie = 1;
457 write_crash_report(message);
459 exit(128);
462 static void alloc_objects(unsigned int cnt)
464 struct object_entry_pool *b;
466 b = xmalloc(sizeof(struct object_entry_pool)
467 + cnt * sizeof(struct object_entry));
468 b->next_pool = blocks;
469 b->next_free = b->entries;
470 b->end = b->entries + cnt;
471 blocks = b;
472 alloc_count += cnt;
475 static struct object_entry *new_object(unsigned char *sha1)
477 struct object_entry *e;
479 if (blocks->next_free == blocks->end)
480 alloc_objects(object_entry_alloc);
482 e = blocks->next_free++;
483 hashcpy(e->sha1, sha1);
484 return e;
487 static struct object_entry *find_object(unsigned char *sha1)
489 unsigned int h = sha1[0] << 8 | sha1[1];
490 struct object_entry *e;
491 for (e = object_table[h]; e; e = e->next)
492 if (!hashcmp(sha1, e->sha1))
493 return e;
494 return NULL;
497 static struct object_entry *insert_object(unsigned char *sha1)
499 unsigned int h = sha1[0] << 8 | sha1[1];
500 struct object_entry *e = object_table[h];
501 struct object_entry *p = NULL;
503 while (e) {
504 if (!hashcmp(sha1, e->sha1))
505 return e;
506 p = e;
507 e = e->next;
510 e = new_object(sha1);
511 e->next = NULL;
512 e->offset = 0;
513 if (p)
514 p->next = e;
515 else
516 object_table[h] = e;
517 return e;
520 static unsigned int hc_str(const char *s, size_t len)
522 unsigned int r = 0;
523 while (len-- > 0)
524 r = r * 31 + *s++;
525 return r;
528 static void *pool_alloc(size_t len)
530 struct mem_pool *p;
531 void *r;
533 for (p = mem_pool; p; p = p->next_pool)
534 if ((p->end - p->next_free >= len))
535 break;
537 if (!p) {
538 if (len >= (mem_pool_alloc/2)) {
539 total_allocd += len;
540 return xmalloc(len);
542 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
543 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
544 p->next_pool = mem_pool;
545 p->next_free = p->space;
546 p->end = p->next_free + mem_pool_alloc;
547 mem_pool = p;
550 r = p->next_free;
551 /* round out to a pointer alignment */
552 if (len & (sizeof(void*) - 1))
553 len += sizeof(void*) - (len & (sizeof(void*) - 1));
554 p->next_free += len;
555 return r;
558 static void *pool_calloc(size_t count, size_t size)
560 size_t len = count * size;
561 void *r = pool_alloc(len);
562 memset(r, 0, len);
563 return r;
566 static char *pool_strdup(const char *s)
568 char *r = pool_alloc(strlen(s) + 1);
569 strcpy(r, s);
570 return r;
573 static void size_dbuf(struct dbuf *b, size_t maxlen)
575 if (b->buffer) {
576 if (b->capacity >= maxlen)
577 return;
578 free(b->buffer);
580 b->capacity = ((maxlen / 1024) + 1) * 1024;
581 b->buffer = xmalloc(b->capacity);
584 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
586 struct mark_set *s = marks;
587 while ((idnum >> s->shift) >= 1024) {
588 s = pool_calloc(1, sizeof(struct mark_set));
589 s->shift = marks->shift + 10;
590 s->data.sets[0] = marks;
591 marks = s;
593 while (s->shift) {
594 uintmax_t i = idnum >> s->shift;
595 idnum -= i << s->shift;
596 if (!s->data.sets[i]) {
597 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
598 s->data.sets[i]->shift = s->shift - 10;
600 s = s->data.sets[i];
602 if (!s->data.marked[idnum])
603 marks_set_count++;
604 s->data.marked[idnum] = oe;
607 static struct object_entry *find_mark(uintmax_t idnum)
609 uintmax_t orig_idnum = idnum;
610 struct mark_set *s = marks;
611 struct object_entry *oe = NULL;
612 if ((idnum >> s->shift) < 1024) {
613 while (s && s->shift) {
614 uintmax_t i = idnum >> s->shift;
615 idnum -= i << s->shift;
616 s = s->data.sets[i];
618 if (s)
619 oe = s->data.marked[idnum];
621 if (!oe)
622 die("mark :%" PRIuMAX " not declared", orig_idnum);
623 return oe;
626 static struct atom_str *to_atom(const char *s, unsigned short len)
628 unsigned int hc = hc_str(s, len) % atom_table_sz;
629 struct atom_str *c;
631 for (c = atom_table[hc]; c; c = c->next_atom)
632 if (c->str_len == len && !strncmp(s, c->str_dat, len))
633 return c;
635 c = pool_alloc(sizeof(struct atom_str) + len + 1);
636 c->str_len = len;
637 strncpy(c->str_dat, s, len);
638 c->str_dat[len] = 0;
639 c->next_atom = atom_table[hc];
640 atom_table[hc] = c;
641 atom_cnt++;
642 return c;
645 static struct branch *lookup_branch(const char *name)
647 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
648 struct branch *b;
650 for (b = branch_table[hc]; b; b = b->table_next_branch)
651 if (!strcmp(name, b->name))
652 return b;
653 return NULL;
656 static struct branch *new_branch(const char *name)
658 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
659 struct branch* b = lookup_branch(name);
661 if (b)
662 die("Invalid attempt to create duplicate branch: %s", name);
663 switch (check_ref_format(name)) {
664 case 0: break; /* its valid */
665 case -2: break; /* valid, but too few '/', allow anyway */
666 default:
667 die("Branch name doesn't conform to GIT standards: %s", name);
670 b = pool_calloc(1, sizeof(struct branch));
671 b->name = pool_strdup(name);
672 b->table_next_branch = branch_table[hc];
673 b->branch_tree.versions[0].mode = S_IFDIR;
674 b->branch_tree.versions[1].mode = S_IFDIR;
675 b->active = 0;
676 b->pack_id = MAX_PACK_ID;
677 branch_table[hc] = b;
678 branch_count++;
679 return b;
682 static unsigned int hc_entries(unsigned int cnt)
684 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
685 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
688 static struct tree_content *new_tree_content(unsigned int cnt)
690 struct avail_tree_content *f, *l = NULL;
691 struct tree_content *t;
692 unsigned int hc = hc_entries(cnt);
694 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
695 if (f->entry_capacity >= cnt)
696 break;
698 if (f) {
699 if (l)
700 l->next_avail = f->next_avail;
701 else
702 avail_tree_table[hc] = f->next_avail;
703 } else {
704 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
705 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
706 f->entry_capacity = cnt;
709 t = (struct tree_content*)f;
710 t->entry_count = 0;
711 t->delta_depth = 0;
712 return t;
715 static void release_tree_entry(struct tree_entry *e);
716 static void release_tree_content(struct tree_content *t)
718 struct avail_tree_content *f = (struct avail_tree_content*)t;
719 unsigned int hc = hc_entries(f->entry_capacity);
720 f->next_avail = avail_tree_table[hc];
721 avail_tree_table[hc] = f;
724 static void release_tree_content_recursive(struct tree_content *t)
726 unsigned int i;
727 for (i = 0; i < t->entry_count; i++)
728 release_tree_entry(t->entries[i]);
729 release_tree_content(t);
732 static struct tree_content *grow_tree_content(
733 struct tree_content *t,
734 int amt)
736 struct tree_content *r = new_tree_content(t->entry_count + amt);
737 r->entry_count = t->entry_count;
738 r->delta_depth = t->delta_depth;
739 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
740 release_tree_content(t);
741 return r;
744 static struct tree_entry *new_tree_entry(void)
746 struct tree_entry *e;
748 if (!avail_tree_entry) {
749 unsigned int n = tree_entry_alloc;
750 total_allocd += n * sizeof(struct tree_entry);
751 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
752 while (n-- > 1) {
753 *((void**)e) = e + 1;
754 e++;
756 *((void**)e) = NULL;
759 e = avail_tree_entry;
760 avail_tree_entry = *((void**)e);
761 return e;
764 static void release_tree_entry(struct tree_entry *e)
766 if (e->tree)
767 release_tree_content_recursive(e->tree);
768 *((void**)e) = avail_tree_entry;
769 avail_tree_entry = e;
772 static struct tree_content *dup_tree_content(struct tree_content *s)
774 struct tree_content *d;
775 struct tree_entry *a, *b;
776 unsigned int i;
778 if (!s)
779 return NULL;
780 d = new_tree_content(s->entry_count);
781 for (i = 0; i < s->entry_count; i++) {
782 a = s->entries[i];
783 b = new_tree_entry();
784 memcpy(b, a, sizeof(*a));
785 if (a->tree && is_null_sha1(b->versions[1].sha1))
786 b->tree = dup_tree_content(a->tree);
787 else
788 b->tree = NULL;
789 d->entries[i] = b;
791 d->entry_count = s->entry_count;
792 d->delta_depth = s->delta_depth;
794 return d;
797 static void start_packfile(void)
799 static char tmpfile[PATH_MAX];
800 struct packed_git *p;
801 struct pack_header hdr;
802 int pack_fd;
804 snprintf(tmpfile, sizeof(tmpfile),
805 "%s/tmp_pack_XXXXXX", get_object_directory());
806 pack_fd = xmkstemp(tmpfile);
807 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
808 strcpy(p->pack_name, tmpfile);
809 p->pack_fd = pack_fd;
811 hdr.hdr_signature = htonl(PACK_SIGNATURE);
812 hdr.hdr_version = htonl(2);
813 hdr.hdr_entries = 0;
814 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
816 pack_data = p;
817 pack_size = sizeof(hdr);
818 object_count = 0;
820 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
821 all_packs[pack_id] = p;
824 static int oecmp (const void *a_, const void *b_)
826 struct object_entry *a = *((struct object_entry**)a_);
827 struct object_entry *b = *((struct object_entry**)b_);
828 return hashcmp(a->sha1, b->sha1);
831 static char *create_index(void)
833 static char tmpfile[PATH_MAX];
834 SHA_CTX ctx;
835 struct sha1file *f;
836 struct object_entry **idx, **c, **last, *e;
837 struct object_entry_pool *o;
838 uint32_t array[256];
839 int i, idx_fd;
841 /* Build the sorted table of object IDs. */
842 idx = xmalloc(object_count * sizeof(struct object_entry*));
843 c = idx;
844 for (o = blocks; o; o = o->next_pool)
845 for (e = o->next_free; e-- != o->entries;)
846 if (pack_id == e->pack_id)
847 *c++ = e;
848 last = idx + object_count;
849 if (c != last)
850 die("internal consistency error creating the index");
851 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
853 /* Generate the fan-out array. */
854 c = idx;
855 for (i = 0; i < 256; i++) {
856 struct object_entry **next = c;;
857 while (next < last) {
858 if ((*next)->sha1[0] != i)
859 break;
860 next++;
862 array[i] = htonl(next - idx);
863 c = next;
866 snprintf(tmpfile, sizeof(tmpfile),
867 "%s/tmp_idx_XXXXXX", get_object_directory());
868 idx_fd = xmkstemp(tmpfile);
869 f = sha1fd(idx_fd, tmpfile);
870 sha1write(f, array, 256 * sizeof(int));
871 SHA1_Init(&ctx);
872 for (c = idx; c != last; c++) {
873 uint32_t offset = htonl((*c)->offset);
874 sha1write(f, &offset, 4);
875 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
876 SHA1_Update(&ctx, (*c)->sha1, 20);
878 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
879 sha1close(f, NULL, 1);
880 free(idx);
881 SHA1_Final(pack_data->sha1, &ctx);
882 return tmpfile;
885 static char *keep_pack(char *curr_index_name)
887 static char name[PATH_MAX];
888 static const char *keep_msg = "fast-import";
889 int keep_fd;
891 chmod(pack_data->pack_name, 0444);
892 chmod(curr_index_name, 0444);
894 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
895 get_object_directory(), sha1_to_hex(pack_data->sha1));
896 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
897 if (keep_fd < 0)
898 die("cannot create keep file");
899 write(keep_fd, keep_msg, strlen(keep_msg));
900 close(keep_fd);
902 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
903 get_object_directory(), sha1_to_hex(pack_data->sha1));
904 if (move_temp_to_file(pack_data->pack_name, name))
905 die("cannot store pack file");
907 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
908 get_object_directory(), sha1_to_hex(pack_data->sha1));
909 if (move_temp_to_file(curr_index_name, name))
910 die("cannot store index file");
911 return name;
914 static void unkeep_all_packs(void)
916 static char name[PATH_MAX];
917 int k;
919 for (k = 0; k < pack_id; k++) {
920 struct packed_git *p = all_packs[k];
921 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
922 get_object_directory(), sha1_to_hex(p->sha1));
923 unlink(name);
927 static void end_packfile(void)
929 struct packed_git *old_p = pack_data, *new_p;
931 if (object_count) {
932 char *idx_name;
933 int i;
934 struct branch *b;
935 struct tag *t;
937 fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
938 pack_data->pack_name, object_count);
939 close(pack_data->pack_fd);
940 idx_name = keep_pack(create_index());
942 /* Register the packfile with core git's machinary. */
943 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
944 if (!new_p)
945 die("core git rejected index %s", idx_name);
946 new_p->windows = old_p->windows;
947 all_packs[pack_id] = new_p;
948 install_packed_git(new_p);
950 /* Print the boundary */
951 if (pack_edges) {
952 fprintf(pack_edges, "%s:", new_p->pack_name);
953 for (i = 0; i < branch_table_sz; i++) {
954 for (b = branch_table[i]; b; b = b->table_next_branch) {
955 if (b->pack_id == pack_id)
956 fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
959 for (t = first_tag; t; t = t->next_tag) {
960 if (t->pack_id == pack_id)
961 fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
963 fputc('\n', pack_edges);
964 fflush(pack_edges);
967 pack_id++;
969 else
970 unlink(old_p->pack_name);
971 free(old_p);
973 /* We can't carry a delta across packfiles. */
974 free(last_blob.data);
975 last_blob.data = NULL;
976 last_blob.len = 0;
977 last_blob.offset = 0;
978 last_blob.depth = 0;
981 static void cycle_packfile(void)
983 end_packfile();
984 start_packfile();
987 static size_t encode_header(
988 enum object_type type,
989 size_t size,
990 unsigned char *hdr)
992 int n = 1;
993 unsigned char c;
995 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
996 die("bad type %d", type);
998 c = (type << 4) | (size & 15);
999 size >>= 4;
1000 while (size) {
1001 *hdr++ = c | 0x80;
1002 c = size & 0x7f;
1003 size >>= 7;
1004 n++;
1006 *hdr = c;
1007 return n;
1010 static int store_object(
1011 enum object_type type,
1012 void *dat,
1013 size_t datlen,
1014 struct last_object *last,
1015 unsigned char *sha1out,
1016 uintmax_t mark)
1018 void *out, *delta;
1019 struct object_entry *e;
1020 unsigned char hdr[96];
1021 unsigned char sha1[20];
1022 unsigned long hdrlen, deltalen;
1023 SHA_CTX c;
1024 z_stream s;
1026 hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
1027 (unsigned long)datlen) + 1;
1028 SHA1_Init(&c);
1029 SHA1_Update(&c, hdr, hdrlen);
1030 SHA1_Update(&c, dat, datlen);
1031 SHA1_Final(sha1, &c);
1032 if (sha1out)
1033 hashcpy(sha1out, sha1);
1035 e = insert_object(sha1);
1036 if (mark)
1037 insert_mark(mark, e);
1038 if (e->offset) {
1039 duplicate_count_by_type[type]++;
1040 return 1;
1041 } else if (find_sha1_pack(sha1, packed_git)) {
1042 e->type = type;
1043 e->pack_id = MAX_PACK_ID;
1044 e->offset = 1; /* just not zero! */
1045 duplicate_count_by_type[type]++;
1046 return 1;
1049 if (last && last->data && last->depth < max_depth) {
1050 delta = diff_delta(last->data, last->len,
1051 dat, datlen,
1052 &deltalen, 0);
1053 if (delta && deltalen >= datlen) {
1054 free(delta);
1055 delta = NULL;
1057 } else
1058 delta = NULL;
1060 memset(&s, 0, sizeof(s));
1061 deflateInit(&s, zlib_compression_level);
1062 if (delta) {
1063 s.next_in = delta;
1064 s.avail_in = deltalen;
1065 } else {
1066 s.next_in = dat;
1067 s.avail_in = datlen;
1069 s.avail_out = deflateBound(&s, s.avail_in);
1070 s.next_out = out = xmalloc(s.avail_out);
1071 while (deflate(&s, Z_FINISH) == Z_OK)
1072 /* nothing */;
1073 deflateEnd(&s);
1075 /* Determine if we should auto-checkpoint. */
1076 if ((pack_size + 60 + s.total_out) > max_packsize
1077 || (pack_size + 60 + s.total_out) < pack_size) {
1079 /* This new object needs to *not* have the current pack_id. */
1080 e->pack_id = pack_id + 1;
1081 cycle_packfile();
1083 /* We cannot carry a delta into the new pack. */
1084 if (delta) {
1085 free(delta);
1086 delta = NULL;
1088 memset(&s, 0, sizeof(s));
1089 deflateInit(&s, zlib_compression_level);
1090 s.next_in = dat;
1091 s.avail_in = datlen;
1092 s.avail_out = deflateBound(&s, s.avail_in);
1093 s.next_out = out = xrealloc(out, s.avail_out);
1094 while (deflate(&s, Z_FINISH) == Z_OK)
1095 /* nothing */;
1096 deflateEnd(&s);
1100 e->type = type;
1101 e->pack_id = pack_id;
1102 e->offset = pack_size;
1103 object_count++;
1104 object_count_by_type[type]++;
1106 if (delta) {
1107 unsigned long ofs = e->offset - last->offset;
1108 unsigned pos = sizeof(hdr) - 1;
1110 delta_count_by_type[type]++;
1111 e->depth = last->depth + 1;
1113 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
1114 write_or_die(pack_data->pack_fd, hdr, hdrlen);
1115 pack_size += hdrlen;
1117 hdr[pos] = ofs & 127;
1118 while (ofs >>= 7)
1119 hdr[--pos] = 128 | (--ofs & 127);
1120 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
1121 pack_size += sizeof(hdr) - pos;
1122 } else {
1123 e->depth = 0;
1124 hdrlen = encode_header(type, datlen, hdr);
1125 write_or_die(pack_data->pack_fd, hdr, hdrlen);
1126 pack_size += hdrlen;
1129 write_or_die(pack_data->pack_fd, out, s.total_out);
1130 pack_size += s.total_out;
1132 free(out);
1133 free(delta);
1134 if (last) {
1135 if (!last->no_free)
1136 free(last->data);
1137 last->data = dat;
1138 last->offset = e->offset;
1139 last->depth = e->depth;
1140 last->len = datlen;
1142 return 0;
1145 static void *gfi_unpack_entry(
1146 struct object_entry *oe,
1147 unsigned long *sizep)
1149 enum object_type type;
1150 struct packed_git *p = all_packs[oe->pack_id];
1151 if (p == pack_data)
1152 p->pack_size = pack_size + 20;
1153 return unpack_entry(p, oe->offset, &type, sizep);
1156 static const char *get_mode(const char *str, uint16_t *modep)
1158 unsigned char c;
1159 uint16_t mode = 0;
1161 while ((c = *str++) != ' ') {
1162 if (c < '0' || c > '7')
1163 return NULL;
1164 mode = (mode << 3) + (c - '0');
1166 *modep = mode;
1167 return str;
1170 static void load_tree(struct tree_entry *root)
1172 unsigned char* sha1 = root->versions[1].sha1;
1173 struct object_entry *myoe;
1174 struct tree_content *t;
1175 unsigned long size;
1176 char *buf;
1177 const char *c;
1179 root->tree = t = new_tree_content(8);
1180 if (is_null_sha1(sha1))
1181 return;
1183 myoe = find_object(sha1);
1184 if (myoe && myoe->pack_id != MAX_PACK_ID) {
1185 if (myoe->type != OBJ_TREE)
1186 die("Not a tree: %s", sha1_to_hex(sha1));
1187 t->delta_depth = myoe->depth;
1188 buf = gfi_unpack_entry(myoe, &size);
1189 } else {
1190 enum object_type type;
1191 buf = read_sha1_file(sha1, &type, &size);
1192 if (!buf || type != OBJ_TREE)
1193 die("Can't load tree %s", sha1_to_hex(sha1));
1196 c = buf;
1197 while (c != (buf + size)) {
1198 struct tree_entry *e = new_tree_entry();
1200 if (t->entry_count == t->entry_capacity)
1201 root->tree = t = grow_tree_content(t, t->entry_count);
1202 t->entries[t->entry_count++] = e;
1204 e->tree = NULL;
1205 c = get_mode(c, &e->versions[1].mode);
1206 if (!c)
1207 die("Corrupt mode in %s", sha1_to_hex(sha1));
1208 e->versions[0].mode = e->versions[1].mode;
1209 e->name = to_atom(c, strlen(c));
1210 c += e->name->str_len + 1;
1211 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1212 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1213 c += 20;
1215 free(buf);
1218 static int tecmp0 (const void *_a, const void *_b)
1220 struct tree_entry *a = *((struct tree_entry**)_a);
1221 struct tree_entry *b = *((struct tree_entry**)_b);
1222 return base_name_compare(
1223 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1224 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1227 static int tecmp1 (const void *_a, const void *_b)
1229 struct tree_entry *a = *((struct tree_entry**)_a);
1230 struct tree_entry *b = *((struct tree_entry**)_b);
1231 return base_name_compare(
1232 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1233 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1236 static void mktree(struct tree_content *t,
1237 int v,
1238 unsigned long *szp,
1239 struct dbuf *b)
1241 size_t maxlen = 0;
1242 unsigned int i;
1243 char *c;
1245 if (!v)
1246 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1247 else
1248 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1250 for (i = 0; i < t->entry_count; i++) {
1251 if (t->entries[i]->versions[v].mode)
1252 maxlen += t->entries[i]->name->str_len + 34;
1255 size_dbuf(b, maxlen);
1256 c = b->buffer;
1257 for (i = 0; i < t->entry_count; i++) {
1258 struct tree_entry *e = t->entries[i];
1259 if (!e->versions[v].mode)
1260 continue;
1261 c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1262 *c++ = ' ';
1263 strcpy(c, e->name->str_dat);
1264 c += e->name->str_len + 1;
1265 hashcpy((unsigned char*)c, e->versions[v].sha1);
1266 c += 20;
1268 *szp = c - (char*)b->buffer;
1271 static void store_tree(struct tree_entry *root)
1273 struct tree_content *t = root->tree;
1274 unsigned int i, j, del;
1275 unsigned long new_len;
1276 struct last_object lo;
1277 struct object_entry *le;
1279 if (!is_null_sha1(root->versions[1].sha1))
1280 return;
1282 for (i = 0; i < t->entry_count; i++) {
1283 if (t->entries[i]->tree)
1284 store_tree(t->entries[i]);
1287 le = find_object(root->versions[0].sha1);
1288 if (!S_ISDIR(root->versions[0].mode)
1289 || !le
1290 || le->pack_id != pack_id) {
1291 lo.data = NULL;
1292 lo.depth = 0;
1293 lo.no_free = 0;
1294 } else {
1295 mktree(t, 0, &lo.len, &old_tree);
1296 lo.data = old_tree.buffer;
1297 lo.offset = le->offset;
1298 lo.depth = t->delta_depth;
1299 lo.no_free = 1;
1302 mktree(t, 1, &new_len, &new_tree);
1303 store_object(OBJ_TREE, new_tree.buffer, new_len,
1304 &lo, root->versions[1].sha1, 0);
1306 t->delta_depth = lo.depth;
1307 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1308 struct tree_entry *e = t->entries[i];
1309 if (e->versions[1].mode) {
1310 e->versions[0].mode = e->versions[1].mode;
1311 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1312 t->entries[j++] = e;
1313 } else {
1314 release_tree_entry(e);
1315 del++;
1318 t->entry_count -= del;
1321 static int tree_content_set(
1322 struct tree_entry *root,
1323 const char *p,
1324 const unsigned char *sha1,
1325 const uint16_t mode,
1326 struct tree_content *subtree)
1328 struct tree_content *t = root->tree;
1329 const char *slash1;
1330 unsigned int i, n;
1331 struct tree_entry *e;
1333 slash1 = strchr(p, '/');
1334 if (slash1)
1335 n = slash1 - p;
1336 else
1337 n = strlen(p);
1338 if (!n)
1339 die("Empty path component found in input");
1340 if (!slash1 && !S_ISDIR(mode) && subtree)
1341 die("Non-directories cannot have subtrees");
1343 for (i = 0; i < t->entry_count; i++) {
1344 e = t->entries[i];
1345 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1346 if (!slash1) {
1347 if (!S_ISDIR(mode)
1348 && e->versions[1].mode == mode
1349 && !hashcmp(e->versions[1].sha1, sha1))
1350 return 0;
1351 e->versions[1].mode = mode;
1352 hashcpy(e->versions[1].sha1, sha1);
1353 if (e->tree)
1354 release_tree_content_recursive(e->tree);
1355 e->tree = subtree;
1356 hashclr(root->versions[1].sha1);
1357 return 1;
1359 if (!S_ISDIR(e->versions[1].mode)) {
1360 e->tree = new_tree_content(8);
1361 e->versions[1].mode = S_IFDIR;
1363 if (!e->tree)
1364 load_tree(e);
1365 if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
1366 hashclr(root->versions[1].sha1);
1367 return 1;
1369 return 0;
1373 if (t->entry_count == t->entry_capacity)
1374 root->tree = t = grow_tree_content(t, t->entry_count);
1375 e = new_tree_entry();
1376 e->name = to_atom(p, n);
1377 e->versions[0].mode = 0;
1378 hashclr(e->versions[0].sha1);
1379 t->entries[t->entry_count++] = e;
1380 if (slash1) {
1381 e->tree = new_tree_content(8);
1382 e->versions[1].mode = S_IFDIR;
1383 tree_content_set(e, slash1 + 1, sha1, mode, subtree);
1384 } else {
1385 e->tree = subtree;
1386 e->versions[1].mode = mode;
1387 hashcpy(e->versions[1].sha1, sha1);
1389 hashclr(root->versions[1].sha1);
1390 return 1;
1393 static int tree_content_remove(
1394 struct tree_entry *root,
1395 const char *p,
1396 struct tree_entry *backup_leaf)
1398 struct tree_content *t = root->tree;
1399 const char *slash1;
1400 unsigned int i, n;
1401 struct tree_entry *e;
1403 slash1 = strchr(p, '/');
1404 if (slash1)
1405 n = slash1 - p;
1406 else
1407 n = strlen(p);
1409 for (i = 0; i < t->entry_count; i++) {
1410 e = t->entries[i];
1411 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1412 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1413 goto del_entry;
1414 if (!e->tree)
1415 load_tree(e);
1416 if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
1417 for (n = 0; n < e->tree->entry_count; n++) {
1418 if (e->tree->entries[n]->versions[1].mode) {
1419 hashclr(root->versions[1].sha1);
1420 return 1;
1423 backup_leaf = NULL;
1424 goto del_entry;
1426 return 0;
1429 return 0;
1431 del_entry:
1432 if (backup_leaf)
1433 memcpy(backup_leaf, e, sizeof(*backup_leaf));
1434 else if (e->tree)
1435 release_tree_content_recursive(e->tree);
1436 e->tree = NULL;
1437 e->versions[1].mode = 0;
1438 hashclr(e->versions[1].sha1);
1439 hashclr(root->versions[1].sha1);
1440 return 1;
1443 static int tree_content_get(
1444 struct tree_entry *root,
1445 const char *p,
1446 struct tree_entry *leaf)
1448 struct tree_content *t = root->tree;
1449 const char *slash1;
1450 unsigned int i, n;
1451 struct tree_entry *e;
1453 slash1 = strchr(p, '/');
1454 if (slash1)
1455 n = slash1 - p;
1456 else
1457 n = strlen(p);
1459 for (i = 0; i < t->entry_count; i++) {
1460 e = t->entries[i];
1461 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1462 if (!slash1) {
1463 memcpy(leaf, e, sizeof(*leaf));
1464 if (e->tree && is_null_sha1(e->versions[1].sha1))
1465 leaf->tree = dup_tree_content(e->tree);
1466 else
1467 leaf->tree = NULL;
1468 return 1;
1470 if (!S_ISDIR(e->versions[1].mode))
1471 return 0;
1472 if (!e->tree)
1473 load_tree(e);
1474 return tree_content_get(e, slash1 + 1, leaf);
1477 return 0;
1480 static int update_branch(struct branch *b)
1482 static const char *msg = "fast-import";
1483 struct ref_lock *lock;
1484 unsigned char old_sha1[20];
1486 if (read_ref(b->name, old_sha1))
1487 hashclr(old_sha1);
1488 lock = lock_any_ref_for_update(b->name, old_sha1, 0);
1489 if (!lock)
1490 return error("Unable to lock %s", b->name);
1491 if (!force_update && !is_null_sha1(old_sha1)) {
1492 struct commit *old_cmit, *new_cmit;
1494 old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1495 new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1496 if (!old_cmit || !new_cmit) {
1497 unlock_ref(lock);
1498 return error("Branch %s is missing commits.", b->name);
1501 if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
1502 unlock_ref(lock);
1503 warning("Not updating %s"
1504 " (new tip %s does not contain %s)",
1505 b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1506 return -1;
1509 if (write_ref_sha1(lock, b->sha1, msg) < 0)
1510 return error("Unable to update %s", b->name);
1511 return 0;
1514 static void dump_branches(void)
1516 unsigned int i;
1517 struct branch *b;
1519 for (i = 0; i < branch_table_sz; i++) {
1520 for (b = branch_table[i]; b; b = b->table_next_branch)
1521 failure |= update_branch(b);
1525 static void dump_tags(void)
1527 static const char *msg = "fast-import";
1528 struct tag *t;
1529 struct ref_lock *lock;
1530 char ref_name[PATH_MAX];
1532 for (t = first_tag; t; t = t->next_tag) {
1533 sprintf(ref_name, "tags/%s", t->name);
1534 lock = lock_ref_sha1(ref_name, NULL);
1535 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1536 failure |= error("Unable to update %s", ref_name);
1540 static void dump_marks_helper(FILE *f,
1541 uintmax_t base,
1542 struct mark_set *m)
1544 uintmax_t k;
1545 if (m->shift) {
1546 for (k = 0; k < 1024; k++) {
1547 if (m->data.sets[k])
1548 dump_marks_helper(f, (base + k) << m->shift,
1549 m->data.sets[k]);
1551 } else {
1552 for (k = 0; k < 1024; k++) {
1553 if (m->data.marked[k])
1554 fprintf(f, ":%" PRIuMAX " %s\n", base + k,
1555 sha1_to_hex(m->data.marked[k]->sha1));
1560 static void dump_marks(void)
1562 static struct lock_file mark_lock;
1563 int mark_fd;
1564 FILE *f;
1566 if (!mark_file)
1567 return;
1569 mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
1570 if (mark_fd < 0) {
1571 failure |= error("Unable to write marks file %s: %s",
1572 mark_file, strerror(errno));
1573 return;
1576 f = fdopen(mark_fd, "w");
1577 if (!f) {
1578 rollback_lock_file(&mark_lock);
1579 failure |= error("Unable to write marks file %s: %s",
1580 mark_file, strerror(errno));
1581 return;
1584 dump_marks_helper(f, 0, marks);
1585 fclose(f);
1586 if (commit_lock_file(&mark_lock))
1587 failure |= error("Unable to write marks file %s: %s",
1588 mark_file, strerror(errno));
1591 static void read_next_command(void)
1593 do {
1594 if (unread_command_buf) {
1595 unread_command_buf = 0;
1596 if (command_buf.eof)
1597 return;
1598 } else {
1599 struct recent_command *rc;
1601 command_buf.buf = NULL;
1602 read_line(&command_buf, stdin, '\n');
1603 if (command_buf.eof)
1604 return;
1606 rc = rc_free;
1607 if (rc)
1608 rc_free = rc->next;
1609 else {
1610 rc = cmd_hist.next;
1611 cmd_hist.next = rc->next;
1612 cmd_hist.next->prev = &cmd_hist;
1613 free(rc->buf);
1616 rc->buf = command_buf.buf;
1617 rc->prev = cmd_tail;
1618 rc->next = cmd_hist.prev;
1619 rc->prev->next = rc;
1620 cmd_tail = rc;
1622 } while (command_buf.buf[0] == '#');
1625 static void skip_optional_lf(void)
1627 int term_char = fgetc(stdin);
1628 if (term_char != '\n' && term_char != EOF)
1629 ungetc(term_char, stdin);
1632 static void cmd_mark(void)
1634 if (!prefixcmp(command_buf.buf, "mark :")) {
1635 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1636 read_next_command();
1638 else
1639 next_mark = 0;
1642 static void *cmd_data (size_t *size)
1644 size_t length;
1645 char *buffer;
1647 if (prefixcmp(command_buf.buf, "data "))
1648 die("Expected 'data n' command, found: %s", command_buf.buf);
1650 if (!prefixcmp(command_buf.buf + 5, "<<")) {
1651 char *term = xstrdup(command_buf.buf + 5 + 2);
1652 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1653 length = 0;
1654 buffer = xmalloc(sz);
1655 command_buf.buf = NULL;
1656 for (;;) {
1657 read_line(&command_buf, stdin, '\n');
1658 if (command_buf.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 ALLOC_GROW(buffer, length + command_buf.len, sz);
1664 memcpy(buffer + length,
1665 command_buf.buf,
1666 command_buf.len - 1);
1667 length += command_buf.len - 1;
1668 buffer[length++] = '\n';
1670 free(term);
1672 else {
1673 size_t n = 0;
1674 length = strtoul(command_buf.buf + 5, NULL, 10);
1675 buffer = xmalloc(length);
1676 while (n < length) {
1677 size_t s = fread(buffer + n, 1, length - n, stdin);
1678 if (!s && feof(stdin))
1679 die("EOF in data (%lu bytes remaining)",
1680 (unsigned long)(length - n));
1681 n += s;
1685 skip_optional_lf();
1686 *size = length;
1687 return buffer;
1690 static int validate_raw_date(const char *src, char *result, int maxlen)
1692 const char *orig_src = src;
1693 char *endp, sign;
1695 strtoul(src, &endp, 10);
1696 if (endp == src || *endp != ' ')
1697 return -1;
1699 src = endp + 1;
1700 if (*src != '-' && *src != '+')
1701 return -1;
1702 sign = *src;
1704 strtoul(src + 1, &endp, 10);
1705 if (endp == src || *endp || (endp - orig_src) >= maxlen)
1706 return -1;
1708 strcpy(result, orig_src);
1709 return 0;
1712 static char *parse_ident(const char *buf)
1714 const char *gt;
1715 size_t name_len;
1716 char *ident;
1718 gt = strrchr(buf, '>');
1719 if (!gt)
1720 die("Missing > in ident string: %s", buf);
1721 gt++;
1722 if (*gt != ' ')
1723 die("Missing space after > in ident string: %s", buf);
1724 gt++;
1725 name_len = gt - buf;
1726 ident = xmalloc(name_len + 24);
1727 strncpy(ident, buf, name_len);
1729 switch (whenspec) {
1730 case WHENSPEC_RAW:
1731 if (validate_raw_date(gt, ident + name_len, 24) < 0)
1732 die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1733 break;
1734 case WHENSPEC_RFC2822:
1735 if (parse_date(gt, ident + name_len, 24) < 0)
1736 die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1737 break;
1738 case WHENSPEC_NOW:
1739 if (strcmp("now", gt))
1740 die("Date in ident must be 'now': %s", buf);
1741 datestamp(ident + name_len, 24);
1742 break;
1745 return ident;
1748 static void cmd_new_blob(void)
1750 size_t l;
1751 void *d;
1753 read_next_command();
1754 cmd_mark();
1755 d = cmd_data(&l);
1757 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1758 free(d);
1761 static void unload_one_branch(void)
1763 while (cur_active_branches
1764 && cur_active_branches >= max_active_branches) {
1765 uintmax_t min_commit = ULONG_MAX;
1766 struct branch *e, *l = NULL, *p = NULL;
1768 for (e = active_branches; e; e = e->active_next_branch) {
1769 if (e->last_commit < min_commit) {
1770 p = l;
1771 min_commit = e->last_commit;
1773 l = e;
1776 if (p) {
1777 e = p->active_next_branch;
1778 p->active_next_branch = e->active_next_branch;
1779 } else {
1780 e = active_branches;
1781 active_branches = e->active_next_branch;
1783 e->active = 0;
1784 e->active_next_branch = NULL;
1785 if (e->branch_tree.tree) {
1786 release_tree_content_recursive(e->branch_tree.tree);
1787 e->branch_tree.tree = NULL;
1789 cur_active_branches--;
1793 static void load_branch(struct branch *b)
1795 load_tree(&b->branch_tree);
1796 if (!b->active) {
1797 b->active = 1;
1798 b->active_next_branch = active_branches;
1799 active_branches = b;
1800 cur_active_branches++;
1801 branch_load_count++;
1805 static void file_change_m(struct branch *b)
1807 const char *p = command_buf.buf + 2;
1808 char *p_uq;
1809 const char *endp;
1810 struct object_entry *oe = oe;
1811 unsigned char sha1[20];
1812 uint16_t mode, inline_data = 0;
1814 p = get_mode(p, &mode);
1815 if (!p)
1816 die("Corrupt mode: %s", command_buf.buf);
1817 switch (mode) {
1818 case S_IFREG | 0644:
1819 case S_IFREG | 0755:
1820 case S_IFLNK:
1821 case 0644:
1822 case 0755:
1823 /* ok */
1824 break;
1825 default:
1826 die("Corrupt mode: %s", command_buf.buf);
1829 if (*p == ':') {
1830 char *x;
1831 oe = find_mark(strtoumax(p + 1, &x, 10));
1832 hashcpy(sha1, oe->sha1);
1833 p = x;
1834 } else if (!prefixcmp(p, "inline")) {
1835 inline_data = 1;
1836 p += 6;
1837 } else {
1838 if (get_sha1_hex(p, sha1))
1839 die("Invalid SHA1: %s", command_buf.buf);
1840 oe = find_object(sha1);
1841 p += 40;
1843 if (*p++ != ' ')
1844 die("Missing space after SHA1: %s", command_buf.buf);
1846 p_uq = unquote_c_style(p, &endp);
1847 if (p_uq) {
1848 if (*endp)
1849 die("Garbage after path in: %s", command_buf.buf);
1850 p = p_uq;
1853 if (inline_data) {
1854 size_t l;
1855 void *d;
1856 if (!p_uq)
1857 p = p_uq = xstrdup(p);
1858 read_next_command();
1859 d = cmd_data(&l);
1860 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1861 free(d);
1862 } else if (oe) {
1863 if (oe->type != OBJ_BLOB)
1864 die("Not a blob (actually a %s): %s",
1865 typename(oe->type), command_buf.buf);
1866 } else {
1867 enum object_type type = sha1_object_info(sha1, NULL);
1868 if (type < 0)
1869 die("Blob not found: %s", command_buf.buf);
1870 if (type != OBJ_BLOB)
1871 die("Not a blob (actually a %s): %s",
1872 typename(type), command_buf.buf);
1875 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
1876 free(p_uq);
1879 static void file_change_d(struct branch *b)
1881 const char *p = command_buf.buf + 2;
1882 char *p_uq;
1883 const char *endp;
1885 p_uq = unquote_c_style(p, &endp);
1886 if (p_uq) {
1887 if (*endp)
1888 die("Garbage after path in: %s", command_buf.buf);
1889 p = p_uq;
1891 tree_content_remove(&b->branch_tree, p, NULL);
1892 free(p_uq);
1895 static void file_change_cr(struct branch *b, int rename)
1897 const char *s, *d;
1898 char *s_uq, *d_uq;
1899 const char *endp;
1900 struct tree_entry leaf;
1902 s = command_buf.buf + 2;
1903 s_uq = unquote_c_style(s, &endp);
1904 if (s_uq) {
1905 if (*endp != ' ')
1906 die("Missing space after source: %s", command_buf.buf);
1908 else {
1909 endp = strchr(s, ' ');
1910 if (!endp)
1911 die("Missing space after source: %s", command_buf.buf);
1912 s_uq = xmalloc(endp - s + 1);
1913 memcpy(s_uq, s, endp - s);
1914 s_uq[endp - s] = 0;
1916 s = s_uq;
1918 endp++;
1919 if (!*endp)
1920 die("Missing dest: %s", command_buf.buf);
1922 d = endp;
1923 d_uq = unquote_c_style(d, &endp);
1924 if (d_uq) {
1925 if (*endp)
1926 die("Garbage after dest in: %s", command_buf.buf);
1927 d = d_uq;
1930 memset(&leaf, 0, sizeof(leaf));
1931 if (rename)
1932 tree_content_remove(&b->branch_tree, s, &leaf);
1933 else
1934 tree_content_get(&b->branch_tree, s, &leaf);
1935 if (!leaf.versions[1].mode)
1936 die("Path %s not in branch", s);
1937 tree_content_set(&b->branch_tree, d,
1938 leaf.versions[1].sha1,
1939 leaf.versions[1].mode,
1940 leaf.tree);
1942 free(s_uq);
1943 free(d_uq);
1946 static void file_change_deleteall(struct branch *b)
1948 release_tree_content_recursive(b->branch_tree.tree);
1949 hashclr(b->branch_tree.versions[0].sha1);
1950 hashclr(b->branch_tree.versions[1].sha1);
1951 load_tree(&b->branch_tree);
1954 static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
1956 if (!buf || size < 46)
1957 die("Not a valid commit: %s", sha1_to_hex(b->sha1));
1958 if (memcmp("tree ", buf, 5)
1959 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1960 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1961 hashcpy(b->branch_tree.versions[0].sha1,
1962 b->branch_tree.versions[1].sha1);
1965 static void cmd_from_existing(struct branch *b)
1967 if (is_null_sha1(b->sha1)) {
1968 hashclr(b->branch_tree.versions[0].sha1);
1969 hashclr(b->branch_tree.versions[1].sha1);
1970 } else {
1971 unsigned long size;
1972 char *buf;
1974 buf = read_object_with_reference(b->sha1,
1975 commit_type, &size, b->sha1);
1976 cmd_from_commit(b, buf, size);
1977 free(buf);
1981 static int cmd_from(struct branch *b)
1983 const char *from;
1984 struct branch *s;
1986 if (prefixcmp(command_buf.buf, "from "))
1987 return 0;
1989 if (b->branch_tree.tree) {
1990 release_tree_content_recursive(b->branch_tree.tree);
1991 b->branch_tree.tree = NULL;
1994 from = strchr(command_buf.buf, ' ') + 1;
1995 s = lookup_branch(from);
1996 if (b == s)
1997 die("Can't create a branch from itself: %s", b->name);
1998 else if (s) {
1999 unsigned char *t = s->branch_tree.versions[1].sha1;
2000 hashcpy(b->sha1, s->sha1);
2001 hashcpy(b->branch_tree.versions[0].sha1, t);
2002 hashcpy(b->branch_tree.versions[1].sha1, t);
2003 } else if (*from == ':') {
2004 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
2005 struct object_entry *oe = find_mark(idnum);
2006 if (oe->type != OBJ_COMMIT)
2007 die("Mark :%" PRIuMAX " not a commit", idnum);
2008 hashcpy(b->sha1, oe->sha1);
2009 if (oe->pack_id != MAX_PACK_ID) {
2010 unsigned long size;
2011 char *buf = gfi_unpack_entry(oe, &size);
2012 cmd_from_commit(b, buf, size);
2013 free(buf);
2014 } else
2015 cmd_from_existing(b);
2016 } else if (!get_sha1(from, b->sha1))
2017 cmd_from_existing(b);
2018 else
2019 die("Invalid ref name or SHA1 expression: %s", from);
2021 read_next_command();
2022 return 1;
2025 static struct hash_list *cmd_merge(unsigned int *count)
2027 struct hash_list *list = NULL, *n, *e = e;
2028 const char *from;
2029 struct branch *s;
2031 *count = 0;
2032 while (!prefixcmp(command_buf.buf, "merge ")) {
2033 from = strchr(command_buf.buf, ' ') + 1;
2034 n = xmalloc(sizeof(*n));
2035 s = lookup_branch(from);
2036 if (s)
2037 hashcpy(n->sha1, s->sha1);
2038 else if (*from == ':') {
2039 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
2040 struct object_entry *oe = find_mark(idnum);
2041 if (oe->type != OBJ_COMMIT)
2042 die("Mark :%" PRIuMAX " not a commit", idnum);
2043 hashcpy(n->sha1, oe->sha1);
2044 } else if (!get_sha1(from, n->sha1)) {
2045 unsigned long size;
2046 char *buf = read_object_with_reference(n->sha1,
2047 commit_type, &size, n->sha1);
2048 if (!buf || size < 46)
2049 die("Not a valid commit: %s", from);
2050 free(buf);
2051 } else
2052 die("Invalid ref name or SHA1 expression: %s", from);
2054 n->next = NULL;
2055 if (list)
2056 e->next = n;
2057 else
2058 list = n;
2059 e = n;
2060 (*count)++;
2061 read_next_command();
2063 return list;
2066 static void cmd_new_commit(void)
2068 struct branch *b;
2069 void *msg;
2070 size_t msglen;
2071 char *sp;
2072 char *author = NULL;
2073 char *committer = NULL;
2074 struct hash_list *merge_list = NULL;
2075 unsigned int merge_count;
2077 /* Obtain the branch name from the rest of our command */
2078 sp = strchr(command_buf.buf, ' ') + 1;
2079 b = lookup_branch(sp);
2080 if (!b)
2081 b = new_branch(sp);
2083 read_next_command();
2084 cmd_mark();
2085 if (!prefixcmp(command_buf.buf, "author ")) {
2086 author = parse_ident(command_buf.buf + 7);
2087 read_next_command();
2089 if (!prefixcmp(command_buf.buf, "committer ")) {
2090 committer = parse_ident(command_buf.buf + 10);
2091 read_next_command();
2093 if (!committer)
2094 die("Expected committer but didn't get one");
2095 msg = cmd_data(&msglen);
2096 read_next_command();
2097 cmd_from(b);
2098 merge_list = cmd_merge(&merge_count);
2100 /* ensure the branch is active/loaded */
2101 if (!b->branch_tree.tree || !max_active_branches) {
2102 unload_one_branch();
2103 load_branch(b);
2106 /* file_change* */
2107 while (!command_buf.eof && command_buf.len > 1) {
2108 if (!prefixcmp(command_buf.buf, "M "))
2109 file_change_m(b);
2110 else if (!prefixcmp(command_buf.buf, "D "))
2111 file_change_d(b);
2112 else if (!prefixcmp(command_buf.buf, "R "))
2113 file_change_cr(b, 1);
2114 else if (!prefixcmp(command_buf.buf, "C "))
2115 file_change_cr(b, 0);
2116 else if (!strcmp("deleteall", command_buf.buf))
2117 file_change_deleteall(b);
2118 else {
2119 unread_command_buf = 1;
2120 break;
2122 read_next_command();
2125 /* build the tree and the commit */
2126 store_tree(&b->branch_tree);
2127 hashcpy(b->branch_tree.versions[0].sha1,
2128 b->branch_tree.versions[1].sha1);
2129 size_dbuf(&new_data, 114 + msglen
2130 + merge_count * 49
2131 + (author
2132 ? strlen(author) + strlen(committer)
2133 : 2 * strlen(committer)));
2134 sp = new_data.buffer;
2135 sp += sprintf(sp, "tree %s\n",
2136 sha1_to_hex(b->branch_tree.versions[1].sha1));
2137 if (!is_null_sha1(b->sha1))
2138 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
2139 while (merge_list) {
2140 struct hash_list *next = merge_list->next;
2141 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
2142 free(merge_list);
2143 merge_list = next;
2145 sp += sprintf(sp, "author %s\n", author ? author : committer);
2146 sp += sprintf(sp, "committer %s\n", committer);
2147 *sp++ = '\n';
2148 memcpy(sp, msg, msglen);
2149 sp += msglen;
2150 free(author);
2151 free(committer);
2152 free(msg);
2154 if (!store_object(OBJ_COMMIT,
2155 new_data.buffer, sp - (char*)new_data.buffer,
2156 NULL, b->sha1, next_mark))
2157 b->pack_id = pack_id;
2158 b->last_commit = object_count_by_type[OBJ_COMMIT];
2161 static void cmd_new_tag(void)
2163 char *sp;
2164 const char *from;
2165 char *tagger;
2166 struct branch *s;
2167 void *msg;
2168 size_t msglen;
2169 struct tag *t;
2170 uintmax_t from_mark = 0;
2171 unsigned char sha1[20];
2173 /* Obtain the new tag name from the rest of our command */
2174 sp = strchr(command_buf.buf, ' ') + 1;
2175 t = pool_alloc(sizeof(struct tag));
2176 t->next_tag = NULL;
2177 t->name = pool_strdup(sp);
2178 if (last_tag)
2179 last_tag->next_tag = t;
2180 else
2181 first_tag = t;
2182 last_tag = t;
2183 read_next_command();
2185 /* from ... */
2186 if (prefixcmp(command_buf.buf, "from "))
2187 die("Expected from command, got %s", command_buf.buf);
2188 from = strchr(command_buf.buf, ' ') + 1;
2189 s = lookup_branch(from);
2190 if (s) {
2191 hashcpy(sha1, s->sha1);
2192 } else if (*from == ':') {
2193 struct object_entry *oe;
2194 from_mark = strtoumax(from + 1, NULL, 10);
2195 oe = find_mark(from_mark);
2196 if (oe->type != OBJ_COMMIT)
2197 die("Mark :%" PRIuMAX " not a commit", from_mark);
2198 hashcpy(sha1, oe->sha1);
2199 } else if (!get_sha1(from, sha1)) {
2200 unsigned long size;
2201 char *buf;
2203 buf = read_object_with_reference(sha1,
2204 commit_type, &size, sha1);
2205 if (!buf || size < 46)
2206 die("Not a valid commit: %s", from);
2207 free(buf);
2208 } else
2209 die("Invalid ref name or SHA1 expression: %s", from);
2210 read_next_command();
2212 /* tagger ... */
2213 if (prefixcmp(command_buf.buf, "tagger "))
2214 die("Expected tagger command, got %s", command_buf.buf);
2215 tagger = parse_ident(command_buf.buf + 7);
2217 /* tag payload/message */
2218 read_next_command();
2219 msg = cmd_data(&msglen);
2221 /* build the tag object */
2222 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
2223 sp = new_data.buffer;
2224 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
2225 sp += sprintf(sp, "type %s\n", commit_type);
2226 sp += sprintf(sp, "tag %s\n", t->name);
2227 sp += sprintf(sp, "tagger %s\n", tagger);
2228 *sp++ = '\n';
2229 memcpy(sp, msg, msglen);
2230 sp += msglen;
2231 free(tagger);
2232 free(msg);
2234 if (store_object(OBJ_TAG, new_data.buffer,
2235 sp - (char*)new_data.buffer,
2236 NULL, t->sha1, 0))
2237 t->pack_id = MAX_PACK_ID;
2238 else
2239 t->pack_id = pack_id;
2242 static void cmd_reset_branch(void)
2244 struct branch *b;
2245 char *sp;
2247 /* Obtain the branch name from the rest of our command */
2248 sp = strchr(command_buf.buf, ' ') + 1;
2249 b = lookup_branch(sp);
2250 if (b) {
2251 hashclr(b->sha1);
2252 hashclr(b->branch_tree.versions[0].sha1);
2253 hashclr(b->branch_tree.versions[1].sha1);
2254 if (b->branch_tree.tree) {
2255 release_tree_content_recursive(b->branch_tree.tree);
2256 b->branch_tree.tree = NULL;
2259 else
2260 b = new_branch(sp);
2261 read_next_command();
2262 if (!cmd_from(b) && command_buf.len > 1)
2263 unread_command_buf = 1;
2266 static void cmd_checkpoint(void)
2268 if (object_count) {
2269 cycle_packfile();
2270 dump_branches();
2271 dump_tags();
2272 dump_marks();
2274 skip_optional_lf();
2277 static void cmd_progress(void)
2279 fwrite(command_buf.buf, 1, command_buf.len - 1, stdout);
2280 fputc('\n', stdout);
2281 fflush(stdout);
2282 skip_optional_lf();
2285 static void import_marks(const char *input_file)
2287 char line[512];
2288 FILE *f = fopen(input_file, "r");
2289 if (!f)
2290 die("cannot read %s: %s", input_file, strerror(errno));
2291 while (fgets(line, sizeof(line), f)) {
2292 uintmax_t mark;
2293 char *end;
2294 unsigned char sha1[20];
2295 struct object_entry *e;
2297 end = strchr(line, '\n');
2298 if (line[0] != ':' || !end)
2299 die("corrupt mark line: %s", line);
2300 *end = 0;
2301 mark = strtoumax(line + 1, &end, 10);
2302 if (!mark || end == line + 1
2303 || *end != ' ' || get_sha1(end + 1, sha1))
2304 die("corrupt mark line: %s", line);
2305 e = find_object(sha1);
2306 if (!e) {
2307 enum object_type type = sha1_object_info(sha1, NULL);
2308 if (type < 0)
2309 die("object not found: %s", sha1_to_hex(sha1));
2310 e = insert_object(sha1);
2311 e->type = type;
2312 e->pack_id = MAX_PACK_ID;
2313 e->offset = 1; /* just not zero! */
2315 insert_mark(mark, e);
2317 fclose(f);
2320 static const char fast_import_usage[] =
2321 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2323 int main(int argc, const char **argv)
2325 unsigned int i, show_stats = 1;
2327 git_config(git_default_config);
2328 alloc_objects(object_entry_alloc);
2329 strbuf_init(&command_buf);
2330 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2331 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2332 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2333 marks = pool_calloc(1, sizeof(struct mark_set));
2335 for (i = 1; i < argc; i++) {
2336 const char *a = argv[i];
2338 if (*a != '-' || !strcmp(a, "--"))
2339 break;
2340 else if (!prefixcmp(a, "--date-format=")) {
2341 const char *fmt = a + 14;
2342 if (!strcmp(fmt, "raw"))
2343 whenspec = WHENSPEC_RAW;
2344 else if (!strcmp(fmt, "rfc2822"))
2345 whenspec = WHENSPEC_RFC2822;
2346 else if (!strcmp(fmt, "now"))
2347 whenspec = WHENSPEC_NOW;
2348 else
2349 die("unknown --date-format argument %s", fmt);
2351 else if (!prefixcmp(a, "--max-pack-size="))
2352 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
2353 else if (!prefixcmp(a, "--depth=")) {
2354 max_depth = strtoul(a + 8, NULL, 0);
2355 if (max_depth > MAX_DEPTH)
2356 die("--depth cannot exceed %u", MAX_DEPTH);
2358 else if (!prefixcmp(a, "--active-branches="))
2359 max_active_branches = strtoul(a + 18, NULL, 0);
2360 else if (!prefixcmp(a, "--import-marks="))
2361 import_marks(a + 15);
2362 else if (!prefixcmp(a, "--export-marks="))
2363 mark_file = a + 15;
2364 else if (!prefixcmp(a, "--export-pack-edges=")) {
2365 if (pack_edges)
2366 fclose(pack_edges);
2367 pack_edges = fopen(a + 20, "a");
2368 if (!pack_edges)
2369 die("Cannot open %s: %s", a + 20, strerror(errno));
2370 } else if (!strcmp(a, "--force"))
2371 force_update = 1;
2372 else if (!strcmp(a, "--quiet"))
2373 show_stats = 0;
2374 else if (!strcmp(a, "--stats"))
2375 show_stats = 1;
2376 else
2377 die("unknown option %s", a);
2379 if (i != argc)
2380 usage(fast_import_usage);
2382 rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
2383 for (i = 0; i < (cmd_save - 1); i++)
2384 rc_free[i].next = &rc_free[i + 1];
2385 rc_free[cmd_save - 1].next = NULL;
2387 prepare_packed_git();
2388 start_packfile();
2389 set_die_routine(die_nicely);
2390 for (;;) {
2391 read_next_command();
2392 if (command_buf.eof)
2393 break;
2394 else if (!strcmp("blob", command_buf.buf))
2395 cmd_new_blob();
2396 else if (!prefixcmp(command_buf.buf, "commit "))
2397 cmd_new_commit();
2398 else if (!prefixcmp(command_buf.buf, "tag "))
2399 cmd_new_tag();
2400 else if (!prefixcmp(command_buf.buf, "reset "))
2401 cmd_reset_branch();
2402 else if (!strcmp("checkpoint", command_buf.buf))
2403 cmd_checkpoint();
2404 else if (!prefixcmp(command_buf.buf, "progress "))
2405 cmd_progress();
2406 else
2407 die("Unsupported command: %s", command_buf.buf);
2409 end_packfile();
2411 dump_branches();
2412 dump_tags();
2413 unkeep_all_packs();
2414 dump_marks();
2416 if (pack_edges)
2417 fclose(pack_edges);
2419 if (show_stats) {
2420 uintmax_t total_count = 0, duplicate_count = 0;
2421 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
2422 total_count += object_count_by_type[i];
2423 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
2424 duplicate_count += duplicate_count_by_type[i];
2426 fprintf(stderr, "%s statistics:\n", argv[0]);
2427 fprintf(stderr, "---------------------------------------------------------------------\n");
2428 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
2429 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
2430 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]);
2431 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]);
2432 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]);
2433 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]);
2434 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
2435 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
2436 fprintf(stderr, " atoms: %10u\n", atom_cnt);
2437 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
2438 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
2439 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
2440 fprintf(stderr, "---------------------------------------------------------------------\n");
2441 pack_report();
2442 fprintf(stderr, "---------------------------------------------------------------------\n");
2443 fprintf(stderr, "\n");
2446 return failure ? 1 : 0;