393020504a1d2ac3690298398a033c6a4d6ea819
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 = -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 int pack_fd;
248 static unsigned long pack_size;
250 /* Table of objects we've written. */
251 static unsigned int object_entry_alloc = 5000;
252 static struct object_entry_pool *blocks;
253 static struct object_entry *object_table[1 << 16];
254 static struct mark_set *marks;
255 static const char* mark_file;
257 /* Our last blob */
258 static struct last_object last_blob;
260 /* Tree management */
261 static unsigned int tree_entry_alloc = 1000;
262 static void *avail_tree_entry;
263 static unsigned int avail_tree_table_sz = 100;
264 static struct avail_tree_content **avail_tree_table;
265 static struct dbuf old_tree;
266 static struct dbuf new_tree;
268 /* Branch data */
269 static unsigned long max_active_branches = 5;
270 static unsigned long cur_active_branches;
271 static unsigned long branch_table_sz = 1039;
272 static struct branch **branch_table;
273 static struct branch *active_branches;
275 /* Tag data */
276 static struct tag *first_tag;
277 static struct tag *last_tag;
279 /* Input stream parsing */
280 static struct strbuf command_buf;
281 static uintmax_t next_mark;
282 static struct dbuf new_data;
283 static FILE* branch_log;
286 static void alloc_objects(unsigned int cnt)
287 {
288 struct object_entry_pool *b;
290 b = xmalloc(sizeof(struct object_entry_pool)
291 + cnt * sizeof(struct object_entry));
292 b->next_pool = blocks;
293 b->next_free = b->entries;
294 b->end = b->entries + cnt;
295 blocks = b;
296 alloc_count += cnt;
297 }
299 static struct object_entry* new_object(unsigned char *sha1)
300 {
301 struct object_entry *e;
303 if (blocks->next_free == blocks->end)
304 alloc_objects(object_entry_alloc);
306 e = blocks->next_free++;
307 hashcpy(e->sha1, sha1);
308 return e;
309 }
311 static struct object_entry* find_object(unsigned char *sha1)
312 {
313 unsigned int h = sha1[0] << 8 | sha1[1];
314 struct object_entry *e;
315 for (e = object_table[h]; e; e = e->next)
316 if (!hashcmp(sha1, e->sha1))
317 return e;
318 return NULL;
319 }
321 static struct object_entry* insert_object(unsigned char *sha1)
322 {
323 unsigned int h = sha1[0] << 8 | sha1[1];
324 struct object_entry *e = object_table[h];
325 struct object_entry *p = NULL;
327 while (e) {
328 if (!hashcmp(sha1, e->sha1))
329 return e;
330 p = e;
331 e = e->next;
332 }
334 e = new_object(sha1);
335 e->next = NULL;
336 e->offset = 0;
337 if (p)
338 p->next = e;
339 else
340 object_table[h] = e;
341 return e;
342 }
344 static unsigned int hc_str(const char *s, size_t len)
345 {
346 unsigned int r = 0;
347 while (len-- > 0)
348 r = r * 31 + *s++;
349 return r;
350 }
352 static void* pool_alloc(size_t len)
353 {
354 struct mem_pool *p;
355 void *r;
357 for (p = mem_pool; p; p = p->next_pool)
358 if ((p->end - p->next_free >= len))
359 break;
361 if (!p) {
362 if (len >= (mem_pool_alloc/2)) {
363 total_allocd += len;
364 return xmalloc(len);
365 }
366 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
367 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
368 p->next_pool = mem_pool;
369 p->next_free = p->space;
370 p->end = p->next_free + mem_pool_alloc;
371 mem_pool = p;
372 }
374 r = p->next_free;
375 /* round out to a pointer alignment */
376 if (len & (sizeof(void*) - 1))
377 len += sizeof(void*) - (len & (sizeof(void*) - 1));
378 p->next_free += len;
379 return r;
380 }
382 static void* pool_calloc(size_t count, size_t size)
383 {
384 size_t len = count * size;
385 void *r = pool_alloc(len);
386 memset(r, 0, len);
387 return r;
388 }
390 static char* pool_strdup(const char *s)
391 {
392 char *r = pool_alloc(strlen(s) + 1);
393 strcpy(r, s);
394 return r;
395 }
397 static void size_dbuf(struct dbuf *b, size_t maxlen)
398 {
399 if (b->buffer) {
400 if (b->capacity >= maxlen)
401 return;
402 free(b->buffer);
403 }
404 b->capacity = ((maxlen / 1024) + 1) * 1024;
405 b->buffer = xmalloc(b->capacity);
406 }
408 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
409 {
410 struct mark_set *s = marks;
411 while ((idnum >> s->shift) >= 1024) {
412 s = pool_calloc(1, sizeof(struct mark_set));
413 s->shift = marks->shift + 10;
414 s->data.sets[0] = marks;
415 marks = s;
416 }
417 while (s->shift) {
418 uintmax_t i = idnum >> s->shift;
419 idnum -= i << s->shift;
420 if (!s->data.sets[i]) {
421 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
422 s->data.sets[i]->shift = s->shift - 10;
423 }
424 s = s->data.sets[i];
425 }
426 if (!s->data.marked[idnum])
427 marks_set_count++;
428 s->data.marked[idnum] = oe;
429 }
431 static struct object_entry* find_mark(uintmax_t idnum)
432 {
433 uintmax_t orig_idnum = idnum;
434 struct mark_set *s = marks;
435 struct object_entry *oe = NULL;
436 if ((idnum >> s->shift) < 1024) {
437 while (s && s->shift) {
438 uintmax_t i = idnum >> s->shift;
439 idnum -= i << s->shift;
440 s = s->data.sets[i];
441 }
442 if (s)
443 oe = s->data.marked[idnum];
444 }
445 if (!oe)
446 die("mark :%ju not declared", orig_idnum);
447 return oe;
448 }
450 static struct atom_str* to_atom(const char *s, size_t len)
451 {
452 unsigned int hc = hc_str(s, len) % atom_table_sz;
453 struct atom_str *c;
455 for (c = atom_table[hc]; c; c = c->next_atom)
456 if (c->str_len == len && !strncmp(s, c->str_dat, len))
457 return c;
459 c = pool_alloc(sizeof(struct atom_str) + len + 1);
460 c->str_len = len;
461 strncpy(c->str_dat, s, len);
462 c->str_dat[len] = 0;
463 c->next_atom = atom_table[hc];
464 atom_table[hc] = c;
465 atom_cnt++;
466 return c;
467 }
469 static struct branch* lookup_branch(const char *name)
470 {
471 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
472 struct branch *b;
474 for (b = branch_table[hc]; b; b = b->table_next_branch)
475 if (!strcmp(name, b->name))
476 return b;
477 return NULL;
478 }
480 static struct branch* new_branch(const char *name)
481 {
482 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
483 struct branch* b = lookup_branch(name);
485 if (b)
486 die("Invalid attempt to create duplicate branch: %s", name);
487 if (check_ref_format(name))
488 die("Branch name doesn't conform to GIT standards: %s", name);
490 b = pool_calloc(1, sizeof(struct branch));
491 b->name = pool_strdup(name);
492 b->table_next_branch = branch_table[hc];
493 b->branch_tree.versions[0].mode = S_IFDIR;
494 b->branch_tree.versions[1].mode = S_IFDIR;
495 branch_table[hc] = b;
496 branch_count++;
497 return b;
498 }
500 static unsigned int hc_entries(unsigned int cnt)
501 {
502 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
503 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
504 }
506 static struct tree_content* new_tree_content(unsigned int cnt)
507 {
508 struct avail_tree_content *f, *l = NULL;
509 struct tree_content *t;
510 unsigned int hc = hc_entries(cnt);
512 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
513 if (f->entry_capacity >= cnt)
514 break;
516 if (f) {
517 if (l)
518 l->next_avail = f->next_avail;
519 else
520 avail_tree_table[hc] = f->next_avail;
521 } else {
522 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
523 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
524 f->entry_capacity = cnt;
525 }
527 t = (struct tree_content*)f;
528 t->entry_count = 0;
529 t->delta_depth = 0;
530 return t;
531 }
533 static void release_tree_entry(struct tree_entry *e);
534 static void release_tree_content(struct tree_content *t)
535 {
536 struct avail_tree_content *f = (struct avail_tree_content*)t;
537 unsigned int hc = hc_entries(f->entry_capacity);
538 f->next_avail = avail_tree_table[hc];
539 avail_tree_table[hc] = f;
540 }
542 static void release_tree_content_recursive(struct tree_content *t)
543 {
544 unsigned int i;
545 for (i = 0; i < t->entry_count; i++)
546 release_tree_entry(t->entries[i]);
547 release_tree_content(t);
548 }
550 static struct tree_content* grow_tree_content(
551 struct tree_content *t,
552 int amt)
553 {
554 struct tree_content *r = new_tree_content(t->entry_count + amt);
555 r->entry_count = t->entry_count;
556 r->delta_depth = t->delta_depth;
557 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
558 release_tree_content(t);
559 return r;
560 }
562 static struct tree_entry* new_tree_entry()
563 {
564 struct tree_entry *e;
566 if (!avail_tree_entry) {
567 unsigned int n = tree_entry_alloc;
568 total_allocd += n * sizeof(struct tree_entry);
569 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
570 while (n-- > 1) {
571 *((void**)e) = e + 1;
572 e++;
573 }
574 *((void**)e) = NULL;
575 }
577 e = avail_tree_entry;
578 avail_tree_entry = *((void**)e);
579 return e;
580 }
582 static void release_tree_entry(struct tree_entry *e)
583 {
584 if (e->tree)
585 release_tree_content_recursive(e->tree);
586 *((void**)e) = avail_tree_entry;
587 avail_tree_entry = e;
588 }
590 static void start_packfile()
591 {
592 static char tmpfile[PATH_MAX];
593 struct packed_git *p;
594 struct pack_header hdr;
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(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 SHA_CTX c;
621 char hdr[8];
622 unsigned long cnt;
623 char *buf;
625 if (lseek(pack_fd, 0, SEEK_SET) != 0)
626 die("Failed seeking to start: %s", strerror(errno));
628 SHA1_Init(&c);
629 if (read_in_full(pack_fd, hdr, 8) != 8)
630 die("Unable to reread header of %s", pack_data->pack_name);
631 SHA1_Update(&c, hdr, 8);
633 cnt = htonl(object_count);
634 SHA1_Update(&c, &cnt, 4);
635 write_or_die(pack_fd, &cnt, 4);
637 buf = xmalloc(128 * 1024);
638 for (;;) {
639 size_t n = xread(pack_fd, buf, 128 * 1024);
640 if (n <= 0)
641 break;
642 SHA1_Update(&c, buf, n);
643 }
644 free(buf);
646 SHA1_Final(pack_data->sha1, &c);
647 write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
648 }
650 static int oecmp (const void *a_, const void *b_)
651 {
652 struct object_entry *a = *((struct object_entry**)a_);
653 struct object_entry *b = *((struct object_entry**)b_);
654 return hashcmp(a->sha1, b->sha1);
655 }
657 static char* create_index()
658 {
659 static char tmpfile[PATH_MAX];
660 SHA_CTX ctx;
661 struct sha1file *f;
662 struct object_entry **idx, **c, **last, *e;
663 struct object_entry_pool *o;
664 unsigned int array[256];
665 int i, idx_fd;
667 /* Build the sorted table of object IDs. */
668 idx = xmalloc(object_count * sizeof(struct object_entry*));
669 c = idx;
670 for (o = blocks; o; o = o->next_pool)
671 for (e = o->next_free; e-- != o->entries;)
672 if (pack_id == e->pack_id)
673 *c++ = e;
674 last = idx + object_count;
675 if (c != last)
676 die("internal consistency error creating the index");
677 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
679 /* Generate the fan-out array. */
680 c = idx;
681 for (i = 0; i < 256; i++) {
682 struct object_entry **next = c;;
683 while (next < last) {
684 if ((*next)->sha1[0] != i)
685 break;
686 next++;
687 }
688 array[i] = htonl(next - idx);
689 c = next;
690 }
692 snprintf(tmpfile, sizeof(tmpfile),
693 "%s/index_XXXXXX", get_object_directory());
694 idx_fd = mkstemp(tmpfile);
695 if (idx_fd < 0)
696 die("Can't create %s: %s", tmpfile, strerror(errno));
697 f = sha1fd(idx_fd, tmpfile);
698 sha1write(f, array, 256 * sizeof(int));
699 SHA1_Init(&ctx);
700 for (c = idx; c != last; c++) {
701 unsigned int offset = htonl((*c)->offset);
702 sha1write(f, &offset, 4);
703 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
704 SHA1_Update(&ctx, (*c)->sha1, 20);
705 }
706 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
707 sha1close(f, NULL, 1);
708 free(idx);
709 SHA1_Final(pack_data->sha1, &ctx);
710 return tmpfile;
711 }
713 static char* keep_pack(char *curr_index_name)
714 {
715 static char name[PATH_MAX];
716 static char *keep_msg = "fast-import";
717 int keep_fd;
719 chmod(pack_data->pack_name, 0444);
720 chmod(curr_index_name, 0444);
722 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
723 get_object_directory(), sha1_to_hex(pack_data->sha1));
724 keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
725 if (keep_fd < 0)
726 die("cannot create keep file");
727 write(keep_fd, keep_msg, strlen(keep_msg));
728 close(keep_fd);
730 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
731 get_object_directory(), sha1_to_hex(pack_data->sha1));
732 if (move_temp_to_file(pack_data->pack_name, name))
733 die("cannot store pack file");
734 printf("%s\n", name);
736 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
737 get_object_directory(), sha1_to_hex(pack_data->sha1));
738 if (move_temp_to_file(curr_index_name, name))
739 die("cannot store index file");
740 return name;
741 }
743 static void unkeep_all_packs()
744 {
745 static char name[PATH_MAX];
746 int k;
748 for (k = 0; k < pack_id; k++) {
749 struct packed_git *p = all_packs[k];
750 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
751 get_object_directory(), sha1_to_hex(p->sha1));
752 unlink(name);
753 }
754 }
756 static void end_packfile()
757 {
758 struct packed_git *old_p = pack_data, *new_p;
760 if (object_count) {
761 char *idx_name;
763 fixup_header_footer();
764 idx_name = keep_pack(create_index());
766 /* Register the packfile with core git's machinary. */
767 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
768 if (!new_p)
769 die("core git rejected index %s", idx_name);
770 new_p->windows = old_p->windows;
771 new_p->pack_fd = old_p->pack_fd;
772 all_packs[pack_id++] = new_p;
773 install_packed_git(new_p);
774 }
775 else {
776 close(pack_fd);
777 unlink(old_p->pack_name);
778 }
779 free(old_p);
781 /* We can't carry a delta across packfiles. */
782 free(last_blob.data);
783 last_blob.data = NULL;
784 last_blob.len = 0;
785 last_blob.offset = 0;
786 last_blob.depth = 0;
787 }
789 static void checkpoint()
790 {
791 end_packfile();
792 start_packfile();
793 }
795 static size_t encode_header(
796 enum object_type type,
797 size_t size,
798 unsigned char *hdr)
799 {
800 int n = 1;
801 unsigned char c;
803 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
804 die("bad type %d", type);
806 c = (type << 4) | (size & 15);
807 size >>= 4;
808 while (size) {
809 *hdr++ = c | 0x80;
810 c = size & 0x7f;
811 size >>= 7;
812 n++;
813 }
814 *hdr = c;
815 return n;
816 }
818 static int store_object(
819 enum object_type type,
820 void *dat,
821 size_t datlen,
822 struct last_object *last,
823 unsigned char *sha1out,
824 uintmax_t mark)
825 {
826 void *out, *delta;
827 struct object_entry *e;
828 unsigned char hdr[96];
829 unsigned char sha1[20];
830 unsigned long hdrlen, deltalen;
831 SHA_CTX c;
832 z_stream s;
834 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
835 SHA1_Init(&c);
836 SHA1_Update(&c, hdr, hdrlen);
837 SHA1_Update(&c, dat, datlen);
838 SHA1_Final(sha1, &c);
839 if (sha1out)
840 hashcpy(sha1out, sha1);
842 e = insert_object(sha1);
843 if (mark)
844 insert_mark(mark, e);
845 if (e->offset) {
846 duplicate_count_by_type[type]++;
847 return 1;
848 }
850 if (last && last->data && last->depth < max_depth) {
851 delta = diff_delta(last->data, last->len,
852 dat, datlen,
853 &deltalen, 0);
854 if (delta && deltalen >= datlen) {
855 free(delta);
856 delta = NULL;
857 }
858 } else
859 delta = NULL;
861 memset(&s, 0, sizeof(s));
862 deflateInit(&s, zlib_compression_level);
863 if (delta) {
864 s.next_in = delta;
865 s.avail_in = deltalen;
866 } else {
867 s.next_in = dat;
868 s.avail_in = datlen;
869 }
870 s.avail_out = deflateBound(&s, s.avail_in);
871 s.next_out = out = xmalloc(s.avail_out);
872 while (deflate(&s, Z_FINISH) == Z_OK)
873 /* nothing */;
874 deflateEnd(&s);
876 /* Determine if we should auto-checkpoint. */
877 if ((object_count + 1) > max_objects
878 || (object_count + 1) < object_count
879 || (pack_size + 60 + s.total_out) > max_packsize
880 || (pack_size + 60 + s.total_out) < pack_size) {
882 /* This new object needs to *not* have the current pack_id. */
883 e->pack_id = pack_id + 1;
884 checkpoint();
886 /* We cannot carry a delta into the new pack. */
887 if (delta) {
888 free(delta);
889 delta = NULL;
891 memset(&s, 0, sizeof(s));
892 deflateInit(&s, zlib_compression_level);
893 s.next_in = dat;
894 s.avail_in = datlen;
895 s.avail_out = deflateBound(&s, s.avail_in);
896 s.next_out = out = xrealloc(out, s.avail_out);
897 while (deflate(&s, Z_FINISH) == Z_OK)
898 /* nothing */;
899 deflateEnd(&s);
900 }
901 }
903 e->type = type;
904 e->pack_id = pack_id;
905 e->offset = pack_size;
906 object_count++;
907 object_count_by_type[type]++;
909 if (delta) {
910 unsigned long ofs = e->offset - last->offset;
911 unsigned pos = sizeof(hdr) - 1;
913 delta_count_by_type[type]++;
914 last->depth++;
916 hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
917 write_or_die(pack_fd, hdr, hdrlen);
918 pack_size += hdrlen;
920 hdr[pos] = ofs & 127;
921 while (ofs >>= 7)
922 hdr[--pos] = 128 | (--ofs & 127);
923 write_or_die(pack_fd, hdr + pos, sizeof(hdr) - pos);
924 pack_size += sizeof(hdr) - pos;
925 } else {
926 if (last)
927 last->depth = 0;
928 hdrlen = encode_header(type, datlen, hdr);
929 write_or_die(pack_fd, hdr, hdrlen);
930 pack_size += hdrlen;
931 }
933 write_or_die(pack_fd, out, s.total_out);
934 pack_size += s.total_out;
936 free(out);
937 if (delta)
938 free(delta);
939 if (last) {
940 if (last->data && !last->no_free)
941 free(last->data);
942 last->data = dat;
943 last->offset = e->offset;
944 last->len = datlen;
945 }
946 return 0;
947 }
949 static void *gfi_unpack_entry(
950 struct object_entry *oe,
951 unsigned long *sizep)
952 {
953 static char type[20];
954 struct packed_git *p = all_packs[oe->pack_id];
955 if (p == pack_data)
956 p->pack_size = pack_size + 20;
957 return unpack_entry(p, oe->offset, type, sizep);
958 }
960 static const char *get_mode(const char *str, unsigned int *modep)
961 {
962 unsigned char c;
963 unsigned int mode = 0;
965 while ((c = *str++) != ' ') {
966 if (c < '0' || c > '7')
967 return NULL;
968 mode = (mode << 3) + (c - '0');
969 }
970 *modep = mode;
971 return str;
972 }
974 static void load_tree(struct tree_entry *root)
975 {
976 unsigned char* sha1 = root->versions[1].sha1;
977 struct object_entry *myoe;
978 struct tree_content *t;
979 unsigned long size;
980 char *buf;
981 const char *c;
983 root->tree = t = new_tree_content(8);
984 if (is_null_sha1(sha1))
985 return;
987 myoe = find_object(sha1);
988 if (myoe) {
989 if (myoe->type != OBJ_TREE)
990 die("Not a tree: %s", sha1_to_hex(sha1));
991 t->delta_depth = 0;
992 buf = gfi_unpack_entry(myoe, &size);
993 } else {
994 char type[20];
995 buf = read_sha1_file(sha1, type, &size);
996 if (!buf || strcmp(type, tree_type))
997 die("Can't load tree %s", sha1_to_hex(sha1));
998 }
1000 c = buf;
1001 while (c != (buf + size)) {
1002 struct tree_entry *e = new_tree_entry();
1004 if (t->entry_count == t->entry_capacity)
1005 root->tree = t = grow_tree_content(t, 8);
1006 t->entries[t->entry_count++] = e;
1008 e->tree = NULL;
1009 c = get_mode(c, &e->versions[1].mode);
1010 if (!c)
1011 die("Corrupt mode in %s", sha1_to_hex(sha1));
1012 e->versions[0].mode = e->versions[1].mode;
1013 e->name = to_atom(c, strlen(c));
1014 c += e->name->str_len + 1;
1015 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1016 hashcpy(e->versions[1].sha1, (unsigned char*)c);
1017 c += 20;
1018 }
1019 free(buf);
1020 }
1022 static int tecmp0 (const void *_a, const void *_b)
1023 {
1024 struct tree_entry *a = *((struct tree_entry**)_a);
1025 struct tree_entry *b = *((struct tree_entry**)_b);
1026 return base_name_compare(
1027 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1028 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1029 }
1031 static int tecmp1 (const void *_a, const void *_b)
1032 {
1033 struct tree_entry *a = *((struct tree_entry**)_a);
1034 struct tree_entry *b = *((struct tree_entry**)_b);
1035 return base_name_compare(
1036 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1037 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1038 }
1040 static void mktree(struct tree_content *t,
1041 int v,
1042 unsigned long *szp,
1043 struct dbuf *b)
1044 {
1045 size_t maxlen = 0;
1046 unsigned int i;
1047 char *c;
1049 if (!v)
1050 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1051 else
1052 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1054 for (i = 0; i < t->entry_count; i++) {
1055 if (t->entries[i]->versions[v].mode)
1056 maxlen += t->entries[i]->name->str_len + 34;
1057 }
1059 size_dbuf(b, maxlen);
1060 c = b->buffer;
1061 for (i = 0; i < t->entry_count; i++) {
1062 struct tree_entry *e = t->entries[i];
1063 if (!e->versions[v].mode)
1064 continue;
1065 c += sprintf(c, "%o", e->versions[v].mode);
1066 *c++ = ' ';
1067 strcpy(c, e->name->str_dat);
1068 c += e->name->str_len + 1;
1069 hashcpy((unsigned char*)c, e->versions[v].sha1);
1070 c += 20;
1071 }
1072 *szp = c - (char*)b->buffer;
1073 }
1075 static void store_tree(struct tree_entry *root)
1076 {
1077 struct tree_content *t = root->tree;
1078 unsigned int i, j, del;
1079 unsigned long new_len;
1080 struct last_object lo;
1081 struct object_entry *le;
1083 if (!is_null_sha1(root->versions[1].sha1))
1084 return;
1086 for (i = 0; i < t->entry_count; i++) {
1087 if (t->entries[i]->tree)
1088 store_tree(t->entries[i]);
1089 }
1091 le = find_object(root->versions[0].sha1);
1092 if (!S_ISDIR(root->versions[0].mode)
1093 || !le
1094 || le->pack_id != pack_id) {
1095 lo.data = NULL;
1096 lo.depth = 0;
1097 } else {
1098 mktree(t, 0, &lo.len, &old_tree);
1099 lo.data = old_tree.buffer;
1100 lo.offset = le->offset;
1101 lo.depth = t->delta_depth;
1102 lo.no_free = 1;
1103 }
1105 mktree(t, 1, &new_len, &new_tree);
1106 store_object(OBJ_TREE, new_tree.buffer, new_len,
1107 &lo, root->versions[1].sha1, 0);
1109 t->delta_depth = lo.depth;
1110 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1111 struct tree_entry *e = t->entries[i];
1112 if (e->versions[1].mode) {
1113 e->versions[0].mode = e->versions[1].mode;
1114 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1115 t->entries[j++] = e;
1116 } else {
1117 release_tree_entry(e);
1118 del++;
1119 }
1120 }
1121 t->entry_count -= del;
1122 }
1124 static int tree_content_set(
1125 struct tree_entry *root,
1126 const char *p,
1127 const unsigned char *sha1,
1128 const unsigned int mode)
1129 {
1130 struct tree_content *t = root->tree;
1131 const char *slash1;
1132 unsigned int i, n;
1133 struct tree_entry *e;
1135 slash1 = strchr(p, '/');
1136 if (slash1)
1137 n = slash1 - p;
1138 else
1139 n = strlen(p);
1141 for (i = 0; i < t->entry_count; i++) {
1142 e = t->entries[i];
1143 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1144 if (!slash1) {
1145 if (e->versions[1].mode == mode
1146 && !hashcmp(e->versions[1].sha1, sha1))
1147 return 0;
1148 e->versions[1].mode = mode;
1149 hashcpy(e->versions[1].sha1, sha1);
1150 if (e->tree) {
1151 release_tree_content_recursive(e->tree);
1152 e->tree = NULL;
1153 }
1154 hashclr(root->versions[1].sha1);
1155 return 1;
1156 }
1157 if (!S_ISDIR(e->versions[1].mode)) {
1158 e->tree = new_tree_content(8);
1159 e->versions[1].mode = S_IFDIR;
1160 }
1161 if (!e->tree)
1162 load_tree(e);
1163 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1164 hashclr(root->versions[1].sha1);
1165 return 1;
1166 }
1167 return 0;
1168 }
1169 }
1171 if (t->entry_count == t->entry_capacity)
1172 root->tree = t = grow_tree_content(t, 8);
1173 e = new_tree_entry();
1174 e->name = to_atom(p, n);
1175 e->versions[0].mode = 0;
1176 hashclr(e->versions[0].sha1);
1177 t->entries[t->entry_count++] = e;
1178 if (slash1) {
1179 e->tree = new_tree_content(8);
1180 e->versions[1].mode = S_IFDIR;
1181 tree_content_set(e, slash1 + 1, sha1, mode);
1182 } else {
1183 e->tree = NULL;
1184 e->versions[1].mode = mode;
1185 hashcpy(e->versions[1].sha1, sha1);
1186 }
1187 hashclr(root->versions[1].sha1);
1188 return 1;
1189 }
1191 static int tree_content_remove(struct tree_entry *root, const char *p)
1192 {
1193 struct tree_content *t = root->tree;
1194 const char *slash1;
1195 unsigned int i, n;
1196 struct tree_entry *e;
1198 slash1 = strchr(p, '/');
1199 if (slash1)
1200 n = slash1 - p;
1201 else
1202 n = strlen(p);
1204 for (i = 0; i < t->entry_count; i++) {
1205 e = t->entries[i];
1206 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1207 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1208 goto del_entry;
1209 if (!e->tree)
1210 load_tree(e);
1211 if (tree_content_remove(e, slash1 + 1)) {
1212 for (n = 0; n < e->tree->entry_count; n++) {
1213 if (e->tree->entries[n]->versions[1].mode) {
1214 hashclr(root->versions[1].sha1);
1215 return 1;
1216 }
1217 }
1218 goto del_entry;
1219 }
1220 return 0;
1221 }
1222 }
1223 return 0;
1225 del_entry:
1226 if (e->tree) {
1227 release_tree_content_recursive(e->tree);
1228 e->tree = NULL;
1229 }
1230 e->versions[1].mode = 0;
1231 hashclr(e->versions[1].sha1);
1232 hashclr(root->versions[1].sha1);
1233 return 1;
1234 }
1236 static void dump_branches()
1237 {
1238 static const char *msg = "fast-import";
1239 unsigned int i;
1240 struct branch *b;
1241 struct ref_lock *lock;
1243 for (i = 0; i < branch_table_sz; i++) {
1244 for (b = branch_table[i]; b; b = b->table_next_branch) {
1245 lock = lock_any_ref_for_update(b->name, NULL);
1246 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1247 die("Can't write %s", b->name);
1248 }
1249 }
1250 }
1252 static void dump_tags()
1253 {
1254 static const char *msg = "fast-import";
1255 struct tag *t;
1256 struct ref_lock *lock;
1257 char path[PATH_MAX];
1259 for (t = first_tag; t; t = t->next_tag) {
1260 sprintf(path, "refs/tags/%s", t->name);
1261 lock = lock_any_ref_for_update(path, NULL);
1262 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1263 die("Can't write %s", path);
1264 }
1265 }
1267 static void dump_marks_helper(FILE *f,
1268 uintmax_t base,
1269 struct mark_set *m)
1270 {
1271 uintmax_t k;
1272 if (m->shift) {
1273 for (k = 0; k < 1024; k++) {
1274 if (m->data.sets[k])
1275 dump_marks_helper(f, (base + k) << m->shift,
1276 m->data.sets[k]);
1277 }
1278 } else {
1279 for (k = 0; k < 1024; k++) {
1280 if (m->data.marked[k])
1281 fprintf(f, ":%ju %s\n", base + k,
1282 sha1_to_hex(m->data.marked[k]->sha1));
1283 }
1284 }
1285 }
1287 static void dump_marks()
1288 {
1289 if (mark_file)
1290 {
1291 FILE *f = fopen(mark_file, "w");
1292 dump_marks_helper(f, 0, marks);
1293 fclose(f);
1294 }
1295 }
1297 static void read_next_command()
1298 {
1299 read_line(&command_buf, stdin, '\n');
1300 }
1302 static void cmd_mark()
1303 {
1304 if (!strncmp("mark :", command_buf.buf, 6)) {
1305 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1306 read_next_command();
1307 }
1308 else
1309 next_mark = 0;
1310 }
1312 static void* cmd_data (size_t *size)
1313 {
1314 size_t n = 0;
1315 void *buffer;
1316 size_t length;
1318 if (strncmp("data ", command_buf.buf, 5))
1319 die("Expected 'data n' command, found: %s", command_buf.buf);
1321 length = strtoul(command_buf.buf + 5, NULL, 10);
1322 buffer = xmalloc(length);
1324 while (n < length) {
1325 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1326 if (!s && feof(stdin))
1327 die("EOF in data (%lu bytes remaining)", length - n);
1328 n += s;
1329 }
1331 if (fgetc(stdin) != '\n')
1332 die("An lf did not trail the binary data as expected.");
1334 *size = length;
1335 return buffer;
1336 }
1338 static void cmd_new_blob()
1339 {
1340 size_t l;
1341 void *d;
1343 read_next_command();
1344 cmd_mark();
1345 d = cmd_data(&l);
1347 if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1348 free(d);
1349 }
1351 static void unload_one_branch()
1352 {
1353 while (cur_active_branches
1354 && cur_active_branches >= max_active_branches) {
1355 unsigned long min_commit = ULONG_MAX;
1356 struct branch *e, *l = NULL, *p = NULL;
1358 for (e = active_branches; e; e = e->active_next_branch) {
1359 if (e->last_commit < min_commit) {
1360 p = l;
1361 min_commit = e->last_commit;
1362 }
1363 l = e;
1364 }
1366 if (p) {
1367 e = p->active_next_branch;
1368 p->active_next_branch = e->active_next_branch;
1369 } else {
1370 e = active_branches;
1371 active_branches = e->active_next_branch;
1372 }
1373 e->active_next_branch = NULL;
1374 if (e->branch_tree.tree) {
1375 release_tree_content_recursive(e->branch_tree.tree);
1376 e->branch_tree.tree = NULL;
1377 }
1378 cur_active_branches--;
1379 }
1380 }
1382 static void load_branch(struct branch *b)
1383 {
1384 load_tree(&b->branch_tree);
1385 b->active_next_branch = active_branches;
1386 active_branches = b;
1387 cur_active_branches++;
1388 branch_load_count++;
1389 }
1391 static void file_change_m(struct branch *b)
1392 {
1393 const char *p = command_buf.buf + 2;
1394 char *p_uq;
1395 const char *endp;
1396 struct object_entry *oe;
1397 unsigned char sha1[20];
1398 unsigned int mode;
1399 char type[20];
1401 p = get_mode(p, &mode);
1402 if (!p)
1403 die("Corrupt mode: %s", command_buf.buf);
1404 switch (mode) {
1405 case S_IFREG | 0644:
1406 case S_IFREG | 0755:
1407 case S_IFLNK:
1408 case 0644:
1409 case 0755:
1410 /* ok */
1411 break;
1412 default:
1413 die("Corrupt mode: %s", command_buf.buf);
1414 }
1416 if (*p == ':') {
1417 char *x;
1418 oe = find_mark(strtoumax(p + 1, &x, 10));
1419 hashcpy(sha1, oe->sha1);
1420 p = x;
1421 } else {
1422 if (get_sha1_hex(p, sha1))
1423 die("Invalid SHA1: %s", command_buf.buf);
1424 oe = find_object(sha1);
1425 p += 40;
1426 }
1427 if (*p++ != ' ')
1428 die("Missing space after SHA1: %s", command_buf.buf);
1430 p_uq = unquote_c_style(p, &endp);
1431 if (p_uq) {
1432 if (*endp)
1433 die("Garbage after path in: %s", command_buf.buf);
1434 p = p_uq;
1435 }
1437 if (oe) {
1438 if (oe->type != OBJ_BLOB)
1439 die("Not a blob (actually a %s): %s",
1440 command_buf.buf, type_names[oe->type]);
1441 } else {
1442 if (sha1_object_info(sha1, type, NULL))
1443 die("Blob not found: %s", command_buf.buf);
1444 if (strcmp(blob_type, type))
1445 die("Not a blob (actually a %s): %s",
1446 command_buf.buf, type);
1447 }
1449 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1451 if (p_uq)
1452 free(p_uq);
1453 }
1455 static void file_change_d(struct branch *b)
1456 {
1457 const char *p = command_buf.buf + 2;
1458 char *p_uq;
1459 const char *endp;
1461 p_uq = unquote_c_style(p, &endp);
1462 if (p_uq) {
1463 if (*endp)
1464 die("Garbage after path in: %s", command_buf.buf);
1465 p = p_uq;
1466 }
1467 tree_content_remove(&b->branch_tree, p);
1468 if (p_uq)
1469 free(p_uq);
1470 }
1472 static void cmd_from(struct branch *b)
1473 {
1474 const char *from, *endp;
1475 char *str_uq;
1476 struct branch *s;
1478 if (strncmp("from ", command_buf.buf, 5))
1479 return;
1481 if (b->last_commit)
1482 die("Can't reinitailize branch %s", b->name);
1484 from = strchr(command_buf.buf, ' ') + 1;
1485 str_uq = unquote_c_style(from, &endp);
1486 if (str_uq) {
1487 if (*endp)
1488 die("Garbage after string in: %s", command_buf.buf);
1489 from = str_uq;
1490 }
1492 s = lookup_branch(from);
1493 if (b == s)
1494 die("Can't create a branch from itself: %s", b->name);
1495 else if (s) {
1496 unsigned char *t = s->branch_tree.versions[1].sha1;
1497 hashcpy(b->sha1, s->sha1);
1498 hashcpy(b->branch_tree.versions[0].sha1, t);
1499 hashcpy(b->branch_tree.versions[1].sha1, t);
1500 } else if (*from == ':') {
1501 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1502 struct object_entry *oe = find_mark(idnum);
1503 unsigned long size;
1504 char *buf;
1505 if (oe->type != OBJ_COMMIT)
1506 die("Mark :%ju not a commit", idnum);
1507 hashcpy(b->sha1, oe->sha1);
1508 buf = gfi_unpack_entry(oe, &size);
1509 if (!buf || size < 46)
1510 die("Not a valid commit: %s", from);
1511 if (memcmp("tree ", buf, 5)
1512 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1513 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1514 free(buf);
1515 hashcpy(b->branch_tree.versions[0].sha1,
1516 b->branch_tree.versions[1].sha1);
1517 } else if (!get_sha1(from, b->sha1)) {
1518 if (is_null_sha1(b->sha1)) {
1519 hashclr(b->branch_tree.versions[0].sha1);
1520 hashclr(b->branch_tree.versions[1].sha1);
1521 } else {
1522 unsigned long size;
1523 char *buf;
1525 buf = read_object_with_reference(b->sha1,
1526 type_names[OBJ_COMMIT], &size, b->sha1);
1527 if (!buf || size < 46)
1528 die("Not a valid commit: %s", from);
1529 if (memcmp("tree ", buf, 5)
1530 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1531 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1532 free(buf);
1533 hashcpy(b->branch_tree.versions[0].sha1,
1534 b->branch_tree.versions[1].sha1);
1535 }
1536 } else
1537 die("Invalid ref name or SHA1 expression: %s", from);
1539 read_next_command();
1540 }
1542 static struct hash_list* cmd_merge(unsigned int *count)
1543 {
1544 struct hash_list *list = NULL, *n, *e;
1545 const char *from, *endp;
1546 char *str_uq;
1547 struct branch *s;
1549 *count = 0;
1550 while (!strncmp("merge ", command_buf.buf, 6)) {
1551 from = strchr(command_buf.buf, ' ') + 1;
1552 str_uq = unquote_c_style(from, &endp);
1553 if (str_uq) {
1554 if (*endp)
1555 die("Garbage after string in: %s", command_buf.buf);
1556 from = str_uq;
1557 }
1559 n = xmalloc(sizeof(*n));
1560 s = lookup_branch(from);
1561 if (s)
1562 hashcpy(n->sha1, s->sha1);
1563 else if (*from == ':') {
1564 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1565 struct object_entry *oe = find_mark(idnum);
1566 if (oe->type != OBJ_COMMIT)
1567 die("Mark :%ju not a commit", idnum);
1568 hashcpy(n->sha1, oe->sha1);
1569 } else if (get_sha1(from, n->sha1))
1570 die("Invalid ref name or SHA1 expression: %s", from);
1572 n->next = NULL;
1573 if (list)
1574 e->next = n;
1575 else
1576 list = n;
1577 e = n;
1578 *count++;
1579 read_next_command();
1580 }
1581 return list;
1582 }
1584 static void cmd_new_commit()
1585 {
1586 struct branch *b;
1587 void *msg;
1588 size_t msglen;
1589 char *str_uq;
1590 const char *endp;
1591 char *sp;
1592 char *author = NULL;
1593 char *committer = NULL;
1594 struct hash_list *merge_list = NULL;
1595 unsigned int merge_count;
1597 /* Obtain the branch name from the rest of our command */
1598 sp = strchr(command_buf.buf, ' ') + 1;
1599 str_uq = unquote_c_style(sp, &endp);
1600 if (str_uq) {
1601 if (*endp)
1602 die("Garbage after ref in: %s", command_buf.buf);
1603 sp = str_uq;
1604 }
1605 b = lookup_branch(sp);
1606 if (!b)
1607 b = new_branch(sp);
1608 if (str_uq)
1609 free(str_uq);
1611 read_next_command();
1612 cmd_mark();
1613 if (!strncmp("author ", command_buf.buf, 7)) {
1614 author = strdup(command_buf.buf);
1615 read_next_command();
1616 }
1617 if (!strncmp("committer ", command_buf.buf, 10)) {
1618 committer = strdup(command_buf.buf);
1619 read_next_command();
1620 }
1621 if (!committer)
1622 die("Expected committer but didn't get one");
1623 msg = cmd_data(&msglen);
1624 read_next_command();
1625 cmd_from(b);
1626 merge_list = cmd_merge(&merge_count);
1628 /* ensure the branch is active/loaded */
1629 if (!b->branch_tree.tree || !max_active_branches) {
1630 unload_one_branch();
1631 load_branch(b);
1632 }
1634 /* file_change* */
1635 for (;;) {
1636 if (1 == command_buf.len)
1637 break;
1638 else if (!strncmp("M ", command_buf.buf, 2))
1639 file_change_m(b);
1640 else if (!strncmp("D ", command_buf.buf, 2))
1641 file_change_d(b);
1642 else
1643 die("Unsupported file_change: %s", command_buf.buf);
1644 read_next_command();
1645 }
1647 /* build the tree and the commit */
1648 store_tree(&b->branch_tree);
1649 hashcpy(b->branch_tree.versions[0].sha1,
1650 b->branch_tree.versions[1].sha1);
1651 size_dbuf(&new_data, 97 + msglen
1652 + merge_count * 49
1653 + (author
1654 ? strlen(author) + strlen(committer)
1655 : 2 * strlen(committer)));
1656 sp = new_data.buffer;
1657 sp += sprintf(sp, "tree %s\n",
1658 sha1_to_hex(b->branch_tree.versions[1].sha1));
1659 if (!is_null_sha1(b->sha1))
1660 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1661 while (merge_list) {
1662 struct hash_list *next = merge_list->next;
1663 sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1664 free(merge_list);
1665 merge_list = next;
1666 }
1667 if (author)
1668 sp += sprintf(sp, "%s\n", author);
1669 else
1670 sp += sprintf(sp, "author %s\n", committer + 10);
1671 sp += sprintf(sp, "%s\n\n", committer);
1672 memcpy(sp, msg, msglen);
1673 sp += msglen;
1674 if (author)
1675 free(author);
1676 free(committer);
1677 free(msg);
1679 store_object(OBJ_COMMIT,
1680 new_data.buffer, sp - (char*)new_data.buffer,
1681 NULL, b->sha1, next_mark);
1682 b->last_commit = object_count_by_type[OBJ_COMMIT];
1684 if (branch_log) {
1685 int need_dq = quote_c_style(b->name, NULL, NULL, 0);
1686 fprintf(branch_log, "commit ");
1687 if (need_dq) {
1688 fputc('"', branch_log);
1689 quote_c_style(b->name, NULL, branch_log, 0);
1690 fputc('"', branch_log);
1691 } else
1692 fprintf(branch_log, "%s", b->name);
1693 fprintf(branch_log," :%ju %s\n",next_mark,sha1_to_hex(b->sha1));
1694 }
1695 }
1697 static void cmd_new_tag()
1698 {
1699 char *str_uq;
1700 const char *endp;
1701 char *sp;
1702 const char *from;
1703 char *tagger;
1704 struct branch *s;
1705 void *msg;
1706 size_t msglen;
1707 struct tag *t;
1708 uintmax_t from_mark = 0;
1709 unsigned char sha1[20];
1711 /* Obtain the new tag name from the rest of our command */
1712 sp = strchr(command_buf.buf, ' ') + 1;
1713 str_uq = unquote_c_style(sp, &endp);
1714 if (str_uq) {
1715 if (*endp)
1716 die("Garbage after tag name in: %s", command_buf.buf);
1717 sp = str_uq;
1718 }
1719 t = pool_alloc(sizeof(struct tag));
1720 t->next_tag = NULL;
1721 t->name = pool_strdup(sp);
1722 if (last_tag)
1723 last_tag->next_tag = t;
1724 else
1725 first_tag = t;
1726 last_tag = t;
1727 if (str_uq)
1728 free(str_uq);
1729 read_next_command();
1731 /* from ... */
1732 if (strncmp("from ", command_buf.buf, 5))
1733 die("Expected from command, got %s", command_buf.buf);
1735 from = strchr(command_buf.buf, ' ') + 1;
1736 str_uq = unquote_c_style(from, &endp);
1737 if (str_uq) {
1738 if (*endp)
1739 die("Garbage after string in: %s", command_buf.buf);
1740 from = str_uq;
1741 }
1743 s = lookup_branch(from);
1744 if (s) {
1745 hashcpy(sha1, s->sha1);
1746 } else if (*from == ':') {
1747 from_mark = strtoumax(from + 1, NULL, 10);
1748 struct object_entry *oe = find_mark(from_mark);
1749 if (oe->type != OBJ_COMMIT)
1750 die("Mark :%ju not a commit", from_mark);
1751 hashcpy(sha1, oe->sha1);
1752 } else if (!get_sha1(from, sha1)) {
1753 unsigned long size;
1754 char *buf;
1756 buf = read_object_with_reference(sha1,
1757 type_names[OBJ_COMMIT], &size, sha1);
1758 if (!buf || size < 46)
1759 die("Not a valid commit: %s", from);
1760 free(buf);
1761 } else
1762 die("Invalid ref name or SHA1 expression: %s", from);
1764 if (str_uq)
1765 free(str_uq);
1766 read_next_command();
1768 /* tagger ... */
1769 if (strncmp("tagger ", command_buf.buf, 7))
1770 die("Expected tagger command, got %s", command_buf.buf);
1771 tagger = strdup(command_buf.buf);
1773 /* tag payload/message */
1774 read_next_command();
1775 msg = cmd_data(&msglen);
1777 /* build the tag object */
1778 size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1779 sp = new_data.buffer;
1780 sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1781 sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1782 sp += sprintf(sp, "tag %s\n", t->name);
1783 sp += sprintf(sp, "%s\n\n", tagger);
1784 memcpy(sp, msg, msglen);
1785 sp += msglen;
1786 free(tagger);
1787 free(msg);
1789 store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
1790 NULL, t->sha1, 0);
1792 if (branch_log) {
1793 int need_dq = quote_c_style(t->name, NULL, NULL, 0);
1794 fprintf(branch_log, "tag ");
1795 if (need_dq) {
1796 fputc('"', branch_log);
1797 quote_c_style(t->name, NULL, branch_log, 0);
1798 fputc('"', branch_log);
1799 } else
1800 fprintf(branch_log, "%s", t->name);
1801 fprintf(branch_log," :%ju %s\n",from_mark,sha1_to_hex(t->sha1));
1802 }
1803 }
1805 static void cmd_reset_branch()
1806 {
1807 struct branch *b;
1808 char *str_uq;
1809 const char *endp;
1810 char *sp;
1812 /* Obtain the branch name from the rest of our command */
1813 sp = strchr(command_buf.buf, ' ') + 1;
1814 str_uq = unquote_c_style(sp, &endp);
1815 if (str_uq) {
1816 if (*endp)
1817 die("Garbage after ref in: %s", command_buf.buf);
1818 sp = str_uq;
1819 }
1820 b = lookup_branch(sp);
1821 if (b) {
1822 b->last_commit = 0;
1823 if (b->branch_tree.tree) {
1824 release_tree_content_recursive(b->branch_tree.tree);
1825 b->branch_tree.tree = NULL;
1826 }
1827 }
1828 else
1829 b = new_branch(sp);
1830 if (str_uq)
1831 free(str_uq);
1832 read_next_command();
1833 cmd_from(b);
1834 }
1836 static void cmd_checkpoint()
1837 {
1838 if (object_count)
1839 checkpoint();
1840 read_next_command();
1841 }
1843 static const char fast_import_usage[] =
1844 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1846 int main(int argc, const char **argv)
1847 {
1848 int i;
1849 uintmax_t est_obj_cnt = object_entry_alloc;
1850 uintmax_t duplicate_count;
1852 setup_ident();
1853 git_config(git_default_config);
1855 for (i = 1; i < argc; i++) {
1856 const char *a = argv[i];
1858 if (*a != '-' || !strcmp(a, "--"))
1859 break;
1860 else if (!strncmp(a, "--objects=", 10))
1861 est_obj_cnt = strtoumax(a + 10, NULL, 0);
1862 else if (!strncmp(a, "--max-objects-per-pack=", 23))
1863 max_objects = strtoumax(a + 23, NULL, 0);
1864 else if (!strncmp(a, "--max-pack-size=", 16))
1865 max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
1866 else if (!strncmp(a, "--depth=", 8))
1867 max_depth = strtoul(a + 8, NULL, 0);
1868 else if (!strncmp(a, "--active-branches=", 18))
1869 max_active_branches = strtoul(a + 18, NULL, 0);
1870 else if (!strncmp(a, "--export-marks=", 15))
1871 mark_file = a + 15;
1872 else if (!strncmp(a, "--branch-log=", 13)) {
1873 branch_log = fopen(a + 13, "w");
1874 if (!branch_log)
1875 die("Can't create %s: %s", a + 13, strerror(errno));
1876 }
1877 else
1878 die("unknown option %s", a);
1879 }
1880 if (i != argc)
1881 usage(fast_import_usage);
1883 alloc_objects(est_obj_cnt);
1884 strbuf_init(&command_buf);
1886 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1887 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1888 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1889 marks = pool_calloc(1, sizeof(struct mark_set));
1891 start_packfile();
1892 for (;;) {
1893 read_next_command();
1894 if (command_buf.eof)
1895 break;
1896 else if (!strcmp("blob", command_buf.buf))
1897 cmd_new_blob();
1898 else if (!strncmp("commit ", command_buf.buf, 7))
1899 cmd_new_commit();
1900 else if (!strncmp("tag ", command_buf.buf, 4))
1901 cmd_new_tag();
1902 else if (!strncmp("reset ", command_buf.buf, 6))
1903 cmd_reset_branch();
1904 else if (!strcmp("checkpoint", command_buf.buf))
1905 cmd_checkpoint();
1906 else
1907 die("Unsupported command: %s", command_buf.buf);
1908 }
1909 end_packfile();
1911 dump_branches();
1912 dump_tags();
1913 unkeep_all_packs();
1914 dump_marks();
1915 if (branch_log)
1916 fclose(branch_log);
1918 duplicate_count = 0;
1919 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
1920 duplicate_count += duplicate_count_by_type[i];
1922 fprintf(stderr, "%s statistics:\n", argv[0]);
1923 fprintf(stderr, "---------------------------------------------------------------------\n");
1924 fprintf(stderr, "Alloc'd objects: %10ju (%10ju overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1925 fprintf(stderr, "Total objects: %10ju (%10ju duplicates )\n", object_count, duplicate_count);
1926 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]);
1927 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]);
1928 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]);
1929 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]);
1930 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
1931 fprintf(stderr, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
1932 fprintf(stderr, " atoms: %10u\n", atom_cnt);
1933 fprintf(stderr, "Memory total: %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1934 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1935 fprintf(stderr, " objects: %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1936 fprintf(stderr, "---------------------------------------------------------------------\n");
1937 pack_report();
1938 fprintf(stderr, "---------------------------------------------------------------------\n");
1939 fprintf(stderr, "\n");
1941 return 0;
1942 }