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 /* Configured limits on output */
219 static unsigned long max_depth = 10;
220 static unsigned long max_packsize = -1;
221 static uintmax_t max_objects = -1;
223 /* Stats and misc. counters */
224 static uintmax_t alloc_count;
225 static uintmax_t object_count;
226 static uintmax_t marks_set_count;
227 static uintmax_t object_count_by_type[1 << TYPE_BITS];
228 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
229 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
230 static unsigned long branch_count;
231 static unsigned long branch_load_count;
234 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
235 static size_t total_allocd;
236 static struct mem_pool *mem_pool;
238 /* Atom management */
239 static unsigned int atom_table_sz = 4451;
240 static unsigned int atom_cnt;
241 static struct atom_str **atom_table;
243 /* The .pack file being generated */
244 static const char *base_name;
245 static unsigned int pack_id;
246 static char *idx_name;
247 static struct packed_git *pack_data;
248 static struct packed_git **all_packs;
250 static unsigned long pack_size;
252 /* Table of objects we've written. */
253 static unsigned int object_entry_alloc = 5000;
254 static struct object_entry_pool *blocks;
255 static struct object_entry *object_table[1 << 16];
256 static struct mark_set *marks;
257 static const char* mark_file;
260 static struct last_object last_blob;
262 /* Tree management */
263 static unsigned int tree_entry_alloc = 1000;
264 static void *avail_tree_entry;
265 static unsigned int avail_tree_table_sz = 100;
266 static struct avail_tree_content **avail_tree_table;
267 static struct dbuf old_tree;
268 static struct dbuf new_tree;
271 static unsigned long max_active_branches = 5;
272 static unsigned long cur_active_branches;
273 static unsigned long branch_table_sz = 1039;
274 static struct branch **branch_table;
275 static struct branch *active_branches;
278 static struct tag *first_tag;
279 static struct tag *last_tag;
281 /* Input stream parsing */
282 static struct strbuf command_buf;
283 static uintmax_t next_mark;
284 static struct dbuf new_data;
285 static FILE* branch_log;
288 static void alloc_objects(unsigned int cnt)
290 struct object_entry_pool *b;
292 b = xmalloc(sizeof(struct object_entry_pool)
293 + cnt * sizeof(struct object_entry));
294 b->next_pool = blocks;
295 b->next_free = b->entries;
296 b->end = b->entries + cnt;
301 static struct object_entry* new_object(unsigned char *sha1)
303 struct object_entry *e;
305 if (blocks->next_free == blocks->end)
306 alloc_objects(object_entry_alloc);
308 e = blocks->next_free++;
309 hashcpy(e->sha1, sha1);
313 static struct object_entry* find_object(unsigned char *sha1)
315 unsigned int h = sha1[0] << 8 | sha1[1];
316 struct object_entry *e;
317 for (e = object_table[h]; e; e = e->next)
318 if (!hashcmp(sha1, e->sha1))
323 static struct object_entry* insert_object(unsigned char *sha1)
325 unsigned int h = sha1[0] << 8 | sha1[1];
326 struct object_entry *e = object_table[h];
327 struct object_entry *p = NULL;
330 if (!hashcmp(sha1, e->sha1))
336 e = new_object(sha1);
346 static unsigned int hc_str(const char *s, size_t len)
354 static void* pool_alloc(size_t len)
359 for (p = mem_pool; p; p = p->next_pool)
360 if ((p->end - p->next_free >= len))
364 if (len >= (mem_pool_alloc/2)) {
368 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
369 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
370 p->next_pool = mem_pool;
371 p->next_free = p->space;
372 p->end = p->next_free + mem_pool_alloc;
377 /* round out to a pointer alignment */
378 if (len & (sizeof(void*) - 1))
379 len += sizeof(void*) - (len & (sizeof(void*) - 1));
384 static void* pool_calloc(size_t count, size_t size)
386 size_t len = count * size;
387 void *r = pool_alloc(len);
392 static char* pool_strdup(const char *s)
394 char *r = pool_alloc(strlen(s) + 1);
399 static void size_dbuf(struct dbuf *b, size_t maxlen)
402 if (b->capacity >= maxlen)
406 b->capacity = ((maxlen / 1024) + 1) * 1024;
407 b->buffer = xmalloc(b->capacity);
410 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
412 struct mark_set *s = marks;
413 while ((idnum >> s->shift) >= 1024) {
414 s = pool_calloc(1, sizeof(struct mark_set));
415 s->shift = marks->shift + 10;
416 s->data.sets[0] = marks;
420 uintmax_t i = idnum >> s->shift;
421 idnum -= i << s->shift;
422 if (!s->data.sets[i]) {
423 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
424 s->data.sets[i]->shift = s->shift - 10;
428 if (!s->data.marked[idnum])
430 s->data.marked[idnum] = oe;
433 static struct object_entry* find_mark(uintmax_t idnum)
435 uintmax_t orig_idnum = idnum;
436 struct mark_set *s = marks;
437 struct object_entry *oe = NULL;
438 if ((idnum >> s->shift) < 1024) {
439 while (s && s->shift) {
440 uintmax_t i = idnum >> s->shift;
441 idnum -= i << s->shift;
445 oe = s->data.marked[idnum];
448 die("mark :%ju not declared", orig_idnum);
452 static struct atom_str* to_atom(const char *s, size_t len)
454 unsigned int hc = hc_str(s, len) % atom_table_sz;
457 for (c = atom_table[hc]; c; c = c->next_atom)
458 if (c->str_len == len && !strncmp(s, c->str_dat, len))
461 c = pool_alloc(sizeof(struct atom_str) + len + 1);
463 strncpy(c->str_dat, s, len);
465 c->next_atom = atom_table[hc];
471 static struct branch* lookup_branch(const char *name)
473 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
476 for (b = branch_table[hc]; b; b = b->table_next_branch)
477 if (!strcmp(name, b->name))
482 static struct branch* new_branch(const char *name)
484 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
485 struct branch* b = lookup_branch(name);
488 die("Invalid attempt to create duplicate branch: %s", name);
489 if (check_ref_format(name))
490 die("Branch name doesn't conform to GIT standards: %s", name);
492 b = pool_calloc(1, sizeof(struct branch));
493 b->name = pool_strdup(name);
494 b->table_next_branch = branch_table[hc];
495 b->branch_tree.versions[0].mode = S_IFDIR;
496 b->branch_tree.versions[1].mode = S_IFDIR;
497 branch_table[hc] = b;
502 static unsigned int hc_entries(unsigned int cnt)
504 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
505 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
508 static struct tree_content* new_tree_content(unsigned int cnt)
510 struct avail_tree_content *f, *l = NULL;
511 struct tree_content *t;
512 unsigned int hc = hc_entries(cnt);
514 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
515 if (f->entry_capacity >= cnt)
520 l->next_avail = f->next_avail;
522 avail_tree_table[hc] = f->next_avail;
524 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
525 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
526 f->entry_capacity = cnt;
529 t = (struct tree_content*)f;
535 static void release_tree_entry(struct tree_entry *e);
536 static void release_tree_content(struct tree_content *t)
538 struct avail_tree_content *f = (struct avail_tree_content*)t;
539 unsigned int hc = hc_entries(f->entry_capacity);
540 f->next_avail = avail_tree_table[hc];
541 avail_tree_table[hc] = f;
544 static void release_tree_content_recursive(struct tree_content *t)
547 for (i = 0; i < t->entry_count; i++)
548 release_tree_entry(t->entries[i]);
549 release_tree_content(t);
552 static struct tree_content* grow_tree_content(
553 struct tree_content *t,
556 struct tree_content *r = new_tree_content(t->entry_count + amt);
557 r->entry_count = t->entry_count;
558 r->delta_depth = t->delta_depth;
559 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
560 release_tree_content(t);
564 static struct tree_entry* new_tree_entry()
566 struct tree_entry *e;
568 if (!avail_tree_entry) {
569 unsigned int n = tree_entry_alloc;
570 total_allocd += n * sizeof(struct tree_entry);
571 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
573 *((void**)e) = e + 1;
579 e = avail_tree_entry;
580 avail_tree_entry = *((void**)e);
584 static void release_tree_entry(struct tree_entry *e)
587 release_tree_content_recursive(e->tree);
588 *((void**)e) = avail_tree_entry;
589 avail_tree_entry = e;
592 static void start_packfile()
594 struct packed_git *p;
595 struct pack_header hdr;
597 idx_name = xmalloc(strlen(base_name) + 11);
598 p = xcalloc(1, sizeof(*p) + strlen(base_name) + 13);
599 sprintf(p->pack_name, "%s%5.5i.pack", base_name, pack_id + 1);
600 sprintf(idx_name, "%s%5.5i.idx", base_name, pack_id + 1);
602 pack_fd = open(p->pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
604 die("Can't create %s: %s", p->pack_name, strerror(errno));
605 p->pack_fd = pack_fd;
607 hdr.hdr_signature = htonl(PACK_SIGNATURE);
608 hdr.hdr_version = htonl(2);
610 write_or_die(pack_fd, &hdr, sizeof(hdr));
613 pack_size = sizeof(hdr);
616 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
617 all_packs[pack_id] = p;
620 static void fixup_header_footer()
627 if (lseek(pack_fd, 0, SEEK_SET) != 0)
628 die("Failed seeking to start: %s", strerror(errno));
631 if (read_in_full(pack_fd, hdr, 8) != 8)
632 die("Unable to reread header of %s", pack_data->pack_name);
633 SHA1_Update(&c, hdr, 8);
635 cnt = htonl(object_count);
636 SHA1_Update(&c, &cnt, 4);
637 write_or_die(pack_fd, &cnt, 4);
639 buf = xmalloc(128 * 1024);
641 size_t n = xread(pack_fd, buf, 128 * 1024);
644 SHA1_Update(&c, buf, n);
648 SHA1_Final(pack_data->sha1, &c);
649 write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
652 static int oecmp (const void *a_, const void *b_)
654 struct object_entry *a = *((struct object_entry**)a_);
655 struct object_entry *b = *((struct object_entry**)b_);
656 return hashcmp(a->sha1, b->sha1);
659 static void write_index(const char *idx_name)
662 struct object_entry **idx, **c, **last, *e;
663 struct object_entry_pool *o;
664 unsigned int array[256];
667 /* Build the sorted table of object IDs. */
668 idx = xmalloc(object_count * sizeof(struct object_entry*));
670 for (o = blocks; o; o = o->next_pool)
671 for (e = o->next_free; e-- != o->entries;)
672 if (pack_id == e->pack_id)
674 last = idx + object_count;
676 die("internal consistency error creating the index");
677 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
679 /* Generate the fan-out array. */
681 for (i = 0; i < 256; i++) {
682 struct object_entry **next = c;;
683 while (next < last) {
684 if ((*next)->sha1[0] != i)
688 array[i] = htonl(next - idx);
692 f = sha1create("%s", idx_name);
693 sha1write(f, array, 256 * sizeof(int));
694 for (c = idx; c != last; c++) {
695 unsigned int offset = htonl((*c)->offset);
696 sha1write(f, &offset, 4);
697 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
699 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
700 sha1close(f, pack_data->sha1, 1);
704 static void end_packfile()
706 struct packed_git *old_p = pack_data, *new_p;
709 fixup_header_footer();
710 write_index(idx_name);
711 fprintf(stdout, "%s\n", old_p->pack_name);
714 /* Register the packfile with core git's machinary. */
715 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
717 die("core git rejected index %s", idx_name);
718 new_p->windows = old_p->windows;
719 new_p->pack_fd = old_p->pack_fd;
720 all_packs[pack_id++] = new_p;
721 install_packed_git(new_p);
725 unlink(old_p->pack_name);
730 /* We can't carry a delta across packfiles. */
731 free(last_blob.data);
732 last_blob.data = NULL;
734 last_blob.offset = 0;
738 static void checkpoint()
744 static size_t encode_header(
745 enum object_type type,
752 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
753 die("bad type %d", type);
755 c = (type << 4) | (size & 15);
767 static int store_object(
768 enum object_type type,
771 struct last_object *last,
772 unsigned char *sha1out,
776 struct object_entry *e;
777 unsigned char hdr[96];
778 unsigned char sha1[20];
779 unsigned long hdrlen, deltalen;
783 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
785 SHA1_Update(&c, hdr, hdrlen);
786 SHA1_Update(&c, dat, datlen);
787 SHA1_Final(sha1, &c);
789 hashcpy(sha1out, sha1);
791 e = insert_object(sha1);
793 insert_mark(mark, e);
795 duplicate_count_by_type[type]++;
799 if (last && last->data && last->depth < max_depth) {
800 delta = diff_delta(last->data, last->len,
803 if (delta && deltalen >= datlen) {
810 memset(&s, 0, sizeof(s));
811 deflateInit(&s, zlib_compression_level);
814 s.avail_in = deltalen;
819 s.avail_out = deflateBound(&s, s.avail_in);
820 s.next_out = out = xmalloc(s.avail_out);
821 while (deflate(&s, Z_FINISH) == Z_OK)
825 /* Determine if we should auto-checkpoint. */
826 if ((object_count + 1) > max_objects
827 || (object_count + 1) < object_count
828 || (pack_size + 60 + s.total_out) > max_packsize
829 || (pack_size + 60 + s.total_out) < pack_size) {
831 /* This new object needs to *not* have the current pack_id. */
832 e->pack_id = pack_id + 1;
835 /* We cannot carry a delta into the new pack. */
840 memset(&s, 0, sizeof(s));
841 deflateInit(&s, zlib_compression_level);
844 s.avail_out = deflateBound(&s, s.avail_in);
845 s.next_out = out = xrealloc(out, s.avail_out);
846 while (deflate(&s, Z_FINISH) == Z_OK)
853 e->pack_id = pack_id;
854 e->offset = pack_size;
856 object_count_by_type[type]++;
859 unsigned long ofs = e->offset - last->offset;
860 unsigned pos = sizeof(hdr) - 1;
862 delta_count_by_type[type]++;
865 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
866 write_or_die(pack_fd, hdr, hdrlen);
869 hdr[pos] = ofs & 127;
871 hdr[--pos] = 128 | (--ofs & 127);
872 write_or_die(pack_fd, hdr + pos, sizeof(hdr) - pos);
873 pack_size += sizeof(hdr) - pos;
877 hdrlen = encode_header(type, datlen, hdr);
878 write_or_die(pack_fd, hdr, hdrlen);
882 write_or_die(pack_fd, out, s.total_out);
883 pack_size += s.total_out;
889 if (last->data && !last->no_free)
892 last->offset = e->offset;
898 static void *gfi_unpack_entry(
899 struct object_entry *oe,
900 unsigned long *sizep)
902 static char type[20];
903 struct packed_git *p = all_packs[oe->pack_id];
905 p->pack_size = pack_size + 20;
906 return unpack_entry(p, oe->offset, type, sizep);
909 static const char *get_mode(const char *str, unsigned int *modep)
912 unsigned int mode = 0;
914 while ((c = *str++) != ' ') {
915 if (c < '0' || c > '7')
917 mode = (mode << 3) + (c - '0');
923 static void load_tree(struct tree_entry *root)
925 unsigned char* sha1 = root->versions[1].sha1;
926 struct object_entry *myoe;
927 struct tree_content *t;
932 root->tree = t = new_tree_content(8);
933 if (is_null_sha1(sha1))
936 myoe = find_object(sha1);
938 if (myoe->type != OBJ_TREE)
939 die("Not a tree: %s", sha1_to_hex(sha1));
941 buf = gfi_unpack_entry(myoe, &size);
944 buf = read_sha1_file(sha1, type, &size);
945 if (!buf || strcmp(type, tree_type))
946 die("Can't load tree %s", sha1_to_hex(sha1));
950 while (c != (buf + size)) {
951 struct tree_entry *e = new_tree_entry();
953 if (t->entry_count == t->entry_capacity)
954 root->tree = t = grow_tree_content(t, 8);
955 t->entries[t->entry_count++] = e;
958 c = get_mode(c, &e->versions[1].mode);
960 die("Corrupt mode in %s", sha1_to_hex(sha1));
961 e->versions[0].mode = e->versions[1].mode;
962 e->name = to_atom(c, strlen(c));
963 c += e->name->str_len + 1;
964 hashcpy(e->versions[0].sha1, (unsigned char*)c);
965 hashcpy(e->versions[1].sha1, (unsigned char*)c);
971 static int tecmp0 (const void *_a, const void *_b)
973 struct tree_entry *a = *((struct tree_entry**)_a);
974 struct tree_entry *b = *((struct tree_entry**)_b);
975 return base_name_compare(
976 a->name->str_dat, a->name->str_len, a->versions[0].mode,
977 b->name->str_dat, b->name->str_len, b->versions[0].mode);
980 static int tecmp1 (const void *_a, const void *_b)
982 struct tree_entry *a = *((struct tree_entry**)_a);
983 struct tree_entry *b = *((struct tree_entry**)_b);
984 return base_name_compare(
985 a->name->str_dat, a->name->str_len, a->versions[1].mode,
986 b->name->str_dat, b->name->str_len, b->versions[1].mode);
989 static void mktree(struct tree_content *t,
999 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1001 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1003 for (i = 0; i < t->entry_count; i++) {
1004 if (t->entries[i]->versions[v].mode)
1005 maxlen += t->entries[i]->name->str_len + 34;
1008 size_dbuf(b, maxlen);
1010 for (i = 0; i < t->entry_count; i++) {
1011 struct tree_entry *e = t->entries[i];
1012 if (!e->versions[v].mode)
1014 c += sprintf(c, "%o", e->versions[v].mode);
1016 strcpy(c, e->name->str_dat);
1017 c += e->name->str_len + 1;
1018 hashcpy((unsigned char*)c, e->versions[v].sha1);
1021 *szp = c - (char*)b->buffer;
1024 static void store_tree(struct tree_entry *root)
1026 struct tree_content *t = root->tree;
1027 unsigned int i, j, del;
1028 unsigned long new_len;
1029 struct last_object lo;
1030 struct object_entry *le;
1032 if (!is_null_sha1(root->versions[1].sha1))
1035 for (i = 0; i < t->entry_count; i++) {
1036 if (t->entries[i]->tree)
1037 store_tree(t->entries[i]);
1040 le = find_object(root->versions[0].sha1);
1041 if (!S_ISDIR(root->versions[0].mode)
1043 || le->pack_id != pack_id) {
1047 mktree(t, 0, &lo.len, &old_tree);
1048 lo.data = old_tree.buffer;
1049 lo.offset = le->offset;
1050 lo.depth = t->delta_depth;
1054 mktree(t, 1, &new_len, &new_tree);
1055 store_object(OBJ_TREE, new_tree.buffer, new_len,
1056 &lo, root->versions[1].sha1, 0);
1058 t->delta_depth = lo.depth;
1059 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1060 struct tree_entry *e = t->entries[i];
1061 if (e->versions[1].mode) {
1062 e->versions[0].mode = e->versions[1].mode;
1063 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1064 t->entries[j++] = e;
1066 release_tree_entry(e);
1070 t->entry_count -= del;
1073 static int tree_content_set(
1074 struct tree_entry *root,
1076 const unsigned char *sha1,
1077 const unsigned int mode)
1079 struct tree_content *t = root->tree;
1082 struct tree_entry *e;
1084 slash1 = strchr(p, '/');
1090 for (i = 0; i < t->entry_count; i++) {
1092 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1094 if (e->versions[1].mode == mode
1095 && !hashcmp(e->versions[1].sha1, sha1))
1097 e->versions[1].mode = mode;
1098 hashcpy(e->versions[1].sha1, sha1);
1100 release_tree_content_recursive(e->tree);
1103 hashclr(root->versions[1].sha1);
1106 if (!S_ISDIR(e->versions[1].mode)) {
1107 e->tree = new_tree_content(8);
1108 e->versions[1].mode = S_IFDIR;
1112 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1113 hashclr(root->versions[1].sha1);
1120 if (t->entry_count == t->entry_capacity)
1121 root->tree = t = grow_tree_content(t, 8);
1122 e = new_tree_entry();
1123 e->name = to_atom(p, n);
1124 e->versions[0].mode = 0;
1125 hashclr(e->versions[0].sha1);
1126 t->entries[t->entry_count++] = e;
1128 e->tree = new_tree_content(8);
1129 e->versions[1].mode = S_IFDIR;
1130 tree_content_set(e, slash1 + 1, sha1, mode);
1133 e->versions[1].mode = mode;
1134 hashcpy(e->versions[1].sha1, sha1);
1136 hashclr(root->versions[1].sha1);
1140 static int tree_content_remove(struct tree_entry *root, const char *p)
1142 struct tree_content *t = root->tree;
1145 struct tree_entry *e;
1147 slash1 = strchr(p, '/');
1153 for (i = 0; i < t->entry_count; i++) {
1155 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1156 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1160 if (tree_content_remove(e, slash1 + 1)) {
1161 for (n = 0; n < e->tree->entry_count; n++) {
1162 if (e->tree->entries[n]->versions[1].mode) {
1163 hashclr(root->versions[1].sha1);
1176 release_tree_content_recursive(e->tree);
1179 e->versions[1].mode = 0;
1180 hashclr(e->versions[1].sha1);
1181 hashclr(root->versions[1].sha1);
1185 static void dump_branches()
1187 static const char *msg = "fast-import";
1190 struct ref_lock *lock;
1192 for (i = 0; i < branch_table_sz; i++) {
1193 for (b = branch_table[i]; b; b = b->table_next_branch) {
1194 lock = lock_any_ref_for_update(b->name, NULL);
1195 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1196 die("Can't write %s", b->name);
1201 static void dump_tags()
1203 static const char *msg = "fast-import";
1205 struct ref_lock *lock;
1206 char path[PATH_MAX];
1208 for (t = first_tag; t; t = t->next_tag) {
1209 sprintf(path, "refs/tags/%s", t->name);
1210 lock = lock_any_ref_for_update(path, NULL);
1211 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1212 die("Can't write %s", path);
1216 static void dump_marks_helper(FILE *f,
1222 for (k = 0; k < 1024; k++) {
1223 if (m->data.sets[k])
1224 dump_marks_helper(f, (base + k) << m->shift,
1228 for (k = 0; k < 1024; k++) {
1229 if (m->data.marked[k])
1230 fprintf(f, ":%ju %s\n", base + k,
1231 sha1_to_hex(m->data.marked[k]->sha1));
1236 static void dump_marks()
1240 FILE *f = fopen(mark_file, "w");
1241 dump_marks_helper(f, 0, marks);
1246 static void read_next_command()
1248 read_line(&command_buf, stdin, '\n');
1251 static void cmd_mark()
1253 if (!strncmp("mark :", command_buf.buf, 6)) {
1254 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1255 read_next_command();
1261 static void* cmd_data (size_t *size)
1267 if (strncmp("data ", command_buf.buf, 5))
1268 die("Expected 'data n' command, found: %s", command_buf.buf);
1270 length = strtoul(command_buf.buf + 5, NULL, 10);
1271 buffer = xmalloc(length);
1273 while (n < length) {
1274 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1275 if (!s && feof(stdin))
1276 die("EOF in data (%lu bytes remaining)", length - n);
1280 if (fgetc(stdin) != '\n')
1281 die("An lf did not trail the binary data as expected.");
1287 static void cmd_new_blob()
1292 read_next_command();
1296 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1300 static void unload_one_branch()
1302 while (cur_active_branches
1303 && cur_active_branches >= max_active_branches) {
1304 unsigned long min_commit = ULONG_MAX;
1305 struct branch *e, *l = NULL, *p = NULL;
1307 for (e = active_branches; e; e = e->active_next_branch) {
1308 if (e->last_commit < min_commit) {
1310 min_commit = e->last_commit;
1316 e = p->active_next_branch;
1317 p->active_next_branch = e->active_next_branch;
1319 e = active_branches;
1320 active_branches = e->active_next_branch;
1322 e->active_next_branch = NULL;
1323 if (e->branch_tree.tree) {
1324 release_tree_content_recursive(e->branch_tree.tree);
1325 e->branch_tree.tree = NULL;
1327 cur_active_branches--;
1331 static void load_branch(struct branch *b)
1333 load_tree(&b->branch_tree);
1334 b->active_next_branch = active_branches;
1335 active_branches = b;
1336 cur_active_branches++;
1337 branch_load_count++;
1340 static void file_change_m(struct branch *b)
1342 const char *p = command_buf.buf + 2;
1345 struct object_entry *oe;
1346 unsigned char sha1[20];
1350 p = get_mode(p, &mode);
1352 die("Corrupt mode: %s", command_buf.buf);
1354 case S_IFREG | 0644:
1355 case S_IFREG | 0755:
1362 die("Corrupt mode: %s", command_buf.buf);
1367 oe = find_mark(strtoumax(p + 1, &x, 10));
1368 hashcpy(sha1, oe->sha1);
1371 if (get_sha1_hex(p, sha1))
1372 die("Invalid SHA1: %s", command_buf.buf);
1373 oe = find_object(sha1);
1377 die("Missing space after SHA1: %s", command_buf.buf);
1379 p_uq = unquote_c_style(p, &endp);
1382 die("Garbage after path in: %s", command_buf.buf);
1387 if (oe->type != OBJ_BLOB)
1388 die("Not a blob (actually a %s): %s",
1389 command_buf.buf, type_names[oe->type]);
1391 if (sha1_object_info(sha1, type, NULL))
1392 die("Blob not found: %s", command_buf.buf);
1393 if (strcmp(blob_type, type))
1394 die("Not a blob (actually a %s): %s",
1395 command_buf.buf, type);
1398 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1404 static void file_change_d(struct branch *b)
1406 const char *p = command_buf.buf + 2;
1410 p_uq = unquote_c_style(p, &endp);
1413 die("Garbage after path in: %s", command_buf.buf);
1416 tree_content_remove(&b->branch_tree, p);
1421 static void cmd_from(struct branch *b)
1423 const char *from, *endp;
1427 if (strncmp("from ", command_buf.buf, 5))
1431 die("Can't reinitailize branch %s", b->name);
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 s = lookup_branch(from);
1443 die("Can't create a branch from itself: %s", b->name);
1445 unsigned char *t = s->branch_tree.versions[1].sha1;
1446 hashcpy(b->sha1, s->sha1);
1447 hashcpy(b->branch_tree.versions[0].sha1, t);
1448 hashcpy(b->branch_tree.versions[1].sha1, t);
1449 } else if (*from == ':') {
1450 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1451 struct object_entry *oe = find_mark(idnum);
1454 if (oe->type != OBJ_COMMIT)
1455 die("Mark :%ju not a commit", idnum);
1456 hashcpy(b->sha1, oe->sha1);
1457 buf = gfi_unpack_entry(oe, &size);
1458 if (!buf || size < 46)
1459 die("Not a valid commit: %s", from);
1460 if (memcmp("tree ", buf, 5)
1461 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1462 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1464 hashcpy(b->branch_tree.versions[0].sha1,
1465 b->branch_tree.versions[1].sha1);
1466 } else if (!get_sha1(from, b->sha1)) {
1467 if (is_null_sha1(b->sha1)) {
1468 hashclr(b->branch_tree.versions[0].sha1);
1469 hashclr(b->branch_tree.versions[1].sha1);
1474 buf = read_object_with_reference(b->sha1,
1475 type_names[OBJ_COMMIT], &size, b->sha1);
1476 if (!buf || size < 46)
1477 die("Not a valid commit: %s", from);
1478 if (memcmp("tree ", buf, 5)
1479 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1480 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1482 hashcpy(b->branch_tree.versions[0].sha1,
1483 b->branch_tree.versions[1].sha1);
1486 die("Invalid ref name or SHA1 expression: %s", from);
1488 read_next_command();
1491 static struct hash_list* cmd_merge(unsigned int *count)
1493 struct hash_list *list = NULL, *n, *e;
1494 const char *from, *endp;
1499 while (!strncmp("merge ", command_buf.buf, 6)) {
1500 from = strchr(command_buf.buf, ' ') + 1;
1501 str_uq = unquote_c_style(from, &endp);
1504 die("Garbage after string in: %s", command_buf.buf);
1508 n = xmalloc(sizeof(*n));
1509 s = lookup_branch(from);
1511 hashcpy(n->sha1, s->sha1);
1512 else if (*from == ':') {
1513 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1514 struct object_entry *oe = find_mark(idnum);
1515 if (oe->type != OBJ_COMMIT)
1516 die("Mark :%ju not a commit", idnum);
1517 hashcpy(n->sha1, oe->sha1);
1518 } else if (get_sha1(from, n->sha1))
1519 die("Invalid ref name or SHA1 expression: %s", from);
1528 read_next_command();
1533 static void cmd_new_commit()
1541 char *author = NULL;
1542 char *committer = NULL;
1543 struct hash_list *merge_list = NULL;
1544 unsigned int merge_count;
1546 /* Obtain the branch name from the rest of our command */
1547 sp = strchr(command_buf.buf, ' ') + 1;
1548 str_uq = unquote_c_style(sp, &endp);
1551 die("Garbage after ref in: %s", command_buf.buf);
1554 b = lookup_branch(sp);
1560 read_next_command();
1562 if (!strncmp("author ", command_buf.buf, 7)) {
1563 author = strdup(command_buf.buf);
1564 read_next_command();
1566 if (!strncmp("committer ", command_buf.buf, 10)) {
1567 committer = strdup(command_buf.buf);
1568 read_next_command();
1571 die("Expected committer but didn't get one");
1572 msg = cmd_data(&msglen);
1573 read_next_command();
1575 merge_list = cmd_merge(&merge_count);
1577 /* ensure the branch is active/loaded */
1578 if (!b->branch_tree.tree || !max_active_branches) {
1579 unload_one_branch();
1585 if (1 == command_buf.len)
1587 else if (!strncmp("M ", command_buf.buf, 2))
1589 else if (!strncmp("D ", command_buf.buf, 2))
1592 die("Unsupported file_change: %s", command_buf.buf);
1593 read_next_command();
1596 /* build the tree and the commit */
1597 store_tree(&b->branch_tree);
1598 hashcpy(b->branch_tree.versions[0].sha1,
1599 b->branch_tree.versions[1].sha1);
1600 size_dbuf(&new_data, 97 + msglen
1603 ? strlen(author) + strlen(committer)
1604 : 2 * strlen(committer)));
1605 sp = new_data.buffer;
1606 sp += sprintf(sp, "tree %s\n",
1607 sha1_to_hex(b->branch_tree.versions[1].sha1));
1608 if (!is_null_sha1(b->sha1))
1609 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1610 while (merge_list) {
1611 struct hash_list *next = merge_list->next;
1612 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1617 sp += sprintf(sp, "%s\n", author);
1619 sp += sprintf(sp, "author %s\n", committer + 10);
1620 sp += sprintf(sp, "%s\n\n", committer);
1621 memcpy(sp, msg, msglen);
1628 store_object(OBJ_COMMIT,
1629 new_data.buffer, sp - (char*)new_data.buffer,
1630 NULL, b->sha1, next_mark);
1631 b->last_commit = object_count_by_type[OBJ_COMMIT];
1634 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1635 fprintf(branch_log, "commit ");
1637 fputc('"', branch_log);
1638 quote_c_style(b->name, NULL, branch_log, 0);
1639 fputc('"', branch_log);
1641 fprintf(branch_log, "%s", b->name);
1642 fprintf(branch_log," :%ju %s\n",next_mark,sha1_to_hex(b->sha1));
1646 static void cmd_new_tag()
1657 uintmax_t from_mark = 0;
1658 unsigned char sha1[20];
1660 /* Obtain the new tag name from the rest of our command */
1661 sp = strchr(command_buf.buf, ' ') + 1;
1662 str_uq = unquote_c_style(sp, &endp);
1665 die("Garbage after tag name in: %s", command_buf.buf);
1668 t = pool_alloc(sizeof(struct tag));
1670 t->name = pool_strdup(sp);
1672 last_tag->next_tag = t;
1678 read_next_command();
1681 if (strncmp("from ", command_buf.buf, 5))
1682 die("Expected from command, got %s", command_buf.buf);
1684 from = strchr(command_buf.buf, ' ') + 1;
1685 str_uq = unquote_c_style(from, &endp);
1688 die("Garbage after string in: %s", command_buf.buf);
1692 s = lookup_branch(from);
1694 hashcpy(sha1, s->sha1);
1695 } else if (*from == ':') {
1696 from_mark = strtoumax(from + 1, NULL, 10);
1697 struct object_entry *oe = find_mark(from_mark);
1698 if (oe->type != OBJ_COMMIT)
1699 die("Mark :%ju not a commit", from_mark);
1700 hashcpy(sha1, oe->sha1);
1701 } else if (!get_sha1(from, sha1)) {
1705 buf = read_object_with_reference(sha1,
1706 type_names[OBJ_COMMIT], &size, sha1);
1707 if (!buf || size < 46)
1708 die("Not a valid commit: %s", from);
1711 die("Invalid ref name or SHA1 expression: %s", from);
1715 read_next_command();
1718 if (strncmp("tagger ", command_buf.buf, 7))
1719 die("Expected tagger command, got %s", command_buf.buf);
1720 tagger = strdup(command_buf.buf);
1722 /* tag payload/message */
1723 read_next_command();
1724 msg = cmd_data(&msglen);
1726 /* build the tag object */
1727 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1728 sp = new_data.buffer;
1729 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1730 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1731 sp += sprintf(sp, "tag %s\n", t->name);
1732 sp += sprintf(sp, "%s\n\n", tagger);
1733 memcpy(sp, msg, msglen);
1738 store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1742 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1743 fprintf(branch_log, "tag ");
1745 fputc('"', branch_log);
1746 quote_c_style(t->name, NULL, branch_log, 0);
1747 fputc('"', branch_log);
1749 fprintf(branch_log, "%s", t->name);
1750 fprintf(branch_log," :%ju %s\n",from_mark,sha1_to_hex(t->sha1));
1754 static void cmd_reset_branch()
1761 /* Obtain the branch name from the rest of our command */
1762 sp = strchr(command_buf.buf, ' ') + 1;
1763 str_uq = unquote_c_style(sp, &endp);
1766 die("Garbage after ref in: %s", command_buf.buf);
1769 b = lookup_branch(sp);
1772 if (b->branch_tree.tree) {
1773 release_tree_content_recursive(b->branch_tree.tree);
1774 b->branch_tree.tree = NULL;
1781 read_next_command();
1785 static void cmd_checkpoint()
1789 read_next_command();
1792 static const char fast_import_usage[] =
1793 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1795 int main(int argc, const char **argv)
1798 uintmax_t est_obj_cnt = object_entry_alloc;
1799 uintmax_t duplicate_count;
1802 git_config(git_default_config);
1804 for (i = 1; i < argc; i++) {
1805 const char *a = argv[i];
1807 if (*a != '-' || !strcmp(a, "--"))
1809 else if (!strncmp(a, "--objects=", 10))
1810 est_obj_cnt = strtoumax(a + 10, NULL, 0);
1811 else if (!strncmp(a, "--max-objects-per-pack=", 23))
1812 max_objects = strtoumax(a + 23, NULL, 0);
1813 else if (!strncmp(a, "--max-pack-size=", 16))
1814 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
1815 else if (!strncmp(a, "--depth=", 8))
1816 max_depth = strtoul(a + 8, NULL, 0);
1817 else if (!strncmp(a, "--active-branches=", 18))
1818 max_active_branches = strtoul(a + 18, NULL, 0);
1819 else if (!strncmp(a, "--export-marks=", 15))
1821 else if (!strncmp(a, "--branch-log=", 13)) {
1822 branch_log = fopen(a + 13, "w");
1824 die("Can't create %s: %s", a + 13, strerror(errno));
1827 die("unknown option %s", a);
1830 usage(fast_import_usage);
1831 base_name = argv[i];
1833 alloc_objects(est_obj_cnt);
1834 strbuf_init(&command_buf);
1836 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1837 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1838 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1839 marks = pool_calloc(1, sizeof(struct mark_set));
1843 read_next_command();
1844 if (command_buf.eof)
1846 else if (!strcmp("blob", command_buf.buf))
1848 else if (!strncmp("commit ", command_buf.buf, 7))
1850 else if (!strncmp("tag ", command_buf.buf, 4))
1852 else if (!strncmp("reset ", command_buf.buf, 6))
1854 else if (!strcmp("checkpoint", command_buf.buf))
1857 die("Unsupported command: %s", command_buf.buf);
1867 duplicate_count = 0;
1868 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
1869 duplicate_count += duplicate_count_by_type[i];
1871 fprintf(stderr, "%s statistics:\n", argv[0]);
1872 fprintf(stderr, "---------------------------------------------------------------------\n");
1873 fprintf(stderr, "Alloc'd objects: %10ju (%10ju overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1874 fprintf(stderr, "Total objects: %10ju (%10ju duplicates )\n", object_count, duplicate_count);
1875 fprintf(stderr, " blobs : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
1876 fprintf(stderr, " trees : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
1877 fprintf(stderr, " commits: %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
1878 fprintf(stderr, " tags : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
1879 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1880 fprintf(stderr, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
1881 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1882 fprintf(stderr, "Memory total: %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1883 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1884 fprintf(stderr, " objects: %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1885 fprintf(stderr, "---------------------------------------------------------------------\n");
1887 fprintf(stderr, "---------------------------------------------------------------------\n");
1888 fprintf(stderr, "\n");