Allow symlink blobs in trees during fast-import.
[git/git.git] / fast-import.c
CommitLineData
463acbe1
SP
1/*
2Format of STDIN stream:
3
4 stream ::= cmd*;
5
6 cmd ::= new_blob
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
ac47a738
SP
124struct last_object
125{
126 void *data;
6bb5b329
SP
127 unsigned int len;
128 unsigned int depth;
ac47a738
SP
129 unsigned char sha1[20];
130};
131
463acbe1
SP
132struct mem_pool
133{
134 struct mem_pool *next_pool;
135 char *next_free;
136 char *end;
137 char space[FLEX_ARRAY]; /* more */
138};
139
140struct atom_str
141{
142 struct atom_str *next_atom;
143 int str_len;
144 char str_dat[FLEX_ARRAY]; /* more */
145};
146
147struct tree_content;
6bb5b329
SP
148struct tree_entry
149{
463acbe1
SP
150 struct tree_content *tree;
151 struct atom_str* name;
152 unsigned int mode;
6bb5b329 153 unsigned char sha1[20];
6bb5b329
SP
154};
155
463acbe1 156struct tree_content
6bb5b329 157{
463acbe1
SP
158 unsigned int entry_capacity; /* must match avail_tree_content */
159 unsigned int entry_count;
160 struct tree_entry *entries[FLEX_ARRAY]; /* more */
161};
162
163struct avail_tree_content
164{
165 unsigned int entry_capacity; /* must match tree_content */
166 struct avail_tree_content *next_avail;
6bb5b329
SP
167};
168
169struct branch
170{
463acbe1
SP
171 struct branch *table_next_branch;
172 struct branch *active_next_branch;
6bb5b329 173 const char *name;
463acbe1
SP
174 unsigned long last_commit;
175 struct tree_entry branch_tree;
176 unsigned char sha1[20];
6bb5b329
SP
177};
178
463acbe1
SP
179
180/* Stats and misc. counters */
db5e523f 181static int max_depth = 10;
27d6d290 182static unsigned long alloc_count;
6bb5b329 183static unsigned long branch_count;
db5e523f 184static unsigned long object_count;
8bcce301 185static unsigned long duplicate_count;
6143f064
SP
186static unsigned long object_count_by_type[9];
187static unsigned long duplicate_count_by_type[9];
ac47a738 188
463acbe1
SP
189/* Memory pools */
190static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
191static size_t total_allocd;
192static struct mem_pool *mem_pool;
193
c44cdc7e 194/* Atom management */
463acbe1
SP
195static unsigned int atom_table_sz = 4451;
196static unsigned int atom_cnt;
197static struct atom_str **atom_table;
198
199/* The .pack file being generated */
ac47a738
SP
200static int pack_fd;
201static unsigned long pack_offset;
202static unsigned char pack_sha1[20];
203
204/* Table of objects we've written. */
463acbe1
SP
205static unsigned int object_entry_alloc = 1000;
206static struct object_entry_pool *blocks;
207static struct object_entry *object_table[1 << 16];
ac47a738
SP
208
209/* Our last blob */
463acbe1
SP
210static struct last_object last_blob;
211
212/* Tree management */
213static unsigned int tree_entry_alloc = 1000;
214static void *avail_tree_entry;
215static unsigned int avail_tree_table_sz = 100;
216static struct avail_tree_content **avail_tree_table;
8bcce301 217
6bb5b329 218/* Branch data */
463acbe1
SP
219static unsigned int max_active_branches = 5;
220static unsigned int cur_active_branches;
221static unsigned int branch_table_sz = 1039;
222static struct branch **branch_table;
223static struct branch *active_branches;
224
c44cdc7e
SP
225/* Input stream parsing */
226static struct strbuf command_buf;
227static unsigned long command_mark;
228
6bb5b329 229
27d6d290 230static void alloc_objects(int cnt)
8bcce301 231{
463acbe1 232 struct object_entry_pool *b;
27d6d290 233
463acbe1 234 b = xmalloc(sizeof(struct object_entry_pool)
27d6d290 235 + cnt * sizeof(struct object_entry));
463acbe1 236 b->next_pool = blocks;
27d6d290
SP
237 b->next_free = b->entries;
238 b->end = b->entries + cnt;
239 blocks = b;
240 alloc_count += cnt;
241}
8bcce301 242
27d6d290 243static struct object_entry* new_object(unsigned char *sha1)
8bcce301 244{
27d6d290 245 struct object_entry *e;
8bcce301 246
27d6d290 247 if (blocks->next_free == blocks->end)
463acbe1 248 alloc_objects(object_entry_alloc);
8bcce301 249
27d6d290
SP
250 e = blocks->next_free++;
251 memcpy(e->sha1, sha1, sizeof(e->sha1));
252 return e;
8bcce301
SP
253}
254
463acbe1
SP
255static struct object_entry* find_object(unsigned char *sha1)
256{
257 unsigned int h = sha1[0] << 8 | sha1[1];
258 struct object_entry *e;
259 for (e = object_table[h]; e; e = e->next)
260 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
261 return e;
262 return NULL;
263}
264
8bcce301
SP
265static struct object_entry* insert_object(unsigned char *sha1)
266{
267 unsigned int h = sha1[0] << 8 | sha1[1];
27d6d290 268 struct object_entry *e = object_table[h];
463acbe1 269 struct object_entry *p = NULL;
8bcce301
SP
270
271 while (e) {
272 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
273 return e;
274 p = e;
275 e = e->next;
276 }
277
278 e = new_object(sha1);
463acbe1 279 e->next = NULL;
8bcce301
SP
280 e->offset = 0;
281 if (p)
282 p->next = e;
283 else
27d6d290 284 object_table[h] = e;
8bcce301
SP
285 return e;
286}
db5e523f 287
463acbe1
SP
288static unsigned int hc_str(const char *s, size_t len)
289{
290 unsigned int r = 0;
291 while (len-- > 0)
292 r = r * 31 + *s++;
293 return r;
294}
295
296static void* pool_alloc(size_t len)
297{
298 struct mem_pool *p;
299 void *r;
300
301 for (p = mem_pool; p; p = p->next_pool)
302 if ((p->end - p->next_free >= len))
303 break;
304
305 if (!p) {
306 if (len >= (mem_pool_alloc/2)) {
307 total_allocd += len;
308 return xmalloc(len);
309 }
310 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
311 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
312 p->next_pool = mem_pool;
313 p->next_free = p->space;
314 p->end = p->next_free + mem_pool_alloc;
315 mem_pool = p;
316 }
317
318 r = p->next_free;
319 p->next_free += len;
320 return r;
321}
322
323static void* pool_calloc(size_t count, size_t size)
324{
325 size_t len = count * size;
326 void *r = pool_alloc(len);
327 memset(r, 0, len);
328 return r;
329}
330
331static char* pool_strdup(const char *s)
332{
333 char *r = pool_alloc(strlen(s) + 1);
334 strcpy(r, s);
335 return r;
336}
337
338static struct atom_str* to_atom(const char *s, size_t len)
339{
340 unsigned int hc = hc_str(s, len) % atom_table_sz;
341 struct atom_str *c;
342
343 for (c = atom_table[hc]; c; c = c->next_atom)
344 if (c->str_len == len && !strncmp(s, c->str_dat, len))
345 return c;
346
347 c = pool_alloc(sizeof(struct atom_str) + len + 1);
348 c->str_len = len;
349 strncpy(c->str_dat, s, len);
350 c->str_dat[len] = 0;
351 c->next_atom = atom_table[hc];
352 atom_table[hc] = c;
353 atom_cnt++;
354 return c;
355}
356
357static struct branch* lookup_branch(const char *name)
358{
359 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
360 struct branch *b;
361
362 for (b = branch_table[hc]; b; b = b->table_next_branch)
363 if (!strcmp(name, b->name))
364 return b;
365 return NULL;
366}
367
368static struct branch* new_branch(const char *name)
369{
370 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
371 struct branch* b = lookup_branch(name);
372
373 if (b)
374 die("Invalid attempt to create duplicate branch: %s", name);
c44cdc7e
SP
375 if (check_ref_format(name))
376 die("Branch name doesn't conform to GIT standards: %s", name);
463acbe1
SP
377
378 b = pool_calloc(1, sizeof(struct branch));
379 b->name = pool_strdup(name);
380 b->table_next_branch = branch_table[hc];
381 branch_table[hc] = b;
382 branch_count++;
383 return b;
384}
385
386static unsigned int hc_entries(unsigned int cnt)
387{
388 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
389 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
390}
391
392static struct tree_content* new_tree_content(unsigned int cnt)
393{
394 struct avail_tree_content *f, *l = NULL;
395 struct tree_content *t;
396 unsigned int hc = hc_entries(cnt);
397
398 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
399 if (f->entry_capacity >= cnt)
400 break;
401
402 if (f) {
403 if (l)
404 l->next_avail = f->next_avail;
405 else
406 avail_tree_table[hc] = f->next_avail;
407 } else {
408 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
409 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
410 f->entry_capacity = cnt;
411 }
412
413 t = (struct tree_content*)f;
414 t->entry_count = 0;
415 return t;
416}
417
418static void release_tree_entry(struct tree_entry *e);
419static void release_tree_content(struct tree_content *t)
420{
421 struct avail_tree_content *f = (struct avail_tree_content*)t;
422 unsigned int hc = hc_entries(f->entry_capacity);
423 unsigned int i;
424 for (i = 0; i < t->entry_count; i++)
425 release_tree_entry(t->entries[i]);
426 f->next_avail = avail_tree_table[hc];
427 avail_tree_table[hc] = f;
428}
429
430static struct tree_content* grow_tree_content(
431 struct tree_content *t,
432 int amt)
433{
434 struct tree_content *r = new_tree_content(t->entry_count + amt);
435 r->entry_count = t->entry_count;
436 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
437 release_tree_content(t);
438 return r;
439}
440
441static struct tree_entry* new_tree_entry()
442{
443 struct tree_entry *e;
444
445 if (!avail_tree_entry) {
446 unsigned int n = tree_entry_alloc;
447 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
448 while (n--) {
449 *((void**)e) = e + 1;
450 e++;
451 }
452 }
453
454 e = avail_tree_entry;
455 avail_tree_entry = *((void**)e);
456 return e;
457}
458
459static void release_tree_entry(struct tree_entry *e)
460{
461 if (e->tree)
462 release_tree_content(e->tree);
463 *((void**)e) = avail_tree_entry;
464 avail_tree_entry = e;
465}
466
467static void yread(int fd, void *buffer, size_t length)
db5e523f
SP
468{
469 ssize_t ret = 0;
470 while (ret < length) {
471 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
463acbe1
SP
472 if (!size)
473 die("Read from descriptor %i: end of stream", fd);
474 if (size < 0)
475 die("Read from descriptor %i: %s", fd, strerror(errno));
476 ret += size;
477 }
478}
479
463acbe1 480static void ywrite(int fd, void *buffer, size_t length)
db5e523f
SP
481{
482 ssize_t ret = 0;
483 while (ret < length) {
484 ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret);
463acbe1
SP
485 if (!size)
486 die("Write to descriptor %i: end of file", fd);
487 if (size < 0)
488 die("Write to descriptor %i: %s", fd, strerror(errno));
db5e523f
SP
489 ret += size;
490 }
db5e523f
SP
491}
492
c44cdc7e 493static size_t encode_header(
ac47a738 494 enum object_type type,
c44cdc7e 495 size_t size,
ac47a738 496 unsigned char *hdr)
db5e523f
SP
497{
498 int n = 1;
499 unsigned char c;
500
501 if (type < OBJ_COMMIT || type > OBJ_DELTA)
502 die("bad type %d", type);
503
504 c = (type << 4) | (size & 15);
505 size >>= 4;
506 while (size) {
507 *hdr++ = c | 0x80;
508 c = size & 0x7f;
509 size >>= 7;
510 n++;
511 }
512 *hdr = c;
513 return n;
514}
515
ac47a738
SP
516static int store_object(
517 enum object_type type,
518 void *dat,
c44cdc7e 519 size_t datlen,
6bb5b329
SP
520 struct last_object *last,
521 unsigned char *sha1out)
db5e523f 522{
db5e523f 523 void *out, *delta;
ac47a738
SP
524 struct object_entry *e;
525 unsigned char hdr[96];
526 unsigned char sha1[20];
db5e523f 527 unsigned long hdrlen, deltalen;
ac47a738
SP
528 SHA_CTX c;
529 z_stream s;
530
531 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
532 SHA1_Init(&c);
533 SHA1_Update(&c, hdr, hdrlen);
534 SHA1_Update(&c, dat, datlen);
535 SHA1_Final(sha1, &c);
6bb5b329
SP
536 if (sha1out)
537 memcpy(sha1out, sha1, sizeof(sha1));
ac47a738
SP
538
539 e = insert_object(sha1);
540 if (e->offset) {
541 duplicate_count++;
6143f064 542 duplicate_count_by_type[type]++;
463acbe1 543 return 1;
ac47a738 544 }
7111feed 545 e->type = type;
ac47a738
SP
546 e->offset = pack_offset;
547 object_count++;
6143f064 548 object_count_by_type[type]++;
db5e523f 549
463acbe1 550 if (last && last->data && last->depth < max_depth)
ac47a738 551 delta = diff_delta(last->data, last->len,
db5e523f
SP
552 dat, datlen,
553 &deltalen, 0);
ac47a738 554 else
db5e523f
SP
555 delta = 0;
556
557 memset(&s, 0, sizeof(s));
558 deflateInit(&s, zlib_compression_level);
559
560 if (delta) {
ac47a738 561 last->depth++;
db5e523f
SP
562 s.next_in = delta;
563 s.avail_in = deltalen;
564 hdrlen = encode_header(OBJ_DELTA, deltalen, hdr);
463acbe1
SP
565 ywrite(pack_fd, hdr, hdrlen);
566 ywrite(pack_fd, last->sha1, sizeof(sha1));
ac47a738 567 pack_offset += hdrlen + sizeof(sha1);
db5e523f 568 } else {
463acbe1
SP
569 if (last)
570 last->depth = 0;
db5e523f
SP
571 s.next_in = dat;
572 s.avail_in = datlen;
ac47a738 573 hdrlen = encode_header(type, datlen, hdr);
463acbe1 574 ywrite(pack_fd, hdr, hdrlen);
ac47a738 575 pack_offset += hdrlen;
db5e523f
SP
576 }
577
578 s.avail_out = deflateBound(&s, s.avail_in);
579 s.next_out = out = xmalloc(s.avail_out);
580 while (deflate(&s, Z_FINISH) == Z_OK)
581 /* nothing */;
582 deflateEnd(&s);
583
463acbe1 584 ywrite(pack_fd, out, s.total_out);
ac47a738 585 pack_offset += s.total_out;
db5e523f
SP
586
587 free(out);
588 if (delta)
589 free(delta);
463acbe1
SP
590 if (last) {
591 if (last->data)
592 free(last->data);
593 last->data = dat;
594 last->len = datlen;
595 memcpy(last->sha1, sha1, sizeof(sha1));
596 }
597 return 0;
598}
599
600static const char *get_mode(const char *str, unsigned int *modep)
601{
602 unsigned char c;
603 unsigned int mode = 0;
604
605 while ((c = *str++) != ' ') {
606 if (c < '0' || c > '7')
607 return NULL;
608 mode = (mode << 3) + (c - '0');
609 }
610 *modep = mode;
611 return str;
612}
613
614static void load_tree(struct tree_entry *root)
615{
616 struct object_entry *myoe;
617 struct tree_content *t;
618 unsigned long size;
619 char *buf;
620 const char *c;
621 char type[20];
622
623 root->tree = t = new_tree_content(8);
624 if (!memcmp(root->sha1, null_sha1, 20))
625 return;
626
627 myoe = find_object(root->sha1);
628 if (myoe) {
629 die("FIXME");
630 } else {
631 buf = read_sha1_file(root->sha1, type, &size);
632 if (!buf || strcmp(type, tree_type))
633 die("Can't load existing tree %s", sha1_to_hex(root->sha1));
634 }
635
636 c = buf;
637 while (c != (buf + size)) {
638 struct tree_entry *e = new_tree_entry();
639
640 if (t->entry_count == t->entry_capacity)
641 root->tree = t = grow_tree_content(t, 8);
642 t->entries[t->entry_count++] = e;
643
644 e->tree = NULL;
645 c = get_mode(c, &e->mode);
646 if (!c)
647 die("Corrupt mode in %s", sha1_to_hex(root->sha1));
648 e->name = to_atom(c, strlen(c));
649 c += e->name->str_len + 1;
650 memcpy(e->sha1, c, sizeof(e->sha1));
651 c += 20;
652 }
653 free(buf);
654}
655
656static int tecmp (const void *_a, const void *_b)
657{
658 struct tree_entry *a = *((struct tree_entry**)_a);
659 struct tree_entry *b = *((struct tree_entry**)_b);
660 return base_name_compare(
661 a->name->str_dat, a->name->str_len, a->mode,
662 b->name->str_dat, b->name->str_len, b->mode);
663}
664
665static void store_tree(struct tree_entry *root)
666{
667 struct tree_content *t = root->tree;
668 unsigned int i;
669 size_t maxlen;
670 char *buf, *c;
671
672 if (memcmp(root->sha1, null_sha1, 20))
673 return;
674
675 maxlen = 0;
676 for (i = 0; i < t->entry_count; i++) {
677 maxlen += t->entries[i]->name->str_len + 34;
678 if (t->entries[i]->tree)
679 store_tree(t->entries[i]);
680 }
681
682 qsort(t->entries, t->entry_count, sizeof(t->entries[0]), tecmp);
683 buf = c = xmalloc(maxlen);
684 for (i = 0; i < t->entry_count; i++) {
685 struct tree_entry *e = t->entries[i];
686 c += sprintf(c, "%o", e->mode);
687 *c++ = ' ';
688 strcpy(c, e->name->str_dat);
689 c += e->name->str_len + 1;
690 memcpy(c, e->sha1, 20);
691 c += 20;
692 }
693 store_object(OBJ_TREE, buf, c - buf, NULL, root->sha1);
694 free(buf);
695}
696
697static int tree_content_set(
698 struct tree_entry *root,
699 const char *p,
700 const unsigned char *sha1,
701 const unsigned int mode)
702{
703 struct tree_content *t = root->tree;
704 const char *slash1;
705 unsigned int i, n;
706 struct tree_entry *e;
707
708 slash1 = strchr(p, '/');
709 if (slash1)
710 n = slash1 - p;
711 else
712 n = strlen(p);
713
714 for (i = 0; i < t->entry_count; i++) {
715 e = t->entries[i];
716 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
717 if (!slash1) {
718 if (e->mode == mode && !memcmp(e->sha1, sha1, 20))
719 return 0;
720 e->mode = mode;
721 memcpy(e->sha1, sha1, 20);
722 if (e->tree) {
723 release_tree_content(e->tree);
724 e->tree = NULL;
725 }
726 memcpy(root->sha1, null_sha1, 20);
727 return 1;
728 }
729 if (!S_ISDIR(e->mode)) {
730 e->tree = new_tree_content(8);
7111feed 731 e->mode = S_IFDIR;
463acbe1
SP
732 }
733 if (!e->tree)
734 load_tree(e);
735 if (tree_content_set(e, slash1 + 1, sha1, mode)) {
736 memcpy(root->sha1, null_sha1, 20);
737 return 1;
738 }
739 return 0;
740 }
741 }
742
743 if (t->entry_count == t->entry_capacity)
744 root->tree = t = grow_tree_content(t, 8);
745 e = new_tree_entry();
746 e->name = to_atom(p, n);
747 t->entries[t->entry_count++] = e;
748 if (slash1) {
749 e->tree = new_tree_content(8);
7111feed 750 e->mode = S_IFDIR;
463acbe1
SP
751 tree_content_set(e, slash1 + 1, sha1, mode);
752 } else {
753 e->tree = NULL;
754 e->mode = mode;
755 memcpy(e->sha1, sha1, 20);
756 }
757 memcpy(root->sha1, null_sha1, 20);
758 return 1;
759}
760
761static int tree_content_remove(struct tree_entry *root, const char *p)
762{
763 struct tree_content *t = root->tree;
764 const char *slash1;
765 unsigned int i, n;
766 struct tree_entry *e;
767
768 slash1 = strchr(p, '/');
769 if (slash1)
770 n = slash1 - p;
771 else
772 n = strlen(p);
773
774 for (i = 0; i < t->entry_count; i++) {
775 e = t->entries[i];
776 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
777 if (!slash1 || !S_ISDIR(e->mode))
778 goto del_entry;
779 if (!e->tree)
780 load_tree(e);
781 if (tree_content_remove(e, slash1 + 1)) {
782 if (!e->tree->entry_count)
783 goto del_entry;
784 memcpy(root->sha1, null_sha1, 20);
785 return 1;
786 }
787 return 0;
788 }
789 }
790 return 0;
791
792del_entry:
793 for (i++; i < t->entry_count; i++)
794 t->entries[i-1] = t->entries[i];
795 t->entry_count--;
796 release_tree_entry(e);
797 memcpy(root->sha1, null_sha1, 20);
ac47a738 798 return 1;
db5e523f
SP
799}
800
8bcce301 801static void init_pack_header()
db5e523f 802{
c90be46a
SP
803 struct pack_header hdr;
804
805 hdr.hdr_signature = htonl(PACK_SIGNATURE);
806 hdr.hdr_version = htonl(2);
807 hdr.hdr_entries = 0;
808
809 ywrite(pack_fd, &hdr, sizeof(hdr));
810 pack_offset = sizeof(hdr);
db5e523f
SP
811}
812
8bcce301 813static void fixup_header_footer()
db5e523f
SP
814{
815 SHA_CTX c;
816 char hdr[8];
db5e523f
SP
817 unsigned long cnt;
818 char *buf;
819 size_t n;
820
ac47a738 821 if (lseek(pack_fd, 0, SEEK_SET) != 0)
db5e523f
SP
822 die("Failed seeking to start: %s", strerror(errno));
823
824 SHA1_Init(&c);
463acbe1 825 yread(pack_fd, hdr, 8);
db5e523f
SP
826 SHA1_Update(&c, hdr, 8);
827
db5e523f
SP
828 cnt = htonl(object_count);
829 SHA1_Update(&c, &cnt, 4);
463acbe1 830 ywrite(pack_fd, &cnt, 4);
db5e523f
SP
831
832 buf = xmalloc(128 * 1024);
833 for (;;) {
ac47a738 834 n = xread(pack_fd, buf, 128 * 1024);
db5e523f
SP
835 if (n <= 0)
836 break;
837 SHA1_Update(&c, buf, n);
838 }
839 free(buf);
840
ac47a738 841 SHA1_Final(pack_sha1, &c);
463acbe1 842 ywrite(pack_fd, pack_sha1, sizeof(pack_sha1));
db5e523f
SP
843}
844
8bcce301 845static int oecmp (const void *_a, const void *_b)
db5e523f 846{
8bcce301
SP
847 struct object_entry *a = *((struct object_entry**)_a);
848 struct object_entry *b = *((struct object_entry**)_b);
849 return memcmp(a->sha1, b->sha1, sizeof(a->sha1));
850}
851
852static void write_index(const char *idx_name)
853{
854 struct sha1file *f;
855 struct object_entry **idx, **c, **last;
856 struct object_entry *e;
463acbe1 857 struct object_entry_pool *o;
8bcce301
SP
858 unsigned int array[256];
859 int i;
860
861 /* Build the sorted table of object IDs. */
862 idx = xmalloc(object_count * sizeof(struct object_entry*));
863 c = idx;
463acbe1 864 for (o = blocks; o; o = o->next_pool)
27d6d290
SP
865 for (e = o->entries; e != o->next_free; e++)
866 *c++ = e;
8bcce301
SP
867 last = idx + object_count;
868 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
869
870 /* Generate the fan-out array. */
871 c = idx;
872 for (i = 0; i < 256; i++) {
873 struct object_entry **next = c;;
874 while (next < last) {
875 if ((*next)->sha1[0] != i)
876 break;
877 next++;
878 }
879 array[i] = htonl(next - idx);
880 c = next;
881 }
882
883 f = sha1create("%s", idx_name);
884 sha1write(f, array, 256 * sizeof(int));
885 for (c = idx; c != last; c++) {
886 unsigned int offset = htonl((*c)->offset);
887 sha1write(f, &offset, 4);
888 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
889 }
ac47a738 890 sha1write(f, pack_sha1, sizeof(pack_sha1));
8bcce301
SP
891 sha1close(f, NULL, 1);
892 free(idx);
893}
894
463acbe1
SP
895static void dump_branches()
896{
897 static const char *msg = "fast-import";
898 unsigned int i;
899 struct branch *b;
900 struct ref_lock *lock;
901
902 for (i = 0; i < branch_table_sz; i++) {
903 for (b = branch_table[i]; b; b = b->table_next_branch) {
904 lock = lock_any_ref_for_update(b->name, NULL, 0);
905 if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
906 die("Can't write %s", b->name);
907 }
908 }
909}
910
c44cdc7e
SP
911static void read_next_command()
912{
913 read_line(&command_buf, stdin, '\n');
914}
915
916static void cmd_mark()
917{
918 if (!strncmp("mark :", command_buf.buf, 6)) {
919 command_mark = strtoul(command_buf.buf + 6, NULL, 10);
920 read_next_command();
921 }
922 else
923 command_mark = 0;
924}
925
926static void* cmd_data (size_t *size)
927{
928 size_t n = 0;
929 void *buffer;
930 size_t length;
931
932 if (strncmp("data ", command_buf.buf, 5))
933 die("Expected 'data n' command, found: %s", command_buf.buf);
934
935 length = strtoul(command_buf.buf + 5, NULL, 10);
936 buffer = xmalloc(length);
937
938 while (n < length) {
939 size_t s = fread((char*)buffer + n, 1, length - n, stdin);
940 if (!s && feof(stdin))
941 die("EOF in data (%lu bytes remaining)", length - n);
942 n += s;
943 }
944
945 if (fgetc(stdin) != '\n')
946 die("An lf did not trail the binary data as expected.");
947
948 *size = length;
949 return buffer;
950}
951
463acbe1 952static void cmd_new_blob()
6143f064 953{
c44cdc7e 954 size_t datlen;
6143f064 955 void *dat;
c44cdc7e
SP
956 unsigned char sha1[20];
957
958 read_next_command();
959 cmd_mark();
960 dat = cmd_data(&datlen);
6143f064 961
463acbe1 962 if (store_object(OBJ_BLOB, dat, datlen, &last_blob, sha1))
6143f064
SP
963 free(dat);
964}
965
463acbe1 966static void unload_one_branch()
6bb5b329 967{
463acbe1
SP
968 while (cur_active_branches >= max_active_branches) {
969 unsigned long min_commit = ULONG_MAX;
970 struct branch *e, *l = NULL, *p = NULL;
971
972 for (e = active_branches; e; e = e->active_next_branch) {
973 if (e->last_commit < min_commit) {
974 p = l;
975 min_commit = e->last_commit;
976 }
977 l = e;
978 }
979
980 if (p) {
981 e = p->active_next_branch;
982 p->active_next_branch = e->active_next_branch;
983 } else {
984 e = active_branches;
985 active_branches = e->active_next_branch;
986 }
987 e->active_next_branch = NULL;
988 if (e->branch_tree.tree) {
989 release_tree_content(e->branch_tree.tree);
990 e->branch_tree.tree = NULL;
991 }
992 cur_active_branches--;
6bb5b329 993 }
6bb5b329
SP
994}
995
463acbe1 996static void load_branch(struct branch *b)
6bb5b329 997{
463acbe1
SP
998 load_tree(&b->branch_tree);
999 b->active_next_branch = active_branches;
1000 active_branches = b;
1001 cur_active_branches++;
6bb5b329
SP
1002}
1003
463acbe1 1004static void file_change_m(struct branch *b)
6bb5b329 1005{
c44cdc7e
SP
1006 const char *p = command_buf.buf + 2;
1007 char *p_uq;
1008 const char *endp;
7111feed 1009 struct object_entry *oe;
463acbe1 1010 unsigned char sha1[20];
c44cdc7e 1011 unsigned int mode;
7111feed 1012 char type[20];
6bb5b329 1013
c44cdc7e
SP
1014 p = get_mode(p, &mode);
1015 if (!p)
1016 die("Corrupt mode: %s", command_buf.buf);
1017 switch (mode) {
1018 case S_IFREG | 0644:
1019 case S_IFREG | 0755:
ace4a9d1 1020 case S_IFLNK:
c44cdc7e
SP
1021 case 0644:
1022 case 0755:
1023 /* ok */
1024 break;
1025 default:
1026 die("Corrupt mode: %s", command_buf.buf);
1027 }
1028
1029 if (get_sha1_hex(p, sha1))
1030 die("Invalid SHA1: %s", command_buf.buf);
1031 p += 40;
1032 if (*p++ != ' ')
1033 die("Missing space after SHA1: %s", command_buf.buf);
1034
1035 p_uq = unquote_c_style(p, &endp);
1036 if (p_uq) {
1037 if (*endp)
1038 die("Garbage after path in: %s", command_buf.buf);
1039 p = p_uq;
1040 }
6bb5b329 1041
7111feed
SP
1042 oe = find_object(sha1);
1043 if (oe) {
1044 if (oe->type != OBJ_BLOB)
c44cdc7e
SP
1045 die("Not a blob (actually a %s): %s",
1046 command_buf.buf, type_names[oe->type]);
7111feed
SP
1047 } else {
1048 if (sha1_object_info(sha1, type, NULL))
c44cdc7e 1049 die("Blob not found: %s", command_buf.buf);
7111feed 1050 if (strcmp(blob_type, type))
c44cdc7e
SP
1051 die("Not a blob (actually a %s): %s",
1052 command_buf.buf, type);
7111feed 1053 }
6bb5b329 1054
c44cdc7e
SP
1055 tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1056
1057 if (p_uq)
1058 free(p_uq);
463acbe1 1059}
6bb5b329 1060
463acbe1
SP
1061static void file_change_d(struct branch *b)
1062{
c44cdc7e
SP
1063 const char *p = command_buf.buf + 2;
1064 char *p_uq;
1065 const char *endp;
1066
1067 p_uq = unquote_c_style(p, &endp);
1068 if (p_uq) {
1069 if (*endp)
1070 die("Garbage after path in: %s", command_buf.buf);
1071 p = p_uq;
1072 }
1073 tree_content_remove(&b->branch_tree, p);
1074 if (p_uq)
1075 free(p_uq);
6bb5b329
SP
1076}
1077
463acbe1 1078static void cmd_new_commit()
6bb5b329 1079{
c44cdc7e
SP
1080 struct branch *b;
1081 void *msg;
1082 size_t msglen;
1083 char *str_uq;
1084 const char *endp;
1085 char *sp;
1086 char *author = NULL;
1087 char *committer = NULL;
1088 char *body;
1089
1090 /* Obtain the branch name from the rest of our command */
1091 sp = strchr(command_buf.buf, ' ') + 1;
1092 str_uq = unquote_c_style(sp, &endp);
1093 if (str_uq) {
1094 if (*endp)
1095 die("Garbage after ref in: %s", command_buf.buf);
1096 sp = str_uq;
1097 }
1098 b = lookup_branch(sp);
463acbe1 1099 if (!b)
c44cdc7e
SP
1100 die("Branch not declared: %s", sp);
1101 if (str_uq)
1102 free(str_uq);
1103
1104 read_next_command();
1105 cmd_mark();
1106 if (!strncmp("author ", command_buf.buf, 7)) {
1107 author = strdup(command_buf.buf);
1108 read_next_command();
1109 }
1110 if (!strncmp("committer ", command_buf.buf, 10)) {
1111 committer = strdup(command_buf.buf);
1112 read_next_command();
1113 }
1114 if (!committer)
1115 die("Expected committer but didn't get one");
1116 msg = cmd_data(&msglen);
1117
1118 /* ensure the branch is active/loaded */
463acbe1
SP
1119 if (!b->branch_tree.tree) {
1120 unload_one_branch();
1121 load_branch(b);
1122 }
6bb5b329 1123
463acbe1
SP
1124 /* file_change* */
1125 for (;;) {
c44cdc7e
SP
1126 read_next_command();
1127 if (1 == command_buf.len)
463acbe1 1128 break;
c44cdc7e 1129 else if (!strncmp("M ", command_buf.buf, 2))
463acbe1 1130 file_change_m(b);
c44cdc7e 1131 else if (!strncmp("D ", command_buf.buf, 2))
463acbe1
SP
1132 file_change_d(b);
1133 else
c44cdc7e 1134 die("Unsupported file_change: %s", command_buf.buf);
6bb5b329 1135 }
6bb5b329 1136
c44cdc7e 1137 /* build the tree and the commit */
463acbe1 1138 store_tree(&b->branch_tree);
c44cdc7e
SP
1139 body = xmalloc(97 + msglen
1140 + (author
1141 ? strlen(author) + strlen(committer)
1142 : 2 * strlen(committer)));
1143 sp = body;
1144 sp += sprintf(sp, "tree %s\n", sha1_to_hex(b->branch_tree.sha1));
1145 if (memcmp(b->sha1, null_sha1, 20))
1146 sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1147 if (author)
1148 sp += sprintf(sp, "%s\n", author);
1149 else
1150 sp += sprintf(sp, "author %s\n", committer + 10);
1151 sp += sprintf(sp, "%s\n\n", committer);
1152 memcpy(sp, msg, msglen);
1153 sp += msglen;
1154 if (author)
1155 free(author);
1156 free(committer);
1157 free(msg);
1158
1159 store_object(OBJ_COMMIT, body, sp - body, NULL, b->sha1);
463acbe1
SP
1160 free(body);
1161 b->last_commit = object_count_by_type[OBJ_COMMIT];
6bb5b329
SP
1162}
1163
463acbe1 1164static void cmd_new_branch()
6bb5b329 1165{
c44cdc7e
SP
1166 struct branch *b;
1167 char *str_uq;
1168 const char *endp;
1169 char *sp;
1170
1171 /* Obtain the new branch name from the rest of our command */
1172 sp = strchr(command_buf.buf, ' ') + 1;
1173 str_uq = unquote_c_style(sp, &endp);
1174 if (str_uq) {
1175 if (*endp)
1176 die("Garbage after ref in: %s", command_buf.buf);
1177 sp = str_uq;
463acbe1 1178 }
c44cdc7e
SP
1179 b = new_branch(sp);
1180 if (str_uq)
1181 free(str_uq);
1182 read_next_command();
1183
1184 /* from ... */
1185 if (!strncmp("from ", command_buf.buf, 5)) {
1186 const char *from;
1187 struct branch *s;
1188
1189 from = strchr(command_buf.buf, ' ') + 1;
1190 str_uq = unquote_c_style(from, &endp);
1191 if (str_uq) {
1192 if (*endp)
1193 die("Garbage after string in: %s", command_buf.buf);
1194 from = str_uq;
463acbe1 1195 }
c44cdc7e
SP
1196
1197 s = lookup_branch(from);
1198 if (b == s)
1199 die("Can't create a branch from itself: %s", b->name);
1200 else if (s) {
1201 memcpy(b->sha1, s->sha1, 20);
1202 memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
1203 } else if (!get_sha1(from, b->sha1)) {
1204 if (!memcmp(b->sha1, null_sha1, 20))
1205 memcpy(b->branch_tree.sha1, null_sha1, 20);
1206 else {
1207 unsigned long size;
1208 char *buf;
1209
1210 buf = read_object_with_reference(b->sha1,
1211 type_names[OBJ_COMMIT], &size, b->sha1);
1212 if (!buf || size < 46)
1213 die("Not a valid commit: %s", from);
1214 if (memcmp("tree ", buf, 5)
1215 || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1216 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1217 free(buf);
1218 }
1219 } else
1220 die("Invalid ref name or SHA1 expression: %s", from);
1221
1222 if (str_uq)
1223 free(str_uq);
1224 read_next_command();
1225 } else {
1226 memcpy(b->sha1, null_sha1, 20);
1227 memcpy(b->branch_tree.sha1, null_sha1, 20);
1228 }
1229
1230 if (command_buf.eof || command_buf.len > 1)
1231 die("An lf did not terminate the branch command as expected.");
6bb5b329
SP
1232}
1233
8bcce301
SP
1234int main(int argc, const char **argv)
1235{
1236 const char *base_name = argv[1];
1237 int est_obj_cnt = atoi(argv[2]);
1238 char *pack_name;
1239 char *idx_name;
6143f064 1240 struct stat sb;
8bcce301 1241
463acbe1
SP
1242 setup_ident();
1243 git_config(git_default_config);
1244
8bcce301
SP
1245 pack_name = xmalloc(strlen(base_name) + 6);
1246 sprintf(pack_name, "%s.pack", base_name);
1247 idx_name = xmalloc(strlen(base_name) + 5);
1248 sprintf(idx_name, "%s.idx", base_name);
1249
ac47a738
SP
1250 pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
1251 if (pack_fd < 0)
6143f064 1252 die("Can't create %s: %s", pack_name, strerror(errno));
8bcce301 1253
c44cdc7e 1254 init_pack_header();
27d6d290 1255 alloc_objects(est_obj_cnt);
c44cdc7e 1256 strbuf_init(&command_buf);
463acbe1
SP
1257
1258 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
1259 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
1260 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
1261
db5e523f 1262 for (;;) {
c44cdc7e
SP
1263 read_next_command();
1264 if (command_buf.eof)
db5e523f 1265 break;
c44cdc7e
SP
1266 else if (!strcmp("blob", command_buf.buf))
1267 cmd_new_blob();
1268 else if (!strncmp("branch ", command_buf.buf, 7))
1269 cmd_new_branch();
1270 else if (!strncmp("commit ", command_buf.buf, 7))
1271 cmd_new_commit();
1272 else
1273 die("Unsupported command: %s", command_buf.buf);
db5e523f 1274 }
c44cdc7e 1275
db5e523f 1276 fixup_header_footer();
ac47a738 1277 close(pack_fd);
8bcce301 1278 write_index(idx_name);
463acbe1 1279 dump_branches();
8bcce301 1280
6143f064
SP
1281 fprintf(stderr, "%s statistics:\n", argv[0]);
1282 fprintf(stderr, "---------------------------------------------------\n");
1283 fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt);
1284 fprintf(stderr, "Total objects: %10lu (%10lu duplicates)\n", object_count, duplicate_count);
1285 fprintf(stderr, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB]);
1286 fprintf(stderr, " trees : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]);
1287 fprintf(stderr, " commits: %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]);
1288 fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
6bb5b329 1289 fprintf(stderr, "Total branches: %10lu\n", branch_count);
463acbe1 1290 fprintf(stderr, "Total atoms: %10u\n", atom_cnt);
7111feed
SP
1291 fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1292 fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1293 fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
6143f064
SP
1294 fprintf(stderr, "---------------------------------------------------\n");
1295
1296 stat(pack_name, &sb);
1297 fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1298 stat(idx_name, &sb);
1299 fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
1300
1301 fprintf(stderr, "\n");
db5e523f
SP
1302
1303 return 0;
1304}