Support creation of merge commits in fast-import.
[git/raj.git] / fast-import.c
blob15db4b39d136403b66ac1ca7d5d5745f24ef577d
1 /*
2 Format of STDIN stream:
4 stream ::= cmd*;
6 cmd ::= new_blob
7 | new_commit
8 | new_tag
9 | reset_branch
12 new_blob ::= 'blob' lf
13 mark?
14 file_content;
15 file_content ::= data;
17 new_commit ::= 'commit' sp ref_str lf
18 mark?
19 ('author' sp name '<' email '>' ts tz lf)?
20 'committer' sp name '<' email '>' ts tz lf
21 commit_msg
22 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
23 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
24 file_change*
25 lf;
26 commit_msg ::= data;
28 file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
29 | 'D' 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
36 tag_msg;
37 tag_msg ::= data;
39 reset_branch ::= 'reset' sp ref_str lf;
41 # note: the first idnum in a stream should be 1 and subsequent
42 # idnums should not have gaps between values as this will cause
43 # the stream parser to reserve space for the gapped values. An
44 # idnum can be updated in the future to a new object by issuing
45 # a new mark directive with the old idnum.
47 mark ::= 'mark' sp idnum lf;
49 # note: declen indicates the length of binary_data in bytes.
50 # declen does not include the lf preceeding or trailing the
51 # binary data.
53 data ::= 'data' sp declen lf
54 binary_data
55 lf;
57 # note: quoted strings are C-style quoting supporting \c for
58 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
59 # is the signed byte value in octal. Note that the only
60 # characters which must actually be escaped to protect the
61 # stream formatting is: \, " and LF. Otherwise these values
62 # are UTF8.
64 ref_str ::= ref | '"' quoted(ref) '"' ;
65 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
66 tag_str ::= tag | '"' quoted(tag) '"' ;
67 path_str ::= path | '"' quoted(path) '"' ;
69 declen ::= # unsigned 32 bit value, ascii base10 notation;
70 binary_data ::= # file content, not interpreted;
72 sp ::= # ASCII space character;
73 lf ::= # ASCII newline (LF) character;
75 # note: a colon (':') must precede the numerical value assigned to
76 # an idnum. This is to distinguish it from a ref or tag name as
77 # GIT does not permit ':' in ref or tag strings.
79 idnum ::= ':' declen;
80 path ::= # GIT style file path, e.g. "a/b/c";
81 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
82 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
83 sha1exp ::= # Any valid GIT SHA1 expression;
84 hexsha1 ::= # SHA1 in hexadecimal format;
86 # note: name and email are UTF8 strings, however name must not
87 # contain '<' or lf and email must not contain any of the
88 # following: '<', '>', lf.
90 name ::= # valid GIT author/committer name;
91 email ::= # valid GIT author/committer email;
92 ts ::= # time since the epoch in seconds, ascii base10 notation;
93 tz ::= # GIT style timezone;
96 #include "builtin.h"
97 #include "cache.h"
98 #include "object.h"
99 #include "blob.h"
100 #include "tree.h"
101 #include "delta.h"
102 #include "pack.h"
103 #include "refs.h"
104 #include "csum-file.h"
105 #include "strbuf.h"
106 #include "quote.h"
108 struct object_entry
110 struct object_entry *next;
111 enum object_type type;
112 unsigned long offset;
113 unsigned char sha1[20];
116 struct object_entry_pool
118 struct object_entry_pool *next_pool;
119 struct object_entry *next_free;
120 struct object_entry *end;
121 struct object_entry entries[FLEX_ARRAY]; /* more */
124 struct mark_set
126 int shift;
127 union {
128 struct object_entry *marked[1024];
129 struct mark_set *sets[1024];
130 } data;
133 struct last_object
135 void *data;
136 unsigned long len;
137 unsigned int depth;
138 int no_free;
139 unsigned char sha1[20];
142 struct mem_pool
144 struct mem_pool *next_pool;
145 char *next_free;
146 char *end;
147 char space[FLEX_ARRAY]; /* more */
150 struct atom_str
152 struct atom_str *next_atom;
153 int str_len;
154 char str_dat[FLEX_ARRAY]; /* more */
157 struct tree_content;
158 struct tree_entry
160 struct tree_content *tree;
161 struct atom_str* name;
162 struct tree_entry_ms
164 unsigned int mode;
165 unsigned char sha1[20];
166 } versions[2];
169 struct tree_content
171 unsigned int entry_capacity; /* must match avail_tree_content */
172 unsigned int entry_count;
173 unsigned int delta_depth;
174 struct tree_entry *entries[FLEX_ARRAY]; /* more */
177 struct avail_tree_content
179 unsigned int entry_capacity; /* must match tree_content */
180 struct avail_tree_content *next_avail;
183 struct branch
185 struct branch *table_next_branch;
186 struct branch *active_next_branch;
187 const char *name;
188 unsigned long last_commit;
189 struct tree_entry branch_tree;
190 unsigned char sha1[20];
193 struct tag
195 struct tag *next_tag;
196 const char *name;
197 unsigned char sha1[20];
200 struct dbuf
202 void *buffer;
203 size_t capacity;
206 struct hash_list
208 struct hash_list *next;
209 unsigned char sha1[20];
212 /* Stats and misc. counters */
213 static unsigned long max_depth = 10;
214 static unsigned long alloc_count;
215 static unsigned long branch_count;
216 static unsigned long branch_load_count;
217 static unsigned long remap_count;
218 static unsigned long object_count;
219 static unsigned long duplicate_count;
220 static unsigned long marks_set_count;
221 static unsigned long object_count_by_type[9];
222 static unsigned long duplicate_count_by_type[9];
223 static unsigned long delta_count_by_type[9];
225 /* Memory pools */
226 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
227 static size_t total_allocd;
228 static struct mem_pool *mem_pool;
230 /* Atom management */
231 static unsigned int atom_table_sz = 4451;
232 static unsigned int atom_cnt;
233 static struct atom_str **atom_table;
235 /* The .pack file being generated */
236 static int pack_fd;
237 static unsigned long pack_size;
238 static unsigned char pack_sha1[20];
239 static unsigned char* pack_base;
240 static unsigned long pack_moff;
241 static unsigned long pack_mlen = 128*1024*1024;
242 static unsigned long page_size;
244 /* Table of objects we've written. */
245 static unsigned int object_entry_alloc = 5000;
246 static struct object_entry_pool *blocks;
247 static struct object_entry *object_table[1 << 16];
248 static struct mark_set *marks;
249 static const char* mark_file;
251 /* Our last blob */
252 static struct last_object last_blob;
254 /* Tree management */
255 static unsigned int tree_entry_alloc = 1000;
256 static void *avail_tree_entry;
257 static unsigned int avail_tree_table_sz = 100;
258 static struct avail_tree_content **avail_tree_table;
259 static struct dbuf old_tree;
260 static struct dbuf new_tree;
262 /* Branch data */
263 static unsigned long max_active_branches = 5;
264 static unsigned long cur_active_branches;
265 static unsigned long branch_table_sz = 1039;
266 static struct branch **branch_table;
267 static struct branch *active_branches;
269 /* Tag data */
270 static struct tag *first_tag;
271 static struct tag *last_tag;
273 /* Input stream parsing */
274 static struct strbuf command_buf;
275 static unsigned long next_mark;
276 static struct dbuf new_data;
277 static FILE* branch_log;
280 static void alloc_objects(int cnt)
282 struct object_entry_pool *b;
284 b = xmalloc(sizeof(struct object_entry_pool)
285 + cnt * sizeof(struct object_entry));
286 b->next_pool = blocks;
287 b->next_free = b->entries;
288 b->end = b->entries + cnt;
289 blocks = b;
290 alloc_count += cnt;
293 static struct object_entry* new_object(unsigned char *sha1)
295 struct object_entry *e;
297 if (blocks->next_free == blocks->end)
298 alloc_objects(object_entry_alloc);
300 e = blocks->next_free++;
301 hashcpy(e->sha1, sha1);
302 return e;
305 static struct object_entry* find_object(unsigned char *sha1)
307 unsigned int h = sha1[0] << 8 | sha1[1];
308 struct object_entry *e;
309 for (e = object_table[h]; e; e = e->next)
310 if (!hashcmp(sha1, e->sha1))
311 return e;
312 return NULL;
315 static struct object_entry* insert_object(unsigned char *sha1)
317 unsigned int h = sha1[0] << 8 | sha1[1];
318 struct object_entry *e = object_table[h];
319 struct object_entry *p = NULL;
321 while (e) {
322 if (!hashcmp(sha1, e->sha1))
323 return e;
324 p = e;
325 e = e->next;
328 e = new_object(sha1);
329 e->next = NULL;
330 e->offset = 0;
331 if (p)
332 p->next = e;
333 else
334 object_table[h] = e;
335 return e;
338 static unsigned int hc_str(const char *s, size_t len)
340 unsigned int r = 0;
341 while (len-- > 0)
342 r = r * 31 + *s++;
343 return r;
346 static void* pool_alloc(size_t len)
348 struct mem_pool *p;
349 void *r;
351 for (p = mem_pool; p; p = p->next_pool)
352 if ((p->end - p->next_free >= len))
353 break;
355 if (!p) {
356 if (len >= (mem_pool_alloc/2)) {
357 total_allocd += len;
358 return xmalloc(len);
360 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
361 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
362 p->next_pool = mem_pool;
363 p->next_free = p->space;
364 p->end = p->next_free + mem_pool_alloc;
365 mem_pool = p;
368 r = p->next_free;
369 /* round out to a pointer alignment */
370 if (len & (sizeof(void*) - 1))
371 len += sizeof(void*) - (len & (sizeof(void*) - 1));
372 p->next_free += len;
373 return r;
376 static void* pool_calloc(size_t count, size_t size)
378 size_t len = count * size;
379 void *r = pool_alloc(len);
380 memset(r, 0, len);
381 return r;
384 static char* pool_strdup(const char *s)
386 char *r = pool_alloc(strlen(s) + 1);
387 strcpy(r, s);
388 return r;
391 static void size_dbuf(struct dbuf *b, size_t maxlen)
393 if (b->buffer) {
394 if (b->capacity >= maxlen)
395 return;
396 free(b->buffer);
398 b->capacity = ((maxlen / 1024) + 1) * 1024;
399 b->buffer = xmalloc(b->capacity);
402 static void insert_mark(unsigned long idnum, struct object_entry *oe)
404 struct mark_set *s = marks;
405 while ((idnum >> s->shift) >= 1024) {
406 s = pool_calloc(1, sizeof(struct mark_set));
407 s->shift = marks->shift + 10;
408 s->data.sets[0] = marks;
409 marks = s;
411 while (s->shift) {
412 unsigned long i = idnum >> s->shift;
413 idnum -= i << s->shift;
414 if (!s->data.sets[i]) {
415 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
416 s->data.sets[i]->shift = s->shift - 10;
418 s = s->data.sets[i];
420 if (!s->data.marked[idnum])
421 marks_set_count++;
422 s->data.marked[idnum] = oe;
425 static struct object_entry* find_mark(unsigned long idnum)
427 unsigned long orig_idnum = idnum;
428 struct mark_set *s = marks;
429 struct object_entry *oe = NULL;
430 if ((idnum >> s->shift) < 1024) {
431 while (s && s->shift) {
432 unsigned long i = idnum >> s->shift;
433 idnum -= i << s->shift;
434 s = s->data.sets[i];
436 if (s)
437 oe = s->data.marked[idnum];
439 if (!oe)
440 die("mark :%lu not declared", orig_idnum);
441 return oe;
444 static struct atom_str* to_atom(const char *s, size_t len)
446 unsigned int hc = hc_str(s, len) % atom_table_sz;
447 struct atom_str *c;
449 for (c = atom_table[hc]; c; c = c->next_atom)
450 if (c->str_len == len && !strncmp(s, c->str_dat, len))
451 return c;
453 c = pool_alloc(sizeof(struct atom_str) + len + 1);
454 c->str_len = len;
455 strncpy(c->str_dat, s, len);
456 c->str_dat[len] = 0;
457 c->next_atom = atom_table[hc];
458 atom_table[hc] = c;
459 atom_cnt++;
460 return c;
463 static struct branch* lookup_branch(const char *name)
465 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
466 struct branch *b;
468 for (b = branch_table[hc]; b; b = b->table_next_branch)
469 if (!strcmp(name, b->name))
470 return b;
471 return NULL;
474 static struct branch* new_branch(const char *name)
476 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
477 struct branch* b = lookup_branch(name);
479 if (b)
480 die("Invalid attempt to create duplicate branch: %s", name);
481 if (check_ref_format(name))
482 die("Branch name doesn't conform to GIT standards: %s", name);
484 b = pool_calloc(1, sizeof(struct branch));
485 b->name = pool_strdup(name);
486 b->table_next_branch = branch_table[hc];
487 b->branch_tree.versions[0].mode = S_IFDIR;
488 b->branch_tree.versions[1].mode = S_IFDIR;
489 branch_table[hc] = b;
490 branch_count++;
491 return b;
494 static unsigned int hc_entries(unsigned int cnt)
496 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
497 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
500 static struct tree_content* new_tree_content(unsigned int cnt)
502 struct avail_tree_content *f, *l = NULL;
503 struct tree_content *t;
504 unsigned int hc = hc_entries(cnt);
506 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
507 if (f->entry_capacity >= cnt)
508 break;
510 if (f) {
511 if (l)
512 l->next_avail = f->next_avail;
513 else
514 avail_tree_table[hc] = f->next_avail;
515 } else {
516 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
517 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
518 f->entry_capacity = cnt;
521 t = (struct tree_content*)f;
522 t->entry_count = 0;
523 t->delta_depth = 0;
524 return t;
527 static void release_tree_entry(struct tree_entry *e);
528 static void release_tree_content(struct tree_content *t)
530 struct avail_tree_content *f = (struct avail_tree_content*)t;
531 unsigned int hc = hc_entries(f->entry_capacity);
532 f->next_avail = avail_tree_table[hc];
533 avail_tree_table[hc] = f;
536 static void release_tree_content_recursive(struct tree_content *t)
538 unsigned int i;
539 for (i = 0; i < t->entry_count; i++)
540 release_tree_entry(t->entries[i]);
541 release_tree_content(t);
544 static struct tree_content* grow_tree_content(
545 struct tree_content *t,
546 int amt)
548 struct tree_content *r = new_tree_content(t->entry_count + amt);
549 r->entry_count = t->entry_count;
550 r->delta_depth = t->delta_depth;
551 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
552 release_tree_content(t);
553 return r;
556 static struct tree_entry* new_tree_entry()
558 struct tree_entry *e;
560 if (!avail_tree_entry) {
561 unsigned int n = tree_entry_alloc;
562 total_allocd += n * sizeof(struct tree_entry);
563 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
564 while (n-- > 1) {
565 *((void**)e) = e + 1;
566 e++;
568 *((void**)e) = NULL;
571 e = avail_tree_entry;
572 avail_tree_entry = *((void**)e);
573 return e;
576 static void release_tree_entry(struct tree_entry *e)
578 if (e->tree)
579 release_tree_content_recursive(e->tree);
580 *((void**)e) = avail_tree_entry;
581 avail_tree_entry = e;
584 static void yread(int fd, void *buffer, size_t length)
586 ssize_t ret = 0;
587 while (ret < length) {
588 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
589 if (!size)
590 die("Read from descriptor %i: end of stream", fd);
591 if (size < 0)
592 die("Read from descriptor %i: %s", fd, strerror(errno));
593 ret += size;
597 static size_t encode_header(
598 enum object_type type,
599 size_t size,
600 unsigned char *hdr)
602 int n = 1;
603 unsigned char c;
605 if (type < OBJ_COMMIT || type > OBJ_DELTA)
606 die("bad type %d", type);
608 c = (type << 4) | (size & 15);
609 size >>= 4;
610 while (size) {
611 *hdr++ = c | 0x80;
612 c = size & 0x7f;
613 size >>= 7;
614 n++;
616 *hdr = c;
617 return n;
620 static int store_object(
621 enum object_type type,
622 void *dat,
623 size_t datlen,
624 struct last_object *last,
625 unsigned char *sha1out,
626 unsigned long mark)
628 void *out, *delta;
629 struct object_entry *e;
630 unsigned char hdr[96];
631 unsigned char sha1[20];
632 unsigned long hdrlen, deltalen;
633 SHA_CTX c;
634 z_stream s;
636 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
637 SHA1_Init(&c);
638 SHA1_Update(&c, hdr, hdrlen);
639 SHA1_Update(&c, dat, datlen);
640 SHA1_Final(sha1, &c);
641 if (sha1out)
642 hashcpy(sha1out, sha1);
644 e = insert_object(sha1);
645 if (mark)
646 insert_mark(mark, e);
647 if (e->offset) {
648 duplicate_count++;
649 duplicate_count_by_type[type]++;
650 return 1;
652 e->type = type;
653 e->offset = pack_size;
654 object_count++;
655 object_count_by_type[type]++;
657 if (last && last->data && last->depth < max_depth)
658 delta = diff_delta(last->data, last->len,
659 dat, datlen,
660 &deltalen, 0);
661 else
662 delta = 0;
664 memset(&s, 0, sizeof(s));
665 deflateInit(&s, zlib_compression_level);
667 if (delta) {
668 delta_count_by_type[type]++;
669 last->depth++;
670 s.next_in = delta;
671 s.avail_in = deltalen;
672 hdrlen = encode_header(OBJ_DELTA, deltalen, hdr);
673 write_or_die(pack_fd, hdr, hdrlen);
674 write_or_die(pack_fd, last->sha1, sizeof(sha1));
675 pack_size += hdrlen + sizeof(sha1);
676 } else {
677 if (last)
678 last->depth = 0;
679 s.next_in = dat;
680 s.avail_in = datlen;
681 hdrlen = encode_header(type, datlen, hdr);
682 write_or_die(pack_fd, hdr, hdrlen);
683 pack_size += hdrlen;
686 s.avail_out = deflateBound(&s, s.avail_in);
687 s.next_out = out = xmalloc(s.avail_out);
688 while (deflate(&s, Z_FINISH) == Z_OK)
689 /* nothing */;
690 deflateEnd(&s);
692 write_or_die(pack_fd, out, s.total_out);
693 pack_size += s.total_out;
695 free(out);
696 if (delta)
697 free(delta);
698 if (last) {
699 if (last->data && !last->no_free)
700 free(last->data);
701 last->data = dat;
702 last->len = datlen;
703 hashcpy(last->sha1, sha1);
705 return 0;
708 static unsigned char* map_pack(unsigned long offset, unsigned int *left)
710 if (offset >= pack_size)
711 die("object offset outside of pack file");
712 if (!pack_base
713 || offset < pack_moff
714 || (offset + 20) >= (pack_moff + pack_mlen)) {
715 if (pack_base)
716 munmap(pack_base, pack_mlen);
717 pack_moff = (offset / page_size) * page_size;
718 pack_base = mmap(NULL,pack_mlen,PROT_READ,MAP_SHARED,
719 pack_fd,pack_moff);
720 if (pack_base == MAP_FAILED)
721 die("Failed to map generated pack: %s", strerror(errno));
722 remap_count++;
724 offset -= pack_moff;
725 if (left)
726 *left = pack_mlen - offset;
727 return pack_base + offset;
730 static unsigned long unpack_object_header(unsigned long offset,
731 enum object_type *type,
732 unsigned long *sizep)
734 unsigned shift;
735 unsigned char c;
736 unsigned long size;
738 c = *map_pack(offset++, NULL);
739 *type = (c >> 4) & 7;
740 size = c & 15;
741 shift = 4;
742 while (c & 0x80) {
743 c = *map_pack(offset++, NULL);
744 size += (c & 0x7f) << shift;
745 shift += 7;
747 *sizep = size;
748 return offset;
751 static void *unpack_non_delta_entry(unsigned long o, unsigned long sz)
753 z_stream stream;
754 unsigned char *result;
756 result = xmalloc(sz + 1);
757 result[sz] = 0;
759 memset(&stream, 0, sizeof(stream));
760 stream.next_in = map_pack(o, &stream.avail_in);
761 stream.next_out = result;
762 stream.avail_out = sz;
764 inflateInit(&stream);
765 for (;;) {
766 int st = inflate(&stream, Z_FINISH);
767 if (st == Z_STREAM_END)
768 break;
769 if (st == Z_OK || st == Z_BUF_ERROR) {
770 o = stream.next_in - pack_base + pack_moff;
771 stream.next_in = map_pack(o, &stream.avail_in);
772 continue;
774 die("Error %i from zlib during inflate.", st);
776 inflateEnd(&stream);
777 if (stream.total_out != sz)
778 die("Error after inflate: sizes mismatch");
779 return result;
782 static void *unpack_entry(unsigned long offset,
783 unsigned long *sizep,
784 unsigned int *delta_depth);
786 static void *unpack_delta_entry(unsigned long offset,
787 unsigned long delta_size,
788 unsigned long *sizep,
789 unsigned int *delta_depth)
791 struct object_entry *base_oe;
792 unsigned char *base_sha1;
793 void *delta_data, *base, *result;
794 unsigned long base_size, result_size;
796 base_sha1 = map_pack(offset, NULL);
797 base_oe = find_object(base_sha1);
798 if (!base_oe)
799 die("I'm broken; I can't find a base I know must be here.");
800 base = unpack_entry(base_oe->offset, &base_size, delta_depth);
801 delta_data = unpack_non_delta_entry(offset + 20, delta_size);
802 result = patch_delta(base, base_size,
803 delta_data, delta_size,
804 &result_size);
805 if (!result)
806 die("failed to apply delta");
807 free(delta_data);
808 free(base);
809 *sizep = result_size;
810 (*delta_depth)++;
811 return result;
814 static void *unpack_entry(unsigned long offset,
815 unsigned long *sizep,
816 unsigned int *delta_depth)
818 unsigned long size;
819 enum object_type kind;
821 offset = unpack_object_header(offset, &kind, &size);
822 switch (kind) {
823 case OBJ_DELTA:
824 return unpack_delta_entry(offset, size, sizep, delta_depth);
825 case OBJ_COMMIT:
826 case OBJ_TREE:
827 case OBJ_BLOB:
828 case OBJ_TAG:
829 *sizep = size;
830 *delta_depth = 0;
831 return unpack_non_delta_entry(offset, size);
832 default:
833 die("I created an object I can't read!");
837 static const char *get_mode(const char *str, unsigned int *modep)
839 unsigned char c;
840 unsigned int mode = 0;
842 while ((c = *str++) != ' ') {
843 if (c < '0' || c > '7')
844 return NULL;
845 mode = (mode << 3) + (c - '0');
847 *modep = mode;
848 return str;
851 static void load_tree(struct tree_entry *root)
853 unsigned char* sha1 = root->versions[1].sha1;
854 struct object_entry *myoe;
855 struct tree_content *t;
856 unsigned long size;
857 char *buf;
858 const char *c;
860 root->tree = t = new_tree_content(8);
861 if (is_null_sha1(sha1))
862 return;
864 myoe = find_object(sha1);
865 if (myoe) {
866 if (myoe->type != OBJ_TREE)
867 die("Not a tree: %s", sha1_to_hex(sha1));
868 buf = unpack_entry(myoe->offset, &size, &t->delta_depth);
869 } else {
870 char type[20];
871 buf = read_sha1_file(sha1, type, &size);
872 if (!buf || strcmp(type, tree_type))
873 die("Can't load tree %s", sha1_to_hex(sha1));
876 c = buf;
877 while (c != (buf + size)) {
878 struct tree_entry *e = new_tree_entry();
880 if (t->entry_count == t->entry_capacity)
881 root->tree = t = grow_tree_content(t, 8);
882 t->entries[t->entry_count++] = e;
884 e->tree = NULL;
885 c = get_mode(c, &e->versions[1].mode);
886 if (!c)
887 die("Corrupt mode in %s", sha1_to_hex(sha1));
888 e->versions[0].mode = e->versions[1].mode;
889 e->name = to_atom(c, strlen(c));
890 c += e->name->str_len + 1;
891 hashcpy(e->versions[0].sha1, (unsigned char*)c);
892 hashcpy(e->versions[1].sha1, (unsigned char*)c);
893 c += 20;
895 free(buf);
898 static int tecmp0 (const void *_a, const void *_b)
900 struct tree_entry *a = *((struct tree_entry**)_a);
901 struct tree_entry *b = *((struct tree_entry**)_b);
902 return base_name_compare(
903 a->name->str_dat, a->name->str_len, a->versions[0].mode,
904 b->name->str_dat, b->name->str_len, b->versions[0].mode);
907 static int tecmp1 (const void *_a, const void *_b)
909 struct tree_entry *a = *((struct tree_entry**)_a);
910 struct tree_entry *b = *((struct tree_entry**)_b);
911 return base_name_compare(
912 a->name->str_dat, a->name->str_len, a->versions[1].mode,
913 b->name->str_dat, b->name->str_len, b->versions[1].mode);
916 static void mktree(struct tree_content *t,
917 int v,
918 unsigned long *szp,
919 struct dbuf *b)
921 size_t maxlen = 0;
922 unsigned int i;
923 char *c;
925 if (!v)
926 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
927 else
928 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
930 for (i = 0; i < t->entry_count; i++) {
931 if (t->entries[i]->versions[v].mode)
932 maxlen += t->entries[i]->name->str_len + 34;
935 size_dbuf(b, maxlen);
936 c = b->buffer;
937 for (i = 0; i < t->entry_count; i++) {
938 struct tree_entry *e = t->entries[i];
939 if (!e->versions[v].mode)
940 continue;
941 c += sprintf(c, "%o", e->versions[v].mode);
942 *c++ = ' ';
943 strcpy(c, e->name->str_dat);
944 c += e->name->str_len + 1;
945 hashcpy((unsigned char*)c, e->versions[v].sha1);
946 c += 20;
948 *szp = c - (char*)b->buffer;
951 static void store_tree(struct tree_entry *root)
953 struct tree_content *t = root->tree;
954 unsigned int i, j, del;
955 unsigned long new_len;
956 struct last_object lo;
958 if (!is_null_sha1(root->versions[1].sha1))
959 return;
961 for (i = 0; i < t->entry_count; i++) {
962 if (t->entries[i]->tree)
963 store_tree(t->entries[i]);
966 if (!S_ISDIR(root->versions[0].mode)
967 || is_null_sha1(root->versions[0].sha1)
968 || !find_object(root->versions[0].sha1)) {
969 lo.data = NULL;
970 lo.depth = 0;
971 } else {
972 mktree(t, 0, &lo.len, &old_tree);
973 lo.data = old_tree.buffer;
974 lo.depth = t->delta_depth;
975 lo.no_free = 1;
976 hashcpy(lo.sha1, root->versions[0].sha1);
979 mktree(t, 1, &new_len, &new_tree);
980 store_object(OBJ_TREE, new_tree.buffer, new_len,
981 &lo, root->versions[1].sha1, 0);
983 t->delta_depth = lo.depth;
984 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
985 struct tree_entry *e = t->entries[i];
986 if (e->versions[1].mode) {
987 e->versions[0].mode = e->versions[1].mode;
988 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
989 t->entries[j++] = e;
990 } else {
991 release_tree_entry(e);
992 del++;
995 t->entry_count -= del;
998 static int tree_content_set(
999 struct tree_entry *root,
1000 const char *p,
1001 const unsigned char *sha1,
1002 const unsigned int mode)
1004 struct tree_content *t = root->tree;
1005 const char *slash1;
1006 unsigned int i, n;
1007 struct tree_entry *e;
1009 slash1 = strchr(p, '/');
1010 if (slash1)
1011 n = slash1 - p;
1012 else
1013 n = strlen(p);
1015 for (i = 0; i < t->entry_count; i++) {
1016 e = t->entries[i];
1017 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1018 if (!slash1) {
1019 if (e->versions[1].mode == mode
1020 && !hashcmp(e->versions[1].sha1, sha1))
1021 return 0;
1022 e->versions[1].mode = mode;
1023 hashcpy(e->versions[1].sha1, sha1);
1024 if (e->tree) {
1025 release_tree_content_recursive(e->tree);
1026 e->tree = NULL;
1028 hashclr(root->versions[1].sha1);
1029 return 1;
1031 if (!S_ISDIR(e->versions[1].mode)) {
1032 e->tree = new_tree_content(8);
1033 e->versions[1].mode = S_IFDIR;
1035 if (!e->tree)
1036 load_tree(e);
1037 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1038 hashclr(root->versions[1].sha1);
1039 return 1;
1041 return 0;
1045 if (t->entry_count == t->entry_capacity)
1046 root->tree = t = grow_tree_content(t, 8);
1047 e = new_tree_entry();
1048 e->name = to_atom(p, n);
1049 e->versions[0].mode = 0;
1050 hashclr(e->versions[0].sha1);
1051 t->entries[t->entry_count++] = e;
1052 if (slash1) {
1053 e->tree = new_tree_content(8);
1054 e->versions[1].mode = S_IFDIR;
1055 tree_content_set(e, slash1 + 1, sha1, mode);
1056 } else {
1057 e->tree = NULL;
1058 e->versions[1].mode = mode;
1059 hashcpy(e->versions[1].sha1, sha1);
1061 hashclr(root->versions[1].sha1);
1062 return 1;
1065 static int tree_content_remove(struct tree_entry *root, const char *p)
1067 struct tree_content *t = root->tree;
1068 const char *slash1;
1069 unsigned int i, n;
1070 struct tree_entry *e;
1072 slash1 = strchr(p, '/');
1073 if (slash1)
1074 n = slash1 - p;
1075 else
1076 n = strlen(p);
1078 for (i = 0; i < t->entry_count; i++) {
1079 e = t->entries[i];
1080 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1081 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1082 goto del_entry;
1083 if (!e->tree)
1084 load_tree(e);
1085 if (tree_content_remove(e, slash1 + 1)) {
1086 for (n = 0; n < e->tree->entry_count; n++) {
1087 if (e->tree->entries[n]->versions[1].mode) {
1088 hashclr(root->versions[1].sha1);
1089 return 1;
1092 goto del_entry;
1094 return 0;
1097 return 0;
1099 del_entry:
1100 if (e->tree) {
1101 release_tree_content_recursive(e->tree);
1102 e->tree = NULL;
1104 e->versions[1].mode = 0;
1105 hashclr(e->versions[1].sha1);
1106 hashclr(root->versions[1].sha1);
1107 return 1;
1110 static void init_pack_header()
1112 struct pack_header hdr;
1114 hdr.hdr_signature = htonl(PACK_SIGNATURE);
1115 hdr.hdr_version = htonl(2);
1116 hdr.hdr_entries = 0;
1118 write_or_die(pack_fd, &hdr, sizeof(hdr));
1119 pack_size = sizeof(hdr);
1122 static void fixup_header_footer()
1124 SHA_CTX c;
1125 char hdr[8];
1126 unsigned long cnt;
1127 char *buf;
1128 size_t n;
1130 if (lseek(pack_fd, 0, SEEK_SET) != 0)
1131 die("Failed seeking to start: %s", strerror(errno));
1133 SHA1_Init(&c);
1134 yread(pack_fd, hdr, 8);
1135 SHA1_Update(&c, hdr, 8);
1137 cnt = htonl(object_count);
1138 SHA1_Update(&c, &cnt, 4);
1139 write_or_die(pack_fd, &cnt, 4);
1141 buf = xmalloc(128 * 1024);
1142 for (;;) {
1143 n = xread(pack_fd, buf, 128 * 1024);
1144 if (n <= 0)
1145 break;
1146 SHA1_Update(&c, buf, n);
1148 free(buf);
1150 SHA1_Final(pack_sha1, &c);
1151 write_or_die(pack_fd, pack_sha1, sizeof(pack_sha1));
1154 static int oecmp (const void *_a, const void *_b)
1156 struct object_entry *a = *((struct object_entry**)_a);
1157 struct object_entry *b = *((struct object_entry**)_b);
1158 return hashcmp(a->sha1, b->sha1);
1161 static void write_index(const char *idx_name)
1163 struct sha1file *f;
1164 struct object_entry **idx, **c, **last;
1165 struct object_entry *e;
1166 struct object_entry_pool *o;
1167 unsigned int array[256];
1168 int i;
1170 /* Build the sorted table of object IDs. */
1171 idx = xmalloc(object_count * sizeof(struct object_entry*));
1172 c = idx;
1173 for (o = blocks; o; o = o->next_pool)
1174 for (e = o->entries; e != o->next_free; e++)
1175 *c++ = e;
1176 last = idx + object_count;
1177 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
1179 /* Generate the fan-out array. */
1180 c = idx;
1181 for (i = 0; i < 256; i++) {
1182 struct object_entry **next = c;;
1183 while (next < last) {
1184 if ((*next)->sha1[0] != i)
1185 break;
1186 next++;
1188 array[i] = htonl(next - idx);
1189 c = next;
1192 f = sha1create("%s", idx_name);
1193 sha1write(f, array, 256 * sizeof(int));
1194 for (c = idx; c != last; c++) {
1195 unsigned int offset = htonl((*c)->offset);
1196 sha1write(f, &offset, 4);
1197 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
1199 sha1write(f, pack_sha1, sizeof(pack_sha1));
1200 sha1close(f, NULL, 1);
1201 free(idx);
1204 static void dump_branches()
1206 static const char *msg = "fast-import";
1207 unsigned int i;
1208 struct branch *b;
1209 struct ref_lock *lock;
1211 for (i = 0; i < branch_table_sz; i++) {
1212 for (b = branch_table[i]; b; b = b->table_next_branch) {
1213 lock = lock_any_ref_for_update(b->name, NULL, 0);
1214 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1215 die("Can't write %s", b->name);
1220 static void dump_tags()
1222 static const char *msg = "fast-import";
1223 struct tag *t;
1224 struct ref_lock *lock;
1225 char path[PATH_MAX];
1227 for (t = first_tag; t; t = t->next_tag) {
1228 sprintf(path, "refs/tags/%s", t->name);
1229 lock = lock_any_ref_for_update(path, NULL, 0);
1230 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1231 die("Can't write %s", path);
1235 static void dump_marks_helper(FILE *f,
1236 unsigned long base,
1237 struct mark_set *m)
1239 int k;
1240 if (m->shift) {
1241 for (k = 0; k < 1024; k++) {
1242 if (m->data.sets[k])
1243 dump_marks_helper(f, (base + k) << m->shift,
1244 m->data.sets[k]);
1246 } else {
1247 for (k = 0; k < 1024; k++) {
1248 if (m->data.marked[k])
1249 fprintf(f, ":%lu %s\n", base + k,
1250 sha1_to_hex(m->data.marked[k]->sha1));
1255 static void dump_marks()
1257 if (mark_file)
1259 FILE *f = fopen(mark_file, "w");
1260 dump_marks_helper(f, 0, marks);
1261 fclose(f);
1265 static void read_next_command()
1267 read_line(&command_buf, stdin, '\n');
1270 static void cmd_mark()
1272 if (!strncmp("mark :", command_buf.buf, 6)) {
1273 next_mark = strtoul(command_buf.buf + 6, NULL, 10);
1274 read_next_command();
1276 else
1277 next_mark = 0;
1280 static void* cmd_data (size_t *size)
1282 size_t n = 0;
1283 void *buffer;
1284 size_t length;
1286 if (strncmp("data ", command_buf.buf, 5))
1287 die("Expected 'data n' command, found: %s", command_buf.buf);
1289 length = strtoul(command_buf.buf + 5, NULL, 10);
1290 buffer = xmalloc(length);
1292 while (n < length) {
1293 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1294 if (!s && feof(stdin))
1295 die("EOF in data (%lu bytes remaining)", length - n);
1296 n += s;
1299 if (fgetc(stdin) != '\n')
1300 die("An lf did not trail the binary data as expected.");
1302 *size = length;
1303 return buffer;
1306 static void cmd_new_blob()
1308 size_t l;
1309 void *d;
1311 read_next_command();
1312 cmd_mark();
1313 d = cmd_data(&l);
1315 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1316 free(d);
1319 static void unload_one_branch()
1321 while (cur_active_branches
1322 && cur_active_branches >= max_active_branches) {
1323 unsigned long min_commit = ULONG_MAX;
1324 struct branch *e, *l = NULL, *p = NULL;
1326 for (e = active_branches; e; e = e->active_next_branch) {
1327 if (e->last_commit < min_commit) {
1328 p = l;
1329 min_commit = e->last_commit;
1331 l = e;
1334 if (p) {
1335 e = p->active_next_branch;
1336 p->active_next_branch = e->active_next_branch;
1337 } else {
1338 e = active_branches;
1339 active_branches = e->active_next_branch;
1341 e->active_next_branch = NULL;
1342 if (e->branch_tree.tree) {
1343 release_tree_content_recursive(e->branch_tree.tree);
1344 e->branch_tree.tree = NULL;
1346 cur_active_branches--;
1350 static void load_branch(struct branch *b)
1352 load_tree(&b->branch_tree);
1353 b->active_next_branch = active_branches;
1354 active_branches = b;
1355 cur_active_branches++;
1356 branch_load_count++;
1359 static void file_change_m(struct branch *b)
1361 const char *p = command_buf.buf + 2;
1362 char *p_uq;
1363 const char *endp;
1364 struct object_entry *oe;
1365 unsigned char sha1[20];
1366 unsigned int mode;
1367 char type[20];
1369 p = get_mode(p, &mode);
1370 if (!p)
1371 die("Corrupt mode: %s", command_buf.buf);
1372 switch (mode) {
1373 case S_IFREG | 0644:
1374 case S_IFREG | 0755:
1375 case S_IFLNK:
1376 case 0644:
1377 case 0755:
1378 /* ok */
1379 break;
1380 default:
1381 die("Corrupt mode: %s", command_buf.buf);
1384 if (*p == ':') {
1385 char *x;
1386 oe = find_mark(strtoul(p + 1, &x, 10));
1387 hashcpy(sha1, oe->sha1);
1388 p = x;
1389 } else {
1390 if (get_sha1_hex(p, sha1))
1391 die("Invalid SHA1: %s", command_buf.buf);
1392 oe = find_object(sha1);
1393 p += 40;
1395 if (*p++ != ' ')
1396 die("Missing space after SHA1: %s", command_buf.buf);
1398 p_uq = unquote_c_style(p, &endp);
1399 if (p_uq) {
1400 if (*endp)
1401 die("Garbage after path in: %s", command_buf.buf);
1402 p = p_uq;
1405 if (oe) {
1406 if (oe->type != OBJ_BLOB)
1407 die("Not a blob (actually a %s): %s",
1408 command_buf.buf, type_names[oe->type]);
1409 } else {
1410 if (sha1_object_info(sha1, type, NULL))
1411 die("Blob not found: %s", command_buf.buf);
1412 if (strcmp(blob_type, type))
1413 die("Not a blob (actually a %s): %s",
1414 command_buf.buf, type);
1417 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1419 if (p_uq)
1420 free(p_uq);
1423 static void file_change_d(struct branch *b)
1425 const char *p = command_buf.buf + 2;
1426 char *p_uq;
1427 const char *endp;
1429 p_uq = unquote_c_style(p, &endp);
1430 if (p_uq) {
1431 if (*endp)
1432 die("Garbage after path in: %s", command_buf.buf);
1433 p = p_uq;
1435 tree_content_remove(&b->branch_tree, p);
1436 if (p_uq)
1437 free(p_uq);
1440 static void cmd_from(struct branch *b)
1442 const char *from, *endp;
1443 char *str_uq;
1444 struct branch *s;
1446 if (strncmp("from ", command_buf.buf, 5))
1447 return;
1449 if (b->last_commit)
1450 die("Can't reinitailize branch %s", b->name);
1452 from = strchr(command_buf.buf, ' ') + 1;
1453 str_uq = unquote_c_style(from, &endp);
1454 if (str_uq) {
1455 if (*endp)
1456 die("Garbage after string in: %s", command_buf.buf);
1457 from = str_uq;
1460 s = lookup_branch(from);
1461 if (b == s)
1462 die("Can't create a branch from itself: %s", b->name);
1463 else if (s) {
1464 unsigned char *t = s->branch_tree.versions[1].sha1;
1465 hashcpy(b->sha1, s->sha1);
1466 hashcpy(b->branch_tree.versions[0].sha1, t);
1467 hashcpy(b->branch_tree.versions[1].sha1, t);
1468 } else if (*from == ':') {
1469 unsigned long idnum = strtoul(from + 1, NULL, 10);
1470 struct object_entry *oe = find_mark(idnum);
1471 unsigned long size;
1472 unsigned int depth;
1473 char *buf;
1474 if (oe->type != OBJ_COMMIT)
1475 die("Mark :%lu not a commit", idnum);
1476 hashcpy(b->sha1, oe->sha1);
1477 buf = unpack_entry(oe->offset, &size, &depth);
1478 if (!buf || size < 46)
1479 die("Not a valid commit: %s", from);
1480 if (memcmp("tree ", buf, 5)
1481 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1482 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1483 free(buf);
1484 hashcpy(b->branch_tree.versions[0].sha1,
1485 b->branch_tree.versions[1].sha1);
1486 } else if (!get_sha1(from, b->sha1)) {
1487 if (is_null_sha1(b->sha1)) {
1488 hashclr(b->branch_tree.versions[0].sha1);
1489 hashclr(b->branch_tree.versions[1].sha1);
1490 } else {
1491 unsigned long size;
1492 char *buf;
1494 buf = read_object_with_reference(b->sha1,
1495 type_names[OBJ_COMMIT], &size, b->sha1);
1496 if (!buf || size < 46)
1497 die("Not a valid commit: %s", from);
1498 if (memcmp("tree ", buf, 5)
1499 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1500 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1501 free(buf);
1502 hashcpy(b->branch_tree.versions[0].sha1,
1503 b->branch_tree.versions[1].sha1);
1505 } else
1506 die("Invalid ref name or SHA1 expression: %s", from);
1508 read_next_command();
1511 static struct hash_list* cmd_merge(unsigned int *count)
1513 struct hash_list *list = NULL, *n, *e;
1514 const char *from, *endp;
1515 char *str_uq;
1516 struct branch *s;
1518 *count = 0;
1519 while (!strncmp("merge ", command_buf.buf, 6)) {
1520 from = strchr(command_buf.buf, ' ') + 1;
1521 str_uq = unquote_c_style(from, &endp);
1522 if (str_uq) {
1523 if (*endp)
1524 die("Garbage after string in: %s", command_buf.buf);
1525 from = str_uq;
1528 n = xmalloc(sizeof(*n));
1529 s = lookup_branch(from);
1530 if (s)
1531 hashcpy(n->sha1, s->sha1);
1532 else if (*from == ':') {
1533 unsigned long idnum = strtoul(from + 1, NULL, 10);
1534 struct object_entry *oe = find_mark(idnum);
1535 if (oe->type != OBJ_COMMIT)
1536 die("Mark :%lu not a commit", idnum);
1537 hashcpy(n->sha1, oe->sha1);
1538 } else if (get_sha1(from, n->sha1))
1539 die("Invalid ref name or SHA1 expression: %s", from);
1541 n->next = NULL;
1542 if (list)
1543 e->next = n;
1544 else
1545 list = n;
1546 e = n;
1547 *count++;
1548 read_next_command();
1550 return list;
1553 static void cmd_new_commit()
1555 struct branch *b;
1556 void *msg;
1557 size_t msglen;
1558 char *str_uq;
1559 const char *endp;
1560 char *sp;
1561 char *author = NULL;
1562 char *committer = NULL;
1563 struct hash_list *merge_list = NULL;
1564 unsigned int merge_count;
1566 /* Obtain the branch name from the rest of our command */
1567 sp = strchr(command_buf.buf, ' ') + 1;
1568 str_uq = unquote_c_style(sp, &endp);
1569 if (str_uq) {
1570 if (*endp)
1571 die("Garbage after ref in: %s", command_buf.buf);
1572 sp = str_uq;
1574 b = lookup_branch(sp);
1575 if (!b)
1576 b = new_branch(sp);
1577 if (str_uq)
1578 free(str_uq);
1580 read_next_command();
1581 cmd_mark();
1582 if (!strncmp("author ", command_buf.buf, 7)) {
1583 author = strdup(command_buf.buf);
1584 read_next_command();
1586 if (!strncmp("committer ", command_buf.buf, 10)) {
1587 committer = strdup(command_buf.buf);
1588 read_next_command();
1590 if (!committer)
1591 die("Expected committer but didn't get one");
1592 msg = cmd_data(&msglen);
1593 read_next_command();
1594 cmd_from(b);
1595 merge_list = cmd_merge(&merge_count);
1597 /* ensure the branch is active/loaded */
1598 if (!b->branch_tree.tree || !max_active_branches) {
1599 unload_one_branch();
1600 load_branch(b);
1603 /* file_change* */
1604 for (;;) {
1605 if (1 == command_buf.len)
1606 break;
1607 else if (!strncmp("M ", command_buf.buf, 2))
1608 file_change_m(b);
1609 else if (!strncmp("D ", command_buf.buf, 2))
1610 file_change_d(b);
1611 else
1612 die("Unsupported file_change: %s", command_buf.buf);
1613 read_next_command();
1616 /* build the tree and the commit */
1617 store_tree(&b->branch_tree);
1618 hashcpy(b->branch_tree.versions[0].sha1,
1619 b->branch_tree.versions[1].sha1);
1620 size_dbuf(&new_data, 97 + msglen
1621 + merge_count * 49
1622 + (author
1623 ? strlen(author) + strlen(committer)
1624 : 2 * strlen(committer)));
1625 sp = new_data.buffer;
1626 sp += sprintf(sp, "tree %s\n",
1627 sha1_to_hex(b->branch_tree.versions[1].sha1));
1628 if (!is_null_sha1(b->sha1))
1629 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1630 while (merge_list) {
1631 struct hash_list *next = merge_list->next;
1632 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1633 free(merge_list);
1634 merge_list = next;
1636 if (author)
1637 sp += sprintf(sp, "%s\n", author);
1638 else
1639 sp += sprintf(sp, "author %s\n", committer + 10);
1640 sp += sprintf(sp, "%s\n\n", committer);
1641 memcpy(sp, msg, msglen);
1642 sp += msglen;
1643 if (author)
1644 free(author);
1645 free(committer);
1646 free(msg);
1648 store_object(OBJ_COMMIT,
1649 new_data.buffer, sp - (char*)new_data.buffer,
1650 NULL, b->sha1, next_mark);
1651 b->last_commit = object_count_by_type[OBJ_COMMIT];
1653 if (branch_log) {
1654 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1655 fprintf(branch_log, "commit ");
1656 if (need_dq) {
1657 fputc('"', branch_log);
1658 quote_c_style(b->name, NULL, branch_log, 0);
1659 fputc('"', branch_log);
1660 } else
1661 fprintf(branch_log, "%s", b->name);
1662 fprintf(branch_log," :%lu %s\n",next_mark,sha1_to_hex(b->sha1));
1666 static void cmd_new_tag()
1668 char *str_uq;
1669 const char *endp;
1670 char *sp;
1671 const char *from;
1672 char *tagger;
1673 struct branch *s;
1674 void *msg;
1675 size_t msglen;
1676 struct tag *t;
1677 unsigned long from_mark = 0;
1678 unsigned char sha1[20];
1680 /* Obtain the new tag name from the rest of our command */
1681 sp = strchr(command_buf.buf, ' ') + 1;
1682 str_uq = unquote_c_style(sp, &endp);
1683 if (str_uq) {
1684 if (*endp)
1685 die("Garbage after tag name in: %s", command_buf.buf);
1686 sp = str_uq;
1688 t = pool_alloc(sizeof(struct tag));
1689 t->next_tag = NULL;
1690 t->name = pool_strdup(sp);
1691 if (last_tag)
1692 last_tag->next_tag = t;
1693 else
1694 first_tag = t;
1695 last_tag = t;
1696 if (str_uq)
1697 free(str_uq);
1698 read_next_command();
1700 /* from ... */
1701 if (strncmp("from ", command_buf.buf, 5))
1702 die("Expected from command, got %s", command_buf.buf);
1704 from = strchr(command_buf.buf, ' ') + 1;
1705 str_uq = unquote_c_style(from, &endp);
1706 if (str_uq) {
1707 if (*endp)
1708 die("Garbage after string in: %s", command_buf.buf);
1709 from = str_uq;
1712 s = lookup_branch(from);
1713 if (s) {
1714 hashcpy(sha1, s->sha1);
1715 } else if (*from == ':') {
1716 from_mark = strtoul(from + 1, NULL, 10);
1717 struct object_entry *oe = find_mark(from_mark);
1718 if (oe->type != OBJ_COMMIT)
1719 die("Mark :%lu not a commit", from_mark);
1720 hashcpy(sha1, oe->sha1);
1721 } else if (!get_sha1(from, sha1)) {
1722 unsigned long size;
1723 char *buf;
1725 buf = read_object_with_reference(sha1,
1726 type_names[OBJ_COMMIT], &size, sha1);
1727 if (!buf || size < 46)
1728 die("Not a valid commit: %s", from);
1729 free(buf);
1730 } else
1731 die("Invalid ref name or SHA1 expression: %s", from);
1733 if (str_uq)
1734 free(str_uq);
1735 read_next_command();
1737 /* tagger ... */
1738 if (strncmp("tagger ", command_buf.buf, 7))
1739 die("Expected tagger command, got %s", command_buf.buf);
1740 tagger = strdup(command_buf.buf);
1742 /* tag payload/message */
1743 read_next_command();
1744 msg = cmd_data(&msglen);
1746 /* build the tag object */
1747 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1748 sp = new_data.buffer;
1749 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1750 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1751 sp += sprintf(sp, "tag %s\n", t->name);
1752 sp += sprintf(sp, "%s\n\n", tagger);
1753 memcpy(sp, msg, msglen);
1754 sp += msglen;
1755 free(tagger);
1756 free(msg);
1758 store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1759 NULL, t->sha1, 0);
1761 if (branch_log) {
1762 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1763 fprintf(branch_log, "tag ");
1764 if (need_dq) {
1765 fputc('"', branch_log);
1766 quote_c_style(t->name, NULL, branch_log, 0);
1767 fputc('"', branch_log);
1768 } else
1769 fprintf(branch_log, "%s", t->name);
1770 fprintf(branch_log," :%lu %s\n",from_mark,sha1_to_hex(t->sha1));
1774 static void cmd_reset_branch()
1776 struct branch *b;
1777 char *str_uq;
1778 const char *endp;
1779 char *sp;
1781 /* Obtain the branch name from the rest of our command */
1782 sp = strchr(command_buf.buf, ' ') + 1;
1783 str_uq = unquote_c_style(sp, &endp);
1784 if (str_uq) {
1785 if (*endp)
1786 die("Garbage after ref in: %s", command_buf.buf);
1787 sp = str_uq;
1789 b = lookup_branch(sp);
1790 if (b) {
1791 b->last_commit = 0;
1792 if (b->branch_tree.tree) {
1793 release_tree_content_recursive(b->branch_tree.tree);
1794 b->branch_tree.tree = NULL;
1797 if (str_uq)
1798 free(str_uq);
1801 static const char fast_import_usage[] =
1802 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1804 int main(int argc, const char **argv)
1806 const char *base_name;
1807 int i;
1808 unsigned long est_obj_cnt = object_entry_alloc;
1809 char *pack_name;
1810 char *idx_name;
1811 struct stat sb;
1813 setup_ident();
1814 git_config(git_default_config);
1815 page_size = getpagesize();
1817 for (i = 1; i < argc; i++) {
1818 const char *a = argv[i];
1820 if (*a != '-' || !strcmp(a, "--"))
1821 break;
1822 else if (!strncmp(a, "--objects=", 10))
1823 est_obj_cnt = strtoul(a + 10, NULL, 0);
1824 else if (!strncmp(a, "--depth=", 8))
1825 max_depth = strtoul(a + 8, NULL, 0);
1826 else if (!strncmp(a, "--active-branches=", 18))
1827 max_active_branches = strtoul(a + 18, NULL, 0);
1828 else if (!strncmp(a, "--export-marks=", 15))
1829 mark_file = a + 15;
1830 else if (!strncmp(a, "--branch-log=", 13)) {
1831 branch_log = fopen(a + 13, "w");
1832 if (!branch_log)
1833 die("Can't create %s: %s", a + 13, strerror(errno));
1835 else
1836 die("unknown option %s", a);
1838 if ((i+1) != argc)
1839 usage(fast_import_usage);
1840 base_name = argv[i];
1842 pack_name = xmalloc(strlen(base_name) + 6);
1843 sprintf(pack_name, "%s.pack", base_name);
1844 idx_name = xmalloc(strlen(base_name) + 5);
1845 sprintf(idx_name, "%s.idx", base_name);
1847 pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
1848 if (pack_fd < 0)
1849 die("Can't create %s: %s", pack_name, strerror(errno));
1851 init_pack_header();
1852 alloc_objects(est_obj_cnt);
1853 strbuf_init(&command_buf);
1855 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1856 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1857 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1858 marks = pool_calloc(1, sizeof(struct mark_set));
1860 for (;;) {
1861 read_next_command();
1862 if (command_buf.eof)
1863 break;
1864 else if (!strcmp("blob", command_buf.buf))
1865 cmd_new_blob();
1866 else if (!strncmp("commit ", command_buf.buf, 7))
1867 cmd_new_commit();
1868 else if (!strncmp("tag ", command_buf.buf, 4))
1869 cmd_new_tag();
1870 else if (!strncmp("reset ", command_buf.buf, 6))
1871 cmd_reset_branch();
1872 else
1873 die("Unsupported command: %s", command_buf.buf);
1876 fixup_header_footer();
1877 close(pack_fd);
1878 write_index(idx_name);
1879 dump_branches();
1880 dump_tags();
1881 dump_marks();
1882 if (branch_log)
1883 fclose(branch_log);
1885 fprintf(stderr, "%s statistics:\n", argv[0]);
1886 fprintf(stderr, "---------------------------------------------------------------------\n");
1887 fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1888 fprintf(stderr, "Total objects: %10lu (%10lu duplicates )\n", object_count, duplicate_count);
1889 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]);
1890 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]);
1891 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]);
1892 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]);
1893 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1894 fprintf(stderr, " marks: %10u (%10lu unique )\n", (1 << marks->shift) * 1024, marks_set_count);
1895 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1896 fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1897 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1898 fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1899 fprintf(stderr, "Pack remaps: %10lu\n", remap_count);
1900 stat(pack_name, &sb);
1901 fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1902 stat(idx_name, &sb);
1903 fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1904 fprintf(stderr, "---------------------------------------------------------------------\n");
1906 fprintf(stderr, "\n");
1908 return 0;