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