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
35 file_clr ::= 'deleteall' lf;
36 file_del ::= 'D' sp path_str lf;
37 file_rnm ::= 'R' sp path_str sp path_str lf;
38 file_cpy ::= 'C' sp path_str sp path_str lf;
39 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
40 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
43 new_tag ::= 'tag' sp tag_str lf
44 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
45 'tagger' sp name '<' email '>' when lf
49 reset_branch ::= 'reset' sp ref_str lf
50 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
53 checkpoint ::= 'checkpoint' lf
56 # note: the first idnum in a stream should be 1 and subsequent
57 # idnums should not have gaps between values as this will cause
58 # the stream parser to reserve space for the gapped values. An
59 # idnum can be updated in the future to a new object by issuing
60 # a new mark directive with the old idnum.
62 mark ::= 'mark' sp idnum lf;
63 data ::= (delimited_data | exact_data)
66 # note: delim may be any string but must not contain lf.
67 # data_line may contain any data but must not be exactly
69 delimited_data ::= 'data' sp '<<' delim lf
73 # note: declen indicates the length of binary_data in bytes.
74 # declen does not include the lf preceeding the binary data.
76 exact_data ::= 'data' sp declen lf
79 # note: quoted strings are C-style quoting supporting \c for
80 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
81 # is the signed byte value in octal. Note that the only
82 # characters which must actually be escaped to protect the
83 # stream formatting is: \, " and LF. Otherwise these values
87 sha1exp_str ::= sha1exp;
89 path_str ::= path | '"' quoted(path) '"' ;
90 mode ::= '100644' | '644'
95 declen ::= # unsigned 32 bit value, ascii base10 notation;
96 bigint ::= # unsigned integer value, ascii base10 notation;
97 binary_data ::= # file content, not interpreted;
99 when ::= raw_when | rfc2822_when;
100 raw_when ::= ts sp tz;
101 rfc2822_when ::= # Valid RFC 2822 date and time;
103 sp ::= # ASCII space character;
104 lf ::= # ASCII newline (LF) character;
106 # note: a colon (':') must precede the numerical value assigned to
107 # an idnum. This is to distinguish it from a ref or tag name as
108 # GIT does not permit ':' in ref or tag strings.
110 idnum ::= ':' bigint;
111 path ::= # GIT style file path, e.g. "a/b/c";
112 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
113 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
114 sha1exp ::= # Any valid GIT SHA1 expression;
115 hexsha1 ::= # SHA1 in hexadecimal format;
117 # note: name and email are UTF8 strings, however name must not
118 # contain '<' or lf and email must not contain any of the
119 # following: '<', '>', lf.
121 name ::= # valid GIT author/committer name;
122 email ::= # valid GIT author/committer email;
123 ts ::= # time since the epoch in seconds, ascii base10 notation;
124 tz ::= # GIT style timezone;
126 # note: comments may appear anywhere in the input, except
127 # within a data command. Any form of the data command
128 # always escapes the related input from comment processing.
130 # In case it is not clear, the '#' that starts the comment
131 # must be the first character on that the line (an lf have
134 comment ::= '#' not_lf* lf;
135 not_lf ::= # Any byte that is not ASCII newline (LF);
147 #include "csum-file.h"
151 #define PACK_ID_BITS 16
152 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
156 struct object_entry
*next
;
158 unsigned type
: TYPE_BITS
;
159 unsigned pack_id
: PACK_ID_BITS
;
160 unsigned char sha1
[20];
163 struct object_entry_pool
165 struct object_entry_pool
*next_pool
;
166 struct object_entry
*next_free
;
167 struct object_entry
*end
;
168 struct object_entry entries
[FLEX_ARRAY
]; /* more */
174 struct object_entry
*marked
[1024];
175 struct mark_set
*sets
[1024];
191 struct mem_pool
*next_pool
;
194 char space
[FLEX_ARRAY
]; /* more */
199 struct atom_str
*next_atom
;
200 unsigned short str_len
;
201 char str_dat
[FLEX_ARRAY
]; /* more */
207 struct tree_content
*tree
;
208 struct atom_str
* name
;
212 unsigned char sha1
[20];
218 unsigned int entry_capacity
; /* must match avail_tree_content */
219 unsigned int entry_count
;
220 unsigned int delta_depth
;
221 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
224 struct avail_tree_content
226 unsigned int entry_capacity
; /* must match tree_content */
227 struct avail_tree_content
*next_avail
;
232 struct branch
*table_next_branch
;
233 struct branch
*active_next_branch
;
235 struct tree_entry branch_tree
;
236 uintmax_t last_commit
;
238 unsigned pack_id
: PACK_ID_BITS
;
239 unsigned char sha1
[20];
244 struct tag
*next_tag
;
246 unsigned int pack_id
;
247 unsigned char sha1
[20];
258 struct hash_list
*next
;
259 unsigned char sha1
[20];
268 /* Configured limits on output */
269 static unsigned long max_depth
= 10;
270 static off_t max_packsize
= (1LL << 32) - 1;
271 static int force_update
;
273 /* Stats and misc. counters */
274 static uintmax_t alloc_count
;
275 static uintmax_t marks_set_count
;
276 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
277 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
278 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
279 static unsigned long object_count
;
280 static unsigned long branch_count
;
281 static unsigned long branch_load_count
;
283 static FILE *pack_edges
;
286 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
287 static size_t total_allocd
;
288 static struct mem_pool
*mem_pool
;
290 /* Atom management */
291 static unsigned int atom_table_sz
= 4451;
292 static unsigned int atom_cnt
;
293 static struct atom_str
**atom_table
;
295 /* The .pack file being generated */
296 static unsigned int pack_id
;
297 static struct packed_git
*pack_data
;
298 static struct packed_git
**all_packs
;
299 static unsigned long pack_size
;
301 /* Table of objects we've written. */
302 static unsigned int object_entry_alloc
= 5000;
303 static struct object_entry_pool
*blocks
;
304 static struct object_entry
*object_table
[1 << 16];
305 static struct mark_set
*marks
;
306 static const char* mark_file
;
309 static struct last_object last_blob
;
311 /* Tree management */
312 static unsigned int tree_entry_alloc
= 1000;
313 static void *avail_tree_entry
;
314 static unsigned int avail_tree_table_sz
= 100;
315 static struct avail_tree_content
**avail_tree_table
;
316 static struct dbuf old_tree
;
317 static struct dbuf new_tree
;
320 static unsigned long max_active_branches
= 5;
321 static unsigned long cur_active_branches
;
322 static unsigned long branch_table_sz
= 1039;
323 static struct branch
**branch_table
;
324 static struct branch
*active_branches
;
327 static struct tag
*first_tag
;
328 static struct tag
*last_tag
;
330 /* Input stream parsing */
331 static whenspec_type whenspec
= WHENSPEC_RAW
;
332 static struct strbuf command_buf
;
333 static int unread_command_buf
;
334 static uintmax_t next_mark
;
335 static struct dbuf new_data
;
338 static void alloc_objects(unsigned int cnt
)
340 struct object_entry_pool
*b
;
342 b
= xmalloc(sizeof(struct object_entry_pool
)
343 + cnt
* sizeof(struct object_entry
));
344 b
->next_pool
= blocks
;
345 b
->next_free
= b
->entries
;
346 b
->end
= b
->entries
+ cnt
;
351 static struct object_entry
*new_object(unsigned char *sha1
)
353 struct object_entry
*e
;
355 if (blocks
->next_free
== blocks
->end
)
356 alloc_objects(object_entry_alloc
);
358 e
= blocks
->next_free
++;
359 hashcpy(e
->sha1
, sha1
);
363 static struct object_entry
*find_object(unsigned char *sha1
)
365 unsigned int h
= sha1
[0] << 8 | sha1
[1];
366 struct object_entry
*e
;
367 for (e
= object_table
[h
]; e
; e
= e
->next
)
368 if (!hashcmp(sha1
, e
->sha1
))
373 static struct object_entry
*insert_object(unsigned char *sha1
)
375 unsigned int h
= sha1
[0] << 8 | sha1
[1];
376 struct object_entry
*e
= object_table
[h
];
377 struct object_entry
*p
= NULL
;
380 if (!hashcmp(sha1
, e
->sha1
))
386 e
= new_object(sha1
);
396 static unsigned int hc_str(const char *s
, size_t len
)
404 static void *pool_alloc(size_t len
)
409 for (p
= mem_pool
; p
; p
= p
->next_pool
)
410 if ((p
->end
- p
->next_free
>= len
))
414 if (len
>= (mem_pool_alloc
/2)) {
418 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
419 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
420 p
->next_pool
= mem_pool
;
421 p
->next_free
= p
->space
;
422 p
->end
= p
->next_free
+ mem_pool_alloc
;
427 /* round out to a pointer alignment */
428 if (len
& (sizeof(void*) - 1))
429 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
434 static void *pool_calloc(size_t count
, size_t size
)
436 size_t len
= count
* size
;
437 void *r
= pool_alloc(len
);
442 static char *pool_strdup(const char *s
)
444 char *r
= pool_alloc(strlen(s
) + 1);
449 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
452 if (b
->capacity
>= maxlen
)
456 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
457 b
->buffer
= xmalloc(b
->capacity
);
460 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
462 struct mark_set
*s
= marks
;
463 while ((idnum
>> s
->shift
) >= 1024) {
464 s
= pool_calloc(1, sizeof(struct mark_set
));
465 s
->shift
= marks
->shift
+ 10;
466 s
->data
.sets
[0] = marks
;
470 uintmax_t i
= idnum
>> s
->shift
;
471 idnum
-= i
<< s
->shift
;
472 if (!s
->data
.sets
[i
]) {
473 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
474 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
478 if (!s
->data
.marked
[idnum
])
480 s
->data
.marked
[idnum
] = oe
;
483 static struct object_entry
*find_mark(uintmax_t idnum
)
485 uintmax_t orig_idnum
= idnum
;
486 struct mark_set
*s
= marks
;
487 struct object_entry
*oe
= NULL
;
488 if ((idnum
>> s
->shift
) < 1024) {
489 while (s
&& s
->shift
) {
490 uintmax_t i
= idnum
>> s
->shift
;
491 idnum
-= i
<< s
->shift
;
495 oe
= s
->data
.marked
[idnum
];
498 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
502 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
504 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
507 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
508 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
511 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
513 strncpy(c
->str_dat
, s
, len
);
515 c
->next_atom
= atom_table
[hc
];
521 static struct branch
*lookup_branch(const char *name
)
523 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
526 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
527 if (!strcmp(name
, b
->name
))
532 static struct branch
*new_branch(const char *name
)
534 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
535 struct branch
* b
= lookup_branch(name
);
538 die("Invalid attempt to create duplicate branch: %s", name
);
539 switch (check_ref_format(name
)) {
540 case 0: break; /* its valid */
541 case -2: break; /* valid, but too few '/', allow anyway */
543 die("Branch name doesn't conform to GIT standards: %s", name
);
546 b
= pool_calloc(1, sizeof(struct branch
));
547 b
->name
= pool_strdup(name
);
548 b
->table_next_branch
= branch_table
[hc
];
549 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
550 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
552 b
->pack_id
= MAX_PACK_ID
;
553 branch_table
[hc
] = b
;
558 static unsigned int hc_entries(unsigned int cnt
)
560 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
561 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
564 static struct tree_content
*new_tree_content(unsigned int cnt
)
566 struct avail_tree_content
*f
, *l
= NULL
;
567 struct tree_content
*t
;
568 unsigned int hc
= hc_entries(cnt
);
570 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
571 if (f
->entry_capacity
>= cnt
)
576 l
->next_avail
= f
->next_avail
;
578 avail_tree_table
[hc
] = f
->next_avail
;
580 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
581 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
582 f
->entry_capacity
= cnt
;
585 t
= (struct tree_content
*)f
;
591 static void release_tree_entry(struct tree_entry
*e
);
592 static void release_tree_content(struct tree_content
*t
)
594 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
595 unsigned int hc
= hc_entries(f
->entry_capacity
);
596 f
->next_avail
= avail_tree_table
[hc
];
597 avail_tree_table
[hc
] = f
;
600 static void release_tree_content_recursive(struct tree_content
*t
)
603 for (i
= 0; i
< t
->entry_count
; i
++)
604 release_tree_entry(t
->entries
[i
]);
605 release_tree_content(t
);
608 static struct tree_content
*grow_tree_content(
609 struct tree_content
*t
,
612 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
613 r
->entry_count
= t
->entry_count
;
614 r
->delta_depth
= t
->delta_depth
;
615 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
616 release_tree_content(t
);
620 static struct tree_entry
*new_tree_entry(void)
622 struct tree_entry
*e
;
624 if (!avail_tree_entry
) {
625 unsigned int n
= tree_entry_alloc
;
626 total_allocd
+= n
* sizeof(struct tree_entry
);
627 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
629 *((void**)e
) = e
+ 1;
635 e
= avail_tree_entry
;
636 avail_tree_entry
= *((void**)e
);
640 static void release_tree_entry(struct tree_entry
*e
)
643 release_tree_content_recursive(e
->tree
);
644 *((void**)e
) = avail_tree_entry
;
645 avail_tree_entry
= e
;
648 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
650 struct tree_content
*d
;
651 struct tree_entry
*a
, *b
;
656 d
= new_tree_content(s
->entry_count
);
657 for (i
= 0; i
< s
->entry_count
; i
++) {
659 b
= new_tree_entry();
660 memcpy(b
, a
, sizeof(*a
));
661 if (a
->tree
&& is_null_sha1(b
->versions
[1].sha1
))
662 b
->tree
= dup_tree_content(a
->tree
);
667 d
->entry_count
= s
->entry_count
;
668 d
->delta_depth
= s
->delta_depth
;
673 static void start_packfile(void)
675 static char tmpfile
[PATH_MAX
];
676 struct packed_git
*p
;
677 struct pack_header hdr
;
680 snprintf(tmpfile
, sizeof(tmpfile
),
681 "%s/tmp_pack_XXXXXX", get_object_directory());
682 pack_fd
= xmkstemp(tmpfile
);
683 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
684 strcpy(p
->pack_name
, tmpfile
);
685 p
->pack_fd
= pack_fd
;
687 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
688 hdr
.hdr_version
= htonl(2);
690 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
693 pack_size
= sizeof(hdr
);
696 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
697 all_packs
[pack_id
] = p
;
700 static int oecmp (const void *a_
, const void *b_
)
702 struct object_entry
*a
= *((struct object_entry
**)a_
);
703 struct object_entry
*b
= *((struct object_entry
**)b_
);
704 return hashcmp(a
->sha1
, b
->sha1
);
707 static char *create_index(void)
709 static char tmpfile
[PATH_MAX
];
712 struct object_entry
**idx
, **c
, **last
, *e
;
713 struct object_entry_pool
*o
;
717 /* Build the sorted table of object IDs. */
718 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
720 for (o
= blocks
; o
; o
= o
->next_pool
)
721 for (e
= o
->next_free
; e
-- != o
->entries
;)
722 if (pack_id
== e
->pack_id
)
724 last
= idx
+ object_count
;
726 die("internal consistency error creating the index");
727 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
729 /* Generate the fan-out array. */
731 for (i
= 0; i
< 256; i
++) {
732 struct object_entry
**next
= c
;;
733 while (next
< last
) {
734 if ((*next
)->sha1
[0] != i
)
738 array
[i
] = htonl(next
- idx
);
742 snprintf(tmpfile
, sizeof(tmpfile
),
743 "%s/tmp_idx_XXXXXX", get_object_directory());
744 idx_fd
= xmkstemp(tmpfile
);
745 f
= sha1fd(idx_fd
, tmpfile
);
746 sha1write(f
, array
, 256 * sizeof(int));
748 for (c
= idx
; c
!= last
; c
++) {
749 uint32_t offset
= htonl((*c
)->offset
);
750 sha1write(f
, &offset
, 4);
751 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
752 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
754 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
755 sha1close(f
, NULL
, 1);
757 SHA1_Final(pack_data
->sha1
, &ctx
);
761 static char *keep_pack(char *curr_index_name
)
763 static char name
[PATH_MAX
];
764 static const char *keep_msg
= "fast-import";
767 chmod(pack_data
->pack_name
, 0444);
768 chmod(curr_index_name
, 0444);
770 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
771 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
772 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
774 die("cannot create keep file");
775 write(keep_fd
, keep_msg
, strlen(keep_msg
));
778 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
779 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
780 if (move_temp_to_file(pack_data
->pack_name
, name
))
781 die("cannot store pack file");
783 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
784 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
785 if (move_temp_to_file(curr_index_name
, name
))
786 die("cannot store index file");
790 static void unkeep_all_packs(void)
792 static char name
[PATH_MAX
];
795 for (k
= 0; k
< pack_id
; k
++) {
796 struct packed_git
*p
= all_packs
[k
];
797 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
798 get_object_directory(), sha1_to_hex(p
->sha1
));
803 static void end_packfile(void)
805 struct packed_git
*old_p
= pack_data
, *new_p
;
813 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->sha1
,
814 pack_data
->pack_name
, object_count
);
815 close(pack_data
->pack_fd
);
816 idx_name
= keep_pack(create_index());
818 /* Register the packfile with core git's machinary. */
819 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
821 die("core git rejected index %s", idx_name
);
822 new_p
->windows
= old_p
->windows
;
823 all_packs
[pack_id
] = new_p
;
824 install_packed_git(new_p
);
826 /* Print the boundary */
828 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
829 for (i
= 0; i
< branch_table_sz
; i
++) {
830 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
831 if (b
->pack_id
== pack_id
)
832 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
835 for (t
= first_tag
; t
; t
= t
->next_tag
) {
836 if (t
->pack_id
== pack_id
)
837 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
839 fputc('\n', pack_edges
);
846 unlink(old_p
->pack_name
);
849 /* We can't carry a delta across packfiles. */
850 free(last_blob
.data
);
851 last_blob
.data
= NULL
;
853 last_blob
.offset
= 0;
857 static void cycle_packfile(void)
863 static size_t encode_header(
864 enum object_type type
,
871 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
872 die("bad type %d", type
);
874 c
= (type
<< 4) | (size
& 15);
886 static int store_object(
887 enum object_type type
,
890 struct last_object
*last
,
891 unsigned char *sha1out
,
895 struct object_entry
*e
;
896 unsigned char hdr
[96];
897 unsigned char sha1
[20];
898 unsigned long hdrlen
, deltalen
;
902 hdrlen
= sprintf((char*)hdr
,"%s %lu", typename(type
),
903 (unsigned long)datlen
) + 1;
905 SHA1_Update(&c
, hdr
, hdrlen
);
906 SHA1_Update(&c
, dat
, datlen
);
907 SHA1_Final(sha1
, &c
);
909 hashcpy(sha1out
, sha1
);
911 e
= insert_object(sha1
);
913 insert_mark(mark
, e
);
915 duplicate_count_by_type
[type
]++;
917 } else if (find_sha1_pack(sha1
, packed_git
)) {
919 e
->pack_id
= MAX_PACK_ID
;
920 e
->offset
= 1; /* just not zero! */
921 duplicate_count_by_type
[type
]++;
925 if (last
&& last
->data
&& last
->depth
< max_depth
) {
926 delta
= diff_delta(last
->data
, last
->len
,
929 if (delta
&& deltalen
>= datlen
) {
936 memset(&s
, 0, sizeof(s
));
937 deflateInit(&s
, zlib_compression_level
);
940 s
.avail_in
= deltalen
;
945 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
946 s
.next_out
= out
= xmalloc(s
.avail_out
);
947 while (deflate(&s
, Z_FINISH
) == Z_OK
)
951 /* Determine if we should auto-checkpoint. */
952 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
953 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
955 /* This new object needs to *not* have the current pack_id. */
956 e
->pack_id
= pack_id
+ 1;
959 /* We cannot carry a delta into the new pack. */
964 memset(&s
, 0, sizeof(s
));
965 deflateInit(&s
, zlib_compression_level
);
968 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
969 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
970 while (deflate(&s
, Z_FINISH
) == Z_OK
)
977 e
->pack_id
= pack_id
;
978 e
->offset
= pack_size
;
980 object_count_by_type
[type
]++;
983 unsigned long ofs
= e
->offset
- last
->offset
;
984 unsigned pos
= sizeof(hdr
) - 1;
986 delta_count_by_type
[type
]++;
989 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
990 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
993 hdr
[pos
] = ofs
& 127;
995 hdr
[--pos
] = 128 | (--ofs
& 127);
996 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
997 pack_size
+= sizeof(hdr
) - pos
;
1001 hdrlen
= encode_header(type
, datlen
, hdr
);
1002 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
1003 pack_size
+= hdrlen
;
1006 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
1007 pack_size
+= s
.total_out
;
1015 last
->offset
= e
->offset
;
1021 static void *gfi_unpack_entry(
1022 struct object_entry
*oe
,
1023 unsigned long *sizep
)
1025 enum object_type type
;
1026 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1028 p
->pack_size
= pack_size
+ 20;
1029 return unpack_entry(p
, oe
->offset
, &type
, sizep
);
1032 static const char *get_mode(const char *str
, uint16_t *modep
)
1037 while ((c
= *str
++) != ' ') {
1038 if (c
< '0' || c
> '7')
1040 mode
= (mode
<< 3) + (c
- '0');
1046 static void load_tree(struct tree_entry
*root
)
1048 unsigned char* sha1
= root
->versions
[1].sha1
;
1049 struct object_entry
*myoe
;
1050 struct tree_content
*t
;
1055 root
->tree
= t
= new_tree_content(8);
1056 if (is_null_sha1(sha1
))
1059 myoe
= find_object(sha1
);
1060 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1061 if (myoe
->type
!= OBJ_TREE
)
1062 die("Not a tree: %s", sha1_to_hex(sha1
));
1064 buf
= gfi_unpack_entry(myoe
, &size
);
1066 enum object_type type
;
1067 buf
= read_sha1_file(sha1
, &type
, &size
);
1068 if (!buf
|| type
!= OBJ_TREE
)
1069 die("Can't load tree %s", sha1_to_hex(sha1
));
1073 while (c
!= (buf
+ size
)) {
1074 struct tree_entry
*e
= new_tree_entry();
1076 if (t
->entry_count
== t
->entry_capacity
)
1077 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1078 t
->entries
[t
->entry_count
++] = e
;
1081 c
= get_mode(c
, &e
->versions
[1].mode
);
1083 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1084 e
->versions
[0].mode
= e
->versions
[1].mode
;
1085 e
->name
= to_atom(c
, strlen(c
));
1086 c
+= e
->name
->str_len
+ 1;
1087 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1088 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1094 static int tecmp0 (const void *_a
, const void *_b
)
1096 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1097 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1098 return base_name_compare(
1099 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1100 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1103 static int tecmp1 (const void *_a
, const void *_b
)
1105 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1106 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1107 return base_name_compare(
1108 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1109 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1112 static void mktree(struct tree_content
*t
,
1122 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1124 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1126 for (i
= 0; i
< t
->entry_count
; i
++) {
1127 if (t
->entries
[i
]->versions
[v
].mode
)
1128 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1131 size_dbuf(b
, maxlen
);
1133 for (i
= 0; i
< t
->entry_count
; i
++) {
1134 struct tree_entry
*e
= t
->entries
[i
];
1135 if (!e
->versions
[v
].mode
)
1137 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1139 strcpy(c
, e
->name
->str_dat
);
1140 c
+= e
->name
->str_len
+ 1;
1141 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1144 *szp
= c
- (char*)b
->buffer
;
1147 static void store_tree(struct tree_entry
*root
)
1149 struct tree_content
*t
= root
->tree
;
1150 unsigned int i
, j
, del
;
1151 unsigned long new_len
;
1152 struct last_object lo
;
1153 struct object_entry
*le
;
1155 if (!is_null_sha1(root
->versions
[1].sha1
))
1158 for (i
= 0; i
< t
->entry_count
; i
++) {
1159 if (t
->entries
[i
]->tree
)
1160 store_tree(t
->entries
[i
]);
1163 le
= find_object(root
->versions
[0].sha1
);
1164 if (!S_ISDIR(root
->versions
[0].mode
)
1166 || le
->pack_id
!= pack_id
) {
1171 mktree(t
, 0, &lo
.len
, &old_tree
);
1172 lo
.data
= old_tree
.buffer
;
1173 lo
.offset
= le
->offset
;
1174 lo
.depth
= t
->delta_depth
;
1178 mktree(t
, 1, &new_len
, &new_tree
);
1179 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1180 &lo
, root
->versions
[1].sha1
, 0);
1182 t
->delta_depth
= lo
.depth
;
1183 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1184 struct tree_entry
*e
= t
->entries
[i
];
1185 if (e
->versions
[1].mode
) {
1186 e
->versions
[0].mode
= e
->versions
[1].mode
;
1187 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1188 t
->entries
[j
++] = e
;
1190 release_tree_entry(e
);
1194 t
->entry_count
-= del
;
1197 static int tree_content_set(
1198 struct tree_entry
*root
,
1200 const unsigned char *sha1
,
1201 const uint16_t mode
,
1202 struct tree_content
*subtree
)
1204 struct tree_content
*t
= root
->tree
;
1207 struct tree_entry
*e
;
1209 slash1
= strchr(p
, '/');
1215 die("Empty path component found in input");
1216 if (!slash1
&& !S_ISDIR(mode
) && subtree
)
1217 die("Non-directories cannot have subtrees");
1219 for (i
= 0; i
< t
->entry_count
; i
++) {
1221 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1224 && e
->versions
[1].mode
== mode
1225 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1227 e
->versions
[1].mode
= mode
;
1228 hashcpy(e
->versions
[1].sha1
, sha1
);
1230 release_tree_content_recursive(e
->tree
);
1232 hashclr(root
->versions
[1].sha1
);
1235 if (!S_ISDIR(e
->versions
[1].mode
)) {
1236 e
->tree
= new_tree_content(8);
1237 e
->versions
[1].mode
= S_IFDIR
;
1241 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
)) {
1242 hashclr(root
->versions
[1].sha1
);
1249 if (t
->entry_count
== t
->entry_capacity
)
1250 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1251 e
= new_tree_entry();
1252 e
->name
= to_atom(p
, n
);
1253 e
->versions
[0].mode
= 0;
1254 hashclr(e
->versions
[0].sha1
);
1255 t
->entries
[t
->entry_count
++] = e
;
1257 e
->tree
= new_tree_content(8);
1258 e
->versions
[1].mode
= S_IFDIR
;
1259 tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
);
1262 e
->versions
[1].mode
= mode
;
1263 hashcpy(e
->versions
[1].sha1
, sha1
);
1265 hashclr(root
->versions
[1].sha1
);
1269 static int tree_content_remove(
1270 struct tree_entry
*root
,
1272 struct tree_entry
*backup_leaf
)
1274 struct tree_content
*t
= root
->tree
;
1277 struct tree_entry
*e
;
1279 slash1
= strchr(p
, '/');
1285 for (i
= 0; i
< t
->entry_count
; i
++) {
1287 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1288 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1292 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
)) {
1293 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1294 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1295 hashclr(root
->versions
[1].sha1
);
1309 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1311 release_tree_content_recursive(e
->tree
);
1313 e
->versions
[1].mode
= 0;
1314 hashclr(e
->versions
[1].sha1
);
1315 hashclr(root
->versions
[1].sha1
);
1319 static int tree_content_get(
1320 struct tree_entry
*root
,
1322 struct tree_entry
*leaf
)
1324 struct tree_content
*t
= root
->tree
;
1327 struct tree_entry
*e
;
1329 slash1
= strchr(p
, '/');
1335 for (i
= 0; i
< t
->entry_count
; i
++) {
1337 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1339 memcpy(leaf
, e
, sizeof(*leaf
));
1340 if (e
->tree
&& is_null_sha1(e
->versions
[1].sha1
))
1341 leaf
->tree
= dup_tree_content(e
->tree
);
1346 if (!S_ISDIR(e
->versions
[1].mode
))
1350 return tree_content_get(e
, slash1
+ 1, leaf
);
1356 static int update_branch(struct branch
*b
)
1358 static const char *msg
= "fast-import";
1359 struct ref_lock
*lock
;
1360 unsigned char old_sha1
[20];
1362 if (read_ref(b
->name
, old_sha1
))
1364 lock
= lock_any_ref_for_update(b
->name
, old_sha1
, 0);
1366 return error("Unable to lock %s", b
->name
);
1367 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1368 struct commit
*old_cmit
, *new_cmit
;
1370 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1371 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1372 if (!old_cmit
|| !new_cmit
) {
1374 return error("Branch %s is missing commits.", b
->name
);
1377 if (!in_merge_bases(old_cmit
, &new_cmit
, 1)) {
1379 warning("Not updating %s"
1380 " (new tip %s does not contain %s)",
1381 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1385 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1386 return error("Unable to update %s", b
->name
);
1390 static void dump_branches(void)
1395 for (i
= 0; i
< branch_table_sz
; i
++) {
1396 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1397 failure
|= update_branch(b
);
1401 static void dump_tags(void)
1403 static const char *msg
= "fast-import";
1405 struct ref_lock
*lock
;
1406 char ref_name
[PATH_MAX
];
1408 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1409 sprintf(ref_name
, "tags/%s", t
->name
);
1410 lock
= lock_ref_sha1(ref_name
, NULL
);
1411 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1412 failure
|= error("Unable to update %s", ref_name
);
1416 static void dump_marks_helper(FILE *f
,
1422 for (k
= 0; k
< 1024; k
++) {
1423 if (m
->data
.sets
[k
])
1424 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1428 for (k
= 0; k
< 1024; k
++) {
1429 if (m
->data
.marked
[k
])
1430 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1431 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1436 static void dump_marks(void)
1438 static struct lock_file mark_lock
;
1445 mark_fd
= hold_lock_file_for_update(&mark_lock
, mark_file
, 0);
1447 failure
|= error("Unable to write marks file %s: %s",
1448 mark_file
, strerror(errno
));
1452 f
= fdopen(mark_fd
, "w");
1454 rollback_lock_file(&mark_lock
);
1455 failure
|= error("Unable to write marks file %s: %s",
1456 mark_file
, strerror(errno
));
1460 dump_marks_helper(f
, 0, marks
);
1462 if (commit_lock_file(&mark_lock
))
1463 failure
|= error("Unable to write marks file %s: %s",
1464 mark_file
, strerror(errno
));
1467 static void read_next_command(void)
1470 if (unread_command_buf
)
1471 unread_command_buf
= 0;
1473 read_line(&command_buf
, stdin
, '\n');
1474 } while (!command_buf
.eof
&& command_buf
.buf
[0] == '#');
1477 static void skip_optional_lf()
1479 int term_char
= fgetc(stdin
);
1480 if (term_char
!= '\n' && term_char
!= EOF
)
1481 ungetc(term_char
, stdin
);
1484 static void cmd_mark(void)
1486 if (!prefixcmp(command_buf
.buf
, "mark :")) {
1487 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1488 read_next_command();
1494 static void *cmd_data (size_t *size
)
1499 if (prefixcmp(command_buf
.buf
, "data "))
1500 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1502 if (!prefixcmp(command_buf
.buf
+ 5, "<<")) {
1503 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1504 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1506 buffer
= xmalloc(sz
);
1508 read_line(&command_buf
, stdin
, '\n');
1509 if (command_buf
.eof
)
1510 die("EOF in data (terminator '%s' not found)", term
);
1511 if (term_len
== command_buf
.len
1512 && !strcmp(term
, command_buf
.buf
))
1514 ALLOC_GROW(buffer
, length
+ command_buf
.len
, sz
);
1515 memcpy(buffer
+ length
,
1517 command_buf
.len
- 1);
1518 length
+= command_buf
.len
- 1;
1519 buffer
[length
++] = '\n';
1525 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1526 buffer
= xmalloc(length
);
1527 while (n
< length
) {
1528 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1529 if (!s
&& feof(stdin
))
1530 die("EOF in data (%lu bytes remaining)",
1531 (unsigned long)(length
- n
));
1541 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1543 const char *orig_src
= src
;
1546 strtoul(src
, &endp
, 10);
1547 if (endp
== src
|| *endp
!= ' ')
1551 if (*src
!= '-' && *src
!= '+')
1555 strtoul(src
+ 1, &endp
, 10);
1556 if (endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1559 strcpy(result
, orig_src
);
1563 static char *parse_ident(const char *buf
)
1569 gt
= strrchr(buf
, '>');
1571 die("Missing > in ident string: %s", buf
);
1574 die("Missing space after > in ident string: %s", buf
);
1576 name_len
= gt
- buf
;
1577 ident
= xmalloc(name_len
+ 24);
1578 strncpy(ident
, buf
, name_len
);
1582 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1583 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1585 case WHENSPEC_RFC2822
:
1586 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1587 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1590 if (strcmp("now", gt
))
1591 die("Date in ident must be 'now': %s", buf
);
1592 datestamp(ident
+ name_len
, 24);
1599 static void cmd_new_blob(void)
1604 read_next_command();
1608 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1612 static void unload_one_branch(void)
1614 while (cur_active_branches
1615 && cur_active_branches
>= max_active_branches
) {
1616 uintmax_t min_commit
= ULONG_MAX
;
1617 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1619 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1620 if (e
->last_commit
< min_commit
) {
1622 min_commit
= e
->last_commit
;
1628 e
= p
->active_next_branch
;
1629 p
->active_next_branch
= e
->active_next_branch
;
1631 e
= active_branches
;
1632 active_branches
= e
->active_next_branch
;
1635 e
->active_next_branch
= NULL
;
1636 if (e
->branch_tree
.tree
) {
1637 release_tree_content_recursive(e
->branch_tree
.tree
);
1638 e
->branch_tree
.tree
= NULL
;
1640 cur_active_branches
--;
1644 static void load_branch(struct branch
*b
)
1646 load_tree(&b
->branch_tree
);
1649 b
->active_next_branch
= active_branches
;
1650 active_branches
= b
;
1651 cur_active_branches
++;
1652 branch_load_count
++;
1656 static void file_change_m(struct branch
*b
)
1658 const char *p
= command_buf
.buf
+ 2;
1661 struct object_entry
*oe
= oe
;
1662 unsigned char sha1
[20];
1663 uint16_t mode
, inline_data
= 0;
1665 p
= get_mode(p
, &mode
);
1667 die("Corrupt mode: %s", command_buf
.buf
);
1669 case S_IFREG
| 0644:
1670 case S_IFREG
| 0755:
1677 die("Corrupt mode: %s", command_buf
.buf
);
1682 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1683 hashcpy(sha1
, oe
->sha1
);
1685 } else if (!prefixcmp(p
, "inline")) {
1689 if (get_sha1_hex(p
, sha1
))
1690 die("Invalid SHA1: %s", command_buf
.buf
);
1691 oe
= find_object(sha1
);
1695 die("Missing space after SHA1: %s", command_buf
.buf
);
1697 p_uq
= unquote_c_style(p
, &endp
);
1700 die("Garbage after path in: %s", command_buf
.buf
);
1708 p
= p_uq
= xstrdup(p
);
1709 read_next_command();
1711 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1714 if (oe
->type
!= OBJ_BLOB
)
1715 die("Not a blob (actually a %s): %s",
1716 command_buf
.buf
, typename(oe
->type
));
1718 enum object_type type
= sha1_object_info(sha1
, NULL
);
1720 die("Blob not found: %s", command_buf
.buf
);
1721 if (type
!= OBJ_BLOB
)
1722 die("Not a blob (actually a %s): %s",
1723 typename(type
), command_buf
.buf
);
1726 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
, NULL
);
1730 static void file_change_d(struct branch
*b
)
1732 const char *p
= command_buf
.buf
+ 2;
1736 p_uq
= unquote_c_style(p
, &endp
);
1739 die("Garbage after path in: %s", command_buf
.buf
);
1742 tree_content_remove(&b
->branch_tree
, p
, NULL
);
1746 static void file_change_cr(struct branch
*b
, int rename
)
1751 struct tree_entry leaf
;
1753 s
= command_buf
.buf
+ 2;
1754 s_uq
= unquote_c_style(s
, &endp
);
1757 die("Missing space after source: %s", command_buf
.buf
);
1760 endp
= strchr(s
, ' ');
1762 die("Missing space after source: %s", command_buf
.buf
);
1763 s_uq
= xmalloc(endp
- s
+ 1);
1764 memcpy(s_uq
, s
, endp
- s
);
1771 die("Missing dest: %s", command_buf
.buf
);
1774 d_uq
= unquote_c_style(d
, &endp
);
1777 die("Garbage after dest in: %s", command_buf
.buf
);
1781 memset(&leaf
, 0, sizeof(leaf
));
1783 tree_content_remove(&b
->branch_tree
, s
, &leaf
);
1785 tree_content_get(&b
->branch_tree
, s
, &leaf
);
1786 if (!leaf
.versions
[1].mode
)
1787 die("Path %s not in branch", s
);
1788 tree_content_set(&b
->branch_tree
, d
,
1789 leaf
.versions
[1].sha1
,
1790 leaf
.versions
[1].mode
,
1797 static void file_change_deleteall(struct branch
*b
)
1799 release_tree_content_recursive(b
->branch_tree
.tree
);
1800 hashclr(b
->branch_tree
.versions
[0].sha1
);
1801 hashclr(b
->branch_tree
.versions
[1].sha1
);
1802 load_tree(&b
->branch_tree
);
1805 static void cmd_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
1807 if (!buf
|| size
< 46)
1808 die("Not a valid commit: %s", sha1_to_hex(b
->sha1
));
1809 if (memcmp("tree ", buf
, 5)
1810 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1811 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1812 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1813 b
->branch_tree
.versions
[1].sha1
);
1816 static void cmd_from_existing(struct branch
*b
)
1818 if (is_null_sha1(b
->sha1
)) {
1819 hashclr(b
->branch_tree
.versions
[0].sha1
);
1820 hashclr(b
->branch_tree
.versions
[1].sha1
);
1825 buf
= read_object_with_reference(b
->sha1
,
1826 commit_type
, &size
, b
->sha1
);
1827 cmd_from_commit(b
, buf
, size
);
1832 static int cmd_from(struct branch
*b
)
1837 if (prefixcmp(command_buf
.buf
, "from "))
1840 if (b
->branch_tree
.tree
) {
1841 release_tree_content_recursive(b
->branch_tree
.tree
);
1842 b
->branch_tree
.tree
= NULL
;
1845 from
= strchr(command_buf
.buf
, ' ') + 1;
1846 s
= lookup_branch(from
);
1848 die("Can't create a branch from itself: %s", b
->name
);
1850 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1851 hashcpy(b
->sha1
, s
->sha1
);
1852 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1853 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1854 } else if (*from
== ':') {
1855 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1856 struct object_entry
*oe
= find_mark(idnum
);
1857 if (oe
->type
!= OBJ_COMMIT
)
1858 die("Mark :%" PRIuMAX
" not a commit", idnum
);
1859 hashcpy(b
->sha1
, oe
->sha1
);
1860 if (oe
->pack_id
!= MAX_PACK_ID
) {
1862 char *buf
= gfi_unpack_entry(oe
, &size
);
1863 cmd_from_commit(b
, buf
, size
);
1866 cmd_from_existing(b
);
1867 } else if (!get_sha1(from
, b
->sha1
))
1868 cmd_from_existing(b
);
1870 die("Invalid ref name or SHA1 expression: %s", from
);
1872 read_next_command();
1876 static struct hash_list
*cmd_merge(unsigned int *count
)
1878 struct hash_list
*list
= NULL
, *n
, *e
= e
;
1883 while (!prefixcmp(command_buf
.buf
, "merge ")) {
1884 from
= strchr(command_buf
.buf
, ' ') + 1;
1885 n
= xmalloc(sizeof(*n
));
1886 s
= lookup_branch(from
);
1888 hashcpy(n
->sha1
, s
->sha1
);
1889 else if (*from
== ':') {
1890 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1891 struct object_entry
*oe
= find_mark(idnum
);
1892 if (oe
->type
!= OBJ_COMMIT
)
1893 die("Mark :%" PRIuMAX
" not a commit", idnum
);
1894 hashcpy(n
->sha1
, oe
->sha1
);
1895 } else if (!get_sha1(from
, n
->sha1
)) {
1897 char *buf
= read_object_with_reference(n
->sha1
,
1898 commit_type
, &size
, n
->sha1
);
1899 if (!buf
|| size
< 46)
1900 die("Not a valid commit: %s", from
);
1903 die("Invalid ref name or SHA1 expression: %s", from
);
1912 read_next_command();
1917 static void cmd_new_commit(void)
1923 char *author
= NULL
;
1924 char *committer
= NULL
;
1925 struct hash_list
*merge_list
= NULL
;
1926 unsigned int merge_count
;
1928 /* Obtain the branch name from the rest of our command */
1929 sp
= strchr(command_buf
.buf
, ' ') + 1;
1930 b
= lookup_branch(sp
);
1934 read_next_command();
1936 if (!prefixcmp(command_buf
.buf
, "author ")) {
1937 author
= parse_ident(command_buf
.buf
+ 7);
1938 read_next_command();
1940 if (!prefixcmp(command_buf
.buf
, "committer ")) {
1941 committer
= parse_ident(command_buf
.buf
+ 10);
1942 read_next_command();
1945 die("Expected committer but didn't get one");
1946 msg
= cmd_data(&msglen
);
1947 read_next_command();
1949 merge_list
= cmd_merge(&merge_count
);
1951 /* ensure the branch is active/loaded */
1952 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1953 unload_one_branch();
1958 while (!command_buf
.eof
&& command_buf
.len
> 1) {
1959 if (!prefixcmp(command_buf
.buf
, "M "))
1961 else if (!prefixcmp(command_buf
.buf
, "D "))
1963 else if (!prefixcmp(command_buf
.buf
, "R "))
1964 file_change_cr(b
, 1);
1965 else if (!prefixcmp(command_buf
.buf
, "C "))
1966 file_change_cr(b
, 0);
1967 else if (!strcmp("deleteall", command_buf
.buf
))
1968 file_change_deleteall(b
);
1970 unread_command_buf
= 1;
1973 read_next_command();
1976 /* build the tree and the commit */
1977 store_tree(&b
->branch_tree
);
1978 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1979 b
->branch_tree
.versions
[1].sha1
);
1980 size_dbuf(&new_data
, 114 + msglen
1983 ? strlen(author
) + strlen(committer
)
1984 : 2 * strlen(committer
)));
1985 sp
= new_data
.buffer
;
1986 sp
+= sprintf(sp
, "tree %s\n",
1987 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1988 if (!is_null_sha1(b
->sha1
))
1989 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1990 while (merge_list
) {
1991 struct hash_list
*next
= merge_list
->next
;
1992 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1996 sp
+= sprintf(sp
, "author %s\n", author
? author
: committer
);
1997 sp
+= sprintf(sp
, "committer %s\n", committer
);
1999 memcpy(sp
, msg
, msglen
);
2005 if (!store_object(OBJ_COMMIT
,
2006 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
2007 NULL
, b
->sha1
, next_mark
))
2008 b
->pack_id
= pack_id
;
2009 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2012 static void cmd_new_tag(void)
2021 uintmax_t from_mark
= 0;
2022 unsigned char sha1
[20];
2024 /* Obtain the new tag name from the rest of our command */
2025 sp
= strchr(command_buf
.buf
, ' ') + 1;
2026 t
= pool_alloc(sizeof(struct tag
));
2028 t
->name
= pool_strdup(sp
);
2030 last_tag
->next_tag
= t
;
2034 read_next_command();
2037 if (prefixcmp(command_buf
.buf
, "from "))
2038 die("Expected from command, got %s", command_buf
.buf
);
2039 from
= strchr(command_buf
.buf
, ' ') + 1;
2040 s
= lookup_branch(from
);
2042 hashcpy(sha1
, s
->sha1
);
2043 } else if (*from
== ':') {
2044 struct object_entry
*oe
;
2045 from_mark
= strtoumax(from
+ 1, NULL
, 10);
2046 oe
= find_mark(from_mark
);
2047 if (oe
->type
!= OBJ_COMMIT
)
2048 die("Mark :%" PRIuMAX
" not a commit", from_mark
);
2049 hashcpy(sha1
, oe
->sha1
);
2050 } else if (!get_sha1(from
, sha1
)) {
2054 buf
= read_object_with_reference(sha1
,
2055 commit_type
, &size
, sha1
);
2056 if (!buf
|| size
< 46)
2057 die("Not a valid commit: %s", from
);
2060 die("Invalid ref name or SHA1 expression: %s", from
);
2061 read_next_command();
2064 if (prefixcmp(command_buf
.buf
, "tagger "))
2065 die("Expected tagger command, got %s", command_buf
.buf
);
2066 tagger
= parse_ident(command_buf
.buf
+ 7);
2068 /* tag payload/message */
2069 read_next_command();
2070 msg
= cmd_data(&msglen
);
2072 /* build the tag object */
2073 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
2074 sp
= new_data
.buffer
;
2075 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
2076 sp
+= sprintf(sp
, "type %s\n", commit_type
);
2077 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
2078 sp
+= sprintf(sp
, "tagger %s\n", tagger
);
2080 memcpy(sp
, msg
, msglen
);
2085 if (store_object(OBJ_TAG
, new_data
.buffer
,
2086 sp
- (char*)new_data
.buffer
,
2088 t
->pack_id
= MAX_PACK_ID
;
2090 t
->pack_id
= pack_id
;
2093 static void cmd_reset_branch(void)
2098 /* Obtain the branch name from the rest of our command */
2099 sp
= strchr(command_buf
.buf
, ' ') + 1;
2100 b
= lookup_branch(sp
);
2103 hashclr(b
->branch_tree
.versions
[0].sha1
);
2104 hashclr(b
->branch_tree
.versions
[1].sha1
);
2105 if (b
->branch_tree
.tree
) {
2106 release_tree_content_recursive(b
->branch_tree
.tree
);
2107 b
->branch_tree
.tree
= NULL
;
2112 read_next_command();
2113 if (!cmd_from(b
) && command_buf
.len
> 1)
2114 unread_command_buf
= 1;
2117 static void cmd_checkpoint(void)
2128 static void import_marks(const char *input_file
)
2131 FILE *f
= fopen(input_file
, "r");
2133 die("cannot read %s: %s", input_file
, strerror(errno
));
2134 while (fgets(line
, sizeof(line
), f
)) {
2137 unsigned char sha1
[20];
2138 struct object_entry
*e
;
2140 end
= strchr(line
, '\n');
2141 if (line
[0] != ':' || !end
)
2142 die("corrupt mark line: %s", line
);
2144 mark
= strtoumax(line
+ 1, &end
, 10);
2145 if (!mark
|| end
== line
+ 1
2146 || *end
!= ' ' || get_sha1(end
+ 1, sha1
))
2147 die("corrupt mark line: %s", line
);
2148 e
= find_object(sha1
);
2150 enum object_type type
= sha1_object_info(sha1
, NULL
);
2152 die("object not found: %s", sha1_to_hex(sha1
));
2153 e
= insert_object(sha1
);
2155 e
->pack_id
= MAX_PACK_ID
;
2156 e
->offset
= 1; /* just not zero! */
2158 insert_mark(mark
, e
);
2163 static const char fast_import_usage
[] =
2164 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2166 int main(int argc
, const char **argv
)
2168 int i
, show_stats
= 1;
2170 git_config(git_default_config
);
2171 alloc_objects(object_entry_alloc
);
2172 strbuf_init(&command_buf
);
2173 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2174 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2175 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2176 marks
= pool_calloc(1, sizeof(struct mark_set
));
2178 for (i
= 1; i
< argc
; i
++) {
2179 const char *a
= argv
[i
];
2181 if (*a
!= '-' || !strcmp(a
, "--"))
2183 else if (!prefixcmp(a
, "--date-format=")) {
2184 const char *fmt
= a
+ 14;
2185 if (!strcmp(fmt
, "raw"))
2186 whenspec
= WHENSPEC_RAW
;
2187 else if (!strcmp(fmt
, "rfc2822"))
2188 whenspec
= WHENSPEC_RFC2822
;
2189 else if (!strcmp(fmt
, "now"))
2190 whenspec
= WHENSPEC_NOW
;
2192 die("unknown --date-format argument %s", fmt
);
2194 else if (!prefixcmp(a
, "--max-pack-size="))
2195 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
2196 else if (!prefixcmp(a
, "--depth="))
2197 max_depth
= strtoul(a
+ 8, NULL
, 0);
2198 else if (!prefixcmp(a
, "--active-branches="))
2199 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
2200 else if (!prefixcmp(a
, "--import-marks="))
2201 import_marks(a
+ 15);
2202 else if (!prefixcmp(a
, "--export-marks="))
2204 else if (!prefixcmp(a
, "--export-pack-edges=")) {
2207 pack_edges
= fopen(a
+ 20, "a");
2209 die("Cannot open %s: %s", a
+ 20, strerror(errno
));
2210 } else if (!strcmp(a
, "--force"))
2212 else if (!strcmp(a
, "--quiet"))
2214 else if (!strcmp(a
, "--stats"))
2217 die("unknown option %s", a
);
2220 usage(fast_import_usage
);
2222 prepare_packed_git();
2225 read_next_command();
2226 if (command_buf
.eof
)
2228 else if (!strcmp("blob", command_buf
.buf
))
2230 else if (!prefixcmp(command_buf
.buf
, "commit "))
2232 else if (!prefixcmp(command_buf
.buf
, "tag "))
2234 else if (!prefixcmp(command_buf
.buf
, "reset "))
2236 else if (!strcmp("checkpoint", command_buf
.buf
))
2239 die("Unsupported command: %s", command_buf
.buf
);
2252 uintmax_t total_count
= 0, duplicate_count
= 0;
2253 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2254 total_count
+= object_count_by_type
[i
];
2255 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2256 duplicate_count
+= duplicate_count_by_type
[i
];
2258 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2259 fprintf(stderr
, "---------------------------------------------------------------------\n");
2260 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
2261 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
2262 fprintf(stderr
, " blobs : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
], delta_count_by_type
[OBJ_BLOB
]);
2263 fprintf(stderr
, " trees : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
], delta_count_by_type
[OBJ_TREE
]);
2264 fprintf(stderr
, " commits: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
], delta_count_by_type
[OBJ_COMMIT
]);
2265 fprintf(stderr
, " tags : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
], delta_count_by_type
[OBJ_TAG
]);
2266 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2267 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2268 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2269 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2270 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
2271 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2272 fprintf(stderr
, "---------------------------------------------------------------------\n");
2274 fprintf(stderr
, "---------------------------------------------------------------------\n");
2275 fprintf(stderr
, "\n");
2278 return failure
? 1 : 0;