git-svn: add a 'rebase' command
[alt-git.git] / fast-import.c
blob5d040fdb001c05daab19d4b6e239512cbdb07aba
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 '>' when lf)?
21 'committer' sp name '<' email '>' when 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_clr | file_del | file_obm | file_inm;
30 file_clr ::= 'deleteall' lf;
31 file_del ::= 'D' sp path_str lf;
32 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
33 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
34 data;
36 new_tag ::= 'tag' sp tag_str lf
37 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
38 'tagger' sp name '<' email '>' when lf
39 tag_msg;
40 tag_msg ::= data;
42 reset_branch ::= 'reset' sp ref_str lf
43 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
44 lf;
46 checkpoint ::= 'checkpoint' lf
47 lf;
49 # note: the first idnum in a stream should be 1 and subsequent
50 # idnums should not have gaps between values as this will cause
51 # the stream parser to reserve space for the gapped values. An
52 # idnum can be updated in the future to a new object by issuing
53 # a new mark directive with the old idnum.
55 mark ::= 'mark' sp idnum lf;
56 data ::= (delimited_data | exact_data)
57 lf;
59 # note: delim may be any string but must not contain lf.
60 # data_line may contain any data but must not be exactly
61 # delim.
62 delimited_data ::= 'data' sp '<<' delim lf
63 (data_line lf)*
64 delim lf;
66 # note: declen indicates the length of binary_data in bytes.
67 # declen does not include the lf preceeding the binary data.
69 exact_data ::= 'data' sp declen lf
70 binary_data;
72 # note: quoted strings are C-style quoting supporting \c for
73 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
74 # is the signed byte value in octal. Note that the only
75 # characters which must actually be escaped to protect the
76 # stream formatting is: \, " and LF. Otherwise these values
77 # are UTF8.
79 ref_str ::= ref;
80 sha1exp_str ::= sha1exp;
81 tag_str ::= tag;
82 path_str ::= path | '"' quoted(path) '"' ;
83 mode ::= '100644' | '644'
84 | '100755' | '755'
85 | '120000'
88 declen ::= # unsigned 32 bit value, ascii base10 notation;
89 bigint ::= # unsigned integer value, ascii base10 notation;
90 binary_data ::= # file content, not interpreted;
92 when ::= raw_when | rfc2822_when;
93 raw_when ::= ts sp tz;
94 rfc2822_when ::= # Valid RFC 2822 date and time;
96 sp ::= # ASCII space character;
97 lf ::= # ASCII newline (LF) character;
99 # note: a colon (':') must precede the numerical value assigned to
100 # an idnum. This is to distinguish it from a ref or tag name as
101 # GIT does not permit ':' in ref or tag strings.
103 idnum ::= ':' bigint;
104 path ::= # GIT style file path, e.g. "a/b/c";
105 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
106 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
107 sha1exp ::= # Any valid GIT SHA1 expression;
108 hexsha1 ::= # SHA1 in hexadecimal format;
110 # note: name and email are UTF8 strings, however name must not
111 # contain '<' or lf and email must not contain any of the
112 # following: '<', '>', lf.
114 name ::= # valid GIT author/committer name;
115 email ::= # valid GIT author/committer email;
116 ts ::= # time since the epoch in seconds, ascii base10 notation;
117 tz ::= # GIT style timezone;
120 #include "builtin.h"
121 #include "cache.h"
122 #include "object.h"
123 #include "blob.h"
124 #include "tree.h"
125 #include "commit.h"
126 #include "delta.h"
127 #include "pack.h"
128 #include "refs.h"
129 #include "csum-file.h"
130 #include "strbuf.h"
131 #include "quote.h"
133 #define PACK_ID_BITS 16
134 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
136 #ifndef PRIuMAX
137 #define PRIuMAX "llu"
138 #endif
140 struct object_entry
142 struct object_entry *next;
143 uint32_t offset;
144 unsigned type : TYPE_BITS;
145 unsigned pack_id : PACK_ID_BITS;
146 unsigned char sha1[20];
149 struct object_entry_pool
151 struct object_entry_pool *next_pool;
152 struct object_entry *next_free;
153 struct object_entry *end;
154 struct object_entry entries[FLEX_ARRAY]; /* more */
157 struct mark_set
159 union {
160 struct object_entry *marked[1024];
161 struct mark_set *sets[1024];
162 } data;
163 unsigned int shift;
166 struct last_object
168 void *data;
169 unsigned long len;
170 uint32_t offset;
171 unsigned int depth;
172 unsigned no_free:1;
175 struct mem_pool
177 struct mem_pool *next_pool;
178 char *next_free;
179 char *end;
180 char space[FLEX_ARRAY]; /* more */
183 struct atom_str
185 struct atom_str *next_atom;
186 unsigned short str_len;
187 char str_dat[FLEX_ARRAY]; /* more */
190 struct tree_content;
191 struct tree_entry
193 struct tree_content *tree;
194 struct atom_str* name;
195 struct tree_entry_ms
197 uint16_t mode;
198 unsigned char sha1[20];
199 } versions[2];
202 struct tree_content
204 unsigned int entry_capacity; /* must match avail_tree_content */
205 unsigned int entry_count;
206 unsigned int delta_depth;
207 struct tree_entry *entries[FLEX_ARRAY]; /* more */
210 struct avail_tree_content
212 unsigned int entry_capacity; /* must match tree_content */
213 struct avail_tree_content *next_avail;
216 struct branch
218 struct branch *table_next_branch;
219 struct branch *active_next_branch;
220 const char *name;
221 struct tree_entry branch_tree;
222 uintmax_t last_commit;
223 unsigned int pack_id;
224 unsigned char sha1[20];
227 struct tag
229 struct tag *next_tag;
230 const char *name;
231 unsigned int pack_id;
232 unsigned char sha1[20];
235 struct dbuf
237 void *buffer;
238 size_t capacity;
241 struct hash_list
243 struct hash_list *next;
244 unsigned char sha1[20];
247 typedef enum {
248 WHENSPEC_RAW = 1,
249 WHENSPEC_RFC2822,
250 WHENSPEC_NOW,
251 } whenspec_type;
253 /* Configured limits on output */
254 static unsigned long max_depth = 10;
255 static unsigned long max_packsize = (1LL << 32) - 1;
256 static int force_update;
258 /* Stats and misc. counters */
259 static uintmax_t alloc_count;
260 static uintmax_t marks_set_count;
261 static uintmax_t object_count_by_type[1 << TYPE_BITS];
262 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
263 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
264 static unsigned long object_count;
265 static unsigned long branch_count;
266 static unsigned long branch_load_count;
267 static int failure;
268 static FILE *pack_edges;
270 /* Memory pools */
271 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
272 static size_t total_allocd;
273 static struct mem_pool *mem_pool;
275 /* Atom management */
276 static unsigned int atom_table_sz = 4451;
277 static unsigned int atom_cnt;
278 static struct atom_str **atom_table;
280 /* The .pack file being generated */
281 static unsigned int pack_id;
282 static struct packed_git *pack_data;
283 static struct packed_git **all_packs;
284 static unsigned long pack_size;
286 /* Table of objects we've written. */
287 static unsigned int object_entry_alloc = 5000;
288 static struct object_entry_pool *blocks;
289 static struct object_entry *object_table[1 << 16];
290 static struct mark_set *marks;
291 static const char* mark_file;
293 /* Our last blob */
294 static struct last_object last_blob;
296 /* Tree management */
297 static unsigned int tree_entry_alloc = 1000;
298 static void *avail_tree_entry;
299 static unsigned int avail_tree_table_sz = 100;
300 static struct avail_tree_content **avail_tree_table;
301 static struct dbuf old_tree;
302 static struct dbuf new_tree;
304 /* Branch data */
305 static unsigned long max_active_branches = 5;
306 static unsigned long cur_active_branches;
307 static unsigned long branch_table_sz = 1039;
308 static struct branch **branch_table;
309 static struct branch *active_branches;
311 /* Tag data */
312 static struct tag *first_tag;
313 static struct tag *last_tag;
315 /* Input stream parsing */
316 static whenspec_type whenspec = WHENSPEC_RAW;
317 static struct strbuf command_buf;
318 static uintmax_t next_mark;
319 static struct dbuf new_data;
322 static void alloc_objects(unsigned int cnt)
324 struct object_entry_pool *b;
326 b = xmalloc(sizeof(struct object_entry_pool)
327 + cnt * sizeof(struct object_entry));
328 b->next_pool = blocks;
329 b->next_free = b->entries;
330 b->end = b->entries + cnt;
331 blocks = b;
332 alloc_count += cnt;
335 static struct object_entry *new_object(unsigned char *sha1)
337 struct object_entry *e;
339 if (blocks->next_free == blocks->end)
340 alloc_objects(object_entry_alloc);
342 e = blocks->next_free++;
343 hashcpy(e->sha1, sha1);
344 return e;
347 static struct object_entry *find_object(unsigned char *sha1)
349 unsigned int h = sha1[0] << 8 | sha1[1];
350 struct object_entry *e;
351 for (e = object_table[h]; e; e = e->next)
352 if (!hashcmp(sha1, e->sha1))
353 return e;
354 return NULL;
357 static struct object_entry *insert_object(unsigned char *sha1)
359 unsigned int h = sha1[0] << 8 | sha1[1];
360 struct object_entry *e = object_table[h];
361 struct object_entry *p = NULL;
363 while (e) {
364 if (!hashcmp(sha1, e->sha1))
365 return e;
366 p = e;
367 e = e->next;
370 e = new_object(sha1);
371 e->next = NULL;
372 e->offset = 0;
373 if (p)
374 p->next = e;
375 else
376 object_table[h] = e;
377 return e;
380 static unsigned int hc_str(const char *s, size_t len)
382 unsigned int r = 0;
383 while (len-- > 0)
384 r = r * 31 + *s++;
385 return r;
388 static void *pool_alloc(size_t len)
390 struct mem_pool *p;
391 void *r;
393 for (p = mem_pool; p; p = p->next_pool)
394 if ((p->end - p->next_free >= len))
395 break;
397 if (!p) {
398 if (len >= (mem_pool_alloc/2)) {
399 total_allocd += len;
400 return xmalloc(len);
402 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
403 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
404 p->next_pool = mem_pool;
405 p->next_free = p->space;
406 p->end = p->next_free + mem_pool_alloc;
407 mem_pool = p;
410 r = p->next_free;
411 /* round out to a pointer alignment */
412 if (len & (sizeof(void*) - 1))
413 len += sizeof(void*) - (len & (sizeof(void*) - 1));
414 p->next_free += len;
415 return r;
418 static void *pool_calloc(size_t count, size_t size)
420 size_t len = count * size;
421 void *r = pool_alloc(len);
422 memset(r, 0, len);
423 return r;
426 static char *pool_strdup(const char *s)
428 char *r = pool_alloc(strlen(s) + 1);
429 strcpy(r, s);
430 return r;
433 static void size_dbuf(struct dbuf *b, size_t maxlen)
435 if (b->buffer) {
436 if (b->capacity >= maxlen)
437 return;
438 free(b->buffer);
440 b->capacity = ((maxlen / 1024) + 1) * 1024;
441 b->buffer = xmalloc(b->capacity);
444 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
446 struct mark_set *s = marks;
447 while ((idnum >> s->shift) >= 1024) {
448 s = pool_calloc(1, sizeof(struct mark_set));
449 s->shift = marks->shift + 10;
450 s->data.sets[0] = marks;
451 marks = s;
453 while (s->shift) {
454 uintmax_t i = idnum >> s->shift;
455 idnum -= i << s->shift;
456 if (!s->data.sets[i]) {
457 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
458 s->data.sets[i]->shift = s->shift - 10;
460 s = s->data.sets[i];
462 if (!s->data.marked[idnum])
463 marks_set_count++;
464 s->data.marked[idnum] = oe;
467 static struct object_entry *find_mark(uintmax_t idnum)
469 uintmax_t orig_idnum = idnum;
470 struct mark_set *s = marks;
471 struct object_entry *oe = NULL;
472 if ((idnum >> s->shift) < 1024) {
473 while (s && s->shift) {
474 uintmax_t i = idnum >> s->shift;
475 idnum -= i << s->shift;
476 s = s->data.sets[i];
478 if (s)
479 oe = s->data.marked[idnum];
481 if (!oe)
482 die("mark :%" PRIuMAX " not declared", orig_idnum);
483 return oe;
486 static struct atom_str *to_atom(const char *s, unsigned short len)
488 unsigned int hc = hc_str(s, len) % atom_table_sz;
489 struct atom_str *c;
491 for (c = atom_table[hc]; c; c = c->next_atom)
492 if (c->str_len == len && !strncmp(s, c->str_dat, len))
493 return c;
495 c = pool_alloc(sizeof(struct atom_str) + len + 1);
496 c->str_len = len;
497 strncpy(c->str_dat, s, len);
498 c->str_dat[len] = 0;
499 c->next_atom = atom_table[hc];
500 atom_table[hc] = c;
501 atom_cnt++;
502 return c;
505 static struct branch *lookup_branch(const char *name)
507 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
508 struct branch *b;
510 for (b = branch_table[hc]; b; b = b->table_next_branch)
511 if (!strcmp(name, b->name))
512 return b;
513 return NULL;
516 static struct branch *new_branch(const char *name)
518 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
519 struct branch* b = lookup_branch(name);
521 if (b)
522 die("Invalid attempt to create duplicate branch: %s", name);
523 if (check_ref_format(name))
524 die("Branch name doesn't conform to GIT standards: %s", name);
526 b = pool_calloc(1, sizeof(struct branch));
527 b->name = pool_strdup(name);
528 b->table_next_branch = branch_table[hc];
529 b->branch_tree.versions[0].mode = S_IFDIR;
530 b->branch_tree.versions[1].mode = S_IFDIR;
531 b->pack_id = MAX_PACK_ID;
532 branch_table[hc] = b;
533 branch_count++;
534 return b;
537 static unsigned int hc_entries(unsigned int cnt)
539 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
540 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
543 static struct tree_content *new_tree_content(unsigned int cnt)
545 struct avail_tree_content *f, *l = NULL;
546 struct tree_content *t;
547 unsigned int hc = hc_entries(cnt);
549 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
550 if (f->entry_capacity >= cnt)
551 break;
553 if (f) {
554 if (l)
555 l->next_avail = f->next_avail;
556 else
557 avail_tree_table[hc] = f->next_avail;
558 } else {
559 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
560 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
561 f->entry_capacity = cnt;
564 t = (struct tree_content*)f;
565 t->entry_count = 0;
566 t->delta_depth = 0;
567 return t;
570 static void release_tree_entry(struct tree_entry *e);
571 static void release_tree_content(struct tree_content *t)
573 struct avail_tree_content *f = (struct avail_tree_content*)t;
574 unsigned int hc = hc_entries(f->entry_capacity);
575 f->next_avail = avail_tree_table[hc];
576 avail_tree_table[hc] = f;
579 static void release_tree_content_recursive(struct tree_content *t)
581 unsigned int i;
582 for (i = 0; i < t->entry_count; i++)
583 release_tree_entry(t->entries[i]);
584 release_tree_content(t);
587 static struct tree_content *grow_tree_content(
588 struct tree_content *t,
589 int amt)
591 struct tree_content *r = new_tree_content(t->entry_count + amt);
592 r->entry_count = t->entry_count;
593 r->delta_depth = t->delta_depth;
594 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
595 release_tree_content(t);
596 return r;
599 static struct tree_entry *new_tree_entry(void)
601 struct tree_entry *e;
603 if (!avail_tree_entry) {
604 unsigned int n = tree_entry_alloc;
605 total_allocd += n * sizeof(struct tree_entry);
606 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
607 while (n-- > 1) {
608 *((void**)e) = e + 1;
609 e++;
611 *((void**)e) = NULL;
614 e = avail_tree_entry;
615 avail_tree_entry = *((void**)e);
616 return e;
619 static void release_tree_entry(struct tree_entry *e)
621 if (e->tree)
622 release_tree_content_recursive(e->tree);
623 *((void**)e) = avail_tree_entry;
624 avail_tree_entry = e;
627 static void start_packfile(void)
629 static char tmpfile[PATH_MAX];
630 struct packed_git *p;
631 struct pack_header hdr;
632 int pack_fd;
634 snprintf(tmpfile, sizeof(tmpfile),
635 "%s/pack_XXXXXX", get_object_directory());
636 pack_fd = mkstemp(tmpfile);
637 if (pack_fd < 0)
638 die("Can't create %s: %s", tmpfile, strerror(errno));
639 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
640 strcpy(p->pack_name, tmpfile);
641 p->pack_fd = pack_fd;
643 hdr.hdr_signature = htonl(PACK_SIGNATURE);
644 hdr.hdr_version = htonl(2);
645 hdr.hdr_entries = 0;
646 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
648 pack_data = p;
649 pack_size = sizeof(hdr);
650 object_count = 0;
652 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
653 all_packs[pack_id] = p;
656 static void fixup_header_footer(void)
658 static const int buf_sz = 128 * 1024;
659 int pack_fd = pack_data->pack_fd;
660 SHA_CTX c;
661 struct pack_header hdr;
662 char *buf;
664 if (lseek(pack_fd, 0, SEEK_SET) != 0)
665 die("Failed seeking to start: %s", strerror(errno));
666 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
667 die("Unable to reread header of %s", pack_data->pack_name);
668 if (lseek(pack_fd, 0, SEEK_SET) != 0)
669 die("Failed seeking to start: %s", strerror(errno));
670 hdr.hdr_entries = htonl(object_count);
671 write_or_die(pack_fd, &hdr, sizeof(hdr));
673 SHA1_Init(&c);
674 SHA1_Update(&c, &hdr, sizeof(hdr));
676 buf = xmalloc(buf_sz);
677 for (;;) {
678 size_t n = xread(pack_fd, buf, buf_sz);
679 if (!n)
680 break;
681 if (n < 0)
682 die("Failed to checksum %s", pack_data->pack_name);
683 SHA1_Update(&c, buf, n);
685 free(buf);
687 SHA1_Final(pack_data->sha1, &c);
688 write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
689 close(pack_fd);
692 static int oecmp (const void *a_, const void *b_)
694 struct object_entry *a = *((struct object_entry**)a_);
695 struct object_entry *b = *((struct object_entry**)b_);
696 return hashcmp(a->sha1, b->sha1);
699 static char *create_index(void)
701 static char tmpfile[PATH_MAX];
702 SHA_CTX ctx;
703 struct sha1file *f;
704 struct object_entry **idx, **c, **last, *e;
705 struct object_entry_pool *o;
706 uint32_t array[256];
707 int i, idx_fd;
709 /* Build the sorted table of object IDs. */
710 idx = xmalloc(object_count * sizeof(struct object_entry*));
711 c = idx;
712 for (o = blocks; o; o = o->next_pool)
713 for (e = o->next_free; e-- != o->entries;)
714 if (pack_id == e->pack_id)
715 *c++ = e;
716 last = idx + object_count;
717 if (c != last)
718 die("internal consistency error creating the index");
719 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
721 /* Generate the fan-out array. */
722 c = idx;
723 for (i = 0; i < 256; i++) {
724 struct object_entry **next = c;;
725 while (next < last) {
726 if ((*next)->sha1[0] != i)
727 break;
728 next++;
730 array[i] = htonl(next - idx);
731 c = next;
734 snprintf(tmpfile, sizeof(tmpfile),
735 "%s/index_XXXXXX", get_object_directory());
736 idx_fd = mkstemp(tmpfile);
737 if (idx_fd < 0)
738 die("Can't create %s: %s", tmpfile, strerror(errno));
739 f = sha1fd(idx_fd, tmpfile);
740 sha1write(f, array, 256 * sizeof(int));
741 SHA1_Init(&ctx);
742 for (c = idx; c != last; c++) {
743 uint32_t offset = htonl((*c)->offset);
744 sha1write(f, &offset, 4);
745 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
746 SHA1_Update(&ctx, (*c)->sha1, 20);
748 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
749 sha1close(f, NULL, 1);
750 free(idx);
751 SHA1_Final(pack_data->sha1, &ctx);
752 return tmpfile;
755 static char *keep_pack(char *curr_index_name)
757 static char name[PATH_MAX];
758 static char *keep_msg = "fast-import";
759 int keep_fd;
761 chmod(pack_data->pack_name, 0444);
762 chmod(curr_index_name, 0444);
764 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
765 get_object_directory(), sha1_to_hex(pack_data->sha1));
766 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
767 if (keep_fd < 0)
768 die("cannot create keep file");
769 write(keep_fd, keep_msg, strlen(keep_msg));
770 close(keep_fd);
772 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
773 get_object_directory(), sha1_to_hex(pack_data->sha1));
774 if (move_temp_to_file(pack_data->pack_name, name))
775 die("cannot store pack file");
777 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
778 get_object_directory(), sha1_to_hex(pack_data->sha1));
779 if (move_temp_to_file(curr_index_name, name))
780 die("cannot store index file");
781 return name;
784 static void unkeep_all_packs(void)
786 static char name[PATH_MAX];
787 int k;
789 for (k = 0; k < pack_id; k++) {
790 struct packed_git *p = all_packs[k];
791 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
792 get_object_directory(), sha1_to_hex(p->sha1));
793 unlink(name);
797 static void end_packfile(void)
799 struct packed_git *old_p = pack_data, *new_p;
801 if (object_count) {
802 char *idx_name;
803 int i;
804 struct branch *b;
805 struct tag *t;
807 fixup_header_footer();
808 idx_name = keep_pack(create_index());
810 /* Register the packfile with core git's machinary. */
811 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
812 if (!new_p)
813 die("core git rejected index %s", idx_name);
814 new_p->windows = old_p->windows;
815 all_packs[pack_id] = new_p;
816 install_packed_git(new_p);
818 /* Print the boundary */
819 if (pack_edges) {
820 fprintf(pack_edges, "%s:", new_p->pack_name);
821 for (i = 0; i < branch_table_sz; i++) {
822 for (b = branch_table[i]; b; b = b->table_next_branch) {
823 if (b->pack_id == pack_id)
824 fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
827 for (t = first_tag; t; t = t->next_tag) {
828 if (t->pack_id == pack_id)
829 fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
831 fputc('\n', pack_edges);
832 fflush(pack_edges);
835 pack_id++;
837 else
838 unlink(old_p->pack_name);
839 free(old_p);
841 /* We can't carry a delta across packfiles. */
842 free(last_blob.data);
843 last_blob.data = NULL;
844 last_blob.len = 0;
845 last_blob.offset = 0;
846 last_blob.depth = 0;
849 static void cycle_packfile(void)
851 end_packfile();
852 start_packfile();
855 static size_t encode_header(
856 enum object_type type,
857 size_t size,
858 unsigned char *hdr)
860 int n = 1;
861 unsigned char c;
863 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
864 die("bad type %d", type);
866 c = (type << 4) | (size & 15);
867 size >>= 4;
868 while (size) {
869 *hdr++ = c | 0x80;
870 c = size & 0x7f;
871 size >>= 7;
872 n++;
874 *hdr = c;
875 return n;
878 static int store_object(
879 enum object_type type,
880 void *dat,
881 size_t datlen,
882 struct last_object *last,
883 unsigned char *sha1out,
884 uintmax_t mark)
886 void *out, *delta;
887 struct object_entry *e;
888 unsigned char hdr[96];
889 unsigned char sha1[20];
890 unsigned long hdrlen, deltalen;
891 SHA_CTX c;
892 z_stream s;
894 hdrlen = sprintf((char*)hdr,"%s %lu", type_names[type],
895 (unsigned long)datlen) + 1;
896 SHA1_Init(&c);
897 SHA1_Update(&c, hdr, hdrlen);
898 SHA1_Update(&c, dat, datlen);
899 SHA1_Final(sha1, &c);
900 if (sha1out)
901 hashcpy(sha1out, sha1);
903 e = insert_object(sha1);
904 if (mark)
905 insert_mark(mark, e);
906 if (e->offset) {
907 duplicate_count_by_type[type]++;
908 return 1;
911 if (last && last->data && last->depth < max_depth) {
912 delta = diff_delta(last->data, last->len,
913 dat, datlen,
914 &deltalen, 0);
915 if (delta && deltalen >= datlen) {
916 free(delta);
917 delta = NULL;
919 } else
920 delta = NULL;
922 memset(&s, 0, sizeof(s));
923 deflateInit(&s, zlib_compression_level);
924 if (delta) {
925 s.next_in = delta;
926 s.avail_in = deltalen;
927 } else {
928 s.next_in = dat;
929 s.avail_in = datlen;
931 s.avail_out = deflateBound(&s, s.avail_in);
932 s.next_out = out = xmalloc(s.avail_out);
933 while (deflate(&s, Z_FINISH) == Z_OK)
934 /* nothing */;
935 deflateEnd(&s);
937 /* Determine if we should auto-checkpoint. */
938 if ((pack_size + 60 + s.total_out) > max_packsize
939 || (pack_size + 60 + s.total_out) < pack_size) {
941 /* This new object needs to *not* have the current pack_id. */
942 e->pack_id = pack_id + 1;
943 cycle_packfile();
945 /* We cannot carry a delta into the new pack. */
946 if (delta) {
947 free(delta);
948 delta = NULL;
950 memset(&s, 0, sizeof(s));
951 deflateInit(&s, zlib_compression_level);
952 s.next_in = dat;
953 s.avail_in = datlen;
954 s.avail_out = deflateBound(&s, s.avail_in);
955 s.next_out = out = xrealloc(out, s.avail_out);
956 while (deflate(&s, Z_FINISH) == Z_OK)
957 /* nothing */;
958 deflateEnd(&s);
962 e->type = type;
963 e->pack_id = pack_id;
964 e->offset = pack_size;
965 object_count++;
966 object_count_by_type[type]++;
968 if (delta) {
969 unsigned long ofs = e->offset - last->offset;
970 unsigned pos = sizeof(hdr) - 1;
972 delta_count_by_type[type]++;
973 last->depth++;
975 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
976 write_or_die(pack_data->pack_fd, hdr, hdrlen);
977 pack_size += hdrlen;
979 hdr[pos] = ofs & 127;
980 while (ofs >>= 7)
981 hdr[--pos] = 128 | (--ofs & 127);
982 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
983 pack_size += sizeof(hdr) - pos;
984 } else {
985 if (last)
986 last->depth = 0;
987 hdrlen = encode_header(type, datlen, hdr);
988 write_or_die(pack_data->pack_fd, hdr, hdrlen);
989 pack_size += hdrlen;
992 write_or_die(pack_data->pack_fd, out, s.total_out);
993 pack_size += s.total_out;
995 free(out);
996 free(delta);
997 if (last) {
998 if (!last->no_free)
999 free(last->data);
1000 last->data = dat;
1001 last->offset = e->offset;
1002 last->len = datlen;
1004 return 0;
1007 static void *gfi_unpack_entry(
1008 struct object_entry *oe,
1009 unsigned long *sizep)
1011 static char type[20];
1012 struct packed_git *p = all_packs[oe->pack_id];
1013 if (p == pack_data)
1014 p->pack_size = pack_size + 20;
1015 return unpack_entry(p, oe->offset, type, sizep);
1018 static const char *get_mode(const char *str, uint16_t *modep)
1020 unsigned char c;
1021 uint16_t mode = 0;
1023 while ((c = *str++) != ' ') {
1024 if (c < '0' || c > '7')
1025 return NULL;
1026 mode = (mode << 3) + (c - '0');
1028 *modep = mode;
1029 return str;
1032 static void load_tree(struct tree_entry *root)
1034 unsigned char* sha1 = root->versions[1].sha1;
1035 struct object_entry *myoe;
1036 struct tree_content *t;
1037 unsigned long size;
1038 char *buf;
1039 const char *c;
1041 root->tree = t = new_tree_content(8);
1042 if (is_null_sha1(sha1))
1043 return;
1045 myoe = find_object(sha1);
1046 if (myoe) {
1047 if (myoe->type != OBJ_TREE)
1048 die("Not a tree: %s", sha1_to_hex(sha1));
1049 t->delta_depth = 0;
1050 buf = gfi_unpack_entry(myoe, &size);
1051 } else {
1052 char type[20];
1053 buf = read_sha1_file(sha1, type, &size);
1054 if (!buf || strcmp(type, tree_type))
1055 die("Can't load tree %s", sha1_to_hex(sha1));
1058 c = buf;
1059 while (c != (buf + size)) {
1060 struct tree_entry *e = new_tree_entry();
1062 if (t->entry_count == t->entry_capacity)
1063 root->tree = t = grow_tree_content(t, 8);
1064 t->entries[t->entry_count++] = e;
1066 e->tree = NULL;
1067 c = get_mode(c, &e->versions[1].mode);
1068 if (!c)
1069 die("Corrupt mode in %s", sha1_to_hex(sha1));
1070 e->versions[0].mode = e->versions[1].mode;
1071 e->name = to_atom(c, (unsigned short)strlen(c));
1072 c += e->name->str_len + 1;
1073 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1074 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1075 c += 20;
1077 free(buf);
1080 static int tecmp0 (const void *_a, const void *_b)
1082 struct tree_entry *a = *((struct tree_entry**)_a);
1083 struct tree_entry *b = *((struct tree_entry**)_b);
1084 return base_name_compare(
1085 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1086 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1089 static int tecmp1 (const void *_a, const void *_b)
1091 struct tree_entry *a = *((struct tree_entry**)_a);
1092 struct tree_entry *b = *((struct tree_entry**)_b);
1093 return base_name_compare(
1094 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1095 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1098 static void mktree(struct tree_content *t,
1099 int v,
1100 unsigned long *szp,
1101 struct dbuf *b)
1103 size_t maxlen = 0;
1104 unsigned int i;
1105 char *c;
1107 if (!v)
1108 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1109 else
1110 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1112 for (i = 0; i < t->entry_count; i++) {
1113 if (t->entries[i]->versions[v].mode)
1114 maxlen += t->entries[i]->name->str_len + 34;
1117 size_dbuf(b, maxlen);
1118 c = b->buffer;
1119 for (i = 0; i < t->entry_count; i++) {
1120 struct tree_entry *e = t->entries[i];
1121 if (!e->versions[v].mode)
1122 continue;
1123 c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1124 *c++ = ' ';
1125 strcpy(c, e->name->str_dat);
1126 c += e->name->str_len + 1;
1127 hashcpy((unsigned char*)c, e->versions[v].sha1);
1128 c += 20;
1130 *szp = c - (char*)b->buffer;
1133 static void store_tree(struct tree_entry *root)
1135 struct tree_content *t = root->tree;
1136 unsigned int i, j, del;
1137 unsigned long new_len;
1138 struct last_object lo;
1139 struct object_entry *le;
1141 if (!is_null_sha1(root->versions[1].sha1))
1142 return;
1144 for (i = 0; i < t->entry_count; i++) {
1145 if (t->entries[i]->tree)
1146 store_tree(t->entries[i]);
1149 le = find_object(root->versions[0].sha1);
1150 if (!S_ISDIR(root->versions[0].mode)
1151 || !le
1152 || le->pack_id != pack_id) {
1153 lo.data = NULL;
1154 lo.depth = 0;
1155 } else {
1156 mktree(t, 0, &lo.len, &old_tree);
1157 lo.data = old_tree.buffer;
1158 lo.offset = le->offset;
1159 lo.depth = t->delta_depth;
1160 lo.no_free = 1;
1163 mktree(t, 1, &new_len, &new_tree);
1164 store_object(OBJ_TREE, new_tree.buffer, new_len,
1165 &lo, root->versions[1].sha1, 0);
1167 t->delta_depth = lo.depth;
1168 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1169 struct tree_entry *e = t->entries[i];
1170 if (e->versions[1].mode) {
1171 e->versions[0].mode = e->versions[1].mode;
1172 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1173 t->entries[j++] = e;
1174 } else {
1175 release_tree_entry(e);
1176 del++;
1179 t->entry_count -= del;
1182 static int tree_content_set(
1183 struct tree_entry *root,
1184 const char *p,
1185 const unsigned char *sha1,
1186 const uint16_t mode)
1188 struct tree_content *t = root->tree;
1189 const char *slash1;
1190 unsigned int i, n;
1191 struct tree_entry *e;
1193 slash1 = strchr(p, '/');
1194 if (slash1)
1195 n = slash1 - p;
1196 else
1197 n = strlen(p);
1199 for (i = 0; i < t->entry_count; i++) {
1200 e = t->entries[i];
1201 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1202 if (!slash1) {
1203 if (e->versions[1].mode == mode
1204 && !hashcmp(e->versions[1].sha1, sha1))
1205 return 0;
1206 e->versions[1].mode = mode;
1207 hashcpy(e->versions[1].sha1, sha1);
1208 if (e->tree) {
1209 release_tree_content_recursive(e->tree);
1210 e->tree = NULL;
1212 hashclr(root->versions[1].sha1);
1213 return 1;
1215 if (!S_ISDIR(e->versions[1].mode)) {
1216 e->tree = new_tree_content(8);
1217 e->versions[1].mode = S_IFDIR;
1219 if (!e->tree)
1220 load_tree(e);
1221 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1222 hashclr(root->versions[1].sha1);
1223 return 1;
1225 return 0;
1229 if (t->entry_count == t->entry_capacity)
1230 root->tree = t = grow_tree_content(t, 8);
1231 e = new_tree_entry();
1232 e->name = to_atom(p, (unsigned short)n);
1233 e->versions[0].mode = 0;
1234 hashclr(e->versions[0].sha1);
1235 t->entries[t->entry_count++] = e;
1236 if (slash1) {
1237 e->tree = new_tree_content(8);
1238 e->versions[1].mode = S_IFDIR;
1239 tree_content_set(e, slash1 + 1, sha1, mode);
1240 } else {
1241 e->tree = NULL;
1242 e->versions[1].mode = mode;
1243 hashcpy(e->versions[1].sha1, sha1);
1245 hashclr(root->versions[1].sha1);
1246 return 1;
1249 static int tree_content_remove(struct tree_entry *root, const char *p)
1251 struct tree_content *t = root->tree;
1252 const char *slash1;
1253 unsigned int i, n;
1254 struct tree_entry *e;
1256 slash1 = strchr(p, '/');
1257 if (slash1)
1258 n = slash1 - p;
1259 else
1260 n = strlen(p);
1262 for (i = 0; i < t->entry_count; i++) {
1263 e = t->entries[i];
1264 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1265 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1266 goto del_entry;
1267 if (!e->tree)
1268 load_tree(e);
1269 if (tree_content_remove(e, slash1 + 1)) {
1270 for (n = 0; n < e->tree->entry_count; n++) {
1271 if (e->tree->entries[n]->versions[1].mode) {
1272 hashclr(root->versions[1].sha1);
1273 return 1;
1276 goto del_entry;
1278 return 0;
1281 return 0;
1283 del_entry:
1284 if (e->tree) {
1285 release_tree_content_recursive(e->tree);
1286 e->tree = NULL;
1288 e->versions[1].mode = 0;
1289 hashclr(e->versions[1].sha1);
1290 hashclr(root->versions[1].sha1);
1291 return 1;
1294 static int update_branch(struct branch *b)
1296 static const char *msg = "fast-import";
1297 struct ref_lock *lock;
1298 unsigned char old_sha1[20];
1300 if (read_ref(b->name, old_sha1))
1301 hashclr(old_sha1);
1302 lock = lock_any_ref_for_update(b->name, old_sha1);
1303 if (!lock)
1304 return error("Unable to lock %s", b->name);
1305 if (!force_update && !is_null_sha1(old_sha1)) {
1306 struct commit *old_cmit, *new_cmit;
1308 old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1309 new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1310 if (!old_cmit || !new_cmit) {
1311 unlock_ref(lock);
1312 return error("Branch %s is missing commits.", b->name);
1315 if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
1316 unlock_ref(lock);
1317 warn("Not updating %s"
1318 " (new tip %s does not contain %s)",
1319 b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1320 return -1;
1323 if (write_ref_sha1(lock, b->sha1, msg) < 0)
1324 return error("Unable to update %s", b->name);
1325 return 0;
1328 static void dump_branches(void)
1330 unsigned int i;
1331 struct branch *b;
1333 for (i = 0; i < branch_table_sz; i++) {
1334 for (b = branch_table[i]; b; b = b->table_next_branch)
1335 failure |= update_branch(b);
1339 static void dump_tags(void)
1341 static const char *msg = "fast-import";
1342 struct tag *t;
1343 struct ref_lock *lock;
1344 char ref_name[PATH_MAX];
1346 for (t = first_tag; t; t = t->next_tag) {
1347 sprintf(ref_name, "tags/%s", t->name);
1348 lock = lock_ref_sha1(ref_name, NULL);
1349 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1350 failure |= error("Unable to update %s", ref_name);
1354 static void dump_marks_helper(FILE *f,
1355 uintmax_t base,
1356 struct mark_set *m)
1358 uintmax_t k;
1359 if (m->shift) {
1360 for (k = 0; k < 1024; k++) {
1361 if (m->data.sets[k])
1362 dump_marks_helper(f, (base + k) << m->shift,
1363 m->data.sets[k]);
1365 } else {
1366 for (k = 0; k < 1024; k++) {
1367 if (m->data.marked[k])
1368 fprintf(f, ":%" PRIuMAX " %s\n", base + k,
1369 sha1_to_hex(m->data.marked[k]->sha1));
1374 static void dump_marks(void)
1376 if (mark_file)
1378 FILE *f = fopen(mark_file, "w");
1379 if (f) {
1380 dump_marks_helper(f, 0, marks);
1381 fclose(f);
1382 } else
1383 failure |= error("Unable to write marks file %s: %s",
1384 mark_file, strerror(errno));
1388 static void read_next_command(void)
1390 read_line(&command_buf, stdin, '\n');
1393 static void cmd_mark(void)
1395 if (!prefixcmp(command_buf.buf, "mark :")) {
1396 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1397 read_next_command();
1399 else
1400 next_mark = 0;
1403 static void *cmd_data (size_t *size)
1405 size_t length;
1406 char *buffer;
1408 if (prefixcmp(command_buf.buf, "data "))
1409 die("Expected 'data n' command, found: %s", command_buf.buf);
1411 if (!prefixcmp(command_buf.buf + 5, "<<")) {
1412 char *term = xstrdup(command_buf.buf + 5 + 2);
1413 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1414 length = 0;
1415 buffer = xmalloc(sz);
1416 for (;;) {
1417 read_next_command();
1418 if (command_buf.eof)
1419 die("EOF in data (terminator '%s' not found)", term);
1420 if (term_len == command_buf.len
1421 && !strcmp(term, command_buf.buf))
1422 break;
1423 if (sz < (length + command_buf.len)) {
1424 sz = sz * 3 / 2 + 16;
1425 if (sz < (length + command_buf.len))
1426 sz = length + command_buf.len;
1427 buffer = xrealloc(buffer, sz);
1429 memcpy(buffer + length,
1430 command_buf.buf,
1431 command_buf.len - 1);
1432 length += command_buf.len - 1;
1433 buffer[length++] = '\n';
1435 free(term);
1437 else {
1438 size_t n = 0;
1439 length = strtoul(command_buf.buf + 5, NULL, 10);
1440 buffer = xmalloc(length);
1441 while (n < length) {
1442 size_t s = fread(buffer + n, 1, length - n, stdin);
1443 if (!s && feof(stdin))
1444 die("EOF in data (%lu bytes remaining)",
1445 (unsigned long)(length - n));
1446 n += s;
1450 if (fgetc(stdin) != '\n')
1451 die("An lf did not trail the binary data as expected.");
1453 *size = length;
1454 return buffer;
1457 static int validate_raw_date(const char *src, char *result, int maxlen)
1459 const char *orig_src = src;
1460 char *endp, sign;
1462 strtoul(src, &endp, 10);
1463 if (endp == src || *endp != ' ')
1464 return -1;
1466 src = endp + 1;
1467 if (*src != '-' && *src != '+')
1468 return -1;
1469 sign = *src;
1471 strtoul(src + 1, &endp, 10);
1472 if (endp == src || *endp || (endp - orig_src) >= maxlen)
1473 return -1;
1475 strcpy(result, orig_src);
1476 return 0;
1479 static char *parse_ident(const char *buf)
1481 const char *gt;
1482 size_t name_len;
1483 char *ident;
1485 gt = strrchr(buf, '>');
1486 if (!gt)
1487 die("Missing > in ident string: %s", buf);
1488 gt++;
1489 if (*gt != ' ')
1490 die("Missing space after > in ident string: %s", buf);
1491 gt++;
1492 name_len = gt - buf;
1493 ident = xmalloc(name_len + 24);
1494 strncpy(ident, buf, name_len);
1496 switch (whenspec) {
1497 case WHENSPEC_RAW:
1498 if (validate_raw_date(gt, ident + name_len, 24) < 0)
1499 die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1500 break;
1501 case WHENSPEC_RFC2822:
1502 if (parse_date(gt, ident + name_len, 24) < 0)
1503 die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1504 break;
1505 case WHENSPEC_NOW:
1506 if (strcmp("now", gt))
1507 die("Date in ident must be 'now': %s", buf);
1508 datestamp(ident + name_len, 24);
1509 break;
1512 return ident;
1515 static void cmd_new_blob(void)
1517 size_t l;
1518 void *d;
1520 read_next_command();
1521 cmd_mark();
1522 d = cmd_data(&l);
1524 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1525 free(d);
1528 static void unload_one_branch(void)
1530 while (cur_active_branches
1531 && cur_active_branches >= max_active_branches) {
1532 unsigned long min_commit = ULONG_MAX;
1533 struct branch *e, *l = NULL, *p = NULL;
1535 for (e = active_branches; e; e = e->active_next_branch) {
1536 if (e->last_commit < min_commit) {
1537 p = l;
1538 min_commit = e->last_commit;
1540 l = e;
1543 if (p) {
1544 e = p->active_next_branch;
1545 p->active_next_branch = e->active_next_branch;
1546 } else {
1547 e = active_branches;
1548 active_branches = e->active_next_branch;
1550 e->active_next_branch = NULL;
1551 if (e->branch_tree.tree) {
1552 release_tree_content_recursive(e->branch_tree.tree);
1553 e->branch_tree.tree = NULL;
1555 cur_active_branches--;
1559 static void load_branch(struct branch *b)
1561 load_tree(&b->branch_tree);
1562 b->active_next_branch = active_branches;
1563 active_branches = b;
1564 cur_active_branches++;
1565 branch_load_count++;
1568 static void file_change_m(struct branch *b)
1570 const char *p = command_buf.buf + 2;
1571 char *p_uq;
1572 const char *endp;
1573 struct object_entry *oe = oe;
1574 unsigned char sha1[20];
1575 uint16_t mode, inline_data = 0;
1576 char type[20];
1578 p = get_mode(p, &mode);
1579 if (!p)
1580 die("Corrupt mode: %s", command_buf.buf);
1581 switch (mode) {
1582 case S_IFREG | 0644:
1583 case S_IFREG | 0755:
1584 case S_IFLNK:
1585 case 0644:
1586 case 0755:
1587 /* ok */
1588 break;
1589 default:
1590 die("Corrupt mode: %s", command_buf.buf);
1593 if (*p == ':') {
1594 char *x;
1595 oe = find_mark(strtoumax(p + 1, &x, 10));
1596 hashcpy(sha1, oe->sha1);
1597 p = x;
1598 } else if (!prefixcmp(p, "inline")) {
1599 inline_data = 1;
1600 p += 6;
1601 } else {
1602 if (get_sha1_hex(p, sha1))
1603 die("Invalid SHA1: %s", command_buf.buf);
1604 oe = find_object(sha1);
1605 p += 40;
1607 if (*p++ != ' ')
1608 die("Missing space after SHA1: %s", command_buf.buf);
1610 p_uq = unquote_c_style(p, &endp);
1611 if (p_uq) {
1612 if (*endp)
1613 die("Garbage after path in: %s", command_buf.buf);
1614 p = p_uq;
1617 if (inline_data) {
1618 size_t l;
1619 void *d;
1620 if (!p_uq)
1621 p = p_uq = xstrdup(p);
1622 read_next_command();
1623 d = cmd_data(&l);
1624 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1625 free(d);
1626 } else if (oe) {
1627 if (oe->type != OBJ_BLOB)
1628 die("Not a blob (actually a %s): %s",
1629 command_buf.buf, type_names[oe->type]);
1630 } else {
1631 if (sha1_object_info(sha1, type, NULL))
1632 die("Blob not found: %s", command_buf.buf);
1633 if (strcmp(blob_type, type))
1634 die("Not a blob (actually a %s): %s",
1635 command_buf.buf, type);
1638 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1639 free(p_uq);
1642 static void file_change_d(struct branch *b)
1644 const char *p = command_buf.buf + 2;
1645 char *p_uq;
1646 const char *endp;
1648 p_uq = unquote_c_style(p, &endp);
1649 if (p_uq) {
1650 if (*endp)
1651 die("Garbage after path in: %s", command_buf.buf);
1652 p = p_uq;
1654 tree_content_remove(&b->branch_tree, p);
1655 free(p_uq);
1658 static void file_change_deleteall(struct branch *b)
1660 release_tree_content_recursive(b->branch_tree.tree);
1661 hashclr(b->branch_tree.versions[0].sha1);
1662 hashclr(b->branch_tree.versions[1].sha1);
1663 load_tree(&b->branch_tree);
1666 static void cmd_from(struct branch *b)
1668 const char *from;
1669 struct branch *s;
1671 if (prefixcmp(command_buf.buf, "from "))
1672 return;
1674 if (b->branch_tree.tree) {
1675 release_tree_content_recursive(b->branch_tree.tree);
1676 b->branch_tree.tree = NULL;
1679 from = strchr(command_buf.buf, ' ') + 1;
1680 s = lookup_branch(from);
1681 if (b == s)
1682 die("Can't create a branch from itself: %s", b->name);
1683 else if (s) {
1684 unsigned char *t = s->branch_tree.versions[1].sha1;
1685 hashcpy(b->sha1, s->sha1);
1686 hashcpy(b->branch_tree.versions[0].sha1, t);
1687 hashcpy(b->branch_tree.versions[1].sha1, t);
1688 } else if (*from == ':') {
1689 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1690 struct object_entry *oe = find_mark(idnum);
1691 unsigned long size;
1692 char *buf;
1693 if (oe->type != OBJ_COMMIT)
1694 die("Mark :%" PRIuMAX " not a commit", idnum);
1695 hashcpy(b->sha1, oe->sha1);
1696 buf = gfi_unpack_entry(oe, &size);
1697 if (!buf || size < 46)
1698 die("Not a valid commit: %s", from);
1699 if (memcmp("tree ", buf, 5)
1700 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1701 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1702 free(buf);
1703 hashcpy(b->branch_tree.versions[0].sha1,
1704 b->branch_tree.versions[1].sha1);
1705 } else if (!get_sha1(from, b->sha1)) {
1706 if (is_null_sha1(b->sha1)) {
1707 hashclr(b->branch_tree.versions[0].sha1);
1708 hashclr(b->branch_tree.versions[1].sha1);
1709 } else {
1710 unsigned long size;
1711 char *buf;
1713 buf = read_object_with_reference(b->sha1,
1714 type_names[OBJ_COMMIT], &size, b->sha1);
1715 if (!buf || size < 46)
1716 die("Not a valid commit: %s", from);
1717 if (memcmp("tree ", buf, 5)
1718 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1719 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1720 free(buf);
1721 hashcpy(b->branch_tree.versions[0].sha1,
1722 b->branch_tree.versions[1].sha1);
1724 } else
1725 die("Invalid ref name or SHA1 expression: %s", from);
1727 read_next_command();
1730 static struct hash_list *cmd_merge(unsigned int *count)
1732 struct hash_list *list = NULL, *n, *e = e;
1733 const char *from;
1734 struct branch *s;
1736 *count = 0;
1737 while (!prefixcmp(command_buf.buf, "merge ")) {
1738 from = strchr(command_buf.buf, ' ') + 1;
1739 n = xmalloc(sizeof(*n));
1740 s = lookup_branch(from);
1741 if (s)
1742 hashcpy(n->sha1, s->sha1);
1743 else if (*from == ':') {
1744 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1745 struct object_entry *oe = find_mark(idnum);
1746 if (oe->type != OBJ_COMMIT)
1747 die("Mark :%" PRIuMAX " not a commit", idnum);
1748 hashcpy(n->sha1, oe->sha1);
1749 } else if (get_sha1(from, n->sha1))
1750 die("Invalid ref name or SHA1 expression: %s", from);
1752 n->next = NULL;
1753 if (list)
1754 e->next = n;
1755 else
1756 list = n;
1757 e = n;
1758 (*count)++;
1759 read_next_command();
1761 return list;
1764 static void cmd_new_commit(void)
1766 struct branch *b;
1767 void *msg;
1768 size_t msglen;
1769 char *sp;
1770 char *author = NULL;
1771 char *committer = NULL;
1772 struct hash_list *merge_list = NULL;
1773 unsigned int merge_count;
1775 /* Obtain the branch name from the rest of our command */
1776 sp = strchr(command_buf.buf, ' ') + 1;
1777 b = lookup_branch(sp);
1778 if (!b)
1779 b = new_branch(sp);
1781 read_next_command();
1782 cmd_mark();
1783 if (!prefixcmp(command_buf.buf, "author ")) {
1784 author = parse_ident(command_buf.buf + 7);
1785 read_next_command();
1787 if (!prefixcmp(command_buf.buf, "committer ")) {
1788 committer = parse_ident(command_buf.buf + 10);
1789 read_next_command();
1791 if (!committer)
1792 die("Expected committer but didn't get one");
1793 msg = cmd_data(&msglen);
1794 read_next_command();
1795 cmd_from(b);
1796 merge_list = cmd_merge(&merge_count);
1798 /* ensure the branch is active/loaded */
1799 if (!b->branch_tree.tree || !max_active_branches) {
1800 unload_one_branch();
1801 load_branch(b);
1804 /* file_change* */
1805 for (;;) {
1806 if (1 == command_buf.len)
1807 break;
1808 else if (!prefixcmp(command_buf.buf, "M "))
1809 file_change_m(b);
1810 else if (!prefixcmp(command_buf.buf, "D "))
1811 file_change_d(b);
1812 else if (!strcmp("deleteall", command_buf.buf))
1813 file_change_deleteall(b);
1814 else
1815 die("Unsupported file_change: %s", command_buf.buf);
1816 read_next_command();
1819 /* build the tree and the commit */
1820 store_tree(&b->branch_tree);
1821 hashcpy(b->branch_tree.versions[0].sha1,
1822 b->branch_tree.versions[1].sha1);
1823 size_dbuf(&new_data, 114 + msglen
1824 + merge_count * 49
1825 + (author
1826 ? strlen(author) + strlen(committer)
1827 : 2 * strlen(committer)));
1828 sp = new_data.buffer;
1829 sp += sprintf(sp, "tree %s\n",
1830 sha1_to_hex(b->branch_tree.versions[1].sha1));
1831 if (!is_null_sha1(b->sha1))
1832 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1833 while (merge_list) {
1834 struct hash_list *next = merge_list->next;
1835 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1836 free(merge_list);
1837 merge_list = next;
1839 sp += sprintf(sp, "author %s\n", author ? author : committer);
1840 sp += sprintf(sp, "committer %s\n", committer);
1841 *sp++ = '\n';
1842 memcpy(sp, msg, msglen);
1843 sp += msglen;
1844 free(author);
1845 free(committer);
1846 free(msg);
1848 if (!store_object(OBJ_COMMIT,
1849 new_data.buffer, sp - (char*)new_data.buffer,
1850 NULL, b->sha1, next_mark))
1851 b->pack_id = pack_id;
1852 b->last_commit = object_count_by_type[OBJ_COMMIT];
1855 static void cmd_new_tag(void)
1857 char *sp;
1858 const char *from;
1859 char *tagger;
1860 struct branch *s;
1861 void *msg;
1862 size_t msglen;
1863 struct tag *t;
1864 uintmax_t from_mark = 0;
1865 unsigned char sha1[20];
1867 /* Obtain the new tag name from the rest of our command */
1868 sp = strchr(command_buf.buf, ' ') + 1;
1869 t = pool_alloc(sizeof(struct tag));
1870 t->next_tag = NULL;
1871 t->name = pool_strdup(sp);
1872 if (last_tag)
1873 last_tag->next_tag = t;
1874 else
1875 first_tag = t;
1876 last_tag = t;
1877 read_next_command();
1879 /* from ... */
1880 if (prefixcmp(command_buf.buf, "from "))
1881 die("Expected from command, got %s", command_buf.buf);
1882 from = strchr(command_buf.buf, ' ') + 1;
1883 s = lookup_branch(from);
1884 if (s) {
1885 hashcpy(sha1, s->sha1);
1886 } else if (*from == ':') {
1887 struct object_entry *oe;
1888 from_mark = strtoumax(from + 1, NULL, 10);
1889 oe = find_mark(from_mark);
1890 if (oe->type != OBJ_COMMIT)
1891 die("Mark :%" PRIuMAX " not a commit", from_mark);
1892 hashcpy(sha1, oe->sha1);
1893 } else if (!get_sha1(from, sha1)) {
1894 unsigned long size;
1895 char *buf;
1897 buf = read_object_with_reference(sha1,
1898 type_names[OBJ_COMMIT], &size, sha1);
1899 if (!buf || size < 46)
1900 die("Not a valid commit: %s", from);
1901 free(buf);
1902 } else
1903 die("Invalid ref name or SHA1 expression: %s", from);
1904 read_next_command();
1906 /* tagger ... */
1907 if (prefixcmp(command_buf.buf, "tagger "))
1908 die("Expected tagger command, got %s", command_buf.buf);
1909 tagger = parse_ident(command_buf.buf + 7);
1911 /* tag payload/message */
1912 read_next_command();
1913 msg = cmd_data(&msglen);
1915 /* build the tag object */
1916 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1917 sp = new_data.buffer;
1918 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1919 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1920 sp += sprintf(sp, "tag %s\n", t->name);
1921 sp += sprintf(sp, "tagger %s\n", tagger);
1922 *sp++ = '\n';
1923 memcpy(sp, msg, msglen);
1924 sp += msglen;
1925 free(tagger);
1926 free(msg);
1928 if (store_object(OBJ_TAG, new_data.buffer,
1929 sp - (char*)new_data.buffer,
1930 NULL, t->sha1, 0))
1931 t->pack_id = MAX_PACK_ID;
1932 else
1933 t->pack_id = pack_id;
1936 static void cmd_reset_branch(void)
1938 struct branch *b;
1939 char *sp;
1941 /* Obtain the branch name from the rest of our command */
1942 sp = strchr(command_buf.buf, ' ') + 1;
1943 b = lookup_branch(sp);
1944 if (b) {
1945 hashclr(b->sha1);
1946 hashclr(b->branch_tree.versions[0].sha1);
1947 hashclr(b->branch_tree.versions[1].sha1);
1948 if (b->branch_tree.tree) {
1949 release_tree_content_recursive(b->branch_tree.tree);
1950 b->branch_tree.tree = NULL;
1953 else
1954 b = new_branch(sp);
1955 read_next_command();
1956 cmd_from(b);
1959 static void cmd_checkpoint(void)
1961 if (object_count) {
1962 cycle_packfile();
1963 dump_branches();
1964 dump_tags();
1965 dump_marks();
1967 read_next_command();
1970 static const char fast_import_usage[] =
1971 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
1973 int main(int argc, const char **argv)
1975 int i, show_stats = 1;
1977 git_config(git_default_config);
1979 for (i = 1; i < argc; i++) {
1980 const char *a = argv[i];
1982 if (*a != '-' || !strcmp(a, "--"))
1983 break;
1984 else if (!prefixcmp(a, "--date-format=")) {
1985 const char *fmt = a + 14;
1986 if (!strcmp(fmt, "raw"))
1987 whenspec = WHENSPEC_RAW;
1988 else if (!strcmp(fmt, "rfc2822"))
1989 whenspec = WHENSPEC_RFC2822;
1990 else if (!strcmp(fmt, "now"))
1991 whenspec = WHENSPEC_NOW;
1992 else
1993 die("unknown --date-format argument %s", fmt);
1995 else if (!prefixcmp(a, "--max-pack-size="))
1996 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
1997 else if (!prefixcmp(a, "--depth="))
1998 max_depth = strtoul(a + 8, NULL, 0);
1999 else if (!prefixcmp(a, "--active-branches="))
2000 max_active_branches = strtoul(a + 18, NULL, 0);
2001 else if (!prefixcmp(a, "--export-marks="))
2002 mark_file = a + 15;
2003 else if (!prefixcmp(a, "--export-pack-edges=")) {
2004 if (pack_edges)
2005 fclose(pack_edges);
2006 pack_edges = fopen(a + 20, "a");
2007 if (!pack_edges)
2008 die("Cannot open %s: %s", a + 20, strerror(errno));
2009 } else if (!strcmp(a, "--force"))
2010 force_update = 1;
2011 else if (!strcmp(a, "--quiet"))
2012 show_stats = 0;
2013 else if (!strcmp(a, "--stats"))
2014 show_stats = 1;
2015 else
2016 die("unknown option %s", a);
2018 if (i != argc)
2019 usage(fast_import_usage);
2021 alloc_objects(object_entry_alloc);
2022 strbuf_init(&command_buf);
2024 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2025 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2026 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2027 marks = pool_calloc(1, sizeof(struct mark_set));
2029 start_packfile();
2030 for (;;) {
2031 read_next_command();
2032 if (command_buf.eof)
2033 break;
2034 else if (!strcmp("blob", command_buf.buf))
2035 cmd_new_blob();
2036 else if (!prefixcmp(command_buf.buf, "commit "))
2037 cmd_new_commit();
2038 else if (!prefixcmp(command_buf.buf, "tag "))
2039 cmd_new_tag();
2040 else if (!prefixcmp(command_buf.buf, "reset "))
2041 cmd_reset_branch();
2042 else if (!strcmp("checkpoint", command_buf.buf))
2043 cmd_checkpoint();
2044 else
2045 die("Unsupported command: %s", command_buf.buf);
2047 end_packfile();
2049 dump_branches();
2050 dump_tags();
2051 unkeep_all_packs();
2052 dump_marks();
2054 if (pack_edges)
2055 fclose(pack_edges);
2057 if (show_stats) {
2058 uintmax_t total_count = 0, duplicate_count = 0;
2059 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
2060 total_count += object_count_by_type[i];
2061 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
2062 duplicate_count += duplicate_count_by_type[i];
2064 fprintf(stderr, "%s statistics:\n", argv[0]);
2065 fprintf(stderr, "---------------------------------------------------------------------\n");
2066 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
2067 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
2068 fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
2069 fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
2070 fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
2071 fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
2072 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
2073 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
2074 fprintf(stderr, " atoms: %10u\n", atom_cnt);
2075 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
2076 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
2077 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
2078 fprintf(stderr, "---------------------------------------------------------------------\n");
2079 pack_report();
2080 fprintf(stderr, "---------------------------------------------------------------------\n");
2081 fprintf(stderr, "\n");
2084 return failure ? 1 : 0;