2 Format of STDIN stream:
13 new_blob ::= 'blob' lf
16 file_content ::= data;
18 new_commit ::= 'commit' sp ref_str lf
20 ('author' sp name '<' email '>' ts tz lf)?
21 'committer' sp name '<' email '>' ts tz lf
23 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
24 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
29 file_change ::= file_del | file_obm | file_inm;
30 file_del ::= 'D' sp path_str lf;
31 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
32 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
35 new_tag ::= 'tag' sp tag_str lf
36 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
37 'tagger' sp name '<' email '>' ts tz lf
41 reset_branch ::= 'reset' sp ref_str lf
42 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
45 checkpoint ::= 'checkpoint' lf
48 # note: the first idnum in a stream should be 1 and subsequent
49 # idnums should not have gaps between values as this will cause
50 # the stream parser to reserve space for the gapped values. An
51 # idnum can be updated in the future to a new object by issuing
52 # a new mark directive with the old idnum.
54 mark ::= 'mark' sp idnum lf;
55 data ::= (delimited_data | exact_data)
58 # note: delim may be any string but must not contain lf.
59 # data_line may contain any data but must not be exactly
61 delimited_data ::= 'data' sp '<<' delim lf
65 # note: declen indicates the length of binary_data in bytes.
66 # declen does not include the lf preceeding the binary data.
68 exact_data ::= 'data' sp declen lf
71 # note: quoted strings are C-style quoting supporting \c for
72 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
73 # is the signed byte value in octal. Note that the only
74 # characters which must actually be escaped to protect the
75 # stream formatting is: \, " and LF. Otherwise these values
79 sha1exp_str ::= sha1exp;
81 path_str ::= path | '"' quoted(path) '"' ;
82 mode ::= '100644' | '644'
87 declen ::= # unsigned 32 bit value, ascii base10 notation;
88 bigint ::= # unsigned integer value, ascii base10 notation;
89 binary_data ::= # file content, not interpreted;
91 sp ::= # ASCII space character;
92 lf ::= # ASCII newline (LF) character;
94 # note: a colon (':') must precede the numerical value assigned to
95 # an idnum. This is to distinguish it from a ref or tag name as
96 # GIT does not permit ':' in ref or tag strings.
99 path ::= # GIT style file path, e.g. "a/b/c";
100 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
101 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
102 sha1exp ::= # Any valid GIT SHA1 expression;
103 hexsha1 ::= # SHA1 in hexadecimal format;
105 # note: name and email are UTF8 strings, however name must not
106 # contain '<' or lf and email must not contain any of the
107 # following: '<', '>', lf.
109 name ::= # valid GIT author/committer name;
110 email ::= # valid GIT author/committer email;
111 ts ::= # time since the epoch in seconds, ascii base10 notation;
112 tz ::= # GIT style timezone;
123 #include "csum-file.h"
127 #define PACK_ID_BITS 16
128 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
132 struct object_entry
*next
;
134 unsigned type
: TYPE_BITS
;
135 unsigned pack_id
: PACK_ID_BITS
;
136 unsigned char sha1
[20];
139 struct object_entry_pool
141 struct object_entry_pool
*next_pool
;
142 struct object_entry
*next_free
;
143 struct object_entry
*end
;
144 struct object_entry entries
[FLEX_ARRAY
]; /* more */
150 struct object_entry
*marked
[1024];
151 struct mark_set
*sets
[1024];
167 struct mem_pool
*next_pool
;
170 char space
[FLEX_ARRAY
]; /* more */
175 struct atom_str
*next_atom
;
176 unsigned short str_len
;
177 char str_dat
[FLEX_ARRAY
]; /* more */
183 struct tree_content
*tree
;
184 struct atom_str
* name
;
188 unsigned char sha1
[20];
194 unsigned int entry_capacity
; /* must match avail_tree_content */
195 unsigned int entry_count
;
196 unsigned int delta_depth
;
197 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
200 struct avail_tree_content
202 unsigned int entry_capacity
; /* must match tree_content */
203 struct avail_tree_content
*next_avail
;
208 struct branch
*table_next_branch
;
209 struct branch
*active_next_branch
;
211 struct tree_entry branch_tree
;
212 uintmax_t last_commit
;
213 unsigned int pack_id
;
214 unsigned char sha1
[20];
219 struct tag
*next_tag
;
221 unsigned int pack_id
;
222 unsigned char sha1
[20];
233 struct hash_list
*next
;
234 unsigned char sha1
[20];
237 /* Configured limits on output */
238 static unsigned long max_depth
= 10;
239 static unsigned long max_packsize
= (1LL << 32) - 1;
241 /* Stats and misc. counters */
242 static uintmax_t alloc_count
;
243 static uintmax_t marks_set_count
;
244 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
245 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
246 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
247 static unsigned long object_count
;
248 static unsigned long branch_count
;
249 static unsigned long branch_load_count
;
252 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
253 static size_t total_allocd
;
254 static struct mem_pool
*mem_pool
;
256 /* Atom management */
257 static unsigned int atom_table_sz
= 4451;
258 static unsigned int atom_cnt
;
259 static struct atom_str
**atom_table
;
261 /* The .pack file being generated */
262 static unsigned int pack_id
;
263 static struct packed_git
*pack_data
;
264 static struct packed_git
**all_packs
;
265 static unsigned long pack_size
;
267 /* Table of objects we've written. */
268 static unsigned int object_entry_alloc
= 5000;
269 static struct object_entry_pool
*blocks
;
270 static struct object_entry
*object_table
[1 << 16];
271 static struct mark_set
*marks
;
272 static const char* mark_file
;
275 static struct last_object last_blob
;
277 /* Tree management */
278 static unsigned int tree_entry_alloc
= 1000;
279 static void *avail_tree_entry
;
280 static unsigned int avail_tree_table_sz
= 100;
281 static struct avail_tree_content
**avail_tree_table
;
282 static struct dbuf old_tree
;
283 static struct dbuf new_tree
;
286 static unsigned long max_active_branches
= 5;
287 static unsigned long cur_active_branches
;
288 static unsigned long branch_table_sz
= 1039;
289 static struct branch
**branch_table
;
290 static struct branch
*active_branches
;
293 static struct tag
*first_tag
;
294 static struct tag
*last_tag
;
296 /* Input stream parsing */
297 static struct strbuf command_buf
;
298 static uintmax_t next_mark
;
299 static struct dbuf new_data
;
300 static FILE* branch_log
;
303 static void alloc_objects(unsigned int cnt
)
305 struct object_entry_pool
*b
;
307 b
= xmalloc(sizeof(struct object_entry_pool
)
308 + cnt
* sizeof(struct object_entry
));
309 b
->next_pool
= blocks
;
310 b
->next_free
= b
->entries
;
311 b
->end
= b
->entries
+ cnt
;
316 static struct object_entry
* new_object(unsigned char *sha1
)
318 struct object_entry
*e
;
320 if (blocks
->next_free
== blocks
->end
)
321 alloc_objects(object_entry_alloc
);
323 e
= blocks
->next_free
++;
324 hashcpy(e
->sha1
, sha1
);
328 static struct object_entry
* find_object(unsigned char *sha1
)
330 unsigned int h
= sha1
[0] << 8 | sha1
[1];
331 struct object_entry
*e
;
332 for (e
= object_table
[h
]; e
; e
= e
->next
)
333 if (!hashcmp(sha1
, e
->sha1
))
338 static struct object_entry
* insert_object(unsigned char *sha1
)
340 unsigned int h
= sha1
[0] << 8 | sha1
[1];
341 struct object_entry
*e
= object_table
[h
];
342 struct object_entry
*p
= NULL
;
345 if (!hashcmp(sha1
, e
->sha1
))
351 e
= new_object(sha1
);
361 static unsigned int hc_str(const char *s
, size_t len
)
369 static void* pool_alloc(size_t len
)
374 for (p
= mem_pool
; p
; p
= p
->next_pool
)
375 if ((p
->end
- p
->next_free
>= len
))
379 if (len
>= (mem_pool_alloc
/2)) {
383 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
384 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
385 p
->next_pool
= mem_pool
;
386 p
->next_free
= p
->space
;
387 p
->end
= p
->next_free
+ mem_pool_alloc
;
392 /* round out to a pointer alignment */
393 if (len
& (sizeof(void*) - 1))
394 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
399 static void* pool_calloc(size_t count
, size_t size
)
401 size_t len
= count
* size
;
402 void *r
= pool_alloc(len
);
407 static char* pool_strdup(const char *s
)
409 char *r
= pool_alloc(strlen(s
) + 1);
414 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
417 if (b
->capacity
>= maxlen
)
421 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
422 b
->buffer
= xmalloc(b
->capacity
);
425 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
427 struct mark_set
*s
= marks
;
428 while ((idnum
>> s
->shift
) >= 1024) {
429 s
= pool_calloc(1, sizeof(struct mark_set
));
430 s
->shift
= marks
->shift
+ 10;
431 s
->data
.sets
[0] = marks
;
435 uintmax_t i
= idnum
>> s
->shift
;
436 idnum
-= i
<< s
->shift
;
437 if (!s
->data
.sets
[i
]) {
438 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
439 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
443 if (!s
->data
.marked
[idnum
])
445 s
->data
.marked
[idnum
] = oe
;
448 static struct object_entry
* find_mark(uintmax_t idnum
)
450 uintmax_t orig_idnum
= idnum
;
451 struct mark_set
*s
= marks
;
452 struct object_entry
*oe
= NULL
;
453 if ((idnum
>> s
->shift
) < 1024) {
454 while (s
&& s
->shift
) {
455 uintmax_t i
= idnum
>> s
->shift
;
456 idnum
-= i
<< s
->shift
;
460 oe
= s
->data
.marked
[idnum
];
463 die("mark :%ju not declared", orig_idnum
);
467 static struct atom_str
* to_atom(const char *s
, unsigned short len
)
469 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
472 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
473 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
476 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
478 strncpy(c
->str_dat
, s
, len
);
480 c
->next_atom
= atom_table
[hc
];
486 static struct branch
* lookup_branch(const char *name
)
488 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
491 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
492 if (!strcmp(name
, b
->name
))
497 static struct branch
* new_branch(const char *name
)
499 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
500 struct branch
* b
= lookup_branch(name
);
503 die("Invalid attempt to create duplicate branch: %s", name
);
504 if (check_ref_format(name
))
505 die("Branch name doesn't conform to GIT standards: %s", name
);
507 b
= pool_calloc(1, sizeof(struct branch
));
508 b
->name
= pool_strdup(name
);
509 b
->table_next_branch
= branch_table
[hc
];
510 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
511 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
512 b
->pack_id
= MAX_PACK_ID
;
513 branch_table
[hc
] = b
;
518 static unsigned int hc_entries(unsigned int cnt
)
520 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
521 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
524 static struct tree_content
* new_tree_content(unsigned int cnt
)
526 struct avail_tree_content
*f
, *l
= NULL
;
527 struct tree_content
*t
;
528 unsigned int hc
= hc_entries(cnt
);
530 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
531 if (f
->entry_capacity
>= cnt
)
536 l
->next_avail
= f
->next_avail
;
538 avail_tree_table
[hc
] = f
->next_avail
;
540 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
541 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
542 f
->entry_capacity
= cnt
;
545 t
= (struct tree_content
*)f
;
551 static void release_tree_entry(struct tree_entry
*e
);
552 static void release_tree_content(struct tree_content
*t
)
554 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
555 unsigned int hc
= hc_entries(f
->entry_capacity
);
556 f
->next_avail
= avail_tree_table
[hc
];
557 avail_tree_table
[hc
] = f
;
560 static void release_tree_content_recursive(struct tree_content
*t
)
563 for (i
= 0; i
< t
->entry_count
; i
++)
564 release_tree_entry(t
->entries
[i
]);
565 release_tree_content(t
);
568 static struct tree_content
* grow_tree_content(
569 struct tree_content
*t
,
572 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
573 r
->entry_count
= t
->entry_count
;
574 r
->delta_depth
= t
->delta_depth
;
575 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
576 release_tree_content(t
);
580 static struct tree_entry
* new_tree_entry(void)
582 struct tree_entry
*e
;
584 if (!avail_tree_entry
) {
585 unsigned int n
= tree_entry_alloc
;
586 total_allocd
+= n
* sizeof(struct tree_entry
);
587 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
589 *((void**)e
) = e
+ 1;
595 e
= avail_tree_entry
;
596 avail_tree_entry
= *((void**)e
);
600 static void release_tree_entry(struct tree_entry
*e
)
603 release_tree_content_recursive(e
->tree
);
604 *((void**)e
) = avail_tree_entry
;
605 avail_tree_entry
= e
;
608 static void start_packfile(void)
610 static char tmpfile
[PATH_MAX
];
611 struct packed_git
*p
;
612 struct pack_header hdr
;
615 snprintf(tmpfile
, sizeof(tmpfile
),
616 "%s/pack_XXXXXX", get_object_directory());
617 pack_fd
= mkstemp(tmpfile
);
619 die("Can't create %s: %s", tmpfile
, strerror(errno
));
620 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
621 strcpy(p
->pack_name
, tmpfile
);
622 p
->pack_fd
= pack_fd
;
624 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
625 hdr
.hdr_version
= htonl(2);
627 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
630 pack_size
= sizeof(hdr
);
633 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
634 all_packs
[pack_id
] = p
;
637 static void fixup_header_footer(void)
639 static const int buf_sz
= 128 * 1024;
640 int pack_fd
= pack_data
->pack_fd
;
642 struct pack_header hdr
;
645 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
646 die("Failed seeking to start: %s", strerror(errno
));
647 if (read_in_full(pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
648 die("Unable to reread header of %s", pack_data
->pack_name
);
649 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
650 die("Failed seeking to start: %s", strerror(errno
));
651 hdr
.hdr_entries
= htonl(object_count
);
652 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
655 SHA1_Update(&c
, &hdr
, sizeof(hdr
));
657 buf
= xmalloc(buf_sz
);
659 size_t n
= xread(pack_fd
, buf
, buf_sz
);
663 die("Failed to checksum %s", pack_data
->pack_name
);
664 SHA1_Update(&c
, buf
, n
);
668 SHA1_Final(pack_data
->sha1
, &c
);
669 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
673 static int oecmp (const void *a_
, const void *b_
)
675 struct object_entry
*a
= *((struct object_entry
**)a_
);
676 struct object_entry
*b
= *((struct object_entry
**)b_
);
677 return hashcmp(a
->sha1
, b
->sha1
);
680 static char* create_index(void)
682 static char tmpfile
[PATH_MAX
];
685 struct object_entry
**idx
, **c
, **last
, *e
;
686 struct object_entry_pool
*o
;
690 /* Build the sorted table of object IDs. */
691 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
693 for (o
= blocks
; o
; o
= o
->next_pool
)
694 for (e
= o
->next_free
; e
-- != o
->entries
;)
695 if (pack_id
== e
->pack_id
)
697 last
= idx
+ object_count
;
699 die("internal consistency error creating the index");
700 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
702 /* Generate the fan-out array. */
704 for (i
= 0; i
< 256; i
++) {
705 struct object_entry
**next
= c
;;
706 while (next
< last
) {
707 if ((*next
)->sha1
[0] != i
)
711 array
[i
] = htonl(next
- idx
);
715 snprintf(tmpfile
, sizeof(tmpfile
),
716 "%s/index_XXXXXX", get_object_directory());
717 idx_fd
= mkstemp(tmpfile
);
719 die("Can't create %s: %s", tmpfile
, strerror(errno
));
720 f
= sha1fd(idx_fd
, tmpfile
);
721 sha1write(f
, array
, 256 * sizeof(int));
723 for (c
= idx
; c
!= last
; c
++) {
724 uint32_t offset
= htonl((*c
)->offset
);
725 sha1write(f
, &offset
, 4);
726 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
727 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
729 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
730 sha1close(f
, NULL
, 1);
732 SHA1_Final(pack_data
->sha1
, &ctx
);
736 static char* keep_pack(char *curr_index_name
)
738 static char name
[PATH_MAX
];
739 static char *keep_msg
= "fast-import";
742 chmod(pack_data
->pack_name
, 0444);
743 chmod(curr_index_name
, 0444);
745 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
746 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
747 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
749 die("cannot create keep file");
750 write(keep_fd
, keep_msg
, strlen(keep_msg
));
753 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
754 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
755 if (move_temp_to_file(pack_data
->pack_name
, name
))
756 die("cannot store pack file");
758 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
759 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
760 if (move_temp_to_file(curr_index_name
, name
))
761 die("cannot store index file");
765 static void unkeep_all_packs(void)
767 static char name
[PATH_MAX
];
770 for (k
= 0; k
< pack_id
; k
++) {
771 struct packed_git
*p
= all_packs
[k
];
772 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
773 get_object_directory(), sha1_to_hex(p
->sha1
));
778 static void end_packfile(void)
780 struct packed_git
*old_p
= pack_data
, *new_p
;
788 fixup_header_footer();
789 idx_name
= keep_pack(create_index());
791 /* Register the packfile with core git's machinary. */
792 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
794 die("core git rejected index %s", idx_name
);
795 new_p
->windows
= old_p
->windows
;
796 all_packs
[pack_id
] = new_p
;
797 install_packed_git(new_p
);
799 /* Print the boundary */
800 fprintf(stdout
, "%s:", new_p
->pack_name
);
801 for (i
= 0; i
< branch_table_sz
; i
++) {
802 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
803 if (b
->pack_id
== pack_id
)
804 fprintf(stdout
, " %s", sha1_to_hex(b
->sha1
));
807 for (t
= first_tag
; t
; t
= t
->next_tag
) {
808 if (t
->pack_id
== pack_id
)
809 fprintf(stdout
, " %s", sha1_to_hex(t
->sha1
));
816 unlink(old_p
->pack_name
);
819 /* We can't carry a delta across packfiles. */
820 free(last_blob
.data
);
821 last_blob
.data
= NULL
;
823 last_blob
.offset
= 0;
827 static void checkpoint(void)
833 static size_t encode_header(
834 enum object_type type
,
841 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
842 die("bad type %d", type
);
844 c
= (type
<< 4) | (size
& 15);
856 static int store_object(
857 enum object_type type
,
860 struct last_object
*last
,
861 unsigned char *sha1out
,
865 struct object_entry
*e
;
866 unsigned char hdr
[96];
867 unsigned char sha1
[20];
868 unsigned long hdrlen
, deltalen
;
872 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
874 SHA1_Update(&c
, hdr
, hdrlen
);
875 SHA1_Update(&c
, dat
, datlen
);
876 SHA1_Final(sha1
, &c
);
878 hashcpy(sha1out
, sha1
);
880 e
= insert_object(sha1
);
882 insert_mark(mark
, e
);
884 duplicate_count_by_type
[type
]++;
888 if (last
&& last
->data
&& last
->depth
< max_depth
) {
889 delta
= diff_delta(last
->data
, last
->len
,
892 if (delta
&& deltalen
>= datlen
) {
899 memset(&s
, 0, sizeof(s
));
900 deflateInit(&s
, zlib_compression_level
);
903 s
.avail_in
= deltalen
;
908 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
909 s
.next_out
= out
= xmalloc(s
.avail_out
);
910 while (deflate(&s
, Z_FINISH
) == Z_OK
)
914 /* Determine if we should auto-checkpoint. */
915 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
916 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
918 /* This new object needs to *not* have the current pack_id. */
919 e
->pack_id
= pack_id
+ 1;
922 /* We cannot carry a delta into the new pack. */
927 memset(&s
, 0, sizeof(s
));
928 deflateInit(&s
, zlib_compression_level
);
931 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
932 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
933 while (deflate(&s
, Z_FINISH
) == Z_OK
)
940 e
->pack_id
= pack_id
;
941 e
->offset
= pack_size
;
943 object_count_by_type
[type
]++;
946 unsigned long ofs
= e
->offset
- last
->offset
;
947 unsigned pos
= sizeof(hdr
) - 1;
949 delta_count_by_type
[type
]++;
952 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
953 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
956 hdr
[pos
] = ofs
& 127;
958 hdr
[--pos
] = 128 | (--ofs
& 127);
959 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
960 pack_size
+= sizeof(hdr
) - pos
;
964 hdrlen
= encode_header(type
, datlen
, hdr
);
965 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
969 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
970 pack_size
+= s
.total_out
;
976 if (last
->data
&& !last
->no_free
)
979 last
->offset
= e
->offset
;
985 static void *gfi_unpack_entry(
986 struct object_entry
*oe
,
987 unsigned long *sizep
)
989 static char type
[20];
990 struct packed_git
*p
= all_packs
[oe
->pack_id
];
992 p
->pack_size
= pack_size
+ 20;
993 return unpack_entry(p
, oe
->offset
, type
, sizep
);
996 static const char *get_mode(const char *str
, uint16_t *modep
)
1001 while ((c
= *str
++) != ' ') {
1002 if (c
< '0' || c
> '7')
1004 mode
= (mode
<< 3) + (c
- '0');
1010 static void load_tree(struct tree_entry
*root
)
1012 unsigned char* sha1
= root
->versions
[1].sha1
;
1013 struct object_entry
*myoe
;
1014 struct tree_content
*t
;
1019 root
->tree
= t
= new_tree_content(8);
1020 if (is_null_sha1(sha1
))
1023 myoe
= find_object(sha1
);
1025 if (myoe
->type
!= OBJ_TREE
)
1026 die("Not a tree: %s", sha1_to_hex(sha1
));
1028 buf
= gfi_unpack_entry(myoe
, &size
);
1031 buf
= read_sha1_file(sha1
, type
, &size
);
1032 if (!buf
|| strcmp(type
, tree_type
))
1033 die("Can't load tree %s", sha1_to_hex(sha1
));
1037 while (c
!= (buf
+ size
)) {
1038 struct tree_entry
*e
= new_tree_entry();
1040 if (t
->entry_count
== t
->entry_capacity
)
1041 root
->tree
= t
= grow_tree_content(t
, 8);
1042 t
->entries
[t
->entry_count
++] = e
;
1045 c
= get_mode(c
, &e
->versions
[1].mode
);
1047 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1048 e
->versions
[0].mode
= e
->versions
[1].mode
;
1049 e
->name
= to_atom(c
, (unsigned short)strlen(c
));
1050 c
+= e
->name
->str_len
+ 1;
1051 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1052 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1058 static int tecmp0 (const void *_a
, const void *_b
)
1060 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1061 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1062 return base_name_compare(
1063 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1064 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1067 static int tecmp1 (const void *_a
, const void *_b
)
1069 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1070 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1071 return base_name_compare(
1072 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1073 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1076 static void mktree(struct tree_content
*t
,
1086 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1088 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1090 for (i
= 0; i
< t
->entry_count
; i
++) {
1091 if (t
->entries
[i
]->versions
[v
].mode
)
1092 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1095 size_dbuf(b
, maxlen
);
1097 for (i
= 0; i
< t
->entry_count
; i
++) {
1098 struct tree_entry
*e
= t
->entries
[i
];
1099 if (!e
->versions
[v
].mode
)
1101 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1103 strcpy(c
, e
->name
->str_dat
);
1104 c
+= e
->name
->str_len
+ 1;
1105 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1108 *szp
= c
- (char*)b
->buffer
;
1111 static void store_tree(struct tree_entry
*root
)
1113 struct tree_content
*t
= root
->tree
;
1114 unsigned int i
, j
, del
;
1115 unsigned long new_len
;
1116 struct last_object lo
;
1117 struct object_entry
*le
;
1119 if (!is_null_sha1(root
->versions
[1].sha1
))
1122 for (i
= 0; i
< t
->entry_count
; i
++) {
1123 if (t
->entries
[i
]->tree
)
1124 store_tree(t
->entries
[i
]);
1127 le
= find_object(root
->versions
[0].sha1
);
1128 if (!S_ISDIR(root
->versions
[0].mode
)
1130 || le
->pack_id
!= pack_id
) {
1134 mktree(t
, 0, &lo
.len
, &old_tree
);
1135 lo
.data
= old_tree
.buffer
;
1136 lo
.offset
= le
->offset
;
1137 lo
.depth
= t
->delta_depth
;
1141 mktree(t
, 1, &new_len
, &new_tree
);
1142 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1143 &lo
, root
->versions
[1].sha1
, 0);
1145 t
->delta_depth
= lo
.depth
;
1146 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1147 struct tree_entry
*e
= t
->entries
[i
];
1148 if (e
->versions
[1].mode
) {
1149 e
->versions
[0].mode
= e
->versions
[1].mode
;
1150 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1151 t
->entries
[j
++] = e
;
1153 release_tree_entry(e
);
1157 t
->entry_count
-= del
;
1160 static int tree_content_set(
1161 struct tree_entry
*root
,
1163 const unsigned char *sha1
,
1164 const uint16_t mode
)
1166 struct tree_content
*t
= root
->tree
;
1169 struct tree_entry
*e
;
1171 slash1
= strchr(p
, '/');
1177 for (i
= 0; i
< t
->entry_count
; i
++) {
1179 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1181 if (e
->versions
[1].mode
== mode
1182 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1184 e
->versions
[1].mode
= mode
;
1185 hashcpy(e
->versions
[1].sha1
, sha1
);
1187 release_tree_content_recursive(e
->tree
);
1190 hashclr(root
->versions
[1].sha1
);
1193 if (!S_ISDIR(e
->versions
[1].mode
)) {
1194 e
->tree
= new_tree_content(8);
1195 e
->versions
[1].mode
= S_IFDIR
;
1199 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1200 hashclr(root
->versions
[1].sha1
);
1207 if (t
->entry_count
== t
->entry_capacity
)
1208 root
->tree
= t
= grow_tree_content(t
, 8);
1209 e
= new_tree_entry();
1210 e
->name
= to_atom(p
, (unsigned short)n
);
1211 e
->versions
[0].mode
= 0;
1212 hashclr(e
->versions
[0].sha1
);
1213 t
->entries
[t
->entry_count
++] = e
;
1215 e
->tree
= new_tree_content(8);
1216 e
->versions
[1].mode
= S_IFDIR
;
1217 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1220 e
->versions
[1].mode
= mode
;
1221 hashcpy(e
->versions
[1].sha1
, sha1
);
1223 hashclr(root
->versions
[1].sha1
);
1227 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1229 struct tree_content
*t
= root
->tree
;
1232 struct tree_entry
*e
;
1234 slash1
= strchr(p
, '/');
1240 for (i
= 0; i
< t
->entry_count
; i
++) {
1242 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1243 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1247 if (tree_content_remove(e
, slash1
+ 1)) {
1248 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1249 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1250 hashclr(root
->versions
[1].sha1
);
1263 release_tree_content_recursive(e
->tree
);
1266 e
->versions
[1].mode
= 0;
1267 hashclr(e
->versions
[1].sha1
);
1268 hashclr(root
->versions
[1].sha1
);
1272 static void dump_branches(void)
1274 static const char *msg
= "fast-import";
1277 struct ref_lock
*lock
;
1279 for (i
= 0; i
< branch_table_sz
; i
++) {
1280 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
1281 lock
= lock_any_ref_for_update(b
->name
, NULL
);
1282 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1283 die("Can't write %s", b
->name
);
1288 static void dump_tags(void)
1290 static const char *msg
= "fast-import";
1292 struct ref_lock
*lock
;
1293 char path
[PATH_MAX
];
1295 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1296 sprintf(path
, "refs/tags/%s", t
->name
);
1297 lock
= lock_any_ref_for_update(path
, NULL
);
1298 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1299 die("Can't write %s", path
);
1303 static void dump_marks_helper(FILE *f
,
1309 for (k
= 0; k
< 1024; k
++) {
1310 if (m
->data
.sets
[k
])
1311 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1315 for (k
= 0; k
< 1024; k
++) {
1316 if (m
->data
.marked
[k
])
1317 fprintf(f
, ":%ju %s\n", base
+ k
,
1318 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1323 static void dump_marks(void)
1327 FILE *f
= fopen(mark_file
, "w");
1328 dump_marks_helper(f
, 0, marks
);
1333 static void read_next_command(void)
1335 read_line(&command_buf
, stdin
, '\n');
1338 static void cmd_mark(void)
1340 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1341 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1342 read_next_command();
1348 static void* cmd_data (size_t *size
)
1353 if (strncmp("data ", command_buf
.buf
, 5))
1354 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1356 if (!strncmp("<<", command_buf
.buf
+ 5, 2)) {
1357 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1358 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1360 buffer
= xmalloc(sz
);
1362 read_next_command();
1363 if (command_buf
.eof
)
1364 die("EOF in data (terminator '%s' not found)", term
);
1365 if (term_len
== command_buf
.len
1366 && !strcmp(term
, command_buf
.buf
))
1368 if (sz
< (length
+ command_buf
.len
)) {
1369 sz
= sz
* 3 / 2 + 16;
1370 if (sz
< (length
+ command_buf
.len
))
1371 sz
= length
+ command_buf
.len
;
1372 buffer
= xrealloc(buffer
, sz
);
1374 memcpy(buffer
+ length
,
1376 command_buf
.len
- 1);
1377 length
+= command_buf
.len
- 1;
1378 buffer
[length
++] = '\n';
1384 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1385 buffer
= xmalloc(length
);
1386 while (n
< length
) {
1387 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1388 if (!s
&& feof(stdin
))
1389 die("EOF in data (%lu bytes remaining)", length
- n
);
1394 if (fgetc(stdin
) != '\n')
1395 die("An lf did not trail the binary data as expected.");
1401 static void cmd_new_blob(void)
1406 read_next_command();
1410 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1414 static void unload_one_branch(void)
1416 while (cur_active_branches
1417 && cur_active_branches
>= max_active_branches
) {
1418 unsigned long min_commit
= ULONG_MAX
;
1419 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1421 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1422 if (e
->last_commit
< min_commit
) {
1424 min_commit
= e
->last_commit
;
1430 e
= p
->active_next_branch
;
1431 p
->active_next_branch
= e
->active_next_branch
;
1433 e
= active_branches
;
1434 active_branches
= e
->active_next_branch
;
1436 e
->active_next_branch
= NULL
;
1437 if (e
->branch_tree
.tree
) {
1438 release_tree_content_recursive(e
->branch_tree
.tree
);
1439 e
->branch_tree
.tree
= NULL
;
1441 cur_active_branches
--;
1445 static void load_branch(struct branch
*b
)
1447 load_tree(&b
->branch_tree
);
1448 b
->active_next_branch
= active_branches
;
1449 active_branches
= b
;
1450 cur_active_branches
++;
1451 branch_load_count
++;
1454 static void file_change_m(struct branch
*b
)
1456 const char *p
= command_buf
.buf
+ 2;
1459 struct object_entry
*oe
;
1460 unsigned char sha1
[20];
1461 uint16_t mode
, inline_data
= 0;
1464 p
= get_mode(p
, &mode
);
1466 die("Corrupt mode: %s", command_buf
.buf
);
1468 case S_IFREG
| 0644:
1469 case S_IFREG
| 0755:
1476 die("Corrupt mode: %s", command_buf
.buf
);
1481 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1482 hashcpy(sha1
, oe
->sha1
);
1484 } else if (!strncmp("inline", p
, 6)) {
1488 if (get_sha1_hex(p
, sha1
))
1489 die("Invalid SHA1: %s", command_buf
.buf
);
1490 oe
= find_object(sha1
);
1494 die("Missing space after SHA1: %s", command_buf
.buf
);
1496 p_uq
= unquote_c_style(p
, &endp
);
1499 die("Garbage after path in: %s", command_buf
.buf
);
1507 p
= p_uq
= xstrdup(p
);
1508 read_next_command();
1510 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1513 if (oe
->type
!= OBJ_BLOB
)
1514 die("Not a blob (actually a %s): %s",
1515 command_buf
.buf
, type_names
[oe
->type
]);
1517 if (sha1_object_info(sha1
, type
, NULL
))
1518 die("Blob not found: %s", command_buf
.buf
);
1519 if (strcmp(blob_type
, type
))
1520 die("Not a blob (actually a %s): %s",
1521 command_buf
.buf
, type
);
1524 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1530 static void file_change_d(struct branch
*b
)
1532 const char *p
= command_buf
.buf
+ 2;
1536 p_uq
= unquote_c_style(p
, &endp
);
1539 die("Garbage after path in: %s", command_buf
.buf
);
1542 tree_content_remove(&b
->branch_tree
, p
);
1547 static void cmd_from(struct branch
*b
)
1552 if (strncmp("from ", command_buf
.buf
, 5))
1556 die("Can't reinitailize branch %s", b
->name
);
1558 from
= strchr(command_buf
.buf
, ' ') + 1;
1559 s
= lookup_branch(from
);
1561 die("Can't create a branch from itself: %s", b
->name
);
1563 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1564 hashcpy(b
->sha1
, s
->sha1
);
1565 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1566 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1567 } else if (*from
== ':') {
1568 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1569 struct object_entry
*oe
= find_mark(idnum
);
1572 if (oe
->type
!= OBJ_COMMIT
)
1573 die("Mark :%ju not a commit", idnum
);
1574 hashcpy(b
->sha1
, oe
->sha1
);
1575 buf
= gfi_unpack_entry(oe
, &size
);
1576 if (!buf
|| size
< 46)
1577 die("Not a valid commit: %s", from
);
1578 if (memcmp("tree ", buf
, 5)
1579 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1580 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1582 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1583 b
->branch_tree
.versions
[1].sha1
);
1584 } else if (!get_sha1(from
, b
->sha1
)) {
1585 if (is_null_sha1(b
->sha1
)) {
1586 hashclr(b
->branch_tree
.versions
[0].sha1
);
1587 hashclr(b
->branch_tree
.versions
[1].sha1
);
1592 buf
= read_object_with_reference(b
->sha1
,
1593 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1594 if (!buf
|| size
< 46)
1595 die("Not a valid commit: %s", from
);
1596 if (memcmp("tree ", buf
, 5)
1597 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1598 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1600 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1601 b
->branch_tree
.versions
[1].sha1
);
1604 die("Invalid ref name or SHA1 expression: %s", from
);
1606 read_next_command();
1609 static struct hash_list
* cmd_merge(unsigned int *count
)
1611 struct hash_list
*list
= NULL
, *n
, *e
;
1616 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1617 from
= strchr(command_buf
.buf
, ' ') + 1;
1618 n
= xmalloc(sizeof(*n
));
1619 s
= lookup_branch(from
);
1621 hashcpy(n
->sha1
, s
->sha1
);
1622 else if (*from
== ':') {
1623 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1624 struct object_entry
*oe
= find_mark(idnum
);
1625 if (oe
->type
!= OBJ_COMMIT
)
1626 die("Mark :%ju not a commit", idnum
);
1627 hashcpy(n
->sha1
, oe
->sha1
);
1628 } else if (get_sha1(from
, n
->sha1
))
1629 die("Invalid ref name or SHA1 expression: %s", from
);
1638 read_next_command();
1643 static void cmd_new_commit(void)
1649 char *author
= NULL
;
1650 char *committer
= NULL
;
1651 struct hash_list
*merge_list
= NULL
;
1652 unsigned int merge_count
;
1654 /* Obtain the branch name from the rest of our command */
1655 sp
= strchr(command_buf
.buf
, ' ') + 1;
1656 b
= lookup_branch(sp
);
1660 read_next_command();
1662 if (!strncmp("author ", command_buf
.buf
, 7)) {
1663 author
= strdup(command_buf
.buf
);
1664 read_next_command();
1666 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1667 committer
= strdup(command_buf
.buf
);
1668 read_next_command();
1671 die("Expected committer but didn't get one");
1672 msg
= cmd_data(&msglen
);
1673 read_next_command();
1675 merge_list
= cmd_merge(&merge_count
);
1677 /* ensure the branch is active/loaded */
1678 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1679 unload_one_branch();
1685 if (1 == command_buf
.len
)
1687 else if (!strncmp("M ", command_buf
.buf
, 2))
1689 else if (!strncmp("D ", command_buf
.buf
, 2))
1692 die("Unsupported file_change: %s", command_buf
.buf
);
1693 read_next_command();
1696 /* build the tree and the commit */
1697 store_tree(&b
->branch_tree
);
1698 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1699 b
->branch_tree
.versions
[1].sha1
);
1700 size_dbuf(&new_data
, 97 + msglen
1703 ? strlen(author
) + strlen(committer
)
1704 : 2 * strlen(committer
)));
1705 sp
= new_data
.buffer
;
1706 sp
+= sprintf(sp
, "tree %s\n",
1707 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1708 if (!is_null_sha1(b
->sha1
))
1709 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1710 while (merge_list
) {
1711 struct hash_list
*next
= merge_list
->next
;
1712 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1717 sp
+= sprintf(sp
, "%s\n", author
);
1719 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1720 sp
+= sprintf(sp
, "%s\n\n", committer
);
1721 memcpy(sp
, msg
, msglen
);
1728 if (!store_object(OBJ_COMMIT
,
1729 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1730 NULL
, b
->sha1
, next_mark
))
1731 b
->pack_id
= pack_id
;
1732 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1735 int need_dq
= quote_c_style(b
->name
, NULL
, NULL
, 0);
1736 fprintf(branch_log
, "commit ");
1738 fputc('"', branch_log
);
1739 quote_c_style(b
->name
, NULL
, branch_log
, 0);
1740 fputc('"', branch_log
);
1742 fprintf(branch_log
, "%s", b
->name
);
1743 fprintf(branch_log
," :%ju %s\n",next_mark
,sha1_to_hex(b
->sha1
));
1747 static void cmd_new_tag(void)
1756 uintmax_t from_mark
= 0;
1757 unsigned char sha1
[20];
1759 /* Obtain the new tag name from the rest of our command */
1760 sp
= strchr(command_buf
.buf
, ' ') + 1;
1761 t
= pool_alloc(sizeof(struct tag
));
1763 t
->name
= pool_strdup(sp
);
1765 last_tag
->next_tag
= t
;
1769 read_next_command();
1772 if (strncmp("from ", command_buf
.buf
, 5))
1773 die("Expected from command, got %s", command_buf
.buf
);
1774 from
= strchr(command_buf
.buf
, ' ') + 1;
1775 s
= lookup_branch(from
);
1777 hashcpy(sha1
, s
->sha1
);
1778 } else if (*from
== ':') {
1779 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1780 struct object_entry
*oe
= find_mark(from_mark
);
1781 if (oe
->type
!= OBJ_COMMIT
)
1782 die("Mark :%ju not a commit", from_mark
);
1783 hashcpy(sha1
, oe
->sha1
);
1784 } else if (!get_sha1(from
, sha1
)) {
1788 buf
= read_object_with_reference(sha1
,
1789 type_names
[OBJ_COMMIT
], &size
, sha1
);
1790 if (!buf
|| size
< 46)
1791 die("Not a valid commit: %s", from
);
1794 die("Invalid ref name or SHA1 expression: %s", from
);
1795 read_next_command();
1798 if (strncmp("tagger ", command_buf
.buf
, 7))
1799 die("Expected tagger command, got %s", command_buf
.buf
);
1800 tagger
= strdup(command_buf
.buf
);
1802 /* tag payload/message */
1803 read_next_command();
1804 msg
= cmd_data(&msglen
);
1806 /* build the tag object */
1807 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1808 sp
= new_data
.buffer
;
1809 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1810 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1811 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1812 sp
+= sprintf(sp
, "%s\n\n", tagger
);
1813 memcpy(sp
, msg
, msglen
);
1818 if (store_object(OBJ_TAG
, new_data
.buffer
,
1819 sp
- (char*)new_data
.buffer
,
1821 t
->pack_id
= MAX_PACK_ID
;
1823 t
->pack_id
= pack_id
;
1826 int need_dq
= quote_c_style(t
->name
, NULL
, NULL
, 0);
1827 fprintf(branch_log
, "tag ");
1829 fputc('"', branch_log
);
1830 quote_c_style(t
->name
, NULL
, branch_log
, 0);
1831 fputc('"', branch_log
);
1833 fprintf(branch_log
, "%s", t
->name
);
1834 fprintf(branch_log
," :%ju %s\n",from_mark
,sha1_to_hex(t
->sha1
));
1838 static void cmd_reset_branch(void)
1843 /* Obtain the branch name from the rest of our command */
1844 sp
= strchr(command_buf
.buf
, ' ') + 1;
1845 b
= lookup_branch(sp
);
1848 if (b
->branch_tree
.tree
) {
1849 release_tree_content_recursive(b
->branch_tree
.tree
);
1850 b
->branch_tree
.tree
= NULL
;
1855 read_next_command();
1859 static void cmd_checkpoint(void)
1863 read_next_command();
1866 static const char fast_import_usage
[] =
1867 "git-fast-import [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1869 int main(int argc
, const char **argv
)
1872 uintmax_t total_count
, duplicate_count
;
1874 git_config(git_default_config
);
1876 for (i
= 1; i
< argc
; i
++) {
1877 const char *a
= argv
[i
];
1879 if (*a
!= '-' || !strcmp(a
, "--"))
1881 else if (!strncmp(a
, "--max-pack-size=", 16))
1882 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1883 else if (!strncmp(a
, "--depth=", 8))
1884 max_depth
= strtoul(a
+ 8, NULL
, 0);
1885 else if (!strncmp(a
, "--active-branches=", 18))
1886 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1887 else if (!strncmp(a
, "--export-marks=", 15))
1889 else if (!strncmp(a
, "--branch-log=", 13)) {
1890 branch_log
= fopen(a
+ 13, "w");
1892 die("Can't create %s: %s", a
+ 13, strerror(errno
));
1895 die("unknown option %s", a
);
1898 usage(fast_import_usage
);
1900 alloc_objects(object_entry_alloc
);
1901 strbuf_init(&command_buf
);
1903 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1904 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1905 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1906 marks
= pool_calloc(1, sizeof(struct mark_set
));
1910 read_next_command();
1911 if (command_buf
.eof
)
1913 else if (!strcmp("blob", command_buf
.buf
))
1915 else if (!strncmp("commit ", command_buf
.buf
, 7))
1917 else if (!strncmp("tag ", command_buf
.buf
, 4))
1919 else if (!strncmp("reset ", command_buf
.buf
, 6))
1921 else if (!strcmp("checkpoint", command_buf
.buf
))
1924 die("Unsupported command: %s", command_buf
.buf
);
1936 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
1937 total_count
+= object_count_by_type
[i
];
1938 duplicate_count
= 0;
1939 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
1940 duplicate_count
+= duplicate_count_by_type
[i
];
1942 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1943 fprintf(stderr
, "---------------------------------------------------------------------\n");
1944 fprintf(stderr
, "Alloc'd objects: %10ju\n", alloc_count
);
1945 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
1946 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
]);
1947 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
]);
1948 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
]);
1949 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
]);
1950 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
1951 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
1952 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
1953 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
1954 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
1955 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
1956 fprintf(stderr
, "---------------------------------------------------------------------\n");
1958 fprintf(stderr
, "---------------------------------------------------------------------\n");
1959 fprintf(stderr
, "\n");