Documentation/git-commit-tree: remove description of a nonexistent limitation
[git/jrn.git] / fast-import.c
bloba1cb13f09b1d661c6fff75d51378e3e63d211f20
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_rnm | file_obm | file_inm;
30 file_clr ::= 'deleteall' lf;
31 file_del ::= 'D' sp path_str lf;
32 file_rnm ::= 'R' sp path_str sp path_str lf;
33 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
34 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
35 data;
37 new_tag ::= 'tag' sp tag_str lf
38 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
39 'tagger' sp name '<' email '>' when lf
40 tag_msg;
41 tag_msg ::= data;
43 reset_branch ::= 'reset' sp ref_str lf
44 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
45 lf;
47 checkpoint ::= 'checkpoint' lf
48 lf;
50 # note: the first idnum in a stream should be 1 and subsequent
51 # idnums should not have gaps between values as this will cause
52 # the stream parser to reserve space for the gapped values. An
53 # idnum can be updated in the future to a new object by issuing
54 # a new mark directive with the old idnum.
56 mark ::= 'mark' sp idnum lf;
57 data ::= (delimited_data | exact_data)
58 lf;
60 # note: delim may be any string but must not contain lf.
61 # data_line may contain any data but must not be exactly
62 # delim.
63 delimited_data ::= 'data' sp '<<' delim lf
64 (data_line lf)*
65 delim lf;
67 # note: declen indicates the length of binary_data in bytes.
68 # declen does not include the lf preceeding the binary data.
70 exact_data ::= 'data' sp declen lf
71 binary_data;
73 # note: quoted strings are C-style quoting supporting \c for
74 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
75 # is the signed byte value in octal. Note that the only
76 # characters which must actually be escaped to protect the
77 # stream formatting is: \, " and LF. Otherwise these values
78 # are UTF8.
80 ref_str ::= ref;
81 sha1exp_str ::= sha1exp;
82 tag_str ::= tag;
83 path_str ::= path | '"' quoted(path) '"' ;
84 mode ::= '100644' | '644'
85 | '100755' | '755'
86 | '120000'
89 declen ::= # unsigned 32 bit value, ascii base10 notation;
90 bigint ::= # unsigned integer value, ascii base10 notation;
91 binary_data ::= # file content, not interpreted;
93 when ::= raw_when | rfc2822_when;
94 raw_when ::= ts sp tz;
95 rfc2822_when ::= # Valid RFC 2822 date and time;
97 sp ::= # ASCII space character;
98 lf ::= # ASCII newline (LF) character;
100 # note: a colon (':') must precede the numerical value assigned to
101 # an idnum. This is to distinguish it from a ref or tag name as
102 # GIT does not permit ':' in ref or tag strings.
104 idnum ::= ':' bigint;
105 path ::= # GIT style file path, e.g. "a/b/c";
106 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
107 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
108 sha1exp ::= # Any valid GIT SHA1 expression;
109 hexsha1 ::= # SHA1 in hexadecimal format;
111 # note: name and email are UTF8 strings, however name must not
112 # contain '<' or lf and email must not contain any of the
113 # following: '<', '>', lf.
115 name ::= # valid GIT author/committer name;
116 email ::= # valid GIT author/committer email;
117 ts ::= # time since the epoch in seconds, ascii base10 notation;
118 tz ::= # GIT style timezone;
121 #include "builtin.h"
122 #include "cache.h"
123 #include "object.h"
124 #include "blob.h"
125 #include "tree.h"
126 #include "commit.h"
127 #include "delta.h"
128 #include "pack.h"
129 #include "refs.h"
130 #include "csum-file.h"
131 #include "strbuf.h"
132 #include "quote.h"
134 #define PACK_ID_BITS 16
135 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
137 struct object_entry
139 struct object_entry *next;
140 uint32_t offset;
141 unsigned type : TYPE_BITS;
142 unsigned pack_id : PACK_ID_BITS;
143 unsigned char sha1[20];
146 struct object_entry_pool
148 struct object_entry_pool *next_pool;
149 struct object_entry *next_free;
150 struct object_entry *end;
151 struct object_entry entries[FLEX_ARRAY]; /* more */
154 struct mark_set
156 union {
157 struct object_entry *marked[1024];
158 struct mark_set *sets[1024];
159 } data;
160 unsigned int shift;
163 struct last_object
165 void *data;
166 unsigned long len;
167 uint32_t offset;
168 unsigned int depth;
169 unsigned no_free:1;
172 struct mem_pool
174 struct mem_pool *next_pool;
175 char *next_free;
176 char *end;
177 char space[FLEX_ARRAY]; /* more */
180 struct atom_str
182 struct atom_str *next_atom;
183 unsigned short str_len;
184 char str_dat[FLEX_ARRAY]; /* more */
187 struct tree_content;
188 struct tree_entry
190 struct tree_content *tree;
191 struct atom_str* name;
192 struct tree_entry_ms
194 uint16_t mode;
195 unsigned char sha1[20];
196 } versions[2];
199 struct tree_content
201 unsigned int entry_capacity; /* must match avail_tree_content */
202 unsigned int entry_count;
203 unsigned int delta_depth;
204 struct tree_entry *entries[FLEX_ARRAY]; /* more */
207 struct avail_tree_content
209 unsigned int entry_capacity; /* must match tree_content */
210 struct avail_tree_content *next_avail;
213 struct branch
215 struct branch *table_next_branch;
216 struct branch *active_next_branch;
217 const char *name;
218 struct tree_entry branch_tree;
219 uintmax_t last_commit;
220 unsigned active : 1;
221 unsigned pack_id : PACK_ID_BITS;
222 unsigned char sha1[20];
225 struct tag
227 struct tag *next_tag;
228 const char *name;
229 unsigned int pack_id;
230 unsigned char sha1[20];
233 struct dbuf
235 void *buffer;
236 size_t capacity;
239 struct hash_list
241 struct hash_list *next;
242 unsigned char sha1[20];
245 typedef enum {
246 WHENSPEC_RAW = 1,
247 WHENSPEC_RFC2822,
248 WHENSPEC_NOW,
249 } whenspec_type;
251 /* Configured limits on output */
252 static unsigned long max_depth = 10;
253 static off_t max_packsize = (1LL << 32) - 1;
254 static int force_update;
256 /* Stats and misc. counters */
257 static uintmax_t alloc_count;
258 static uintmax_t marks_set_count;
259 static uintmax_t object_count_by_type[1 << TYPE_BITS];
260 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
261 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
262 static unsigned long object_count;
263 static unsigned long branch_count;
264 static unsigned long branch_load_count;
265 static int failure;
266 static FILE *pack_edges;
268 /* Memory pools */
269 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
270 static size_t total_allocd;
271 static struct mem_pool *mem_pool;
273 /* Atom management */
274 static unsigned int atom_table_sz = 4451;
275 static unsigned int atom_cnt;
276 static struct atom_str **atom_table;
278 /* The .pack file being generated */
279 static unsigned int pack_id;
280 static struct packed_git *pack_data;
281 static struct packed_git **all_packs;
282 static unsigned long pack_size;
284 /* Table of objects we've written. */
285 static unsigned int object_entry_alloc = 5000;
286 static struct object_entry_pool *blocks;
287 static struct object_entry *object_table[1 << 16];
288 static struct mark_set *marks;
289 static const char* mark_file;
291 /* Our last blob */
292 static struct last_object last_blob;
294 /* Tree management */
295 static unsigned int tree_entry_alloc = 1000;
296 static void *avail_tree_entry;
297 static unsigned int avail_tree_table_sz = 100;
298 static struct avail_tree_content **avail_tree_table;
299 static struct dbuf old_tree;
300 static struct dbuf new_tree;
302 /* Branch data */
303 static unsigned long max_active_branches = 5;
304 static unsigned long cur_active_branches;
305 static unsigned long branch_table_sz = 1039;
306 static struct branch **branch_table;
307 static struct branch *active_branches;
309 /* Tag data */
310 static struct tag *first_tag;
311 static struct tag *last_tag;
313 /* Input stream parsing */
314 static whenspec_type whenspec = WHENSPEC_RAW;
315 static struct strbuf command_buf;
316 static uintmax_t next_mark;
317 static struct dbuf new_data;
320 static void alloc_objects(unsigned int cnt)
322 struct object_entry_pool *b;
324 b = xmalloc(sizeof(struct object_entry_pool)
325 + cnt * sizeof(struct object_entry));
326 b->next_pool = blocks;
327 b->next_free = b->entries;
328 b->end = b->entries + cnt;
329 blocks = b;
330 alloc_count += cnt;
333 static struct object_entry *new_object(unsigned char *sha1)
335 struct object_entry *e;
337 if (blocks->next_free == blocks->end)
338 alloc_objects(object_entry_alloc);
340 e = blocks->next_free++;
341 hashcpy(e->sha1, sha1);
342 return e;
345 static struct object_entry *find_object(unsigned char *sha1)
347 unsigned int h = sha1[0] << 8 | sha1[1];
348 struct object_entry *e;
349 for (e = object_table[h]; e; e = e->next)
350 if (!hashcmp(sha1, e->sha1))
351 return e;
352 return NULL;
355 static struct object_entry *insert_object(unsigned char *sha1)
357 unsigned int h = sha1[0] << 8 | sha1[1];
358 struct object_entry *e = object_table[h];
359 struct object_entry *p = NULL;
361 while (e) {
362 if (!hashcmp(sha1, e->sha1))
363 return e;
364 p = e;
365 e = e->next;
368 e = new_object(sha1);
369 e->next = NULL;
370 e->offset = 0;
371 if (p)
372 p->next = e;
373 else
374 object_table[h] = e;
375 return e;
378 static unsigned int hc_str(const char *s, size_t len)
380 unsigned int r = 0;
381 while (len-- > 0)
382 r = r * 31 + *s++;
383 return r;
386 static void *pool_alloc(size_t len)
388 struct mem_pool *p;
389 void *r;
391 for (p = mem_pool; p; p = p->next_pool)
392 if ((p->end - p->next_free >= len))
393 break;
395 if (!p) {
396 if (len >= (mem_pool_alloc/2)) {
397 total_allocd += len;
398 return xmalloc(len);
400 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
401 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
402 p->next_pool = mem_pool;
403 p->next_free = p->space;
404 p->end = p->next_free + mem_pool_alloc;
405 mem_pool = p;
408 r = p->next_free;
409 /* round out to a pointer alignment */
410 if (len & (sizeof(void*) - 1))
411 len += sizeof(void*) - (len & (sizeof(void*) - 1));
412 p->next_free += len;
413 return r;
416 static void *pool_calloc(size_t count, size_t size)
418 size_t len = count * size;
419 void *r = pool_alloc(len);
420 memset(r, 0, len);
421 return r;
424 static char *pool_strdup(const char *s)
426 char *r = pool_alloc(strlen(s) + 1);
427 strcpy(r, s);
428 return r;
431 static void size_dbuf(struct dbuf *b, size_t maxlen)
433 if (b->buffer) {
434 if (b->capacity >= maxlen)
435 return;
436 free(b->buffer);
438 b->capacity = ((maxlen / 1024) + 1) * 1024;
439 b->buffer = xmalloc(b->capacity);
442 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
444 struct mark_set *s = marks;
445 while ((idnum >> s->shift) >= 1024) {
446 s = pool_calloc(1, sizeof(struct mark_set));
447 s->shift = marks->shift + 10;
448 s->data.sets[0] = marks;
449 marks = s;
451 while (s->shift) {
452 uintmax_t i = idnum >> s->shift;
453 idnum -= i << s->shift;
454 if (!s->data.sets[i]) {
455 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
456 s->data.sets[i]->shift = s->shift - 10;
458 s = s->data.sets[i];
460 if (!s->data.marked[idnum])
461 marks_set_count++;
462 s->data.marked[idnum] = oe;
465 static struct object_entry *find_mark(uintmax_t idnum)
467 uintmax_t orig_idnum = idnum;
468 struct mark_set *s = marks;
469 struct object_entry *oe = NULL;
470 if ((idnum >> s->shift) < 1024) {
471 while (s && s->shift) {
472 uintmax_t i = idnum >> s->shift;
473 idnum -= i << s->shift;
474 s = s->data.sets[i];
476 if (s)
477 oe = s->data.marked[idnum];
479 if (!oe)
480 die("mark :%" PRIuMAX " not declared", orig_idnum);
481 return oe;
484 static struct atom_str *to_atom(const char *s, unsigned short len)
486 unsigned int hc = hc_str(s, len) % atom_table_sz;
487 struct atom_str *c;
489 for (c = atom_table[hc]; c; c = c->next_atom)
490 if (c->str_len == len && !strncmp(s, c->str_dat, len))
491 return c;
493 c = pool_alloc(sizeof(struct atom_str) + len + 1);
494 c->str_len = len;
495 strncpy(c->str_dat, s, len);
496 c->str_dat[len] = 0;
497 c->next_atom = atom_table[hc];
498 atom_table[hc] = c;
499 atom_cnt++;
500 return c;
503 static struct branch *lookup_branch(const char *name)
505 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
506 struct branch *b;
508 for (b = branch_table[hc]; b; b = b->table_next_branch)
509 if (!strcmp(name, b->name))
510 return b;
511 return NULL;
514 static struct branch *new_branch(const char *name)
516 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
517 struct branch* b = lookup_branch(name);
519 if (b)
520 die("Invalid attempt to create duplicate branch: %s", name);
521 if (check_ref_format(name))
522 die("Branch name doesn't conform to GIT standards: %s", name);
524 b = pool_calloc(1, sizeof(struct branch));
525 b->name = pool_strdup(name);
526 b->table_next_branch = branch_table[hc];
527 b->branch_tree.versions[0].mode = S_IFDIR;
528 b->branch_tree.versions[1].mode = S_IFDIR;
529 b->active = 0;
530 b->pack_id = MAX_PACK_ID;
531 branch_table[hc] = b;
532 branch_count++;
533 return b;
536 static unsigned int hc_entries(unsigned int cnt)
538 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
539 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
542 static struct tree_content *new_tree_content(unsigned int cnt)
544 struct avail_tree_content *f, *l = NULL;
545 struct tree_content *t;
546 unsigned int hc = hc_entries(cnt);
548 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
549 if (f->entry_capacity >= cnt)
550 break;
552 if (f) {
553 if (l)
554 l->next_avail = f->next_avail;
555 else
556 avail_tree_table[hc] = f->next_avail;
557 } else {
558 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
559 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
560 f->entry_capacity = cnt;
563 t = (struct tree_content*)f;
564 t->entry_count = 0;
565 t->delta_depth = 0;
566 return t;
569 static void release_tree_entry(struct tree_entry *e);
570 static void release_tree_content(struct tree_content *t)
572 struct avail_tree_content *f = (struct avail_tree_content*)t;
573 unsigned int hc = hc_entries(f->entry_capacity);
574 f->next_avail = avail_tree_table[hc];
575 avail_tree_table[hc] = f;
578 static void release_tree_content_recursive(struct tree_content *t)
580 unsigned int i;
581 for (i = 0; i < t->entry_count; i++)
582 release_tree_entry(t->entries[i]);
583 release_tree_content(t);
586 static struct tree_content *grow_tree_content(
587 struct tree_content *t,
588 int amt)
590 struct tree_content *r = new_tree_content(t->entry_count + amt);
591 r->entry_count = t->entry_count;
592 r->delta_depth = t->delta_depth;
593 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
594 release_tree_content(t);
595 return r;
598 static struct tree_entry *new_tree_entry(void)
600 struct tree_entry *e;
602 if (!avail_tree_entry) {
603 unsigned int n = tree_entry_alloc;
604 total_allocd += n * sizeof(struct tree_entry);
605 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
606 while (n-- > 1) {
607 *((void**)e) = e + 1;
608 e++;
610 *((void**)e) = NULL;
613 e = avail_tree_entry;
614 avail_tree_entry = *((void**)e);
615 return e;
618 static void release_tree_entry(struct tree_entry *e)
620 if (e->tree)
621 release_tree_content_recursive(e->tree);
622 *((void**)e) = avail_tree_entry;
623 avail_tree_entry = e;
626 static void start_packfile(void)
628 static char tmpfile[PATH_MAX];
629 struct packed_git *p;
630 struct pack_header hdr;
631 int pack_fd;
633 snprintf(tmpfile, sizeof(tmpfile),
634 "%s/tmp_pack_XXXXXX", get_object_directory());
635 pack_fd = mkstemp(tmpfile);
636 if (pack_fd < 0)
637 die("Can't create %s: %s", tmpfile, strerror(errno));
638 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
639 strcpy(p->pack_name, tmpfile);
640 p->pack_fd = pack_fd;
642 hdr.hdr_signature = htonl(PACK_SIGNATURE);
643 hdr.hdr_version = htonl(2);
644 hdr.hdr_entries = 0;
645 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
647 pack_data = p;
648 pack_size = sizeof(hdr);
649 object_count = 0;
651 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
652 all_packs[pack_id] = p;
655 static int oecmp (const void *a_, const void *b_)
657 struct object_entry *a = *((struct object_entry**)a_);
658 struct object_entry *b = *((struct object_entry**)b_);
659 return hashcmp(a->sha1, b->sha1);
662 static char *create_index(void)
664 static char tmpfile[PATH_MAX];
665 SHA_CTX ctx;
666 struct sha1file *f;
667 struct object_entry **idx, **c, **last, *e;
668 struct object_entry_pool *o;
669 uint32_t array[256];
670 int i, idx_fd;
672 /* Build the sorted table of object IDs. */
673 idx = xmalloc(object_count * sizeof(struct object_entry*));
674 c = idx;
675 for (o = blocks; o; o = o->next_pool)
676 for (e = o->next_free; e-- != o->entries;)
677 if (pack_id == e->pack_id)
678 *c++ = e;
679 last = idx + object_count;
680 if (c != last)
681 die("internal consistency error creating the index");
682 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
684 /* Generate the fan-out array. */
685 c = idx;
686 for (i = 0; i < 256; i++) {
687 struct object_entry **next = c;;
688 while (next < last) {
689 if ((*next)->sha1[0] != i)
690 break;
691 next++;
693 array[i] = htonl(next - idx);
694 c = next;
697 snprintf(tmpfile, sizeof(tmpfile),
698 "%s/tmp_idx_XXXXXX", get_object_directory());
699 idx_fd = mkstemp(tmpfile);
700 if (idx_fd < 0)
701 die("Can't create %s: %s", tmpfile, strerror(errno));
702 f = sha1fd(idx_fd, tmpfile);
703 sha1write(f, array, 256 * sizeof(int));
704 SHA1_Init(&ctx);
705 for (c = idx; c != last; c++) {
706 uint32_t offset = htonl((*c)->offset);
707 sha1write(f, &offset, 4);
708 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
709 SHA1_Update(&ctx, (*c)->sha1, 20);
711 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
712 sha1close(f, NULL, 1);
713 free(idx);
714 SHA1_Final(pack_data->sha1, &ctx);
715 return tmpfile;
718 static char *keep_pack(char *curr_index_name)
720 static char name[PATH_MAX];
721 static const char *keep_msg = "fast-import";
722 int keep_fd;
724 chmod(pack_data->pack_name, 0444);
725 chmod(curr_index_name, 0444);
727 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
728 get_object_directory(), sha1_to_hex(pack_data->sha1));
729 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
730 if (keep_fd < 0)
731 die("cannot create keep file");
732 write(keep_fd, keep_msg, strlen(keep_msg));
733 close(keep_fd);
735 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
736 get_object_directory(), sha1_to_hex(pack_data->sha1));
737 if (move_temp_to_file(pack_data->pack_name, name))
738 die("cannot store pack file");
740 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
741 get_object_directory(), sha1_to_hex(pack_data->sha1));
742 if (move_temp_to_file(curr_index_name, name))
743 die("cannot store index file");
744 return name;
747 static void unkeep_all_packs(void)
749 static char name[PATH_MAX];
750 int k;
752 for (k = 0; k < pack_id; k++) {
753 struct packed_git *p = all_packs[k];
754 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
755 get_object_directory(), sha1_to_hex(p->sha1));
756 unlink(name);
760 static void end_packfile(void)
762 struct packed_git *old_p = pack_data, *new_p;
764 if (object_count) {
765 char *idx_name;
766 int i;
767 struct branch *b;
768 struct tag *t;
770 fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
771 pack_data->pack_name, object_count);
772 close(pack_data->pack_fd);
773 idx_name = keep_pack(create_index());
775 /* Register the packfile with core git's machinary. */
776 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
777 if (!new_p)
778 die("core git rejected index %s", idx_name);
779 new_p->windows = old_p->windows;
780 all_packs[pack_id] = new_p;
781 install_packed_git(new_p);
783 /* Print the boundary */
784 if (pack_edges) {
785 fprintf(pack_edges, "%s:", new_p->pack_name);
786 for (i = 0; i < branch_table_sz; i++) {
787 for (b = branch_table[i]; b; b = b->table_next_branch) {
788 if (b->pack_id == pack_id)
789 fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
792 for (t = first_tag; t; t = t->next_tag) {
793 if (t->pack_id == pack_id)
794 fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
796 fputc('\n', pack_edges);
797 fflush(pack_edges);
800 pack_id++;
802 else
803 unlink(old_p->pack_name);
804 free(old_p);
806 /* We can't carry a delta across packfiles. */
807 free(last_blob.data);
808 last_blob.data = NULL;
809 last_blob.len = 0;
810 last_blob.offset = 0;
811 last_blob.depth = 0;
814 static void cycle_packfile(void)
816 end_packfile();
817 start_packfile();
820 static size_t encode_header(
821 enum object_type type,
822 size_t size,
823 unsigned char *hdr)
825 int n = 1;
826 unsigned char c;
828 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
829 die("bad type %d", type);
831 c = (type << 4) | (size & 15);
832 size >>= 4;
833 while (size) {
834 *hdr++ = c | 0x80;
835 c = size & 0x7f;
836 size >>= 7;
837 n++;
839 *hdr = c;
840 return n;
843 static int store_object(
844 enum object_type type,
845 void *dat,
846 size_t datlen,
847 struct last_object *last,
848 unsigned char *sha1out,
849 uintmax_t mark)
851 void *out, *delta;
852 struct object_entry *e;
853 unsigned char hdr[96];
854 unsigned char sha1[20];
855 unsigned long hdrlen, deltalen;
856 SHA_CTX c;
857 z_stream s;
859 hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
860 (unsigned long)datlen) + 1;
861 SHA1_Init(&c);
862 SHA1_Update(&c, hdr, hdrlen);
863 SHA1_Update(&c, dat, datlen);
864 SHA1_Final(sha1, &c);
865 if (sha1out)
866 hashcpy(sha1out, sha1);
868 e = insert_object(sha1);
869 if (mark)
870 insert_mark(mark, e);
871 if (e->offset) {
872 duplicate_count_by_type[type]++;
873 return 1;
874 } else if (find_sha1_pack(sha1, packed_git)) {
875 e->type = type;
876 e->pack_id = MAX_PACK_ID;
877 e->offset = 1; /* just not zero! */
878 duplicate_count_by_type[type]++;
879 return 1;
882 if (last && last->data && last->depth < max_depth) {
883 delta = diff_delta(last->data, last->len,
884 dat, datlen,
885 &deltalen, 0);
886 if (delta && deltalen >= datlen) {
887 free(delta);
888 delta = NULL;
890 } else
891 delta = NULL;
893 memset(&s, 0, sizeof(s));
894 deflateInit(&s, zlib_compression_level);
895 if (delta) {
896 s.next_in = delta;
897 s.avail_in = deltalen;
898 } else {
899 s.next_in = dat;
900 s.avail_in = datlen;
902 s.avail_out = deflateBound(&s, s.avail_in);
903 s.next_out = out = xmalloc(s.avail_out);
904 while (deflate(&s, Z_FINISH) == Z_OK)
905 /* nothing */;
906 deflateEnd(&s);
908 /* Determine if we should auto-checkpoint. */
909 if ((pack_size + 60 + s.total_out) > max_packsize
910 || (pack_size + 60 + s.total_out) < pack_size) {
912 /* This new object needs to *not* have the current pack_id. */
913 e->pack_id = pack_id + 1;
914 cycle_packfile();
916 /* We cannot carry a delta into the new pack. */
917 if (delta) {
918 free(delta);
919 delta = NULL;
921 memset(&s, 0, sizeof(s));
922 deflateInit(&s, zlib_compression_level);
923 s.next_in = dat;
924 s.avail_in = datlen;
925 s.avail_out = deflateBound(&s, s.avail_in);
926 s.next_out = out = xrealloc(out, s.avail_out);
927 while (deflate(&s, Z_FINISH) == Z_OK)
928 /* nothing */;
929 deflateEnd(&s);
933 e->type = type;
934 e->pack_id = pack_id;
935 e->offset = pack_size;
936 object_count++;
937 object_count_by_type[type]++;
939 if (delta) {
940 unsigned long ofs = e->offset - last->offset;
941 unsigned pos = sizeof(hdr) - 1;
943 delta_count_by_type[type]++;
944 last->depth++;
946 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
947 write_or_die(pack_data->pack_fd, hdr, hdrlen);
948 pack_size += hdrlen;
950 hdr[pos] = ofs & 127;
951 while (ofs >>= 7)
952 hdr[--pos] = 128 | (--ofs & 127);
953 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
954 pack_size += sizeof(hdr) - pos;
955 } else {
956 if (last)
957 last->depth = 0;
958 hdrlen = encode_header(type, datlen, hdr);
959 write_or_die(pack_data->pack_fd, hdr, hdrlen);
960 pack_size += hdrlen;
963 write_or_die(pack_data->pack_fd, out, s.total_out);
964 pack_size += s.total_out;
966 free(out);
967 free(delta);
968 if (last) {
969 if (!last->no_free)
970 free(last->data);
971 last->data = dat;
972 last->offset = e->offset;
973 last->len = datlen;
975 return 0;
978 static void *gfi_unpack_entry(
979 struct object_entry *oe,
980 unsigned long *sizep)
982 enum object_type type;
983 struct packed_git *p = all_packs[oe->pack_id];
984 if (p == pack_data)
985 p->pack_size = pack_size + 20;
986 return unpack_entry(p, oe->offset, &type, sizep);
989 static const char *get_mode(const char *str, uint16_t *modep)
991 unsigned char c;
992 uint16_t mode = 0;
994 while ((c = *str++) != ' ') {
995 if (c < '0' || c > '7')
996 return NULL;
997 mode = (mode << 3) + (c - '0');
999 *modep = mode;
1000 return str;
1003 static void load_tree(struct tree_entry *root)
1005 unsigned char* sha1 = root->versions[1].sha1;
1006 struct object_entry *myoe;
1007 struct tree_content *t;
1008 unsigned long size;
1009 char *buf;
1010 const char *c;
1012 root->tree = t = new_tree_content(8);
1013 if (is_null_sha1(sha1))
1014 return;
1016 myoe = find_object(sha1);
1017 if (myoe && myoe->pack_id != MAX_PACK_ID) {
1018 if (myoe->type != OBJ_TREE)
1019 die("Not a tree: %s", sha1_to_hex(sha1));
1020 t->delta_depth = 0;
1021 buf = gfi_unpack_entry(myoe, &size);
1022 } else {
1023 enum object_type type;
1024 buf = read_sha1_file(sha1, &type, &size);
1025 if (!buf || type != OBJ_TREE)
1026 die("Can't load tree %s", sha1_to_hex(sha1));
1029 c = buf;
1030 while (c != (buf + size)) {
1031 struct tree_entry *e = new_tree_entry();
1033 if (t->entry_count == t->entry_capacity)
1034 root->tree = t = grow_tree_content(t, t->entry_count);
1035 t->entries[t->entry_count++] = e;
1037 e->tree = NULL;
1038 c = get_mode(c, &e->versions[1].mode);
1039 if (!c)
1040 die("Corrupt mode in %s", sha1_to_hex(sha1));
1041 e->versions[0].mode = e->versions[1].mode;
1042 e->name = to_atom(c, strlen(c));
1043 c += e->name->str_len + 1;
1044 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1045 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1046 c += 20;
1048 free(buf);
1051 static int tecmp0 (const void *_a, const void *_b)
1053 struct tree_entry *a = *((struct tree_entry**)_a);
1054 struct tree_entry *b = *((struct tree_entry**)_b);
1055 return base_name_compare(
1056 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1057 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1060 static int tecmp1 (const void *_a, const void *_b)
1062 struct tree_entry *a = *((struct tree_entry**)_a);
1063 struct tree_entry *b = *((struct tree_entry**)_b);
1064 return base_name_compare(
1065 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1066 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1069 static void mktree(struct tree_content *t,
1070 int v,
1071 unsigned long *szp,
1072 struct dbuf *b)
1074 size_t maxlen = 0;
1075 unsigned int i;
1076 char *c;
1078 if (!v)
1079 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1080 else
1081 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1083 for (i = 0; i < t->entry_count; i++) {
1084 if (t->entries[i]->versions[v].mode)
1085 maxlen += t->entries[i]->name->str_len + 34;
1088 size_dbuf(b, maxlen);
1089 c = b->buffer;
1090 for (i = 0; i < t->entry_count; i++) {
1091 struct tree_entry *e = t->entries[i];
1092 if (!e->versions[v].mode)
1093 continue;
1094 c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1095 *c++ = ' ';
1096 strcpy(c, e->name->str_dat);
1097 c += e->name->str_len + 1;
1098 hashcpy((unsigned char*)c, e->versions[v].sha1);
1099 c += 20;
1101 *szp = c - (char*)b->buffer;
1104 static void store_tree(struct tree_entry *root)
1106 struct tree_content *t = root->tree;
1107 unsigned int i, j, del;
1108 unsigned long new_len;
1109 struct last_object lo;
1110 struct object_entry *le;
1112 if (!is_null_sha1(root->versions[1].sha1))
1113 return;
1115 for (i = 0; i < t->entry_count; i++) {
1116 if (t->entries[i]->tree)
1117 store_tree(t->entries[i]);
1120 le = find_object(root->versions[0].sha1);
1121 if (!S_ISDIR(root->versions[0].mode)
1122 || !le
1123 || le->pack_id != pack_id) {
1124 lo.data = NULL;
1125 lo.depth = 0;
1126 lo.no_free = 0;
1127 } else {
1128 mktree(t, 0, &lo.len, &old_tree);
1129 lo.data = old_tree.buffer;
1130 lo.offset = le->offset;
1131 lo.depth = t->delta_depth;
1132 lo.no_free = 1;
1135 mktree(t, 1, &new_len, &new_tree);
1136 store_object(OBJ_TREE, new_tree.buffer, new_len,
1137 &lo, root->versions[1].sha1, 0);
1139 t->delta_depth = lo.depth;
1140 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1141 struct tree_entry *e = t->entries[i];
1142 if (e->versions[1].mode) {
1143 e->versions[0].mode = e->versions[1].mode;
1144 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1145 t->entries[j++] = e;
1146 } else {
1147 release_tree_entry(e);
1148 del++;
1151 t->entry_count -= del;
1154 static int tree_content_set(
1155 struct tree_entry *root,
1156 const char *p,
1157 const unsigned char *sha1,
1158 const uint16_t mode,
1159 struct tree_content *subtree)
1161 struct tree_content *t = root->tree;
1162 const char *slash1;
1163 unsigned int i, n;
1164 struct tree_entry *e;
1166 slash1 = strchr(p, '/');
1167 if (slash1)
1168 n = slash1 - p;
1169 else
1170 n = strlen(p);
1171 if (!n)
1172 die("Empty path component found in input");
1173 if (!slash1 && !S_ISDIR(mode) && subtree)
1174 die("Non-directories cannot have subtrees");
1176 for (i = 0; i < t->entry_count; i++) {
1177 e = t->entries[i];
1178 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1179 if (!slash1) {
1180 if (!S_ISDIR(mode)
1181 && e->versions[1].mode == mode
1182 && !hashcmp(e->versions[1].sha1, sha1))
1183 return 0;
1184 e->versions[1].mode = mode;
1185 hashcpy(e->versions[1].sha1, sha1);
1186 if (e->tree)
1187 release_tree_content_recursive(e->tree);
1188 e->tree = subtree;
1189 hashclr(root->versions[1].sha1);
1190 return 1;
1192 if (!S_ISDIR(e->versions[1].mode)) {
1193 e->tree = new_tree_content(8);
1194 e->versions[1].mode = S_IFDIR;
1196 if (!e->tree)
1197 load_tree(e);
1198 if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
1199 hashclr(root->versions[1].sha1);
1200 return 1;
1202 return 0;
1206 if (t->entry_count == t->entry_capacity)
1207 root->tree = t = grow_tree_content(t, t->entry_count);
1208 e = new_tree_entry();
1209 e->name = to_atom(p, n);
1210 e->versions[0].mode = 0;
1211 hashclr(e->versions[0].sha1);
1212 t->entries[t->entry_count++] = e;
1213 if (slash1) {
1214 e->tree = new_tree_content(8);
1215 e->versions[1].mode = S_IFDIR;
1216 tree_content_set(e, slash1 + 1, sha1, mode, subtree);
1217 } else {
1218 e->tree = subtree;
1219 e->versions[1].mode = mode;
1220 hashcpy(e->versions[1].sha1, sha1);
1222 hashclr(root->versions[1].sha1);
1223 return 1;
1226 static int tree_content_remove(
1227 struct tree_entry *root,
1228 const char *p,
1229 struct tree_entry *backup_leaf)
1231 struct tree_content *t = root->tree;
1232 const char *slash1;
1233 unsigned int i, n;
1234 struct tree_entry *e;
1236 slash1 = strchr(p, '/');
1237 if (slash1)
1238 n = slash1 - p;
1239 else
1240 n = strlen(p);
1242 for (i = 0; i < t->entry_count; i++) {
1243 e = t->entries[i];
1244 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1245 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1246 goto del_entry;
1247 if (!e->tree)
1248 load_tree(e);
1249 if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
1250 for (n = 0; n < e->tree->entry_count; n++) {
1251 if (e->tree->entries[n]->versions[1].mode) {
1252 hashclr(root->versions[1].sha1);
1253 return 1;
1256 backup_leaf = NULL;
1257 goto del_entry;
1259 return 0;
1262 return 0;
1264 del_entry:
1265 if (backup_leaf)
1266 memcpy(backup_leaf, e, sizeof(*backup_leaf));
1267 else if (e->tree)
1268 release_tree_content_recursive(e->tree);
1269 e->tree = NULL;
1270 e->versions[1].mode = 0;
1271 hashclr(e->versions[1].sha1);
1272 hashclr(root->versions[1].sha1);
1273 return 1;
1276 static int update_branch(struct branch *b)
1278 static const char *msg = "fast-import";
1279 struct ref_lock *lock;
1280 unsigned char old_sha1[20];
1282 if (read_ref(b->name, old_sha1))
1283 hashclr(old_sha1);
1284 lock = lock_any_ref_for_update(b->name, old_sha1, 0);
1285 if (!lock)
1286 return error("Unable to lock %s", b->name);
1287 if (!force_update && !is_null_sha1(old_sha1)) {
1288 struct commit *old_cmit, *new_cmit;
1290 old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1291 new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1292 if (!old_cmit || !new_cmit) {
1293 unlock_ref(lock);
1294 return error("Branch %s is missing commits.", b->name);
1297 if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
1298 unlock_ref(lock);
1299 warning("Not updating %s"
1300 " (new tip %s does not contain %s)",
1301 b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1302 return -1;
1305 if (write_ref_sha1(lock, b->sha1, msg) < 0)
1306 return error("Unable to update %s", b->name);
1307 return 0;
1310 static void dump_branches(void)
1312 unsigned int i;
1313 struct branch *b;
1315 for (i = 0; i < branch_table_sz; i++) {
1316 for (b = branch_table[i]; b; b = b->table_next_branch)
1317 failure |= update_branch(b);
1321 static void dump_tags(void)
1323 static const char *msg = "fast-import";
1324 struct tag *t;
1325 struct ref_lock *lock;
1326 char ref_name[PATH_MAX];
1328 for (t = first_tag; t; t = t->next_tag) {
1329 sprintf(ref_name, "tags/%s", t->name);
1330 lock = lock_ref_sha1(ref_name, NULL);
1331 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1332 failure |= error("Unable to update %s", ref_name);
1336 static void dump_marks_helper(FILE *f,
1337 uintmax_t base,
1338 struct mark_set *m)
1340 uintmax_t k;
1341 if (m->shift) {
1342 for (k = 0; k < 1024; k++) {
1343 if (m->data.sets[k])
1344 dump_marks_helper(f, (base + k) << m->shift,
1345 m->data.sets[k]);
1347 } else {
1348 for (k = 0; k < 1024; k++) {
1349 if (m->data.marked[k])
1350 fprintf(f, ":%" PRIuMAX " %s\n", base + k,
1351 sha1_to_hex(m->data.marked[k]->sha1));
1356 static void dump_marks(void)
1358 static struct lock_file mark_lock;
1359 int mark_fd;
1360 FILE *f;
1362 if (!mark_file)
1363 return;
1365 mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
1366 if (mark_fd < 0) {
1367 failure |= error("Unable to write marks file %s: %s",
1368 mark_file, strerror(errno));
1369 return;
1372 f = fdopen(mark_fd, "w");
1373 if (!f) {
1374 rollback_lock_file(&mark_lock);
1375 failure |= error("Unable to write marks file %s: %s",
1376 mark_file, strerror(errno));
1377 return;
1380 dump_marks_helper(f, 0, marks);
1381 fclose(f);
1382 if (commit_lock_file(&mark_lock))
1383 failure |= error("Unable to write marks file %s: %s",
1384 mark_file, strerror(errno));
1387 static void read_next_command(void)
1389 read_line(&command_buf, stdin, '\n');
1392 static void cmd_mark(void)
1394 if (!prefixcmp(command_buf.buf, "mark :")) {
1395 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1396 read_next_command();
1398 else
1399 next_mark = 0;
1402 static void *cmd_data (size_t *size)
1404 size_t length;
1405 char *buffer;
1407 if (prefixcmp(command_buf.buf, "data "))
1408 die("Expected 'data n' command, found: %s", command_buf.buf);
1410 if (!prefixcmp(command_buf.buf + 5, "<<")) {
1411 char *term = xstrdup(command_buf.buf + 5 + 2);
1412 size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1413 length = 0;
1414 buffer = xmalloc(sz);
1415 for (;;) {
1416 read_next_command();
1417 if (command_buf.eof)
1418 die("EOF in data (terminator '%s' not found)", term);
1419 if (term_len == command_buf.len
1420 && !strcmp(term, command_buf.buf))
1421 break;
1422 if (sz < (length + command_buf.len)) {
1423 sz = sz * 3 / 2 + 16;
1424 if (sz < (length + command_buf.len))
1425 sz = length + command_buf.len;
1426 buffer = xrealloc(buffer, sz);
1428 memcpy(buffer + length,
1429 command_buf.buf,
1430 command_buf.len - 1);
1431 length += command_buf.len - 1;
1432 buffer[length++] = '\n';
1434 free(term);
1436 else {
1437 size_t n = 0;
1438 length = strtoul(command_buf.buf + 5, NULL, 10);
1439 buffer = xmalloc(length);
1440 while (n < length) {
1441 size_t s = fread(buffer + n, 1, length - n, stdin);
1442 if (!s && feof(stdin))
1443 die("EOF in data (%lu bytes remaining)",
1444 (unsigned long)(length - n));
1445 n += s;
1449 if (fgetc(stdin) != '\n')
1450 die("An lf did not trail the binary data as expected.");
1452 *size = length;
1453 return buffer;
1456 static int validate_raw_date(const char *src, char *result, int maxlen)
1458 const char *orig_src = src;
1459 char *endp, sign;
1461 strtoul(src, &endp, 10);
1462 if (endp == src || *endp != ' ')
1463 return -1;
1465 src = endp + 1;
1466 if (*src != '-' && *src != '+')
1467 return -1;
1468 sign = *src;
1470 strtoul(src + 1, &endp, 10);
1471 if (endp == src || *endp || (endp - orig_src) >= maxlen)
1472 return -1;
1474 strcpy(result, orig_src);
1475 return 0;
1478 static char *parse_ident(const char *buf)
1480 const char *gt;
1481 size_t name_len;
1482 char *ident;
1484 gt = strrchr(buf, '>');
1485 if (!gt)
1486 die("Missing > in ident string: %s", buf);
1487 gt++;
1488 if (*gt != ' ')
1489 die("Missing space after > in ident string: %s", buf);
1490 gt++;
1491 name_len = gt - buf;
1492 ident = xmalloc(name_len + 24);
1493 strncpy(ident, buf, name_len);
1495 switch (whenspec) {
1496 case WHENSPEC_RAW:
1497 if (validate_raw_date(gt, ident + name_len, 24) < 0)
1498 die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1499 break;
1500 case WHENSPEC_RFC2822:
1501 if (parse_date(gt, ident + name_len, 24) < 0)
1502 die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1503 break;
1504 case WHENSPEC_NOW:
1505 if (strcmp("now", gt))
1506 die("Date in ident must be 'now': %s", buf);
1507 datestamp(ident + name_len, 24);
1508 break;
1511 return ident;
1514 static void cmd_new_blob(void)
1516 size_t l;
1517 void *d;
1519 read_next_command();
1520 cmd_mark();
1521 d = cmd_data(&l);
1523 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1524 free(d);
1527 static void unload_one_branch(void)
1529 while (cur_active_branches
1530 && cur_active_branches >= max_active_branches) {
1531 uintmax_t min_commit = ULONG_MAX;
1532 struct branch *e, *l = NULL, *p = NULL;
1534 for (e = active_branches; e; e = e->active_next_branch) {
1535 if (e->last_commit < min_commit) {
1536 p = l;
1537 min_commit = e->last_commit;
1539 l = e;
1542 if (p) {
1543 e = p->active_next_branch;
1544 p->active_next_branch = e->active_next_branch;
1545 } else {
1546 e = active_branches;
1547 active_branches = e->active_next_branch;
1549 e->active = 0;
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 if (!b->active) {
1563 b->active = 1;
1564 b->active_next_branch = active_branches;
1565 active_branches = b;
1566 cur_active_branches++;
1567 branch_load_count++;
1571 static void file_change_m(struct branch *b)
1573 const char *p = command_buf.buf + 2;
1574 char *p_uq;
1575 const char *endp;
1576 struct object_entry *oe = oe;
1577 unsigned char sha1[20];
1578 uint16_t mode, inline_data = 0;
1580 p = get_mode(p, &mode);
1581 if (!p)
1582 die("Corrupt mode: %s", command_buf.buf);
1583 switch (mode) {
1584 case S_IFREG | 0644:
1585 case S_IFREG | 0755:
1586 case S_IFLNK:
1587 case 0644:
1588 case 0755:
1589 /* ok */
1590 break;
1591 default:
1592 die("Corrupt mode: %s", command_buf.buf);
1595 if (*p == ':') {
1596 char *x;
1597 oe = find_mark(strtoumax(p + 1, &x, 10));
1598 hashcpy(sha1, oe->sha1);
1599 p = x;
1600 } else if (!prefixcmp(p, "inline")) {
1601 inline_data = 1;
1602 p += 6;
1603 } else {
1604 if (get_sha1_hex(p, sha1))
1605 die("Invalid SHA1: %s", command_buf.buf);
1606 oe = find_object(sha1);
1607 p += 40;
1609 if (*p++ != ' ')
1610 die("Missing space after SHA1: %s", command_buf.buf);
1612 p_uq = unquote_c_style(p, &endp);
1613 if (p_uq) {
1614 if (*endp)
1615 die("Garbage after path in: %s", command_buf.buf);
1616 p = p_uq;
1619 if (inline_data) {
1620 size_t l;
1621 void *d;
1622 if (!p_uq)
1623 p = p_uq = xstrdup(p);
1624 read_next_command();
1625 d = cmd_data(&l);
1626 if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1627 free(d);
1628 } else if (oe) {
1629 if (oe->type != OBJ_BLOB)
1630 die("Not a blob (actually a %s): %s",
1631 command_buf.buf, typename(oe->type));
1632 } else {
1633 enum object_type type = sha1_object_info(sha1, NULL);
1634 if (type < 0)
1635 die("Blob not found: %s", command_buf.buf);
1636 if (type != OBJ_BLOB)
1637 die("Not a blob (actually a %s): %s",
1638 typename(type), command_buf.buf);
1641 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
1642 free(p_uq);
1645 static void file_change_d(struct branch *b)
1647 const char *p = command_buf.buf + 2;
1648 char *p_uq;
1649 const char *endp;
1651 p_uq = unquote_c_style(p, &endp);
1652 if (p_uq) {
1653 if (*endp)
1654 die("Garbage after path in: %s", command_buf.buf);
1655 p = p_uq;
1657 tree_content_remove(&b->branch_tree, p, NULL);
1658 free(p_uq);
1661 static void file_change_r(struct branch *b)
1663 const char *s, *d;
1664 char *s_uq, *d_uq;
1665 const char *endp;
1666 struct tree_entry leaf;
1668 s = command_buf.buf + 2;
1669 s_uq = unquote_c_style(s, &endp);
1670 if (s_uq) {
1671 if (*endp != ' ')
1672 die("Missing space after source: %s", command_buf.buf);
1674 else {
1675 endp = strchr(s, ' ');
1676 if (!endp)
1677 die("Missing space after source: %s", command_buf.buf);
1678 s_uq = xmalloc(endp - s + 1);
1679 memcpy(s_uq, s, endp - s);
1680 s_uq[endp - s] = 0;
1682 s = s_uq;
1684 endp++;
1685 if (!*endp)
1686 die("Missing dest: %s", command_buf.buf);
1688 d = endp;
1689 d_uq = unquote_c_style(d, &endp);
1690 if (d_uq) {
1691 if (*endp)
1692 die("Garbage after dest in: %s", command_buf.buf);
1693 d = d_uq;
1696 memset(&leaf, 0, sizeof(leaf));
1697 tree_content_remove(&b->branch_tree, s, &leaf);
1698 if (!leaf.versions[1].mode)
1699 die("Path %s not in branch", s);
1700 tree_content_set(&b->branch_tree, d,
1701 leaf.versions[1].sha1,
1702 leaf.versions[1].mode,
1703 leaf.tree);
1705 free(s_uq);
1706 free(d_uq);
1709 static void file_change_deleteall(struct branch *b)
1711 release_tree_content_recursive(b->branch_tree.tree);
1712 hashclr(b->branch_tree.versions[0].sha1);
1713 hashclr(b->branch_tree.versions[1].sha1);
1714 load_tree(&b->branch_tree);
1717 static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
1719 if (!buf || size < 46)
1720 die("Not a valid commit: %s", sha1_to_hex(b->sha1));
1721 if (memcmp("tree ", buf, 5)
1722 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1723 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1724 hashcpy(b->branch_tree.versions[0].sha1,
1725 b->branch_tree.versions[1].sha1);
1728 static void cmd_from_existing(struct branch *b)
1730 if (is_null_sha1(b->sha1)) {
1731 hashclr(b->branch_tree.versions[0].sha1);
1732 hashclr(b->branch_tree.versions[1].sha1);
1733 } else {
1734 unsigned long size;
1735 char *buf;
1737 buf = read_object_with_reference(b->sha1,
1738 commit_type, &size, b->sha1);
1739 cmd_from_commit(b, buf, size);
1740 free(buf);
1744 static void cmd_from(struct branch *b)
1746 const char *from;
1747 struct branch *s;
1749 if (prefixcmp(command_buf.buf, "from "))
1750 return;
1752 if (b->branch_tree.tree) {
1753 release_tree_content_recursive(b->branch_tree.tree);
1754 b->branch_tree.tree = NULL;
1757 from = strchr(command_buf.buf, ' ') + 1;
1758 s = lookup_branch(from);
1759 if (b == s)
1760 die("Can't create a branch from itself: %s", b->name);
1761 else if (s) {
1762 unsigned char *t = s->branch_tree.versions[1].sha1;
1763 hashcpy(b->sha1, s->sha1);
1764 hashcpy(b->branch_tree.versions[0].sha1, t);
1765 hashcpy(b->branch_tree.versions[1].sha1, t);
1766 } else if (*from == ':') {
1767 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1768 struct object_entry *oe = find_mark(idnum);
1769 if (oe->type != OBJ_COMMIT)
1770 die("Mark :%" PRIuMAX " not a commit", idnum);
1771 hashcpy(b->sha1, oe->sha1);
1772 if (oe->pack_id != MAX_PACK_ID) {
1773 unsigned long size;
1774 char *buf = gfi_unpack_entry(oe, &size);
1775 cmd_from_commit(b, buf, size);
1776 free(buf);
1777 } else
1778 cmd_from_existing(b);
1779 } else if (!get_sha1(from, b->sha1))
1780 cmd_from_existing(b);
1781 else
1782 die("Invalid ref name or SHA1 expression: %s", from);
1784 read_next_command();
1787 static struct hash_list *cmd_merge(unsigned int *count)
1789 struct hash_list *list = NULL, *n, *e = e;
1790 const char *from;
1791 struct branch *s;
1793 *count = 0;
1794 while (!prefixcmp(command_buf.buf, "merge ")) {
1795 from = strchr(command_buf.buf, ' ') + 1;
1796 n = xmalloc(sizeof(*n));
1797 s = lookup_branch(from);
1798 if (s)
1799 hashcpy(n->sha1, s->sha1);
1800 else if (*from == ':') {
1801 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1802 struct object_entry *oe = find_mark(idnum);
1803 if (oe->type != OBJ_COMMIT)
1804 die("Mark :%" PRIuMAX " not a commit", idnum);
1805 hashcpy(n->sha1, oe->sha1);
1806 } else if (!get_sha1(from, n->sha1)) {
1807 unsigned long size;
1808 char *buf = read_object_with_reference(n->sha1,
1809 commit_type, &size, n->sha1);
1810 if (!buf || size < 46)
1811 die("Not a valid commit: %s", from);
1812 free(buf);
1813 } else
1814 die("Invalid ref name or SHA1 expression: %s", from);
1816 n->next = NULL;
1817 if (list)
1818 e->next = n;
1819 else
1820 list = n;
1821 e = n;
1822 (*count)++;
1823 read_next_command();
1825 return list;
1828 static void cmd_new_commit(void)
1830 struct branch *b;
1831 void *msg;
1832 size_t msglen;
1833 char *sp;
1834 char *author = NULL;
1835 char *committer = NULL;
1836 struct hash_list *merge_list = NULL;
1837 unsigned int merge_count;
1839 /* Obtain the branch name from the rest of our command */
1840 sp = strchr(command_buf.buf, ' ') + 1;
1841 b = lookup_branch(sp);
1842 if (!b)
1843 b = new_branch(sp);
1845 read_next_command();
1846 cmd_mark();
1847 if (!prefixcmp(command_buf.buf, "author ")) {
1848 author = parse_ident(command_buf.buf + 7);
1849 read_next_command();
1851 if (!prefixcmp(command_buf.buf, "committer ")) {
1852 committer = parse_ident(command_buf.buf + 10);
1853 read_next_command();
1855 if (!committer)
1856 die("Expected committer but didn't get one");
1857 msg = cmd_data(&msglen);
1858 read_next_command();
1859 cmd_from(b);
1860 merge_list = cmd_merge(&merge_count);
1862 /* ensure the branch is active/loaded */
1863 if (!b->branch_tree.tree || !max_active_branches) {
1864 unload_one_branch();
1865 load_branch(b);
1868 /* file_change* */
1869 for (;;) {
1870 if (1 == command_buf.len)
1871 break;
1872 else if (!prefixcmp(command_buf.buf, "M "))
1873 file_change_m(b);
1874 else if (!prefixcmp(command_buf.buf, "D "))
1875 file_change_d(b);
1876 else if (!prefixcmp(command_buf.buf, "R "))
1877 file_change_r(b);
1878 else if (!strcmp("deleteall", command_buf.buf))
1879 file_change_deleteall(b);
1880 else
1881 die("Unsupported file_change: %s", command_buf.buf);
1882 read_next_command();
1885 /* build the tree and the commit */
1886 store_tree(&b->branch_tree);
1887 hashcpy(b->branch_tree.versions[0].sha1,
1888 b->branch_tree.versions[1].sha1);
1889 size_dbuf(&new_data, 114 + msglen
1890 + merge_count * 49
1891 + (author
1892 ? strlen(author) + strlen(committer)
1893 : 2 * strlen(committer)));
1894 sp = new_data.buffer;
1895 sp += sprintf(sp, "tree %s\n",
1896 sha1_to_hex(b->branch_tree.versions[1].sha1));
1897 if (!is_null_sha1(b->sha1))
1898 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1899 while (merge_list) {
1900 struct hash_list *next = merge_list->next;
1901 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1902 free(merge_list);
1903 merge_list = next;
1905 sp += sprintf(sp, "author %s\n", author ? author : committer);
1906 sp += sprintf(sp, "committer %s\n", committer);
1907 *sp++ = '\n';
1908 memcpy(sp, msg, msglen);
1909 sp += msglen;
1910 free(author);
1911 free(committer);
1912 free(msg);
1914 if (!store_object(OBJ_COMMIT,
1915 new_data.buffer, sp - (char*)new_data.buffer,
1916 NULL, b->sha1, next_mark))
1917 b->pack_id = pack_id;
1918 b->last_commit = object_count_by_type[OBJ_COMMIT];
1921 static void cmd_new_tag(void)
1923 char *sp;
1924 const char *from;
1925 char *tagger;
1926 struct branch *s;
1927 void *msg;
1928 size_t msglen;
1929 struct tag *t;
1930 uintmax_t from_mark = 0;
1931 unsigned char sha1[20];
1933 /* Obtain the new tag name from the rest of our command */
1934 sp = strchr(command_buf.buf, ' ') + 1;
1935 t = pool_alloc(sizeof(struct tag));
1936 t->next_tag = NULL;
1937 t->name = pool_strdup(sp);
1938 if (last_tag)
1939 last_tag->next_tag = t;
1940 else
1941 first_tag = t;
1942 last_tag = t;
1943 read_next_command();
1945 /* from ... */
1946 if (prefixcmp(command_buf.buf, "from "))
1947 die("Expected from command, got %s", command_buf.buf);
1948 from = strchr(command_buf.buf, ' ') + 1;
1949 s = lookup_branch(from);
1950 if (s) {
1951 hashcpy(sha1, s->sha1);
1952 } else if (*from == ':') {
1953 struct object_entry *oe;
1954 from_mark = strtoumax(from + 1, NULL, 10);
1955 oe = find_mark(from_mark);
1956 if (oe->type != OBJ_COMMIT)
1957 die("Mark :%" PRIuMAX " not a commit", from_mark);
1958 hashcpy(sha1, oe->sha1);
1959 } else if (!get_sha1(from, sha1)) {
1960 unsigned long size;
1961 char *buf;
1963 buf = read_object_with_reference(sha1,
1964 commit_type, &size, sha1);
1965 if (!buf || size < 46)
1966 die("Not a valid commit: %s", from);
1967 free(buf);
1968 } else
1969 die("Invalid ref name or SHA1 expression: %s", from);
1970 read_next_command();
1972 /* tagger ... */
1973 if (prefixcmp(command_buf.buf, "tagger "))
1974 die("Expected tagger command, got %s", command_buf.buf);
1975 tagger = parse_ident(command_buf.buf + 7);
1977 /* tag payload/message */
1978 read_next_command();
1979 msg = cmd_data(&msglen);
1981 /* build the tag object */
1982 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1983 sp = new_data.buffer;
1984 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1985 sp += sprintf(sp, "type %s\n", commit_type);
1986 sp += sprintf(sp, "tag %s\n", t->name);
1987 sp += sprintf(sp, "tagger %s\n", tagger);
1988 *sp++ = '\n';
1989 memcpy(sp, msg, msglen);
1990 sp += msglen;
1991 free(tagger);
1992 free(msg);
1994 if (store_object(OBJ_TAG, new_data.buffer,
1995 sp - (char*)new_data.buffer,
1996 NULL, t->sha1, 0))
1997 t->pack_id = MAX_PACK_ID;
1998 else
1999 t->pack_id = pack_id;
2002 static void cmd_reset_branch(void)
2004 struct branch *b;
2005 char *sp;
2007 /* Obtain the branch name from the rest of our command */
2008 sp = strchr(command_buf.buf, ' ') + 1;
2009 b = lookup_branch(sp);
2010 if (b) {
2011 hashclr(b->sha1);
2012 hashclr(b->branch_tree.versions[0].sha1);
2013 hashclr(b->branch_tree.versions[1].sha1);
2014 if (b->branch_tree.tree) {
2015 release_tree_content_recursive(b->branch_tree.tree);
2016 b->branch_tree.tree = NULL;
2019 else
2020 b = new_branch(sp);
2021 read_next_command();
2022 cmd_from(b);
2025 static void cmd_checkpoint(void)
2027 if (object_count) {
2028 cycle_packfile();
2029 dump_branches();
2030 dump_tags();
2031 dump_marks();
2033 read_next_command();
2036 static void import_marks(const char *input_file)
2038 char line[512];
2039 FILE *f = fopen(input_file, "r");
2040 if (!f)
2041 die("cannot read %s: %s", input_file, strerror(errno));
2042 while (fgets(line, sizeof(line), f)) {
2043 uintmax_t mark;
2044 char *end;
2045 unsigned char sha1[20];
2046 struct object_entry *e;
2048 end = strchr(line, '\n');
2049 if (line[0] != ':' || !end)
2050 die("corrupt mark line: %s", line);
2051 *end = 0;
2052 mark = strtoumax(line + 1, &end, 10);
2053 if (!mark || end == line + 1
2054 || *end != ' ' || get_sha1(end + 1, sha1))
2055 die("corrupt mark line: %s", line);
2056 e = find_object(sha1);
2057 if (!e) {
2058 enum object_type type = sha1_object_info(sha1, NULL);
2059 if (type < 0)
2060 die("object not found: %s", sha1_to_hex(sha1));
2061 e = insert_object(sha1);
2062 e->type = type;
2063 e->pack_id = MAX_PACK_ID;
2064 e->offset = 1; /* just not zero! */
2066 insert_mark(mark, e);
2068 fclose(f);
2071 static const char fast_import_usage[] =
2072 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2074 int main(int argc, const char **argv)
2076 int i, show_stats = 1;
2078 git_config(git_default_config);
2079 alloc_objects(object_entry_alloc);
2080 strbuf_init(&command_buf);
2081 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2082 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2083 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2084 marks = pool_calloc(1, sizeof(struct mark_set));
2086 for (i = 1; i < argc; i++) {
2087 const char *a = argv[i];
2089 if (*a != '-' || !strcmp(a, "--"))
2090 break;
2091 else if (!prefixcmp(a, "--date-format=")) {
2092 const char *fmt = a + 14;
2093 if (!strcmp(fmt, "raw"))
2094 whenspec = WHENSPEC_RAW;
2095 else if (!strcmp(fmt, "rfc2822"))
2096 whenspec = WHENSPEC_RFC2822;
2097 else if (!strcmp(fmt, "now"))
2098 whenspec = WHENSPEC_NOW;
2099 else
2100 die("unknown --date-format argument %s", fmt);
2102 else if (!prefixcmp(a, "--max-pack-size="))
2103 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
2104 else if (!prefixcmp(a, "--depth="))
2105 max_depth = strtoul(a + 8, NULL, 0);
2106 else if (!prefixcmp(a, "--active-branches="))
2107 max_active_branches = strtoul(a + 18, NULL, 0);
2108 else if (!prefixcmp(a, "--import-marks="))
2109 import_marks(a + 15);
2110 else if (!prefixcmp(a, "--export-marks="))
2111 mark_file = a + 15;
2112 else if (!prefixcmp(a, "--export-pack-edges=")) {
2113 if (pack_edges)
2114 fclose(pack_edges);
2115 pack_edges = fopen(a + 20, "a");
2116 if (!pack_edges)
2117 die("Cannot open %s: %s", a + 20, strerror(errno));
2118 } else if (!strcmp(a, "--force"))
2119 force_update = 1;
2120 else if (!strcmp(a, "--quiet"))
2121 show_stats = 0;
2122 else if (!strcmp(a, "--stats"))
2123 show_stats = 1;
2124 else
2125 die("unknown option %s", a);
2127 if (i != argc)
2128 usage(fast_import_usage);
2130 prepare_packed_git();
2131 start_packfile();
2132 for (;;) {
2133 read_next_command();
2134 if (command_buf.eof)
2135 break;
2136 else if (!strcmp("blob", command_buf.buf))
2137 cmd_new_blob();
2138 else if (!prefixcmp(command_buf.buf, "commit "))
2139 cmd_new_commit();
2140 else if (!prefixcmp(command_buf.buf, "tag "))
2141 cmd_new_tag();
2142 else if (!prefixcmp(command_buf.buf, "reset "))
2143 cmd_reset_branch();
2144 else if (!strcmp("checkpoint", command_buf.buf))
2145 cmd_checkpoint();
2146 else
2147 die("Unsupported command: %s", command_buf.buf);
2149 end_packfile();
2151 dump_branches();
2152 dump_tags();
2153 unkeep_all_packs();
2154 dump_marks();
2156 if (pack_edges)
2157 fclose(pack_edges);
2159 if (show_stats) {
2160 uintmax_t total_count = 0, duplicate_count = 0;
2161 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
2162 total_count += object_count_by_type[i];
2163 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
2164 duplicate_count += duplicate_count_by_type[i];
2166 fprintf(stderr, "%s statistics:\n", argv[0]);
2167 fprintf(stderr, "---------------------------------------------------------------------\n");
2168 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
2169 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
2170 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]);
2171 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]);
2172 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]);
2173 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]);
2174 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
2175 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
2176 fprintf(stderr, " atoms: %10u\n", atom_cnt);
2177 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
2178 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
2179 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
2180 fprintf(stderr, "---------------------------------------------------------------------\n");
2181 pack_report();
2182 fprintf(stderr, "---------------------------------------------------------------------\n");
2183 fprintf(stderr, "\n");
2186 return failure ? 1 : 0;