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_clr | file_del | file_obm | file_inm;
30 file_clr ::= 'deleteall' lf;
31 file_del ::= 'D' sp path_str lf;
32 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
33 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
36 new_tag ::= 'tag' sp tag_str lf
37 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
38 'tagger' sp name '<' email '>' when lf
42 reset_branch ::= 'reset' sp ref_str lf
43 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
46 checkpoint ::= 'checkpoint' lf
49 # note: the first idnum in a stream should be 1 and subsequent
50 # idnums should not have gaps between values as this will cause
51 # the stream parser to reserve space for the gapped values. An
52 # idnum can be updated in the future to a new object by issuing
53 # a new mark directive with the old idnum.
55 mark ::= 'mark' sp idnum lf;
56 data ::= (delimited_data | exact_data)
59 # note: delim may be any string but must not contain lf.
60 # data_line may contain any data but must not be exactly
62 delimited_data ::= 'data' sp '<<' delim lf
66 # note: declen indicates the length of binary_data in bytes.
67 # declen does not include the lf preceeding the binary data.
69 exact_data ::= 'data' sp declen lf
72 # note: quoted strings are C-style quoting supporting \c for
73 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
74 # is the signed byte value in octal. Note that the only
75 # characters which must actually be escaped to protect the
76 # stream formatting is: \, " and LF. Otherwise these values
80 sha1exp_str ::= sha1exp;
82 path_str ::= path | '"' quoted(path) '"' ;
83 mode ::= '100644' | '644'
88 declen ::= # unsigned 32 bit value, ascii base10 notation;
89 bigint ::= # unsigned integer value, ascii base10 notation;
90 binary_data ::= # file content, not interpreted;
92 when ::= raw_when | rfc2822_when;
93 raw_when ::= ts sp tz;
94 rfc2822_when ::= # Valid RFC 2822 date and time;
96 sp ::= # ASCII space character;
97 lf ::= # ASCII newline (LF) character;
99 # note: a colon (':') must precede the numerical value assigned to
100 # an idnum. This is to distinguish it from a ref or tag name as
101 # GIT does not permit ':' in ref or tag strings.
103 idnum ::= ':' bigint;
104 path ::= # GIT style file path, e.g. "a/b/c";
105 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
106 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
107 sha1exp ::= # Any valid GIT SHA1 expression;
108 hexsha1 ::= # SHA1 in hexadecimal format;
110 # note: name and email are UTF8 strings, however name must not
111 # contain '<' or lf and email must not contain any of the
112 # following: '<', '>', lf.
114 name ::= # valid GIT author/committer name;
115 email ::= # valid GIT author/committer email;
116 ts ::= # time since the epoch in seconds, ascii base10 notation;
117 tz ::= # GIT style timezone;
129 #include "csum-file.h"
133 #define PACK_ID_BITS 16
134 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
138 struct object_entry
*next
;
140 unsigned type
: TYPE_BITS
;
141 unsigned pack_id
: PACK_ID_BITS
;
142 unsigned char sha1
[20];
145 struct object_entry_pool
147 struct object_entry_pool
*next_pool
;
148 struct object_entry
*next_free
;
149 struct object_entry
*end
;
150 struct object_entry entries
[FLEX_ARRAY
]; /* more */
156 struct object_entry
*marked
[1024];
157 struct mark_set
*sets
[1024];
173 struct mem_pool
*next_pool
;
176 char space
[FLEX_ARRAY
]; /* more */
181 struct atom_str
*next_atom
;
182 unsigned short str_len
;
183 char str_dat
[FLEX_ARRAY
]; /* more */
189 struct tree_content
*tree
;
190 struct atom_str
* name
;
194 unsigned char sha1
[20];
200 unsigned int entry_capacity
; /* must match avail_tree_content */
201 unsigned int entry_count
;
202 unsigned int delta_depth
;
203 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
206 struct avail_tree_content
208 unsigned int entry_capacity
; /* must match tree_content */
209 struct avail_tree_content
*next_avail
;
214 struct branch
*table_next_branch
;
215 struct branch
*active_next_branch
;
217 struct tree_entry branch_tree
;
218 uintmax_t last_commit
;
219 unsigned int pack_id
;
220 unsigned char sha1
[20];
225 struct tag
*next_tag
;
227 unsigned int pack_id
;
228 unsigned char sha1
[20];
239 struct hash_list
*next
;
240 unsigned char sha1
[20];
249 /* Configured limits on output */
250 static unsigned long max_depth
= 10;
251 static unsigned long max_packsize
= (1LL << 32) - 1;
252 static int force_update
;
254 /* Stats and misc. counters */
255 static uintmax_t alloc_count
;
256 static uintmax_t marks_set_count
;
257 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
258 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
259 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
260 static unsigned long object_count
;
261 static unsigned long branch_count
;
262 static unsigned long branch_load_count
;
266 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
267 static size_t total_allocd
;
268 static struct mem_pool
*mem_pool
;
270 /* Atom management */
271 static unsigned int atom_table_sz
= 4451;
272 static unsigned int atom_cnt
;
273 static struct atom_str
**atom_table
;
275 /* The .pack file being generated */
276 static unsigned int pack_id
;
277 static struct packed_git
*pack_data
;
278 static struct packed_git
**all_packs
;
279 static unsigned long pack_size
;
281 /* Table of objects we've written. */
282 static unsigned int object_entry_alloc
= 5000;
283 static struct object_entry_pool
*blocks
;
284 static struct object_entry
*object_table
[1 << 16];
285 static struct mark_set
*marks
;
286 static const char* mark_file
;
289 static struct last_object last_blob
;
291 /* Tree management */
292 static unsigned int tree_entry_alloc
= 1000;
293 static void *avail_tree_entry
;
294 static unsigned int avail_tree_table_sz
= 100;
295 static struct avail_tree_content
**avail_tree_table
;
296 static struct dbuf old_tree
;
297 static struct dbuf new_tree
;
300 static unsigned long max_active_branches
= 5;
301 static unsigned long cur_active_branches
;
302 static unsigned long branch_table_sz
= 1039;
303 static struct branch
**branch_table
;
304 static struct branch
*active_branches
;
307 static struct tag
*first_tag
;
308 static struct tag
*last_tag
;
310 /* Input stream parsing */
311 static whenspec_type whenspec
= WHENSPEC_RAW
;
312 static struct strbuf command_buf
;
313 static uintmax_t next_mark
;
314 static struct dbuf new_data
;
317 static void alloc_objects(unsigned int cnt
)
319 struct object_entry_pool
*b
;
321 b
= xmalloc(sizeof(struct object_entry_pool
)
322 + cnt
* sizeof(struct object_entry
));
323 b
->next_pool
= blocks
;
324 b
->next_free
= b
->entries
;
325 b
->end
= b
->entries
+ cnt
;
330 static struct object_entry
*new_object(unsigned char *sha1
)
332 struct object_entry
*e
;
334 if (blocks
->next_free
== blocks
->end
)
335 alloc_objects(object_entry_alloc
);
337 e
= blocks
->next_free
++;
338 hashcpy(e
->sha1
, sha1
);
342 static struct object_entry
*find_object(unsigned char *sha1
)
344 unsigned int h
= sha1
[0] << 8 | sha1
[1];
345 struct object_entry
*e
;
346 for (e
= object_table
[h
]; e
; e
= e
->next
)
347 if (!hashcmp(sha1
, e
->sha1
))
352 static struct object_entry
*insert_object(unsigned char *sha1
)
354 unsigned int h
= sha1
[0] << 8 | sha1
[1];
355 struct object_entry
*e
= object_table
[h
];
356 struct object_entry
*p
= NULL
;
359 if (!hashcmp(sha1
, e
->sha1
))
365 e
= new_object(sha1
);
375 static unsigned int hc_str(const char *s
, size_t len
)
383 static void *pool_alloc(size_t len
)
388 for (p
= mem_pool
; p
; p
= p
->next_pool
)
389 if ((p
->end
- p
->next_free
>= len
))
393 if (len
>= (mem_pool_alloc
/2)) {
397 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
398 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
399 p
->next_pool
= mem_pool
;
400 p
->next_free
= p
->space
;
401 p
->end
= p
->next_free
+ mem_pool_alloc
;
406 /* round out to a pointer alignment */
407 if (len
& (sizeof(void*) - 1))
408 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
413 static void *pool_calloc(size_t count
, size_t size
)
415 size_t len
= count
* size
;
416 void *r
= pool_alloc(len
);
421 static char *pool_strdup(const char *s
)
423 char *r
= pool_alloc(strlen(s
) + 1);
428 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
431 if (b
->capacity
>= maxlen
)
435 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
436 b
->buffer
= xmalloc(b
->capacity
);
439 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
441 struct mark_set
*s
= marks
;
442 while ((idnum
>> s
->shift
) >= 1024) {
443 s
= pool_calloc(1, sizeof(struct mark_set
));
444 s
->shift
= marks
->shift
+ 10;
445 s
->data
.sets
[0] = marks
;
449 uintmax_t i
= idnum
>> s
->shift
;
450 idnum
-= i
<< s
->shift
;
451 if (!s
->data
.sets
[i
]) {
452 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
453 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
457 if (!s
->data
.marked
[idnum
])
459 s
->data
.marked
[idnum
] = oe
;
462 static struct object_entry
*find_mark(uintmax_t idnum
)
464 uintmax_t orig_idnum
= idnum
;
465 struct mark_set
*s
= marks
;
466 struct object_entry
*oe
= NULL
;
467 if ((idnum
>> s
->shift
) < 1024) {
468 while (s
&& s
->shift
) {
469 uintmax_t i
= idnum
>> s
->shift
;
470 idnum
-= i
<< s
->shift
;
474 oe
= s
->data
.marked
[idnum
];
477 die("mark :%ju not declared", orig_idnum
);
481 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
483 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
486 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
487 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
490 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
492 strncpy(c
->str_dat
, s
, len
);
494 c
->next_atom
= atom_table
[hc
];
500 static struct branch
*lookup_branch(const char *name
)
502 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
505 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
506 if (!strcmp(name
, b
->name
))
511 static struct branch
*new_branch(const char *name
)
513 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
514 struct branch
* b
= lookup_branch(name
);
517 die("Invalid attempt to create duplicate branch: %s", name
);
518 if (check_ref_format(name
))
519 die("Branch name doesn't conform to GIT standards: %s", name
);
521 b
= pool_calloc(1, sizeof(struct branch
));
522 b
->name
= pool_strdup(name
);
523 b
->table_next_branch
= branch_table
[hc
];
524 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
525 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
526 b
->pack_id
= MAX_PACK_ID
;
527 branch_table
[hc
] = b
;
532 static unsigned int hc_entries(unsigned int cnt
)
534 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
535 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
538 static struct tree_content
*new_tree_content(unsigned int cnt
)
540 struct avail_tree_content
*f
, *l
= NULL
;
541 struct tree_content
*t
;
542 unsigned int hc
= hc_entries(cnt
);
544 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
545 if (f
->entry_capacity
>= cnt
)
550 l
->next_avail
= f
->next_avail
;
552 avail_tree_table
[hc
] = f
->next_avail
;
554 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
555 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
556 f
->entry_capacity
= cnt
;
559 t
= (struct tree_content
*)f
;
565 static void release_tree_entry(struct tree_entry
*e
);
566 static void release_tree_content(struct tree_content
*t
)
568 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
569 unsigned int hc
= hc_entries(f
->entry_capacity
);
570 f
->next_avail
= avail_tree_table
[hc
];
571 avail_tree_table
[hc
] = f
;
574 static void release_tree_content_recursive(struct tree_content
*t
)
577 for (i
= 0; i
< t
->entry_count
; i
++)
578 release_tree_entry(t
->entries
[i
]);
579 release_tree_content(t
);
582 static struct tree_content
*grow_tree_content(
583 struct tree_content
*t
,
586 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
587 r
->entry_count
= t
->entry_count
;
588 r
->delta_depth
= t
->delta_depth
;
589 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
590 release_tree_content(t
);
594 static struct tree_entry
*new_tree_entry(void)
596 struct tree_entry
*e
;
598 if (!avail_tree_entry
) {
599 unsigned int n
= tree_entry_alloc
;
600 total_allocd
+= n
* sizeof(struct tree_entry
);
601 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
603 *((void**)e
) = e
+ 1;
609 e
= avail_tree_entry
;
610 avail_tree_entry
= *((void**)e
);
614 static void release_tree_entry(struct tree_entry
*e
)
617 release_tree_content_recursive(e
->tree
);
618 *((void**)e
) = avail_tree_entry
;
619 avail_tree_entry
= e
;
622 static void start_packfile(void)
624 static char tmpfile
[PATH_MAX
];
625 struct packed_git
*p
;
626 struct pack_header hdr
;
629 snprintf(tmpfile
, sizeof(tmpfile
),
630 "%s/pack_XXXXXX", get_object_directory());
631 pack_fd
= mkstemp(tmpfile
);
633 die("Can't create %s: %s", tmpfile
, strerror(errno
));
634 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
635 strcpy(p
->pack_name
, tmpfile
);
636 p
->pack_fd
= pack_fd
;
638 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
639 hdr
.hdr_version
= htonl(2);
641 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
644 pack_size
= sizeof(hdr
);
647 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
648 all_packs
[pack_id
] = p
;
651 static void fixup_header_footer(void)
653 static const int buf_sz
= 128 * 1024;
654 int pack_fd
= pack_data
->pack_fd
;
656 struct pack_header hdr
;
659 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
660 die("Failed seeking to start: %s", strerror(errno
));
661 if (read_in_full(pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
662 die("Unable to reread header of %s", pack_data
->pack_name
);
663 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
664 die("Failed seeking to start: %s", strerror(errno
));
665 hdr
.hdr_entries
= htonl(object_count
);
666 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
669 SHA1_Update(&c
, &hdr
, sizeof(hdr
));
671 buf
= xmalloc(buf_sz
);
673 size_t n
= xread(pack_fd
, buf
, buf_sz
);
677 die("Failed to checksum %s", pack_data
->pack_name
);
678 SHA1_Update(&c
, buf
, n
);
682 SHA1_Final(pack_data
->sha1
, &c
);
683 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
687 static int oecmp (const void *a_
, const void *b_
)
689 struct object_entry
*a
= *((struct object_entry
**)a_
);
690 struct object_entry
*b
= *((struct object_entry
**)b_
);
691 return hashcmp(a
->sha1
, b
->sha1
);
694 static char *create_index(void)
696 static char tmpfile
[PATH_MAX
];
699 struct object_entry
**idx
, **c
, **last
, *e
;
700 struct object_entry_pool
*o
;
704 /* Build the sorted table of object IDs. */
705 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
707 for (o
= blocks
; o
; o
= o
->next_pool
)
708 for (e
= o
->next_free
; e
-- != o
->entries
;)
709 if (pack_id
== e
->pack_id
)
711 last
= idx
+ object_count
;
713 die("internal consistency error creating the index");
714 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
716 /* Generate the fan-out array. */
718 for (i
= 0; i
< 256; i
++) {
719 struct object_entry
**next
= c
;;
720 while (next
< last
) {
721 if ((*next
)->sha1
[0] != i
)
725 array
[i
] = htonl(next
- idx
);
729 snprintf(tmpfile
, sizeof(tmpfile
),
730 "%s/index_XXXXXX", get_object_directory());
731 idx_fd
= mkstemp(tmpfile
);
733 die("Can't create %s: %s", tmpfile
, strerror(errno
));
734 f
= sha1fd(idx_fd
, tmpfile
);
735 sha1write(f
, array
, 256 * sizeof(int));
737 for (c
= idx
; c
!= last
; c
++) {
738 uint32_t offset
= htonl((*c
)->offset
);
739 sha1write(f
, &offset
, 4);
740 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
741 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
743 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
744 sha1close(f
, NULL
, 1);
746 SHA1_Final(pack_data
->sha1
, &ctx
);
750 static char *keep_pack(char *curr_index_name
)
752 static char name
[PATH_MAX
];
753 static char *keep_msg
= "fast-import";
756 chmod(pack_data
->pack_name
, 0444);
757 chmod(curr_index_name
, 0444);
759 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
760 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
761 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
763 die("cannot create keep file");
764 write(keep_fd
, keep_msg
, strlen(keep_msg
));
767 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
768 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
769 if (move_temp_to_file(pack_data
->pack_name
, name
))
770 die("cannot store pack file");
772 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
773 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
774 if (move_temp_to_file(curr_index_name
, name
))
775 die("cannot store index file");
779 static void unkeep_all_packs(void)
781 static char name
[PATH_MAX
];
784 for (k
= 0; k
< pack_id
; k
++) {
785 struct packed_git
*p
= all_packs
[k
];
786 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
787 get_object_directory(), sha1_to_hex(p
->sha1
));
792 static void end_packfile(void)
794 struct packed_git
*old_p
= pack_data
, *new_p
;
802 fixup_header_footer();
803 idx_name
= keep_pack(create_index());
805 /* Register the packfile with core git's machinary. */
806 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
808 die("core git rejected index %s", idx_name
);
809 new_p
->windows
= old_p
->windows
;
810 all_packs
[pack_id
] = new_p
;
811 install_packed_git(new_p
);
813 /* Print the boundary */
814 fprintf(stdout
, "%s:", new_p
->pack_name
);
815 for (i
= 0; i
< branch_table_sz
; i
++) {
816 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
817 if (b
->pack_id
== pack_id
)
818 fprintf(stdout
, " %s", sha1_to_hex(b
->sha1
));
821 for (t
= first_tag
; t
; t
= t
->next_tag
) {
822 if (t
->pack_id
== pack_id
)
823 fprintf(stdout
, " %s", sha1_to_hex(t
->sha1
));
830 unlink(old_p
->pack_name
);
833 /* We can't carry a delta across packfiles. */
834 free(last_blob
.data
);
835 last_blob
.data
= NULL
;
837 last_blob
.offset
= 0;
841 static void cycle_packfile(void)
847 static size_t encode_header(
848 enum object_type type
,
855 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
856 die("bad type %d", type
);
858 c
= (type
<< 4) | (size
& 15);
870 static int store_object(
871 enum object_type type
,
874 struct last_object
*last
,
875 unsigned char *sha1out
,
879 struct object_entry
*e
;
880 unsigned char hdr
[96];
881 unsigned char sha1
[20];
882 unsigned long hdrlen
, deltalen
;
886 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
888 SHA1_Update(&c
, hdr
, hdrlen
);
889 SHA1_Update(&c
, dat
, datlen
);
890 SHA1_Final(sha1
, &c
);
892 hashcpy(sha1out
, sha1
);
894 e
= insert_object(sha1
);
896 insert_mark(mark
, e
);
898 duplicate_count_by_type
[type
]++;
902 if (last
&& last
->data
&& last
->depth
< max_depth
) {
903 delta
= diff_delta(last
->data
, last
->len
,
906 if (delta
&& deltalen
>= datlen
) {
913 memset(&s
, 0, sizeof(s
));
914 deflateInit(&s
, zlib_compression_level
);
917 s
.avail_in
= deltalen
;
922 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
923 s
.next_out
= out
= xmalloc(s
.avail_out
);
924 while (deflate(&s
, Z_FINISH
) == Z_OK
)
928 /* Determine if we should auto-checkpoint. */
929 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
930 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
932 /* This new object needs to *not* have the current pack_id. */
933 e
->pack_id
= pack_id
+ 1;
936 /* We cannot carry a delta into the new pack. */
941 memset(&s
, 0, sizeof(s
));
942 deflateInit(&s
, zlib_compression_level
);
945 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
946 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
947 while (deflate(&s
, Z_FINISH
) == Z_OK
)
954 e
->pack_id
= pack_id
;
955 e
->offset
= pack_size
;
957 object_count_by_type
[type
]++;
960 unsigned long ofs
= e
->offset
- last
->offset
;
961 unsigned pos
= sizeof(hdr
) - 1;
963 delta_count_by_type
[type
]++;
966 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
967 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
970 hdr
[pos
] = ofs
& 127;
972 hdr
[--pos
] = 128 | (--ofs
& 127);
973 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
974 pack_size
+= sizeof(hdr
) - pos
;
978 hdrlen
= encode_header(type
, datlen
, hdr
);
979 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
983 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
984 pack_size
+= s
.total_out
;
992 last
->offset
= e
->offset
;
998 static void *gfi_unpack_entry(
999 struct object_entry
*oe
,
1000 unsigned long *sizep
)
1002 static char type
[20];
1003 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1005 p
->pack_size
= pack_size
+ 20;
1006 return unpack_entry(p
, oe
->offset
, type
, sizep
);
1009 static const char *get_mode(const char *str
, uint16_t *modep
)
1014 while ((c
= *str
++) != ' ') {
1015 if (c
< '0' || c
> '7')
1017 mode
= (mode
<< 3) + (c
- '0');
1023 static void load_tree(struct tree_entry
*root
)
1025 unsigned char* sha1
= root
->versions
[1].sha1
;
1026 struct object_entry
*myoe
;
1027 struct tree_content
*t
;
1032 root
->tree
= t
= new_tree_content(8);
1033 if (is_null_sha1(sha1
))
1036 myoe
= find_object(sha1
);
1038 if (myoe
->type
!= OBJ_TREE
)
1039 die("Not a tree: %s", sha1_to_hex(sha1
));
1041 buf
= gfi_unpack_entry(myoe
, &size
);
1044 buf
= read_sha1_file(sha1
, type
, &size
);
1045 if (!buf
|| strcmp(type
, tree_type
))
1046 die("Can't load tree %s", sha1_to_hex(sha1
));
1050 while (c
!= (buf
+ size
)) {
1051 struct tree_entry
*e
= new_tree_entry();
1053 if (t
->entry_count
== t
->entry_capacity
)
1054 root
->tree
= t
= grow_tree_content(t
, 8);
1055 t
->entries
[t
->entry_count
++] = e
;
1058 c
= get_mode(c
, &e
->versions
[1].mode
);
1060 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1061 e
->versions
[0].mode
= e
->versions
[1].mode
;
1062 e
->name
= to_atom(c
, (unsigned short)strlen(c
));
1063 c
+= e
->name
->str_len
+ 1;
1064 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1065 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1071 static int tecmp0 (const void *_a
, const void *_b
)
1073 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1074 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1075 return base_name_compare(
1076 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1077 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1080 static int tecmp1 (const void *_a
, const void *_b
)
1082 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1083 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1084 return base_name_compare(
1085 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1086 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1089 static void mktree(struct tree_content
*t
,
1099 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1101 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1103 for (i
= 0; i
< t
->entry_count
; i
++) {
1104 if (t
->entries
[i
]->versions
[v
].mode
)
1105 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1108 size_dbuf(b
, maxlen
);
1110 for (i
= 0; i
< t
->entry_count
; i
++) {
1111 struct tree_entry
*e
= t
->entries
[i
];
1112 if (!e
->versions
[v
].mode
)
1114 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1116 strcpy(c
, e
->name
->str_dat
);
1117 c
+= e
->name
->str_len
+ 1;
1118 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1121 *szp
= c
- (char*)b
->buffer
;
1124 static void store_tree(struct tree_entry
*root
)
1126 struct tree_content
*t
= root
->tree
;
1127 unsigned int i
, j
, del
;
1128 unsigned long new_len
;
1129 struct last_object lo
;
1130 struct object_entry
*le
;
1132 if (!is_null_sha1(root
->versions
[1].sha1
))
1135 for (i
= 0; i
< t
->entry_count
; i
++) {
1136 if (t
->entries
[i
]->tree
)
1137 store_tree(t
->entries
[i
]);
1140 le
= find_object(root
->versions
[0].sha1
);
1141 if (!S_ISDIR(root
->versions
[0].mode
)
1143 || le
->pack_id
!= pack_id
) {
1147 mktree(t
, 0, &lo
.len
, &old_tree
);
1148 lo
.data
= old_tree
.buffer
;
1149 lo
.offset
= le
->offset
;
1150 lo
.depth
= t
->delta_depth
;
1154 mktree(t
, 1, &new_len
, &new_tree
);
1155 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1156 &lo
, root
->versions
[1].sha1
, 0);
1158 t
->delta_depth
= lo
.depth
;
1159 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1160 struct tree_entry
*e
= t
->entries
[i
];
1161 if (e
->versions
[1].mode
) {
1162 e
->versions
[0].mode
= e
->versions
[1].mode
;
1163 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1164 t
->entries
[j
++] = e
;
1166 release_tree_entry(e
);
1170 t
->entry_count
-= del
;
1173 static int tree_content_set(
1174 struct tree_entry
*root
,
1176 const unsigned char *sha1
,
1177 const uint16_t mode
)
1179 struct tree_content
*t
= root
->tree
;
1182 struct tree_entry
*e
;
1184 slash1
= strchr(p
, '/');
1190 for (i
= 0; i
< t
->entry_count
; i
++) {
1192 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1194 if (e
->versions
[1].mode
== mode
1195 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1197 e
->versions
[1].mode
= mode
;
1198 hashcpy(e
->versions
[1].sha1
, sha1
);
1200 release_tree_content_recursive(e
->tree
);
1203 hashclr(root
->versions
[1].sha1
);
1206 if (!S_ISDIR(e
->versions
[1].mode
)) {
1207 e
->tree
= new_tree_content(8);
1208 e
->versions
[1].mode
= S_IFDIR
;
1212 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1213 hashclr(root
->versions
[1].sha1
);
1220 if (t
->entry_count
== t
->entry_capacity
)
1221 root
->tree
= t
= grow_tree_content(t
, 8);
1222 e
= new_tree_entry();
1223 e
->name
= to_atom(p
, (unsigned short)n
);
1224 e
->versions
[0].mode
= 0;
1225 hashclr(e
->versions
[0].sha1
);
1226 t
->entries
[t
->entry_count
++] = e
;
1228 e
->tree
= new_tree_content(8);
1229 e
->versions
[1].mode
= S_IFDIR
;
1230 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1233 e
->versions
[1].mode
= mode
;
1234 hashcpy(e
->versions
[1].sha1
, sha1
);
1236 hashclr(root
->versions
[1].sha1
);
1240 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1242 struct tree_content
*t
= root
->tree
;
1245 struct tree_entry
*e
;
1247 slash1
= strchr(p
, '/');
1253 for (i
= 0; i
< t
->entry_count
; i
++) {
1255 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1256 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1260 if (tree_content_remove(e
, slash1
+ 1)) {
1261 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1262 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1263 hashclr(root
->versions
[1].sha1
);
1276 release_tree_content_recursive(e
->tree
);
1279 e
->versions
[1].mode
= 0;
1280 hashclr(e
->versions
[1].sha1
);
1281 hashclr(root
->versions
[1].sha1
);
1285 static int update_branch(struct branch
*b
)
1287 static const char *msg
= "fast-import";
1288 struct ref_lock
*lock
;
1289 unsigned char old_sha1
[20];
1291 if (read_ref(b
->name
, old_sha1
))
1293 lock
= lock_any_ref_for_update(b
->name
, old_sha1
);
1295 return error("Unable to lock %s", b
->name
);
1296 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1297 struct commit
*old_cmit
, *new_cmit
;
1299 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1300 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1301 if (!old_cmit
|| !new_cmit
) {
1303 return error("Branch %s is missing commits.", b
->name
);
1306 if (!in_merge_bases(old_cmit
, new_cmit
)) {
1308 warn("Not updating %s"
1309 " (new tip %s does not contain %s)",
1310 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1314 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1315 return error("Unable to update %s", b
->name
);
1319 static void dump_branches(void)
1324 for (i
= 0; i
< branch_table_sz
; i
++) {
1325 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1326 failure
|= update_branch(b
);
1330 static void dump_tags(void)
1332 static const char *msg
= "fast-import";
1334 struct ref_lock
*lock
;
1335 char ref_name
[PATH_MAX
];
1337 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1338 sprintf(ref_name
, "tags/%s", t
->name
);
1339 lock
= lock_ref_sha1(ref_name
, NULL
);
1340 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1341 failure
|= error("Unable to update %s", ref_name
);
1345 static void dump_marks_helper(FILE *f
,
1351 for (k
= 0; k
< 1024; k
++) {
1352 if (m
->data
.sets
[k
])
1353 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1357 for (k
= 0; k
< 1024; k
++) {
1358 if (m
->data
.marked
[k
])
1359 fprintf(f
, ":%ju %s\n", base
+ k
,
1360 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1365 static void dump_marks(void)
1369 FILE *f
= fopen(mark_file
, "w");
1371 dump_marks_helper(f
, 0, marks
);
1374 failure
|= error("Unable to write marks file %s: %s",
1375 mark_file
, strerror(errno
));
1379 static void read_next_command(void)
1381 read_line(&command_buf
, stdin
, '\n');
1384 static void cmd_mark(void)
1386 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1387 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1388 read_next_command();
1394 static void *cmd_data (size_t *size
)
1399 if (strncmp("data ", command_buf
.buf
, 5))
1400 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1402 if (!strncmp("<<", command_buf
.buf
+ 5, 2)) {
1403 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1404 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1406 buffer
= xmalloc(sz
);
1408 read_next_command();
1409 if (command_buf
.eof
)
1410 die("EOF in data (terminator '%s' not found)", term
);
1411 if (term_len
== command_buf
.len
1412 && !strcmp(term
, command_buf
.buf
))
1414 if (sz
< (length
+ command_buf
.len
)) {
1415 sz
= sz
* 3 / 2 + 16;
1416 if (sz
< (length
+ command_buf
.len
))
1417 sz
= length
+ command_buf
.len
;
1418 buffer
= xrealloc(buffer
, sz
);
1420 memcpy(buffer
+ length
,
1422 command_buf
.len
- 1);
1423 length
+= command_buf
.len
- 1;
1424 buffer
[length
++] = '\n';
1430 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1431 buffer
= xmalloc(length
);
1432 while (n
< length
) {
1433 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1434 if (!s
&& feof(stdin
))
1435 die("EOF in data (%lu bytes remaining)", length
- n
);
1440 if (fgetc(stdin
) != '\n')
1441 die("An lf did not trail the binary data as expected.");
1447 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1449 const char *orig_src
= src
;
1452 strtoul(src
, &endp
, 10);
1453 if (endp
== src
|| *endp
!= ' ')
1457 if (*src
!= '-' && *src
!= '+')
1461 strtoul(src
+ 1, &endp
, 10);
1462 if (endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1465 strcpy(result
, orig_src
);
1469 static char *parse_ident(const char *buf
)
1475 gt
= strrchr(buf
, '>');
1477 die("Missing > in ident string: %s", buf
);
1480 die("Missing space after > in ident string: %s", buf
);
1482 name_len
= gt
- buf
;
1483 ident
= xmalloc(name_len
+ 24);
1484 strncpy(ident
, buf
, name_len
);
1488 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1489 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1491 case WHENSPEC_RFC2822
:
1492 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1493 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1496 if (strcmp("now", gt
))
1497 die("Date in ident must be 'now': %s", buf
);
1498 datestamp(ident
+ name_len
, 24);
1505 static void cmd_new_blob(void)
1510 read_next_command();
1514 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1518 static void unload_one_branch(void)
1520 while (cur_active_branches
1521 && cur_active_branches
>= max_active_branches
) {
1522 unsigned long min_commit
= ULONG_MAX
;
1523 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1525 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1526 if (e
->last_commit
< min_commit
) {
1528 min_commit
= e
->last_commit
;
1534 e
= p
->active_next_branch
;
1535 p
->active_next_branch
= e
->active_next_branch
;
1537 e
= active_branches
;
1538 active_branches
= e
->active_next_branch
;
1540 e
->active_next_branch
= NULL
;
1541 if (e
->branch_tree
.tree
) {
1542 release_tree_content_recursive(e
->branch_tree
.tree
);
1543 e
->branch_tree
.tree
= NULL
;
1545 cur_active_branches
--;
1549 static void load_branch(struct branch
*b
)
1551 load_tree(&b
->branch_tree
);
1552 b
->active_next_branch
= active_branches
;
1553 active_branches
= b
;
1554 cur_active_branches
++;
1555 branch_load_count
++;
1558 static void file_change_m(struct branch
*b
)
1560 const char *p
= command_buf
.buf
+ 2;
1563 struct object_entry
*oe
= oe
;
1564 unsigned char sha1
[20];
1565 uint16_t mode
, inline_data
= 0;
1568 p
= get_mode(p
, &mode
);
1570 die("Corrupt mode: %s", command_buf
.buf
);
1572 case S_IFREG
| 0644:
1573 case S_IFREG
| 0755:
1580 die("Corrupt mode: %s", command_buf
.buf
);
1585 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1586 hashcpy(sha1
, oe
->sha1
);
1588 } else if (!strncmp("inline", p
, 6)) {
1592 if (get_sha1_hex(p
, sha1
))
1593 die("Invalid SHA1: %s", command_buf
.buf
);
1594 oe
= find_object(sha1
);
1598 die("Missing space after SHA1: %s", command_buf
.buf
);
1600 p_uq
= unquote_c_style(p
, &endp
);
1603 die("Garbage after path in: %s", command_buf
.buf
);
1611 p
= p_uq
= xstrdup(p
);
1612 read_next_command();
1614 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1617 if (oe
->type
!= OBJ_BLOB
)
1618 die("Not a blob (actually a %s): %s",
1619 command_buf
.buf
, type_names
[oe
->type
]);
1621 if (sha1_object_info(sha1
, type
, NULL
))
1622 die("Blob not found: %s", command_buf
.buf
);
1623 if (strcmp(blob_type
, type
))
1624 die("Not a blob (actually a %s): %s",
1625 command_buf
.buf
, type
);
1628 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1632 static void file_change_d(struct branch
*b
)
1634 const char *p
= command_buf
.buf
+ 2;
1638 p_uq
= unquote_c_style(p
, &endp
);
1641 die("Garbage after path in: %s", command_buf
.buf
);
1644 tree_content_remove(&b
->branch_tree
, p
);
1648 static void file_change_deleteall(struct branch
*b
)
1650 release_tree_content_recursive(b
->branch_tree
.tree
);
1651 hashclr(b
->branch_tree
.versions
[0].sha1
);
1652 hashclr(b
->branch_tree
.versions
[1].sha1
);
1653 load_tree(&b
->branch_tree
);
1656 static void cmd_from(struct branch
*b
)
1661 if (strncmp("from ", command_buf
.buf
, 5))
1665 die("Can't reinitailize branch %s", b
->name
);
1667 from
= strchr(command_buf
.buf
, ' ') + 1;
1668 s
= lookup_branch(from
);
1670 die("Can't create a branch from itself: %s", b
->name
);
1672 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1673 hashcpy(b
->sha1
, s
->sha1
);
1674 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1675 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1676 } else if (*from
== ':') {
1677 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1678 struct object_entry
*oe
= find_mark(idnum
);
1681 if (oe
->type
!= OBJ_COMMIT
)
1682 die("Mark :%ju not a commit", idnum
);
1683 hashcpy(b
->sha1
, oe
->sha1
);
1684 buf
= gfi_unpack_entry(oe
, &size
);
1685 if (!buf
|| size
< 46)
1686 die("Not a valid commit: %s", from
);
1687 if (memcmp("tree ", buf
, 5)
1688 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1689 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1691 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1692 b
->branch_tree
.versions
[1].sha1
);
1693 } else if (!get_sha1(from
, b
->sha1
)) {
1694 if (is_null_sha1(b
->sha1
)) {
1695 hashclr(b
->branch_tree
.versions
[0].sha1
);
1696 hashclr(b
->branch_tree
.versions
[1].sha1
);
1701 buf
= read_object_with_reference(b
->sha1
,
1702 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1703 if (!buf
|| size
< 46)
1704 die("Not a valid commit: %s", from
);
1705 if (memcmp("tree ", buf
, 5)
1706 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1707 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1709 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1710 b
->branch_tree
.versions
[1].sha1
);
1713 die("Invalid ref name or SHA1 expression: %s", from
);
1715 read_next_command();
1718 static struct hash_list
*cmd_merge(unsigned int *count
)
1720 struct hash_list
*list
= NULL
, *n
, *e
= e
;
1725 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1726 from
= strchr(command_buf
.buf
, ' ') + 1;
1727 n
= xmalloc(sizeof(*n
));
1728 s
= lookup_branch(from
);
1730 hashcpy(n
->sha1
, s
->sha1
);
1731 else if (*from
== ':') {
1732 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1733 struct object_entry
*oe
= find_mark(idnum
);
1734 if (oe
->type
!= OBJ_COMMIT
)
1735 die("Mark :%ju not a commit", idnum
);
1736 hashcpy(n
->sha1
, oe
->sha1
);
1737 } else if (get_sha1(from
, n
->sha1
))
1738 die("Invalid ref name or SHA1 expression: %s", from
);
1747 read_next_command();
1752 static void cmd_new_commit(void)
1758 char *author
= NULL
;
1759 char *committer
= NULL
;
1760 struct hash_list
*merge_list
= NULL
;
1761 unsigned int merge_count
;
1763 /* Obtain the branch name from the rest of our command */
1764 sp
= strchr(command_buf
.buf
, ' ') + 1;
1765 b
= lookup_branch(sp
);
1769 read_next_command();
1771 if (!strncmp("author ", command_buf
.buf
, 7)) {
1772 author
= parse_ident(command_buf
.buf
+ 7);
1773 read_next_command();
1775 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1776 committer
= parse_ident(command_buf
.buf
+ 10);
1777 read_next_command();
1780 die("Expected committer but didn't get one");
1781 msg
= cmd_data(&msglen
);
1782 read_next_command();
1784 merge_list
= cmd_merge(&merge_count
);
1786 /* ensure the branch is active/loaded */
1787 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1788 unload_one_branch();
1794 if (1 == command_buf
.len
)
1796 else if (!strncmp("M ", command_buf
.buf
, 2))
1798 else if (!strncmp("D ", command_buf
.buf
, 2))
1800 else if (!strcmp("deleteall", command_buf
.buf
))
1801 file_change_deleteall(b
);
1803 die("Unsupported file_change: %s", command_buf
.buf
);
1804 read_next_command();
1807 /* build the tree and the commit */
1808 store_tree(&b
->branch_tree
);
1809 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1810 b
->branch_tree
.versions
[1].sha1
);
1811 size_dbuf(&new_data
, 114 + msglen
1814 ? strlen(author
) + strlen(committer
)
1815 : 2 * strlen(committer
)));
1816 sp
= new_data
.buffer
;
1817 sp
+= sprintf(sp
, "tree %s\n",
1818 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1819 if (!is_null_sha1(b
->sha1
))
1820 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1821 while (merge_list
) {
1822 struct hash_list
*next
= merge_list
->next
;
1823 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1827 sp
+= sprintf(sp
, "author %s\n", author
? author
: committer
);
1828 sp
+= sprintf(sp
, "committer %s\n", committer
);
1830 memcpy(sp
, msg
, msglen
);
1836 if (!store_object(OBJ_COMMIT
,
1837 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1838 NULL
, b
->sha1
, next_mark
))
1839 b
->pack_id
= pack_id
;
1840 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1843 static void cmd_new_tag(void)
1852 uintmax_t from_mark
= 0;
1853 unsigned char sha1
[20];
1855 /* Obtain the new tag name from the rest of our command */
1856 sp
= strchr(command_buf
.buf
, ' ') + 1;
1857 t
= pool_alloc(sizeof(struct tag
));
1859 t
->name
= pool_strdup(sp
);
1861 last_tag
->next_tag
= t
;
1865 read_next_command();
1868 if (strncmp("from ", command_buf
.buf
, 5))
1869 die("Expected from command, got %s", command_buf
.buf
);
1870 from
= strchr(command_buf
.buf
, ' ') + 1;
1871 s
= lookup_branch(from
);
1873 hashcpy(sha1
, s
->sha1
);
1874 } else if (*from
== ':') {
1875 struct object_entry
*oe
;
1876 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1877 oe
= find_mark(from_mark
);
1878 if (oe
->type
!= OBJ_COMMIT
)
1879 die("Mark :%ju not a commit", from_mark
);
1880 hashcpy(sha1
, oe
->sha1
);
1881 } else if (!get_sha1(from
, sha1
)) {
1885 buf
= read_object_with_reference(sha1
,
1886 type_names
[OBJ_COMMIT
], &size
, sha1
);
1887 if (!buf
|| size
< 46)
1888 die("Not a valid commit: %s", from
);
1891 die("Invalid ref name or SHA1 expression: %s", from
);
1892 read_next_command();
1895 if (strncmp("tagger ", command_buf
.buf
, 7))
1896 die("Expected tagger command, got %s", command_buf
.buf
);
1897 tagger
= parse_ident(command_buf
.buf
+ 7);
1899 /* tag payload/message */
1900 read_next_command();
1901 msg
= cmd_data(&msglen
);
1903 /* build the tag object */
1904 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1905 sp
= new_data
.buffer
;
1906 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1907 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1908 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1909 sp
+= sprintf(sp
, "tagger %s\n", tagger
);
1911 memcpy(sp
, msg
, msglen
);
1916 if (store_object(OBJ_TAG
, new_data
.buffer
,
1917 sp
- (char*)new_data
.buffer
,
1919 t
->pack_id
= MAX_PACK_ID
;
1921 t
->pack_id
= pack_id
;
1924 static void cmd_reset_branch(void)
1929 /* Obtain the branch name from the rest of our command */
1930 sp
= strchr(command_buf
.buf
, ' ') + 1;
1931 b
= lookup_branch(sp
);
1934 if (b
->branch_tree
.tree
) {
1935 release_tree_content_recursive(b
->branch_tree
.tree
);
1936 b
->branch_tree
.tree
= NULL
;
1941 read_next_command();
1945 static void cmd_checkpoint(void)
1953 read_next_command();
1956 static const char fast_import_usage
[] =
1957 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
1959 int main(int argc
, const char **argv
)
1961 int i
, show_stats
= 1;
1963 git_config(git_default_config
);
1965 for (i
= 1; i
< argc
; i
++) {
1966 const char *a
= argv
[i
];
1968 if (*a
!= '-' || !strcmp(a
, "--"))
1970 else if (!strncmp(a
, "--date-format=", 14)) {
1971 const char *fmt
= a
+ 14;
1972 if (!strcmp(fmt
, "raw"))
1973 whenspec
= WHENSPEC_RAW
;
1974 else if (!strcmp(fmt
, "rfc2822"))
1975 whenspec
= WHENSPEC_RFC2822
;
1976 else if (!strcmp(fmt
, "now"))
1977 whenspec
= WHENSPEC_NOW
;
1979 die("unknown --date-format argument %s", fmt
);
1981 else if (!strncmp(a
, "--max-pack-size=", 16))
1982 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1983 else if (!strncmp(a
, "--depth=", 8))
1984 max_depth
= strtoul(a
+ 8, NULL
, 0);
1985 else if (!strncmp(a
, "--active-branches=", 18))
1986 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1987 else if (!strncmp(a
, "--export-marks=", 15))
1989 else if (!strcmp(a
, "--force"))
1991 else if (!strcmp(a
, "--quiet"))
1993 else if (!strcmp(a
, "--stats"))
1996 die("unknown option %s", a
);
1999 usage(fast_import_usage
);
2001 alloc_objects(object_entry_alloc
);
2002 strbuf_init(&command_buf
);
2004 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2005 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2006 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2007 marks
= pool_calloc(1, sizeof(struct mark_set
));
2011 read_next_command();
2012 if (command_buf
.eof
)
2014 else if (!strcmp("blob", command_buf
.buf
))
2016 else if (!strncmp("commit ", command_buf
.buf
, 7))
2018 else if (!strncmp("tag ", command_buf
.buf
, 4))
2020 else if (!strncmp("reset ", command_buf
.buf
, 6))
2022 else if (!strcmp("checkpoint", command_buf
.buf
))
2025 die("Unsupported command: %s", command_buf
.buf
);
2035 uintmax_t total_count
= 0, duplicate_count
= 0;
2036 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2037 total_count
+= object_count_by_type
[i
];
2038 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2039 duplicate_count
+= duplicate_count_by_type
[i
];
2041 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2042 fprintf(stderr
, "---------------------------------------------------------------------\n");
2043 fprintf(stderr
, "Alloc'd objects: %10ju\n", alloc_count
);
2044 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
2045 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
]);
2046 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
]);
2047 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
]);
2048 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
]);
2049 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2050 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2051 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2052 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2053 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
2054 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2055 fprintf(stderr
, "---------------------------------------------------------------------\n");
2057 fprintf(stderr
, "---------------------------------------------------------------------\n");
2058 fprintf(stderr
, "\n");
2061 return failure
? 1 : 0;