]>
git.ipfire.org Git - thirdparty/git.git/blob - fast-import.c
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
40 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
43 checkpoint ::= 'checkpoint' lf
46 # note: the first idnum in a stream should be 1 and subsequent
47 # idnums should not have gaps between values as this will cause
48 # the stream parser to reserve space for the gapped values. An
49 # idnum can be updated in the future to a new object by issuing
50 # a new mark directive with the old idnum.
52 mark ::= 'mark' sp idnum lf;
54 # note: declen indicates the length of binary_data in bytes.
55 # declen does not include the lf preceeding or trailing the
58 data ::= 'data' sp declen lf
62 # note: quoted strings are C-style quoting supporting \c for
63 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
64 # is the signed byte value in octal. Note that the only
65 # characters which must actually be escaped to protect the
66 # stream formatting is: \, " and LF. Otherwise these values
69 ref_str ::= ref | '"' quoted(ref) '"' ;
70 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
71 tag_str ::= tag | '"' quoted(tag) '"' ;
72 path_str ::= path | '"' quoted(path) '"' ;
74 declen ::= # unsigned 32 bit value, ascii base10 notation;
75 binary_data ::= # file content, not interpreted;
77 sp ::= # ASCII space character;
78 lf ::= # ASCII newline (LF) character;
80 # note: a colon (':') must precede the numerical value assigned to
81 # an idnum. This is to distinguish it from a ref or tag name as
82 # GIT does not permit ':' in ref or tag strings.
85 path ::= # GIT style file path, e.g. "a/b/c";
86 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
87 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
88 sha1exp ::= # Any valid GIT SHA1 expression;
89 hexsha1 ::= # SHA1 in hexadecimal format;
91 # note: name and email are UTF8 strings, however name must not
92 # contain '<' or lf and email must not contain any of the
93 # following: '<', '>', lf.
95 name ::= # valid GIT author/committer name;
96 email ::= # valid GIT author/committer email;
97 ts ::= # time since the epoch in seconds, ascii base10 notation;
98 tz ::= # GIT style timezone;
109 #include "csum-file.h"
115 struct object_entry
*next
;
116 unsigned long offset
;
117 unsigned type
: TYPE_BITS
;
118 unsigned pack_id
: 16;
119 unsigned char sha1
[20];
122 struct object_entry_pool
124 struct object_entry_pool
*next_pool
;
125 struct object_entry
*next_free
;
126 struct object_entry
*end
;
127 struct object_entry entries
[FLEX_ARRAY
]; /* more */
134 struct object_entry
*marked
[1024];
135 struct mark_set
*sets
[1024];
143 unsigned long offset
;
150 struct mem_pool
*next_pool
;
153 char space
[FLEX_ARRAY
]; /* more */
158 struct atom_str
*next_atom
;
160 char str_dat
[FLEX_ARRAY
]; /* more */
166 struct tree_content
*tree
;
167 struct atom_str
* name
;
171 unsigned char sha1
[20];
177 unsigned int entry_capacity
; /* must match avail_tree_content */
178 unsigned int entry_count
;
179 unsigned int delta_depth
;
180 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
183 struct avail_tree_content
185 unsigned int entry_capacity
; /* must match tree_content */
186 struct avail_tree_content
*next_avail
;
191 struct branch
*table_next_branch
;
192 struct branch
*active_next_branch
;
194 unsigned long last_commit
;
195 struct tree_entry branch_tree
;
196 unsigned char sha1
[20];
201 struct tag
*next_tag
;
203 unsigned char sha1
[20];
214 struct hash_list
*next
;
215 unsigned char sha1
[20];
218 /* Configured limits on output */
219 static unsigned long max_depth
= 10;
220 static unsigned long max_packsize
= (1LL << 32) - 1;
221 static uintmax_t max_objects
= -1;
223 /* Stats and misc. counters */
224 static uintmax_t alloc_count
;
225 static uintmax_t marks_set_count
;
226 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
227 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
228 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
229 static unsigned long object_count
;
230 static unsigned long branch_count
;
231 static unsigned long branch_load_count
;
234 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
235 static size_t total_allocd
;
236 static struct mem_pool
*mem_pool
;
238 /* Atom management */
239 static unsigned int atom_table_sz
= 4451;
240 static unsigned int atom_cnt
;
241 static struct atom_str
**atom_table
;
243 /* The .pack file being generated */
244 static unsigned int pack_id
;
245 static struct packed_git
*pack_data
;
246 static struct packed_git
**all_packs
;
247 static unsigned long pack_size
;
249 /* Table of objects we've written. */
250 static unsigned int object_entry_alloc
= 5000;
251 static struct object_entry_pool
*blocks
;
252 static struct object_entry
*object_table
[1 << 16];
253 static struct mark_set
*marks
;
254 static const char* mark_file
;
257 static struct last_object last_blob
;
259 /* Tree management */
260 static unsigned int tree_entry_alloc
= 1000;
261 static void *avail_tree_entry
;
262 static unsigned int avail_tree_table_sz
= 100;
263 static struct avail_tree_content
**avail_tree_table
;
264 static struct dbuf old_tree
;
265 static struct dbuf new_tree
;
268 static unsigned long max_active_branches
= 5;
269 static unsigned long cur_active_branches
;
270 static unsigned long branch_table_sz
= 1039;
271 static struct branch
**branch_table
;
272 static struct branch
*active_branches
;
275 static struct tag
*first_tag
;
276 static struct tag
*last_tag
;
278 /* Input stream parsing */
279 static struct strbuf command_buf
;
280 static uintmax_t next_mark
;
281 static struct dbuf new_data
;
282 static FILE* branch_log
;
285 static void alloc_objects(unsigned int cnt
)
287 struct object_entry_pool
*b
;
289 b
= xmalloc(sizeof(struct object_entry_pool
)
290 + cnt
* sizeof(struct object_entry
));
291 b
->next_pool
= blocks
;
292 b
->next_free
= b
->entries
;
293 b
->end
= b
->entries
+ cnt
;
298 static struct object_entry
* new_object(unsigned char *sha1
)
300 struct object_entry
*e
;
302 if (blocks
->next_free
== blocks
->end
)
303 alloc_objects(object_entry_alloc
);
305 e
= blocks
->next_free
++;
306 hashcpy(e
->sha1
, sha1
);
310 static struct object_entry
* find_object(unsigned char *sha1
)
312 unsigned int h
= sha1
[0] << 8 | sha1
[1];
313 struct object_entry
*e
;
314 for (e
= object_table
[h
]; e
; e
= e
->next
)
315 if (!hashcmp(sha1
, e
->sha1
))
320 static struct object_entry
* insert_object(unsigned char *sha1
)
322 unsigned int h
= sha1
[0] << 8 | sha1
[1];
323 struct object_entry
*e
= object_table
[h
];
324 struct object_entry
*p
= NULL
;
327 if (!hashcmp(sha1
, e
->sha1
))
333 e
= new_object(sha1
);
343 static unsigned int hc_str(const char *s
, size_t len
)
351 static void* pool_alloc(size_t len
)
356 for (p
= mem_pool
; p
; p
= p
->next_pool
)
357 if ((p
->end
- p
->next_free
>= len
))
361 if (len
>= (mem_pool_alloc
/2)) {
365 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
366 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
367 p
->next_pool
= mem_pool
;
368 p
->next_free
= p
->space
;
369 p
->end
= p
->next_free
+ mem_pool_alloc
;
374 /* round out to a pointer alignment */
375 if (len
& (sizeof(void*) - 1))
376 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
381 static void* pool_calloc(size_t count
, size_t size
)
383 size_t len
= count
* size
;
384 void *r
= pool_alloc(len
);
389 static char* pool_strdup(const char *s
)
391 char *r
= pool_alloc(strlen(s
) + 1);
396 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
399 if (b
->capacity
>= maxlen
)
403 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
404 b
->buffer
= xmalloc(b
->capacity
);
407 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
409 struct mark_set
*s
= marks
;
410 while ((idnum
>> s
->shift
) >= 1024) {
411 s
= pool_calloc(1, sizeof(struct mark_set
));
412 s
->shift
= marks
->shift
+ 10;
413 s
->data
.sets
[0] = marks
;
417 uintmax_t i
= idnum
>> s
->shift
;
418 idnum
-= i
<< s
->shift
;
419 if (!s
->data
.sets
[i
]) {
420 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
421 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
425 if (!s
->data
.marked
[idnum
])
427 s
->data
.marked
[idnum
] = oe
;
430 static struct object_entry
* find_mark(uintmax_t idnum
)
432 uintmax_t orig_idnum
= idnum
;
433 struct mark_set
*s
= marks
;
434 struct object_entry
*oe
= NULL
;
435 if ((idnum
>> s
->shift
) < 1024) {
436 while (s
&& s
->shift
) {
437 uintmax_t i
= idnum
>> s
->shift
;
438 idnum
-= i
<< s
->shift
;
442 oe
= s
->data
.marked
[idnum
];
445 die("mark :%ju not declared", orig_idnum
);
449 static struct atom_str
* to_atom(const char *s
, size_t len
)
451 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
454 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
455 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
458 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
460 strncpy(c
->str_dat
, s
, len
);
462 c
->next_atom
= atom_table
[hc
];
468 static struct branch
* lookup_branch(const char *name
)
470 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
473 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
474 if (!strcmp(name
, b
->name
))
479 static struct branch
* new_branch(const char *name
)
481 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
482 struct branch
* b
= lookup_branch(name
);
485 die("Invalid attempt to create duplicate branch: %s", name
);
486 if (check_ref_format(name
))
487 die("Branch name doesn't conform to GIT standards: %s", name
);
489 b
= pool_calloc(1, sizeof(struct branch
));
490 b
->name
= pool_strdup(name
);
491 b
->table_next_branch
= branch_table
[hc
];
492 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
493 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
494 branch_table
[hc
] = b
;
499 static unsigned int hc_entries(unsigned int cnt
)
501 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
502 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
505 static struct tree_content
* new_tree_content(unsigned int cnt
)
507 struct avail_tree_content
*f
, *l
= NULL
;
508 struct tree_content
*t
;
509 unsigned int hc
= hc_entries(cnt
);
511 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
512 if (f
->entry_capacity
>= cnt
)
517 l
->next_avail
= f
->next_avail
;
519 avail_tree_table
[hc
] = f
->next_avail
;
521 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
522 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
523 f
->entry_capacity
= cnt
;
526 t
= (struct tree_content
*)f
;
532 static void release_tree_entry(struct tree_entry
*e
);
533 static void release_tree_content(struct tree_content
*t
)
535 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
536 unsigned int hc
= hc_entries(f
->entry_capacity
);
537 f
->next_avail
= avail_tree_table
[hc
];
538 avail_tree_table
[hc
] = f
;
541 static void release_tree_content_recursive(struct tree_content
*t
)
544 for (i
= 0; i
< t
->entry_count
; i
++)
545 release_tree_entry(t
->entries
[i
]);
546 release_tree_content(t
);
549 static struct tree_content
* grow_tree_content(
550 struct tree_content
*t
,
553 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
554 r
->entry_count
= t
->entry_count
;
555 r
->delta_depth
= t
->delta_depth
;
556 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
557 release_tree_content(t
);
561 static struct tree_entry
* new_tree_entry()
563 struct tree_entry
*e
;
565 if (!avail_tree_entry
) {
566 unsigned int n
= tree_entry_alloc
;
567 total_allocd
+= n
* sizeof(struct tree_entry
);
568 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
570 *((void**)e
) = e
+ 1;
576 e
= avail_tree_entry
;
577 avail_tree_entry
= *((void**)e
);
581 static void release_tree_entry(struct tree_entry
*e
)
584 release_tree_content_recursive(e
->tree
);
585 *((void**)e
) = avail_tree_entry
;
586 avail_tree_entry
= e
;
589 static void start_packfile()
591 static char tmpfile
[PATH_MAX
];
592 struct packed_git
*p
;
593 struct pack_header hdr
;
596 snprintf(tmpfile
, sizeof(tmpfile
),
597 "%s/pack_XXXXXX", get_object_directory());
598 pack_fd
= mkstemp(tmpfile
);
600 die("Can't create %s: %s", tmpfile
, strerror(errno
));
601 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
602 strcpy(p
->pack_name
, tmpfile
);
603 p
->pack_fd
= pack_fd
;
605 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
606 hdr
.hdr_version
= htonl(2);
608 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
611 pack_size
= sizeof(hdr
);
614 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
615 all_packs
[pack_id
] = p
;
618 static void fixup_header_footer()
620 int pack_fd
= pack_data
->pack_fd
;
626 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
627 die("Failed seeking to start: %s", strerror(errno
));
630 if (read_in_full(pack_fd
, hdr
, 8) != 8)
631 die("Unable to reread header of %s", pack_data
->pack_name
);
632 SHA1_Update(&c
, hdr
, 8);
634 cnt
= htonl(object_count
);
635 SHA1_Update(&c
, &cnt
, 4);
636 write_or_die(pack_fd
, &cnt
, 4);
638 buf
= xmalloc(128 * 1024);
640 size_t n
= xread(pack_fd
, buf
, 128 * 1024);
643 SHA1_Update(&c
, buf
, n
);
647 SHA1_Final(pack_data
->sha1
, &c
);
648 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
652 static int oecmp (const void *a_
, const void *b_
)
654 struct object_entry
*a
= *((struct object_entry
**)a_
);
655 struct object_entry
*b
= *((struct object_entry
**)b_
);
656 return hashcmp(a
->sha1
, b
->sha1
);
659 static char* create_index()
661 static char tmpfile
[PATH_MAX
];
664 struct object_entry
**idx
, **c
, **last
, *e
;
665 struct object_entry_pool
*o
;
666 unsigned int array
[256];
669 /* Build the sorted table of object IDs. */
670 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
672 for (o
= blocks
; o
; o
= o
->next_pool
)
673 for (e
= o
->next_free
; e
-- != o
->entries
;)
674 if (pack_id
== e
->pack_id
)
676 last
= idx
+ object_count
;
678 die("internal consistency error creating the index");
679 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
681 /* Generate the fan-out array. */
683 for (i
= 0; i
< 256; i
++) {
684 struct object_entry
**next
= c
;;
685 while (next
< last
) {
686 if ((*next
)->sha1
[0] != i
)
690 array
[i
] = htonl(next
- idx
);
694 snprintf(tmpfile
, sizeof(tmpfile
),
695 "%s/index_XXXXXX", get_object_directory());
696 idx_fd
= mkstemp(tmpfile
);
698 die("Can't create %s: %s", tmpfile
, strerror(errno
));
699 f
= sha1fd(idx_fd
, tmpfile
);
700 sha1write(f
, array
, 256 * sizeof(int));
702 for (c
= idx
; c
!= last
; c
++) {
703 unsigned int offset
= htonl((*c
)->offset
);
704 sha1write(f
, &offset
, 4);
705 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
706 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
708 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
709 sha1close(f
, NULL
, 1);
711 SHA1_Final(pack_data
->sha1
, &ctx
);
715 static char* keep_pack(char *curr_index_name
)
717 static char name
[PATH_MAX
];
718 static char *keep_msg
= "fast-import";
721 chmod(pack_data
->pack_name
, 0444);
722 chmod(curr_index_name
, 0444);
724 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
725 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
726 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
728 die("cannot create keep file");
729 write(keep_fd
, keep_msg
, strlen(keep_msg
));
732 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
733 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
734 if (move_temp_to_file(pack_data
->pack_name
, name
))
735 die("cannot store pack file");
736 printf("%s\n", name
);
738 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
739 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
740 if (move_temp_to_file(curr_index_name
, name
))
741 die("cannot store index file");
745 static void unkeep_all_packs()
747 static char name
[PATH_MAX
];
750 for (k
= 0; k
< pack_id
; k
++) {
751 struct packed_git
*p
= all_packs
[k
];
752 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
753 get_object_directory(), sha1_to_hex(p
->sha1
));
758 static void end_packfile()
760 struct packed_git
*old_p
= pack_data
, *new_p
;
765 fixup_header_footer();
766 idx_name
= keep_pack(create_index());
768 /* Register the packfile with core git's machinary. */
769 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
771 die("core git rejected index %s", idx_name
);
772 new_p
->windows
= old_p
->windows
;
773 all_packs
[pack_id
++] = new_p
;
774 install_packed_git(new_p
);
777 unlink(old_p
->pack_name
);
780 /* We can't carry a delta across packfiles. */
781 free(last_blob
.data
);
782 last_blob
.data
= NULL
;
784 last_blob
.offset
= 0;
788 static void checkpoint()
794 static size_t encode_header(
795 enum object_type type
,
802 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
803 die("bad type %d", type
);
805 c
= (type
<< 4) | (size
& 15);
817 static int store_object(
818 enum object_type type
,
821 struct last_object
*last
,
822 unsigned char *sha1out
,
826 struct object_entry
*e
;
827 unsigned char hdr
[96];
828 unsigned char sha1
[20];
829 unsigned long hdrlen
, deltalen
;
833 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
835 SHA1_Update(&c
, hdr
, hdrlen
);
836 SHA1_Update(&c
, dat
, datlen
);
837 SHA1_Final(sha1
, &c
);
839 hashcpy(sha1out
, sha1
);
841 e
= insert_object(sha1
);
843 insert_mark(mark
, e
);
845 duplicate_count_by_type
[type
]++;
849 if (last
&& last
->data
&& last
->depth
< max_depth
) {
850 delta
= diff_delta(last
->data
, last
->len
,
853 if (delta
&& deltalen
>= datlen
) {
860 memset(&s
, 0, sizeof(s
));
861 deflateInit(&s
, zlib_compression_level
);
864 s
.avail_in
= deltalen
;
869 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
870 s
.next_out
= out
= xmalloc(s
.avail_out
);
871 while (deflate(&s
, Z_FINISH
) == Z_OK
)
875 /* Determine if we should auto-checkpoint. */
876 if ((object_count
+ 1) > max_objects
877 || (object_count
+ 1) < object_count
878 || (pack_size
+ 60 + s
.total_out
) > max_packsize
879 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
881 /* This new object needs to *not* have the current pack_id. */
882 e
->pack_id
= pack_id
+ 1;
885 /* We cannot carry a delta into the new pack. */
890 memset(&s
, 0, sizeof(s
));
891 deflateInit(&s
, zlib_compression_level
);
894 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
895 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
896 while (deflate(&s
, Z_FINISH
) == Z_OK
)
903 e
->pack_id
= pack_id
;
904 e
->offset
= pack_size
;
906 object_count_by_type
[type
]++;
909 unsigned long ofs
= e
->offset
- last
->offset
;
910 unsigned pos
= sizeof(hdr
) - 1;
912 delta_count_by_type
[type
]++;
915 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
916 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
919 hdr
[pos
] = ofs
& 127;
921 hdr
[--pos
] = 128 | (--ofs
& 127);
922 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
923 pack_size
+= sizeof(hdr
) - pos
;
927 hdrlen
= encode_header(type
, datlen
, hdr
);
928 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
932 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
933 pack_size
+= s
.total_out
;
939 if (last
->data
&& !last
->no_free
)
942 last
->offset
= e
->offset
;
948 static void *gfi_unpack_entry(
949 struct object_entry
*oe
,
950 unsigned long *sizep
)
952 static char type
[20];
953 struct packed_git
*p
= all_packs
[oe
->pack_id
];
955 p
->pack_size
= pack_size
+ 20;
956 return unpack_entry(p
, oe
->offset
, type
, sizep
);
959 static const char *get_mode(const char *str
, unsigned int *modep
)
962 unsigned int mode
= 0;
964 while ((c
= *str
++) != ' ') {
965 if (c
< '0' || c
> '7')
967 mode
= (mode
<< 3) + (c
- '0');
973 static void load_tree(struct tree_entry
*root
)
975 unsigned char* sha1
= root
->versions
[1].sha1
;
976 struct object_entry
*myoe
;
977 struct tree_content
*t
;
982 root
->tree
= t
= new_tree_content(8);
983 if (is_null_sha1(sha1
))
986 myoe
= find_object(sha1
);
988 if (myoe
->type
!= OBJ_TREE
)
989 die("Not a tree: %s", sha1_to_hex(sha1
));
991 buf
= gfi_unpack_entry(myoe
, &size
);
994 buf
= read_sha1_file(sha1
, type
, &size
);
995 if (!buf
|| strcmp(type
, tree_type
))
996 die("Can't load tree %s", sha1_to_hex(sha1
));
1000 while (c
!= (buf
+ size
)) {
1001 struct tree_entry
*e
= new_tree_entry();
1003 if (t
->entry_count
== t
->entry_capacity
)
1004 root
->tree
= t
= grow_tree_content(t
, 8);
1005 t
->entries
[t
->entry_count
++] = e
;
1008 c
= get_mode(c
, &e
->versions
[1].mode
);
1010 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1011 e
->versions
[0].mode
= e
->versions
[1].mode
;
1012 e
->name
= to_atom(c
, strlen(c
));
1013 c
+= e
->name
->str_len
+ 1;
1014 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1015 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1021 static int tecmp0 (const void *_a
, const void *_b
)
1023 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1024 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1025 return base_name_compare(
1026 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1027 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1030 static int tecmp1 (const void *_a
, const void *_b
)
1032 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1033 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1034 return base_name_compare(
1035 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1036 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1039 static void mktree(struct tree_content
*t
,
1049 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1051 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1053 for (i
= 0; i
< t
->entry_count
; i
++) {
1054 if (t
->entries
[i
]->versions
[v
].mode
)
1055 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1058 size_dbuf(b
, maxlen
);
1060 for (i
= 0; i
< t
->entry_count
; i
++) {
1061 struct tree_entry
*e
= t
->entries
[i
];
1062 if (!e
->versions
[v
].mode
)
1064 c
+= sprintf(c
, "%o", e
->versions
[v
].mode
);
1066 strcpy(c
, e
->name
->str_dat
);
1067 c
+= e
->name
->str_len
+ 1;
1068 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1071 *szp
= c
- (char*)b
->buffer
;
1074 static void store_tree(struct tree_entry
*root
)
1076 struct tree_content
*t
= root
->tree
;
1077 unsigned int i
, j
, del
;
1078 unsigned long new_len
;
1079 struct last_object lo
;
1080 struct object_entry
*le
;
1082 if (!is_null_sha1(root
->versions
[1].sha1
))
1085 for (i
= 0; i
< t
->entry_count
; i
++) {
1086 if (t
->entries
[i
]->tree
)
1087 store_tree(t
->entries
[i
]);
1090 le
= find_object(root
->versions
[0].sha1
);
1091 if (!S_ISDIR(root
->versions
[0].mode
)
1093 || le
->pack_id
!= pack_id
) {
1097 mktree(t
, 0, &lo
.len
, &old_tree
);
1098 lo
.data
= old_tree
.buffer
;
1099 lo
.offset
= le
->offset
;
1100 lo
.depth
= t
->delta_depth
;
1104 mktree(t
, 1, &new_len
, &new_tree
);
1105 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1106 &lo
, root
->versions
[1].sha1
, 0);
1108 t
->delta_depth
= lo
.depth
;
1109 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1110 struct tree_entry
*e
= t
->entries
[i
];
1111 if (e
->versions
[1].mode
) {
1112 e
->versions
[0].mode
= e
->versions
[1].mode
;
1113 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1114 t
->entries
[j
++] = e
;
1116 release_tree_entry(e
);
1120 t
->entry_count
-= del
;
1123 static int tree_content_set(
1124 struct tree_entry
*root
,
1126 const unsigned char *sha1
,
1127 const unsigned int mode
)
1129 struct tree_content
*t
= root
->tree
;
1132 struct tree_entry
*e
;
1134 slash1
= strchr(p
, '/');
1140 for (i
= 0; i
< t
->entry_count
; i
++) {
1142 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1144 if (e
->versions
[1].mode
== mode
1145 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1147 e
->versions
[1].mode
= mode
;
1148 hashcpy(e
->versions
[1].sha1
, sha1
);
1150 release_tree_content_recursive(e
->tree
);
1153 hashclr(root
->versions
[1].sha1
);
1156 if (!S_ISDIR(e
->versions
[1].mode
)) {
1157 e
->tree
= new_tree_content(8);
1158 e
->versions
[1].mode
= S_IFDIR
;
1162 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1163 hashclr(root
->versions
[1].sha1
);
1170 if (t
->entry_count
== t
->entry_capacity
)
1171 root
->tree
= t
= grow_tree_content(t
, 8);
1172 e
= new_tree_entry();
1173 e
->name
= to_atom(p
, n
);
1174 e
->versions
[0].mode
= 0;
1175 hashclr(e
->versions
[0].sha1
);
1176 t
->entries
[t
->entry_count
++] = e
;
1178 e
->tree
= new_tree_content(8);
1179 e
->versions
[1].mode
= S_IFDIR
;
1180 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1183 e
->versions
[1].mode
= mode
;
1184 hashcpy(e
->versions
[1].sha1
, sha1
);
1186 hashclr(root
->versions
[1].sha1
);
1190 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1192 struct tree_content
*t
= root
->tree
;
1195 struct tree_entry
*e
;
1197 slash1
= strchr(p
, '/');
1203 for (i
= 0; i
< t
->entry_count
; i
++) {
1205 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1206 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1210 if (tree_content_remove(e
, slash1
+ 1)) {
1211 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1212 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1213 hashclr(root
->versions
[1].sha1
);
1226 release_tree_content_recursive(e
->tree
);
1229 e
->versions
[1].mode
= 0;
1230 hashclr(e
->versions
[1].sha1
);
1231 hashclr(root
->versions
[1].sha1
);
1235 static void dump_branches()
1237 static const char *msg
= "fast-import";
1240 struct ref_lock
*lock
;
1242 for (i
= 0; i
< branch_table_sz
; i
++) {
1243 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
1244 lock
= lock_any_ref_for_update(b
->name
, NULL
);
1245 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1246 die("Can't write %s", b
->name
);
1251 static void dump_tags()
1253 static const char *msg
= "fast-import";
1255 struct ref_lock
*lock
;
1256 char path
[PATH_MAX
];
1258 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1259 sprintf(path
, "refs/tags/%s", t
->name
);
1260 lock
= lock_any_ref_for_update(path
, NULL
);
1261 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1262 die("Can't write %s", path
);
1266 static void dump_marks_helper(FILE *f
,
1272 for (k
= 0; k
< 1024; k
++) {
1273 if (m
->data
.sets
[k
])
1274 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1278 for (k
= 0; k
< 1024; k
++) {
1279 if (m
->data
.marked
[k
])
1280 fprintf(f
, ":%ju %s\n", base
+ k
,
1281 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1286 static void dump_marks()
1290 FILE *f
= fopen(mark_file
, "w");
1291 dump_marks_helper(f
, 0, marks
);
1296 static void read_next_command()
1298 read_line(&command_buf
, stdin
, '\n');
1301 static void cmd_mark()
1303 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1304 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1305 read_next_command();
1311 static void* cmd_data (size_t *size
)
1317 if (strncmp("data ", command_buf
.buf
, 5))
1318 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1320 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1321 buffer
= xmalloc(length
);
1323 while (n
< length
) {
1324 size_t s
= fread((char*)buffer
+ n
, 1, length
- n
, stdin
);
1325 if (!s
&& feof(stdin
))
1326 die("EOF in data (%lu bytes remaining)", length
- n
);
1330 if (fgetc(stdin
) != '\n')
1331 die("An lf did not trail the binary data as expected.");
1337 static void cmd_new_blob()
1342 read_next_command();
1346 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1350 static void unload_one_branch()
1352 while (cur_active_branches
1353 && cur_active_branches
>= max_active_branches
) {
1354 unsigned long min_commit
= ULONG_MAX
;
1355 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1357 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1358 if (e
->last_commit
< min_commit
) {
1360 min_commit
= e
->last_commit
;
1366 e
= p
->active_next_branch
;
1367 p
->active_next_branch
= e
->active_next_branch
;
1369 e
= active_branches
;
1370 active_branches
= e
->active_next_branch
;
1372 e
->active_next_branch
= NULL
;
1373 if (e
->branch_tree
.tree
) {
1374 release_tree_content_recursive(e
->branch_tree
.tree
);
1375 e
->branch_tree
.tree
= NULL
;
1377 cur_active_branches
--;
1381 static void load_branch(struct branch
*b
)
1383 load_tree(&b
->branch_tree
);
1384 b
->active_next_branch
= active_branches
;
1385 active_branches
= b
;
1386 cur_active_branches
++;
1387 branch_load_count
++;
1390 static void file_change_m(struct branch
*b
)
1392 const char *p
= command_buf
.buf
+ 2;
1395 struct object_entry
*oe
;
1396 unsigned char sha1
[20];
1400 p
= get_mode(p
, &mode
);
1402 die("Corrupt mode: %s", command_buf
.buf
);
1404 case S_IFREG
| 0644:
1405 case S_IFREG
| 0755:
1412 die("Corrupt mode: %s", command_buf
.buf
);
1417 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1418 hashcpy(sha1
, oe
->sha1
);
1421 if (get_sha1_hex(p
, sha1
))
1422 die("Invalid SHA1: %s", command_buf
.buf
);
1423 oe
= find_object(sha1
);
1427 die("Missing space after SHA1: %s", command_buf
.buf
);
1429 p_uq
= unquote_c_style(p
, &endp
);
1432 die("Garbage after path in: %s", command_buf
.buf
);
1437 if (oe
->type
!= OBJ_BLOB
)
1438 die("Not a blob (actually a %s): %s",
1439 command_buf
.buf
, type_names
[oe
->type
]);
1441 if (sha1_object_info(sha1
, type
, NULL
))
1442 die("Blob not found: %s", command_buf
.buf
);
1443 if (strcmp(blob_type
, type
))
1444 die("Not a blob (actually a %s): %s",
1445 command_buf
.buf
, type
);
1448 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1454 static void file_change_d(struct branch
*b
)
1456 const char *p
= command_buf
.buf
+ 2;
1460 p_uq
= unquote_c_style(p
, &endp
);
1463 die("Garbage after path in: %s", command_buf
.buf
);
1466 tree_content_remove(&b
->branch_tree
, p
);
1471 static void cmd_from(struct branch
*b
)
1473 const char *from
, *endp
;
1477 if (strncmp("from ", command_buf
.buf
, 5))
1481 die("Can't reinitailize branch %s", b
->name
);
1483 from
= strchr(command_buf
.buf
, ' ') + 1;
1484 str_uq
= unquote_c_style(from
, &endp
);
1487 die("Garbage after string in: %s", command_buf
.buf
);
1491 s
= lookup_branch(from
);
1493 die("Can't create a branch from itself: %s", b
->name
);
1495 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1496 hashcpy(b
->sha1
, s
->sha1
);
1497 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1498 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1499 } else if (*from
== ':') {
1500 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1501 struct object_entry
*oe
= find_mark(idnum
);
1504 if (oe
->type
!= OBJ_COMMIT
)
1505 die("Mark :%ju not a commit", idnum
);
1506 hashcpy(b
->sha1
, oe
->sha1
);
1507 buf
= gfi_unpack_entry(oe
, &size
);
1508 if (!buf
|| size
< 46)
1509 die("Not a valid commit: %s", from
);
1510 if (memcmp("tree ", buf
, 5)
1511 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1512 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1514 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1515 b
->branch_tree
.versions
[1].sha1
);
1516 } else if (!get_sha1(from
, b
->sha1
)) {
1517 if (is_null_sha1(b
->sha1
)) {
1518 hashclr(b
->branch_tree
.versions
[0].sha1
);
1519 hashclr(b
->branch_tree
.versions
[1].sha1
);
1524 buf
= read_object_with_reference(b
->sha1
,
1525 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1526 if (!buf
|| size
< 46)
1527 die("Not a valid commit: %s", from
);
1528 if (memcmp("tree ", buf
, 5)
1529 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1530 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1532 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1533 b
->branch_tree
.versions
[1].sha1
);
1536 die("Invalid ref name or SHA1 expression: %s", from
);
1538 read_next_command();
1541 static struct hash_list
* cmd_merge(unsigned int *count
)
1543 struct hash_list
*list
= NULL
, *n
, *e
;
1544 const char *from
, *endp
;
1549 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1550 from
= strchr(command_buf
.buf
, ' ') + 1;
1551 str_uq
= unquote_c_style(from
, &endp
);
1554 die("Garbage after string in: %s", command_buf
.buf
);
1558 n
= xmalloc(sizeof(*n
));
1559 s
= lookup_branch(from
);
1561 hashcpy(n
->sha1
, s
->sha1
);
1562 else if (*from
== ':') {
1563 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1564 struct object_entry
*oe
= find_mark(idnum
);
1565 if (oe
->type
!= OBJ_COMMIT
)
1566 die("Mark :%ju not a commit", idnum
);
1567 hashcpy(n
->sha1
, oe
->sha1
);
1568 } else if (get_sha1(from
, n
->sha1
))
1569 die("Invalid ref name or SHA1 expression: %s", from
);
1578 read_next_command();
1583 static void cmd_new_commit()
1591 char *author
= NULL
;
1592 char *committer
= NULL
;
1593 struct hash_list
*merge_list
= NULL
;
1594 unsigned int merge_count
;
1596 /* Obtain the branch name from the rest of our command */
1597 sp
= strchr(command_buf
.buf
, ' ') + 1;
1598 str_uq
= unquote_c_style(sp
, &endp
);
1601 die("Garbage after ref in: %s", command_buf
.buf
);
1604 b
= lookup_branch(sp
);
1610 read_next_command();
1612 if (!strncmp("author ", command_buf
.buf
, 7)) {
1613 author
= strdup(command_buf
.buf
);
1614 read_next_command();
1616 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1617 committer
= strdup(command_buf
.buf
);
1618 read_next_command();
1621 die("Expected committer but didn't get one");
1622 msg
= cmd_data(&msglen
);
1623 read_next_command();
1625 merge_list
= cmd_merge(&merge_count
);
1627 /* ensure the branch is active/loaded */
1628 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1629 unload_one_branch();
1635 if (1 == command_buf
.len
)
1637 else if (!strncmp("M ", command_buf
.buf
, 2))
1639 else if (!strncmp("D ", command_buf
.buf
, 2))
1642 die("Unsupported file_change: %s", command_buf
.buf
);
1643 read_next_command();
1646 /* build the tree and the commit */
1647 store_tree(&b
->branch_tree
);
1648 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1649 b
->branch_tree
.versions
[1].sha1
);
1650 size_dbuf(&new_data
, 97 + msglen
1653 ? strlen(author
) + strlen(committer
)
1654 : 2 * strlen(committer
)));
1655 sp
= new_data
.buffer
;
1656 sp
+= sprintf(sp
, "tree %s\n",
1657 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1658 if (!is_null_sha1(b
->sha1
))
1659 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1660 while (merge_list
) {
1661 struct hash_list
*next
= merge_list
->next
;
1662 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1667 sp
+= sprintf(sp
, "%s\n", author
);
1669 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1670 sp
+= sprintf(sp
, "%s\n\n", committer
);
1671 memcpy(sp
, msg
, msglen
);
1678 store_object(OBJ_COMMIT
,
1679 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1680 NULL
, b
->sha1
, next_mark
);
1681 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1684 int need_dq
= quote_c_style(b
->name
, NULL
, NULL
, 0);
1685 fprintf(branch_log
, "commit ");
1687 fputc('"', branch_log
);
1688 quote_c_style(b
->name
, NULL
, branch_log
, 0);
1689 fputc('"', branch_log
);
1691 fprintf(branch_log
, "%s", b
->name
);
1692 fprintf(branch_log
," :%ju %s\n",next_mark
,sha1_to_hex(b
->sha1
));
1696 static void cmd_new_tag()
1707 uintmax_t from_mark
= 0;
1708 unsigned char sha1
[20];
1710 /* Obtain the new tag name from the rest of our command */
1711 sp
= strchr(command_buf
.buf
, ' ') + 1;
1712 str_uq
= unquote_c_style(sp
, &endp
);
1715 die("Garbage after tag name in: %s", command_buf
.buf
);
1718 t
= pool_alloc(sizeof(struct tag
));
1720 t
->name
= pool_strdup(sp
);
1722 last_tag
->next_tag
= t
;
1728 read_next_command();
1731 if (strncmp("from ", command_buf
.buf
, 5))
1732 die("Expected from command, got %s", command_buf
.buf
);
1734 from
= strchr(command_buf
.buf
, ' ') + 1;
1735 str_uq
= unquote_c_style(from
, &endp
);
1738 die("Garbage after string in: %s", command_buf
.buf
);
1742 s
= lookup_branch(from
);
1744 hashcpy(sha1
, s
->sha1
);
1745 } else if (*from
== ':') {
1746 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1747 struct object_entry
*oe
= find_mark(from_mark
);
1748 if (oe
->type
!= OBJ_COMMIT
)
1749 die("Mark :%ju not a commit", from_mark
);
1750 hashcpy(sha1
, oe
->sha1
);
1751 } else if (!get_sha1(from
, sha1
)) {
1755 buf
= read_object_with_reference(sha1
,
1756 type_names
[OBJ_COMMIT
], &size
, sha1
);
1757 if (!buf
|| size
< 46)
1758 die("Not a valid commit: %s", from
);
1761 die("Invalid ref name or SHA1 expression: %s", from
);
1765 read_next_command();
1768 if (strncmp("tagger ", command_buf
.buf
, 7))
1769 die("Expected tagger command, got %s", command_buf
.buf
);
1770 tagger
= strdup(command_buf
.buf
);
1772 /* tag payload/message */
1773 read_next_command();
1774 msg
= cmd_data(&msglen
);
1776 /* build the tag object */
1777 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1778 sp
= new_data
.buffer
;
1779 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1780 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1781 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1782 sp
+= sprintf(sp
, "%s\n\n", tagger
);
1783 memcpy(sp
, msg
, msglen
);
1788 store_object(OBJ_TAG
, new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1792 int need_dq
= quote_c_style(t
->name
, NULL
, NULL
, 0);
1793 fprintf(branch_log
, "tag ");
1795 fputc('"', branch_log
);
1796 quote_c_style(t
->name
, NULL
, branch_log
, 0);
1797 fputc('"', branch_log
);
1799 fprintf(branch_log
, "%s", t
->name
);
1800 fprintf(branch_log
," :%ju %s\n",from_mark
,sha1_to_hex(t
->sha1
));
1804 static void cmd_reset_branch()
1811 /* Obtain the branch name from the rest of our command */
1812 sp
= strchr(command_buf
.buf
, ' ') + 1;
1813 str_uq
= unquote_c_style(sp
, &endp
);
1816 die("Garbage after ref in: %s", command_buf
.buf
);
1819 b
= lookup_branch(sp
);
1822 if (b
->branch_tree
.tree
) {
1823 release_tree_content_recursive(b
->branch_tree
.tree
);
1824 b
->branch_tree
.tree
= NULL
;
1831 read_next_command();
1835 static void cmd_checkpoint()
1839 read_next_command();
1842 static const char fast_import_usage
[] =
1843 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1845 int main(int argc
, const char **argv
)
1848 uintmax_t est_obj_cnt
= object_entry_alloc
;
1849 uintmax_t total_count
, duplicate_count
;
1852 git_config(git_default_config
);
1854 for (i
= 1; i
< argc
; i
++) {
1855 const char *a
= argv
[i
];
1857 if (*a
!= '-' || !strcmp(a
, "--"))
1859 else if (!strncmp(a
, "--objects=", 10))
1860 est_obj_cnt
= strtoumax(a
+ 10, NULL
, 0);
1861 else if (!strncmp(a
, "--max-objects-per-pack=", 23))
1862 max_objects
= strtoumax(a
+ 23, NULL
, 0);
1863 else if (!strncmp(a
, "--max-pack-size=", 16))
1864 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1865 else if (!strncmp(a
, "--depth=", 8))
1866 max_depth
= strtoul(a
+ 8, NULL
, 0);
1867 else if (!strncmp(a
, "--active-branches=", 18))
1868 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1869 else if (!strncmp(a
, "--export-marks=", 15))
1871 else if (!strncmp(a
, "--branch-log=", 13)) {
1872 branch_log
= fopen(a
+ 13, "w");
1874 die("Can't create %s: %s", a
+ 13, strerror(errno
));
1877 die("unknown option %s", a
);
1880 usage(fast_import_usage
);
1882 alloc_objects(est_obj_cnt
);
1883 strbuf_init(&command_buf
);
1885 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1886 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1887 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1888 marks
= pool_calloc(1, sizeof(struct mark_set
));
1892 read_next_command();
1893 if (command_buf
.eof
)
1895 else if (!strcmp("blob", command_buf
.buf
))
1897 else if (!strncmp("commit ", command_buf
.buf
, 7))
1899 else if (!strncmp("tag ", command_buf
.buf
, 4))
1901 else if (!strncmp("reset ", command_buf
.buf
, 6))
1903 else if (!strcmp("checkpoint", command_buf
.buf
))
1906 die("Unsupported command: %s", command_buf
.buf
);
1918 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
1919 total_count
+= object_count_by_type
[i
];
1920 duplicate_count
= 0;
1921 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
1922 duplicate_count
+= duplicate_count_by_type
[i
];
1924 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1925 fprintf(stderr
, "---------------------------------------------------------------------\n");
1926 fprintf(stderr
, "Alloc'd objects: %10ju (%10ju overflow )\n", alloc_count
, alloc_count
- est_obj_cnt
);
1927 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
1928 fprintf(stderr
, " blobs : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
], delta_count_by_type
[OBJ_BLOB
]);
1929 fprintf(stderr
, " trees : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
], delta_count_by_type
[OBJ_TREE
]);
1930 fprintf(stderr
, " commits: %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
], delta_count_by_type
[OBJ_COMMIT
]);
1931 fprintf(stderr
, " tags : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
], delta_count_by_type
[OBJ_TAG
]);
1932 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
1933 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
1934 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
1935 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
1936 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
1937 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
1938 fprintf(stderr
, "---------------------------------------------------------------------\n");
1940 fprintf(stderr
, "---------------------------------------------------------------------\n");
1941 fprintf(stderr
, "\n");