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