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