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