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