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