Don't support shell-quoted refnames in fast-import.
[git/dscho.git] / fast-import.c
blobe6342386fc709ff661375b9e25317da0c780eb2f
1 /*
2 Format of STDIN stream:
4 stream ::= cmd*;
6 cmd ::= new_blob
7 | new_commit
8 | new_tag
9 | reset_branch
10 | checkpoint
13 new_blob ::= 'blob' lf
14 mark?
15 file_content;
16 file_content ::= data;
18 new_commit ::= 'commit' sp ref_str lf
19 mark?
20 ('author' sp name '<' email '>' ts tz lf)?
21 'committer' sp name '<' email '>' ts tz lf
22 commit_msg
23 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
24 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
25 file_change*
26 lf;
27 commit_msg ::= data;
29 file_change ::= file_del | file_obm | file_inm;
30 file_del ::= 'D' sp path_str lf;
31 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
32 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
33 data;
35 new_tag ::= 'tag' sp tag_str lf
36 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
37 'tagger' sp name '<' email '>' ts tz lf
38 tag_msg;
39 tag_msg ::= data;
41 reset_branch ::= 'reset' sp ref_str lf
42 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
43 lf;
45 checkpoint ::= 'checkpoint' lf
46 lf;
48 # note: the first idnum in a stream should be 1 and subsequent
49 # idnums should not have gaps between values as this will cause
50 # the stream parser to reserve space for the gapped values. An
51 # idnum can be updated in the future to a new object by issuing
52 # a new mark directive with the old idnum.
54 mark ::= 'mark' sp idnum lf;
55 data ::= (delimited_data | exact_data)
56 lf;
58 # note: delim may be any string but must not contain lf.
59 # data_line may contain any data but must not be exactly
60 # delim.
61 delimited_data ::= 'data' sp '<<' delim lf
62 (data_line lf)*
63 delim lf;
65 # note: declen indicates the length of binary_data in bytes.
66 # declen does not include the lf preceeding the binary data.
68 exact_data ::= 'data' sp declen lf
69 binary_data;
71 # note: quoted strings are C-style quoting supporting \c for
72 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
73 # is the signed byte value in octal. Note that the only
74 # characters which must actually be escaped to protect the
75 # stream formatting is: \, " and LF. Otherwise these values
76 # are UTF8.
78 ref_str ::= ref;
79 sha1exp_str ::= sha1exp;
80 tag_str ::= tag;
81 path_str ::= path | '"' quoted(path) '"' ;
82 mode ::= '100644' | '644'
83 | '100755' | '755'
84 | '140000'
87 declen ::= # unsigned 32 bit value, ascii base10 notation;
88 bigint ::= # unsigned integer value, ascii base10 notation;
89 binary_data ::= # file content, not interpreted;
91 sp ::= # ASCII space character;
92 lf ::= # ASCII newline (LF) character;
94 # note: a colon (':') must precede the numerical value assigned to
95 # an idnum. This is to distinguish it from a ref or tag name as
96 # GIT does not permit ':' in ref or tag strings.
98 idnum ::= ':' bigint;
99 path ::= # GIT style file path, e.g. "a/b/c";
100 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
101 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
102 sha1exp ::= # Any valid GIT SHA1 expression;
103 hexsha1 ::= # SHA1 in hexadecimal format;
105 # note: name and email are UTF8 strings, however name must not
106 # contain '<' or lf and email must not contain any of the
107 # following: '<', '>', lf.
109 name ::= # valid GIT author/committer name;
110 email ::= # valid GIT author/committer email;
111 ts ::= # time since the epoch in seconds, ascii base10 notation;
112 tz ::= # GIT style timezone;
115 #include "builtin.h"
116 #include "cache.h"
117 #include "object.h"
118 #include "blob.h"
119 #include "tree.h"
120 #include "delta.h"
121 #include "pack.h"
122 #include "refs.h"
123 #include "csum-file.h"
124 #include "strbuf.h"
125 #include "quote.h"
127 #define PACK_ID_BITS 16
128 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
130 struct object_entry
132 struct object_entry *next;
133 uint32_t offset;
134 unsigned type : TYPE_BITS;
135 unsigned pack_id : PACK_ID_BITS;
136 unsigned char sha1[20];
139 struct object_entry_pool
141 struct object_entry_pool *next_pool;
142 struct object_entry *next_free;
143 struct object_entry *end;
144 struct object_entry entries[FLEX_ARRAY]; /* more */
147 struct mark_set
149 union {
150 struct object_entry *marked[1024];
151 struct mark_set *sets[1024];
152 } data;
153 unsigned int shift;
156 struct last_object
158 void *data;
159 unsigned long len;
160 uint32_t offset;
161 unsigned int depth;
162 unsigned no_free:1;
165 struct mem_pool
167 struct mem_pool *next_pool;
168 char *next_free;
169 char *end;
170 char space[FLEX_ARRAY]; /* more */
173 struct atom_str
175 struct atom_str *next_atom;
176 unsigned short str_len;
177 char str_dat[FLEX_ARRAY]; /* more */
180 struct tree_content;
181 struct tree_entry
183 struct tree_content *tree;
184 struct atom_str* name;
185 struct tree_entry_ms
187 uint16_t mode;
188 unsigned char sha1[20];
189 } versions[2];
192 struct tree_content
194 unsigned int entry_capacity; /* must match avail_tree_content */
195 unsigned int entry_count;
196 unsigned int delta_depth;
197 struct tree_entry *entries[FLEX_ARRAY]; /* more */
200 struct avail_tree_content
202 unsigned int entry_capacity; /* must match tree_content */
203 struct avail_tree_content *next_avail;
206 struct branch
208 struct branch *table_next_branch;
209 struct branch *active_next_branch;
210 const char *name;
211 struct tree_entry branch_tree;
212 uintmax_t last_commit;
213 unsigned int pack_id;
214 unsigned char sha1[20];
217 struct tag
219 struct tag *next_tag;
220 const char *name;
221 unsigned int pack_id;
222 unsigned char sha1[20];
225 struct dbuf
227 void *buffer;
228 size_t capacity;
231 struct hash_list
233 struct hash_list *next;
234 unsigned char sha1[20];
237 /* Configured limits on output */
238 static unsigned long max_depth = 10;
239 static unsigned long max_packsize = (1LL << 32) - 1;
241 /* Stats and misc. counters */
242 static uintmax_t alloc_count;
243 static uintmax_t marks_set_count;
244 static uintmax_t object_count_by_type[1 << TYPE_BITS];
245 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
246 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
247 static unsigned long object_count;
248 static unsigned long branch_count;
249 static unsigned long branch_load_count;
251 /* Memory pools */
252 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
253 static size_t total_allocd;
254 static struct mem_pool *mem_pool;
256 /* Atom management */
257 static unsigned int atom_table_sz = 4451;
258 static unsigned int atom_cnt;
259 static struct atom_str **atom_table;
261 /* The .pack file being generated */
262 static unsigned int pack_id;
263 static struct packed_git *pack_data;
264 static struct packed_git **all_packs;
265 static unsigned long pack_size;
267 /* Table of objects we've written. */
268 static unsigned int object_entry_alloc = 5000;
269 static struct object_entry_pool *blocks;
270 static struct object_entry *object_table[1 << 16];
271 static struct mark_set *marks;
272 static const char* mark_file;
274 /* Our last blob */
275 static struct last_object last_blob;
277 /* Tree management */
278 static unsigned int tree_entry_alloc = 1000;
279 static void *avail_tree_entry;
280 static unsigned int avail_tree_table_sz = 100;
281 static struct avail_tree_content **avail_tree_table;
282 static struct dbuf old_tree;
283 static struct dbuf new_tree;
285 /* Branch data */
286 static unsigned long max_active_branches = 5;
287 static unsigned long cur_active_branches;
288 static unsigned long branch_table_sz = 1039;
289 static struct branch **branch_table;
290 static struct branch *active_branches;
292 /* Tag data */
293 static struct tag *first_tag;
294 static struct tag *last_tag;
296 /* Input stream parsing */
297 static struct strbuf command_buf;
298 static uintmax_t next_mark;
299 static struct dbuf new_data;
300 static FILE* branch_log;
303 static void alloc_objects(unsigned int cnt)
305 struct object_entry_pool *b;
307 b = xmalloc(sizeof(struct object_entry_pool)
308 + cnt * sizeof(struct object_entry));
309 b->next_pool = blocks;
310 b->next_free = b->entries;
311 b->end = b->entries + cnt;
312 blocks = b;
313 alloc_count += cnt;
316 static struct object_entry* new_object(unsigned char *sha1)
318 struct object_entry *e;
320 if (blocks->next_free == blocks->end)
321 alloc_objects(object_entry_alloc);
323 e = blocks->next_free++;
324 hashcpy(e->sha1, sha1);
325 return e;
328 static struct object_entry* find_object(unsigned char *sha1)
330 unsigned int h = sha1[0] << 8 | sha1[1];
331 struct object_entry *e;
332 for (e = object_table[h]; e; e = e->next)
333 if (!hashcmp(sha1, e->sha1))
334 return e;
335 return NULL;
338 static struct object_entry* insert_object(unsigned char *sha1)
340 unsigned int h = sha1[0] << 8 | sha1[1];
341 struct object_entry *e = object_table[h];
342 struct object_entry *p = NULL;
344 while (e) {
345 if (!hashcmp(sha1, e->sha1))
346 return e;
347 p = e;
348 e = e->next;
351 e = new_object(sha1);
352 e->next = NULL;
353 e->offset = 0;
354 if (p)
355 p->next = e;
356 else
357 object_table[h] = e;
358 return e;
361 static unsigned int hc_str(const char *s, size_t len)
363 unsigned int r = 0;
364 while (len-- > 0)
365 r = r * 31 + *s++;
366 return r;
369 static void* pool_alloc(size_t len)
371 struct mem_pool *p;
372 void *r;
374 for (p = mem_pool; p; p = p->next_pool)
375 if ((p->end - p->next_free >= len))
376 break;
378 if (!p) {
379 if (len >= (mem_pool_alloc/2)) {
380 total_allocd += len;
381 return xmalloc(len);
383 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
384 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
385 p->next_pool = mem_pool;
386 p->next_free = p->space;
387 p->end = p->next_free + mem_pool_alloc;
388 mem_pool = p;
391 r = p->next_free;
392 /* round out to a pointer alignment */
393 if (len & (sizeof(void*) - 1))
394 len += sizeof(void*) - (len & (sizeof(void*) - 1));
395 p->next_free += len;
396 return r;
399 static void* pool_calloc(size_t count, size_t size)
401 size_t len = count * size;
402 void *r = pool_alloc(len);
403 memset(r, 0, len);
404 return r;
407 static char* pool_strdup(const char *s)
409 char *r = pool_alloc(strlen(s) + 1);
410 strcpy(r, s);
411 return r;
414 static void size_dbuf(struct dbuf *b, size_t maxlen)
416 if (b->buffer) {
417 if (b->capacity >= maxlen)
418 return;
419 free(b->buffer);
421 b->capacity = ((maxlen / 1024) + 1) * 1024;
422 b->buffer = xmalloc(b->capacity);
425 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
427 struct mark_set *s = marks;
428 while ((idnum >> s->shift) >= 1024) {
429 s = pool_calloc(1, sizeof(struct mark_set));
430 s->shift = marks->shift + 10;
431 s->data.sets[0] = marks;
432 marks = s;
434 while (s->shift) {
435 uintmax_t i = idnum >> s->shift;
436 idnum -= i << s->shift;
437 if (!s->data.sets[i]) {
438 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
439 s->data.sets[i]->shift = s->shift - 10;
441 s = s->data.sets[i];
443 if (!s->data.marked[idnum])
444 marks_set_count++;
445 s->data.marked[idnum] = oe;
448 static struct object_entry* find_mark(uintmax_t idnum)
450 uintmax_t orig_idnum = idnum;
451 struct mark_set *s = marks;
452 struct object_entry *oe = NULL;
453 if ((idnum >> s->shift) < 1024) {
454 while (s && s->shift) {
455 uintmax_t i = idnum >> s->shift;
456 idnum -= i << s->shift;
457 s = s->data.sets[i];
459 if (s)
460 oe = s->data.marked[idnum];
462 if (!oe)
463 die("mark :%ju not declared", orig_idnum);
464 return oe;
467 static struct atom_str* to_atom(const char *s, unsigned short len)
469 unsigned int hc = hc_str(s, len) % atom_table_sz;
470 struct atom_str *c;
472 for (c = atom_table[hc]; c; c = c->next_atom)
473 if (c->str_len == len && !strncmp(s, c->str_dat, len))
474 return c;
476 c = pool_alloc(sizeof(struct atom_str) + len + 1);
477 c->str_len = len;
478 strncpy(c->str_dat, s, len);
479 c->str_dat[len] = 0;
480 c->next_atom = atom_table[hc];
481 atom_table[hc] = c;
482 atom_cnt++;
483 return c;
486 static struct branch* lookup_branch(const char *name)
488 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
489 struct branch *b;
491 for (b = branch_table[hc]; b; b = b->table_next_branch)
492 if (!strcmp(name, b->name))
493 return b;
494 return NULL;
497 static struct branch* new_branch(const char *name)
499 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
500 struct branch* b = lookup_branch(name);
502 if (b)
503 die("Invalid attempt to create duplicate branch: %s", name);
504 if (check_ref_format(name))
505 die("Branch name doesn't conform to GIT standards: %s", name);
507 b = pool_calloc(1, sizeof(struct branch));
508 b->name = pool_strdup(name);
509 b->table_next_branch = branch_table[hc];
510 b->branch_tree.versions[0].mode = S_IFDIR;
511 b->branch_tree.versions[1].mode = S_IFDIR;
512 b->pack_id = MAX_PACK_ID;
513 branch_table[hc] = b;
514 branch_count++;
515 return b;
518 static unsigned int hc_entries(unsigned int cnt)
520 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
521 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
524 static struct tree_content* new_tree_content(unsigned int cnt)
526 struct avail_tree_content *f, *l = NULL;
527 struct tree_content *t;
528 unsigned int hc = hc_entries(cnt);
530 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
531 if (f->entry_capacity >= cnt)
532 break;
534 if (f) {
535 if (l)
536 l->next_avail = f->next_avail;
537 else
538 avail_tree_table[hc] = f->next_avail;
539 } else {
540 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
541 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
542 f->entry_capacity = cnt;
545 t = (struct tree_content*)f;
546 t->entry_count = 0;
547 t->delta_depth = 0;
548 return t;
551 static void release_tree_entry(struct tree_entry *e);
552 static void release_tree_content(struct tree_content *t)
554 struct avail_tree_content *f = (struct avail_tree_content*)t;
555 unsigned int hc = hc_entries(f->entry_capacity);
556 f->next_avail = avail_tree_table[hc];
557 avail_tree_table[hc] = f;
560 static void release_tree_content_recursive(struct tree_content *t)
562 unsigned int i;
563 for (i = 0; i < t->entry_count; i++)
564 release_tree_entry(t->entries[i]);
565 release_tree_content(t);
568 static struct tree_content* grow_tree_content(
569 struct tree_content *t,
570 int amt)
572 struct tree_content *r = new_tree_content(t->entry_count + amt);
573 r->entry_count = t->entry_count;
574 r->delta_depth = t->delta_depth;
575 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
576 release_tree_content(t);
577 return r;
580 static struct tree_entry* new_tree_entry(void)
582 struct tree_entry *e;
584 if (!avail_tree_entry) {
585 unsigned int n = tree_entry_alloc;
586 total_allocd += n * sizeof(struct tree_entry);
587 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
588 while (n-- > 1) {
589 *((void**)e) = e + 1;
590 e++;
592 *((void**)e) = NULL;
595 e = avail_tree_entry;
596 avail_tree_entry = *((void**)e);
597 return e;
600 static void release_tree_entry(struct tree_entry *e)
602 if (e->tree)
603 release_tree_content_recursive(e->tree);
604 *((void**)e) = avail_tree_entry;
605 avail_tree_entry = e;
608 static void start_packfile(void)
610 static char tmpfile[PATH_MAX];
611 struct packed_git *p;
612 struct pack_header hdr;
613 int pack_fd;
615 snprintf(tmpfile, sizeof(tmpfile),
616 "%s/pack_XXXXXX", get_object_directory());
617 pack_fd = mkstemp(tmpfile);
618 if (pack_fd < 0)
619 die("Can't create %s: %s", tmpfile, strerror(errno));
620 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
621 strcpy(p->pack_name, tmpfile);
622 p->pack_fd = pack_fd;
624 hdr.hdr_signature = htonl(PACK_SIGNATURE);
625 hdr.hdr_version = htonl(2);
626 hdr.hdr_entries = 0;
627 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
629 pack_data = p;
630 pack_size = sizeof(hdr);
631 object_count = 0;
633 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
634 all_packs[pack_id] = p;
637 static void fixup_header_footer(void)
639 static const int buf_sz = 128 * 1024;
640 int pack_fd = pack_data->pack_fd;
641 SHA_CTX c;
642 struct pack_header hdr;
643 char *buf;
645 if (lseek(pack_fd, 0, SEEK_SET) != 0)
646 die("Failed seeking to start: %s", strerror(errno));
647 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
648 die("Unable to reread header of %s", pack_data->pack_name);
649 if (lseek(pack_fd, 0, SEEK_SET) != 0)
650 die("Failed seeking to start: %s", strerror(errno));
651 hdr.hdr_entries = htonl(object_count);
652 write_or_die(pack_fd, &hdr, sizeof(hdr));
654 SHA1_Init(&c);
655 SHA1_Update(&c, &hdr, sizeof(hdr));
657 buf = xmalloc(buf_sz);
658 for (;;) {
659 size_t n = xread(pack_fd, buf, buf_sz);
660 if (!n)
661 break;
662 if (n < 0)
663 die("Failed to checksum %s", pack_data->pack_name);
664 SHA1_Update(&c, buf, n);
666 free(buf);
668 SHA1_Final(pack_data->sha1, &c);
669 write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
670 close(pack_fd);
673 static int oecmp (const void *a_, const void *b_)
675 struct object_entry *a = *((struct object_entry**)a_);
676 struct object_entry *b = *((struct object_entry**)b_);
677 return hashcmp(a->sha1, b->sha1);
680 static char* create_index(void)
682 static char tmpfile[PATH_MAX];
683 SHA_CTX ctx;
684 struct sha1file *f;
685 struct object_entry **idx, **c, **last, *e;
686 struct object_entry_pool *o;
687 uint32_t array[256];
688 int i, idx_fd;
690 /* Build the sorted table of object IDs. */
691 idx = xmalloc(object_count * sizeof(struct object_entry*));
692 c = idx;
693 for (o = blocks; o; o = o->next_pool)
694 for (e = o->next_free; e-- != o->entries;)
695 if (pack_id == e->pack_id)
696 *c++ = e;
697 last = idx + object_count;
698 if (c != last)
699 die("internal consistency error creating the index");
700 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
702 /* Generate the fan-out array. */
703 c = idx;
704 for (i = 0; i < 256; i++) {
705 struct object_entry **next = c;;
706 while (next < last) {
707 if ((*next)->sha1[0] != i)
708 break;
709 next++;
711 array[i] = htonl(next - idx);
712 c = next;
715 snprintf(tmpfile, sizeof(tmpfile),
716 "%s/index_XXXXXX", get_object_directory());
717 idx_fd = mkstemp(tmpfile);
718 if (idx_fd < 0)
719 die("Can't create %s: %s", tmpfile, strerror(errno));
720 f = sha1fd(idx_fd, tmpfile);
721 sha1write(f, array, 256 * sizeof(int));
722 SHA1_Init(&ctx);
723 for (c = idx; c != last; c++) {
724 uint32_t offset = htonl((*c)->offset);
725 sha1write(f, &offset, 4);
726 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
727 SHA1_Update(&ctx, (*c)->sha1, 20);
729 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
730 sha1close(f, NULL, 1);
731 free(idx);
732 SHA1_Final(pack_data->sha1, &ctx);
733 return tmpfile;
736 static char* keep_pack(char *curr_index_name)
738 static char name[PATH_MAX];
739 static char *keep_msg = "fast-import";
740 int keep_fd;
742 chmod(pack_data->pack_name, 0444);
743 chmod(curr_index_name, 0444);
745 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
746 get_object_directory(), sha1_to_hex(pack_data->sha1));
747 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
748 if (keep_fd < 0)
749 die("cannot create keep file");
750 write(keep_fd, keep_msg, strlen(keep_msg));
751 close(keep_fd);
753 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
754 get_object_directory(), sha1_to_hex(pack_data->sha1));
755 if (move_temp_to_file(pack_data->pack_name, name))
756 die("cannot store pack file");
758 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
759 get_object_directory(), sha1_to_hex(pack_data->sha1));
760 if (move_temp_to_file(curr_index_name, name))
761 die("cannot store index file");
762 return name;
765 static void unkeep_all_packs(void)
767 static char name[PATH_MAX];
768 int k;
770 for (k = 0; k < pack_id; k++) {
771 struct packed_git *p = all_packs[k];
772 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
773 get_object_directory(), sha1_to_hex(p->sha1));
774 unlink(name);
778 static void end_packfile(void)
780 struct packed_git *old_p = pack_data, *new_p;
782 if (object_count) {
783 char *idx_name;
784 int i;
785 struct branch *b;
786 struct tag *t;
788 fixup_header_footer();
789 idx_name = keep_pack(create_index());
791 /* Register the packfile with core git's machinary. */
792 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
793 if (!new_p)
794 die("core git rejected index %s", idx_name);
795 new_p->windows = old_p->windows;
796 all_packs[pack_id] = new_p;
797 install_packed_git(new_p);
799 /* Print the boundary */
800 fprintf(stdout, "%s:", new_p->pack_name);
801 for (i = 0; i < branch_table_sz; i++) {
802 for (b = branch_table[i]; b; b = b->table_next_branch) {
803 if (b->pack_id == pack_id)
804 fprintf(stdout, " %s", sha1_to_hex(b->sha1));
807 for (t = first_tag; t; t = t->next_tag) {
808 if (t->pack_id == pack_id)
809 fprintf(stdout, " %s", sha1_to_hex(t->sha1));
811 fputc('\n', stdout);
813 pack_id++;
815 else
816 unlink(old_p->pack_name);
817 free(old_p);
819 /* We can't carry a delta across packfiles. */
820 free(last_blob.data);
821 last_blob.data = NULL;
822 last_blob.len = 0;
823 last_blob.offset = 0;
824 last_blob.depth = 0;
827 static void checkpoint(void)
829 end_packfile();
830 start_packfile();
833 static size_t encode_header(
834 enum object_type type,
835 size_t size,
836 unsigned char *hdr)
838 int n = 1;
839 unsigned char c;
841 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
842 die("bad type %d", type);
844 c = (type << 4) | (size & 15);
845 size >>= 4;
846 while (size) {
847 *hdr++ = c | 0x80;
848 c = size & 0x7f;
849 size >>= 7;
850 n++;
852 *hdr = c;
853 return n;
856 static int store_object(
857 enum object_type type,
858 void *dat,
859 size_t datlen,
860 struct last_object *last,
861 unsigned char *sha1out,
862 uintmax_t mark)
864 void *out, *delta;
865 struct object_entry *e;
866 unsigned char hdr[96];
867 unsigned char sha1[20];
868 unsigned long hdrlen, deltalen;
869 SHA_CTX c;
870 z_stream s;
872 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
873 SHA1_Init(&c);
874 SHA1_Update(&c, hdr, hdrlen);
875 SHA1_Update(&c, dat, datlen);
876 SHA1_Final(sha1, &c);
877 if (sha1out)
878 hashcpy(sha1out, sha1);
880 e = insert_object(sha1);
881 if (mark)
882 insert_mark(mark, e);
883 if (e->offset) {
884 duplicate_count_by_type[type]++;
885 return 1;
888 if (last && last->data && last->depth < max_depth) {
889 delta = diff_delta(last->data, last->len,
890 dat, datlen,
891 &deltalen, 0);
892 if (delta && deltalen >= datlen) {
893 free(delta);
894 delta = NULL;
896 } else
897 delta = NULL;
899 memset(&s, 0, sizeof(s));
900 deflateInit(&s, zlib_compression_level);
901 if (delta) {
902 s.next_in = delta;
903 s.avail_in = deltalen;
904 } else {
905 s.next_in = dat;
906 s.avail_in = datlen;
908 s.avail_out = deflateBound(&s, s.avail_in);
909 s.next_out = out = xmalloc(s.avail_out);
910 while (deflate(&s, Z_FINISH) == Z_OK)
911 /* nothing */;
912 deflateEnd(&s);
914 /* Determine if we should auto-checkpoint. */
915 if ((pack_size + 60 + s.total_out) > max_packsize
916 || (pack_size + 60 + s.total_out) < pack_size) {
918 /* This new object needs to *not* have the current pack_id. */
919 e->pack_id = pack_id + 1;
920 checkpoint();
922 /* We cannot carry a delta into the new pack. */
923 if (delta) {
924 free(delta);
925 delta = NULL;
927 memset(&s, 0, sizeof(s));
928 deflateInit(&s, zlib_compression_level);
929 s.next_in = dat;
930 s.avail_in = datlen;
931 s.avail_out = deflateBound(&s, s.avail_in);
932 s.next_out = out = xrealloc(out, s.avail_out);
933 while (deflate(&s, Z_FINISH) == Z_OK)
934 /* nothing */;
935 deflateEnd(&s);
939 e->type = type;
940 e->pack_id = pack_id;
941 e->offset = pack_size;
942 object_count++;
943 object_count_by_type[type]++;
945 if (delta) {
946 unsigned long ofs = e->offset - last->offset;
947 unsigned pos = sizeof(hdr) - 1;
949 delta_count_by_type[type]++;
950 last->depth++;
952 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
953 write_or_die(pack_data->pack_fd, hdr, hdrlen);
954 pack_size += hdrlen;
956 hdr[pos] = ofs & 127;
957 while (ofs >>= 7)
958 hdr[--pos] = 128 | (--ofs & 127);
959 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
960 pack_size += sizeof(hdr) - pos;
961 } else {
962 if (last)
963 last->depth = 0;
964 hdrlen = encode_header(type, datlen, hdr);
965 write_or_die(pack_data->pack_fd, hdr, hdrlen);
966 pack_size += hdrlen;
969 write_or_die(pack_data->pack_fd, out, s.total_out);
970 pack_size += s.total_out;
972 free(out);
973 if (delta)
974 free(delta);
975 if (last) {
976 if (last->data && !last->no_free)
977 free(last->data);
978 last->data = dat;
979 last->offset = e->offset;
980 last->len = datlen;
982 return 0;
985 static void *gfi_unpack_entry(
986 struct object_entry *oe,
987 unsigned long *sizep)
989 static char type[20];
990 struct packed_git *p = all_packs[oe->pack_id];
991 if (p == pack_data)
992 p->pack_size = pack_size + 20;
993 return unpack_entry(p, oe->offset, type, sizep);
996 static const char *get_mode(const char *str, uint16_t *modep)
998 unsigned char c;
999 uint16_t mode = 0;
1001 while ((c = *str++) != ' ') {
1002 if (c < '0' || c > '7')
1003 return NULL;
1004 mode = (mode << 3) + (c - '0');
1006 *modep = mode;
1007 return str;
1010 static void load_tree(struct tree_entry *root)
1012 unsigned char* sha1 = root->versions[1].sha1;
1013 struct object_entry *myoe;
1014 struct tree_content *t;
1015 unsigned long size;
1016 char *buf;
1017 const char *c;
1019 root->tree = t = new_tree_content(8);
1020 if (is_null_sha1(sha1))
1021 return;
1023 myoe = find_object(sha1);
1024 if (myoe) {
1025 if (myoe->type != OBJ_TREE)
1026 die("Not a tree: %s", sha1_to_hex(sha1));
1027 t->delta_depth = 0;
1028 buf = gfi_unpack_entry(myoe, &size);
1029 } else {
1030 char type[20];
1031 buf = read_sha1_file(sha1, type, &size);
1032 if (!buf || strcmp(type, tree_type))
1033 die("Can't load tree %s", sha1_to_hex(sha1));
1036 c = buf;
1037 while (c != (buf + size)) {
1038 struct tree_entry *e = new_tree_entry();
1040 if (t->entry_count == t->entry_capacity)
1041 root->tree = t = grow_tree_content(t, 8);
1042 t->entries[t->entry_count++] = e;
1044 e->tree = NULL;
1045 c = get_mode(c, &e->versions[1].mode);
1046 if (!c)
1047 die("Corrupt mode in %s", sha1_to_hex(sha1));
1048 e->versions[0].mode = e->versions[1].mode;
1049 e->name = to_atom(c, (unsigned short)strlen(c));
1050 c += e->name->str_len + 1;
1051 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1052 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1053 c += 20;
1055 free(buf);
1058 static int tecmp0 (const void *_a, const void *_b)
1060 struct tree_entry *a = *((struct tree_entry**)_a);
1061 struct tree_entry *b = *((struct tree_entry**)_b);
1062 return base_name_compare(
1063 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1064 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1067 static int tecmp1 (const void *_a, const void *_b)
1069 struct tree_entry *a = *((struct tree_entry**)_a);
1070 struct tree_entry *b = *((struct tree_entry**)_b);
1071 return base_name_compare(
1072 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1073 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1076 static void mktree(struct tree_content *t,
1077 int v,
1078 unsigned long *szp,
1079 struct dbuf *b)
1081 size_t maxlen = 0;
1082 unsigned int i;
1083 char *c;
1085 if (!v)
1086 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1087 else
1088 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1090 for (i = 0; i < t->entry_count; i++) {
1091 if (t->entries[i]->versions[v].mode)
1092 maxlen += t->entries[i]->name->str_len + 34;
1095 size_dbuf(b, maxlen);
1096 c = b->buffer;
1097 for (i = 0; i < t->entry_count; i++) {
1098 struct tree_entry *e = t->entries[i];
1099 if (!e->versions[v].mode)
1100 continue;
1101 c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1102 *c++ = ' ';
1103 strcpy(c, e->name->str_dat);
1104 c += e->name->str_len + 1;
1105 hashcpy((unsigned char*)c, e->versions[v].sha1);
1106 c += 20;
1108 *szp = c - (char*)b->buffer;
1111 static void store_tree(struct tree_entry *root)
1113 struct tree_content *t = root->tree;
1114 unsigned int i, j, del;
1115 unsigned long new_len;
1116 struct last_object lo;
1117 struct object_entry *le;
1119 if (!is_null_sha1(root->versions[1].sha1))
1120 return;
1122 for (i = 0; i < t->entry_count; i++) {
1123 if (t->entries[i]->tree)
1124 store_tree(t->entries[i]);
1127 le = find_object(root->versions[0].sha1);
1128 if (!S_ISDIR(root->versions[0].mode)
1129 || !le
1130 || le->pack_id != pack_id) {
1131 lo.data = NULL;
1132 lo.depth = 0;
1133 } else {
1134 mktree(t, 0, &lo.len, &old_tree);
1135 lo.data = old_tree.buffer;
1136 lo.offset = le->offset;
1137 lo.depth = t->delta_depth;
1138 lo.no_free = 1;
1141 mktree(t, 1, &new_len, &new_tree);
1142 store_object(OBJ_TREE, new_tree.buffer, new_len,
1143 &lo, root->versions[1].sha1, 0);
1145 t->delta_depth = lo.depth;
1146 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1147 struct tree_entry *e = t->entries[i];
1148 if (e->versions[1].mode) {
1149 e->versions[0].mode = e->versions[1].mode;
1150 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1151 t->entries[j++] = e;
1152 } else {
1153 release_tree_entry(e);
1154 del++;
1157 t->entry_count -= del;
1160 static int tree_content_set(
1161 struct tree_entry *root,
1162 const char *p,
1163 const unsigned char *sha1,
1164 const uint16_t mode)
1166 struct tree_content *t = root->tree;
1167 const char *slash1;
1168 unsigned int i, n;
1169 struct tree_entry *e;
1171 slash1 = strchr(p, '/');
1172 if (slash1)
1173 n = slash1 - p;
1174 else
1175 n = strlen(p);
1177 for (i = 0; i < t->entry_count; i++) {
1178 e = t->entries[i];
1179 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1180 if (!slash1) {
1181 if (e->versions[1].mode == mode
1182 && !hashcmp(e->versions[1].sha1, sha1))
1183 return 0;
1184 e->versions[1].mode = mode;
1185 hashcpy(e->versions[1].sha1, sha1);
1186 if (e->tree) {
1187 release_tree_content_recursive(e->tree);
1188 e->tree = NULL;
1190 hashclr(root->versions[1].sha1);
1191 return 1;
1193 if (!S_ISDIR(e->versions[1].mode)) {
1194 e->tree = new_tree_content(8);
1195 e->versions[1].mode = S_IFDIR;
1197 if (!e->tree)
1198 load_tree(e);
1199 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1200 hashclr(root->versions[1].sha1);
1201 return 1;
1203 return 0;
1207 if (t->entry_count == t->entry_capacity)
1208 root->tree = t = grow_tree_content(t, 8);
1209 e = new_tree_entry();
1210 e->name = to_atom(p, (unsigned short)n);
1211 e->versions[0].mode = 0;
1212 hashclr(e->versions[0].sha1);
1213 t->entries[t->entry_count++] = e;
1214 if (slash1) {
1215 e->tree = new_tree_content(8);
1216 e->versions[1].mode = S_IFDIR;
1217 tree_content_set(e, slash1 + 1, sha1, mode);
1218 } else {
1219 e->tree = NULL;
1220 e->versions[1].mode = mode;
1221 hashcpy(e->versions[1].sha1, sha1);
1223 hashclr(root->versions[1].sha1);
1224 return 1;
1227 static int tree_content_remove(struct tree_entry *root, const char *p)
1229 struct tree_content *t = root->tree;
1230 const char *slash1;
1231 unsigned int i, n;
1232 struct tree_entry *e;
1234 slash1 = strchr(p, '/');
1235 if (slash1)
1236 n = slash1 - p;
1237 else
1238 n = strlen(p);
1240 for (i = 0; i < t->entry_count; i++) {
1241 e = t->entries[i];
1242 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1243 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1244 goto del_entry;
1245 if (!e->tree)
1246 load_tree(e);
1247 if (tree_content_remove(e, slash1 + 1)) {
1248 for (n = 0; n < e->tree->entry_count; n++) {
1249 if (e->tree->entries[n]->versions[1].mode) {
1250 hashclr(root->versions[1].sha1);
1251 return 1;
1254 goto del_entry;
1256 return 0;
1259 return 0;
1261 del_entry:
1262 if (e->tree) {
1263 release_tree_content_recursive(e->tree);
1264 e->tree = NULL;
1266 e->versions[1].mode = 0;
1267 hashclr(e->versions[1].sha1);
1268 hashclr(root->versions[1].sha1);
1269 return 1;
1272 static void dump_branches(void)
1274 static const char *msg = "fast-import";
1275 unsigned int i;
1276 struct branch *b;
1277 struct ref_lock *lock;
1279 for (i = 0; i < branch_table_sz; i++) {
1280 for (b = branch_table[i]; b; b = b->table_next_branch) {
1281 lock = lock_any_ref_for_update(b->name, NULL);
1282 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1283 die("Can't write %s", b->name);
1288 static void dump_tags(void)
1290 static const char *msg = "fast-import";
1291 struct tag *t;
1292 struct ref_lock *lock;
1293 char path[PATH_MAX];
1295 for (t = first_tag; t; t = t->next_tag) {
1296 sprintf(path, "refs/tags/%s", t->name);
1297 lock = lock_any_ref_for_update(path, NULL);
1298 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1299 die("Can't write %s", path);
1303 static void dump_marks_helper(FILE *f,
1304 uintmax_t base,
1305 struct mark_set *m)
1307 uintmax_t k;
1308 if (m->shift) {
1309 for (k = 0; k < 1024; k++) {
1310 if (m->data.sets[k])
1311 dump_marks_helper(f, (base + k) << m->shift,
1312 m->data.sets[k]);
1314 } else {
1315 for (k = 0; k < 1024; k++) {
1316 if (m->data.marked[k])
1317 fprintf(f, ":%ju %s\n", base + k,
1318 sha1_to_hex(m->data.marked[k]->sha1));
1323 static void dump_marks(void)
1325 if (mark_file)
1327 FILE *f = fopen(mark_file, "w");
1328 dump_marks_helper(f, 0, marks);
1329 fclose(f);
1333 static void read_next_command(void)
1335 read_line(&command_buf, stdin, '\n');
1338 static void cmd_mark(void)
1340 if (!strncmp("mark :", command_buf.buf, 6)) {
1341 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1342 read_next_command();
1344 else
1345 next_mark = 0;
1348 static void* cmd_data (size_t *size)
1350 size_t length;
1351 char *buffer;
1353 if (strncmp("data ", command_buf.buf, 5))
1354 die("Expected 'data n' command, found: %s", command_buf.buf);
1356 if (!strncmp("<<", command_buf.buf + 5, 2)) {
1357 char *term = xstrdup(command_buf.buf + 5 + 2);
1358 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1359 length = 0;
1360 buffer = xmalloc(sz);
1361 for (;;) {
1362 read_next_command();
1363 if (command_buf.eof)
1364 die("EOF in data (terminator '%s' not found)", term);
1365 if (term_len == command_buf.len
1366 && !strcmp(term, command_buf.buf))
1367 break;
1368 if (sz < (length + command_buf.len)) {
1369 sz = sz * 3 / 2 + 16;
1370 if (sz < (length + command_buf.len))
1371 sz = length + command_buf.len;
1372 buffer = xrealloc(buffer, sz);
1374 memcpy(buffer + length,
1375 command_buf.buf,
1376 command_buf.len - 1);
1377 length += command_buf.len - 1;
1378 buffer[length++] = '\n';
1380 free(term);
1382 else {
1383 size_t n = 0;
1384 length = strtoul(command_buf.buf + 5, NULL, 10);
1385 buffer = xmalloc(length);
1386 while (n < length) {
1387 size_t s = fread(buffer + n, 1, length - n, stdin);
1388 if (!s && feof(stdin))
1389 die("EOF in data (%lu bytes remaining)", length - n);
1390 n += s;
1394 if (fgetc(stdin) != '\n')
1395 die("An lf did not trail the binary data as expected.");
1397 *size = length;
1398 return buffer;
1401 static void cmd_new_blob(void)
1403 size_t l;
1404 void *d;
1406 read_next_command();
1407 cmd_mark();
1408 d = cmd_data(&l);
1410 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1411 free(d);
1414 static void unload_one_branch(void)
1416 while (cur_active_branches
1417 && cur_active_branches >= max_active_branches) {
1418 unsigned long min_commit = ULONG_MAX;
1419 struct branch *e, *l = NULL, *p = NULL;
1421 for (e = active_branches; e; e = e->active_next_branch) {
1422 if (e->last_commit < min_commit) {
1423 p = l;
1424 min_commit = e->last_commit;
1426 l = e;
1429 if (p) {
1430 e = p->active_next_branch;
1431 p->active_next_branch = e->active_next_branch;
1432 } else {
1433 e = active_branches;
1434 active_branches = e->active_next_branch;
1436 e->active_next_branch = NULL;
1437 if (e->branch_tree.tree) {
1438 release_tree_content_recursive(e->branch_tree.tree);
1439 e->branch_tree.tree = NULL;
1441 cur_active_branches--;
1445 static void load_branch(struct branch *b)
1447 load_tree(&b->branch_tree);
1448 b->active_next_branch = active_branches;
1449 active_branches = b;
1450 cur_active_branches++;
1451 branch_load_count++;
1454 static void file_change_m(struct branch *b)
1456 const char *p = command_buf.buf + 2;
1457 char *p_uq;
1458 const char *endp;
1459 struct object_entry *oe;
1460 unsigned char sha1[20];
1461 uint16_t mode, inline_data = 0;
1462 char type[20];
1464 p = get_mode(p, &mode);
1465 if (!p)
1466 die("Corrupt mode: %s", command_buf.buf);
1467 switch (mode) {
1468 case S_IFREG | 0644:
1469 case S_IFREG | 0755:
1470 case S_IFLNK:
1471 case 0644:
1472 case 0755:
1473 /* ok */
1474 break;
1475 default:
1476 die("Corrupt mode: %s", command_buf.buf);
1479 if (*p == ':') {
1480 char *x;
1481 oe = find_mark(strtoumax(p + 1, &x, 10));
1482 hashcpy(sha1, oe->sha1);
1483 p = x;
1484 } else if (!strncmp("inline", p, 6)) {
1485 inline_data = 1;
1486 p += 6;
1487 } else {
1488 if (get_sha1_hex(p, sha1))
1489 die("Invalid SHA1: %s", command_buf.buf);
1490 oe = find_object(sha1);
1491 p += 40;
1493 if (*p++ != ' ')
1494 die("Missing space after SHA1: %s", command_buf.buf);
1496 p_uq = unquote_c_style(p, &endp);
1497 if (p_uq) {
1498 if (*endp)
1499 die("Garbage after path in: %s", command_buf.buf);
1500 p = p_uq;
1503 if (inline_data) {
1504 size_t l;
1505 void *d;
1506 if (!p_uq)
1507 p = p_uq = xstrdup(p);
1508 read_next_command();
1509 d = cmd_data(&l);
1510 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1511 free(d);
1512 } else if (oe) {
1513 if (oe->type != OBJ_BLOB)
1514 die("Not a blob (actually a %s): %s",
1515 command_buf.buf, type_names[oe->type]);
1516 } else {
1517 if (sha1_object_info(sha1, type, NULL))
1518 die("Blob not found: %s", command_buf.buf);
1519 if (strcmp(blob_type, type))
1520 die("Not a blob (actually a %s): %s",
1521 command_buf.buf, type);
1524 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1526 if (p_uq)
1527 free(p_uq);
1530 static void file_change_d(struct branch *b)
1532 const char *p = command_buf.buf + 2;
1533 char *p_uq;
1534 const char *endp;
1536 p_uq = unquote_c_style(p, &endp);
1537 if (p_uq) {
1538 if (*endp)
1539 die("Garbage after path in: %s", command_buf.buf);
1540 p = p_uq;
1542 tree_content_remove(&b->branch_tree, p);
1543 if (p_uq)
1544 free(p_uq);
1547 static void cmd_from(struct branch *b)
1549 const char *from;
1550 struct branch *s;
1552 if (strncmp("from ", command_buf.buf, 5))
1553 return;
1555 if (b->last_commit)
1556 die("Can't reinitailize branch %s", b->name);
1558 from = strchr(command_buf.buf, ' ') + 1;
1559 s = lookup_branch(from);
1560 if (b == s)
1561 die("Can't create a branch from itself: %s", b->name);
1562 else if (s) {
1563 unsigned char *t = s->branch_tree.versions[1].sha1;
1564 hashcpy(b->sha1, s->sha1);
1565 hashcpy(b->branch_tree.versions[0].sha1, t);
1566 hashcpy(b->branch_tree.versions[1].sha1, t);
1567 } else if (*from == ':') {
1568 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1569 struct object_entry *oe = find_mark(idnum);
1570 unsigned long size;
1571 char *buf;
1572 if (oe->type != OBJ_COMMIT)
1573 die("Mark :%ju not a commit", idnum);
1574 hashcpy(b->sha1, oe->sha1);
1575 buf = gfi_unpack_entry(oe, &size);
1576 if (!buf || size < 46)
1577 die("Not a valid commit: %s", from);
1578 if (memcmp("tree ", buf, 5)
1579 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1580 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1581 free(buf);
1582 hashcpy(b->branch_tree.versions[0].sha1,
1583 b->branch_tree.versions[1].sha1);
1584 } else if (!get_sha1(from, b->sha1)) {
1585 if (is_null_sha1(b->sha1)) {
1586 hashclr(b->branch_tree.versions[0].sha1);
1587 hashclr(b->branch_tree.versions[1].sha1);
1588 } else {
1589 unsigned long size;
1590 char *buf;
1592 buf = read_object_with_reference(b->sha1,
1593 type_names[OBJ_COMMIT], &size, b->sha1);
1594 if (!buf || size < 46)
1595 die("Not a valid commit: %s", from);
1596 if (memcmp("tree ", buf, 5)
1597 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1598 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1599 free(buf);
1600 hashcpy(b->branch_tree.versions[0].sha1,
1601 b->branch_tree.versions[1].sha1);
1603 } else
1604 die("Invalid ref name or SHA1 expression: %s", from);
1606 read_next_command();
1609 static struct hash_list* cmd_merge(unsigned int *count)
1611 struct hash_list *list = NULL, *n, *e;
1612 const char *from;
1613 struct branch *s;
1615 *count = 0;
1616 while (!strncmp("merge ", command_buf.buf, 6)) {
1617 from = strchr(command_buf.buf, ' ') + 1;
1618 n = xmalloc(sizeof(*n));
1619 s = lookup_branch(from);
1620 if (s)
1621 hashcpy(n->sha1, s->sha1);
1622 else if (*from == ':') {
1623 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1624 struct object_entry *oe = find_mark(idnum);
1625 if (oe->type != OBJ_COMMIT)
1626 die("Mark :%ju not a commit", idnum);
1627 hashcpy(n->sha1, oe->sha1);
1628 } else if (get_sha1(from, n->sha1))
1629 die("Invalid ref name or SHA1 expression: %s", from);
1631 n->next = NULL;
1632 if (list)
1633 e->next = n;
1634 else
1635 list = n;
1636 e = n;
1637 *count++;
1638 read_next_command();
1640 return list;
1643 static void cmd_new_commit(void)
1645 struct branch *b;
1646 void *msg;
1647 size_t msglen;
1648 char *sp;
1649 char *author = NULL;
1650 char *committer = NULL;
1651 struct hash_list *merge_list = NULL;
1652 unsigned int merge_count;
1654 /* Obtain the branch name from the rest of our command */
1655 sp = strchr(command_buf.buf, ' ') + 1;
1656 b = lookup_branch(sp);
1657 if (!b)
1658 b = new_branch(sp);
1660 read_next_command();
1661 cmd_mark();
1662 if (!strncmp("author ", command_buf.buf, 7)) {
1663 author = strdup(command_buf.buf);
1664 read_next_command();
1666 if (!strncmp("committer ", command_buf.buf, 10)) {
1667 committer = strdup(command_buf.buf);
1668 read_next_command();
1670 if (!committer)
1671 die("Expected committer but didn't get one");
1672 msg = cmd_data(&msglen);
1673 read_next_command();
1674 cmd_from(b);
1675 merge_list = cmd_merge(&merge_count);
1677 /* ensure the branch is active/loaded */
1678 if (!b->branch_tree.tree || !max_active_branches) {
1679 unload_one_branch();
1680 load_branch(b);
1683 /* file_change* */
1684 for (;;) {
1685 if (1 == command_buf.len)
1686 break;
1687 else if (!strncmp("M ", command_buf.buf, 2))
1688 file_change_m(b);
1689 else if (!strncmp("D ", command_buf.buf, 2))
1690 file_change_d(b);
1691 else
1692 die("Unsupported file_change: %s", command_buf.buf);
1693 read_next_command();
1696 /* build the tree and the commit */
1697 store_tree(&b->branch_tree);
1698 hashcpy(b->branch_tree.versions[0].sha1,
1699 b->branch_tree.versions[1].sha1);
1700 size_dbuf(&new_data, 97 + msglen
1701 + merge_count * 49
1702 + (author
1703 ? strlen(author) + strlen(committer)
1704 : 2 * strlen(committer)));
1705 sp = new_data.buffer;
1706 sp += sprintf(sp, "tree %s\n",
1707 sha1_to_hex(b->branch_tree.versions[1].sha1));
1708 if (!is_null_sha1(b->sha1))
1709 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1710 while (merge_list) {
1711 struct hash_list *next = merge_list->next;
1712 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1713 free(merge_list);
1714 merge_list = next;
1716 if (author)
1717 sp += sprintf(sp, "%s\n", author);
1718 else
1719 sp += sprintf(sp, "author %s\n", committer + 10);
1720 sp += sprintf(sp, "%s\n\n", committer);
1721 memcpy(sp, msg, msglen);
1722 sp += msglen;
1723 if (author)
1724 free(author);
1725 free(committer);
1726 free(msg);
1728 if (!store_object(OBJ_COMMIT,
1729 new_data.buffer, sp - (char*)new_data.buffer,
1730 NULL, b->sha1, next_mark))
1731 b->pack_id = pack_id;
1732 b->last_commit = object_count_by_type[OBJ_COMMIT];
1734 if (branch_log) {
1735 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1736 fprintf(branch_log, "commit ");
1737 if (need_dq) {
1738 fputc('"', branch_log);
1739 quote_c_style(b->name, NULL, branch_log, 0);
1740 fputc('"', branch_log);
1741 } else
1742 fprintf(branch_log, "%s", b->name);
1743 fprintf(branch_log," :%ju %s\n",next_mark,sha1_to_hex(b->sha1));
1747 static void cmd_new_tag(void)
1749 char *sp;
1750 const char *from;
1751 char *tagger;
1752 struct branch *s;
1753 void *msg;
1754 size_t msglen;
1755 struct tag *t;
1756 uintmax_t from_mark = 0;
1757 unsigned char sha1[20];
1759 /* Obtain the new tag name from the rest of our command */
1760 sp = strchr(command_buf.buf, ' ') + 1;
1761 t = pool_alloc(sizeof(struct tag));
1762 t->next_tag = NULL;
1763 t->name = pool_strdup(sp);
1764 if (last_tag)
1765 last_tag->next_tag = t;
1766 else
1767 first_tag = t;
1768 last_tag = t;
1769 read_next_command();
1771 /* from ... */
1772 if (strncmp("from ", command_buf.buf, 5))
1773 die("Expected from command, got %s", command_buf.buf);
1774 from = strchr(command_buf.buf, ' ') + 1;
1775 s = lookup_branch(from);
1776 if (s) {
1777 hashcpy(sha1, s->sha1);
1778 } else if (*from == ':') {
1779 from_mark = strtoumax(from + 1, NULL, 10);
1780 struct object_entry *oe = find_mark(from_mark);
1781 if (oe->type != OBJ_COMMIT)
1782 die("Mark :%ju not a commit", from_mark);
1783 hashcpy(sha1, oe->sha1);
1784 } else if (!get_sha1(from, sha1)) {
1785 unsigned long size;
1786 char *buf;
1788 buf = read_object_with_reference(sha1,
1789 type_names[OBJ_COMMIT], &size, sha1);
1790 if (!buf || size < 46)
1791 die("Not a valid commit: %s", from);
1792 free(buf);
1793 } else
1794 die("Invalid ref name or SHA1 expression: %s", from);
1795 read_next_command();
1797 /* tagger ... */
1798 if (strncmp("tagger ", command_buf.buf, 7))
1799 die("Expected tagger command, got %s", command_buf.buf);
1800 tagger = strdup(command_buf.buf);
1802 /* tag payload/message */
1803 read_next_command();
1804 msg = cmd_data(&msglen);
1806 /* build the tag object */
1807 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1808 sp = new_data.buffer;
1809 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1810 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1811 sp += sprintf(sp, "tag %s\n", t->name);
1812 sp += sprintf(sp, "%s\n\n", tagger);
1813 memcpy(sp, msg, msglen);
1814 sp += msglen;
1815 free(tagger);
1816 free(msg);
1818 if (store_object(OBJ_TAG, new_data.buffer,
1819 sp - (char*)new_data.buffer,
1820 NULL, t->sha1, 0))
1821 t->pack_id = MAX_PACK_ID;
1822 else
1823 t->pack_id = pack_id;
1825 if (branch_log) {
1826 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1827 fprintf(branch_log, "tag ");
1828 if (need_dq) {
1829 fputc('"', branch_log);
1830 quote_c_style(t->name, NULL, branch_log, 0);
1831 fputc('"', branch_log);
1832 } else
1833 fprintf(branch_log, "%s", t->name);
1834 fprintf(branch_log," :%ju %s\n",from_mark,sha1_to_hex(t->sha1));
1838 static void cmd_reset_branch(void)
1840 struct branch *b;
1841 char *sp;
1843 /* Obtain the branch name from the rest of our command */
1844 sp = strchr(command_buf.buf, ' ') + 1;
1845 b = lookup_branch(sp);
1846 if (b) {
1847 b->last_commit = 0;
1848 if (b->branch_tree.tree) {
1849 release_tree_content_recursive(b->branch_tree.tree);
1850 b->branch_tree.tree = NULL;
1853 else
1854 b = new_branch(sp);
1855 read_next_command();
1856 cmd_from(b);
1859 static void cmd_checkpoint(void)
1861 if (object_count)
1862 checkpoint();
1863 read_next_command();
1866 static const char fast_import_usage[] =
1867 "git-fast-import [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1869 int main(int argc, const char **argv)
1871 int i;
1872 uintmax_t total_count, duplicate_count;
1874 git_config(git_default_config);
1876 for (i = 1; i < argc; i++) {
1877 const char *a = argv[i];
1879 if (*a != '-' || !strcmp(a, "--"))
1880 break;
1881 else if (!strncmp(a, "--max-pack-size=", 16))
1882 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
1883 else if (!strncmp(a, "--depth=", 8))
1884 max_depth = strtoul(a + 8, NULL, 0);
1885 else if (!strncmp(a, "--active-branches=", 18))
1886 max_active_branches = strtoul(a + 18, NULL, 0);
1887 else if (!strncmp(a, "--export-marks=", 15))
1888 mark_file = a + 15;
1889 else if (!strncmp(a, "--branch-log=", 13)) {
1890 branch_log = fopen(a + 13, "w");
1891 if (!branch_log)
1892 die("Can't create %s: %s", a + 13, strerror(errno));
1894 else
1895 die("unknown option %s", a);
1897 if (i != argc)
1898 usage(fast_import_usage);
1900 alloc_objects(object_entry_alloc);
1901 strbuf_init(&command_buf);
1903 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1904 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1905 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1906 marks = pool_calloc(1, sizeof(struct mark_set));
1908 start_packfile();
1909 for (;;) {
1910 read_next_command();
1911 if (command_buf.eof)
1912 break;
1913 else if (!strcmp("blob", command_buf.buf))
1914 cmd_new_blob();
1915 else if (!strncmp("commit ", command_buf.buf, 7))
1916 cmd_new_commit();
1917 else if (!strncmp("tag ", command_buf.buf, 4))
1918 cmd_new_tag();
1919 else if (!strncmp("reset ", command_buf.buf, 6))
1920 cmd_reset_branch();
1921 else if (!strcmp("checkpoint", command_buf.buf))
1922 cmd_checkpoint();
1923 else
1924 die("Unsupported command: %s", command_buf.buf);
1926 end_packfile();
1928 dump_branches();
1929 dump_tags();
1930 unkeep_all_packs();
1931 dump_marks();
1932 if (branch_log)
1933 fclose(branch_log);
1935 total_count = 0;
1936 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
1937 total_count += object_count_by_type[i];
1938 duplicate_count = 0;
1939 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
1940 duplicate_count += duplicate_count_by_type[i];
1942 fprintf(stderr, "%s statistics:\n", argv[0]);
1943 fprintf(stderr, "---------------------------------------------------------------------\n");
1944 fprintf(stderr, "Alloc'd objects: %10ju\n", alloc_count);
1945 fprintf(stderr, "Total objects: %10ju (%10ju duplicates )\n", total_count, duplicate_count);
1946 fprintf(stderr, " blobs : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
1947 fprintf(stderr, " trees : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
1948 fprintf(stderr, " commits: %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
1949 fprintf(stderr, " tags : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
1950 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1951 fprintf(stderr, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
1952 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1953 fprintf(stderr, "Memory total: %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1954 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1955 fprintf(stderr, " objects: %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1956 fprintf(stderr, "---------------------------------------------------------------------\n");
1957 pack_report();
1958 fprintf(stderr, "---------------------------------------------------------------------\n");
1959 fprintf(stderr, "\n");
1961 return 0;