8342314bb0ee80f50315555aafa5eb46eeac5ef8
1 /*
2 Format of STDIN stream:
4 stream ::= cmd*;
6 cmd ::= new_blob
7 | new_commit
8 | new_tag
9 | reset_branch
10 ;
12 new_blob ::= 'blob' lf
13 mark?
14 file_content;
15 file_content ::= data;
17 new_commit ::= 'commit' sp ref_str lf
18 mark?
19 ('author' sp name '<' email '>' ts tz lf)?
20 'committer' sp name '<' email '>' ts tz lf
21 commit_msg
22 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
23 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
24 file_change*
25 lf;
26 commit_msg ::= data;
28 file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
29 | 'D' sp path_str lf
30 ;
31 mode ::= '644' | '755';
33 new_tag ::= 'tag' sp tag_str lf
34 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
35 'tagger' sp name '<' email '>' ts tz lf
36 tag_msg;
37 tag_msg ::= data;
39 reset_branch ::= 'reset' sp ref_str lf
40 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
41 lf;
43 checkpoint ::= 'checkpoint' lf
44 lf;
46 # note: the first idnum in a stream should be 1 and subsequent
47 # idnums should not have gaps between values as this will cause
48 # the stream parser to reserve space for the gapped values. An
49 # idnum can be updated in the future to a new object by issuing
50 # a new mark directive with the old idnum.
51 #
52 mark ::= 'mark' sp idnum lf;
54 # note: declen indicates the length of binary_data in bytes.
55 # declen does not include the lf preceeding or trailing the
56 # binary data.
57 #
58 data ::= 'data' sp declen lf
59 binary_data
60 lf;
62 # note: quoted strings are C-style quoting supporting \c for
63 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
64 # is the signed byte value in octal. Note that the only
65 # characters which must actually be escaped to protect the
66 # stream formatting is: \, " and LF. Otherwise these values
67 # are UTF8.
68 #
69 ref_str ::= ref | '"' quoted(ref) '"' ;
70 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
71 tag_str ::= tag | '"' quoted(tag) '"' ;
72 path_str ::= path | '"' quoted(path) '"' ;
74 declen ::= # unsigned 32 bit value, ascii base10 notation;
75 binary_data ::= # file content, not interpreted;
77 sp ::= # ASCII space character;
78 lf ::= # ASCII newline (LF) character;
80 # note: a colon (':') must precede the numerical value assigned to
81 # an idnum. This is to distinguish it from a ref or tag name as
82 # GIT does not permit ':' in ref or tag strings.
83 #
84 idnum ::= ':' declen;
85 path ::= # GIT style file path, e.g. "a/b/c";
86 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
87 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
88 sha1exp ::= # Any valid GIT SHA1 expression;
89 hexsha1 ::= # SHA1 in hexadecimal format;
91 # note: name and email are UTF8 strings, however name must not
92 # contain '<' or lf and email must not contain any of the
93 # following: '<', '>', lf.
94 #
95 name ::= # valid GIT author/committer name;
96 email ::= # valid GIT author/committer email;
97 ts ::= # time since the epoch in seconds, ascii base10 notation;
98 tz ::= # GIT style timezone;
99 */
101 #include "builtin.h"
102 #include "cache.h"
103 #include "object.h"
104 #include "blob.h"
105 #include "tree.h"
106 #include "delta.h"
107 #include "pack.h"
108 #include "refs.h"
109 #include "csum-file.h"
110 #include "strbuf.h"
111 #include "quote.h"
113 struct object_entry
114 {
115 struct object_entry *next;
116 unsigned long offset;
117 unsigned type : TYPE_BITS;
118 unsigned pack_id : 16;
119 unsigned char sha1[20];
120 };
122 struct object_entry_pool
123 {
124 struct object_entry_pool *next_pool;
125 struct object_entry *next_free;
126 struct object_entry *end;
127 struct object_entry entries[FLEX_ARRAY]; /* more */
128 };
130 struct mark_set
131 {
132 int shift;
133 union {
134 struct object_entry *marked[1024];
135 struct mark_set *sets[1024];
136 } data;
137 };
139 struct last_object
140 {
141 void *data;
142 unsigned long len;
143 unsigned long offset;
144 unsigned int depth;
145 unsigned no_free:1;
146 };
148 struct mem_pool
149 {
150 struct mem_pool *next_pool;
151 char *next_free;
152 char *end;
153 char space[FLEX_ARRAY]; /* more */
154 };
156 struct atom_str
157 {
158 struct atom_str *next_atom;
159 int str_len;
160 char str_dat[FLEX_ARRAY]; /* more */
161 };
163 struct tree_content;
164 struct tree_entry
165 {
166 struct tree_content *tree;
167 struct atom_str* name;
168 struct tree_entry_ms
169 {
170 unsigned int mode;
171 unsigned char sha1[20];
172 } versions[2];
173 };
175 struct tree_content
176 {
177 unsigned int entry_capacity; /* must match avail_tree_content */
178 unsigned int entry_count;
179 unsigned int delta_depth;
180 struct tree_entry *entries[FLEX_ARRAY]; /* more */
181 };
183 struct avail_tree_content
184 {
185 unsigned int entry_capacity; /* must match tree_content */
186 struct avail_tree_content *next_avail;
187 };
189 struct branch
190 {
191 struct branch *table_next_branch;
192 struct branch *active_next_branch;
193 const char *name;
194 unsigned long last_commit;
195 struct tree_entry branch_tree;
196 unsigned char sha1[20];
197 };
199 struct tag
200 {
201 struct tag *next_tag;
202 const char *name;
203 unsigned char sha1[20];
204 };
206 struct dbuf
207 {
208 void *buffer;
209 size_t capacity;
210 };
212 struct hash_list
213 {
214 struct hash_list *next;
215 unsigned char sha1[20];
216 };
218 /* Configured limits on output */
219 static unsigned long max_depth = 10;
220 static unsigned long max_packsize = (1LL << 32) - 1;
221 static uintmax_t max_objects = -1;
223 /* Stats and misc. counters */
224 static uintmax_t alloc_count;
225 static uintmax_t object_count;
226 static uintmax_t marks_set_count;
227 static uintmax_t object_count_by_type[1 << TYPE_BITS];
228 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
229 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
230 static unsigned long branch_count;
231 static unsigned long branch_load_count;
233 /* Memory pools */
234 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
235 static size_t total_allocd;
236 static struct mem_pool *mem_pool;
238 /* Atom management */
239 static unsigned int atom_table_sz = 4451;
240 static unsigned int atom_cnt;
241 static struct atom_str **atom_table;
243 /* The .pack file being generated */
244 static unsigned int pack_id;
245 static struct packed_git *pack_data;
246 static struct packed_git **all_packs;
247 static unsigned long pack_size;
249 /* Table of objects we've written. */
250 static unsigned int object_entry_alloc = 5000;
251 static struct object_entry_pool *blocks;
252 static struct object_entry *object_table[1 << 16];
253 static struct mark_set *marks;
254 static const char* mark_file;
256 /* Our last blob */
257 static struct last_object last_blob;
259 /* Tree management */
260 static unsigned int tree_entry_alloc = 1000;
261 static void *avail_tree_entry;
262 static unsigned int avail_tree_table_sz = 100;
263 static struct avail_tree_content **avail_tree_table;
264 static struct dbuf old_tree;
265 static struct dbuf new_tree;
267 /* Branch data */
268 static unsigned long max_active_branches = 5;
269 static unsigned long cur_active_branches;
270 static unsigned long branch_table_sz = 1039;
271 static struct branch **branch_table;
272 static struct branch *active_branches;
274 /* Tag data */
275 static struct tag *first_tag;
276 static struct tag *last_tag;
278 /* Input stream parsing */
279 static struct strbuf command_buf;
280 static uintmax_t next_mark;
281 static struct dbuf new_data;
282 static FILE* branch_log;
285 static void alloc_objects(unsigned int cnt)
286 {
287 struct object_entry_pool *b;
289 b = xmalloc(sizeof(struct object_entry_pool)
290 + cnt * sizeof(struct object_entry));
291 b->next_pool = blocks;
292 b->next_free = b->entries;
293 b->end = b->entries + cnt;
294 blocks = b;
295 alloc_count += cnt;
296 }
298 static struct object_entry* new_object(unsigned char *sha1)
299 {
300 struct object_entry *e;
302 if (blocks->next_free == blocks->end)
303 alloc_objects(object_entry_alloc);
305 e = blocks->next_free++;
306 hashcpy(e->sha1, sha1);
307 return e;
308 }
310 static struct object_entry* find_object(unsigned char *sha1)
311 {
312 unsigned int h = sha1[0] << 8 | sha1[1];
313 struct object_entry *e;
314 for (e = object_table[h]; e; e = e->next)
315 if (!hashcmp(sha1, e->sha1))
316 return e;
317 return NULL;
318 }
320 static struct object_entry* insert_object(unsigned char *sha1)
321 {
322 unsigned int h = sha1[0] << 8 | sha1[1];
323 struct object_entry *e = object_table[h];
324 struct object_entry *p = NULL;
326 while (e) {
327 if (!hashcmp(sha1, e->sha1))
328 return e;
329 p = e;
330 e = e->next;
331 }
333 e = new_object(sha1);
334 e->next = NULL;
335 e->offset = 0;
336 if (p)
337 p->next = e;
338 else
339 object_table[h] = e;
340 return e;
341 }
343 static unsigned int hc_str(const char *s, size_t len)
344 {
345 unsigned int r = 0;
346 while (len-- > 0)
347 r = r * 31 + *s++;
348 return r;
349 }
351 static void* pool_alloc(size_t len)
352 {
353 struct mem_pool *p;
354 void *r;
356 for (p = mem_pool; p; p = p->next_pool)
357 if ((p->end - p->next_free >= len))
358 break;
360 if (!p) {
361 if (len >= (mem_pool_alloc/2)) {
362 total_allocd += len;
363 return xmalloc(len);
364 }
365 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
366 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
367 p->next_pool = mem_pool;
368 p->next_free = p->space;
369 p->end = p->next_free + mem_pool_alloc;
370 mem_pool = p;
371 }
373 r = p->next_free;
374 /* round out to a pointer alignment */
375 if (len & (sizeof(void*) - 1))
376 len += sizeof(void*) - (len & (sizeof(void*) - 1));
377 p->next_free += len;
378 return r;
379 }
381 static void* pool_calloc(size_t count, size_t size)
382 {
383 size_t len = count * size;
384 void *r = pool_alloc(len);
385 memset(r, 0, len);
386 return r;
387 }
389 static char* pool_strdup(const char *s)
390 {
391 char *r = pool_alloc(strlen(s) + 1);
392 strcpy(r, s);
393 return r;
394 }
396 static void size_dbuf(struct dbuf *b, size_t maxlen)
397 {
398 if (b->buffer) {
399 if (b->capacity >= maxlen)
400 return;
401 free(b->buffer);
402 }
403 b->capacity = ((maxlen / 1024) + 1) * 1024;
404 b->buffer = xmalloc(b->capacity);
405 }
407 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
408 {
409 struct mark_set *s = marks;
410 while ((idnum >> s->shift) >= 1024) {
411 s = pool_calloc(1, sizeof(struct mark_set));
412 s->shift = marks->shift + 10;
413 s->data.sets[0] = marks;
414 marks = s;
415 }
416 while (s->shift) {
417 uintmax_t i = idnum >> s->shift;
418 idnum -= i << s->shift;
419 if (!s->data.sets[i]) {
420 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
421 s->data.sets[i]->shift = s->shift - 10;
422 }
423 s = s->data.sets[i];
424 }
425 if (!s->data.marked[idnum])
426 marks_set_count++;
427 s->data.marked[idnum] = oe;
428 }
430 static struct object_entry* find_mark(uintmax_t idnum)
431 {
432 uintmax_t orig_idnum = idnum;
433 struct mark_set *s = marks;
434 struct object_entry *oe = NULL;
435 if ((idnum >> s->shift) < 1024) {
436 while (s && s->shift) {
437 uintmax_t i = idnum >> s->shift;
438 idnum -= i << s->shift;
439 s = s->data.sets[i];
440 }
441 if (s)
442 oe = s->data.marked[idnum];
443 }
444 if (!oe)
445 die("mark :%ju not declared", orig_idnum);
446 return oe;
447 }
449 static struct atom_str* to_atom(const char *s, size_t len)
450 {
451 unsigned int hc = hc_str(s, len) % atom_table_sz;
452 struct atom_str *c;
454 for (c = atom_table[hc]; c; c = c->next_atom)
455 if (c->str_len == len && !strncmp(s, c->str_dat, len))
456 return c;
458 c = pool_alloc(sizeof(struct atom_str) + len + 1);
459 c->str_len = len;
460 strncpy(c->str_dat, s, len);
461 c->str_dat[len] = 0;
462 c->next_atom = atom_table[hc];
463 atom_table[hc] = c;
464 atom_cnt++;
465 return c;
466 }
468 static struct branch* lookup_branch(const char *name)
469 {
470 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
471 struct branch *b;
473 for (b = branch_table[hc]; b; b = b->table_next_branch)
474 if (!strcmp(name, b->name))
475 return b;
476 return NULL;
477 }
479 static struct branch* new_branch(const char *name)
480 {
481 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
482 struct branch* b = lookup_branch(name);
484 if (b)
485 die("Invalid attempt to create duplicate branch: %s", name);
486 if (check_ref_format(name))
487 die("Branch name doesn't conform to GIT standards: %s", name);
489 b = pool_calloc(1, sizeof(struct branch));
490 b->name = pool_strdup(name);
491 b->table_next_branch = branch_table[hc];
492 b->branch_tree.versions[0].mode = S_IFDIR;
493 b->branch_tree.versions[1].mode = S_IFDIR;
494 branch_table[hc] = b;
495 branch_count++;
496 return b;
497 }
499 static unsigned int hc_entries(unsigned int cnt)
500 {
501 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
502 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
503 }
505 static struct tree_content* new_tree_content(unsigned int cnt)
506 {
507 struct avail_tree_content *f, *l = NULL;
508 struct tree_content *t;
509 unsigned int hc = hc_entries(cnt);
511 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
512 if (f->entry_capacity >= cnt)
513 break;
515 if (f) {
516 if (l)
517 l->next_avail = f->next_avail;
518 else
519 avail_tree_table[hc] = f->next_avail;
520 } else {
521 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
522 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
523 f->entry_capacity = cnt;
524 }
526 t = (struct tree_content*)f;
527 t->entry_count = 0;
528 t->delta_depth = 0;
529 return t;
530 }
532 static void release_tree_entry(struct tree_entry *e);
533 static void release_tree_content(struct tree_content *t)
534 {
535 struct avail_tree_content *f = (struct avail_tree_content*)t;
536 unsigned int hc = hc_entries(f->entry_capacity);
537 f->next_avail = avail_tree_table[hc];
538 avail_tree_table[hc] = f;
539 }
541 static void release_tree_content_recursive(struct tree_content *t)
542 {
543 unsigned int i;
544 for (i = 0; i < t->entry_count; i++)
545 release_tree_entry(t->entries[i]);
546 release_tree_content(t);
547 }
549 static struct tree_content* grow_tree_content(
550 struct tree_content *t,
551 int amt)
552 {
553 struct tree_content *r = new_tree_content(t->entry_count + amt);
554 r->entry_count = t->entry_count;
555 r->delta_depth = t->delta_depth;
556 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
557 release_tree_content(t);
558 return r;
559 }
561 static struct tree_entry* new_tree_entry()
562 {
563 struct tree_entry *e;
565 if (!avail_tree_entry) {
566 unsigned int n = tree_entry_alloc;
567 total_allocd += n * sizeof(struct tree_entry);
568 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
569 while (n-- > 1) {
570 *((void**)e) = e + 1;
571 e++;
572 }
573 *((void**)e) = NULL;
574 }
576 e = avail_tree_entry;
577 avail_tree_entry = *((void**)e);
578 return e;
579 }
581 static void release_tree_entry(struct tree_entry *e)
582 {
583 if (e->tree)
584 release_tree_content_recursive(e->tree);
585 *((void**)e) = avail_tree_entry;
586 avail_tree_entry = e;
587 }
589 static void start_packfile()
590 {
591 static char tmpfile[PATH_MAX];
592 struct packed_git *p;
593 struct pack_header hdr;
594 int pack_fd;
596 snprintf(tmpfile, sizeof(tmpfile),
597 "%s/pack_XXXXXX", get_object_directory());
598 pack_fd = mkstemp(tmpfile);
599 if (pack_fd < 0)
600 die("Can't create %s: %s", tmpfile, strerror(errno));
601 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
602 strcpy(p->pack_name, tmpfile);
603 p->pack_fd = pack_fd;
605 hdr.hdr_signature = htonl(PACK_SIGNATURE);
606 hdr.hdr_version = htonl(2);
607 hdr.hdr_entries = 0;
608 write_or_die(p->pack_fd, &hdr, sizeof(hdr));
610 pack_data = p;
611 pack_size = sizeof(hdr);
612 object_count = 0;
614 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
615 all_packs[pack_id] = p;
616 }
618 static void fixup_header_footer()
619 {
620 int pack_fd = pack_data->pack_fd;
621 SHA_CTX c;
622 char hdr[8];
623 unsigned long cnt;
624 char *buf;
626 if (lseek(pack_fd, 0, SEEK_SET) != 0)
627 die("Failed seeking to start: %s", strerror(errno));
629 SHA1_Init(&c);
630 if (read_in_full(pack_fd, hdr, 8) != 8)
631 die("Unable to reread header of %s", pack_data->pack_name);
632 SHA1_Update(&c, hdr, 8);
634 cnt = htonl(object_count);
635 SHA1_Update(&c, &cnt, 4);
636 write_or_die(pack_fd, &cnt, 4);
638 buf = xmalloc(128 * 1024);
639 for (;;) {
640 size_t n = xread(pack_fd, buf, 128 * 1024);
641 if (n <= 0)
642 break;
643 SHA1_Update(&c, buf, n);
644 }
645 free(buf);
647 SHA1_Final(pack_data->sha1, &c);
648 write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
649 close(pack_fd);
650 }
652 static int oecmp (const void *a_, const void *b_)
653 {
654 struct object_entry *a = *((struct object_entry**)a_);
655 struct object_entry *b = *((struct object_entry**)b_);
656 return hashcmp(a->sha1, b->sha1);
657 }
659 static char* create_index()
660 {
661 static char tmpfile[PATH_MAX];
662 SHA_CTX ctx;
663 struct sha1file *f;
664 struct object_entry **idx, **c, **last, *e;
665 struct object_entry_pool *o;
666 unsigned int array[256];
667 int i, idx_fd;
669 /* Build the sorted table of object IDs. */
670 idx = xmalloc(object_count * sizeof(struct object_entry*));
671 c = idx;
672 for (o = blocks; o; o = o->next_pool)
673 for (e = o->next_free; e-- != o->entries;)
674 if (pack_id == e->pack_id)
675 *c++ = e;
676 last = idx + object_count;
677 if (c != last)
678 die("internal consistency error creating the index");
679 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
681 /* Generate the fan-out array. */
682 c = idx;
683 for (i = 0; i < 256; i++) {
684 struct object_entry **next = c;;
685 while (next < last) {
686 if ((*next)->sha1[0] != i)
687 break;
688 next++;
689 }
690 array[i] = htonl(next - idx);
691 c = next;
692 }
694 snprintf(tmpfile, sizeof(tmpfile),
695 "%s/index_XXXXXX", get_object_directory());
696 idx_fd = mkstemp(tmpfile);
697 if (idx_fd < 0)
698 die("Can't create %s: %s", tmpfile, strerror(errno));
699 f = sha1fd(idx_fd, tmpfile);
700 sha1write(f, array, 256 * sizeof(int));
701 SHA1_Init(&ctx);
702 for (c = idx; c != last; c++) {
703 unsigned int offset = htonl((*c)->offset);
704 sha1write(f, &offset, 4);
705 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
706 SHA1_Update(&ctx, (*c)->sha1, 20);
707 }
708 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
709 sha1close(f, NULL, 1);
710 free(idx);
711 SHA1_Final(pack_data->sha1, &ctx);
712 return tmpfile;
713 }
715 static char* keep_pack(char *curr_index_name)
716 {
717 static char name[PATH_MAX];
718 static char *keep_msg = "fast-import";
719 int keep_fd;
721 chmod(pack_data->pack_name, 0444);
722 chmod(curr_index_name, 0444);
724 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
725 get_object_directory(), sha1_to_hex(pack_data->sha1));
726 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
727 if (keep_fd < 0)
728 die("cannot create keep file");
729 write(keep_fd, keep_msg, strlen(keep_msg));
730 close(keep_fd);
732 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
733 get_object_directory(), sha1_to_hex(pack_data->sha1));
734 if (move_temp_to_file(pack_data->pack_name, name))
735 die("cannot store pack file");
736 printf("%s\n", name);
738 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
739 get_object_directory(), sha1_to_hex(pack_data->sha1));
740 if (move_temp_to_file(curr_index_name, name))
741 die("cannot store index file");
742 return name;
743 }
745 static void unkeep_all_packs()
746 {
747 static char name[PATH_MAX];
748 int k;
750 for (k = 0; k < pack_id; k++) {
751 struct packed_git *p = all_packs[k];
752 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
753 get_object_directory(), sha1_to_hex(p->sha1));
754 unlink(name);
755 }
756 }
758 static void end_packfile()
759 {
760 struct packed_git *old_p = pack_data, *new_p;
762 if (object_count) {
763 char *idx_name;
765 fixup_header_footer();
766 idx_name = keep_pack(create_index());
768 /* Register the packfile with core git's machinary. */
769 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
770 if (!new_p)
771 die("core git rejected index %s", idx_name);
772 new_p->windows = old_p->windows;
773 all_packs[pack_id++] = new_p;
774 install_packed_git(new_p);
775 }
776 else
777 unlink(old_p->pack_name);
778 free(old_p);
780 /* We can't carry a delta across packfiles. */
781 free(last_blob.data);
782 last_blob.data = NULL;
783 last_blob.len = 0;
784 last_blob.offset = 0;
785 last_blob.depth = 0;
786 }
788 static void checkpoint()
789 {
790 end_packfile();
791 start_packfile();
792 }
794 static size_t encode_header(
795 enum object_type type,
796 size_t size,
797 unsigned char *hdr)
798 {
799 int n = 1;
800 unsigned char c;
802 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
803 die("bad type %d", type);
805 c = (type << 4) | (size & 15);
806 size >>= 4;
807 while (size) {
808 *hdr++ = c | 0x80;
809 c = size & 0x7f;
810 size >>= 7;
811 n++;
812 }
813 *hdr = c;
814 return n;
815 }
817 static int store_object(
818 enum object_type type,
819 void *dat,
820 size_t datlen,
821 struct last_object *last,
822 unsigned char *sha1out,
823 uintmax_t mark)
824 {
825 void *out, *delta;
826 struct object_entry *e;
827 unsigned char hdr[96];
828 unsigned char sha1[20];
829 unsigned long hdrlen, deltalen;
830 SHA_CTX c;
831 z_stream s;
833 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
834 SHA1_Init(&c);
835 SHA1_Update(&c, hdr, hdrlen);
836 SHA1_Update(&c, dat, datlen);
837 SHA1_Final(sha1, &c);
838 if (sha1out)
839 hashcpy(sha1out, sha1);
841 e = insert_object(sha1);
842 if (mark)
843 insert_mark(mark, e);
844 if (e->offset) {
845 duplicate_count_by_type[type]++;
846 return 1;
847 }
849 if (last && last->data && last->depth < max_depth) {
850 delta = diff_delta(last->data, last->len,
851 dat, datlen,
852 &deltalen, 0);
853 if (delta && deltalen >= datlen) {
854 free(delta);
855 delta = NULL;
856 }
857 } else
858 delta = NULL;
860 memset(&s, 0, sizeof(s));
861 deflateInit(&s, zlib_compression_level);
862 if (delta) {
863 s.next_in = delta;
864 s.avail_in = deltalen;
865 } else {
866 s.next_in = dat;
867 s.avail_in = datlen;
868 }
869 s.avail_out = deflateBound(&s, s.avail_in);
870 s.next_out = out = xmalloc(s.avail_out);
871 while (deflate(&s, Z_FINISH) == Z_OK)
872 /* nothing */;
873 deflateEnd(&s);
875 /* Determine if we should auto-checkpoint. */
876 if ((object_count + 1) > max_objects
877 || (object_count + 1) < object_count
878 || (pack_size + 60 + s.total_out) > max_packsize
879 || (pack_size + 60 + s.total_out) < pack_size) {
881 /* This new object needs to *not* have the current pack_id. */
882 e->pack_id = pack_id + 1;
883 checkpoint();
885 /* We cannot carry a delta into the new pack. */
886 if (delta) {
887 free(delta);
888 delta = NULL;
890 memset(&s, 0, sizeof(s));
891 deflateInit(&s, zlib_compression_level);
892 s.next_in = dat;
893 s.avail_in = datlen;
894 s.avail_out = deflateBound(&s, s.avail_in);
895 s.next_out = out = xrealloc(out, s.avail_out);
896 while (deflate(&s, Z_FINISH) == Z_OK)
897 /* nothing */;
898 deflateEnd(&s);
899 }
900 }
902 e->type = type;
903 e->pack_id = pack_id;
904 e->offset = pack_size;
905 object_count++;
906 object_count_by_type[type]++;
908 if (delta) {
909 unsigned long ofs = e->offset - last->offset;
910 unsigned pos = sizeof(hdr) - 1;
912 delta_count_by_type[type]++;
913 last->depth++;
915 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
916 write_or_die(pack_data->pack_fd, hdr, hdrlen);
917 pack_size += hdrlen;
919 hdr[pos] = ofs & 127;
920 while (ofs >>= 7)
921 hdr[--pos] = 128 | (--ofs & 127);
922 write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
923 pack_size += sizeof(hdr) - pos;
924 } else {
925 if (last)
926 last->depth = 0;
927 hdrlen = encode_header(type, datlen, hdr);
928 write_or_die(pack_data->pack_fd, hdr, hdrlen);
929 pack_size += hdrlen;
930 }
932 write_or_die(pack_data->pack_fd, out, s.total_out);
933 pack_size += s.total_out;
935 free(out);
936 if (delta)
937 free(delta);
938 if (last) {
939 if (last->data && !last->no_free)
940 free(last->data);
941 last->data = dat;
942 last->offset = e->offset;
943 last->len = datlen;
944 }
945 return 0;
946 }
948 static void *gfi_unpack_entry(
949 struct object_entry *oe,
950 unsigned long *sizep)
951 {
952 static char type[20];
953 struct packed_git *p = all_packs[oe->pack_id];
954 if (p == pack_data)
955 p->pack_size = pack_size + 20;
956 return unpack_entry(p, oe->offset, type, sizep);
957 }
959 static const char *get_mode(const char *str, unsigned int *modep)
960 {
961 unsigned char c;
962 unsigned int mode = 0;
964 while ((c = *str++) != ' ') {
965 if (c < '0' || c > '7')
966 return NULL;
967 mode = (mode << 3) + (c - '0');
968 }
969 *modep = mode;
970 return str;
971 }
973 static void load_tree(struct tree_entry *root)
974 {
975 unsigned char* sha1 = root->versions[1].sha1;
976 struct object_entry *myoe;
977 struct tree_content *t;
978 unsigned long size;
979 char *buf;
980 const char *c;
982 root->tree = t = new_tree_content(8);
983 if (is_null_sha1(sha1))
984 return;
986 myoe = find_object(sha1);
987 if (myoe) {
988 if (myoe->type != OBJ_TREE)
989 die("Not a tree: %s", sha1_to_hex(sha1));
990 t->delta_depth = 0;
991 buf = gfi_unpack_entry(myoe, &size);
992 } else {
993 char type[20];
994 buf = read_sha1_file(sha1, type, &size);
995 if (!buf || strcmp(type, tree_type))
996 die("Can't load tree %s", sha1_to_hex(sha1));
997 }
999 c = buf;
1000 while (c != (buf + size)) {
1001 struct tree_entry *e = new_tree_entry();
1003 if (t->entry_count == t->entry_capacity)
1004 root->tree = t = grow_tree_content(t, 8);
1005 t->entries[t->entry_count++] = e;
1007 e->tree = NULL;
1008 c = get_mode(c, &e->versions[1].mode);
1009 if (!c)
1010 die("Corrupt mode in %s", sha1_to_hex(sha1));
1011 e->versions[0].mode = e->versions[1].mode;
1012 e->name = to_atom(c, strlen(c));
1013 c += e->name->str_len + 1;
1014 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1015 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1016 c += 20;
1017 }
1018 free(buf);
1019 }
1021 static int tecmp0 (const void *_a, const void *_b)
1022 {
1023 struct tree_entry *a = *((struct tree_entry**)_a);
1024 struct tree_entry *b = *((struct tree_entry**)_b);
1025 return base_name_compare(
1026 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1027 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1028 }
1030 static int tecmp1 (const void *_a, const void *_b)
1031 {
1032 struct tree_entry *a = *((struct tree_entry**)_a);
1033 struct tree_entry *b = *((struct tree_entry**)_b);
1034 return base_name_compare(
1035 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1036 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1037 }
1039 static void mktree(struct tree_content *t,
1040 int v,
1041 unsigned long *szp,
1042 struct dbuf *b)
1043 {
1044 size_t maxlen = 0;
1045 unsigned int i;
1046 char *c;
1048 if (!v)
1049 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1050 else
1051 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1053 for (i = 0; i < t->entry_count; i++) {
1054 if (t->entries[i]->versions[v].mode)
1055 maxlen += t->entries[i]->name->str_len + 34;
1056 }
1058 size_dbuf(b, maxlen);
1059 c = b->buffer;
1060 for (i = 0; i < t->entry_count; i++) {
1061 struct tree_entry *e = t->entries[i];
1062 if (!e->versions[v].mode)
1063 continue;
1064 c += sprintf(c, "%o", e->versions[v].mode);
1065 *c++ = ' ';
1066 strcpy(c, e->name->str_dat);
1067 c += e->name->str_len + 1;
1068 hashcpy((unsigned char*)c, e->versions[v].sha1);
1069 c += 20;
1070 }
1071 *szp = c - (char*)b->buffer;
1072 }
1074 static void store_tree(struct tree_entry *root)
1075 {
1076 struct tree_content *t = root->tree;
1077 unsigned int i, j, del;
1078 unsigned long new_len;
1079 struct last_object lo;
1080 struct object_entry *le;
1082 if (!is_null_sha1(root->versions[1].sha1))
1083 return;
1085 for (i = 0; i < t->entry_count; i++) {
1086 if (t->entries[i]->tree)
1087 store_tree(t->entries[i]);
1088 }
1090 le = find_object(root->versions[0].sha1);
1091 if (!S_ISDIR(root->versions[0].mode)
1092 || !le
1093 || le->pack_id != pack_id) {
1094 lo.data = NULL;
1095 lo.depth = 0;
1096 } else {
1097 mktree(t, 0, &lo.len, &old_tree);
1098 lo.data = old_tree.buffer;
1099 lo.offset = le->offset;
1100 lo.depth = t->delta_depth;
1101 lo.no_free = 1;
1102 }
1104 mktree(t, 1, &new_len, &new_tree);
1105 store_object(OBJ_TREE, new_tree.buffer, new_len,
1106 &lo, root->versions[1].sha1, 0);
1108 t->delta_depth = lo.depth;
1109 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1110 struct tree_entry *e = t->entries[i];
1111 if (e->versions[1].mode) {
1112 e->versions[0].mode = e->versions[1].mode;
1113 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1114 t->entries[j++] = e;
1115 } else {
1116 release_tree_entry(e);
1117 del++;
1118 }
1119 }
1120 t->entry_count -= del;
1121 }
1123 static int tree_content_set(
1124 struct tree_entry *root,
1125 const char *p,
1126 const unsigned char *sha1,
1127 const unsigned int mode)
1128 {
1129 struct tree_content *t = root->tree;
1130 const char *slash1;
1131 unsigned int i, n;
1132 struct tree_entry *e;
1134 slash1 = strchr(p, '/');
1135 if (slash1)
1136 n = slash1 - p;
1137 else
1138 n = strlen(p);
1140 for (i = 0; i < t->entry_count; i++) {
1141 e = t->entries[i];
1142 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1143 if (!slash1) {
1144 if (e->versions[1].mode == mode
1145 && !hashcmp(e->versions[1].sha1, sha1))
1146 return 0;
1147 e->versions[1].mode = mode;
1148 hashcpy(e->versions[1].sha1, sha1);
1149 if (e->tree) {
1150 release_tree_content_recursive(e->tree);
1151 e->tree = NULL;
1152 }
1153 hashclr(root->versions[1].sha1);
1154 return 1;
1155 }
1156 if (!S_ISDIR(e->versions[1].mode)) {
1157 e->tree = new_tree_content(8);
1158 e->versions[1].mode = S_IFDIR;
1159 }
1160 if (!e->tree)
1161 load_tree(e);
1162 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1163 hashclr(root->versions[1].sha1);
1164 return 1;
1165 }
1166 return 0;
1167 }
1168 }
1170 if (t->entry_count == t->entry_capacity)
1171 root->tree = t = grow_tree_content(t, 8);
1172 e = new_tree_entry();
1173 e->name = to_atom(p, n);
1174 e->versions[0].mode = 0;
1175 hashclr(e->versions[0].sha1);
1176 t->entries[t->entry_count++] = e;
1177 if (slash1) {
1178 e->tree = new_tree_content(8);
1179 e->versions[1].mode = S_IFDIR;
1180 tree_content_set(e, slash1 + 1, sha1, mode);
1181 } else {
1182 e->tree = NULL;
1183 e->versions[1].mode = mode;
1184 hashcpy(e->versions[1].sha1, sha1);
1185 }
1186 hashclr(root->versions[1].sha1);
1187 return 1;
1188 }
1190 static int tree_content_remove(struct tree_entry *root, const char *p)
1191 {
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);
1203 for (i = 0; i < t->entry_count; i++) {
1204 e = t->entries[i];
1205 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1206 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1207 goto del_entry;
1208 if (!e->tree)
1209 load_tree(e);
1210 if (tree_content_remove(e, slash1 + 1)) {
1211 for (n = 0; n < e->tree->entry_count; n++) {
1212 if (e->tree->entries[n]->versions[1].mode) {
1213 hashclr(root->versions[1].sha1);
1214 return 1;
1215 }
1216 }
1217 goto del_entry;
1218 }
1219 return 0;
1220 }
1221 }
1222 return 0;
1224 del_entry:
1225 if (e->tree) {
1226 release_tree_content_recursive(e->tree);
1227 e->tree = NULL;
1228 }
1229 e->versions[1].mode = 0;
1230 hashclr(e->versions[1].sha1);
1231 hashclr(root->versions[1].sha1);
1232 return 1;
1233 }
1235 static void dump_branches()
1236 {
1237 static const char *msg = "fast-import";
1238 unsigned int i;
1239 struct branch *b;
1240 struct ref_lock *lock;
1242 for (i = 0; i < branch_table_sz; i++) {
1243 for (b = branch_table[i]; b; b = b->table_next_branch) {
1244 lock = lock_any_ref_for_update(b->name, NULL);
1245 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1246 die("Can't write %s", b->name);
1247 }
1248 }
1249 }
1251 static void dump_tags()
1252 {
1253 static const char *msg = "fast-import";
1254 struct tag *t;
1255 struct ref_lock *lock;
1256 char path[PATH_MAX];
1258 for (t = first_tag; t; t = t->next_tag) {
1259 sprintf(path, "refs/tags/%s", t->name);
1260 lock = lock_any_ref_for_update(path, NULL);
1261 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1262 die("Can't write %s", path);
1263 }
1264 }
1266 static void dump_marks_helper(FILE *f,
1267 uintmax_t base,
1268 struct mark_set *m)
1269 {
1270 uintmax_t k;
1271 if (m->shift) {
1272 for (k = 0; k < 1024; k++) {
1273 if (m->data.sets[k])
1274 dump_marks_helper(f, (base + k) << m->shift,
1275 m->data.sets[k]);
1276 }
1277 } else {
1278 for (k = 0; k < 1024; k++) {
1279 if (m->data.marked[k])
1280 fprintf(f, ":%ju %s\n", base + k,
1281 sha1_to_hex(m->data.marked[k]->sha1));
1282 }
1283 }
1284 }
1286 static void dump_marks()
1287 {
1288 if (mark_file)
1289 {
1290 FILE *f = fopen(mark_file, "w");
1291 dump_marks_helper(f, 0, marks);
1292 fclose(f);
1293 }
1294 }
1296 static void read_next_command()
1297 {
1298 read_line(&command_buf, stdin, '\n');
1299 }
1301 static void cmd_mark()
1302 {
1303 if (!strncmp("mark :", command_buf.buf, 6)) {
1304 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1305 read_next_command();
1306 }
1307 else
1308 next_mark = 0;
1309 }
1311 static void* cmd_data (size_t *size)
1312 {
1313 size_t n = 0;
1314 void *buffer;
1315 size_t length;
1317 if (strncmp("data ", command_buf.buf, 5))
1318 die("Expected 'data n' command, found: %s", command_buf.buf);
1320 length = strtoul(command_buf.buf + 5, NULL, 10);
1321 buffer = xmalloc(length);
1323 while (n < length) {
1324 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1325 if (!s && feof(stdin))
1326 die("EOF in data (%lu bytes remaining)", length - n);
1327 n += s;
1328 }
1330 if (fgetc(stdin) != '\n')
1331 die("An lf did not trail the binary data as expected.");
1333 *size = length;
1334 return buffer;
1335 }
1337 static void cmd_new_blob()
1338 {
1339 size_t l;
1340 void *d;
1342 read_next_command();
1343 cmd_mark();
1344 d = cmd_data(&l);
1346 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1347 free(d);
1348 }
1350 static void unload_one_branch()
1351 {
1352 while (cur_active_branches
1353 && cur_active_branches >= max_active_branches) {
1354 unsigned long min_commit = ULONG_MAX;
1355 struct branch *e, *l = NULL, *p = NULL;
1357 for (e = active_branches; e; e = e->active_next_branch) {
1358 if (e->last_commit < min_commit) {
1359 p = l;
1360 min_commit = e->last_commit;
1361 }
1362 l = e;
1363 }
1365 if (p) {
1366 e = p->active_next_branch;
1367 p->active_next_branch = e->active_next_branch;
1368 } else {
1369 e = active_branches;
1370 active_branches = e->active_next_branch;
1371 }
1372 e->active_next_branch = NULL;
1373 if (e->branch_tree.tree) {
1374 release_tree_content_recursive(e->branch_tree.tree);
1375 e->branch_tree.tree = NULL;
1376 }
1377 cur_active_branches--;
1378 }
1379 }
1381 static void load_branch(struct branch *b)
1382 {
1383 load_tree(&b->branch_tree);
1384 b->active_next_branch = active_branches;
1385 active_branches = b;
1386 cur_active_branches++;
1387 branch_load_count++;
1388 }
1390 static void file_change_m(struct branch *b)
1391 {
1392 const char *p = command_buf.buf + 2;
1393 char *p_uq;
1394 const char *endp;
1395 struct object_entry *oe;
1396 unsigned char sha1[20];
1397 unsigned int mode;
1398 char type[20];
1400 p = get_mode(p, &mode);
1401 if (!p)
1402 die("Corrupt mode: %s", command_buf.buf);
1403 switch (mode) {
1404 case S_IFREG | 0644:
1405 case S_IFREG | 0755:
1406 case S_IFLNK:
1407 case 0644:
1408 case 0755:
1409 /* ok */
1410 break;
1411 default:
1412 die("Corrupt mode: %s", command_buf.buf);
1413 }
1415 if (*p == ':') {
1416 char *x;
1417 oe = find_mark(strtoumax(p + 1, &x, 10));
1418 hashcpy(sha1, oe->sha1);
1419 p = x;
1420 } else {
1421 if (get_sha1_hex(p, sha1))
1422 die("Invalid SHA1: %s", command_buf.buf);
1423 oe = find_object(sha1);
1424 p += 40;
1425 }
1426 if (*p++ != ' ')
1427 die("Missing space after SHA1: %s", command_buf.buf);
1429 p_uq = unquote_c_style(p, &endp);
1430 if (p_uq) {
1431 if (*endp)
1432 die("Garbage after path in: %s", command_buf.buf);
1433 p = p_uq;
1434 }
1436 if (oe) {
1437 if (oe->type != OBJ_BLOB)
1438 die("Not a blob (actually a %s): %s",
1439 command_buf.buf, type_names[oe->type]);
1440 } else {
1441 if (sha1_object_info(sha1, type, NULL))
1442 die("Blob not found: %s", command_buf.buf);
1443 if (strcmp(blob_type, type))
1444 die("Not a blob (actually a %s): %s",
1445 command_buf.buf, type);
1446 }
1448 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1450 if (p_uq)
1451 free(p_uq);
1452 }
1454 static void file_change_d(struct branch *b)
1455 {
1456 const char *p = command_buf.buf + 2;
1457 char *p_uq;
1458 const char *endp;
1460 p_uq = unquote_c_style(p, &endp);
1461 if (p_uq) {
1462 if (*endp)
1463 die("Garbage after path in: %s", command_buf.buf);
1464 p = p_uq;
1465 }
1466 tree_content_remove(&b->branch_tree, p);
1467 if (p_uq)
1468 free(p_uq);
1469 }
1471 static void cmd_from(struct branch *b)
1472 {
1473 const char *from, *endp;
1474 char *str_uq;
1475 struct branch *s;
1477 if (strncmp("from ", command_buf.buf, 5))
1478 return;
1480 if (b->last_commit)
1481 die("Can't reinitailize branch %s", b->name);
1483 from = strchr(command_buf.buf, ' ') + 1;
1484 str_uq = unquote_c_style(from, &endp);
1485 if (str_uq) {
1486 if (*endp)
1487 die("Garbage after string in: %s", command_buf.buf);
1488 from = str_uq;
1489 }
1491 s = lookup_branch(from);
1492 if (b == s)
1493 die("Can't create a branch from itself: %s", b->name);
1494 else if (s) {
1495 unsigned char *t = s->branch_tree.versions[1].sha1;
1496 hashcpy(b->sha1, s->sha1);
1497 hashcpy(b->branch_tree.versions[0].sha1, t);
1498 hashcpy(b->branch_tree.versions[1].sha1, t);
1499 } else if (*from == ':') {
1500 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1501 struct object_entry *oe = find_mark(idnum);
1502 unsigned long size;
1503 char *buf;
1504 if (oe->type != OBJ_COMMIT)
1505 die("Mark :%ju not a commit", idnum);
1506 hashcpy(b->sha1, oe->sha1);
1507 buf = gfi_unpack_entry(oe, &size);
1508 if (!buf || size < 46)
1509 die("Not a valid commit: %s", from);
1510 if (memcmp("tree ", buf, 5)
1511 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1512 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1513 free(buf);
1514 hashcpy(b->branch_tree.versions[0].sha1,
1515 b->branch_tree.versions[1].sha1);
1516 } else if (!get_sha1(from, b->sha1)) {
1517 if (is_null_sha1(b->sha1)) {
1518 hashclr(b->branch_tree.versions[0].sha1);
1519 hashclr(b->branch_tree.versions[1].sha1);
1520 } else {
1521 unsigned long size;
1522 char *buf;
1524 buf = read_object_with_reference(b->sha1,
1525 type_names[OBJ_COMMIT], &size, b->sha1);
1526 if (!buf || size < 46)
1527 die("Not a valid commit: %s", from);
1528 if (memcmp("tree ", buf, 5)
1529 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1530 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1531 free(buf);
1532 hashcpy(b->branch_tree.versions[0].sha1,
1533 b->branch_tree.versions[1].sha1);
1534 }
1535 } else
1536 die("Invalid ref name or SHA1 expression: %s", from);
1538 read_next_command();
1539 }
1541 static struct hash_list* cmd_merge(unsigned int *count)
1542 {
1543 struct hash_list *list = NULL, *n, *e;
1544 const char *from, *endp;
1545 char *str_uq;
1546 struct branch *s;
1548 *count = 0;
1549 while (!strncmp("merge ", command_buf.buf, 6)) {
1550 from = strchr(command_buf.buf, ' ') + 1;
1551 str_uq = unquote_c_style(from, &endp);
1552 if (str_uq) {
1553 if (*endp)
1554 die("Garbage after string in: %s", command_buf.buf);
1555 from = str_uq;
1556 }
1558 n = xmalloc(sizeof(*n));
1559 s = lookup_branch(from);
1560 if (s)
1561 hashcpy(n->sha1, s->sha1);
1562 else if (*from == ':') {
1563 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1564 struct object_entry *oe = find_mark(idnum);
1565 if (oe->type != OBJ_COMMIT)
1566 die("Mark :%ju not a commit", idnum);
1567 hashcpy(n->sha1, oe->sha1);
1568 } else if (get_sha1(from, n->sha1))
1569 die("Invalid ref name or SHA1 expression: %s", from);
1571 n->next = NULL;
1572 if (list)
1573 e->next = n;
1574 else
1575 list = n;
1576 e = n;
1577 *count++;
1578 read_next_command();
1579 }
1580 return list;
1581 }
1583 static void cmd_new_commit()
1584 {
1585 struct branch *b;
1586 void *msg;
1587 size_t msglen;
1588 char *str_uq;
1589 const char *endp;
1590 char *sp;
1591 char *author = NULL;
1592 char *committer = NULL;
1593 struct hash_list *merge_list = NULL;
1594 unsigned int merge_count;
1596 /* Obtain the branch name from the rest of our command */
1597 sp = strchr(command_buf.buf, ' ') + 1;
1598 str_uq = unquote_c_style(sp, &endp);
1599 if (str_uq) {
1600 if (*endp)
1601 die("Garbage after ref in: %s", command_buf.buf);
1602 sp = str_uq;
1603 }
1604 b = lookup_branch(sp);
1605 if (!b)
1606 b = new_branch(sp);
1607 if (str_uq)
1608 free(str_uq);
1610 read_next_command();
1611 cmd_mark();
1612 if (!strncmp("author ", command_buf.buf, 7)) {
1613 author = strdup(command_buf.buf);
1614 read_next_command();
1615 }
1616 if (!strncmp("committer ", command_buf.buf, 10)) {
1617 committer = strdup(command_buf.buf);
1618 read_next_command();
1619 }
1620 if (!committer)
1621 die("Expected committer but didn't get one");
1622 msg = cmd_data(&msglen);
1623 read_next_command();
1624 cmd_from(b);
1625 merge_list = cmd_merge(&merge_count);
1627 /* ensure the branch is active/loaded */
1628 if (!b->branch_tree.tree || !max_active_branches) {
1629 unload_one_branch();
1630 load_branch(b);
1631 }
1633 /* file_change* */
1634 for (;;) {
1635 if (1 == command_buf.len)
1636 break;
1637 else if (!strncmp("M ", command_buf.buf, 2))
1638 file_change_m(b);
1639 else if (!strncmp("D ", command_buf.buf, 2))
1640 file_change_d(b);
1641 else
1642 die("Unsupported file_change: %s", command_buf.buf);
1643 read_next_command();
1644 }
1646 /* build the tree and the commit */
1647 store_tree(&b->branch_tree);
1648 hashcpy(b->branch_tree.versions[0].sha1,
1649 b->branch_tree.versions[1].sha1);
1650 size_dbuf(&new_data, 97 + msglen
1651 + merge_count * 49
1652 + (author
1653 ? strlen(author) + strlen(committer)
1654 : 2 * strlen(committer)));
1655 sp = new_data.buffer;
1656 sp += sprintf(sp, "tree %s\n",
1657 sha1_to_hex(b->branch_tree.versions[1].sha1));
1658 if (!is_null_sha1(b->sha1))
1659 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1660 while (merge_list) {
1661 struct hash_list *next = merge_list->next;
1662 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1663 free(merge_list);
1664 merge_list = next;
1665 }
1666 if (author)
1667 sp += sprintf(sp, "%s\n", author);
1668 else
1669 sp += sprintf(sp, "author %s\n", committer + 10);
1670 sp += sprintf(sp, "%s\n\n", committer);
1671 memcpy(sp, msg, msglen);
1672 sp += msglen;
1673 if (author)
1674 free(author);
1675 free(committer);
1676 free(msg);
1678 store_object(OBJ_COMMIT,
1679 new_data.buffer, sp - (char*)new_data.buffer,
1680 NULL, b->sha1, next_mark);
1681 b->last_commit = object_count_by_type[OBJ_COMMIT];
1683 if (branch_log) {
1684 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1685 fprintf(branch_log, "commit ");
1686 if (need_dq) {
1687 fputc('"', branch_log);
1688 quote_c_style(b->name, NULL, branch_log, 0);
1689 fputc('"', branch_log);
1690 } else
1691 fprintf(branch_log, "%s", b->name);
1692 fprintf(branch_log," :%ju %s\n",next_mark,sha1_to_hex(b->sha1));
1693 }
1694 }
1696 static void cmd_new_tag()
1697 {
1698 char *str_uq;
1699 const char *endp;
1700 char *sp;
1701 const char *from;
1702 char *tagger;
1703 struct branch *s;
1704 void *msg;
1705 size_t msglen;
1706 struct tag *t;
1707 uintmax_t from_mark = 0;
1708 unsigned char sha1[20];
1710 /* Obtain the new tag name from the rest of our command */
1711 sp = strchr(command_buf.buf, ' ') + 1;
1712 str_uq = unquote_c_style(sp, &endp);
1713 if (str_uq) {
1714 if (*endp)
1715 die("Garbage after tag name in: %s", command_buf.buf);
1716 sp = str_uq;
1717 }
1718 t = pool_alloc(sizeof(struct tag));
1719 t->next_tag = NULL;
1720 t->name = pool_strdup(sp);
1721 if (last_tag)
1722 last_tag->next_tag = t;
1723 else
1724 first_tag = t;
1725 last_tag = t;
1726 if (str_uq)
1727 free(str_uq);
1728 read_next_command();
1730 /* from ... */
1731 if (strncmp("from ", command_buf.buf, 5))
1732 die("Expected from command, got %s", command_buf.buf);
1734 from = strchr(command_buf.buf, ' ') + 1;
1735 str_uq = unquote_c_style(from, &endp);
1736 if (str_uq) {
1737 if (*endp)
1738 die("Garbage after string in: %s", command_buf.buf);
1739 from = str_uq;
1740 }
1742 s = lookup_branch(from);
1743 if (s) {
1744 hashcpy(sha1, s->sha1);
1745 } else if (*from == ':') {
1746 from_mark = strtoumax(from + 1, NULL, 10);
1747 struct object_entry *oe = find_mark(from_mark);
1748 if (oe->type != OBJ_COMMIT)
1749 die("Mark :%ju not a commit", from_mark);
1750 hashcpy(sha1, oe->sha1);
1751 } else if (!get_sha1(from, sha1)) {
1752 unsigned long size;
1753 char *buf;
1755 buf = read_object_with_reference(sha1,
1756 type_names[OBJ_COMMIT], &size, sha1);
1757 if (!buf || size < 46)
1758 die("Not a valid commit: %s", from);
1759 free(buf);
1760 } else
1761 die("Invalid ref name or SHA1 expression: %s", from);
1763 if (str_uq)
1764 free(str_uq);
1765 read_next_command();
1767 /* tagger ... */
1768 if (strncmp("tagger ", command_buf.buf, 7))
1769 die("Expected tagger command, got %s", command_buf.buf);
1770 tagger = strdup(command_buf.buf);
1772 /* tag payload/message */
1773 read_next_command();
1774 msg = cmd_data(&msglen);
1776 /* build the tag object */
1777 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1778 sp = new_data.buffer;
1779 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1780 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1781 sp += sprintf(sp, "tag %s\n", t->name);
1782 sp += sprintf(sp, "%s\n\n", tagger);
1783 memcpy(sp, msg, msglen);
1784 sp += msglen;
1785 free(tagger);
1786 free(msg);
1788 store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1789 NULL, t->sha1, 0);
1791 if (branch_log) {
1792 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1793 fprintf(branch_log, "tag ");
1794 if (need_dq) {
1795 fputc('"', branch_log);
1796 quote_c_style(t->name, NULL, branch_log, 0);
1797 fputc('"', branch_log);
1798 } else
1799 fprintf(branch_log, "%s", t->name);
1800 fprintf(branch_log," :%ju %s\n",from_mark,sha1_to_hex(t->sha1));
1801 }
1802 }
1804 static void cmd_reset_branch()
1805 {
1806 struct branch *b;
1807 char *str_uq;
1808 const char *endp;
1809 char *sp;
1811 /* Obtain the branch name from the rest of our command */
1812 sp = strchr(command_buf.buf, ' ') + 1;
1813 str_uq = unquote_c_style(sp, &endp);
1814 if (str_uq) {
1815 if (*endp)
1816 die("Garbage after ref in: %s", command_buf.buf);
1817 sp = str_uq;
1818 }
1819 b = lookup_branch(sp);
1820 if (b) {
1821 b->last_commit = 0;
1822 if (b->branch_tree.tree) {
1823 release_tree_content_recursive(b->branch_tree.tree);
1824 b->branch_tree.tree = NULL;
1825 }
1826 }
1827 else
1828 b = new_branch(sp);
1829 if (str_uq)
1830 free(str_uq);
1831 read_next_command();
1832 cmd_from(b);
1833 }
1835 static void cmd_checkpoint()
1836 {
1837 if (object_count)
1838 checkpoint();
1839 read_next_command();
1840 }
1842 static const char fast_import_usage[] =
1843 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1845 int main(int argc, const char **argv)
1846 {
1847 int i;
1848 uintmax_t est_obj_cnt = object_entry_alloc;
1849 uintmax_t duplicate_count;
1851 setup_ident();
1852 git_config(git_default_config);
1854 for (i = 1; i < argc; i++) {
1855 const char *a = argv[i];
1857 if (*a != '-' || !strcmp(a, "--"))
1858 break;
1859 else if (!strncmp(a, "--objects=", 10))
1860 est_obj_cnt = strtoumax(a + 10, NULL, 0);
1861 else if (!strncmp(a, "--max-objects-per-pack=", 23))
1862 max_objects = strtoumax(a + 23, NULL, 0);
1863 else if (!strncmp(a, "--max-pack-size=", 16))
1864 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
1865 else if (!strncmp(a, "--depth=", 8))
1866 max_depth = strtoul(a + 8, NULL, 0);
1867 else if (!strncmp(a, "--active-branches=", 18))
1868 max_active_branches = strtoul(a + 18, NULL, 0);
1869 else if (!strncmp(a, "--export-marks=", 15))
1870 mark_file = a + 15;
1871 else if (!strncmp(a, "--branch-log=", 13)) {
1872 branch_log = fopen(a + 13, "w");
1873 if (!branch_log)
1874 die("Can't create %s: %s", a + 13, strerror(errno));
1875 }
1876 else
1877 die("unknown option %s", a);
1878 }
1879 if (i != argc)
1880 usage(fast_import_usage);
1882 alloc_objects(est_obj_cnt);
1883 strbuf_init(&command_buf);
1885 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1886 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1887 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1888 marks = pool_calloc(1, sizeof(struct mark_set));
1890 start_packfile();
1891 for (;;) {
1892 read_next_command();
1893 if (command_buf.eof)
1894 break;
1895 else if (!strcmp("blob", command_buf.buf))
1896 cmd_new_blob();
1897 else if (!strncmp("commit ", command_buf.buf, 7))
1898 cmd_new_commit();
1899 else if (!strncmp("tag ", command_buf.buf, 4))
1900 cmd_new_tag();
1901 else if (!strncmp("reset ", command_buf.buf, 6))
1902 cmd_reset_branch();
1903 else if (!strcmp("checkpoint", command_buf.buf))
1904 cmd_checkpoint();
1905 else
1906 die("Unsupported command: %s", command_buf.buf);
1907 }
1908 end_packfile();
1910 dump_branches();
1911 dump_tags();
1912 unkeep_all_packs();
1913 dump_marks();
1914 if (branch_log)
1915 fclose(branch_log);
1917 duplicate_count = 0;
1918 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
1919 duplicate_count += duplicate_count_by_type[i];
1921 fprintf(stderr, "%s statistics:\n", argv[0]);
1922 fprintf(stderr, "---------------------------------------------------------------------\n");
1923 fprintf(stderr, "Alloc'd objects: %10ju (%10ju overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1924 fprintf(stderr, "Total objects: %10ju (%10ju duplicates )\n", object_count, duplicate_count);
1925 fprintf(stderr, " blobs : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
1926 fprintf(stderr, " trees : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
1927 fprintf(stderr, " commits: %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
1928 fprintf(stderr, " tags : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
1929 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1930 fprintf(stderr, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
1931 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1932 fprintf(stderr, "Memory total: %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1933 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1934 fprintf(stderr, " objects: %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1935 fprintf(stderr, "---------------------------------------------------------------------\n");
1936 pack_report();
1937 fprintf(stderr, "---------------------------------------------------------------------\n");
1938 fprintf(stderr, "\n");
1940 return 0;
1941 }