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