Remove unnecessary null pointer checks in fast-import.
[git/dscho.git] / fast-import.c
blob4dcba416e098efc785fe7b28a37f1fa823d5548b
1 /*
2 Format of STDIN stream:
4 stream ::= cmd*;
6 cmd ::= new_blob
7 | new_commit
8 | new_tag
9 | reset_branch
10 | checkpoint
13 new_blob ::= 'blob' lf
14 mark?
15 file_content;
16 file_content ::= data;
18 new_commit ::= 'commit' sp ref_str lf
19 mark?
20 ('author' sp name '<' email '>' ts tz lf)?
21 'committer' sp name '<' email '>' ts tz lf
22 commit_msg
23 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
24 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
25 file_change*
26 lf;
27 commit_msg ::= data;
29 file_change ::= file_del | file_obm | file_inm;
30 file_del ::= 'D' sp path_str lf;
31 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
32 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
33 data;
35 new_tag ::= 'tag' sp tag_str lf
36 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
37 'tagger' sp name '<' email '>' ts tz lf
38 tag_msg;
39 tag_msg ::= data;
41 reset_branch ::= 'reset' sp ref_str lf
42 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
43 lf;
45 checkpoint ::= 'checkpoint' lf
46 lf;
48 # note: the first idnum in a stream should be 1 and subsequent
49 # idnums should not have gaps between values as this will cause
50 # the stream parser to reserve space for the gapped values. An
51 # idnum can be updated in the future to a new object by issuing
52 # a new mark directive with the old idnum.
54 mark ::= 'mark' sp idnum lf;
55 data ::= (delimited_data | exact_data)
56 lf;
58 # note: delim may be any string but must not contain lf.
59 # data_line may contain any data but must not be exactly
60 # delim.
61 delimited_data ::= 'data' sp '<<' delim lf
62 (data_line lf)*
63 delim lf;
65 # note: declen indicates the length of binary_data in bytes.
66 # declen does not include the lf preceeding the binary data.
68 exact_data ::= 'data' sp declen lf
69 binary_data;
71 # note: quoted strings are C-style quoting supporting \c for
72 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
73 # is the signed byte value in octal. Note that the only
74 # characters which must actually be escaped to protect the
75 # stream formatting is: \, " and LF. Otherwise these values
76 # are UTF8.
78 ref_str ::= ref;
79 sha1exp_str ::= sha1exp;
80 tag_str ::= tag;
81 path_str ::= path | '"' quoted(path) '"' ;
82 mode ::= '100644' | '644'
83 | '100755' | '755'
84 | '140000'
87 declen ::= # unsigned 32 bit value, ascii base10 notation;
88 bigint ::= # unsigned integer value, ascii base10 notation;
89 binary_data ::= # file content, not interpreted;
91 sp ::= # ASCII space character;
92 lf ::= # ASCII newline (LF) character;
94 # note: a colon (':') must precede the numerical value assigned to
95 # an idnum. This is to distinguish it from a ref or tag name as
96 # GIT does not permit ':' in ref or tag strings.
98 idnum ::= ':' bigint;
99 path ::= # GIT style file path, e.g. "a/b/c";
100 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
101 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
102 sha1exp ::= # Any valid GIT SHA1 expression;
103 hexsha1 ::= # SHA1 in hexadecimal format;
105 # note: name and email are UTF8 strings, however name must not
106 # contain '<' or lf and email must not contain any of the
107 # following: '<', '>', lf.
109 name ::= # valid GIT author/committer name;
110 email ::= # valid GIT author/committer email;
111 ts ::= # time since the epoch in seconds, ascii base10 notation;
112 tz ::= # GIT style timezone;
115 #include "builtin.h"
116 #include "cache.h"
117 #include "object.h"
118 #include "blob.h"
119 #include "tree.h"
120 #include "delta.h"
121 #include "pack.h"
122 #include "refs.h"
123 #include "csum-file.h"
124 #include "strbuf.h"
125 #include "quote.h"
127 #define PACK_ID_BITS 16
128 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
130 struct object_entry
132 struct object_entry *next;
133 uint32_t offset;
134 unsigned type : TYPE_BITS;
135 unsigned pack_id : PACK_ID_BITS;
136 unsigned char sha1[20];
139 struct object_entry_pool
141 struct object_entry_pool *next_pool;
142 struct object_entry *next_free;
143 struct object_entry *end;
144 struct object_entry entries[FLEX_ARRAY]; /* more */
147 struct mark_set
149 union {
150 struct object_entry *marked[1024];
151 struct mark_set *sets[1024];
152 } data;
153 unsigned int shift;
156 struct last_object
158 void *data;
159 unsigned long len;
160 uint32_t offset;
161 unsigned int depth;
162 unsigned no_free:1;
165 struct mem_pool
167 struct mem_pool *next_pool;
168 char *next_free;
169 char *end;
170 char space[FLEX_ARRAY]; /* more */
173 struct atom_str
175 struct atom_str *next_atom;
176 unsigned short str_len;
177 char str_dat[FLEX_ARRAY]; /* more */
180 struct tree_content;
181 struct tree_entry
183 struct tree_content *tree;
184 struct atom_str* name;
185 struct tree_entry_ms
187 uint16_t mode;
188 unsigned char sha1[20];
189 } versions[2];
192 struct tree_content
194 unsigned int entry_capacity; /* must match avail_tree_content */
195 unsigned int entry_count;
196 unsigned int delta_depth;
197 struct tree_entry *entries[FLEX_ARRAY]; /* more */
200 struct avail_tree_content
202 unsigned int entry_capacity; /* must match tree_content */
203 struct avail_tree_content *next_avail;
206 struct branch
208 struct branch *table_next_branch;
209 struct branch *active_next_branch;
210 const char *name;
211 struct tree_entry branch_tree;
212 uintmax_t last_commit;
213 unsigned int pack_id;
214 unsigned char sha1[20];
217 struct tag
219 struct tag *next_tag;
220 const char *name;
221 unsigned int pack_id;
222 unsigned char sha1[20];
225 struct dbuf
227 void *buffer;
228 size_t capacity;
231 struct hash_list
233 struct hash_list *next;
234 unsigned char sha1[20];
237 /* Configured limits on output */
238 static unsigned long max_depth = 10;
239 static unsigned long max_packsize = (1LL << 32) - 1;
241 /* Stats and misc. counters */
242 static uintmax_t alloc_count;
243 static uintmax_t marks_set_count;
244 static uintmax_t object_count_by_type[1 << TYPE_BITS];
245 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
246 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
247 static unsigned long object_count;
248 static unsigned long branch_count;
249 static unsigned long branch_load_count;
251 /* Memory pools */
252 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
253 static size_t total_allocd;
254 static struct mem_pool *mem_pool;
256 /* Atom management */
257 static unsigned int atom_table_sz = 4451;
258 static unsigned int atom_cnt;
259 static struct atom_str **atom_table;
261 /* The .pack file being generated */
262 static unsigned int pack_id;
263 static struct packed_git *pack_data;
264 static struct packed_git **all_packs;
265 static unsigned long pack_size;
267 /* Table of objects we've written. */
268 static unsigned int object_entry_alloc = 5000;
269 static struct object_entry_pool *blocks;
270 static struct object_entry *object_table[1 << 16];
271 static struct mark_set *marks;
272 static const char* mark_file;
274 /* Our last blob */
275 static struct last_object last_blob;
277 /* Tree management */
278 static unsigned int tree_entry_alloc = 1000;
279 static void *avail_tree_entry;
280 static unsigned int avail_tree_table_sz = 100;
281 static struct avail_tree_content **avail_tree_table;
282 static struct dbuf old_tree;
283 static struct dbuf new_tree;
285 /* Branch data */
286 static unsigned long max_active_branches = 5;
287 static unsigned long cur_active_branches;
288 static unsigned long branch_table_sz = 1039;
289 static struct branch **branch_table;
290 static struct branch *active_branches;
292 /* Tag data */
293 static struct tag *first_tag;
294 static struct tag *last_tag;
296 /* Input stream parsing */
297 static struct strbuf command_buf;
298 static uintmax_t next_mark;
299 static struct dbuf new_data;
302 static void alloc_objects(unsigned int cnt)
304 struct object_entry_pool *b;
306 b = xmalloc(sizeof(struct object_entry_pool)
307 + cnt * sizeof(struct object_entry));
308 b->next_pool = blocks;
309 b->next_free = b->entries;
310 b->end = b->entries + cnt;
311 blocks = b;
312 alloc_count += cnt;
315 static struct object_entry *new_object(unsigned char *sha1)
317 struct object_entry *e;
319 if (blocks->next_free == blocks->end)
320 alloc_objects(object_entry_alloc);
322 e = blocks->next_free++;
323 hashcpy(e->sha1, sha1);
324 return e;
327 static struct object_entry *find_object(unsigned char *sha1)
329 unsigned int h = sha1[0] << 8 | sha1[1];
330 struct object_entry *e;
331 for (e = object_table[h]; e; e = e->next)
332 if (!hashcmp(sha1, e->sha1))
333 return e;
334 return NULL;
337 static struct object_entry *insert_object(unsigned char *sha1)
339 unsigned int h = sha1[0] << 8 | sha1[1];
340 struct object_entry *e = object_table[h];
341 struct object_entry *p = NULL;
343 while (e) {
344 if (!hashcmp(sha1, e->sha1))
345 return e;
346 p = e;
347 e = e->next;
350 e = new_object(sha1);
351 e->next = NULL;
352 e->offset = 0;
353 if (p)
354 p->next = e;
355 else
356 object_table[h] = e;
357 return e;
360 static unsigned int hc_str(const char *s, size_t len)
362 unsigned int r = 0;
363 while (len-- > 0)
364 r = r * 31 + *s++;
365 return r;
368 static void *pool_alloc(size_t len)
370 struct mem_pool *p;
371 void *r;
373 for (p = mem_pool; p; p = p->next_pool)
374 if ((p->end - p->next_free >= len))
375 break;
377 if (!p) {
378 if (len >= (mem_pool_alloc/2)) {
379 total_allocd += len;
380 return xmalloc(len);
382 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
383 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
384 p->next_pool = mem_pool;
385 p->next_free = p->space;
386 p->end = p->next_free + mem_pool_alloc;
387 mem_pool = p;
390 r = p->next_free;
391 /* round out to a pointer alignment */
392 if (len & (sizeof(void*) - 1))
393 len += sizeof(void*) - (len & (sizeof(void*) - 1));
394 p->next_free += len;
395 return r;
398 static void *pool_calloc(size_t count, size_t size)
400 size_t len = count * size;
401 void *r = pool_alloc(len);
402 memset(r, 0, len);
403 return r;
406 static char *pool_strdup(const char *s)
408 char *r = pool_alloc(strlen(s) + 1);
409 strcpy(r, s);
410 return r;
413 static void size_dbuf(struct dbuf *b, size_t maxlen)
415 if (b->buffer) {
416 if (b->capacity >= maxlen)
417 return;
418 free(b->buffer);
420 b->capacity = ((maxlen / 1024) + 1) * 1024;
421 b->buffer = xmalloc(b->capacity);
424 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
426 struct mark_set *s = marks;
427 while ((idnum >> s->shift) >= 1024) {
428 s = pool_calloc(1, sizeof(struct mark_set));
429 s->shift = marks->shift + 10;
430 s->data.sets[0] = marks;
431 marks = s;
433 while (s->shift) {
434 uintmax_t i = idnum >> s->shift;
435 idnum -= i << s->shift;
436 if (!s->data.sets[i]) {
437 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
438 s->data.sets[i]->shift = s->shift - 10;
440 s = s->data.sets[i];
442 if (!s->data.marked[idnum])
443 marks_set_count++;
444 s->data.marked[idnum] = oe;
447 static struct object_entry *find_mark(uintmax_t idnum)
449 uintmax_t orig_idnum = idnum;
450 struct mark_set *s = marks;
451 struct object_entry *oe = NULL;
452 if ((idnum >> s->shift) < 1024) {
453 while (s && s->shift) {
454 uintmax_t i = idnum >> s->shift;
455 idnum -= i << s->shift;
456 s = s->data.sets[i];
458 if (s)
459 oe = s->data.marked[idnum];
461 if (!oe)
462 die("mark :%ju not declared", orig_idnum);
463 return oe;
466 static struct atom_str *to_atom(const char *s, unsigned short len)
468 unsigned int hc = hc_str(s, len) % atom_table_sz;
469 struct atom_str *c;
471 for (c = atom_table[hc]; c; c = c->next_atom)
472 if (c->str_len == len && !strncmp(s, c->str_dat, len))
473 return c;
475 c = pool_alloc(sizeof(struct atom_str) + len + 1);
476 c->str_len = len;
477 strncpy(c->str_dat, s, len);
478 c->str_dat[len] = 0;
479 c->next_atom = atom_table[hc];
480 atom_table[hc] = c;
481 atom_cnt++;
482 return c;
485 static struct branch *lookup_branch(const char *name)
487 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
488 struct branch *b;
490 for (b = branch_table[hc]; b; b = b->table_next_branch)
491 if (!strcmp(name, b->name))
492 return b;
493 return NULL;
496 static struct branch *new_branch(const char *name)
498 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
499 struct branch* b = lookup_branch(name);
501 if (b)
502 die("Invalid attempt to create duplicate branch: %s", name);
503 if (check_ref_format(name))
504 die("Branch name doesn't conform to GIT standards: %s", name);
506 b = pool_calloc(1, sizeof(struct branch));
507 b->name = pool_strdup(name);
508 b->table_next_branch = branch_table[hc];
509 b->branch_tree.versions[0].mode = S_IFDIR;
510 b->branch_tree.versions[1].mode = S_IFDIR;
511 b->pack_id = MAX_PACK_ID;
512 branch_table[hc] = b;
513 branch_count++;
514 return b;
517 static unsigned int hc_entries(unsigned int cnt)
519 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
520 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
523 static struct tree_content *new_tree_content(unsigned int cnt)
525 struct avail_tree_content *f, *l = NULL;
526 struct tree_content *t;
527 unsigned int hc = hc_entries(cnt);
529 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
530 if (f->entry_capacity >= cnt)
531 break;
533 if (f) {
534 if (l)
535 l->next_avail = f->next_avail;
536 else
537 avail_tree_table[hc] = f->next_avail;
538 } else {
539 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
540 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
541 f->entry_capacity = cnt;
544 t = (struct tree_content*)f;
545 t->entry_count = 0;
546 t->delta_depth = 0;
547 return t;
550 static void release_tree_entry(struct tree_entry *e);
551 static void release_tree_content(struct tree_content *t)
553 struct avail_tree_content *f = (struct avail_tree_content*)t;
554 unsigned int hc = hc_entries(f->entry_capacity);
555 f->next_avail = avail_tree_table[hc];
556 avail_tree_table[hc] = f;
559 static void release_tree_content_recursive(struct tree_content *t)
561 unsigned int i;
562 for (i = 0; i < t->entry_count; i++)
563 release_tree_entry(t->entries[i]);
564 release_tree_content(t);
567 static struct tree_content *grow_tree_content(
568 struct tree_content *t,
569 int amt)
571 struct tree_content *r = new_tree_content(t->entry_count + amt);
572 r->entry_count = t->entry_count;
573 r->delta_depth = t->delta_depth;
574 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
575 release_tree_content(t);
576 return r;
579 static struct tree_entry *new_tree_entry(void)
581 struct tree_entry *e;
583 if (!avail_tree_entry) {
584 unsigned int n = tree_entry_alloc;
585 total_allocd += n * sizeof(struct tree_entry);
586 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
587 while (n-- > 1) {
588 *((void**)e) = e + 1;
589 e++;
591 *((void**)e) = NULL;
594 e = avail_tree_entry;
595 avail_tree_entry = *((void**)e);
596 return e;
599 static void release_tree_entry(struct tree_entry *e)
601 if (e->tree)
602 release_tree_content_recursive(e->tree);
603 *((void**)e) = avail_tree_entry;
604 avail_tree_entry = e;
607 static void start_packfile(void)
609 static char tmpfile[PATH_MAX];
610 struct packed_git *p;
611 struct pack_header hdr;
612 int pack_fd;
614 snprintf(tmpfile, sizeof(tmpfile),
615 "%s/pack_XXXXXX", get_object_directory());
616 pack_fd = mkstemp(tmpfile);
617 if (pack_fd < 0)
618 die("Can't create %s: %s", tmpfile, strerror(errno));
619 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
620 strcpy(p->pack_name, tmpfile);
621 p->pack_fd = pack_fd;
623 hdr.hdr_signature = htonl(PACK_SIGNATURE);
624 hdr.hdr_version = htonl(2);
625 hdr.hdr_entries = 0;
626 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
628 pack_data = p;
629 pack_size = sizeof(hdr);
630 object_count = 0;
632 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
633 all_packs[pack_id] = p;
636 static void fixup_header_footer(void)
638 static const int buf_sz = 128 * 1024;
639 int pack_fd = pack_data->pack_fd;
640 SHA_CTX c;
641 struct pack_header hdr;
642 char *buf;
644 if (lseek(pack_fd, 0, SEEK_SET) != 0)
645 die("Failed seeking to start: %s", strerror(errno));
646 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
647 die("Unable to reread header of %s", pack_data->pack_name);
648 if (lseek(pack_fd, 0, SEEK_SET) != 0)
649 die("Failed seeking to start: %s", strerror(errno));
650 hdr.hdr_entries = htonl(object_count);
651 write_or_die(pack_fd, &hdr, sizeof(hdr));
653 SHA1_Init(&c);
654 SHA1_Update(&c, &hdr, sizeof(hdr));
656 buf = xmalloc(buf_sz);
657 for (;;) {
658 size_t n = xread(pack_fd, buf, buf_sz);
659 if (!n)
660 break;
661 if (n < 0)
662 die("Failed to checksum %s", pack_data->pack_name);
663 SHA1_Update(&c, buf, n);
665 free(buf);
667 SHA1_Final(pack_data->sha1, &c);
668 write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
669 close(pack_fd);
672 static int oecmp (const void *a_, const void *b_)
674 struct object_entry *a = *((struct object_entry**)a_);
675 struct object_entry *b = *((struct object_entry**)b_);
676 return hashcmp(a->sha1, b->sha1);
679 static char *create_index(void)
681 static char tmpfile[PATH_MAX];
682 SHA_CTX ctx;
683 struct sha1file *f;
684 struct object_entry **idx, **c, **last, *e;
685 struct object_entry_pool *o;
686 uint32_t array[256];
687 int i, idx_fd;
689 /* Build the sorted table of object IDs. */
690 idx = xmalloc(object_count * sizeof(struct object_entry*));
691 c = idx;
692 for (o = blocks; o; o = o->next_pool)
693 for (e = o->next_free; e-- != o->entries;)
694 if (pack_id == e->pack_id)
695 *c++ = e;
696 last = idx + object_count;
697 if (c != last)
698 die("internal consistency error creating the index");
699 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
701 /* Generate the fan-out array. */
702 c = idx;
703 for (i = 0; i < 256; i++) {
704 struct object_entry **next = c;;
705 while (next < last) {
706 if ((*next)->sha1[0] != i)
707 break;
708 next++;
710 array[i] = htonl(next - idx);
711 c = next;
714 snprintf(tmpfile, sizeof(tmpfile),
715 "%s/index_XXXXXX", get_object_directory());
716 idx_fd = mkstemp(tmpfile);
717 if (idx_fd < 0)
718 die("Can't create %s: %s", tmpfile, strerror(errno));
719 f = sha1fd(idx_fd, tmpfile);
720 sha1write(f, array, 256 * sizeof(int));
721 SHA1_Init(&ctx);
722 for (c = idx; c != last; c++) {
723 uint32_t offset = htonl((*c)->offset);
724 sha1write(f, &offset, 4);
725 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
726 SHA1_Update(&ctx, (*c)->sha1, 20);
728 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
729 sha1close(f, NULL, 1);
730 free(idx);
731 SHA1_Final(pack_data->sha1, &ctx);
732 return tmpfile;
735 static char *keep_pack(char *curr_index_name)
737 static char name[PATH_MAX];
738 static char *keep_msg = "fast-import";
739 int keep_fd;
741 chmod(pack_data->pack_name, 0444);
742 chmod(curr_index_name, 0444);
744 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
745 get_object_directory(), sha1_to_hex(pack_data->sha1));
746 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
747 if (keep_fd < 0)
748 die("cannot create keep file");
749 write(keep_fd, keep_msg, strlen(keep_msg));
750 close(keep_fd);
752 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
753 get_object_directory(), sha1_to_hex(pack_data->sha1));
754 if (move_temp_to_file(pack_data->pack_name, name))
755 die("cannot store pack file");
757 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
758 get_object_directory(), sha1_to_hex(pack_data->sha1));
759 if (move_temp_to_file(curr_index_name, name))
760 die("cannot store index file");
761 return name;
764 static void unkeep_all_packs(void)
766 static char name[PATH_MAX];
767 int k;
769 for (k = 0; k < pack_id; k++) {
770 struct packed_git *p = all_packs[k];
771 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
772 get_object_directory(), sha1_to_hex(p->sha1));
773 unlink(name);
777 static void end_packfile(void)
779 struct packed_git *old_p = pack_data, *new_p;
781 if (object_count) {
782 char *idx_name;
783 int i;
784 struct branch *b;
785 struct tag *t;
787 fixup_header_footer();
788 idx_name = keep_pack(create_index());
790 /* Register the packfile with core git's machinary. */
791 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
792 if (!new_p)
793 die("core git rejected index %s", idx_name);
794 new_p->windows = old_p->windows;
795 all_packs[pack_id] = new_p;
796 install_packed_git(new_p);
798 /* Print the boundary */
799 fprintf(stdout, "%s:", new_p->pack_name);
800 for (i = 0; i < branch_table_sz; i++) {
801 for (b = branch_table[i]; b; b = b->table_next_branch) {
802 if (b->pack_id == pack_id)
803 fprintf(stdout, " %s", sha1_to_hex(b->sha1));
806 for (t = first_tag; t; t = t->next_tag) {
807 if (t->pack_id == pack_id)
808 fprintf(stdout, " %s", sha1_to_hex(t->sha1));
810 fputc('\n', stdout);
812 pack_id++;
814 else
815 unlink(old_p->pack_name);
816 free(old_p);
818 /* We can't carry a delta across packfiles. */
819 free(last_blob.data);
820 last_blob.data = NULL;
821 last_blob.len = 0;
822 last_blob.offset = 0;
823 last_blob.depth = 0;
826 static void checkpoint(void)
828 end_packfile();
829 start_packfile();
832 static size_t encode_header(
833 enum object_type type,
834 size_t size,
835 unsigned char *hdr)
837 int n = 1;
838 unsigned char c;
840 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
841 die("bad type %d", type);
843 c = (type << 4) | (size & 15);
844 size >>= 4;
845 while (size) {
846 *hdr++ = c | 0x80;
847 c = size & 0x7f;
848 size >>= 7;
849 n++;
851 *hdr = c;
852 return n;
855 static int store_object(
856 enum object_type type,
857 void *dat,
858 size_t datlen,
859 struct last_object *last,
860 unsigned char *sha1out,
861 uintmax_t mark)
863 void *out, *delta;
864 struct object_entry *e;
865 unsigned char hdr[96];
866 unsigned char sha1[20];
867 unsigned long hdrlen, deltalen;
868 SHA_CTX c;
869 z_stream s;
871 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
872 SHA1_Init(&c);
873 SHA1_Update(&c, hdr, hdrlen);
874 SHA1_Update(&c, dat, datlen);
875 SHA1_Final(sha1, &c);
876 if (sha1out)
877 hashcpy(sha1out, sha1);
879 e = insert_object(sha1);
880 if (mark)
881 insert_mark(mark, e);
882 if (e->offset) {
883 duplicate_count_by_type[type]++;
884 return 1;
887 if (last && last->data && last->depth < max_depth) {
888 delta = diff_delta(last->data, last->len,
889 dat, datlen,
890 &deltalen, 0);
891 if (delta && deltalen >= datlen) {
892 free(delta);
893 delta = NULL;
895 } else
896 delta = NULL;
898 memset(&s, 0, sizeof(s));
899 deflateInit(&s, zlib_compression_level);
900 if (delta) {
901 s.next_in = delta;
902 s.avail_in = deltalen;
903 } else {
904 s.next_in = dat;
905 s.avail_in = datlen;
907 s.avail_out = deflateBound(&s, s.avail_in);
908 s.next_out = out = xmalloc(s.avail_out);
909 while (deflate(&s, Z_FINISH) == Z_OK)
910 /* nothing */;
911 deflateEnd(&s);
913 /* Determine if we should auto-checkpoint. */
914 if ((pack_size + 60 + s.total_out) > max_packsize
915 || (pack_size + 60 + s.total_out) < pack_size) {
917 /* This new object needs to *not* have the current pack_id. */
918 e->pack_id = pack_id + 1;
919 checkpoint();
921 /* We cannot carry a delta into the new pack. */
922 if (delta) {
923 free(delta);
924 delta = NULL;
926 memset(&s, 0, sizeof(s));
927 deflateInit(&s, zlib_compression_level);
928 s.next_in = dat;
929 s.avail_in = datlen;
930 s.avail_out = deflateBound(&s, s.avail_in);
931 s.next_out = out = xrealloc(out, s.avail_out);
932 while (deflate(&s, Z_FINISH) == Z_OK)
933 /* nothing */;
934 deflateEnd(&s);
938 e->type = type;
939 e->pack_id = pack_id;
940 e->offset = pack_size;
941 object_count++;
942 object_count_by_type[type]++;
944 if (delta) {
945 unsigned long ofs = e->offset - last->offset;
946 unsigned pos = sizeof(hdr) - 1;
948 delta_count_by_type[type]++;
949 last->depth++;
951 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
952 write_or_die(pack_data->pack_fd, hdr, hdrlen);
953 pack_size += hdrlen;
955 hdr[pos] = ofs & 127;
956 while (ofs >>= 7)
957 hdr[--pos] = 128 | (--ofs & 127);
958 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
959 pack_size += sizeof(hdr) - pos;
960 } else {
961 if (last)
962 last->depth = 0;
963 hdrlen = encode_header(type, datlen, hdr);
964 write_or_die(pack_data->pack_fd, hdr, hdrlen);
965 pack_size += hdrlen;
968 write_or_die(pack_data->pack_fd, out, s.total_out);
969 pack_size += s.total_out;
971 free(out);
972 free(delta);
973 if (last) {
974 if (!last->no_free)
975 free(last->data);
976 last->data = dat;
977 last->offset = e->offset;
978 last->len = datlen;
980 return 0;
983 static void *gfi_unpack_entry(
984 struct object_entry *oe,
985 unsigned long *sizep)
987 static char type[20];
988 struct packed_git *p = all_packs[oe->pack_id];
989 if (p == pack_data)
990 p->pack_size = pack_size + 20;
991 return unpack_entry(p, oe->offset, type, sizep);
994 static const char *get_mode(const char *str, uint16_t *modep)
996 unsigned char c;
997 uint16_t mode = 0;
999 while ((c = *str++) != ' ') {
1000 if (c < '0' || c > '7')
1001 return NULL;
1002 mode = (mode << 3) + (c - '0');
1004 *modep = mode;
1005 return str;
1008 static void load_tree(struct tree_entry *root)
1010 unsigned char* sha1 = root->versions[1].sha1;
1011 struct object_entry *myoe;
1012 struct tree_content *t;
1013 unsigned long size;
1014 char *buf;
1015 const char *c;
1017 root->tree = t = new_tree_content(8);
1018 if (is_null_sha1(sha1))
1019 return;
1021 myoe = find_object(sha1);
1022 if (myoe) {
1023 if (myoe->type != OBJ_TREE)
1024 die("Not a tree: %s", sha1_to_hex(sha1));
1025 t->delta_depth = 0;
1026 buf = gfi_unpack_entry(myoe, &size);
1027 } else {
1028 char type[20];
1029 buf = read_sha1_file(sha1, type, &size);
1030 if (!buf || strcmp(type, tree_type))
1031 die("Can't load tree %s", sha1_to_hex(sha1));
1034 c = buf;
1035 while (c != (buf + size)) {
1036 struct tree_entry *e = new_tree_entry();
1038 if (t->entry_count == t->entry_capacity)
1039 root->tree = t = grow_tree_content(t, 8);
1040 t->entries[t->entry_count++] = e;
1042 e->tree = NULL;
1043 c = get_mode(c, &e->versions[1].mode);
1044 if (!c)
1045 die("Corrupt mode in %s", sha1_to_hex(sha1));
1046 e->versions[0].mode = e->versions[1].mode;
1047 e->name = to_atom(c, (unsigned short)strlen(c));
1048 c += e->name->str_len + 1;
1049 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1050 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1051 c += 20;
1053 free(buf);
1056 static int tecmp0 (const void *_a, const void *_b)
1058 struct tree_entry *a = *((struct tree_entry**)_a);
1059 struct tree_entry *b = *((struct tree_entry**)_b);
1060 return base_name_compare(
1061 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1062 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1065 static int tecmp1 (const void *_a, const void *_b)
1067 struct tree_entry *a = *((struct tree_entry**)_a);
1068 struct tree_entry *b = *((struct tree_entry**)_b);
1069 return base_name_compare(
1070 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1071 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1074 static void mktree(struct tree_content *t,
1075 int v,
1076 unsigned long *szp,
1077 struct dbuf *b)
1079 size_t maxlen = 0;
1080 unsigned int i;
1081 char *c;
1083 if (!v)
1084 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1085 else
1086 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1088 for (i = 0; i < t->entry_count; i++) {
1089 if (t->entries[i]->versions[v].mode)
1090 maxlen += t->entries[i]->name->str_len + 34;
1093 size_dbuf(b, maxlen);
1094 c = b->buffer;
1095 for (i = 0; i < t->entry_count; i++) {
1096 struct tree_entry *e = t->entries[i];
1097 if (!e->versions[v].mode)
1098 continue;
1099 c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1100 *c++ = ' ';
1101 strcpy(c, e->name->str_dat);
1102 c += e->name->str_len + 1;
1103 hashcpy((unsigned char*)c, e->versions[v].sha1);
1104 c += 20;
1106 *szp = c - (char*)b->buffer;
1109 static void store_tree(struct tree_entry *root)
1111 struct tree_content *t = root->tree;
1112 unsigned int i, j, del;
1113 unsigned long new_len;
1114 struct last_object lo;
1115 struct object_entry *le;
1117 if (!is_null_sha1(root->versions[1].sha1))
1118 return;
1120 for (i = 0; i < t->entry_count; i++) {
1121 if (t->entries[i]->tree)
1122 store_tree(t->entries[i]);
1125 le = find_object(root->versions[0].sha1);
1126 if (!S_ISDIR(root->versions[0].mode)
1127 || !le
1128 || le->pack_id != pack_id) {
1129 lo.data = NULL;
1130 lo.depth = 0;
1131 } else {
1132 mktree(t, 0, &lo.len, &old_tree);
1133 lo.data = old_tree.buffer;
1134 lo.offset = le->offset;
1135 lo.depth = t->delta_depth;
1136 lo.no_free = 1;
1139 mktree(t, 1, &new_len, &new_tree);
1140 store_object(OBJ_TREE, new_tree.buffer, new_len,
1141 &lo, root->versions[1].sha1, 0);
1143 t->delta_depth = lo.depth;
1144 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1145 struct tree_entry *e = t->entries[i];
1146 if (e->versions[1].mode) {
1147 e->versions[0].mode = e->versions[1].mode;
1148 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1149 t->entries[j++] = e;
1150 } else {
1151 release_tree_entry(e);
1152 del++;
1155 t->entry_count -= del;
1158 static int tree_content_set(
1159 struct tree_entry *root,
1160 const char *p,
1161 const unsigned char *sha1,
1162 const uint16_t mode)
1164 struct tree_content *t = root->tree;
1165 const char *slash1;
1166 unsigned int i, n;
1167 struct tree_entry *e;
1169 slash1 = strchr(p, '/');
1170 if (slash1)
1171 n = slash1 - p;
1172 else
1173 n = strlen(p);
1175 for (i = 0; i < t->entry_count; i++) {
1176 e = t->entries[i];
1177 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1178 if (!slash1) {
1179 if (e->versions[1].mode == mode
1180 && !hashcmp(e->versions[1].sha1, sha1))
1181 return 0;
1182 e->versions[1].mode = mode;
1183 hashcpy(e->versions[1].sha1, sha1);
1184 if (e->tree) {
1185 release_tree_content_recursive(e->tree);
1186 e->tree = NULL;
1188 hashclr(root->versions[1].sha1);
1189 return 1;
1191 if (!S_ISDIR(e->versions[1].mode)) {
1192 e->tree = new_tree_content(8);
1193 e->versions[1].mode = S_IFDIR;
1195 if (!e->tree)
1196 load_tree(e);
1197 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1198 hashclr(root->versions[1].sha1);
1199 return 1;
1201 return 0;
1205 if (t->entry_count == t->entry_capacity)
1206 root->tree = t = grow_tree_content(t, 8);
1207 e = new_tree_entry();
1208 e->name = to_atom(p, (unsigned short)n);
1209 e->versions[0].mode = 0;
1210 hashclr(e->versions[0].sha1);
1211 t->entries[t->entry_count++] = e;
1212 if (slash1) {
1213 e->tree = new_tree_content(8);
1214 e->versions[1].mode = S_IFDIR;
1215 tree_content_set(e, slash1 + 1, sha1, mode);
1216 } else {
1217 e->tree = NULL;
1218 e->versions[1].mode = mode;
1219 hashcpy(e->versions[1].sha1, sha1);
1221 hashclr(root->versions[1].sha1);
1222 return 1;
1225 static int tree_content_remove(struct tree_entry *root, const char *p)
1227 struct tree_content *t = root->tree;
1228 const char *slash1;
1229 unsigned int i, n;
1230 struct tree_entry *e;
1232 slash1 = strchr(p, '/');
1233 if (slash1)
1234 n = slash1 - p;
1235 else
1236 n = strlen(p);
1238 for (i = 0; i < t->entry_count; i++) {
1239 e = t->entries[i];
1240 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1241 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1242 goto del_entry;
1243 if (!e->tree)
1244 load_tree(e);
1245 if (tree_content_remove(e, slash1 + 1)) {
1246 for (n = 0; n < e->tree->entry_count; n++) {
1247 if (e->tree->entries[n]->versions[1].mode) {
1248 hashclr(root->versions[1].sha1);
1249 return 1;
1252 goto del_entry;
1254 return 0;
1257 return 0;
1259 del_entry:
1260 if (e->tree) {
1261 release_tree_content_recursive(e->tree);
1262 e->tree = NULL;
1264 e->versions[1].mode = 0;
1265 hashclr(e->versions[1].sha1);
1266 hashclr(root->versions[1].sha1);
1267 return 1;
1270 static void dump_branches(void)
1272 static const char *msg = "fast-import";
1273 unsigned int i;
1274 struct branch *b;
1275 struct ref_lock *lock;
1277 for (i = 0; i < branch_table_sz; i++) {
1278 for (b = branch_table[i]; b; b = b->table_next_branch) {
1279 lock = lock_any_ref_for_update(b->name, NULL);
1280 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1281 die("Can't write %s", b->name);
1286 static void dump_tags(void)
1288 static const char *msg = "fast-import";
1289 struct tag *t;
1290 struct ref_lock *lock;
1291 char path[PATH_MAX];
1293 for (t = first_tag; t; t = t->next_tag) {
1294 sprintf(path, "refs/tags/%s", t->name);
1295 lock = lock_any_ref_for_update(path, NULL);
1296 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1297 die("Can't write %s", path);
1301 static void dump_marks_helper(FILE *f,
1302 uintmax_t base,
1303 struct mark_set *m)
1305 uintmax_t k;
1306 if (m->shift) {
1307 for (k = 0; k < 1024; k++) {
1308 if (m->data.sets[k])
1309 dump_marks_helper(f, (base + k) << m->shift,
1310 m->data.sets[k]);
1312 } else {
1313 for (k = 0; k < 1024; k++) {
1314 if (m->data.marked[k])
1315 fprintf(f, ":%ju %s\n", base + k,
1316 sha1_to_hex(m->data.marked[k]->sha1));
1321 static void dump_marks(void)
1323 if (mark_file)
1325 FILE *f = fopen(mark_file, "w");
1326 dump_marks_helper(f, 0, marks);
1327 fclose(f);
1331 static void read_next_command(void)
1333 read_line(&command_buf, stdin, '\n');
1336 static void cmd_mark(void)
1338 if (!strncmp("mark :", command_buf.buf, 6)) {
1339 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1340 read_next_command();
1342 else
1343 next_mark = 0;
1346 static void *cmd_data (size_t *size)
1348 size_t length;
1349 char *buffer;
1351 if (strncmp("data ", command_buf.buf, 5))
1352 die("Expected 'data n' command, found: %s", command_buf.buf);
1354 if (!strncmp("<<", command_buf.buf + 5, 2)) {
1355 char *term = xstrdup(command_buf.buf + 5 + 2);
1356 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1357 length = 0;
1358 buffer = xmalloc(sz);
1359 for (;;) {
1360 read_next_command();
1361 if (command_buf.eof)
1362 die("EOF in data (terminator '%s' not found)", term);
1363 if (term_len == command_buf.len
1364 && !strcmp(term, command_buf.buf))
1365 break;
1366 if (sz < (length + command_buf.len)) {
1367 sz = sz * 3 / 2 + 16;
1368 if (sz < (length + command_buf.len))
1369 sz = length + command_buf.len;
1370 buffer = xrealloc(buffer, sz);
1372 memcpy(buffer + length,
1373 command_buf.buf,
1374 command_buf.len - 1);
1375 length += command_buf.len - 1;
1376 buffer[length++] = '\n';
1378 free(term);
1380 else {
1381 size_t n = 0;
1382 length = strtoul(command_buf.buf + 5, NULL, 10);
1383 buffer = xmalloc(length);
1384 while (n < length) {
1385 size_t s = fread(buffer + n, 1, length - n, stdin);
1386 if (!s && feof(stdin))
1387 die("EOF in data (%lu bytes remaining)", length - n);
1388 n += s;
1392 if (fgetc(stdin) != '\n')
1393 die("An lf did not trail the binary data as expected.");
1395 *size = length;
1396 return buffer;
1399 static void cmd_new_blob(void)
1401 size_t l;
1402 void *d;
1404 read_next_command();
1405 cmd_mark();
1406 d = cmd_data(&l);
1408 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1409 free(d);
1412 static void unload_one_branch(void)
1414 while (cur_active_branches
1415 && cur_active_branches >= max_active_branches) {
1416 unsigned long min_commit = ULONG_MAX;
1417 struct branch *e, *l = NULL, *p = NULL;
1419 for (e = active_branches; e; e = e->active_next_branch) {
1420 if (e->last_commit < min_commit) {
1421 p = l;
1422 min_commit = e->last_commit;
1424 l = e;
1427 if (p) {
1428 e = p->active_next_branch;
1429 p->active_next_branch = e->active_next_branch;
1430 } else {
1431 e = active_branches;
1432 active_branches = e->active_next_branch;
1434 e->active_next_branch = NULL;
1435 if (e->branch_tree.tree) {
1436 release_tree_content_recursive(e->branch_tree.tree);
1437 e->branch_tree.tree = NULL;
1439 cur_active_branches--;
1443 static void load_branch(struct branch *b)
1445 load_tree(&b->branch_tree);
1446 b->active_next_branch = active_branches;
1447 active_branches = b;
1448 cur_active_branches++;
1449 branch_load_count++;
1452 static void file_change_m(struct branch *b)
1454 const char *p = command_buf.buf + 2;
1455 char *p_uq;
1456 const char *endp;
1457 struct object_entry *oe = oe;
1458 unsigned char sha1[20];
1459 uint16_t mode, inline_data = 0;
1460 char type[20];
1462 p = get_mode(p, &mode);
1463 if (!p)
1464 die("Corrupt mode: %s", command_buf.buf);
1465 switch (mode) {
1466 case S_IFREG | 0644:
1467 case S_IFREG | 0755:
1468 case S_IFLNK:
1469 case 0644:
1470 case 0755:
1471 /* ok */
1472 break;
1473 default:
1474 die("Corrupt mode: %s", command_buf.buf);
1477 if (*p == ':') {
1478 char *x;
1479 oe = find_mark(strtoumax(p + 1, &x, 10));
1480 hashcpy(sha1, oe->sha1);
1481 p = x;
1482 } else if (!strncmp("inline", p, 6)) {
1483 inline_data = 1;
1484 p += 6;
1485 } else {
1486 if (get_sha1_hex(p, sha1))
1487 die("Invalid SHA1: %s", command_buf.buf);
1488 oe = find_object(sha1);
1489 p += 40;
1491 if (*p++ != ' ')
1492 die("Missing space after SHA1: %s", command_buf.buf);
1494 p_uq = unquote_c_style(p, &endp);
1495 if (p_uq) {
1496 if (*endp)
1497 die("Garbage after path in: %s", command_buf.buf);
1498 p = p_uq;
1501 if (inline_data) {
1502 size_t l;
1503 void *d;
1504 if (!p_uq)
1505 p = p_uq = xstrdup(p);
1506 read_next_command();
1507 d = cmd_data(&l);
1508 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1509 free(d);
1510 } else if (oe) {
1511 if (oe->type != OBJ_BLOB)
1512 die("Not a blob (actually a %s): %s",
1513 command_buf.buf, type_names[oe->type]);
1514 } else {
1515 if (sha1_object_info(sha1, type, NULL))
1516 die("Blob not found: %s", command_buf.buf);
1517 if (strcmp(blob_type, type))
1518 die("Not a blob (actually a %s): %s",
1519 command_buf.buf, type);
1522 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1523 free(p_uq);
1526 static void file_change_d(struct branch *b)
1528 const char *p = command_buf.buf + 2;
1529 char *p_uq;
1530 const char *endp;
1532 p_uq = unquote_c_style(p, &endp);
1533 if (p_uq) {
1534 if (*endp)
1535 die("Garbage after path in: %s", command_buf.buf);
1536 p = p_uq;
1538 tree_content_remove(&b->branch_tree, p);
1539 free(p_uq);
1542 static void cmd_from(struct branch *b)
1544 const char *from;
1545 struct branch *s;
1547 if (strncmp("from ", command_buf.buf, 5))
1548 return;
1550 if (b->last_commit)
1551 die("Can't reinitailize branch %s", b->name);
1553 from = strchr(command_buf.buf, ' ') + 1;
1554 s = lookup_branch(from);
1555 if (b == s)
1556 die("Can't create a branch from itself: %s", b->name);
1557 else if (s) {
1558 unsigned char *t = s->branch_tree.versions[1].sha1;
1559 hashcpy(b->sha1, s->sha1);
1560 hashcpy(b->branch_tree.versions[0].sha1, t);
1561 hashcpy(b->branch_tree.versions[1].sha1, t);
1562 } else if (*from == ':') {
1563 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1564 struct object_entry *oe = find_mark(idnum);
1565 unsigned long size;
1566 char *buf;
1567 if (oe->type != OBJ_COMMIT)
1568 die("Mark :%ju not a commit", idnum);
1569 hashcpy(b->sha1, oe->sha1);
1570 buf = gfi_unpack_entry(oe, &size);
1571 if (!buf || size < 46)
1572 die("Not a valid commit: %s", from);
1573 if (memcmp("tree ", buf, 5)
1574 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1575 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1576 free(buf);
1577 hashcpy(b->branch_tree.versions[0].sha1,
1578 b->branch_tree.versions[1].sha1);
1579 } else if (!get_sha1(from, b->sha1)) {
1580 if (is_null_sha1(b->sha1)) {
1581 hashclr(b->branch_tree.versions[0].sha1);
1582 hashclr(b->branch_tree.versions[1].sha1);
1583 } else {
1584 unsigned long size;
1585 char *buf;
1587 buf = read_object_with_reference(b->sha1,
1588 type_names[OBJ_COMMIT], &size, b->sha1);
1589 if (!buf || size < 46)
1590 die("Not a valid commit: %s", from);
1591 if (memcmp("tree ", buf, 5)
1592 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1593 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1594 free(buf);
1595 hashcpy(b->branch_tree.versions[0].sha1,
1596 b->branch_tree.versions[1].sha1);
1598 } else
1599 die("Invalid ref name or SHA1 expression: %s", from);
1601 read_next_command();
1604 static struct hash_list *cmd_merge(unsigned int *count)
1606 struct hash_list *list = NULL, *n, *e = e;
1607 const char *from;
1608 struct branch *s;
1610 *count = 0;
1611 while (!strncmp("merge ", command_buf.buf, 6)) {
1612 from = strchr(command_buf.buf, ' ') + 1;
1613 n = xmalloc(sizeof(*n));
1614 s = lookup_branch(from);
1615 if (s)
1616 hashcpy(n->sha1, s->sha1);
1617 else if (*from == ':') {
1618 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1619 struct object_entry *oe = find_mark(idnum);
1620 if (oe->type != OBJ_COMMIT)
1621 die("Mark :%ju not a commit", idnum);
1622 hashcpy(n->sha1, oe->sha1);
1623 } else if (get_sha1(from, n->sha1))
1624 die("Invalid ref name or SHA1 expression: %s", from);
1626 n->next = NULL;
1627 if (list)
1628 e->next = n;
1629 else
1630 list = n;
1631 e = n;
1632 (*count)++;
1633 read_next_command();
1635 return list;
1638 static void cmd_new_commit(void)
1640 struct branch *b;
1641 void *msg;
1642 size_t msglen;
1643 char *sp;
1644 char *author = NULL;
1645 char *committer = NULL;
1646 struct hash_list *merge_list = NULL;
1647 unsigned int merge_count;
1649 /* Obtain the branch name from the rest of our command */
1650 sp = strchr(command_buf.buf, ' ') + 1;
1651 b = lookup_branch(sp);
1652 if (!b)
1653 b = new_branch(sp);
1655 read_next_command();
1656 cmd_mark();
1657 if (!strncmp("author ", command_buf.buf, 7)) {
1658 author = strdup(command_buf.buf);
1659 read_next_command();
1661 if (!strncmp("committer ", command_buf.buf, 10)) {
1662 committer = strdup(command_buf.buf);
1663 read_next_command();
1665 if (!committer)
1666 die("Expected committer but didn't get one");
1667 msg = cmd_data(&msglen);
1668 read_next_command();
1669 cmd_from(b);
1670 merge_list = cmd_merge(&merge_count);
1672 /* ensure the branch is active/loaded */
1673 if (!b->branch_tree.tree || !max_active_branches) {
1674 unload_one_branch();
1675 load_branch(b);
1678 /* file_change* */
1679 for (;;) {
1680 if (1 == command_buf.len)
1681 break;
1682 else if (!strncmp("M ", command_buf.buf, 2))
1683 file_change_m(b);
1684 else if (!strncmp("D ", command_buf.buf, 2))
1685 file_change_d(b);
1686 else
1687 die("Unsupported file_change: %s", command_buf.buf);
1688 read_next_command();
1691 /* build the tree and the commit */
1692 store_tree(&b->branch_tree);
1693 hashcpy(b->branch_tree.versions[0].sha1,
1694 b->branch_tree.versions[1].sha1);
1695 size_dbuf(&new_data, 97 + msglen
1696 + merge_count * 49
1697 + (author
1698 ? strlen(author) + strlen(committer)
1699 : 2 * strlen(committer)));
1700 sp = new_data.buffer;
1701 sp += sprintf(sp, "tree %s\n",
1702 sha1_to_hex(b->branch_tree.versions[1].sha1));
1703 if (!is_null_sha1(b->sha1))
1704 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1705 while (merge_list) {
1706 struct hash_list *next = merge_list->next;
1707 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1708 free(merge_list);
1709 merge_list = next;
1711 if (author)
1712 sp += sprintf(sp, "%s\n", author);
1713 else
1714 sp += sprintf(sp, "author %s\n", committer + 10);
1715 sp += sprintf(sp, "%s\n\n", committer);
1716 memcpy(sp, msg, msglen);
1717 sp += msglen;
1718 free(author);
1719 free(committer);
1720 free(msg);
1722 if (!store_object(OBJ_COMMIT,
1723 new_data.buffer, sp - (char*)new_data.buffer,
1724 NULL, b->sha1, next_mark))
1725 b->pack_id = pack_id;
1726 b->last_commit = object_count_by_type[OBJ_COMMIT];
1729 static void cmd_new_tag(void)
1731 char *sp;
1732 const char *from;
1733 char *tagger;
1734 struct branch *s;
1735 void *msg;
1736 size_t msglen;
1737 struct tag *t;
1738 uintmax_t from_mark = 0;
1739 unsigned char sha1[20];
1741 /* Obtain the new tag name from the rest of our command */
1742 sp = strchr(command_buf.buf, ' ') + 1;
1743 t = pool_alloc(sizeof(struct tag));
1744 t->next_tag = NULL;
1745 t->name = pool_strdup(sp);
1746 if (last_tag)
1747 last_tag->next_tag = t;
1748 else
1749 first_tag = t;
1750 last_tag = t;
1751 read_next_command();
1753 /* from ... */
1754 if (strncmp("from ", command_buf.buf, 5))
1755 die("Expected from command, got %s", command_buf.buf);
1756 from = strchr(command_buf.buf, ' ') + 1;
1757 s = lookup_branch(from);
1758 if (s) {
1759 hashcpy(sha1, s->sha1);
1760 } else if (*from == ':') {
1761 struct object_entry *oe;
1762 from_mark = strtoumax(from + 1, NULL, 10);
1763 oe = find_mark(from_mark);
1764 if (oe->type != OBJ_COMMIT)
1765 die("Mark :%ju not a commit", from_mark);
1766 hashcpy(sha1, oe->sha1);
1767 } else if (!get_sha1(from, sha1)) {
1768 unsigned long size;
1769 char *buf;
1771 buf = read_object_with_reference(sha1,
1772 type_names[OBJ_COMMIT], &size, sha1);
1773 if (!buf || size < 46)
1774 die("Not a valid commit: %s", from);
1775 free(buf);
1776 } else
1777 die("Invalid ref name or SHA1 expression: %s", from);
1778 read_next_command();
1780 /* tagger ... */
1781 if (strncmp("tagger ", command_buf.buf, 7))
1782 die("Expected tagger command, got %s", command_buf.buf);
1783 tagger = strdup(command_buf.buf);
1785 /* tag payload/message */
1786 read_next_command();
1787 msg = cmd_data(&msglen);
1789 /* build the tag object */
1790 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1791 sp = new_data.buffer;
1792 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1793 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1794 sp += sprintf(sp, "tag %s\n", t->name);
1795 sp += sprintf(sp, "%s\n\n", tagger);
1796 memcpy(sp, msg, msglen);
1797 sp += msglen;
1798 free(tagger);
1799 free(msg);
1801 if (store_object(OBJ_TAG, new_data.buffer,
1802 sp - (char*)new_data.buffer,
1803 NULL, t->sha1, 0))
1804 t->pack_id = MAX_PACK_ID;
1805 else
1806 t->pack_id = pack_id;
1809 static void cmd_reset_branch(void)
1811 struct branch *b;
1812 char *sp;
1814 /* Obtain the branch name from the rest of our command */
1815 sp = strchr(command_buf.buf, ' ') + 1;
1816 b = lookup_branch(sp);
1817 if (b) {
1818 b->last_commit = 0;
1819 if (b->branch_tree.tree) {
1820 release_tree_content_recursive(b->branch_tree.tree);
1821 b->branch_tree.tree = NULL;
1824 else
1825 b = new_branch(sp);
1826 read_next_command();
1827 cmd_from(b);
1830 static void cmd_checkpoint(void)
1832 if (object_count)
1833 checkpoint();
1834 read_next_command();
1837 static const char fast_import_usage[] =
1838 "git-fast-import [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1840 int main(int argc, const char **argv)
1842 int i;
1843 uintmax_t total_count, duplicate_count;
1845 git_config(git_default_config);
1847 for (i = 1; i < argc; i++) {
1848 const char *a = argv[i];
1850 if (*a != '-' || !strcmp(a, "--"))
1851 break;
1852 else if (!strncmp(a, "--max-pack-size=", 16))
1853 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
1854 else if (!strncmp(a, "--depth=", 8))
1855 max_depth = strtoul(a + 8, NULL, 0);
1856 else if (!strncmp(a, "--active-branches=", 18))
1857 max_active_branches = strtoul(a + 18, NULL, 0);
1858 else if (!strncmp(a, "--export-marks=", 15))
1859 mark_file = a + 15;
1860 else
1861 die("unknown option %s", a);
1863 if (i != argc)
1864 usage(fast_import_usage);
1866 alloc_objects(object_entry_alloc);
1867 strbuf_init(&command_buf);
1869 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1870 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1871 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1872 marks = pool_calloc(1, sizeof(struct mark_set));
1874 start_packfile();
1875 for (;;) {
1876 read_next_command();
1877 if (command_buf.eof)
1878 break;
1879 else if (!strcmp("blob", command_buf.buf))
1880 cmd_new_blob();
1881 else if (!strncmp("commit ", command_buf.buf, 7))
1882 cmd_new_commit();
1883 else if (!strncmp("tag ", command_buf.buf, 4))
1884 cmd_new_tag();
1885 else if (!strncmp("reset ", command_buf.buf, 6))
1886 cmd_reset_branch();
1887 else if (!strcmp("checkpoint", command_buf.buf))
1888 cmd_checkpoint();
1889 else
1890 die("Unsupported command: %s", command_buf.buf);
1892 end_packfile();
1894 dump_branches();
1895 dump_tags();
1896 unkeep_all_packs();
1897 dump_marks();
1899 total_count = 0;
1900 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
1901 total_count += object_count_by_type[i];
1902 duplicate_count = 0;
1903 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
1904 duplicate_count += duplicate_count_by_type[i];
1906 fprintf(stderr, "%s statistics:\n", argv[0]);
1907 fprintf(stderr, "---------------------------------------------------------------------\n");
1908 fprintf(stderr, "Alloc'd objects: %10ju\n", alloc_count);
1909 fprintf(stderr, "Total objects: %10ju (%10ju duplicates )\n", total_count, duplicate_count);
1910 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]);
1911 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]);
1912 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]);
1913 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]);
1914 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1915 fprintf(stderr, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
1916 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1917 fprintf(stderr, "Memory total: %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1918 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1919 fprintf(stderr, " objects: %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1920 fprintf(stderr, "---------------------------------------------------------------------\n");
1921 pack_report();
1922 fprintf(stderr, "---------------------------------------------------------------------\n");
1923 fprintf(stderr, "\n");
1925 return 0;