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