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 (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
26 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) 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
45 new_tag ::= 'tag' sp tag_str lf
46 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
47 ('tagger' sp name sp '<' email '>' sp when lf)?
51 reset_branch ::= 'reset' sp ref_str lf
52 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
55 checkpoint ::= 'checkpoint' lf
58 progress ::= 'progress' sp not_lf* lf
61 # note: the first idnum in a stream should be 1 and subsequent
62 # idnums should not have gaps between values as this will cause
63 # the stream parser to reserve space for the gapped values. An
64 # idnum can be updated in the future to a new object by issuing
65 # a new mark directive with the old idnum.
67 mark ::= 'mark' sp idnum lf;
68 data ::= (delimited_data | exact_data)
71 # note: delim may be any string but must not contain lf.
72 # data_line may contain any data but must not be exactly
74 delimited_data ::= 'data' sp '<<' delim lf
78 # note: declen indicates the length of binary_data in bytes.
79 # declen does not include the lf preceding the binary data.
81 exact_data ::= 'data' sp declen lf
84 # note: quoted strings are C-style quoting supporting \c for
85 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
86 # is the signed byte value in octal. Note that the only
87 # characters which must actually be escaped to protect the
88 # stream formatting is: \, " and LF. Otherwise these values
92 sha1exp_str ::= sha1exp;
94 path_str ::= path | '"' quoted(path) '"' ;
95 mode ::= '100644' | '644'
100 declen ::= # unsigned 32 bit value, ascii base10 notation;
101 bigint ::= # unsigned integer value, ascii base10 notation;
102 binary_data ::= # file content, not interpreted;
104 when ::= raw_when | rfc2822_when;
105 raw_when ::= ts sp tz;
106 rfc2822_when ::= # Valid RFC 2822 date and time;
108 sp ::= # ASCII space character;
109 lf ::= # ASCII newline (LF) character;
111 # note: a colon (':') must precede the numerical value assigned to
112 # an idnum. This is to distinguish it from a ref or tag name as
113 # GIT does not permit ':' in ref or tag strings.
115 idnum ::= ':' bigint;
116 path ::= # GIT style file path, e.g. "a/b/c";
117 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
118 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
119 sha1exp ::= # Any valid GIT SHA1 expression;
120 hexsha1 ::= # SHA1 in hexadecimal format;
122 # note: name and email are UTF8 strings, however name must not
123 # contain '<' or lf and email must not contain any of the
124 # following: '<', '>', lf.
126 name ::= # valid GIT author/committer name;
127 email ::= # valid GIT author/committer email;
128 ts ::= # time since the epoch in seconds, ascii base10 notation;
129 tz ::= # GIT style timezone;
131 # note: comments may appear anywhere in the input, except
132 # within a data command. Any form of the data command
133 # always escapes the related input from comment processing.
135 # In case it is not clear, the '#' that starts the comment
136 # must be the first character on that line (an lf
139 comment ::= '#' not_lf* lf;
140 not_lf ::= # Any byte that is not ASCII newline (LF);
152 #include "csum-file.h"
154 #include "exec_cmd.h"
156 #define PACK_ID_BITS 16
157 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
158 #define DEPTH_BITS 13
159 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
163 struct object_entry
*next
;
165 uint32_t type
: TYPE_BITS
,
166 pack_id
: PACK_ID_BITS
,
168 unsigned char sha1
[20];
171 struct object_entry_pool
173 struct object_entry_pool
*next_pool
;
174 struct object_entry
*next_free
;
175 struct object_entry
*end
;
176 struct object_entry entries
[FLEX_ARRAY
]; /* more */
182 struct object_entry
*marked
[1024];
183 struct mark_set
*sets
[1024];
193 unsigned no_swap
: 1;
198 struct mem_pool
*next_pool
;
201 uintmax_t 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];
259 struct hash_list
*next
;
260 unsigned char sha1
[20];
269 struct recent_command
271 struct recent_command
*prev
;
272 struct recent_command
*next
;
276 /* Configured limits on output */
277 static unsigned long max_depth
= 10;
278 static off_t max_packsize
= (1LL << 32) - 1;
279 static int force_update
;
280 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
281 static int pack_compression_seen
;
283 /* Stats and misc. counters */
284 static uintmax_t alloc_count
;
285 static uintmax_t marks_set_count
;
286 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
287 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
288 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
289 static unsigned long object_count
;
290 static unsigned long branch_count
;
291 static unsigned long branch_load_count
;
293 static FILE *pack_edges
;
294 static unsigned int show_stats
= 1;
295 static int global_argc
;
296 static const char **global_argv
;
299 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
300 static size_t total_allocd
;
301 static struct mem_pool
*mem_pool
;
303 /* Atom management */
304 static unsigned int atom_table_sz
= 4451;
305 static unsigned int atom_cnt
;
306 static struct atom_str
**atom_table
;
308 /* The .pack file being generated */
309 static unsigned int pack_id
;
310 static struct packed_git
*pack_data
;
311 static struct packed_git
**all_packs
;
312 static unsigned long pack_size
;
314 /* Table of objects we've written. */
315 static unsigned int object_entry_alloc
= 5000;
316 static struct object_entry_pool
*blocks
;
317 static struct object_entry
*object_table
[1 << 16];
318 static struct mark_set
*marks
;
319 static const char *mark_file
;
320 static const char *input_file
;
323 static struct last_object last_blob
= { STRBUF_INIT
, 0, 0, 0 };
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 strbuf old_tree
= STRBUF_INIT
;
331 static struct strbuf new_tree
= STRBUF_INIT
;
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
= STRBUF_INIT
;
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 strbuf new_data
= STRBUF_INIT
;
354 static int options_enabled
;
355 static int seen_non_option_command
;
357 static void parse_argv(void);
359 static void write_branch_report(FILE *rpt
, struct branch
*b
)
361 fprintf(rpt
, "%s:\n", b
->name
);
363 fprintf(rpt
, " status :");
365 fputs(" active", rpt
);
366 if (b
->branch_tree
.tree
)
367 fputs(" loaded", rpt
);
368 if (is_null_sha1(b
->branch_tree
.versions
[1].sha1
))
369 fputs(" dirty", rpt
);
372 fprintf(rpt
, " tip commit : %s\n", sha1_to_hex(b
->sha1
));
373 fprintf(rpt
, " old tree : %s\n", sha1_to_hex(b
->branch_tree
.versions
[0].sha1
));
374 fprintf(rpt
, " cur tree : %s\n", sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
375 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
377 fputs(" last pack : ", rpt
);
378 if (b
->pack_id
< MAX_PACK_ID
)
379 fprintf(rpt
, "%u", b
->pack_id
);
385 static void dump_marks_helper(FILE *, uintmax_t, struct mark_set
*);
387 static void write_crash_report(const char *err
)
389 char *loc
= git_path("fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
390 FILE *rpt
= fopen(loc
, "w");
393 struct recent_command
*rc
;
396 error("can't write crash report %s: %s", loc
, strerror(errno
));
400 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
402 fprintf(rpt
, "fast-import crash report:\n");
403 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
404 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
405 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_LOCAL
));
408 fputs("fatal: ", rpt
);
413 fputs("Most Recent Commands Before Crash\n", rpt
);
414 fputs("---------------------------------\n", rpt
);
415 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
416 if (rc
->next
== &cmd_hist
)
425 fputs("Active Branch LRU\n", rpt
);
426 fputs("-----------------\n", rpt
);
427 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
429 max_active_branches
);
431 fputs(" pos clock name\n", rpt
);
432 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
433 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
434 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
435 ++lu
, b
->last_commit
, b
->name
);
438 fputs("Inactive Branches\n", rpt
);
439 fputs("-----------------\n", rpt
);
440 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
441 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
442 write_branch_report(rpt
, b
);
448 fputs("Annotated Tags\n", rpt
);
449 fputs("--------------\n", rpt
);
450 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
451 fputs(sha1_to_hex(tg
->sha1
), rpt
);
453 fputs(tg
->name
, rpt
);
459 fputs("Marks\n", rpt
);
460 fputs("-----\n", rpt
);
462 fprintf(rpt
, " exported to %s\n", mark_file
);
464 dump_marks_helper(rpt
, 0, marks
);
467 fputs("-------------------\n", rpt
);
468 fputs("END OF CRASH REPORT\n", rpt
);
472 static void end_packfile(void);
473 static void unkeep_all_packs(void);
474 static void dump_marks(void);
476 static NORETURN
void die_nicely(const char *err
, va_list params
)
479 char message
[2 * PATH_MAX
];
481 vsnprintf(message
, sizeof(message
), err
, params
);
482 fputs("fatal: ", stderr
);
483 fputs(message
, stderr
);
488 write_crash_report(message
);
496 static void alloc_objects(unsigned int cnt
)
498 struct object_entry_pool
*b
;
500 b
= xmalloc(sizeof(struct object_entry_pool
)
501 + cnt
* sizeof(struct object_entry
));
502 b
->next_pool
= blocks
;
503 b
->next_free
= b
->entries
;
504 b
->end
= b
->entries
+ cnt
;
509 static struct object_entry
*new_object(unsigned char *sha1
)
511 struct object_entry
*e
;
513 if (blocks
->next_free
== blocks
->end
)
514 alloc_objects(object_entry_alloc
);
516 e
= blocks
->next_free
++;
517 hashcpy(e
->sha1
, sha1
);
521 static struct object_entry
*find_object(unsigned char *sha1
)
523 unsigned int h
= sha1
[0] << 8 | sha1
[1];
524 struct object_entry
*e
;
525 for (e
= object_table
[h
]; e
; e
= e
->next
)
526 if (!hashcmp(sha1
, e
->sha1
))
531 static struct object_entry
*insert_object(unsigned char *sha1
)
533 unsigned int h
= sha1
[0] << 8 | sha1
[1];
534 struct object_entry
*e
= object_table
[h
];
535 struct object_entry
*p
= NULL
;
538 if (!hashcmp(sha1
, e
->sha1
))
544 e
= new_object(sha1
);
554 static unsigned int hc_str(const char *s
, size_t len
)
562 static void *pool_alloc(size_t len
)
567 /* round up to a 'uintmax_t' alignment */
568 if (len
& (sizeof(uintmax_t) - 1))
569 len
+= sizeof(uintmax_t) - (len
& (sizeof(uintmax_t) - 1));
571 for (p
= mem_pool
; p
; p
= p
->next_pool
)
572 if ((p
->end
- p
->next_free
>= len
))
576 if (len
>= (mem_pool_alloc
/2)) {
580 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
581 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
582 p
->next_pool
= mem_pool
;
583 p
->next_free
= (char *) p
->space
;
584 p
->end
= p
->next_free
+ mem_pool_alloc
;
593 static void *pool_calloc(size_t count
, size_t size
)
595 size_t len
= count
* size
;
596 void *r
= pool_alloc(len
);
601 static char *pool_strdup(const char *s
)
603 char *r
= pool_alloc(strlen(s
) + 1);
608 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
610 struct mark_set
*s
= marks
;
611 while ((idnum
>> s
->shift
) >= 1024) {
612 s
= pool_calloc(1, sizeof(struct mark_set
));
613 s
->shift
= marks
->shift
+ 10;
614 s
->data
.sets
[0] = marks
;
618 uintmax_t i
= idnum
>> s
->shift
;
619 idnum
-= i
<< s
->shift
;
620 if (!s
->data
.sets
[i
]) {
621 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
622 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
626 if (!s
->data
.marked
[idnum
])
628 s
->data
.marked
[idnum
] = oe
;
631 static struct object_entry
*find_mark(uintmax_t idnum
)
633 uintmax_t orig_idnum
= idnum
;
634 struct mark_set
*s
= marks
;
635 struct object_entry
*oe
= NULL
;
636 if ((idnum
>> s
->shift
) < 1024) {
637 while (s
&& s
->shift
) {
638 uintmax_t i
= idnum
>> s
->shift
;
639 idnum
-= i
<< s
->shift
;
643 oe
= s
->data
.marked
[idnum
];
646 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
650 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
652 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
655 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
656 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
659 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
661 strncpy(c
->str_dat
, s
, len
);
663 c
->next_atom
= atom_table
[hc
];
669 static struct branch
*lookup_branch(const char *name
)
671 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
674 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
675 if (!strcmp(name
, b
->name
))
680 static struct branch
*new_branch(const char *name
)
682 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
683 struct branch
*b
= lookup_branch(name
);
686 die("Invalid attempt to create duplicate branch: %s", name
);
687 switch (check_ref_format(name
)) {
688 case 0: break; /* its valid */
689 case CHECK_REF_FORMAT_ONELEVEL
:
690 break; /* valid, but too few '/', allow anyway */
692 die("Branch name doesn't conform to GIT standards: %s", name
);
695 b
= pool_calloc(1, sizeof(struct branch
));
696 b
->name
= pool_strdup(name
);
697 b
->table_next_branch
= branch_table
[hc
];
698 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
699 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
701 b
->pack_id
= MAX_PACK_ID
;
702 branch_table
[hc
] = b
;
707 static unsigned int hc_entries(unsigned int cnt
)
709 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
710 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
713 static struct tree_content
*new_tree_content(unsigned int cnt
)
715 struct avail_tree_content
*f
, *l
= NULL
;
716 struct tree_content
*t
;
717 unsigned int hc
= hc_entries(cnt
);
719 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
720 if (f
->entry_capacity
>= cnt
)
725 l
->next_avail
= f
->next_avail
;
727 avail_tree_table
[hc
] = f
->next_avail
;
729 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
730 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
731 f
->entry_capacity
= cnt
;
734 t
= (struct tree_content
*)f
;
740 static void release_tree_entry(struct tree_entry
*e
);
741 static void release_tree_content(struct tree_content
*t
)
743 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
744 unsigned int hc
= hc_entries(f
->entry_capacity
);
745 f
->next_avail
= avail_tree_table
[hc
];
746 avail_tree_table
[hc
] = f
;
749 static void release_tree_content_recursive(struct tree_content
*t
)
752 for (i
= 0; i
< t
->entry_count
; i
++)
753 release_tree_entry(t
->entries
[i
]);
754 release_tree_content(t
);
757 static struct tree_content
*grow_tree_content(
758 struct tree_content
*t
,
761 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
762 r
->entry_count
= t
->entry_count
;
763 r
->delta_depth
= t
->delta_depth
;
764 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
765 release_tree_content(t
);
769 static struct tree_entry
*new_tree_entry(void)
771 struct tree_entry
*e
;
773 if (!avail_tree_entry
) {
774 unsigned int n
= tree_entry_alloc
;
775 total_allocd
+= n
* sizeof(struct tree_entry
);
776 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
778 *((void**)e
) = e
+ 1;
784 e
= avail_tree_entry
;
785 avail_tree_entry
= *((void**)e
);
789 static void release_tree_entry(struct tree_entry
*e
)
792 release_tree_content_recursive(e
->tree
);
793 *((void**)e
) = avail_tree_entry
;
794 avail_tree_entry
= e
;
797 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
799 struct tree_content
*d
;
800 struct tree_entry
*a
, *b
;
805 d
= new_tree_content(s
->entry_count
);
806 for (i
= 0; i
< s
->entry_count
; i
++) {
808 b
= new_tree_entry();
809 memcpy(b
, a
, sizeof(*a
));
810 if (a
->tree
&& is_null_sha1(b
->versions
[1].sha1
))
811 b
->tree
= dup_tree_content(a
->tree
);
816 d
->entry_count
= s
->entry_count
;
817 d
->delta_depth
= s
->delta_depth
;
822 static void start_packfile(void)
824 static char tmpfile
[PATH_MAX
];
825 struct packed_git
*p
;
826 struct pack_header hdr
;
829 pack_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
830 "pack/tmp_pack_XXXXXX");
831 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
832 strcpy(p
->pack_name
, tmpfile
);
833 p
->pack_fd
= pack_fd
;
835 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
836 hdr
.hdr_version
= htonl(2);
838 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
841 pack_size
= sizeof(hdr
);
844 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
845 all_packs
[pack_id
] = p
;
848 static int oecmp (const void *a_
, const void *b_
)
850 struct object_entry
*a
= *((struct object_entry
**)a_
);
851 struct object_entry
*b
= *((struct object_entry
**)b_
);
852 return hashcmp(a
->sha1
, b
->sha1
);
855 static char *create_index(void)
857 static char tmpfile
[PATH_MAX
];
860 struct object_entry
**idx
, **c
, **last
, *e
;
861 struct object_entry_pool
*o
;
865 /* Build the sorted table of object IDs. */
866 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
868 for (o
= blocks
; o
; o
= o
->next_pool
)
869 for (e
= o
->next_free
; e
-- != o
->entries
;)
870 if (pack_id
== e
->pack_id
)
872 last
= idx
+ object_count
;
874 die("internal consistency error creating the index");
875 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
877 /* Generate the fan-out array. */
879 for (i
= 0; i
< 256; i
++) {
880 struct object_entry
**next
= c
;
881 while (next
< last
) {
882 if ((*next
)->sha1
[0] != i
)
886 array
[i
] = htonl(next
- idx
);
890 idx_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
891 "pack/tmp_idx_XXXXXX");
892 f
= sha1fd(idx_fd
, tmpfile
);
893 sha1write(f
, array
, 256 * sizeof(int));
895 for (c
= idx
; c
!= last
; c
++) {
896 uint32_t offset
= htonl((*c
)->offset
);
897 sha1write(f
, &offset
, 4);
898 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
899 git_SHA1_Update(&ctx
, (*c
)->sha1
, 20);
901 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
902 sha1close(f
, NULL
, CSUM_FSYNC
);
904 git_SHA1_Final(pack_data
->sha1
, &ctx
);
908 static char *keep_pack(char *curr_index_name
)
910 static char name
[PATH_MAX
];
911 static const char *keep_msg
= "fast-import";
914 keep_fd
= odb_pack_keep(name
, sizeof(name
), pack_data
->sha1
);
916 die_errno("cannot create keep file");
917 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
919 die_errno("failed to write keep file");
921 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
922 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
923 if (move_temp_to_file(pack_data
->pack_name
, name
))
924 die("cannot store pack file");
926 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
927 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
928 if (move_temp_to_file(curr_index_name
, name
))
929 die("cannot store index file");
933 static void unkeep_all_packs(void)
935 static char name
[PATH_MAX
];
938 for (k
= 0; k
< pack_id
; k
++) {
939 struct packed_git
*p
= all_packs
[k
];
940 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
941 get_object_directory(), sha1_to_hex(p
->sha1
));
942 unlink_or_warn(name
);
946 static void end_packfile(void)
948 struct packed_git
*old_p
= pack_data
, *new_p
;
950 clear_delta_base_cache();
957 close_pack_windows(pack_data
);
958 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->sha1
,
959 pack_data
->pack_name
, object_count
,
961 close(pack_data
->pack_fd
);
962 idx_name
= keep_pack(create_index());
964 /* Register the packfile with core git's machinery. */
965 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
967 die("core git rejected index %s", idx_name
);
968 all_packs
[pack_id
] = new_p
;
969 install_packed_git(new_p
);
971 /* Print the boundary */
973 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
974 for (i
= 0; i
< branch_table_sz
; i
++) {
975 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
976 if (b
->pack_id
== pack_id
)
977 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
980 for (t
= first_tag
; t
; t
= t
->next_tag
) {
981 if (t
->pack_id
== pack_id
)
982 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
984 fputc('\n', pack_edges
);
991 close(old_p
->pack_fd
);
992 unlink_or_warn(old_p
->pack_name
);
996 /* We can't carry a delta across packfiles. */
997 strbuf_release(&last_blob
.data
);
998 last_blob
.offset
= 0;
1002 static void cycle_packfile(void)
1008 static size_t encode_header(
1009 enum object_type type
,
1016 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
1017 die("bad type %d", type
);
1019 c
= (type
<< 4) | (size
& 15);
1031 static int store_object(
1032 enum object_type type
,
1034 struct last_object
*last
,
1035 unsigned char *sha1out
,
1039 struct object_entry
*e
;
1040 unsigned char hdr
[96];
1041 unsigned char sha1
[20];
1042 unsigned long hdrlen
, deltalen
;
1046 hdrlen
= sprintf((char *)hdr
,"%s %lu", typename(type
),
1047 (unsigned long)dat
->len
) + 1;
1049 git_SHA1_Update(&c
, hdr
, hdrlen
);
1050 git_SHA1_Update(&c
, dat
->buf
, dat
->len
);
1051 git_SHA1_Final(sha1
, &c
);
1053 hashcpy(sha1out
, sha1
);
1055 e
= insert_object(sha1
);
1057 insert_mark(mark
, e
);
1059 duplicate_count_by_type
[type
]++;
1061 } else if (find_sha1_pack(sha1
, packed_git
)) {
1063 e
->pack_id
= MAX_PACK_ID
;
1064 e
->offset
= 1; /* just not zero! */
1065 duplicate_count_by_type
[type
]++;
1069 if (last
&& last
->data
.buf
&& last
->depth
< max_depth
) {
1070 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
1073 if (delta
&& deltalen
>= dat
->len
) {
1080 memset(&s
, 0, sizeof(s
));
1081 deflateInit(&s
, pack_compression_level
);
1084 s
.avail_in
= deltalen
;
1086 s
.next_in
= (void *)dat
->buf
;
1087 s
.avail_in
= dat
->len
;
1089 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
1090 s
.next_out
= out
= xmalloc(s
.avail_out
);
1091 while (deflate(&s
, Z_FINISH
) == Z_OK
)
1095 /* Determine if we should auto-checkpoint. */
1096 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
1097 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
1099 /* This new object needs to *not* have the current pack_id. */
1100 e
->pack_id
= pack_id
+ 1;
1103 /* We cannot carry a delta into the new pack. */
1108 memset(&s
, 0, sizeof(s
));
1109 deflateInit(&s
, pack_compression_level
);
1110 s
.next_in
= (void *)dat
->buf
;
1111 s
.avail_in
= dat
->len
;
1112 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
1113 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
1114 while (deflate(&s
, Z_FINISH
) == Z_OK
)
1121 e
->pack_id
= pack_id
;
1122 e
->offset
= pack_size
;
1124 object_count_by_type
[type
]++;
1127 unsigned long ofs
= e
->offset
- last
->offset
;
1128 unsigned pos
= sizeof(hdr
) - 1;
1130 delta_count_by_type
[type
]++;
1131 e
->depth
= last
->depth
+ 1;
1133 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
1134 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
1135 pack_size
+= hdrlen
;
1137 hdr
[pos
] = ofs
& 127;
1139 hdr
[--pos
] = 128 | (--ofs
& 127);
1140 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
1141 pack_size
+= sizeof(hdr
) - pos
;
1144 hdrlen
= encode_header(type
, dat
->len
, hdr
);
1145 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
1146 pack_size
+= hdrlen
;
1149 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
1150 pack_size
+= s
.total_out
;
1155 if (last
->no_swap
) {
1158 strbuf_swap(&last
->data
, dat
);
1160 last
->offset
= e
->offset
;
1161 last
->depth
= e
->depth
;
1166 /* All calls must be guarded by find_object() or find_mark() to
1167 * ensure the 'struct object_entry' passed was written by this
1168 * process instance. We unpack the entry by the offset, avoiding
1169 * the need for the corresponding .idx file. This unpacking rule
1170 * works because we only use OBJ_REF_DELTA within the packfiles
1171 * created by fast-import.
1173 * oe must not be NULL. Such an oe usually comes from giving
1174 * an unknown SHA-1 to find_object() or an undefined mark to
1175 * find_mark(). Callers must test for this condition and use
1176 * the standard read_sha1_file() when it happens.
1178 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1179 * find_mark(), where the mark was reloaded from an existing marks
1180 * file and is referencing an object that this fast-import process
1181 * instance did not write out to a packfile. Callers must test for
1182 * this condition and use read_sha1_file() instead.
1184 static void *gfi_unpack_entry(
1185 struct object_entry
*oe
,
1186 unsigned long *sizep
)
1188 enum object_type type
;
1189 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1190 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ 20)) {
1191 /* The object is stored in the packfile we are writing to
1192 * and we have modified it since the last time we scanned
1193 * back to read a previously written object. If an old
1194 * window covered [p->pack_size, p->pack_size + 20) its
1195 * data is stale and is not valid. Closing all windows
1196 * and updating the packfile length ensures we can read
1197 * the newly written data.
1199 close_pack_windows(p
);
1201 /* We have to offer 20 bytes additional on the end of
1202 * the packfile as the core unpacker code assumes the
1203 * footer is present at the file end and must promise
1204 * at least 20 bytes within any window it maps. But
1205 * we don't actually create the footer here.
1207 p
->pack_size
= pack_size
+ 20;
1209 return unpack_entry(p
, oe
->offset
, &type
, sizep
);
1212 static const char *get_mode(const char *str
, uint16_t *modep
)
1217 while ((c
= *str
++) != ' ') {
1218 if (c
< '0' || c
> '7')
1220 mode
= (mode
<< 3) + (c
- '0');
1226 static void load_tree(struct tree_entry
*root
)
1228 unsigned char *sha1
= root
->versions
[1].sha1
;
1229 struct object_entry
*myoe
;
1230 struct tree_content
*t
;
1235 root
->tree
= t
= new_tree_content(8);
1236 if (is_null_sha1(sha1
))
1239 myoe
= find_object(sha1
);
1240 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1241 if (myoe
->type
!= OBJ_TREE
)
1242 die("Not a tree: %s", sha1_to_hex(sha1
));
1243 t
->delta_depth
= myoe
->depth
;
1244 buf
= gfi_unpack_entry(myoe
, &size
);
1246 die("Can't load tree %s", sha1_to_hex(sha1
));
1248 enum object_type type
;
1249 buf
= read_sha1_file(sha1
, &type
, &size
);
1250 if (!buf
|| type
!= OBJ_TREE
)
1251 die("Can't load tree %s", sha1_to_hex(sha1
));
1255 while (c
!= (buf
+ size
)) {
1256 struct tree_entry
*e
= new_tree_entry();
1258 if (t
->entry_count
== t
->entry_capacity
)
1259 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1260 t
->entries
[t
->entry_count
++] = e
;
1263 c
= get_mode(c
, &e
->versions
[1].mode
);
1265 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1266 e
->versions
[0].mode
= e
->versions
[1].mode
;
1267 e
->name
= to_atom(c
, strlen(c
));
1268 c
+= e
->name
->str_len
+ 1;
1269 hashcpy(e
->versions
[0].sha1
, (unsigned char *)c
);
1270 hashcpy(e
->versions
[1].sha1
, (unsigned char *)c
);
1276 static int tecmp0 (const void *_a
, const void *_b
)
1278 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1279 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1280 return base_name_compare(
1281 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1282 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1285 static int tecmp1 (const void *_a
, const void *_b
)
1287 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1288 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1289 return base_name_compare(
1290 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1291 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1294 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1300 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1302 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1304 for (i
= 0; i
< t
->entry_count
; i
++) {
1305 if (t
->entries
[i
]->versions
[v
].mode
)
1306 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1310 strbuf_grow(b
, maxlen
);
1311 for (i
= 0; i
< t
->entry_count
; i
++) {
1312 struct tree_entry
*e
= t
->entries
[i
];
1313 if (!e
->versions
[v
].mode
)
1315 strbuf_addf(b
, "%o %s%c", (unsigned int)e
->versions
[v
].mode
,
1316 e
->name
->str_dat
, '\0');
1317 strbuf_add(b
, e
->versions
[v
].sha1
, 20);
1321 static void store_tree(struct tree_entry
*root
)
1323 struct tree_content
*t
= root
->tree
;
1324 unsigned int i
, j
, del
;
1325 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1326 struct object_entry
*le
;
1328 if (!is_null_sha1(root
->versions
[1].sha1
))
1331 for (i
= 0; i
< t
->entry_count
; i
++) {
1332 if (t
->entries
[i
]->tree
)
1333 store_tree(t
->entries
[i
]);
1336 le
= find_object(root
->versions
[0].sha1
);
1337 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1338 mktree(t
, 0, &old_tree
);
1340 lo
.offset
= le
->offset
;
1341 lo
.depth
= t
->delta_depth
;
1344 mktree(t
, 1, &new_tree
);
1345 store_object(OBJ_TREE
, &new_tree
, &lo
, root
->versions
[1].sha1
, 0);
1347 t
->delta_depth
= lo
.depth
;
1348 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1349 struct tree_entry
*e
= t
->entries
[i
];
1350 if (e
->versions
[1].mode
) {
1351 e
->versions
[0].mode
= e
->versions
[1].mode
;
1352 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1353 t
->entries
[j
++] = e
;
1355 release_tree_entry(e
);
1359 t
->entry_count
-= del
;
1362 static int tree_content_set(
1363 struct tree_entry
*root
,
1365 const unsigned char *sha1
,
1366 const uint16_t mode
,
1367 struct tree_content
*subtree
)
1369 struct tree_content
*t
= root
->tree
;
1372 struct tree_entry
*e
;
1374 slash1
= strchr(p
, '/');
1380 die("Empty path component found in input");
1381 if (!slash1
&& !S_ISDIR(mode
) && subtree
)
1382 die("Non-directories cannot have subtrees");
1384 for (i
= 0; i
< t
->entry_count
; i
++) {
1386 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1389 && e
->versions
[1].mode
== mode
1390 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1392 e
->versions
[1].mode
= mode
;
1393 hashcpy(e
->versions
[1].sha1
, sha1
);
1395 release_tree_content_recursive(e
->tree
);
1397 hashclr(root
->versions
[1].sha1
);
1400 if (!S_ISDIR(e
->versions
[1].mode
)) {
1401 e
->tree
= new_tree_content(8);
1402 e
->versions
[1].mode
= S_IFDIR
;
1406 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
)) {
1407 hashclr(root
->versions
[1].sha1
);
1414 if (t
->entry_count
== t
->entry_capacity
)
1415 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1416 e
= new_tree_entry();
1417 e
->name
= to_atom(p
, n
);
1418 e
->versions
[0].mode
= 0;
1419 hashclr(e
->versions
[0].sha1
);
1420 t
->entries
[t
->entry_count
++] = e
;
1422 e
->tree
= new_tree_content(8);
1423 e
->versions
[1].mode
= S_IFDIR
;
1424 tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
);
1427 e
->versions
[1].mode
= mode
;
1428 hashcpy(e
->versions
[1].sha1
, sha1
);
1430 hashclr(root
->versions
[1].sha1
);
1434 static int tree_content_remove(
1435 struct tree_entry
*root
,
1437 struct tree_entry
*backup_leaf
)
1439 struct tree_content
*t
= root
->tree
;
1442 struct tree_entry
*e
;
1444 slash1
= strchr(p
, '/');
1450 for (i
= 0; i
< t
->entry_count
; i
++) {
1452 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1453 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1457 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
)) {
1458 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1459 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1460 hashclr(root
->versions
[1].sha1
);
1474 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1476 release_tree_content_recursive(e
->tree
);
1478 e
->versions
[1].mode
= 0;
1479 hashclr(e
->versions
[1].sha1
);
1480 hashclr(root
->versions
[1].sha1
);
1484 static int tree_content_get(
1485 struct tree_entry
*root
,
1487 struct tree_entry
*leaf
)
1489 struct tree_content
*t
= root
->tree
;
1492 struct tree_entry
*e
;
1494 slash1
= strchr(p
, '/');
1500 for (i
= 0; i
< t
->entry_count
; i
++) {
1502 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1504 memcpy(leaf
, e
, sizeof(*leaf
));
1505 if (e
->tree
&& is_null_sha1(e
->versions
[1].sha1
))
1506 leaf
->tree
= dup_tree_content(e
->tree
);
1511 if (!S_ISDIR(e
->versions
[1].mode
))
1515 return tree_content_get(e
, slash1
+ 1, leaf
);
1521 static int update_branch(struct branch
*b
)
1523 static const char *msg
= "fast-import";
1524 struct ref_lock
*lock
;
1525 unsigned char old_sha1
[20];
1527 if (is_null_sha1(b
->sha1
))
1529 if (read_ref(b
->name
, old_sha1
))
1531 lock
= lock_any_ref_for_update(b
->name
, old_sha1
, 0);
1533 return error("Unable to lock %s", b
->name
);
1534 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1535 struct commit
*old_cmit
, *new_cmit
;
1537 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1538 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1539 if (!old_cmit
|| !new_cmit
) {
1541 return error("Branch %s is missing commits.", b
->name
);
1544 if (!in_merge_bases(old_cmit
, &new_cmit
, 1)) {
1546 warning("Not updating %s"
1547 " (new tip %s does not contain %s)",
1548 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1552 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1553 return error("Unable to update %s", b
->name
);
1557 static void dump_branches(void)
1562 for (i
= 0; i
< branch_table_sz
; i
++) {
1563 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1564 failure
|= update_branch(b
);
1568 static void dump_tags(void)
1570 static const char *msg
= "fast-import";
1572 struct ref_lock
*lock
;
1573 char ref_name
[PATH_MAX
];
1575 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1576 sprintf(ref_name
, "tags/%s", t
->name
);
1577 lock
= lock_ref_sha1(ref_name
, NULL
);
1578 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1579 failure
|= error("Unable to update %s", ref_name
);
1583 static void dump_marks_helper(FILE *f
,
1589 for (k
= 0; k
< 1024; k
++) {
1590 if (m
->data
.sets
[k
])
1591 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1595 for (k
= 0; k
< 1024; k
++) {
1596 if (m
->data
.marked
[k
])
1597 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1598 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1603 static void dump_marks(void)
1605 static struct lock_file mark_lock
;
1612 mark_fd
= hold_lock_file_for_update(&mark_lock
, mark_file
, 0);
1614 failure
|= error("Unable to write marks file %s: %s",
1615 mark_file
, strerror(errno
));
1619 f
= fdopen(mark_fd
, "w");
1621 int saved_errno
= errno
;
1622 rollback_lock_file(&mark_lock
);
1623 failure
|= error("Unable to write marks file %s: %s",
1624 mark_file
, strerror(saved_errno
));
1629 * Since the lock file was fdopen()'ed, it should not be close()'ed.
1630 * Assign -1 to the lock file descriptor so that commit_lock_file()
1631 * won't try to close() it.
1635 dump_marks_helper(f
, 0, marks
);
1636 if (ferror(f
) || fclose(f
)) {
1637 int saved_errno
= errno
;
1638 rollback_lock_file(&mark_lock
);
1639 failure
|= error("Unable to write marks file %s: %s",
1640 mark_file
, strerror(saved_errno
));
1644 if (commit_lock_file(&mark_lock
)) {
1645 int saved_errno
= errno
;
1646 rollback_lock_file(&mark_lock
);
1647 failure
|= error("Unable to commit marks file %s: %s",
1648 mark_file
, strerror(saved_errno
));
1653 static void read_marks(void)
1656 FILE *f
= fopen(input_file
, "r");
1658 die_errno("cannot read '%s'", input_file
);
1659 while (fgets(line
, sizeof(line
), f
)) {
1662 unsigned char sha1
[20];
1663 struct object_entry
*e
;
1665 end
= strchr(line
, '\n');
1666 if (line
[0] != ':' || !end
)
1667 die("corrupt mark line: %s", line
);
1669 mark
= strtoumax(line
+ 1, &end
, 10);
1670 if (!mark
|| end
== line
+ 1
1671 || *end
!= ' ' || get_sha1(end
+ 1, sha1
))
1672 die("corrupt mark line: %s", line
);
1673 e
= find_object(sha1
);
1675 enum object_type type
= sha1_object_info(sha1
, NULL
);
1677 die("object not found: %s", sha1_to_hex(sha1
));
1678 e
= insert_object(sha1
);
1680 e
->pack_id
= MAX_PACK_ID
;
1681 e
->offset
= 1; /* just not zero! */
1683 insert_mark(mark
, e
);
1689 static int read_next_command(void)
1691 static int stdin_eof
= 0;
1694 unread_command_buf
= 0;
1699 if (unread_command_buf
) {
1700 unread_command_buf
= 0;
1702 struct recent_command
*rc
;
1704 strbuf_detach(&command_buf
, NULL
);
1705 stdin_eof
= strbuf_getline(&command_buf
, stdin
, '\n');
1709 if (!seen_non_option_command
1710 && prefixcmp(command_buf
.buf
, "feature ")
1711 && prefixcmp(command_buf
.buf
, "option ")) {
1720 cmd_hist
.next
= rc
->next
;
1721 cmd_hist
.next
->prev
= &cmd_hist
;
1725 rc
->buf
= command_buf
.buf
;
1726 rc
->prev
= cmd_tail
;
1727 rc
->next
= cmd_hist
.prev
;
1728 rc
->prev
->next
= rc
;
1731 } while (command_buf
.buf
[0] == '#');
1736 static void skip_optional_lf(void)
1738 int term_char
= fgetc(stdin
);
1739 if (term_char
!= '\n' && term_char
!= EOF
)
1740 ungetc(term_char
, stdin
);
1743 static void parse_mark(void)
1745 if (!prefixcmp(command_buf
.buf
, "mark :")) {
1746 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1747 read_next_command();
1753 static void parse_data(struct strbuf
*sb
)
1757 if (prefixcmp(command_buf
.buf
, "data "))
1758 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1760 if (!prefixcmp(command_buf
.buf
+ 5, "<<")) {
1761 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1762 size_t term_len
= command_buf
.len
- 5 - 2;
1764 strbuf_detach(&command_buf
, NULL
);
1766 if (strbuf_getline(&command_buf
, stdin
, '\n') == EOF
)
1767 die("EOF in data (terminator '%s' not found)", term
);
1768 if (term_len
== command_buf
.len
1769 && !strcmp(term
, command_buf
.buf
))
1771 strbuf_addbuf(sb
, &command_buf
);
1772 strbuf_addch(sb
, '\n');
1777 size_t n
= 0, length
;
1779 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1781 while (n
< length
) {
1782 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1783 if (!s
&& feof(stdin
))
1784 die("EOF in data (%lu bytes remaining)",
1785 (unsigned long)(length
- n
));
1793 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1795 const char *orig_src
= src
;
1800 strtoul(src
, &endp
, 10);
1801 if (errno
|| endp
== src
|| *endp
!= ' ')
1805 if (*src
!= '-' && *src
!= '+')
1808 strtoul(src
+ 1, &endp
, 10);
1809 if (errno
|| endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1812 strcpy(result
, orig_src
);
1816 static char *parse_ident(const char *buf
)
1822 gt
= strrchr(buf
, '>');
1824 die("Missing > in ident string: %s", buf
);
1827 die("Missing space after > in ident string: %s", buf
);
1829 name_len
= gt
- buf
;
1830 ident
= xmalloc(name_len
+ 24);
1831 strncpy(ident
, buf
, name_len
);
1835 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1836 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1838 case WHENSPEC_RFC2822
:
1839 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1840 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1843 if (strcmp("now", gt
))
1844 die("Date in ident must be 'now': %s", buf
);
1845 datestamp(ident
+ name_len
, 24);
1852 static void parse_new_blob(void)
1854 static struct strbuf buf
= STRBUF_INIT
;
1856 read_next_command();
1859 store_object(OBJ_BLOB
, &buf
, &last_blob
, NULL
, next_mark
);
1862 static void unload_one_branch(void)
1864 while (cur_active_branches
1865 && cur_active_branches
>= max_active_branches
) {
1866 uintmax_t min_commit
= ULONG_MAX
;
1867 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1869 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1870 if (e
->last_commit
< min_commit
) {
1872 min_commit
= e
->last_commit
;
1878 e
= p
->active_next_branch
;
1879 p
->active_next_branch
= e
->active_next_branch
;
1881 e
= active_branches
;
1882 active_branches
= e
->active_next_branch
;
1885 e
->active_next_branch
= NULL
;
1886 if (e
->branch_tree
.tree
) {
1887 release_tree_content_recursive(e
->branch_tree
.tree
);
1888 e
->branch_tree
.tree
= NULL
;
1890 cur_active_branches
--;
1894 static void load_branch(struct branch
*b
)
1896 load_tree(&b
->branch_tree
);
1899 b
->active_next_branch
= active_branches
;
1900 active_branches
= b
;
1901 cur_active_branches
++;
1902 branch_load_count
++;
1906 static void file_change_m(struct branch
*b
)
1908 const char *p
= command_buf
.buf
+ 2;
1909 static struct strbuf uq
= STRBUF_INIT
;
1911 struct object_entry
*oe
= oe
;
1912 unsigned char sha1
[20];
1913 uint16_t mode
, inline_data
= 0;
1915 p
= get_mode(p
, &mode
);
1917 die("Corrupt mode: %s", command_buf
.buf
);
1922 case S_IFREG
| 0644:
1923 case S_IFREG
| 0755:
1929 die("Corrupt mode: %s", command_buf
.buf
);
1934 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1935 hashcpy(sha1
, oe
->sha1
);
1937 } else if (!prefixcmp(p
, "inline")) {
1941 if (get_sha1_hex(p
, sha1
))
1942 die("Invalid SHA1: %s", command_buf
.buf
);
1943 oe
= find_object(sha1
);
1947 die("Missing space after SHA1: %s", command_buf
.buf
);
1950 if (!unquote_c_style(&uq
, p
, &endp
)) {
1952 die("Garbage after path in: %s", command_buf
.buf
);
1956 if (S_ISGITLINK(mode
)) {
1958 die("Git links cannot be specified 'inline': %s",
1961 if (oe
->type
!= OBJ_COMMIT
)
1962 die("Not a commit (actually a %s): %s",
1963 typename(oe
->type
), command_buf
.buf
);
1966 * Accept the sha1 without checking; it expected to be in
1967 * another repository.
1969 } else if (inline_data
) {
1970 static struct strbuf buf
= STRBUF_INIT
;
1973 strbuf_addstr(&uq
, p
);
1976 read_next_command();
1978 store_object(OBJ_BLOB
, &buf
, &last_blob
, sha1
, 0);
1980 if (oe
->type
!= OBJ_BLOB
)
1981 die("Not a blob (actually a %s): %s",
1982 typename(oe
->type
), command_buf
.buf
);
1984 enum object_type type
= sha1_object_info(sha1
, NULL
);
1986 die("Blob not found: %s", command_buf
.buf
);
1987 if (type
!= OBJ_BLOB
)
1988 die("Not a blob (actually a %s): %s",
1989 typename(type
), command_buf
.buf
);
1992 tree_content_set(&b
->branch_tree
, p
, sha1
, mode
, NULL
);
1995 static void file_change_d(struct branch
*b
)
1997 const char *p
= command_buf
.buf
+ 2;
1998 static struct strbuf uq
= STRBUF_INIT
;
2002 if (!unquote_c_style(&uq
, p
, &endp
)) {
2004 die("Garbage after path in: %s", command_buf
.buf
);
2007 tree_content_remove(&b
->branch_tree
, p
, NULL
);
2010 static void file_change_cr(struct branch
*b
, int rename
)
2013 static struct strbuf s_uq
= STRBUF_INIT
;
2014 static struct strbuf d_uq
= STRBUF_INIT
;
2016 struct tree_entry leaf
;
2018 s
= command_buf
.buf
+ 2;
2019 strbuf_reset(&s_uq
);
2020 if (!unquote_c_style(&s_uq
, s
, &endp
)) {
2022 die("Missing space after source: %s", command_buf
.buf
);
2024 endp
= strchr(s
, ' ');
2026 die("Missing space after source: %s", command_buf
.buf
);
2027 strbuf_add(&s_uq
, s
, endp
- s
);
2033 die("Missing dest: %s", command_buf
.buf
);
2036 strbuf_reset(&d_uq
);
2037 if (!unquote_c_style(&d_uq
, d
, &endp
)) {
2039 die("Garbage after dest in: %s", command_buf
.buf
);
2043 memset(&leaf
, 0, sizeof(leaf
));
2045 tree_content_remove(&b
->branch_tree
, s
, &leaf
);
2047 tree_content_get(&b
->branch_tree
, s
, &leaf
);
2048 if (!leaf
.versions
[1].mode
)
2049 die("Path %s not in branch", s
);
2050 tree_content_set(&b
->branch_tree
, d
,
2051 leaf
.versions
[1].sha1
,
2052 leaf
.versions
[1].mode
,
2056 static void file_change_deleteall(struct branch
*b
)
2058 release_tree_content_recursive(b
->branch_tree
.tree
);
2059 hashclr(b
->branch_tree
.versions
[0].sha1
);
2060 hashclr(b
->branch_tree
.versions
[1].sha1
);
2061 load_tree(&b
->branch_tree
);
2064 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2066 if (!buf
|| size
< 46)
2067 die("Not a valid commit: %s", sha1_to_hex(b
->sha1
));
2068 if (memcmp("tree ", buf
, 5)
2069 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
2070 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
2071 hashcpy(b
->branch_tree
.versions
[0].sha1
,
2072 b
->branch_tree
.versions
[1].sha1
);
2075 static void parse_from_existing(struct branch
*b
)
2077 if (is_null_sha1(b
->sha1
)) {
2078 hashclr(b
->branch_tree
.versions
[0].sha1
);
2079 hashclr(b
->branch_tree
.versions
[1].sha1
);
2084 buf
= read_object_with_reference(b
->sha1
,
2085 commit_type
, &size
, b
->sha1
);
2086 parse_from_commit(b
, buf
, size
);
2091 static int parse_from(struct branch
*b
)
2096 if (prefixcmp(command_buf
.buf
, "from "))
2099 if (b
->branch_tree
.tree
) {
2100 release_tree_content_recursive(b
->branch_tree
.tree
);
2101 b
->branch_tree
.tree
= NULL
;
2104 from
= strchr(command_buf
.buf
, ' ') + 1;
2105 s
= lookup_branch(from
);
2107 die("Can't create a branch from itself: %s", b
->name
);
2109 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
2110 hashcpy(b
->sha1
, s
->sha1
);
2111 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
2112 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
2113 } else if (*from
== ':') {
2114 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
2115 struct object_entry
*oe
= find_mark(idnum
);
2116 if (oe
->type
!= OBJ_COMMIT
)
2117 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2118 hashcpy(b
->sha1
, oe
->sha1
);
2119 if (oe
->pack_id
!= MAX_PACK_ID
) {
2121 char *buf
= gfi_unpack_entry(oe
, &size
);
2122 parse_from_commit(b
, buf
, size
);
2125 parse_from_existing(b
);
2126 } else if (!get_sha1(from
, b
->sha1
))
2127 parse_from_existing(b
);
2129 die("Invalid ref name or SHA1 expression: %s", from
);
2131 read_next_command();
2135 static struct hash_list
*parse_merge(unsigned int *count
)
2137 struct hash_list
*list
= NULL
, *n
, *e
= e
;
2142 while (!prefixcmp(command_buf
.buf
, "merge ")) {
2143 from
= strchr(command_buf
.buf
, ' ') + 1;
2144 n
= xmalloc(sizeof(*n
));
2145 s
= lookup_branch(from
);
2147 hashcpy(n
->sha1
, s
->sha1
);
2148 else if (*from
== ':') {
2149 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
2150 struct object_entry
*oe
= find_mark(idnum
);
2151 if (oe
->type
!= OBJ_COMMIT
)
2152 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2153 hashcpy(n
->sha1
, oe
->sha1
);
2154 } else if (!get_sha1(from
, n
->sha1
)) {
2156 char *buf
= read_object_with_reference(n
->sha1
,
2157 commit_type
, &size
, n
->sha1
);
2158 if (!buf
|| size
< 46)
2159 die("Not a valid commit: %s", from
);
2162 die("Invalid ref name or SHA1 expression: %s", from
);
2171 read_next_command();
2176 static void parse_new_commit(void)
2178 static struct strbuf msg
= STRBUF_INIT
;
2181 char *author
= NULL
;
2182 char *committer
= NULL
;
2183 struct hash_list
*merge_list
= NULL
;
2184 unsigned int merge_count
;
2186 /* Obtain the branch name from the rest of our command */
2187 sp
= strchr(command_buf
.buf
, ' ') + 1;
2188 b
= lookup_branch(sp
);
2192 read_next_command();
2194 if (!prefixcmp(command_buf
.buf
, "author ")) {
2195 author
= parse_ident(command_buf
.buf
+ 7);
2196 read_next_command();
2198 if (!prefixcmp(command_buf
.buf
, "committer ")) {
2199 committer
= parse_ident(command_buf
.buf
+ 10);
2200 read_next_command();
2203 die("Expected committer but didn't get one");
2205 read_next_command();
2207 merge_list
= parse_merge(&merge_count
);
2209 /* ensure the branch is active/loaded */
2210 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2211 unload_one_branch();
2216 while (command_buf
.len
> 0) {
2217 if (!prefixcmp(command_buf
.buf
, "M "))
2219 else if (!prefixcmp(command_buf
.buf
, "D "))
2221 else if (!prefixcmp(command_buf
.buf
, "R "))
2222 file_change_cr(b
, 1);
2223 else if (!prefixcmp(command_buf
.buf
, "C "))
2224 file_change_cr(b
, 0);
2225 else if (!strcmp("deleteall", command_buf
.buf
))
2226 file_change_deleteall(b
);
2228 unread_command_buf
= 1;
2231 if (read_next_command() == EOF
)
2235 /* build the tree and the commit */
2236 store_tree(&b
->branch_tree
);
2237 hashcpy(b
->branch_tree
.versions
[0].sha1
,
2238 b
->branch_tree
.versions
[1].sha1
);
2240 strbuf_reset(&new_data
);
2241 strbuf_addf(&new_data
, "tree %s\n",
2242 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
2243 if (!is_null_sha1(b
->sha1
))
2244 strbuf_addf(&new_data
, "parent %s\n", sha1_to_hex(b
->sha1
));
2245 while (merge_list
) {
2246 struct hash_list
*next
= merge_list
->next
;
2247 strbuf_addf(&new_data
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
2251 strbuf_addf(&new_data
,
2255 author
? author
: committer
, committer
);
2256 strbuf_addbuf(&new_data
, &msg
);
2260 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, b
->sha1
, next_mark
))
2261 b
->pack_id
= pack_id
;
2262 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2265 static void parse_new_tag(void)
2267 static struct strbuf msg
= STRBUF_INIT
;
2273 uintmax_t from_mark
= 0;
2274 unsigned char sha1
[20];
2276 /* Obtain the new tag name from the rest of our command */
2277 sp
= strchr(command_buf
.buf
, ' ') + 1;
2278 t
= pool_alloc(sizeof(struct tag
));
2280 t
->name
= pool_strdup(sp
);
2282 last_tag
->next_tag
= t
;
2286 read_next_command();
2289 if (prefixcmp(command_buf
.buf
, "from "))
2290 die("Expected from command, got %s", command_buf
.buf
);
2291 from
= strchr(command_buf
.buf
, ' ') + 1;
2292 s
= lookup_branch(from
);
2294 hashcpy(sha1
, s
->sha1
);
2295 } else if (*from
== ':') {
2296 struct object_entry
*oe
;
2297 from_mark
= strtoumax(from
+ 1, NULL
, 10);
2298 oe
= find_mark(from_mark
);
2299 if (oe
->type
!= OBJ_COMMIT
)
2300 die("Mark :%" PRIuMAX
" not a commit", from_mark
);
2301 hashcpy(sha1
, oe
->sha1
);
2302 } else if (!get_sha1(from
, sha1
)) {
2306 buf
= read_object_with_reference(sha1
,
2307 commit_type
, &size
, sha1
);
2308 if (!buf
|| size
< 46)
2309 die("Not a valid commit: %s", from
);
2312 die("Invalid ref name or SHA1 expression: %s", from
);
2313 read_next_command();
2316 if (!prefixcmp(command_buf
.buf
, "tagger ")) {
2317 tagger
= parse_ident(command_buf
.buf
+ 7);
2318 read_next_command();
2322 /* tag payload/message */
2325 /* build the tag object */
2326 strbuf_reset(&new_data
);
2328 strbuf_addf(&new_data
,
2332 sha1_to_hex(sha1
), commit_type
, t
->name
);
2334 strbuf_addf(&new_data
,
2335 "tagger %s\n", tagger
);
2336 strbuf_addch(&new_data
, '\n');
2337 strbuf_addbuf(&new_data
, &msg
);
2340 if (store_object(OBJ_TAG
, &new_data
, NULL
, t
->sha1
, 0))
2341 t
->pack_id
= MAX_PACK_ID
;
2343 t
->pack_id
= pack_id
;
2346 static void parse_reset_branch(void)
2351 /* Obtain the branch name from the rest of our command */
2352 sp
= strchr(command_buf
.buf
, ' ') + 1;
2353 b
= lookup_branch(sp
);
2356 hashclr(b
->branch_tree
.versions
[0].sha1
);
2357 hashclr(b
->branch_tree
.versions
[1].sha1
);
2358 if (b
->branch_tree
.tree
) {
2359 release_tree_content_recursive(b
->branch_tree
.tree
);
2360 b
->branch_tree
.tree
= NULL
;
2365 read_next_command();
2367 if (command_buf
.len
> 0)
2368 unread_command_buf
= 1;
2371 static void parse_checkpoint(void)
2382 static void parse_progress(void)
2384 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
2385 fputc('\n', stdout
);
2390 static void option_import_marks(const char *marks
)
2392 input_file
= xstrdup(marks
);
2395 static void option_date_format(const char *fmt
)
2397 if (!strcmp(fmt
, "raw"))
2398 whenspec
= WHENSPEC_RAW
;
2399 else if (!strcmp(fmt
, "rfc2822"))
2400 whenspec
= WHENSPEC_RFC2822
;
2401 else if (!strcmp(fmt
, "now"))
2402 whenspec
= WHENSPEC_NOW
;
2404 die("unknown --date-format argument %s", fmt
);
2407 static void option_max_pack_size(const char *packsize
)
2409 max_packsize
= strtoumax(packsize
, NULL
, 0) * 1024 * 1024;
2412 static void option_depth(const char *depth
)
2414 max_depth
= strtoul(depth
, NULL
, 0);
2415 if (max_depth
> MAX_DEPTH
)
2416 die("--depth cannot exceed %u", MAX_DEPTH
);
2419 static void option_active_branches(const char *branches
)
2421 max_active_branches
= strtoul(branches
, NULL
, 0);
2424 static void option_export_marks(const char *marks
)
2426 mark_file
= xstrdup(marks
);
2429 static void option_export_pack_edges(const char *edges
)
2433 pack_edges
= fopen(edges
, "a");
2435 die_errno("Cannot open '%s'", edges
);
2438 static void parse_one_option(const char *option
, int optional
)
2440 if (!prefixcmp(option
, "date-format=")) {
2441 option_date_format(option
+ 12);
2442 } else if (!prefixcmp(option
, "max-pack-size=")) {
2443 option_max_pack_size(option
+ 14);
2444 } else if (!prefixcmp(option
, "depth=")) {
2445 option_depth(option
+ 6);
2446 } else if (!prefixcmp(option
, "active-branches=")) {
2447 option_active_branches(option
+ 16);
2448 } else if (!prefixcmp(option
, "import-marks=")) {
2449 option_import_marks(option
+ 13);
2450 } else if (!prefixcmp(option
, "export-marks=")) {
2451 option_export_marks(option
+ 13);
2452 } else if (!prefixcmp(option
, "export-pack-edges=")) {
2453 option_export_pack_edges(option
+ 18);
2454 } else if (!prefixcmp(option
, "force")) {
2456 } else if (!prefixcmp(option
, "quiet")) {
2458 } else if (!prefixcmp(option
, "stats")) {
2460 } else if (!optional
) {
2461 die("Unsupported option: %s", option
);
2465 static void parse_feature(void)
2467 char *feature
= command_buf
.buf
+ 8;
2469 if (!prefixcmp(feature
, "date-format=")) {
2470 option_date_format(feature
+ 12);
2471 } else if (!strcmp("git-options", feature
)) {
2472 options_enabled
= 1;
2474 die("This version of fast-import does not support feature %s.", feature
);
2478 static void parse_option(void)
2480 char *option
= command_buf
.buf
+ 11;
2482 if (!options_enabled
)
2483 die("Got option command '%s' before options feature'", option
);
2485 if (seen_non_option_command
)
2486 die("Got option command '%s' after non-option command", option
);
2488 /* ignore unknown options, instead of erroring */
2489 parse_one_option(option
, 1);
2492 static void parse_nongit_option(void)
2497 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2499 if (!strcmp(k
, "pack.depth")) {
2500 max_depth
= git_config_int(k
, v
);
2501 if (max_depth
> MAX_DEPTH
)
2502 max_depth
= MAX_DEPTH
;
2505 if (!strcmp(k
, "pack.compression")) {
2506 int level
= git_config_int(k
, v
);
2508 level
= Z_DEFAULT_COMPRESSION
;
2509 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
2510 die("bad pack compression level %d", level
);
2511 pack_compression_level
= level
;
2512 pack_compression_seen
= 1;
2515 return git_default_config(k
, v
, cb
);
2518 static const char fast_import_usage
[] =
2519 "git fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2521 static void parse_argv(void)
2525 for (i
= 1; i
< global_argc
; i
++) {
2526 const char *a
= global_argv
[i
];
2528 if (*a
!= '-' || !strcmp(a
, "--"))
2531 /* error on unknown options */
2532 parse_one_option(a
+ 2, 0);
2534 if (i
!= global_argc
)
2535 usage(fast_import_usage
);
2537 seen_non_option_command
= 1;
2542 int main(int argc
, const char **argv
)
2546 git_extract_argv0_path(argv
[0]);
2548 setup_git_directory();
2549 git_config(git_pack_config
, NULL
);
2550 if (!pack_compression_seen
&& core_compression_seen
)
2551 pack_compression_level
= core_compression_level
;
2553 alloc_objects(object_entry_alloc
);
2554 strbuf_init(&command_buf
, 0);
2555 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2556 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2557 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2558 marks
= pool_calloc(1, sizeof(struct mark_set
));
2563 rc_free
= pool_alloc(cmd_save
* sizeof(*rc_free
));
2564 for (i
= 0; i
< (cmd_save
- 1); i
++)
2565 rc_free
[i
].next
= &rc_free
[i
+ 1];
2566 rc_free
[cmd_save
- 1].next
= NULL
;
2568 prepare_packed_git();
2570 set_die_routine(die_nicely
);
2571 while (read_next_command() != EOF
) {
2572 if (!strcmp("blob", command_buf
.buf
))
2574 else if (!prefixcmp(command_buf
.buf
, "commit "))
2576 else if (!prefixcmp(command_buf
.buf
, "tag "))
2578 else if (!prefixcmp(command_buf
.buf
, "reset "))
2579 parse_reset_branch();
2580 else if (!strcmp("checkpoint", command_buf
.buf
))
2582 else if (!prefixcmp(command_buf
.buf
, "progress "))
2584 else if (!prefixcmp(command_buf
.buf
, "feature "))
2586 else if (!prefixcmp(command_buf
.buf
, "option git "))
2588 else if (!prefixcmp(command_buf
.buf
, "option "))
2589 parse_nongit_option();
2591 die("Unsupported command: %s", command_buf
.buf
);
2594 /* argv hasn't been parsed yet, do so */
2595 if (!seen_non_option_command
)
2609 uintmax_t total_count
= 0, duplicate_count
= 0;
2610 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2611 total_count
+= object_count_by_type
[i
];
2612 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2613 duplicate_count
+= duplicate_count_by_type
[i
];
2615 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2616 fprintf(stderr
, "---------------------------------------------------------------------\n");
2617 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
2618 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
2619 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
]);
2620 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
]);
2621 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
]);
2622 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
]);
2623 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2624 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2625 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2626 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2627 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
2628 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2629 fprintf(stderr
, "---------------------------------------------------------------------\n");
2631 fprintf(stderr
, "---------------------------------------------------------------------\n");
2632 fprintf(stderr
, "\n");
2635 return failure
? 1 : 0;