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