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
;
264 static FILE *pack_edges
;
267 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
268 static size_t total_allocd
;
269 static struct mem_pool
*mem_pool
;
271 /* Atom management */
272 static unsigned int atom_table_sz
= 4451;
273 static unsigned int atom_cnt
;
274 static struct atom_str
**atom_table
;
276 /* The .pack file being generated */
277 static unsigned int pack_id
;
278 static struct packed_git
*pack_data
;
279 static struct packed_git
**all_packs
;
280 static unsigned long pack_size
;
282 /* Table of objects we've written. */
283 static unsigned int object_entry_alloc
= 5000;
284 static struct object_entry_pool
*blocks
;
285 static struct object_entry
*object_table
[1 << 16];
286 static struct mark_set
*marks
;
287 static const char* mark_file
;
290 static struct last_object last_blob
;
292 /* Tree management */
293 static unsigned int tree_entry_alloc
= 1000;
294 static void *avail_tree_entry
;
295 static unsigned int avail_tree_table_sz
= 100;
296 static struct avail_tree_content
**avail_tree_table
;
297 static struct dbuf old_tree
;
298 static struct dbuf new_tree
;
301 static unsigned long max_active_branches
= 5;
302 static unsigned long cur_active_branches
;
303 static unsigned long branch_table_sz
= 1039;
304 static struct branch
**branch_table
;
305 static struct branch
*active_branches
;
308 static struct tag
*first_tag
;
309 static struct tag
*last_tag
;
311 /* Input stream parsing */
312 static whenspec_type whenspec
= WHENSPEC_RAW
;
313 static struct strbuf command_buf
;
314 static uintmax_t next_mark
;
315 static struct dbuf new_data
;
318 static void alloc_objects(unsigned int cnt
)
320 struct object_entry_pool
*b
;
322 b
= xmalloc(sizeof(struct object_entry_pool
)
323 + cnt
* sizeof(struct object_entry
));
324 b
->next_pool
= blocks
;
325 b
->next_free
= b
->entries
;
326 b
->end
= b
->entries
+ cnt
;
331 static struct object_entry
*new_object(unsigned char *sha1
)
333 struct object_entry
*e
;
335 if (blocks
->next_free
== blocks
->end
)
336 alloc_objects(object_entry_alloc
);
338 e
= blocks
->next_free
++;
339 hashcpy(e
->sha1
, sha1
);
343 static struct object_entry
*find_object(unsigned char *sha1
)
345 unsigned int h
= sha1
[0] << 8 | sha1
[1];
346 struct object_entry
*e
;
347 for (e
= object_table
[h
]; e
; e
= e
->next
)
348 if (!hashcmp(sha1
, e
->sha1
))
353 static struct object_entry
*insert_object(unsigned char *sha1
)
355 unsigned int h
= sha1
[0] << 8 | sha1
[1];
356 struct object_entry
*e
= object_table
[h
];
357 struct object_entry
*p
= NULL
;
360 if (!hashcmp(sha1
, e
->sha1
))
366 e
= new_object(sha1
);
376 static unsigned int hc_str(const char *s
, size_t len
)
384 static void *pool_alloc(size_t len
)
389 for (p
= mem_pool
; p
; p
= p
->next_pool
)
390 if ((p
->end
- p
->next_free
>= len
))
394 if (len
>= (mem_pool_alloc
/2)) {
398 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
399 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
400 p
->next_pool
= mem_pool
;
401 p
->next_free
= p
->space
;
402 p
->end
= p
->next_free
+ mem_pool_alloc
;
407 /* round out to a pointer alignment */
408 if (len
& (sizeof(void*) - 1))
409 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
414 static void *pool_calloc(size_t count
, size_t size
)
416 size_t len
= count
* size
;
417 void *r
= pool_alloc(len
);
422 static char *pool_strdup(const char *s
)
424 char *r
= pool_alloc(strlen(s
) + 1);
429 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
432 if (b
->capacity
>= maxlen
)
436 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
437 b
->buffer
= xmalloc(b
->capacity
);
440 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
442 struct mark_set
*s
= marks
;
443 while ((idnum
>> s
->shift
) >= 1024) {
444 s
= pool_calloc(1, sizeof(struct mark_set
));
445 s
->shift
= marks
->shift
+ 10;
446 s
->data
.sets
[0] = marks
;
450 uintmax_t i
= idnum
>> s
->shift
;
451 idnum
-= i
<< s
->shift
;
452 if (!s
->data
.sets
[i
]) {
453 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
454 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
458 if (!s
->data
.marked
[idnum
])
460 s
->data
.marked
[idnum
] = oe
;
463 static struct object_entry
*find_mark(uintmax_t idnum
)
465 uintmax_t orig_idnum
= idnum
;
466 struct mark_set
*s
= marks
;
467 struct object_entry
*oe
= NULL
;
468 if ((idnum
>> s
->shift
) < 1024) {
469 while (s
&& s
->shift
) {
470 uintmax_t i
= idnum
>> s
->shift
;
471 idnum
-= i
<< s
->shift
;
475 oe
= s
->data
.marked
[idnum
];
478 die("mark :%ju not declared", orig_idnum
);
482 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
484 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
487 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
488 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
491 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
493 strncpy(c
->str_dat
, s
, len
);
495 c
->next_atom
= atom_table
[hc
];
501 static struct branch
*lookup_branch(const char *name
)
503 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
506 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
507 if (!strcmp(name
, b
->name
))
512 static struct branch
*new_branch(const char *name
)
514 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
515 struct branch
* b
= lookup_branch(name
);
518 die("Invalid attempt to create duplicate branch: %s", name
);
519 if (check_ref_format(name
))
520 die("Branch name doesn't conform to GIT standards: %s", name
);
522 b
= pool_calloc(1, sizeof(struct branch
));
523 b
->name
= pool_strdup(name
);
524 b
->table_next_branch
= branch_table
[hc
];
525 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
526 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
527 b
->pack_id
= MAX_PACK_ID
;
528 branch_table
[hc
] = b
;
533 static unsigned int hc_entries(unsigned int cnt
)
535 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
536 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
539 static struct tree_content
*new_tree_content(unsigned int cnt
)
541 struct avail_tree_content
*f
, *l
= NULL
;
542 struct tree_content
*t
;
543 unsigned int hc
= hc_entries(cnt
);
545 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
546 if (f
->entry_capacity
>= cnt
)
551 l
->next_avail
= f
->next_avail
;
553 avail_tree_table
[hc
] = f
->next_avail
;
555 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
556 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
557 f
->entry_capacity
= cnt
;
560 t
= (struct tree_content
*)f
;
566 static void release_tree_entry(struct tree_entry
*e
);
567 static void release_tree_content(struct tree_content
*t
)
569 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
570 unsigned int hc
= hc_entries(f
->entry_capacity
);
571 f
->next_avail
= avail_tree_table
[hc
];
572 avail_tree_table
[hc
] = f
;
575 static void release_tree_content_recursive(struct tree_content
*t
)
578 for (i
= 0; i
< t
->entry_count
; i
++)
579 release_tree_entry(t
->entries
[i
]);
580 release_tree_content(t
);
583 static struct tree_content
*grow_tree_content(
584 struct tree_content
*t
,
587 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
588 r
->entry_count
= t
->entry_count
;
589 r
->delta_depth
= t
->delta_depth
;
590 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
591 release_tree_content(t
);
595 static struct tree_entry
*new_tree_entry(void)
597 struct tree_entry
*e
;
599 if (!avail_tree_entry
) {
600 unsigned int n
= tree_entry_alloc
;
601 total_allocd
+= n
* sizeof(struct tree_entry
);
602 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
604 *((void**)e
) = e
+ 1;
610 e
= avail_tree_entry
;
611 avail_tree_entry
= *((void**)e
);
615 static void release_tree_entry(struct tree_entry
*e
)
618 release_tree_content_recursive(e
->tree
);
619 *((void**)e
) = avail_tree_entry
;
620 avail_tree_entry
= e
;
623 static void start_packfile(void)
625 static char tmpfile
[PATH_MAX
];
626 struct packed_git
*p
;
627 struct pack_header hdr
;
630 snprintf(tmpfile
, sizeof(tmpfile
),
631 "%s/pack_XXXXXX", get_object_directory());
632 pack_fd
= mkstemp(tmpfile
);
634 die("Can't create %s: %s", tmpfile
, strerror(errno
));
635 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
636 strcpy(p
->pack_name
, tmpfile
);
637 p
->pack_fd
= pack_fd
;
639 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
640 hdr
.hdr_version
= htonl(2);
642 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
645 pack_size
= sizeof(hdr
);
648 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
649 all_packs
[pack_id
] = p
;
652 static void fixup_header_footer(void)
654 static const int buf_sz
= 128 * 1024;
655 int pack_fd
= pack_data
->pack_fd
;
657 struct pack_header hdr
;
660 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
661 die("Failed seeking to start: %s", strerror(errno
));
662 if (read_in_full(pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
663 die("Unable to reread header of %s", pack_data
->pack_name
);
664 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
665 die("Failed seeking to start: %s", strerror(errno
));
666 hdr
.hdr_entries
= htonl(object_count
);
667 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
670 SHA1_Update(&c
, &hdr
, sizeof(hdr
));
672 buf
= xmalloc(buf_sz
);
674 size_t n
= xread(pack_fd
, buf
, buf_sz
);
678 die("Failed to checksum %s", pack_data
->pack_name
);
679 SHA1_Update(&c
, buf
, n
);
683 SHA1_Final(pack_data
->sha1
, &c
);
684 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
688 static int oecmp (const void *a_
, const void *b_
)
690 struct object_entry
*a
= *((struct object_entry
**)a_
);
691 struct object_entry
*b
= *((struct object_entry
**)b_
);
692 return hashcmp(a
->sha1
, b
->sha1
);
695 static char *create_index(void)
697 static char tmpfile
[PATH_MAX
];
700 struct object_entry
**idx
, **c
, **last
, *e
;
701 struct object_entry_pool
*o
;
705 /* Build the sorted table of object IDs. */
706 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
708 for (o
= blocks
; o
; o
= o
->next_pool
)
709 for (e
= o
->next_free
; e
-- != o
->entries
;)
710 if (pack_id
== e
->pack_id
)
712 last
= idx
+ object_count
;
714 die("internal consistency error creating the index");
715 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
717 /* Generate the fan-out array. */
719 for (i
= 0; i
< 256; i
++) {
720 struct object_entry
**next
= c
;;
721 while (next
< last
) {
722 if ((*next
)->sha1
[0] != i
)
726 array
[i
] = htonl(next
- idx
);
730 snprintf(tmpfile
, sizeof(tmpfile
),
731 "%s/index_XXXXXX", get_object_directory());
732 idx_fd
= mkstemp(tmpfile
);
734 die("Can't create %s: %s", tmpfile
, strerror(errno
));
735 f
= sha1fd(idx_fd
, tmpfile
);
736 sha1write(f
, array
, 256 * sizeof(int));
738 for (c
= idx
; c
!= last
; c
++) {
739 uint32_t offset
= htonl((*c
)->offset
);
740 sha1write(f
, &offset
, 4);
741 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
742 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
744 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
745 sha1close(f
, NULL
, 1);
747 SHA1_Final(pack_data
->sha1
, &ctx
);
751 static char *keep_pack(char *curr_index_name
)
753 static char name
[PATH_MAX
];
754 static char *keep_msg
= "fast-import";
757 chmod(pack_data
->pack_name
, 0444);
758 chmod(curr_index_name
, 0444);
760 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
761 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
762 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
764 die("cannot create keep file");
765 write(keep_fd
, keep_msg
, strlen(keep_msg
));
768 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
769 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
770 if (move_temp_to_file(pack_data
->pack_name
, name
))
771 die("cannot store pack file");
773 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
774 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
775 if (move_temp_to_file(curr_index_name
, name
))
776 die("cannot store index file");
780 static void unkeep_all_packs(void)
782 static char name
[PATH_MAX
];
785 for (k
= 0; k
< pack_id
; k
++) {
786 struct packed_git
*p
= all_packs
[k
];
787 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
788 get_object_directory(), sha1_to_hex(p
->sha1
));
793 static void end_packfile(void)
795 struct packed_git
*old_p
= pack_data
, *new_p
;
803 fixup_header_footer();
804 idx_name
= keep_pack(create_index());
806 /* Register the packfile with core git's machinary. */
807 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
809 die("core git rejected index %s", idx_name
);
810 new_p
->windows
= old_p
->windows
;
811 all_packs
[pack_id
] = new_p
;
812 install_packed_git(new_p
);
814 /* Print the boundary */
816 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
817 for (i
= 0; i
< branch_table_sz
; i
++) {
818 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
819 if (b
->pack_id
== pack_id
)
820 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
823 for (t
= first_tag
; t
; t
= t
->next_tag
) {
824 if (t
->pack_id
== pack_id
)
825 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
827 fputc('\n', pack_edges
);
834 unlink(old_p
->pack_name
);
837 /* We can't carry a delta across packfiles. */
838 free(last_blob
.data
);
839 last_blob
.data
= NULL
;
841 last_blob
.offset
= 0;
845 static void cycle_packfile(void)
851 static size_t encode_header(
852 enum object_type type
,
859 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
860 die("bad type %d", type
);
862 c
= (type
<< 4) | (size
& 15);
874 static int store_object(
875 enum object_type type
,
878 struct last_object
*last
,
879 unsigned char *sha1out
,
883 struct object_entry
*e
;
884 unsigned char hdr
[96];
885 unsigned char sha1
[20];
886 unsigned long hdrlen
, deltalen
;
890 hdrlen
= sprintf((char*)hdr
,"%s %lu", type_names
[type
],
891 (unsigned long)datlen
) + 1;
893 SHA1_Update(&c
, hdr
, hdrlen
);
894 SHA1_Update(&c
, dat
, datlen
);
895 SHA1_Final(sha1
, &c
);
897 hashcpy(sha1out
, sha1
);
899 e
= insert_object(sha1
);
901 insert_mark(mark
, e
);
903 duplicate_count_by_type
[type
]++;
907 if (last
&& last
->data
&& last
->depth
< max_depth
) {
908 delta
= diff_delta(last
->data
, last
->len
,
911 if (delta
&& deltalen
>= datlen
) {
918 memset(&s
, 0, sizeof(s
));
919 deflateInit(&s
, zlib_compression_level
);
922 s
.avail_in
= deltalen
;
927 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
928 s
.next_out
= out
= xmalloc(s
.avail_out
);
929 while (deflate(&s
, Z_FINISH
) == Z_OK
)
933 /* Determine if we should auto-checkpoint. */
934 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
935 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
937 /* This new object needs to *not* have the current pack_id. */
938 e
->pack_id
= pack_id
+ 1;
941 /* We cannot carry a delta into the new pack. */
946 memset(&s
, 0, sizeof(s
));
947 deflateInit(&s
, zlib_compression_level
);
950 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
951 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
952 while (deflate(&s
, Z_FINISH
) == Z_OK
)
959 e
->pack_id
= pack_id
;
960 e
->offset
= pack_size
;
962 object_count_by_type
[type
]++;
965 unsigned long ofs
= e
->offset
- last
->offset
;
966 unsigned pos
= sizeof(hdr
) - 1;
968 delta_count_by_type
[type
]++;
971 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
972 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
975 hdr
[pos
] = ofs
& 127;
977 hdr
[--pos
] = 128 | (--ofs
& 127);
978 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
979 pack_size
+= sizeof(hdr
) - pos
;
983 hdrlen
= encode_header(type
, datlen
, hdr
);
984 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
988 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
989 pack_size
+= s
.total_out
;
997 last
->offset
= e
->offset
;
1003 static void *gfi_unpack_entry(
1004 struct object_entry
*oe
,
1005 unsigned long *sizep
)
1007 static char type
[20];
1008 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1010 p
->pack_size
= pack_size
+ 20;
1011 return unpack_entry(p
, oe
->offset
, type
, sizep
);
1014 static const char *get_mode(const char *str
, uint16_t *modep
)
1019 while ((c
= *str
++) != ' ') {
1020 if (c
< '0' || c
> '7')
1022 mode
= (mode
<< 3) + (c
- '0');
1028 static void load_tree(struct tree_entry
*root
)
1030 unsigned char* sha1
= root
->versions
[1].sha1
;
1031 struct object_entry
*myoe
;
1032 struct tree_content
*t
;
1037 root
->tree
= t
= new_tree_content(8);
1038 if (is_null_sha1(sha1
))
1041 myoe
= find_object(sha1
);
1043 if (myoe
->type
!= OBJ_TREE
)
1044 die("Not a tree: %s", sha1_to_hex(sha1
));
1046 buf
= gfi_unpack_entry(myoe
, &size
);
1049 buf
= read_sha1_file(sha1
, type
, &size
);
1050 if (!buf
|| strcmp(type
, tree_type
))
1051 die("Can't load tree %s", sha1_to_hex(sha1
));
1055 while (c
!= (buf
+ size
)) {
1056 struct tree_entry
*e
= new_tree_entry();
1058 if (t
->entry_count
== t
->entry_capacity
)
1059 root
->tree
= t
= grow_tree_content(t
, 8);
1060 t
->entries
[t
->entry_count
++] = e
;
1063 c
= get_mode(c
, &e
->versions
[1].mode
);
1065 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1066 e
->versions
[0].mode
= e
->versions
[1].mode
;
1067 e
->name
= to_atom(c
, (unsigned short)strlen(c
));
1068 c
+= e
->name
->str_len
+ 1;
1069 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1070 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1076 static int tecmp0 (const void *_a
, const void *_b
)
1078 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1079 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1080 return base_name_compare(
1081 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1082 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1085 static int tecmp1 (const void *_a
, const void *_b
)
1087 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1088 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1089 return base_name_compare(
1090 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1091 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1094 static void mktree(struct tree_content
*t
,
1104 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1106 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1108 for (i
= 0; i
< t
->entry_count
; i
++) {
1109 if (t
->entries
[i
]->versions
[v
].mode
)
1110 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1113 size_dbuf(b
, maxlen
);
1115 for (i
= 0; i
< t
->entry_count
; i
++) {
1116 struct tree_entry
*e
= t
->entries
[i
];
1117 if (!e
->versions
[v
].mode
)
1119 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1121 strcpy(c
, e
->name
->str_dat
);
1122 c
+= e
->name
->str_len
+ 1;
1123 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1126 *szp
= c
- (char*)b
->buffer
;
1129 static void store_tree(struct tree_entry
*root
)
1131 struct tree_content
*t
= root
->tree
;
1132 unsigned int i
, j
, del
;
1133 unsigned long new_len
;
1134 struct last_object lo
;
1135 struct object_entry
*le
;
1137 if (!is_null_sha1(root
->versions
[1].sha1
))
1140 for (i
= 0; i
< t
->entry_count
; i
++) {
1141 if (t
->entries
[i
]->tree
)
1142 store_tree(t
->entries
[i
]);
1145 le
= find_object(root
->versions
[0].sha1
);
1146 if (!S_ISDIR(root
->versions
[0].mode
)
1148 || le
->pack_id
!= pack_id
) {
1152 mktree(t
, 0, &lo
.len
, &old_tree
);
1153 lo
.data
= old_tree
.buffer
;
1154 lo
.offset
= le
->offset
;
1155 lo
.depth
= t
->delta_depth
;
1159 mktree(t
, 1, &new_len
, &new_tree
);
1160 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1161 &lo
, root
->versions
[1].sha1
, 0);
1163 t
->delta_depth
= lo
.depth
;
1164 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1165 struct tree_entry
*e
= t
->entries
[i
];
1166 if (e
->versions
[1].mode
) {
1167 e
->versions
[0].mode
= e
->versions
[1].mode
;
1168 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1169 t
->entries
[j
++] = e
;
1171 release_tree_entry(e
);
1175 t
->entry_count
-= del
;
1178 static int tree_content_set(
1179 struct tree_entry
*root
,
1181 const unsigned char *sha1
,
1182 const uint16_t mode
)
1184 struct tree_content
*t
= root
->tree
;
1187 struct tree_entry
*e
;
1189 slash1
= strchr(p
, '/');
1195 for (i
= 0; i
< t
->entry_count
; i
++) {
1197 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1199 if (e
->versions
[1].mode
== mode
1200 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1202 e
->versions
[1].mode
= mode
;
1203 hashcpy(e
->versions
[1].sha1
, sha1
);
1205 release_tree_content_recursive(e
->tree
);
1208 hashclr(root
->versions
[1].sha1
);
1211 if (!S_ISDIR(e
->versions
[1].mode
)) {
1212 e
->tree
= new_tree_content(8);
1213 e
->versions
[1].mode
= S_IFDIR
;
1217 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1218 hashclr(root
->versions
[1].sha1
);
1225 if (t
->entry_count
== t
->entry_capacity
)
1226 root
->tree
= t
= grow_tree_content(t
, 8);
1227 e
= new_tree_entry();
1228 e
->name
= to_atom(p
, (unsigned short)n
);
1229 e
->versions
[0].mode
= 0;
1230 hashclr(e
->versions
[0].sha1
);
1231 t
->entries
[t
->entry_count
++] = e
;
1233 e
->tree
= new_tree_content(8);
1234 e
->versions
[1].mode
= S_IFDIR
;
1235 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1238 e
->versions
[1].mode
= mode
;
1239 hashcpy(e
->versions
[1].sha1
, sha1
);
1241 hashclr(root
->versions
[1].sha1
);
1245 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1247 struct tree_content
*t
= root
->tree
;
1250 struct tree_entry
*e
;
1252 slash1
= strchr(p
, '/');
1258 for (i
= 0; i
< t
->entry_count
; i
++) {
1260 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1261 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1265 if (tree_content_remove(e
, slash1
+ 1)) {
1266 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1267 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1268 hashclr(root
->versions
[1].sha1
);
1281 release_tree_content_recursive(e
->tree
);
1284 e
->versions
[1].mode
= 0;
1285 hashclr(e
->versions
[1].sha1
);
1286 hashclr(root
->versions
[1].sha1
);
1290 static int update_branch(struct branch
*b
)
1292 static const char *msg
= "fast-import";
1293 struct ref_lock
*lock
;
1294 unsigned char old_sha1
[20];
1296 if (read_ref(b
->name
, old_sha1
))
1298 lock
= lock_any_ref_for_update(b
->name
, old_sha1
);
1300 return error("Unable to lock %s", b
->name
);
1301 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1302 struct commit
*old_cmit
, *new_cmit
;
1304 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1305 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1306 if (!old_cmit
|| !new_cmit
) {
1308 return error("Branch %s is missing commits.", b
->name
);
1311 if (!in_merge_bases(old_cmit
, new_cmit
)) {
1313 warn("Not updating %s"
1314 " (new tip %s does not contain %s)",
1315 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1319 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1320 return error("Unable to update %s", b
->name
);
1324 static void dump_branches(void)
1329 for (i
= 0; i
< branch_table_sz
; i
++) {
1330 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1331 failure
|= update_branch(b
);
1335 static void dump_tags(void)
1337 static const char *msg
= "fast-import";
1339 struct ref_lock
*lock
;
1340 char ref_name
[PATH_MAX
];
1342 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1343 sprintf(ref_name
, "tags/%s", t
->name
);
1344 lock
= lock_ref_sha1(ref_name
, NULL
);
1345 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1346 failure
|= error("Unable to update %s", ref_name
);
1350 static void dump_marks_helper(FILE *f
,
1356 for (k
= 0; k
< 1024; k
++) {
1357 if (m
->data
.sets
[k
])
1358 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1362 for (k
= 0; k
< 1024; k
++) {
1363 if (m
->data
.marked
[k
])
1364 fprintf(f
, ":%ju %s\n", base
+ k
,
1365 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1370 static void dump_marks(void)
1374 FILE *f
= fopen(mark_file
, "w");
1376 dump_marks_helper(f
, 0, marks
);
1379 failure
|= error("Unable to write marks file %s: %s",
1380 mark_file
, strerror(errno
));
1384 static void read_next_command(void)
1386 read_line(&command_buf
, stdin
, '\n');
1389 static void cmd_mark(void)
1391 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1392 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1393 read_next_command();
1399 static void *cmd_data (size_t *size
)
1404 if (strncmp("data ", command_buf
.buf
, 5))
1405 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1407 if (!strncmp("<<", command_buf
.buf
+ 5, 2)) {
1408 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1409 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1411 buffer
= xmalloc(sz
);
1413 read_next_command();
1414 if (command_buf
.eof
)
1415 die("EOF in data (terminator '%s' not found)", term
);
1416 if (term_len
== command_buf
.len
1417 && !strcmp(term
, command_buf
.buf
))
1419 if (sz
< (length
+ command_buf
.len
)) {
1420 sz
= sz
* 3 / 2 + 16;
1421 if (sz
< (length
+ command_buf
.len
))
1422 sz
= length
+ command_buf
.len
;
1423 buffer
= xrealloc(buffer
, sz
);
1425 memcpy(buffer
+ length
,
1427 command_buf
.len
- 1);
1428 length
+= command_buf
.len
- 1;
1429 buffer
[length
++] = '\n';
1435 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1436 buffer
= xmalloc(length
);
1437 while (n
< length
) {
1438 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1439 if (!s
&& feof(stdin
))
1440 die("EOF in data (%lu bytes remaining)",
1441 (unsigned long)(length
- n
));
1446 if (fgetc(stdin
) != '\n')
1447 die("An lf did not trail the binary data as expected.");
1453 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1455 const char *orig_src
= src
;
1458 strtoul(src
, &endp
, 10);
1459 if (endp
== src
|| *endp
!= ' ')
1463 if (*src
!= '-' && *src
!= '+')
1467 strtoul(src
+ 1, &endp
, 10);
1468 if (endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1471 strcpy(result
, orig_src
);
1475 static char *parse_ident(const char *buf
)
1481 gt
= strrchr(buf
, '>');
1483 die("Missing > in ident string: %s", buf
);
1486 die("Missing space after > in ident string: %s", buf
);
1488 name_len
= gt
- buf
;
1489 ident
= xmalloc(name_len
+ 24);
1490 strncpy(ident
, buf
, name_len
);
1494 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1495 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1497 case WHENSPEC_RFC2822
:
1498 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1499 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1502 if (strcmp("now", gt
))
1503 die("Date in ident must be 'now': %s", buf
);
1504 datestamp(ident
+ name_len
, 24);
1511 static void cmd_new_blob(void)
1516 read_next_command();
1520 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1524 static void unload_one_branch(void)
1526 while (cur_active_branches
1527 && cur_active_branches
>= max_active_branches
) {
1528 unsigned long min_commit
= ULONG_MAX
;
1529 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1531 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1532 if (e
->last_commit
< min_commit
) {
1534 min_commit
= e
->last_commit
;
1540 e
= p
->active_next_branch
;
1541 p
->active_next_branch
= e
->active_next_branch
;
1543 e
= active_branches
;
1544 active_branches
= e
->active_next_branch
;
1546 e
->active_next_branch
= NULL
;
1547 if (e
->branch_tree
.tree
) {
1548 release_tree_content_recursive(e
->branch_tree
.tree
);
1549 e
->branch_tree
.tree
= NULL
;
1551 cur_active_branches
--;
1555 static void load_branch(struct branch
*b
)
1557 load_tree(&b
->branch_tree
);
1558 b
->active_next_branch
= active_branches
;
1559 active_branches
= b
;
1560 cur_active_branches
++;
1561 branch_load_count
++;
1564 static void file_change_m(struct branch
*b
)
1566 const char *p
= command_buf
.buf
+ 2;
1569 struct object_entry
*oe
= oe
;
1570 unsigned char sha1
[20];
1571 uint16_t mode
, inline_data
= 0;
1574 p
= get_mode(p
, &mode
);
1576 die("Corrupt mode: %s", command_buf
.buf
);
1578 case S_IFREG
| 0644:
1579 case S_IFREG
| 0755:
1586 die("Corrupt mode: %s", command_buf
.buf
);
1591 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1592 hashcpy(sha1
, oe
->sha1
);
1594 } else if (!strncmp("inline", p
, 6)) {
1598 if (get_sha1_hex(p
, sha1
))
1599 die("Invalid SHA1: %s", command_buf
.buf
);
1600 oe
= find_object(sha1
);
1604 die("Missing space after SHA1: %s", command_buf
.buf
);
1606 p_uq
= unquote_c_style(p
, &endp
);
1609 die("Garbage after path in: %s", command_buf
.buf
);
1617 p
= p_uq
= xstrdup(p
);
1618 read_next_command();
1620 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1623 if (oe
->type
!= OBJ_BLOB
)
1624 die("Not a blob (actually a %s): %s",
1625 command_buf
.buf
, type_names
[oe
->type
]);
1627 if (sha1_object_info(sha1
, type
, NULL
))
1628 die("Blob not found: %s", command_buf
.buf
);
1629 if (strcmp(blob_type
, type
))
1630 die("Not a blob (actually a %s): %s",
1631 command_buf
.buf
, type
);
1634 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1638 static void file_change_d(struct branch
*b
)
1640 const char *p
= command_buf
.buf
+ 2;
1644 p_uq
= unquote_c_style(p
, &endp
);
1647 die("Garbage after path in: %s", command_buf
.buf
);
1650 tree_content_remove(&b
->branch_tree
, p
);
1654 static void file_change_deleteall(struct branch
*b
)
1656 release_tree_content_recursive(b
->branch_tree
.tree
);
1657 hashclr(b
->branch_tree
.versions
[0].sha1
);
1658 hashclr(b
->branch_tree
.versions
[1].sha1
);
1659 load_tree(&b
->branch_tree
);
1662 static void cmd_from(struct branch
*b
)
1667 if (strncmp("from ", command_buf
.buf
, 5))
1671 die("Can't reinitailize branch %s", b
->name
);
1673 from
= strchr(command_buf
.buf
, ' ') + 1;
1674 s
= lookup_branch(from
);
1676 die("Can't create a branch from itself: %s", b
->name
);
1678 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1679 hashcpy(b
->sha1
, s
->sha1
);
1680 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1681 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1682 } else if (*from
== ':') {
1683 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1684 struct object_entry
*oe
= find_mark(idnum
);
1687 if (oe
->type
!= OBJ_COMMIT
)
1688 die("Mark :%ju not a commit", idnum
);
1689 hashcpy(b
->sha1
, oe
->sha1
);
1690 buf
= gfi_unpack_entry(oe
, &size
);
1691 if (!buf
|| size
< 46)
1692 die("Not a valid commit: %s", from
);
1693 if (memcmp("tree ", buf
, 5)
1694 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1695 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1697 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1698 b
->branch_tree
.versions
[1].sha1
);
1699 } else if (!get_sha1(from
, b
->sha1
)) {
1700 if (is_null_sha1(b
->sha1
)) {
1701 hashclr(b
->branch_tree
.versions
[0].sha1
);
1702 hashclr(b
->branch_tree
.versions
[1].sha1
);
1707 buf
= read_object_with_reference(b
->sha1
,
1708 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1709 if (!buf
|| size
< 46)
1710 die("Not a valid commit: %s", from
);
1711 if (memcmp("tree ", buf
, 5)
1712 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1713 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1715 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1716 b
->branch_tree
.versions
[1].sha1
);
1719 die("Invalid ref name or SHA1 expression: %s", from
);
1721 read_next_command();
1724 static struct hash_list
*cmd_merge(unsigned int *count
)
1726 struct hash_list
*list
= NULL
, *n
, *e
= e
;
1731 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1732 from
= strchr(command_buf
.buf
, ' ') + 1;
1733 n
= xmalloc(sizeof(*n
));
1734 s
= lookup_branch(from
);
1736 hashcpy(n
->sha1
, s
->sha1
);
1737 else if (*from
== ':') {
1738 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1739 struct object_entry
*oe
= find_mark(idnum
);
1740 if (oe
->type
!= OBJ_COMMIT
)
1741 die("Mark :%ju not a commit", idnum
);
1742 hashcpy(n
->sha1
, oe
->sha1
);
1743 } else if (get_sha1(from
, n
->sha1
))
1744 die("Invalid ref name or SHA1 expression: %s", from
);
1753 read_next_command();
1758 static void cmd_new_commit(void)
1764 char *author
= NULL
;
1765 char *committer
= NULL
;
1766 struct hash_list
*merge_list
= NULL
;
1767 unsigned int merge_count
;
1769 /* Obtain the branch name from the rest of our command */
1770 sp
= strchr(command_buf
.buf
, ' ') + 1;
1771 b
= lookup_branch(sp
);
1775 read_next_command();
1777 if (!strncmp("author ", command_buf
.buf
, 7)) {
1778 author
= parse_ident(command_buf
.buf
+ 7);
1779 read_next_command();
1781 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1782 committer
= parse_ident(command_buf
.buf
+ 10);
1783 read_next_command();
1786 die("Expected committer but didn't get one");
1787 msg
= cmd_data(&msglen
);
1788 read_next_command();
1790 merge_list
= cmd_merge(&merge_count
);
1792 /* ensure the branch is active/loaded */
1793 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1794 unload_one_branch();
1800 if (1 == command_buf
.len
)
1802 else if (!strncmp("M ", command_buf
.buf
, 2))
1804 else if (!strncmp("D ", command_buf
.buf
, 2))
1806 else if (!strcmp("deleteall", command_buf
.buf
))
1807 file_change_deleteall(b
);
1809 die("Unsupported file_change: %s", command_buf
.buf
);
1810 read_next_command();
1813 /* build the tree and the commit */
1814 store_tree(&b
->branch_tree
);
1815 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1816 b
->branch_tree
.versions
[1].sha1
);
1817 size_dbuf(&new_data
, 114 + msglen
1820 ? strlen(author
) + strlen(committer
)
1821 : 2 * strlen(committer
)));
1822 sp
= new_data
.buffer
;
1823 sp
+= sprintf(sp
, "tree %s\n",
1824 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1825 if (!is_null_sha1(b
->sha1
))
1826 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1827 while (merge_list
) {
1828 struct hash_list
*next
= merge_list
->next
;
1829 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1833 sp
+= sprintf(sp
, "author %s\n", author
? author
: committer
);
1834 sp
+= sprintf(sp
, "committer %s\n", committer
);
1836 memcpy(sp
, msg
, msglen
);
1842 if (!store_object(OBJ_COMMIT
,
1843 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1844 NULL
, b
->sha1
, next_mark
))
1845 b
->pack_id
= pack_id
;
1846 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1849 static void cmd_new_tag(void)
1858 uintmax_t from_mark
= 0;
1859 unsigned char sha1
[20];
1861 /* Obtain the new tag name from the rest of our command */
1862 sp
= strchr(command_buf
.buf
, ' ') + 1;
1863 t
= pool_alloc(sizeof(struct tag
));
1865 t
->name
= pool_strdup(sp
);
1867 last_tag
->next_tag
= t
;
1871 read_next_command();
1874 if (strncmp("from ", command_buf
.buf
, 5))
1875 die("Expected from command, got %s", command_buf
.buf
);
1876 from
= strchr(command_buf
.buf
, ' ') + 1;
1877 s
= lookup_branch(from
);
1879 hashcpy(sha1
, s
->sha1
);
1880 } else if (*from
== ':') {
1881 struct object_entry
*oe
;
1882 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1883 oe
= find_mark(from_mark
);
1884 if (oe
->type
!= OBJ_COMMIT
)
1885 die("Mark :%ju not a commit", from_mark
);
1886 hashcpy(sha1
, oe
->sha1
);
1887 } else if (!get_sha1(from
, sha1
)) {
1891 buf
= read_object_with_reference(sha1
,
1892 type_names
[OBJ_COMMIT
], &size
, sha1
);
1893 if (!buf
|| size
< 46)
1894 die("Not a valid commit: %s", from
);
1897 die("Invalid ref name or SHA1 expression: %s", from
);
1898 read_next_command();
1901 if (strncmp("tagger ", command_buf
.buf
, 7))
1902 die("Expected tagger command, got %s", command_buf
.buf
);
1903 tagger
= parse_ident(command_buf
.buf
+ 7);
1905 /* tag payload/message */
1906 read_next_command();
1907 msg
= cmd_data(&msglen
);
1909 /* build the tag object */
1910 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1911 sp
= new_data
.buffer
;
1912 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1913 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1914 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1915 sp
+= sprintf(sp
, "tagger %s\n", tagger
);
1917 memcpy(sp
, msg
, msglen
);
1922 if (store_object(OBJ_TAG
, new_data
.buffer
,
1923 sp
- (char*)new_data
.buffer
,
1925 t
->pack_id
= MAX_PACK_ID
;
1927 t
->pack_id
= pack_id
;
1930 static void cmd_reset_branch(void)
1935 /* Obtain the branch name from the rest of our command */
1936 sp
= strchr(command_buf
.buf
, ' ') + 1;
1937 b
= lookup_branch(sp
);
1940 if (b
->branch_tree
.tree
) {
1941 release_tree_content_recursive(b
->branch_tree
.tree
);
1942 b
->branch_tree
.tree
= NULL
;
1947 read_next_command();
1951 static void cmd_checkpoint(void)
1959 read_next_command();
1962 static const char fast_import_usage
[] =
1963 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
1965 int main(int argc
, const char **argv
)
1967 int i
, show_stats
= 1;
1969 git_config(git_default_config
);
1971 for (i
= 1; i
< argc
; i
++) {
1972 const char *a
= argv
[i
];
1974 if (*a
!= '-' || !strcmp(a
, "--"))
1976 else if (!strncmp(a
, "--date-format=", 14)) {
1977 const char *fmt
= a
+ 14;
1978 if (!strcmp(fmt
, "raw"))
1979 whenspec
= WHENSPEC_RAW
;
1980 else if (!strcmp(fmt
, "rfc2822"))
1981 whenspec
= WHENSPEC_RFC2822
;
1982 else if (!strcmp(fmt
, "now"))
1983 whenspec
= WHENSPEC_NOW
;
1985 die("unknown --date-format argument %s", fmt
);
1987 else if (!strncmp(a
, "--max-pack-size=", 16))
1988 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1989 else if (!strncmp(a
, "--depth=", 8))
1990 max_depth
= strtoul(a
+ 8, NULL
, 0);
1991 else if (!strncmp(a
, "--active-branches=", 18))
1992 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1993 else if (!strncmp(a
, "--export-marks=", 15))
1995 else if (!strncmp(a
, "--export-pack-edges=", 20)) {
1998 pack_edges
= fopen(a
+ 20, "a");
2000 die("Cannot open %s: %s", a
+ 20, strerror(errno
));
2001 } else if (!strcmp(a
, "--force"))
2003 else if (!strcmp(a
, "--quiet"))
2005 else if (!strcmp(a
, "--stats"))
2008 die("unknown option %s", a
);
2011 usage(fast_import_usage
);
2013 alloc_objects(object_entry_alloc
);
2014 strbuf_init(&command_buf
);
2016 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2017 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2018 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2019 marks
= pool_calloc(1, sizeof(struct mark_set
));
2023 read_next_command();
2024 if (command_buf
.eof
)
2026 else if (!strcmp("blob", command_buf
.buf
))
2028 else if (!strncmp("commit ", command_buf
.buf
, 7))
2030 else if (!strncmp("tag ", command_buf
.buf
, 4))
2032 else if (!strncmp("reset ", command_buf
.buf
, 6))
2034 else if (!strcmp("checkpoint", command_buf
.buf
))
2037 die("Unsupported command: %s", command_buf
.buf
);
2050 uintmax_t total_count
= 0, duplicate_count
= 0;
2051 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2052 total_count
+= object_count_by_type
[i
];
2053 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2054 duplicate_count
+= duplicate_count_by_type
[i
];
2056 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2057 fprintf(stderr
, "---------------------------------------------------------------------\n");
2058 fprintf(stderr
, "Alloc'd objects: %10ju\n", alloc_count
);
2059 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
2060 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
]);
2061 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
]);
2062 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
]);
2063 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
]);
2064 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2065 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2066 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2067 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2068 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
2069 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2070 fprintf(stderr
, "---------------------------------------------------------------------\n");
2072 fprintf(stderr
, "---------------------------------------------------------------------\n");
2073 fprintf(stderr
, "\n");
2076 return failure
? 1 : 0;