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 checkpoint ::= 'checkpoint' lf
46 # note: the first idnum in a stream should be 1 and subsequent
47 # idnums should not have gaps between values as this will cause
48 # the stream parser to reserve space for the gapped values. An
49 # idnum can be updated in the future to a new object by issuing
50 # a new mark directive with the old idnum.
52 mark ::= 'mark' sp idnum lf;
54 # note: declen indicates the length of binary_data in bytes.
55 # declen does not include the lf preceeding or trailing the
58 data ::= 'data' sp declen lf
62 # note: quoted strings are C-style quoting supporting \c for
63 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
64 # is the signed byte value in octal. Note that the only
65 # characters which must actually be escaped to protect the
66 # stream formatting is: \, " and LF. Otherwise these values
69 ref_str ::= ref | '"' quoted(ref) '"' ;
70 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
71 tag_str ::= tag | '"' quoted(tag) '"' ;
72 path_str ::= path | '"' quoted(path) '"' ;
74 declen ::= # unsigned 32 bit value, ascii base10 notation;
75 binary_data ::= # file content, not interpreted;
77 sp ::= # ASCII space character;
78 lf ::= # ASCII newline (LF) character;
80 # note: a colon (':') must precede the numerical value assigned to
81 # an idnum. This is to distinguish it from a ref or tag name as
82 # GIT does not permit ':' in ref or tag strings.
85 path ::= # GIT style file path, e.g. "a/b/c";
86 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
87 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
88 sha1exp ::= # Any valid GIT SHA1 expression;
89 hexsha1 ::= # SHA1 in hexadecimal format;
91 # note: name and email are UTF8 strings, however name must not
92 # contain '<' or lf and email must not contain any of the
93 # following: '<', '>', lf.
95 name ::= # valid GIT author/committer name;
96 email ::= # valid GIT author/committer email;
97 ts ::= # time since the epoch in seconds, ascii base10 notation;
98 tz ::= # GIT style timezone;
109 #include "csum-file.h"
115 struct object_entry *next;
116 unsigned long offset;
117 unsigned type : TYPE_BITS;
118 unsigned pack_id : 16;
119 unsigned char sha1[20];
122 struct object_entry_pool
124 struct object_entry_pool *next_pool;
125 struct object_entry *next_free;
126 struct object_entry *end;
127 struct object_entry entries[FLEX_ARRAY]; /* more */
134 struct object_entry *marked[1024];
135 struct mark_set *sets[1024];
143 unsigned long offset;
150 struct mem_pool *next_pool;
153 char space[FLEX_ARRAY]; /* more */
158 struct atom_str *next_atom;
160 char str_dat[FLEX_ARRAY]; /* more */
166 struct tree_content *tree;
167 struct atom_str* name;
171 unsigned char sha1[20];
177 unsigned int entry_capacity; /* must match avail_tree_content */
178 unsigned int entry_count;
179 unsigned int delta_depth;
180 struct tree_entry *entries[FLEX_ARRAY]; /* more */
183 struct avail_tree_content
185 unsigned int entry_capacity; /* must match tree_content */
186 struct avail_tree_content *next_avail;
191 struct branch *table_next_branch;
192 struct branch *active_next_branch;
194 unsigned long last_commit;
195 struct tree_entry branch_tree;
196 unsigned char sha1[20];
201 struct tag *next_tag;
203 unsigned char sha1[20];
214 struct hash_list *next;
215 unsigned char sha1[20];
218 /* Stats and misc. counters */
219 static unsigned long max_depth = 10;
220 static unsigned long max_objects = -1;
221 static unsigned long max_packsize = -1;
222 static unsigned long alloc_count;
223 static unsigned long branch_count;
224 static unsigned long branch_load_count;
225 static unsigned long object_count;
226 static unsigned long marks_set_count;
227 static unsigned long object_count_by_type[1 << TYPE_BITS];
228 static unsigned long duplicate_count_by_type[1 << TYPE_BITS];
229 static unsigned long delta_count_by_type[1 << TYPE_BITS];
232 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
233 static size_t total_allocd;
234 static struct mem_pool *mem_pool;
236 /* Atom management */
237 static unsigned int atom_table_sz = 4451;
238 static unsigned int atom_cnt;
239 static struct atom_str **atom_table;
241 /* The .pack file being generated */
242 static const char *base_name;
243 static unsigned int pack_id;
244 static char *idx_name;
245 static struct packed_git *pack_data;
246 static struct packed_git **all_packs;
248 static unsigned long pack_size;
249 static unsigned char pack_sha1[20];
251 /* Table of objects we've written. */
252 static unsigned int object_entry_alloc = 5000;
253 static struct object_entry_pool *blocks;
254 static struct object_entry *object_table[1 << 16];
255 static struct mark_set *marks;
256 static const char* mark_file;
259 static struct last_object last_blob;
261 /* Tree management */
262 static unsigned int tree_entry_alloc = 1000;
263 static void *avail_tree_entry;
264 static unsigned int avail_tree_table_sz = 100;
265 static struct avail_tree_content **avail_tree_table;
266 static struct dbuf old_tree;
267 static struct dbuf new_tree;
270 static unsigned long max_active_branches = 5;
271 static unsigned long cur_active_branches;
272 static unsigned long branch_table_sz = 1039;
273 static struct branch **branch_table;
274 static struct branch *active_branches;
277 static struct tag *first_tag;
278 static struct tag *last_tag;
280 /* Input stream parsing */
281 static struct strbuf command_buf;
282 static unsigned long next_mark;
283 static struct dbuf new_data;
284 static FILE* branch_log;
287 static void alloc_objects(unsigned int cnt)
289 struct object_entry_pool *b;
291 b = xmalloc(sizeof(struct object_entry_pool)
292 + cnt * sizeof(struct object_entry));
293 b->next_pool = blocks;
294 b->next_free = b->entries;
295 b->end = b->entries + cnt;
300 static struct object_entry* new_object(unsigned char *sha1)
302 struct object_entry *e;
304 if (blocks->next_free == blocks->end)
305 alloc_objects(object_entry_alloc);
307 e = blocks->next_free++;
308 hashcpy(e->sha1, sha1);
312 static struct object_entry* find_object(unsigned char *sha1)
314 unsigned int h = sha1[0] << 8 | sha1[1];
315 struct object_entry *e;
316 for (e = object_table[h]; e; e = e->next)
317 if (!hashcmp(sha1, e->sha1))
322 static struct object_entry* insert_object(unsigned char *sha1)
324 unsigned int h = sha1[0] << 8 | sha1[1];
325 struct object_entry *e = object_table[h];
326 struct object_entry *p = NULL;
329 if (!hashcmp(sha1, e->sha1))
335 e = new_object(sha1);
345 static unsigned int hc_str(const char *s, size_t len)
353 static void* pool_alloc(size_t len)
358 for (p = mem_pool; p; p = p->next_pool)
359 if ((p->end - p->next_free >= len))
363 if (len >= (mem_pool_alloc/2)) {
367 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
368 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
369 p->next_pool = mem_pool;
370 p->next_free = p->space;
371 p->end = p->next_free + mem_pool_alloc;
376 /* round out to a pointer alignment */
377 if (len & (sizeof(void*) - 1))
378 len += sizeof(void*) - (len & (sizeof(void*) - 1));
383 static void* pool_calloc(size_t count, size_t size)
385 size_t len = count * size;
386 void *r = pool_alloc(len);
391 static char* pool_strdup(const char *s)
393 char *r = pool_alloc(strlen(s) + 1);
398 static void size_dbuf(struct dbuf *b, size_t maxlen)
401 if (b->capacity >= maxlen)
405 b->capacity = ((maxlen / 1024) + 1) * 1024;
406 b->buffer = xmalloc(b->capacity);
409 static void insert_mark(unsigned long idnum, struct object_entry *oe)
411 struct mark_set *s = marks;
412 while ((idnum >> s->shift) >= 1024) {
413 s = pool_calloc(1, sizeof(struct mark_set));
414 s->shift = marks->shift + 10;
415 s->data.sets[0] = marks;
419 unsigned long i = idnum >> s->shift;
420 idnum -= i << s->shift;
421 if (!s->data.sets[i]) {
422 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
423 s->data.sets[i]->shift = s->shift - 10;
427 if (!s->data.marked[idnum])
429 s->data.marked[idnum] = oe;
432 static struct object_entry* find_mark(unsigned long idnum)
434 unsigned long orig_idnum = idnum;
435 struct mark_set *s = marks;
436 struct object_entry *oe = NULL;
437 if ((idnum >> s->shift) < 1024) {
438 while (s && s->shift) {
439 unsigned long i = idnum >> s->shift;
440 idnum -= i << s->shift;
444 oe = s->data.marked[idnum];
447 die("mark :%lu not declared", orig_idnum);
451 static struct atom_str* to_atom(const char *s, size_t len)
453 unsigned int hc = hc_str(s, len) % atom_table_sz;
456 for (c = atom_table[hc]; c; c = c->next_atom)
457 if (c->str_len == len && !strncmp(s, c->str_dat, len))
460 c = pool_alloc(sizeof(struct atom_str) + len + 1);
462 strncpy(c->str_dat, s, len);
464 c->next_atom = atom_table[hc];
470 static struct branch* lookup_branch(const char *name)
472 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
475 for (b = branch_table[hc]; b; b = b->table_next_branch)
476 if (!strcmp(name, b->name))
481 static struct branch* new_branch(const char *name)
483 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
484 struct branch* b = lookup_branch(name);
487 die("Invalid attempt to create duplicate branch: %s", name);
488 if (check_ref_format(name))
489 die("Branch name doesn't conform to GIT standards: %s", name);
491 b = pool_calloc(1, sizeof(struct branch));
492 b->name = pool_strdup(name);
493 b->table_next_branch = branch_table[hc];
494 b->branch_tree.versions[0].mode = S_IFDIR;
495 b->branch_tree.versions[1].mode = S_IFDIR;
496 branch_table[hc] = b;
501 static unsigned int hc_entries(unsigned int cnt)
503 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
504 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
507 static struct tree_content* new_tree_content(unsigned int cnt)
509 struct avail_tree_content *f, *l = NULL;
510 struct tree_content *t;
511 unsigned int hc = hc_entries(cnt);
513 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
514 if (f->entry_capacity >= cnt)
519 l->next_avail = f->next_avail;
521 avail_tree_table[hc] = f->next_avail;
523 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
524 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
525 f->entry_capacity = cnt;
528 t = (struct tree_content*)f;
534 static void release_tree_entry(struct tree_entry *e);
535 static void release_tree_content(struct tree_content *t)
537 struct avail_tree_content *f = (struct avail_tree_content*)t;
538 unsigned int hc = hc_entries(f->entry_capacity);
539 f->next_avail = avail_tree_table[hc];
540 avail_tree_table[hc] = f;
543 static void release_tree_content_recursive(struct tree_content *t)
546 for (i = 0; i < t->entry_count; i++)
547 release_tree_entry(t->entries[i]);
548 release_tree_content(t);
551 static struct tree_content* grow_tree_content(
552 struct tree_content *t,
555 struct tree_content *r = new_tree_content(t->entry_count + amt);
556 r->entry_count = t->entry_count;
557 r->delta_depth = t->delta_depth;
558 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
559 release_tree_content(t);
563 static struct tree_entry* new_tree_entry()
565 struct tree_entry *e;
567 if (!avail_tree_entry) {
568 unsigned int n = tree_entry_alloc;
569 total_allocd += n * sizeof(struct tree_entry);
570 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
572 *((void**)e) = e + 1;
578 e = avail_tree_entry;
579 avail_tree_entry = *((void**)e);
583 static void release_tree_entry(struct tree_entry *e)
586 release_tree_content_recursive(e->tree);
587 *((void**)e) = avail_tree_entry;
588 avail_tree_entry = e;
591 static void yread(int fd, void *buffer, size_t length)
594 while (ret < length) {
595 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
597 die("Read from descriptor %i: end of stream", fd);
599 die("Read from descriptor %i: %s", fd, strerror(errno));
604 static void start_packfile()
606 struct packed_git *p;
607 struct pack_header hdr;
609 idx_name = xmalloc(strlen(base_name) + 11);
610 p = xcalloc(1, sizeof(*p) + strlen(base_name) + 13);
611 sprintf(p->pack_name, "%s%5.5i.pack", base_name, pack_id + 1);
612 sprintf(idx_name, "%s%5.5i.idx", base_name, pack_id + 1);
614 pack_fd = open(p->pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
616 die("Can't create %s: %s", p->pack_name, strerror(errno));
617 p->pack_fd = pack_fd;
619 hdr.hdr_signature = htonl(PACK_SIGNATURE);
620 hdr.hdr_version = htonl(2);
622 write_or_die(pack_fd, &hdr, sizeof(hdr));
625 pack_size = sizeof(hdr);
628 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
629 all_packs[pack_id] = p;
632 static void fixup_header_footer()
639 if (lseek(pack_fd, 0, SEEK_SET) != 0)
640 die("Failed seeking to start: %s", strerror(errno));
643 yread(pack_fd, hdr, 8);
644 SHA1_Update(&c, hdr, 8);
646 cnt = htonl(object_count);
647 SHA1_Update(&c, &cnt, 4);
648 write_or_die(pack_fd, &cnt, 4);
650 buf = xmalloc(128 * 1024);
652 size_t n = xread(pack_fd, buf, 128 * 1024);
655 SHA1_Update(&c, buf, n);
659 SHA1_Final(pack_sha1, &c);
660 write_or_die(pack_fd, pack_sha1, sizeof(pack_sha1));
663 static int oecmp (const void *a_, const void *b_)
665 struct object_entry *a = *((struct object_entry**)a_);
666 struct object_entry *b = *((struct object_entry**)b_);
667 return hashcmp(a->sha1, b->sha1);
670 static void write_index(const char *idx_name)
673 struct object_entry **idx, **c, **last, *e;
674 struct object_entry_pool *o;
675 unsigned int array[256];
678 /* Build the sorted table of object IDs. */
679 idx = xmalloc(object_count * sizeof(struct object_entry*));
681 for (o = blocks; o; o = o->next_pool)
682 for (e = o->next_free; e-- != o->entries;)
683 if (pack_id == e->pack_id)
685 last = idx + object_count;
687 die("internal consistency error creating the index");
688 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
690 /* Generate the fan-out array. */
692 for (i = 0; i < 256; i++) {
693 struct object_entry **next = c;;
694 while (next < last) {
695 if ((*next)->sha1[0] != i)
699 array[i] = htonl(next - idx);
703 f = sha1create("%s", idx_name);
704 sha1write(f, array, 256 * sizeof(int));
705 for (c = idx; c != last; c++) {
706 unsigned int offset = htonl((*c)->offset);
707 sha1write(f, &offset, 4);
708 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
710 sha1write(f, pack_sha1, sizeof(pack_sha1));
711 sha1close(f, NULL, 1);
715 static void end_packfile()
717 struct packed_git *old_p = pack_data, *new_p;
720 fixup_header_footer();
721 write_index(idx_name);
722 fprintf(stdout, "%s\n", old_p->pack_name);
725 /* Register the packfile with core git's machinary. */
726 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
728 die("core git rejected index %s", idx_name);
729 new_p->windows = old_p->windows;
730 new_p->pack_fd = old_p->pack_fd;
731 all_packs[pack_id++] = new_p;
732 install_packed_git(new_p);
736 unlink(old_p->pack_name);
741 /* We can't carry a delta across packfiles. */
742 free(last_blob.data);
743 last_blob.data = NULL;
745 last_blob.offset = 0;
749 static void checkpoint()
755 static size_t encode_header(
756 enum object_type type,
763 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
764 die("bad type %d", type);
766 c = (type << 4) | (size & 15);
778 static int store_object(
779 enum object_type type,
782 struct last_object *last,
783 unsigned char *sha1out,
787 struct object_entry *e;
788 unsigned char hdr[96];
789 unsigned char sha1[20];
790 unsigned long hdrlen, deltalen;
794 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
796 SHA1_Update(&c, hdr, hdrlen);
797 SHA1_Update(&c, dat, datlen);
798 SHA1_Final(sha1, &c);
800 hashcpy(sha1out, sha1);
802 e = insert_object(sha1);
804 insert_mark(mark, e);
806 duplicate_count_by_type[type]++;
810 if (last && last->data && last->depth < max_depth) {
811 delta = diff_delta(last->data, last->len,
814 if (delta && deltalen >= datlen) {
821 memset(&s, 0, sizeof(s));
822 deflateInit(&s, zlib_compression_level);
825 s.avail_in = deltalen;
830 s.avail_out = deflateBound(&s, s.avail_in);
831 s.next_out = out = xmalloc(s.avail_out);
832 while (deflate(&s, Z_FINISH) == Z_OK)
836 /* Determine if we should auto-checkpoint. */
837 if ((object_count + 1) > max_objects
838 || (object_count + 1) < object_count
839 || (pack_size + 60 + s.total_out) > max_packsize
840 || (pack_size + 60 + s.total_out) < pack_size) {
842 /* This new object needs to *not* have the current pack_id. */
843 e->pack_id = pack_id + 1;
846 /* We cannot carry a delta into the new pack. */
851 memset(&s, 0, sizeof(s));
852 deflateInit(&s, zlib_compression_level);
855 s.avail_out = deflateBound(&s, s.avail_in);
856 s.next_out = out = xrealloc(out, s.avail_out);
857 while (deflate(&s, Z_FINISH) == Z_OK)
864 e->pack_id = pack_id;
865 e->offset = pack_size;
867 object_count_by_type[type]++;
870 unsigned long ofs = e->offset - last->offset;
871 unsigned pos = sizeof(hdr) - 1;
873 delta_count_by_type[type]++;
876 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
877 write_or_die(pack_fd, hdr, hdrlen);
880 hdr[pos] = ofs & 127;
882 hdr[--pos] = 128 | (--ofs & 127);
883 write_or_die(pack_fd, hdr + pos, sizeof(hdr) - pos);
884 pack_size += sizeof(hdr) - pos;
888 hdrlen = encode_header(type, datlen, hdr);
889 write_or_die(pack_fd, hdr, hdrlen);
893 write_or_die(pack_fd, out, s.total_out);
894 pack_size += s.total_out;
900 if (last->data && !last->no_free)
903 last->offset = e->offset;
909 static void *gfi_unpack_entry(
910 struct object_entry *oe,
911 unsigned long *sizep)
913 static char type[20];
914 struct packed_git *p = all_packs[oe->pack_id];
916 p->pack_size = pack_size + 20;
917 return unpack_entry(p, oe->offset, type, sizep);
920 static const char *get_mode(const char *str, unsigned int *modep)
923 unsigned int mode = 0;
925 while ((c = *str++) != ' ') {
926 if (c < '0' || c > '7')
928 mode = (mode << 3) + (c - '0');
934 static void load_tree(struct tree_entry *root)
936 unsigned char* sha1 = root->versions[1].sha1;
937 struct object_entry *myoe;
938 struct tree_content *t;
943 root->tree = t = new_tree_content(8);
944 if (is_null_sha1(sha1))
947 myoe = find_object(sha1);
949 if (myoe->type != OBJ_TREE)
950 die("Not a tree: %s", sha1_to_hex(sha1));
952 buf = gfi_unpack_entry(myoe, &size);
955 buf = read_sha1_file(sha1, type, &size);
956 if (!buf || strcmp(type, tree_type))
957 die("Can't load tree %s", sha1_to_hex(sha1));
961 while (c != (buf + size)) {
962 struct tree_entry *e = new_tree_entry();
964 if (t->entry_count == t->entry_capacity)
965 root->tree = t = grow_tree_content(t, 8);
966 t->entries[t->entry_count++] = e;
969 c = get_mode(c, &e->versions[1].mode);
971 die("Corrupt mode in %s", sha1_to_hex(sha1));
972 e->versions[0].mode = e->versions[1].mode;
973 e->name = to_atom(c, strlen(c));
974 c += e->name->str_len + 1;
975 hashcpy(e->versions[0].sha1, (unsigned char*)c);
976 hashcpy(e->versions[1].sha1, (unsigned char*)c);
982 static int tecmp0 (const void *_a, const void *_b)
984 struct tree_entry *a = *((struct tree_entry**)_a);
985 struct tree_entry *b = *((struct tree_entry**)_b);
986 return base_name_compare(
987 a->name->str_dat, a->name->str_len, a->versions[0].mode,
988 b->name->str_dat, b->name->str_len, b->versions[0].mode);
991 static int tecmp1 (const void *_a, const void *_b)
993 struct tree_entry *a = *((struct tree_entry**)_a);
994 struct tree_entry *b = *((struct tree_entry**)_b);
995 return base_name_compare(
996 a->name->str_dat, a->name->str_len, a->versions[1].mode,
997 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1000 static void mktree(struct tree_content *t,
1010 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1012 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1014 for (i = 0; i < t->entry_count; i++) {
1015 if (t->entries[i]->versions[v].mode)
1016 maxlen += t->entries[i]->name->str_len + 34;
1019 size_dbuf(b, maxlen);
1021 for (i = 0; i < t->entry_count; i++) {
1022 struct tree_entry *e = t->entries[i];
1023 if (!e->versions[v].mode)
1025 c += sprintf(c, "%o", e->versions[v].mode);
1027 strcpy(c, e->name->str_dat);
1028 c += e->name->str_len + 1;
1029 hashcpy((unsigned char*)c, e->versions[v].sha1);
1032 *szp = c - (char*)b->buffer;
1035 static void store_tree(struct tree_entry *root)
1037 struct tree_content *t = root->tree;
1038 unsigned int i, j, del;
1039 unsigned long new_len;
1040 struct last_object lo;
1041 struct object_entry *le;
1043 if (!is_null_sha1(root->versions[1].sha1))
1046 for (i = 0; i < t->entry_count; i++) {
1047 if (t->entries[i]->tree)
1048 store_tree(t->entries[i]);
1051 le = find_object(root->versions[0].sha1);
1052 if (!S_ISDIR(root->versions[0].mode)
1054 || le->pack_id != pack_id) {
1058 mktree(t, 0, &lo.len, &old_tree);
1059 lo.data = old_tree.buffer;
1060 lo.offset = le->offset;
1061 lo.depth = t->delta_depth;
1065 mktree(t, 1, &new_len, &new_tree);
1066 store_object(OBJ_TREE, new_tree.buffer, new_len,
1067 &lo, root->versions[1].sha1, 0);
1069 t->delta_depth = lo.depth;
1070 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1071 struct tree_entry *e = t->entries[i];
1072 if (e->versions[1].mode) {
1073 e->versions[0].mode = e->versions[1].mode;
1074 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1075 t->entries[j++] = e;
1077 release_tree_entry(e);
1081 t->entry_count -= del;
1084 static int tree_content_set(
1085 struct tree_entry *root,
1087 const unsigned char *sha1,
1088 const unsigned int mode)
1090 struct tree_content *t = root->tree;
1093 struct tree_entry *e;
1095 slash1 = strchr(p, '/');
1101 for (i = 0; i < t->entry_count; i++) {
1103 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1105 if (e->versions[1].mode == mode
1106 && !hashcmp(e->versions[1].sha1, sha1))
1108 e->versions[1].mode = mode;
1109 hashcpy(e->versions[1].sha1, sha1);
1111 release_tree_content_recursive(e->tree);
1114 hashclr(root->versions[1].sha1);
1117 if (!S_ISDIR(e->versions[1].mode)) {
1118 e->tree = new_tree_content(8);
1119 e->versions[1].mode = S_IFDIR;
1123 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1124 hashclr(root->versions[1].sha1);
1131 if (t->entry_count == t->entry_capacity)
1132 root->tree = t = grow_tree_content(t, 8);
1133 e = new_tree_entry();
1134 e->name = to_atom(p, n);
1135 e->versions[0].mode = 0;
1136 hashclr(e->versions[0].sha1);
1137 t->entries[t->entry_count++] = e;
1139 e->tree = new_tree_content(8);
1140 e->versions[1].mode = S_IFDIR;
1141 tree_content_set(e, slash1 + 1, sha1, mode);
1144 e->versions[1].mode = mode;
1145 hashcpy(e->versions[1].sha1, sha1);
1147 hashclr(root->versions[1].sha1);
1151 static int tree_content_remove(struct tree_entry *root, const char *p)
1153 struct tree_content *t = root->tree;
1156 struct tree_entry *e;
1158 slash1 = strchr(p, '/');
1164 for (i = 0; i < t->entry_count; i++) {
1166 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1167 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1171 if (tree_content_remove(e, slash1 + 1)) {
1172 for (n = 0; n < e->tree->entry_count; n++) {
1173 if (e->tree->entries[n]->versions[1].mode) {
1174 hashclr(root->versions[1].sha1);
1187 release_tree_content_recursive(e->tree);
1190 e->versions[1].mode = 0;
1191 hashclr(e->versions[1].sha1);
1192 hashclr(root->versions[1].sha1);
1196 static void dump_branches()
1198 static const char *msg = "fast-import";
1201 struct ref_lock *lock;
1203 for (i = 0; i < branch_table_sz; i++) {
1204 for (b = branch_table[i]; b; b = b->table_next_branch) {
1205 lock = lock_any_ref_for_update(b->name, NULL);
1206 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1207 die("Can't write %s", b->name);
1212 static void dump_tags()
1214 static const char *msg = "fast-import";
1216 struct ref_lock *lock;
1217 char path[PATH_MAX];
1219 for (t = first_tag; t; t = t->next_tag) {
1220 sprintf(path, "refs/tags/%s", t->name);
1221 lock = lock_any_ref_for_update(path, NULL);
1222 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1223 die("Can't write %s", path);
1227 static void dump_marks_helper(FILE *f,
1233 for (k = 0; k < 1024; k++) {
1234 if (m->data.sets[k])
1235 dump_marks_helper(f, (base + k) << m->shift,
1239 for (k = 0; k < 1024; k++) {
1240 if (m->data.marked[k])
1241 fprintf(f, ":%lu %s\n", base + k,
1242 sha1_to_hex(m->data.marked[k]->sha1));
1247 static void dump_marks()
1251 FILE *f = fopen(mark_file, "w");
1252 dump_marks_helper(f, 0, marks);
1257 static void read_next_command()
1259 read_line(&command_buf, stdin, '\n');
1262 static void cmd_mark()
1264 if (!strncmp("mark :", command_buf.buf, 6)) {
1265 next_mark = strtoul(command_buf.buf + 6, NULL, 10);
1266 read_next_command();
1272 static void* cmd_data (size_t *size)
1278 if (strncmp("data ", command_buf.buf, 5))
1279 die("Expected 'data n' command, found: %s", command_buf.buf);
1281 length = strtoul(command_buf.buf + 5, NULL, 10);
1282 buffer = xmalloc(length);
1284 while (n < length) {
1285 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1286 if (!s && feof(stdin))
1287 die("EOF in data (%lu bytes remaining)", length - n);
1291 if (fgetc(stdin) != '\n')
1292 die("An lf did not trail the binary data as expected.");
1298 static void cmd_new_blob()
1303 read_next_command();
1307 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1311 static void unload_one_branch()
1313 while (cur_active_branches
1314 && cur_active_branches >= max_active_branches) {
1315 unsigned long min_commit = ULONG_MAX;
1316 struct branch *e, *l = NULL, *p = NULL;
1318 for (e = active_branches; e; e = e->active_next_branch) {
1319 if (e->last_commit < min_commit) {
1321 min_commit = e->last_commit;
1327 e = p->active_next_branch;
1328 p->active_next_branch = e->active_next_branch;
1330 e = active_branches;
1331 active_branches = e->active_next_branch;
1333 e->active_next_branch = NULL;
1334 if (e->branch_tree.tree) {
1335 release_tree_content_recursive(e->branch_tree.tree);
1336 e->branch_tree.tree = NULL;
1338 cur_active_branches--;
1342 static void load_branch(struct branch *b)
1344 load_tree(&b->branch_tree);
1345 b->active_next_branch = active_branches;
1346 active_branches = b;
1347 cur_active_branches++;
1348 branch_load_count++;
1351 static void file_change_m(struct branch *b)
1353 const char *p = command_buf.buf + 2;
1356 struct object_entry *oe;
1357 unsigned char sha1[20];
1361 p = get_mode(p, &mode);
1363 die("Corrupt mode: %s", command_buf.buf);
1365 case S_IFREG | 0644:
1366 case S_IFREG | 0755:
1373 die("Corrupt mode: %s", command_buf.buf);
1378 oe = find_mark(strtoul(p + 1, &x, 10));
1379 hashcpy(sha1, oe->sha1);
1382 if (get_sha1_hex(p, sha1))
1383 die("Invalid SHA1: %s", command_buf.buf);
1384 oe = find_object(sha1);
1388 die("Missing space after SHA1: %s", command_buf.buf);
1390 p_uq = unquote_c_style(p, &endp);
1393 die("Garbage after path in: %s", command_buf.buf);
1398 if (oe->type != OBJ_BLOB)
1399 die("Not a blob (actually a %s): %s",
1400 command_buf.buf, type_names[oe->type]);
1402 if (sha1_object_info(sha1, type, NULL))
1403 die("Blob not found: %s", command_buf.buf);
1404 if (strcmp(blob_type, type))
1405 die("Not a blob (actually a %s): %s",
1406 command_buf.buf, type);
1409 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1415 static void file_change_d(struct branch *b)
1417 const char *p = command_buf.buf + 2;
1421 p_uq = unquote_c_style(p, &endp);
1424 die("Garbage after path in: %s", command_buf.buf);
1427 tree_content_remove(&b->branch_tree, p);
1432 static void cmd_from(struct branch *b)
1434 const char *from, *endp;
1438 if (strncmp("from ", command_buf.buf, 5))
1442 die("Can't reinitailize branch %s", b->name);
1444 from = strchr(command_buf.buf, ' ') + 1;
1445 str_uq = unquote_c_style(from, &endp);
1448 die("Garbage after string in: %s", command_buf.buf);
1452 s = lookup_branch(from);
1454 die("Can't create a branch from itself: %s", b->name);
1456 unsigned char *t = s->branch_tree.versions[1].sha1;
1457 hashcpy(b->sha1, s->sha1);
1458 hashcpy(b->branch_tree.versions[0].sha1, t);
1459 hashcpy(b->branch_tree.versions[1].sha1, t);
1460 } else if (*from == ':') {
1461 unsigned long idnum = strtoul(from + 1, NULL, 10);
1462 struct object_entry *oe = find_mark(idnum);
1465 if (oe->type != OBJ_COMMIT)
1466 die("Mark :%lu not a commit", idnum);
1467 hashcpy(b->sha1, oe->sha1);
1468 buf = gfi_unpack_entry(oe, &size);
1469 if (!buf || size < 46)
1470 die("Not a valid commit: %s", from);
1471 if (memcmp("tree ", buf, 5)
1472 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1473 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1475 hashcpy(b->branch_tree.versions[0].sha1,
1476 b->branch_tree.versions[1].sha1);
1477 } else if (!get_sha1(from, b->sha1)) {
1478 if (is_null_sha1(b->sha1)) {
1479 hashclr(b->branch_tree.versions[0].sha1);
1480 hashclr(b->branch_tree.versions[1].sha1);
1485 buf = read_object_with_reference(b->sha1,
1486 type_names[OBJ_COMMIT], &size, b->sha1);
1487 if (!buf || size < 46)
1488 die("Not a valid commit: %s", from);
1489 if (memcmp("tree ", buf, 5)
1490 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1491 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1493 hashcpy(b->branch_tree.versions[0].sha1,
1494 b->branch_tree.versions[1].sha1);
1497 die("Invalid ref name or SHA1 expression: %s", from);
1499 read_next_command();
1502 static struct hash_list* cmd_merge(unsigned int *count)
1504 struct hash_list *list = NULL, *n, *e;
1505 const char *from, *endp;
1510 while (!strncmp("merge ", command_buf.buf, 6)) {
1511 from = strchr(command_buf.buf, ' ') + 1;
1512 str_uq = unquote_c_style(from, &endp);
1515 die("Garbage after string in: %s", command_buf.buf);
1519 n = xmalloc(sizeof(*n));
1520 s = lookup_branch(from);
1522 hashcpy(n->sha1, s->sha1);
1523 else if (*from == ':') {
1524 unsigned long idnum = strtoul(from + 1, NULL, 10);
1525 struct object_entry *oe = find_mark(idnum);
1526 if (oe->type != OBJ_COMMIT)
1527 die("Mark :%lu not a commit", idnum);
1528 hashcpy(n->sha1, oe->sha1);
1529 } else if (get_sha1(from, n->sha1))
1530 die("Invalid ref name or SHA1 expression: %s", from);
1539 read_next_command();
1544 static void cmd_new_commit()
1552 char *author = NULL;
1553 char *committer = NULL;
1554 struct hash_list *merge_list = NULL;
1555 unsigned int merge_count;
1557 /* Obtain the branch name from the rest of our command */
1558 sp = strchr(command_buf.buf, ' ') + 1;
1559 str_uq = unquote_c_style(sp, &endp);
1562 die("Garbage after ref in: %s", command_buf.buf);
1565 b = lookup_branch(sp);
1571 read_next_command();
1573 if (!strncmp("author ", command_buf.buf, 7)) {
1574 author = strdup(command_buf.buf);
1575 read_next_command();
1577 if (!strncmp("committer ", command_buf.buf, 10)) {
1578 committer = strdup(command_buf.buf);
1579 read_next_command();
1582 die("Expected committer but didn't get one");
1583 msg = cmd_data(&msglen);
1584 read_next_command();
1586 merge_list = cmd_merge(&merge_count);
1588 /* ensure the branch is active/loaded */
1589 if (!b->branch_tree.tree || !max_active_branches) {
1590 unload_one_branch();
1596 if (1 == command_buf.len)
1598 else if (!strncmp("M ", command_buf.buf, 2))
1600 else if (!strncmp("D ", command_buf.buf, 2))
1603 die("Unsupported file_change: %s", command_buf.buf);
1604 read_next_command();
1607 /* build the tree and the commit */
1608 store_tree(&b->branch_tree);
1609 hashcpy(b->branch_tree.versions[0].sha1,
1610 b->branch_tree.versions[1].sha1);
1611 size_dbuf(&new_data, 97 + msglen
1614 ? strlen(author) + strlen(committer)
1615 : 2 * strlen(committer)));
1616 sp = new_data.buffer;
1617 sp += sprintf(sp, "tree %s\n",
1618 sha1_to_hex(b->branch_tree.versions[1].sha1));
1619 if (!is_null_sha1(b->sha1))
1620 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1621 while (merge_list) {
1622 struct hash_list *next = merge_list->next;
1623 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1628 sp += sprintf(sp, "%s\n", author);
1630 sp += sprintf(sp, "author %s\n", committer + 10);
1631 sp += sprintf(sp, "%s\n\n", committer);
1632 memcpy(sp, msg, msglen);
1639 store_object(OBJ_COMMIT,
1640 new_data.buffer, sp - (char*)new_data.buffer,
1641 NULL, b->sha1, next_mark);
1642 b->last_commit = object_count_by_type[OBJ_COMMIT];
1645 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1646 fprintf(branch_log, "commit ");
1648 fputc('"', branch_log);
1649 quote_c_style(b->name, NULL, branch_log, 0);
1650 fputc('"', branch_log);
1652 fprintf(branch_log, "%s", b->name);
1653 fprintf(branch_log," :%lu %s\n",next_mark,sha1_to_hex(b->sha1));
1657 static void cmd_new_tag()
1668 unsigned long from_mark = 0;
1669 unsigned char sha1[20];
1671 /* Obtain the new tag name from the rest of our command */
1672 sp = strchr(command_buf.buf, ' ') + 1;
1673 str_uq = unquote_c_style(sp, &endp);
1676 die("Garbage after tag name in: %s", command_buf.buf);
1679 t = pool_alloc(sizeof(struct tag));
1681 t->name = pool_strdup(sp);
1683 last_tag->next_tag = t;
1689 read_next_command();
1692 if (strncmp("from ", command_buf.buf, 5))
1693 die("Expected from command, got %s", command_buf.buf);
1695 from = strchr(command_buf.buf, ' ') + 1;
1696 str_uq = unquote_c_style(from, &endp);
1699 die("Garbage after string in: %s", command_buf.buf);
1703 s = lookup_branch(from);
1705 hashcpy(sha1, s->sha1);
1706 } else if (*from == ':') {
1707 from_mark = strtoul(from + 1, NULL, 10);
1708 struct object_entry *oe = find_mark(from_mark);
1709 if (oe->type != OBJ_COMMIT)
1710 die("Mark :%lu not a commit", from_mark);
1711 hashcpy(sha1, oe->sha1);
1712 } else if (!get_sha1(from, sha1)) {
1716 buf = read_object_with_reference(sha1,
1717 type_names[OBJ_COMMIT], &size, sha1);
1718 if (!buf || size < 46)
1719 die("Not a valid commit: %s", from);
1722 die("Invalid ref name or SHA1 expression: %s", from);
1726 read_next_command();
1729 if (strncmp("tagger ", command_buf.buf, 7))
1730 die("Expected tagger command, got %s", command_buf.buf);
1731 tagger = strdup(command_buf.buf);
1733 /* tag payload/message */
1734 read_next_command();
1735 msg = cmd_data(&msglen);
1737 /* build the tag object */
1738 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1739 sp = new_data.buffer;
1740 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1741 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1742 sp += sprintf(sp, "tag %s\n", t->name);
1743 sp += sprintf(sp, "%s\n\n", tagger);
1744 memcpy(sp, msg, msglen);
1749 store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1753 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1754 fprintf(branch_log, "tag ");
1756 fputc('"', branch_log);
1757 quote_c_style(t->name, NULL, branch_log, 0);
1758 fputc('"', branch_log);
1760 fprintf(branch_log, "%s", t->name);
1761 fprintf(branch_log," :%lu %s\n",from_mark,sha1_to_hex(t->sha1));
1765 static void cmd_reset_branch()
1772 /* Obtain the branch name from the rest of our command */
1773 sp = strchr(command_buf.buf, ' ') + 1;
1774 str_uq = unquote_c_style(sp, &endp);
1777 die("Garbage after ref in: %s", command_buf.buf);
1780 b = lookup_branch(sp);
1783 if (b->branch_tree.tree) {
1784 release_tree_content_recursive(b->branch_tree.tree);
1785 b->branch_tree.tree = NULL;
1792 read_next_command();
1796 static void cmd_checkpoint()
1800 read_next_command();
1803 static const char fast_import_usage[] =
1804 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1806 int main(int argc, const char **argv)
1809 unsigned long est_obj_cnt = object_entry_alloc;
1810 unsigned long duplicate_count;
1813 git_config(git_default_config);
1815 for (i = 1; i < argc; i++) {
1816 const char *a = argv[i];
1818 if (*a != '-' || !strcmp(a, "--"))
1820 else if (!strncmp(a, "--objects=", 10))
1821 est_obj_cnt = strtoul(a + 10, NULL, 0);
1822 else if (!strncmp(a, "--max-objects-per-pack=", 23))
1823 max_objects = strtoul(a + 23, NULL, 0);
1824 else if (!strncmp(a, "--max-pack-size=", 16))
1825 max_packsize = strtoul(a + 16, NULL, 0) * 1024 * 1024;
1826 else if (!strncmp(a, "--depth=", 8))
1827 max_depth = strtoul(a + 8, NULL, 0);
1828 else if (!strncmp(a, "--active-branches=", 18))
1829 max_active_branches = strtoul(a + 18, NULL, 0);
1830 else if (!strncmp(a, "--export-marks=", 15))
1832 else if (!strncmp(a, "--branch-log=", 13)) {
1833 branch_log = fopen(a + 13, "w");
1835 die("Can't create %s: %s", a + 13, strerror(errno));
1838 die("unknown option %s", a);
1841 usage(fast_import_usage);
1842 base_name = argv[i];
1844 alloc_objects(est_obj_cnt);
1845 strbuf_init(&command_buf);
1847 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1848 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1849 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1850 marks = pool_calloc(1, sizeof(struct mark_set));
1854 read_next_command();
1855 if (command_buf.eof)
1857 else if (!strcmp("blob", command_buf.buf))
1859 else if (!strncmp("commit ", command_buf.buf, 7))
1861 else if (!strncmp("tag ", command_buf.buf, 4))
1863 else if (!strncmp("reset ", command_buf.buf, 6))
1865 else if (!strcmp("checkpoint", command_buf.buf))
1868 die("Unsupported command: %s", command_buf.buf);
1878 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
1879 duplicate_count += duplicate_count_by_type[i];
1881 fprintf(stderr, "%s statistics:\n", argv[0]);
1882 fprintf(stderr, "---------------------------------------------------------------------\n");
1883 fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1884 fprintf(stderr, "Total objects: %10lu (%10lu duplicates )\n", object_count, duplicate_count);
1885 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]);
1886 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]);
1887 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]);
1888 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]);
1889 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1890 fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count);
1891 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1892 fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1893 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1894 fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1895 fprintf(stderr, "---------------------------------------------------------------------\n");
1897 fprintf(stderr, "---------------------------------------------------------------------\n");
1898 fprintf(stderr, "\n");