]>
git.ipfire.org Git - thirdparty/git.git/blob - fast-import.c
2 Format of STDIN stream:
12 new_blob ::= 'blob' blob_data;
14 new_commit ::= 'comt' ref_name author_committer_msg
18 new_branch ::= 'brch' dst_ref_name src_ref_name;
19 dst_ref_name ::= ref_name;
20 src_ref_name ::= ref_name | sha1_exp;
22 new_tag ::= 'tagg' ref_name tag_name tagger_msg;
24 file_change ::= 'M' path_name hexsha1
28 author_committer_msg ::= len32
29 'author' sp name '<' email '>' ts tz lf
30 'committer' sp name '<' email '>' ts tz lf
35 'tagger' sp name '<' email '>' ts tz lf
39 blob_data ::= len32 binary_data; # max len is 2^32-1
40 path_name ::= len32 path; # max len is PATH_MAX-1
41 ref_name ::= len32 ref; # max len is PATH_MAX-1
42 tag_name ::= len32 tag; # max len is PATH_MAX-1
43 sha1_exp ::= len32 sha1exp; # max len is PATH_MAX-1
45 len32 ::= # unsigned 32 bit value, native format;
46 binary_data ::= # file content, not interpreted;
47 sp ::= # ASCII space character;
48 lf ::= # ASCII newline (LF) character;
49 path ::= # GIT style file path, e.g. "a/b/c";
50 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
51 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
52 sha1exp ::= # Any valid GIT SHA1 expression;
53 hexsha1 ::= # SHA1 in hexadecimal format;
54 name ::= # valid GIT author/committer name;
55 email ::= # valid GIT author/committer email;
56 ts ::= # time since the epoch in seconds, ascii decimal;
57 tz ::= # GIT style timezone;
68 #include "csum-file.h"
72 struct object_entry
*next
;
74 unsigned char sha1
[20];
77 struct object_entry_pool
79 struct object_entry_pool
*next_pool
;
80 struct object_entry
*next_free
;
81 struct object_entry
*end
;
82 struct object_entry entries
[FLEX_ARRAY
]; /* more */
90 unsigned char sha1
[20];
95 struct mem_pool
*next_pool
;
98 char space
[FLEX_ARRAY
]; /* more */
103 struct atom_str
*next_atom
;
105 char str_dat
[FLEX_ARRAY
]; /* more */
111 struct tree_content
*tree
;
112 struct atom_str
* name
;
114 unsigned char sha1
[20];
119 unsigned int entry_capacity
; /* must match avail_tree_content */
120 unsigned int entry_count
;
121 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
124 struct avail_tree_content
126 unsigned int entry_capacity
; /* must match tree_content */
127 struct avail_tree_content
*next_avail
;
132 struct branch
*table_next_branch
;
133 struct branch
*active_next_branch
;
135 unsigned long last_commit
;
136 struct tree_entry branch_tree
;
137 unsigned char sha1
[20];
141 /* Stats and misc. counters */
142 static int max_depth
= 10;
143 static unsigned long alloc_count
;
144 static unsigned long branch_count
;
145 static unsigned long object_count
;
146 static unsigned long duplicate_count
;
147 static unsigned long object_count_by_type
[9];
148 static unsigned long duplicate_count_by_type
[9];
151 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
152 static size_t total_allocd
;
153 static struct mem_pool
*mem_pool
;
155 /* atom management */
156 static unsigned int atom_table_sz
= 4451;
157 static unsigned int atom_cnt
;
158 static struct atom_str
**atom_table
;
160 /* The .pack file being generated */
162 static unsigned long pack_offset
;
163 static unsigned char pack_sha1
[20];
165 /* Table of objects we've written. */
166 static unsigned int object_entry_alloc
= 1000;
167 static struct object_entry_pool
*blocks
;
168 static struct object_entry
*object_table
[1 << 16];
171 static struct last_object last_blob
;
173 /* Tree management */
174 static unsigned int tree_entry_alloc
= 1000;
175 static void *avail_tree_entry
;
176 static unsigned int avail_tree_table_sz
= 100;
177 static struct avail_tree_content
**avail_tree_table
;
180 static unsigned int max_active_branches
= 5;
181 static unsigned int cur_active_branches
;
182 static unsigned int branch_table_sz
= 1039;
183 static struct branch
**branch_table
;
184 static struct branch
*active_branches
;
187 static void alloc_objects(int cnt
)
189 struct object_entry_pool
*b
;
191 b
= xmalloc(sizeof(struct object_entry_pool
)
192 + cnt
* sizeof(struct object_entry
));
193 b
->next_pool
= blocks
;
194 b
->next_free
= b
->entries
;
195 b
->end
= b
->entries
+ cnt
;
200 static struct object_entry
* new_object(unsigned char *sha1
)
202 struct object_entry
*e
;
204 if (blocks
->next_free
== blocks
->end
)
205 alloc_objects(object_entry_alloc
);
207 e
= blocks
->next_free
++;
208 memcpy(e
->sha1
, sha1
, sizeof(e
->sha1
));
212 static struct object_entry
* find_object(unsigned char *sha1
)
214 unsigned int h
= sha1
[0] << 8 | sha1
[1];
215 struct object_entry
*e
;
216 for (e
= object_table
[h
]; e
; e
= e
->next
)
217 if (!memcmp(sha1
, e
->sha1
, sizeof(e
->sha1
)))
222 static struct object_entry
* insert_object(unsigned char *sha1
)
224 unsigned int h
= sha1
[0] << 8 | sha1
[1];
225 struct object_entry
*e
= object_table
[h
];
226 struct object_entry
*p
= NULL
;
229 if (!memcmp(sha1
, e
->sha1
, sizeof(e
->sha1
)))
235 e
= new_object(sha1
);
245 static unsigned int hc_str(const char *s
, size_t len
)
253 static void* pool_alloc(size_t len
)
258 for (p
= mem_pool
; p
; p
= p
->next_pool
)
259 if ((p
->end
- p
->next_free
>= len
))
263 if (len
>= (mem_pool_alloc
/2)) {
267 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
268 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
269 p
->next_pool
= mem_pool
;
270 p
->next_free
= p
->space
;
271 p
->end
= p
->next_free
+ mem_pool_alloc
;
280 static void* pool_calloc(size_t count
, size_t size
)
282 size_t len
= count
* size
;
283 void *r
= pool_alloc(len
);
288 static char* pool_strdup(const char *s
)
290 char *r
= pool_alloc(strlen(s
) + 1);
295 static struct atom_str
* to_atom(const char *s
, size_t len
)
297 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
300 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
301 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
304 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
306 strncpy(c
->str_dat
, s
, len
);
308 c
->next_atom
= atom_table
[hc
];
314 static struct branch
* lookup_branch(const char *name
)
316 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
319 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
320 if (!strcmp(name
, b
->name
))
325 static struct branch
* new_branch(const char *name
)
327 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
328 struct branch
* b
= lookup_branch(name
);
331 die("Invalid attempt to create duplicate branch: %s", name
);
333 b
= pool_calloc(1, sizeof(struct branch
));
334 b
->name
= pool_strdup(name
);
335 b
->table_next_branch
= branch_table
[hc
];
336 branch_table
[hc
] = b
;
341 static unsigned int hc_entries(unsigned int cnt
)
343 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
344 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
347 static struct tree_content
* new_tree_content(unsigned int cnt
)
349 struct avail_tree_content
*f
, *l
= NULL
;
350 struct tree_content
*t
;
351 unsigned int hc
= hc_entries(cnt
);
353 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
354 if (f
->entry_capacity
>= cnt
)
359 l
->next_avail
= f
->next_avail
;
361 avail_tree_table
[hc
] = f
->next_avail
;
363 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
364 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
365 f
->entry_capacity
= cnt
;
368 t
= (struct tree_content
*)f
;
373 static void release_tree_entry(struct tree_entry
*e
);
374 static void release_tree_content(struct tree_content
*t
)
376 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
377 unsigned int hc
= hc_entries(f
->entry_capacity
);
379 for (i
= 0; i
< t
->entry_count
; i
++)
380 release_tree_entry(t
->entries
[i
]);
381 f
->next_avail
= avail_tree_table
[hc
];
382 avail_tree_table
[hc
] = f
;
385 static struct tree_content
* grow_tree_content(
386 struct tree_content
*t
,
389 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
390 r
->entry_count
= t
->entry_count
;
391 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
392 release_tree_content(t
);
396 static struct tree_entry
* new_tree_entry()
398 struct tree_entry
*e
;
400 if (!avail_tree_entry
) {
401 unsigned int n
= tree_entry_alloc
;
402 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
404 *((void**)e
) = e
+ 1;
409 e
= avail_tree_entry
;
410 avail_tree_entry
= *((void**)e
);
414 static void release_tree_entry(struct tree_entry
*e
)
417 release_tree_content(e
->tree
);
418 *((void**)e
) = avail_tree_entry
;
419 avail_tree_entry
= e
;
422 static void yread(int fd
, void *buffer
, size_t length
)
425 while (ret
< length
) {
426 ssize_t size
= xread(fd
, (char *) buffer
+ ret
, length
- ret
);
428 die("Read from descriptor %i: end of stream", fd
);
430 die("Read from descriptor %i: %s", fd
, strerror(errno
));
435 static int optional_read(int fd
, void *buffer
, size_t length
)
438 while (ret
< length
) {
439 ssize_t size
= xread(fd
, (char *) buffer
+ ret
, length
- ret
);
443 die("Read from descriptor %i: end of stream", fd
);
445 die("Read from descriptor %i: %s", fd
, strerror(errno
));
451 static void ywrite(int fd
, void *buffer
, size_t length
)
454 while (ret
< length
) {
455 ssize_t size
= xwrite(fd
, (char *) buffer
+ ret
, length
- ret
);
457 die("Write to descriptor %i: end of file", fd
);
459 die("Write to descriptor %i: %s", fd
, strerror(errno
));
464 static const char* read_path()
466 static char sn
[PATH_MAX
];
471 die("Expected string command parameter, didn't find one");
472 if (slen
> (PATH_MAX
- 1))
473 die("Can't handle excessive string length %lu", slen
);
479 static unsigned long encode_header(
480 enum object_type type
,
487 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
488 die("bad type %d", type
);
490 c
= (type
<< 4) | (size
& 15);
502 static int store_object(
503 enum object_type type
,
505 unsigned long datlen
,
506 struct last_object
*last
,
507 unsigned char *sha1out
)
510 struct object_entry
*e
;
511 unsigned char hdr
[96];
512 unsigned char sha1
[20];
513 unsigned long hdrlen
, deltalen
;
517 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
519 SHA1_Update(&c
, hdr
, hdrlen
);
520 SHA1_Update(&c
, dat
, datlen
);
521 SHA1_Final(sha1
, &c
);
523 memcpy(sha1out
, sha1
, sizeof(sha1
));
525 e
= insert_object(sha1
);
528 duplicate_count_by_type
[type
]++;
531 e
->offset
= pack_offset
;
533 object_count_by_type
[type
]++;
535 if (last
&& last
->data
&& last
->depth
< max_depth
)
536 delta
= diff_delta(last
->data
, last
->len
,
542 memset(&s
, 0, sizeof(s
));
543 deflateInit(&s
, zlib_compression_level
);
548 s
.avail_in
= deltalen
;
549 hdrlen
= encode_header(OBJ_DELTA
, deltalen
, hdr
);
550 ywrite(pack_fd
, hdr
, hdrlen
);
551 ywrite(pack_fd
, last
->sha1
, sizeof(sha1
));
552 pack_offset
+= hdrlen
+ sizeof(sha1
);
558 hdrlen
= encode_header(type
, datlen
, hdr
);
559 ywrite(pack_fd
, hdr
, hdrlen
);
560 pack_offset
+= hdrlen
;
563 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
564 s
.next_out
= out
= xmalloc(s
.avail_out
);
565 while (deflate(&s
, Z_FINISH
) == Z_OK
)
569 ywrite(pack_fd
, out
, s
.total_out
);
570 pack_offset
+= s
.total_out
;
580 memcpy(last
->sha1
, sha1
, sizeof(sha1
));
585 static const char *get_mode(const char *str
, unsigned int *modep
)
588 unsigned int mode
= 0;
590 while ((c
= *str
++) != ' ') {
591 if (c
< '0' || c
> '7')
593 mode
= (mode
<< 3) + (c
- '0');
599 static void load_tree(struct tree_entry
*root
)
601 struct object_entry
*myoe
;
602 struct tree_content
*t
;
608 root
->tree
= t
= new_tree_content(8);
609 if (!memcmp(root
->sha1
, null_sha1
, 20))
612 myoe
= find_object(root
->sha1
);
616 buf
= read_sha1_file(root
->sha1
, type
, &size
);
617 if (!buf
|| strcmp(type
, tree_type
))
618 die("Can't load existing tree %s", sha1_to_hex(root
->sha1
));
622 while (c
!= (buf
+ size
)) {
623 struct tree_entry
*e
= new_tree_entry();
625 if (t
->entry_count
== t
->entry_capacity
)
626 root
->tree
= t
= grow_tree_content(t
, 8);
627 t
->entries
[t
->entry_count
++] = e
;
630 c
= get_mode(c
, &e
->mode
);
632 die("Corrupt mode in %s", sha1_to_hex(root
->sha1
));
633 e
->name
= to_atom(c
, strlen(c
));
634 c
+= e
->name
->str_len
+ 1;
635 memcpy(e
->sha1
, c
, sizeof(e
->sha1
));
641 static int tecmp (const void *_a
, const void *_b
)
643 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
644 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
645 return base_name_compare(
646 a
->name
->str_dat
, a
->name
->str_len
, a
->mode
,
647 b
->name
->str_dat
, b
->name
->str_len
, b
->mode
);
650 static void store_tree(struct tree_entry
*root
)
652 struct tree_content
*t
= root
->tree
;
657 if (memcmp(root
->sha1
, null_sha1
, 20))
661 for (i
= 0; i
< t
->entry_count
; i
++) {
662 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
663 if (t
->entries
[i
]->tree
)
664 store_tree(t
->entries
[i
]);
667 qsort(t
->entries
, t
->entry_count
, sizeof(t
->entries
[0]), tecmp
);
668 buf
= c
= xmalloc(maxlen
);
669 for (i
= 0; i
< t
->entry_count
; i
++) {
670 struct tree_entry
*e
= t
->entries
[i
];
671 c
+= sprintf(c
, "%o", e
->mode
);
673 strcpy(c
, e
->name
->str_dat
);
674 c
+= e
->name
->str_len
+ 1;
675 memcpy(c
, e
->sha1
, 20);
678 store_object(OBJ_TREE
, buf
, c
- buf
, NULL
, root
->sha1
);
682 static int tree_content_set(
683 struct tree_entry
*root
,
685 const unsigned char *sha1
,
686 const unsigned int mode
)
688 struct tree_content
*t
= root
->tree
;
691 struct tree_entry
*e
;
693 slash1
= strchr(p
, '/');
699 for (i
= 0; i
< t
->entry_count
; i
++) {
701 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
703 if (e
->mode
== mode
&& !memcmp(e
->sha1
, sha1
, 20))
706 memcpy(e
->sha1
, sha1
, 20);
708 release_tree_content(e
->tree
);
711 memcpy(root
->sha1
, null_sha1
, 20);
714 if (!S_ISDIR(e
->mode
)) {
715 e
->tree
= new_tree_content(8);
720 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
721 memcpy(root
->sha1
, null_sha1
, 20);
728 if (t
->entry_count
== t
->entry_capacity
)
729 root
->tree
= t
= grow_tree_content(t
, 8);
730 e
= new_tree_entry();
731 e
->name
= to_atom(p
, n
);
732 t
->entries
[t
->entry_count
++] = e
;
734 e
->tree
= new_tree_content(8);
736 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
740 memcpy(e
->sha1
, sha1
, 20);
742 memcpy(root
->sha1
, null_sha1
, 20);
746 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
748 struct tree_content
*t
= root
->tree
;
751 struct tree_entry
*e
;
753 slash1
= strchr(p
, '/');
759 for (i
= 0; i
< t
->entry_count
; i
++) {
761 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
762 if (!slash1
|| !S_ISDIR(e
->mode
))
766 if (tree_content_remove(e
, slash1
+ 1)) {
767 if (!e
->tree
->entry_count
)
769 memcpy(root
->sha1
, null_sha1
, 20);
778 for (i
++; i
< t
->entry_count
; i
++)
779 t
->entries
[i
-1] = t
->entries
[i
];
781 release_tree_entry(e
);
782 memcpy(root
->sha1
, null_sha1
, 20);
786 static void init_pack_header()
788 const char* magic
= "PACK";
789 unsigned long version
= 3;
790 unsigned long zero
= 0;
792 version
= htonl(version
);
793 ywrite(pack_fd
, (char*)magic
, 4);
794 ywrite(pack_fd
, &version
, 4);
795 ywrite(pack_fd
, &zero
, 4);
799 static void fixup_header_footer()
807 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
808 die("Failed seeking to start: %s", strerror(errno
));
811 yread(pack_fd
, hdr
, 8);
812 SHA1_Update(&c
, hdr
, 8);
814 cnt
= htonl(object_count
);
815 SHA1_Update(&c
, &cnt
, 4);
816 ywrite(pack_fd
, &cnt
, 4);
818 buf
= xmalloc(128 * 1024);
820 n
= xread(pack_fd
, buf
, 128 * 1024);
823 SHA1_Update(&c
, buf
, n
);
827 SHA1_Final(pack_sha1
, &c
);
828 ywrite(pack_fd
, pack_sha1
, sizeof(pack_sha1
));
831 static int oecmp (const void *_a
, const void *_b
)
833 struct object_entry
*a
= *((struct object_entry
**)_a
);
834 struct object_entry
*b
= *((struct object_entry
**)_b
);
835 return memcmp(a
->sha1
, b
->sha1
, sizeof(a
->sha1
));
838 static void write_index(const char *idx_name
)
841 struct object_entry
**idx
, **c
, **last
;
842 struct object_entry
*e
;
843 struct object_entry_pool
*o
;
844 unsigned int array
[256];
847 /* Build the sorted table of object IDs. */
848 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
850 for (o
= blocks
; o
; o
= o
->next_pool
)
851 for (e
= o
->entries
; e
!= o
->next_free
; e
++)
853 last
= idx
+ object_count
;
854 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
856 /* Generate the fan-out array. */
858 for (i
= 0; i
< 256; i
++) {
859 struct object_entry
**next
= c
;;
860 while (next
< last
) {
861 if ((*next
)->sha1
[0] != i
)
865 array
[i
] = htonl(next
- idx
);
869 f
= sha1create("%s", idx_name
);
870 sha1write(f
, array
, 256 * sizeof(int));
871 for (c
= idx
; c
!= last
; c
++) {
872 unsigned int offset
= htonl((*c
)->offset
);
873 sha1write(f
, &offset
, 4);
874 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
876 sha1write(f
, pack_sha1
, sizeof(pack_sha1
));
877 sha1close(f
, NULL
, 1);
881 static void dump_branches()
883 static const char *msg
= "fast-import";
886 struct ref_lock
*lock
;
888 for (i
= 0; i
< branch_table_sz
; i
++) {
889 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
890 lock
= lock_any_ref_for_update(b
->name
, NULL
, 0);
891 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
892 die("Can't write %s", b
->name
);
897 static void cmd_new_blob()
899 unsigned long datlen
;
900 unsigned char sha1
[20];
903 yread(0, &datlen
, 4);
904 dat
= xmalloc(datlen
);
905 yread(0, dat
, datlen
);
906 if (store_object(OBJ_BLOB
, dat
, datlen
, &last_blob
, sha1
))
910 static void unload_one_branch()
912 while (cur_active_branches
>= max_active_branches
) {
913 unsigned long min_commit
= ULONG_MAX
;
914 struct branch
*e
, *l
= NULL
, *p
= NULL
;
916 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
917 if (e
->last_commit
< min_commit
) {
919 min_commit
= e
->last_commit
;
925 e
= p
->active_next_branch
;
926 p
->active_next_branch
= e
->active_next_branch
;
929 active_branches
= e
->active_next_branch
;
931 e
->active_next_branch
= NULL
;
932 if (e
->branch_tree
.tree
) {
933 release_tree_content(e
->branch_tree
.tree
);
934 e
->branch_tree
.tree
= NULL
;
936 cur_active_branches
--;
940 static void load_branch(struct branch
*b
)
942 load_tree(&b
->branch_tree
);
943 b
->active_next_branch
= active_branches
;
945 cur_active_branches
++;
948 static void file_change_m(struct branch
*b
)
950 const char *path
= read_path();
952 unsigned char sha1
[20];
954 yread(0, hexsha1
, 40);
957 if (get_sha1_hex(hexsha1
, sha1
))
958 die("Invalid sha1 %s for %s", hexsha1
, path
);
960 tree_content_set(&b
->branch_tree
, path
, sha1
, 0100644);
963 static void file_change_d(struct branch
*b
)
965 tree_content_remove(&b
->branch_tree
, read_path());
968 static void cmd_new_commit()
970 static const unsigned int max_hdr_len
= 94;
971 const char *name
= read_path();
972 struct branch
*b
= lookup_branch(name
);
973 unsigned int acmsglen
;
977 die("Branch not declared: %s", name
);
978 if (!b
->branch_tree
.tree
) {
983 /* author_committer_msg */
984 yread(0, &acmsglen
, 4);
985 body
= xmalloc(acmsglen
+ max_hdr_len
);
986 c
= body
+ max_hdr_len
;
987 yread(0, c
, acmsglen
);
1000 die("Unsupported file_change: %c", cmd
);
1003 if (memcmp(b
->sha1
, null_sha1
, 20)) {
1004 sprintf(c
- 48, "parent %s", sha1_to_hex(b
->sha1
));
1008 store_tree(&b
->branch_tree
);
1009 sprintf(c
- 46, "tree %s", sha1_to_hex(b
->branch_tree
.sha1
));
1013 store_object(OBJ_COMMIT
,
1014 c
, (body
+ max_hdr_len
+ acmsglen
) - c
,
1017 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1020 static void cmd_new_branch()
1022 struct branch
*b
= new_branch(read_path());
1023 const char *base
= read_path();
1024 struct branch
*s
= lookup_branch(base
);
1026 if (!strcmp(b
->name
, base
))
1027 die("Can't create a branch from itself: %s", base
);
1029 memcpy(b
->sha1
, s
->sha1
, 20);
1030 memcpy(b
->branch_tree
.sha1
, s
->branch_tree
.sha1
, 20);
1032 else if (!get_sha1(base
, b
->sha1
)) {
1033 if (!memcmp(b
->sha1
, null_sha1
, 20))
1034 memcpy(b
->branch_tree
.sha1
, null_sha1
, 20);
1039 buf
= read_object_with_reference(b
->sha1
,
1040 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1041 if (!buf
|| size
< 46)
1042 die("Not a valid commit: %s", base
);
1043 if (memcmp("tree ", buf
, 5)
1044 || get_sha1_hex(buf
+ 5, b
->branch_tree
.sha1
))
1045 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1049 die("Not a SHA1 or branch: %s", base
);
1052 int main(int argc
, const char **argv
)
1054 const char *base_name
= argv
[1];
1055 int est_obj_cnt
= atoi(argv
[2]);
1061 git_config(git_default_config
);
1063 pack_name
= xmalloc(strlen(base_name
) + 6);
1064 sprintf(pack_name
, "%s.pack", base_name
);
1065 idx_name
= xmalloc(strlen(base_name
) + 5);
1066 sprintf(idx_name
, "%s.idx", base_name
);
1068 pack_fd
= open(pack_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
1070 die("Can't create %s: %s", pack_name
, strerror(errno
));
1072 alloc_objects(est_obj_cnt
);
1074 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1075 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1076 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1081 if (optional_read(0, &cmd
, 4))
1084 switch (ntohl(cmd
)) {
1085 case 'blob': cmd_new_blob(); break;
1086 case 'comt': cmd_new_commit(); break;
1087 case 'brch': cmd_new_branch(); break;
1089 die("Invalid command %lu", cmd
);
1092 fixup_header_footer();
1094 write_index(idx_name
);
1097 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1098 fprintf(stderr
, "---------------------------------------------------\n");
1099 fprintf(stderr
, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count
, alloc_count
- est_obj_cnt
);
1100 fprintf(stderr
, "Total objects: %10lu (%10lu duplicates)\n", object_count
, duplicate_count
);
1101 fprintf(stderr
, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
]);
1102 fprintf(stderr
, " trees : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
]);
1103 fprintf(stderr
, " commits: %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
]);
1104 fprintf(stderr
, " tags : %10lu (%10lu duplicates)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
]);
1105 fprintf(stderr
, "Total branches: %10lu\n", branch_count
);
1106 fprintf(stderr
, "Total atoms: %10u\n", atom_cnt
);
1107 fprintf(stderr
, "Memory pools: %10lu MiB\n", total_allocd
/(1024*1024));
1108 fprintf(stderr
, "---------------------------------------------------\n");
1110 stat(pack_name
, &sb
);
1111 fprintf(stderr
, "Pack size: %10lu KiB\n", (unsigned long)(sb
.st_size
/1024));
1112 stat(idx_name
, &sb
);
1113 fprintf(stderr
, "Index size: %10lu KiB\n", (unsigned long)(sb
.st_size
/1024));
1115 fprintf(stderr
, "\n");