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