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