]>
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 bigint ::= # unsigned integer value, ascii base10 notation;
76 binary_data ::= # file content, not interpreted;
78 sp ::= # ASCII space character;
79 lf ::= # ASCII newline (LF) character;
81 # note: a colon (':') must precede the numerical value assigned to
82 # an idnum. This is to distinguish it from a ref or tag name as
83 # GIT does not permit ':' in ref or tag strings.
86 path ::= # GIT style file path, e.g. "a/b/c";
87 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
88 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
89 sha1exp ::= # Any valid GIT SHA1 expression;
90 hexsha1 ::= # SHA1 in hexadecimal format;
92 # note: name and email are UTF8 strings, however name must not
93 # contain '<' or lf and email must not contain any of the
94 # following: '<', '>', lf.
96 name ::= # valid GIT author/committer name;
97 email ::= # valid GIT author/committer email;
98 ts ::= # time since the epoch in seconds, ascii base10 notation;
99 tz ::= # GIT style timezone;
110 #include "csum-file.h"
114 #define PACK_ID_BITS 16
115 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
119 struct object_entry
*next
;
120 unsigned long offset
;
121 unsigned type
: TYPE_BITS
;
122 unsigned pack_id
: PACK_ID_BITS
;
123 unsigned char sha1
[20];
126 struct object_entry_pool
128 struct object_entry_pool
*next_pool
;
129 struct object_entry
*next_free
;
130 struct object_entry
*end
;
131 struct object_entry entries
[FLEX_ARRAY
]; /* more */
137 struct object_entry
*marked
[1024];
138 struct mark_set
*sets
[1024];
147 unsigned long offset
;
154 struct mem_pool
*next_pool
;
157 char space
[FLEX_ARRAY
]; /* more */
162 struct atom_str
*next_atom
;
163 unsigned int str_len
;
164 char str_dat
[FLEX_ARRAY
]; /* more */
170 struct tree_content
*tree
;
171 struct atom_str
* name
;
175 unsigned char sha1
[20];
181 unsigned int entry_capacity
; /* must match avail_tree_content */
182 unsigned int entry_count
;
183 unsigned int delta_depth
;
184 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
187 struct avail_tree_content
189 unsigned int entry_capacity
; /* must match tree_content */
190 struct avail_tree_content
*next_avail
;
195 struct branch
*table_next_branch
;
196 struct branch
*active_next_branch
;
198 struct tree_entry branch_tree
;
199 uintmax_t last_commit
;
200 unsigned int pack_id
;
201 unsigned char sha1
[20];
206 struct tag
*next_tag
;
208 unsigned int pack_id
;
209 unsigned char sha1
[20];
220 struct hash_list
*next
;
221 unsigned char sha1
[20];
224 /* Configured limits on output */
225 static unsigned long max_depth
= 10;
226 static unsigned long max_packsize
= (1LL << 32) - 1;
227 static uintmax_t max_objects
= -1;
229 /* Stats and misc. counters */
230 static uintmax_t alloc_count
;
231 static uintmax_t marks_set_count
;
232 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
233 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
234 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
235 static unsigned long object_count
;
236 static unsigned long branch_count
;
237 static unsigned long branch_load_count
;
240 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
241 static size_t total_allocd
;
242 static struct mem_pool
*mem_pool
;
244 /* Atom management */
245 static unsigned int atom_table_sz
= 4451;
246 static unsigned int atom_cnt
;
247 static struct atom_str
**atom_table
;
249 /* The .pack file being generated */
250 static unsigned int pack_id
;
251 static struct packed_git
*pack_data
;
252 static struct packed_git
**all_packs
;
253 static unsigned long pack_size
;
255 /* Table of objects we've written. */
256 static unsigned int object_entry_alloc
= 5000;
257 static struct object_entry_pool
*blocks
;
258 static struct object_entry
*object_table
[1 << 16];
259 static struct mark_set
*marks
;
260 static const char* mark_file
;
263 static struct last_object last_blob
;
265 /* Tree management */
266 static unsigned int tree_entry_alloc
= 1000;
267 static void *avail_tree_entry
;
268 static unsigned int avail_tree_table_sz
= 100;
269 static struct avail_tree_content
**avail_tree_table
;
270 static struct dbuf old_tree
;
271 static struct dbuf new_tree
;
274 static unsigned long max_active_branches
= 5;
275 static unsigned long cur_active_branches
;
276 static unsigned long branch_table_sz
= 1039;
277 static struct branch
**branch_table
;
278 static struct branch
*active_branches
;
281 static struct tag
*first_tag
;
282 static struct tag
*last_tag
;
284 /* Input stream parsing */
285 static struct strbuf command_buf
;
286 static uintmax_t next_mark
;
287 static struct dbuf new_data
;
288 static FILE* branch_log
;
291 static void alloc_objects(unsigned int cnt
)
293 struct object_entry_pool
*b
;
295 b
= xmalloc(sizeof(struct object_entry_pool
)
296 + cnt
* sizeof(struct object_entry
));
297 b
->next_pool
= blocks
;
298 b
->next_free
= b
->entries
;
299 b
->end
= b
->entries
+ cnt
;
304 static struct object_entry
* new_object(unsigned char *sha1
)
306 struct object_entry
*e
;
308 if (blocks
->next_free
== blocks
->end
)
309 alloc_objects(object_entry_alloc
);
311 e
= blocks
->next_free
++;
312 hashcpy(e
->sha1
, sha1
);
316 static struct object_entry
* find_object(unsigned char *sha1
)
318 unsigned int h
= sha1
[0] << 8 | sha1
[1];
319 struct object_entry
*e
;
320 for (e
= object_table
[h
]; e
; e
= e
->next
)
321 if (!hashcmp(sha1
, e
->sha1
))
326 static struct object_entry
* insert_object(unsigned char *sha1
)
328 unsigned int h
= sha1
[0] << 8 | sha1
[1];
329 struct object_entry
*e
= object_table
[h
];
330 struct object_entry
*p
= NULL
;
333 if (!hashcmp(sha1
, e
->sha1
))
339 e
= new_object(sha1
);
349 static unsigned int hc_str(const char *s
, size_t len
)
357 static void* pool_alloc(size_t len
)
362 for (p
= mem_pool
; p
; p
= p
->next_pool
)
363 if ((p
->end
- p
->next_free
>= len
))
367 if (len
>= (mem_pool_alloc
/2)) {
371 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
372 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
373 p
->next_pool
= mem_pool
;
374 p
->next_free
= p
->space
;
375 p
->end
= p
->next_free
+ mem_pool_alloc
;
380 /* round out to a pointer alignment */
381 if (len
& (sizeof(void*) - 1))
382 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
387 static void* pool_calloc(size_t count
, size_t size
)
389 size_t len
= count
* size
;
390 void *r
= pool_alloc(len
);
395 static char* pool_strdup(const char *s
)
397 char *r
= pool_alloc(strlen(s
) + 1);
402 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
405 if (b
->capacity
>= maxlen
)
409 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
410 b
->buffer
= xmalloc(b
->capacity
);
413 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
415 struct mark_set
*s
= marks
;
416 while ((idnum
>> s
->shift
) >= 1024) {
417 s
= pool_calloc(1, sizeof(struct mark_set
));
418 s
->shift
= marks
->shift
+ 10;
419 s
->data
.sets
[0] = marks
;
423 uintmax_t i
= idnum
>> s
->shift
;
424 idnum
-= i
<< s
->shift
;
425 if (!s
->data
.sets
[i
]) {
426 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
427 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
431 if (!s
->data
.marked
[idnum
])
433 s
->data
.marked
[idnum
] = oe
;
436 static struct object_entry
* find_mark(uintmax_t idnum
)
438 uintmax_t orig_idnum
= idnum
;
439 struct mark_set
*s
= marks
;
440 struct object_entry
*oe
= NULL
;
441 if ((idnum
>> s
->shift
) < 1024) {
442 while (s
&& s
->shift
) {
443 uintmax_t i
= idnum
>> s
->shift
;
444 idnum
-= i
<< s
->shift
;
448 oe
= s
->data
.marked
[idnum
];
451 die("mark :%ju not declared", orig_idnum
);
455 static struct atom_str
* to_atom(const char *s
, size_t len
)
457 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
460 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
461 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
464 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
466 strncpy(c
->str_dat
, s
, len
);
468 c
->next_atom
= atom_table
[hc
];
474 static struct branch
* lookup_branch(const char *name
)
476 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
479 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
480 if (!strcmp(name
, b
->name
))
485 static struct branch
* new_branch(const char *name
)
487 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
488 struct branch
* b
= lookup_branch(name
);
491 die("Invalid attempt to create duplicate branch: %s", name
);
492 if (check_ref_format(name
))
493 die("Branch name doesn't conform to GIT standards: %s", name
);
495 b
= pool_calloc(1, sizeof(struct branch
));
496 b
->name
= pool_strdup(name
);
497 b
->table_next_branch
= branch_table
[hc
];
498 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
499 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
500 b
->pack_id
= MAX_PACK_ID
;
501 branch_table
[hc
] = b
;
506 static unsigned int hc_entries(unsigned int cnt
)
508 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
509 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
512 static struct tree_content
* new_tree_content(unsigned int cnt
)
514 struct avail_tree_content
*f
, *l
= NULL
;
515 struct tree_content
*t
;
516 unsigned int hc
= hc_entries(cnt
);
518 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
519 if (f
->entry_capacity
>= cnt
)
524 l
->next_avail
= f
->next_avail
;
526 avail_tree_table
[hc
] = f
->next_avail
;
528 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
529 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
530 f
->entry_capacity
= cnt
;
533 t
= (struct tree_content
*)f
;
539 static void release_tree_entry(struct tree_entry
*e
);
540 static void release_tree_content(struct tree_content
*t
)
542 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
543 unsigned int hc
= hc_entries(f
->entry_capacity
);
544 f
->next_avail
= avail_tree_table
[hc
];
545 avail_tree_table
[hc
] = f
;
548 static void release_tree_content_recursive(struct tree_content
*t
)
551 for (i
= 0; i
< t
->entry_count
; i
++)
552 release_tree_entry(t
->entries
[i
]);
553 release_tree_content(t
);
556 static struct tree_content
* grow_tree_content(
557 struct tree_content
*t
,
560 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
561 r
->entry_count
= t
->entry_count
;
562 r
->delta_depth
= t
->delta_depth
;
563 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
564 release_tree_content(t
);
568 static struct tree_entry
* new_tree_entry(void)
570 struct tree_entry
*e
;
572 if (!avail_tree_entry
) {
573 unsigned int n
= tree_entry_alloc
;
574 total_allocd
+= n
* sizeof(struct tree_entry
);
575 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
577 *((void**)e
) = e
+ 1;
583 e
= avail_tree_entry
;
584 avail_tree_entry
= *((void**)e
);
588 static void release_tree_entry(struct tree_entry
*e
)
591 release_tree_content_recursive(e
->tree
);
592 *((void**)e
) = avail_tree_entry
;
593 avail_tree_entry
= e
;
596 static void start_packfile(void)
598 static char tmpfile
[PATH_MAX
];
599 struct packed_git
*p
;
600 struct pack_header hdr
;
603 snprintf(tmpfile
, sizeof(tmpfile
),
604 "%s/pack_XXXXXX", get_object_directory());
605 pack_fd
= mkstemp(tmpfile
);
607 die("Can't create %s: %s", tmpfile
, strerror(errno
));
608 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
609 strcpy(p
->pack_name
, tmpfile
);
610 p
->pack_fd
= pack_fd
;
612 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
613 hdr
.hdr_version
= htonl(2);
615 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
618 pack_size
= sizeof(hdr
);
621 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
622 all_packs
[pack_id
] = p
;
625 static void fixup_header_footer(void)
627 int pack_fd
= pack_data
->pack_fd
;
633 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
634 die("Failed seeking to start: %s", strerror(errno
));
637 if (read_in_full(pack_fd
, hdr
, 8) != 8)
638 die("Unable to reread header of %s", pack_data
->pack_name
);
639 SHA1_Update(&c
, hdr
, 8);
641 cnt
= htonl(object_count
);
642 SHA1_Update(&c
, &cnt
, 4);
643 write_or_die(pack_fd
, &cnt
, 4);
645 buf
= xmalloc(128 * 1024);
647 size_t n
= xread(pack_fd
, buf
, 128 * 1024);
650 SHA1_Update(&c
, buf
, n
);
654 SHA1_Final(pack_data
->sha1
, &c
);
655 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
659 static int oecmp (const void *a_
, const void *b_
)
661 struct object_entry
*a
= *((struct object_entry
**)a_
);
662 struct object_entry
*b
= *((struct object_entry
**)b_
);
663 return hashcmp(a
->sha1
, b
->sha1
);
666 static char* create_index(void)
668 static char tmpfile
[PATH_MAX
];
671 struct object_entry
**idx
, **c
, **last
, *e
;
672 struct object_entry_pool
*o
;
673 unsigned int array
[256];
676 /* Build the sorted table of object IDs. */
677 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
679 for (o
= blocks
; o
; o
= o
->next_pool
)
680 for (e
= o
->next_free
; e
-- != o
->entries
;)
681 if (pack_id
== e
->pack_id
)
683 last
= idx
+ object_count
;
685 die("internal consistency error creating the index");
686 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
688 /* Generate the fan-out array. */
690 for (i
= 0; i
< 256; i
++) {
691 struct object_entry
**next
= c
;;
692 while (next
< last
) {
693 if ((*next
)->sha1
[0] != i
)
697 array
[i
] = htonl(next
- idx
);
701 snprintf(tmpfile
, sizeof(tmpfile
),
702 "%s/index_XXXXXX", get_object_directory());
703 idx_fd
= mkstemp(tmpfile
);
705 die("Can't create %s: %s", tmpfile
, strerror(errno
));
706 f
= sha1fd(idx_fd
, tmpfile
);
707 sha1write(f
, array
, 256 * sizeof(int));
709 for (c
= idx
; c
!= last
; c
++) {
710 unsigned int offset
= htonl((*c
)->offset
);
711 sha1write(f
, &offset
, 4);
712 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
713 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
715 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
716 sha1close(f
, NULL
, 1);
718 SHA1_Final(pack_data
->sha1
, &ctx
);
722 static char* keep_pack(char *curr_index_name
)
724 static char name
[PATH_MAX
];
725 static char *keep_msg
= "fast-import";
728 chmod(pack_data
->pack_name
, 0444);
729 chmod(curr_index_name
, 0444);
731 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
732 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
733 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
735 die("cannot create keep file");
736 write(keep_fd
, keep_msg
, strlen(keep_msg
));
739 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
740 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
741 if (move_temp_to_file(pack_data
->pack_name
, name
))
742 die("cannot store pack file");
744 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
745 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
746 if (move_temp_to_file(curr_index_name
, name
))
747 die("cannot store index file");
751 static void unkeep_all_packs(void)
753 static char name
[PATH_MAX
];
756 for (k
= 0; k
< pack_id
; k
++) {
757 struct packed_git
*p
= all_packs
[k
];
758 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
759 get_object_directory(), sha1_to_hex(p
->sha1
));
764 static void end_packfile(void)
766 struct packed_git
*old_p
= pack_data
, *new_p
;
774 fixup_header_footer();
775 idx_name
= keep_pack(create_index());
777 /* Register the packfile with core git's machinary. */
778 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
780 die("core git rejected index %s", idx_name
);
781 new_p
->windows
= old_p
->windows
;
782 all_packs
[pack_id
] = new_p
;
783 install_packed_git(new_p
);
785 /* Print the boundary */
786 fprintf(stdout
, "%s:", new_p
->pack_name
);
787 for (i
= 0; i
< branch_table_sz
; i
++) {
788 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
789 if (b
->pack_id
== pack_id
)
790 fprintf(stdout
, " %s", sha1_to_hex(b
->sha1
));
793 for (t
= first_tag
; t
; t
= t
->next_tag
) {
794 if (t
->pack_id
== pack_id
)
795 fprintf(stdout
, " %s", sha1_to_hex(t
->sha1
));
802 unlink(old_p
->pack_name
);
805 /* We can't carry a delta across packfiles. */
806 free(last_blob
.data
);
807 last_blob
.data
= NULL
;
809 last_blob
.offset
= 0;
813 static void checkpoint(void)
819 static size_t encode_header(
820 enum object_type type
,
827 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
828 die("bad type %d", type
);
830 c
= (type
<< 4) | (size
& 15);
842 static int store_object(
843 enum object_type type
,
846 struct last_object
*last
,
847 unsigned char *sha1out
,
851 struct object_entry
*e
;
852 unsigned char hdr
[96];
853 unsigned char sha1
[20];
854 unsigned long hdrlen
, deltalen
;
858 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
860 SHA1_Update(&c
, hdr
, hdrlen
);
861 SHA1_Update(&c
, dat
, datlen
);
862 SHA1_Final(sha1
, &c
);
864 hashcpy(sha1out
, sha1
);
866 e
= insert_object(sha1
);
868 insert_mark(mark
, e
);
870 duplicate_count_by_type
[type
]++;
874 if (last
&& last
->data
&& last
->depth
< max_depth
) {
875 delta
= diff_delta(last
->data
, last
->len
,
878 if (delta
&& deltalen
>= datlen
) {
885 memset(&s
, 0, sizeof(s
));
886 deflateInit(&s
, zlib_compression_level
);
889 s
.avail_in
= deltalen
;
894 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
895 s
.next_out
= out
= xmalloc(s
.avail_out
);
896 while (deflate(&s
, Z_FINISH
) == Z_OK
)
900 /* Determine if we should auto-checkpoint. */
901 if ((object_count
+ 1) > max_objects
902 || (object_count
+ 1) < object_count
903 || (pack_size
+ 60 + s
.total_out
) > max_packsize
904 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
906 /* This new object needs to *not* have the current pack_id. */
907 e
->pack_id
= pack_id
+ 1;
910 /* We cannot carry a delta into the new pack. */
915 memset(&s
, 0, sizeof(s
));
916 deflateInit(&s
, zlib_compression_level
);
919 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
920 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
921 while (deflate(&s
, Z_FINISH
) == Z_OK
)
928 e
->pack_id
= pack_id
;
929 e
->offset
= pack_size
;
931 object_count_by_type
[type
]++;
934 unsigned long ofs
= e
->offset
- last
->offset
;
935 unsigned pos
= sizeof(hdr
) - 1;
937 delta_count_by_type
[type
]++;
940 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
941 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
944 hdr
[pos
] = ofs
& 127;
946 hdr
[--pos
] = 128 | (--ofs
& 127);
947 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
948 pack_size
+= sizeof(hdr
) - pos
;
952 hdrlen
= encode_header(type
, datlen
, hdr
);
953 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
957 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
958 pack_size
+= s
.total_out
;
964 if (last
->data
&& !last
->no_free
)
967 last
->offset
= e
->offset
;
973 static void *gfi_unpack_entry(
974 struct object_entry
*oe
,
975 unsigned long *sizep
)
977 static char type
[20];
978 struct packed_git
*p
= all_packs
[oe
->pack_id
];
980 p
->pack_size
= pack_size
+ 20;
981 return unpack_entry(p
, oe
->offset
, type
, sizep
);
984 static const char *get_mode(const char *str
, unsigned int *modep
)
987 unsigned int mode
= 0;
989 while ((c
= *str
++) != ' ') {
990 if (c
< '0' || c
> '7')
992 mode
= (mode
<< 3) + (c
- '0');
998 static void load_tree(struct tree_entry
*root
)
1000 unsigned char* sha1
= root
->versions
[1].sha1
;
1001 struct object_entry
*myoe
;
1002 struct tree_content
*t
;
1007 root
->tree
= t
= new_tree_content(8);
1008 if (is_null_sha1(sha1
))
1011 myoe
= find_object(sha1
);
1013 if (myoe
->type
!= OBJ_TREE
)
1014 die("Not a tree: %s", sha1_to_hex(sha1
));
1016 buf
= gfi_unpack_entry(myoe
, &size
);
1019 buf
= read_sha1_file(sha1
, type
, &size
);
1020 if (!buf
|| strcmp(type
, tree_type
))
1021 die("Can't load tree %s", sha1_to_hex(sha1
));
1025 while (c
!= (buf
+ size
)) {
1026 struct tree_entry
*e
= new_tree_entry();
1028 if (t
->entry_count
== t
->entry_capacity
)
1029 root
->tree
= t
= grow_tree_content(t
, 8);
1030 t
->entries
[t
->entry_count
++] = e
;
1033 c
= get_mode(c
, &e
->versions
[1].mode
);
1035 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1036 e
->versions
[0].mode
= e
->versions
[1].mode
;
1037 e
->name
= to_atom(c
, strlen(c
));
1038 c
+= e
->name
->str_len
+ 1;
1039 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1040 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1046 static int tecmp0 (const void *_a
, const void *_b
)
1048 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1049 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1050 return base_name_compare(
1051 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1052 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1055 static int tecmp1 (const void *_a
, const void *_b
)
1057 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1058 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1059 return base_name_compare(
1060 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1061 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1064 static void mktree(struct tree_content
*t
,
1074 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1076 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1078 for (i
= 0; i
< t
->entry_count
; i
++) {
1079 if (t
->entries
[i
]->versions
[v
].mode
)
1080 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1083 size_dbuf(b
, maxlen
);
1085 for (i
= 0; i
< t
->entry_count
; i
++) {
1086 struct tree_entry
*e
= t
->entries
[i
];
1087 if (!e
->versions
[v
].mode
)
1089 c
+= sprintf(c
, "%o", e
->versions
[v
].mode
);
1091 strcpy(c
, e
->name
->str_dat
);
1092 c
+= e
->name
->str_len
+ 1;
1093 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1096 *szp
= c
- (char*)b
->buffer
;
1099 static void store_tree(struct tree_entry
*root
)
1101 struct tree_content
*t
= root
->tree
;
1102 unsigned int i
, j
, del
;
1103 unsigned long new_len
;
1104 struct last_object lo
;
1105 struct object_entry
*le
;
1107 if (!is_null_sha1(root
->versions
[1].sha1
))
1110 for (i
= 0; i
< t
->entry_count
; i
++) {
1111 if (t
->entries
[i
]->tree
)
1112 store_tree(t
->entries
[i
]);
1115 le
= find_object(root
->versions
[0].sha1
);
1116 if (!S_ISDIR(root
->versions
[0].mode
)
1118 || le
->pack_id
!= pack_id
) {
1122 mktree(t
, 0, &lo
.len
, &old_tree
);
1123 lo
.data
= old_tree
.buffer
;
1124 lo
.offset
= le
->offset
;
1125 lo
.depth
= t
->delta_depth
;
1129 mktree(t
, 1, &new_len
, &new_tree
);
1130 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1131 &lo
, root
->versions
[1].sha1
, 0);
1133 t
->delta_depth
= lo
.depth
;
1134 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1135 struct tree_entry
*e
= t
->entries
[i
];
1136 if (e
->versions
[1].mode
) {
1137 e
->versions
[0].mode
= e
->versions
[1].mode
;
1138 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1139 t
->entries
[j
++] = e
;
1141 release_tree_entry(e
);
1145 t
->entry_count
-= del
;
1148 static int tree_content_set(
1149 struct tree_entry
*root
,
1151 const unsigned char *sha1
,
1152 const unsigned int mode
)
1154 struct tree_content
*t
= root
->tree
;
1157 struct tree_entry
*e
;
1159 slash1
= strchr(p
, '/');
1165 for (i
= 0; i
< t
->entry_count
; i
++) {
1167 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1169 if (e
->versions
[1].mode
== mode
1170 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1172 e
->versions
[1].mode
= mode
;
1173 hashcpy(e
->versions
[1].sha1
, sha1
);
1175 release_tree_content_recursive(e
->tree
);
1178 hashclr(root
->versions
[1].sha1
);
1181 if (!S_ISDIR(e
->versions
[1].mode
)) {
1182 e
->tree
= new_tree_content(8);
1183 e
->versions
[1].mode
= S_IFDIR
;
1187 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1188 hashclr(root
->versions
[1].sha1
);
1195 if (t
->entry_count
== t
->entry_capacity
)
1196 root
->tree
= t
= grow_tree_content(t
, 8);
1197 e
= new_tree_entry();
1198 e
->name
= to_atom(p
, n
);
1199 e
->versions
[0].mode
= 0;
1200 hashclr(e
->versions
[0].sha1
);
1201 t
->entries
[t
->entry_count
++] = e
;
1203 e
->tree
= new_tree_content(8);
1204 e
->versions
[1].mode
= S_IFDIR
;
1205 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1208 e
->versions
[1].mode
= mode
;
1209 hashcpy(e
->versions
[1].sha1
, sha1
);
1211 hashclr(root
->versions
[1].sha1
);
1215 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1217 struct tree_content
*t
= root
->tree
;
1220 struct tree_entry
*e
;
1222 slash1
= strchr(p
, '/');
1228 for (i
= 0; i
< t
->entry_count
; i
++) {
1230 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1231 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1235 if (tree_content_remove(e
, slash1
+ 1)) {
1236 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1237 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1238 hashclr(root
->versions
[1].sha1
);
1251 release_tree_content_recursive(e
->tree
);
1254 e
->versions
[1].mode
= 0;
1255 hashclr(e
->versions
[1].sha1
);
1256 hashclr(root
->versions
[1].sha1
);
1260 static void dump_branches(void)
1262 static const char *msg
= "fast-import";
1265 struct ref_lock
*lock
;
1267 for (i
= 0; i
< branch_table_sz
; i
++) {
1268 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
1269 lock
= lock_any_ref_for_update(b
->name
, NULL
);
1270 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1271 die("Can't write %s", b
->name
);
1276 static void dump_tags(void)
1278 static const char *msg
= "fast-import";
1280 struct ref_lock
*lock
;
1281 char path
[PATH_MAX
];
1283 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1284 sprintf(path
, "refs/tags/%s", t
->name
);
1285 lock
= lock_any_ref_for_update(path
, NULL
);
1286 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1287 die("Can't write %s", path
);
1291 static void dump_marks_helper(FILE *f
,
1297 for (k
= 0; k
< 1024; k
++) {
1298 if (m
->data
.sets
[k
])
1299 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1303 for (k
= 0; k
< 1024; k
++) {
1304 if (m
->data
.marked
[k
])
1305 fprintf(f
, ":%ju %s\n", base
+ k
,
1306 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1311 static void dump_marks(void)
1315 FILE *f
= fopen(mark_file
, "w");
1316 dump_marks_helper(f
, 0, marks
);
1321 static void read_next_command(void)
1323 read_line(&command_buf
, stdin
, '\n');
1326 static void cmd_mark(void)
1328 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1329 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1330 read_next_command();
1336 static void* cmd_data (size_t *size
)
1342 if (strncmp("data ", command_buf
.buf
, 5))
1343 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1345 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1346 buffer
= xmalloc(length
);
1348 while (n
< length
) {
1349 size_t s
= fread((char*)buffer
+ n
, 1, length
- n
, stdin
);
1350 if (!s
&& feof(stdin
))
1351 die("EOF in data (%lu bytes remaining)", length
- n
);
1355 if (fgetc(stdin
) != '\n')
1356 die("An lf did not trail the binary data as expected.");
1362 static void cmd_new_blob(void)
1367 read_next_command();
1371 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1375 static void unload_one_branch(void)
1377 while (cur_active_branches
1378 && cur_active_branches
>= max_active_branches
) {
1379 unsigned long min_commit
= ULONG_MAX
;
1380 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1382 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1383 if (e
->last_commit
< min_commit
) {
1385 min_commit
= e
->last_commit
;
1391 e
= p
->active_next_branch
;
1392 p
->active_next_branch
= e
->active_next_branch
;
1394 e
= active_branches
;
1395 active_branches
= e
->active_next_branch
;
1397 e
->active_next_branch
= NULL
;
1398 if (e
->branch_tree
.tree
) {
1399 release_tree_content_recursive(e
->branch_tree
.tree
);
1400 e
->branch_tree
.tree
= NULL
;
1402 cur_active_branches
--;
1406 static void load_branch(struct branch
*b
)
1408 load_tree(&b
->branch_tree
);
1409 b
->active_next_branch
= active_branches
;
1410 active_branches
= b
;
1411 cur_active_branches
++;
1412 branch_load_count
++;
1415 static void file_change_m(struct branch
*b
)
1417 const char *p
= command_buf
.buf
+ 2;
1420 struct object_entry
*oe
;
1421 unsigned char sha1
[20];
1425 p
= get_mode(p
, &mode
);
1427 die("Corrupt mode: %s", command_buf
.buf
);
1429 case S_IFREG
| 0644:
1430 case S_IFREG
| 0755:
1437 die("Corrupt mode: %s", command_buf
.buf
);
1442 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1443 hashcpy(sha1
, oe
->sha1
);
1446 if (get_sha1_hex(p
, sha1
))
1447 die("Invalid SHA1: %s", command_buf
.buf
);
1448 oe
= find_object(sha1
);
1452 die("Missing space after SHA1: %s", command_buf
.buf
);
1454 p_uq
= unquote_c_style(p
, &endp
);
1457 die("Garbage after path in: %s", command_buf
.buf
);
1462 if (oe
->type
!= OBJ_BLOB
)
1463 die("Not a blob (actually a %s): %s",
1464 command_buf
.buf
, type_names
[oe
->type
]);
1466 if (sha1_object_info(sha1
, type
, NULL
))
1467 die("Blob not found: %s", command_buf
.buf
);
1468 if (strcmp(blob_type
, type
))
1469 die("Not a blob (actually a %s): %s",
1470 command_buf
.buf
, type
);
1473 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1479 static void file_change_d(struct branch
*b
)
1481 const char *p
= command_buf
.buf
+ 2;
1485 p_uq
= unquote_c_style(p
, &endp
);
1488 die("Garbage after path in: %s", command_buf
.buf
);
1491 tree_content_remove(&b
->branch_tree
, p
);
1496 static void cmd_from(struct branch
*b
)
1498 const char *from
, *endp
;
1502 if (strncmp("from ", command_buf
.buf
, 5))
1506 die("Can't reinitailize branch %s", b
->name
);
1508 from
= strchr(command_buf
.buf
, ' ') + 1;
1509 str_uq
= unquote_c_style(from
, &endp
);
1512 die("Garbage after string in: %s", command_buf
.buf
);
1516 s
= lookup_branch(from
);
1518 die("Can't create a branch from itself: %s", b
->name
);
1520 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1521 hashcpy(b
->sha1
, s
->sha1
);
1522 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1523 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1524 } else if (*from
== ':') {
1525 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1526 struct object_entry
*oe
= find_mark(idnum
);
1529 if (oe
->type
!= OBJ_COMMIT
)
1530 die("Mark :%ju not a commit", idnum
);
1531 hashcpy(b
->sha1
, oe
->sha1
);
1532 buf
= gfi_unpack_entry(oe
, &size
);
1533 if (!buf
|| size
< 46)
1534 die("Not a valid commit: %s", from
);
1535 if (memcmp("tree ", buf
, 5)
1536 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1537 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1539 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1540 b
->branch_tree
.versions
[1].sha1
);
1541 } else if (!get_sha1(from
, b
->sha1
)) {
1542 if (is_null_sha1(b
->sha1
)) {
1543 hashclr(b
->branch_tree
.versions
[0].sha1
);
1544 hashclr(b
->branch_tree
.versions
[1].sha1
);
1549 buf
= read_object_with_reference(b
->sha1
,
1550 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1551 if (!buf
|| size
< 46)
1552 die("Not a valid commit: %s", from
);
1553 if (memcmp("tree ", buf
, 5)
1554 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1555 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1557 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1558 b
->branch_tree
.versions
[1].sha1
);
1561 die("Invalid ref name or SHA1 expression: %s", from
);
1563 read_next_command();
1566 static struct hash_list
* cmd_merge(unsigned int *count
)
1568 struct hash_list
*list
= NULL
, *n
, *e
;
1569 const char *from
, *endp
;
1574 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1575 from
= strchr(command_buf
.buf
, ' ') + 1;
1576 str_uq
= unquote_c_style(from
, &endp
);
1579 die("Garbage after string in: %s", command_buf
.buf
);
1583 n
= xmalloc(sizeof(*n
));
1584 s
= lookup_branch(from
);
1586 hashcpy(n
->sha1
, s
->sha1
);
1587 else if (*from
== ':') {
1588 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1589 struct object_entry
*oe
= find_mark(idnum
);
1590 if (oe
->type
!= OBJ_COMMIT
)
1591 die("Mark :%ju not a commit", idnum
);
1592 hashcpy(n
->sha1
, oe
->sha1
);
1593 } else if (get_sha1(from
, n
->sha1
))
1594 die("Invalid ref name or SHA1 expression: %s", from
);
1603 read_next_command();
1608 static void cmd_new_commit(void)
1616 char *author
= NULL
;
1617 char *committer
= NULL
;
1618 struct hash_list
*merge_list
= NULL
;
1619 unsigned int merge_count
;
1621 /* Obtain the branch name from the rest of our command */
1622 sp
= strchr(command_buf
.buf
, ' ') + 1;
1623 str_uq
= unquote_c_style(sp
, &endp
);
1626 die("Garbage after ref in: %s", command_buf
.buf
);
1629 b
= lookup_branch(sp
);
1635 read_next_command();
1637 if (!strncmp("author ", command_buf
.buf
, 7)) {
1638 author
= strdup(command_buf
.buf
);
1639 read_next_command();
1641 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1642 committer
= strdup(command_buf
.buf
);
1643 read_next_command();
1646 die("Expected committer but didn't get one");
1647 msg
= cmd_data(&msglen
);
1648 read_next_command();
1650 merge_list
= cmd_merge(&merge_count
);
1652 /* ensure the branch is active/loaded */
1653 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1654 unload_one_branch();
1660 if (1 == command_buf
.len
)
1662 else if (!strncmp("M ", command_buf
.buf
, 2))
1664 else if (!strncmp("D ", command_buf
.buf
, 2))
1667 die("Unsupported file_change: %s", command_buf
.buf
);
1668 read_next_command();
1671 /* build the tree and the commit */
1672 store_tree(&b
->branch_tree
);
1673 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1674 b
->branch_tree
.versions
[1].sha1
);
1675 size_dbuf(&new_data
, 97 + msglen
1678 ? strlen(author
) + strlen(committer
)
1679 : 2 * strlen(committer
)));
1680 sp
= new_data
.buffer
;
1681 sp
+= sprintf(sp
, "tree %s\n",
1682 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1683 if (!is_null_sha1(b
->sha1
))
1684 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1685 while (merge_list
) {
1686 struct hash_list
*next
= merge_list
->next
;
1687 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1692 sp
+= sprintf(sp
, "%s\n", author
);
1694 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1695 sp
+= sprintf(sp
, "%s\n\n", committer
);
1696 memcpy(sp
, msg
, msglen
);
1703 if (!store_object(OBJ_COMMIT
,
1704 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1705 NULL
, b
->sha1
, next_mark
))
1706 b
->pack_id
= pack_id
;
1707 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1710 int need_dq
= quote_c_style(b
->name
, NULL
, NULL
, 0);
1711 fprintf(branch_log
, "commit ");
1713 fputc('"', branch_log
);
1714 quote_c_style(b
->name
, NULL
, branch_log
, 0);
1715 fputc('"', branch_log
);
1717 fprintf(branch_log
, "%s", b
->name
);
1718 fprintf(branch_log
," :%ju %s\n",next_mark
,sha1_to_hex(b
->sha1
));
1722 static void cmd_new_tag(void)
1733 uintmax_t from_mark
= 0;
1734 unsigned char sha1
[20];
1736 /* Obtain the new tag name from the rest of our command */
1737 sp
= strchr(command_buf
.buf
, ' ') + 1;
1738 str_uq
= unquote_c_style(sp
, &endp
);
1741 die("Garbage after tag name in: %s", command_buf
.buf
);
1744 t
= pool_alloc(sizeof(struct tag
));
1746 t
->name
= pool_strdup(sp
);
1748 last_tag
->next_tag
= t
;
1754 read_next_command();
1757 if (strncmp("from ", command_buf
.buf
, 5))
1758 die("Expected from command, got %s", command_buf
.buf
);
1760 from
= strchr(command_buf
.buf
, ' ') + 1;
1761 str_uq
= unquote_c_style(from
, &endp
);
1764 die("Garbage after string in: %s", command_buf
.buf
);
1768 s
= lookup_branch(from
);
1770 hashcpy(sha1
, s
->sha1
);
1771 } else if (*from
== ':') {
1772 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1773 struct object_entry
*oe
= find_mark(from_mark
);
1774 if (oe
->type
!= OBJ_COMMIT
)
1775 die("Mark :%ju not a commit", from_mark
);
1776 hashcpy(sha1
, oe
->sha1
);
1777 } else if (!get_sha1(from
, sha1
)) {
1781 buf
= read_object_with_reference(sha1
,
1782 type_names
[OBJ_COMMIT
], &size
, sha1
);
1783 if (!buf
|| size
< 46)
1784 die("Not a valid commit: %s", from
);
1787 die("Invalid ref name or SHA1 expression: %s", from
);
1791 read_next_command();
1794 if (strncmp("tagger ", command_buf
.buf
, 7))
1795 die("Expected tagger command, got %s", command_buf
.buf
);
1796 tagger
= strdup(command_buf
.buf
);
1798 /* tag payload/message */
1799 read_next_command();
1800 msg
= cmd_data(&msglen
);
1802 /* build the tag object */
1803 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1804 sp
= new_data
.buffer
;
1805 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1806 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1807 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1808 sp
+= sprintf(sp
, "%s\n\n", tagger
);
1809 memcpy(sp
, msg
, msglen
);
1814 if (store_object(OBJ_TAG
, new_data
.buffer
,
1815 sp
- (char*)new_data
.buffer
,
1817 t
->pack_id
= MAX_PACK_ID
;
1819 t
->pack_id
= pack_id
;
1822 int need_dq
= quote_c_style(t
->name
, NULL
, NULL
, 0);
1823 fprintf(branch_log
, "tag ");
1825 fputc('"', branch_log
);
1826 quote_c_style(t
->name
, NULL
, branch_log
, 0);
1827 fputc('"', branch_log
);
1829 fprintf(branch_log
, "%s", t
->name
);
1830 fprintf(branch_log
," :%ju %s\n",from_mark
,sha1_to_hex(t
->sha1
));
1834 static void cmd_reset_branch(void)
1841 /* Obtain the branch name from the rest of our command */
1842 sp
= strchr(command_buf
.buf
, ' ') + 1;
1843 str_uq
= unquote_c_style(sp
, &endp
);
1846 die("Garbage after ref in: %s", command_buf
.buf
);
1849 b
= lookup_branch(sp
);
1852 if (b
->branch_tree
.tree
) {
1853 release_tree_content_recursive(b
->branch_tree
.tree
);
1854 b
->branch_tree
.tree
= NULL
;
1861 read_next_command();
1865 static void cmd_checkpoint(void)
1869 read_next_command();
1872 static const char fast_import_usage
[] =
1873 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1875 int main(int argc
, const char **argv
)
1878 uintmax_t est_obj_cnt
= object_entry_alloc
;
1879 uintmax_t total_count
, duplicate_count
;
1882 git_config(git_default_config
);
1884 for (i
= 1; i
< argc
; i
++) {
1885 const char *a
= argv
[i
];
1887 if (*a
!= '-' || !strcmp(a
, "--"))
1889 else if (!strncmp(a
, "--objects=", 10))
1890 est_obj_cnt
= strtoumax(a
+ 10, NULL
, 0);
1891 else if (!strncmp(a
, "--max-objects-per-pack=", 23))
1892 max_objects
= strtoumax(a
+ 23, NULL
, 0);
1893 else if (!strncmp(a
, "--max-pack-size=", 16))
1894 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1895 else if (!strncmp(a
, "--depth=", 8))
1896 max_depth
= strtoul(a
+ 8, NULL
, 0);
1897 else if (!strncmp(a
, "--active-branches=", 18))
1898 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1899 else if (!strncmp(a
, "--export-marks=", 15))
1901 else if (!strncmp(a
, "--branch-log=", 13)) {
1902 branch_log
= fopen(a
+ 13, "w");
1904 die("Can't create %s: %s", a
+ 13, strerror(errno
));
1907 die("unknown option %s", a
);
1910 usage(fast_import_usage
);
1912 alloc_objects(est_obj_cnt
);
1913 strbuf_init(&command_buf
);
1915 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1916 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1917 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1918 marks
= pool_calloc(1, sizeof(struct mark_set
));
1922 read_next_command();
1923 if (command_buf
.eof
)
1925 else if (!strcmp("blob", command_buf
.buf
))
1927 else if (!strncmp("commit ", command_buf
.buf
, 7))
1929 else if (!strncmp("tag ", command_buf
.buf
, 4))
1931 else if (!strncmp("reset ", command_buf
.buf
, 6))
1933 else if (!strcmp("checkpoint", command_buf
.buf
))
1936 die("Unsupported command: %s", command_buf
.buf
);
1948 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
1949 total_count
+= object_count_by_type
[i
];
1950 duplicate_count
= 0;
1951 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
1952 duplicate_count
+= duplicate_count_by_type
[i
];
1954 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1955 fprintf(stderr
, "---------------------------------------------------------------------\n");
1956 fprintf(stderr
, "Alloc'd objects: %10ju (%10ju overflow )\n", alloc_count
, alloc_count
- est_obj_cnt
);
1957 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
1958 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
]);
1959 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
]);
1960 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
]);
1961 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
]);
1962 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
1963 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
1964 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
1965 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
1966 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
1967 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
1968 fprintf(stderr
, "---------------------------------------------------------------------\n");
1970 fprintf(stderr
, "---------------------------------------------------------------------\n");
1971 fprintf(stderr
, "\n");