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"
116 struct object_entry
*next
;
117 unsigned long offset
;
118 unsigned type
: TYPE_BITS
;
119 unsigned pack_id
: 16;
120 unsigned char sha1
[20];
123 struct object_entry_pool
125 struct object_entry_pool
*next_pool
;
126 struct object_entry
*next_free
;
127 struct object_entry
*end
;
128 struct object_entry entries
[FLEX_ARRAY
]; /* more */
134 struct object_entry
*marked
[1024];
135 struct mark_set
*sets
[1024];
144 unsigned long offset
;
151 struct mem_pool
*next_pool
;
154 char space
[FLEX_ARRAY
]; /* more */
159 struct atom_str
*next_atom
;
160 unsigned int str_len
;
161 char str_dat
[FLEX_ARRAY
]; /* more */
167 struct tree_content
*tree
;
168 struct atom_str
* name
;
172 unsigned char sha1
[20];
178 unsigned int entry_capacity
; /* must match avail_tree_content */
179 unsigned int entry_count
;
180 unsigned int delta_depth
;
181 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
184 struct avail_tree_content
186 unsigned int entry_capacity
; /* must match tree_content */
187 struct avail_tree_content
*next_avail
;
192 struct branch
*table_next_branch
;
193 struct branch
*active_next_branch
;
195 struct tree_entry branch_tree
;
196 unsigned long last_commit
;
197 unsigned int pack_id
;
198 unsigned char sha1
[20];
203 struct tag
*next_tag
;
205 unsigned int pack_id
;
206 unsigned char sha1
[20];
217 struct hash_list
*next
;
218 unsigned char sha1
[20];
221 /* Configured limits on output */
222 static unsigned long max_depth
= 10;
223 static unsigned long max_packsize
= (1LL << 32) - 1;
224 static uintmax_t max_objects
= -1;
226 /* Stats and misc. counters */
227 static uintmax_t alloc_count
;
228 static uintmax_t marks_set_count
;
229 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
230 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
231 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
232 static unsigned long object_count
;
233 static unsigned long branch_count
;
234 static unsigned long branch_load_count
;
237 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
238 static size_t total_allocd
;
239 static struct mem_pool
*mem_pool
;
241 /* Atom management */
242 static unsigned int atom_table_sz
= 4451;
243 static unsigned int atom_cnt
;
244 static struct atom_str
**atom_table
;
246 /* The .pack file being generated */
247 static unsigned int pack_id
;
248 static struct packed_git
*pack_data
;
249 static struct packed_git
**all_packs
;
250 static unsigned long pack_size
;
252 /* Table of objects we've written. */
253 static unsigned int object_entry_alloc
= 5000;
254 static struct object_entry_pool
*blocks
;
255 static struct object_entry
*object_table
[1 << 16];
256 static struct mark_set
*marks
;
257 static const char* mark_file
;
260 static struct last_object last_blob
;
262 /* Tree management */
263 static unsigned int tree_entry_alloc
= 1000;
264 static void *avail_tree_entry
;
265 static unsigned int avail_tree_table_sz
= 100;
266 static struct avail_tree_content
**avail_tree_table
;
267 static struct dbuf old_tree
;
268 static struct dbuf new_tree
;
271 static unsigned long max_active_branches
= 5;
272 static unsigned long cur_active_branches
;
273 static unsigned long branch_table_sz
= 1039;
274 static struct branch
**branch_table
;
275 static struct branch
*active_branches
;
278 static struct tag
*first_tag
;
279 static struct tag
*last_tag
;
281 /* Input stream parsing */
282 static struct strbuf command_buf
;
283 static uintmax_t next_mark
;
284 static struct dbuf new_data
;
285 static FILE* branch_log
;
288 static void alloc_objects(unsigned int cnt
)
290 struct object_entry_pool
*b
;
292 b
= xmalloc(sizeof(struct object_entry_pool
)
293 + cnt
* sizeof(struct object_entry
));
294 b
->next_pool
= blocks
;
295 b
->next_free
= b
->entries
;
296 b
->end
= b
->entries
+ cnt
;
301 static struct object_entry
* new_object(unsigned char *sha1
)
303 struct object_entry
*e
;
305 if (blocks
->next_free
== blocks
->end
)
306 alloc_objects(object_entry_alloc
);
308 e
= blocks
->next_free
++;
309 hashcpy(e
->sha1
, sha1
);
313 static struct object_entry
* find_object(unsigned char *sha1
)
315 unsigned int h
= sha1
[0] << 8 | sha1
[1];
316 struct object_entry
*e
;
317 for (e
= object_table
[h
]; e
; e
= e
->next
)
318 if (!hashcmp(sha1
, e
->sha1
))
323 static struct object_entry
* insert_object(unsigned char *sha1
)
325 unsigned int h
= sha1
[0] << 8 | sha1
[1];
326 struct object_entry
*e
= object_table
[h
];
327 struct object_entry
*p
= NULL
;
330 if (!hashcmp(sha1
, e
->sha1
))
336 e
= new_object(sha1
);
346 static unsigned int hc_str(const char *s
, size_t len
)
354 static void* pool_alloc(size_t len
)
359 for (p
= mem_pool
; p
; p
= p
->next_pool
)
360 if ((p
->end
- p
->next_free
>= len
))
364 if (len
>= (mem_pool_alloc
/2)) {
368 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
369 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
370 p
->next_pool
= mem_pool
;
371 p
->next_free
= p
->space
;
372 p
->end
= p
->next_free
+ mem_pool_alloc
;
377 /* round out to a pointer alignment */
378 if (len
& (sizeof(void*) - 1))
379 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
384 static void* pool_calloc(size_t count
, size_t size
)
386 size_t len
= count
* size
;
387 void *r
= pool_alloc(len
);
392 static char* pool_strdup(const char *s
)
394 char *r
= pool_alloc(strlen(s
) + 1);
399 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
402 if (b
->capacity
>= maxlen
)
406 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
407 b
->buffer
= xmalloc(b
->capacity
);
410 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
412 struct mark_set
*s
= marks
;
413 while ((idnum
>> s
->shift
) >= 1024) {
414 s
= pool_calloc(1, sizeof(struct mark_set
));
415 s
->shift
= marks
->shift
+ 10;
416 s
->data
.sets
[0] = marks
;
420 uintmax_t i
= idnum
>> s
->shift
;
421 idnum
-= i
<< s
->shift
;
422 if (!s
->data
.sets
[i
]) {
423 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
424 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
428 if (!s
->data
.marked
[idnum
])
430 s
->data
.marked
[idnum
] = oe
;
433 static struct object_entry
* find_mark(uintmax_t idnum
)
435 uintmax_t orig_idnum
= idnum
;
436 struct mark_set
*s
= marks
;
437 struct object_entry
*oe
= NULL
;
438 if ((idnum
>> s
->shift
) < 1024) {
439 while (s
&& s
->shift
) {
440 uintmax_t i
= idnum
>> s
->shift
;
441 idnum
-= i
<< s
->shift
;
445 oe
= s
->data
.marked
[idnum
];
448 die("mark :%ju not declared", orig_idnum
);
452 static struct atom_str
* to_atom(const char *s
, size_t len
)
454 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
457 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
458 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
461 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
463 strncpy(c
->str_dat
, s
, len
);
465 c
->next_atom
= atom_table
[hc
];
471 static struct branch
* lookup_branch(const char *name
)
473 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
476 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
477 if (!strcmp(name
, b
->name
))
482 static struct branch
* new_branch(const char *name
)
484 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
485 struct branch
* b
= lookup_branch(name
);
488 die("Invalid attempt to create duplicate branch: %s", name
);
489 if (check_ref_format(name
))
490 die("Branch name doesn't conform to GIT standards: %s", name
);
492 b
= pool_calloc(1, sizeof(struct branch
));
493 b
->name
= pool_strdup(name
);
494 b
->table_next_branch
= branch_table
[hc
];
495 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
496 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
497 branch_table
[hc
] = b
;
502 static unsigned int hc_entries(unsigned int cnt
)
504 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
505 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
508 static struct tree_content
* new_tree_content(unsigned int cnt
)
510 struct avail_tree_content
*f
, *l
= NULL
;
511 struct tree_content
*t
;
512 unsigned int hc
= hc_entries(cnt
);
514 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
515 if (f
->entry_capacity
>= cnt
)
520 l
->next_avail
= f
->next_avail
;
522 avail_tree_table
[hc
] = f
->next_avail
;
524 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
525 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
526 f
->entry_capacity
= cnt
;
529 t
= (struct tree_content
*)f
;
535 static void release_tree_entry(struct tree_entry
*e
);
536 static void release_tree_content(struct tree_content
*t
)
538 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
539 unsigned int hc
= hc_entries(f
->entry_capacity
);
540 f
->next_avail
= avail_tree_table
[hc
];
541 avail_tree_table
[hc
] = f
;
544 static void release_tree_content_recursive(struct tree_content
*t
)
547 for (i
= 0; i
< t
->entry_count
; i
++)
548 release_tree_entry(t
->entries
[i
]);
549 release_tree_content(t
);
552 static struct tree_content
* grow_tree_content(
553 struct tree_content
*t
,
556 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
557 r
->entry_count
= t
->entry_count
;
558 r
->delta_depth
= t
->delta_depth
;
559 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
560 release_tree_content(t
);
564 static struct tree_entry
* new_tree_entry()
566 struct tree_entry
*e
;
568 if (!avail_tree_entry
) {
569 unsigned int n
= tree_entry_alloc
;
570 total_allocd
+= n
* sizeof(struct tree_entry
);
571 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
573 *((void**)e
) = e
+ 1;
579 e
= avail_tree_entry
;
580 avail_tree_entry
= *((void**)e
);
584 static void release_tree_entry(struct tree_entry
*e
)
587 release_tree_content_recursive(e
->tree
);
588 *((void**)e
) = avail_tree_entry
;
589 avail_tree_entry
= e
;
592 static void start_packfile()
594 static char tmpfile
[PATH_MAX
];
595 struct packed_git
*p
;
596 struct pack_header hdr
;
599 snprintf(tmpfile
, sizeof(tmpfile
),
600 "%s/pack_XXXXXX", get_object_directory());
601 pack_fd
= mkstemp(tmpfile
);
603 die("Can't create %s: %s", tmpfile
, strerror(errno
));
604 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
605 strcpy(p
->pack_name
, tmpfile
);
606 p
->pack_fd
= pack_fd
;
608 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
609 hdr
.hdr_version
= htonl(2);
611 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
614 pack_size
= sizeof(hdr
);
617 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
618 all_packs
[pack_id
] = p
;
621 static void fixup_header_footer()
623 int pack_fd
= pack_data
->pack_fd
;
629 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
630 die("Failed seeking to start: %s", strerror(errno
));
633 if (read_in_full(pack_fd
, hdr
, 8) != 8)
634 die("Unable to reread header of %s", pack_data
->pack_name
);
635 SHA1_Update(&c
, hdr
, 8);
637 cnt
= htonl(object_count
);
638 SHA1_Update(&c
, &cnt
, 4);
639 write_or_die(pack_fd
, &cnt
, 4);
641 buf
= xmalloc(128 * 1024);
643 size_t n
= xread(pack_fd
, buf
, 128 * 1024);
646 SHA1_Update(&c
, buf
, n
);
650 SHA1_Final(pack_data
->sha1
, &c
);
651 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
655 static int oecmp (const void *a_
, const void *b_
)
657 struct object_entry
*a
= *((struct object_entry
**)a_
);
658 struct object_entry
*b
= *((struct object_entry
**)b_
);
659 return hashcmp(a
->sha1
, b
->sha1
);
662 static char* create_index()
664 static char tmpfile
[PATH_MAX
];
667 struct object_entry
**idx
, **c
, **last
, *e
;
668 struct object_entry_pool
*o
;
669 unsigned int array
[256];
672 /* Build the sorted table of object IDs. */
673 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
675 for (o
= blocks
; o
; o
= o
->next_pool
)
676 for (e
= o
->next_free
; e
-- != o
->entries
;)
677 if (pack_id
== e
->pack_id
)
679 last
= idx
+ object_count
;
681 die("internal consistency error creating the index");
682 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
684 /* Generate the fan-out array. */
686 for (i
= 0; i
< 256; i
++) {
687 struct object_entry
**next
= c
;;
688 while (next
< last
) {
689 if ((*next
)->sha1
[0] != i
)
693 array
[i
] = htonl(next
- idx
);
697 snprintf(tmpfile
, sizeof(tmpfile
),
698 "%s/index_XXXXXX", get_object_directory());
699 idx_fd
= mkstemp(tmpfile
);
701 die("Can't create %s: %s", tmpfile
, strerror(errno
));
702 f
= sha1fd(idx_fd
, tmpfile
);
703 sha1write(f
, array
, 256 * sizeof(int));
705 for (c
= idx
; c
!= last
; c
++) {
706 unsigned int offset
= htonl((*c
)->offset
);
707 sha1write(f
, &offset
, 4);
708 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
709 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
711 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
712 sha1close(f
, NULL
, 1);
714 SHA1_Final(pack_data
->sha1
, &ctx
);
718 static char* keep_pack(char *curr_index_name
)
720 static char name
[PATH_MAX
];
721 static char *keep_msg
= "fast-import";
724 chmod(pack_data
->pack_name
, 0444);
725 chmod(curr_index_name
, 0444);
727 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
728 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
729 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
731 die("cannot create keep file");
732 write(keep_fd
, keep_msg
, strlen(keep_msg
));
735 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
736 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
737 if (move_temp_to_file(pack_data
->pack_name
, name
))
738 die("cannot store pack file");
740 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
741 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
742 if (move_temp_to_file(curr_index_name
, name
))
743 die("cannot store index file");
747 static void unkeep_all_packs()
749 static char name
[PATH_MAX
];
752 for (k
= 0; k
< pack_id
; k
++) {
753 struct packed_git
*p
= all_packs
[k
];
754 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
755 get_object_directory(), sha1_to_hex(p
->sha1
));
760 static void end_packfile()
762 struct packed_git
*old_p
= pack_data
, *new_p
;
770 fixup_header_footer();
771 idx_name
= keep_pack(create_index());
773 /* Register the packfile with core git's machinary. */
774 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
776 die("core git rejected index %s", idx_name
);
777 new_p
->windows
= old_p
->windows
;
778 all_packs
[pack_id
] = new_p
;
779 install_packed_git(new_p
);
781 /* Print the boundary */
782 fprintf(stdout
, "%s:", new_p
->pack_name
);
783 for (i
= 0; i
< branch_table_sz
; i
++) {
784 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
785 if (b
->pack_id
== pack_id
)
786 fprintf(stdout
, " %s", sha1_to_hex(b
->sha1
));
789 for (t
= first_tag
; t
; t
= t
->next_tag
) {
790 if (t
->pack_id
== pack_id
)
791 fprintf(stdout
, " %s", sha1_to_hex(t
->sha1
));
798 unlink(old_p
->pack_name
);
801 /* We can't carry a delta across packfiles. */
802 free(last_blob
.data
);
803 last_blob
.data
= NULL
;
805 last_blob
.offset
= 0;
809 static void checkpoint()
815 static size_t encode_header(
816 enum object_type type
,
823 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
824 die("bad type %d", type
);
826 c
= (type
<< 4) | (size
& 15);
838 static int store_object(
839 enum object_type type
,
842 struct last_object
*last
,
843 unsigned char *sha1out
,
847 struct object_entry
*e
;
848 unsigned char hdr
[96];
849 unsigned char sha1
[20];
850 unsigned long hdrlen
, deltalen
;
854 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
856 SHA1_Update(&c
, hdr
, hdrlen
);
857 SHA1_Update(&c
, dat
, datlen
);
858 SHA1_Final(sha1
, &c
);
860 hashcpy(sha1out
, sha1
);
862 e
= insert_object(sha1
);
864 insert_mark(mark
, e
);
866 duplicate_count_by_type
[type
]++;
870 if (last
&& last
->data
&& last
->depth
< max_depth
) {
871 delta
= diff_delta(last
->data
, last
->len
,
874 if (delta
&& deltalen
>= datlen
) {
881 memset(&s
, 0, sizeof(s
));
882 deflateInit(&s
, zlib_compression_level
);
885 s
.avail_in
= deltalen
;
890 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
891 s
.next_out
= out
= xmalloc(s
.avail_out
);
892 while (deflate(&s
, Z_FINISH
) == Z_OK
)
896 /* Determine if we should auto-checkpoint. */
897 if ((object_count
+ 1) > max_objects
898 || (object_count
+ 1) < object_count
899 || (pack_size
+ 60 + s
.total_out
) > max_packsize
900 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
902 /* This new object needs to *not* have the current pack_id. */
903 e
->pack_id
= pack_id
+ 1;
906 /* We cannot carry a delta into the new pack. */
911 memset(&s
, 0, sizeof(s
));
912 deflateInit(&s
, zlib_compression_level
);
915 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
916 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
917 while (deflate(&s
, Z_FINISH
) == Z_OK
)
924 e
->pack_id
= pack_id
;
925 e
->offset
= pack_size
;
927 object_count_by_type
[type
]++;
930 unsigned long ofs
= e
->offset
- last
->offset
;
931 unsigned pos
= sizeof(hdr
) - 1;
933 delta_count_by_type
[type
]++;
936 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
937 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
940 hdr
[pos
] = ofs
& 127;
942 hdr
[--pos
] = 128 | (--ofs
& 127);
943 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
944 pack_size
+= sizeof(hdr
) - pos
;
948 hdrlen
= encode_header(type
, datlen
, hdr
);
949 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
953 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
954 pack_size
+= s
.total_out
;
960 if (last
->data
&& !last
->no_free
)
963 last
->offset
= e
->offset
;
969 static void *gfi_unpack_entry(
970 struct object_entry
*oe
,
971 unsigned long *sizep
)
973 static char type
[20];
974 struct packed_git
*p
= all_packs
[oe
->pack_id
];
976 p
->pack_size
= pack_size
+ 20;
977 return unpack_entry(p
, oe
->offset
, type
, sizep
);
980 static const char *get_mode(const char *str
, unsigned int *modep
)
983 unsigned int mode
= 0;
985 while ((c
= *str
++) != ' ') {
986 if (c
< '0' || c
> '7')
988 mode
= (mode
<< 3) + (c
- '0');
994 static void load_tree(struct tree_entry
*root
)
996 unsigned char* sha1
= root
->versions
[1].sha1
;
997 struct object_entry
*myoe
;
998 struct tree_content
*t
;
1003 root
->tree
= t
= new_tree_content(8);
1004 if (is_null_sha1(sha1
))
1007 myoe
= find_object(sha1
);
1009 if (myoe
->type
!= OBJ_TREE
)
1010 die("Not a tree: %s", sha1_to_hex(sha1
));
1012 buf
= gfi_unpack_entry(myoe
, &size
);
1015 buf
= read_sha1_file(sha1
, type
, &size
);
1016 if (!buf
|| strcmp(type
, tree_type
))
1017 die("Can't load tree %s", sha1_to_hex(sha1
));
1021 while (c
!= (buf
+ size
)) {
1022 struct tree_entry
*e
= new_tree_entry();
1024 if (t
->entry_count
== t
->entry_capacity
)
1025 root
->tree
= t
= grow_tree_content(t
, 8);
1026 t
->entries
[t
->entry_count
++] = e
;
1029 c
= get_mode(c
, &e
->versions
[1].mode
);
1031 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1032 e
->versions
[0].mode
= e
->versions
[1].mode
;
1033 e
->name
= to_atom(c
, strlen(c
));
1034 c
+= e
->name
->str_len
+ 1;
1035 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1036 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1042 static int tecmp0 (const void *_a
, const void *_b
)
1044 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1045 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1046 return base_name_compare(
1047 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1048 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1051 static int tecmp1 (const void *_a
, const void *_b
)
1053 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1054 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1055 return base_name_compare(
1056 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1057 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1060 static void mktree(struct tree_content
*t
,
1070 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1072 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1074 for (i
= 0; i
< t
->entry_count
; i
++) {
1075 if (t
->entries
[i
]->versions
[v
].mode
)
1076 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1079 size_dbuf(b
, maxlen
);
1081 for (i
= 0; i
< t
->entry_count
; i
++) {
1082 struct tree_entry
*e
= t
->entries
[i
];
1083 if (!e
->versions
[v
].mode
)
1085 c
+= sprintf(c
, "%o", e
->versions
[v
].mode
);
1087 strcpy(c
, e
->name
->str_dat
);
1088 c
+= e
->name
->str_len
+ 1;
1089 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1092 *szp
= c
- (char*)b
->buffer
;
1095 static void store_tree(struct tree_entry
*root
)
1097 struct tree_content
*t
= root
->tree
;
1098 unsigned int i
, j
, del
;
1099 unsigned long new_len
;
1100 struct last_object lo
;
1101 struct object_entry
*le
;
1103 if (!is_null_sha1(root
->versions
[1].sha1
))
1106 for (i
= 0; i
< t
->entry_count
; i
++) {
1107 if (t
->entries
[i
]->tree
)
1108 store_tree(t
->entries
[i
]);
1111 le
= find_object(root
->versions
[0].sha1
);
1112 if (!S_ISDIR(root
->versions
[0].mode
)
1114 || le
->pack_id
!= pack_id
) {
1118 mktree(t
, 0, &lo
.len
, &old_tree
);
1119 lo
.data
= old_tree
.buffer
;
1120 lo
.offset
= le
->offset
;
1121 lo
.depth
= t
->delta_depth
;
1125 mktree(t
, 1, &new_len
, &new_tree
);
1126 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1127 &lo
, root
->versions
[1].sha1
, 0);
1129 t
->delta_depth
= lo
.depth
;
1130 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1131 struct tree_entry
*e
= t
->entries
[i
];
1132 if (e
->versions
[1].mode
) {
1133 e
->versions
[0].mode
= e
->versions
[1].mode
;
1134 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1135 t
->entries
[j
++] = e
;
1137 release_tree_entry(e
);
1141 t
->entry_count
-= del
;
1144 static int tree_content_set(
1145 struct tree_entry
*root
,
1147 const unsigned char *sha1
,
1148 const unsigned int mode
)
1150 struct tree_content
*t
= root
->tree
;
1153 struct tree_entry
*e
;
1155 slash1
= strchr(p
, '/');
1161 for (i
= 0; i
< t
->entry_count
; i
++) {
1163 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1165 if (e
->versions
[1].mode
== mode
1166 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1168 e
->versions
[1].mode
= mode
;
1169 hashcpy(e
->versions
[1].sha1
, sha1
);
1171 release_tree_content_recursive(e
->tree
);
1174 hashclr(root
->versions
[1].sha1
);
1177 if (!S_ISDIR(e
->versions
[1].mode
)) {
1178 e
->tree
= new_tree_content(8);
1179 e
->versions
[1].mode
= S_IFDIR
;
1183 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1184 hashclr(root
->versions
[1].sha1
);
1191 if (t
->entry_count
== t
->entry_capacity
)
1192 root
->tree
= t
= grow_tree_content(t
, 8);
1193 e
= new_tree_entry();
1194 e
->name
= to_atom(p
, n
);
1195 e
->versions
[0].mode
= 0;
1196 hashclr(e
->versions
[0].sha1
);
1197 t
->entries
[t
->entry_count
++] = e
;
1199 e
->tree
= new_tree_content(8);
1200 e
->versions
[1].mode
= S_IFDIR
;
1201 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1204 e
->versions
[1].mode
= mode
;
1205 hashcpy(e
->versions
[1].sha1
, sha1
);
1207 hashclr(root
->versions
[1].sha1
);
1211 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1213 struct tree_content
*t
= root
->tree
;
1216 struct tree_entry
*e
;
1218 slash1
= strchr(p
, '/');
1224 for (i
= 0; i
< t
->entry_count
; i
++) {
1226 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1227 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1231 if (tree_content_remove(e
, slash1
+ 1)) {
1232 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1233 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1234 hashclr(root
->versions
[1].sha1
);
1247 release_tree_content_recursive(e
->tree
);
1250 e
->versions
[1].mode
= 0;
1251 hashclr(e
->versions
[1].sha1
);
1252 hashclr(root
->versions
[1].sha1
);
1256 static void dump_branches()
1258 static const char *msg
= "fast-import";
1261 struct ref_lock
*lock
;
1263 for (i
= 0; i
< branch_table_sz
; i
++) {
1264 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
1265 lock
= lock_any_ref_for_update(b
->name
, NULL
);
1266 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1267 die("Can't write %s", b
->name
);
1272 static void dump_tags()
1274 static const char *msg
= "fast-import";
1276 struct ref_lock
*lock
;
1277 char path
[PATH_MAX
];
1279 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1280 sprintf(path
, "refs/tags/%s", t
->name
);
1281 lock
= lock_any_ref_for_update(path
, NULL
);
1282 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1283 die("Can't write %s", path
);
1287 static void dump_marks_helper(FILE *f
,
1293 for (k
= 0; k
< 1024; k
++) {
1294 if (m
->data
.sets
[k
])
1295 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1299 for (k
= 0; k
< 1024; k
++) {
1300 if (m
->data
.marked
[k
])
1301 fprintf(f
, ":%ju %s\n", base
+ k
,
1302 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1307 static void dump_marks()
1311 FILE *f
= fopen(mark_file
, "w");
1312 dump_marks_helper(f
, 0, marks
);
1317 static void read_next_command()
1319 read_line(&command_buf
, stdin
, '\n');
1322 static void cmd_mark()
1324 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1325 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1326 read_next_command();
1332 static void* cmd_data (size_t *size
)
1338 if (strncmp("data ", command_buf
.buf
, 5))
1339 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1341 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1342 buffer
= xmalloc(length
);
1344 while (n
< length
) {
1345 size_t s
= fread((char*)buffer
+ n
, 1, length
- n
, stdin
);
1346 if (!s
&& feof(stdin
))
1347 die("EOF in data (%lu bytes remaining)", length
- n
);
1351 if (fgetc(stdin
) != '\n')
1352 die("An lf did not trail the binary data as expected.");
1358 static void cmd_new_blob()
1363 read_next_command();
1367 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1371 static void unload_one_branch()
1373 while (cur_active_branches
1374 && cur_active_branches
>= max_active_branches
) {
1375 unsigned long min_commit
= ULONG_MAX
;
1376 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1378 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1379 if (e
->last_commit
< min_commit
) {
1381 min_commit
= e
->last_commit
;
1387 e
= p
->active_next_branch
;
1388 p
->active_next_branch
= e
->active_next_branch
;
1390 e
= active_branches
;
1391 active_branches
= e
->active_next_branch
;
1393 e
->active_next_branch
= NULL
;
1394 if (e
->branch_tree
.tree
) {
1395 release_tree_content_recursive(e
->branch_tree
.tree
);
1396 e
->branch_tree
.tree
= NULL
;
1398 cur_active_branches
--;
1402 static void load_branch(struct branch
*b
)
1404 load_tree(&b
->branch_tree
);
1405 b
->active_next_branch
= active_branches
;
1406 active_branches
= b
;
1407 cur_active_branches
++;
1408 branch_load_count
++;
1411 static void file_change_m(struct branch
*b
)
1413 const char *p
= command_buf
.buf
+ 2;
1416 struct object_entry
*oe
;
1417 unsigned char sha1
[20];
1421 p
= get_mode(p
, &mode
);
1423 die("Corrupt mode: %s", command_buf
.buf
);
1425 case S_IFREG
| 0644:
1426 case S_IFREG
| 0755:
1433 die("Corrupt mode: %s", command_buf
.buf
);
1438 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1439 hashcpy(sha1
, oe
->sha1
);
1442 if (get_sha1_hex(p
, sha1
))
1443 die("Invalid SHA1: %s", command_buf
.buf
);
1444 oe
= find_object(sha1
);
1448 die("Missing space after SHA1: %s", command_buf
.buf
);
1450 p_uq
= unquote_c_style(p
, &endp
);
1453 die("Garbage after path in: %s", command_buf
.buf
);
1458 if (oe
->type
!= OBJ_BLOB
)
1459 die("Not a blob (actually a %s): %s",
1460 command_buf
.buf
, type_names
[oe
->type
]);
1462 if (sha1_object_info(sha1
, type
, NULL
))
1463 die("Blob not found: %s", command_buf
.buf
);
1464 if (strcmp(blob_type
, type
))
1465 die("Not a blob (actually a %s): %s",
1466 command_buf
.buf
, type
);
1469 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1475 static void file_change_d(struct branch
*b
)
1477 const char *p
= command_buf
.buf
+ 2;
1481 p_uq
= unquote_c_style(p
, &endp
);
1484 die("Garbage after path in: %s", command_buf
.buf
);
1487 tree_content_remove(&b
->branch_tree
, p
);
1492 static void cmd_from(struct branch
*b
)
1494 const char *from
, *endp
;
1498 if (strncmp("from ", command_buf
.buf
, 5))
1502 die("Can't reinitailize branch %s", b
->name
);
1504 from
= strchr(command_buf
.buf
, ' ') + 1;
1505 str_uq
= unquote_c_style(from
, &endp
);
1508 die("Garbage after string in: %s", command_buf
.buf
);
1512 s
= lookup_branch(from
);
1514 die("Can't create a branch from itself: %s", b
->name
);
1516 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1517 hashcpy(b
->sha1
, s
->sha1
);
1518 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1519 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1520 } else if (*from
== ':') {
1521 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1522 struct object_entry
*oe
= find_mark(idnum
);
1525 if (oe
->type
!= OBJ_COMMIT
)
1526 die("Mark :%ju not a commit", idnum
);
1527 hashcpy(b
->sha1
, oe
->sha1
);
1528 buf
= gfi_unpack_entry(oe
, &size
);
1529 if (!buf
|| size
< 46)
1530 die("Not a valid commit: %s", from
);
1531 if (memcmp("tree ", buf
, 5)
1532 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1533 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1535 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1536 b
->branch_tree
.versions
[1].sha1
);
1537 } else if (!get_sha1(from
, b
->sha1
)) {
1538 if (is_null_sha1(b
->sha1
)) {
1539 hashclr(b
->branch_tree
.versions
[0].sha1
);
1540 hashclr(b
->branch_tree
.versions
[1].sha1
);
1545 buf
= read_object_with_reference(b
->sha1
,
1546 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1547 if (!buf
|| size
< 46)
1548 die("Not a valid commit: %s", from
);
1549 if (memcmp("tree ", buf
, 5)
1550 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1551 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1553 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1554 b
->branch_tree
.versions
[1].sha1
);
1557 die("Invalid ref name or SHA1 expression: %s", from
);
1559 read_next_command();
1562 static struct hash_list
* cmd_merge(unsigned int *count
)
1564 struct hash_list
*list
= NULL
, *n
, *e
;
1565 const char *from
, *endp
;
1570 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1571 from
= strchr(command_buf
.buf
, ' ') + 1;
1572 str_uq
= unquote_c_style(from
, &endp
);
1575 die("Garbage after string in: %s", command_buf
.buf
);
1579 n
= xmalloc(sizeof(*n
));
1580 s
= lookup_branch(from
);
1582 hashcpy(n
->sha1
, s
->sha1
);
1583 else if (*from
== ':') {
1584 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1585 struct object_entry
*oe
= find_mark(idnum
);
1586 if (oe
->type
!= OBJ_COMMIT
)
1587 die("Mark :%ju not a commit", idnum
);
1588 hashcpy(n
->sha1
, oe
->sha1
);
1589 } else if (get_sha1(from
, n
->sha1
))
1590 die("Invalid ref name or SHA1 expression: %s", from
);
1599 read_next_command();
1604 static void cmd_new_commit()
1612 char *author
= NULL
;
1613 char *committer
= NULL
;
1614 struct hash_list
*merge_list
= NULL
;
1615 unsigned int merge_count
;
1617 /* Obtain the branch name from the rest of our command */
1618 sp
= strchr(command_buf
.buf
, ' ') + 1;
1619 str_uq
= unquote_c_style(sp
, &endp
);
1622 die("Garbage after ref in: %s", command_buf
.buf
);
1625 b
= lookup_branch(sp
);
1631 read_next_command();
1633 if (!strncmp("author ", command_buf
.buf
, 7)) {
1634 author
= strdup(command_buf
.buf
);
1635 read_next_command();
1637 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1638 committer
= strdup(command_buf
.buf
);
1639 read_next_command();
1642 die("Expected committer but didn't get one");
1643 msg
= cmd_data(&msglen
);
1644 read_next_command();
1646 merge_list
= cmd_merge(&merge_count
);
1648 /* ensure the branch is active/loaded */
1649 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1650 unload_one_branch();
1656 if (1 == command_buf
.len
)
1658 else if (!strncmp("M ", command_buf
.buf
, 2))
1660 else if (!strncmp("D ", command_buf
.buf
, 2))
1663 die("Unsupported file_change: %s", command_buf
.buf
);
1664 read_next_command();
1667 /* build the tree and the commit */
1668 store_tree(&b
->branch_tree
);
1669 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1670 b
->branch_tree
.versions
[1].sha1
);
1671 size_dbuf(&new_data
, 97 + msglen
1674 ? strlen(author
) + strlen(committer
)
1675 : 2 * strlen(committer
)));
1676 sp
= new_data
.buffer
;
1677 sp
+= sprintf(sp
, "tree %s\n",
1678 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1679 if (!is_null_sha1(b
->sha1
))
1680 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1681 while (merge_list
) {
1682 struct hash_list
*next
= merge_list
->next
;
1683 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1688 sp
+= sprintf(sp
, "%s\n", author
);
1690 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1691 sp
+= sprintf(sp
, "%s\n\n", committer
);
1692 memcpy(sp
, msg
, msglen
);
1699 store_object(OBJ_COMMIT
,
1700 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1701 NULL
, b
->sha1
, next_mark
);
1702 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1703 b
->pack_id
= pack_id
;
1706 int need_dq
= quote_c_style(b
->name
, NULL
, NULL
, 0);
1707 fprintf(branch_log
, "commit ");
1709 fputc('"', branch_log
);
1710 quote_c_style(b
->name
, NULL
, branch_log
, 0);
1711 fputc('"', branch_log
);
1713 fprintf(branch_log
, "%s", b
->name
);
1714 fprintf(branch_log
," :%ju %s\n",next_mark
,sha1_to_hex(b
->sha1
));
1718 static void cmd_new_tag()
1729 uintmax_t from_mark
= 0;
1730 unsigned char sha1
[20];
1732 /* Obtain the new tag name from the rest of our command */
1733 sp
= strchr(command_buf
.buf
, ' ') + 1;
1734 str_uq
= unquote_c_style(sp
, &endp
);
1737 die("Garbage after tag name in: %s", command_buf
.buf
);
1740 t
= pool_alloc(sizeof(struct tag
));
1742 t
->name
= pool_strdup(sp
);
1744 last_tag
->next_tag
= t
;
1750 read_next_command();
1753 if (strncmp("from ", command_buf
.buf
, 5))
1754 die("Expected from command, got %s", command_buf
.buf
);
1756 from
= strchr(command_buf
.buf
, ' ') + 1;
1757 str_uq
= unquote_c_style(from
, &endp
);
1760 die("Garbage after string in: %s", command_buf
.buf
);
1764 s
= lookup_branch(from
);
1766 hashcpy(sha1
, s
->sha1
);
1767 } else if (*from
== ':') {
1768 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1769 struct object_entry
*oe
= find_mark(from_mark
);
1770 if (oe
->type
!= OBJ_COMMIT
)
1771 die("Mark :%ju not a commit", from_mark
);
1772 hashcpy(sha1
, oe
->sha1
);
1773 } else if (!get_sha1(from
, sha1
)) {
1777 buf
= read_object_with_reference(sha1
,
1778 type_names
[OBJ_COMMIT
], &size
, sha1
);
1779 if (!buf
|| size
< 46)
1780 die("Not a valid commit: %s", from
);
1783 die("Invalid ref name or SHA1 expression: %s", from
);
1787 read_next_command();
1790 if (strncmp("tagger ", command_buf
.buf
, 7))
1791 die("Expected tagger command, got %s", command_buf
.buf
);
1792 tagger
= strdup(command_buf
.buf
);
1794 /* tag payload/message */
1795 read_next_command();
1796 msg
= cmd_data(&msglen
);
1798 /* build the tag object */
1799 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1800 sp
= new_data
.buffer
;
1801 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1802 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1803 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1804 sp
+= sprintf(sp
, "%s\n\n", tagger
);
1805 memcpy(sp
, msg
, msglen
);
1810 store_object(OBJ_TAG
, new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1812 t
->pack_id
= pack_id
;
1815 int need_dq
= quote_c_style(t
->name
, NULL
, NULL
, 0);
1816 fprintf(branch_log
, "tag ");
1818 fputc('"', branch_log
);
1819 quote_c_style(t
->name
, NULL
, branch_log
, 0);
1820 fputc('"', branch_log
);
1822 fprintf(branch_log
, "%s", t
->name
);
1823 fprintf(branch_log
," :%ju %s\n",from_mark
,sha1_to_hex(t
->sha1
));
1827 static void cmd_reset_branch()
1834 /* Obtain the branch name from the rest of our command */
1835 sp
= strchr(command_buf
.buf
, ' ') + 1;
1836 str_uq
= unquote_c_style(sp
, &endp
);
1839 die("Garbage after ref in: %s", command_buf
.buf
);
1842 b
= lookup_branch(sp
);
1845 if (b
->branch_tree
.tree
) {
1846 release_tree_content_recursive(b
->branch_tree
.tree
);
1847 b
->branch_tree
.tree
= NULL
;
1854 read_next_command();
1858 static void cmd_checkpoint()
1862 read_next_command();
1865 static const char fast_import_usage
[] =
1866 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1868 int main(int argc
, const char **argv
)
1871 uintmax_t est_obj_cnt
= object_entry_alloc
;
1872 uintmax_t total_count
, duplicate_count
;
1875 git_config(git_default_config
);
1877 for (i
= 1; i
< argc
; i
++) {
1878 const char *a
= argv
[i
];
1880 if (*a
!= '-' || !strcmp(a
, "--"))
1882 else if (!strncmp(a
, "--objects=", 10))
1883 est_obj_cnt
= strtoumax(a
+ 10, NULL
, 0);
1884 else if (!strncmp(a
, "--max-objects-per-pack=", 23))
1885 max_objects
= strtoumax(a
+ 23, NULL
, 0);
1886 else if (!strncmp(a
, "--max-pack-size=", 16))
1887 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1888 else if (!strncmp(a
, "--depth=", 8))
1889 max_depth
= strtoul(a
+ 8, NULL
, 0);
1890 else if (!strncmp(a
, "--active-branches=", 18))
1891 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1892 else if (!strncmp(a
, "--export-marks=", 15))
1894 else if (!strncmp(a
, "--branch-log=", 13)) {
1895 branch_log
= fopen(a
+ 13, "w");
1897 die("Can't create %s: %s", a
+ 13, strerror(errno
));
1900 die("unknown option %s", a
);
1903 usage(fast_import_usage
);
1905 alloc_objects(est_obj_cnt
);
1906 strbuf_init(&command_buf
);
1908 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1909 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1910 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1911 marks
= pool_calloc(1, sizeof(struct mark_set
));
1915 read_next_command();
1916 if (command_buf
.eof
)
1918 else if (!strcmp("blob", command_buf
.buf
))
1920 else if (!strncmp("commit ", command_buf
.buf
, 7))
1922 else if (!strncmp("tag ", command_buf
.buf
, 4))
1924 else if (!strncmp("reset ", command_buf
.buf
, 6))
1926 else if (!strcmp("checkpoint", command_buf
.buf
))
1929 die("Unsupported command: %s", command_buf
.buf
);
1941 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
1942 total_count
+= object_count_by_type
[i
];
1943 duplicate_count
= 0;
1944 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
1945 duplicate_count
+= duplicate_count_by_type
[i
];
1947 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1948 fprintf(stderr
, "---------------------------------------------------------------------\n");
1949 fprintf(stderr
, "Alloc'd objects: %10ju (%10ju overflow )\n", alloc_count
, alloc_count
- est_obj_cnt
);
1950 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
1951 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
]);
1952 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
]);
1953 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
]);
1954 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
]);
1955 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
1956 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
1957 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
1958 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
1959 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
1960 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
1961 fprintf(stderr
, "---------------------------------------------------------------------\n");
1963 fprintf(stderr
, "---------------------------------------------------------------------\n");
1964 fprintf(stderr
, "\n");