2 Format of STDIN stream:
12 new_blob ::= 'blob' lf
15 file_content ::= data;
17 new_commit ::= 'commit' sp ref_str lf
19 ('author' sp name '<' email '>' ts tz lf)?
20 'committer' sp name '<' email '>' ts tz lf
22 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
23 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
28 file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
31 mode ::= '644' | '755';
33 new_tag ::= 'tag' sp tag_str lf
34 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
35 'tagger' sp name '<' email '>' ts tz lf
39 reset_branch ::= 'reset' sp ref_str lf;
41 # note: the first idnum in a stream should be 1 and subsequent
42 # idnums should not have gaps between values as this will cause
43 # the stream parser to reserve space for the gapped values. An
44 # idnum can be updated in the future to a new object by issuing
45 # a new mark directive with the old idnum.
47 mark ::= 'mark' sp idnum lf;
49 # note: declen indicates the length of binary_data in bytes.
50 # declen does not include the lf preceeding or trailing the
53 data ::= 'data' sp declen lf
57 # note: quoted strings are C-style quoting supporting \c for
58 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
59 # is the signed byte value in octal. Note that the only
60 # characters which must actually be escaped to protect the
61 # stream formatting is: \, " and LF. Otherwise these values
64 ref_str ::= ref | '"' quoted(ref) '"' ;
65 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
66 tag_str ::= tag | '"' quoted(tag) '"' ;
67 path_str ::= path | '"' quoted(path) '"' ;
69 declen ::= # unsigned 32 bit value, ascii base10 notation;
70 binary_data ::= # file content, not interpreted;
72 sp ::= # ASCII space character;
73 lf ::= # ASCII newline (LF) character;
75 # note: a colon (':') must precede the numerical value assigned to
76 # an idnum. This is to distinguish it from a ref or tag name as
77 # GIT does not permit ':' in ref or tag strings.
80 path ::= # GIT style file path, e.g. "a/b/c";
81 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
82 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
83 sha1exp ::= # Any valid GIT SHA1 expression;
84 hexsha1 ::= # SHA1 in hexadecimal format;
86 # note: name and email are UTF8 strings, however name must not
87 # contain '<' or lf and email must not contain any of the
88 # following: '<', '>', lf.
90 name ::= # valid GIT author/committer name;
91 email ::= # valid GIT author/committer email;
92 ts ::= # time since the epoch in seconds, ascii base10 notation;
93 tz ::= # GIT style timezone;
104 #include "csum-file.h"
110 struct object_entry
*next
;
111 enum object_type type
;
112 unsigned long offset
;
113 unsigned char sha1
[20];
116 struct object_entry_pool
118 struct object_entry_pool
*next_pool
;
119 struct object_entry
*next_free
;
120 struct object_entry
*end
;
121 struct object_entry entries
[FLEX_ARRAY
]; /* more */
128 struct object_entry
*marked
[1024];
129 struct mark_set
*sets
[1024];
139 unsigned char sha1
[20];
144 struct mem_pool
*next_pool
;
147 char space
[FLEX_ARRAY
]; /* more */
152 struct atom_str
*next_atom
;
154 char str_dat
[FLEX_ARRAY
]; /* more */
160 struct tree_content
*tree
;
161 struct atom_str
* name
;
165 unsigned char sha1
[20];
171 unsigned int entry_capacity
; /* must match avail_tree_content */
172 unsigned int entry_count
;
173 unsigned int delta_depth
;
174 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
177 struct avail_tree_content
179 unsigned int entry_capacity
; /* must match tree_content */
180 struct avail_tree_content
*next_avail
;
185 struct branch
*table_next_branch
;
186 struct branch
*active_next_branch
;
188 unsigned long last_commit
;
189 struct tree_entry branch_tree
;
190 unsigned char sha1
[20];
195 struct tag
*next_tag
;
197 unsigned char sha1
[20];
208 struct hash_list
*next
;
209 unsigned char sha1
[20];
212 /* Stats and misc. counters */
213 static unsigned long max_depth
= 10;
214 static unsigned long alloc_count
;
215 static unsigned long branch_count
;
216 static unsigned long branch_load_count
;
217 static unsigned long remap_count
;
218 static unsigned long object_count
;
219 static unsigned long duplicate_count
;
220 static unsigned long marks_set_count
;
221 static unsigned long object_count_by_type
[9];
222 static unsigned long duplicate_count_by_type
[9];
223 static unsigned long delta_count_by_type
[9];
226 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
227 static size_t total_allocd
;
228 static struct mem_pool
*mem_pool
;
230 /* Atom management */
231 static unsigned int atom_table_sz
= 4451;
232 static unsigned int atom_cnt
;
233 static struct atom_str
**atom_table
;
235 /* The .pack file being generated */
237 static unsigned long pack_size
;
238 static unsigned char pack_sha1
[20];
239 static unsigned char* pack_base
;
240 static unsigned long pack_moff
;
241 static unsigned long pack_mlen
= 128*1024*1024;
242 static unsigned long page_size
;
244 /* Table of objects we've written. */
245 static unsigned int object_entry_alloc
= 5000;
246 static struct object_entry_pool
*blocks
;
247 static struct object_entry
*object_table
[1 << 16];
248 static struct mark_set
*marks
;
249 static const char* mark_file
;
252 static struct last_object last_blob
;
254 /* Tree management */
255 static unsigned int tree_entry_alloc
= 1000;
256 static void *avail_tree_entry
;
257 static unsigned int avail_tree_table_sz
= 100;
258 static struct avail_tree_content
**avail_tree_table
;
259 static struct dbuf old_tree
;
260 static struct dbuf new_tree
;
263 static unsigned long max_active_branches
= 5;
264 static unsigned long cur_active_branches
;
265 static unsigned long branch_table_sz
= 1039;
266 static struct branch
**branch_table
;
267 static struct branch
*active_branches
;
270 static struct tag
*first_tag
;
271 static struct tag
*last_tag
;
273 /* Input stream parsing */
274 static struct strbuf command_buf
;
275 static unsigned long next_mark
;
276 static struct dbuf new_data
;
277 static FILE* branch_log
;
280 static void alloc_objects(int cnt
)
282 struct object_entry_pool
*b
;
284 b
= xmalloc(sizeof(struct object_entry_pool
)
285 + cnt
* sizeof(struct object_entry
));
286 b
->next_pool
= blocks
;
287 b
->next_free
= b
->entries
;
288 b
->end
= b
->entries
+ cnt
;
293 static struct object_entry
* new_object(unsigned char *sha1
)
295 struct object_entry
*e
;
297 if (blocks
->next_free
== blocks
->end
)
298 alloc_objects(object_entry_alloc
);
300 e
= blocks
->next_free
++;
301 hashcpy(e
->sha1
, sha1
);
305 static struct object_entry
* find_object(unsigned char *sha1
)
307 unsigned int h
= sha1
[0] << 8 | sha1
[1];
308 struct object_entry
*e
;
309 for (e
= object_table
[h
]; e
; e
= e
->next
)
310 if (!hashcmp(sha1
, e
->sha1
))
315 static struct object_entry
* insert_object(unsigned char *sha1
)
317 unsigned int h
= sha1
[0] << 8 | sha1
[1];
318 struct object_entry
*e
= object_table
[h
];
319 struct object_entry
*p
= NULL
;
322 if (!hashcmp(sha1
, e
->sha1
))
328 e
= new_object(sha1
);
338 static unsigned int hc_str(const char *s
, size_t len
)
346 static void* pool_alloc(size_t len
)
351 for (p
= mem_pool
; p
; p
= p
->next_pool
)
352 if ((p
->end
- p
->next_free
>= len
))
356 if (len
>= (mem_pool_alloc
/2)) {
360 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
361 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
362 p
->next_pool
= mem_pool
;
363 p
->next_free
= p
->space
;
364 p
->end
= p
->next_free
+ mem_pool_alloc
;
369 /* round out to a pointer alignment */
370 if (len
& (sizeof(void*) - 1))
371 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
376 static void* pool_calloc(size_t count
, size_t size
)
378 size_t len
= count
* size
;
379 void *r
= pool_alloc(len
);
384 static char* pool_strdup(const char *s
)
386 char *r
= pool_alloc(strlen(s
) + 1);
391 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
394 if (b
->capacity
>= maxlen
)
398 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
399 b
->buffer
= xmalloc(b
->capacity
);
402 static void insert_mark(unsigned long idnum
, struct object_entry
*oe
)
404 struct mark_set
*s
= marks
;
405 while ((idnum
>> s
->shift
) >= 1024) {
406 s
= pool_calloc(1, sizeof(struct mark_set
));
407 s
->shift
= marks
->shift
+ 10;
408 s
->data
.sets
[0] = marks
;
412 unsigned long i
= idnum
>> s
->shift
;
413 idnum
-= i
<< s
->shift
;
414 if (!s
->data
.sets
[i
]) {
415 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
416 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
420 if (!s
->data
.marked
[idnum
])
422 s
->data
.marked
[idnum
] = oe
;
425 static struct object_entry
* find_mark(unsigned long idnum
)
427 unsigned long orig_idnum
= idnum
;
428 struct mark_set
*s
= marks
;
429 struct object_entry
*oe
= NULL
;
430 if ((idnum
>> s
->shift
) < 1024) {
431 while (s
&& s
->shift
) {
432 unsigned long i
= idnum
>> s
->shift
;
433 idnum
-= i
<< s
->shift
;
437 oe
= s
->data
.marked
[idnum
];
440 die("mark :%lu not declared", orig_idnum
);
444 static struct atom_str
* to_atom(const char *s
, size_t len
)
446 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
449 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
450 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
453 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
455 strncpy(c
->str_dat
, s
, len
);
457 c
->next_atom
= atom_table
[hc
];
463 static struct branch
* lookup_branch(const char *name
)
465 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
468 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
469 if (!strcmp(name
, b
->name
))
474 static struct branch
* new_branch(const char *name
)
476 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
477 struct branch
* b
= lookup_branch(name
);
480 die("Invalid attempt to create duplicate branch: %s", name
);
481 if (check_ref_format(name
))
482 die("Branch name doesn't conform to GIT standards: %s", name
);
484 b
= pool_calloc(1, sizeof(struct branch
));
485 b
->name
= pool_strdup(name
);
486 b
->table_next_branch
= branch_table
[hc
];
487 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
488 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
489 branch_table
[hc
] = b
;
494 static unsigned int hc_entries(unsigned int cnt
)
496 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
497 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
500 static struct tree_content
* new_tree_content(unsigned int cnt
)
502 struct avail_tree_content
*f
, *l
= NULL
;
503 struct tree_content
*t
;
504 unsigned int hc
= hc_entries(cnt
);
506 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
507 if (f
->entry_capacity
>= cnt
)
512 l
->next_avail
= f
->next_avail
;
514 avail_tree_table
[hc
] = f
->next_avail
;
516 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
517 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
518 f
->entry_capacity
= cnt
;
521 t
= (struct tree_content
*)f
;
527 static void release_tree_entry(struct tree_entry
*e
);
528 static void release_tree_content(struct tree_content
*t
)
530 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
531 unsigned int hc
= hc_entries(f
->entry_capacity
);
532 f
->next_avail
= avail_tree_table
[hc
];
533 avail_tree_table
[hc
] = f
;
536 static void release_tree_content_recursive(struct tree_content
*t
)
539 for (i
= 0; i
< t
->entry_count
; i
++)
540 release_tree_entry(t
->entries
[i
]);
541 release_tree_content(t
);
544 static struct tree_content
* grow_tree_content(
545 struct tree_content
*t
,
548 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
549 r
->entry_count
= t
->entry_count
;
550 r
->delta_depth
= t
->delta_depth
;
551 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
552 release_tree_content(t
);
556 static struct tree_entry
* new_tree_entry()
558 struct tree_entry
*e
;
560 if (!avail_tree_entry
) {
561 unsigned int n
= tree_entry_alloc
;
562 total_allocd
+= n
* sizeof(struct tree_entry
);
563 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
565 *((void**)e
) = e
+ 1;
571 e
= avail_tree_entry
;
572 avail_tree_entry
= *((void**)e
);
576 static void release_tree_entry(struct tree_entry
*e
)
579 release_tree_content_recursive(e
->tree
);
580 *((void**)e
) = avail_tree_entry
;
581 avail_tree_entry
= e
;
584 static void yread(int fd
, void *buffer
, size_t length
)
587 while (ret
< length
) {
588 ssize_t size
= xread(fd
, (char *) buffer
+ ret
, length
- ret
);
590 die("Read from descriptor %i: end of stream", fd
);
592 die("Read from descriptor %i: %s", fd
, strerror(errno
));
597 static size_t encode_header(
598 enum object_type type
,
605 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
606 die("bad type %d", type
);
608 c
= (type
<< 4) | (size
& 15);
620 static int store_object(
621 enum object_type type
,
624 struct last_object
*last
,
625 unsigned char *sha1out
,
629 struct object_entry
*e
;
630 unsigned char hdr
[96];
631 unsigned char sha1
[20];
632 unsigned long hdrlen
, deltalen
;
636 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
638 SHA1_Update(&c
, hdr
, hdrlen
);
639 SHA1_Update(&c
, dat
, datlen
);
640 SHA1_Final(sha1
, &c
);
642 hashcpy(sha1out
, sha1
);
644 e
= insert_object(sha1
);
646 insert_mark(mark
, e
);
649 duplicate_count_by_type
[type
]++;
653 e
->offset
= pack_size
;
655 object_count_by_type
[type
]++;
657 if (last
&& last
->data
&& last
->depth
< max_depth
)
658 delta
= diff_delta(last
->data
, last
->len
,
664 memset(&s
, 0, sizeof(s
));
665 deflateInit(&s
, zlib_compression_level
);
668 delta_count_by_type
[type
]++;
671 s
.avail_in
= deltalen
;
672 hdrlen
= encode_header(OBJ_DELTA
, deltalen
, hdr
);
673 write_or_die(pack_fd
, hdr
, hdrlen
);
674 write_or_die(pack_fd
, last
->sha1
, sizeof(sha1
));
675 pack_size
+= hdrlen
+ sizeof(sha1
);
681 hdrlen
= encode_header(type
, datlen
, hdr
);
682 write_or_die(pack_fd
, hdr
, hdrlen
);
686 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
687 s
.next_out
= out
= xmalloc(s
.avail_out
);
688 while (deflate(&s
, Z_FINISH
) == Z_OK
)
692 write_or_die(pack_fd
, out
, s
.total_out
);
693 pack_size
+= s
.total_out
;
699 if (last
->data
&& !last
->no_free
)
703 hashcpy(last
->sha1
, sha1
);
708 static unsigned char* map_pack(unsigned long offset
, unsigned int *left
)
710 if (offset
>= pack_size
)
711 die("object offset outside of pack file");
713 || offset
< pack_moff
714 || (offset
+ 20) >= (pack_moff
+ pack_mlen
)) {
716 munmap(pack_base
, pack_mlen
);
717 pack_moff
= (offset
/ page_size
) * page_size
;
718 pack_base
= mmap(NULL
,pack_mlen
,PROT_READ
,MAP_SHARED
,
720 if (pack_base
== MAP_FAILED
)
721 die("Failed to map generated pack: %s", strerror(errno
));
726 *left
= pack_mlen
- offset
;
727 return pack_base
+ offset
;
730 static unsigned long unpack_object_header(unsigned long offset
,
731 enum object_type
*type
,
732 unsigned long *sizep
)
738 c
= *map_pack(offset
++, NULL
);
739 *type
= (c
>> 4) & 7;
743 c
= *map_pack(offset
++, NULL
);
744 size
+= (c
& 0x7f) << shift
;
751 static void *unpack_non_delta_entry(unsigned long o
, unsigned long sz
)
754 unsigned char *result
;
756 result
= xmalloc(sz
+ 1);
759 memset(&stream
, 0, sizeof(stream
));
760 stream
.next_in
= map_pack(o
, &stream
.avail_in
);
761 stream
.next_out
= result
;
762 stream
.avail_out
= sz
;
764 inflateInit(&stream
);
766 int st
= inflate(&stream
, Z_FINISH
);
767 if (st
== Z_STREAM_END
)
769 if (st
== Z_OK
|| st
== Z_BUF_ERROR
) {
770 o
= stream
.next_in
- pack_base
+ pack_moff
;
771 stream
.next_in
= map_pack(o
, &stream
.avail_in
);
774 die("Error %i from zlib during inflate.", st
);
777 if (stream
.total_out
!= sz
)
778 die("Error after inflate: sizes mismatch");
782 static void *unpack_entry(unsigned long offset
,
783 unsigned long *sizep
,
784 unsigned int *delta_depth
);
786 static void *unpack_delta_entry(unsigned long offset
,
787 unsigned long delta_size
,
788 unsigned long *sizep
,
789 unsigned int *delta_depth
)
791 struct object_entry
*base_oe
;
792 unsigned char *base_sha1
;
793 void *delta_data
, *base
, *result
;
794 unsigned long base_size
, result_size
;
796 base_sha1
= map_pack(offset
, NULL
);
797 base_oe
= find_object(base_sha1
);
799 die("I'm broken; I can't find a base I know must be here.");
800 base
= unpack_entry(base_oe
->offset
, &base_size
, delta_depth
);
801 delta_data
= unpack_non_delta_entry(offset
+ 20, delta_size
);
802 result
= patch_delta(base
, base_size
,
803 delta_data
, delta_size
,
806 die("failed to apply delta");
809 *sizep
= result_size
;
814 static void *unpack_entry(unsigned long offset
,
815 unsigned long *sizep
,
816 unsigned int *delta_depth
)
819 enum object_type kind
;
821 offset
= unpack_object_header(offset
, &kind
, &size
);
824 return unpack_delta_entry(offset
, size
, sizep
, delta_depth
);
831 return unpack_non_delta_entry(offset
, size
);
833 die("I created an object I can't read!");
837 static const char *get_mode(const char *str
, unsigned int *modep
)
840 unsigned int mode
= 0;
842 while ((c
= *str
++) != ' ') {
843 if (c
< '0' || c
> '7')
845 mode
= (mode
<< 3) + (c
- '0');
851 static void load_tree(struct tree_entry
*root
)
853 unsigned char* sha1
= root
->versions
[1].sha1
;
854 struct object_entry
*myoe
;
855 struct tree_content
*t
;
860 root
->tree
= t
= new_tree_content(8);
861 if (is_null_sha1(sha1
))
864 myoe
= find_object(sha1
);
866 if (myoe
->type
!= OBJ_TREE
)
867 die("Not a tree: %s", sha1_to_hex(sha1
));
868 buf
= unpack_entry(myoe
->offset
, &size
, &t
->delta_depth
);
871 buf
= read_sha1_file(sha1
, type
, &size
);
872 if (!buf
|| strcmp(type
, tree_type
))
873 die("Can't load tree %s", sha1_to_hex(sha1
));
877 while (c
!= (buf
+ size
)) {
878 struct tree_entry
*e
= new_tree_entry();
880 if (t
->entry_count
== t
->entry_capacity
)
881 root
->tree
= t
= grow_tree_content(t
, 8);
882 t
->entries
[t
->entry_count
++] = e
;
885 c
= get_mode(c
, &e
->versions
[1].mode
);
887 die("Corrupt mode in %s", sha1_to_hex(sha1
));
888 e
->versions
[0].mode
= e
->versions
[1].mode
;
889 e
->name
= to_atom(c
, strlen(c
));
890 c
+= e
->name
->str_len
+ 1;
891 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
892 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
898 static int tecmp0 (const void *_a
, const void *_b
)
900 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
901 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
902 return base_name_compare(
903 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
904 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
907 static int tecmp1 (const void *_a
, const void *_b
)
909 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
910 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
911 return base_name_compare(
912 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
913 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
916 static void mktree(struct tree_content
*t
,
926 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
928 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
930 for (i
= 0; i
< t
->entry_count
; i
++) {
931 if (t
->entries
[i
]->versions
[v
].mode
)
932 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
935 size_dbuf(b
, maxlen
);
937 for (i
= 0; i
< t
->entry_count
; i
++) {
938 struct tree_entry
*e
= t
->entries
[i
];
939 if (!e
->versions
[v
].mode
)
941 c
+= sprintf(c
, "%o", e
->versions
[v
].mode
);
943 strcpy(c
, e
->name
->str_dat
);
944 c
+= e
->name
->str_len
+ 1;
945 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
948 *szp
= c
- (char*)b
->buffer
;
951 static void store_tree(struct tree_entry
*root
)
953 struct tree_content
*t
= root
->tree
;
954 unsigned int i
, j
, del
;
955 unsigned long new_len
;
956 struct last_object lo
;
958 if (!is_null_sha1(root
->versions
[1].sha1
))
961 for (i
= 0; i
< t
->entry_count
; i
++) {
962 if (t
->entries
[i
]->tree
)
963 store_tree(t
->entries
[i
]);
966 if (!S_ISDIR(root
->versions
[0].mode
)
967 || is_null_sha1(root
->versions
[0].sha1
)
968 || !find_object(root
->versions
[0].sha1
)) {
972 mktree(t
, 0, &lo
.len
, &old_tree
);
973 lo
.data
= old_tree
.buffer
;
974 lo
.depth
= t
->delta_depth
;
976 hashcpy(lo
.sha1
, root
->versions
[0].sha1
);
979 mktree(t
, 1, &new_len
, &new_tree
);
980 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
981 &lo
, root
->versions
[1].sha1
, 0);
983 t
->delta_depth
= lo
.depth
;
984 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
985 struct tree_entry
*e
= t
->entries
[i
];
986 if (e
->versions
[1].mode
) {
987 e
->versions
[0].mode
= e
->versions
[1].mode
;
988 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
991 release_tree_entry(e
);
995 t
->entry_count
-= del
;
998 static int tree_content_set(
999 struct tree_entry
*root
,
1001 const unsigned char *sha1
,
1002 const unsigned int mode
)
1004 struct tree_content
*t
= root
->tree
;
1007 struct tree_entry
*e
;
1009 slash1
= strchr(p
, '/');
1015 for (i
= 0; i
< t
->entry_count
; i
++) {
1017 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1019 if (e
->versions
[1].mode
== mode
1020 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1022 e
->versions
[1].mode
= mode
;
1023 hashcpy(e
->versions
[1].sha1
, sha1
);
1025 release_tree_content_recursive(e
->tree
);
1028 hashclr(root
->versions
[1].sha1
);
1031 if (!S_ISDIR(e
->versions
[1].mode
)) {
1032 e
->tree
= new_tree_content(8);
1033 e
->versions
[1].mode
= S_IFDIR
;
1037 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1038 hashclr(root
->versions
[1].sha1
);
1045 if (t
->entry_count
== t
->entry_capacity
)
1046 root
->tree
= t
= grow_tree_content(t
, 8);
1047 e
= new_tree_entry();
1048 e
->name
= to_atom(p
, n
);
1049 e
->versions
[0].mode
= 0;
1050 hashclr(e
->versions
[0].sha1
);
1051 t
->entries
[t
->entry_count
++] = e
;
1053 e
->tree
= new_tree_content(8);
1054 e
->versions
[1].mode
= S_IFDIR
;
1055 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1058 e
->versions
[1].mode
= mode
;
1059 hashcpy(e
->versions
[1].sha1
, sha1
);
1061 hashclr(root
->versions
[1].sha1
);
1065 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1067 struct tree_content
*t
= root
->tree
;
1070 struct tree_entry
*e
;
1072 slash1
= strchr(p
, '/');
1078 for (i
= 0; i
< t
->entry_count
; i
++) {
1080 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1081 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1085 if (tree_content_remove(e
, slash1
+ 1)) {
1086 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1087 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1088 hashclr(root
->versions
[1].sha1
);
1101 release_tree_content_recursive(e
->tree
);
1104 e
->versions
[1].mode
= 0;
1105 hashclr(e
->versions
[1].sha1
);
1106 hashclr(root
->versions
[1].sha1
);
1110 static void init_pack_header()
1112 struct pack_header hdr
;
1114 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
1115 hdr
.hdr_version
= htonl(2);
1116 hdr
.hdr_entries
= 0;
1118 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
1119 pack_size
= sizeof(hdr
);
1122 static void fixup_header_footer()
1130 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
1131 die("Failed seeking to start: %s", strerror(errno
));
1134 yread(pack_fd
, hdr
, 8);
1135 SHA1_Update(&c
, hdr
, 8);
1137 cnt
= htonl(object_count
);
1138 SHA1_Update(&c
, &cnt
, 4);
1139 write_or_die(pack_fd
, &cnt
, 4);
1141 buf
= xmalloc(128 * 1024);
1143 n
= xread(pack_fd
, buf
, 128 * 1024);
1146 SHA1_Update(&c
, buf
, n
);
1150 SHA1_Final(pack_sha1
, &c
);
1151 write_or_die(pack_fd
, pack_sha1
, sizeof(pack_sha1
));
1154 static int oecmp (const void *_a
, const void *_b
)
1156 struct object_entry
*a
= *((struct object_entry
**)_a
);
1157 struct object_entry
*b
= *((struct object_entry
**)_b
);
1158 return hashcmp(a
->sha1
, b
->sha1
);
1161 static void write_index(const char *idx_name
)
1164 struct object_entry
**idx
, **c
, **last
;
1165 struct object_entry
*e
;
1166 struct object_entry_pool
*o
;
1167 unsigned int array
[256];
1170 /* Build the sorted table of object IDs. */
1171 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
1173 for (o
= blocks
; o
; o
= o
->next_pool
)
1174 for (e
= o
->entries
; e
!= o
->next_free
; e
++)
1176 last
= idx
+ object_count
;
1177 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
1179 /* Generate the fan-out array. */
1181 for (i
= 0; i
< 256; i
++) {
1182 struct object_entry
**next
= c
;;
1183 while (next
< last
) {
1184 if ((*next
)->sha1
[0] != i
)
1188 array
[i
] = htonl(next
- idx
);
1192 f
= sha1create("%s", idx_name
);
1193 sha1write(f
, array
, 256 * sizeof(int));
1194 for (c
= idx
; c
!= last
; c
++) {
1195 unsigned int offset
= htonl((*c
)->offset
);
1196 sha1write(f
, &offset
, 4);
1197 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
1199 sha1write(f
, pack_sha1
, sizeof(pack_sha1
));
1200 sha1close(f
, NULL
, 1);
1204 static void dump_branches()
1206 static const char *msg
= "fast-import";
1209 struct ref_lock
*lock
;
1211 for (i
= 0; i
< branch_table_sz
; i
++) {
1212 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
1213 lock
= lock_any_ref_for_update(b
->name
, NULL
, 0);
1214 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1215 die("Can't write %s", b
->name
);
1220 static void dump_tags()
1222 static const char *msg
= "fast-import";
1224 struct ref_lock
*lock
;
1225 char path
[PATH_MAX
];
1227 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1228 sprintf(path
, "refs/tags/%s", t
->name
);
1229 lock
= lock_any_ref_for_update(path
, NULL
, 0);
1230 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1231 die("Can't write %s", path
);
1235 static void dump_marks_helper(FILE *f
,
1241 for (k
= 0; k
< 1024; k
++) {
1242 if (m
->data
.sets
[k
])
1243 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1247 for (k
= 0; k
< 1024; k
++) {
1248 if (m
->data
.marked
[k
])
1249 fprintf(f
, ":%lu %s\n", base
+ k
,
1250 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1255 static void dump_marks()
1259 FILE *f
= fopen(mark_file
, "w");
1260 dump_marks_helper(f
, 0, marks
);
1265 static void read_next_command()
1267 read_line(&command_buf
, stdin
, '\n');
1270 static void cmd_mark()
1272 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1273 next_mark
= strtoul(command_buf
.buf
+ 6, NULL
, 10);
1274 read_next_command();
1280 static void* cmd_data (size_t *size
)
1286 if (strncmp("data ", command_buf
.buf
, 5))
1287 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1289 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1290 buffer
= xmalloc(length
);
1292 while (n
< length
) {
1293 size_t s
= fread((char*)buffer
+ n
, 1, length
- n
, stdin
);
1294 if (!s
&& feof(stdin
))
1295 die("EOF in data (%lu bytes remaining)", length
- n
);
1299 if (fgetc(stdin
) != '\n')
1300 die("An lf did not trail the binary data as expected.");
1306 static void cmd_new_blob()
1311 read_next_command();
1315 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1319 static void unload_one_branch()
1321 while (cur_active_branches
1322 && cur_active_branches
>= max_active_branches
) {
1323 unsigned long min_commit
= ULONG_MAX
;
1324 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1326 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1327 if (e
->last_commit
< min_commit
) {
1329 min_commit
= e
->last_commit
;
1335 e
= p
->active_next_branch
;
1336 p
->active_next_branch
= e
->active_next_branch
;
1338 e
= active_branches
;
1339 active_branches
= e
->active_next_branch
;
1341 e
->active_next_branch
= NULL
;
1342 if (e
->branch_tree
.tree
) {
1343 release_tree_content_recursive(e
->branch_tree
.tree
);
1344 e
->branch_tree
.tree
= NULL
;
1346 cur_active_branches
--;
1350 static void load_branch(struct branch
*b
)
1352 load_tree(&b
->branch_tree
);
1353 b
->active_next_branch
= active_branches
;
1354 active_branches
= b
;
1355 cur_active_branches
++;
1356 branch_load_count
++;
1359 static void file_change_m(struct branch
*b
)
1361 const char *p
= command_buf
.buf
+ 2;
1364 struct object_entry
*oe
;
1365 unsigned char sha1
[20];
1369 p
= get_mode(p
, &mode
);
1371 die("Corrupt mode: %s", command_buf
.buf
);
1373 case S_IFREG
| 0644:
1374 case S_IFREG
| 0755:
1381 die("Corrupt mode: %s", command_buf
.buf
);
1386 oe
= find_mark(strtoul(p
+ 1, &x
, 10));
1387 hashcpy(sha1
, oe
->sha1
);
1390 if (get_sha1_hex(p
, sha1
))
1391 die("Invalid SHA1: %s", command_buf
.buf
);
1392 oe
= find_object(sha1
);
1396 die("Missing space after SHA1: %s", command_buf
.buf
);
1398 p_uq
= unquote_c_style(p
, &endp
);
1401 die("Garbage after path in: %s", command_buf
.buf
);
1406 if (oe
->type
!= OBJ_BLOB
)
1407 die("Not a blob (actually a %s): %s",
1408 command_buf
.buf
, type_names
[oe
->type
]);
1410 if (sha1_object_info(sha1
, type
, NULL
))
1411 die("Blob not found: %s", command_buf
.buf
);
1412 if (strcmp(blob_type
, type
))
1413 die("Not a blob (actually a %s): %s",
1414 command_buf
.buf
, type
);
1417 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1423 static void file_change_d(struct branch
*b
)
1425 const char *p
= command_buf
.buf
+ 2;
1429 p_uq
= unquote_c_style(p
, &endp
);
1432 die("Garbage after path in: %s", command_buf
.buf
);
1435 tree_content_remove(&b
->branch_tree
, p
);
1440 static void cmd_from(struct branch
*b
)
1442 const char *from
, *endp
;
1446 if (strncmp("from ", command_buf
.buf
, 5))
1450 die("Can't reinitailize branch %s", b
->name
);
1452 from
= strchr(command_buf
.buf
, ' ') + 1;
1453 str_uq
= unquote_c_style(from
, &endp
);
1456 die("Garbage after string in: %s", command_buf
.buf
);
1460 s
= lookup_branch(from
);
1462 die("Can't create a branch from itself: %s", b
->name
);
1464 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1465 hashcpy(b
->sha1
, s
->sha1
);
1466 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1467 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1468 } else if (*from
== ':') {
1469 unsigned long idnum
= strtoul(from
+ 1, NULL
, 10);
1470 struct object_entry
*oe
= find_mark(idnum
);
1474 if (oe
->type
!= OBJ_COMMIT
)
1475 die("Mark :%lu not a commit", idnum
);
1476 hashcpy(b
->sha1
, oe
->sha1
);
1477 buf
= unpack_entry(oe
->offset
, &size
, &depth
);
1478 if (!buf
|| size
< 46)
1479 die("Not a valid commit: %s", from
);
1480 if (memcmp("tree ", buf
, 5)
1481 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1482 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1484 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1485 b
->branch_tree
.versions
[1].sha1
);
1486 } else if (!get_sha1(from
, b
->sha1
)) {
1487 if (is_null_sha1(b
->sha1
)) {
1488 hashclr(b
->branch_tree
.versions
[0].sha1
);
1489 hashclr(b
->branch_tree
.versions
[1].sha1
);
1494 buf
= read_object_with_reference(b
->sha1
,
1495 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1496 if (!buf
|| size
< 46)
1497 die("Not a valid commit: %s", from
);
1498 if (memcmp("tree ", buf
, 5)
1499 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1500 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1502 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1503 b
->branch_tree
.versions
[1].sha1
);
1506 die("Invalid ref name or SHA1 expression: %s", from
);
1508 read_next_command();
1511 static struct hash_list
* cmd_merge(unsigned int *count
)
1513 struct hash_list
*list
= NULL
, *n
, *e
;
1514 const char *from
, *endp
;
1519 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1520 from
= strchr(command_buf
.buf
, ' ') + 1;
1521 str_uq
= unquote_c_style(from
, &endp
);
1524 die("Garbage after string in: %s", command_buf
.buf
);
1528 n
= xmalloc(sizeof(*n
));
1529 s
= lookup_branch(from
);
1531 hashcpy(n
->sha1
, s
->sha1
);
1532 else if (*from
== ':') {
1533 unsigned long idnum
= strtoul(from
+ 1, NULL
, 10);
1534 struct object_entry
*oe
= find_mark(idnum
);
1535 if (oe
->type
!= OBJ_COMMIT
)
1536 die("Mark :%lu not a commit", idnum
);
1537 hashcpy(n
->sha1
, oe
->sha1
);
1538 } else if (get_sha1(from
, n
->sha1
))
1539 die("Invalid ref name or SHA1 expression: %s", from
);
1548 read_next_command();
1553 static void cmd_new_commit()
1561 char *author
= NULL
;
1562 char *committer
= NULL
;
1563 struct hash_list
*merge_list
= NULL
;
1564 unsigned int merge_count
;
1566 /* Obtain the branch name from the rest of our command */
1567 sp
= strchr(command_buf
.buf
, ' ') + 1;
1568 str_uq
= unquote_c_style(sp
, &endp
);
1571 die("Garbage after ref in: %s", command_buf
.buf
);
1574 b
= lookup_branch(sp
);
1580 read_next_command();
1582 if (!strncmp("author ", command_buf
.buf
, 7)) {
1583 author
= strdup(command_buf
.buf
);
1584 read_next_command();
1586 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1587 committer
= strdup(command_buf
.buf
);
1588 read_next_command();
1591 die("Expected committer but didn't get one");
1592 msg
= cmd_data(&msglen
);
1593 read_next_command();
1595 merge_list
= cmd_merge(&merge_count
);
1597 /* ensure the branch is active/loaded */
1598 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1599 unload_one_branch();
1605 if (1 == command_buf
.len
)
1607 else if (!strncmp("M ", command_buf
.buf
, 2))
1609 else if (!strncmp("D ", command_buf
.buf
, 2))
1612 die("Unsupported file_change: %s", command_buf
.buf
);
1613 read_next_command();
1616 /* build the tree and the commit */
1617 store_tree(&b
->branch_tree
);
1618 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1619 b
->branch_tree
.versions
[1].sha1
);
1620 size_dbuf(&new_data
, 97 + msglen
1623 ? strlen(author
) + strlen(committer
)
1624 : 2 * strlen(committer
)));
1625 sp
= new_data
.buffer
;
1626 sp
+= sprintf(sp
, "tree %s\n",
1627 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1628 if (!is_null_sha1(b
->sha1
))
1629 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1630 while (merge_list
) {
1631 struct hash_list
*next
= merge_list
->next
;
1632 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1637 sp
+= sprintf(sp
, "%s\n", author
);
1639 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1640 sp
+= sprintf(sp
, "%s\n\n", committer
);
1641 memcpy(sp
, msg
, msglen
);
1648 store_object(OBJ_COMMIT
,
1649 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1650 NULL
, b
->sha1
, next_mark
);
1651 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1654 int need_dq
= quote_c_style(b
->name
, NULL
, NULL
, 0);
1655 fprintf(branch_log
, "commit ");
1657 fputc('"', branch_log
);
1658 quote_c_style(b
->name
, NULL
, branch_log
, 0);
1659 fputc('"', branch_log
);
1661 fprintf(branch_log
, "%s", b
->name
);
1662 fprintf(branch_log
," :%lu %s\n",next_mark
,sha1_to_hex(b
->sha1
));
1666 static void cmd_new_tag()
1677 unsigned long from_mark
= 0;
1678 unsigned char sha1
[20];
1680 /* Obtain the new tag name from the rest of our command */
1681 sp
= strchr(command_buf
.buf
, ' ') + 1;
1682 str_uq
= unquote_c_style(sp
, &endp
);
1685 die("Garbage after tag name in: %s", command_buf
.buf
);
1688 t
= pool_alloc(sizeof(struct tag
));
1690 t
->name
= pool_strdup(sp
);
1692 last_tag
->next_tag
= t
;
1698 read_next_command();
1701 if (strncmp("from ", command_buf
.buf
, 5))
1702 die("Expected from command, got %s", command_buf
.buf
);
1704 from
= strchr(command_buf
.buf
, ' ') + 1;
1705 str_uq
= unquote_c_style(from
, &endp
);
1708 die("Garbage after string in: %s", command_buf
.buf
);
1712 s
= lookup_branch(from
);
1714 hashcpy(sha1
, s
->sha1
);
1715 } else if (*from
== ':') {
1716 from_mark
= strtoul(from
+ 1, NULL
, 10);
1717 struct object_entry
*oe
= find_mark(from_mark
);
1718 if (oe
->type
!= OBJ_COMMIT
)
1719 die("Mark :%lu not a commit", from_mark
);
1720 hashcpy(sha1
, oe
->sha1
);
1721 } else if (!get_sha1(from
, sha1
)) {
1725 buf
= read_object_with_reference(sha1
,
1726 type_names
[OBJ_COMMIT
], &size
, sha1
);
1727 if (!buf
|| size
< 46)
1728 die("Not a valid commit: %s", from
);
1731 die("Invalid ref name or SHA1 expression: %s", from
);
1735 read_next_command();
1738 if (strncmp("tagger ", command_buf
.buf
, 7))
1739 die("Expected tagger command, got %s", command_buf
.buf
);
1740 tagger
= strdup(command_buf
.buf
);
1742 /* tag payload/message */
1743 read_next_command();
1744 msg
= cmd_data(&msglen
);
1746 /* build the tag object */
1747 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1748 sp
= new_data
.buffer
;
1749 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1750 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1751 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1752 sp
+= sprintf(sp
, "%s\n\n", tagger
);
1753 memcpy(sp
, msg
, msglen
);
1758 store_object(OBJ_TAG
, new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1762 int need_dq
= quote_c_style(t
->name
, NULL
, NULL
, 0);
1763 fprintf(branch_log
, "tag ");
1765 fputc('"', branch_log
);
1766 quote_c_style(t
->name
, NULL
, branch_log
, 0);
1767 fputc('"', branch_log
);
1769 fprintf(branch_log
, "%s", t
->name
);
1770 fprintf(branch_log
," :%lu %s\n",from_mark
,sha1_to_hex(t
->sha1
));
1774 static void cmd_reset_branch()
1781 /* Obtain the branch name from the rest of our command */
1782 sp
= strchr(command_buf
.buf
, ' ') + 1;
1783 str_uq
= unquote_c_style(sp
, &endp
);
1786 die("Garbage after ref in: %s", command_buf
.buf
);
1789 b
= lookup_branch(sp
);
1792 if (b
->branch_tree
.tree
) {
1793 release_tree_content_recursive(b
->branch_tree
.tree
);
1794 b
->branch_tree
.tree
= NULL
;
1801 static const char fast_import_usage
[] =
1802 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1804 int main(int argc
, const char **argv
)
1806 const char *base_name
;
1808 unsigned long est_obj_cnt
= object_entry_alloc
;
1814 git_config(git_default_config
);
1815 page_size
= getpagesize();
1817 for (i
= 1; i
< argc
; i
++) {
1818 const char *a
= argv
[i
];
1820 if (*a
!= '-' || !strcmp(a
, "--"))
1822 else if (!strncmp(a
, "--objects=", 10))
1823 est_obj_cnt
= strtoul(a
+ 10, NULL
, 0);
1824 else if (!strncmp(a
, "--depth=", 8))
1825 max_depth
= strtoul(a
+ 8, NULL
, 0);
1826 else if (!strncmp(a
, "--active-branches=", 18))
1827 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1828 else if (!strncmp(a
, "--export-marks=", 15))
1830 else if (!strncmp(a
, "--branch-log=", 13)) {
1831 branch_log
= fopen(a
+ 13, "w");
1833 die("Can't create %s: %s", a
+ 13, strerror(errno
));
1836 die("unknown option %s", a
);
1839 usage(fast_import_usage
);
1840 base_name
= argv
[i
];
1842 pack_name
= xmalloc(strlen(base_name
) + 6);
1843 sprintf(pack_name
, "%s.pack", base_name
);
1844 idx_name
= xmalloc(strlen(base_name
) + 5);
1845 sprintf(idx_name
, "%s.idx", base_name
);
1847 pack_fd
= open(pack_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1849 die("Can't create %s: %s", pack_name
, strerror(errno
));
1852 alloc_objects(est_obj_cnt
);
1853 strbuf_init(&command_buf
);
1855 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1856 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1857 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1858 marks
= pool_calloc(1, sizeof(struct mark_set
));
1861 read_next_command();
1862 if (command_buf
.eof
)
1864 else if (!strcmp("blob", command_buf
.buf
))
1866 else if (!strncmp("commit ", command_buf
.buf
, 7))
1868 else if (!strncmp("tag ", command_buf
.buf
, 4))
1870 else if (!strncmp("reset ", command_buf
.buf
, 6))
1873 die("Unsupported command: %s", command_buf
.buf
);
1876 fixup_header_footer();
1878 write_index(idx_name
);
1885 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1886 fprintf(stderr
, "---------------------------------------------------------------------\n");
1887 fprintf(stderr
, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count
, alloc_count
- est_obj_cnt
);
1888 fprintf(stderr
, "Total objects: %10lu (%10lu duplicates )\n", object_count
, duplicate_count
);
1889 fprintf(stderr
, " blobs : %10lu (%10lu duplicates %10lu deltas)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
], delta_count_by_type
[OBJ_BLOB
]);
1890 fprintf(stderr
, " trees : %10lu (%10lu duplicates %10lu deltas)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
], delta_count_by_type
[OBJ_TREE
]);
1891 fprintf(stderr
, " commits: %10lu (%10lu duplicates %10lu deltas)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
], delta_count_by_type
[OBJ_COMMIT
]);
1892 fprintf(stderr
, " tags : %10lu (%10lu duplicates %10lu deltas)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
], delta_count_by_type
[OBJ_TAG
]);
1893 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
1894 fprintf(stderr
, " marks: %10u (%10lu unique )\n", (1 << marks
->shift
) * 1024, marks_set_count
);
1895 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
1896 fprintf(stderr
, "Memory total: %10lu KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
1897 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
1898 fprintf(stderr
, " objects: %10lu KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
1899 fprintf(stderr
, "Pack remaps: %10lu\n", remap_count
);
1900 stat(pack_name
, &sb
);
1901 fprintf(stderr
, "Pack size: %10lu KiB\n", (unsigned long)(sb
.st_size
/1024));
1902 stat(idx_name
, &sb
);
1903 fprintf(stderr
, "Index size: %10lu KiB\n", (unsigned long)(sb
.st_size
/1024));
1904 fprintf(stderr
, "---------------------------------------------------------------------\n");
1906 fprintf(stderr
, "\n");