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 '>' when lf)?
21 'committer' sp name '<' email '>' when 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 '>' when 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 when ::= raw_when | rfc2822_when;
92 raw_when ::= ts sp tz;
93 rfc2822_when ::= # Valid RFC 2822 date and time;
95 sp ::= # ASCII space character;
96 lf ::= # ASCII newline (LF) character;
98 # note: a colon (':') must precede the numerical value assigned to
99 # an idnum. This is to distinguish it from a ref or tag name as
100 # GIT does not permit ':' in ref or tag strings.
102 idnum ::= ':' bigint;
103 path ::= # GIT style file path, e.g. "a/b/c";
104 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
105 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
106 sha1exp ::= # Any valid GIT SHA1 expression;
107 hexsha1 ::= # SHA1 in hexadecimal format;
109 # note: name and email are UTF8 strings, however name must not
110 # contain '<' or lf and email must not contain any of the
111 # following: '<', '>', lf.
113 name ::= # valid GIT author/committer name;
114 email ::= # valid GIT author/committer email;
115 ts ::= # time since the epoch in seconds, ascii base10 notation;
116 tz ::= # GIT style timezone;
128 #include "csum-file.h"
132 #define PACK_ID_BITS 16
133 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
137 struct object_entry
*next
;
139 unsigned type
: TYPE_BITS
;
140 unsigned pack_id
: PACK_ID_BITS
;
141 unsigned char sha1
[20];
144 struct object_entry_pool
146 struct object_entry_pool
*next_pool
;
147 struct object_entry
*next_free
;
148 struct object_entry
*end
;
149 struct object_entry entries
[FLEX_ARRAY
]; /* more */
155 struct object_entry
*marked
[1024];
156 struct mark_set
*sets
[1024];
172 struct mem_pool
*next_pool
;
175 char space
[FLEX_ARRAY
]; /* more */
180 struct atom_str
*next_atom
;
181 unsigned short str_len
;
182 char str_dat
[FLEX_ARRAY
]; /* more */
188 struct tree_content
*tree
;
189 struct atom_str
* name
;
193 unsigned char sha1
[20];
199 unsigned int entry_capacity
; /* must match avail_tree_content */
200 unsigned int entry_count
;
201 unsigned int delta_depth
;
202 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
205 struct avail_tree_content
207 unsigned int entry_capacity
; /* must match tree_content */
208 struct avail_tree_content
*next_avail
;
213 struct branch
*table_next_branch
;
214 struct branch
*active_next_branch
;
216 struct tree_entry branch_tree
;
217 uintmax_t last_commit
;
218 unsigned int pack_id
;
219 unsigned char sha1
[20];
224 struct tag
*next_tag
;
226 unsigned int pack_id
;
227 unsigned char sha1
[20];
238 struct hash_list
*next
;
239 unsigned char sha1
[20];
248 /* Configured limits on output */
249 static unsigned long max_depth
= 10;
250 static unsigned long max_packsize
= (1LL << 32) - 1;
251 static int force_update
;
253 /* Stats and misc. counters */
254 static uintmax_t alloc_count
;
255 static uintmax_t marks_set_count
;
256 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
257 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
258 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
259 static unsigned long object_count
;
260 static unsigned long branch_count
;
261 static unsigned long branch_load_count
;
265 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
266 static size_t total_allocd
;
267 static struct mem_pool
*mem_pool
;
269 /* Atom management */
270 static unsigned int atom_table_sz
= 4451;
271 static unsigned int atom_cnt
;
272 static struct atom_str
**atom_table
;
274 /* The .pack file being generated */
275 static unsigned int pack_id
;
276 static struct packed_git
*pack_data
;
277 static struct packed_git
**all_packs
;
278 static unsigned long pack_size
;
280 /* Table of objects we've written. */
281 static unsigned int object_entry_alloc
= 5000;
282 static struct object_entry_pool
*blocks
;
283 static struct object_entry
*object_table
[1 << 16];
284 static struct mark_set
*marks
;
285 static const char* mark_file
;
288 static struct last_object last_blob
;
290 /* Tree management */
291 static unsigned int tree_entry_alloc
= 1000;
292 static void *avail_tree_entry
;
293 static unsigned int avail_tree_table_sz
= 100;
294 static struct avail_tree_content
**avail_tree_table
;
295 static struct dbuf old_tree
;
296 static struct dbuf new_tree
;
299 static unsigned long max_active_branches
= 5;
300 static unsigned long cur_active_branches
;
301 static unsigned long branch_table_sz
= 1039;
302 static struct branch
**branch_table
;
303 static struct branch
*active_branches
;
306 static struct tag
*first_tag
;
307 static struct tag
*last_tag
;
309 /* Input stream parsing */
310 static whenspec_type whenspec
= WHENSPEC_RAW
;
311 static struct strbuf command_buf
;
312 static uintmax_t next_mark
;
313 static struct dbuf new_data
;
316 static void alloc_objects(unsigned int cnt
)
318 struct object_entry_pool
*b
;
320 b
= xmalloc(sizeof(struct object_entry_pool
)
321 + cnt
* sizeof(struct object_entry
));
322 b
->next_pool
= blocks
;
323 b
->next_free
= b
->entries
;
324 b
->end
= b
->entries
+ cnt
;
329 static struct object_entry
*new_object(unsigned char *sha1
)
331 struct object_entry
*e
;
333 if (blocks
->next_free
== blocks
->end
)
334 alloc_objects(object_entry_alloc
);
336 e
= blocks
->next_free
++;
337 hashcpy(e
->sha1
, sha1
);
341 static struct object_entry
*find_object(unsigned char *sha1
)
343 unsigned int h
= sha1
[0] << 8 | sha1
[1];
344 struct object_entry
*e
;
345 for (e
= object_table
[h
]; e
; e
= e
->next
)
346 if (!hashcmp(sha1
, e
->sha1
))
351 static struct object_entry
*insert_object(unsigned char *sha1
)
353 unsigned int h
= sha1
[0] << 8 | sha1
[1];
354 struct object_entry
*e
= object_table
[h
];
355 struct object_entry
*p
= NULL
;
358 if (!hashcmp(sha1
, e
->sha1
))
364 e
= new_object(sha1
);
374 static unsigned int hc_str(const char *s
, size_t len
)
382 static void *pool_alloc(size_t len
)
387 for (p
= mem_pool
; p
; p
= p
->next_pool
)
388 if ((p
->end
- p
->next_free
>= len
))
392 if (len
>= (mem_pool_alloc
/2)) {
396 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
397 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
398 p
->next_pool
= mem_pool
;
399 p
->next_free
= p
->space
;
400 p
->end
= p
->next_free
+ mem_pool_alloc
;
405 /* round out to a pointer alignment */
406 if (len
& (sizeof(void*) - 1))
407 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
412 static void *pool_calloc(size_t count
, size_t size
)
414 size_t len
= count
* size
;
415 void *r
= pool_alloc(len
);
420 static char *pool_strdup(const char *s
)
422 char *r
= pool_alloc(strlen(s
) + 1);
427 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
430 if (b
->capacity
>= maxlen
)
434 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
435 b
->buffer
= xmalloc(b
->capacity
);
438 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
440 struct mark_set
*s
= marks
;
441 while ((idnum
>> s
->shift
) >= 1024) {
442 s
= pool_calloc(1, sizeof(struct mark_set
));
443 s
->shift
= marks
->shift
+ 10;
444 s
->data
.sets
[0] = marks
;
448 uintmax_t i
= idnum
>> s
->shift
;
449 idnum
-= i
<< s
->shift
;
450 if (!s
->data
.sets
[i
]) {
451 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
452 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
456 if (!s
->data
.marked
[idnum
])
458 s
->data
.marked
[idnum
] = oe
;
461 static struct object_entry
*find_mark(uintmax_t idnum
)
463 uintmax_t orig_idnum
= idnum
;
464 struct mark_set
*s
= marks
;
465 struct object_entry
*oe
= NULL
;
466 if ((idnum
>> s
->shift
) < 1024) {
467 while (s
&& s
->shift
) {
468 uintmax_t i
= idnum
>> s
->shift
;
469 idnum
-= i
<< s
->shift
;
473 oe
= s
->data
.marked
[idnum
];
476 die("mark :%ju not declared", orig_idnum
);
480 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
482 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
485 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
486 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
489 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
491 strncpy(c
->str_dat
, s
, len
);
493 c
->next_atom
= atom_table
[hc
];
499 static struct branch
*lookup_branch(const char *name
)
501 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
504 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
505 if (!strcmp(name
, b
->name
))
510 static struct branch
*new_branch(const char *name
)
512 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
513 struct branch
* b
= lookup_branch(name
);
516 die("Invalid attempt to create duplicate branch: %s", name
);
517 if (check_ref_format(name
))
518 die("Branch name doesn't conform to GIT standards: %s", name
);
520 b
= pool_calloc(1, sizeof(struct branch
));
521 b
->name
= pool_strdup(name
);
522 b
->table_next_branch
= branch_table
[hc
];
523 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
524 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
525 b
->pack_id
= MAX_PACK_ID
;
526 branch_table
[hc
] = b
;
531 static unsigned int hc_entries(unsigned int cnt
)
533 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
534 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
537 static struct tree_content
*new_tree_content(unsigned int cnt
)
539 struct avail_tree_content
*f
, *l
= NULL
;
540 struct tree_content
*t
;
541 unsigned int hc
= hc_entries(cnt
);
543 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
544 if (f
->entry_capacity
>= cnt
)
549 l
->next_avail
= f
->next_avail
;
551 avail_tree_table
[hc
] = f
->next_avail
;
553 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
554 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
555 f
->entry_capacity
= cnt
;
558 t
= (struct tree_content
*)f
;
564 static void release_tree_entry(struct tree_entry
*e
);
565 static void release_tree_content(struct tree_content
*t
)
567 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
568 unsigned int hc
= hc_entries(f
->entry_capacity
);
569 f
->next_avail
= avail_tree_table
[hc
];
570 avail_tree_table
[hc
] = f
;
573 static void release_tree_content_recursive(struct tree_content
*t
)
576 for (i
= 0; i
< t
->entry_count
; i
++)
577 release_tree_entry(t
->entries
[i
]);
578 release_tree_content(t
);
581 static struct tree_content
*grow_tree_content(
582 struct tree_content
*t
,
585 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
586 r
->entry_count
= t
->entry_count
;
587 r
->delta_depth
= t
->delta_depth
;
588 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
589 release_tree_content(t
);
593 static struct tree_entry
*new_tree_entry(void)
595 struct tree_entry
*e
;
597 if (!avail_tree_entry
) {
598 unsigned int n
= tree_entry_alloc
;
599 total_allocd
+= n
* sizeof(struct tree_entry
);
600 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
602 *((void**)e
) = e
+ 1;
608 e
= avail_tree_entry
;
609 avail_tree_entry
= *((void**)e
);
613 static void release_tree_entry(struct tree_entry
*e
)
616 release_tree_content_recursive(e
->tree
);
617 *((void**)e
) = avail_tree_entry
;
618 avail_tree_entry
= e
;
621 static void start_packfile(void)
623 static char tmpfile
[PATH_MAX
];
624 struct packed_git
*p
;
625 struct pack_header hdr
;
628 snprintf(tmpfile
, sizeof(tmpfile
),
629 "%s/pack_XXXXXX", get_object_directory());
630 pack_fd
= mkstemp(tmpfile
);
632 die("Can't create %s: %s", tmpfile
, strerror(errno
));
633 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
634 strcpy(p
->pack_name
, tmpfile
);
635 p
->pack_fd
= pack_fd
;
637 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
638 hdr
.hdr_version
= htonl(2);
640 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
643 pack_size
= sizeof(hdr
);
646 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
647 all_packs
[pack_id
] = p
;
650 static void fixup_header_footer(void)
652 static const int buf_sz
= 128 * 1024;
653 int pack_fd
= pack_data
->pack_fd
;
655 struct pack_header hdr
;
658 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
659 die("Failed seeking to start: %s", strerror(errno
));
660 if (read_in_full(pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
661 die("Unable to reread header of %s", pack_data
->pack_name
);
662 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
663 die("Failed seeking to start: %s", strerror(errno
));
664 hdr
.hdr_entries
= htonl(object_count
);
665 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
668 SHA1_Update(&c
, &hdr
, sizeof(hdr
));
670 buf
= xmalloc(buf_sz
);
672 size_t n
= xread(pack_fd
, buf
, buf_sz
);
676 die("Failed to checksum %s", pack_data
->pack_name
);
677 SHA1_Update(&c
, buf
, n
);
681 SHA1_Final(pack_data
->sha1
, &c
);
682 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
686 static int oecmp (const void *a_
, const void *b_
)
688 struct object_entry
*a
= *((struct object_entry
**)a_
);
689 struct object_entry
*b
= *((struct object_entry
**)b_
);
690 return hashcmp(a
->sha1
, b
->sha1
);
693 static char *create_index(void)
695 static char tmpfile
[PATH_MAX
];
698 struct object_entry
**idx
, **c
, **last
, *e
;
699 struct object_entry_pool
*o
;
703 /* Build the sorted table of object IDs. */
704 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
706 for (o
= blocks
; o
; o
= o
->next_pool
)
707 for (e
= o
->next_free
; e
-- != o
->entries
;)
708 if (pack_id
== e
->pack_id
)
710 last
= idx
+ object_count
;
712 die("internal consistency error creating the index");
713 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
715 /* Generate the fan-out array. */
717 for (i
= 0; i
< 256; i
++) {
718 struct object_entry
**next
= c
;;
719 while (next
< last
) {
720 if ((*next
)->sha1
[0] != i
)
724 array
[i
] = htonl(next
- idx
);
728 snprintf(tmpfile
, sizeof(tmpfile
),
729 "%s/index_XXXXXX", get_object_directory());
730 idx_fd
= mkstemp(tmpfile
);
732 die("Can't create %s: %s", tmpfile
, strerror(errno
));
733 f
= sha1fd(idx_fd
, tmpfile
);
734 sha1write(f
, array
, 256 * sizeof(int));
736 for (c
= idx
; c
!= last
; c
++) {
737 uint32_t offset
= htonl((*c
)->offset
);
738 sha1write(f
, &offset
, 4);
739 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
740 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
742 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
743 sha1close(f
, NULL
, 1);
745 SHA1_Final(pack_data
->sha1
, &ctx
);
749 static char *keep_pack(char *curr_index_name
)
751 static char name
[PATH_MAX
];
752 static char *keep_msg
= "fast-import";
755 chmod(pack_data
->pack_name
, 0444);
756 chmod(curr_index_name
, 0444);
758 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
759 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
760 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
762 die("cannot create keep file");
763 write(keep_fd
, keep_msg
, strlen(keep_msg
));
766 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
767 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
768 if (move_temp_to_file(pack_data
->pack_name
, name
))
769 die("cannot store pack file");
771 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
772 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
773 if (move_temp_to_file(curr_index_name
, name
))
774 die("cannot store index file");
778 static void unkeep_all_packs(void)
780 static char name
[PATH_MAX
];
783 for (k
= 0; k
< pack_id
; k
++) {
784 struct packed_git
*p
= all_packs
[k
];
785 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
786 get_object_directory(), sha1_to_hex(p
->sha1
));
791 static void end_packfile(void)
793 struct packed_git
*old_p
= pack_data
, *new_p
;
801 fixup_header_footer();
802 idx_name
= keep_pack(create_index());
804 /* Register the packfile with core git's machinary. */
805 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
807 die("core git rejected index %s", idx_name
);
808 new_p
->windows
= old_p
->windows
;
809 all_packs
[pack_id
] = new_p
;
810 install_packed_git(new_p
);
812 /* Print the boundary */
813 fprintf(stdout
, "%s:", new_p
->pack_name
);
814 for (i
= 0; i
< branch_table_sz
; i
++) {
815 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
816 if (b
->pack_id
== pack_id
)
817 fprintf(stdout
, " %s", sha1_to_hex(b
->sha1
));
820 for (t
= first_tag
; t
; t
= t
->next_tag
) {
821 if (t
->pack_id
== pack_id
)
822 fprintf(stdout
, " %s", sha1_to_hex(t
->sha1
));
829 unlink(old_p
->pack_name
);
832 /* We can't carry a delta across packfiles. */
833 free(last_blob
.data
);
834 last_blob
.data
= NULL
;
836 last_blob
.offset
= 0;
840 static void checkpoint(void)
846 static size_t encode_header(
847 enum object_type type
,
854 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
855 die("bad type %d", type
);
857 c
= (type
<< 4) | (size
& 15);
869 static int store_object(
870 enum object_type type
,
873 struct last_object
*last
,
874 unsigned char *sha1out
,
878 struct object_entry
*e
;
879 unsigned char hdr
[96];
880 unsigned char sha1
[20];
881 unsigned long hdrlen
, deltalen
;
885 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
887 SHA1_Update(&c
, hdr
, hdrlen
);
888 SHA1_Update(&c
, dat
, datlen
);
889 SHA1_Final(sha1
, &c
);
891 hashcpy(sha1out
, sha1
);
893 e
= insert_object(sha1
);
895 insert_mark(mark
, e
);
897 duplicate_count_by_type
[type
]++;
901 if (last
&& last
->data
&& last
->depth
< max_depth
) {
902 delta
= diff_delta(last
->data
, last
->len
,
905 if (delta
&& deltalen
>= datlen
) {
912 memset(&s
, 0, sizeof(s
));
913 deflateInit(&s
, zlib_compression_level
);
916 s
.avail_in
= deltalen
;
921 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
922 s
.next_out
= out
= xmalloc(s
.avail_out
);
923 while (deflate(&s
, Z_FINISH
) == Z_OK
)
927 /* Determine if we should auto-checkpoint. */
928 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
929 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
931 /* This new object needs to *not* have the current pack_id. */
932 e
->pack_id
= pack_id
+ 1;
935 /* We cannot carry a delta into the new pack. */
940 memset(&s
, 0, sizeof(s
));
941 deflateInit(&s
, zlib_compression_level
);
944 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
945 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
946 while (deflate(&s
, Z_FINISH
) == Z_OK
)
953 e
->pack_id
= pack_id
;
954 e
->offset
= pack_size
;
956 object_count_by_type
[type
]++;
959 unsigned long ofs
= e
->offset
- last
->offset
;
960 unsigned pos
= sizeof(hdr
) - 1;
962 delta_count_by_type
[type
]++;
965 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
966 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
969 hdr
[pos
] = ofs
& 127;
971 hdr
[--pos
] = 128 | (--ofs
& 127);
972 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
973 pack_size
+= sizeof(hdr
) - pos
;
977 hdrlen
= encode_header(type
, datlen
, hdr
);
978 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
982 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
983 pack_size
+= s
.total_out
;
991 last
->offset
= e
->offset
;
997 static void *gfi_unpack_entry(
998 struct object_entry
*oe
,
999 unsigned long *sizep
)
1001 static char type
[20];
1002 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1004 p
->pack_size
= pack_size
+ 20;
1005 return unpack_entry(p
, oe
->offset
, type
, sizep
);
1008 static const char *get_mode(const char *str
, uint16_t *modep
)
1013 while ((c
= *str
++) != ' ') {
1014 if (c
< '0' || c
> '7')
1016 mode
= (mode
<< 3) + (c
- '0');
1022 static void load_tree(struct tree_entry
*root
)
1024 unsigned char* sha1
= root
->versions
[1].sha1
;
1025 struct object_entry
*myoe
;
1026 struct tree_content
*t
;
1031 root
->tree
= t
= new_tree_content(8);
1032 if (is_null_sha1(sha1
))
1035 myoe
= find_object(sha1
);
1037 if (myoe
->type
!= OBJ_TREE
)
1038 die("Not a tree: %s", sha1_to_hex(sha1
));
1040 buf
= gfi_unpack_entry(myoe
, &size
);
1043 buf
= read_sha1_file(sha1
, type
, &size
);
1044 if (!buf
|| strcmp(type
, tree_type
))
1045 die("Can't load tree %s", sha1_to_hex(sha1
));
1049 while (c
!= (buf
+ size
)) {
1050 struct tree_entry
*e
= new_tree_entry();
1052 if (t
->entry_count
== t
->entry_capacity
)
1053 root
->tree
= t
= grow_tree_content(t
, 8);
1054 t
->entries
[t
->entry_count
++] = e
;
1057 c
= get_mode(c
, &e
->versions
[1].mode
);
1059 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1060 e
->versions
[0].mode
= e
->versions
[1].mode
;
1061 e
->name
= to_atom(c
, (unsigned short)strlen(c
));
1062 c
+= e
->name
->str_len
+ 1;
1063 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1064 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1070 static int tecmp0 (const void *_a
, const void *_b
)
1072 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1073 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1074 return base_name_compare(
1075 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1076 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1079 static int tecmp1 (const void *_a
, const void *_b
)
1081 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1082 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1083 return base_name_compare(
1084 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1085 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1088 static void mktree(struct tree_content
*t
,
1098 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1100 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1102 for (i
= 0; i
< t
->entry_count
; i
++) {
1103 if (t
->entries
[i
]->versions
[v
].mode
)
1104 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1107 size_dbuf(b
, maxlen
);
1109 for (i
= 0; i
< t
->entry_count
; i
++) {
1110 struct tree_entry
*e
= t
->entries
[i
];
1111 if (!e
->versions
[v
].mode
)
1113 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1115 strcpy(c
, e
->name
->str_dat
);
1116 c
+= e
->name
->str_len
+ 1;
1117 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1120 *szp
= c
- (char*)b
->buffer
;
1123 static void store_tree(struct tree_entry
*root
)
1125 struct tree_content
*t
= root
->tree
;
1126 unsigned int i
, j
, del
;
1127 unsigned long new_len
;
1128 struct last_object lo
;
1129 struct object_entry
*le
;
1131 if (!is_null_sha1(root
->versions
[1].sha1
))
1134 for (i
= 0; i
< t
->entry_count
; i
++) {
1135 if (t
->entries
[i
]->tree
)
1136 store_tree(t
->entries
[i
]);
1139 le
= find_object(root
->versions
[0].sha1
);
1140 if (!S_ISDIR(root
->versions
[0].mode
)
1142 || le
->pack_id
!= pack_id
) {
1146 mktree(t
, 0, &lo
.len
, &old_tree
);
1147 lo
.data
= old_tree
.buffer
;
1148 lo
.offset
= le
->offset
;
1149 lo
.depth
= t
->delta_depth
;
1153 mktree(t
, 1, &new_len
, &new_tree
);
1154 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1155 &lo
, root
->versions
[1].sha1
, 0);
1157 t
->delta_depth
= lo
.depth
;
1158 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1159 struct tree_entry
*e
= t
->entries
[i
];
1160 if (e
->versions
[1].mode
) {
1161 e
->versions
[0].mode
= e
->versions
[1].mode
;
1162 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1163 t
->entries
[j
++] = e
;
1165 release_tree_entry(e
);
1169 t
->entry_count
-= del
;
1172 static int tree_content_set(
1173 struct tree_entry
*root
,
1175 const unsigned char *sha1
,
1176 const uint16_t mode
)
1178 struct tree_content
*t
= root
->tree
;
1181 struct tree_entry
*e
;
1183 slash1
= strchr(p
, '/');
1189 for (i
= 0; i
< t
->entry_count
; i
++) {
1191 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1193 if (e
->versions
[1].mode
== mode
1194 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1196 e
->versions
[1].mode
= mode
;
1197 hashcpy(e
->versions
[1].sha1
, sha1
);
1199 release_tree_content_recursive(e
->tree
);
1202 hashclr(root
->versions
[1].sha1
);
1205 if (!S_ISDIR(e
->versions
[1].mode
)) {
1206 e
->tree
= new_tree_content(8);
1207 e
->versions
[1].mode
= S_IFDIR
;
1211 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1212 hashclr(root
->versions
[1].sha1
);
1219 if (t
->entry_count
== t
->entry_capacity
)
1220 root
->tree
= t
= grow_tree_content(t
, 8);
1221 e
= new_tree_entry();
1222 e
->name
= to_atom(p
, (unsigned short)n
);
1223 e
->versions
[0].mode
= 0;
1224 hashclr(e
->versions
[0].sha1
);
1225 t
->entries
[t
->entry_count
++] = e
;
1227 e
->tree
= new_tree_content(8);
1228 e
->versions
[1].mode
= S_IFDIR
;
1229 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1232 e
->versions
[1].mode
= mode
;
1233 hashcpy(e
->versions
[1].sha1
, sha1
);
1235 hashclr(root
->versions
[1].sha1
);
1239 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1241 struct tree_content
*t
= root
->tree
;
1244 struct tree_entry
*e
;
1246 slash1
= strchr(p
, '/');
1252 for (i
= 0; i
< t
->entry_count
; i
++) {
1254 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1255 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1259 if (tree_content_remove(e
, slash1
+ 1)) {
1260 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1261 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1262 hashclr(root
->versions
[1].sha1
);
1275 release_tree_content_recursive(e
->tree
);
1278 e
->versions
[1].mode
= 0;
1279 hashclr(e
->versions
[1].sha1
);
1280 hashclr(root
->versions
[1].sha1
);
1284 static int update_branch(struct branch
*b
)
1286 static const char *msg
= "fast-import";
1287 struct ref_lock
*lock
;
1288 unsigned char old_sha1
[20];
1290 if (read_ref(b
->name
, old_sha1
))
1292 lock
= lock_any_ref_for_update(b
->name
, old_sha1
);
1294 return error("Unable to lock %s", b
->name
);
1295 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1296 struct commit
*old_cmit
, *new_cmit
;
1298 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1299 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1300 if (!old_cmit
|| !new_cmit
) {
1302 return error("Branch %s is missing commits.", b
->name
);
1305 if (!in_merge_bases(old_cmit
, new_cmit
)) {
1307 warn("Not updating %s"
1308 " (new tip %s does not contain %s)",
1309 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1313 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1314 return error("Unable to update %s", b
->name
);
1318 static void dump_branches(void)
1323 for (i
= 0; i
< branch_table_sz
; i
++) {
1324 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1325 failure
|= update_branch(b
);
1329 static void dump_tags(void)
1331 static const char *msg
= "fast-import";
1333 struct ref_lock
*lock
;
1334 char ref_name
[PATH_MAX
];
1336 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1337 sprintf(ref_name
, "tags/%s", t
->name
);
1338 lock
= lock_ref_sha1(ref_name
, NULL
);
1339 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1340 failure
|= error("Unable to update %s", ref_name
);
1344 static void dump_marks_helper(FILE *f
,
1350 for (k
= 0; k
< 1024; k
++) {
1351 if (m
->data
.sets
[k
])
1352 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1356 for (k
= 0; k
< 1024; k
++) {
1357 if (m
->data
.marked
[k
])
1358 fprintf(f
, ":%ju %s\n", base
+ k
,
1359 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1364 static void dump_marks(void)
1368 FILE *f
= fopen(mark_file
, "w");
1369 dump_marks_helper(f
, 0, marks
);
1374 static void read_next_command(void)
1376 read_line(&command_buf
, stdin
, '\n');
1379 static void cmd_mark(void)
1381 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1382 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1383 read_next_command();
1389 static void *cmd_data (size_t *size
)
1394 if (strncmp("data ", command_buf
.buf
, 5))
1395 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1397 if (!strncmp("<<", command_buf
.buf
+ 5, 2)) {
1398 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1399 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1401 buffer
= xmalloc(sz
);
1403 read_next_command();
1404 if (command_buf
.eof
)
1405 die("EOF in data (terminator '%s' not found)", term
);
1406 if (term_len
== command_buf
.len
1407 && !strcmp(term
, command_buf
.buf
))
1409 if (sz
< (length
+ command_buf
.len
)) {
1410 sz
= sz
* 3 / 2 + 16;
1411 if (sz
< (length
+ command_buf
.len
))
1412 sz
= length
+ command_buf
.len
;
1413 buffer
= xrealloc(buffer
, sz
);
1415 memcpy(buffer
+ length
,
1417 command_buf
.len
- 1);
1418 length
+= command_buf
.len
- 1;
1419 buffer
[length
++] = '\n';
1425 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1426 buffer
= xmalloc(length
);
1427 while (n
< length
) {
1428 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1429 if (!s
&& feof(stdin
))
1430 die("EOF in data (%lu bytes remaining)", length
- n
);
1435 if (fgetc(stdin
) != '\n')
1436 die("An lf did not trail the binary data as expected.");
1442 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1444 const char *orig_src
= src
;
1447 strtoul(src
, &endp
, 10);
1448 if (endp
== src
|| *endp
!= ' ')
1452 if (*src
!= '-' && *src
!= '+')
1456 strtoul(src
+ 1, &endp
, 10);
1457 if (endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1460 strcpy(result
, orig_src
);
1464 static char *parse_ident(const char *buf
)
1470 gt
= strrchr(buf
, '>');
1472 die("Missing > in ident string: %s", buf
);
1475 die("Missing space after > in ident string: %s", buf
);
1477 name_len
= gt
- buf
;
1478 ident
= xmalloc(name_len
+ 24);
1479 strncpy(ident
, buf
, name_len
);
1483 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1484 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1486 case WHENSPEC_RFC2822
:
1487 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1488 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1491 if (strcmp("now", gt
))
1492 die("Date in ident must be 'now': %s", buf
);
1493 datestamp(ident
+ name_len
, 24);
1500 static void cmd_new_blob(void)
1505 read_next_command();
1509 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1513 static void unload_one_branch(void)
1515 while (cur_active_branches
1516 && cur_active_branches
>= max_active_branches
) {
1517 unsigned long min_commit
= ULONG_MAX
;
1518 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1520 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1521 if (e
->last_commit
< min_commit
) {
1523 min_commit
= e
->last_commit
;
1529 e
= p
->active_next_branch
;
1530 p
->active_next_branch
= e
->active_next_branch
;
1532 e
= active_branches
;
1533 active_branches
= e
->active_next_branch
;
1535 e
->active_next_branch
= NULL
;
1536 if (e
->branch_tree
.tree
) {
1537 release_tree_content_recursive(e
->branch_tree
.tree
);
1538 e
->branch_tree
.tree
= NULL
;
1540 cur_active_branches
--;
1544 static void load_branch(struct branch
*b
)
1546 load_tree(&b
->branch_tree
);
1547 b
->active_next_branch
= active_branches
;
1548 active_branches
= b
;
1549 cur_active_branches
++;
1550 branch_load_count
++;
1553 static void file_change_m(struct branch
*b
)
1555 const char *p
= command_buf
.buf
+ 2;
1558 struct object_entry
*oe
= oe
;
1559 unsigned char sha1
[20];
1560 uint16_t mode
, inline_data
= 0;
1563 p
= get_mode(p
, &mode
);
1565 die("Corrupt mode: %s", command_buf
.buf
);
1567 case S_IFREG
| 0644:
1568 case S_IFREG
| 0755:
1575 die("Corrupt mode: %s", command_buf
.buf
);
1580 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1581 hashcpy(sha1
, oe
->sha1
);
1583 } else if (!strncmp("inline", p
, 6)) {
1587 if (get_sha1_hex(p
, sha1
))
1588 die("Invalid SHA1: %s", command_buf
.buf
);
1589 oe
= find_object(sha1
);
1593 die("Missing space after SHA1: %s", command_buf
.buf
);
1595 p_uq
= unquote_c_style(p
, &endp
);
1598 die("Garbage after path in: %s", command_buf
.buf
);
1606 p
= p_uq
= xstrdup(p
);
1607 read_next_command();
1609 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1612 if (oe
->type
!= OBJ_BLOB
)
1613 die("Not a blob (actually a %s): %s",
1614 command_buf
.buf
, type_names
[oe
->type
]);
1616 if (sha1_object_info(sha1
, type
, NULL
))
1617 die("Blob not found: %s", command_buf
.buf
);
1618 if (strcmp(blob_type
, type
))
1619 die("Not a blob (actually a %s): %s",
1620 command_buf
.buf
, type
);
1623 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1627 static void file_change_d(struct branch
*b
)
1629 const char *p
= command_buf
.buf
+ 2;
1633 p_uq
= unquote_c_style(p
, &endp
);
1636 die("Garbage after path in: %s", command_buf
.buf
);
1639 tree_content_remove(&b
->branch_tree
, p
);
1643 static void cmd_from(struct branch
*b
)
1648 if (strncmp("from ", command_buf
.buf
, 5))
1652 die("Can't reinitailize branch %s", b
->name
);
1654 from
= strchr(command_buf
.buf
, ' ') + 1;
1655 s
= lookup_branch(from
);
1657 die("Can't create a branch from itself: %s", b
->name
);
1659 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1660 hashcpy(b
->sha1
, s
->sha1
);
1661 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1662 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1663 } else if (*from
== ':') {
1664 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1665 struct object_entry
*oe
= find_mark(idnum
);
1668 if (oe
->type
!= OBJ_COMMIT
)
1669 die("Mark :%ju not a commit", idnum
);
1670 hashcpy(b
->sha1
, oe
->sha1
);
1671 buf
= gfi_unpack_entry(oe
, &size
);
1672 if (!buf
|| size
< 46)
1673 die("Not a valid commit: %s", from
);
1674 if (memcmp("tree ", buf
, 5)
1675 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1676 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1678 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1679 b
->branch_tree
.versions
[1].sha1
);
1680 } else if (!get_sha1(from
, b
->sha1
)) {
1681 if (is_null_sha1(b
->sha1
)) {
1682 hashclr(b
->branch_tree
.versions
[0].sha1
);
1683 hashclr(b
->branch_tree
.versions
[1].sha1
);
1688 buf
= read_object_with_reference(b
->sha1
,
1689 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1690 if (!buf
|| size
< 46)
1691 die("Not a valid commit: %s", from
);
1692 if (memcmp("tree ", buf
, 5)
1693 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1694 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1696 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1697 b
->branch_tree
.versions
[1].sha1
);
1700 die("Invalid ref name or SHA1 expression: %s", from
);
1702 read_next_command();
1705 static struct hash_list
*cmd_merge(unsigned int *count
)
1707 struct hash_list
*list
= NULL
, *n
, *e
= e
;
1712 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1713 from
= strchr(command_buf
.buf
, ' ') + 1;
1714 n
= xmalloc(sizeof(*n
));
1715 s
= lookup_branch(from
);
1717 hashcpy(n
->sha1
, s
->sha1
);
1718 else if (*from
== ':') {
1719 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1720 struct object_entry
*oe
= find_mark(idnum
);
1721 if (oe
->type
!= OBJ_COMMIT
)
1722 die("Mark :%ju not a commit", idnum
);
1723 hashcpy(n
->sha1
, oe
->sha1
);
1724 } else if (get_sha1(from
, n
->sha1
))
1725 die("Invalid ref name or SHA1 expression: %s", from
);
1734 read_next_command();
1739 static void cmd_new_commit(void)
1745 char *author
= NULL
;
1746 char *committer
= NULL
;
1747 struct hash_list
*merge_list
= NULL
;
1748 unsigned int merge_count
;
1750 /* Obtain the branch name from the rest of our command */
1751 sp
= strchr(command_buf
.buf
, ' ') + 1;
1752 b
= lookup_branch(sp
);
1756 read_next_command();
1758 if (!strncmp("author ", command_buf
.buf
, 7)) {
1759 author
= parse_ident(command_buf
.buf
+ 7);
1760 read_next_command();
1762 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1763 committer
= parse_ident(command_buf
.buf
+ 10);
1764 read_next_command();
1767 die("Expected committer but didn't get one");
1768 msg
= cmd_data(&msglen
);
1769 read_next_command();
1771 merge_list
= cmd_merge(&merge_count
);
1773 /* ensure the branch is active/loaded */
1774 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1775 unload_one_branch();
1781 if (1 == command_buf
.len
)
1783 else if (!strncmp("M ", command_buf
.buf
, 2))
1785 else if (!strncmp("D ", command_buf
.buf
, 2))
1788 die("Unsupported file_change: %s", command_buf
.buf
);
1789 read_next_command();
1792 /* build the tree and the commit */
1793 store_tree(&b
->branch_tree
);
1794 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1795 b
->branch_tree
.versions
[1].sha1
);
1796 size_dbuf(&new_data
, 114 + msglen
1799 ? strlen(author
) + strlen(committer
)
1800 : 2 * strlen(committer
)));
1801 sp
= new_data
.buffer
;
1802 sp
+= sprintf(sp
, "tree %s\n",
1803 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1804 if (!is_null_sha1(b
->sha1
))
1805 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1806 while (merge_list
) {
1807 struct hash_list
*next
= merge_list
->next
;
1808 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1812 sp
+= sprintf(sp
, "author %s\n", author
? author
: committer
);
1813 sp
+= sprintf(sp
, "committer %s\n", committer
);
1815 memcpy(sp
, msg
, msglen
);
1821 if (!store_object(OBJ_COMMIT
,
1822 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1823 NULL
, b
->sha1
, next_mark
))
1824 b
->pack_id
= pack_id
;
1825 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1828 static void cmd_new_tag(void)
1837 uintmax_t from_mark
= 0;
1838 unsigned char sha1
[20];
1840 /* Obtain the new tag name from the rest of our command */
1841 sp
= strchr(command_buf
.buf
, ' ') + 1;
1842 t
= pool_alloc(sizeof(struct tag
));
1844 t
->name
= pool_strdup(sp
);
1846 last_tag
->next_tag
= t
;
1850 read_next_command();
1853 if (strncmp("from ", command_buf
.buf
, 5))
1854 die("Expected from command, got %s", command_buf
.buf
);
1855 from
= strchr(command_buf
.buf
, ' ') + 1;
1856 s
= lookup_branch(from
);
1858 hashcpy(sha1
, s
->sha1
);
1859 } else if (*from
== ':') {
1860 struct object_entry
*oe
;
1861 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1862 oe
= find_mark(from_mark
);
1863 if (oe
->type
!= OBJ_COMMIT
)
1864 die("Mark :%ju not a commit", from_mark
);
1865 hashcpy(sha1
, oe
->sha1
);
1866 } else if (!get_sha1(from
, sha1
)) {
1870 buf
= read_object_with_reference(sha1
,
1871 type_names
[OBJ_COMMIT
], &size
, sha1
);
1872 if (!buf
|| size
< 46)
1873 die("Not a valid commit: %s", from
);
1876 die("Invalid ref name or SHA1 expression: %s", from
);
1877 read_next_command();
1880 if (strncmp("tagger ", command_buf
.buf
, 7))
1881 die("Expected tagger command, got %s", command_buf
.buf
);
1882 tagger
= parse_ident(command_buf
.buf
+ 7);
1884 /* tag payload/message */
1885 read_next_command();
1886 msg
= cmd_data(&msglen
);
1888 /* build the tag object */
1889 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1890 sp
= new_data
.buffer
;
1891 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1892 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1893 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1894 sp
+= sprintf(sp
, "tagger %s\n", tagger
);
1896 memcpy(sp
, msg
, msglen
);
1901 if (store_object(OBJ_TAG
, new_data
.buffer
,
1902 sp
- (char*)new_data
.buffer
,
1904 t
->pack_id
= MAX_PACK_ID
;
1906 t
->pack_id
= pack_id
;
1909 static void cmd_reset_branch(void)
1914 /* Obtain the branch name from the rest of our command */
1915 sp
= strchr(command_buf
.buf
, ' ') + 1;
1916 b
= lookup_branch(sp
);
1919 if (b
->branch_tree
.tree
) {
1920 release_tree_content_recursive(b
->branch_tree
.tree
);
1921 b
->branch_tree
.tree
= NULL
;
1926 read_next_command();
1930 static void cmd_checkpoint(void)
1934 read_next_command();
1937 static const char fast_import_usage
[] =
1938 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
1940 int main(int argc
, const char **argv
)
1943 uintmax_t total_count
, duplicate_count
;
1945 git_config(git_default_config
);
1947 for (i
= 1; i
< argc
; i
++) {
1948 const char *a
= argv
[i
];
1950 if (*a
!= '-' || !strcmp(a
, "--"))
1952 else if (!strncmp(a
, "--date-format=", 14)) {
1953 const char *fmt
= a
+ 14;
1954 if (!strcmp(fmt
, "raw"))
1955 whenspec
= WHENSPEC_RAW
;
1956 else if (!strcmp(fmt
, "rfc2822"))
1957 whenspec
= WHENSPEC_RFC2822
;
1958 else if (!strcmp(fmt
, "now"))
1959 whenspec
= WHENSPEC_NOW
;
1961 die("unknown --date-format argument %s", fmt
);
1963 else if (!strncmp(a
, "--max-pack-size=", 16))
1964 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1965 else if (!strncmp(a
, "--depth=", 8))
1966 max_depth
= strtoul(a
+ 8, NULL
, 0);
1967 else if (!strncmp(a
, "--active-branches=", 18))
1968 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1969 else if (!strncmp(a
, "--export-marks=", 15))
1971 else if (!strcmp(a
, "--force"))
1974 die("unknown option %s", a
);
1977 usage(fast_import_usage
);
1979 alloc_objects(object_entry_alloc
);
1980 strbuf_init(&command_buf
);
1982 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1983 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1984 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1985 marks
= pool_calloc(1, sizeof(struct mark_set
));
1989 read_next_command();
1990 if (command_buf
.eof
)
1992 else if (!strcmp("blob", command_buf
.buf
))
1994 else if (!strncmp("commit ", command_buf
.buf
, 7))
1996 else if (!strncmp("tag ", command_buf
.buf
, 4))
1998 else if (!strncmp("reset ", command_buf
.buf
, 6))
2000 else if (!strcmp("checkpoint", command_buf
.buf
))
2003 die("Unsupported command: %s", command_buf
.buf
);
2013 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2014 total_count
+= object_count_by_type
[i
];
2015 duplicate_count
= 0;
2016 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2017 duplicate_count
+= duplicate_count_by_type
[i
];
2019 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2020 fprintf(stderr
, "---------------------------------------------------------------------\n");
2021 fprintf(stderr
, "Alloc'd objects: %10ju\n", alloc_count
);
2022 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
2023 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
]);
2024 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
]);
2025 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
]);
2026 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
]);
2027 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2028 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2029 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2030 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2031 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
2032 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2033 fprintf(stderr
, "---------------------------------------------------------------------\n");
2035 fprintf(stderr
, "---------------------------------------------------------------------\n");
2036 fprintf(stderr
, "\n");
2038 return failure
? 1 : 0;