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