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