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