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)?
23 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
28 file_change ::= file_del | file_obm | file_inm;
29 file_del ::= 'D' sp path_str lf;
30 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
31 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
34 new_tag ::= 'tag' sp tag_str lf
35 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
36 'tagger' sp name '<' email '>' ts tz lf
40 reset_branch ::= 'reset' sp ref_str lf
41 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
44 checkpoint ::= 'checkpoint' lf
47 # note: the first idnum in a stream should be 1 and subsequent
48 # idnums should not have gaps between values as this will cause
49 # the stream parser to reserve space for the gapped values. An
50 # idnum can be updated in the future to a new object by issuing
51 # a new mark directive with the old idnum.
53 mark ::= 'mark' sp idnum lf;
54 data ::= (delimited_data | exact_data)
57 # note: delim may be any string but must not contain lf.
58 # data_line may contain any data but must not be exactly
60 delimited_data ::= 'data' sp '<<' delim lf
64 # note: declen indicates the length of binary_data in bytes.
65 # declen does not include the lf preceeding the binary data.
67 exact_data ::= 'data' sp declen lf
70 # note: quoted strings are C-style quoting supporting \c for
71 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
72 # is the signed byte value in octal. Note that the only
73 # characters which must actually be escaped to protect the
74 # stream formatting is: \, " and LF. Otherwise these values
77 ref_str ::= ref | '"' quoted(ref) '"' ;
78 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
79 tag_str ::= tag | '"' quoted(tag) '"' ;
80 path_str ::= path | '"' quoted(path) '"' ;
81 mode ::= '100644' | '644'
86 declen ::= # unsigned 32 bit value, ascii base10 notation;
87 bigint ::= # unsigned integer value, ascii base10 notation;
88 binary_data ::= # file content, not interpreted;
90 sp ::= # ASCII space character;
91 lf ::= # ASCII newline (LF) character;
93 # note: a colon (':') must precede the numerical value assigned to
94 # an idnum. This is to distinguish it from a ref or tag name as
95 # GIT does not permit ':' in ref or tag strings.
98 path ::= # GIT style file path, e.g. "a/b/c";
99 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
100 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
101 sha1exp ::= # Any valid GIT SHA1 expression;
102 hexsha1 ::= # SHA1 in hexadecimal format;
104 # note: name and email are UTF8 strings, however name must not
105 # contain '<' or lf and email must not contain any of the
106 # following: '<', '>', lf.
108 name ::= # valid GIT author/committer name;
109 email ::= # valid GIT author/committer email;
110 ts ::= # time since the epoch in seconds, ascii base10 notation;
111 tz ::= # GIT style timezone;
122 #include "csum-file.h"
126 #define PACK_ID_BITS 16
127 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
131 struct object_entry
*next
;
132 unsigned long offset
;
133 unsigned type
: TYPE_BITS
;
134 unsigned pack_id
: PACK_ID_BITS
;
135 unsigned char sha1
[20];
138 struct object_entry_pool
140 struct object_entry_pool
*next_pool
;
141 struct object_entry
*next_free
;
142 struct object_entry
*end
;
143 struct object_entry entries
[FLEX_ARRAY
]; /* more */
149 struct object_entry
*marked
[1024];
150 struct mark_set
*sets
[1024];
159 unsigned long offset
;
166 struct mem_pool
*next_pool
;
169 char space
[FLEX_ARRAY
]; /* more */
174 struct atom_str
*next_atom
;
175 unsigned int str_len
;
176 char str_dat
[FLEX_ARRAY
]; /* more */
182 struct tree_content
*tree
;
183 struct atom_str
* name
;
187 unsigned char sha1
[20];
193 unsigned int entry_capacity
; /* must match avail_tree_content */
194 unsigned int entry_count
;
195 unsigned int delta_depth
;
196 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
199 struct avail_tree_content
201 unsigned int entry_capacity
; /* must match tree_content */
202 struct avail_tree_content
*next_avail
;
207 struct branch
*table_next_branch
;
208 struct branch
*active_next_branch
;
210 struct tree_entry branch_tree
;
211 uintmax_t last_commit
;
212 unsigned int pack_id
;
213 unsigned char sha1
[20];
218 struct tag
*next_tag
;
220 unsigned int pack_id
;
221 unsigned char sha1
[20];
232 struct hash_list
*next
;
233 unsigned char sha1
[20];
236 /* Configured limits on output */
237 static unsigned long max_depth
= 10;
238 static unsigned long max_packsize
= (1LL << 32) - 1;
240 /* Stats and misc. counters */
241 static uintmax_t alloc_count
;
242 static uintmax_t marks_set_count
;
243 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
244 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
245 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
246 static unsigned long object_count
;
247 static unsigned long branch_count
;
248 static unsigned long branch_load_count
;
251 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
252 static size_t total_allocd
;
253 static struct mem_pool
*mem_pool
;
255 /* Atom management */
256 static unsigned int atom_table_sz
= 4451;
257 static unsigned int atom_cnt
;
258 static struct atom_str
**atom_table
;
260 /* The .pack file being generated */
261 static unsigned int pack_id
;
262 static struct packed_git
*pack_data
;
263 static struct packed_git
**all_packs
;
264 static unsigned long pack_size
;
266 /* Table of objects we've written. */
267 static unsigned int object_entry_alloc
= 5000;
268 static struct object_entry_pool
*blocks
;
269 static struct object_entry
*object_table
[1 << 16];
270 static struct mark_set
*marks
;
271 static const char* mark_file
;
274 static struct last_object last_blob
;
276 /* Tree management */
277 static unsigned int tree_entry_alloc
= 1000;
278 static void *avail_tree_entry
;
279 static unsigned int avail_tree_table_sz
= 100;
280 static struct avail_tree_content
**avail_tree_table
;
281 static struct dbuf old_tree
;
282 static struct dbuf new_tree
;
285 static unsigned long max_active_branches
= 5;
286 static unsigned long cur_active_branches
;
287 static unsigned long branch_table_sz
= 1039;
288 static struct branch
**branch_table
;
289 static struct branch
*active_branches
;
292 static struct tag
*first_tag
;
293 static struct tag
*last_tag
;
295 /* Input stream parsing */
296 static struct strbuf command_buf
;
297 static uintmax_t next_mark
;
298 static struct dbuf new_data
;
299 static FILE* branch_log
;
302 static void alloc_objects(unsigned int cnt
)
304 struct object_entry_pool
*b
;
306 b
= xmalloc(sizeof(struct object_entry_pool
)
307 + cnt
* sizeof(struct object_entry
));
308 b
->next_pool
= blocks
;
309 b
->next_free
= b
->entries
;
310 b
->end
= b
->entries
+ cnt
;
315 static struct object_entry
* new_object(unsigned char *sha1
)
317 struct object_entry
*e
;
319 if (blocks
->next_free
== blocks
->end
)
320 alloc_objects(object_entry_alloc
);
322 e
= blocks
->next_free
++;
323 hashcpy(e
->sha1
, sha1
);
327 static struct object_entry
* find_object(unsigned char *sha1
)
329 unsigned int h
= sha1
[0] << 8 | sha1
[1];
330 struct object_entry
*e
;
331 for (e
= object_table
[h
]; e
; e
= e
->next
)
332 if (!hashcmp(sha1
, e
->sha1
))
337 static struct object_entry
* insert_object(unsigned char *sha1
)
339 unsigned int h
= sha1
[0] << 8 | sha1
[1];
340 struct object_entry
*e
= object_table
[h
];
341 struct object_entry
*p
= NULL
;
344 if (!hashcmp(sha1
, e
->sha1
))
350 e
= new_object(sha1
);
360 static unsigned int hc_str(const char *s
, size_t len
)
368 static void* pool_alloc(size_t len
)
373 for (p
= mem_pool
; p
; p
= p
->next_pool
)
374 if ((p
->end
- p
->next_free
>= len
))
378 if (len
>= (mem_pool_alloc
/2)) {
382 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
383 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
384 p
->next_pool
= mem_pool
;
385 p
->next_free
= p
->space
;
386 p
->end
= p
->next_free
+ mem_pool_alloc
;
391 /* round out to a pointer alignment */
392 if (len
& (sizeof(void*) - 1))
393 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
398 static void* pool_calloc(size_t count
, size_t size
)
400 size_t len
= count
* size
;
401 void *r
= pool_alloc(len
);
406 static char* pool_strdup(const char *s
)
408 char *r
= pool_alloc(strlen(s
) + 1);
413 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
416 if (b
->capacity
>= maxlen
)
420 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
421 b
->buffer
= xmalloc(b
->capacity
);
424 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
426 struct mark_set
*s
= marks
;
427 while ((idnum
>> s
->shift
) >= 1024) {
428 s
= pool_calloc(1, sizeof(struct mark_set
));
429 s
->shift
= marks
->shift
+ 10;
430 s
->data
.sets
[0] = marks
;
434 uintmax_t i
= idnum
>> s
->shift
;
435 idnum
-= i
<< s
->shift
;
436 if (!s
->data
.sets
[i
]) {
437 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
438 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
442 if (!s
->data
.marked
[idnum
])
444 s
->data
.marked
[idnum
] = oe
;
447 static struct object_entry
* find_mark(uintmax_t idnum
)
449 uintmax_t orig_idnum
= idnum
;
450 struct mark_set
*s
= marks
;
451 struct object_entry
*oe
= NULL
;
452 if ((idnum
>> s
->shift
) < 1024) {
453 while (s
&& s
->shift
) {
454 uintmax_t i
= idnum
>> s
->shift
;
455 idnum
-= i
<< s
->shift
;
459 oe
= s
->data
.marked
[idnum
];
462 die("mark :%ju not declared", orig_idnum
);
466 static struct atom_str
* to_atom(const char *s
, size_t len
)
468 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
471 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
472 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
475 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
477 strncpy(c
->str_dat
, s
, len
);
479 c
->next_atom
= atom_table
[hc
];
485 static struct branch
* lookup_branch(const char *name
)
487 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
490 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
491 if (!strcmp(name
, b
->name
))
496 static struct branch
* new_branch(const char *name
)
498 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
499 struct branch
* b
= lookup_branch(name
);
502 die("Invalid attempt to create duplicate branch: %s", name
);
503 if (check_ref_format(name
))
504 die("Branch name doesn't conform to GIT standards: %s", name
);
506 b
= pool_calloc(1, sizeof(struct branch
));
507 b
->name
= pool_strdup(name
);
508 b
->table_next_branch
= branch_table
[hc
];
509 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
510 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
511 b
->pack_id
= MAX_PACK_ID
;
512 branch_table
[hc
] = b
;
517 static unsigned int hc_entries(unsigned int cnt
)
519 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
520 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
523 static struct tree_content
* new_tree_content(unsigned int cnt
)
525 struct avail_tree_content
*f
, *l
= NULL
;
526 struct tree_content
*t
;
527 unsigned int hc
= hc_entries(cnt
);
529 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
530 if (f
->entry_capacity
>= cnt
)
535 l
->next_avail
= f
->next_avail
;
537 avail_tree_table
[hc
] = f
->next_avail
;
539 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
540 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
541 f
->entry_capacity
= cnt
;
544 t
= (struct tree_content
*)f
;
550 static void release_tree_entry(struct tree_entry
*e
);
551 static void release_tree_content(struct tree_content
*t
)
553 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
554 unsigned int hc
= hc_entries(f
->entry_capacity
);
555 f
->next_avail
= avail_tree_table
[hc
];
556 avail_tree_table
[hc
] = f
;
559 static void release_tree_content_recursive(struct tree_content
*t
)
562 for (i
= 0; i
< t
->entry_count
; i
++)
563 release_tree_entry(t
->entries
[i
]);
564 release_tree_content(t
);
567 static struct tree_content
* grow_tree_content(
568 struct tree_content
*t
,
571 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
572 r
->entry_count
= t
->entry_count
;
573 r
->delta_depth
= t
->delta_depth
;
574 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
575 release_tree_content(t
);
579 static struct tree_entry
* new_tree_entry(void)
581 struct tree_entry
*e
;
583 if (!avail_tree_entry
) {
584 unsigned int n
= tree_entry_alloc
;
585 total_allocd
+= n
* sizeof(struct tree_entry
);
586 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
588 *((void**)e
) = e
+ 1;
594 e
= avail_tree_entry
;
595 avail_tree_entry
= *((void**)e
);
599 static void release_tree_entry(struct tree_entry
*e
)
602 release_tree_content_recursive(e
->tree
);
603 *((void**)e
) = avail_tree_entry
;
604 avail_tree_entry
= e
;
607 static void start_packfile(void)
609 static char tmpfile
[PATH_MAX
];
610 struct packed_git
*p
;
611 struct pack_header hdr
;
614 snprintf(tmpfile
, sizeof(tmpfile
),
615 "%s/pack_XXXXXX", get_object_directory());
616 pack_fd
= mkstemp(tmpfile
);
618 die("Can't create %s: %s", tmpfile
, strerror(errno
));
619 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
620 strcpy(p
->pack_name
, tmpfile
);
621 p
->pack_fd
= pack_fd
;
623 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
624 hdr
.hdr_version
= htonl(2);
626 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
629 pack_size
= sizeof(hdr
);
632 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
633 all_packs
[pack_id
] = p
;
636 static void fixup_header_footer(void)
638 static const int buf_sz
= 128 * 1024;
639 int pack_fd
= pack_data
->pack_fd
;
641 struct pack_header hdr
;
644 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
645 die("Failed seeking to start: %s", strerror(errno
));
646 if (read_in_full(pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
647 die("Unable to reread header of %s", pack_data
->pack_name
);
648 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
649 die("Failed seeking to start: %s", strerror(errno
));
650 hdr
.hdr_entries
= htonl(object_count
);
651 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
654 SHA1_Update(&c
, &hdr
, sizeof(hdr
));
656 buf
= xmalloc(buf_sz
);
658 size_t n
= xread(pack_fd
, buf
, buf_sz
);
662 die("Failed to checksum %s", pack_data
->pack_name
);
663 SHA1_Update(&c
, buf
, n
);
667 SHA1_Final(pack_data
->sha1
, &c
);
668 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
672 static int oecmp (const void *a_
, const void *b_
)
674 struct object_entry
*a
= *((struct object_entry
**)a_
);
675 struct object_entry
*b
= *((struct object_entry
**)b_
);
676 return hashcmp(a
->sha1
, b
->sha1
);
679 static char* create_index(void)
681 static char tmpfile
[PATH_MAX
];
684 struct object_entry
**idx
, **c
, **last
, *e
;
685 struct object_entry_pool
*o
;
689 /* Build the sorted table of object IDs. */
690 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
692 for (o
= blocks
; o
; o
= o
->next_pool
)
693 for (e
= o
->next_free
; e
-- != o
->entries
;)
694 if (pack_id
== e
->pack_id
)
696 last
= idx
+ object_count
;
698 die("internal consistency error creating the index");
699 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
701 /* Generate the fan-out array. */
703 for (i
= 0; i
< 256; i
++) {
704 struct object_entry
**next
= c
;;
705 while (next
< last
) {
706 if ((*next
)->sha1
[0] != i
)
710 array
[i
] = htonl(next
- idx
);
714 snprintf(tmpfile
, sizeof(tmpfile
),
715 "%s/index_XXXXXX", get_object_directory());
716 idx_fd
= mkstemp(tmpfile
);
718 die("Can't create %s: %s", tmpfile
, strerror(errno
));
719 f
= sha1fd(idx_fd
, tmpfile
);
720 sha1write(f
, array
, 256 * sizeof(int));
722 for (c
= idx
; c
!= last
; c
++) {
723 uint32_t offset
= htonl((*c
)->offset
);
724 sha1write(f
, &offset
, 4);
725 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
726 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
728 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
729 sha1close(f
, NULL
, 1);
731 SHA1_Final(pack_data
->sha1
, &ctx
);
735 static char* keep_pack(char *curr_index_name
)
737 static char name
[PATH_MAX
];
738 static char *keep_msg
= "fast-import";
741 chmod(pack_data
->pack_name
, 0444);
742 chmod(curr_index_name
, 0444);
744 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
745 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
746 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
748 die("cannot create keep file");
749 write(keep_fd
, keep_msg
, strlen(keep_msg
));
752 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
753 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
754 if (move_temp_to_file(pack_data
->pack_name
, name
))
755 die("cannot store pack file");
757 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
758 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
759 if (move_temp_to_file(curr_index_name
, name
))
760 die("cannot store index file");
764 static void unkeep_all_packs(void)
766 static char name
[PATH_MAX
];
769 for (k
= 0; k
< pack_id
; k
++) {
770 struct packed_git
*p
= all_packs
[k
];
771 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
772 get_object_directory(), sha1_to_hex(p
->sha1
));
777 static void end_packfile(void)
779 struct packed_git
*old_p
= pack_data
, *new_p
;
787 fixup_header_footer();
788 idx_name
= keep_pack(create_index());
790 /* Register the packfile with core git's machinary. */
791 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
793 die("core git rejected index %s", idx_name
);
794 new_p
->windows
= old_p
->windows
;
795 all_packs
[pack_id
] = new_p
;
796 install_packed_git(new_p
);
798 /* Print the boundary */
799 fprintf(stdout
, "%s:", new_p
->pack_name
);
800 for (i
= 0; i
< branch_table_sz
; i
++) {
801 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
802 if (b
->pack_id
== pack_id
)
803 fprintf(stdout
, " %s", sha1_to_hex(b
->sha1
));
806 for (t
= first_tag
; t
; t
= t
->next_tag
) {
807 if (t
->pack_id
== pack_id
)
808 fprintf(stdout
, " %s", sha1_to_hex(t
->sha1
));
815 unlink(old_p
->pack_name
);
818 /* We can't carry a delta across packfiles. */
819 free(last_blob
.data
);
820 last_blob
.data
= NULL
;
822 last_blob
.offset
= 0;
826 static void checkpoint(void)
832 static size_t encode_header(
833 enum object_type type
,
840 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
841 die("bad type %d", type
);
843 c
= (type
<< 4) | (size
& 15);
855 static int store_object(
856 enum object_type type
,
859 struct last_object
*last
,
860 unsigned char *sha1out
,
864 struct object_entry
*e
;
865 unsigned char hdr
[96];
866 unsigned char sha1
[20];
867 unsigned long hdrlen
, deltalen
;
871 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
873 SHA1_Update(&c
, hdr
, hdrlen
);
874 SHA1_Update(&c
, dat
, datlen
);
875 SHA1_Final(sha1
, &c
);
877 hashcpy(sha1out
, sha1
);
879 e
= insert_object(sha1
);
881 insert_mark(mark
, e
);
883 duplicate_count_by_type
[type
]++;
887 if (last
&& last
->data
&& last
->depth
< max_depth
) {
888 delta
= diff_delta(last
->data
, last
->len
,
891 if (delta
&& deltalen
>= datlen
) {
898 memset(&s
, 0, sizeof(s
));
899 deflateInit(&s
, zlib_compression_level
);
902 s
.avail_in
= deltalen
;
907 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
908 s
.next_out
= out
= xmalloc(s
.avail_out
);
909 while (deflate(&s
, Z_FINISH
) == Z_OK
)
913 /* Determine if we should auto-checkpoint. */
914 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
915 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
917 /* This new object needs to *not* have the current pack_id. */
918 e
->pack_id
= pack_id
+ 1;
921 /* We cannot carry a delta into the new pack. */
926 memset(&s
, 0, sizeof(s
));
927 deflateInit(&s
, zlib_compression_level
);
930 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
931 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
932 while (deflate(&s
, Z_FINISH
) == Z_OK
)
939 e
->pack_id
= pack_id
;
940 e
->offset
= pack_size
;
942 object_count_by_type
[type
]++;
945 unsigned long ofs
= e
->offset
- last
->offset
;
946 unsigned pos
= sizeof(hdr
) - 1;
948 delta_count_by_type
[type
]++;
951 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
952 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
955 hdr
[pos
] = ofs
& 127;
957 hdr
[--pos
] = 128 | (--ofs
& 127);
958 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
959 pack_size
+= sizeof(hdr
) - pos
;
963 hdrlen
= encode_header(type
, datlen
, hdr
);
964 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
968 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
969 pack_size
+= s
.total_out
;
975 if (last
->data
&& !last
->no_free
)
978 last
->offset
= e
->offset
;
984 static void *gfi_unpack_entry(
985 struct object_entry
*oe
,
986 unsigned long *sizep
)
988 static char type
[20];
989 struct packed_git
*p
= all_packs
[oe
->pack_id
];
991 p
->pack_size
= pack_size
+ 20;
992 return unpack_entry(p
, oe
->offset
, type
, sizep
);
995 static const char *get_mode(const char *str
, unsigned int *modep
)
998 unsigned int mode
= 0;
1000 while ((c
= *str
++) != ' ') {
1001 if (c
< '0' || c
> '7')
1003 mode
= (mode
<< 3) + (c
- '0');
1009 static void load_tree(struct tree_entry
*root
)
1011 unsigned char* sha1
= root
->versions
[1].sha1
;
1012 struct object_entry
*myoe
;
1013 struct tree_content
*t
;
1018 root
->tree
= t
= new_tree_content(8);
1019 if (is_null_sha1(sha1
))
1022 myoe
= find_object(sha1
);
1024 if (myoe
->type
!= OBJ_TREE
)
1025 die("Not a tree: %s", sha1_to_hex(sha1
));
1027 buf
= gfi_unpack_entry(myoe
, &size
);
1030 buf
= read_sha1_file(sha1
, type
, &size
);
1031 if (!buf
|| strcmp(type
, tree_type
))
1032 die("Can't load tree %s", sha1_to_hex(sha1
));
1036 while (c
!= (buf
+ size
)) {
1037 struct tree_entry
*e
= new_tree_entry();
1039 if (t
->entry_count
== t
->entry_capacity
)
1040 root
->tree
= t
= grow_tree_content(t
, 8);
1041 t
->entries
[t
->entry_count
++] = e
;
1044 c
= get_mode(c
, &e
->versions
[1].mode
);
1046 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1047 e
->versions
[0].mode
= e
->versions
[1].mode
;
1048 e
->name
= to_atom(c
, strlen(c
));
1049 c
+= e
->name
->str_len
+ 1;
1050 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1051 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1057 static int tecmp0 (const void *_a
, const void *_b
)
1059 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1060 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1061 return base_name_compare(
1062 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1063 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1066 static int tecmp1 (const void *_a
, const void *_b
)
1068 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1069 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1070 return base_name_compare(
1071 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1072 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1075 static void mktree(struct tree_content
*t
,
1085 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1087 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1089 for (i
= 0; i
< t
->entry_count
; i
++) {
1090 if (t
->entries
[i
]->versions
[v
].mode
)
1091 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1094 size_dbuf(b
, maxlen
);
1096 for (i
= 0; i
< t
->entry_count
; i
++) {
1097 struct tree_entry
*e
= t
->entries
[i
];
1098 if (!e
->versions
[v
].mode
)
1100 c
+= sprintf(c
, "%o", e
->versions
[v
].mode
);
1102 strcpy(c
, e
->name
->str_dat
);
1103 c
+= e
->name
->str_len
+ 1;
1104 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1107 *szp
= c
- (char*)b
->buffer
;
1110 static void store_tree(struct tree_entry
*root
)
1112 struct tree_content
*t
= root
->tree
;
1113 unsigned int i
, j
, del
;
1114 unsigned long new_len
;
1115 struct last_object lo
;
1116 struct object_entry
*le
;
1118 if (!is_null_sha1(root
->versions
[1].sha1
))
1121 for (i
= 0; i
< t
->entry_count
; i
++) {
1122 if (t
->entries
[i
]->tree
)
1123 store_tree(t
->entries
[i
]);
1126 le
= find_object(root
->versions
[0].sha1
);
1127 if (!S_ISDIR(root
->versions
[0].mode
)
1129 || le
->pack_id
!= pack_id
) {
1133 mktree(t
, 0, &lo
.len
, &old_tree
);
1134 lo
.data
= old_tree
.buffer
;
1135 lo
.offset
= le
->offset
;
1136 lo
.depth
= t
->delta_depth
;
1140 mktree(t
, 1, &new_len
, &new_tree
);
1141 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1142 &lo
, root
->versions
[1].sha1
, 0);
1144 t
->delta_depth
= lo
.depth
;
1145 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1146 struct tree_entry
*e
= t
->entries
[i
];
1147 if (e
->versions
[1].mode
) {
1148 e
->versions
[0].mode
= e
->versions
[1].mode
;
1149 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1150 t
->entries
[j
++] = e
;
1152 release_tree_entry(e
);
1156 t
->entry_count
-= del
;
1159 static int tree_content_set(
1160 struct tree_entry
*root
,
1162 const unsigned char *sha1
,
1163 const unsigned int mode
)
1165 struct tree_content
*t
= root
->tree
;
1168 struct tree_entry
*e
;
1170 slash1
= strchr(p
, '/');
1176 for (i
= 0; i
< t
->entry_count
; i
++) {
1178 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1180 if (e
->versions
[1].mode
== mode
1181 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1183 e
->versions
[1].mode
= mode
;
1184 hashcpy(e
->versions
[1].sha1
, sha1
);
1186 release_tree_content_recursive(e
->tree
);
1189 hashclr(root
->versions
[1].sha1
);
1192 if (!S_ISDIR(e
->versions
[1].mode
)) {
1193 e
->tree
= new_tree_content(8);
1194 e
->versions
[1].mode
= S_IFDIR
;
1198 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1199 hashclr(root
->versions
[1].sha1
);
1206 if (t
->entry_count
== t
->entry_capacity
)
1207 root
->tree
= t
= grow_tree_content(t
, 8);
1208 e
= new_tree_entry();
1209 e
->name
= to_atom(p
, n
);
1210 e
->versions
[0].mode
= 0;
1211 hashclr(e
->versions
[0].sha1
);
1212 t
->entries
[t
->entry_count
++] = e
;
1214 e
->tree
= new_tree_content(8);
1215 e
->versions
[1].mode
= S_IFDIR
;
1216 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1219 e
->versions
[1].mode
= mode
;
1220 hashcpy(e
->versions
[1].sha1
, sha1
);
1222 hashclr(root
->versions
[1].sha1
);
1226 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1228 struct tree_content
*t
= root
->tree
;
1231 struct tree_entry
*e
;
1233 slash1
= strchr(p
, '/');
1239 for (i
= 0; i
< t
->entry_count
; i
++) {
1241 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1242 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1246 if (tree_content_remove(e
, slash1
+ 1)) {
1247 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1248 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1249 hashclr(root
->versions
[1].sha1
);
1262 release_tree_content_recursive(e
->tree
);
1265 e
->versions
[1].mode
= 0;
1266 hashclr(e
->versions
[1].sha1
);
1267 hashclr(root
->versions
[1].sha1
);
1271 static void dump_branches(void)
1273 static const char *msg
= "fast-import";
1276 struct ref_lock
*lock
;
1278 for (i
= 0; i
< branch_table_sz
; i
++) {
1279 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
1280 lock
= lock_any_ref_for_update(b
->name
, NULL
);
1281 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1282 die("Can't write %s", b
->name
);
1287 static void dump_tags(void)
1289 static const char *msg
= "fast-import";
1291 struct ref_lock
*lock
;
1292 char path
[PATH_MAX
];
1294 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1295 sprintf(path
, "refs/tags/%s", t
->name
);
1296 lock
= lock_any_ref_for_update(path
, NULL
);
1297 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1298 die("Can't write %s", path
);
1302 static void dump_marks_helper(FILE *f
,
1308 for (k
= 0; k
< 1024; k
++) {
1309 if (m
->data
.sets
[k
])
1310 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1314 for (k
= 0; k
< 1024; k
++) {
1315 if (m
->data
.marked
[k
])
1316 fprintf(f
, ":%ju %s\n", base
+ k
,
1317 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1322 static void dump_marks(void)
1326 FILE *f
= fopen(mark_file
, "w");
1327 dump_marks_helper(f
, 0, marks
);
1332 static void read_next_command(void)
1334 read_line(&command_buf
, stdin
, '\n');
1337 static void cmd_mark(void)
1339 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1340 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1341 read_next_command();
1347 static void* cmd_data (size_t *size
)
1352 if (strncmp("data ", command_buf
.buf
, 5))
1353 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1355 if (!strncmp("<<", command_buf
.buf
+ 5, 2)) {
1356 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1357 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1359 buffer
= xmalloc(sz
);
1361 read_next_command();
1362 if (command_buf
.eof
)
1363 die("EOF in data (terminator '%s' not found)", term
);
1364 if (term_len
== command_buf
.len
1365 && !strcmp(term
, command_buf
.buf
))
1367 if (sz
< (length
+ command_buf
.len
)) {
1368 sz
= sz
* 3 / 2 + 16;
1369 if (sz
< (length
+ command_buf
.len
))
1370 sz
= length
+ command_buf
.len
;
1371 buffer
= xrealloc(buffer
, sz
);
1373 memcpy(buffer
+ length
,
1375 command_buf
.len
- 1);
1376 length
+= command_buf
.len
- 1;
1377 buffer
[length
++] = '\n';
1383 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1384 buffer
= xmalloc(length
);
1385 while (n
< length
) {
1386 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1387 if (!s
&& feof(stdin
))
1388 die("EOF in data (%lu bytes remaining)", length
- n
);
1393 if (fgetc(stdin
) != '\n')
1394 die("An lf did not trail the binary data as expected.");
1400 static void cmd_new_blob(void)
1405 read_next_command();
1409 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1413 static void unload_one_branch(void)
1415 while (cur_active_branches
1416 && cur_active_branches
>= max_active_branches
) {
1417 unsigned long min_commit
= ULONG_MAX
;
1418 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1420 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1421 if (e
->last_commit
< min_commit
) {
1423 min_commit
= e
->last_commit
;
1429 e
= p
->active_next_branch
;
1430 p
->active_next_branch
= e
->active_next_branch
;
1432 e
= active_branches
;
1433 active_branches
= e
->active_next_branch
;
1435 e
->active_next_branch
= NULL
;
1436 if (e
->branch_tree
.tree
) {
1437 release_tree_content_recursive(e
->branch_tree
.tree
);
1438 e
->branch_tree
.tree
= NULL
;
1440 cur_active_branches
--;
1444 static void load_branch(struct branch
*b
)
1446 load_tree(&b
->branch_tree
);
1447 b
->active_next_branch
= active_branches
;
1448 active_branches
= b
;
1449 cur_active_branches
++;
1450 branch_load_count
++;
1453 static void file_change_m(struct branch
*b
)
1455 const char *p
= command_buf
.buf
+ 2;
1458 struct object_entry
*oe
;
1459 unsigned char sha1
[20];
1460 unsigned int mode
, inline_data
= 0;
1463 p
= get_mode(p
, &mode
);
1465 die("Corrupt mode: %s", command_buf
.buf
);
1467 case S_IFREG
| 0644:
1468 case S_IFREG
| 0755:
1475 die("Corrupt mode: %s", command_buf
.buf
);
1480 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1481 hashcpy(sha1
, oe
->sha1
);
1483 } else if (!strncmp("inline", p
, 6)) {
1487 if (get_sha1_hex(p
, sha1
))
1488 die("Invalid SHA1: %s", command_buf
.buf
);
1489 oe
= find_object(sha1
);
1493 die("Missing space after SHA1: %s", command_buf
.buf
);
1495 p_uq
= unquote_c_style(p
, &endp
);
1498 die("Garbage after path in: %s", command_buf
.buf
);
1506 p
= p_uq
= xstrdup(p
);
1507 read_next_command();
1509 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1512 if (oe
->type
!= OBJ_BLOB
)
1513 die("Not a blob (actually a %s): %s",
1514 command_buf
.buf
, type_names
[oe
->type
]);
1516 if (sha1_object_info(sha1
, type
, NULL
))
1517 die("Blob not found: %s", command_buf
.buf
);
1518 if (strcmp(blob_type
, type
))
1519 die("Not a blob (actually a %s): %s",
1520 command_buf
.buf
, type
);
1523 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1529 static void file_change_d(struct branch
*b
)
1531 const char *p
= command_buf
.buf
+ 2;
1535 p_uq
= unquote_c_style(p
, &endp
);
1538 die("Garbage after path in: %s", command_buf
.buf
);
1541 tree_content_remove(&b
->branch_tree
, p
);
1546 static void cmd_from(struct branch
*b
)
1548 const char *from
, *endp
;
1552 if (strncmp("from ", command_buf
.buf
, 5))
1556 die("Can't reinitailize branch %s", b
->name
);
1558 from
= strchr(command_buf
.buf
, ' ') + 1;
1559 str_uq
= unquote_c_style(from
, &endp
);
1562 die("Garbage after string in: %s", command_buf
.buf
);
1566 s
= lookup_branch(from
);
1568 die("Can't create a branch from itself: %s", b
->name
);
1570 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1571 hashcpy(b
->sha1
, s
->sha1
);
1572 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1573 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1574 } else if (*from
== ':') {
1575 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1576 struct object_entry
*oe
= find_mark(idnum
);
1579 if (oe
->type
!= OBJ_COMMIT
)
1580 die("Mark :%ju not a commit", idnum
);
1581 hashcpy(b
->sha1
, oe
->sha1
);
1582 buf
= gfi_unpack_entry(oe
, &size
);
1583 if (!buf
|| size
< 46)
1584 die("Not a valid commit: %s", from
);
1585 if (memcmp("tree ", buf
, 5)
1586 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1587 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1589 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1590 b
->branch_tree
.versions
[1].sha1
);
1591 } else if (!get_sha1(from
, b
->sha1
)) {
1592 if (is_null_sha1(b
->sha1
)) {
1593 hashclr(b
->branch_tree
.versions
[0].sha1
);
1594 hashclr(b
->branch_tree
.versions
[1].sha1
);
1599 buf
= read_object_with_reference(b
->sha1
,
1600 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1601 if (!buf
|| size
< 46)
1602 die("Not a valid commit: %s", from
);
1603 if (memcmp("tree ", buf
, 5)
1604 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1605 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1607 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1608 b
->branch_tree
.versions
[1].sha1
);
1611 die("Invalid ref name or SHA1 expression: %s", from
);
1613 read_next_command();
1616 static struct hash_list
* cmd_merge(unsigned int *count
)
1618 struct hash_list
*list
= NULL
, *n
, *e
;
1619 const char *from
, *endp
;
1624 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1625 from
= strchr(command_buf
.buf
, ' ') + 1;
1626 str_uq
= unquote_c_style(from
, &endp
);
1629 die("Garbage after string in: %s", command_buf
.buf
);
1633 n
= xmalloc(sizeof(*n
));
1634 s
= lookup_branch(from
);
1636 hashcpy(n
->sha1
, s
->sha1
);
1637 else if (*from
== ':') {
1638 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1639 struct object_entry
*oe
= find_mark(idnum
);
1640 if (oe
->type
!= OBJ_COMMIT
)
1641 die("Mark :%ju not a commit", idnum
);
1642 hashcpy(n
->sha1
, oe
->sha1
);
1643 } else if (get_sha1(from
, n
->sha1
))
1644 die("Invalid ref name or SHA1 expression: %s", from
);
1653 read_next_command();
1658 static void cmd_new_commit(void)
1666 char *author
= NULL
;
1667 char *committer
= NULL
;
1668 struct hash_list
*merge_list
= NULL
;
1669 unsigned int merge_count
;
1671 /* Obtain the branch name from the rest of our command */
1672 sp
= strchr(command_buf
.buf
, ' ') + 1;
1673 str_uq
= unquote_c_style(sp
, &endp
);
1676 die("Garbage after ref in: %s", command_buf
.buf
);
1679 b
= lookup_branch(sp
);
1685 read_next_command();
1687 if (!strncmp("author ", command_buf
.buf
, 7)) {
1688 author
= strdup(command_buf
.buf
);
1689 read_next_command();
1691 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1692 committer
= strdup(command_buf
.buf
);
1693 read_next_command();
1696 die("Expected committer but didn't get one");
1697 msg
= cmd_data(&msglen
);
1698 read_next_command();
1700 merge_list
= cmd_merge(&merge_count
);
1702 /* ensure the branch is active/loaded */
1703 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1704 unload_one_branch();
1710 if (1 == command_buf
.len
)
1712 else if (!strncmp("M ", command_buf
.buf
, 2))
1714 else if (!strncmp("D ", command_buf
.buf
, 2))
1717 die("Unsupported file_change: %s", command_buf
.buf
);
1718 read_next_command();
1721 /* build the tree and the commit */
1722 store_tree(&b
->branch_tree
);
1723 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1724 b
->branch_tree
.versions
[1].sha1
);
1725 size_dbuf(&new_data
, 97 + msglen
1728 ? strlen(author
) + strlen(committer
)
1729 : 2 * strlen(committer
)));
1730 sp
= new_data
.buffer
;
1731 sp
+= sprintf(sp
, "tree %s\n",
1732 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1733 if (!is_null_sha1(b
->sha1
))
1734 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1735 while (merge_list
) {
1736 struct hash_list
*next
= merge_list
->next
;
1737 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1742 sp
+= sprintf(sp
, "%s\n", author
);
1744 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1745 sp
+= sprintf(sp
, "%s\n\n", committer
);
1746 memcpy(sp
, msg
, msglen
);
1753 if (!store_object(OBJ_COMMIT
,
1754 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1755 NULL
, b
->sha1
, next_mark
))
1756 b
->pack_id
= pack_id
;
1757 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1760 int need_dq
= quote_c_style(b
->name
, NULL
, NULL
, 0);
1761 fprintf(branch_log
, "commit ");
1763 fputc('"', branch_log
);
1764 quote_c_style(b
->name
, NULL
, branch_log
, 0);
1765 fputc('"', branch_log
);
1767 fprintf(branch_log
, "%s", b
->name
);
1768 fprintf(branch_log
," :%ju %s\n",next_mark
,sha1_to_hex(b
->sha1
));
1772 static void cmd_new_tag(void)
1783 uintmax_t from_mark
= 0;
1784 unsigned char sha1
[20];
1786 /* Obtain the new tag name from the rest of our command */
1787 sp
= strchr(command_buf
.buf
, ' ') + 1;
1788 str_uq
= unquote_c_style(sp
, &endp
);
1791 die("Garbage after tag name in: %s", command_buf
.buf
);
1794 t
= pool_alloc(sizeof(struct tag
));
1796 t
->name
= pool_strdup(sp
);
1798 last_tag
->next_tag
= t
;
1804 read_next_command();
1807 if (strncmp("from ", command_buf
.buf
, 5))
1808 die("Expected from command, got %s", command_buf
.buf
);
1810 from
= strchr(command_buf
.buf
, ' ') + 1;
1811 str_uq
= unquote_c_style(from
, &endp
);
1814 die("Garbage after string in: %s", command_buf
.buf
);
1818 s
= lookup_branch(from
);
1820 hashcpy(sha1
, s
->sha1
);
1821 } else if (*from
== ':') {
1822 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1823 struct object_entry
*oe
= find_mark(from_mark
);
1824 if (oe
->type
!= OBJ_COMMIT
)
1825 die("Mark :%ju not a commit", from_mark
);
1826 hashcpy(sha1
, oe
->sha1
);
1827 } else if (!get_sha1(from
, sha1
)) {
1831 buf
= read_object_with_reference(sha1
,
1832 type_names
[OBJ_COMMIT
], &size
, sha1
);
1833 if (!buf
|| size
< 46)
1834 die("Not a valid commit: %s", from
);
1837 die("Invalid ref name or SHA1 expression: %s", from
);
1841 read_next_command();
1844 if (strncmp("tagger ", command_buf
.buf
, 7))
1845 die("Expected tagger command, got %s", command_buf
.buf
);
1846 tagger
= strdup(command_buf
.buf
);
1848 /* tag payload/message */
1849 read_next_command();
1850 msg
= cmd_data(&msglen
);
1852 /* build the tag object */
1853 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1854 sp
= new_data
.buffer
;
1855 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1856 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1857 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1858 sp
+= sprintf(sp
, "%s\n\n", tagger
);
1859 memcpy(sp
, msg
, msglen
);
1864 if (store_object(OBJ_TAG
, new_data
.buffer
,
1865 sp
- (char*)new_data
.buffer
,
1867 t
->pack_id
= MAX_PACK_ID
;
1869 t
->pack_id
= pack_id
;
1872 int need_dq
= quote_c_style(t
->name
, NULL
, NULL
, 0);
1873 fprintf(branch_log
, "tag ");
1875 fputc('"', branch_log
);
1876 quote_c_style(t
->name
, NULL
, branch_log
, 0);
1877 fputc('"', branch_log
);
1879 fprintf(branch_log
, "%s", t
->name
);
1880 fprintf(branch_log
," :%ju %s\n",from_mark
,sha1_to_hex(t
->sha1
));
1884 static void cmd_reset_branch(void)
1891 /* Obtain the branch name from the rest of our command */
1892 sp
= strchr(command_buf
.buf
, ' ') + 1;
1893 str_uq
= unquote_c_style(sp
, &endp
);
1896 die("Garbage after ref in: %s", command_buf
.buf
);
1899 b
= lookup_branch(sp
);
1902 if (b
->branch_tree
.tree
) {
1903 release_tree_content_recursive(b
->branch_tree
.tree
);
1904 b
->branch_tree
.tree
= NULL
;
1911 read_next_command();
1915 static void cmd_checkpoint(void)
1919 read_next_command();
1922 static const char fast_import_usage
[] =
1923 "git-fast-import [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1925 int main(int argc
, const char **argv
)
1928 uintmax_t total_count
, duplicate_count
;
1931 git_config(git_default_config
);
1933 for (i
= 1; i
< argc
; i
++) {
1934 const char *a
= argv
[i
];
1936 if (*a
!= '-' || !strcmp(a
, "--"))
1938 else if (!strncmp(a
, "--max-pack-size=", 16))
1939 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1940 else if (!strncmp(a
, "--depth=", 8))
1941 max_depth
= strtoul(a
+ 8, NULL
, 0);
1942 else if (!strncmp(a
, "--active-branches=", 18))
1943 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1944 else if (!strncmp(a
, "--export-marks=", 15))
1946 else if (!strncmp(a
, "--branch-log=", 13)) {
1947 branch_log
= fopen(a
+ 13, "w");
1949 die("Can't create %s: %s", a
+ 13, strerror(errno
));
1952 die("unknown option %s", a
);
1955 usage(fast_import_usage
);
1957 alloc_objects(object_entry_alloc
);
1958 strbuf_init(&command_buf
);
1960 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1961 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1962 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1963 marks
= pool_calloc(1, sizeof(struct mark_set
));
1967 read_next_command();
1968 if (command_buf
.eof
)
1970 else if (!strcmp("blob", command_buf
.buf
))
1972 else if (!strncmp("commit ", command_buf
.buf
, 7))
1974 else if (!strncmp("tag ", command_buf
.buf
, 4))
1976 else if (!strncmp("reset ", command_buf
.buf
, 6))
1978 else if (!strcmp("checkpoint", command_buf
.buf
))
1981 die("Unsupported command: %s", command_buf
.buf
);
1993 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
1994 total_count
+= object_count_by_type
[i
];
1995 duplicate_count
= 0;
1996 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
1997 duplicate_count
+= duplicate_count_by_type
[i
];
1999 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2000 fprintf(stderr
, "---------------------------------------------------------------------\n");
2001 fprintf(stderr
, "Alloc'd objects: %10ju\n", alloc_count
);
2002 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
2003 fprintf(stderr
, " blobs : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
], delta_count_by_type
[OBJ_BLOB
]);
2004 fprintf(stderr
, " trees : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
], delta_count_by_type
[OBJ_TREE
]);
2005 fprintf(stderr
, " commits: %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
], delta_count_by_type
[OBJ_COMMIT
]);
2006 fprintf(stderr
, " tags : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
], delta_count_by_type
[OBJ_TAG
]);
2007 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2008 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2009 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2010 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2011 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
2012 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2013 fprintf(stderr
, "---------------------------------------------------------------------\n");
2015 fprintf(stderr
, "---------------------------------------------------------------------\n");
2016 fprintf(stderr
, "\n");