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