2 Format of STDIN stream:
12 new_blob ::= 'blob' lf
15 file_content ::= data;
17 new_commit ::= 'commit' sp ref_str lf
19 ('author' sp name '<' email '>' ts tz lf)?
20 'committer' sp name '<' email '>' ts tz lf
22 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
23 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
28 file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
31 mode ::= '644' | '755';
33 new_tag ::= 'tag' sp tag_str lf
34 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
35 'tagger' sp name '<' email '>' ts tz lf
39 reset_branch ::= 'reset' sp ref_str lf
40 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
43 # note: the first idnum in a stream should be 1 and subsequent
44 # idnums should not have gaps between values as this will cause
45 # the stream parser to reserve space for the gapped values. An
46 # idnum can be updated in the future to a new object by issuing
47 # a new mark directive with the old idnum.
49 mark ::= 'mark' sp idnum lf;
51 # note: declen indicates the length of binary_data in bytes.
52 # declen does not include the lf preceeding or trailing the
55 data ::= 'data' sp declen lf
59 # note: quoted strings are C-style quoting supporting \c for
60 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
61 # is the signed byte value in octal. Note that the only
62 # characters which must actually be escaped to protect the
63 # stream formatting is: \, " and LF. Otherwise these values
66 ref_str ::= ref | '"' quoted(ref) '"' ;
67 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
68 tag_str ::= tag | '"' quoted(tag) '"' ;
69 path_str ::= path | '"' quoted(path) '"' ;
71 declen ::= # unsigned 32 bit value, ascii base10 notation;
72 binary_data ::= # file content, not interpreted;
74 sp ::= # ASCII space character;
75 lf ::= # ASCII newline (LF) character;
77 # note: a colon (':') must precede the numerical value assigned to
78 # an idnum. This is to distinguish it from a ref or tag name as
79 # GIT does not permit ':' in ref or tag strings.
82 path ::= # GIT style file path, e.g. "a/b/c";
83 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
84 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
85 sha1exp ::= # Any valid GIT SHA1 expression;
86 hexsha1 ::= # SHA1 in hexadecimal format;
88 # note: name and email are UTF8 strings, however name must not
89 # contain '<' or lf and email must not contain any of the
90 # following: '<', '>', lf.
92 name ::= # valid GIT author/committer name;
93 email ::= # valid GIT author/committer email;
94 ts ::= # time since the epoch in seconds, ascii base10 notation;
95 tz ::= # GIT style timezone;
106 #include "csum-file.h"
112 struct object_entry *next;
113 unsigned long offset;
114 unsigned type : TYPE_BITS;
115 unsigned char sha1[20];
118 struct object_entry_pool
120 struct object_entry_pool *next_pool;
121 struct object_entry *next_free;
122 struct object_entry *end;
123 struct object_entry entries[FLEX_ARRAY]; /* more */
130 struct object_entry *marked[1024];
131 struct mark_set *sets[1024];
139 unsigned long offset;
146 struct mem_pool *next_pool;
149 char space[FLEX_ARRAY]; /* more */
154 struct atom_str *next_atom;
156 char str_dat[FLEX_ARRAY]; /* more */
162 struct tree_content *tree;
163 struct atom_str* name;
167 unsigned char sha1[20];
173 unsigned int entry_capacity; /* must match avail_tree_content */
174 unsigned int entry_count;
175 unsigned int delta_depth;
176 struct tree_entry *entries[FLEX_ARRAY]; /* more */
179 struct avail_tree_content
181 unsigned int entry_capacity; /* must match tree_content */
182 struct avail_tree_content *next_avail;
187 struct branch *table_next_branch;
188 struct branch *active_next_branch;
190 unsigned long last_commit;
191 struct tree_entry branch_tree;
192 unsigned char sha1[20];
197 struct tag *next_tag;
199 unsigned char sha1[20];
210 struct hash_list *next;
211 unsigned char sha1[20];
214 /* Stats and misc. counters */
215 static unsigned long max_depth = 10;
216 static unsigned long alloc_count;
217 static unsigned long branch_count;
218 static unsigned long branch_load_count;
219 static unsigned long object_count;
220 static unsigned long duplicate_count;
221 static unsigned long marks_set_count;
222 static unsigned long object_count_by_type[1 << TYPE_BITS];
223 static unsigned long duplicate_count_by_type[1 << TYPE_BITS];
224 static unsigned long delta_count_by_type[1 << TYPE_BITS];
227 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
228 static size_t total_allocd;
229 static struct mem_pool *mem_pool;
231 /* Atom management */
232 static unsigned int atom_table_sz = 4451;
233 static unsigned int atom_cnt;
234 static struct atom_str **atom_table;
236 /* The .pack file being generated */
237 static const char *base_name;
238 static unsigned int pack_count;
239 static char *pack_name;
240 static char *idx_name;
241 static struct packed_git *pack_data;
243 static unsigned long pack_size;
244 static unsigned char pack_sha1[20];
246 /* Table of objects we've written. */
247 static unsigned int object_entry_alloc = 5000;
248 static struct object_entry_pool *blocks;
249 static struct object_entry *object_table[1 << 16];
250 static struct mark_set *marks;
251 static const char* mark_file;
254 static struct last_object last_blob;
256 /* Tree management */
257 static unsigned int tree_entry_alloc = 1000;
258 static void *avail_tree_entry;
259 static unsigned int avail_tree_table_sz = 100;
260 static struct avail_tree_content **avail_tree_table;
261 static struct dbuf old_tree;
262 static struct dbuf new_tree;
265 static unsigned long max_active_branches = 5;
266 static unsigned long cur_active_branches;
267 static unsigned long branch_table_sz = 1039;
268 static struct branch **branch_table;
269 static struct branch *active_branches;
272 static struct tag *first_tag;
273 static struct tag *last_tag;
275 /* Input stream parsing */
276 static struct strbuf command_buf;
277 static unsigned long next_mark;
278 static struct dbuf new_data;
279 static FILE* branch_log;
282 static void alloc_objects(unsigned int cnt)
284 struct object_entry_pool *b;
286 b = xmalloc(sizeof(struct object_entry_pool)
287 + cnt * sizeof(struct object_entry));
288 b->next_pool = blocks;
289 b->next_free = b->entries;
290 b->end = b->entries + cnt;
295 static struct object_entry* new_object(unsigned char *sha1)
297 struct object_entry *e;
299 if (blocks->next_free == blocks->end)
300 alloc_objects(object_entry_alloc);
302 e = blocks->next_free++;
303 hashcpy(e->sha1, sha1);
307 static struct object_entry* find_object(unsigned char *sha1)
309 unsigned int h = sha1[0] << 8 | sha1[1];
310 struct object_entry *e;
311 for (e = object_table[h]; e; e = e->next)
312 if (!hashcmp(sha1, e->sha1))
317 static struct object_entry* insert_object(unsigned char *sha1)
319 unsigned int h = sha1[0] << 8 | sha1[1];
320 struct object_entry *e = object_table[h];
321 struct object_entry *p = NULL;
324 if (!hashcmp(sha1, e->sha1))
330 e = new_object(sha1);
340 static unsigned int hc_str(const char *s, size_t len)
348 static void* pool_alloc(size_t len)
353 for (p = mem_pool; p; p = p->next_pool)
354 if ((p->end - p->next_free >= len))
358 if (len >= (mem_pool_alloc/2)) {
362 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
363 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
364 p->next_pool = mem_pool;
365 p->next_free = p->space;
366 p->end = p->next_free + mem_pool_alloc;
371 /* round out to a pointer alignment */
372 if (len & (sizeof(void*) - 1))
373 len += sizeof(void*) - (len & (sizeof(void*) - 1));
378 static void* pool_calloc(size_t count, size_t size)
380 size_t len = count * size;
381 void *r = pool_alloc(len);
386 static char* pool_strdup(const char *s)
388 char *r = pool_alloc(strlen(s) + 1);
393 static void size_dbuf(struct dbuf *b, size_t maxlen)
396 if (b->capacity >= maxlen)
400 b->capacity = ((maxlen / 1024) + 1) * 1024;
401 b->buffer = xmalloc(b->capacity);
404 static void insert_mark(unsigned long idnum, struct object_entry *oe)
406 struct mark_set *s = marks;
407 while ((idnum >> s->shift) >= 1024) {
408 s = pool_calloc(1, sizeof(struct mark_set));
409 s->shift = marks->shift + 10;
410 s->data.sets[0] = marks;
414 unsigned long i = idnum >> s->shift;
415 idnum -= i << s->shift;
416 if (!s->data.sets[i]) {
417 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
418 s->data.sets[i]->shift = s->shift - 10;
422 if (!s->data.marked[idnum])
424 s->data.marked[idnum] = oe;
427 static struct object_entry* find_mark(unsigned long idnum)
429 unsigned long orig_idnum = idnum;
430 struct mark_set *s = marks;
431 struct object_entry *oe = NULL;
432 if ((idnum >> s->shift) < 1024) {
433 while (s && s->shift) {
434 unsigned long i = idnum >> s->shift;
435 idnum -= i << s->shift;
439 oe = s->data.marked[idnum];
442 die("mark :%lu not declared", orig_idnum);
446 static struct atom_str* to_atom(const char *s, size_t len)
448 unsigned int hc = hc_str(s, len) % atom_table_sz;
451 for (c = atom_table[hc]; c; c = c->next_atom)
452 if (c->str_len == len && !strncmp(s, c->str_dat, len))
455 c = pool_alloc(sizeof(struct atom_str) + len + 1);
457 strncpy(c->str_dat, s, len);
459 c->next_atom = atom_table[hc];
465 static struct branch* lookup_branch(const char *name)
467 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
470 for (b = branch_table[hc]; b; b = b->table_next_branch)
471 if (!strcmp(name, b->name))
476 static struct branch* new_branch(const char *name)
478 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
479 struct branch* b = lookup_branch(name);
482 die("Invalid attempt to create duplicate branch: %s", name);
483 if (check_ref_format(name))
484 die("Branch name doesn't conform to GIT standards: %s", name);
486 b = pool_calloc(1, sizeof(struct branch));
487 b->name = pool_strdup(name);
488 b->table_next_branch = branch_table[hc];
489 b->branch_tree.versions[0].mode = S_IFDIR;
490 b->branch_tree.versions[1].mode = S_IFDIR;
491 branch_table[hc] = b;
496 static unsigned int hc_entries(unsigned int cnt)
498 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
499 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
502 static struct tree_content* new_tree_content(unsigned int cnt)
504 struct avail_tree_content *f, *l = NULL;
505 struct tree_content *t;
506 unsigned int hc = hc_entries(cnt);
508 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
509 if (f->entry_capacity >= cnt)
514 l->next_avail = f->next_avail;
516 avail_tree_table[hc] = f->next_avail;
518 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
519 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
520 f->entry_capacity = cnt;
523 t = (struct tree_content*)f;
529 static void release_tree_entry(struct tree_entry *e);
530 static void release_tree_content(struct tree_content *t)
532 struct avail_tree_content *f = (struct avail_tree_content*)t;
533 unsigned int hc = hc_entries(f->entry_capacity);
534 f->next_avail = avail_tree_table[hc];
535 avail_tree_table[hc] = f;
538 static void release_tree_content_recursive(struct tree_content *t)
541 for (i = 0; i < t->entry_count; i++)
542 release_tree_entry(t->entries[i]);
543 release_tree_content(t);
546 static struct tree_content* grow_tree_content(
547 struct tree_content *t,
550 struct tree_content *r = new_tree_content(t->entry_count + amt);
551 r->entry_count = t->entry_count;
552 r->delta_depth = t->delta_depth;
553 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
554 release_tree_content(t);
558 static struct tree_entry* new_tree_entry()
560 struct tree_entry *e;
562 if (!avail_tree_entry) {
563 unsigned int n = tree_entry_alloc;
564 total_allocd += n * sizeof(struct tree_entry);
565 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
567 *((void**)e) = e + 1;
573 e = avail_tree_entry;
574 avail_tree_entry = *((void**)e);
578 static void release_tree_entry(struct tree_entry *e)
581 release_tree_content_recursive(e->tree);
582 *((void**)e) = avail_tree_entry;
583 avail_tree_entry = e;
586 static void yread(int fd, void *buffer, size_t length)
589 while (ret < length) {
590 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
592 die("Read from descriptor %i: end of stream", fd);
594 die("Read from descriptor %i: %s", fd, strerror(errno));
599 static void start_packfile()
601 struct pack_header hdr;
604 pack_name = xmalloc(strlen(base_name) + 11);
605 idx_name = xmalloc(strlen(base_name) + 11);
606 sprintf(pack_name, "%s%5.5i.pack", base_name, pack_count);
607 sprintf(idx_name, "%s%5.5i.idx", base_name, pack_count);
609 pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
611 die("Can't create %s: %s", pack_name, strerror(errno));
613 pack_data = xcalloc(1, sizeof(*pack_data) + strlen(pack_name) + 2);
614 strcpy(pack_data->pack_name, pack_name);
615 pack_data->pack_fd = pack_fd;
617 hdr.hdr_signature = htonl(PACK_SIGNATURE);
618 hdr.hdr_version = htonl(2);
621 write_or_die(pack_fd, &hdr, sizeof(hdr));
622 pack_size = sizeof(hdr);
626 static void fixup_header_footer()
633 if (lseek(pack_fd, 0, SEEK_SET) != 0)
634 die("Failed seeking to start: %s", strerror(errno));
637 yread(pack_fd, hdr, 8);
638 SHA1_Update(&c, hdr, 8);
640 cnt = htonl(object_count);
641 SHA1_Update(&c, &cnt, 4);
642 write_or_die(pack_fd, &cnt, 4);
644 buf = xmalloc(128 * 1024);
646 size_t n = xread(pack_fd, buf, 128 * 1024);
649 SHA1_Update(&c, buf, n);
653 SHA1_Final(pack_sha1, &c);
654 write_or_die(pack_fd, pack_sha1, sizeof(pack_sha1));
657 static int oecmp (const void *a_, const void *b_)
659 struct object_entry *a = *((struct object_entry**)a_);
660 struct object_entry *b = *((struct object_entry**)b_);
661 return hashcmp(a->sha1, b->sha1);
664 static void write_index(const char *idx_name)
667 struct object_entry **idx, **c, **last, *e;
668 struct object_entry_pool *o;
669 unsigned int array[256];
672 /* Build the sorted table of object IDs. */
673 idx = xmalloc(object_count * sizeof(struct object_entry*));
675 for (o = blocks; o; o = o->next_pool)
676 for (e = o->entries; e != o->next_free; e++)
678 last = idx + object_count;
679 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
681 /* Generate the fan-out array. */
683 for (i = 0; i < 256; i++) {
684 struct object_entry **next = c;;
685 while (next < last) {
686 if ((*next)->sha1[0] != i)
690 array[i] = htonl(next - idx);
694 f = sha1create("%s", idx_name);
695 sha1write(f, array, 256 * sizeof(int));
696 for (c = idx; c != last; c++) {
697 unsigned int offset = htonl((*c)->offset);
698 sha1write(f, &offset, 4);
699 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
701 sha1write(f, pack_sha1, sizeof(pack_sha1));
702 sha1close(f, NULL, 1);
706 static void end_packfile()
708 fixup_header_footer();
710 write_index(idx_name);
717 static size_t encode_header(
718 enum object_type type,
725 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
726 die("bad type %d", type);
728 c = (type << 4) | (size & 15);
740 static int store_object(
741 enum object_type type,
744 struct last_object *last,
745 unsigned char *sha1out,
749 struct object_entry *e;
750 unsigned char hdr[96];
751 unsigned char sha1[20];
752 unsigned long hdrlen, deltalen;
756 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
758 SHA1_Update(&c, hdr, hdrlen);
759 SHA1_Update(&c, dat, datlen);
760 SHA1_Final(sha1, &c);
762 hashcpy(sha1out, sha1);
764 e = insert_object(sha1);
766 insert_mark(mark, e);
769 duplicate_count_by_type[type]++;
773 e->offset = pack_size;
775 object_count_by_type[type]++;
777 if (last && last->data && last->depth < max_depth)
778 delta = diff_delta(last->data, last->len,
784 memset(&s, 0, sizeof(s));
785 deflateInit(&s, zlib_compression_level);
788 unsigned long ofs = e->offset - last->offset;
789 unsigned pos = sizeof(hdr) - 1;
791 delta_count_by_type[type]++;
794 s.avail_in = deltalen;
796 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
797 write_or_die(pack_fd, hdr, hdrlen);
800 hdr[pos] = ofs & 127;
802 hdr[--pos] = 128 | (--ofs & 127);
803 write_or_die(pack_fd, hdr + pos, sizeof(hdr) - pos);
804 pack_size += sizeof(hdr) - pos;
810 hdrlen = encode_header(type, datlen, hdr);
811 write_or_die(pack_fd, hdr, hdrlen);
815 s.avail_out = deflateBound(&s, s.avail_in);
816 s.next_out = out = xmalloc(s.avail_out);
817 while (deflate(&s, Z_FINISH) == Z_OK)
821 write_or_die(pack_fd, out, s.total_out);
822 pack_size += s.total_out;
828 if (last->data && !last->no_free)
831 last->offset = e->offset;
837 static void *gfi_unpack_entry(unsigned long ofs, unsigned long *sizep)
840 pack_data->pack_size = pack_size + 20;
841 return unpack_entry(pack_data, ofs, type, sizep);
844 static const char *get_mode(const char *str, unsigned int *modep)
847 unsigned int mode = 0;
849 while ((c = *str++) != ' ') {
850 if (c < '0' || c > '7')
852 mode = (mode << 3) + (c - '0');
858 static void load_tree(struct tree_entry *root)
860 unsigned char* sha1 = root->versions[1].sha1;
861 struct object_entry *myoe;
862 struct tree_content *t;
867 root->tree = t = new_tree_content(8);
868 if (is_null_sha1(sha1))
871 myoe = find_object(sha1);
873 if (myoe->type != OBJ_TREE)
874 die("Not a tree: %s", sha1_to_hex(sha1));
876 buf = gfi_unpack_entry(myoe->offset, &size);
879 buf = read_sha1_file(sha1, type, &size);
880 if (!buf || strcmp(type, tree_type))
881 die("Can't load tree %s", sha1_to_hex(sha1));
885 while (c != (buf + size)) {
886 struct tree_entry *e = new_tree_entry();
888 if (t->entry_count == t->entry_capacity)
889 root->tree = t = grow_tree_content(t, 8);
890 t->entries[t->entry_count++] = e;
893 c = get_mode(c, &e->versions[1].mode);
895 die("Corrupt mode in %s", sha1_to_hex(sha1));
896 e->versions[0].mode = e->versions[1].mode;
897 e->name = to_atom(c, strlen(c));
898 c += e->name->str_len + 1;
899 hashcpy(e->versions[0].sha1, (unsigned char*)c);
900 hashcpy(e->versions[1].sha1, (unsigned char*)c);
906 static int tecmp0 (const void *_a, const void *_b)
908 struct tree_entry *a = *((struct tree_entry**)_a);
909 struct tree_entry *b = *((struct tree_entry**)_b);
910 return base_name_compare(
911 a->name->str_dat, a->name->str_len, a->versions[0].mode,
912 b->name->str_dat, b->name->str_len, b->versions[0].mode);
915 static int tecmp1 (const void *_a, const void *_b)
917 struct tree_entry *a = *((struct tree_entry**)_a);
918 struct tree_entry *b = *((struct tree_entry**)_b);
919 return base_name_compare(
920 a->name->str_dat, a->name->str_len, a->versions[1].mode,
921 b->name->str_dat, b->name->str_len, b->versions[1].mode);
924 static void mktree(struct tree_content *t,
934 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
936 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
938 for (i = 0; i < t->entry_count; i++) {
939 if (t->entries[i]->versions[v].mode)
940 maxlen += t->entries[i]->name->str_len + 34;
943 size_dbuf(b, maxlen);
945 for (i = 0; i < t->entry_count; i++) {
946 struct tree_entry *e = t->entries[i];
947 if (!e->versions[v].mode)
949 c += sprintf(c, "%o", e->versions[v].mode);
951 strcpy(c, e->name->str_dat);
952 c += e->name->str_len + 1;
953 hashcpy((unsigned char*)c, e->versions[v].sha1);
956 *szp = c - (char*)b->buffer;
959 static void store_tree(struct tree_entry *root)
961 struct tree_content *t = root->tree;
962 unsigned int i, j, del;
963 unsigned long new_len;
964 struct last_object lo;
965 struct object_entry *le;
967 if (!is_null_sha1(root->versions[1].sha1))
970 for (i = 0; i < t->entry_count; i++) {
971 if (t->entries[i]->tree)
972 store_tree(t->entries[i]);
975 le = find_object(root->versions[0].sha1);
976 if (!S_ISDIR(root->versions[0].mode) || !le) {
980 mktree(t, 0, &lo.len, &old_tree);
981 lo.data = old_tree.buffer;
982 lo.offset = le->offset;
983 lo.depth = t->delta_depth;
987 mktree(t, 1, &new_len, &new_tree);
988 store_object(OBJ_TREE, new_tree.buffer, new_len,
989 &lo, root->versions[1].sha1, 0);
991 t->delta_depth = lo.depth;
992 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
993 struct tree_entry *e = t->entries[i];
994 if (e->versions[1].mode) {
995 e->versions[0].mode = e->versions[1].mode;
996 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
999 release_tree_entry(e);
1003 t->entry_count -= del;
1006 static int tree_content_set(
1007 struct tree_entry *root,
1009 const unsigned char *sha1,
1010 const unsigned int mode)
1012 struct tree_content *t = root->tree;
1015 struct tree_entry *e;
1017 slash1 = strchr(p, '/');
1023 for (i = 0; i < t->entry_count; i++) {
1025 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1027 if (e->versions[1].mode == mode
1028 && !hashcmp(e->versions[1].sha1, sha1))
1030 e->versions[1].mode = mode;
1031 hashcpy(e->versions[1].sha1, sha1);
1033 release_tree_content_recursive(e->tree);
1036 hashclr(root->versions[1].sha1);
1039 if (!S_ISDIR(e->versions[1].mode)) {
1040 e->tree = new_tree_content(8);
1041 e->versions[1].mode = S_IFDIR;
1045 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1046 hashclr(root->versions[1].sha1);
1053 if (t->entry_count == t->entry_capacity)
1054 root->tree = t = grow_tree_content(t, 8);
1055 e = new_tree_entry();
1056 e->name = to_atom(p, n);
1057 e->versions[0].mode = 0;
1058 hashclr(e->versions[0].sha1);
1059 t->entries[t->entry_count++] = e;
1061 e->tree = new_tree_content(8);
1062 e->versions[1].mode = S_IFDIR;
1063 tree_content_set(e, slash1 + 1, sha1, mode);
1066 e->versions[1].mode = mode;
1067 hashcpy(e->versions[1].sha1, sha1);
1069 hashclr(root->versions[1].sha1);
1073 static int tree_content_remove(struct tree_entry *root, const char *p)
1075 struct tree_content *t = root->tree;
1078 struct tree_entry *e;
1080 slash1 = strchr(p, '/');
1086 for (i = 0; i < t->entry_count; i++) {
1088 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1089 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1093 if (tree_content_remove(e, slash1 + 1)) {
1094 for (n = 0; n < e->tree->entry_count; n++) {
1095 if (e->tree->entries[n]->versions[1].mode) {
1096 hashclr(root->versions[1].sha1);
1109 release_tree_content_recursive(e->tree);
1112 e->versions[1].mode = 0;
1113 hashclr(e->versions[1].sha1);
1114 hashclr(root->versions[1].sha1);
1118 static void dump_branches()
1120 static const char *msg = "fast-import";
1123 struct ref_lock *lock;
1125 for (i = 0; i < branch_table_sz; i++) {
1126 for (b = branch_table[i]; b; b = b->table_next_branch) {
1127 lock = lock_any_ref_for_update(b->name, NULL);
1128 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1129 die("Can't write %s", b->name);
1134 static void dump_tags()
1136 static const char *msg = "fast-import";
1138 struct ref_lock *lock;
1139 char path[PATH_MAX];
1141 for (t = first_tag; t; t = t->next_tag) {
1142 sprintf(path, "refs/tags/%s", t->name);
1143 lock = lock_any_ref_for_update(path, NULL);
1144 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1145 die("Can't write %s", path);
1149 static void dump_marks_helper(FILE *f,
1155 for (k = 0; k < 1024; k++) {
1156 if (m->data.sets[k])
1157 dump_marks_helper(f, (base + k) << m->shift,
1161 for (k = 0; k < 1024; k++) {
1162 if (m->data.marked[k])
1163 fprintf(f, ":%lu %s\n", base + k,
1164 sha1_to_hex(m->data.marked[k]->sha1));
1169 static void dump_marks()
1173 FILE *f = fopen(mark_file, "w");
1174 dump_marks_helper(f, 0, marks);
1179 static void read_next_command()
1181 read_line(&command_buf, stdin, '\n');
1184 static void cmd_mark()
1186 if (!strncmp("mark :", command_buf.buf, 6)) {
1187 next_mark = strtoul(command_buf.buf + 6, NULL, 10);
1188 read_next_command();
1194 static void* cmd_data (size_t *size)
1200 if (strncmp("data ", command_buf.buf, 5))
1201 die("Expected 'data n' command, found: %s", command_buf.buf);
1203 length = strtoul(command_buf.buf + 5, NULL, 10);
1204 buffer = xmalloc(length);
1206 while (n < length) {
1207 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1208 if (!s && feof(stdin))
1209 die("EOF in data (%lu bytes remaining)", length - n);
1213 if (fgetc(stdin) != '\n')
1214 die("An lf did not trail the binary data as expected.");
1220 static void cmd_new_blob()
1225 read_next_command();
1229 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1233 static void unload_one_branch()
1235 while (cur_active_branches
1236 && cur_active_branches >= max_active_branches) {
1237 unsigned long min_commit = ULONG_MAX;
1238 struct branch *e, *l = NULL, *p = NULL;
1240 for (e = active_branches; e; e = e->active_next_branch) {
1241 if (e->last_commit < min_commit) {
1243 min_commit = e->last_commit;
1249 e = p->active_next_branch;
1250 p->active_next_branch = e->active_next_branch;
1252 e = active_branches;
1253 active_branches = e->active_next_branch;
1255 e->active_next_branch = NULL;
1256 if (e->branch_tree.tree) {
1257 release_tree_content_recursive(e->branch_tree.tree);
1258 e->branch_tree.tree = NULL;
1260 cur_active_branches--;
1264 static void load_branch(struct branch *b)
1266 load_tree(&b->branch_tree);
1267 b->active_next_branch = active_branches;
1268 active_branches = b;
1269 cur_active_branches++;
1270 branch_load_count++;
1273 static void file_change_m(struct branch *b)
1275 const char *p = command_buf.buf + 2;
1278 struct object_entry *oe;
1279 unsigned char sha1[20];
1283 p = get_mode(p, &mode);
1285 die("Corrupt mode: %s", command_buf.buf);
1287 case S_IFREG | 0644:
1288 case S_IFREG | 0755:
1295 die("Corrupt mode: %s", command_buf.buf);
1300 oe = find_mark(strtoul(p + 1, &x, 10));
1301 hashcpy(sha1, oe->sha1);
1304 if (get_sha1_hex(p, sha1))
1305 die("Invalid SHA1: %s", command_buf.buf);
1306 oe = find_object(sha1);
1310 die("Missing space after SHA1: %s", command_buf.buf);
1312 p_uq = unquote_c_style(p, &endp);
1315 die("Garbage after path in: %s", command_buf.buf);
1320 if (oe->type != OBJ_BLOB)
1321 die("Not a blob (actually a %s): %s",
1322 command_buf.buf, type_names[oe->type]);
1324 if (sha1_object_info(sha1, type, NULL))
1325 die("Blob not found: %s", command_buf.buf);
1326 if (strcmp(blob_type, type))
1327 die("Not a blob (actually a %s): %s",
1328 command_buf.buf, type);
1331 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1337 static void file_change_d(struct branch *b)
1339 const char *p = command_buf.buf + 2;
1343 p_uq = unquote_c_style(p, &endp);
1346 die("Garbage after path in: %s", command_buf.buf);
1349 tree_content_remove(&b->branch_tree, p);
1354 static void cmd_from(struct branch *b)
1356 const char *from, *endp;
1360 if (strncmp("from ", command_buf.buf, 5))
1364 die("Can't reinitailize branch %s", b->name);
1366 from = strchr(command_buf.buf, ' ') + 1;
1367 str_uq = unquote_c_style(from, &endp);
1370 die("Garbage after string in: %s", command_buf.buf);
1374 s = lookup_branch(from);
1376 die("Can't create a branch from itself: %s", b->name);
1378 unsigned char *t = s->branch_tree.versions[1].sha1;
1379 hashcpy(b->sha1, s->sha1);
1380 hashcpy(b->branch_tree.versions[0].sha1, t);
1381 hashcpy(b->branch_tree.versions[1].sha1, t);
1382 } else if (*from == ':') {
1383 unsigned long idnum = strtoul(from + 1, NULL, 10);
1384 struct object_entry *oe = find_mark(idnum);
1387 if (oe->type != OBJ_COMMIT)
1388 die("Mark :%lu not a commit", idnum);
1389 hashcpy(b->sha1, oe->sha1);
1390 buf = gfi_unpack_entry(oe->offset, &size);
1391 if (!buf || size < 46)
1392 die("Not a valid commit: %s", from);
1393 if (memcmp("tree ", buf, 5)
1394 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1395 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1397 hashcpy(b->branch_tree.versions[0].sha1,
1398 b->branch_tree.versions[1].sha1);
1399 } else if (!get_sha1(from, b->sha1)) {
1400 if (is_null_sha1(b->sha1)) {
1401 hashclr(b->branch_tree.versions[0].sha1);
1402 hashclr(b->branch_tree.versions[1].sha1);
1407 buf = read_object_with_reference(b->sha1,
1408 type_names[OBJ_COMMIT], &size, b->sha1);
1409 if (!buf || size < 46)
1410 die("Not a valid commit: %s", from);
1411 if (memcmp("tree ", buf, 5)
1412 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1413 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1415 hashcpy(b->branch_tree.versions[0].sha1,
1416 b->branch_tree.versions[1].sha1);
1419 die("Invalid ref name or SHA1 expression: %s", from);
1421 read_next_command();
1424 static struct hash_list* cmd_merge(unsigned int *count)
1426 struct hash_list *list = NULL, *n, *e;
1427 const char *from, *endp;
1432 while (!strncmp("merge ", command_buf.buf, 6)) {
1433 from = strchr(command_buf.buf, ' ') + 1;
1434 str_uq = unquote_c_style(from, &endp);
1437 die("Garbage after string in: %s", command_buf.buf);
1441 n = xmalloc(sizeof(*n));
1442 s = lookup_branch(from);
1444 hashcpy(n->sha1, s->sha1);
1445 else if (*from == ':') {
1446 unsigned long idnum = strtoul(from + 1, NULL, 10);
1447 struct object_entry *oe = find_mark(idnum);
1448 if (oe->type != OBJ_COMMIT)
1449 die("Mark :%lu not a commit", idnum);
1450 hashcpy(n->sha1, oe->sha1);
1451 } else if (get_sha1(from, n->sha1))
1452 die("Invalid ref name or SHA1 expression: %s", from);
1461 read_next_command();
1466 static void cmd_new_commit()
1474 char *author = NULL;
1475 char *committer = NULL;
1476 struct hash_list *merge_list = NULL;
1477 unsigned int merge_count;
1479 /* Obtain the branch name from the rest of our command */
1480 sp = strchr(command_buf.buf, ' ') + 1;
1481 str_uq = unquote_c_style(sp, &endp);
1484 die("Garbage after ref in: %s", command_buf.buf);
1487 b = lookup_branch(sp);
1493 read_next_command();
1495 if (!strncmp("author ", command_buf.buf, 7)) {
1496 author = strdup(command_buf.buf);
1497 read_next_command();
1499 if (!strncmp("committer ", command_buf.buf, 10)) {
1500 committer = strdup(command_buf.buf);
1501 read_next_command();
1504 die("Expected committer but didn't get one");
1505 msg = cmd_data(&msglen);
1506 read_next_command();
1508 merge_list = cmd_merge(&merge_count);
1510 /* ensure the branch is active/loaded */
1511 if (!b->branch_tree.tree || !max_active_branches) {
1512 unload_one_branch();
1518 if (1 == command_buf.len)
1520 else if (!strncmp("M ", command_buf.buf, 2))
1522 else if (!strncmp("D ", command_buf.buf, 2))
1525 die("Unsupported file_change: %s", command_buf.buf);
1526 read_next_command();
1529 /* build the tree and the commit */
1530 store_tree(&b->branch_tree);
1531 hashcpy(b->branch_tree.versions[0].sha1,
1532 b->branch_tree.versions[1].sha1);
1533 size_dbuf(&new_data, 97 + msglen
1536 ? strlen(author) + strlen(committer)
1537 : 2 * strlen(committer)));
1538 sp = new_data.buffer;
1539 sp += sprintf(sp, "tree %s\n",
1540 sha1_to_hex(b->branch_tree.versions[1].sha1));
1541 if (!is_null_sha1(b->sha1))
1542 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1543 while (merge_list) {
1544 struct hash_list *next = merge_list->next;
1545 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1550 sp += sprintf(sp, "%s\n", author);
1552 sp += sprintf(sp, "author %s\n", committer + 10);
1553 sp += sprintf(sp, "%s\n\n", committer);
1554 memcpy(sp, msg, msglen);
1561 store_object(OBJ_COMMIT,
1562 new_data.buffer, sp - (char*)new_data.buffer,
1563 NULL, b->sha1, next_mark);
1564 b->last_commit = object_count_by_type[OBJ_COMMIT];
1567 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1568 fprintf(branch_log, "commit ");
1570 fputc('"', branch_log);
1571 quote_c_style(b->name, NULL, branch_log, 0);
1572 fputc('"', branch_log);
1574 fprintf(branch_log, "%s", b->name);
1575 fprintf(branch_log," :%lu %s\n",next_mark,sha1_to_hex(b->sha1));
1579 static void cmd_new_tag()
1590 unsigned long from_mark = 0;
1591 unsigned char sha1[20];
1593 /* Obtain the new tag name from the rest of our command */
1594 sp = strchr(command_buf.buf, ' ') + 1;
1595 str_uq = unquote_c_style(sp, &endp);
1598 die("Garbage after tag name in: %s", command_buf.buf);
1601 t = pool_alloc(sizeof(struct tag));
1603 t->name = pool_strdup(sp);
1605 last_tag->next_tag = t;
1611 read_next_command();
1614 if (strncmp("from ", command_buf.buf, 5))
1615 die("Expected from command, got %s", command_buf.buf);
1617 from = strchr(command_buf.buf, ' ') + 1;
1618 str_uq = unquote_c_style(from, &endp);
1621 die("Garbage after string in: %s", command_buf.buf);
1625 s = lookup_branch(from);
1627 hashcpy(sha1, s->sha1);
1628 } else if (*from == ':') {
1629 from_mark = strtoul(from + 1, NULL, 10);
1630 struct object_entry *oe = find_mark(from_mark);
1631 if (oe->type != OBJ_COMMIT)
1632 die("Mark :%lu not a commit", from_mark);
1633 hashcpy(sha1, oe->sha1);
1634 } else if (!get_sha1(from, sha1)) {
1638 buf = read_object_with_reference(sha1,
1639 type_names[OBJ_COMMIT], &size, sha1);
1640 if (!buf || size < 46)
1641 die("Not a valid commit: %s", from);
1644 die("Invalid ref name or SHA1 expression: %s", from);
1648 read_next_command();
1651 if (strncmp("tagger ", command_buf.buf, 7))
1652 die("Expected tagger command, got %s", command_buf.buf);
1653 tagger = strdup(command_buf.buf);
1655 /* tag payload/message */
1656 read_next_command();
1657 msg = cmd_data(&msglen);
1659 /* build the tag object */
1660 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1661 sp = new_data.buffer;
1662 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1663 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1664 sp += sprintf(sp, "tag %s\n", t->name);
1665 sp += sprintf(sp, "%s\n\n", tagger);
1666 memcpy(sp, msg, msglen);
1671 store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1675 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1676 fprintf(branch_log, "tag ");
1678 fputc('"', branch_log);
1679 quote_c_style(t->name, NULL, branch_log, 0);
1680 fputc('"', branch_log);
1682 fprintf(branch_log, "%s", t->name);
1683 fprintf(branch_log," :%lu %s\n",from_mark,sha1_to_hex(t->sha1));
1687 static void cmd_reset_branch()
1694 /* Obtain the branch name from the rest of our command */
1695 sp = strchr(command_buf.buf, ' ') + 1;
1696 str_uq = unquote_c_style(sp, &endp);
1699 die("Garbage after ref in: %s", command_buf.buf);
1702 b = lookup_branch(sp);
1705 if (b->branch_tree.tree) {
1706 release_tree_content_recursive(b->branch_tree.tree);
1707 b->branch_tree.tree = NULL;
1714 read_next_command();
1718 static const char fast_import_usage[] =
1719 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1721 int main(int argc, const char **argv)
1724 unsigned long est_obj_cnt = object_entry_alloc;
1728 git_config(git_default_config);
1730 for (i = 1; i < argc; i++) {
1731 const char *a = argv[i];
1733 if (*a != '-' || !strcmp(a, "--"))
1735 else if (!strncmp(a, "--objects=", 10))
1736 est_obj_cnt = strtoul(a + 10, NULL, 0);
1737 else if (!strncmp(a, "--depth=", 8))
1738 max_depth = strtoul(a + 8, NULL, 0);
1739 else if (!strncmp(a, "--active-branches=", 18))
1740 max_active_branches = strtoul(a + 18, NULL, 0);
1741 else if (!strncmp(a, "--export-marks=", 15))
1743 else if (!strncmp(a, "--branch-log=", 13)) {
1744 branch_log = fopen(a + 13, "w");
1746 die("Can't create %s: %s", a + 13, strerror(errno));
1749 die("unknown option %s", a);
1752 usage(fast_import_usage);
1753 base_name = argv[i];
1755 alloc_objects(est_obj_cnt);
1756 strbuf_init(&command_buf);
1758 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1759 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1760 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1761 marks = pool_calloc(1, sizeof(struct mark_set));
1765 read_next_command();
1766 if (command_buf.eof)
1768 else if (!strcmp("blob", command_buf.buf))
1770 else if (!strncmp("commit ", command_buf.buf, 7))
1772 else if (!strncmp("tag ", command_buf.buf, 4))
1774 else if (!strncmp("reset ", command_buf.buf, 6))
1777 die("Unsupported command: %s", command_buf.buf);
1787 fprintf(stderr, "%s statistics:\n", argv[0]);
1788 fprintf(stderr, "---------------------------------------------------------------------\n");
1789 fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1790 fprintf(stderr, "Total objects: %10lu (%10lu duplicates )\n", object_count, duplicate_count);
1791 fprintf(stderr, " blobs : %10lu (%10lu duplicates %10lu deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
1792 fprintf(stderr, " trees : %10lu (%10lu duplicates %10lu deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
1793 fprintf(stderr, " commits: %10lu (%10lu duplicates %10lu deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
1794 fprintf(stderr, " tags : %10lu (%10lu duplicates %10lu deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
1795 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1796 fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count);
1797 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1798 fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1799 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1800 fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1801 fprintf(stderr, "---------------------------------------------------------------------\n");
1802 fprintf(stderr, "\n");