Revert "Add tcl/tk, so gitk and git-gui will work"
[msysgit/historical-msysgit.git] / git / fast-import.c
blob99a19d89e8b50417fc8abc86d9253276bc8943a8
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
30 | file_del
31 | file_rnm
32 | file_cpy
33 | file_obm
34 | file_inm;
35 file_clr ::= 'deleteall' lf;
36 file_del ::= 'D' sp path_str lf;
37 file_rnm ::= 'R' sp path_str sp path_str lf;
38 file_cpy ::= 'C' sp path_str sp path_str lf;
39 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
40 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
41 data;
43 new_tag ::= 'tag' sp tag_str lf
44 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
45 'tagger' sp name '<' email '>' when lf
46 tag_msg;
47 tag_msg ::= data;
49 reset_branch ::= 'reset' sp ref_str lf
50 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
51 lf;
53 checkpoint ::= 'checkpoint' lf
54 lf;
56 # note: the first idnum in a stream should be 1 and subsequent
57 # idnums should not have gaps between values as this will cause
58 # the stream parser to reserve space for the gapped values. An
59 # idnum can be updated in the future to a new object by issuing
60 # a new mark directive with the old idnum.
62 mark ::= 'mark' sp idnum lf;
63 data ::= (delimited_data | exact_data)
64 lf;
66 # note: delim may be any string but must not contain lf.
67 # data_line may contain any data but must not be exactly
68 # delim.
69 delimited_data ::= 'data' sp '<<' delim lf
70 (data_line lf)*
71 delim lf;
73 # note: declen indicates the length of binary_data in bytes.
74 # declen does not include the lf preceeding the binary data.
76 exact_data ::= 'data' sp declen lf
77 binary_data;
79 # note: quoted strings are C-style quoting supporting \c for
80 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
81 # is the signed byte value in octal. Note that the only
82 # characters which must actually be escaped to protect the
83 # stream formatting is: \, " and LF. Otherwise these values
84 # are UTF8.
86 ref_str ::= ref;
87 sha1exp_str ::= sha1exp;
88 tag_str ::= tag;
89 path_str ::= path | '"' quoted(path) '"' ;
90 mode ::= '100644' | '644'
91 | '100755' | '755'
92 | '120000'
95 declen ::= # unsigned 32 bit value, ascii base10 notation;
96 bigint ::= # unsigned integer value, ascii base10 notation;
97 binary_data ::= # file content, not interpreted;
99 when ::= raw_when | rfc2822_when;
100 raw_when ::= ts sp tz;
101 rfc2822_when ::= # Valid RFC 2822 date and time;
103 sp ::= # ASCII space character;
104 lf ::= # ASCII newline (LF) character;
106 # note: a colon (':') must precede the numerical value assigned to
107 # an idnum. This is to distinguish it from a ref or tag name as
108 # GIT does not permit ':' in ref or tag strings.
110 idnum ::= ':' bigint;
111 path ::= # GIT style file path, e.g. "a/b/c";
112 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
113 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
114 sha1exp ::= # Any valid GIT SHA1 expression;
115 hexsha1 ::= # SHA1 in hexadecimal format;
117 # note: name and email are UTF8 strings, however name must not
118 # contain '<' or lf and email must not contain any of the
119 # following: '<', '>', lf.
121 name ::= # valid GIT author/committer name;
122 email ::= # valid GIT author/committer email;
123 ts ::= # time since the epoch in seconds, ascii base10 notation;
124 tz ::= # GIT style timezone;
127 #include "builtin.h"
128 #include "cache.h"
129 #include "object.h"
130 #include "blob.h"
131 #include "tree.h"
132 #include "commit.h"
133 #include "delta.h"
134 #include "pack.h"
135 #include "refs.h"
136 #include "csum-file.h"
137 #include "strbuf.h"
138 #include "quote.h"
140 #define PACK_ID_BITS 16
141 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
143 struct object_entry
145 struct object_entry *next;
146 uint32_t offset;
147 unsigned type : TYPE_BITS;
148 unsigned pack_id : PACK_ID_BITS;
149 unsigned char sha1[20];
152 struct object_entry_pool
154 struct object_entry_pool *next_pool;
155 struct object_entry *next_free;
156 struct object_entry *end;
157 struct object_entry entries[FLEX_ARRAY]; /* more */
160 struct mark_set
162 union {
163 struct object_entry *marked[1024];
164 struct mark_set *sets[1024];
165 } data;
166 unsigned int shift;
169 struct last_object
171 void *data;
172 unsigned long len;
173 uint32_t offset;
174 unsigned int depth;
175 unsigned no_free:1;
178 struct mem_pool
180 struct mem_pool *next_pool;
181 char *next_free;
182 char *end;
183 char space[FLEX_ARRAY]; /* more */
186 struct atom_str
188 struct atom_str *next_atom;
189 unsigned short str_len;
190 char str_dat[FLEX_ARRAY]; /* more */
193 struct tree_content;
194 struct tree_entry
196 struct tree_content *tree;
197 struct atom_str* name;
198 struct tree_entry_ms
200 uint16_t mode;
201 unsigned char sha1[20];
202 } versions[2];
205 struct tree_content
207 unsigned int entry_capacity; /* must match avail_tree_content */
208 unsigned int entry_count;
209 unsigned int delta_depth;
210 struct tree_entry *entries[FLEX_ARRAY]; /* more */
213 struct avail_tree_content
215 unsigned int entry_capacity; /* must match tree_content */
216 struct avail_tree_content *next_avail;
219 struct branch
221 struct branch *table_next_branch;
222 struct branch *active_next_branch;
223 const char *name;
224 struct tree_entry branch_tree;
225 uintmax_t last_commit;
226 unsigned active : 1;
227 unsigned pack_id : PACK_ID_BITS;
228 unsigned char sha1[20];
231 struct tag
233 struct tag *next_tag;
234 const char *name;
235 unsigned int pack_id;
236 unsigned char sha1[20];
239 struct dbuf
241 void *buffer;
242 size_t capacity;
245 struct hash_list
247 struct hash_list *next;
248 unsigned char sha1[20];
251 typedef enum {
252 WHENSPEC_RAW = 1,
253 WHENSPEC_RFC2822,
254 WHENSPEC_NOW,
255 } whenspec_type;
257 /* Configured limits on output */
258 static unsigned long max_depth = 10;
259 static off_t max_packsize = (1LL << 32) - 1;
260 static int force_update;
262 /* Stats and misc. counters */
263 static uintmax_t alloc_count;
264 static uintmax_t marks_set_count;
265 static uintmax_t object_count_by_type[1 << TYPE_BITS];
266 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
267 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
268 static unsigned long object_count;
269 static unsigned long branch_count;
270 static unsigned long branch_load_count;
271 static int failure;
272 static FILE *pack_edges;
274 /* Memory pools */
275 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
276 static size_t total_allocd;
277 static struct mem_pool *mem_pool;
279 /* Atom management */
280 static unsigned int atom_table_sz = 4451;
281 static unsigned int atom_cnt;
282 static struct atom_str **atom_table;
284 /* The .pack file being generated */
285 static unsigned int pack_id;
286 static struct packed_git *pack_data;
287 static struct packed_git **all_packs;
288 static unsigned long pack_size;
290 /* Table of objects we've written. */
291 static unsigned int object_entry_alloc = 5000;
292 static struct object_entry_pool *blocks;
293 static struct object_entry *object_table[1 << 16];
294 static struct mark_set *marks;
295 static const char* mark_file;
297 /* Our last blob */
298 static struct last_object last_blob;
300 /* Tree management */
301 static unsigned int tree_entry_alloc = 1000;
302 static void *avail_tree_entry;
303 static unsigned int avail_tree_table_sz = 100;
304 static struct avail_tree_content **avail_tree_table;
305 static struct dbuf old_tree;
306 static struct dbuf new_tree;
308 /* Branch data */
309 static unsigned long max_active_branches = 5;
310 static unsigned long cur_active_branches;
311 static unsigned long branch_table_sz = 1039;
312 static struct branch **branch_table;
313 static struct branch *active_branches;
315 /* Tag data */
316 static struct tag *first_tag;
317 static struct tag *last_tag;
319 /* Input stream parsing */
320 static whenspec_type whenspec = WHENSPEC_RAW;
321 static struct strbuf command_buf;
322 static uintmax_t next_mark;
323 static struct dbuf new_data;
326 static void alloc_objects(unsigned int cnt)
328 struct object_entry_pool *b;
330 b = xmalloc(sizeof(struct object_entry_pool)
331 + cnt * sizeof(struct object_entry));
332 b->next_pool = blocks;
333 b->next_free = b->entries;
334 b->end = b->entries + cnt;
335 blocks = b;
336 alloc_count += cnt;
339 static struct object_entry *new_object(unsigned char *sha1)
341 struct object_entry *e;
343 if (blocks->next_free == blocks->end)
344 alloc_objects(object_entry_alloc);
346 e = blocks->next_free++;
347 hashcpy(e->sha1, sha1);
348 return e;
351 static struct object_entry *find_object(unsigned char *sha1)
353 unsigned int h = sha1[0] << 8 | sha1[1];
354 struct object_entry *e;
355 for (e = object_table[h]; e; e = e->next)
356 if (!hashcmp(sha1, e->sha1))
357 return e;
358 return NULL;
361 static struct object_entry *insert_object(unsigned char *sha1)
363 unsigned int h = sha1[0] << 8 | sha1[1];
364 struct object_entry *e = object_table[h];
365 struct object_entry *p = NULL;
367 while (e) {
368 if (!hashcmp(sha1, e->sha1))
369 return e;
370 p = e;
371 e = e->next;
374 e = new_object(sha1);
375 e->next = NULL;
376 e->offset = 0;
377 if (p)
378 p->next = e;
379 else
380 object_table[h] = e;
381 return e;
384 static unsigned int hc_str(const char *s, size_t len)
386 unsigned int r = 0;
387 while (len-- > 0)
388 r = r * 31 + *s++;
389 return r;
392 static void *pool_alloc(size_t len)
394 struct mem_pool *p;
395 void *r;
397 for (p = mem_pool; p; p = p->next_pool)
398 if ((p->end - p->next_free >= len))
399 break;
401 if (!p) {
402 if (len >= (mem_pool_alloc/2)) {
403 total_allocd += len;
404 return xmalloc(len);
406 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
407 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
408 p->next_pool = mem_pool;
409 p->next_free = p->space;
410 p->end = p->next_free + mem_pool_alloc;
411 mem_pool = p;
414 r = p->next_free;
415 /* round out to a pointer alignment */
416 if (len & (sizeof(void*) - 1))
417 len += sizeof(void*) - (len & (sizeof(void*) - 1));
418 p->next_free += len;
419 return r;
422 static void *pool_calloc(size_t count, size_t size)
424 size_t len = count * size;
425 void *r = pool_alloc(len);
426 memset(r, 0, len);
427 return r;
430 static char *pool_strdup(const char *s)
432 char *r = pool_alloc(strlen(s) + 1);
433 strcpy(r, s);
434 return r;
437 static void size_dbuf(struct dbuf *b, size_t maxlen)
439 if (b->buffer) {
440 if (b->capacity >= maxlen)
441 return;
442 free(b->buffer);
444 b->capacity = ((maxlen / 1024) + 1) * 1024;
445 b->buffer = xmalloc(b->capacity);
448 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
450 struct mark_set *s = marks;
451 while ((idnum >> s->shift) >= 1024) {
452 s = pool_calloc(1, sizeof(struct mark_set));
453 s->shift = marks->shift + 10;
454 s->data.sets[0] = marks;
455 marks = s;
457 while (s->shift) {
458 uintmax_t i = idnum >> s->shift;
459 idnum -= i << s->shift;
460 if (!s->data.sets[i]) {
461 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
462 s->data.sets[i]->shift = s->shift - 10;
464 s = s->data.sets[i];
466 if (!s->data.marked[idnum])
467 marks_set_count++;
468 s->data.marked[idnum] = oe;
471 static struct object_entry *find_mark(uintmax_t idnum)
473 uintmax_t orig_idnum = idnum;
474 struct mark_set *s = marks;
475 struct object_entry *oe = NULL;
476 if ((idnum >> s->shift) < 1024) {
477 while (s && s->shift) {
478 uintmax_t i = idnum >> s->shift;
479 idnum -= i << s->shift;
480 s = s->data.sets[i];
482 if (s)
483 oe = s->data.marked[idnum];
485 if (!oe)
486 die("mark :%" PRIuMAX " not declared", orig_idnum);
487 return oe;
490 static struct atom_str *to_atom(const char *s, unsigned short len)
492 unsigned int hc = hc_str(s, len) % atom_table_sz;
493 struct atom_str *c;
495 for (c = atom_table[hc]; c; c = c->next_atom)
496 if (c->str_len == len && !strncmp(s, c->str_dat, len))
497 return c;
499 c = pool_alloc(sizeof(struct atom_str) + len + 1);
500 c->str_len = len;
501 strncpy(c->str_dat, s, len);
502 c->str_dat[len] = 0;
503 c->next_atom = atom_table[hc];
504 atom_table[hc] = c;
505 atom_cnt++;
506 return c;
509 static struct branch *lookup_branch(const char *name)
511 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
512 struct branch *b;
514 for (b = branch_table[hc]; b; b = b->table_next_branch)
515 if (!strcmp(name, b->name))
516 return b;
517 return NULL;
520 static struct branch *new_branch(const char *name)
522 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
523 struct branch* b = lookup_branch(name);
525 if (b)
526 die("Invalid attempt to create duplicate branch: %s", name);
527 if (check_ref_format(name))
528 die("Branch name doesn't conform to GIT standards: %s", name);
530 b = pool_calloc(1, sizeof(struct branch));
531 b->name = pool_strdup(name);
532 b->table_next_branch = branch_table[hc];
533 b->branch_tree.versions[0].mode = S_IFDIR;
534 b->branch_tree.versions[1].mode = S_IFDIR;
535 b->active = 0;
536 b->pack_id = MAX_PACK_ID;
537 branch_table[hc] = b;
538 branch_count++;
539 return b;
542 static unsigned int hc_entries(unsigned int cnt)
544 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
545 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
548 static struct tree_content *new_tree_content(unsigned int cnt)
550 struct avail_tree_content *f, *l = NULL;
551 struct tree_content *t;
552 unsigned int hc = hc_entries(cnt);
554 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
555 if (f->entry_capacity >= cnt)
556 break;
558 if (f) {
559 if (l)
560 l->next_avail = f->next_avail;
561 else
562 avail_tree_table[hc] = f->next_avail;
563 } else {
564 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
565 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
566 f->entry_capacity = cnt;
569 t = (struct tree_content*)f;
570 t->entry_count = 0;
571 t->delta_depth = 0;
572 return t;
575 static void release_tree_entry(struct tree_entry *e);
576 static void release_tree_content(struct tree_content *t)
578 struct avail_tree_content *f = (struct avail_tree_content*)t;
579 unsigned int hc = hc_entries(f->entry_capacity);
580 f->next_avail = avail_tree_table[hc];
581 avail_tree_table[hc] = f;
584 static void release_tree_content_recursive(struct tree_content *t)
586 unsigned int i;
587 for (i = 0; i < t->entry_count; i++)
588 release_tree_entry(t->entries[i]);
589 release_tree_content(t);
592 static struct tree_content *grow_tree_content(
593 struct tree_content *t,
594 int amt)
596 struct tree_content *r = new_tree_content(t->entry_count + amt);
597 r->entry_count = t->entry_count;
598 r->delta_depth = t->delta_depth;
599 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
600 release_tree_content(t);
601 return r;
604 static struct tree_entry *new_tree_entry(void)
606 struct tree_entry *e;
608 if (!avail_tree_entry) {
609 unsigned int n = tree_entry_alloc;
610 total_allocd += n * sizeof(struct tree_entry);
611 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
612 while (n-- > 1) {
613 *((void**)e) = e + 1;
614 e++;
616 *((void**)e) = NULL;
619 e = avail_tree_entry;
620 avail_tree_entry = *((void**)e);
621 return e;
624 static void release_tree_entry(struct tree_entry *e)
626 if (e->tree)
627 release_tree_content_recursive(e->tree);
628 *((void**)e) = avail_tree_entry;
629 avail_tree_entry = e;
632 static struct tree_content *dup_tree_content(struct tree_content *s)
634 struct tree_content *d;
635 struct tree_entry *a, *b;
636 unsigned int i;
638 if (!s)
639 return NULL;
640 d = new_tree_content(s->entry_count);
641 for (i = 0; i < s->entry_count; i++) {
642 a = s->entries[i];
643 b = new_tree_entry();
644 memcpy(b, a, sizeof(*a));
645 if (a->tree && is_null_sha1(b->versions[1].sha1))
646 b->tree = dup_tree_content(a->tree);
647 else
648 b->tree = NULL;
649 d->entries[i] = b;
651 d->entry_count = s->entry_count;
652 d->delta_depth = s->delta_depth;
654 return d;
657 static void start_packfile(void)
659 static char tmpfile[PATH_MAX];
660 struct packed_git *p;
661 struct pack_header hdr;
662 int pack_fd;
664 snprintf(tmpfile, sizeof(tmpfile),
665 "%s/tmp_pack_XXXXXX", get_object_directory());
666 pack_fd = mkstemp(tmpfile);
667 if (pack_fd < 0)
668 die("Can't create %s: %s", tmpfile, strerror(errno));
669 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
670 strcpy(p->pack_name, tmpfile);
671 p->pack_fd = pack_fd;
673 hdr.hdr_signature = htonl(PACK_SIGNATURE);
674 hdr.hdr_version = htonl(2);
675 hdr.hdr_entries = 0;
676 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
678 pack_data = p;
679 pack_size = sizeof(hdr);
680 object_count = 0;
682 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
683 all_packs[pack_id] = p;
686 static int oecmp (const void *a_, const void *b_)
688 struct object_entry *a = *((struct object_entry**)a_);
689 struct object_entry *b = *((struct object_entry**)b_);
690 return hashcmp(a->sha1, b->sha1);
693 static char *create_index(void)
695 static char tmpfile[PATH_MAX];
696 SHA_CTX ctx;
697 struct sha1file *f;
698 struct object_entry **idx, **c, **last, *e;
699 struct object_entry_pool *o;
700 uint32_t array[256];
701 int i, idx_fd;
703 /* Build the sorted table of object IDs. */
704 idx = xmalloc(object_count * sizeof(struct object_entry*));
705 c = idx;
706 for (o = blocks; o; o = o->next_pool)
707 for (e = o->next_free; e-- != o->entries;)
708 if (pack_id == e->pack_id)
709 *c++ = e;
710 last = idx + object_count;
711 if (c != last)
712 die("internal consistency error creating the index");
713 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
715 /* Generate the fan-out array. */
716 c = idx;
717 for (i = 0; i < 256; i++) {
718 struct object_entry **next = c;;
719 while (next < last) {
720 if ((*next)->sha1[0] != i)
721 break;
722 next++;
724 array[i] = htonl(next - idx);
725 c = next;
728 snprintf(tmpfile, sizeof(tmpfile),
729 "%s/tmp_idx_XXXXXX", get_object_directory());
730 idx_fd = mkstemp(tmpfile);
731 if (idx_fd < 0)
732 die("Can't create %s: %s", tmpfile, strerror(errno));
733 f = sha1fd(idx_fd, tmpfile);
734 sha1write(f, array, 256 * sizeof(int));
735 SHA1_Init(&ctx);
736 for (c = idx; c != last; c++) {
737 uint32_t offset = htonl((*c)->offset);
738 sha1write(f, &offset, 4);
739 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
740 SHA1_Update(&ctx, (*c)->sha1, 20);
742 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
743 sha1close(f, NULL, 1);
744 free(idx);
745 SHA1_Final(pack_data->sha1, &ctx);
746 return tmpfile;
749 static char *keep_pack(char *curr_index_name)
751 static char name[PATH_MAX];
752 static const char *keep_msg = "fast-import";
753 int keep_fd;
755 chmod(pack_data->pack_name, 0444);
756 chmod(curr_index_name, 0444);
758 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
759 get_object_directory(), sha1_to_hex(pack_data->sha1));
760 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
761 if (keep_fd < 0)
762 die("cannot create keep file");
763 write(keep_fd, keep_msg, strlen(keep_msg));
764 close(keep_fd);
766 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
767 get_object_directory(), sha1_to_hex(pack_data->sha1));
768 if (move_temp_to_file(pack_data->pack_name, name))
769 die("cannot store pack file");
771 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
772 get_object_directory(), sha1_to_hex(pack_data->sha1));
773 if (move_temp_to_file(curr_index_name, name))
774 die("cannot store index file");
775 return name;
778 static void unkeep_all_packs(void)
780 static char name[PATH_MAX];
781 int k;
783 for (k = 0; k < pack_id; k++) {
784 struct packed_git *p = all_packs[k];
785 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
786 get_object_directory(), sha1_to_hex(p->sha1));
787 unlink(name);
791 static void end_packfile(void)
793 struct packed_git *old_p = pack_data, *new_p;
795 if (object_count) {
796 char *idx_name;
797 int i;
798 struct branch *b;
799 struct tag *t;
801 fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
802 pack_data->pack_name, object_count);
803 close(pack_data->pack_fd);
804 idx_name = keep_pack(create_index());
806 /* Register the packfile with core git's machinary. */
807 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
808 if (!new_p)
809 die("core git rejected index %s", idx_name);
810 new_p->windows = old_p->windows;
811 all_packs[pack_id] = new_p;
812 install_packed_git(new_p);
814 /* Print the boundary */
815 if (pack_edges) {
816 fprintf(pack_edges, "%s:", new_p->pack_name);
817 for (i = 0; i < branch_table_sz; i++) {
818 for (b = branch_table[i]; b; b = b->table_next_branch) {
819 if (b->pack_id == pack_id)
820 fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
823 for (t = first_tag; t; t = t->next_tag) {
824 if (t->pack_id == pack_id)
825 fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
827 fputc('\n', pack_edges);
828 fflush(pack_edges);
831 pack_id++;
833 else
834 unlink(old_p->pack_name);
835 free(old_p);
837 /* We can't carry a delta across packfiles. */
838 free(last_blob.data);
839 last_blob.data = NULL;
840 last_blob.len = 0;
841 last_blob.offset = 0;
842 last_blob.depth = 0;
845 static void cycle_packfile(void)
847 end_packfile();
848 start_packfile();
851 static size_t encode_header(
852 enum object_type type,
853 size_t size,
854 unsigned char *hdr)
856 int n = 1;
857 unsigned char c;
859 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
860 die("bad type %d", type);
862 c = (type << 4) | (size & 15);
863 size >>= 4;
864 while (size) {
865 *hdr++ = c | 0x80;
866 c = size & 0x7f;
867 size >>= 7;
868 n++;
870 *hdr = c;
871 return n;
874 static int store_object(
875 enum object_type type,
876 void *dat,
877 size_t datlen,
878 struct last_object *last,
879 unsigned char *sha1out,
880 uintmax_t mark)
882 void *out, *delta;
883 struct object_entry *e;
884 unsigned char hdr[96];
885 unsigned char sha1[20];
886 unsigned long hdrlen, deltalen;
887 SHA_CTX c;
888 z_stream s;
890 hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
891 (unsigned long)datlen) + 1;
892 SHA1_Init(&c);
893 SHA1_Update(&c, hdr, hdrlen);
894 SHA1_Update(&c, dat, datlen);
895 SHA1_Final(sha1, &c);
896 if (sha1out)
897 hashcpy(sha1out, sha1);
899 e = insert_object(sha1);
900 if (mark)
901 insert_mark(mark, e);
902 if (e->offset) {
903 duplicate_count_by_type[type]++;
904 return 1;
905 } else if (find_sha1_pack(sha1, packed_git)) {
906 e->type = type;
907 e->pack_id = MAX_PACK_ID;
908 e->offset = 1; /* just not zero! */
909 duplicate_count_by_type[type]++;
910 return 1;
913 if (last && last->data && last->depth < max_depth) {
914 delta = diff_delta(last->data, last->len,
915 dat, datlen,
916 &deltalen, 0);
917 if (delta && deltalen >= datlen) {
918 free(delta);
919 delta = NULL;
921 } else
922 delta = NULL;
924 memset(&s, 0, sizeof(s));
925 deflateInit(&s, zlib_compression_level);
926 if (delta) {
927 s.next_in = delta;
928 s.avail_in = deltalen;
929 } else {
930 s.next_in = dat;
931 s.avail_in = datlen;
933 s.avail_out = deflateBound(&s, s.avail_in);
934 s.next_out = out = xmalloc(s.avail_out);
935 while (deflate(&s, Z_FINISH) == Z_OK)
936 /* nothing */;
937 deflateEnd(&s);
939 /* Determine if we should auto-checkpoint. */
940 if ((pack_size + 60 + s.total_out) > max_packsize
941 || (pack_size + 60 + s.total_out) < pack_size) {
943 /* This new object needs to *not* have the current pack_id. */
944 e->pack_id = pack_id + 1;
945 cycle_packfile();
947 /* We cannot carry a delta into the new pack. */
948 if (delta) {
949 free(delta);
950 delta = NULL;
952 memset(&s, 0, sizeof(s));
953 deflateInit(&s, zlib_compression_level);
954 s.next_in = dat;
955 s.avail_in = datlen;
956 s.avail_out = deflateBound(&s, s.avail_in);
957 s.next_out = out = xrealloc(out, s.avail_out);
958 while (deflate(&s, Z_FINISH) == Z_OK)
959 /* nothing */;
960 deflateEnd(&s);
964 e->type = type;
965 e->pack_id = pack_id;
966 e->offset = pack_size;
967 object_count++;
968 object_count_by_type[type]++;
970 if (delta) {
971 unsigned long ofs = e->offset - last->offset;
972 unsigned pos = sizeof(hdr) - 1;
974 delta_count_by_type[type]++;
975 last->depth++;
977 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
978 write_or_die(pack_data->pack_fd, hdr, hdrlen);
979 pack_size += hdrlen;
981 hdr[pos] = ofs & 127;
982 while (ofs >>= 7)
983 hdr[--pos] = 128 | (--ofs & 127);
984 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
985 pack_size += sizeof(hdr) - pos;
986 } else {
987 if (last)
988 last->depth = 0;
989 hdrlen = encode_header(type, datlen, hdr);
990 write_or_die(pack_data->pack_fd, hdr, hdrlen);
991 pack_size += hdrlen;
994 write_or_die(pack_data->pack_fd, out, s.total_out);
995 pack_size += s.total_out;
997 free(out);
998 free(delta);
999 if (last) {
1000 if (!last->no_free)
1001 free(last->data);
1002 last->data = dat;
1003 last->offset = e->offset;
1004 last->len = datlen;
1006 return 0;
1009 static void *gfi_unpack_entry(
1010 struct object_entry *oe,
1011 unsigned long *sizep)
1013 enum object_type type;
1014 struct packed_git *p = all_packs[oe->pack_id];
1015 if (p == pack_data)
1016 p->pack_size = pack_size + 20;
1017 return unpack_entry(p, oe->offset, &type, sizep);
1020 static const char *get_mode(const char *str, uint16_t *modep)
1022 unsigned char c;
1023 uint16_t mode = 0;
1025 while ((c = *str++) != ' ') {
1026 if (c < '0' || c > '7')
1027 return NULL;
1028 mode = (mode << 3) + (c - '0');
1030 *modep = mode;
1031 return str;
1034 static void load_tree(struct tree_entry *root)
1036 unsigned char* sha1 = root->versions[1].sha1;
1037 struct object_entry *myoe;
1038 struct tree_content *t;
1039 unsigned long size;
1040 char *buf;
1041 const char *c;
1043 root->tree = t = new_tree_content(8);
1044 if (is_null_sha1(sha1))
1045 return;
1047 myoe = find_object(sha1);
1048 if (myoe && myoe->pack_id != MAX_PACK_ID) {
1049 if (myoe->type != OBJ_TREE)
1050 die("Not a tree: %s", sha1_to_hex(sha1));
1051 t->delta_depth = 0;
1052 buf = gfi_unpack_entry(myoe, &size);
1053 } else {
1054 enum object_type type;
1055 buf = read_sha1_file(sha1, &type, &size);
1056 if (!buf || type != OBJ_TREE)
1057 die("Can't load tree %s", sha1_to_hex(sha1));
1060 c = buf;
1061 while (c != (buf + size)) {
1062 struct tree_entry *e = new_tree_entry();
1064 if (t->entry_count == t->entry_capacity)
1065 root->tree = t = grow_tree_content(t, t->entry_count);
1066 t->entries[t->entry_count++] = e;
1068 e->tree = NULL;
1069 c = get_mode(c, &e->versions[1].mode);
1070 if (!c)
1071 die("Corrupt mode in %s", sha1_to_hex(sha1));
1072 e->versions[0].mode = e->versions[1].mode;
1073 e->name = to_atom(c, strlen(c));
1074 c += e->name->str_len + 1;
1075 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1076 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1077 c += 20;
1079 free(buf);
1082 static int tecmp0 (const void *_a, const void *_b)
1084 struct tree_entry *a = *((struct tree_entry**)_a);
1085 struct tree_entry *b = *((struct tree_entry**)_b);
1086 return base_name_compare(
1087 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1088 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1091 static int tecmp1 (const void *_a, const void *_b)
1093 struct tree_entry *a = *((struct tree_entry**)_a);
1094 struct tree_entry *b = *((struct tree_entry**)_b);
1095 return base_name_compare(
1096 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1097 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1100 static void mktree(struct tree_content *t,
1101 int v,
1102 unsigned long *szp,
1103 struct dbuf *b)
1105 size_t maxlen = 0;
1106 unsigned int i;
1107 char *c;
1109 if (!v)
1110 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1111 else
1112 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1114 for (i = 0; i < t->entry_count; i++) {
1115 if (t->entries[i]->versions[v].mode)
1116 maxlen += t->entries[i]->name->str_len + 34;
1119 size_dbuf(b, maxlen);
1120 c = b->buffer;
1121 for (i = 0; i < t->entry_count; i++) {
1122 struct tree_entry *e = t->entries[i];
1123 if (!e->versions[v].mode)
1124 continue;
1125 c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1126 *c++ = ' ';
1127 strcpy(c, e->name->str_dat);
1128 c += e->name->str_len + 1;
1129 hashcpy((unsigned char*)c, e->versions[v].sha1);
1130 c += 20;
1132 *szp = c - (char*)b->buffer;
1135 static void store_tree(struct tree_entry *root)
1137 struct tree_content *t = root->tree;
1138 unsigned int i, j, del;
1139 unsigned long new_len;
1140 struct last_object lo;
1141 struct object_entry *le;
1143 if (!is_null_sha1(root->versions[1].sha1))
1144 return;
1146 for (i = 0; i < t->entry_count; i++) {
1147 if (t->entries[i]->tree)
1148 store_tree(t->entries[i]);
1151 le = find_object(root->versions[0].sha1);
1152 if (!S_ISDIR(root->versions[0].mode)
1153 || !le
1154 || le->pack_id != pack_id) {
1155 lo.data = NULL;
1156 lo.depth = 0;
1157 lo.no_free = 0;
1158 } else {
1159 mktree(t, 0, &lo.len, &old_tree);
1160 lo.data = old_tree.buffer;
1161 lo.offset = le->offset;
1162 lo.depth = t->delta_depth;
1163 lo.no_free = 1;
1166 mktree(t, 1, &new_len, &new_tree);
1167 store_object(OBJ_TREE, new_tree.buffer, new_len,
1168 &lo, root->versions[1].sha1, 0);
1170 t->delta_depth = lo.depth;
1171 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1172 struct tree_entry *e = t->entries[i];
1173 if (e->versions[1].mode) {
1174 e->versions[0].mode = e->versions[1].mode;
1175 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1176 t->entries[j++] = e;
1177 } else {
1178 release_tree_entry(e);
1179 del++;
1182 t->entry_count -= del;
1185 static int tree_content_set(
1186 struct tree_entry *root,
1187 const char *p,
1188 const unsigned char *sha1,
1189 const uint16_t mode,
1190 struct tree_content *subtree)
1192 struct tree_content *t = root->tree;
1193 const char *slash1;
1194 unsigned int i, n;
1195 struct tree_entry *e;
1197 slash1 = strchr(p, '/');
1198 if (slash1)
1199 n = slash1 - p;
1200 else
1201 n = strlen(p);
1202 if (!n)
1203 die("Empty path component found in input");
1204 if (!slash1 && !S_ISDIR(mode) && subtree)
1205 die("Non-directories cannot have subtrees");
1207 for (i = 0; i < t->entry_count; i++) {
1208 e = t->entries[i];
1209 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1210 if (!slash1) {
1211 if (!S_ISDIR(mode)
1212 && e->versions[1].mode == mode
1213 && !hashcmp(e->versions[1].sha1, sha1))
1214 return 0;
1215 e->versions[1].mode = mode;
1216 hashcpy(e->versions[1].sha1, sha1);
1217 if (e->tree)
1218 release_tree_content_recursive(e->tree);
1219 e->tree = subtree;
1220 hashclr(root->versions[1].sha1);
1221 return 1;
1223 if (!S_ISDIR(e->versions[1].mode)) {
1224 e->tree = new_tree_content(8);
1225 e->versions[1].mode = S_IFDIR;
1227 if (!e->tree)
1228 load_tree(e);
1229 if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
1230 hashclr(root->versions[1].sha1);
1231 return 1;
1233 return 0;
1237 if (t->entry_count == t->entry_capacity)
1238 root->tree = t = grow_tree_content(t, t->entry_count);
1239 e = new_tree_entry();
1240 e->name = to_atom(p, n);
1241 e->versions[0].mode = 0;
1242 hashclr(e->versions[0].sha1);
1243 t->entries[t->entry_count++] = e;
1244 if (slash1) {
1245 e->tree = new_tree_content(8);
1246 e->versions[1].mode = S_IFDIR;
1247 tree_content_set(e, slash1 + 1, sha1, mode, subtree);
1248 } else {
1249 e->tree = subtree;
1250 e->versions[1].mode = mode;
1251 hashcpy(e->versions[1].sha1, sha1);
1253 hashclr(root->versions[1].sha1);
1254 return 1;
1257 static int tree_content_remove(
1258 struct tree_entry *root,
1259 const char *p,
1260 struct tree_entry *backup_leaf)
1262 struct tree_content *t = root->tree;
1263 const char *slash1;
1264 unsigned int i, n;
1265 struct tree_entry *e;
1267 slash1 = strchr(p, '/');
1268 if (slash1)
1269 n = slash1 - p;
1270 else
1271 n = strlen(p);
1273 for (i = 0; i < t->entry_count; i++) {
1274 e = t->entries[i];
1275 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1276 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1277 goto del_entry;
1278 if (!e->tree)
1279 load_tree(e);
1280 if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
1281 for (n = 0; n < e->tree->entry_count; n++) {
1282 if (e->tree->entries[n]->versions[1].mode) {
1283 hashclr(root->versions[1].sha1);
1284 return 1;
1287 backup_leaf = NULL;
1288 goto del_entry;
1290 return 0;
1293 return 0;
1295 del_entry:
1296 if (backup_leaf)
1297 memcpy(backup_leaf, e, sizeof(*backup_leaf));
1298 else if (e->tree)
1299 release_tree_content_recursive(e->tree);
1300 e->tree = NULL;
1301 e->versions[1].mode = 0;
1302 hashclr(e->versions[1].sha1);
1303 hashclr(root->versions[1].sha1);
1304 return 1;
1307 static int tree_content_get(
1308 struct tree_entry *root,
1309 const char *p,
1310 struct tree_entry *leaf)
1312 struct tree_content *t = root->tree;
1313 const char *slash1;
1314 unsigned int i, n;
1315 struct tree_entry *e;
1317 slash1 = strchr(p, '/');
1318 if (slash1)
1319 n = slash1 - p;
1320 else
1321 n = strlen(p);
1323 for (i = 0; i < t->entry_count; i++) {
1324 e = t->entries[i];
1325 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1326 if (!slash1) {
1327 memcpy(leaf, e, sizeof(*leaf));
1328 if (e->tree && is_null_sha1(e->versions[1].sha1))
1329 leaf->tree = dup_tree_content(e->tree);
1330 else
1331 leaf->tree = NULL;
1332 return 1;
1334 if (!S_ISDIR(e->versions[1].mode))
1335 return 0;
1336 if (!e->tree)
1337 load_tree(e);
1338 return tree_content_get(e, slash1 + 1, leaf);
1341 return 0;
1344 static int update_branch(struct branch *b)
1346 static const char *msg = "fast-import";
1347 struct ref_lock *lock;
1348 unsigned char old_sha1[20];
1350 if (read_ref(b->name, old_sha1))
1351 hashclr(old_sha1);
1352 lock = lock_any_ref_for_update(b->name, old_sha1, 0);
1353 if (!lock)
1354 return error("Unable to lock %s", b->name);
1355 if (!force_update && !is_null_sha1(old_sha1)) {
1356 struct commit *old_cmit, *new_cmit;
1358 old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1359 new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1360 if (!old_cmit || !new_cmit) {
1361 unlock_ref(lock);
1362 return error("Branch %s is missing commits.", b->name);
1365 if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
1366 unlock_ref(lock);
1367 warning("Not updating %s"
1368 " (new tip %s does not contain %s)",
1369 b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1370 return -1;
1373 if (write_ref_sha1(lock, b->sha1, msg) < 0)
1374 return error("Unable to update %s", b->name);
1375 return 0;
1378 static void dump_branches(void)
1380 unsigned int i;
1381 struct branch *b;
1383 for (i = 0; i < branch_table_sz; i++) {
1384 for (b = branch_table[i]; b; b = b->table_next_branch)
1385 failure |= update_branch(b);
1389 static void dump_tags(void)
1391 static const char *msg = "fast-import";
1392 struct tag *t;
1393 struct ref_lock *lock;
1394 char ref_name[PATH_MAX];
1396 for (t = first_tag; t; t = t->next_tag) {
1397 sprintf(ref_name, "tags/%s", t->name);
1398 lock = lock_ref_sha1(ref_name, NULL);
1399 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1400 failure |= error("Unable to update %s", ref_name);
1404 static void dump_marks_helper(FILE *f,
1405 uintmax_t base,
1406 struct mark_set *m)
1408 uintmax_t k;
1409 if (m->shift) {
1410 for (k = 0; k < 1024; k++) {
1411 if (m->data.sets[k])
1412 dump_marks_helper(f, (base + k) << m->shift,
1413 m->data.sets[k]);
1415 } else {
1416 for (k = 0; k < 1024; k++) {
1417 if (m->data.marked[k])
1418 fprintf(f, ":%" PRIuMAX " %s\n", base + k,
1419 sha1_to_hex(m->data.marked[k]->sha1));
1424 static void dump_marks(void)
1426 static struct lock_file mark_lock;
1427 int mark_fd;
1428 FILE *f;
1430 if (!mark_file)
1431 return;
1433 mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
1434 if (mark_fd < 0) {
1435 failure |= error("Unable to write marks file %s: %s",
1436 mark_file, strerror(errno));
1437 return;
1440 f = fdopen(mark_fd, "w");
1441 if (!f) {
1442 rollback_lock_file(&mark_lock);
1443 failure |= error("Unable to write marks file %s: %s",
1444 mark_file, strerror(errno));
1445 return;
1448 dump_marks_helper(f, 0, marks);
1449 fclose(f);
1450 if (commit_lock_file(&mark_lock))
1451 failure |= error("Unable to write marks file %s: %s",
1452 mark_file, strerror(errno));
1455 static void read_next_command(void)
1457 read_line(&command_buf, stdin, '\n');
1460 static void cmd_mark(void)
1462 if (!prefixcmp(command_buf.buf, "mark :")) {
1463 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1464 read_next_command();
1466 else
1467 next_mark = 0;
1470 static void *cmd_data (size_t *size)
1472 size_t length;
1473 char *buffer;
1475 if (prefixcmp(command_buf.buf, "data "))
1476 die("Expected 'data n' command, found: %s", command_buf.buf);
1478 if (!prefixcmp(command_buf.buf + 5, "<<")) {
1479 char *term = xstrdup(command_buf.buf + 5 + 2);
1480 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1481 length = 0;
1482 buffer = xmalloc(sz);
1483 for (;;) {
1484 read_next_command();
1485 if (command_buf.eof)
1486 die("EOF in data (terminator '%s' not found)", term);
1487 if (term_len == command_buf.len
1488 && !strcmp(term, command_buf.buf))
1489 break;
1490 if (sz < (length + command_buf.len)) {
1491 sz = sz * 3 / 2 + 16;
1492 if (sz < (length + command_buf.len))
1493 sz = length + command_buf.len;
1494 buffer = xrealloc(buffer, sz);
1496 memcpy(buffer + length,
1497 command_buf.buf,
1498 command_buf.len - 1);
1499 length += command_buf.len - 1;
1500 buffer[length++] = '\n';
1502 free(term);
1504 else {
1505 size_t n = 0;
1506 length = strtoul(command_buf.buf + 5, NULL, 10);
1507 buffer = xmalloc(length);
1508 while (n < length) {
1509 size_t s = fread(buffer + n, 1, length - n, stdin);
1510 if (!s && feof(stdin))
1511 die("EOF in data (%lu bytes remaining)",
1512 (unsigned long)(length - n));
1513 n += s;
1517 if (fgetc(stdin) != '\n')
1518 die("An lf did not trail the binary data as expected.");
1520 *size = length;
1521 return buffer;
1524 static int validate_raw_date(const char *src, char *result, int maxlen)
1526 const char *orig_src = src;
1527 char *endp, sign;
1529 strtoul(src, &endp, 10);
1530 if (endp == src || *endp != ' ')
1531 return -1;
1533 src = endp + 1;
1534 if (*src != '-' && *src != '+')
1535 return -1;
1536 sign = *src;
1538 strtoul(src + 1, &endp, 10);
1539 if (endp == src || *endp || (endp - orig_src) >= maxlen)
1540 return -1;
1542 strcpy(result, orig_src);
1543 return 0;
1546 static char *parse_ident(const char *buf)
1548 const char *gt;
1549 size_t name_len;
1550 char *ident;
1552 gt = strrchr(buf, '>');
1553 if (!gt)
1554 die("Missing > in ident string: %s", buf);
1555 gt++;
1556 if (*gt != ' ')
1557 die("Missing space after > in ident string: %s", buf);
1558 gt++;
1559 name_len = gt - buf;
1560 ident = xmalloc(name_len + 24);
1561 strncpy(ident, buf, name_len);
1563 switch (whenspec) {
1564 case WHENSPEC_RAW:
1565 if (validate_raw_date(gt, ident + name_len, 24) < 0)
1566 die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1567 break;
1568 case WHENSPEC_RFC2822:
1569 if (parse_date(gt, ident + name_len, 24) < 0)
1570 die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1571 break;
1572 case WHENSPEC_NOW:
1573 if (strcmp("now", gt))
1574 die("Date in ident must be 'now': %s", buf);
1575 datestamp(ident + name_len, 24);
1576 break;
1579 return ident;
1582 static void cmd_new_blob(void)
1584 size_t l;
1585 void *d;
1587 read_next_command();
1588 cmd_mark();
1589 d = cmd_data(&l);
1591 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1592 free(d);
1595 static void unload_one_branch(void)
1597 while (cur_active_branches
1598 && cur_active_branches >= max_active_branches) {
1599 uintmax_t min_commit = ULONG_MAX;
1600 struct branch *e, *l = NULL, *p = NULL;
1602 for (e = active_branches; e; e = e->active_next_branch) {
1603 if (e->last_commit < min_commit) {
1604 p = l;
1605 min_commit = e->last_commit;
1607 l = e;
1610 if (p) {
1611 e = p->active_next_branch;
1612 p->active_next_branch = e->active_next_branch;
1613 } else {
1614 e = active_branches;
1615 active_branches = e->active_next_branch;
1617 e->active = 0;
1618 e->active_next_branch = NULL;
1619 if (e->branch_tree.tree) {
1620 release_tree_content_recursive(e->branch_tree.tree);
1621 e->branch_tree.tree = NULL;
1623 cur_active_branches--;
1627 static void load_branch(struct branch *b)
1629 load_tree(&b->branch_tree);
1630 if (!b->active) {
1631 b->active = 1;
1632 b->active_next_branch = active_branches;
1633 active_branches = b;
1634 cur_active_branches++;
1635 branch_load_count++;
1639 static void file_change_m(struct branch *b)
1641 const char *p = command_buf.buf + 2;
1642 char *p_uq;
1643 const char *endp;
1644 struct object_entry *oe = oe;
1645 unsigned char sha1[20];
1646 uint16_t mode, inline_data = 0;
1648 p = get_mode(p, &mode);
1649 if (!p)
1650 die("Corrupt mode: %s", command_buf.buf);
1651 switch (mode) {
1652 case S_IFREG | 0644:
1653 case S_IFREG | 0755:
1654 case S_IFLNK:
1655 case 0644:
1656 case 0755:
1657 /* ok */
1658 break;
1659 default:
1660 die("Corrupt mode: %s", command_buf.buf);
1663 if (*p == ':') {
1664 char *x;
1665 oe = find_mark(strtoumax(p + 1, &x, 10));
1666 hashcpy(sha1, oe->sha1);
1667 p = x;
1668 } else if (!prefixcmp(p, "inline")) {
1669 inline_data = 1;
1670 p += 6;
1671 } else {
1672 if (get_sha1_hex(p, sha1))
1673 die("Invalid SHA1: %s", command_buf.buf);
1674 oe = find_object(sha1);
1675 p += 40;
1677 if (*p++ != ' ')
1678 die("Missing space after SHA1: %s", command_buf.buf);
1680 p_uq = unquote_c_style(p, &endp);
1681 if (p_uq) {
1682 if (*endp)
1683 die("Garbage after path in: %s", command_buf.buf);
1684 p = p_uq;
1687 if (inline_data) {
1688 size_t l;
1689 void *d;
1690 if (!p_uq)
1691 p = p_uq = xstrdup(p);
1692 read_next_command();
1693 d = cmd_data(&l);
1694 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1695 free(d);
1696 } else if (oe) {
1697 if (oe->type != OBJ_BLOB)
1698 die("Not a blob (actually a %s): %s",
1699 command_buf.buf, typename(oe->type));
1700 } else {
1701 enum object_type type = sha1_object_info(sha1, NULL);
1702 if (type < 0)
1703 die("Blob not found: %s", command_buf.buf);
1704 if (type != OBJ_BLOB)
1705 die("Not a blob (actually a %s): %s",
1706 typename(type), command_buf.buf);
1709 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
1710 free(p_uq);
1713 static void file_change_d(struct branch *b)
1715 const char *p = command_buf.buf + 2;
1716 char *p_uq;
1717 const char *endp;
1719 p_uq = unquote_c_style(p, &endp);
1720 if (p_uq) {
1721 if (*endp)
1722 die("Garbage after path in: %s", command_buf.buf);
1723 p = p_uq;
1725 tree_content_remove(&b->branch_tree, p, NULL);
1726 free(p_uq);
1729 static void file_change_cr(struct branch *b, int rename)
1731 const char *s, *d;
1732 char *s_uq, *d_uq;
1733 const char *endp;
1734 struct tree_entry leaf;
1736 s = command_buf.buf + 2;
1737 s_uq = unquote_c_style(s, &endp);
1738 if (s_uq) {
1739 if (*endp != ' ')
1740 die("Missing space after source: %s", command_buf.buf);
1742 else {
1743 endp = strchr(s, ' ');
1744 if (!endp)
1745 die("Missing space after source: %s", command_buf.buf);
1746 s_uq = xmalloc(endp - s + 1);
1747 memcpy(s_uq, s, endp - s);
1748 s_uq[endp - s] = 0;
1750 s = s_uq;
1752 endp++;
1753 if (!*endp)
1754 die("Missing dest: %s", command_buf.buf);
1756 d = endp;
1757 d_uq = unquote_c_style(d, &endp);
1758 if (d_uq) {
1759 if (*endp)
1760 die("Garbage after dest in: %s", command_buf.buf);
1761 d = d_uq;
1764 memset(&leaf, 0, sizeof(leaf));
1765 if (rename)
1766 tree_content_remove(&b->branch_tree, s, &leaf);
1767 else
1768 tree_content_get(&b->branch_tree, s, &leaf);
1769 if (!leaf.versions[1].mode)
1770 die("Path %s not in branch", s);
1771 tree_content_set(&b->branch_tree, d,
1772 leaf.versions[1].sha1,
1773 leaf.versions[1].mode,
1774 leaf.tree);
1776 free(s_uq);
1777 free(d_uq);
1780 static void file_change_deleteall(struct branch *b)
1782 release_tree_content_recursive(b->branch_tree.tree);
1783 hashclr(b->branch_tree.versions[0].sha1);
1784 hashclr(b->branch_tree.versions[1].sha1);
1785 load_tree(&b->branch_tree);
1788 static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
1790 if (!buf || size < 46)
1791 die("Not a valid commit: %s", sha1_to_hex(b->sha1));
1792 if (memcmp("tree ", buf, 5)
1793 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1794 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1795 hashcpy(b->branch_tree.versions[0].sha1,
1796 b->branch_tree.versions[1].sha1);
1799 static void cmd_from_existing(struct branch *b)
1801 if (is_null_sha1(b->sha1)) {
1802 hashclr(b->branch_tree.versions[0].sha1);
1803 hashclr(b->branch_tree.versions[1].sha1);
1804 } else {
1805 unsigned long size;
1806 char *buf;
1808 buf = read_object_with_reference(b->sha1,
1809 commit_type, &size, b->sha1);
1810 cmd_from_commit(b, buf, size);
1811 free(buf);
1815 static void cmd_from(struct branch *b)
1817 const char *from;
1818 struct branch *s;
1820 if (prefixcmp(command_buf.buf, "from "))
1821 return;
1823 if (b->branch_tree.tree) {
1824 release_tree_content_recursive(b->branch_tree.tree);
1825 b->branch_tree.tree = NULL;
1828 from = strchr(command_buf.buf, ' ') + 1;
1829 s = lookup_branch(from);
1830 if (b == s)
1831 die("Can't create a branch from itself: %s", b->name);
1832 else if (s) {
1833 unsigned char *t = s->branch_tree.versions[1].sha1;
1834 hashcpy(b->sha1, s->sha1);
1835 hashcpy(b->branch_tree.versions[0].sha1, t);
1836 hashcpy(b->branch_tree.versions[1].sha1, t);
1837 } else if (*from == ':') {
1838 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1839 struct object_entry *oe = find_mark(idnum);
1840 if (oe->type != OBJ_COMMIT)
1841 die("Mark :%" PRIuMAX " not a commit", idnum);
1842 hashcpy(b->sha1, oe->sha1);
1843 if (oe->pack_id != MAX_PACK_ID) {
1844 unsigned long size;
1845 char *buf = gfi_unpack_entry(oe, &size);
1846 cmd_from_commit(b, buf, size);
1847 free(buf);
1848 } else
1849 cmd_from_existing(b);
1850 } else if (!get_sha1(from, b->sha1))
1851 cmd_from_existing(b);
1852 else
1853 die("Invalid ref name or SHA1 expression: %s", from);
1855 read_next_command();
1858 static struct hash_list *cmd_merge(unsigned int *count)
1860 struct hash_list *list = NULL, *n, *e = e;
1861 const char *from;
1862 struct branch *s;
1864 *count = 0;
1865 while (!prefixcmp(command_buf.buf, "merge ")) {
1866 from = strchr(command_buf.buf, ' ') + 1;
1867 n = xmalloc(sizeof(*n));
1868 s = lookup_branch(from);
1869 if (s)
1870 hashcpy(n->sha1, s->sha1);
1871 else if (*from == ':') {
1872 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1873 struct object_entry *oe = find_mark(idnum);
1874 if (oe->type != OBJ_COMMIT)
1875 die("Mark :%" PRIuMAX " not a commit", idnum);
1876 hashcpy(n->sha1, oe->sha1);
1877 } else if (!get_sha1(from, n->sha1)) {
1878 unsigned long size;
1879 char *buf = read_object_with_reference(n->sha1,
1880 commit_type, &size, n->sha1);
1881 if (!buf || size < 46)
1882 die("Not a valid commit: %s", from);
1883 free(buf);
1884 } else
1885 die("Invalid ref name or SHA1 expression: %s", from);
1887 n->next = NULL;
1888 if (list)
1889 e->next = n;
1890 else
1891 list = n;
1892 e = n;
1893 (*count)++;
1894 read_next_command();
1896 return list;
1899 static void cmd_new_commit(void)
1901 struct branch *b;
1902 void *msg;
1903 size_t msglen;
1904 char *sp;
1905 char *author = NULL;
1906 char *committer = NULL;
1907 struct hash_list *merge_list = NULL;
1908 unsigned int merge_count;
1910 /* Obtain the branch name from the rest of our command */
1911 sp = strchr(command_buf.buf, ' ') + 1;
1912 b = lookup_branch(sp);
1913 if (!b)
1914 b = new_branch(sp);
1916 read_next_command();
1917 cmd_mark();
1918 if (!prefixcmp(command_buf.buf, "author ")) {
1919 author = parse_ident(command_buf.buf + 7);
1920 read_next_command();
1922 if (!prefixcmp(command_buf.buf, "committer ")) {
1923 committer = parse_ident(command_buf.buf + 10);
1924 read_next_command();
1926 if (!committer)
1927 die("Expected committer but didn't get one");
1928 msg = cmd_data(&msglen);
1929 read_next_command();
1930 cmd_from(b);
1931 merge_list = cmd_merge(&merge_count);
1933 /* ensure the branch is active/loaded */
1934 if (!b->branch_tree.tree || !max_active_branches) {
1935 unload_one_branch();
1936 load_branch(b);
1939 /* file_change* */
1940 for (;;) {
1941 if (1 == command_buf.len)
1942 break;
1943 else if (!prefixcmp(command_buf.buf, "M "))
1944 file_change_m(b);
1945 else if (!prefixcmp(command_buf.buf, "D "))
1946 file_change_d(b);
1947 else if (!prefixcmp(command_buf.buf, "R "))
1948 file_change_cr(b, 1);
1949 else if (!prefixcmp(command_buf.buf, "C "))
1950 file_change_cr(b, 0);
1951 else if (!strcmp("deleteall", command_buf.buf))
1952 file_change_deleteall(b);
1953 else
1954 die("Unsupported file_change: %s", command_buf.buf);
1955 read_next_command();
1958 /* build the tree and the commit */
1959 store_tree(&b->branch_tree);
1960 hashcpy(b->branch_tree.versions[0].sha1,
1961 b->branch_tree.versions[1].sha1);
1962 size_dbuf(&new_data, 114 + msglen
1963 + merge_count * 49
1964 + (author
1965 ? strlen(author) + strlen(committer)
1966 : 2 * strlen(committer)));
1967 sp = new_data.buffer;
1968 sp += sprintf(sp, "tree %s\n",
1969 sha1_to_hex(b->branch_tree.versions[1].sha1));
1970 if (!is_null_sha1(b->sha1))
1971 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1972 while (merge_list) {
1973 struct hash_list *next = merge_list->next;
1974 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1975 free(merge_list);
1976 merge_list = next;
1978 sp += sprintf(sp, "author %s\n", author ? author : committer);
1979 sp += sprintf(sp, "committer %s\n", committer);
1980 *sp++ = '\n';
1981 memcpy(sp, msg, msglen);
1982 sp += msglen;
1983 free(author);
1984 free(committer);
1985 free(msg);
1987 if (!store_object(OBJ_COMMIT,
1988 new_data.buffer, sp - (char*)new_data.buffer,
1989 NULL, b->sha1, next_mark))
1990 b->pack_id = pack_id;
1991 b->last_commit = object_count_by_type[OBJ_COMMIT];
1994 static void cmd_new_tag(void)
1996 char *sp;
1997 const char *from;
1998 char *tagger;
1999 struct branch *s;
2000 void *msg;
2001 size_t msglen;
2002 struct tag *t;
2003 uintmax_t from_mark = 0;
2004 unsigned char sha1[20];
2006 /* Obtain the new tag name from the rest of our command */
2007 sp = strchr(command_buf.buf, ' ') + 1;
2008 t = pool_alloc(sizeof(struct tag));
2009 t->next_tag = NULL;
2010 t->name = pool_strdup(sp);
2011 if (last_tag)
2012 last_tag->next_tag = t;
2013 else
2014 first_tag = t;
2015 last_tag = t;
2016 read_next_command();
2018 /* from ... */
2019 if (prefixcmp(command_buf.buf, "from "))
2020 die("Expected from command, got %s", command_buf.buf);
2021 from = strchr(command_buf.buf, ' ') + 1;
2022 s = lookup_branch(from);
2023 if (s) {
2024 hashcpy(sha1, s->sha1);
2025 } else if (*from == ':') {
2026 struct object_entry *oe;
2027 from_mark = strtoumax(from + 1, NULL, 10);
2028 oe = find_mark(from_mark);
2029 if (oe->type != OBJ_COMMIT)
2030 die("Mark :%" PRIuMAX " not a commit", from_mark);
2031 hashcpy(sha1, oe->sha1);
2032 } else if (!get_sha1(from, sha1)) {
2033 unsigned long size;
2034 char *buf;
2036 buf = read_object_with_reference(sha1,
2037 commit_type, &size, sha1);
2038 if (!buf || size < 46)
2039 die("Not a valid commit: %s", from);
2040 free(buf);
2041 } else
2042 die("Invalid ref name or SHA1 expression: %s", from);
2043 read_next_command();
2045 /* tagger ... */
2046 if (prefixcmp(command_buf.buf, "tagger "))
2047 die("Expected tagger command, got %s", command_buf.buf);
2048 tagger = parse_ident(command_buf.buf + 7);
2050 /* tag payload/message */
2051 read_next_command();
2052 msg = cmd_data(&msglen);
2054 /* build the tag object */
2055 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
2056 sp = new_data.buffer;
2057 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
2058 sp += sprintf(sp, "type %s\n", commit_type);
2059 sp += sprintf(sp, "tag %s\n", t->name);
2060 sp += sprintf(sp, "tagger %s\n", tagger);
2061 *sp++ = '\n';
2062 memcpy(sp, msg, msglen);
2063 sp += msglen;
2064 free(tagger);
2065 free(msg);
2067 if (store_object(OBJ_TAG, new_data.buffer,
2068 sp - (char*)new_data.buffer,
2069 NULL, t->sha1, 0))
2070 t->pack_id = MAX_PACK_ID;
2071 else
2072 t->pack_id = pack_id;
2075 static void cmd_reset_branch(void)
2077 struct branch *b;
2078 char *sp;
2080 /* Obtain the branch name from the rest of our command */
2081 sp = strchr(command_buf.buf, ' ') + 1;
2082 b = lookup_branch(sp);
2083 if (b) {
2084 hashclr(b->sha1);
2085 hashclr(b->branch_tree.versions[0].sha1);
2086 hashclr(b->branch_tree.versions[1].sha1);
2087 if (b->branch_tree.tree) {
2088 release_tree_content_recursive(b->branch_tree.tree);
2089 b->branch_tree.tree = NULL;
2092 else
2093 b = new_branch(sp);
2094 read_next_command();
2095 cmd_from(b);
2098 static void cmd_checkpoint(void)
2100 if (object_count) {
2101 cycle_packfile();
2102 dump_branches();
2103 dump_tags();
2104 dump_marks();
2106 read_next_command();
2109 static void import_marks(const char *input_file)
2111 char line[512];
2112 FILE *f = fopen(input_file, "r");
2113 if (!f)
2114 die("cannot read %s: %s", input_file, strerror(errno));
2115 while (fgets(line, sizeof(line), f)) {
2116 uintmax_t mark;
2117 char *end;
2118 unsigned char sha1[20];
2119 struct object_entry *e;
2121 end = strchr(line, '\n');
2122 if (line[0] != ':' || !end)
2123 die("corrupt mark line: %s", line);
2124 *end = 0;
2125 mark = strtoumax(line + 1, &end, 10);
2126 if (!mark || end == line + 1
2127 || *end != ' ' || get_sha1(end + 1, sha1))
2128 die("corrupt mark line: %s", line);
2129 e = find_object(sha1);
2130 if (!e) {
2131 enum object_type type = sha1_object_info(sha1, NULL);
2132 if (type < 0)
2133 die("object not found: %s", sha1_to_hex(sha1));
2134 e = insert_object(sha1);
2135 e->type = type;
2136 e->pack_id = MAX_PACK_ID;
2137 e->offset = 1; /* just not zero! */
2139 insert_mark(mark, e);
2141 fclose(f);
2144 static const char fast_import_usage[] =
2145 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2147 int main(int argc, const char **argv)
2149 int i, show_stats = 1;
2151 git_config(git_default_config);
2152 alloc_objects(object_entry_alloc);
2153 strbuf_init(&command_buf);
2154 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2155 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2156 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2157 marks = pool_calloc(1, sizeof(struct mark_set));
2159 for (i = 1; i < argc; i++) {
2160 const char *a = argv[i];
2162 if (*a != '-' || !strcmp(a, "--"))
2163 break;
2164 else if (!prefixcmp(a, "--date-format=")) {
2165 const char *fmt = a + 14;
2166 if (!strcmp(fmt, "raw"))
2167 whenspec = WHENSPEC_RAW;
2168 else if (!strcmp(fmt, "rfc2822"))
2169 whenspec = WHENSPEC_RFC2822;
2170 else if (!strcmp(fmt, "now"))
2171 whenspec = WHENSPEC_NOW;
2172 else
2173 die("unknown --date-format argument %s", fmt);
2175 else if (!prefixcmp(a, "--max-pack-size="))
2176 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
2177 else if (!prefixcmp(a, "--depth="))
2178 max_depth = strtoul(a + 8, NULL, 0);
2179 else if (!prefixcmp(a, "--active-branches="))
2180 max_active_branches = strtoul(a + 18, NULL, 0);
2181 else if (!prefixcmp(a, "--import-marks="))
2182 import_marks(a + 15);
2183 else if (!prefixcmp(a, "--export-marks="))
2184 mark_file = a + 15;
2185 else if (!prefixcmp(a, "--export-pack-edges=")) {
2186 if (pack_edges)
2187 fclose(pack_edges);
2188 pack_edges = fopen(a + 20, "a");
2189 if (!pack_edges)
2190 die("Cannot open %s: %s", a + 20, strerror(errno));
2191 } else if (!strcmp(a, "--force"))
2192 force_update = 1;
2193 else if (!strcmp(a, "--quiet"))
2194 show_stats = 0;
2195 else if (!strcmp(a, "--stats"))
2196 show_stats = 1;
2197 else
2198 die("unknown option %s", a);
2200 if (i != argc)
2201 usage(fast_import_usage);
2203 prepare_packed_git();
2204 start_packfile();
2205 for (;;) {
2206 read_next_command();
2207 if (command_buf.eof)
2208 break;
2209 else if (!strcmp("blob", command_buf.buf))
2210 cmd_new_blob();
2211 else if (!prefixcmp(command_buf.buf, "commit "))
2212 cmd_new_commit();
2213 else if (!prefixcmp(command_buf.buf, "tag "))
2214 cmd_new_tag();
2215 else if (!prefixcmp(command_buf.buf, "reset "))
2216 cmd_reset_branch();
2217 else if (!strcmp("checkpoint", command_buf.buf))
2218 cmd_checkpoint();
2219 else
2220 die("Unsupported command: %s", command_buf.buf);
2222 end_packfile();
2224 dump_branches();
2225 dump_tags();
2226 unkeep_all_packs();
2227 dump_marks();
2229 if (pack_edges)
2230 fclose(pack_edges);
2232 if (show_stats) {
2233 uintmax_t total_count = 0, duplicate_count = 0;
2234 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
2235 total_count += object_count_by_type[i];
2236 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
2237 duplicate_count += duplicate_count_by_type[i];
2239 fprintf(stderr, "%s statistics:\n", argv[0]);
2240 fprintf(stderr, "---------------------------------------------------------------------\n");
2241 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
2242 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
2243 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]);
2244 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]);
2245 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]);
2246 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]);
2247 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
2248 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
2249 fprintf(stderr, " atoms: %10u\n", atom_cnt);
2250 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
2251 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
2252 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
2253 fprintf(stderr, "---------------------------------------------------------------------\n");
2254 pack_report();
2255 fprintf(stderr, "---------------------------------------------------------------------\n");
2256 fprintf(stderr, "\n");
2259 return failure ? 1 : 0;