2 Format of STDIN stream:
12 new_blob ::= 'blob' lf
15 file_content ::= data;
17 new_commit ::= 'commit' sp ref_str lf
19 ('author' sp name '<' email '>' ts tz lf)?
20 'committer' sp name '<' email '>' ts tz lf
22 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
27 file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
30 mode ::= '644' | '755';
32 new_tag ::= 'tag' sp tag_str lf
33 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
34 'tagger' sp name '<' email '>' ts tz lf
38 reset_branch ::= 'reset' sp ref_str lf;
40 # note: the first idnum in a stream should be 1 and subsequent
41 # idnums should not have gaps between values as this will cause
42 # the stream parser to reserve space for the gapped values. An
43 # idnum can be updated in the future to a new object by issuing
44 # a new mark directive with the old idnum.
46 mark ::= 'mark' sp idnum lf;
48 # note: declen indicates the length of binary_data in bytes.
49 # declen does not include the lf preceeding or trailing the
52 data ::= 'data' sp declen lf
56 # note: quoted strings are C-style quoting supporting \c for
57 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
58 # is the signed byte value in octal. Note that the only
59 # characters which must actually be escaped to protect the
60 # stream formatting is: \, " and LF. Otherwise these values
63 ref_str ::= ref | '"' quoted(ref) '"' ;
64 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
65 tag_str ::= tag | '"' quoted(tag) '"' ;
66 path_str ::= path | '"' quoted(path) '"' ;
68 declen ::= # unsigned 32 bit value, ascii base10 notation;
69 binary_data ::= # file content, not interpreted;
71 sp ::= # ASCII space character;
72 lf ::= # ASCII newline (LF) character;
74 # note: a colon (':') must precede the numerical value assigned to
75 # an idnum. This is to distinguish it from a ref or tag name as
76 # GIT does not permit ':' in ref or tag strings.
79 path ::= # GIT style file path, e.g. "a/b/c";
80 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
81 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
82 sha1exp ::= # Any valid GIT SHA1 expression;
83 hexsha1 ::= # SHA1 in hexadecimal format;
85 # note: name and email are UTF8 strings, however name must not
86 # contain '<' or lf and email must not contain any of the
87 # following: '<', '>', lf.
89 name ::= # valid GIT author/committer name;
90 email ::= # valid GIT author/committer email;
91 ts ::= # time since the epoch in seconds, ascii base10 notation;
92 tz ::= # GIT style timezone;
103 #include "csum-file.h"
109 struct object_entry
*next
;
110 enum object_type type
;
111 unsigned long offset
;
112 unsigned char sha1
[20];
115 struct object_entry_pool
117 struct object_entry_pool
*next_pool
;
118 struct object_entry
*next_free
;
119 struct object_entry
*end
;
120 struct object_entry entries
[FLEX_ARRAY
]; /* more */
127 struct object_entry
*marked
[1024];
128 struct mark_set
*sets
[1024];
137 unsigned char sha1
[20];
142 struct mem_pool
*next_pool
;
145 char space
[FLEX_ARRAY
]; /* more */
150 struct atom_str
*next_atom
;
152 char str_dat
[FLEX_ARRAY
]; /* more */
158 struct tree_content
*tree
;
159 struct atom_str
* name
;
161 unsigned char sha1
[20];
166 unsigned int entry_capacity
; /* must match avail_tree_content */
167 unsigned int entry_count
;
168 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
171 struct avail_tree_content
173 unsigned int entry_capacity
; /* must match tree_content */
174 struct avail_tree_content
*next_avail
;
179 struct branch
*table_next_branch
;
180 struct branch
*active_next_branch
;
182 unsigned long last_commit
;
183 struct tree_entry branch_tree
;
184 unsigned char sha1
[20];
189 struct tag
*next_tag
;
191 unsigned char sha1
[20];
195 /* Stats and misc. counters */
196 static unsigned long max_depth
= 10;
197 static unsigned long alloc_count
;
198 static unsigned long branch_count
;
199 static unsigned long branch_load_count
;
200 static unsigned long remap_count
;
201 static unsigned long object_count
;
202 static unsigned long duplicate_count
;
203 static unsigned long marks_set_count
;
204 static unsigned long object_count_by_type
[9];
205 static unsigned long duplicate_count_by_type
[9];
208 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
209 static size_t total_allocd
;
210 static struct mem_pool
*mem_pool
;
212 /* Atom management */
213 static unsigned int atom_table_sz
= 4451;
214 static unsigned int atom_cnt
;
215 static struct atom_str
**atom_table
;
217 /* The .pack file being generated */
219 static unsigned long pack_size
;
220 static unsigned char pack_sha1
[20];
221 static unsigned char* pack_base
;
222 static unsigned long pack_moff
;
223 static unsigned long pack_mlen
= 128*1024*1024;
224 static unsigned long page_size
;
226 /* Table of objects we've written. */
227 static unsigned int object_entry_alloc
= 1000;
228 static struct object_entry_pool
*blocks
;
229 static struct object_entry
*object_table
[1 << 16];
230 static struct mark_set
*marks
;
231 static const char* mark_file
;
234 static struct last_object last_blob
;
236 /* Tree management */
237 static unsigned int tree_entry_alloc
= 1000;
238 static void *avail_tree_entry
;
239 static unsigned int avail_tree_table_sz
= 100;
240 static struct avail_tree_content
**avail_tree_table
;
243 static unsigned long max_active_branches
= 5;
244 static unsigned long cur_active_branches
;
245 static unsigned long branch_table_sz
= 1039;
246 static struct branch
**branch_table
;
247 static struct branch
*active_branches
;
250 static struct tag
*first_tag
;
251 static struct tag
*last_tag
;
253 /* Input stream parsing */
254 static struct strbuf command_buf
;
255 static unsigned long next_mark
;
256 static FILE* branch_log
;
259 static void alloc_objects(int cnt
)
261 struct object_entry_pool
*b
;
263 b
= xmalloc(sizeof(struct object_entry_pool
)
264 + cnt
* sizeof(struct object_entry
));
265 b
->next_pool
= blocks
;
266 b
->next_free
= b
->entries
;
267 b
->end
= b
->entries
+ cnt
;
272 static struct object_entry
* new_object(unsigned char *sha1
)
274 struct object_entry
*e
;
276 if (blocks
->next_free
== blocks
->end
)
277 alloc_objects(object_entry_alloc
);
279 e
= blocks
->next_free
++;
280 memcpy(e
->sha1
, sha1
, sizeof(e
->sha1
));
284 static struct object_entry
* find_object(unsigned char *sha1
)
286 unsigned int h
= sha1
[0] << 8 | sha1
[1];
287 struct object_entry
*e
;
288 for (e
= object_table
[h
]; e
; e
= e
->next
)
289 if (!memcmp(sha1
, e
->sha1
, sizeof(e
->sha1
)))
294 static struct object_entry
* insert_object(unsigned char *sha1
)
296 unsigned int h
= sha1
[0] << 8 | sha1
[1];
297 struct object_entry
*e
= object_table
[h
];
298 struct object_entry
*p
= NULL
;
301 if (!memcmp(sha1
, e
->sha1
, sizeof(e
->sha1
)))
307 e
= new_object(sha1
);
317 static unsigned int hc_str(const char *s
, size_t len
)
325 static void* pool_alloc(size_t len
)
330 for (p
= mem_pool
; p
; p
= p
->next_pool
)
331 if ((p
->end
- p
->next_free
>= len
))
335 if (len
>= (mem_pool_alloc
/2)) {
339 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
340 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
341 p
->next_pool
= mem_pool
;
342 p
->next_free
= p
->space
;
343 p
->end
= p
->next_free
+ mem_pool_alloc
;
348 /* round out to a pointer alignment */
349 if (len
& (sizeof(void*) - 1))
350 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
355 static void* pool_calloc(size_t count
, size_t size
)
357 size_t len
= count
* size
;
358 void *r
= pool_alloc(len
);
363 static char* pool_strdup(const char *s
)
365 char *r
= pool_alloc(strlen(s
) + 1);
370 static void insert_mark(unsigned long idnum
, struct object_entry
*oe
)
372 struct mark_set
*s
= marks
;
373 while ((idnum
>> s
->shift
) >= 1024) {
374 s
= pool_calloc(1, sizeof(struct mark_set
));
375 s
->shift
= marks
->shift
+ 10;
376 s
->data
.sets
[0] = marks
;
380 unsigned long i
= idnum
>> s
->shift
;
381 idnum
-= i
<< s
->shift
;
382 if (!s
->data
.sets
[i
]) {
383 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
384 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
388 if (!s
->data
.marked
[idnum
])
390 s
->data
.marked
[idnum
] = oe
;
393 static struct object_entry
* find_mark(unsigned long idnum
)
395 unsigned long orig_idnum
= idnum
;
396 struct mark_set
*s
= marks
;
397 struct object_entry
*oe
= NULL
;
398 if ((idnum
>> s
->shift
) < 1024) {
399 while (s
&& s
->shift
) {
400 unsigned long i
= idnum
>> s
->shift
;
401 idnum
-= i
<< s
->shift
;
405 oe
= s
->data
.marked
[idnum
];
408 die("mark :%lu not declared", orig_idnum
);
412 static struct atom_str
* to_atom(const char *s
, size_t len
)
414 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
417 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
418 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
421 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
423 strncpy(c
->str_dat
, s
, len
);
425 c
->next_atom
= atom_table
[hc
];
431 static struct branch
* lookup_branch(const char *name
)
433 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
436 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
437 if (!strcmp(name
, b
->name
))
442 static struct branch
* new_branch(const char *name
)
444 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
445 struct branch
* b
= lookup_branch(name
);
448 die("Invalid attempt to create duplicate branch: %s", name
);
449 if (check_ref_format(name
))
450 die("Branch name doesn't conform to GIT standards: %s", name
);
452 b
= pool_calloc(1, sizeof(struct branch
));
453 b
->name
= pool_strdup(name
);
454 b
->table_next_branch
= branch_table
[hc
];
455 branch_table
[hc
] = b
;
460 static unsigned int hc_entries(unsigned int cnt
)
462 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
463 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
466 static struct tree_content
* new_tree_content(unsigned int cnt
)
468 struct avail_tree_content
*f
, *l
= NULL
;
469 struct tree_content
*t
;
470 unsigned int hc
= hc_entries(cnt
);
472 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
473 if (f
->entry_capacity
>= cnt
)
478 l
->next_avail
= f
->next_avail
;
480 avail_tree_table
[hc
] = f
->next_avail
;
482 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
483 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
484 f
->entry_capacity
= cnt
;
487 t
= (struct tree_content
*)f
;
492 static void release_tree_entry(struct tree_entry
*e
);
493 static void release_tree_content(struct tree_content
*t
)
495 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
496 unsigned int hc
= hc_entries(f
->entry_capacity
);
497 f
->next_avail
= avail_tree_table
[hc
];
498 avail_tree_table
[hc
] = f
;
501 static void release_tree_content_recursive(struct tree_content
*t
)
504 for (i
= 0; i
< t
->entry_count
; i
++)
505 release_tree_entry(t
->entries
[i
]);
506 release_tree_content(t
);
509 static struct tree_content
* grow_tree_content(
510 struct tree_content
*t
,
513 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
514 r
->entry_count
= t
->entry_count
;
515 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
516 release_tree_content(t
);
520 static struct tree_entry
* new_tree_entry()
522 struct tree_entry
*e
;
524 if (!avail_tree_entry
) {
525 unsigned int n
= tree_entry_alloc
;
526 total_allocd
+= n
* sizeof(struct tree_entry
);
527 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
529 *((void**)e
) = e
+ 1;
535 e
= avail_tree_entry
;
536 avail_tree_entry
= *((void**)e
);
540 static void release_tree_entry(struct tree_entry
*e
)
543 release_tree_content_recursive(e
->tree
);
544 *((void**)e
) = avail_tree_entry
;
545 avail_tree_entry
= e
;
548 static void yread(int fd
, void *buffer
, size_t length
)
551 while (ret
< length
) {
552 ssize_t size
= xread(fd
, (char *) buffer
+ ret
, length
- ret
);
554 die("Read from descriptor %i: end of stream", fd
);
556 die("Read from descriptor %i: %s", fd
, strerror(errno
));
561 static void ywrite(int fd
, void *buffer
, size_t length
)
564 while (ret
< length
) {
565 ssize_t size
= xwrite(fd
, (char *) buffer
+ ret
, length
- ret
);
567 die("Write to descriptor %i: end of file", fd
);
569 die("Write to descriptor %i: %s", fd
, strerror(errno
));
574 static size_t encode_header(
575 enum object_type type
,
582 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
583 die("bad type %d", type
);
585 c
= (type
<< 4) | (size
& 15);
597 static int store_object(
598 enum object_type type
,
601 struct last_object
*last
,
602 unsigned char *sha1out
,
606 struct object_entry
*e
;
607 unsigned char hdr
[96];
608 unsigned char sha1
[20];
609 unsigned long hdrlen
, deltalen
;
613 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
615 SHA1_Update(&c
, hdr
, hdrlen
);
616 SHA1_Update(&c
, dat
, datlen
);
617 SHA1_Final(sha1
, &c
);
619 memcpy(sha1out
, sha1
, sizeof(sha1
));
621 e
= insert_object(sha1
);
623 insert_mark(mark
, e
);
626 duplicate_count_by_type
[type
]++;
630 e
->offset
= pack_size
;
632 object_count_by_type
[type
]++;
634 if (last
&& last
->data
&& last
->depth
< max_depth
)
635 delta
= diff_delta(last
->data
, last
->len
,
641 memset(&s
, 0, sizeof(s
));
642 deflateInit(&s
, zlib_compression_level
);
647 s
.avail_in
= deltalen
;
648 hdrlen
= encode_header(OBJ_DELTA
, deltalen
, hdr
);
649 ywrite(pack_fd
, hdr
, hdrlen
);
650 ywrite(pack_fd
, last
->sha1
, sizeof(sha1
));
651 pack_size
+= hdrlen
+ sizeof(sha1
);
657 hdrlen
= encode_header(type
, datlen
, hdr
);
658 ywrite(pack_fd
, hdr
, hdrlen
);
662 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
663 s
.next_out
= out
= xmalloc(s
.avail_out
);
664 while (deflate(&s
, Z_FINISH
) == Z_OK
)
668 ywrite(pack_fd
, out
, s
.total_out
);
669 pack_size
+= s
.total_out
;
679 memcpy(last
->sha1
, sha1
, sizeof(sha1
));
684 static unsigned char* map_pack(unsigned long offset
, unsigned int *left
)
686 if (offset
>= pack_size
)
687 die("object offset outside of pack file");
689 || offset
< pack_moff
690 || (offset
+ 20) >= (pack_moff
+ pack_mlen
)) {
692 munmap(pack_base
, pack_mlen
);
693 pack_moff
= (offset
/ page_size
) * page_size
;
694 pack_base
= mmap(NULL
,pack_mlen
,PROT_READ
,MAP_SHARED
,
696 if (pack_base
== MAP_FAILED
)
697 die("Failed to map generated pack: %s", strerror(errno
));
702 *left
= pack_mlen
- offset
;
703 return pack_base
+ offset
;
706 static unsigned long unpack_object_header(unsigned long offset
,
707 enum object_type
*type
,
708 unsigned long *sizep
)
714 c
= *map_pack(offset
++, NULL
);
715 *type
= (c
>> 4) & 7;
719 c
= *map_pack(offset
++, NULL
);
720 size
+= (c
& 0x7f) << shift
;
727 static void *unpack_non_delta_entry(unsigned long o
, unsigned long sz
)
730 unsigned char *result
;
732 result
= xmalloc(sz
+ 1);
735 memset(&stream
, 0, sizeof(stream
));
736 stream
.next_in
= map_pack(o
, &stream
.avail_in
);
737 stream
.next_out
= result
;
738 stream
.avail_out
= sz
;
740 inflateInit(&stream
);
742 int st
= inflate(&stream
, Z_FINISH
);
743 if (st
== Z_STREAM_END
)
745 if (st
== Z_OK
|| st
== Z_BUF_ERROR
) {
746 o
= stream
.next_in
- pack_base
+ pack_moff
;
747 stream
.next_in
= map_pack(o
, &stream
.avail_in
);
750 die("Error %i from zlib during inflate.", st
);
753 if (stream
.total_out
!= sz
)
754 die("Error after inflate: sizes mismatch");
758 static void *unpack_entry(unsigned long offset
, unsigned long *sizep
);
760 static void *unpack_delta_entry(unsigned long offset
,
761 unsigned long delta_size
,
762 unsigned long *sizep
)
764 struct object_entry
*base_oe
;
765 unsigned char *base_sha1
;
766 void *delta_data
, *base
, *result
;
767 unsigned long base_size
, result_size
;
769 base_sha1
= map_pack(offset
, NULL
);
770 base_oe
= find_object(base_sha1
);
772 die("I'm broken; I can't find a base I know must be here.");
773 base
= unpack_entry(base_oe
->offset
, &base_size
);
774 delta_data
= unpack_non_delta_entry(offset
+ 20, delta_size
);
775 result
= patch_delta(base
, base_size
,
776 delta_data
, delta_size
,
779 die("failed to apply delta");
782 *sizep
= result_size
;
786 static void *unpack_entry(unsigned long offset
, unsigned long *sizep
)
789 enum object_type kind
;
791 offset
= unpack_object_header(offset
, &kind
, &size
);
794 return unpack_delta_entry(offset
, size
, sizep
);
800 return unpack_non_delta_entry(offset
, size
);
802 die("I created an object I can't read!");
806 static const char *get_mode(const char *str
, unsigned int *modep
)
809 unsigned int mode
= 0;
811 while ((c
= *str
++) != ' ') {
812 if (c
< '0' || c
> '7')
814 mode
= (mode
<< 3) + (c
- '0');
820 static void load_tree(struct tree_entry
*root
)
822 struct object_entry
*myoe
;
823 struct tree_content
*t
;
828 root
->tree
= t
= new_tree_content(8);
829 if (!memcmp(root
->sha1
, null_sha1
, 20))
832 myoe
= find_object(root
->sha1
);
834 if (myoe
->type
!= OBJ_TREE
)
835 die("Not a tree: %s", sha1_to_hex(root
->sha1
));
836 buf
= unpack_entry(myoe
->offset
, &size
);
839 buf
= read_sha1_file(root
->sha1
, type
, &size
);
840 if (!buf
|| strcmp(type
, tree_type
))
841 die("Can't load tree %s", sha1_to_hex(root
->sha1
));
845 while (c
!= (buf
+ size
)) {
846 struct tree_entry
*e
= new_tree_entry();
848 if (t
->entry_count
== t
->entry_capacity
)
849 root
->tree
= t
= grow_tree_content(t
, 8);
850 t
->entries
[t
->entry_count
++] = e
;
853 c
= get_mode(c
, &e
->mode
);
855 die("Corrupt mode in %s", sha1_to_hex(root
->sha1
));
856 e
->name
= to_atom(c
, strlen(c
));
857 c
+= e
->name
->str_len
+ 1;
858 memcpy(e
->sha1
, c
, sizeof(e
->sha1
));
864 static int tecmp (const void *_a
, const void *_b
)
866 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
867 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
868 return base_name_compare(
869 a
->name
->str_dat
, a
->name
->str_len
, a
->mode
,
870 b
->name
->str_dat
, b
->name
->str_len
, b
->mode
);
873 static void store_tree(struct tree_entry
*root
)
875 struct tree_content
*t
= root
->tree
;
880 if (memcmp(root
->sha1
, null_sha1
, 20))
884 for (i
= 0; i
< t
->entry_count
; i
++) {
885 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
886 if (t
->entries
[i
]->tree
)
887 store_tree(t
->entries
[i
]);
890 qsort(t
->entries
, t
->entry_count
, sizeof(t
->entries
[0]), tecmp
);
891 buf
= c
= xmalloc(maxlen
);
892 for (i
= 0; i
< t
->entry_count
; i
++) {
893 struct tree_entry
*e
= t
->entries
[i
];
894 c
+= sprintf(c
, "%o", e
->mode
);
896 strcpy(c
, e
->name
->str_dat
);
897 c
+= e
->name
->str_len
+ 1;
898 memcpy(c
, e
->sha1
, 20);
901 store_object(OBJ_TREE
, buf
, c
- buf
, NULL
, root
->sha1
, 0);
905 static int tree_content_set(
906 struct tree_entry
*root
,
908 const unsigned char *sha1
,
909 const unsigned int mode
)
911 struct tree_content
*t
= root
->tree
;
914 struct tree_entry
*e
;
916 slash1
= strchr(p
, '/');
922 for (i
= 0; i
< t
->entry_count
; i
++) {
924 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
926 if (e
->mode
== mode
&& !memcmp(e
->sha1
, sha1
, 20))
929 memcpy(e
->sha1
, sha1
, 20);
931 release_tree_content_recursive(e
->tree
);
934 memcpy(root
->sha1
, null_sha1
, 20);
937 if (!S_ISDIR(e
->mode
)) {
938 e
->tree
= new_tree_content(8);
943 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
944 memcpy(root
->sha1
, null_sha1
, 20);
951 if (t
->entry_count
== t
->entry_capacity
)
952 root
->tree
= t
= grow_tree_content(t
, 8);
953 e
= new_tree_entry();
954 e
->name
= to_atom(p
, n
);
955 t
->entries
[t
->entry_count
++] = e
;
957 e
->tree
= new_tree_content(8);
959 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
963 memcpy(e
->sha1
, sha1
, 20);
965 memcpy(root
->sha1
, null_sha1
, 20);
969 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
971 struct tree_content
*t
= root
->tree
;
974 struct tree_entry
*e
;
976 slash1
= strchr(p
, '/');
982 for (i
= 0; i
< t
->entry_count
; i
++) {
984 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
985 if (!slash1
|| !S_ISDIR(e
->mode
))
989 if (tree_content_remove(e
, slash1
+ 1)) {
990 if (!e
->tree
->entry_count
)
992 memcpy(root
->sha1
, null_sha1
, 20);
1001 for (i
++; i
< t
->entry_count
; i
++)
1002 t
->entries
[i
-1] = t
->entries
[i
];
1004 release_tree_entry(e
);
1005 memcpy(root
->sha1
, null_sha1
, 20);
1009 static void init_pack_header()
1011 struct pack_header hdr
;
1013 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
1014 hdr
.hdr_version
= htonl(2);
1015 hdr
.hdr_entries
= 0;
1017 ywrite(pack_fd
, &hdr
, sizeof(hdr
));
1018 pack_size
= sizeof(hdr
);
1021 static void fixup_header_footer()
1029 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
1030 die("Failed seeking to start: %s", strerror(errno
));
1033 yread(pack_fd
, hdr
, 8);
1034 SHA1_Update(&c
, hdr
, 8);
1036 cnt
= htonl(object_count
);
1037 SHA1_Update(&c
, &cnt
, 4);
1038 ywrite(pack_fd
, &cnt
, 4);
1040 buf
= xmalloc(128 * 1024);
1042 n
= xread(pack_fd
, buf
, 128 * 1024);
1045 SHA1_Update(&c
, buf
, n
);
1049 SHA1_Final(pack_sha1
, &c
);
1050 ywrite(pack_fd
, pack_sha1
, sizeof(pack_sha1
));
1053 static int oecmp (const void *_a
, const void *_b
)
1055 struct object_entry
*a
= *((struct object_entry
**)_a
);
1056 struct object_entry
*b
= *((struct object_entry
**)_b
);
1057 return memcmp(a
->sha1
, b
->sha1
, sizeof(a
->sha1
));
1060 static void write_index(const char *idx_name
)
1063 struct object_entry
**idx
, **c
, **last
;
1064 struct object_entry
*e
;
1065 struct object_entry_pool
*o
;
1066 unsigned int array
[256];
1069 /* Build the sorted table of object IDs. */
1070 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
1072 for (o
= blocks
; o
; o
= o
->next_pool
)
1073 for (e
= o
->entries
; e
!= o
->next_free
; e
++)
1075 last
= idx
+ object_count
;
1076 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
1078 /* Generate the fan-out array. */
1080 for (i
= 0; i
< 256; i
++) {
1081 struct object_entry
**next
= c
;;
1082 while (next
< last
) {
1083 if ((*next
)->sha1
[0] != i
)
1087 array
[i
] = htonl(next
- idx
);
1091 f
= sha1create("%s", idx_name
);
1092 sha1write(f
, array
, 256 * sizeof(int));
1093 for (c
= idx
; c
!= last
; c
++) {
1094 unsigned int offset
= htonl((*c
)->offset
);
1095 sha1write(f
, &offset
, 4);
1096 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
1098 sha1write(f
, pack_sha1
, sizeof(pack_sha1
));
1099 sha1close(f
, NULL
, 1);
1103 static void dump_branches()
1105 static const char *msg
= "fast-import";
1108 struct ref_lock
*lock
;
1110 for (i
= 0; i
< branch_table_sz
; i
++) {
1111 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
1112 lock
= lock_any_ref_for_update(b
->name
, NULL
, 0);
1113 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1114 die("Can't write %s", b
->name
);
1119 static void dump_tags()
1121 static const char *msg
= "fast-import";
1123 struct ref_lock
*lock
;
1124 char path
[PATH_MAX
];
1126 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1127 sprintf(path
, "refs/tags/%s", t
->name
);
1128 lock
= lock_any_ref_for_update(path
, NULL
, 0);
1129 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1130 die("Can't write %s", path
);
1134 static void dump_marks_helper(FILE *f
,
1140 for (k
= 0; k
< 1024; k
++) {
1141 if (m
->data
.sets
[k
])
1142 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1146 for (k
= 0; k
< 1024; k
++) {
1147 if (m
->data
.marked
[k
])
1148 fprintf(f
, ":%lu %s\n", base
+ k
,
1149 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1154 static void dump_marks()
1158 FILE *f
= fopen(mark_file
, "w");
1159 dump_marks_helper(f
, 0, marks
);
1164 static void read_next_command()
1166 read_line(&command_buf
, stdin
, '\n');
1169 static void cmd_mark()
1171 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1172 next_mark
= strtoul(command_buf
.buf
+ 6, NULL
, 10);
1173 read_next_command();
1179 static void* cmd_data (size_t *size
)
1185 if (strncmp("data ", command_buf
.buf
, 5))
1186 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1188 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1189 buffer
= xmalloc(length
);
1191 while (n
< length
) {
1192 size_t s
= fread((char*)buffer
+ n
, 1, length
- n
, stdin
);
1193 if (!s
&& feof(stdin
))
1194 die("EOF in data (%lu bytes remaining)", length
- n
);
1198 if (fgetc(stdin
) != '\n')
1199 die("An lf did not trail the binary data as expected.");
1205 static void cmd_new_blob()
1210 read_next_command();
1214 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1218 static void unload_one_branch()
1220 while (cur_active_branches
1221 && cur_active_branches
>= max_active_branches
) {
1222 unsigned long min_commit
= ULONG_MAX
;
1223 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1225 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1226 if (e
->last_commit
< min_commit
) {
1228 min_commit
= e
->last_commit
;
1234 e
= p
->active_next_branch
;
1235 p
->active_next_branch
= e
->active_next_branch
;
1237 e
= active_branches
;
1238 active_branches
= e
->active_next_branch
;
1240 e
->active_next_branch
= NULL
;
1241 if (e
->branch_tree
.tree
) {
1242 release_tree_content_recursive(e
->branch_tree
.tree
);
1243 e
->branch_tree
.tree
= NULL
;
1245 cur_active_branches
--;
1249 static void load_branch(struct branch
*b
)
1251 load_tree(&b
->branch_tree
);
1252 b
->active_next_branch
= active_branches
;
1253 active_branches
= b
;
1254 cur_active_branches
++;
1255 branch_load_count
++;
1258 static void file_change_m(struct branch
*b
)
1260 const char *p
= command_buf
.buf
+ 2;
1263 struct object_entry
*oe
;
1264 unsigned char sha1
[20];
1268 p
= get_mode(p
, &mode
);
1270 die("Corrupt mode: %s", command_buf
.buf
);
1272 case S_IFREG
| 0644:
1273 case S_IFREG
| 0755:
1280 die("Corrupt mode: %s", command_buf
.buf
);
1285 oe
= find_mark(strtoul(p
+ 1, &x
, 10));
1288 if (get_sha1_hex(p
, sha1
))
1289 die("Invalid SHA1: %s", command_buf
.buf
);
1290 oe
= find_object(sha1
);
1294 die("Missing space after SHA1: %s", command_buf
.buf
);
1296 p_uq
= unquote_c_style(p
, &endp
);
1299 die("Garbage after path in: %s", command_buf
.buf
);
1304 if (oe
->type
!= OBJ_BLOB
)
1305 die("Not a blob (actually a %s): %s",
1306 command_buf
.buf
, type_names
[oe
->type
]);
1308 if (sha1_object_info(sha1
, type
, NULL
))
1309 die("Blob not found: %s", command_buf
.buf
);
1310 if (strcmp(blob_type
, type
))
1311 die("Not a blob (actually a %s): %s",
1312 command_buf
.buf
, type
);
1315 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1321 static void file_change_d(struct branch
*b
)
1323 const char *p
= command_buf
.buf
+ 2;
1327 p_uq
= unquote_c_style(p
, &endp
);
1330 die("Garbage after path in: %s", command_buf
.buf
);
1333 tree_content_remove(&b
->branch_tree
, p
);
1338 static void cmd_from(struct branch
*b
)
1340 const char *from
, *endp
;
1344 if (strncmp("from ", command_buf
.buf
, 5))
1348 die("Can't reinitailize branch %s", b
->name
);
1350 from
= strchr(command_buf
.buf
, ' ') + 1;
1351 str_uq
= unquote_c_style(from
, &endp
);
1354 die("Garbage after string in: %s", command_buf
.buf
);
1358 s
= lookup_branch(from
);
1360 die("Can't create a branch from itself: %s", b
->name
);
1362 memcpy(b
->sha1
, s
->sha1
, 20);
1363 memcpy(b
->branch_tree
.sha1
, s
->branch_tree
.sha1
, 20);
1364 } else if (*from
== ':') {
1365 unsigned long idnum
= strtoul(from
+ 1, NULL
, 10);
1366 struct object_entry
*oe
= find_mark(idnum
);
1369 if (oe
->type
!= OBJ_COMMIT
)
1370 die("Mark :%lu not a commit", idnum
);
1371 memcpy(b
->sha1
, oe
->sha1
, 20);
1372 buf
= unpack_entry(oe
->offset
, &size
);
1373 if (!buf
|| size
< 46)
1374 die("Not a valid commit: %s", from
);
1375 if (memcmp("tree ", buf
, 5)
1376 || get_sha1_hex(buf
+ 5, b
->branch_tree
.sha1
))
1377 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1379 } else if (!get_sha1(from
, b
->sha1
)) {
1380 if (!memcmp(b
->sha1
, null_sha1
, 20))
1381 memcpy(b
->branch_tree
.sha1
, null_sha1
, 20);
1386 buf
= read_object_with_reference(b
->sha1
,
1387 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1388 if (!buf
|| size
< 46)
1389 die("Not a valid commit: %s", from
);
1390 if (memcmp("tree ", buf
, 5)
1391 || get_sha1_hex(buf
+ 5, b
->branch_tree
.sha1
))
1392 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1396 die("Invalid ref name or SHA1 expression: %s", from
);
1398 read_next_command();
1401 static void cmd_new_commit()
1409 char *author
= NULL
;
1410 char *committer
= NULL
;
1413 /* Obtain the branch name from the rest of our command */
1414 sp
= strchr(command_buf
.buf
, ' ') + 1;
1415 str_uq
= unquote_c_style(sp
, &endp
);
1418 die("Garbage after ref in: %s", command_buf
.buf
);
1421 b
= lookup_branch(sp
);
1427 read_next_command();
1429 if (!strncmp("author ", command_buf
.buf
, 7)) {
1430 author
= strdup(command_buf
.buf
);
1431 read_next_command();
1433 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1434 committer
= strdup(command_buf
.buf
);
1435 read_next_command();
1438 die("Expected committer but didn't get one");
1439 msg
= cmd_data(&msglen
);
1440 read_next_command();
1443 /* ensure the branch is active/loaded */
1444 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1445 unload_one_branch();
1451 if (1 == command_buf
.len
)
1453 else if (!strncmp("M ", command_buf
.buf
, 2))
1455 else if (!strncmp("D ", command_buf
.buf
, 2))
1458 die("Unsupported file_change: %s", command_buf
.buf
);
1459 read_next_command();
1462 /* build the tree and the commit */
1463 store_tree(&b
->branch_tree
);
1464 body
= xmalloc(97 + msglen
1466 ? strlen(author
) + strlen(committer
)
1467 : 2 * strlen(committer
)));
1469 sp
+= sprintf(sp
, "tree %s\n", sha1_to_hex(b
->branch_tree
.sha1
));
1470 if (memcmp(b
->sha1
, null_sha1
, 20))
1471 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1473 sp
+= sprintf(sp
, "%s\n", author
);
1475 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1476 sp
+= sprintf(sp
, "%s\n\n", committer
);
1477 memcpy(sp
, msg
, msglen
);
1484 store_object(OBJ_COMMIT
, body
, sp
- body
, NULL
, b
->sha1
, next_mark
);
1486 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1489 int need_dq
= quote_c_style(b
->name
, NULL
, NULL
, 0);
1490 fprintf(branch_log
, "commit ");
1492 fputc('"', branch_log
);
1493 quote_c_style(b
->name
, NULL
, branch_log
, 0);
1494 fputc('"', branch_log
);
1496 fprintf(branch_log
, "%s", b
->name
);
1497 fprintf(branch_log
," :%lu %s\n",next_mark
,sha1_to_hex(b
->sha1
));
1501 static void cmd_new_tag()
1513 unsigned long from_mark
= 0;
1514 unsigned char sha1
[20];
1516 /* Obtain the new tag name from the rest of our command */
1517 sp
= strchr(command_buf
.buf
, ' ') + 1;
1518 str_uq
= unquote_c_style(sp
, &endp
);
1521 die("Garbage after tag name in: %s", command_buf
.buf
);
1524 t
= pool_alloc(sizeof(struct tag
));
1526 t
->name
= pool_strdup(sp
);
1528 last_tag
->next_tag
= t
;
1534 read_next_command();
1537 if (strncmp("from ", command_buf
.buf
, 5))
1538 die("Expected from command, got %s", command_buf
.buf
);
1540 from
= strchr(command_buf
.buf
, ' ') + 1;
1541 str_uq
= unquote_c_style(from
, &endp
);
1544 die("Garbage after string in: %s", command_buf
.buf
);
1548 s
= lookup_branch(from
);
1550 memcpy(sha1
, s
->sha1
, 20);
1551 } else if (*from
== ':') {
1552 from_mark
= strtoul(from
+ 1, NULL
, 10);
1553 struct object_entry
*oe
= find_mark(from_mark
);
1554 if (oe
->type
!= OBJ_COMMIT
)
1555 die("Mark :%lu not a commit", from_mark
);
1556 memcpy(sha1
, oe
->sha1
, 20);
1557 } else if (!get_sha1(from
, sha1
)) {
1561 buf
= read_object_with_reference(sha1
,
1562 type_names
[OBJ_COMMIT
], &size
, sha1
);
1563 if (!buf
|| size
< 46)
1564 die("Not a valid commit: %s", from
);
1567 die("Invalid ref name or SHA1 expression: %s", from
);
1571 read_next_command();
1574 if (strncmp("tagger ", command_buf
.buf
, 7))
1575 die("Expected tagger command, got %s", command_buf
.buf
);
1576 tagger
= strdup(command_buf
.buf
);
1578 /* tag payload/message */
1579 read_next_command();
1580 msg
= cmd_data(&msglen
);
1582 /* build the tag object */
1583 body
= xmalloc(67 + strlen(t
->name
) + strlen(tagger
) + msglen
);
1585 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1586 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1587 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1588 sp
+= sprintf(sp
, "%s\n\n", tagger
);
1589 memcpy(sp
, msg
, msglen
);
1594 store_object(OBJ_TAG
, body
, sp
- body
, NULL
, t
->sha1
, 0);
1598 int need_dq
= quote_c_style(t
->name
, NULL
, NULL
, 0);
1599 fprintf(branch_log
, "tag ");
1601 fputc('"', branch_log
);
1602 quote_c_style(t
->name
, NULL
, branch_log
, 0);
1603 fputc('"', branch_log
);
1605 fprintf(branch_log
, "%s", t
->name
);
1606 fprintf(branch_log
," :%lu %s\n",from_mark
,sha1_to_hex(t
->sha1
));
1610 static void cmd_reset_branch()
1617 /* Obtain the branch name from the rest of our command */
1618 sp
= strchr(command_buf
.buf
, ' ') + 1;
1619 str_uq
= unquote_c_style(sp
, &endp
);
1622 die("Garbage after ref in: %s", command_buf
.buf
);
1625 b
= lookup_branch(sp
);
1628 if (b
->branch_tree
.tree
) {
1629 release_tree_content_recursive(b
->branch_tree
.tree
);
1630 b
->branch_tree
.tree
= NULL
;
1637 static const char fast_import_usage
[] =
1638 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1640 int main(int argc
, const char **argv
)
1642 const char *base_name
;
1644 unsigned long est_obj_cnt
= 1000;
1650 git_config(git_default_config
);
1651 page_size
= getpagesize();
1653 for (i
= 1; i
< argc
; i
++) {
1654 const char *a
= argv
[i
];
1656 if (*a
!= '-' || !strcmp(a
, "--"))
1658 else if (!strncmp(a
, "--objects=", 10))
1659 est_obj_cnt
= strtoul(a
+ 10, NULL
, 0);
1660 else if (!strncmp(a
, "--depth=", 8))
1661 max_depth
= strtoul(a
+ 8, NULL
, 0);
1662 else if (!strncmp(a
, "--active-branches=", 18))
1663 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1664 else if (!strncmp(a
, "--export-marks=", 15))
1666 else if (!strncmp(a
, "--branch-log=", 13)) {
1667 branch_log
= fopen(a
+ 13, "w");
1669 die("Can't create %s: %s", a
+ 13, strerror(errno
));
1672 die("unknown option %s", a
);
1675 usage(fast_import_usage
);
1676 base_name
= argv
[i
];
1678 pack_name
= xmalloc(strlen(base_name
) + 6);
1679 sprintf(pack_name
, "%s.pack", base_name
);
1680 idx_name
= xmalloc(strlen(base_name
) + 5);
1681 sprintf(idx_name
, "%s.idx", base_name
);
1683 pack_fd
= open(pack_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1685 die("Can't create %s: %s", pack_name
, strerror(errno
));
1688 alloc_objects(est_obj_cnt
);
1689 strbuf_init(&command_buf
);
1691 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1692 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1693 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1694 marks
= pool_calloc(1, sizeof(struct mark_set
));
1697 read_next_command();
1698 if (command_buf
.eof
)
1700 else if (!strcmp("blob", command_buf
.buf
))
1702 else if (!strncmp("commit ", command_buf
.buf
, 7))
1704 else if (!strncmp("tag ", command_buf
.buf
, 4))
1706 else if (!strncmp("reset ", command_buf
.buf
, 6))
1709 die("Unsupported command: %s", command_buf
.buf
);
1712 fixup_header_footer();
1714 write_index(idx_name
);
1721 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1722 fprintf(stderr
, "---------------------------------------------------\n");
1723 fprintf(stderr
, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count
, alloc_count
- est_obj_cnt
);
1724 fprintf(stderr
, "Total objects: %10lu (%10lu duplicates)\n", object_count
, duplicate_count
);
1725 fprintf(stderr
, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
]);
1726 fprintf(stderr
, " trees : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
]);
1727 fprintf(stderr
, " commits: %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
]);
1728 fprintf(stderr
, " tags : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
]);
1729 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
1730 fprintf(stderr
, " marks: %10u (%10lu unique )\n", (1 << marks
->shift
) * 1024, marks_set_count
);
1731 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
1732 fprintf(stderr
, "Memory total: %10lu KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
1733 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
1734 fprintf(stderr
, " objects: %10lu KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
1735 fprintf(stderr
, "Pack remaps: %10lu\n", remap_count
);
1736 fprintf(stderr
, "---------------------------------------------------\n");
1738 stat(pack_name
, &sb
);
1739 fprintf(stderr
, "Pack size: %10lu KiB\n", (unsigned long)(sb
.st_size
/1024));
1740 stat(idx_name
, &sb
);
1741 fprintf(stderr
, "Index size: %10lu KiB\n", (unsigned long)(sb
.st_size
/1024));
1743 fprintf(stderr
, "\n");