2 Format of STDIN stream:
14 new_blob ::= 'blob' lf
17 file_content ::= data;
19 new_commit ::= 'commit' sp ref_str lf
21 ('author' sp name '<' email '>' when lf)?
22 'committer' sp name '<' email '>' when lf
24 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
25 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
30 file_change ::= file_clr
36 file_clr ::= 'deleteall' lf;
37 file_del ::= 'D' sp path_str lf;
38 file_rnm ::= 'R' sp path_str sp path_str lf;
39 file_cpy ::= 'C' sp path_str sp path_str lf;
40 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
41 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
44 new_tag ::= 'tag' sp tag_str lf
45 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
46 'tagger' sp name '<' email '>' when lf
50 reset_branch ::= 'reset' sp ref_str lf
51 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
54 checkpoint ::= 'checkpoint' lf
57 progress ::= 'progress' sp not_lf* lf
60 # note: the first idnum in a stream should be 1 and subsequent
61 # idnums should not have gaps between values as this will cause
62 # the stream parser to reserve space for the gapped values. An
63 # idnum can be updated in the future to a new object by issuing
64 # a new mark directive with the old idnum.
66 mark ::= 'mark' sp idnum lf;
67 data ::= (delimited_data | exact_data)
70 # note: delim may be any string but must not contain lf.
71 # data_line may contain any data but must not be exactly
73 delimited_data ::= 'data' sp '<<' delim lf
77 # note: declen indicates the length of binary_data in bytes.
78 # declen does not include the lf preceeding the binary data.
80 exact_data ::= 'data' sp declen lf
83 # note: quoted strings are C-style quoting supporting \c for
84 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
85 # is the signed byte value in octal. Note that the only
86 # characters which must actually be escaped to protect the
87 # stream formatting is: \, " and LF. Otherwise these values
91 sha1exp_str ::= sha1exp;
93 path_str ::= path | '"' quoted(path) '"' ;
94 mode ::= '100644' | '644'
99 declen ::= # unsigned 32 bit value, ascii base10 notation;
100 bigint ::= # unsigned integer value, ascii base10 notation;
101 binary_data ::= # file content, not interpreted;
103 when ::= raw_when | rfc2822_when;
104 raw_when ::= ts sp tz;
105 rfc2822_when ::= # Valid RFC 2822 date and time;
107 sp ::= # ASCII space character;
108 lf ::= # ASCII newline (LF) character;
110 # note: a colon (':') must precede the numerical value assigned to
111 # an idnum. This is to distinguish it from a ref or tag name as
112 # GIT does not permit ':' in ref or tag strings.
114 idnum ::= ':' bigint;
115 path ::= # GIT style file path, e.g. "a/b/c";
116 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
117 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
118 sha1exp ::= # Any valid GIT SHA1 expression;
119 hexsha1 ::= # SHA1 in hexadecimal format;
121 # note: name and email are UTF8 strings, however name must not
122 # contain '<' or lf and email must not contain any of the
123 # following: '<', '>', lf.
125 name ::= # valid GIT author/committer name;
126 email ::= # valid GIT author/committer email;
127 ts ::= # time since the epoch in seconds, ascii base10 notation;
128 tz ::= # GIT style timezone;
130 # note: comments may appear anywhere in the input, except
131 # within a data command. Any form of the data command
132 # always escapes the related input from comment processing.
134 # In case it is not clear, the '#' that starts the comment
135 # must be the first character on that the line (an lf have
138 comment ::= '#' not_lf* lf;
139 not_lf ::= # Any byte that is not ASCII newline (LF);
151 #include "csum-file.h"
155 #define PACK_ID_BITS 16
156 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
157 #define DEPTH_BITS 13
158 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
162 struct object_entry
*next
;
164 uint32_t type
: TYPE_BITS
,
165 pack_id
: PACK_ID_BITS
,
167 unsigned char sha1
[20];
170 struct object_entry_pool
172 struct object_entry_pool
*next_pool
;
173 struct object_entry
*next_free
;
174 struct object_entry
*end
;
175 struct object_entry entries
[FLEX_ARRAY
]; /* more */
181 struct object_entry
*marked
[1024];
182 struct mark_set
*sets
[1024];
198 struct mem_pool
*next_pool
;
201 char space
[FLEX_ARRAY
]; /* more */
206 struct atom_str
*next_atom
;
207 unsigned short str_len
;
208 char str_dat
[FLEX_ARRAY
]; /* more */
214 struct tree_content
*tree
;
215 struct atom_str
* name
;
219 unsigned char sha1
[20];
225 unsigned int entry_capacity
; /* must match avail_tree_content */
226 unsigned int entry_count
;
227 unsigned int delta_depth
;
228 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
231 struct avail_tree_content
233 unsigned int entry_capacity
; /* must match tree_content */
234 struct avail_tree_content
*next_avail
;
239 struct branch
*table_next_branch
;
240 struct branch
*active_next_branch
;
242 struct tree_entry branch_tree
;
243 uintmax_t last_commit
;
245 unsigned pack_id
: PACK_ID_BITS
;
246 unsigned char sha1
[20];
251 struct tag
*next_tag
;
253 unsigned int pack_id
;
254 unsigned char sha1
[20];
265 struct hash_list
*next
;
266 unsigned char sha1
[20];
275 struct recent_command
277 struct recent_command
*prev
;
278 struct recent_command
*next
;
282 /* Configured limits on output */
283 static unsigned long max_depth
= 10;
284 static off_t max_packsize
= (1LL << 32) - 1;
285 static int force_update
;
287 /* Stats and misc. counters */
288 static uintmax_t alloc_count
;
289 static uintmax_t marks_set_count
;
290 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
291 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
292 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
293 static unsigned long object_count
;
294 static unsigned long branch_count
;
295 static unsigned long branch_load_count
;
297 static FILE *pack_edges
;
300 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
301 static size_t total_allocd
;
302 static struct mem_pool
*mem_pool
;
304 /* Atom management */
305 static unsigned int atom_table_sz
= 4451;
306 static unsigned int atom_cnt
;
307 static struct atom_str
**atom_table
;
309 /* The .pack file being generated */
310 static unsigned int pack_id
;
311 static struct packed_git
*pack_data
;
312 static struct packed_git
**all_packs
;
313 static unsigned long pack_size
;
315 /* Table of objects we've written. */
316 static unsigned int object_entry_alloc
= 5000;
317 static struct object_entry_pool
*blocks
;
318 static struct object_entry
*object_table
[1 << 16];
319 static struct mark_set
*marks
;
320 static const char* mark_file
;
323 static struct last_object last_blob
;
325 /* Tree management */
326 static unsigned int tree_entry_alloc
= 1000;
327 static void *avail_tree_entry
;
328 static unsigned int avail_tree_table_sz
= 100;
329 static struct avail_tree_content
**avail_tree_table
;
330 static struct dbuf old_tree
;
331 static struct dbuf new_tree
;
334 static unsigned long max_active_branches
= 5;
335 static unsigned long cur_active_branches
;
336 static unsigned long branch_table_sz
= 1039;
337 static struct branch
**branch_table
;
338 static struct branch
*active_branches
;
341 static struct tag
*first_tag
;
342 static struct tag
*last_tag
;
344 /* Input stream parsing */
345 static whenspec_type whenspec
= WHENSPEC_RAW
;
346 static struct strbuf command_buf
;
347 static int unread_command_buf
;
348 static struct recent_command cmd_hist
= {&cmd_hist
, &cmd_hist
, NULL
};
349 static struct recent_command
*cmd_tail
= &cmd_hist
;
350 static struct recent_command
*rc_free
;
351 static unsigned int cmd_save
= 100;
352 static uintmax_t next_mark
;
353 static struct dbuf new_data
;
355 static void write_branch_report(FILE *rpt
, struct branch
*b
)
357 fprintf(rpt
, "%s:\n", b
->name
);
359 fprintf(rpt
, " status :");
361 fputs(" active", rpt
);
362 if (b
->branch_tree
.tree
)
363 fputs(" loaded", rpt
);
364 if (is_null_sha1(b
->branch_tree
.versions
[1].sha1
))
365 fputs(" dirty", rpt
);
368 fprintf(rpt
, " tip commit : %s\n", sha1_to_hex(b
->sha1
));
369 fprintf(rpt
, " old tree : %s\n", sha1_to_hex(b
->branch_tree
.versions
[0].sha1
));
370 fprintf(rpt
, " cur tree : %s\n", sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
371 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
373 fputs(" last pack : ", rpt
);
374 if (b
->pack_id
< MAX_PACK_ID
)
375 fprintf(rpt
, "%u", b
->pack_id
);
381 static void write_crash_report(const char *err
)
383 char *loc
= git_path("fast_import_crash_%d", getpid());
384 FILE *rpt
= fopen(loc
, "w");
387 struct recent_command
*rc
;
390 error("can't write crash report %s: %s", loc
, strerror(errno
));
394 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
396 fprintf(rpt
, "fast-import crash report:\n");
397 fprintf(rpt
, " fast-import process: %d\n", getpid());
398 fprintf(rpt
, " parent process : %d\n", getppid());
399 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_LOCAL
));
402 fputs("fatal: ", rpt
);
407 fputs("Most Recent Commands Before Crash\n", rpt
);
408 fputs("---------------------------------\n", rpt
);
409 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
410 if (rc
->next
== &cmd_hist
)
419 fputs("Active Branch LRU\n", rpt
);
420 fputs("-----------------\n", rpt
);
421 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
423 max_active_branches
);
425 fputs(" pos clock name\n", rpt
);
426 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
427 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
428 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
429 ++lu
, b
->last_commit
, b
->name
);
432 fputs("Inactive Branches\n", rpt
);
433 fputs("-----------------\n", rpt
);
434 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
435 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
436 write_branch_report(rpt
, b
);
440 fputs("-------------------\n", rpt
);
441 fputs("END OF CRASH REPORT\n", rpt
);
445 static NORETURN
void die_nicely(const char *err
, va_list params
)
448 char message
[2 * PATH_MAX
];
450 vsnprintf(message
, sizeof(message
), err
, params
);
451 fputs("fatal: ", stderr
);
452 fputs(message
, stderr
);
457 write_crash_report(message
);
462 static void alloc_objects(unsigned int cnt
)
464 struct object_entry_pool
*b
;
466 b
= xmalloc(sizeof(struct object_entry_pool
)
467 + cnt
* sizeof(struct object_entry
));
468 b
->next_pool
= blocks
;
469 b
->next_free
= b
->entries
;
470 b
->end
= b
->entries
+ cnt
;
475 static struct object_entry
*new_object(unsigned char *sha1
)
477 struct object_entry
*e
;
479 if (blocks
->next_free
== blocks
->end
)
480 alloc_objects(object_entry_alloc
);
482 e
= blocks
->next_free
++;
483 hashcpy(e
->sha1
, sha1
);
487 static struct object_entry
*find_object(unsigned char *sha1
)
489 unsigned int h
= sha1
[0] << 8 | sha1
[1];
490 struct object_entry
*e
;
491 for (e
= object_table
[h
]; e
; e
= e
->next
)
492 if (!hashcmp(sha1
, e
->sha1
))
497 static struct object_entry
*insert_object(unsigned char *sha1
)
499 unsigned int h
= sha1
[0] << 8 | sha1
[1];
500 struct object_entry
*e
= object_table
[h
];
501 struct object_entry
*p
= NULL
;
504 if (!hashcmp(sha1
, e
->sha1
))
510 e
= new_object(sha1
);
520 static unsigned int hc_str(const char *s
, size_t len
)
528 static void *pool_alloc(size_t len
)
533 for (p
= mem_pool
; p
; p
= p
->next_pool
)
534 if ((p
->end
- p
->next_free
>= len
))
538 if (len
>= (mem_pool_alloc
/2)) {
542 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
543 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
544 p
->next_pool
= mem_pool
;
545 p
->next_free
= p
->space
;
546 p
->end
= p
->next_free
+ mem_pool_alloc
;
551 /* round out to a pointer alignment */
552 if (len
& (sizeof(void*) - 1))
553 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
558 static void *pool_calloc(size_t count
, size_t size
)
560 size_t len
= count
* size
;
561 void *r
= pool_alloc(len
);
566 static char *pool_strdup(const char *s
)
568 char *r
= pool_alloc(strlen(s
) + 1);
573 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
576 if (b
->capacity
>= maxlen
)
580 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
581 b
->buffer
= xmalloc(b
->capacity
);
584 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
586 struct mark_set
*s
= marks
;
587 while ((idnum
>> s
->shift
) >= 1024) {
588 s
= pool_calloc(1, sizeof(struct mark_set
));
589 s
->shift
= marks
->shift
+ 10;
590 s
->data
.sets
[0] = marks
;
594 uintmax_t i
= idnum
>> s
->shift
;
595 idnum
-= i
<< s
->shift
;
596 if (!s
->data
.sets
[i
]) {
597 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
598 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
602 if (!s
->data
.marked
[idnum
])
604 s
->data
.marked
[idnum
] = oe
;
607 static struct object_entry
*find_mark(uintmax_t idnum
)
609 uintmax_t orig_idnum
= idnum
;
610 struct mark_set
*s
= marks
;
611 struct object_entry
*oe
= NULL
;
612 if ((idnum
>> s
->shift
) < 1024) {
613 while (s
&& s
->shift
) {
614 uintmax_t i
= idnum
>> s
->shift
;
615 idnum
-= i
<< s
->shift
;
619 oe
= s
->data
.marked
[idnum
];
622 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
626 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
628 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
631 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
632 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
635 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
637 strncpy(c
->str_dat
, s
, len
);
639 c
->next_atom
= atom_table
[hc
];
645 static struct branch
*lookup_branch(const char *name
)
647 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
650 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
651 if (!strcmp(name
, b
->name
))
656 static struct branch
*new_branch(const char *name
)
658 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
659 struct branch
* b
= lookup_branch(name
);
662 die("Invalid attempt to create duplicate branch: %s", name
);
663 switch (check_ref_format(name
)) {
664 case 0: break; /* its valid */
665 case -2: break; /* valid, but too few '/', allow anyway */
667 die("Branch name doesn't conform to GIT standards: %s", name
);
670 b
= pool_calloc(1, sizeof(struct branch
));
671 b
->name
= pool_strdup(name
);
672 b
->table_next_branch
= branch_table
[hc
];
673 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
674 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
676 b
->pack_id
= MAX_PACK_ID
;
677 branch_table
[hc
] = b
;
682 static unsigned int hc_entries(unsigned int cnt
)
684 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
685 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
688 static struct tree_content
*new_tree_content(unsigned int cnt
)
690 struct avail_tree_content
*f
, *l
= NULL
;
691 struct tree_content
*t
;
692 unsigned int hc
= hc_entries(cnt
);
694 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
695 if (f
->entry_capacity
>= cnt
)
700 l
->next_avail
= f
->next_avail
;
702 avail_tree_table
[hc
] = f
->next_avail
;
704 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
705 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
706 f
->entry_capacity
= cnt
;
709 t
= (struct tree_content
*)f
;
715 static void release_tree_entry(struct tree_entry
*e
);
716 static void release_tree_content(struct tree_content
*t
)
718 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
719 unsigned int hc
= hc_entries(f
->entry_capacity
);
720 f
->next_avail
= avail_tree_table
[hc
];
721 avail_tree_table
[hc
] = f
;
724 static void release_tree_content_recursive(struct tree_content
*t
)
727 for (i
= 0; i
< t
->entry_count
; i
++)
728 release_tree_entry(t
->entries
[i
]);
729 release_tree_content(t
);
732 static struct tree_content
*grow_tree_content(
733 struct tree_content
*t
,
736 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
737 r
->entry_count
= t
->entry_count
;
738 r
->delta_depth
= t
->delta_depth
;
739 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
740 release_tree_content(t
);
744 static struct tree_entry
*new_tree_entry(void)
746 struct tree_entry
*e
;
748 if (!avail_tree_entry
) {
749 unsigned int n
= tree_entry_alloc
;
750 total_allocd
+= n
* sizeof(struct tree_entry
);
751 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
753 *((void**)e
) = e
+ 1;
759 e
= avail_tree_entry
;
760 avail_tree_entry
= *((void**)e
);
764 static void release_tree_entry(struct tree_entry
*e
)
767 release_tree_content_recursive(e
->tree
);
768 *((void**)e
) = avail_tree_entry
;
769 avail_tree_entry
= e
;
772 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
774 struct tree_content
*d
;
775 struct tree_entry
*a
, *b
;
780 d
= new_tree_content(s
->entry_count
);
781 for (i
= 0; i
< s
->entry_count
; i
++) {
783 b
= new_tree_entry();
784 memcpy(b
, a
, sizeof(*a
));
785 if (a
->tree
&& is_null_sha1(b
->versions
[1].sha1
))
786 b
->tree
= dup_tree_content(a
->tree
);
791 d
->entry_count
= s
->entry_count
;
792 d
->delta_depth
= s
->delta_depth
;
797 static void start_packfile(void)
799 static char tmpfile
[PATH_MAX
];
800 struct packed_git
*p
;
801 struct pack_header hdr
;
804 snprintf(tmpfile
, sizeof(tmpfile
),
805 "%s/tmp_pack_XXXXXX", get_object_directory());
806 pack_fd
= xmkstemp(tmpfile
);
807 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
808 strcpy(p
->pack_name
, tmpfile
);
809 p
->pack_fd
= pack_fd
;
811 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
812 hdr
.hdr_version
= htonl(2);
814 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
817 pack_size
= sizeof(hdr
);
820 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
821 all_packs
[pack_id
] = p
;
824 static int oecmp (const void *a_
, const void *b_
)
826 struct object_entry
*a
= *((struct object_entry
**)a_
);
827 struct object_entry
*b
= *((struct object_entry
**)b_
);
828 return hashcmp(a
->sha1
, b
->sha1
);
831 static char *create_index(void)
833 static char tmpfile
[PATH_MAX
];
836 struct object_entry
**idx
, **c
, **last
, *e
;
837 struct object_entry_pool
*o
;
841 /* Build the sorted table of object IDs. */
842 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
844 for (o
= blocks
; o
; o
= o
->next_pool
)
845 for (e
= o
->next_free
; e
-- != o
->entries
;)
846 if (pack_id
== e
->pack_id
)
848 last
= idx
+ object_count
;
850 die("internal consistency error creating the index");
851 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
853 /* Generate the fan-out array. */
855 for (i
= 0; i
< 256; i
++) {
856 struct object_entry
**next
= c
;;
857 while (next
< last
) {
858 if ((*next
)->sha1
[0] != i
)
862 array
[i
] = htonl(next
- idx
);
866 snprintf(tmpfile
, sizeof(tmpfile
),
867 "%s/tmp_idx_XXXXXX", get_object_directory());
868 idx_fd
= xmkstemp(tmpfile
);
869 f
= sha1fd(idx_fd
, tmpfile
);
870 sha1write(f
, array
, 256 * sizeof(int));
872 for (c
= idx
; c
!= last
; c
++) {
873 uint32_t offset
= htonl((*c
)->offset
);
874 sha1write(f
, &offset
, 4);
875 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
876 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
878 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
879 sha1close(f
, NULL
, 1);
881 SHA1_Final(pack_data
->sha1
, &ctx
);
885 static char *keep_pack(char *curr_index_name
)
887 static char name
[PATH_MAX
];
888 static const char *keep_msg
= "fast-import";
891 chmod(pack_data
->pack_name
, 0444);
892 chmod(curr_index_name
, 0444);
894 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
895 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
896 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
898 die("cannot create keep file");
899 write(keep_fd
, keep_msg
, strlen(keep_msg
));
902 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
903 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
904 if (move_temp_to_file(pack_data
->pack_name
, name
))
905 die("cannot store pack file");
907 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
908 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
909 if (move_temp_to_file(curr_index_name
, name
))
910 die("cannot store index file");
914 static void unkeep_all_packs(void)
916 static char name
[PATH_MAX
];
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
));
927 static void end_packfile(void)
929 struct packed_git
*old_p
= pack_data
, *new_p
;
937 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->sha1
,
938 pack_data
->pack_name
, object_count
);
939 close(pack_data
->pack_fd
);
940 idx_name
= keep_pack(create_index());
942 /* Register the packfile with core git's machinary. */
943 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
945 die("core git rejected index %s", idx_name
);
946 new_p
->windows
= old_p
->windows
;
947 all_packs
[pack_id
] = new_p
;
948 install_packed_git(new_p
);
950 /* Print the boundary */
952 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
953 for (i
= 0; i
< branch_table_sz
; i
++) {
954 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
955 if (b
->pack_id
== pack_id
)
956 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
959 for (t
= first_tag
; t
; t
= t
->next_tag
) {
960 if (t
->pack_id
== pack_id
)
961 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
963 fputc('\n', pack_edges
);
970 unlink(old_p
->pack_name
);
973 /* We can't carry a delta across packfiles. */
974 free(last_blob
.data
);
975 last_blob
.data
= NULL
;
977 last_blob
.offset
= 0;
981 static void cycle_packfile(void)
987 static size_t encode_header(
988 enum object_type type
,
995 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
996 die("bad type %d", type
);
998 c
= (type
<< 4) | (size
& 15);
1010 static int store_object(
1011 enum object_type type
,
1014 struct last_object
*last
,
1015 unsigned char *sha1out
,
1019 struct object_entry
*e
;
1020 unsigned char hdr
[96];
1021 unsigned char sha1
[20];
1022 unsigned long hdrlen
, deltalen
;
1026 hdrlen
= sprintf((char*)hdr
,"%s %lu", typename(type
),
1027 (unsigned long)datlen
) + 1;
1029 SHA1_Update(&c
, hdr
, hdrlen
);
1030 SHA1_Update(&c
, dat
, datlen
);
1031 SHA1_Final(sha1
, &c
);
1033 hashcpy(sha1out
, sha1
);
1035 e
= insert_object(sha1
);
1037 insert_mark(mark
, e
);
1039 duplicate_count_by_type
[type
]++;
1041 } else if (find_sha1_pack(sha1
, packed_git
)) {
1043 e
->pack_id
= MAX_PACK_ID
;
1044 e
->offset
= 1; /* just not zero! */
1045 duplicate_count_by_type
[type
]++;
1049 if (last
&& last
->data
&& last
->depth
< max_depth
) {
1050 delta
= diff_delta(last
->data
, last
->len
,
1053 if (delta
&& deltalen
>= datlen
) {
1060 memset(&s
, 0, sizeof(s
));
1061 deflateInit(&s
, zlib_compression_level
);
1064 s
.avail_in
= deltalen
;
1067 s
.avail_in
= datlen
;
1069 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
1070 s
.next_out
= out
= xmalloc(s
.avail_out
);
1071 while (deflate(&s
, Z_FINISH
) == Z_OK
)
1075 /* Determine if we should auto-checkpoint. */
1076 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
1077 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
1079 /* This new object needs to *not* have the current pack_id. */
1080 e
->pack_id
= pack_id
+ 1;
1083 /* We cannot carry a delta into the new pack. */
1088 memset(&s
, 0, sizeof(s
));
1089 deflateInit(&s
, zlib_compression_level
);
1091 s
.avail_in
= datlen
;
1092 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
1093 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
1094 while (deflate(&s
, Z_FINISH
) == Z_OK
)
1101 e
->pack_id
= pack_id
;
1102 e
->offset
= pack_size
;
1104 object_count_by_type
[type
]++;
1107 unsigned long ofs
= e
->offset
- last
->offset
;
1108 unsigned pos
= sizeof(hdr
) - 1;
1110 delta_count_by_type
[type
]++;
1111 e
->depth
= last
->depth
+ 1;
1113 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
1114 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
1115 pack_size
+= hdrlen
;
1117 hdr
[pos
] = ofs
& 127;
1119 hdr
[--pos
] = 128 | (--ofs
& 127);
1120 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
1121 pack_size
+= sizeof(hdr
) - pos
;
1124 hdrlen
= encode_header(type
, datlen
, hdr
);
1125 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
1126 pack_size
+= hdrlen
;
1129 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
1130 pack_size
+= s
.total_out
;
1138 last
->offset
= e
->offset
;
1139 last
->depth
= e
->depth
;
1145 static void *gfi_unpack_entry(
1146 struct object_entry
*oe
,
1147 unsigned long *sizep
)
1149 enum object_type type
;
1150 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1152 p
->pack_size
= pack_size
+ 20;
1153 return unpack_entry(p
, oe
->offset
, &type
, sizep
);
1156 static const char *get_mode(const char *str
, uint16_t *modep
)
1161 while ((c
= *str
++) != ' ') {
1162 if (c
< '0' || c
> '7')
1164 mode
= (mode
<< 3) + (c
- '0');
1170 static void load_tree(struct tree_entry
*root
)
1172 unsigned char* sha1
= root
->versions
[1].sha1
;
1173 struct object_entry
*myoe
;
1174 struct tree_content
*t
;
1179 root
->tree
= t
= new_tree_content(8);
1180 if (is_null_sha1(sha1
))
1183 myoe
= find_object(sha1
);
1184 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1185 if (myoe
->type
!= OBJ_TREE
)
1186 die("Not a tree: %s", sha1_to_hex(sha1
));
1187 t
->delta_depth
= myoe
->depth
;
1188 buf
= gfi_unpack_entry(myoe
, &size
);
1190 enum object_type type
;
1191 buf
= read_sha1_file(sha1
, &type
, &size
);
1192 if (!buf
|| type
!= OBJ_TREE
)
1193 die("Can't load tree %s", sha1_to_hex(sha1
));
1197 while (c
!= (buf
+ size
)) {
1198 struct tree_entry
*e
= new_tree_entry();
1200 if (t
->entry_count
== t
->entry_capacity
)
1201 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1202 t
->entries
[t
->entry_count
++] = e
;
1205 c
= get_mode(c
, &e
->versions
[1].mode
);
1207 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1208 e
->versions
[0].mode
= e
->versions
[1].mode
;
1209 e
->name
= to_atom(c
, strlen(c
));
1210 c
+= e
->name
->str_len
+ 1;
1211 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1212 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1218 static int tecmp0 (const void *_a
, const void *_b
)
1220 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1221 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1222 return base_name_compare(
1223 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1224 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1227 static int tecmp1 (const void *_a
, const void *_b
)
1229 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1230 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1231 return base_name_compare(
1232 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1233 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1236 static void mktree(struct tree_content
*t
,
1246 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1248 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1250 for (i
= 0; i
< t
->entry_count
; i
++) {
1251 if (t
->entries
[i
]->versions
[v
].mode
)
1252 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1255 size_dbuf(b
, maxlen
);
1257 for (i
= 0; i
< t
->entry_count
; i
++) {
1258 struct tree_entry
*e
= t
->entries
[i
];
1259 if (!e
->versions
[v
].mode
)
1261 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1263 strcpy(c
, e
->name
->str_dat
);
1264 c
+= e
->name
->str_len
+ 1;
1265 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1268 *szp
= c
- (char*)b
->buffer
;
1271 static void store_tree(struct tree_entry
*root
)
1273 struct tree_content
*t
= root
->tree
;
1274 unsigned int i
, j
, del
;
1275 unsigned long new_len
;
1276 struct last_object lo
;
1277 struct object_entry
*le
;
1279 if (!is_null_sha1(root
->versions
[1].sha1
))
1282 for (i
= 0; i
< t
->entry_count
; i
++) {
1283 if (t
->entries
[i
]->tree
)
1284 store_tree(t
->entries
[i
]);
1287 le
= find_object(root
->versions
[0].sha1
);
1288 if (!S_ISDIR(root
->versions
[0].mode
)
1290 || le
->pack_id
!= pack_id
) {
1295 mktree(t
, 0, &lo
.len
, &old_tree
);
1296 lo
.data
= old_tree
.buffer
;
1297 lo
.offset
= le
->offset
;
1298 lo
.depth
= t
->delta_depth
;
1302 mktree(t
, 1, &new_len
, &new_tree
);
1303 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1304 &lo
, root
->versions
[1].sha1
, 0);
1306 t
->delta_depth
= lo
.depth
;
1307 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1308 struct tree_entry
*e
= t
->entries
[i
];
1309 if (e
->versions
[1].mode
) {
1310 e
->versions
[0].mode
= e
->versions
[1].mode
;
1311 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1312 t
->entries
[j
++] = e
;
1314 release_tree_entry(e
);
1318 t
->entry_count
-= del
;
1321 static int tree_content_set(
1322 struct tree_entry
*root
,
1324 const unsigned char *sha1
,
1325 const uint16_t mode
,
1326 struct tree_content
*subtree
)
1328 struct tree_content
*t
= root
->tree
;
1331 struct tree_entry
*e
;
1333 slash1
= strchr(p
, '/');
1339 die("Empty path component found in input");
1340 if (!slash1
&& !S_ISDIR(mode
) && subtree
)
1341 die("Non-directories cannot have subtrees");
1343 for (i
= 0; i
< t
->entry_count
; i
++) {
1345 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1348 && e
->versions
[1].mode
== mode
1349 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1351 e
->versions
[1].mode
= mode
;
1352 hashcpy(e
->versions
[1].sha1
, sha1
);
1354 release_tree_content_recursive(e
->tree
);
1356 hashclr(root
->versions
[1].sha1
);
1359 if (!S_ISDIR(e
->versions
[1].mode
)) {
1360 e
->tree
= new_tree_content(8);
1361 e
->versions
[1].mode
= S_IFDIR
;
1365 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
)) {
1366 hashclr(root
->versions
[1].sha1
);
1373 if (t
->entry_count
== t
->entry_capacity
)
1374 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1375 e
= new_tree_entry();
1376 e
->name
= to_atom(p
, n
);
1377 e
->versions
[0].mode
= 0;
1378 hashclr(e
->versions
[0].sha1
);
1379 t
->entries
[t
->entry_count
++] = e
;
1381 e
->tree
= new_tree_content(8);
1382 e
->versions
[1].mode
= S_IFDIR
;
1383 tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
);
1386 e
->versions
[1].mode
= mode
;
1387 hashcpy(e
->versions
[1].sha1
, sha1
);
1389 hashclr(root
->versions
[1].sha1
);
1393 static int tree_content_remove(
1394 struct tree_entry
*root
,
1396 struct tree_entry
*backup_leaf
)
1398 struct tree_content
*t
= root
->tree
;
1401 struct tree_entry
*e
;
1403 slash1
= strchr(p
, '/');
1409 for (i
= 0; i
< t
->entry_count
; i
++) {
1411 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1412 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1416 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
)) {
1417 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1418 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1419 hashclr(root
->versions
[1].sha1
);
1433 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1435 release_tree_content_recursive(e
->tree
);
1437 e
->versions
[1].mode
= 0;
1438 hashclr(e
->versions
[1].sha1
);
1439 hashclr(root
->versions
[1].sha1
);
1443 static int tree_content_get(
1444 struct tree_entry
*root
,
1446 struct tree_entry
*leaf
)
1448 struct tree_content
*t
= root
->tree
;
1451 struct tree_entry
*e
;
1453 slash1
= strchr(p
, '/');
1459 for (i
= 0; i
< t
->entry_count
; i
++) {
1461 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1463 memcpy(leaf
, e
, sizeof(*leaf
));
1464 if (e
->tree
&& is_null_sha1(e
->versions
[1].sha1
))
1465 leaf
->tree
= dup_tree_content(e
->tree
);
1470 if (!S_ISDIR(e
->versions
[1].mode
))
1474 return tree_content_get(e
, slash1
+ 1, leaf
);
1480 static int update_branch(struct branch
*b
)
1482 static const char *msg
= "fast-import";
1483 struct ref_lock
*lock
;
1484 unsigned char old_sha1
[20];
1486 if (read_ref(b
->name
, old_sha1
))
1488 lock
= lock_any_ref_for_update(b
->name
, old_sha1
, 0);
1490 return error("Unable to lock %s", b
->name
);
1491 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1492 struct commit
*old_cmit
, *new_cmit
;
1494 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1495 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1496 if (!old_cmit
|| !new_cmit
) {
1498 return error("Branch %s is missing commits.", b
->name
);
1501 if (!in_merge_bases(old_cmit
, &new_cmit
, 1)) {
1503 warning("Not updating %s"
1504 " (new tip %s does not contain %s)",
1505 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1509 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1510 return error("Unable to update %s", b
->name
);
1514 static void dump_branches(void)
1519 for (i
= 0; i
< branch_table_sz
; i
++) {
1520 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1521 failure
|= update_branch(b
);
1525 static void dump_tags(void)
1527 static const char *msg
= "fast-import";
1529 struct ref_lock
*lock
;
1530 char ref_name
[PATH_MAX
];
1532 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1533 sprintf(ref_name
, "tags/%s", t
->name
);
1534 lock
= lock_ref_sha1(ref_name
, NULL
);
1535 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1536 failure
|= error("Unable to update %s", ref_name
);
1540 static void dump_marks_helper(FILE *f
,
1546 for (k
= 0; k
< 1024; k
++) {
1547 if (m
->data
.sets
[k
])
1548 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1552 for (k
= 0; k
< 1024; k
++) {
1553 if (m
->data
.marked
[k
])
1554 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1555 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1560 static void dump_marks(void)
1562 static struct lock_file mark_lock
;
1569 mark_fd
= hold_lock_file_for_update(&mark_lock
, mark_file
, 0);
1571 failure
|= error("Unable to write marks file %s: %s",
1572 mark_file
, strerror(errno
));
1576 f
= fdopen(mark_fd
, "w");
1578 rollback_lock_file(&mark_lock
);
1579 failure
|= error("Unable to write marks file %s: %s",
1580 mark_file
, strerror(errno
));
1584 dump_marks_helper(f
, 0, marks
);
1586 if (commit_lock_file(&mark_lock
))
1587 failure
|= error("Unable to write marks file %s: %s",
1588 mark_file
, strerror(errno
));
1591 static void read_next_command(void)
1594 if (unread_command_buf
) {
1595 unread_command_buf
= 0;
1596 if (command_buf
.eof
)
1599 struct recent_command
*rc
;
1601 command_buf
.buf
= NULL
;
1602 read_line(&command_buf
, stdin
, '\n');
1603 if (command_buf
.eof
)
1611 cmd_hist
.next
= rc
->next
;
1612 cmd_hist
.next
->prev
= &cmd_hist
;
1616 rc
->buf
= command_buf
.buf
;
1617 rc
->prev
= cmd_tail
;
1618 rc
->next
= cmd_hist
.prev
;
1619 rc
->prev
->next
= rc
;
1622 } while (command_buf
.buf
[0] == '#');
1625 static void skip_optional_lf(void)
1627 int term_char
= fgetc(stdin
);
1628 if (term_char
!= '\n' && term_char
!= EOF
)
1629 ungetc(term_char
, stdin
);
1632 static void cmd_mark(void)
1634 if (!prefixcmp(command_buf
.buf
, "mark :")) {
1635 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1636 read_next_command();
1642 static void *cmd_data (size_t *size
)
1647 if (prefixcmp(command_buf
.buf
, "data "))
1648 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1650 if (!prefixcmp(command_buf
.buf
+ 5, "<<")) {
1651 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1652 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1654 buffer
= xmalloc(sz
);
1655 command_buf
.buf
= NULL
;
1657 read_line(&command_buf
, stdin
, '\n');
1658 if (command_buf
.eof
)
1659 die("EOF in data (terminator '%s' not found)", term
);
1660 if (term_len
== command_buf
.len
1661 && !strcmp(term
, command_buf
.buf
))
1663 ALLOC_GROW(buffer
, length
+ command_buf
.len
, sz
);
1664 memcpy(buffer
+ length
,
1666 command_buf
.len
- 1);
1667 length
+= command_buf
.len
- 1;
1668 buffer
[length
++] = '\n';
1674 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1675 buffer
= xmalloc(length
);
1676 while (n
< length
) {
1677 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1678 if (!s
&& feof(stdin
))
1679 die("EOF in data (%lu bytes remaining)",
1680 (unsigned long)(length
- n
));
1690 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1692 const char *orig_src
= src
;
1695 strtoul(src
, &endp
, 10);
1696 if (endp
== src
|| *endp
!= ' ')
1700 if (*src
!= '-' && *src
!= '+')
1704 strtoul(src
+ 1, &endp
, 10);
1705 if (endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1708 strcpy(result
, orig_src
);
1712 static char *parse_ident(const char *buf
)
1718 gt
= strrchr(buf
, '>');
1720 die("Missing > in ident string: %s", buf
);
1723 die("Missing space after > in ident string: %s", buf
);
1725 name_len
= gt
- buf
;
1726 ident
= xmalloc(name_len
+ 24);
1727 strncpy(ident
, buf
, name_len
);
1731 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1732 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1734 case WHENSPEC_RFC2822
:
1735 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1736 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1739 if (strcmp("now", gt
))
1740 die("Date in ident must be 'now': %s", buf
);
1741 datestamp(ident
+ name_len
, 24);
1748 static void cmd_new_blob(void)
1753 read_next_command();
1757 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1761 static void unload_one_branch(void)
1763 while (cur_active_branches
1764 && cur_active_branches
>= max_active_branches
) {
1765 uintmax_t min_commit
= ULONG_MAX
;
1766 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1768 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1769 if (e
->last_commit
< min_commit
) {
1771 min_commit
= e
->last_commit
;
1777 e
= p
->active_next_branch
;
1778 p
->active_next_branch
= e
->active_next_branch
;
1780 e
= active_branches
;
1781 active_branches
= e
->active_next_branch
;
1784 e
->active_next_branch
= NULL
;
1785 if (e
->branch_tree
.tree
) {
1786 release_tree_content_recursive(e
->branch_tree
.tree
);
1787 e
->branch_tree
.tree
= NULL
;
1789 cur_active_branches
--;
1793 static void load_branch(struct branch
*b
)
1795 load_tree(&b
->branch_tree
);
1798 b
->active_next_branch
= active_branches
;
1799 active_branches
= b
;
1800 cur_active_branches
++;
1801 branch_load_count
++;
1805 static void file_change_m(struct branch
*b
)
1807 const char *p
= command_buf
.buf
+ 2;
1810 struct object_entry
*oe
= oe
;
1811 unsigned char sha1
[20];
1812 uint16_t mode
, inline_data
= 0;
1814 p
= get_mode(p
, &mode
);
1816 die("Corrupt mode: %s", command_buf
.buf
);
1818 case S_IFREG
| 0644:
1819 case S_IFREG
| 0755:
1826 die("Corrupt mode: %s", command_buf
.buf
);
1831 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1832 hashcpy(sha1
, oe
->sha1
);
1834 } else if (!prefixcmp(p
, "inline")) {
1838 if (get_sha1_hex(p
, sha1
))
1839 die("Invalid SHA1: %s", command_buf
.buf
);
1840 oe
= find_object(sha1
);
1844 die("Missing space after SHA1: %s", command_buf
.buf
);
1846 p_uq
= unquote_c_style(p
, &endp
);
1849 die("Garbage after path in: %s", command_buf
.buf
);
1857 p
= p_uq
= xstrdup(p
);
1858 read_next_command();
1860 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1863 if (oe
->type
!= OBJ_BLOB
)
1864 die("Not a blob (actually a %s): %s",
1865 typename(oe
->type
), command_buf
.buf
);
1867 enum object_type type
= sha1_object_info(sha1
, NULL
);
1869 die("Blob not found: %s", command_buf
.buf
);
1870 if (type
!= OBJ_BLOB
)
1871 die("Not a blob (actually a %s): %s",
1872 typename(type
), command_buf
.buf
);
1875 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
, NULL
);
1879 static void file_change_d(struct branch
*b
)
1881 const char *p
= command_buf
.buf
+ 2;
1885 p_uq
= unquote_c_style(p
, &endp
);
1888 die("Garbage after path in: %s", command_buf
.buf
);
1891 tree_content_remove(&b
->branch_tree
, p
, NULL
);
1895 static void file_change_cr(struct branch
*b
, int rename
)
1900 struct tree_entry leaf
;
1902 s
= command_buf
.buf
+ 2;
1903 s_uq
= unquote_c_style(s
, &endp
);
1906 die("Missing space after source: %s", command_buf
.buf
);
1909 endp
= strchr(s
, ' ');
1911 die("Missing space after source: %s", command_buf
.buf
);
1912 s_uq
= xmalloc(endp
- s
+ 1);
1913 memcpy(s_uq
, s
, endp
- s
);
1920 die("Missing dest: %s", command_buf
.buf
);
1923 d_uq
= unquote_c_style(d
, &endp
);
1926 die("Garbage after dest in: %s", command_buf
.buf
);
1930 memset(&leaf
, 0, sizeof(leaf
));
1932 tree_content_remove(&b
->branch_tree
, s
, &leaf
);
1934 tree_content_get(&b
->branch_tree
, s
, &leaf
);
1935 if (!leaf
.versions
[1].mode
)
1936 die("Path %s not in branch", s
);
1937 tree_content_set(&b
->branch_tree
, d
,
1938 leaf
.versions
[1].sha1
,
1939 leaf
.versions
[1].mode
,
1946 static void file_change_deleteall(struct branch
*b
)
1948 release_tree_content_recursive(b
->branch_tree
.tree
);
1949 hashclr(b
->branch_tree
.versions
[0].sha1
);
1950 hashclr(b
->branch_tree
.versions
[1].sha1
);
1951 load_tree(&b
->branch_tree
);
1954 static void cmd_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
1956 if (!buf
|| size
< 46)
1957 die("Not a valid commit: %s", sha1_to_hex(b
->sha1
));
1958 if (memcmp("tree ", buf
, 5)
1959 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1960 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1961 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1962 b
->branch_tree
.versions
[1].sha1
);
1965 static void cmd_from_existing(struct branch
*b
)
1967 if (is_null_sha1(b
->sha1
)) {
1968 hashclr(b
->branch_tree
.versions
[0].sha1
);
1969 hashclr(b
->branch_tree
.versions
[1].sha1
);
1974 buf
= read_object_with_reference(b
->sha1
,
1975 commit_type
, &size
, b
->sha1
);
1976 cmd_from_commit(b
, buf
, size
);
1981 static int cmd_from(struct branch
*b
)
1986 if (prefixcmp(command_buf
.buf
, "from "))
1989 if (b
->branch_tree
.tree
) {
1990 release_tree_content_recursive(b
->branch_tree
.tree
);
1991 b
->branch_tree
.tree
= NULL
;
1994 from
= strchr(command_buf
.buf
, ' ') + 1;
1995 s
= lookup_branch(from
);
1997 die("Can't create a branch from itself: %s", b
->name
);
1999 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
2000 hashcpy(b
->sha1
, s
->sha1
);
2001 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
2002 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
2003 } else if (*from
== ':') {
2004 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
2005 struct object_entry
*oe
= find_mark(idnum
);
2006 if (oe
->type
!= OBJ_COMMIT
)
2007 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2008 hashcpy(b
->sha1
, oe
->sha1
);
2009 if (oe
->pack_id
!= MAX_PACK_ID
) {
2011 char *buf
= gfi_unpack_entry(oe
, &size
);
2012 cmd_from_commit(b
, buf
, size
);
2015 cmd_from_existing(b
);
2016 } else if (!get_sha1(from
, b
->sha1
))
2017 cmd_from_existing(b
);
2019 die("Invalid ref name or SHA1 expression: %s", from
);
2021 read_next_command();
2025 static struct hash_list
*cmd_merge(unsigned int *count
)
2027 struct hash_list
*list
= NULL
, *n
, *e
= e
;
2032 while (!prefixcmp(command_buf
.buf
, "merge ")) {
2033 from
= strchr(command_buf
.buf
, ' ') + 1;
2034 n
= xmalloc(sizeof(*n
));
2035 s
= lookup_branch(from
);
2037 hashcpy(n
->sha1
, s
->sha1
);
2038 else if (*from
== ':') {
2039 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
2040 struct object_entry
*oe
= find_mark(idnum
);
2041 if (oe
->type
!= OBJ_COMMIT
)
2042 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2043 hashcpy(n
->sha1
, oe
->sha1
);
2044 } else if (!get_sha1(from
, n
->sha1
)) {
2046 char *buf
= read_object_with_reference(n
->sha1
,
2047 commit_type
, &size
, n
->sha1
);
2048 if (!buf
|| size
< 46)
2049 die("Not a valid commit: %s", from
);
2052 die("Invalid ref name or SHA1 expression: %s", from
);
2061 read_next_command();
2066 static void cmd_new_commit(void)
2072 char *author
= NULL
;
2073 char *committer
= NULL
;
2074 struct hash_list
*merge_list
= NULL
;
2075 unsigned int merge_count
;
2077 /* Obtain the branch name from the rest of our command */
2078 sp
= strchr(command_buf
.buf
, ' ') + 1;
2079 b
= lookup_branch(sp
);
2083 read_next_command();
2085 if (!prefixcmp(command_buf
.buf
, "author ")) {
2086 author
= parse_ident(command_buf
.buf
+ 7);
2087 read_next_command();
2089 if (!prefixcmp(command_buf
.buf
, "committer ")) {
2090 committer
= parse_ident(command_buf
.buf
+ 10);
2091 read_next_command();
2094 die("Expected committer but didn't get one");
2095 msg
= cmd_data(&msglen
);
2096 read_next_command();
2098 merge_list
= cmd_merge(&merge_count
);
2100 /* ensure the branch is active/loaded */
2101 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2102 unload_one_branch();
2107 while (!command_buf
.eof
&& command_buf
.len
> 1) {
2108 if (!prefixcmp(command_buf
.buf
, "M "))
2110 else if (!prefixcmp(command_buf
.buf
, "D "))
2112 else if (!prefixcmp(command_buf
.buf
, "R "))
2113 file_change_cr(b
, 1);
2114 else if (!prefixcmp(command_buf
.buf
, "C "))
2115 file_change_cr(b
, 0);
2116 else if (!strcmp("deleteall", command_buf
.buf
))
2117 file_change_deleteall(b
);
2119 unread_command_buf
= 1;
2122 read_next_command();
2125 /* build the tree and the commit */
2126 store_tree(&b
->branch_tree
);
2127 hashcpy(b
->branch_tree
.versions
[0].sha1
,
2128 b
->branch_tree
.versions
[1].sha1
);
2129 size_dbuf(&new_data
, 114 + msglen
2132 ? strlen(author
) + strlen(committer
)
2133 : 2 * strlen(committer
)));
2134 sp
= new_data
.buffer
;
2135 sp
+= sprintf(sp
, "tree %s\n",
2136 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
2137 if (!is_null_sha1(b
->sha1
))
2138 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
2139 while (merge_list
) {
2140 struct hash_list
*next
= merge_list
->next
;
2141 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
2145 sp
+= sprintf(sp
, "author %s\n", author
? author
: committer
);
2146 sp
+= sprintf(sp
, "committer %s\n", committer
);
2148 memcpy(sp
, msg
, msglen
);
2154 if (!store_object(OBJ_COMMIT
,
2155 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
2156 NULL
, b
->sha1
, next_mark
))
2157 b
->pack_id
= pack_id
;
2158 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2161 static void cmd_new_tag(void)
2170 uintmax_t from_mark
= 0;
2171 unsigned char sha1
[20];
2173 /* Obtain the new tag name from the rest of our command */
2174 sp
= strchr(command_buf
.buf
, ' ') + 1;
2175 t
= pool_alloc(sizeof(struct tag
));
2177 t
->name
= pool_strdup(sp
);
2179 last_tag
->next_tag
= t
;
2183 read_next_command();
2186 if (prefixcmp(command_buf
.buf
, "from "))
2187 die("Expected from command, got %s", command_buf
.buf
);
2188 from
= strchr(command_buf
.buf
, ' ') + 1;
2189 s
= lookup_branch(from
);
2191 hashcpy(sha1
, s
->sha1
);
2192 } else if (*from
== ':') {
2193 struct object_entry
*oe
;
2194 from_mark
= strtoumax(from
+ 1, NULL
, 10);
2195 oe
= find_mark(from_mark
);
2196 if (oe
->type
!= OBJ_COMMIT
)
2197 die("Mark :%" PRIuMAX
" not a commit", from_mark
);
2198 hashcpy(sha1
, oe
->sha1
);
2199 } else if (!get_sha1(from
, sha1
)) {
2203 buf
= read_object_with_reference(sha1
,
2204 commit_type
, &size
, sha1
);
2205 if (!buf
|| size
< 46)
2206 die("Not a valid commit: %s", from
);
2209 die("Invalid ref name or SHA1 expression: %s", from
);
2210 read_next_command();
2213 if (prefixcmp(command_buf
.buf
, "tagger "))
2214 die("Expected tagger command, got %s", command_buf
.buf
);
2215 tagger
= parse_ident(command_buf
.buf
+ 7);
2217 /* tag payload/message */
2218 read_next_command();
2219 msg
= cmd_data(&msglen
);
2221 /* build the tag object */
2222 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
2223 sp
= new_data
.buffer
;
2224 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
2225 sp
+= sprintf(sp
, "type %s\n", commit_type
);
2226 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
2227 sp
+= sprintf(sp
, "tagger %s\n", tagger
);
2229 memcpy(sp
, msg
, msglen
);
2234 if (store_object(OBJ_TAG
, new_data
.buffer
,
2235 sp
- (char*)new_data
.buffer
,
2237 t
->pack_id
= MAX_PACK_ID
;
2239 t
->pack_id
= pack_id
;
2242 static void cmd_reset_branch(void)
2247 /* Obtain the branch name from the rest of our command */
2248 sp
= strchr(command_buf
.buf
, ' ') + 1;
2249 b
= lookup_branch(sp
);
2252 hashclr(b
->branch_tree
.versions
[0].sha1
);
2253 hashclr(b
->branch_tree
.versions
[1].sha1
);
2254 if (b
->branch_tree
.tree
) {
2255 release_tree_content_recursive(b
->branch_tree
.tree
);
2256 b
->branch_tree
.tree
= NULL
;
2261 read_next_command();
2262 if (!cmd_from(b
) && command_buf
.len
> 1)
2263 unread_command_buf
= 1;
2266 static void cmd_checkpoint(void)
2277 static void cmd_progress(void)
2279 fwrite(command_buf
.buf
, 1, command_buf
.len
- 1, stdout
);
2280 fputc('\n', stdout
);
2285 static void import_marks(const char *input_file
)
2288 FILE *f
= fopen(input_file
, "r");
2290 die("cannot read %s: %s", input_file
, strerror(errno
));
2291 while (fgets(line
, sizeof(line
), f
)) {
2294 unsigned char sha1
[20];
2295 struct object_entry
*e
;
2297 end
= strchr(line
, '\n');
2298 if (line
[0] != ':' || !end
)
2299 die("corrupt mark line: %s", line
);
2301 mark
= strtoumax(line
+ 1, &end
, 10);
2302 if (!mark
|| end
== line
+ 1
2303 || *end
!= ' ' || get_sha1(end
+ 1, sha1
))
2304 die("corrupt mark line: %s", line
);
2305 e
= find_object(sha1
);
2307 enum object_type type
= sha1_object_info(sha1
, NULL
);
2309 die("object not found: %s", sha1_to_hex(sha1
));
2310 e
= insert_object(sha1
);
2312 e
->pack_id
= MAX_PACK_ID
;
2313 e
->offset
= 1; /* just not zero! */
2315 insert_mark(mark
, e
);
2320 static const char fast_import_usage
[] =
2321 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2323 int main(int argc
, const char **argv
)
2325 unsigned int i
, show_stats
= 1;
2327 git_config(git_default_config
);
2328 alloc_objects(object_entry_alloc
);
2329 strbuf_init(&command_buf
);
2330 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2331 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2332 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2333 marks
= pool_calloc(1, sizeof(struct mark_set
));
2335 for (i
= 1; i
< argc
; i
++) {
2336 const char *a
= argv
[i
];
2338 if (*a
!= '-' || !strcmp(a
, "--"))
2340 else if (!prefixcmp(a
, "--date-format=")) {
2341 const char *fmt
= a
+ 14;
2342 if (!strcmp(fmt
, "raw"))
2343 whenspec
= WHENSPEC_RAW
;
2344 else if (!strcmp(fmt
, "rfc2822"))
2345 whenspec
= WHENSPEC_RFC2822
;
2346 else if (!strcmp(fmt
, "now"))
2347 whenspec
= WHENSPEC_NOW
;
2349 die("unknown --date-format argument %s", fmt
);
2351 else if (!prefixcmp(a
, "--max-pack-size="))
2352 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
2353 else if (!prefixcmp(a
, "--depth=")) {
2354 max_depth
= strtoul(a
+ 8, NULL
, 0);
2355 if (max_depth
> MAX_DEPTH
)
2356 die("--depth cannot exceed %u", MAX_DEPTH
);
2358 else if (!prefixcmp(a
, "--active-branches="))
2359 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
2360 else if (!prefixcmp(a
, "--import-marks="))
2361 import_marks(a
+ 15);
2362 else if (!prefixcmp(a
, "--export-marks="))
2364 else if (!prefixcmp(a
, "--export-pack-edges=")) {
2367 pack_edges
= fopen(a
+ 20, "a");
2369 die("Cannot open %s: %s", a
+ 20, strerror(errno
));
2370 } else if (!strcmp(a
, "--force"))
2372 else if (!strcmp(a
, "--quiet"))
2374 else if (!strcmp(a
, "--stats"))
2377 die("unknown option %s", a
);
2380 usage(fast_import_usage
);
2382 rc_free
= pool_alloc(cmd_save
* sizeof(*rc_free
));
2383 for (i
= 0; i
< (cmd_save
- 1); i
++)
2384 rc_free
[i
].next
= &rc_free
[i
+ 1];
2385 rc_free
[cmd_save
- 1].next
= NULL
;
2387 prepare_packed_git();
2389 set_die_routine(die_nicely
);
2391 read_next_command();
2392 if (command_buf
.eof
)
2394 else if (!strcmp("blob", command_buf
.buf
))
2396 else if (!prefixcmp(command_buf
.buf
, "commit "))
2398 else if (!prefixcmp(command_buf
.buf
, "tag "))
2400 else if (!prefixcmp(command_buf
.buf
, "reset "))
2402 else if (!strcmp("checkpoint", command_buf
.buf
))
2404 else if (!prefixcmp(command_buf
.buf
, "progress "))
2407 die("Unsupported command: %s", command_buf
.buf
);
2420 uintmax_t total_count
= 0, duplicate_count
= 0;
2421 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2422 total_count
+= object_count_by_type
[i
];
2423 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2424 duplicate_count
+= duplicate_count_by_type
[i
];
2426 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2427 fprintf(stderr
, "---------------------------------------------------------------------\n");
2428 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
2429 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
2430 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
]);
2431 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
]);
2432 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
]);
2433 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
]);
2434 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2435 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2436 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2437 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2438 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
2439 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2440 fprintf(stderr
, "---------------------------------------------------------------------\n");
2442 fprintf(stderr
, "---------------------------------------------------------------------\n");
2443 fprintf(stderr
, "\n");
2446 return failure
? 1 : 0;