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 enum object_type type;
114 unsigned long offset;
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];
141 unsigned char sha1[20];
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 remap_count;
220 static unsigned long object_count;
221 static unsigned long duplicate_count;
222 static unsigned long marks_set_count;
223 static unsigned long object_count_by_type[9];
224 static unsigned long duplicate_count_by_type[9];
225 static unsigned long delta_count_by_type[9];
228 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
229 static size_t total_allocd;
230 static struct mem_pool *mem_pool;
232 /* Atom management */
233 static unsigned int atom_table_sz = 4451;
234 static unsigned int atom_cnt;
235 static struct atom_str **atom_table;
237 /* The .pack file being generated */
239 static unsigned long pack_size;
240 static unsigned char pack_sha1[20];
241 static unsigned char* pack_base;
242 static unsigned long pack_moff;
243 static unsigned long pack_mlen = 128*1024*1024;
244 static unsigned long page_size;
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(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 size_t encode_header(
600 enum object_type type,
607 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
608 die("bad type %d", type);
610 c = (type << 4) | (size & 15);
622 static int store_object(
623 enum object_type type,
626 struct last_object *last,
627 unsigned char *sha1out,
631 struct object_entry *e;
632 unsigned char hdr[96];
633 unsigned char sha1[20];
634 unsigned long hdrlen, deltalen;
638 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
640 SHA1_Update(&c, hdr, hdrlen);
641 SHA1_Update(&c, dat, datlen);
642 SHA1_Final(sha1, &c);
644 hashcpy(sha1out, sha1);
646 e = insert_object(sha1);
648 insert_mark(mark, e);
651 duplicate_count_by_type[type]++;
655 e->offset = pack_size;
657 object_count_by_type[type]++;
659 if (last && last->data && last->depth < max_depth)
660 delta = diff_delta(last->data, last->len,
666 memset(&s, 0, sizeof(s));
667 deflateInit(&s, zlib_compression_level);
670 delta_count_by_type[type]++;
673 s.avail_in = deltalen;
674 hdrlen = encode_header(OBJ_REF_DELTA, deltalen, hdr);
675 write_or_die(pack_fd, hdr, hdrlen);
676 write_or_die(pack_fd, last->sha1, sizeof(sha1));
677 pack_size += hdrlen + sizeof(sha1);
683 hdrlen = encode_header(type, datlen, hdr);
684 write_or_die(pack_fd, hdr, hdrlen);
688 s.avail_out = deflateBound(&s, s.avail_in);
689 s.next_out = out = xmalloc(s.avail_out);
690 while (deflate(&s, Z_FINISH) == Z_OK)
694 write_or_die(pack_fd, out, s.total_out);
695 pack_size += s.total_out;
701 if (last->data && !last->no_free)
705 hashcpy(last->sha1, sha1);
710 static unsigned char* map_pack(unsigned long offset, unsigned int *left)
712 if (offset >= pack_size)
713 die("object offset outside of pack file");
715 || offset < pack_moff
716 || (offset + 20) >= (pack_moff + pack_mlen)) {
718 munmap(pack_base, pack_mlen);
719 pack_moff = (offset / page_size) * page_size;
720 pack_base = mmap(NULL,pack_mlen,PROT_READ,MAP_SHARED,
722 if (pack_base == MAP_FAILED)
723 die("Failed to map generated pack: %s", strerror(errno));
728 *left = pack_mlen - offset;
729 return pack_base + offset;
732 static unsigned long unpack_object_header(unsigned long offset,
733 enum object_type *type,
734 unsigned long *sizep)
740 c = *map_pack(offset++, NULL);
741 *type = (c >> 4) & 7;
745 c = *map_pack(offset++, NULL);
746 size += (c & 0x7f) << shift;
753 static void *unpack_non_delta_entry(unsigned long o, unsigned long sz)
756 unsigned char *result;
758 result = xmalloc(sz + 1);
761 memset(&stream, 0, sizeof(stream));
762 stream.next_in = map_pack(o, &stream.avail_in);
763 stream.next_out = result;
764 stream.avail_out = sz;
766 inflateInit(&stream);
768 int st = inflate(&stream, Z_FINISH);
769 if (st == Z_STREAM_END)
771 if (st == Z_OK || st == Z_BUF_ERROR) {
772 o = stream.next_in - pack_base + pack_moff;
773 stream.next_in = map_pack(o, &stream.avail_in);
776 die("Error %i from zlib during inflate.", st);
779 if (stream.total_out != sz)
780 die("Error after inflate: sizes mismatch");
784 static void *gfi_unpack_entry(unsigned long offset,
785 unsigned long *sizep,
786 unsigned int *delta_depth);
788 static void *unpack_delta_entry(unsigned long offset,
789 unsigned long delta_size,
790 unsigned long *sizep,
791 unsigned int *delta_depth)
793 struct object_entry *base_oe;
794 unsigned char *base_sha1;
795 void *delta_data, *base, *result;
796 unsigned long base_size, result_size;
798 base_sha1 = map_pack(offset, NULL);
799 base_oe = find_object(base_sha1);
801 die("I'm broken; I can't find a base I know must be here.");
802 base = gfi_unpack_entry(base_oe->offset, &base_size, delta_depth);
803 delta_data = unpack_non_delta_entry(offset + 20, delta_size);
804 result = patch_delta(base, base_size,
805 delta_data, delta_size,
808 die("failed to apply delta");
811 *sizep = result_size;
816 static void *gfi_unpack_entry(unsigned long offset,
817 unsigned long *sizep,
818 unsigned int *delta_depth)
821 enum object_type kind;
823 offset = unpack_object_header(offset, &kind, &size);
826 return unpack_delta_entry(offset, size, sizep, delta_depth);
833 return unpack_non_delta_entry(offset, size);
835 die("I created an object I can't read!");
839 static const char *get_mode(const char *str, unsigned int *modep)
842 unsigned int mode = 0;
844 while ((c = *str++) != ' ') {
845 if (c < '0' || c > '7')
847 mode = (mode << 3) + (c - '0');
853 static void load_tree(struct tree_entry *root)
855 unsigned char* sha1 = root->versions[1].sha1;
856 struct object_entry *myoe;
857 struct tree_content *t;
862 root->tree = t = new_tree_content(8);
863 if (is_null_sha1(sha1))
866 myoe = find_object(sha1);
868 if (myoe->type != OBJ_TREE)
869 die("Not a tree: %s", sha1_to_hex(sha1));
870 buf = gfi_unpack_entry(myoe->offset, &size, &t->delta_depth);
873 buf = read_sha1_file(sha1, type, &size);
874 if (!buf || strcmp(type, tree_type))
875 die("Can't load tree %s", sha1_to_hex(sha1));
879 while (c != (buf + size)) {
880 struct tree_entry *e = new_tree_entry();
882 if (t->entry_count == t->entry_capacity)
883 root->tree = t = grow_tree_content(t, 8);
884 t->entries[t->entry_count++] = e;
887 c = get_mode(c, &e->versions[1].mode);
889 die("Corrupt mode in %s", sha1_to_hex(sha1));
890 e->versions[0].mode = e->versions[1].mode;
891 e->name = to_atom(c, strlen(c));
892 c += e->name->str_len + 1;
893 hashcpy(e->versions[0].sha1, (unsigned char*)c);
894 hashcpy(e->versions[1].sha1, (unsigned char*)c);
900 static int tecmp0 (const void *_a, const void *_b)
902 struct tree_entry *a = *((struct tree_entry**)_a);
903 struct tree_entry *b = *((struct tree_entry**)_b);
904 return base_name_compare(
905 a->name->str_dat, a->name->str_len, a->versions[0].mode,
906 b->name->str_dat, b->name->str_len, b->versions[0].mode);
909 static int tecmp1 (const void *_a, const void *_b)
911 struct tree_entry *a = *((struct tree_entry**)_a);
912 struct tree_entry *b = *((struct tree_entry**)_b);
913 return base_name_compare(
914 a->name->str_dat, a->name->str_len, a->versions[1].mode,
915 b->name->str_dat, b->name->str_len, b->versions[1].mode);
918 static void mktree(struct tree_content *t,
928 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
930 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
932 for (i = 0; i < t->entry_count; i++) {
933 if (t->entries[i]->versions[v].mode)
934 maxlen += t->entries[i]->name->str_len + 34;
937 size_dbuf(b, maxlen);
939 for (i = 0; i < t->entry_count; i++) {
940 struct tree_entry *e = t->entries[i];
941 if (!e->versions[v].mode)
943 c += sprintf(c, "%o", e->versions[v].mode);
945 strcpy(c, e->name->str_dat);
946 c += e->name->str_len + 1;
947 hashcpy((unsigned char*)c, e->versions[v].sha1);
950 *szp = c - (char*)b->buffer;
953 static void store_tree(struct tree_entry *root)
955 struct tree_content *t = root->tree;
956 unsigned int i, j, del;
957 unsigned long new_len;
958 struct last_object lo;
960 if (!is_null_sha1(root->versions[1].sha1))
963 for (i = 0; i < t->entry_count; i++) {
964 if (t->entries[i]->tree)
965 store_tree(t->entries[i]);
968 if (!S_ISDIR(root->versions[0].mode)
969 || is_null_sha1(root->versions[0].sha1)
970 || !find_object(root->versions[0].sha1)) {
974 mktree(t, 0, &lo.len, &old_tree);
975 lo.data = old_tree.buffer;
976 lo.depth = t->delta_depth;
978 hashcpy(lo.sha1, root->versions[0].sha1);
981 mktree(t, 1, &new_len, &new_tree);
982 store_object(OBJ_TREE, new_tree.buffer, new_len,
983 &lo, root->versions[1].sha1, 0);
985 t->delta_depth = lo.depth;
986 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
987 struct tree_entry *e = t->entries[i];
988 if (e->versions[1].mode) {
989 e->versions[0].mode = e->versions[1].mode;
990 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
993 release_tree_entry(e);
997 t->entry_count -= del;
1000 static int tree_content_set(
1001 struct tree_entry *root,
1003 const unsigned char *sha1,
1004 const unsigned int mode)
1006 struct tree_content *t = root->tree;
1009 struct tree_entry *e;
1011 slash1 = strchr(p, '/');
1017 for (i = 0; i < t->entry_count; i++) {
1019 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1021 if (e->versions[1].mode == mode
1022 && !hashcmp(e->versions[1].sha1, sha1))
1024 e->versions[1].mode = mode;
1025 hashcpy(e->versions[1].sha1, sha1);
1027 release_tree_content_recursive(e->tree);
1030 hashclr(root->versions[1].sha1);
1033 if (!S_ISDIR(e->versions[1].mode)) {
1034 e->tree = new_tree_content(8);
1035 e->versions[1].mode = S_IFDIR;
1039 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1040 hashclr(root->versions[1].sha1);
1047 if (t->entry_count == t->entry_capacity)
1048 root->tree = t = grow_tree_content(t, 8);
1049 e = new_tree_entry();
1050 e->name = to_atom(p, n);
1051 e->versions[0].mode = 0;
1052 hashclr(e->versions[0].sha1);
1053 t->entries[t->entry_count++] = e;
1055 e->tree = new_tree_content(8);
1056 e->versions[1].mode = S_IFDIR;
1057 tree_content_set(e, slash1 + 1, sha1, mode);
1060 e->versions[1].mode = mode;
1061 hashcpy(e->versions[1].sha1, sha1);
1063 hashclr(root->versions[1].sha1);
1067 static int tree_content_remove(struct tree_entry *root, const char *p)
1069 struct tree_content *t = root->tree;
1072 struct tree_entry *e;
1074 slash1 = strchr(p, '/');
1080 for (i = 0; i < t->entry_count; i++) {
1082 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1083 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1087 if (tree_content_remove(e, slash1 + 1)) {
1088 for (n = 0; n < e->tree->entry_count; n++) {
1089 if (e->tree->entries[n]->versions[1].mode) {
1090 hashclr(root->versions[1].sha1);
1103 release_tree_content_recursive(e->tree);
1106 e->versions[1].mode = 0;
1107 hashclr(e->versions[1].sha1);
1108 hashclr(root->versions[1].sha1);
1112 static void init_pack_header()
1114 struct pack_header hdr;
1116 hdr.hdr_signature = htonl(PACK_SIGNATURE);
1117 hdr.hdr_version = htonl(2);
1118 hdr.hdr_entries = 0;
1120 write_or_die(pack_fd, &hdr, sizeof(hdr));
1121 pack_size = sizeof(hdr);
1124 static void fixup_header_footer()
1132 if (lseek(pack_fd, 0, SEEK_SET) != 0)
1133 die("Failed seeking to start: %s", strerror(errno));
1136 yread(pack_fd, hdr, 8);
1137 SHA1_Update(&c, hdr, 8);
1139 cnt = htonl(object_count);
1140 SHA1_Update(&c, &cnt, 4);
1141 write_or_die(pack_fd, &cnt, 4);
1143 buf = xmalloc(128 * 1024);
1145 n = xread(pack_fd, buf, 128 * 1024);
1148 SHA1_Update(&c, buf, n);
1152 SHA1_Final(pack_sha1, &c);
1153 write_or_die(pack_fd, pack_sha1, sizeof(pack_sha1));
1156 static int oecmp (const void *_a, const void *_b)
1158 struct object_entry *a = *((struct object_entry**)_a);
1159 struct object_entry *b = *((struct object_entry**)_b);
1160 return hashcmp(a->sha1, b->sha1);
1163 static void write_index(const char *idx_name)
1166 struct object_entry **idx, **c, **last;
1167 struct object_entry *e;
1168 struct object_entry_pool *o;
1169 unsigned int array[256];
1172 /* Build the sorted table of object IDs. */
1173 idx = xmalloc(object_count * sizeof(struct object_entry*));
1175 for (o = blocks; o; o = o->next_pool)
1176 for (e = o->entries; e != o->next_free; e++)
1178 last = idx + object_count;
1179 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
1181 /* Generate the fan-out array. */
1183 for (i = 0; i < 256; i++) {
1184 struct object_entry **next = c;;
1185 while (next < last) {
1186 if ((*next)->sha1[0] != i)
1190 array[i] = htonl(next - idx);
1194 f = sha1create("%s", idx_name);
1195 sha1write(f, array, 256 * sizeof(int));
1196 for (c = idx; c != last; c++) {
1197 unsigned int offset = htonl((*c)->offset);
1198 sha1write(f, &offset, 4);
1199 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
1201 sha1write(f, pack_sha1, sizeof(pack_sha1));
1202 sha1close(f, NULL, 1);
1206 static void dump_branches()
1208 static const char *msg = "fast-import";
1211 struct ref_lock *lock;
1213 for (i = 0; i < branch_table_sz; i++) {
1214 for (b = branch_table[i]; b; b = b->table_next_branch) {
1215 lock = lock_any_ref_for_update(b->name, NULL);
1216 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1217 die("Can't write %s", b->name);
1222 static void dump_tags()
1224 static const char *msg = "fast-import";
1226 struct ref_lock *lock;
1227 char path[PATH_MAX];
1229 for (t = first_tag; t; t = t->next_tag) {
1230 sprintf(path, "refs/tags/%s", t->name);
1231 lock = lock_any_ref_for_update(path, NULL);
1232 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1233 die("Can't write %s", path);
1237 static void dump_marks_helper(FILE *f,
1243 for (k = 0; k < 1024; k++) {
1244 if (m->data.sets[k])
1245 dump_marks_helper(f, (base + k) << m->shift,
1249 for (k = 0; k < 1024; k++) {
1250 if (m->data.marked[k])
1251 fprintf(f, ":%lu %s\n", base + k,
1252 sha1_to_hex(m->data.marked[k]->sha1));
1257 static void dump_marks()
1261 FILE *f = fopen(mark_file, "w");
1262 dump_marks_helper(f, 0, marks);
1267 static void read_next_command()
1269 read_line(&command_buf, stdin, '\n');
1272 static void cmd_mark()
1274 if (!strncmp("mark :", command_buf.buf, 6)) {
1275 next_mark = strtoul(command_buf.buf + 6, NULL, 10);
1276 read_next_command();
1282 static void* cmd_data (size_t *size)
1288 if (strncmp("data ", command_buf.buf, 5))
1289 die("Expected 'data n' command, found: %s", command_buf.buf);
1291 length = strtoul(command_buf.buf + 5, NULL, 10);
1292 buffer = xmalloc(length);
1294 while (n < length) {
1295 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1296 if (!s && feof(stdin))
1297 die("EOF in data (%lu bytes remaining)", length - n);
1301 if (fgetc(stdin) != '\n')
1302 die("An lf did not trail the binary data as expected.");
1308 static void cmd_new_blob()
1313 read_next_command();
1317 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1321 static void unload_one_branch()
1323 while (cur_active_branches
1324 && cur_active_branches >= max_active_branches) {
1325 unsigned long min_commit = ULONG_MAX;
1326 struct branch *e, *l = NULL, *p = NULL;
1328 for (e = active_branches; e; e = e->active_next_branch) {
1329 if (e->last_commit < min_commit) {
1331 min_commit = e->last_commit;
1337 e = p->active_next_branch;
1338 p->active_next_branch = e->active_next_branch;
1340 e = active_branches;
1341 active_branches = e->active_next_branch;
1343 e->active_next_branch = NULL;
1344 if (e->branch_tree.tree) {
1345 release_tree_content_recursive(e->branch_tree.tree);
1346 e->branch_tree.tree = NULL;
1348 cur_active_branches--;
1352 static void load_branch(struct branch *b)
1354 load_tree(&b->branch_tree);
1355 b->active_next_branch = active_branches;
1356 active_branches = b;
1357 cur_active_branches++;
1358 branch_load_count++;
1361 static void file_change_m(struct branch *b)
1363 const char *p = command_buf.buf + 2;
1366 struct object_entry *oe;
1367 unsigned char sha1[20];
1371 p = get_mode(p, &mode);
1373 die("Corrupt mode: %s", command_buf.buf);
1375 case S_IFREG | 0644:
1376 case S_IFREG | 0755:
1383 die("Corrupt mode: %s", command_buf.buf);
1388 oe = find_mark(strtoul(p + 1, &x, 10));
1389 hashcpy(sha1, oe->sha1);
1392 if (get_sha1_hex(p, sha1))
1393 die("Invalid SHA1: %s", command_buf.buf);
1394 oe = find_object(sha1);
1398 die("Missing space after SHA1: %s", command_buf.buf);
1400 p_uq = unquote_c_style(p, &endp);
1403 die("Garbage after path in: %s", command_buf.buf);
1408 if (oe->type != OBJ_BLOB)
1409 die("Not a blob (actually a %s): %s",
1410 command_buf.buf, type_names[oe->type]);
1412 if (sha1_object_info(sha1, type, NULL))
1413 die("Blob not found: %s", command_buf.buf);
1414 if (strcmp(blob_type, type))
1415 die("Not a blob (actually a %s): %s",
1416 command_buf.buf, type);
1419 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1425 static void file_change_d(struct branch *b)
1427 const char *p = command_buf.buf + 2;
1431 p_uq = unquote_c_style(p, &endp);
1434 die("Garbage after path in: %s", command_buf.buf);
1437 tree_content_remove(&b->branch_tree, p);
1442 static void cmd_from(struct branch *b)
1444 const char *from, *endp;
1448 if (strncmp("from ", command_buf.buf, 5))
1452 die("Can't reinitailize branch %s", b->name);
1454 from = strchr(command_buf.buf, ' ') + 1;
1455 str_uq = unquote_c_style(from, &endp);
1458 die("Garbage after string in: %s", command_buf.buf);
1462 s = lookup_branch(from);
1464 die("Can't create a branch from itself: %s", b->name);
1466 unsigned char *t = s->branch_tree.versions[1].sha1;
1467 hashcpy(b->sha1, s->sha1);
1468 hashcpy(b->branch_tree.versions[0].sha1, t);
1469 hashcpy(b->branch_tree.versions[1].sha1, t);
1470 } else if (*from == ':') {
1471 unsigned long idnum = strtoul(from + 1, NULL, 10);
1472 struct object_entry *oe = find_mark(idnum);
1476 if (oe->type != OBJ_COMMIT)
1477 die("Mark :%lu not a commit", idnum);
1478 hashcpy(b->sha1, oe->sha1);
1479 buf = gfi_unpack_entry(oe->offset, &size, &depth);
1480 if (!buf || size < 46)
1481 die("Not a valid commit: %s", from);
1482 if (memcmp("tree ", buf, 5)
1483 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1484 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1486 hashcpy(b->branch_tree.versions[0].sha1,
1487 b->branch_tree.versions[1].sha1);
1488 } else if (!get_sha1(from, b->sha1)) {
1489 if (is_null_sha1(b->sha1)) {
1490 hashclr(b->branch_tree.versions[0].sha1);
1491 hashclr(b->branch_tree.versions[1].sha1);
1496 buf = read_object_with_reference(b->sha1,
1497 type_names[OBJ_COMMIT], &size, b->sha1);
1498 if (!buf || size < 46)
1499 die("Not a valid commit: %s", from);
1500 if (memcmp("tree ", buf, 5)
1501 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1502 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1504 hashcpy(b->branch_tree.versions[0].sha1,
1505 b->branch_tree.versions[1].sha1);
1508 die("Invalid ref name or SHA1 expression: %s", from);
1510 read_next_command();
1513 static struct hash_list* cmd_merge(unsigned int *count)
1515 struct hash_list *list = NULL, *n, *e;
1516 const char *from, *endp;
1521 while (!strncmp("merge ", command_buf.buf, 6)) {
1522 from = strchr(command_buf.buf, ' ') + 1;
1523 str_uq = unquote_c_style(from, &endp);
1526 die("Garbage after string in: %s", command_buf.buf);
1530 n = xmalloc(sizeof(*n));
1531 s = lookup_branch(from);
1533 hashcpy(n->sha1, s->sha1);
1534 else if (*from == ':') {
1535 unsigned long idnum = strtoul(from + 1, NULL, 10);
1536 struct object_entry *oe = find_mark(idnum);
1537 if (oe->type != OBJ_COMMIT)
1538 die("Mark :%lu not a commit", idnum);
1539 hashcpy(n->sha1, oe->sha1);
1540 } else if (get_sha1(from, n->sha1))
1541 die("Invalid ref name or SHA1 expression: %s", from);
1550 read_next_command();
1555 static void cmd_new_commit()
1563 char *author = NULL;
1564 char *committer = NULL;
1565 struct hash_list *merge_list = NULL;
1566 unsigned int merge_count;
1568 /* Obtain the branch name from the rest of our command */
1569 sp = strchr(command_buf.buf, ' ') + 1;
1570 str_uq = unquote_c_style(sp, &endp);
1573 die("Garbage after ref in: %s", command_buf.buf);
1576 b = lookup_branch(sp);
1582 read_next_command();
1584 if (!strncmp("author ", command_buf.buf, 7)) {
1585 author = strdup(command_buf.buf);
1586 read_next_command();
1588 if (!strncmp("committer ", command_buf.buf, 10)) {
1589 committer = strdup(command_buf.buf);
1590 read_next_command();
1593 die("Expected committer but didn't get one");
1594 msg = cmd_data(&msglen);
1595 read_next_command();
1597 merge_list = cmd_merge(&merge_count);
1599 /* ensure the branch is active/loaded */
1600 if (!b->branch_tree.tree || !max_active_branches) {
1601 unload_one_branch();
1607 if (1 == command_buf.len)
1609 else if (!strncmp("M ", command_buf.buf, 2))
1611 else if (!strncmp("D ", command_buf.buf, 2))
1614 die("Unsupported file_change: %s", command_buf.buf);
1615 read_next_command();
1618 /* build the tree and the commit */
1619 store_tree(&b->branch_tree);
1620 hashcpy(b->branch_tree.versions[0].sha1,
1621 b->branch_tree.versions[1].sha1);
1622 size_dbuf(&new_data, 97 + msglen
1625 ? strlen(author) + strlen(committer)
1626 : 2 * strlen(committer)));
1627 sp = new_data.buffer;
1628 sp += sprintf(sp, "tree %s\n",
1629 sha1_to_hex(b->branch_tree.versions[1].sha1));
1630 if (!is_null_sha1(b->sha1))
1631 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1632 while (merge_list) {
1633 struct hash_list *next = merge_list->next;
1634 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1639 sp += sprintf(sp, "%s\n", author);
1641 sp += sprintf(sp, "author %s\n", committer + 10);
1642 sp += sprintf(sp, "%s\n\n", committer);
1643 memcpy(sp, msg, msglen);
1650 store_object(OBJ_COMMIT,
1651 new_data.buffer, sp - (char*)new_data.buffer,
1652 NULL, b->sha1, next_mark);
1653 b->last_commit = object_count_by_type[OBJ_COMMIT];
1656 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1657 fprintf(branch_log, "commit ");
1659 fputc('"', branch_log);
1660 quote_c_style(b->name, NULL, branch_log, 0);
1661 fputc('"', branch_log);
1663 fprintf(branch_log, "%s", b->name);
1664 fprintf(branch_log," :%lu %s\n",next_mark,sha1_to_hex(b->sha1));
1668 static void cmd_new_tag()
1679 unsigned long from_mark = 0;
1680 unsigned char sha1[20];
1682 /* Obtain the new tag name from the rest of our command */
1683 sp = strchr(command_buf.buf, ' ') + 1;
1684 str_uq = unquote_c_style(sp, &endp);
1687 die("Garbage after tag name in: %s", command_buf.buf);
1690 t = pool_alloc(sizeof(struct tag));
1692 t->name = pool_strdup(sp);
1694 last_tag->next_tag = t;
1700 read_next_command();
1703 if (strncmp("from ", command_buf.buf, 5))
1704 die("Expected from command, got %s", command_buf.buf);
1706 from = strchr(command_buf.buf, ' ') + 1;
1707 str_uq = unquote_c_style(from, &endp);
1710 die("Garbage after string in: %s", command_buf.buf);
1714 s = lookup_branch(from);
1716 hashcpy(sha1, s->sha1);
1717 } else if (*from == ':') {
1718 from_mark = strtoul(from + 1, NULL, 10);
1719 struct object_entry *oe = find_mark(from_mark);
1720 if (oe->type != OBJ_COMMIT)
1721 die("Mark :%lu not a commit", from_mark);
1722 hashcpy(sha1, oe->sha1);
1723 } else if (!get_sha1(from, sha1)) {
1727 buf = read_object_with_reference(sha1,
1728 type_names[OBJ_COMMIT], &size, sha1);
1729 if (!buf || size < 46)
1730 die("Not a valid commit: %s", from);
1733 die("Invalid ref name or SHA1 expression: %s", from);
1737 read_next_command();
1740 if (strncmp("tagger ", command_buf.buf, 7))
1741 die("Expected tagger command, got %s", command_buf.buf);
1742 tagger = strdup(command_buf.buf);
1744 /* tag payload/message */
1745 read_next_command();
1746 msg = cmd_data(&msglen);
1748 /* build the tag object */
1749 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1750 sp = new_data.buffer;
1751 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1752 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1753 sp += sprintf(sp, "tag %s\n", t->name);
1754 sp += sprintf(sp, "%s\n\n", tagger);
1755 memcpy(sp, msg, msglen);
1760 store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1764 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1765 fprintf(branch_log, "tag ");
1767 fputc('"', branch_log);
1768 quote_c_style(t->name, NULL, branch_log, 0);
1769 fputc('"', branch_log);
1771 fprintf(branch_log, "%s", t->name);
1772 fprintf(branch_log," :%lu %s\n",from_mark,sha1_to_hex(t->sha1));
1776 static void cmd_reset_branch()
1783 /* Obtain the branch name from the rest of our command */
1784 sp = strchr(command_buf.buf, ' ') + 1;
1785 str_uq = unquote_c_style(sp, &endp);
1788 die("Garbage after ref in: %s", command_buf.buf);
1791 b = lookup_branch(sp);
1794 if (b->branch_tree.tree) {
1795 release_tree_content_recursive(b->branch_tree.tree);
1796 b->branch_tree.tree = NULL;
1803 read_next_command();
1807 static const char fast_import_usage[] =
1808 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1810 int main(int argc, const char **argv)
1812 const char *base_name;
1814 unsigned long est_obj_cnt = object_entry_alloc;
1820 git_config(git_default_config);
1821 page_size = getpagesize();
1823 for (i = 1; i < argc; i++) {
1824 const char *a = argv[i];
1826 if (*a != '-' || !strcmp(a, "--"))
1828 else if (!strncmp(a, "--objects=", 10))
1829 est_obj_cnt = strtoul(a + 10, NULL, 0);
1830 else if (!strncmp(a, "--depth=", 8))
1831 max_depth = strtoul(a + 8, NULL, 0);
1832 else if (!strncmp(a, "--active-branches=", 18))
1833 max_active_branches = strtoul(a + 18, NULL, 0);
1834 else if (!strncmp(a, "--export-marks=", 15))
1836 else if (!strncmp(a, "--branch-log=", 13)) {
1837 branch_log = fopen(a + 13, "w");
1839 die("Can't create %s: %s", a + 13, strerror(errno));
1842 die("unknown option %s", a);
1845 usage(fast_import_usage);
1846 base_name = argv[i];
1848 pack_name = xmalloc(strlen(base_name) + 6);
1849 sprintf(pack_name, "%s.pack", base_name);
1850 idx_name = xmalloc(strlen(base_name) + 5);
1851 sprintf(idx_name, "%s.idx", base_name);
1853 pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
1855 die("Can't create %s: %s", pack_name, strerror(errno));
1858 alloc_objects(est_obj_cnt);
1859 strbuf_init(&command_buf);
1861 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1862 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1863 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1864 marks = pool_calloc(1, sizeof(struct mark_set));
1867 read_next_command();
1868 if (command_buf.eof)
1870 else if (!strcmp("blob", command_buf.buf))
1872 else if (!strncmp("commit ", command_buf.buf, 7))
1874 else if (!strncmp("tag ", command_buf.buf, 4))
1876 else if (!strncmp("reset ", command_buf.buf, 6))
1879 die("Unsupported command: %s", command_buf.buf);
1882 fixup_header_footer();
1884 write_index(idx_name);
1891 fprintf(stderr, "%s statistics:\n", argv[0]);
1892 fprintf(stderr, "---------------------------------------------------------------------\n");
1893 fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1894 fprintf(stderr, "Total objects: %10lu (%10lu duplicates )\n", object_count, duplicate_count);
1895 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]);
1896 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]);
1897 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]);
1898 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]);
1899 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1900 fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count);
1901 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1902 fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1903 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1904 fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1905 fprintf(stderr, "Pack remaps: %10lu\n", remap_count);
1906 stat(pack_name, &sb);
1907 fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1908 stat(idx_name, &sb);
1909 fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1910 fprintf(stderr, "---------------------------------------------------------------------\n");
1912 fprintf(stderr, "\n");