2 (See Documentation/git-fast-import.txt for maintained documentation.)
3 Format of STDIN stream:
15 new_blob ::= 'blob' lf
18 file_content ::= data;
20 new_commit ::= 'commit' sp ref_str lf
22 ('author' sp name sp '<' email '>' sp when lf)?
23 'committer' sp name sp '<' email '>' sp when lf
25 ('from' sp committish lf)?
26 ('merge' sp committish lf)*
31 file_change ::= file_clr
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
44 note_obm ::= 'N' sp (hexsha1 | idnum) sp committish lf;
45 note_inm ::= 'N' sp 'inline' sp committish lf
48 new_tag ::= 'tag' sp tag_str lf
49 'from' sp committish lf
50 ('tagger' sp name sp '<' email '>' sp when lf)?
54 reset_branch ::= 'reset' sp ref_str lf
55 ('from' sp committish lf)?
58 checkpoint ::= 'checkpoint' lf
61 progress ::= 'progress' sp not_lf* 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)
74 # note: delim may be any string but must not contain lf.
75 # data_line may contain any data but must not be exactly
77 delimited_data ::= 'data' sp '<<' 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
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
94 committish ::= (ref_str | hexsha1 | sha1exp_str | idnum);
96 sha1exp_str ::= sha1exp;
98 path_str ::= path | '"' quoted(path) '"' ;
99 mode ::= '100644' | '644'
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 may appear anywhere in the input, except
136 # within a data command. Any form of the data command
137 # always escapes the related input from comment processing.
139 # In case it is not clear, the '#' that starts the comment
140 # must be the first character on that line (an lf
143 comment ::= '#' not_lf* lf;
144 not_lf ::= # Any byte that is not ASCII newline (LF);
156 #include "csum-file.h"
158 #include "exec_cmd.h"
160 #define PACK_ID_BITS 16
161 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
162 #define DEPTH_BITS 13
163 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
167 struct object_entry
*next
;
169 uint32_t type
: TYPE_BITS
,
170 pack_id
: PACK_ID_BITS
,
172 unsigned char sha1
[20];
175 struct object_entry_pool
177 struct object_entry_pool
*next_pool
;
178 struct object_entry
*next_free
;
179 struct object_entry
*end
;
180 struct object_entry entries
[FLEX_ARRAY
]; /* more */
186 struct object_entry
*marked
[1024];
187 struct mark_set
*sets
[1024];
197 unsigned no_swap
: 1;
202 struct mem_pool
*next_pool
;
205 uintmax_t space
[FLEX_ARRAY
]; /* more */
210 struct atom_str
*next_atom
;
211 unsigned short str_len
;
212 char str_dat
[FLEX_ARRAY
]; /* more */
218 struct tree_content
*tree
;
219 struct atom_str
*name
;
223 unsigned char sha1
[20];
229 unsigned int entry_capacity
; /* must match avail_tree_content */
230 unsigned int entry_count
;
231 unsigned int delta_depth
;
232 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
235 struct avail_tree_content
237 unsigned int entry_capacity
; /* must match tree_content */
238 struct avail_tree_content
*next_avail
;
243 struct branch
*table_next_branch
;
244 struct branch
*active_next_branch
;
246 struct tree_entry branch_tree
;
247 uintmax_t last_commit
;
249 unsigned pack_id
: PACK_ID_BITS
;
250 unsigned char sha1
[20];
255 struct tag
*next_tag
;
257 unsigned int pack_id
;
258 unsigned char sha1
[20];
263 struct hash_list
*next
;
264 unsigned char sha1
[20];
273 struct recent_command
275 struct recent_command
*prev
;
276 struct recent_command
*next
;
280 /* Configured limits on output */
281 static unsigned long max_depth
= 10;
282 static off_t max_packsize
= (1LL << 32) - 1;
283 static int force_update
;
284 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
285 static int pack_compression_seen
;
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
;
298 static unsigned int show_stats
= 1;
299 static int global_argc
;
300 static const char **global_argv
;
303 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
304 static size_t total_allocd
;
305 static struct mem_pool
*mem_pool
;
307 /* Atom management */
308 static unsigned int atom_table_sz
= 4451;
309 static unsigned int atom_cnt
;
310 static struct atom_str
**atom_table
;
312 /* The .pack file being generated */
313 static unsigned int pack_id
;
314 static struct packed_git
*pack_data
;
315 static struct packed_git
**all_packs
;
316 static unsigned long pack_size
;
318 /* Table of objects we've written. */
319 static unsigned int object_entry_alloc
= 5000;
320 static struct object_entry_pool
*blocks
;
321 static struct object_entry
*object_table
[1 << 16];
322 static struct mark_set
*marks
;
323 static const char *mark_file
;
324 static const char *input_file
;
327 static struct last_object last_blob
= { STRBUF_INIT
, 0, 0, 0 };
329 /* Tree management */
330 static unsigned int tree_entry_alloc
= 1000;
331 static void *avail_tree_entry
;
332 static unsigned int avail_tree_table_sz
= 100;
333 static struct avail_tree_content
**avail_tree_table
;
334 static struct strbuf old_tree
= STRBUF_INIT
;
335 static struct strbuf new_tree
= STRBUF_INIT
;
338 static unsigned long max_active_branches
= 5;
339 static unsigned long cur_active_branches
;
340 static unsigned long branch_table_sz
= 1039;
341 static struct branch
**branch_table
;
342 static struct branch
*active_branches
;
345 static struct tag
*first_tag
;
346 static struct tag
*last_tag
;
348 /* Input stream parsing */
349 static whenspec_type whenspec
= WHENSPEC_RAW
;
350 static struct strbuf command_buf
= STRBUF_INIT
;
351 static int unread_command_buf
;
352 static struct recent_command cmd_hist
= {&cmd_hist
, &cmd_hist
, NULL
};
353 static struct recent_command
*cmd_tail
= &cmd_hist
;
354 static struct recent_command
*rc_free
;
355 static unsigned int cmd_save
= 100;
356 static uintmax_t next_mark
;
357 static struct strbuf new_data
= STRBUF_INIT
;
358 static int options_enabled
;
359 static int seen_non_option_command
;
361 static void parse_argv(void);
363 static void write_branch_report(FILE *rpt
, struct branch
*b
)
365 fprintf(rpt
, "%s:\n", b
->name
);
367 fprintf(rpt
, " status :");
369 fputs(" active", rpt
);
370 if (b
->branch_tree
.tree
)
371 fputs(" loaded", rpt
);
372 if (is_null_sha1(b
->branch_tree
.versions
[1].sha1
))
373 fputs(" dirty", rpt
);
376 fprintf(rpt
, " tip commit : %s\n", sha1_to_hex(b
->sha1
));
377 fprintf(rpt
, " old tree : %s\n", sha1_to_hex(b
->branch_tree
.versions
[0].sha1
));
378 fprintf(rpt
, " cur tree : %s\n", sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
379 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
381 fputs(" last pack : ", rpt
);
382 if (b
->pack_id
< MAX_PACK_ID
)
383 fprintf(rpt
, "%u", b
->pack_id
);
389 static void dump_marks_helper(FILE *, uintmax_t, struct mark_set
*);
391 static void write_crash_report(const char *err
)
393 char *loc
= git_path("fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
394 FILE *rpt
= fopen(loc
, "w");
397 struct recent_command
*rc
;
400 error("can't write crash report %s: %s", loc
, strerror(errno
));
404 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
406 fprintf(rpt
, "fast-import crash report:\n");
407 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
408 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
409 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_LOCAL
));
412 fputs("fatal: ", rpt
);
417 fputs("Most Recent Commands Before Crash\n", rpt
);
418 fputs("---------------------------------\n", rpt
);
419 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
420 if (rc
->next
== &cmd_hist
)
429 fputs("Active Branch LRU\n", rpt
);
430 fputs("-----------------\n", rpt
);
431 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
433 max_active_branches
);
435 fputs(" pos clock name\n", rpt
);
436 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
437 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
438 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
439 ++lu
, b
->last_commit
, b
->name
);
442 fputs("Inactive Branches\n", rpt
);
443 fputs("-----------------\n", rpt
);
444 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
445 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
446 write_branch_report(rpt
, b
);
452 fputs("Annotated Tags\n", rpt
);
453 fputs("--------------\n", rpt
);
454 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
455 fputs(sha1_to_hex(tg
->sha1
), rpt
);
457 fputs(tg
->name
, rpt
);
463 fputs("Marks\n", rpt
);
464 fputs("-----\n", rpt
);
466 fprintf(rpt
, " exported to %s\n", mark_file
);
468 dump_marks_helper(rpt
, 0, marks
);
471 fputs("-------------------\n", rpt
);
472 fputs("END OF CRASH REPORT\n", rpt
);
476 static void end_packfile(void);
477 static void unkeep_all_packs(void);
478 static void dump_marks(void);
480 static NORETURN
void die_nicely(const char *err
, va_list params
)
483 char message
[2 * PATH_MAX
];
485 vsnprintf(message
, sizeof(message
), err
, params
);
486 fputs("fatal: ", stderr
);
487 fputs(message
, stderr
);
492 write_crash_report(message
);
500 static void alloc_objects(unsigned int cnt
)
502 struct object_entry_pool
*b
;
504 b
= xmalloc(sizeof(struct object_entry_pool
)
505 + cnt
* sizeof(struct object_entry
));
506 b
->next_pool
= blocks
;
507 b
->next_free
= b
->entries
;
508 b
->end
= b
->entries
+ cnt
;
513 static struct object_entry
*new_object(unsigned char *sha1
)
515 struct object_entry
*e
;
517 if (blocks
->next_free
== blocks
->end
)
518 alloc_objects(object_entry_alloc
);
520 e
= blocks
->next_free
++;
521 hashcpy(e
->sha1
, sha1
);
525 static struct object_entry
*find_object(unsigned char *sha1
)
527 unsigned int h
= sha1
[0] << 8 | sha1
[1];
528 struct object_entry
*e
;
529 for (e
= object_table
[h
]; e
; e
= e
->next
)
530 if (!hashcmp(sha1
, e
->sha1
))
535 static struct object_entry
*insert_object(unsigned char *sha1
)
537 unsigned int h
= sha1
[0] << 8 | sha1
[1];
538 struct object_entry
*e
= object_table
[h
];
539 struct object_entry
*p
= NULL
;
542 if (!hashcmp(sha1
, e
->sha1
))
548 e
= new_object(sha1
);
558 static unsigned int hc_str(const char *s
, size_t len
)
566 static void *pool_alloc(size_t len
)
571 /* round up to a 'uintmax_t' alignment */
572 if (len
& (sizeof(uintmax_t) - 1))
573 len
+= sizeof(uintmax_t) - (len
& (sizeof(uintmax_t) - 1));
575 for (p
= mem_pool
; p
; p
= p
->next_pool
)
576 if ((p
->end
- p
->next_free
>= len
))
580 if (len
>= (mem_pool_alloc
/2)) {
584 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
585 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
586 p
->next_pool
= mem_pool
;
587 p
->next_free
= (char *) p
->space
;
588 p
->end
= p
->next_free
+ mem_pool_alloc
;
597 static void *pool_calloc(size_t count
, size_t size
)
599 size_t len
= count
* size
;
600 void *r
= pool_alloc(len
);
605 static char *pool_strdup(const char *s
)
607 char *r
= pool_alloc(strlen(s
) + 1);
612 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
614 struct mark_set
*s
= marks
;
615 while ((idnum
>> s
->shift
) >= 1024) {
616 s
= pool_calloc(1, sizeof(struct mark_set
));
617 s
->shift
= marks
->shift
+ 10;
618 s
->data
.sets
[0] = marks
;
622 uintmax_t i
= idnum
>> s
->shift
;
623 idnum
-= i
<< s
->shift
;
624 if (!s
->data
.sets
[i
]) {
625 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
626 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
630 if (!s
->data
.marked
[idnum
])
632 s
->data
.marked
[idnum
] = oe
;
635 static struct object_entry
*find_mark(uintmax_t idnum
)
637 uintmax_t orig_idnum
= idnum
;
638 struct mark_set
*s
= marks
;
639 struct object_entry
*oe
= NULL
;
640 if ((idnum
>> s
->shift
) < 1024) {
641 while (s
&& s
->shift
) {
642 uintmax_t i
= idnum
>> s
->shift
;
643 idnum
-= i
<< s
->shift
;
647 oe
= s
->data
.marked
[idnum
];
650 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
654 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
656 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
659 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
660 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
663 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
665 strncpy(c
->str_dat
, s
, len
);
667 c
->next_atom
= atom_table
[hc
];
673 static struct branch
*lookup_branch(const char *name
)
675 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
678 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
679 if (!strcmp(name
, b
->name
))
684 static struct branch
*new_branch(const char *name
)
686 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
687 struct branch
*b
= lookup_branch(name
);
690 die("Invalid attempt to create duplicate branch: %s", name
);
691 switch (check_ref_format(name
)) {
692 case 0: break; /* its valid */
693 case CHECK_REF_FORMAT_ONELEVEL
:
694 break; /* valid, but too few '/', allow anyway */
696 die("Branch name doesn't conform to GIT standards: %s", name
);
699 b
= pool_calloc(1, sizeof(struct branch
));
700 b
->name
= pool_strdup(name
);
701 b
->table_next_branch
= branch_table
[hc
];
702 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
703 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
705 b
->pack_id
= MAX_PACK_ID
;
706 branch_table
[hc
] = b
;
711 static unsigned int hc_entries(unsigned int cnt
)
713 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
714 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
717 static struct tree_content
*new_tree_content(unsigned int cnt
)
719 struct avail_tree_content
*f
, *l
= NULL
;
720 struct tree_content
*t
;
721 unsigned int hc
= hc_entries(cnt
);
723 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
724 if (f
->entry_capacity
>= cnt
)
729 l
->next_avail
= f
->next_avail
;
731 avail_tree_table
[hc
] = f
->next_avail
;
733 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
734 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
735 f
->entry_capacity
= cnt
;
738 t
= (struct tree_content
*)f
;
744 static void release_tree_entry(struct tree_entry
*e
);
745 static void release_tree_content(struct tree_content
*t
)
747 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
748 unsigned int hc
= hc_entries(f
->entry_capacity
);
749 f
->next_avail
= avail_tree_table
[hc
];
750 avail_tree_table
[hc
] = f
;
753 static void release_tree_content_recursive(struct tree_content
*t
)
756 for (i
= 0; i
< t
->entry_count
; i
++)
757 release_tree_entry(t
->entries
[i
]);
758 release_tree_content(t
);
761 static struct tree_content
*grow_tree_content(
762 struct tree_content
*t
,
765 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
766 r
->entry_count
= t
->entry_count
;
767 r
->delta_depth
= t
->delta_depth
;
768 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
769 release_tree_content(t
);
773 static struct tree_entry
*new_tree_entry(void)
775 struct tree_entry
*e
;
777 if (!avail_tree_entry
) {
778 unsigned int n
= tree_entry_alloc
;
779 total_allocd
+= n
* sizeof(struct tree_entry
);
780 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
782 *((void**)e
) = e
+ 1;
788 e
= avail_tree_entry
;
789 avail_tree_entry
= *((void**)e
);
793 static void release_tree_entry(struct tree_entry
*e
)
796 release_tree_content_recursive(e
->tree
);
797 *((void**)e
) = avail_tree_entry
;
798 avail_tree_entry
= e
;
801 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
803 struct tree_content
*d
;
804 struct tree_entry
*a
, *b
;
809 d
= new_tree_content(s
->entry_count
);
810 for (i
= 0; i
< s
->entry_count
; i
++) {
812 b
= new_tree_entry();
813 memcpy(b
, a
, sizeof(*a
));
814 if (a
->tree
&& is_null_sha1(b
->versions
[1].sha1
))
815 b
->tree
= dup_tree_content(a
->tree
);
820 d
->entry_count
= s
->entry_count
;
821 d
->delta_depth
= s
->delta_depth
;
826 static void start_packfile(void)
828 static char tmpfile
[PATH_MAX
];
829 struct packed_git
*p
;
830 struct pack_header hdr
;
833 pack_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
834 "pack/tmp_pack_XXXXXX");
835 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
836 strcpy(p
->pack_name
, tmpfile
);
837 p
->pack_fd
= pack_fd
;
839 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
840 hdr
.hdr_version
= htonl(2);
842 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
845 pack_size
= sizeof(hdr
);
848 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
849 all_packs
[pack_id
] = p
;
852 static int oecmp (const void *a_
, const void *b_
)
854 struct object_entry
*a
= *((struct object_entry
**)a_
);
855 struct object_entry
*b
= *((struct object_entry
**)b_
);
856 return hashcmp(a
->sha1
, b
->sha1
);
859 static char *create_index(void)
861 static char tmpfile
[PATH_MAX
];
864 struct object_entry
**idx
, **c
, **last
, *e
;
865 struct object_entry_pool
*o
;
869 /* Build the sorted table of object IDs. */
870 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
872 for (o
= blocks
; o
; o
= o
->next_pool
)
873 for (e
= o
->next_free
; e
-- != o
->entries
;)
874 if (pack_id
== e
->pack_id
)
876 last
= idx
+ object_count
;
878 die("internal consistency error creating the index");
879 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
881 /* Generate the fan-out array. */
883 for (i
= 0; i
< 256; i
++) {
884 struct object_entry
**next
= c
;
885 while (next
< last
) {
886 if ((*next
)->sha1
[0] != i
)
890 array
[i
] = htonl(next
- idx
);
894 idx_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
895 "pack/tmp_idx_XXXXXX");
896 f
= sha1fd(idx_fd
, tmpfile
);
897 sha1write(f
, array
, 256 * sizeof(int));
899 for (c
= idx
; c
!= last
; c
++) {
900 uint32_t offset
= htonl((*c
)->offset
);
901 sha1write(f
, &offset
, 4);
902 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
903 git_SHA1_Update(&ctx
, (*c
)->sha1
, 20);
905 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
906 sha1close(f
, NULL
, CSUM_FSYNC
);
908 git_SHA1_Final(pack_data
->sha1
, &ctx
);
912 static char *keep_pack(char *curr_index_name
)
914 static char name
[PATH_MAX
];
915 static const char *keep_msg
= "fast-import";
918 keep_fd
= odb_pack_keep(name
, sizeof(name
), pack_data
->sha1
);
920 die_errno("cannot create keep file");
921 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
923 die_errno("failed to write keep file");
925 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
926 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
927 if (move_temp_to_file(pack_data
->pack_name
, name
))
928 die("cannot store pack file");
930 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
931 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
932 if (move_temp_to_file(curr_index_name
, name
))
933 die("cannot store index file");
937 static void unkeep_all_packs(void)
939 static char name
[PATH_MAX
];
942 for (k
= 0; k
< pack_id
; k
++) {
943 struct packed_git
*p
= all_packs
[k
];
944 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
945 get_object_directory(), sha1_to_hex(p
->sha1
));
946 unlink_or_warn(name
);
950 static void end_packfile(void)
952 struct packed_git
*old_p
= pack_data
, *new_p
;
954 clear_delta_base_cache();
961 close_pack_windows(pack_data
);
962 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->sha1
,
963 pack_data
->pack_name
, object_count
,
965 close(pack_data
->pack_fd
);
966 idx_name
= keep_pack(create_index());
968 /* Register the packfile with core git's machinery. */
969 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
971 die("core git rejected index %s", idx_name
);
972 all_packs
[pack_id
] = new_p
;
973 install_packed_git(new_p
);
975 /* Print the boundary */
977 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
978 for (i
= 0; i
< branch_table_sz
; i
++) {
979 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
980 if (b
->pack_id
== pack_id
)
981 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
984 for (t
= first_tag
; t
; t
= t
->next_tag
) {
985 if (t
->pack_id
== pack_id
)
986 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
988 fputc('\n', pack_edges
);
995 close(old_p
->pack_fd
);
996 unlink_or_warn(old_p
->pack_name
);
1000 /* We can't carry a delta across packfiles. */
1001 strbuf_release(&last_blob
.data
);
1002 last_blob
.offset
= 0;
1003 last_blob
.depth
= 0;
1006 static void cycle_packfile(void)
1012 static size_t encode_header(
1013 enum object_type type
,
1020 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
1021 die("bad type %d", type
);
1023 c
= (type
<< 4) | (size
& 15);
1035 static int store_object(
1036 enum object_type type
,
1038 struct last_object
*last
,
1039 unsigned char *sha1out
,
1043 struct object_entry
*e
;
1044 unsigned char hdr
[96];
1045 unsigned char sha1
[20];
1046 unsigned long hdrlen
, deltalen
;
1050 hdrlen
= sprintf((char *)hdr
,"%s %lu", typename(type
),
1051 (unsigned long)dat
->len
) + 1;
1053 git_SHA1_Update(&c
, hdr
, hdrlen
);
1054 git_SHA1_Update(&c
, dat
->buf
, dat
->len
);
1055 git_SHA1_Final(sha1
, &c
);
1057 hashcpy(sha1out
, sha1
);
1059 e
= insert_object(sha1
);
1061 insert_mark(mark
, e
);
1063 duplicate_count_by_type
[type
]++;
1065 } else if (find_sha1_pack(sha1
, packed_git
)) {
1067 e
->pack_id
= MAX_PACK_ID
;
1068 e
->offset
= 1; /* just not zero! */
1069 duplicate_count_by_type
[type
]++;
1073 if (last
&& last
->data
.buf
&& last
->depth
< max_depth
) {
1074 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
1077 if (delta
&& deltalen
>= dat
->len
) {
1084 memset(&s
, 0, sizeof(s
));
1085 deflateInit(&s
, pack_compression_level
);
1088 s
.avail_in
= deltalen
;
1090 s
.next_in
= (void *)dat
->buf
;
1091 s
.avail_in
= dat
->len
;
1093 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
1094 s
.next_out
= out
= xmalloc(s
.avail_out
);
1095 while (deflate(&s
, Z_FINISH
) == Z_OK
)
1099 /* Determine if we should auto-checkpoint. */
1100 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
1101 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
1103 /* This new object needs to *not* have the current pack_id. */
1104 e
->pack_id
= pack_id
+ 1;
1107 /* We cannot carry a delta into the new pack. */
1112 memset(&s
, 0, sizeof(s
));
1113 deflateInit(&s
, pack_compression_level
);
1114 s
.next_in
= (void *)dat
->buf
;
1115 s
.avail_in
= dat
->len
;
1116 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
1117 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
1118 while (deflate(&s
, Z_FINISH
) == Z_OK
)
1125 e
->pack_id
= pack_id
;
1126 e
->offset
= pack_size
;
1128 object_count_by_type
[type
]++;
1131 unsigned long ofs
= e
->offset
- last
->offset
;
1132 unsigned pos
= sizeof(hdr
) - 1;
1134 delta_count_by_type
[type
]++;
1135 e
->depth
= last
->depth
+ 1;
1137 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
1138 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
1139 pack_size
+= hdrlen
;
1141 hdr
[pos
] = ofs
& 127;
1143 hdr
[--pos
] = 128 | (--ofs
& 127);
1144 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
1145 pack_size
+= sizeof(hdr
) - pos
;
1148 hdrlen
= encode_header(type
, dat
->len
, hdr
);
1149 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
1150 pack_size
+= hdrlen
;
1153 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
1154 pack_size
+= s
.total_out
;
1159 if (last
->no_swap
) {
1162 strbuf_swap(&last
->data
, dat
);
1164 last
->offset
= e
->offset
;
1165 last
->depth
= e
->depth
;
1170 /* All calls must be guarded by find_object() or find_mark() to
1171 * ensure the 'struct object_entry' passed was written by this
1172 * process instance. We unpack the entry by the offset, avoiding
1173 * the need for the corresponding .idx file. This unpacking rule
1174 * works because we only use OBJ_REF_DELTA within the packfiles
1175 * created by fast-import.
1177 * oe must not be NULL. Such an oe usually comes from giving
1178 * an unknown SHA-1 to find_object() or an undefined mark to
1179 * find_mark(). Callers must test for this condition and use
1180 * the standard read_sha1_file() when it happens.
1182 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1183 * find_mark(), where the mark was reloaded from an existing marks
1184 * file and is referencing an object that this fast-import process
1185 * instance did not write out to a packfile. Callers must test for
1186 * this condition and use read_sha1_file() instead.
1188 static void *gfi_unpack_entry(
1189 struct object_entry
*oe
,
1190 unsigned long *sizep
)
1192 enum object_type type
;
1193 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1194 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ 20)) {
1195 /* The object is stored in the packfile we are writing to
1196 * and we have modified it since the last time we scanned
1197 * back to read a previously written object. If an old
1198 * window covered [p->pack_size, p->pack_size + 20) its
1199 * data is stale and is not valid. Closing all windows
1200 * and updating the packfile length ensures we can read
1201 * the newly written data.
1203 close_pack_windows(p
);
1205 /* We have to offer 20 bytes additional on the end of
1206 * the packfile as the core unpacker code assumes the
1207 * footer is present at the file end and must promise
1208 * at least 20 bytes within any window it maps. But
1209 * we don't actually create the footer here.
1211 p
->pack_size
= pack_size
+ 20;
1213 return unpack_entry(p
, oe
->offset
, &type
, sizep
);
1216 static const char *get_mode(const char *str
, uint16_t *modep
)
1221 while ((c
= *str
++) != ' ') {
1222 if (c
< '0' || c
> '7')
1224 mode
= (mode
<< 3) + (c
- '0');
1230 static void load_tree(struct tree_entry
*root
)
1232 unsigned char *sha1
= root
->versions
[1].sha1
;
1233 struct object_entry
*myoe
;
1234 struct tree_content
*t
;
1239 root
->tree
= t
= new_tree_content(8);
1240 if (is_null_sha1(sha1
))
1243 myoe
= find_object(sha1
);
1244 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1245 if (myoe
->type
!= OBJ_TREE
)
1246 die("Not a tree: %s", sha1_to_hex(sha1
));
1247 t
->delta_depth
= myoe
->depth
;
1248 buf
= gfi_unpack_entry(myoe
, &size
);
1250 die("Can't load tree %s", sha1_to_hex(sha1
));
1252 enum object_type type
;
1253 buf
= read_sha1_file(sha1
, &type
, &size
);
1254 if (!buf
|| type
!= OBJ_TREE
)
1255 die("Can't load tree %s", sha1_to_hex(sha1
));
1259 while (c
!= (buf
+ size
)) {
1260 struct tree_entry
*e
= new_tree_entry();
1262 if (t
->entry_count
== t
->entry_capacity
)
1263 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1264 t
->entries
[t
->entry_count
++] = e
;
1267 c
= get_mode(c
, &e
->versions
[1].mode
);
1269 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1270 e
->versions
[0].mode
= e
->versions
[1].mode
;
1271 e
->name
= to_atom(c
, strlen(c
));
1272 c
+= e
->name
->str_len
+ 1;
1273 hashcpy(e
->versions
[0].sha1
, (unsigned char *)c
);
1274 hashcpy(e
->versions
[1].sha1
, (unsigned char *)c
);
1280 static int tecmp0 (const void *_a
, const void *_b
)
1282 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1283 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1284 return base_name_compare(
1285 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1286 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1289 static int tecmp1 (const void *_a
, const void *_b
)
1291 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1292 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1293 return base_name_compare(
1294 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1295 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1298 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1304 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1306 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1308 for (i
= 0; i
< t
->entry_count
; i
++) {
1309 if (t
->entries
[i
]->versions
[v
].mode
)
1310 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1314 strbuf_grow(b
, maxlen
);
1315 for (i
= 0; i
< t
->entry_count
; i
++) {
1316 struct tree_entry
*e
= t
->entries
[i
];
1317 if (!e
->versions
[v
].mode
)
1319 strbuf_addf(b
, "%o %s%c", (unsigned int)e
->versions
[v
].mode
,
1320 e
->name
->str_dat
, '\0');
1321 strbuf_add(b
, e
->versions
[v
].sha1
, 20);
1325 static void store_tree(struct tree_entry
*root
)
1327 struct tree_content
*t
= root
->tree
;
1328 unsigned int i
, j
, del
;
1329 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1330 struct object_entry
*le
;
1332 if (!is_null_sha1(root
->versions
[1].sha1
))
1335 for (i
= 0; i
< t
->entry_count
; i
++) {
1336 if (t
->entries
[i
]->tree
)
1337 store_tree(t
->entries
[i
]);
1340 le
= find_object(root
->versions
[0].sha1
);
1341 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1342 mktree(t
, 0, &old_tree
);
1344 lo
.offset
= le
->offset
;
1345 lo
.depth
= t
->delta_depth
;
1348 mktree(t
, 1, &new_tree
);
1349 store_object(OBJ_TREE
, &new_tree
, &lo
, root
->versions
[1].sha1
, 0);
1351 t
->delta_depth
= lo
.depth
;
1352 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1353 struct tree_entry
*e
= t
->entries
[i
];
1354 if (e
->versions
[1].mode
) {
1355 e
->versions
[0].mode
= e
->versions
[1].mode
;
1356 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1357 t
->entries
[j
++] = e
;
1359 release_tree_entry(e
);
1363 t
->entry_count
-= del
;
1366 static int tree_content_set(
1367 struct tree_entry
*root
,
1369 const unsigned char *sha1
,
1370 const uint16_t mode
,
1371 struct tree_content
*subtree
)
1373 struct tree_content
*t
= root
->tree
;
1376 struct tree_entry
*e
;
1378 slash1
= strchr(p
, '/');
1384 die("Empty path component found in input");
1385 if (!slash1
&& !S_ISDIR(mode
) && subtree
)
1386 die("Non-directories cannot have subtrees");
1388 for (i
= 0; i
< t
->entry_count
; i
++) {
1390 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1393 && e
->versions
[1].mode
== mode
1394 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1396 e
->versions
[1].mode
= mode
;
1397 hashcpy(e
->versions
[1].sha1
, sha1
);
1399 release_tree_content_recursive(e
->tree
);
1401 hashclr(root
->versions
[1].sha1
);
1404 if (!S_ISDIR(e
->versions
[1].mode
)) {
1405 e
->tree
= new_tree_content(8);
1406 e
->versions
[1].mode
= S_IFDIR
;
1410 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
)) {
1411 hashclr(root
->versions
[1].sha1
);
1418 if (t
->entry_count
== t
->entry_capacity
)
1419 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1420 e
= new_tree_entry();
1421 e
->name
= to_atom(p
, n
);
1422 e
->versions
[0].mode
= 0;
1423 hashclr(e
->versions
[0].sha1
);
1424 t
->entries
[t
->entry_count
++] = e
;
1426 e
->tree
= new_tree_content(8);
1427 e
->versions
[1].mode
= S_IFDIR
;
1428 tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
);
1431 e
->versions
[1].mode
= mode
;
1432 hashcpy(e
->versions
[1].sha1
, sha1
);
1434 hashclr(root
->versions
[1].sha1
);
1438 static int tree_content_remove(
1439 struct tree_entry
*root
,
1441 struct tree_entry
*backup_leaf
)
1443 struct tree_content
*t
= root
->tree
;
1446 struct tree_entry
*e
;
1448 slash1
= strchr(p
, '/');
1454 for (i
= 0; i
< t
->entry_count
; i
++) {
1456 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1457 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1461 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
)) {
1462 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1463 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1464 hashclr(root
->versions
[1].sha1
);
1478 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1480 release_tree_content_recursive(e
->tree
);
1482 e
->versions
[1].mode
= 0;
1483 hashclr(e
->versions
[1].sha1
);
1484 hashclr(root
->versions
[1].sha1
);
1488 static int tree_content_get(
1489 struct tree_entry
*root
,
1491 struct tree_entry
*leaf
)
1493 struct tree_content
*t
= root
->tree
;
1496 struct tree_entry
*e
;
1498 slash1
= strchr(p
, '/');
1504 for (i
= 0; i
< t
->entry_count
; i
++) {
1506 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1508 memcpy(leaf
, e
, sizeof(*leaf
));
1509 if (e
->tree
&& is_null_sha1(e
->versions
[1].sha1
))
1510 leaf
->tree
= dup_tree_content(e
->tree
);
1515 if (!S_ISDIR(e
->versions
[1].mode
))
1519 return tree_content_get(e
, slash1
+ 1, leaf
);
1525 static int update_branch(struct branch
*b
)
1527 static const char *msg
= "fast-import";
1528 struct ref_lock
*lock
;
1529 unsigned char old_sha1
[20];
1531 if (is_null_sha1(b
->sha1
))
1533 if (read_ref(b
->name
, old_sha1
))
1535 lock
= lock_any_ref_for_update(b
->name
, old_sha1
, 0);
1537 return error("Unable to lock %s", b
->name
);
1538 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1539 struct commit
*old_cmit
, *new_cmit
;
1541 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1542 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1543 if (!old_cmit
|| !new_cmit
) {
1545 return error("Branch %s is missing commits.", b
->name
);
1548 if (!in_merge_bases(old_cmit
, &new_cmit
, 1)) {
1550 warning("Not updating %s"
1551 " (new tip %s does not contain %s)",
1552 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1556 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1557 return error("Unable to update %s", b
->name
);
1561 static void dump_branches(void)
1566 for (i
= 0; i
< branch_table_sz
; i
++) {
1567 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1568 failure
|= update_branch(b
);
1572 static void dump_tags(void)
1574 static const char *msg
= "fast-import";
1576 struct ref_lock
*lock
;
1577 char ref_name
[PATH_MAX
];
1579 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1580 sprintf(ref_name
, "tags/%s", t
->name
);
1581 lock
= lock_ref_sha1(ref_name
, NULL
);
1582 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1583 failure
|= error("Unable to update %s", ref_name
);
1587 static void dump_marks_helper(FILE *f
,
1593 for (k
= 0; k
< 1024; k
++) {
1594 if (m
->data
.sets
[k
])
1595 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1599 for (k
= 0; k
< 1024; k
++) {
1600 if (m
->data
.marked
[k
])
1601 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1602 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1607 static void dump_marks(void)
1609 static struct lock_file mark_lock
;
1616 mark_fd
= hold_lock_file_for_update(&mark_lock
, mark_file
, 0);
1618 failure
|= error("Unable to write marks file %s: %s",
1619 mark_file
, strerror(errno
));
1623 f
= fdopen(mark_fd
, "w");
1625 int saved_errno
= errno
;
1626 rollback_lock_file(&mark_lock
);
1627 failure
|= error("Unable to write marks file %s: %s",
1628 mark_file
, strerror(saved_errno
));
1633 * Since the lock file was fdopen()'ed, it should not be close()'ed.
1634 * Assign -1 to the lock file descriptor so that commit_lock_file()
1635 * won't try to close() it.
1639 dump_marks_helper(f
, 0, marks
);
1640 if (ferror(f
) || fclose(f
)) {
1641 int saved_errno
= errno
;
1642 rollback_lock_file(&mark_lock
);
1643 failure
|= error("Unable to write marks file %s: %s",
1644 mark_file
, strerror(saved_errno
));
1648 if (commit_lock_file(&mark_lock
)) {
1649 int saved_errno
= errno
;
1650 rollback_lock_file(&mark_lock
);
1651 failure
|= error("Unable to commit marks file %s: %s",
1652 mark_file
, strerror(saved_errno
));
1657 static void read_marks(void)
1660 FILE *f
= fopen(input_file
, "r");
1662 die_errno("cannot read '%s'", input_file
);
1663 while (fgets(line
, sizeof(line
), f
)) {
1666 unsigned char sha1
[20];
1667 struct object_entry
*e
;
1669 end
= strchr(line
, '\n');
1670 if (line
[0] != ':' || !end
)
1671 die("corrupt mark line: %s", line
);
1673 mark
= strtoumax(line
+ 1, &end
, 10);
1674 if (!mark
|| end
== line
+ 1
1675 || *end
!= ' ' || get_sha1(end
+ 1, sha1
))
1676 die("corrupt mark line: %s", line
);
1677 e
= find_object(sha1
);
1679 enum object_type type
= sha1_object_info(sha1
, NULL
);
1681 die("object not found: %s", sha1_to_hex(sha1
));
1682 e
= insert_object(sha1
);
1684 e
->pack_id
= MAX_PACK_ID
;
1685 e
->offset
= 1; /* just not zero! */
1687 insert_mark(mark
, e
);
1693 static int read_next_command(void)
1695 static int stdin_eof
= 0;
1698 unread_command_buf
= 0;
1703 if (unread_command_buf
) {
1704 unread_command_buf
= 0;
1706 struct recent_command
*rc
;
1708 strbuf_detach(&command_buf
, NULL
);
1709 stdin_eof
= strbuf_getline(&command_buf
, stdin
, '\n');
1713 if (!seen_non_option_command
1714 && prefixcmp(command_buf
.buf
, "feature ")
1715 && prefixcmp(command_buf
.buf
, "option ")) {
1724 cmd_hist
.next
= rc
->next
;
1725 cmd_hist
.next
->prev
= &cmd_hist
;
1729 rc
->buf
= command_buf
.buf
;
1730 rc
->prev
= cmd_tail
;
1731 rc
->next
= cmd_hist
.prev
;
1732 rc
->prev
->next
= rc
;
1735 } while (command_buf
.buf
[0] == '#');
1740 static void skip_optional_lf(void)
1742 int term_char
= fgetc(stdin
);
1743 if (term_char
!= '\n' && term_char
!= EOF
)
1744 ungetc(term_char
, stdin
);
1747 static void parse_mark(void)
1749 if (!prefixcmp(command_buf
.buf
, "mark :")) {
1750 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1751 read_next_command();
1757 static void parse_data(struct strbuf
*sb
)
1761 if (prefixcmp(command_buf
.buf
, "data "))
1762 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1764 if (!prefixcmp(command_buf
.buf
+ 5, "<<")) {
1765 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1766 size_t term_len
= command_buf
.len
- 5 - 2;
1768 strbuf_detach(&command_buf
, NULL
);
1770 if (strbuf_getline(&command_buf
, stdin
, '\n') == EOF
)
1771 die("EOF in data (terminator '%s' not found)", term
);
1772 if (term_len
== command_buf
.len
1773 && !strcmp(term
, command_buf
.buf
))
1775 strbuf_addbuf(sb
, &command_buf
);
1776 strbuf_addch(sb
, '\n');
1781 size_t n
= 0, length
;
1783 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1785 while (n
< length
) {
1786 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1787 if (!s
&& feof(stdin
))
1788 die("EOF in data (%lu bytes remaining)",
1789 (unsigned long)(length
- n
));
1797 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1799 const char *orig_src
= src
;
1804 strtoul(src
, &endp
, 10);
1805 if (errno
|| endp
== src
|| *endp
!= ' ')
1809 if (*src
!= '-' && *src
!= '+')
1812 strtoul(src
+ 1, &endp
, 10);
1813 if (errno
|| endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1816 strcpy(result
, orig_src
);
1820 static char *parse_ident(const char *buf
)
1826 gt
= strrchr(buf
, '>');
1828 die("Missing > in ident string: %s", buf
);
1831 die("Missing space after > in ident string: %s", buf
);
1833 name_len
= gt
- buf
;
1834 ident
= xmalloc(name_len
+ 24);
1835 strncpy(ident
, buf
, name_len
);
1839 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1840 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1842 case WHENSPEC_RFC2822
:
1843 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1844 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1847 if (strcmp("now", gt
))
1848 die("Date in ident must be 'now': %s", buf
);
1849 datestamp(ident
+ name_len
, 24);
1856 static void parse_new_blob(void)
1858 static struct strbuf buf
= STRBUF_INIT
;
1860 read_next_command();
1863 store_object(OBJ_BLOB
, &buf
, &last_blob
, NULL
, next_mark
);
1866 static void unload_one_branch(void)
1868 while (cur_active_branches
1869 && cur_active_branches
>= max_active_branches
) {
1870 uintmax_t min_commit
= ULONG_MAX
;
1871 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1873 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1874 if (e
->last_commit
< min_commit
) {
1876 min_commit
= e
->last_commit
;
1882 e
= p
->active_next_branch
;
1883 p
->active_next_branch
= e
->active_next_branch
;
1885 e
= active_branches
;
1886 active_branches
= e
->active_next_branch
;
1889 e
->active_next_branch
= NULL
;
1890 if (e
->branch_tree
.tree
) {
1891 release_tree_content_recursive(e
->branch_tree
.tree
);
1892 e
->branch_tree
.tree
= NULL
;
1894 cur_active_branches
--;
1898 static void load_branch(struct branch
*b
)
1900 load_tree(&b
->branch_tree
);
1903 b
->active_next_branch
= active_branches
;
1904 active_branches
= b
;
1905 cur_active_branches
++;
1906 branch_load_count
++;
1910 static void file_change_m(struct branch
*b
)
1912 const char *p
= command_buf
.buf
+ 2;
1913 static struct strbuf uq
= STRBUF_INIT
;
1915 struct object_entry
*oe
= oe
;
1916 unsigned char sha1
[20];
1917 uint16_t mode
, inline_data
= 0;
1919 p
= get_mode(p
, &mode
);
1921 die("Corrupt mode: %s", command_buf
.buf
);
1926 case S_IFREG
| 0644:
1927 case S_IFREG
| 0755:
1933 die("Corrupt mode: %s", command_buf
.buf
);
1938 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1939 hashcpy(sha1
, oe
->sha1
);
1941 } else if (!prefixcmp(p
, "inline")) {
1945 if (get_sha1_hex(p
, sha1
))
1946 die("Invalid SHA1: %s", command_buf
.buf
);
1947 oe
= find_object(sha1
);
1951 die("Missing space after SHA1: %s", command_buf
.buf
);
1954 if (!unquote_c_style(&uq
, p
, &endp
)) {
1956 die("Garbage after path in: %s", command_buf
.buf
);
1960 if (S_ISGITLINK(mode
)) {
1962 die("Git links cannot be specified 'inline': %s",
1965 if (oe
->type
!= OBJ_COMMIT
)
1966 die("Not a commit (actually a %s): %s",
1967 typename(oe
->type
), command_buf
.buf
);
1970 * Accept the sha1 without checking; it expected to be in
1971 * another repository.
1973 } else if (inline_data
) {
1974 static struct strbuf buf
= STRBUF_INIT
;
1977 strbuf_addstr(&uq
, p
);
1980 read_next_command();
1982 store_object(OBJ_BLOB
, &buf
, &last_blob
, sha1
, 0);
1984 if (oe
->type
!= OBJ_BLOB
)
1985 die("Not a blob (actually a %s): %s",
1986 typename(oe
->type
), command_buf
.buf
);
1988 enum object_type type
= sha1_object_info(sha1
, NULL
);
1990 die("Blob not found: %s", command_buf
.buf
);
1991 if (type
!= OBJ_BLOB
)
1992 die("Not a blob (actually a %s): %s",
1993 typename(type
), command_buf
.buf
);
1996 tree_content_set(&b
->branch_tree
, p
, sha1
, mode
, NULL
);
1999 static void file_change_d(struct branch
*b
)
2001 const char *p
= command_buf
.buf
+ 2;
2002 static struct strbuf uq
= STRBUF_INIT
;
2006 if (!unquote_c_style(&uq
, p
, &endp
)) {
2008 die("Garbage after path in: %s", command_buf
.buf
);
2011 tree_content_remove(&b
->branch_tree
, p
, NULL
);
2014 static void file_change_cr(struct branch
*b
, int rename
)
2017 static struct strbuf s_uq
= STRBUF_INIT
;
2018 static struct strbuf d_uq
= STRBUF_INIT
;
2020 struct tree_entry leaf
;
2022 s
= command_buf
.buf
+ 2;
2023 strbuf_reset(&s_uq
);
2024 if (!unquote_c_style(&s_uq
, s
, &endp
)) {
2026 die("Missing space after source: %s", command_buf
.buf
);
2028 endp
= strchr(s
, ' ');
2030 die("Missing space after source: %s", command_buf
.buf
);
2031 strbuf_add(&s_uq
, s
, endp
- s
);
2037 die("Missing dest: %s", command_buf
.buf
);
2040 strbuf_reset(&d_uq
);
2041 if (!unquote_c_style(&d_uq
, d
, &endp
)) {
2043 die("Garbage after dest in: %s", command_buf
.buf
);
2047 memset(&leaf
, 0, sizeof(leaf
));
2049 tree_content_remove(&b
->branch_tree
, s
, &leaf
);
2051 tree_content_get(&b
->branch_tree
, s
, &leaf
);
2052 if (!leaf
.versions
[1].mode
)
2053 die("Path %s not in branch", s
);
2054 tree_content_set(&b
->branch_tree
, d
,
2055 leaf
.versions
[1].sha1
,
2056 leaf
.versions
[1].mode
,
2060 static void note_change_n(struct branch
*b
)
2062 const char *p
= command_buf
.buf
+ 2;
2063 static struct strbuf uq
= STRBUF_INIT
;
2064 struct object_entry
*oe
= oe
;
2066 unsigned char sha1
[20], commit_sha1
[20];
2067 uint16_t inline_data
= 0;
2069 /* <dataref> or 'inline' */
2072 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
2073 hashcpy(sha1
, oe
->sha1
);
2075 } else if (!prefixcmp(p
, "inline")) {
2079 if (get_sha1_hex(p
, sha1
))
2080 die("Invalid SHA1: %s", command_buf
.buf
);
2081 oe
= find_object(sha1
);
2085 die("Missing space after SHA1: %s", command_buf
.buf
);
2088 s
= lookup_branch(p
);
2090 hashcpy(commit_sha1
, s
->sha1
);
2091 } else if (*p
== ':') {
2092 uintmax_t commit_mark
= strtoumax(p
+ 1, NULL
, 10);
2093 struct object_entry
*commit_oe
= find_mark(commit_mark
);
2094 if (commit_oe
->type
!= OBJ_COMMIT
)
2095 die("Mark :%" PRIuMAX
" not a commit", commit_mark
);
2096 hashcpy(commit_sha1
, commit_oe
->sha1
);
2097 } else if (!get_sha1(p
, commit_sha1
)) {
2099 char *buf
= read_object_with_reference(commit_sha1
,
2100 commit_type
, &size
, commit_sha1
);
2101 if (!buf
|| size
< 46)
2102 die("Not a valid commit: %s", p
);
2105 die("Invalid ref name or SHA1 expression: %s", p
);
2108 static struct strbuf buf
= STRBUF_INIT
;
2111 strbuf_addstr(&uq
, p
);
2114 read_next_command();
2116 store_object(OBJ_BLOB
, &buf
, &last_blob
, sha1
, 0);
2118 if (oe
->type
!= OBJ_BLOB
)
2119 die("Not a blob (actually a %s): %s",
2120 typename(oe
->type
), command_buf
.buf
);
2122 enum object_type type
= sha1_object_info(sha1
, NULL
);
2124 die("Blob not found: %s", command_buf
.buf
);
2125 if (type
!= OBJ_BLOB
)
2126 die("Not a blob (actually a %s): %s",
2127 typename(type
), command_buf
.buf
);
2130 tree_content_set(&b
->branch_tree
, sha1_to_hex(commit_sha1
), sha1
,
2131 S_IFREG
| 0644, NULL
);
2134 static void file_change_deleteall(struct branch
*b
)
2136 release_tree_content_recursive(b
->branch_tree
.tree
);
2137 hashclr(b
->branch_tree
.versions
[0].sha1
);
2138 hashclr(b
->branch_tree
.versions
[1].sha1
);
2139 load_tree(&b
->branch_tree
);
2142 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2144 if (!buf
|| size
< 46)
2145 die("Not a valid commit: %s", sha1_to_hex(b
->sha1
));
2146 if (memcmp("tree ", buf
, 5)
2147 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
2148 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
2149 hashcpy(b
->branch_tree
.versions
[0].sha1
,
2150 b
->branch_tree
.versions
[1].sha1
);
2153 static void parse_from_existing(struct branch
*b
)
2155 if (is_null_sha1(b
->sha1
)) {
2156 hashclr(b
->branch_tree
.versions
[0].sha1
);
2157 hashclr(b
->branch_tree
.versions
[1].sha1
);
2162 buf
= read_object_with_reference(b
->sha1
,
2163 commit_type
, &size
, b
->sha1
);
2164 parse_from_commit(b
, buf
, size
);
2169 static int parse_from(struct branch
*b
)
2174 if (prefixcmp(command_buf
.buf
, "from "))
2177 if (b
->branch_tree
.tree
) {
2178 release_tree_content_recursive(b
->branch_tree
.tree
);
2179 b
->branch_tree
.tree
= NULL
;
2182 from
= strchr(command_buf
.buf
, ' ') + 1;
2183 s
= lookup_branch(from
);
2185 die("Can't create a branch from itself: %s", b
->name
);
2187 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
2188 hashcpy(b
->sha1
, s
->sha1
);
2189 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
2190 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
2191 } else if (*from
== ':') {
2192 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
2193 struct object_entry
*oe
= find_mark(idnum
);
2194 if (oe
->type
!= OBJ_COMMIT
)
2195 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2196 hashcpy(b
->sha1
, oe
->sha1
);
2197 if (oe
->pack_id
!= MAX_PACK_ID
) {
2199 char *buf
= gfi_unpack_entry(oe
, &size
);
2200 parse_from_commit(b
, buf
, size
);
2203 parse_from_existing(b
);
2204 } else if (!get_sha1(from
, b
->sha1
))
2205 parse_from_existing(b
);
2207 die("Invalid ref name or SHA1 expression: %s", from
);
2209 read_next_command();
2213 static struct hash_list
*parse_merge(unsigned int *count
)
2215 struct hash_list
*list
= NULL
, *n
, *e
= e
;
2220 while (!prefixcmp(command_buf
.buf
, "merge ")) {
2221 from
= strchr(command_buf
.buf
, ' ') + 1;
2222 n
= xmalloc(sizeof(*n
));
2223 s
= lookup_branch(from
);
2225 hashcpy(n
->sha1
, s
->sha1
);
2226 else if (*from
== ':') {
2227 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
2228 struct object_entry
*oe
= find_mark(idnum
);
2229 if (oe
->type
!= OBJ_COMMIT
)
2230 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2231 hashcpy(n
->sha1
, oe
->sha1
);
2232 } else if (!get_sha1(from
, n
->sha1
)) {
2234 char *buf
= read_object_with_reference(n
->sha1
,
2235 commit_type
, &size
, n
->sha1
);
2236 if (!buf
|| size
< 46)
2237 die("Not a valid commit: %s", from
);
2240 die("Invalid ref name or SHA1 expression: %s", from
);
2249 read_next_command();
2254 static void parse_new_commit(void)
2256 static struct strbuf msg
= STRBUF_INIT
;
2259 char *author
= NULL
;
2260 char *committer
= NULL
;
2261 struct hash_list
*merge_list
= NULL
;
2262 unsigned int merge_count
;
2264 /* Obtain the branch name from the rest of our command */
2265 sp
= strchr(command_buf
.buf
, ' ') + 1;
2266 b
= lookup_branch(sp
);
2270 read_next_command();
2272 if (!prefixcmp(command_buf
.buf
, "author ")) {
2273 author
= parse_ident(command_buf
.buf
+ 7);
2274 read_next_command();
2276 if (!prefixcmp(command_buf
.buf
, "committer ")) {
2277 committer
= parse_ident(command_buf
.buf
+ 10);
2278 read_next_command();
2281 die("Expected committer but didn't get one");
2283 read_next_command();
2285 merge_list
= parse_merge(&merge_count
);
2287 /* ensure the branch is active/loaded */
2288 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2289 unload_one_branch();
2294 while (command_buf
.len
> 0) {
2295 if (!prefixcmp(command_buf
.buf
, "M "))
2297 else if (!prefixcmp(command_buf
.buf
, "D "))
2299 else if (!prefixcmp(command_buf
.buf
, "R "))
2300 file_change_cr(b
, 1);
2301 else if (!prefixcmp(command_buf
.buf
, "C "))
2302 file_change_cr(b
, 0);
2303 else if (!prefixcmp(command_buf
.buf
, "N "))
2305 else if (!strcmp("deleteall", command_buf
.buf
))
2306 file_change_deleteall(b
);
2308 unread_command_buf
= 1;
2311 if (read_next_command() == EOF
)
2315 /* build the tree and the commit */
2316 store_tree(&b
->branch_tree
);
2317 hashcpy(b
->branch_tree
.versions
[0].sha1
,
2318 b
->branch_tree
.versions
[1].sha1
);
2320 strbuf_reset(&new_data
);
2321 strbuf_addf(&new_data
, "tree %s\n",
2322 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
2323 if (!is_null_sha1(b
->sha1
))
2324 strbuf_addf(&new_data
, "parent %s\n", sha1_to_hex(b
->sha1
));
2325 while (merge_list
) {
2326 struct hash_list
*next
= merge_list
->next
;
2327 strbuf_addf(&new_data
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
2331 strbuf_addf(&new_data
,
2335 author
? author
: committer
, committer
);
2336 strbuf_addbuf(&new_data
, &msg
);
2340 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, b
->sha1
, next_mark
))
2341 b
->pack_id
= pack_id
;
2342 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2345 static void parse_new_tag(void)
2347 static struct strbuf msg
= STRBUF_INIT
;
2353 uintmax_t from_mark
= 0;
2354 unsigned char sha1
[20];
2356 /* Obtain the new tag name from the rest of our command */
2357 sp
= strchr(command_buf
.buf
, ' ') + 1;
2358 t
= pool_alloc(sizeof(struct tag
));
2360 t
->name
= pool_strdup(sp
);
2362 last_tag
->next_tag
= t
;
2366 read_next_command();
2369 if (prefixcmp(command_buf
.buf
, "from "))
2370 die("Expected from command, got %s", command_buf
.buf
);
2371 from
= strchr(command_buf
.buf
, ' ') + 1;
2372 s
= lookup_branch(from
);
2374 hashcpy(sha1
, s
->sha1
);
2375 } else if (*from
== ':') {
2376 struct object_entry
*oe
;
2377 from_mark
= strtoumax(from
+ 1, NULL
, 10);
2378 oe
= find_mark(from_mark
);
2379 if (oe
->type
!= OBJ_COMMIT
)
2380 die("Mark :%" PRIuMAX
" not a commit", from_mark
);
2381 hashcpy(sha1
, oe
->sha1
);
2382 } else if (!get_sha1(from
, sha1
)) {
2386 buf
= read_object_with_reference(sha1
,
2387 commit_type
, &size
, sha1
);
2388 if (!buf
|| size
< 46)
2389 die("Not a valid commit: %s", from
);
2392 die("Invalid ref name or SHA1 expression: %s", from
);
2393 read_next_command();
2396 if (!prefixcmp(command_buf
.buf
, "tagger ")) {
2397 tagger
= parse_ident(command_buf
.buf
+ 7);
2398 read_next_command();
2402 /* tag payload/message */
2405 /* build the tag object */
2406 strbuf_reset(&new_data
);
2408 strbuf_addf(&new_data
,
2412 sha1_to_hex(sha1
), commit_type
, t
->name
);
2414 strbuf_addf(&new_data
,
2415 "tagger %s\n", tagger
);
2416 strbuf_addch(&new_data
, '\n');
2417 strbuf_addbuf(&new_data
, &msg
);
2420 if (store_object(OBJ_TAG
, &new_data
, NULL
, t
->sha1
, 0))
2421 t
->pack_id
= MAX_PACK_ID
;
2423 t
->pack_id
= pack_id
;
2426 static void parse_reset_branch(void)
2431 /* Obtain the branch name from the rest of our command */
2432 sp
= strchr(command_buf
.buf
, ' ') + 1;
2433 b
= lookup_branch(sp
);
2436 hashclr(b
->branch_tree
.versions
[0].sha1
);
2437 hashclr(b
->branch_tree
.versions
[1].sha1
);
2438 if (b
->branch_tree
.tree
) {
2439 release_tree_content_recursive(b
->branch_tree
.tree
);
2440 b
->branch_tree
.tree
= NULL
;
2445 read_next_command();
2447 if (command_buf
.len
> 0)
2448 unread_command_buf
= 1;
2451 static void parse_checkpoint(void)
2462 static void parse_progress(void)
2464 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
2465 fputc('\n', stdout
);
2470 static void option_import_marks(const char *marks
)
2472 input_file
= xstrdup(marks
);
2475 static void option_date_format(const char *fmt
)
2477 if (!strcmp(fmt
, "raw"))
2478 whenspec
= WHENSPEC_RAW
;
2479 else if (!strcmp(fmt
, "rfc2822"))
2480 whenspec
= WHENSPEC_RFC2822
;
2481 else if (!strcmp(fmt
, "now"))
2482 whenspec
= WHENSPEC_NOW
;
2484 die("unknown --date-format argument %s", fmt
);
2487 static void option_max_pack_size(const char *packsize
)
2489 max_packsize
= strtoumax(packsize
, NULL
, 0) * 1024 * 1024;
2492 static void option_depth(const char *depth
)
2494 max_depth
= strtoul(depth
, NULL
, 0);
2495 if (max_depth
> MAX_DEPTH
)
2496 die("--depth cannot exceed %u", MAX_DEPTH
);
2499 static void option_active_branches(const char *branches
)
2501 max_active_branches
= strtoul(branches
, NULL
, 0);
2504 static void option_export_marks(const char *marks
)
2506 mark_file
= xstrdup(marks
);
2509 static void option_export_pack_edges(const char *edges
)
2513 pack_edges
= fopen(edges
, "a");
2515 die_errno("Cannot open '%s'", edges
);
2518 static void parse_one_option(const char *option
, int optional
)
2520 if (!prefixcmp(option
, "date-format=")) {
2521 option_date_format(option
+ 12);
2522 } else if (!prefixcmp(option
, "max-pack-size=")) {
2523 option_max_pack_size(option
+ 14);
2524 } else if (!prefixcmp(option
, "depth=")) {
2525 option_depth(option
+ 6);
2526 } else if (!prefixcmp(option
, "active-branches=")) {
2527 option_active_branches(option
+ 16);
2528 } else if (!prefixcmp(option
, "import-marks=")) {
2529 option_import_marks(option
+ 13);
2530 } else if (!prefixcmp(option
, "export-marks=")) {
2531 option_export_marks(option
+ 13);
2532 } else if (!prefixcmp(option
, "export-pack-edges=")) {
2533 option_export_pack_edges(option
+ 18);
2534 } else if (!prefixcmp(option
, "force")) {
2536 } else if (!prefixcmp(option
, "quiet")) {
2538 } else if (!prefixcmp(option
, "stats")) {
2540 } else if (!optional
) {
2541 die("Unsupported option: %s", option
);
2545 static void parse_feature(void)
2547 char *feature
= command_buf
.buf
+ 8;
2549 if (!prefixcmp(feature
, "date-format=")) {
2550 option_date_format(feature
+ 12);
2551 } else if (!strcmp("git-options", feature
)) {
2552 options_enabled
= 1;
2554 die("This version of fast-import does not support feature %s.", feature
);
2558 static void parse_option(void)
2560 char *option
= command_buf
.buf
+ 11;
2562 if (!options_enabled
)
2563 die("Got option command '%s' before options feature'", option
);
2565 if (seen_non_option_command
)
2566 die("Got option command '%s' after non-option command", option
);
2568 /* ignore unknown options, instead of erroring */
2569 parse_one_option(option
, 1);
2572 static void parse_nongit_option(void)
2577 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2579 if (!strcmp(k
, "pack.depth")) {
2580 max_depth
= git_config_int(k
, v
);
2581 if (max_depth
> MAX_DEPTH
)
2582 max_depth
= MAX_DEPTH
;
2585 if (!strcmp(k
, "pack.compression")) {
2586 int level
= git_config_int(k
, v
);
2588 level
= Z_DEFAULT_COMPRESSION
;
2589 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
2590 die("bad pack compression level %d", level
);
2591 pack_compression_level
= level
;
2592 pack_compression_seen
= 1;
2595 return git_default_config(k
, v
, cb
);
2598 static const char fast_import_usage
[] =
2599 "git fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2601 static void parse_argv(void)
2605 for (i
= 1; i
< global_argc
; i
++) {
2606 const char *a
= global_argv
[i
];
2608 if (*a
!= '-' || !strcmp(a
, "--"))
2611 /* error on unknown options */
2612 parse_one_option(a
+ 2, 0);
2614 if (i
!= global_argc
)
2615 usage(fast_import_usage
);
2617 seen_non_option_command
= 1;
2622 int main(int argc
, const char **argv
)
2626 git_extract_argv0_path(argv
[0]);
2628 setup_git_directory();
2629 git_config(git_pack_config
, NULL
);
2630 if (!pack_compression_seen
&& core_compression_seen
)
2631 pack_compression_level
= core_compression_level
;
2633 alloc_objects(object_entry_alloc
);
2634 strbuf_init(&command_buf
, 0);
2635 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2636 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2637 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2638 marks
= pool_calloc(1, sizeof(struct mark_set
));
2643 rc_free
= pool_alloc(cmd_save
* sizeof(*rc_free
));
2644 for (i
= 0; i
< (cmd_save
- 1); i
++)
2645 rc_free
[i
].next
= &rc_free
[i
+ 1];
2646 rc_free
[cmd_save
- 1].next
= NULL
;
2648 prepare_packed_git();
2650 set_die_routine(die_nicely
);
2651 while (read_next_command() != EOF
) {
2652 if (!strcmp("blob", command_buf
.buf
))
2654 else if (!prefixcmp(command_buf
.buf
, "commit "))
2656 else if (!prefixcmp(command_buf
.buf
, "tag "))
2658 else if (!prefixcmp(command_buf
.buf
, "reset "))
2659 parse_reset_branch();
2660 else if (!strcmp("checkpoint", command_buf
.buf
))
2662 else if (!prefixcmp(command_buf
.buf
, "progress "))
2664 else if (!prefixcmp(command_buf
.buf
, "feature "))
2666 else if (!prefixcmp(command_buf
.buf
, "option git "))
2668 else if (!prefixcmp(command_buf
.buf
, "option "))
2669 parse_nongit_option();
2671 die("Unsupported command: %s", command_buf
.buf
);
2674 /* argv hasn't been parsed yet, do so */
2675 if (!seen_non_option_command
)
2689 uintmax_t total_count
= 0, duplicate_count
= 0;
2690 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2691 total_count
+= object_count_by_type
[i
];
2692 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2693 duplicate_count
+= duplicate_count_by_type
[i
];
2695 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2696 fprintf(stderr
, "---------------------------------------------------------------------\n");
2697 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
2698 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
2699 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
]);
2700 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
]);
2701 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
]);
2702 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
]);
2703 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2704 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2705 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2706 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2707 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
2708 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2709 fprintf(stderr
, "---------------------------------------------------------------------\n");
2711 fprintf(stderr
, "---------------------------------------------------------------------\n");
2712 fprintf(stderr
, "\n");
2715 return failure
? 1 : 0;