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