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