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