vcs-svn: Limit bytes read and written strictly
[git/barrbrain.git] / fast-import.c
blob87246d50b80e06834c472e39f1d0e7c951cfd32d
1 /*
2 (See Documentation/git-fast-import.txt for maintained documentation.)
3 Format of STDIN stream:
5 stream ::= cmd*;
7 cmd ::= new_blob
8 | new_commit
9 | new_tag
10 | reset_branch
11 | checkpoint
12 | progress
15 new_blob ::= 'blob' lf
16 mark?
17 file_content;
18 file_content ::= data;
20 new_commit ::= 'commit' sp ref_str lf
21 mark?
22 ('author' (sp name)? sp '<' email '>' sp when lf)?
23 'committer' (sp name)? sp '<' email '>' sp when lf
24 commit_msg
25 ('from' sp committish lf)?
26 ('merge' sp committish lf)*
27 file_change*
28 lf?;
29 commit_msg ::= data;
31 file_change ::= file_clr
32 | file_del
33 | file_rnm
34 | file_cpy
35 | file_obm
36 | file_inm;
37 file_clr ::= 'deleteall' lf;
38 file_del ::= 'D' sp path_str lf;
39 file_rnm ::= 'R' sp path_str sp path_str lf;
40 file_cpy ::= 'C' sp path_str sp path_str lf;
41 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
42 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
43 data;
44 note_obm ::= 'N' sp (hexsha1 | idnum) sp committish lf;
45 note_inm ::= 'N' sp 'inline' sp committish lf
46 data;
48 new_tag ::= 'tag' sp tag_str lf
49 'from' sp committish lf
50 ('tagger' (sp name)? sp '<' email '>' sp when lf)?
51 tag_msg;
52 tag_msg ::= data;
54 reset_branch ::= 'reset' sp ref_str lf
55 ('from' sp committish lf)?
56 lf?;
58 checkpoint ::= 'checkpoint' lf
59 lf?;
61 progress ::= 'progress' sp not_lf* lf
62 lf?;
64 # note: the first idnum in a stream should be 1 and subsequent
65 # idnums should not have gaps between values as this will cause
66 # the stream parser to reserve space for the gapped values. An
67 # idnum can be updated in the future to a new object by issuing
68 # a new mark directive with the old idnum.
70 mark ::= 'mark' sp idnum lf;
71 data ::= (delimited_data | exact_data)
72 lf?;
74 # note: delim may be any string but must not contain lf.
75 # data_line may contain any data but must not be exactly
76 # delim.
77 delimited_data ::= 'data' sp '<<' delim lf
78 (data_line lf)*
79 delim lf;
81 # note: declen indicates the length of binary_data in bytes.
82 # declen does not include the lf preceding the binary data.
84 exact_data ::= 'data' sp declen lf
85 binary_data;
87 # note: quoted strings are C-style quoting supporting \c for
88 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
89 # is the signed byte value in octal. Note that the only
90 # characters which must actually be escaped to protect the
91 # stream formatting is: \, " and LF. Otherwise these values
92 # are UTF8.
94 committish ::= (ref_str | hexsha1 | sha1exp_str | idnum);
95 ref_str ::= ref;
96 sha1exp_str ::= sha1exp;
97 tag_str ::= tag;
98 path_str ::= path | '"' quoted(path) '"' ;
99 mode ::= '100644' | '644'
100 | '100755' | '755'
101 | '120000'
104 declen ::= # unsigned 32 bit value, ascii base10 notation;
105 bigint ::= # unsigned integer value, ascii base10 notation;
106 binary_data ::= # file content, not interpreted;
108 when ::= raw_when | rfc2822_when;
109 raw_when ::= ts sp tz;
110 rfc2822_when ::= # Valid RFC 2822 date and time;
112 sp ::= # ASCII space character;
113 lf ::= # ASCII newline (LF) character;
115 # note: a colon (':') must precede the numerical value assigned to
116 # an idnum. This is to distinguish it from a ref or tag name as
117 # GIT does not permit ':' in ref or tag strings.
119 idnum ::= ':' bigint;
120 path ::= # GIT style file path, e.g. "a/b/c";
121 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
122 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
123 sha1exp ::= # Any valid GIT SHA1 expression;
124 hexsha1 ::= # SHA1 in hexadecimal format;
126 # note: name and email are UTF8 strings, however name must not
127 # contain '<' or lf and email must not contain any of the
128 # following: '<', '>', lf.
130 name ::= # valid GIT author/committer name;
131 email ::= # valid GIT author/committer email;
132 ts ::= # time since the epoch in seconds, ascii base10 notation;
133 tz ::= # GIT style timezone;
135 # note: comments and cat requests may appear anywhere
136 # in the input, except within a data command. Any form
137 # of the data command always escapes the related input
138 # from comment processing.
140 # In case it is not clear, the '#' that starts the comment
141 # must be the first character on that line (an lf
142 # preceded it).
144 cat_request ::= 'cat' sp (hexsha1 | idnum) lf
145 | 'cat' sp hexsha1 sp path_str lf;
147 comment ::= '#' not_lf* lf;
148 not_lf ::= # Any byte that is not ASCII newline (LF);
151 #include "builtin.h"
152 #include "cache.h"
153 #include "object.h"
154 #include "blob.h"
155 #include "tree.h"
156 #include "commit.h"
157 #include "delta.h"
158 #include "pack.h"
159 #include "refs.h"
160 #include "csum-file.h"
161 #include "quote.h"
162 #include "exec_cmd.h"
164 #define PACK_ID_BITS 16
165 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
166 #define DEPTH_BITS 13
167 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
169 struct object_entry
171 struct pack_idx_entry idx;
172 struct object_entry *next;
173 uint32_t type : TYPE_BITS,
174 pack_id : PACK_ID_BITS,
175 depth : DEPTH_BITS;
178 struct object_entry_pool
180 struct object_entry_pool *next_pool;
181 struct object_entry *next_free;
182 struct object_entry *end;
183 struct object_entry entries[FLEX_ARRAY]; /* more */
186 struct mark_set
188 union {
189 struct object_entry *marked[1024];
190 struct mark_set *sets[1024];
191 } data;
192 unsigned int shift;
195 struct last_object
197 struct strbuf data;
198 off_t offset;
199 unsigned int depth;
200 unsigned no_swap : 1;
203 struct mem_pool
205 struct mem_pool *next_pool;
206 char *next_free;
207 char *end;
208 uintmax_t space[FLEX_ARRAY]; /* more */
211 struct atom_str
213 struct atom_str *next_atom;
214 unsigned short str_len;
215 char str_dat[FLEX_ARRAY]; /* more */
218 struct tree_content;
219 struct tree_entry
221 struct tree_content *tree;
222 struct atom_str *name;
223 struct tree_entry_ms
225 uint16_t mode;
226 unsigned char sha1[20];
227 } versions[2];
230 struct tree_content
232 unsigned int entry_capacity; /* must match avail_tree_content */
233 unsigned int entry_count;
234 unsigned int delta_depth;
235 struct tree_entry *entries[FLEX_ARRAY]; /* more */
238 struct avail_tree_content
240 unsigned int entry_capacity; /* must match tree_content */
241 struct avail_tree_content *next_avail;
244 struct branch
246 struct branch *table_next_branch;
247 struct branch *active_next_branch;
248 const char *name;
249 struct tree_entry branch_tree;
250 uintmax_t last_commit;
251 uintmax_t num_notes;
252 unsigned active : 1;
253 unsigned pack_id : PACK_ID_BITS;
254 unsigned char sha1[20];
257 struct tag
259 struct tag *next_tag;
260 const char *name;
261 unsigned int pack_id;
262 unsigned char sha1[20];
265 struct hash_list
267 struct hash_list *next;
268 unsigned char sha1[20];
271 typedef enum {
272 WHENSPEC_RAW = 1,
273 WHENSPEC_RFC2822,
274 WHENSPEC_NOW
275 } whenspec_type;
277 struct recent_command
279 struct recent_command *prev;
280 struct recent_command *next;
281 char *buf;
284 /* Configured limits on output */
285 static unsigned long max_depth = 10;
286 static off_t max_packsize;
287 static uintmax_t big_file_threshold = 512 * 1024 * 1024;
288 static int force_update;
289 static int pack_compression_level = Z_DEFAULT_COMPRESSION;
290 static int pack_compression_seen;
292 /* Stats and misc. counters */
293 static uintmax_t alloc_count;
294 static uintmax_t marks_set_count;
295 static uintmax_t object_count_by_type[1 << TYPE_BITS];
296 static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
297 static uintmax_t delta_count_by_type[1 << TYPE_BITS];
298 static unsigned long object_count;
299 static unsigned long branch_count;
300 static unsigned long branch_load_count;
301 static int failure;
302 static FILE *pack_edges;
303 static unsigned int show_stats = 1;
304 static int global_argc;
305 static const char **global_argv;
307 /* Memory pools */
308 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
309 static size_t total_allocd;
310 static struct mem_pool *mem_pool;
312 /* Atom management */
313 static unsigned int atom_table_sz = 4451;
314 static unsigned int atom_cnt;
315 static struct atom_str **atom_table;
317 /* The .pack file being generated */
318 static unsigned int pack_id;
319 static struct sha1file *pack_file;
320 static struct packed_git *pack_data;
321 static struct packed_git **all_packs;
322 static off_t pack_size;
324 /* Table of objects we've written. */
325 static unsigned int object_entry_alloc = 5000;
326 static struct object_entry_pool *blocks;
327 static struct object_entry *object_table[1 << 16];
328 static struct mark_set *marks;
329 static const char *export_marks_file;
330 static const char *import_marks_file;
331 static int import_marks_file_from_stream;
332 static int relative_marks_paths;
334 /* Our last blob */
335 static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 };
337 /* Tree management */
338 static unsigned int tree_entry_alloc = 1000;
339 static void *avail_tree_entry;
340 static unsigned int avail_tree_table_sz = 100;
341 static struct avail_tree_content **avail_tree_table;
342 static struct strbuf old_tree = STRBUF_INIT;
343 static struct strbuf new_tree = STRBUF_INIT;
345 /* Branch data */
346 static unsigned long max_active_branches = 5;
347 static unsigned long cur_active_branches;
348 static unsigned long branch_table_sz = 1039;
349 static struct branch **branch_table;
350 static struct branch *active_branches;
352 /* Tag data */
353 static struct tag *first_tag;
354 static struct tag *last_tag;
356 /* Input stream parsing */
357 static whenspec_type whenspec = WHENSPEC_RAW;
358 static struct strbuf command_buf = STRBUF_INIT;
359 static int unread_command_buf;
360 static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
361 static struct recent_command *cmd_tail = &cmd_hist;
362 static struct recent_command *rc_free;
363 static unsigned int cmd_save = 100;
364 static uintmax_t next_mark;
365 static struct strbuf new_data = STRBUF_INIT;
366 static int seen_data_command;
368 /* Where to report commits */
369 static int report_fd = -1;
371 static void parse_argv(void);
372 static void parse_cat_request(void);
374 static void write_branch_report(FILE *rpt, struct branch *b)
376 fprintf(rpt, "%s:\n", b->name);
378 fprintf(rpt, " status :");
379 if (b->active)
380 fputs(" active", rpt);
381 if (b->branch_tree.tree)
382 fputs(" loaded", rpt);
383 if (is_null_sha1(b->branch_tree.versions[1].sha1))
384 fputs(" dirty", rpt);
385 fputc('\n', rpt);
387 fprintf(rpt, " tip commit : %s\n", sha1_to_hex(b->sha1));
388 fprintf(rpt, " old tree : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1));
389 fprintf(rpt, " cur tree : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1));
390 fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit);
392 fputs(" last pack : ", rpt);
393 if (b->pack_id < MAX_PACK_ID)
394 fprintf(rpt, "%u", b->pack_id);
395 fputc('\n', rpt);
397 fputc('\n', rpt);
400 static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
402 static void write_crash_report(const char *err)
404 char *loc = git_path("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
405 FILE *rpt = fopen(loc, "w");
406 struct branch *b;
407 unsigned long lu;
408 struct recent_command *rc;
410 if (!rpt) {
411 error("can't write crash report %s: %s", loc, strerror(errno));
412 return;
415 fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
417 fprintf(rpt, "fast-import crash report:\n");
418 fprintf(rpt, " fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid());
419 fprintf(rpt, " parent process : %"PRIuMAX"\n", (uintmax_t) getppid());
420 fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
421 fputc('\n', rpt);
423 fputs("fatal: ", rpt);
424 fputs(err, rpt);
425 fputc('\n', rpt);
427 fputc('\n', rpt);
428 fputs("Most Recent Commands Before Crash\n", rpt);
429 fputs("---------------------------------\n", rpt);
430 for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
431 if (rc->next == &cmd_hist)
432 fputs("* ", rpt);
433 else
434 fputs(" ", rpt);
435 fputs(rc->buf, rpt);
436 fputc('\n', rpt);
439 fputc('\n', rpt);
440 fputs("Active Branch LRU\n", rpt);
441 fputs("-----------------\n", rpt);
442 fprintf(rpt, " active_branches = %lu cur, %lu max\n",
443 cur_active_branches,
444 max_active_branches);
445 fputc('\n', rpt);
446 fputs(" pos clock name\n", rpt);
447 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
448 for (b = active_branches, lu = 0; b; b = b->active_next_branch)
449 fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n",
450 ++lu, b->last_commit, b->name);
452 fputc('\n', rpt);
453 fputs("Inactive Branches\n", rpt);
454 fputs("-----------------\n", rpt);
455 for (lu = 0; lu < branch_table_sz; lu++) {
456 for (b = branch_table[lu]; b; b = b->table_next_branch)
457 write_branch_report(rpt, b);
460 if (first_tag) {
461 struct tag *tg;
462 fputc('\n', rpt);
463 fputs("Annotated Tags\n", rpt);
464 fputs("--------------\n", rpt);
465 for (tg = first_tag; tg; tg = tg->next_tag) {
466 fputs(sha1_to_hex(tg->sha1), rpt);
467 fputc(' ', rpt);
468 fputs(tg->name, rpt);
469 fputc('\n', rpt);
473 fputc('\n', rpt);
474 fputs("Marks\n", rpt);
475 fputs("-----\n", rpt);
476 if (export_marks_file)
477 fprintf(rpt, " exported to %s\n", export_marks_file);
478 else
479 dump_marks_helper(rpt, 0, marks);
481 fputc('\n', rpt);
482 fputs("-------------------\n", rpt);
483 fputs("END OF CRASH REPORT\n", rpt);
484 fclose(rpt);
487 static void end_packfile(void);
488 static void unkeep_all_packs(void);
489 static void dump_marks(void);
491 static NORETURN void die_nicely(const char *err, va_list params)
493 static int zombie;
494 char message[2 * PATH_MAX];
496 vsnprintf(message, sizeof(message), err, params);
497 fputs("fatal: ", stderr);
498 fputs(message, stderr);
499 fputc('\n', stderr);
501 if (!zombie) {
502 zombie = 1;
503 write_crash_report(message);
504 end_packfile();
505 unkeep_all_packs();
506 dump_marks();
508 exit(128);
511 static void alloc_objects(unsigned int cnt)
513 struct object_entry_pool *b;
515 b = xmalloc(sizeof(struct object_entry_pool)
516 + cnt * sizeof(struct object_entry));
517 b->next_pool = blocks;
518 b->next_free = b->entries;
519 b->end = b->entries + cnt;
520 blocks = b;
521 alloc_count += cnt;
524 static struct object_entry *new_object(unsigned char *sha1)
526 struct object_entry *e;
528 if (blocks->next_free == blocks->end)
529 alloc_objects(object_entry_alloc);
531 e = blocks->next_free++;
532 hashcpy(e->idx.sha1, sha1);
533 return e;
536 static struct object_entry *find_object(unsigned char *sha1)
538 unsigned int h = sha1[0] << 8 | sha1[1];
539 struct object_entry *e;
540 for (e = object_table[h]; e; e = e->next)
541 if (!hashcmp(sha1, e->idx.sha1))
542 return e;
543 return NULL;
546 static struct object_entry *insert_object(unsigned char *sha1)
548 unsigned int h = sha1[0] << 8 | sha1[1];
549 struct object_entry *e = object_table[h];
550 struct object_entry *p = NULL;
552 while (e) {
553 if (!hashcmp(sha1, e->idx.sha1))
554 return e;
555 p = e;
556 e = e->next;
559 e = new_object(sha1);
560 e->next = NULL;
561 e->idx.offset = 0;
562 if (p)
563 p->next = e;
564 else
565 object_table[h] = e;
566 return e;
569 static unsigned int hc_str(const char *s, size_t len)
571 unsigned int r = 0;
572 while (len-- > 0)
573 r = r * 31 + *s++;
574 return r;
577 static void *pool_alloc(size_t len)
579 struct mem_pool *p;
580 void *r;
582 /* round up to a 'uintmax_t' alignment */
583 if (len & (sizeof(uintmax_t) - 1))
584 len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
586 for (p = mem_pool; p; p = p->next_pool)
587 if ((p->end - p->next_free >= len))
588 break;
590 if (!p) {
591 if (len >= (mem_pool_alloc/2)) {
592 total_allocd += len;
593 return xmalloc(len);
595 total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
596 p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
597 p->next_pool = mem_pool;
598 p->next_free = (char *) p->space;
599 p->end = p->next_free + mem_pool_alloc;
600 mem_pool = p;
603 r = p->next_free;
604 p->next_free += len;
605 return r;
608 static void *pool_calloc(size_t count, size_t size)
610 size_t len = count * size;
611 void *r = pool_alloc(len);
612 memset(r, 0, len);
613 return r;
616 static char *pool_strdup(const char *s)
618 char *r = pool_alloc(strlen(s) + 1);
619 strcpy(r, s);
620 return r;
623 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
625 struct mark_set *s = marks;
626 while ((idnum >> s->shift) >= 1024) {
627 s = pool_calloc(1, sizeof(struct mark_set));
628 s->shift = marks->shift + 10;
629 s->data.sets[0] = marks;
630 marks = s;
632 while (s->shift) {
633 uintmax_t i = idnum >> s->shift;
634 idnum -= i << s->shift;
635 if (!s->data.sets[i]) {
636 s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
637 s->data.sets[i]->shift = s->shift - 10;
639 s = s->data.sets[i];
641 if (!s->data.marked[idnum])
642 marks_set_count++;
643 s->data.marked[idnum] = oe;
646 static struct object_entry *find_mark(uintmax_t idnum)
648 uintmax_t orig_idnum = idnum;
649 struct mark_set *s = marks;
650 struct object_entry *oe = NULL;
651 if ((idnum >> s->shift) < 1024) {
652 while (s && s->shift) {
653 uintmax_t i = idnum >> s->shift;
654 idnum -= i << s->shift;
655 s = s->data.sets[i];
657 if (s)
658 oe = s->data.marked[idnum];
660 if (!oe)
661 die("mark :%" PRIuMAX " not declared", orig_idnum);
662 return oe;
665 static struct atom_str *to_atom(const char *s, unsigned short len)
667 unsigned int hc = hc_str(s, len) % atom_table_sz;
668 struct atom_str *c;
670 for (c = atom_table[hc]; c; c = c->next_atom)
671 if (c->str_len == len && !strncmp(s, c->str_dat, len))
672 return c;
674 c = pool_alloc(sizeof(struct atom_str) + len + 1);
675 c->str_len = len;
676 strncpy(c->str_dat, s, len);
677 c->str_dat[len] = 0;
678 c->next_atom = atom_table[hc];
679 atom_table[hc] = c;
680 atom_cnt++;
681 return c;
684 static struct branch *lookup_branch(const char *name)
686 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
687 struct branch *b;
689 for (b = branch_table[hc]; b; b = b->table_next_branch)
690 if (!strcmp(name, b->name))
691 return b;
692 return NULL;
695 static struct branch *new_branch(const char *name)
697 unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
698 struct branch *b = lookup_branch(name);
700 if (b)
701 die("Invalid attempt to create duplicate branch: %s", name);
702 switch (check_ref_format(name)) {
703 case 0: break; /* its valid */
704 case CHECK_REF_FORMAT_ONELEVEL:
705 break; /* valid, but too few '/', allow anyway */
706 default:
707 die("Branch name doesn't conform to GIT standards: %s", name);
710 b = pool_calloc(1, sizeof(struct branch));
711 b->name = pool_strdup(name);
712 b->table_next_branch = branch_table[hc];
713 b->branch_tree.versions[0].mode = S_IFDIR;
714 b->branch_tree.versions[1].mode = S_IFDIR;
715 b->num_notes = 0;
716 b->active = 0;
717 b->pack_id = MAX_PACK_ID;
718 branch_table[hc] = b;
719 branch_count++;
720 return b;
723 static unsigned int hc_entries(unsigned int cnt)
725 cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
726 return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
729 static struct tree_content *new_tree_content(unsigned int cnt)
731 struct avail_tree_content *f, *l = NULL;
732 struct tree_content *t;
733 unsigned int hc = hc_entries(cnt);
735 for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
736 if (f->entry_capacity >= cnt)
737 break;
739 if (f) {
740 if (l)
741 l->next_avail = f->next_avail;
742 else
743 avail_tree_table[hc] = f->next_avail;
744 } else {
745 cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
746 f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
747 f->entry_capacity = cnt;
750 t = (struct tree_content*)f;
751 t->entry_count = 0;
752 t->delta_depth = 0;
753 return t;
756 static void release_tree_entry(struct tree_entry *e);
757 static void release_tree_content(struct tree_content *t)
759 struct avail_tree_content *f = (struct avail_tree_content*)t;
760 unsigned int hc = hc_entries(f->entry_capacity);
761 f->next_avail = avail_tree_table[hc];
762 avail_tree_table[hc] = f;
765 static void release_tree_content_recursive(struct tree_content *t)
767 unsigned int i;
768 for (i = 0; i < t->entry_count; i++)
769 release_tree_entry(t->entries[i]);
770 release_tree_content(t);
773 static struct tree_content *grow_tree_content(
774 struct tree_content *t,
775 int amt)
777 struct tree_content *r = new_tree_content(t->entry_count + amt);
778 r->entry_count = t->entry_count;
779 r->delta_depth = t->delta_depth;
780 memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
781 release_tree_content(t);
782 return r;
785 static struct tree_entry *new_tree_entry(void)
787 struct tree_entry *e;
789 if (!avail_tree_entry) {
790 unsigned int n = tree_entry_alloc;
791 total_allocd += n * sizeof(struct tree_entry);
792 avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
793 while (n-- > 1) {
794 *((void**)e) = e + 1;
795 e++;
797 *((void**)e) = NULL;
800 e = avail_tree_entry;
801 avail_tree_entry = *((void**)e);
802 return e;
805 static void release_tree_entry(struct tree_entry *e)
807 if (e->tree)
808 release_tree_content_recursive(e->tree);
809 *((void**)e) = avail_tree_entry;
810 avail_tree_entry = e;
813 static struct tree_content *dup_tree_content(struct tree_content *s)
815 struct tree_content *d;
816 struct tree_entry *a, *b;
817 unsigned int i;
819 if (!s)
820 return NULL;
821 d = new_tree_content(s->entry_count);
822 for (i = 0; i < s->entry_count; i++) {
823 a = s->entries[i];
824 b = new_tree_entry();
825 memcpy(b, a, sizeof(*a));
826 if (a->tree && is_null_sha1(b->versions[1].sha1))
827 b->tree = dup_tree_content(a->tree);
828 else
829 b->tree = NULL;
830 d->entries[i] = b;
832 d->entry_count = s->entry_count;
833 d->delta_depth = s->delta_depth;
835 return d;
838 static void start_packfile(void)
840 static char tmpfile[PATH_MAX];
841 struct packed_git *p;
842 struct pack_header hdr;
843 int pack_fd;
845 pack_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
846 "pack/tmp_pack_XXXXXX");
847 p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
848 strcpy(p->pack_name, tmpfile);
849 p->pack_fd = pack_fd;
850 pack_file = sha1fd(pack_fd, p->pack_name);
852 hdr.hdr_signature = htonl(PACK_SIGNATURE);
853 hdr.hdr_version = htonl(2);
854 hdr.hdr_entries = 0;
855 sha1write(pack_file, &hdr, sizeof(hdr));
857 pack_data = p;
858 pack_size = sizeof(hdr);
859 object_count = 0;
861 all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
862 all_packs[pack_id] = p;
865 static const char *create_index(void)
867 const char *tmpfile;
868 struct pack_idx_entry **idx, **c, **last;
869 struct object_entry *e;
870 struct object_entry_pool *o;
872 /* Build the table of object IDs. */
873 idx = xmalloc(object_count * sizeof(*idx));
874 c = idx;
875 for (o = blocks; o; o = o->next_pool)
876 for (e = o->next_free; e-- != o->entries;)
877 if (pack_id == e->pack_id)
878 *c++ = &e->idx;
879 last = idx + object_count;
880 if (c != last)
881 die("internal consistency error creating the index");
883 tmpfile = write_idx_file(NULL, idx, object_count, pack_data->sha1);
884 free(idx);
885 return tmpfile;
888 static char *keep_pack(const char *curr_index_name)
890 static char name[PATH_MAX];
891 static const char *keep_msg = "fast-import";
892 int keep_fd;
894 keep_fd = odb_pack_keep(name, sizeof(name), pack_data->sha1);
895 if (keep_fd < 0)
896 die_errno("cannot create keep file");
897 write_or_die(keep_fd, keep_msg, strlen(keep_msg));
898 if (close(keep_fd))
899 die_errno("failed to write keep file");
901 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
902 get_object_directory(), sha1_to_hex(pack_data->sha1));
903 if (move_temp_to_file(pack_data->pack_name, name))
904 die("cannot store pack file");
906 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
907 get_object_directory(), sha1_to_hex(pack_data->sha1));
908 if (move_temp_to_file(curr_index_name, name))
909 die("cannot store index file");
910 free((void *)curr_index_name);
911 return name;
914 static void unkeep_all_packs(void)
916 static char name[PATH_MAX];
917 int k;
919 for (k = 0; k < pack_id; k++) {
920 struct packed_git *p = all_packs[k];
921 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
922 get_object_directory(), sha1_to_hex(p->sha1));
923 unlink_or_warn(name);
927 static void end_packfile(void)
929 struct packed_git *old_p = pack_data, *new_p;
931 clear_delta_base_cache();
932 if (object_count) {
933 unsigned char cur_pack_sha1[20];
934 char *idx_name;
935 int i;
936 struct branch *b;
937 struct tag *t;
939 close_pack_windows(pack_data);
940 sha1close(pack_file, cur_pack_sha1, 0);
941 fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
942 pack_data->pack_name, object_count,
943 cur_pack_sha1, pack_size);
944 close(pack_data->pack_fd);
945 idx_name = keep_pack(create_index());
947 /* Register the packfile with core git's machinery. */
948 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
949 if (!new_p)
950 die("core git rejected index %s", idx_name);
951 all_packs[pack_id] = new_p;
952 install_packed_git(new_p);
954 /* Print the boundary */
955 if (pack_edges) {
956 fprintf(pack_edges, "%s:", new_p->pack_name);
957 for (i = 0; i < branch_table_sz; i++) {
958 for (b = branch_table[i]; b; b = b->table_next_branch) {
959 if (b->pack_id == pack_id)
960 fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
963 for (t = first_tag; t; t = t->next_tag) {
964 if (t->pack_id == pack_id)
965 fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
967 fputc('\n', pack_edges);
968 fflush(pack_edges);
971 pack_id++;
973 else {
974 close(old_p->pack_fd);
975 unlink_or_warn(old_p->pack_name);
977 free(old_p);
979 /* We can't carry a delta across packfiles. */
980 strbuf_release(&last_blob.data);
981 last_blob.offset = 0;
982 last_blob.depth = 0;
985 static void cycle_packfile(void)
987 end_packfile();
988 start_packfile();
991 static int store_object(
992 enum object_type type,
993 struct strbuf *dat,
994 struct last_object *last,
995 unsigned char *sha1out,
996 uintmax_t mark)
998 void *out, *delta;
999 struct object_entry *e;
1000 unsigned char hdr[96];
1001 unsigned char sha1[20];
1002 unsigned long hdrlen, deltalen;
1003 git_SHA_CTX c;
1004 z_stream s;
1006 hdrlen = sprintf((char *)hdr,"%s %lu", typename(type),
1007 (unsigned long)dat->len) + 1;
1008 git_SHA1_Init(&c);
1009 git_SHA1_Update(&c, hdr, hdrlen);
1010 git_SHA1_Update(&c, dat->buf, dat->len);
1011 git_SHA1_Final(sha1, &c);
1012 if (sha1out)
1013 hashcpy(sha1out, sha1);
1015 e = insert_object(sha1);
1016 if (mark)
1017 insert_mark(mark, e);
1018 if (e->idx.offset) {
1019 duplicate_count_by_type[type]++;
1020 return 1;
1021 } else if (find_sha1_pack(sha1, packed_git)) {
1022 e->type = type;
1023 e->pack_id = MAX_PACK_ID;
1024 e->idx.offset = 1; /* just not zero! */
1025 duplicate_count_by_type[type]++;
1026 return 1;
1029 if (last && last->data.buf && last->depth < max_depth && dat->len > 20) {
1030 delta = diff_delta(last->data.buf, last->data.len,
1031 dat->buf, dat->len,
1032 &deltalen, dat->len - 20);
1033 } else
1034 delta = NULL;
1036 memset(&s, 0, sizeof(s));
1037 deflateInit(&s, pack_compression_level);
1038 if (delta) {
1039 s.next_in = delta;
1040 s.avail_in = deltalen;
1041 } else {
1042 s.next_in = (void *)dat->buf;
1043 s.avail_in = dat->len;
1045 s.avail_out = deflateBound(&s, s.avail_in);
1046 s.next_out = out = xmalloc(s.avail_out);
1047 while (deflate(&s, Z_FINISH) == Z_OK)
1048 /* nothing */;
1049 deflateEnd(&s);
1051 /* Determine if we should auto-checkpoint. */
1052 if ((max_packsize && (pack_size + 60 + s.total_out) > max_packsize)
1053 || (pack_size + 60 + s.total_out) < pack_size) {
1055 /* This new object needs to *not* have the current pack_id. */
1056 e->pack_id = pack_id + 1;
1057 cycle_packfile();
1059 /* We cannot carry a delta into the new pack. */
1060 if (delta) {
1061 free(delta);
1062 delta = NULL;
1064 memset(&s, 0, sizeof(s));
1065 deflateInit(&s, pack_compression_level);
1066 s.next_in = (void *)dat->buf;
1067 s.avail_in = dat->len;
1068 s.avail_out = deflateBound(&s, s.avail_in);
1069 s.next_out = out = xrealloc(out, s.avail_out);
1070 while (deflate(&s, Z_FINISH) == Z_OK)
1071 /* nothing */;
1072 deflateEnd(&s);
1076 e->type = type;
1077 e->pack_id = pack_id;
1078 e->idx.offset = pack_size;
1079 object_count++;
1080 object_count_by_type[type]++;
1082 crc32_begin(pack_file);
1084 if (delta) {
1085 off_t ofs = e->idx.offset - last->offset;
1086 unsigned pos = sizeof(hdr) - 1;
1088 delta_count_by_type[type]++;
1089 e->depth = last->depth + 1;
1091 hdrlen = encode_in_pack_object_header(OBJ_OFS_DELTA, deltalen, hdr);
1092 sha1write(pack_file, hdr, hdrlen);
1093 pack_size += hdrlen;
1095 hdr[pos] = ofs & 127;
1096 while (ofs >>= 7)
1097 hdr[--pos] = 128 | (--ofs & 127);
1098 sha1write(pack_file, hdr + pos, sizeof(hdr) - pos);
1099 pack_size += sizeof(hdr) - pos;
1100 } else {
1101 e->depth = 0;
1102 hdrlen = encode_in_pack_object_header(type, dat->len, hdr);
1103 sha1write(pack_file, hdr, hdrlen);
1104 pack_size += hdrlen;
1107 sha1write(pack_file, out, s.total_out);
1108 pack_size += s.total_out;
1110 e->idx.crc32 = crc32_end(pack_file);
1112 free(out);
1113 free(delta);
1114 if (last) {
1115 if (last->no_swap) {
1116 last->data = *dat;
1117 } else {
1118 strbuf_swap(&last->data, dat);
1120 last->offset = e->idx.offset;
1121 last->depth = e->depth;
1123 return 0;
1126 static void truncate_pack(off_t to, git_SHA_CTX *ctx)
1128 if (ftruncate(pack_data->pack_fd, to)
1129 || lseek(pack_data->pack_fd, to, SEEK_SET) != to)
1130 die_errno("cannot truncate pack to skip duplicate");
1131 pack_size = to;
1133 /* yes this is a layering violation */
1134 pack_file->total = to;
1135 pack_file->offset = 0;
1136 pack_file->ctx = *ctx;
1139 static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
1141 size_t in_sz = 64 * 1024, out_sz = 64 * 1024;
1142 unsigned char *in_buf = xmalloc(in_sz);
1143 unsigned char *out_buf = xmalloc(out_sz);
1144 struct object_entry *e;
1145 unsigned char sha1[20];
1146 unsigned long hdrlen;
1147 off_t offset;
1148 git_SHA_CTX c;
1149 git_SHA_CTX pack_file_ctx;
1150 z_stream s;
1151 int status = Z_OK;
1153 /* Determine if we should auto-checkpoint. */
1154 if ((max_packsize && (pack_size + 60 + len) > max_packsize)
1155 || (pack_size + 60 + len) < pack_size)
1156 cycle_packfile();
1158 offset = pack_size;
1160 /* preserve the pack_file SHA1 ctx in case we have to truncate later */
1161 sha1flush(pack_file);
1162 pack_file_ctx = pack_file->ctx;
1164 hdrlen = snprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
1165 if (out_sz <= hdrlen)
1166 die("impossibly large object header");
1168 git_SHA1_Init(&c);
1169 git_SHA1_Update(&c, out_buf, hdrlen);
1171 crc32_begin(pack_file);
1173 memset(&s, 0, sizeof(s));
1174 deflateInit(&s, pack_compression_level);
1176 hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
1177 if (out_sz <= hdrlen)
1178 die("impossibly large object header");
1180 s.next_out = out_buf + hdrlen;
1181 s.avail_out = out_sz - hdrlen;
1183 while (status != Z_STREAM_END) {
1184 if (0 < len && !s.avail_in) {
1185 size_t cnt = in_sz < len ? in_sz : (size_t)len;
1186 size_t n = fread(in_buf, 1, cnt, stdin);
1187 if (!n && feof(stdin))
1188 die("EOF in data (%" PRIuMAX " bytes remaining)", len);
1190 git_SHA1_Update(&c, in_buf, n);
1191 s.next_in = in_buf;
1192 s.avail_in = n;
1193 len -= n;
1196 status = deflate(&s, len ? 0 : Z_FINISH);
1198 if (!s.avail_out || status == Z_STREAM_END) {
1199 size_t n = s.next_out - out_buf;
1200 sha1write(pack_file, out_buf, n);
1201 pack_size += n;
1202 s.next_out = out_buf;
1203 s.avail_out = out_sz;
1206 switch (status) {
1207 case Z_OK:
1208 case Z_BUF_ERROR:
1209 case Z_STREAM_END:
1210 continue;
1211 default:
1212 die("unexpected deflate failure: %d", status);
1215 deflateEnd(&s);
1216 git_SHA1_Final(sha1, &c);
1218 if (sha1out)
1219 hashcpy(sha1out, sha1);
1221 e = insert_object(sha1);
1223 if (mark)
1224 insert_mark(mark, e);
1226 if (e->idx.offset) {
1227 duplicate_count_by_type[OBJ_BLOB]++;
1228 truncate_pack(offset, &pack_file_ctx);
1230 } else if (find_sha1_pack(sha1, packed_git)) {
1231 e->type = OBJ_BLOB;
1232 e->pack_id = MAX_PACK_ID;
1233 e->idx.offset = 1; /* just not zero! */
1234 duplicate_count_by_type[OBJ_BLOB]++;
1235 truncate_pack(offset, &pack_file_ctx);
1237 } else {
1238 e->depth = 0;
1239 e->type = OBJ_BLOB;
1240 e->pack_id = pack_id;
1241 e->idx.offset = offset;
1242 e->idx.crc32 = crc32_end(pack_file);
1243 object_count++;
1244 object_count_by_type[OBJ_BLOB]++;
1247 free(in_buf);
1248 free(out_buf);
1251 /* All calls must be guarded by find_object() or find_mark() to
1252 * ensure the 'struct object_entry' passed was written by this
1253 * process instance. We unpack the entry by the offset, avoiding
1254 * the need for the corresponding .idx file. This unpacking rule
1255 * works because we only use OBJ_REF_DELTA within the packfiles
1256 * created by fast-import.
1258 * oe must not be NULL. Such an oe usually comes from giving
1259 * an unknown SHA-1 to find_object() or an undefined mark to
1260 * find_mark(). Callers must test for this condition and use
1261 * the standard read_sha1_file() when it happens.
1263 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1264 * find_mark(), where the mark was reloaded from an existing marks
1265 * file and is referencing an object that this fast-import process
1266 * instance did not write out to a packfile. Callers must test for
1267 * this condition and use read_sha1_file() instead.
1269 static void *gfi_unpack_entry(
1270 struct object_entry *oe,
1271 unsigned long *sizep)
1273 enum object_type type;
1274 struct packed_git *p = all_packs[oe->pack_id];
1275 if (p == pack_data && p->pack_size < (pack_size + 20)) {
1276 /* The object is stored in the packfile we are writing to
1277 * and we have modified it since the last time we scanned
1278 * back to read a previously written object. If an old
1279 * window covered [p->pack_size, p->pack_size + 20) its
1280 * data is stale and is not valid. Closing all windows
1281 * and updating the packfile length ensures we can read
1282 * the newly written data.
1284 close_pack_windows(p);
1285 sha1flush(pack_file);
1287 /* We have to offer 20 bytes additional on the end of
1288 * the packfile as the core unpacker code assumes the
1289 * footer is present at the file end and must promise
1290 * at least 20 bytes within any window it maps. But
1291 * we don't actually create the footer here.
1293 p->pack_size = pack_size + 20;
1295 return unpack_entry(p, oe->idx.offset, &type, sizep);
1298 static const char *get_mode(const char *str, uint16_t *modep)
1300 unsigned char c;
1301 uint16_t mode = 0;
1303 while ((c = *str++) != ' ') {
1304 if (c < '0' || c > '7')
1305 return NULL;
1306 mode = (mode << 3) + (c - '0');
1308 *modep = mode;
1309 return str;
1312 static void load_tree(struct tree_entry *root)
1314 unsigned char *sha1 = root->versions[1].sha1;
1315 struct object_entry *myoe;
1316 struct tree_content *t;
1317 unsigned long size;
1318 char *buf;
1319 const char *c;
1321 root->tree = t = new_tree_content(8);
1322 if (is_null_sha1(sha1))
1323 return;
1325 myoe = find_object(sha1);
1326 if (myoe && myoe->pack_id != MAX_PACK_ID) {
1327 if (myoe->type != OBJ_TREE)
1328 die("Not a tree: %s", sha1_to_hex(sha1));
1329 t->delta_depth = myoe->depth;
1330 buf = gfi_unpack_entry(myoe, &size);
1331 if (!buf)
1332 die("Can't load tree %s", sha1_to_hex(sha1));
1333 } else {
1334 enum object_type type;
1335 buf = read_sha1_file(sha1, &type, &size);
1336 if (!buf || type != OBJ_TREE)
1337 die("Can't load tree %s", sha1_to_hex(sha1));
1340 c = buf;
1341 while (c != (buf + size)) {
1342 struct tree_entry *e = new_tree_entry();
1344 if (t->entry_count == t->entry_capacity)
1345 root->tree = t = grow_tree_content(t, t->entry_count);
1346 t->entries[t->entry_count++] = e;
1348 e->tree = NULL;
1349 c = get_mode(c, &e->versions[1].mode);
1350 if (!c)
1351 die("Corrupt mode in %s", sha1_to_hex(sha1));
1352 e->versions[0].mode = e->versions[1].mode;
1353 e->name = to_atom(c, strlen(c));
1354 c += e->name->str_len + 1;
1355 hashcpy(e->versions[0].sha1, (unsigned char *)c);
1356 hashcpy(e->versions[1].sha1, (unsigned char *)c);
1357 c += 20;
1359 free(buf);
1362 static int tecmp0 (const void *_a, const void *_b)
1364 struct tree_entry *a = *((struct tree_entry**)_a);
1365 struct tree_entry *b = *((struct tree_entry**)_b);
1366 return base_name_compare(
1367 a->name->str_dat, a->name->str_len, a->versions[0].mode,
1368 b->name->str_dat, b->name->str_len, b->versions[0].mode);
1371 static int tecmp1 (const void *_a, const void *_b)
1373 struct tree_entry *a = *((struct tree_entry**)_a);
1374 struct tree_entry *b = *((struct tree_entry**)_b);
1375 return base_name_compare(
1376 a->name->str_dat, a->name->str_len, a->versions[1].mode,
1377 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1380 static void mktree(struct tree_content *t, int v, struct strbuf *b)
1382 size_t maxlen = 0;
1383 unsigned int i;
1385 if (!v)
1386 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1387 else
1388 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1390 for (i = 0; i < t->entry_count; i++) {
1391 if (t->entries[i]->versions[v].mode)
1392 maxlen += t->entries[i]->name->str_len + 34;
1395 strbuf_reset(b);
1396 strbuf_grow(b, maxlen);
1397 for (i = 0; i < t->entry_count; i++) {
1398 struct tree_entry *e = t->entries[i];
1399 if (!e->versions[v].mode)
1400 continue;
1401 strbuf_addf(b, "%o %s%c", (unsigned int)e->versions[v].mode,
1402 e->name->str_dat, '\0');
1403 strbuf_add(b, e->versions[v].sha1, 20);
1407 static void store_tree(struct tree_entry *root)
1409 struct tree_content *t = root->tree;
1410 unsigned int i, j, del;
1411 struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
1412 struct object_entry *le;
1414 if (!is_null_sha1(root->versions[1].sha1))
1415 return;
1417 for (i = 0; i < t->entry_count; i++) {
1418 if (t->entries[i]->tree)
1419 store_tree(t->entries[i]);
1422 le = find_object(root->versions[0].sha1);
1423 if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
1424 mktree(t, 0, &old_tree);
1425 lo.data = old_tree;
1426 lo.offset = le->idx.offset;
1427 lo.depth = t->delta_depth;
1430 mktree(t, 1, &new_tree);
1431 store_object(OBJ_TREE, &new_tree, &lo, root->versions[1].sha1, 0);
1433 t->delta_depth = lo.depth;
1434 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1435 struct tree_entry *e = t->entries[i];
1436 if (e->versions[1].mode) {
1437 e->versions[0].mode = e->versions[1].mode;
1438 hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1439 t->entries[j++] = e;
1440 } else {
1441 release_tree_entry(e);
1442 del++;
1445 t->entry_count -= del;
1448 static int tree_content_set(
1449 struct tree_entry *root,
1450 const char *p,
1451 const unsigned char *sha1,
1452 const uint16_t mode,
1453 struct tree_content *subtree)
1455 struct tree_content *t = root->tree;
1456 const char *slash1;
1457 unsigned int i, n;
1458 struct tree_entry *e;
1460 slash1 = strchr(p, '/');
1461 if (slash1)
1462 n = slash1 - p;
1463 else
1464 n = strlen(p);
1465 if (!n)
1466 die("Empty path component found in input");
1467 if (!slash1 && !S_ISDIR(mode) && subtree)
1468 die("Non-directories cannot have subtrees");
1470 for (i = 0; i < t->entry_count; i++) {
1471 e = t->entries[i];
1472 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1473 if (!slash1) {
1474 if (!S_ISDIR(mode)
1475 && e->versions[1].mode == mode
1476 && !hashcmp(e->versions[1].sha1, sha1))
1477 return 0;
1478 e->versions[1].mode = mode;
1479 hashcpy(e->versions[1].sha1, sha1);
1480 if (e->tree)
1481 release_tree_content_recursive(e->tree);
1482 e->tree = subtree;
1483 hashclr(root->versions[1].sha1);
1484 return 1;
1486 if (!S_ISDIR(e->versions[1].mode)) {
1487 e->tree = new_tree_content(8);
1488 e->versions[1].mode = S_IFDIR;
1490 if (!e->tree)
1491 load_tree(e);
1492 if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
1493 hashclr(root->versions[1].sha1);
1494 return 1;
1496 return 0;
1500 if (t->entry_count == t->entry_capacity)
1501 root->tree = t = grow_tree_content(t, t->entry_count);
1502 e = new_tree_entry();
1503 e->name = to_atom(p, n);
1504 e->versions[0].mode = 0;
1505 hashclr(e->versions[0].sha1);
1506 t->entries[t->entry_count++] = e;
1507 if (slash1) {
1508 e->tree = new_tree_content(8);
1509 e->versions[1].mode = S_IFDIR;
1510 tree_content_set(e, slash1 + 1, sha1, mode, subtree);
1511 } else {
1512 e->tree = subtree;
1513 e->versions[1].mode = mode;
1514 hashcpy(e->versions[1].sha1, sha1);
1516 hashclr(root->versions[1].sha1);
1517 return 1;
1520 static int tree_content_remove(
1521 struct tree_entry *root,
1522 const char *p,
1523 struct tree_entry *backup_leaf)
1525 struct tree_content *t = root->tree;
1526 const char *slash1;
1527 unsigned int i, n;
1528 struct tree_entry *e;
1530 slash1 = strchr(p, '/');
1531 if (slash1)
1532 n = slash1 - p;
1533 else
1534 n = strlen(p);
1536 for (i = 0; i < t->entry_count; i++) {
1537 e = t->entries[i];
1538 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1539 if (slash1 && !S_ISDIR(e->versions[1].mode))
1541 * If p names a file in some subdirectory, and a
1542 * file or symlink matching the name of the
1543 * parent directory of p exists, then p cannot
1544 * exist and need not be deleted.
1546 return 1;
1547 if (!slash1 || !S_ISDIR(e->versions[1].mode))
1548 goto del_entry;
1549 if (!e->tree)
1550 load_tree(e);
1551 if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
1552 for (n = 0; n < e->tree->entry_count; n++) {
1553 if (e->tree->entries[n]->versions[1].mode) {
1554 hashclr(root->versions[1].sha1);
1555 return 1;
1558 backup_leaf = NULL;
1559 goto del_entry;
1561 return 0;
1564 return 0;
1566 del_entry:
1567 if (backup_leaf)
1568 memcpy(backup_leaf, e, sizeof(*backup_leaf));
1569 else if (e->tree)
1570 release_tree_content_recursive(e->tree);
1571 e->tree = NULL;
1572 e->versions[1].mode = 0;
1573 hashclr(e->versions[1].sha1);
1574 hashclr(root->versions[1].sha1);
1575 return 1;
1578 static int tree_content_get(
1579 struct tree_entry *root,
1580 const char *p,
1581 struct tree_entry *leaf)
1583 struct tree_content *t = root->tree;
1584 const char *slash1;
1585 unsigned int i, n;
1586 struct tree_entry *e;
1588 slash1 = strchr(p, '/');
1589 if (slash1)
1590 n = slash1 - p;
1591 else
1592 n = strlen(p);
1594 for (i = 0; i < t->entry_count; i++) {
1595 e = t->entries[i];
1596 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1597 if (!slash1) {
1598 memcpy(leaf, e, sizeof(*leaf));
1599 if (e->tree && is_null_sha1(e->versions[1].sha1))
1600 leaf->tree = dup_tree_content(e->tree);
1601 else
1602 leaf->tree = NULL;
1603 return 1;
1605 if (!S_ISDIR(e->versions[1].mode))
1606 return 0;
1607 if (!e->tree)
1608 load_tree(e);
1609 return tree_content_get(e, slash1 + 1, leaf);
1612 return 0;
1615 static int update_branch(struct branch *b)
1617 static const char *msg = "fast-import";
1618 struct ref_lock *lock;
1619 unsigned char old_sha1[20];
1621 if (is_null_sha1(b->sha1))
1622 return 0;
1623 if (read_ref(b->name, old_sha1))
1624 hashclr(old_sha1);
1625 lock = lock_any_ref_for_update(b->name, old_sha1, 0);
1626 if (!lock)
1627 return error("Unable to lock %s", b->name);
1628 if (!force_update && !is_null_sha1(old_sha1)) {
1629 struct commit *old_cmit, *new_cmit;
1631 old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1632 new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1633 if (!old_cmit || !new_cmit) {
1634 unlock_ref(lock);
1635 return error("Branch %s is missing commits.", b->name);
1638 if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
1639 unlock_ref(lock);
1640 warning("Not updating %s"
1641 " (new tip %s does not contain %s)",
1642 b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1643 return -1;
1646 if (write_ref_sha1(lock, b->sha1, msg) < 0)
1647 return error("Unable to update %s", b->name);
1648 return 0;
1651 static void dump_branches(void)
1653 unsigned int i;
1654 struct branch *b;
1656 for (i = 0; i < branch_table_sz; i++) {
1657 for (b = branch_table[i]; b; b = b->table_next_branch)
1658 failure |= update_branch(b);
1662 static void dump_tags(void)
1664 static const char *msg = "fast-import";
1665 struct tag *t;
1666 struct ref_lock *lock;
1667 char ref_name[PATH_MAX];
1669 for (t = first_tag; t; t = t->next_tag) {
1670 sprintf(ref_name, "tags/%s", t->name);
1671 lock = lock_ref_sha1(ref_name, NULL);
1672 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1673 failure |= error("Unable to update %s", ref_name);
1677 static void dump_marks_helper(FILE *f,
1678 uintmax_t base,
1679 struct mark_set *m)
1681 uintmax_t k;
1682 if (m->shift) {
1683 for (k = 0; k < 1024; k++) {
1684 if (m->data.sets[k])
1685 dump_marks_helper(f, base + (k << m->shift),
1686 m->data.sets[k]);
1688 } else {
1689 for (k = 0; k < 1024; k++) {
1690 if (m->data.marked[k])
1691 fprintf(f, ":%" PRIuMAX " %s\n", base + k,
1692 sha1_to_hex(m->data.marked[k]->idx.sha1));
1697 static void dump_marks(void)
1699 static struct lock_file mark_lock;
1700 int mark_fd;
1701 FILE *f;
1703 if (!export_marks_file)
1704 return;
1706 mark_fd = hold_lock_file_for_update(&mark_lock, export_marks_file, 0);
1707 if (mark_fd < 0) {
1708 failure |= error("Unable to write marks file %s: %s",
1709 export_marks_file, strerror(errno));
1710 return;
1713 f = fdopen(mark_fd, "w");
1714 if (!f) {
1715 int saved_errno = errno;
1716 rollback_lock_file(&mark_lock);
1717 failure |= error("Unable to write marks file %s: %s",
1718 export_marks_file, strerror(saved_errno));
1719 return;
1723 * Since the lock file was fdopen()'ed, it should not be close()'ed.
1724 * Assign -1 to the lock file descriptor so that commit_lock_file()
1725 * won't try to close() it.
1727 mark_lock.fd = -1;
1729 dump_marks_helper(f, 0, marks);
1730 if (ferror(f) || fclose(f)) {
1731 int saved_errno = errno;
1732 rollback_lock_file(&mark_lock);
1733 failure |= error("Unable to write marks file %s: %s",
1734 export_marks_file, strerror(saved_errno));
1735 return;
1738 if (commit_lock_file(&mark_lock)) {
1739 int saved_errno = errno;
1740 rollback_lock_file(&mark_lock);
1741 failure |= error("Unable to commit marks file %s: %s",
1742 export_marks_file, strerror(saved_errno));
1743 return;
1747 static void read_marks(void)
1749 char line[512];
1750 FILE *f = fopen(import_marks_file, "r");
1751 if (!f)
1752 die_errno("cannot read '%s'", import_marks_file);
1753 while (fgets(line, sizeof(line), f)) {
1754 uintmax_t mark;
1755 char *end;
1756 unsigned char sha1[20];
1757 struct object_entry *e;
1759 end = strchr(line, '\n');
1760 if (line[0] != ':' || !end)
1761 die("corrupt mark line: %s", line);
1762 *end = 0;
1763 mark = strtoumax(line + 1, &end, 10);
1764 if (!mark || end == line + 1
1765 || *end != ' ' || get_sha1(end + 1, sha1))
1766 die("corrupt mark line: %s", line);
1767 e = find_object(sha1);
1768 if (!e) {
1769 enum object_type type = sha1_object_info(sha1, NULL);
1770 if (type < 0)
1771 die("object not found: %s", sha1_to_hex(sha1));
1772 e = insert_object(sha1);
1773 e->type = type;
1774 e->pack_id = MAX_PACK_ID;
1775 e->idx.offset = 1; /* just not zero! */
1777 insert_mark(mark, e);
1779 fclose(f);
1782 static int read_next_command(void)
1784 static int stdin_eof = 0;
1786 if (stdin_eof) {
1787 unread_command_buf = 0;
1788 return EOF;
1791 for (;;) {
1792 if (unread_command_buf) {
1793 unread_command_buf = 0;
1794 } else {
1795 struct recent_command *rc;
1797 strbuf_detach(&command_buf, NULL);
1798 stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
1799 if (stdin_eof)
1800 return EOF;
1802 if (!seen_data_command
1803 && prefixcmp(command_buf.buf, "feature ")
1804 && prefixcmp(command_buf.buf, "option ")) {
1805 parse_argv();
1808 rc = rc_free;
1809 if (rc)
1810 rc_free = rc->next;
1811 else {
1812 rc = cmd_hist.next;
1813 cmd_hist.next = rc->next;
1814 cmd_hist.next->prev = &cmd_hist;
1815 free(rc->buf);
1818 rc->buf = command_buf.buf;
1819 rc->prev = cmd_tail;
1820 rc->next = cmd_hist.prev;
1821 rc->prev->next = rc;
1822 cmd_tail = rc;
1824 if (!prefixcmp(command_buf.buf, "cat ")) {
1825 parse_cat_request();
1826 continue;
1828 if (command_buf.buf[0] != '#')
1829 return 0;
1832 return 0;
1835 static void skip_optional_lf(void)
1837 int term_char = fgetc(stdin);
1838 if (term_char != '\n' && term_char != EOF)
1839 ungetc(term_char, stdin);
1842 static void parse_mark(void)
1844 if (!prefixcmp(command_buf.buf, "mark :")) {
1845 next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1846 read_next_command();
1848 else
1849 next_mark = 0;
1852 static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
1854 strbuf_reset(sb);
1856 if (prefixcmp(command_buf.buf, "data "))
1857 die("Expected 'data n' command, found: %s", command_buf.buf);
1859 if (!prefixcmp(command_buf.buf + 5, "<<")) {
1860 char *term = xstrdup(command_buf.buf + 5 + 2);
1861 size_t term_len = command_buf.len - 5 - 2;
1863 strbuf_detach(&command_buf, NULL);
1864 for (;;) {
1865 if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
1866 die("EOF in data (terminator '%s' not found)", term);
1867 if (term_len == command_buf.len
1868 && !strcmp(term, command_buf.buf))
1869 break;
1870 strbuf_addbuf(sb, &command_buf);
1871 strbuf_addch(sb, '\n');
1873 free(term);
1875 else {
1876 uintmax_t len = strtoumax(command_buf.buf + 5, NULL, 10);
1877 size_t n = 0, length = (size_t)len;
1879 if (limit && limit < len) {
1880 *len_res = len;
1881 return 0;
1883 if (length < len)
1884 die("data is too large to use in this context");
1886 while (n < length) {
1887 size_t s = strbuf_fread(sb, length - n, stdin);
1888 if (!s && feof(stdin))
1889 die("EOF in data (%lu bytes remaining)",
1890 (unsigned long)(length - n));
1891 n += s;
1895 skip_optional_lf();
1896 return 1;
1899 static int validate_raw_date(const char *src, char *result, int maxlen)
1901 const char *orig_src = src;
1902 char *endp;
1903 unsigned long num;
1905 errno = 0;
1907 num = strtoul(src, &endp, 10);
1908 /* NEEDSWORK: perhaps check for reasonable values? */
1909 if (errno || endp == src || *endp != ' ')
1910 return -1;
1912 src = endp + 1;
1913 if (*src != '-' && *src != '+')
1914 return -1;
1916 num = strtoul(src + 1, &endp, 10);
1917 if (errno || endp == src + 1 || *endp || (endp - orig_src) >= maxlen ||
1918 1400 < num)
1919 return -1;
1921 strcpy(result, orig_src);
1922 return 0;
1925 static char *parse_ident(const char *buf)
1927 const char *gt;
1928 size_t name_len;
1929 char *ident;
1931 gt = strrchr(buf, '>');
1932 if (!gt)
1933 die("Missing > in ident string: %s", buf);
1934 gt++;
1935 if (*gt != ' ')
1936 die("Missing space after > in ident string: %s", buf);
1937 gt++;
1938 name_len = gt - buf;
1939 ident = xmalloc(name_len + 24);
1940 strncpy(ident, buf, name_len);
1942 switch (whenspec) {
1943 case WHENSPEC_RAW:
1944 if (validate_raw_date(gt, ident + name_len, 24) < 0)
1945 die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1946 break;
1947 case WHENSPEC_RFC2822:
1948 if (parse_date(gt, ident + name_len, 24) < 0)
1949 die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1950 break;
1951 case WHENSPEC_NOW:
1952 if (strcmp("now", gt))
1953 die("Date in ident must be 'now': %s", buf);
1954 datestamp(ident + name_len, 24);
1955 break;
1958 return ident;
1961 static void parse_and_store_blob(
1962 struct last_object *last,
1963 unsigned char *sha1out,
1964 uintmax_t mark)
1966 static struct strbuf buf = STRBUF_INIT;
1967 uintmax_t len;
1969 if (parse_data(&buf, big_file_threshold, &len))
1970 store_object(OBJ_BLOB, &buf, last, sha1out, mark);
1971 else {
1972 if (last) {
1973 strbuf_release(&last->data);
1974 last->offset = 0;
1975 last->depth = 0;
1977 stream_blob(len, sha1out, mark);
1978 skip_optional_lf();
1982 static void parse_new_blob(void)
1984 read_next_command();
1985 parse_mark();
1986 parse_and_store_blob(&last_blob, NULL, next_mark);
1989 static void unload_one_branch(void)
1991 while (cur_active_branches
1992 && cur_active_branches >= max_active_branches) {
1993 uintmax_t min_commit = ULONG_MAX;
1994 struct branch *e, *l = NULL, *p = NULL;
1996 for (e = active_branches; e; e = e->active_next_branch) {
1997 if (e->last_commit < min_commit) {
1998 p = l;
1999 min_commit = e->last_commit;
2001 l = e;
2004 if (p) {
2005 e = p->active_next_branch;
2006 p->active_next_branch = e->active_next_branch;
2007 } else {
2008 e = active_branches;
2009 active_branches = e->active_next_branch;
2011 e->active = 0;
2012 e->active_next_branch = NULL;
2013 if (e->branch_tree.tree) {
2014 release_tree_content_recursive(e->branch_tree.tree);
2015 e->branch_tree.tree = NULL;
2017 cur_active_branches--;
2021 static void load_branch(struct branch *b)
2023 load_tree(&b->branch_tree);
2024 if (!b->active) {
2025 b->active = 1;
2026 b->active_next_branch = active_branches;
2027 active_branches = b;
2028 cur_active_branches++;
2029 branch_load_count++;
2033 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes)
2035 unsigned char fanout = 0;
2036 while ((num_notes >>= 8))
2037 fanout++;
2038 return fanout;
2041 static void construct_path_with_fanout(const char *hex_sha1,
2042 unsigned char fanout, char *path)
2044 unsigned int i = 0, j = 0;
2045 if (fanout >= 20)
2046 die("Too large fanout (%u)", fanout);
2047 while (fanout) {
2048 path[i++] = hex_sha1[j++];
2049 path[i++] = hex_sha1[j++];
2050 path[i++] = '/';
2051 fanout--;
2053 memcpy(path + i, hex_sha1 + j, 40 - j);
2054 path[i + 40 - j] = '\0';
2057 static uintmax_t do_change_note_fanout(
2058 struct tree_entry *orig_root, struct tree_entry *root,
2059 char *hex_sha1, unsigned int hex_sha1_len,
2060 char *fullpath, unsigned int fullpath_len,
2061 unsigned char fanout)
2063 struct tree_content *t = root->tree;
2064 struct tree_entry *e, leaf;
2065 unsigned int i, tmp_hex_sha1_len, tmp_fullpath_len;
2066 uintmax_t num_notes = 0;
2067 unsigned char sha1[20];
2068 char realpath[60];
2070 for (i = 0; t && i < t->entry_count; i++) {
2071 e = t->entries[i];
2072 tmp_hex_sha1_len = hex_sha1_len + e->name->str_len;
2073 tmp_fullpath_len = fullpath_len;
2076 * We're interested in EITHER existing note entries (entries
2077 * with exactly 40 hex chars in path, not including directory
2078 * separators), OR directory entries that may contain note
2079 * entries (with < 40 hex chars in path).
2080 * Also, each path component in a note entry must be a multiple
2081 * of 2 chars.
2083 if (!e->versions[1].mode ||
2084 tmp_hex_sha1_len > 40 ||
2085 e->name->str_len % 2)
2086 continue;
2088 /* This _may_ be a note entry, or a subdir containing notes */
2089 memcpy(hex_sha1 + hex_sha1_len, e->name->str_dat,
2090 e->name->str_len);
2091 if (tmp_fullpath_len)
2092 fullpath[tmp_fullpath_len++] = '/';
2093 memcpy(fullpath + tmp_fullpath_len, e->name->str_dat,
2094 e->name->str_len);
2095 tmp_fullpath_len += e->name->str_len;
2096 fullpath[tmp_fullpath_len] = '\0';
2098 if (tmp_hex_sha1_len == 40 && !get_sha1_hex(hex_sha1, sha1)) {
2099 /* This is a note entry */
2100 construct_path_with_fanout(hex_sha1, fanout, realpath);
2101 if (!strcmp(fullpath, realpath)) {
2102 /* Note entry is in correct location */
2103 num_notes++;
2104 continue;
2107 /* Rename fullpath to realpath */
2108 if (!tree_content_remove(orig_root, fullpath, &leaf))
2109 die("Failed to remove path %s", fullpath);
2110 tree_content_set(orig_root, realpath,
2111 leaf.versions[1].sha1,
2112 leaf.versions[1].mode,
2113 leaf.tree);
2114 } else if (S_ISDIR(e->versions[1].mode)) {
2115 /* This is a subdir that may contain note entries */
2116 if (!e->tree)
2117 load_tree(e);
2118 num_notes += do_change_note_fanout(orig_root, e,
2119 hex_sha1, tmp_hex_sha1_len,
2120 fullpath, tmp_fullpath_len, fanout);
2123 /* The above may have reallocated the current tree_content */
2124 t = root->tree;
2126 return num_notes;
2129 static uintmax_t change_note_fanout(struct tree_entry *root,
2130 unsigned char fanout)
2132 char hex_sha1[40], path[60];
2133 return do_change_note_fanout(root, root, hex_sha1, 0, path, 0, fanout);
2136 static void file_change_m(struct branch *b)
2138 const char *p = command_buf.buf + 2;
2139 static struct strbuf uq = STRBUF_INIT;
2140 const char *endp;
2141 struct object_entry *oe = oe;
2142 unsigned char sha1[20];
2143 uint16_t mode, inline_data = 0;
2145 p = get_mode(p, &mode);
2146 if (!p)
2147 die("Corrupt mode: %s", command_buf.buf);
2148 switch (mode) {
2149 case 0644:
2150 case 0755:
2151 mode |= S_IFREG;
2152 case S_IFREG | 0644:
2153 case S_IFREG | 0755:
2154 case S_IFLNK:
2155 case S_IFDIR:
2156 case S_IFGITLINK:
2157 /* ok */
2158 break;
2159 default:
2160 die("Corrupt mode: %s", command_buf.buf);
2163 if (*p == ':') {
2164 char *x;
2165 oe = find_mark(strtoumax(p + 1, &x, 10));
2166 hashcpy(sha1, oe->idx.sha1);
2167 p = x;
2168 } else if (!prefixcmp(p, "inline")) {
2169 inline_data = 1;
2170 p += 6;
2171 } else {
2172 if (get_sha1_hex(p, sha1))
2173 die("Invalid SHA1: %s", command_buf.buf);
2174 oe = find_object(sha1);
2175 p += 40;
2177 if (*p++ != ' ')
2178 die("Missing space after SHA1: %s", command_buf.buf);
2180 strbuf_reset(&uq);
2181 if (!unquote_c_style(&uq, p, &endp)) {
2182 if (*endp)
2183 die("Garbage after path in: %s", command_buf.buf);
2184 p = uq.buf;
2187 if (S_ISGITLINK(mode)) {
2188 if (inline_data)
2189 die("Git links cannot be specified 'inline': %s",
2190 command_buf.buf);
2191 else if (oe) {
2192 if (oe->type != OBJ_COMMIT)
2193 die("Not a commit (actually a %s): %s",
2194 typename(oe->type), command_buf.buf);
2197 * Accept the sha1 without checking; it expected to be in
2198 * another repository.
2200 } else if (inline_data) {
2201 if (S_ISDIR(mode))
2202 die("Directories cannot be specified 'inline': %s",
2203 command_buf.buf);
2204 if (p != uq.buf) {
2205 strbuf_addstr(&uq, p);
2206 p = uq.buf;
2208 read_next_command();
2209 parse_and_store_blob(&last_blob, sha1, 0);
2210 } else {
2211 enum object_type expected = S_ISDIR(mode) ?
2212 OBJ_TREE: OBJ_BLOB;
2213 enum object_type type = oe ? oe->type :
2214 sha1_object_info(sha1, NULL);
2215 if (type < 0)
2216 die("%s not found: %s",
2217 S_ISDIR(mode) ? "Tree" : "Blob",
2218 command_buf.buf);
2219 if (type != expected)
2220 die("Not a %s (actually a %s): %s",
2221 typename(expected), typename(type),
2222 command_buf.buf);
2225 tree_content_set(&b->branch_tree, p, sha1, mode, NULL);
2228 static void file_change_d(struct branch *b)
2230 const char *p = command_buf.buf + 2;
2231 static struct strbuf uq = STRBUF_INIT;
2232 const char *endp;
2234 strbuf_reset(&uq);
2235 if (!unquote_c_style(&uq, p, &endp)) {
2236 if (*endp)
2237 die("Garbage after path in: %s", command_buf.buf);
2238 p = uq.buf;
2240 tree_content_remove(&b->branch_tree, p, NULL);
2243 static void file_change_cr(struct branch *b, int rename)
2245 const char *s, *d;
2246 static struct strbuf s_uq = STRBUF_INIT;
2247 static struct strbuf d_uq = STRBUF_INIT;
2248 const char *endp;
2249 struct tree_entry leaf;
2251 s = command_buf.buf + 2;
2252 strbuf_reset(&s_uq);
2253 if (!unquote_c_style(&s_uq, s, &endp)) {
2254 if (*endp != ' ')
2255 die("Missing space after source: %s", command_buf.buf);
2256 } else {
2257 endp = strchr(s, ' ');
2258 if (!endp)
2259 die("Missing space after source: %s", command_buf.buf);
2260 strbuf_add(&s_uq, s, endp - s);
2262 s = s_uq.buf;
2264 endp++;
2265 if (!*endp)
2266 die("Missing dest: %s", command_buf.buf);
2268 d = endp;
2269 strbuf_reset(&d_uq);
2270 if (!unquote_c_style(&d_uq, d, &endp)) {
2271 if (*endp)
2272 die("Garbage after dest in: %s", command_buf.buf);
2273 d = d_uq.buf;
2276 memset(&leaf, 0, sizeof(leaf));
2277 if (rename)
2278 tree_content_remove(&b->branch_tree, s, &leaf);
2279 else
2280 tree_content_get(&b->branch_tree, s, &leaf);
2281 if (!leaf.versions[1].mode)
2282 die("Path %s not in branch", s);
2283 tree_content_set(&b->branch_tree, d,
2284 leaf.versions[1].sha1,
2285 leaf.versions[1].mode,
2286 leaf.tree);
2289 static void note_change_n(struct branch *b, unsigned char old_fanout)
2291 const char *p = command_buf.buf + 2;
2292 static struct strbuf uq = STRBUF_INIT;
2293 struct object_entry *oe = oe;
2294 struct branch *s;
2295 unsigned char sha1[20], commit_sha1[20];
2296 char path[60];
2297 uint16_t inline_data = 0;
2298 unsigned char new_fanout;
2300 /* <dataref> or 'inline' */
2301 if (*p == ':') {
2302 char *x;
2303 oe = find_mark(strtoumax(p + 1, &x, 10));
2304 hashcpy(sha1, oe->idx.sha1);
2305 p = x;
2306 } else if (!prefixcmp(p, "inline")) {
2307 inline_data = 1;
2308 p += 6;
2309 } else {
2310 if (get_sha1_hex(p, sha1))
2311 die("Invalid SHA1: %s", command_buf.buf);
2312 oe = find_object(sha1);
2313 p += 40;
2315 if (*p++ != ' ')
2316 die("Missing space after SHA1: %s", command_buf.buf);
2318 /* <committish> */
2319 s = lookup_branch(p);
2320 if (s) {
2321 hashcpy(commit_sha1, s->sha1);
2322 } else if (*p == ':') {
2323 uintmax_t commit_mark = strtoumax(p + 1, NULL, 10);
2324 struct object_entry *commit_oe = find_mark(commit_mark);
2325 if (commit_oe->type != OBJ_COMMIT)
2326 die("Mark :%" PRIuMAX " not a commit", commit_mark);
2327 hashcpy(commit_sha1, commit_oe->idx.sha1);
2328 } else if (!get_sha1(p, commit_sha1)) {
2329 unsigned long size;
2330 char *buf = read_object_with_reference(commit_sha1,
2331 commit_type, &size, commit_sha1);
2332 if (!buf || size < 46)
2333 die("Not a valid commit: %s", p);
2334 free(buf);
2335 } else
2336 die("Invalid ref name or SHA1 expression: %s", p);
2338 if (inline_data) {
2339 if (p != uq.buf) {
2340 strbuf_addstr(&uq, p);
2341 p = uq.buf;
2343 read_next_command();
2344 parse_and_store_blob(&last_blob, sha1, 0);
2345 } else if (oe) {
2346 if (oe->type != OBJ_BLOB)
2347 die("Not a blob (actually a %s): %s",
2348 typename(oe->type), command_buf.buf);
2349 } else if (!is_null_sha1(sha1)) {
2350 enum object_type type = sha1_object_info(sha1, NULL);
2351 if (type < 0)
2352 die("Blob not found: %s", command_buf.buf);
2353 if (type != OBJ_BLOB)
2354 die("Not a blob (actually a %s): %s",
2355 typename(type), command_buf.buf);
2358 construct_path_with_fanout(sha1_to_hex(commit_sha1), old_fanout, path);
2359 if (tree_content_remove(&b->branch_tree, path, NULL))
2360 b->num_notes--;
2362 if (is_null_sha1(sha1))
2363 return; /* nothing to insert */
2365 b->num_notes++;
2366 new_fanout = convert_num_notes_to_fanout(b->num_notes);
2367 construct_path_with_fanout(sha1_to_hex(commit_sha1), new_fanout, path);
2368 tree_content_set(&b->branch_tree, path, sha1, S_IFREG | 0644, NULL);
2371 static void file_change_deleteall(struct branch *b)
2373 release_tree_content_recursive(b->branch_tree.tree);
2374 hashclr(b->branch_tree.versions[0].sha1);
2375 hashclr(b->branch_tree.versions[1].sha1);
2376 load_tree(&b->branch_tree);
2377 b->num_notes = 0;
2380 static void parse_from_commit(struct branch *b, char *buf, unsigned long size)
2382 if (!buf || size < 46)
2383 die("Not a valid commit: %s", sha1_to_hex(b->sha1));
2384 if (memcmp("tree ", buf, 5)
2385 || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
2386 die("The commit %s is corrupt", sha1_to_hex(b->sha1));
2387 hashcpy(b->branch_tree.versions[0].sha1,
2388 b->branch_tree.versions[1].sha1);
2391 static void parse_from_existing(struct branch *b)
2393 if (is_null_sha1(b->sha1)) {
2394 hashclr(b->branch_tree.versions[0].sha1);
2395 hashclr(b->branch_tree.versions[1].sha1);
2396 } else {
2397 unsigned long size;
2398 char *buf;
2400 buf = read_object_with_reference(b->sha1,
2401 commit_type, &size, b->sha1);
2402 parse_from_commit(b, buf, size);
2403 free(buf);
2407 static int parse_from(struct branch *b)
2409 const char *from;
2410 struct branch *s;
2412 if (prefixcmp(command_buf.buf, "from "))
2413 return 0;
2415 if (b->branch_tree.tree) {
2416 release_tree_content_recursive(b->branch_tree.tree);
2417 b->branch_tree.tree = NULL;
2420 from = strchr(command_buf.buf, ' ') + 1;
2421 s = lookup_branch(from);
2422 if (b == s)
2423 die("Can't create a branch from itself: %s", b->name);
2424 else if (s) {
2425 unsigned char *t = s->branch_tree.versions[1].sha1;
2426 hashcpy(b->sha1, s->sha1);
2427 hashcpy(b->branch_tree.versions[0].sha1, t);
2428 hashcpy(b->branch_tree.versions[1].sha1, t);
2429 } else if (*from == ':') {
2430 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
2431 struct object_entry *oe = find_mark(idnum);
2432 if (oe->type != OBJ_COMMIT)
2433 die("Mark :%" PRIuMAX " not a commit", idnum);
2434 hashcpy(b->sha1, oe->idx.sha1);
2435 if (oe->pack_id != MAX_PACK_ID) {
2436 unsigned long size;
2437 char *buf = gfi_unpack_entry(oe, &size);
2438 parse_from_commit(b, buf, size);
2439 free(buf);
2440 } else
2441 parse_from_existing(b);
2442 } else if (!get_sha1(from, b->sha1))
2443 parse_from_existing(b);
2444 else
2445 die("Invalid ref name or SHA1 expression: %s", from);
2447 read_next_command();
2448 return 1;
2451 static struct hash_list *parse_merge(unsigned int *count)
2453 struct hash_list *list = NULL, *n, *e = e;
2454 const char *from;
2455 struct branch *s;
2457 *count = 0;
2458 while (!prefixcmp(command_buf.buf, "merge ")) {
2459 from = strchr(command_buf.buf, ' ') + 1;
2460 n = xmalloc(sizeof(*n));
2461 s = lookup_branch(from);
2462 if (s)
2463 hashcpy(n->sha1, s->sha1);
2464 else if (*from == ':') {
2465 uintmax_t idnum = strtoumax(from + 1, NULL, 10);
2466 struct object_entry *oe = find_mark(idnum);
2467 if (oe->type != OBJ_COMMIT)
2468 die("Mark :%" PRIuMAX " not a commit", idnum);
2469 hashcpy(n->sha1, oe->idx.sha1);
2470 } else if (!get_sha1(from, n->sha1)) {
2471 unsigned long size;
2472 char *buf = read_object_with_reference(n->sha1,
2473 commit_type, &size, n->sha1);
2474 if (!buf || size < 46)
2475 die("Not a valid commit: %s", from);
2476 free(buf);
2477 } else
2478 die("Invalid ref name or SHA1 expression: %s", from);
2480 n->next = NULL;
2481 if (list)
2482 e->next = n;
2483 else
2484 list = n;
2485 e = n;
2486 (*count)++;
2487 read_next_command();
2489 return list;
2492 static void parse_new_commit(void)
2494 static struct strbuf msg = STRBUF_INIT;
2495 struct branch *b;
2496 char *sp;
2497 char *author = NULL;
2498 char *committer = NULL;
2499 struct hash_list *merge_list = NULL;
2500 unsigned int merge_count;
2501 unsigned char prev_fanout, new_fanout;
2503 /* Obtain the branch name from the rest of our command */
2504 sp = strchr(command_buf.buf, ' ') + 1;
2505 b = lookup_branch(sp);
2506 if (!b)
2507 b = new_branch(sp);
2509 read_next_command();
2510 parse_mark();
2511 if (!prefixcmp(command_buf.buf, "author ")) {
2512 author = parse_ident(command_buf.buf + 7);
2513 read_next_command();
2515 if (!prefixcmp(command_buf.buf, "committer ")) {
2516 committer = parse_ident(command_buf.buf + 10);
2517 read_next_command();
2519 if (!committer)
2520 die("Expected committer but didn't get one");
2521 parse_data(&msg, 0, NULL);
2522 read_next_command();
2523 parse_from(b);
2524 merge_list = parse_merge(&merge_count);
2526 /* ensure the branch is active/loaded */
2527 if (!b->branch_tree.tree || !max_active_branches) {
2528 unload_one_branch();
2529 load_branch(b);
2532 prev_fanout = convert_num_notes_to_fanout(b->num_notes);
2534 /* file_change* */
2535 while (command_buf.len > 0) {
2536 if (!prefixcmp(command_buf.buf, "M "))
2537 file_change_m(b);
2538 else if (!prefixcmp(command_buf.buf, "D "))
2539 file_change_d(b);
2540 else if (!prefixcmp(command_buf.buf, "R "))
2541 file_change_cr(b, 1);
2542 else if (!prefixcmp(command_buf.buf, "C "))
2543 file_change_cr(b, 0);
2544 else if (!prefixcmp(command_buf.buf, "N "))
2545 note_change_n(b, prev_fanout);
2546 else if (!strcmp("deleteall", command_buf.buf))
2547 file_change_deleteall(b);
2548 else {
2549 unread_command_buf = 1;
2550 break;
2552 if (read_next_command() == EOF)
2553 break;
2556 new_fanout = convert_num_notes_to_fanout(b->num_notes);
2557 if (new_fanout != prev_fanout)
2558 b->num_notes = change_note_fanout(&b->branch_tree, new_fanout);
2560 /* build the tree and the commit */
2561 store_tree(&b->branch_tree);
2562 hashcpy(b->branch_tree.versions[0].sha1,
2563 b->branch_tree.versions[1].sha1);
2565 strbuf_reset(&new_data);
2566 strbuf_addf(&new_data, "tree %s\n",
2567 sha1_to_hex(b->branch_tree.versions[1].sha1));
2568 if (!is_null_sha1(b->sha1))
2569 strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(b->sha1));
2570 while (merge_list) {
2571 struct hash_list *next = merge_list->next;
2572 strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(merge_list->sha1));
2573 free(merge_list);
2574 merge_list = next;
2576 strbuf_addf(&new_data,
2577 "author %s\n"
2578 "committer %s\n"
2579 "\n",
2580 author ? author : committer, committer);
2581 strbuf_addbuf(&new_data, &msg);
2582 free(author);
2583 free(committer);
2585 if (!store_object(OBJ_COMMIT, &new_data, NULL, b->sha1, next_mark))
2586 b->pack_id = pack_id;
2587 if (report_fd != -1) {
2588 char *buf = sha1_to_hex(b->sha1);
2589 buf[40] = '\n';
2590 write_or_die(report_fd, buf, 41);
2592 b->last_commit = object_count_by_type[OBJ_COMMIT];
2595 static void parse_new_tag(void)
2597 static struct strbuf msg = STRBUF_INIT;
2598 char *sp;
2599 const char *from;
2600 char *tagger;
2601 struct branch *s;
2602 struct tag *t;
2603 uintmax_t from_mark = 0;
2604 unsigned char sha1[20];
2605 enum object_type type;
2607 /* Obtain the new tag name from the rest of our command */
2608 sp = strchr(command_buf.buf, ' ') + 1;
2609 t = pool_alloc(sizeof(struct tag));
2610 t->next_tag = NULL;
2611 t->name = pool_strdup(sp);
2612 if (last_tag)
2613 last_tag->next_tag = t;
2614 else
2615 first_tag = t;
2616 last_tag = t;
2617 read_next_command();
2619 /* from ... */
2620 if (prefixcmp(command_buf.buf, "from "))
2621 die("Expected from command, got %s", command_buf.buf);
2622 from = strchr(command_buf.buf, ' ') + 1;
2623 s = lookup_branch(from);
2624 if (s) {
2625 hashcpy(sha1, s->sha1);
2626 type = OBJ_COMMIT;
2627 } else if (*from == ':') {
2628 struct object_entry *oe;
2629 from_mark = strtoumax(from + 1, NULL, 10);
2630 oe = find_mark(from_mark);
2631 type = oe->type;
2632 hashcpy(sha1, oe->idx.sha1);
2633 } else if (!get_sha1(from, sha1)) {
2634 unsigned long size;
2635 char *buf;
2637 buf = read_sha1_file(sha1, &type, &size);
2638 if (!buf || size < 46)
2639 die("Not a valid commit: %s", from);
2640 free(buf);
2641 } else
2642 die("Invalid ref name or SHA1 expression: %s", from);
2643 read_next_command();
2645 /* tagger ... */
2646 if (!prefixcmp(command_buf.buf, "tagger ")) {
2647 tagger = parse_ident(command_buf.buf + 7);
2648 read_next_command();
2649 } else
2650 tagger = NULL;
2652 /* tag payload/message */
2653 parse_data(&msg, 0, NULL);
2655 /* build the tag object */
2656 strbuf_reset(&new_data);
2658 strbuf_addf(&new_data,
2659 "object %s\n"
2660 "type %s\n"
2661 "tag %s\n",
2662 sha1_to_hex(sha1), typename(type), t->name);
2663 if (tagger)
2664 strbuf_addf(&new_data,
2665 "tagger %s\n", tagger);
2666 strbuf_addch(&new_data, '\n');
2667 strbuf_addbuf(&new_data, &msg);
2668 free(tagger);
2670 if (store_object(OBJ_TAG, &new_data, NULL, t->sha1, 0))
2671 t->pack_id = MAX_PACK_ID;
2672 else
2673 t->pack_id = pack_id;
2676 static void parse_reset_branch(void)
2678 struct branch *b;
2679 char *sp;
2681 /* Obtain the branch name from the rest of our command */
2682 sp = strchr(command_buf.buf, ' ') + 1;
2683 b = lookup_branch(sp);
2684 if (b) {
2685 hashclr(b->sha1);
2686 hashclr(b->branch_tree.versions[0].sha1);
2687 hashclr(b->branch_tree.versions[1].sha1);
2688 if (b->branch_tree.tree) {
2689 release_tree_content_recursive(b->branch_tree.tree);
2690 b->branch_tree.tree = NULL;
2693 else
2694 b = new_branch(sp);
2695 read_next_command();
2696 parse_from(b);
2697 if (command_buf.len > 0)
2698 unread_command_buf = 1;
2701 static void quoted_path_sha1(unsigned char sha1[20], struct tree_entry *root,
2702 const char *path, const char *line)
2704 struct strbuf uq = STRBUF_INIT;
2705 struct tree_entry leaf = {0};
2706 const char *x;
2708 if (unquote_c_style(&uq, path, &x))
2709 die("Invalid path: %s", line);
2710 if (*x)
2711 die("Garbage after path: %s", line);
2712 if (uq.len == 0) {
2713 hashcpy(sha1, root->versions[1].sha1);
2714 return;
2716 tree_content_get(root, uq.buf, &leaf);
2717 if (!leaf.versions[1].mode)
2718 die("Path %s not in tree", uq.buf);
2719 hashcpy(sha1, leaf.versions[1].sha1);
2722 static void sendreport(const char *buf, unsigned long size)
2724 if (write_in_full(report_fd, buf, size) != size)
2725 die_errno("Write to frontend failed");
2728 static void cat_object(struct object_entry *oe, unsigned char sha1[20])
2730 struct strbuf line = STRBUF_INIT;
2731 unsigned long size;
2732 enum object_type type = 0;
2733 char *buf;
2735 if (report_fd < 0)
2736 die("Internal error: bad report_fd %d", report_fd);
2737 if (oe && oe->pack_id != MAX_PACK_ID) {
2738 type = oe->type;
2739 buf = gfi_unpack_entry(oe, &size);
2740 } else {
2741 buf = read_sha1_file(sha1, &type, &size);
2743 if (!buf)
2744 die("Can't read object %s", sha1_to_hex(sha1));
2747 * Output based on batch_one_object() from cat-file.c.
2749 if (type <= 0) {
2750 strbuf_reset(&line);
2751 strbuf_addf(&line, "%s missing\n", sha1_to_hex(sha1));
2752 if (write_in_full(report_fd, line.buf, line.len) != line.len)
2753 die_errno("Write to frontend failed 1");
2755 strbuf_reset(&line);
2756 strbuf_addf(&line, "%s %s %lu\n", sha1_to_hex(sha1),
2757 typename(type), size);
2758 sendreport(line.buf, line.len);
2759 sendreport(buf, size);
2760 sendreport("\n", 1);
2761 free(buf);
2765 static void parse_cat_request(void)
2767 const char *p;
2768 struct object_entry *oe = oe;
2769 unsigned char sha1[20];
2770 struct tree_entry root = {0};
2772 /* cat SP <object> */
2773 p = command_buf.buf + strlen("cat ");
2774 if (report_fd < 0)
2775 die("The cat command requires the report-fd feature.");
2776 if (*p == ':') {
2777 char *x;
2778 oe = find_mark(strtoumax(p + 1, &x, 10));
2779 if (x == p + 1)
2780 die("Invalid mark: %s", command_buf.buf);
2781 if (!oe)
2782 die("Unknown mark: %s", command_buf.buf);
2783 p = x;
2784 hashcpy(sha1, oe->idx.sha1);
2785 } else {
2786 if (get_sha1_hex(p, sha1))
2787 die("Invalid SHA1: %s", command_buf.buf);
2788 p += 40;
2789 if (!*p)
2790 oe = find_object(sha1);
2793 /* [ SP "<path>" ] */
2794 if (*p) {
2795 if (*p++ != ' ')
2796 die("Missing space after SHA1: %s", command_buf.buf);
2798 /* cat <tree> "<path>" form. */
2799 hashcpy(root.versions[1].sha1, sha1);
2800 load_tree(&root);
2801 quoted_path_sha1(sha1, &root, p, command_buf.buf);
2802 oe = find_object(sha1);
2805 cat_object(oe, sha1);
2808 static void parse_checkpoint(void)
2810 if (object_count) {
2811 cycle_packfile();
2812 dump_branches();
2813 dump_tags();
2814 dump_marks();
2816 skip_optional_lf();
2819 static void parse_progress(void)
2821 fwrite(command_buf.buf, 1, command_buf.len, stdout);
2822 fputc('\n', stdout);
2823 fflush(stdout);
2824 skip_optional_lf();
2827 static char* make_fast_import_path(const char *path)
2829 struct strbuf abs_path = STRBUF_INIT;
2831 if (!relative_marks_paths || is_absolute_path(path))
2832 return xstrdup(path);
2833 strbuf_addf(&abs_path, "%s/info/fast-import/%s", get_git_dir(), path);
2834 return strbuf_detach(&abs_path, NULL);
2837 static void option_import_marks(const char *marks, int from_stream)
2839 if (import_marks_file) {
2840 if (from_stream)
2841 die("Only one import-marks command allowed per stream");
2843 /* read previous mark file */
2844 if(!import_marks_file_from_stream)
2845 read_marks();
2848 import_marks_file = make_fast_import_path(marks);
2849 safe_create_leading_directories_const(import_marks_file);
2850 import_marks_file_from_stream = from_stream;
2853 static void option_date_format(const char *fmt)
2855 if (!strcmp(fmt, "raw"))
2856 whenspec = WHENSPEC_RAW;
2857 else if (!strcmp(fmt, "rfc2822"))
2858 whenspec = WHENSPEC_RFC2822;
2859 else if (!strcmp(fmt, "now"))
2860 whenspec = WHENSPEC_NOW;
2861 else
2862 die("unknown --date-format argument %s", fmt);
2865 static void option_depth(const char *depth)
2867 max_depth = strtoul(depth, NULL, 0);
2868 if (max_depth > MAX_DEPTH)
2869 die("--depth cannot exceed %u", MAX_DEPTH);
2872 static void option_active_branches(const char *branches)
2874 max_active_branches = strtoul(branches, NULL, 0);
2877 static void option_export_marks(const char *marks)
2879 export_marks_file = make_fast_import_path(marks);
2880 safe_create_leading_directories_const(export_marks_file);
2883 static void option_report_fd(const char *fd)
2885 unsigned long n = strtoul(fd, NULL, 0);
2886 if (n > (unsigned long) INT_MAX)
2887 die("--report-fd cannot exceed %d", INT_MAX);
2888 report_fd = (int) n;
2891 static void option_export_pack_edges(const char *edges)
2893 if (pack_edges)
2894 fclose(pack_edges);
2895 pack_edges = fopen(edges, "a");
2896 if (!pack_edges)
2897 die_errno("Cannot open '%s'", edges);
2900 static int parse_one_option(const char *option)
2902 if (!prefixcmp(option, "max-pack-size=")) {
2903 unsigned long v;
2904 if (!git_parse_ulong(option + 14, &v))
2905 return 0;
2906 if (v < 8192) {
2907 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v);
2908 v *= 1024 * 1024;
2909 } else if (v < 1024 * 1024) {
2910 warning("minimum max-pack-size is 1 MiB");
2911 v = 1024 * 1024;
2913 max_packsize = v;
2914 } else if (!prefixcmp(option, "big-file-threshold=")) {
2915 unsigned long v;
2916 if (!git_parse_ulong(option + 19, &v))
2917 return 0;
2918 big_file_threshold = v;
2919 } else if (!prefixcmp(option, "depth=")) {
2920 option_depth(option + 6);
2921 } else if (!prefixcmp(option, "active-branches=")) {
2922 option_active_branches(option + 16);
2923 } else if (!prefixcmp(option, "export-pack-edges=")) {
2924 option_export_pack_edges(option + 18);
2925 } else if (!prefixcmp(option, "quiet")) {
2926 show_stats = 0;
2927 } else if (!prefixcmp(option, "stats")) {
2928 show_stats = 1;
2929 } else {
2930 return 0;
2933 return 1;
2936 static int parse_one_feature(const char *feature, int from_stream)
2938 if (!prefixcmp(feature, "date-format=")) {
2939 option_date_format(feature + 12);
2940 } else if (!prefixcmp(feature, "import-marks=")) {
2941 option_import_marks(feature + 13, from_stream);
2942 } else if (!prefixcmp(feature, "export-marks=")) {
2943 option_export_marks(feature + 13);
2944 } else if (!prefixcmp(feature, "report-fd=")) {
2945 option_report_fd(feature + strlen("report-fd="));
2946 } else if (!prefixcmp(feature, "relative-marks")) {
2947 relative_marks_paths = 1;
2948 } else if (!prefixcmp(feature, "no-relative-marks")) {
2949 relative_marks_paths = 0;
2950 } else if (!prefixcmp(feature, "force")) {
2951 force_update = 1;
2952 } else {
2953 return 0;
2956 return 1;
2959 static void parse_feature(void)
2961 char *feature = command_buf.buf + 8;
2963 if (seen_data_command)
2964 die("Got feature command '%s' after data command", feature);
2966 if (parse_one_feature(feature, 1))
2967 return;
2969 die("This version of fast-import does not support feature %s.", feature);
2972 static void parse_option(void)
2974 char *option = command_buf.buf + 11;
2976 if (seen_data_command)
2977 die("Got option command '%s' after data command", option);
2979 if (parse_one_option(option))
2980 return;
2982 die("This version of fast-import does not support option: %s", option);
2985 static int git_pack_config(const char *k, const char *v, void *cb)
2987 if (!strcmp(k, "pack.depth")) {
2988 max_depth = git_config_int(k, v);
2989 if (max_depth > MAX_DEPTH)
2990 max_depth = MAX_DEPTH;
2991 return 0;
2993 if (!strcmp(k, "pack.compression")) {
2994 int level = git_config_int(k, v);
2995 if (level == -1)
2996 level = Z_DEFAULT_COMPRESSION;
2997 else if (level < 0 || level > Z_BEST_COMPRESSION)
2998 die("bad pack compression level %d", level);
2999 pack_compression_level = level;
3000 pack_compression_seen = 1;
3001 return 0;
3003 if (!strcmp(k, "pack.indexversion")) {
3004 pack_idx_default_version = git_config_int(k, v);
3005 if (pack_idx_default_version > 2)
3006 die("bad pack.indexversion=%"PRIu32,
3007 pack_idx_default_version);
3008 return 0;
3010 if (!strcmp(k, "pack.packsizelimit")) {
3011 max_packsize = git_config_ulong(k, v);
3012 return 0;
3014 if (!strcmp(k, "core.bigfilethreshold")) {
3015 long n = git_config_int(k, v);
3016 big_file_threshold = 0 < n ? n : 0;
3018 return git_default_config(k, v, cb);
3021 static const char fast_import_usage[] =
3022 "git fast-import [--date-format=f] [--max-pack-size=n] [--big-file-threshold=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
3024 static void parse_argv(void)
3026 unsigned int i;
3028 for (i = 1; i < global_argc; i++) {
3029 const char *a = global_argv[i];
3031 if (*a != '-' || !strcmp(a, "--"))
3032 break;
3034 if (parse_one_option(a + 2))
3035 continue;
3037 if (parse_one_feature(a + 2, 0))
3038 continue;
3040 die("unknown option %s", a);
3042 if (i != global_argc)
3043 usage(fast_import_usage);
3045 seen_data_command = 1;
3046 if (import_marks_file)
3047 read_marks();
3050 int main(int argc, const char **argv)
3052 unsigned int i;
3054 git_extract_argv0_path(argv[0]);
3056 if (argc == 2 && !strcmp(argv[1], "-h"))
3057 usage(fast_import_usage);
3059 setup_git_directory();
3060 git_config(git_pack_config, NULL);
3061 if (!pack_compression_seen && core_compression_seen)
3062 pack_compression_level = core_compression_level;
3064 alloc_objects(object_entry_alloc);
3065 strbuf_init(&command_buf, 0);
3066 atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
3067 branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
3068 avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
3069 marks = pool_calloc(1, sizeof(struct mark_set));
3071 global_argc = argc;
3072 global_argv = argv;
3074 rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
3075 for (i = 0; i < (cmd_save - 1); i++)
3076 rc_free[i].next = &rc_free[i + 1];
3077 rc_free[cmd_save - 1].next = NULL;
3079 prepare_packed_git();
3080 start_packfile();
3081 set_die_routine(die_nicely);
3082 while (read_next_command() != EOF) {
3083 if (!strcmp("blob", command_buf.buf))
3084 parse_new_blob();
3085 else if (!prefixcmp(command_buf.buf, "commit "))
3086 parse_new_commit();
3087 else if (!prefixcmp(command_buf.buf, "tag "))
3088 parse_new_tag();
3089 else if (!prefixcmp(command_buf.buf, "reset "))
3090 parse_reset_branch();
3091 else if (!strcmp("checkpoint", command_buf.buf))
3092 parse_checkpoint();
3093 else if (!prefixcmp(command_buf.buf, "progress "))
3094 parse_progress();
3095 else if (!prefixcmp(command_buf.buf, "feature "))
3096 parse_feature();
3097 else if (!prefixcmp(command_buf.buf, "option git "))
3098 parse_option();
3099 else if (!prefixcmp(command_buf.buf, "option "))
3100 /* ignore non-git options*/;
3101 else
3102 die("Unsupported command: %s", command_buf.buf);
3105 /* argv hasn't been parsed yet, do so */
3106 if (!seen_data_command)
3107 parse_argv();
3109 end_packfile();
3111 dump_branches();
3112 dump_tags();
3113 unkeep_all_packs();
3114 dump_marks();
3116 if (pack_edges)
3117 fclose(pack_edges);
3119 if (show_stats) {
3120 uintmax_t total_count = 0, duplicate_count = 0;
3121 for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
3122 total_count += object_count_by_type[i];
3123 for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
3124 duplicate_count += duplicate_count_by_type[i];
3126 fprintf(stderr, "%s statistics:\n", argv[0]);
3127 fprintf(stderr, "---------------------------------------------------------------------\n");
3128 fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
3129 fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
3130 fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
3131 fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
3132 fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
3133 fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
3134 fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
3135 fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
3136 fprintf(stderr, " atoms: %10u\n", atom_cnt);
3137 fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
3138 fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
3139 fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
3140 fprintf(stderr, "---------------------------------------------------------------------\n");
3141 pack_report();
3142 fprintf(stderr, "---------------------------------------------------------------------\n");
3143 fprintf(stderr, "\n");
3146 return failure ? 1 : 0;