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