]>
git.ipfire.org Git - thirdparty/git.git/blob - fast-import.c
2 Format of STDIN stream:
12 new_blob ::= 'blob' lf
15 file_content ::= data;
17 new_branch ::= 'branch' sp ref_str lf
18 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
21 new_commit ::= 'commit' sp ref_str lf
23 ('author' sp name '<' email '>' ts tz lf)?
24 'committer' sp name '<' email '>' ts tz lf
30 file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
33 mode ::= '644' | '755';
35 new_tag ::= 'tag' sp tag_str lf
36 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
37 'tagger' sp name '<' email '>' ts tz lf
41 # note: the first idnum in a stream should be 1 and subsequent
42 # idnums should not have gaps between values as this will cause
43 # the stream parser to reserve space for the gapped values. An
44 # idnum can be updated in the future to a new object by issuing
45 # a new mark directive with the old idnum.
47 mark ::= 'mark' sp idnum lf;
49 # note: declen indicates the length of binary_data in bytes.
50 # declen does not include the lf preceeding or trailing the
53 data ::= 'data' sp declen lf
57 # note: quoted strings are C-style quoting supporting \c for
58 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
59 # is the signed byte value in octal. Note that the only
60 # characters which must actually be escaped to protect the
61 # stream formatting is: \, " and LF. Otherwise these values
64 ref_str ::= ref | '"' quoted(ref) '"' ;
65 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
66 tag_str ::= tag | '"' quoted(tag) '"' ;
67 path_str ::= path | '"' quoted(path) '"' ;
69 declen ::= # unsigned 32 bit value, ascii base10 notation;
70 binary_data ::= # file content, not interpreted;
72 sp ::= # ASCII space character;
73 lf ::= # ASCII newline (LF) character;
75 # note: a colon (':') must precede the numerical value assigned to
76 # an idnum. This is to distinguish it from a ref or tag name as
77 # GIT does not permit ':' in ref or tag strings.
80 path ::= # GIT style file path, e.g. "a/b/c";
81 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
82 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
83 sha1exp ::= # Any valid GIT SHA1 expression;
84 hexsha1 ::= # SHA1 in hexadecimal format;
86 # note: name and email are UTF8 strings, however name must not
87 # contain '<' or lf and email must not contain any of the
88 # following: '<', '>', lf.
90 name ::= # valid GIT author/committer name;
91 email ::= # valid GIT author/committer email;
92 ts ::= # time since the epoch in seconds, ascii base10 notation;
93 tz ::= # GIT style timezone;
104 #include "csum-file.h"
110 struct object_entry
*next
;
111 enum object_type type
;
112 unsigned long offset
;
113 unsigned char sha1
[20];
116 struct object_entry_pool
118 struct object_entry_pool
*next_pool
;
119 struct object_entry
*next_free
;
120 struct object_entry
*end
;
121 struct object_entry entries
[FLEX_ARRAY
]; /* more */
128 struct object_entry
*marked
[1024];
129 struct mark_set
*sets
[1024];
138 unsigned char sha1
[20];
143 struct mem_pool
*next_pool
;
146 char space
[FLEX_ARRAY
]; /* more */
151 struct atom_str
*next_atom
;
153 char str_dat
[FLEX_ARRAY
]; /* more */
159 struct tree_content
*tree
;
160 struct atom_str
* name
;
162 unsigned char sha1
[20];
167 unsigned int entry_capacity
; /* must match avail_tree_content */
168 unsigned int entry_count
;
169 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
172 struct avail_tree_content
174 unsigned int entry_capacity
; /* must match tree_content */
175 struct avail_tree_content
*next_avail
;
180 struct branch
*table_next_branch
;
181 struct branch
*active_next_branch
;
183 unsigned long last_commit
;
184 struct tree_entry branch_tree
;
185 unsigned char sha1
[20];
189 /* Stats and misc. counters */
190 static unsigned long max_depth
= 10;
191 static unsigned long alloc_count
;
192 static unsigned long branch_count
;
193 static unsigned long branch_load_count
;
194 static unsigned long object_count
;
195 static unsigned long duplicate_count
;
196 static unsigned long marks_set_count
;
197 static unsigned long object_count_by_type
[9];
198 static unsigned long duplicate_count_by_type
[9];
201 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
202 static size_t total_allocd
;
203 static struct mem_pool
*mem_pool
;
205 /* Atom management */
206 static unsigned int atom_table_sz
= 4451;
207 static unsigned int atom_cnt
;
208 static struct atom_str
**atom_table
;
210 /* The .pack file being generated */
212 static unsigned long pack_offset
;
213 static unsigned char pack_sha1
[20];
215 /* Table of objects we've written. */
216 static unsigned int object_entry_alloc
= 1000;
217 static struct object_entry_pool
*blocks
;
218 static struct object_entry
*object_table
[1 << 16];
219 static struct mark_set
*marks
;
222 static struct last_object last_blob
;
224 /* Tree management */
225 static unsigned int tree_entry_alloc
= 1000;
226 static void *avail_tree_entry
;
227 static unsigned int avail_tree_table_sz
= 100;
228 static struct avail_tree_content
**avail_tree_table
;
231 static unsigned long max_active_branches
= 5;
232 static unsigned long cur_active_branches
;
233 static unsigned long branch_table_sz
= 1039;
234 static struct branch
**branch_table
;
235 static struct branch
*active_branches
;
237 /* Input stream parsing */
238 static struct strbuf command_buf
;
239 static unsigned long next_mark
;
242 static void alloc_objects(int cnt
)
244 struct object_entry_pool
*b
;
246 b
= xmalloc(sizeof(struct object_entry_pool
)
247 + cnt
* sizeof(struct object_entry
));
248 b
->next_pool
= blocks
;
249 b
->next_free
= b
->entries
;
250 b
->end
= b
->entries
+ cnt
;
255 static struct object_entry
* new_object(unsigned char *sha1
)
257 struct object_entry
*e
;
259 if (blocks
->next_free
== blocks
->end
)
260 alloc_objects(object_entry_alloc
);
262 e
= blocks
->next_free
++;
263 memcpy(e
->sha1
, sha1
, sizeof(e
->sha1
));
267 static struct object_entry
* find_object(unsigned char *sha1
)
269 unsigned int h
= sha1
[0] << 8 | sha1
[1];
270 struct object_entry
*e
;
271 for (e
= object_table
[h
]; e
; e
= e
->next
)
272 if (!memcmp(sha1
, e
->sha1
, sizeof(e
->sha1
)))
277 static struct object_entry
* insert_object(unsigned char *sha1
)
279 unsigned int h
= sha1
[0] << 8 | sha1
[1];
280 struct object_entry
*e
= object_table
[h
];
281 struct object_entry
*p
= NULL
;
284 if (!memcmp(sha1
, e
->sha1
, sizeof(e
->sha1
)))
290 e
= new_object(sha1
);
300 static unsigned int hc_str(const char *s
, size_t len
)
308 static void* pool_alloc(size_t len
)
313 for (p
= mem_pool
; p
; p
= p
->next_pool
)
314 if ((p
->end
- p
->next_free
>= len
))
318 if (len
>= (mem_pool_alloc
/2)) {
322 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
323 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
324 p
->next_pool
= mem_pool
;
325 p
->next_free
= p
->space
;
326 p
->end
= p
->next_free
+ mem_pool_alloc
;
335 static void* pool_calloc(size_t count
, size_t size
)
337 size_t len
= count
* size
;
338 void *r
= pool_alloc(len
);
343 static char* pool_strdup(const char *s
)
345 char *r
= pool_alloc(strlen(s
) + 1);
350 static void insert_mark(unsigned long idnum
, struct object_entry
*oe
)
352 struct mark_set
*s
= marks
;
353 while ((idnum
>> s
->shift
) >= 1024) {
354 s
= pool_calloc(1, sizeof(struct mark_set
));
355 s
->shift
= marks
->shift
+ 10;
356 s
->data
.sets
[0] = marks
;
360 unsigned long i
= idnum
>> s
->shift
;
361 idnum
-= i
<< s
->shift
;
362 if (!s
->data
.sets
[i
]) {
363 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
364 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
368 if (!s
->data
.marked
[idnum
])
370 s
->data
.marked
[idnum
] = oe
;
373 static struct object_entry
* find_mark(unsigned long idnum
)
375 unsigned long orig_idnum
= idnum
;
376 struct mark_set
*s
= marks
;
377 struct object_entry
*oe
= NULL
;
378 if ((idnum
>> s
->shift
) < 1024) {
379 while (s
&& s
->shift
) {
380 unsigned long i
= idnum
>> s
->shift
;
381 idnum
-= i
<< s
->shift
;
385 oe
= s
->data
.marked
[idnum
];
388 die("mark :%lu not declared", orig_idnum
);
392 static struct atom_str
* to_atom(const char *s
, size_t len
)
394 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
397 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
398 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
401 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
403 strncpy(c
->str_dat
, s
, len
);
405 c
->next_atom
= atom_table
[hc
];
411 static struct branch
* lookup_branch(const char *name
)
413 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
416 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
417 if (!strcmp(name
, b
->name
))
422 static struct branch
* new_branch(const char *name
)
424 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
425 struct branch
* b
= lookup_branch(name
);
428 die("Invalid attempt to create duplicate branch: %s", name
);
429 if (check_ref_format(name
))
430 die("Branch name doesn't conform to GIT standards: %s", name
);
432 b
= pool_calloc(1, sizeof(struct branch
));
433 b
->name
= pool_strdup(name
);
434 b
->table_next_branch
= branch_table
[hc
];
435 branch_table
[hc
] = b
;
440 static unsigned int hc_entries(unsigned int cnt
)
442 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
443 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
446 static struct tree_content
* new_tree_content(unsigned int cnt
)
448 struct avail_tree_content
*f
, *l
= NULL
;
449 struct tree_content
*t
;
450 unsigned int hc
= hc_entries(cnt
);
452 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
453 if (f
->entry_capacity
>= cnt
)
458 l
->next_avail
= f
->next_avail
;
460 avail_tree_table
[hc
] = f
->next_avail
;
462 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
463 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
464 f
->entry_capacity
= cnt
;
467 t
= (struct tree_content
*)f
;
472 static void release_tree_entry(struct tree_entry
*e
);
473 static void release_tree_content(struct tree_content
*t
)
475 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
476 unsigned int hc
= hc_entries(f
->entry_capacity
);
477 f
->next_avail
= avail_tree_table
[hc
];
478 avail_tree_table
[hc
] = f
;
481 static void release_tree_content_recursive(struct tree_content
*t
)
484 for (i
= 0; i
< t
->entry_count
; i
++)
485 release_tree_entry(t
->entries
[i
]);
486 release_tree_content(t
);
489 static struct tree_content
* grow_tree_content(
490 struct tree_content
*t
,
493 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
494 r
->entry_count
= t
->entry_count
;
495 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
496 release_tree_content(t
);
500 static struct tree_entry
* new_tree_entry()
502 struct tree_entry
*e
;
504 if (!avail_tree_entry
) {
505 unsigned int n
= tree_entry_alloc
;
506 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
508 *((void**)e
) = e
+ 1;
513 e
= avail_tree_entry
;
514 avail_tree_entry
= *((void**)e
);
518 static void release_tree_entry(struct tree_entry
*e
)
521 release_tree_content_recursive(e
->tree
);
522 *((void**)e
) = avail_tree_entry
;
523 avail_tree_entry
= e
;
526 static void yread(int fd
, void *buffer
, size_t length
)
529 while (ret
< length
) {
530 ssize_t size
= xread(fd
, (char *) buffer
+ ret
, length
- ret
);
532 die("Read from descriptor %i: end of stream", fd
);
534 die("Read from descriptor %i: %s", fd
, strerror(errno
));
539 static void ywrite(int fd
, void *buffer
, size_t length
)
542 while (ret
< length
) {
543 ssize_t size
= xwrite(fd
, (char *) buffer
+ ret
, length
- ret
);
545 die("Write to descriptor %i: end of file", fd
);
547 die("Write to descriptor %i: %s", fd
, strerror(errno
));
552 static size_t encode_header(
553 enum object_type type
,
560 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
561 die("bad type %d", type
);
563 c
= (type
<< 4) | (size
& 15);
575 static int store_object(
576 enum object_type type
,
579 struct last_object
*last
,
580 unsigned char *sha1out
,
584 struct object_entry
*e
;
585 unsigned char hdr
[96];
586 unsigned char sha1
[20];
587 unsigned long hdrlen
, deltalen
;
591 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
593 SHA1_Update(&c
, hdr
, hdrlen
);
594 SHA1_Update(&c
, dat
, datlen
);
595 SHA1_Final(sha1
, &c
);
597 memcpy(sha1out
, sha1
, sizeof(sha1
));
599 e
= insert_object(sha1
);
601 insert_mark(mark
, e
);
604 duplicate_count_by_type
[type
]++;
608 e
->offset
= pack_offset
;
610 object_count_by_type
[type
]++;
612 if (last
&& last
->data
&& last
->depth
< max_depth
)
613 delta
= diff_delta(last
->data
, last
->len
,
619 memset(&s
, 0, sizeof(s
));
620 deflateInit(&s
, zlib_compression_level
);
625 s
.avail_in
= deltalen
;
626 hdrlen
= encode_header(OBJ_DELTA
, deltalen
, hdr
);
627 ywrite(pack_fd
, hdr
, hdrlen
);
628 ywrite(pack_fd
, last
->sha1
, sizeof(sha1
));
629 pack_offset
+= hdrlen
+ sizeof(sha1
);
635 hdrlen
= encode_header(type
, datlen
, hdr
);
636 ywrite(pack_fd
, hdr
, hdrlen
);
637 pack_offset
+= hdrlen
;
640 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
641 s
.next_out
= out
= xmalloc(s
.avail_out
);
642 while (deflate(&s
, Z_FINISH
) == Z_OK
)
646 ywrite(pack_fd
, out
, s
.total_out
);
647 pack_offset
+= s
.total_out
;
657 memcpy(last
->sha1
, sha1
, sizeof(sha1
));
662 static const char *get_mode(const char *str
, unsigned int *modep
)
665 unsigned int mode
= 0;
667 while ((c
= *str
++) != ' ') {
668 if (c
< '0' || c
> '7')
670 mode
= (mode
<< 3) + (c
- '0');
676 static void load_tree(struct tree_entry
*root
)
678 struct object_entry
*myoe
;
679 struct tree_content
*t
;
685 root
->tree
= t
= new_tree_content(8);
686 if (!memcmp(root
->sha1
, null_sha1
, 20))
689 myoe
= find_object(root
->sha1
);
693 buf
= read_sha1_file(root
->sha1
, type
, &size
);
694 if (!buf
|| strcmp(type
, tree_type
))
695 die("Can't load existing tree %s", sha1_to_hex(root
->sha1
));
699 while (c
!= (buf
+ size
)) {
700 struct tree_entry
*e
= new_tree_entry();
702 if (t
->entry_count
== t
->entry_capacity
)
703 root
->tree
= t
= grow_tree_content(t
, 8);
704 t
->entries
[t
->entry_count
++] = e
;
707 c
= get_mode(c
, &e
->mode
);
709 die("Corrupt mode in %s", sha1_to_hex(root
->sha1
));
710 e
->name
= to_atom(c
, strlen(c
));
711 c
+= e
->name
->str_len
+ 1;
712 memcpy(e
->sha1
, c
, sizeof(e
->sha1
));
718 static int tecmp (const void *_a
, const void *_b
)
720 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
721 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
722 return base_name_compare(
723 a
->name
->str_dat
, a
->name
->str_len
, a
->mode
,
724 b
->name
->str_dat
, b
->name
->str_len
, b
->mode
);
727 static void store_tree(struct tree_entry
*root
)
729 struct tree_content
*t
= root
->tree
;
734 if (memcmp(root
->sha1
, null_sha1
, 20))
738 for (i
= 0; i
< t
->entry_count
; i
++) {
739 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
740 if (t
->entries
[i
]->tree
)
741 store_tree(t
->entries
[i
]);
744 qsort(t
->entries
, t
->entry_count
, sizeof(t
->entries
[0]), tecmp
);
745 buf
= c
= xmalloc(maxlen
);
746 for (i
= 0; i
< t
->entry_count
; i
++) {
747 struct tree_entry
*e
= t
->entries
[i
];
748 c
+= sprintf(c
, "%o", e
->mode
);
750 strcpy(c
, e
->name
->str_dat
);
751 c
+= e
->name
->str_len
+ 1;
752 memcpy(c
, e
->sha1
, 20);
755 store_object(OBJ_TREE
, buf
, c
- buf
, NULL
, root
->sha1
, 0);
759 static int tree_content_set(
760 struct tree_entry
*root
,
762 const unsigned char *sha1
,
763 const unsigned int mode
)
765 struct tree_content
*t
= root
->tree
;
768 struct tree_entry
*e
;
770 slash1
= strchr(p
, '/');
776 for (i
= 0; i
< t
->entry_count
; i
++) {
778 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
780 if (e
->mode
== mode
&& !memcmp(e
->sha1
, sha1
, 20))
783 memcpy(e
->sha1
, sha1
, 20);
785 release_tree_content_recursive(e
->tree
);
788 memcpy(root
->sha1
, null_sha1
, 20);
791 if (!S_ISDIR(e
->mode
)) {
792 e
->tree
= new_tree_content(8);
797 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
798 memcpy(root
->sha1
, null_sha1
, 20);
805 if (t
->entry_count
== t
->entry_capacity
)
806 root
->tree
= t
= grow_tree_content(t
, 8);
807 e
= new_tree_entry();
808 e
->name
= to_atom(p
, n
);
809 t
->entries
[t
->entry_count
++] = e
;
811 e
->tree
= new_tree_content(8);
813 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
817 memcpy(e
->sha1
, sha1
, 20);
819 memcpy(root
->sha1
, null_sha1
, 20);
823 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
825 struct tree_content
*t
= root
->tree
;
828 struct tree_entry
*e
;
830 slash1
= strchr(p
, '/');
836 for (i
= 0; i
< t
->entry_count
; i
++) {
838 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
839 if (!slash1
|| !S_ISDIR(e
->mode
))
843 if (tree_content_remove(e
, slash1
+ 1)) {
844 if (!e
->tree
->entry_count
)
846 memcpy(root
->sha1
, null_sha1
, 20);
855 for (i
++; i
< t
->entry_count
; i
++)
856 t
->entries
[i
-1] = t
->entries
[i
];
858 release_tree_entry(e
);
859 memcpy(root
->sha1
, null_sha1
, 20);
863 static void init_pack_header()
865 struct pack_header hdr
;
867 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
868 hdr
.hdr_version
= htonl(2);
871 ywrite(pack_fd
, &hdr
, sizeof(hdr
));
872 pack_offset
= sizeof(hdr
);
875 static void fixup_header_footer()
883 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
884 die("Failed seeking to start: %s", strerror(errno
));
887 yread(pack_fd
, hdr
, 8);
888 SHA1_Update(&c
, hdr
, 8);
890 cnt
= htonl(object_count
);
891 SHA1_Update(&c
, &cnt
, 4);
892 ywrite(pack_fd
, &cnt
, 4);
894 buf
= xmalloc(128 * 1024);
896 n
= xread(pack_fd
, buf
, 128 * 1024);
899 SHA1_Update(&c
, buf
, n
);
903 SHA1_Final(pack_sha1
, &c
);
904 ywrite(pack_fd
, pack_sha1
, sizeof(pack_sha1
));
907 static int oecmp (const void *_a
, const void *_b
)
909 struct object_entry
*a
= *((struct object_entry
**)_a
);
910 struct object_entry
*b
= *((struct object_entry
**)_b
);
911 return memcmp(a
->sha1
, b
->sha1
, sizeof(a
->sha1
));
914 static void write_index(const char *idx_name
)
917 struct object_entry
**idx
, **c
, **last
;
918 struct object_entry
*e
;
919 struct object_entry_pool
*o
;
920 unsigned int array
[256];
923 /* Build the sorted table of object IDs. */
924 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
926 for (o
= blocks
; o
; o
= o
->next_pool
)
927 for (e
= o
->entries
; e
!= o
->next_free
; e
++)
929 last
= idx
+ object_count
;
930 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
932 /* Generate the fan-out array. */
934 for (i
= 0; i
< 256; i
++) {
935 struct object_entry
**next
= c
;;
936 while (next
< last
) {
937 if ((*next
)->sha1
[0] != i
)
941 array
[i
] = htonl(next
- idx
);
945 f
= sha1create("%s", idx_name
);
946 sha1write(f
, array
, 256 * sizeof(int));
947 for (c
= idx
; c
!= last
; c
++) {
948 unsigned int offset
= htonl((*c
)->offset
);
949 sha1write(f
, &offset
, 4);
950 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
952 sha1write(f
, pack_sha1
, sizeof(pack_sha1
));
953 sha1close(f
, NULL
, 1);
957 static void dump_branches()
959 static const char *msg
= "fast-import";
962 struct ref_lock
*lock
;
964 for (i
= 0; i
< branch_table_sz
; i
++) {
965 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
966 lock
= lock_any_ref_for_update(b
->name
, NULL
, 0);
967 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
968 die("Can't write %s", b
->name
);
973 static void read_next_command()
975 read_line(&command_buf
, stdin
, '\n');
978 static void cmd_mark()
980 if (!strncmp("mark :", command_buf
.buf
, 6)) {
981 next_mark
= strtoul(command_buf
.buf
+ 6, NULL
, 10);
988 static void* cmd_data (size_t *size
)
994 if (strncmp("data ", command_buf
.buf
, 5))
995 die("Expected 'data n' command, found: %s", command_buf
.buf
);
997 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
998 buffer
= xmalloc(length
);
1000 while (n
< length
) {
1001 size_t s
= fread((char*)buffer
+ n
, 1, length
- n
, stdin
);
1002 if (!s
&& feof(stdin
))
1003 die("EOF in data (%lu bytes remaining)", length
- n
);
1007 if (fgetc(stdin
) != '\n')
1008 die("An lf did not trail the binary data as expected.");
1014 static void cmd_new_blob()
1019 read_next_command();
1023 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1027 static void unload_one_branch()
1029 while (cur_active_branches
>= max_active_branches
) {
1030 unsigned long min_commit
= ULONG_MAX
;
1031 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1033 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1034 if (e
->last_commit
< min_commit
) {
1036 min_commit
= e
->last_commit
;
1042 e
= p
->active_next_branch
;
1043 p
->active_next_branch
= e
->active_next_branch
;
1045 e
= active_branches
;
1046 active_branches
= e
->active_next_branch
;
1048 e
->active_next_branch
= NULL
;
1049 if (e
->branch_tree
.tree
) {
1050 release_tree_content_recursive(e
->branch_tree
.tree
);
1051 e
->branch_tree
.tree
= NULL
;
1053 cur_active_branches
--;
1057 static void load_branch(struct branch
*b
)
1059 load_tree(&b
->branch_tree
);
1060 b
->active_next_branch
= active_branches
;
1061 active_branches
= b
;
1062 cur_active_branches
++;
1063 branch_load_count
++;
1066 static void file_change_m(struct branch
*b
)
1068 const char *p
= command_buf
.buf
+ 2;
1071 struct object_entry
*oe
;
1072 unsigned char sha1
[20];
1076 p
= get_mode(p
, &mode
);
1078 die("Corrupt mode: %s", command_buf
.buf
);
1080 case S_IFREG
| 0644:
1081 case S_IFREG
| 0755:
1088 die("Corrupt mode: %s", command_buf
.buf
);
1093 oe
= find_mark(strtoul(p
+ 1, &x
, 10));
1096 if (get_sha1_hex(p
, sha1
))
1097 die("Invalid SHA1: %s", command_buf
.buf
);
1098 oe
= find_object(sha1
);
1102 die("Missing space after SHA1: %s", command_buf
.buf
);
1104 p_uq
= unquote_c_style(p
, &endp
);
1107 die("Garbage after path in: %s", command_buf
.buf
);
1112 if (oe
->type
!= OBJ_BLOB
)
1113 die("Not a blob (actually a %s): %s",
1114 command_buf
.buf
, type_names
[oe
->type
]);
1116 if (sha1_object_info(sha1
, type
, NULL
))
1117 die("Blob not found: %s", command_buf
.buf
);
1118 if (strcmp(blob_type
, type
))
1119 die("Not a blob (actually a %s): %s",
1120 command_buf
.buf
, type
);
1123 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1129 static void file_change_d(struct branch
*b
)
1131 const char *p
= command_buf
.buf
+ 2;
1135 p_uq
= unquote_c_style(p
, &endp
);
1138 die("Garbage after path in: %s", command_buf
.buf
);
1141 tree_content_remove(&b
->branch_tree
, p
);
1146 static void cmd_new_commit()
1154 char *author
= NULL
;
1155 char *committer
= NULL
;
1158 /* Obtain the branch name from the rest of our command */
1159 sp
= strchr(command_buf
.buf
, ' ') + 1;
1160 str_uq
= unquote_c_style(sp
, &endp
);
1163 die("Garbage after ref in: %s", command_buf
.buf
);
1166 b
= lookup_branch(sp
);
1168 die("Branch not declared: %s", sp
);
1172 read_next_command();
1174 if (!strncmp("author ", command_buf
.buf
, 7)) {
1175 author
= strdup(command_buf
.buf
);
1176 read_next_command();
1178 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1179 committer
= strdup(command_buf
.buf
);
1180 read_next_command();
1183 die("Expected committer but didn't get one");
1184 msg
= cmd_data(&msglen
);
1186 /* ensure the branch is active/loaded */
1187 if (!b
->branch_tree
.tree
) {
1188 unload_one_branch();
1194 read_next_command();
1195 if (1 == command_buf
.len
)
1197 else if (!strncmp("M ", command_buf
.buf
, 2))
1199 else if (!strncmp("D ", command_buf
.buf
, 2))
1202 die("Unsupported file_change: %s", command_buf
.buf
);
1205 /* build the tree and the commit */
1206 store_tree(&b
->branch_tree
);
1207 body
= xmalloc(97 + msglen
1209 ? strlen(author
) + strlen(committer
)
1210 : 2 * strlen(committer
)));
1212 sp
+= sprintf(sp
, "tree %s\n", sha1_to_hex(b
->branch_tree
.sha1
));
1213 if (memcmp(b
->sha1
, null_sha1
, 20))
1214 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1216 sp
+= sprintf(sp
, "%s\n", author
);
1218 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1219 sp
+= sprintf(sp
, "%s\n\n", committer
);
1220 memcpy(sp
, msg
, msglen
);
1227 store_object(OBJ_COMMIT
, body
, sp
- body
, NULL
, b
->sha1
, next_mark
);
1229 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1232 static void cmd_new_branch()
1239 /* Obtain the new branch name from the rest of our command */
1240 sp
= strchr(command_buf
.buf
, ' ') + 1;
1241 str_uq
= unquote_c_style(sp
, &endp
);
1244 die("Garbage after ref in: %s", command_buf
.buf
);
1250 read_next_command();
1253 if (!strncmp("from ", command_buf
.buf
, 5)) {
1257 from
= strchr(command_buf
.buf
, ' ') + 1;
1258 str_uq
= unquote_c_style(from
, &endp
);
1261 die("Garbage after string in: %s", command_buf
.buf
);
1265 s
= lookup_branch(from
);
1267 die("Can't create a branch from itself: %s", b
->name
);
1269 memcpy(b
->sha1
, s
->sha1
, 20);
1270 memcpy(b
->branch_tree
.sha1
, s
->branch_tree
.sha1
, 20);
1271 } else if (*from
== ':') {
1272 unsigned long idnum
= strtoul(from
+ 1, NULL
, 10);
1273 struct object_entry
*oe
= find_mark(idnum
);
1274 if (oe
->type
!= OBJ_COMMIT
)
1275 die("Mark :%lu not a commit", idnum
);
1276 memcpy(b
->sha1
, oe
->sha1
, 20);
1277 memcpy(b
->branch_tree
.sha1
, null_sha1
, 20);
1278 } else if (!get_sha1(from
, b
->sha1
)) {
1279 if (!memcmp(b
->sha1
, null_sha1
, 20))
1280 memcpy(b
->branch_tree
.sha1
, null_sha1
, 20);
1285 buf
= read_object_with_reference(b
->sha1
,
1286 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1287 if (!buf
|| size
< 46)
1288 die("Not a valid commit: %s", from
);
1289 if (memcmp("tree ", buf
, 5)
1290 || get_sha1_hex(buf
+ 5, b
->branch_tree
.sha1
))
1291 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1295 die("Invalid ref name or SHA1 expression: %s", from
);
1299 read_next_command();
1301 memcpy(b
->sha1
, null_sha1
, 20);
1302 memcpy(b
->branch_tree
.sha1
, null_sha1
, 20);
1305 if (command_buf
.eof
|| command_buf
.len
> 1)
1306 die("An lf did not terminate the branch command as expected.");
1309 static const char fast_import_usage
[] =
1310 "git-fast-import [--objects=n] [--depth=n] [--active-branches=n] temp.pack";
1312 int main(int argc
, const char **argv
)
1314 const char *base_name
;
1316 unsigned long est_obj_cnt
= 1000;
1322 git_config(git_default_config
);
1324 for (i
= 1; i
< argc
; i
++) {
1325 const char *a
= argv
[i
];
1327 if (*a
!= '-' || !strcmp(a
, "--"))
1329 else if (!strncmp(a
, "--objects=", 10))
1330 est_obj_cnt
= strtoul(a
+ 10, NULL
, 0);
1331 else if (!strncmp(a
, "--depth=", 8))
1332 max_depth
= strtoul(a
+ 8, NULL
, 0);
1333 else if (!strncmp(a
, "--active-branches=", 18))
1334 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1336 die("unknown option %s", a
);
1339 usage(fast_import_usage
);
1340 base_name
= argv
[i
];
1342 pack_name
= xmalloc(strlen(base_name
) + 6);
1343 sprintf(pack_name
, "%s.pack", base_name
);
1344 idx_name
= xmalloc(strlen(base_name
) + 5);
1345 sprintf(idx_name
, "%s.idx", base_name
);
1347 pack_fd
= open(pack_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1349 die("Can't create %s: %s", pack_name
, strerror(errno
));
1352 alloc_objects(est_obj_cnt
);
1353 strbuf_init(&command_buf
);
1355 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1356 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1357 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1358 marks
= pool_calloc(1, sizeof(struct mark_set
));
1361 read_next_command();
1362 if (command_buf
.eof
)
1364 else if (!strcmp("blob", command_buf
.buf
))
1366 else if (!strncmp("branch ", command_buf
.buf
, 7))
1368 else if (!strncmp("commit ", command_buf
.buf
, 7))
1371 die("Unsupported command: %s", command_buf
.buf
);
1374 fixup_header_footer();
1376 write_index(idx_name
);
1379 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1380 fprintf(stderr
, "---------------------------------------------------\n");
1381 fprintf(stderr
, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count
, alloc_count
- est_obj_cnt
);
1382 fprintf(stderr
, "Total objects: %10lu (%10lu duplicates)\n", object_count
, duplicate_count
);
1383 fprintf(stderr
, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
]);
1384 fprintf(stderr
, " trees : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
]);
1385 fprintf(stderr
, " commits: %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
]);
1386 fprintf(stderr
, " tags : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
]);
1387 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
1388 fprintf(stderr
, " marks: %10u (%10lu unique )\n", (1 << marks
->shift
) * 1024, marks_set_count
);
1389 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
1390 fprintf(stderr
, "Memory total: %10lu KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
1391 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
1392 fprintf(stderr
, " objects: %10lu KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
1393 fprintf(stderr
, "---------------------------------------------------\n");
1395 stat(pack_name
, &sb
);
1396 fprintf(stderr
, "Pack size: %10lu KiB\n", (unsigned long)(sb
.st_size
/1024));
1397 stat(idx_name
, &sb
);
1398 fprintf(stderr
, "Index size: %10lu KiB\n", (unsigned long)(sb
.st_size
/1024));
1400 fprintf(stderr
, "\n");