]>
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_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 ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
31 mode ::= '644' | '755';
33 new_tag ::= 'tag' sp tag_str lf
34 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
35 'tagger' sp name '<' email '>' ts tz lf
39 reset_branch ::= 'reset' sp ref_str lf
40 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
43 checkpoint ::= 'checkpoint' lf
46 # note: the first idnum in a stream should be 1 and subsequent
47 # idnums should not have gaps between values as this will cause
48 # the stream parser to reserve space for the gapped values. An
49 # idnum can be updated in the future to a new object by issuing
50 # a new mark directive with the old idnum.
52 mark ::= 'mark' sp idnum lf;
53 data ::= (delimited_data | exact_data)
56 # note: delim may be any string but must not contain lf.
57 # data_line may contain any data but must not be exactly
59 delimited_data ::= 'data' sp '<<' delim lf
63 # note: declen indicates the length of binary_data in bytes.
64 # declen does not include the lf preceeding the binary data.
66 exact_data ::= 'data' sp declen lf
69 # note: quoted strings are C-style quoting supporting \c for
70 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
71 # is the signed byte value in octal. Note that the only
72 # characters which must actually be escaped to protect the
73 # stream formatting is: \, " and LF. Otherwise these values
76 ref_str ::= ref | '"' quoted(ref) '"' ;
77 sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
78 tag_str ::= tag | '"' quoted(tag) '"' ;
79 path_str ::= path | '"' quoted(path) '"' ;
81 declen ::= # unsigned 32 bit value, ascii base10 notation;
82 bigint ::= # unsigned integer value, ascii base10 notation;
83 binary_data ::= # file content, not interpreted;
85 sp ::= # ASCII space character;
86 lf ::= # ASCII newline (LF) character;
88 # note: a colon (':') must precede the numerical value assigned to
89 # an idnum. This is to distinguish it from a ref or tag name as
90 # GIT does not permit ':' in ref or tag strings.
93 path ::= # GIT style file path, e.g. "a/b/c";
94 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
95 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
96 sha1exp ::= # Any valid GIT SHA1 expression;
97 hexsha1 ::= # SHA1 in hexadecimal format;
99 # note: name and email are UTF8 strings, however name must not
100 # contain '<' or lf and email must not contain any of the
101 # following: '<', '>', lf.
103 name ::= # valid GIT author/committer name;
104 email ::= # valid GIT author/committer email;
105 ts ::= # time since the epoch in seconds, ascii base10 notation;
106 tz ::= # GIT style timezone;
117 #include "csum-file.h"
121 #define PACK_ID_BITS 16
122 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
126 struct object_entry
*next
;
127 unsigned long offset
;
128 unsigned type
: TYPE_BITS
;
129 unsigned pack_id
: PACK_ID_BITS
;
130 unsigned char sha1
[20];
133 struct object_entry_pool
135 struct object_entry_pool
*next_pool
;
136 struct object_entry
*next_free
;
137 struct object_entry
*end
;
138 struct object_entry entries
[FLEX_ARRAY
]; /* more */
144 struct object_entry
*marked
[1024];
145 struct mark_set
*sets
[1024];
154 unsigned long offset
;
161 struct mem_pool
*next_pool
;
164 char space
[FLEX_ARRAY
]; /* more */
169 struct atom_str
*next_atom
;
170 unsigned int str_len
;
171 char str_dat
[FLEX_ARRAY
]; /* more */
177 struct tree_content
*tree
;
178 struct atom_str
* name
;
182 unsigned char sha1
[20];
188 unsigned int entry_capacity
; /* must match avail_tree_content */
189 unsigned int entry_count
;
190 unsigned int delta_depth
;
191 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
194 struct avail_tree_content
196 unsigned int entry_capacity
; /* must match tree_content */
197 struct avail_tree_content
*next_avail
;
202 struct branch
*table_next_branch
;
203 struct branch
*active_next_branch
;
205 struct tree_entry branch_tree
;
206 uintmax_t last_commit
;
207 unsigned int pack_id
;
208 unsigned char sha1
[20];
213 struct tag
*next_tag
;
215 unsigned int pack_id
;
216 unsigned char sha1
[20];
227 struct hash_list
*next
;
228 unsigned char sha1
[20];
231 /* Configured limits on output */
232 static unsigned long max_depth
= 10;
233 static unsigned long max_packsize
= (1LL << 32) - 1;
235 /* Stats and misc. counters */
236 static uintmax_t alloc_count
;
237 static uintmax_t marks_set_count
;
238 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
239 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
240 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
241 static unsigned long object_count
;
242 static unsigned long branch_count
;
243 static unsigned long branch_load_count
;
246 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
247 static size_t total_allocd
;
248 static struct mem_pool
*mem_pool
;
250 /* Atom management */
251 static unsigned int atom_table_sz
= 4451;
252 static unsigned int atom_cnt
;
253 static struct atom_str
**atom_table
;
255 /* The .pack file being generated */
256 static unsigned int pack_id
;
257 static struct packed_git
*pack_data
;
258 static struct packed_git
**all_packs
;
259 static unsigned long pack_size
;
261 /* Table of objects we've written. */
262 static unsigned int object_entry_alloc
= 5000;
263 static struct object_entry_pool
*blocks
;
264 static struct object_entry
*object_table
[1 << 16];
265 static struct mark_set
*marks
;
266 static const char* mark_file
;
269 static struct last_object last_blob
;
271 /* Tree management */
272 static unsigned int tree_entry_alloc
= 1000;
273 static void *avail_tree_entry
;
274 static unsigned int avail_tree_table_sz
= 100;
275 static struct avail_tree_content
**avail_tree_table
;
276 static struct dbuf old_tree
;
277 static struct dbuf new_tree
;
280 static unsigned long max_active_branches
= 5;
281 static unsigned long cur_active_branches
;
282 static unsigned long branch_table_sz
= 1039;
283 static struct branch
**branch_table
;
284 static struct branch
*active_branches
;
287 static struct tag
*first_tag
;
288 static struct tag
*last_tag
;
290 /* Input stream parsing */
291 static struct strbuf command_buf
;
292 static uintmax_t next_mark
;
293 static struct dbuf new_data
;
294 static FILE* branch_log
;
297 static void alloc_objects(unsigned int cnt
)
299 struct object_entry_pool
*b
;
301 b
= xmalloc(sizeof(struct object_entry_pool
)
302 + cnt
* sizeof(struct object_entry
));
303 b
->next_pool
= blocks
;
304 b
->next_free
= b
->entries
;
305 b
->end
= b
->entries
+ cnt
;
310 static struct object_entry
* new_object(unsigned char *sha1
)
312 struct object_entry
*e
;
314 if (blocks
->next_free
== blocks
->end
)
315 alloc_objects(object_entry_alloc
);
317 e
= blocks
->next_free
++;
318 hashcpy(e
->sha1
, sha1
);
322 static struct object_entry
* find_object(unsigned char *sha1
)
324 unsigned int h
= sha1
[0] << 8 | sha1
[1];
325 struct object_entry
*e
;
326 for (e
= object_table
[h
]; e
; e
= e
->next
)
327 if (!hashcmp(sha1
, e
->sha1
))
332 static struct object_entry
* insert_object(unsigned char *sha1
)
334 unsigned int h
= sha1
[0] << 8 | sha1
[1];
335 struct object_entry
*e
= object_table
[h
];
336 struct object_entry
*p
= NULL
;
339 if (!hashcmp(sha1
, e
->sha1
))
345 e
= new_object(sha1
);
355 static unsigned int hc_str(const char *s
, size_t len
)
363 static void* pool_alloc(size_t len
)
368 for (p
= mem_pool
; p
; p
= p
->next_pool
)
369 if ((p
->end
- p
->next_free
>= len
))
373 if (len
>= (mem_pool_alloc
/2)) {
377 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
378 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
379 p
->next_pool
= mem_pool
;
380 p
->next_free
= p
->space
;
381 p
->end
= p
->next_free
+ mem_pool_alloc
;
386 /* round out to a pointer alignment */
387 if (len
& (sizeof(void*) - 1))
388 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
393 static void* pool_calloc(size_t count
, size_t size
)
395 size_t len
= count
* size
;
396 void *r
= pool_alloc(len
);
401 static char* pool_strdup(const char *s
)
403 char *r
= pool_alloc(strlen(s
) + 1);
408 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
411 if (b
->capacity
>= maxlen
)
415 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
416 b
->buffer
= xmalloc(b
->capacity
);
419 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
421 struct mark_set
*s
= marks
;
422 while ((idnum
>> s
->shift
) >= 1024) {
423 s
= pool_calloc(1, sizeof(struct mark_set
));
424 s
->shift
= marks
->shift
+ 10;
425 s
->data
.sets
[0] = marks
;
429 uintmax_t i
= idnum
>> s
->shift
;
430 idnum
-= i
<< s
->shift
;
431 if (!s
->data
.sets
[i
]) {
432 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
433 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
437 if (!s
->data
.marked
[idnum
])
439 s
->data
.marked
[idnum
] = oe
;
442 static struct object_entry
* find_mark(uintmax_t idnum
)
444 uintmax_t orig_idnum
= idnum
;
445 struct mark_set
*s
= marks
;
446 struct object_entry
*oe
= NULL
;
447 if ((idnum
>> s
->shift
) < 1024) {
448 while (s
&& s
->shift
) {
449 uintmax_t i
= idnum
>> s
->shift
;
450 idnum
-= i
<< s
->shift
;
454 oe
= s
->data
.marked
[idnum
];
457 die("mark :%ju not declared", orig_idnum
);
461 static struct atom_str
* to_atom(const char *s
, size_t len
)
463 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
466 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
467 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
470 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
472 strncpy(c
->str_dat
, s
, len
);
474 c
->next_atom
= atom_table
[hc
];
480 static struct branch
* lookup_branch(const char *name
)
482 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
485 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
486 if (!strcmp(name
, b
->name
))
491 static struct branch
* new_branch(const char *name
)
493 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
494 struct branch
* b
= lookup_branch(name
);
497 die("Invalid attempt to create duplicate branch: %s", name
);
498 if (check_ref_format(name
))
499 die("Branch name doesn't conform to GIT standards: %s", name
);
501 b
= pool_calloc(1, sizeof(struct branch
));
502 b
->name
= pool_strdup(name
);
503 b
->table_next_branch
= branch_table
[hc
];
504 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
505 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
506 b
->pack_id
= MAX_PACK_ID
;
507 branch_table
[hc
] = b
;
512 static unsigned int hc_entries(unsigned int cnt
)
514 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
515 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
518 static struct tree_content
* new_tree_content(unsigned int cnt
)
520 struct avail_tree_content
*f
, *l
= NULL
;
521 struct tree_content
*t
;
522 unsigned int hc
= hc_entries(cnt
);
524 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
525 if (f
->entry_capacity
>= cnt
)
530 l
->next_avail
= f
->next_avail
;
532 avail_tree_table
[hc
] = f
->next_avail
;
534 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
535 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
536 f
->entry_capacity
= cnt
;
539 t
= (struct tree_content
*)f
;
545 static void release_tree_entry(struct tree_entry
*e
);
546 static void release_tree_content(struct tree_content
*t
)
548 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
549 unsigned int hc
= hc_entries(f
->entry_capacity
);
550 f
->next_avail
= avail_tree_table
[hc
];
551 avail_tree_table
[hc
] = f
;
554 static void release_tree_content_recursive(struct tree_content
*t
)
557 for (i
= 0; i
< t
->entry_count
; i
++)
558 release_tree_entry(t
->entries
[i
]);
559 release_tree_content(t
);
562 static struct tree_content
* grow_tree_content(
563 struct tree_content
*t
,
566 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
567 r
->entry_count
= t
->entry_count
;
568 r
->delta_depth
= t
->delta_depth
;
569 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
570 release_tree_content(t
);
574 static struct tree_entry
* new_tree_entry(void)
576 struct tree_entry
*e
;
578 if (!avail_tree_entry
) {
579 unsigned int n
= tree_entry_alloc
;
580 total_allocd
+= n
* sizeof(struct tree_entry
);
581 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
583 *((void**)e
) = e
+ 1;
589 e
= avail_tree_entry
;
590 avail_tree_entry
= *((void**)e
);
594 static void release_tree_entry(struct tree_entry
*e
)
597 release_tree_content_recursive(e
->tree
);
598 *((void**)e
) = avail_tree_entry
;
599 avail_tree_entry
= e
;
602 static void start_packfile(void)
604 static char tmpfile
[PATH_MAX
];
605 struct packed_git
*p
;
606 struct pack_header hdr
;
609 snprintf(tmpfile
, sizeof(tmpfile
),
610 "%s/pack_XXXXXX", get_object_directory());
611 pack_fd
= mkstemp(tmpfile
);
613 die("Can't create %s: %s", tmpfile
, strerror(errno
));
614 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
615 strcpy(p
->pack_name
, tmpfile
);
616 p
->pack_fd
= pack_fd
;
618 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
619 hdr
.hdr_version
= htonl(2);
621 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
624 pack_size
= sizeof(hdr
);
627 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
628 all_packs
[pack_id
] = p
;
631 static void fixup_header_footer(void)
633 static const int buf_sz
= 128 * 1024;
634 int pack_fd
= pack_data
->pack_fd
;
636 struct pack_header hdr
;
639 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
640 die("Failed seeking to start: %s", strerror(errno
));
641 if (read_in_full(pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
642 die("Unable to reread header of %s", pack_data
->pack_name
);
643 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
644 die("Failed seeking to start: %s", strerror(errno
));
645 hdr
.hdr_entries
= htonl(object_count
);
646 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
649 SHA1_Update(&c
, &hdr
, sizeof(hdr
));
651 buf
= xmalloc(buf_sz
);
653 size_t n
= xread(pack_fd
, buf
, buf_sz
);
657 die("Failed to checksum %s", pack_data
->pack_name
);
658 SHA1_Update(&c
, buf
, n
);
662 SHA1_Final(pack_data
->sha1
, &c
);
663 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
667 static int oecmp (const void *a_
, const void *b_
)
669 struct object_entry
*a
= *((struct object_entry
**)a_
);
670 struct object_entry
*b
= *((struct object_entry
**)b_
);
671 return hashcmp(a
->sha1
, b
->sha1
);
674 static char* create_index(void)
676 static char tmpfile
[PATH_MAX
];
679 struct object_entry
**idx
, **c
, **last
, *e
;
680 struct object_entry_pool
*o
;
684 /* Build the sorted table of object IDs. */
685 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
687 for (o
= blocks
; o
; o
= o
->next_pool
)
688 for (e
= o
->next_free
; e
-- != o
->entries
;)
689 if (pack_id
== e
->pack_id
)
691 last
= idx
+ object_count
;
693 die("internal consistency error creating the index");
694 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
696 /* Generate the fan-out array. */
698 for (i
= 0; i
< 256; i
++) {
699 struct object_entry
**next
= c
;;
700 while (next
< last
) {
701 if ((*next
)->sha1
[0] != i
)
705 array
[i
] = htonl(next
- idx
);
709 snprintf(tmpfile
, sizeof(tmpfile
),
710 "%s/index_XXXXXX", get_object_directory());
711 idx_fd
= mkstemp(tmpfile
);
713 die("Can't create %s: %s", tmpfile
, strerror(errno
));
714 f
= sha1fd(idx_fd
, tmpfile
);
715 sha1write(f
, array
, 256 * sizeof(int));
717 for (c
= idx
; c
!= last
; c
++) {
718 uint32_t offset
= htonl((*c
)->offset
);
719 sha1write(f
, &offset
, 4);
720 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
721 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
723 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
724 sha1close(f
, NULL
, 1);
726 SHA1_Final(pack_data
->sha1
, &ctx
);
730 static char* keep_pack(char *curr_index_name
)
732 static char name
[PATH_MAX
];
733 static char *keep_msg
= "fast-import";
736 chmod(pack_data
->pack_name
, 0444);
737 chmod(curr_index_name
, 0444);
739 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
740 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
741 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
743 die("cannot create keep file");
744 write(keep_fd
, keep_msg
, strlen(keep_msg
));
747 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
748 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
749 if (move_temp_to_file(pack_data
->pack_name
, name
))
750 die("cannot store pack file");
752 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
753 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
754 if (move_temp_to_file(curr_index_name
, name
))
755 die("cannot store index file");
759 static void unkeep_all_packs(void)
761 static char name
[PATH_MAX
];
764 for (k
= 0; k
< pack_id
; k
++) {
765 struct packed_git
*p
= all_packs
[k
];
766 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
767 get_object_directory(), sha1_to_hex(p
->sha1
));
772 static void end_packfile(void)
774 struct packed_git
*old_p
= pack_data
, *new_p
;
782 fixup_header_footer();
783 idx_name
= keep_pack(create_index());
785 /* Register the packfile with core git's machinary. */
786 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
788 die("core git rejected index %s", idx_name
);
789 new_p
->windows
= old_p
->windows
;
790 all_packs
[pack_id
] = new_p
;
791 install_packed_git(new_p
);
793 /* Print the boundary */
794 fprintf(stdout
, "%s:", new_p
->pack_name
);
795 for (i
= 0; i
< branch_table_sz
; i
++) {
796 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
797 if (b
->pack_id
== pack_id
)
798 fprintf(stdout
, " %s", sha1_to_hex(b
->sha1
));
801 for (t
= first_tag
; t
; t
= t
->next_tag
) {
802 if (t
->pack_id
== pack_id
)
803 fprintf(stdout
, " %s", sha1_to_hex(t
->sha1
));
810 unlink(old_p
->pack_name
);
813 /* We can't carry a delta across packfiles. */
814 free(last_blob
.data
);
815 last_blob
.data
= NULL
;
817 last_blob
.offset
= 0;
821 static void checkpoint(void)
827 static size_t encode_header(
828 enum object_type type
,
835 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
836 die("bad type %d", type
);
838 c
= (type
<< 4) | (size
& 15);
850 static int store_object(
851 enum object_type type
,
854 struct last_object
*last
,
855 unsigned char *sha1out
,
859 struct object_entry
*e
;
860 unsigned char hdr
[96];
861 unsigned char sha1
[20];
862 unsigned long hdrlen
, deltalen
;
866 hdrlen
= sprintf((char*)hdr
,"%s %lu",type_names
[type
],datlen
) + 1;
868 SHA1_Update(&c
, hdr
, hdrlen
);
869 SHA1_Update(&c
, dat
, datlen
);
870 SHA1_Final(sha1
, &c
);
872 hashcpy(sha1out
, sha1
);
874 e
= insert_object(sha1
);
876 insert_mark(mark
, e
);
878 duplicate_count_by_type
[type
]++;
882 if (last
&& last
->data
&& last
->depth
< max_depth
) {
883 delta
= diff_delta(last
->data
, last
->len
,
886 if (delta
&& deltalen
>= datlen
) {
893 memset(&s
, 0, sizeof(s
));
894 deflateInit(&s
, zlib_compression_level
);
897 s
.avail_in
= deltalen
;
902 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
903 s
.next_out
= out
= xmalloc(s
.avail_out
);
904 while (deflate(&s
, Z_FINISH
) == Z_OK
)
908 /* Determine if we should auto-checkpoint. */
909 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
910 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
912 /* This new object needs to *not* have the current pack_id. */
913 e
->pack_id
= pack_id
+ 1;
916 /* We cannot carry a delta into the new pack. */
921 memset(&s
, 0, sizeof(s
));
922 deflateInit(&s
, zlib_compression_level
);
925 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
926 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
927 while (deflate(&s
, Z_FINISH
) == Z_OK
)
934 e
->pack_id
= pack_id
;
935 e
->offset
= pack_size
;
937 object_count_by_type
[type
]++;
940 unsigned long ofs
= e
->offset
- last
->offset
;
941 unsigned pos
= sizeof(hdr
) - 1;
943 delta_count_by_type
[type
]++;
946 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
947 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
950 hdr
[pos
] = ofs
& 127;
952 hdr
[--pos
] = 128 | (--ofs
& 127);
953 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
954 pack_size
+= sizeof(hdr
) - pos
;
958 hdrlen
= encode_header(type
, datlen
, hdr
);
959 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
963 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
964 pack_size
+= s
.total_out
;
970 if (last
->data
&& !last
->no_free
)
973 last
->offset
= e
->offset
;
979 static void *gfi_unpack_entry(
980 struct object_entry
*oe
,
981 unsigned long *sizep
)
983 static char type
[20];
984 struct packed_git
*p
= all_packs
[oe
->pack_id
];
986 p
->pack_size
= pack_size
+ 20;
987 return unpack_entry(p
, oe
->offset
, type
, sizep
);
990 static const char *get_mode(const char *str
, unsigned int *modep
)
993 unsigned int mode
= 0;
995 while ((c
= *str
++) != ' ') {
996 if (c
< '0' || c
> '7')
998 mode
= (mode
<< 3) + (c
- '0');
1004 static void load_tree(struct tree_entry
*root
)
1006 unsigned char* sha1
= root
->versions
[1].sha1
;
1007 struct object_entry
*myoe
;
1008 struct tree_content
*t
;
1013 root
->tree
= t
= new_tree_content(8);
1014 if (is_null_sha1(sha1
))
1017 myoe
= find_object(sha1
);
1019 if (myoe
->type
!= OBJ_TREE
)
1020 die("Not a tree: %s", sha1_to_hex(sha1
));
1022 buf
= gfi_unpack_entry(myoe
, &size
);
1025 buf
= read_sha1_file(sha1
, type
, &size
);
1026 if (!buf
|| strcmp(type
, tree_type
))
1027 die("Can't load tree %s", sha1_to_hex(sha1
));
1031 while (c
!= (buf
+ size
)) {
1032 struct tree_entry
*e
= new_tree_entry();
1034 if (t
->entry_count
== t
->entry_capacity
)
1035 root
->tree
= t
= grow_tree_content(t
, 8);
1036 t
->entries
[t
->entry_count
++] = e
;
1039 c
= get_mode(c
, &e
->versions
[1].mode
);
1041 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1042 e
->versions
[0].mode
= e
->versions
[1].mode
;
1043 e
->name
= to_atom(c
, strlen(c
));
1044 c
+= e
->name
->str_len
+ 1;
1045 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1046 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1052 static int tecmp0 (const void *_a
, const void *_b
)
1054 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1055 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1056 return base_name_compare(
1057 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1058 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1061 static int tecmp1 (const void *_a
, const void *_b
)
1063 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1064 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1065 return base_name_compare(
1066 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1067 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1070 static void mktree(struct tree_content
*t
,
1080 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1082 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1084 for (i
= 0; i
< t
->entry_count
; i
++) {
1085 if (t
->entries
[i
]->versions
[v
].mode
)
1086 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1089 size_dbuf(b
, maxlen
);
1091 for (i
= 0; i
< t
->entry_count
; i
++) {
1092 struct tree_entry
*e
= t
->entries
[i
];
1093 if (!e
->versions
[v
].mode
)
1095 c
+= sprintf(c
, "%o", e
->versions
[v
].mode
);
1097 strcpy(c
, e
->name
->str_dat
);
1098 c
+= e
->name
->str_len
+ 1;
1099 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1102 *szp
= c
- (char*)b
->buffer
;
1105 static void store_tree(struct tree_entry
*root
)
1107 struct tree_content
*t
= root
->tree
;
1108 unsigned int i
, j
, del
;
1109 unsigned long new_len
;
1110 struct last_object lo
;
1111 struct object_entry
*le
;
1113 if (!is_null_sha1(root
->versions
[1].sha1
))
1116 for (i
= 0; i
< t
->entry_count
; i
++) {
1117 if (t
->entries
[i
]->tree
)
1118 store_tree(t
->entries
[i
]);
1121 le
= find_object(root
->versions
[0].sha1
);
1122 if (!S_ISDIR(root
->versions
[0].mode
)
1124 || le
->pack_id
!= pack_id
) {
1128 mktree(t
, 0, &lo
.len
, &old_tree
);
1129 lo
.data
= old_tree
.buffer
;
1130 lo
.offset
= le
->offset
;
1131 lo
.depth
= t
->delta_depth
;
1135 mktree(t
, 1, &new_len
, &new_tree
);
1136 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1137 &lo
, root
->versions
[1].sha1
, 0);
1139 t
->delta_depth
= lo
.depth
;
1140 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1141 struct tree_entry
*e
= t
->entries
[i
];
1142 if (e
->versions
[1].mode
) {
1143 e
->versions
[0].mode
= e
->versions
[1].mode
;
1144 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1145 t
->entries
[j
++] = e
;
1147 release_tree_entry(e
);
1151 t
->entry_count
-= del
;
1154 static int tree_content_set(
1155 struct tree_entry
*root
,
1157 const unsigned char *sha1
,
1158 const unsigned int mode
)
1160 struct tree_content
*t
= root
->tree
;
1163 struct tree_entry
*e
;
1165 slash1
= strchr(p
, '/');
1171 for (i
= 0; i
< t
->entry_count
; i
++) {
1173 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1175 if (e
->versions
[1].mode
== mode
1176 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1178 e
->versions
[1].mode
= mode
;
1179 hashcpy(e
->versions
[1].sha1
, sha1
);
1181 release_tree_content_recursive(e
->tree
);
1184 hashclr(root
->versions
[1].sha1
);
1187 if (!S_ISDIR(e
->versions
[1].mode
)) {
1188 e
->tree
= new_tree_content(8);
1189 e
->versions
[1].mode
= S_IFDIR
;
1193 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1194 hashclr(root
->versions
[1].sha1
);
1201 if (t
->entry_count
== t
->entry_capacity
)
1202 root
->tree
= t
= grow_tree_content(t
, 8);
1203 e
= new_tree_entry();
1204 e
->name
= to_atom(p
, n
);
1205 e
->versions
[0].mode
= 0;
1206 hashclr(e
->versions
[0].sha1
);
1207 t
->entries
[t
->entry_count
++] = e
;
1209 e
->tree
= new_tree_content(8);
1210 e
->versions
[1].mode
= S_IFDIR
;
1211 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1214 e
->versions
[1].mode
= mode
;
1215 hashcpy(e
->versions
[1].sha1
, sha1
);
1217 hashclr(root
->versions
[1].sha1
);
1221 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1223 struct tree_content
*t
= root
->tree
;
1226 struct tree_entry
*e
;
1228 slash1
= strchr(p
, '/');
1234 for (i
= 0; i
< t
->entry_count
; i
++) {
1236 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1237 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1241 if (tree_content_remove(e
, slash1
+ 1)) {
1242 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1243 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1244 hashclr(root
->versions
[1].sha1
);
1257 release_tree_content_recursive(e
->tree
);
1260 e
->versions
[1].mode
= 0;
1261 hashclr(e
->versions
[1].sha1
);
1262 hashclr(root
->versions
[1].sha1
);
1266 static void dump_branches(void)
1268 static const char *msg
= "fast-import";
1271 struct ref_lock
*lock
;
1273 for (i
= 0; i
< branch_table_sz
; i
++) {
1274 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
1275 lock
= lock_any_ref_for_update(b
->name
, NULL
);
1276 if (!lock
|| write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1277 die("Can't write %s", b
->name
);
1282 static void dump_tags(void)
1284 static const char *msg
= "fast-import";
1286 struct ref_lock
*lock
;
1287 char path
[PATH_MAX
];
1289 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1290 sprintf(path
, "refs/tags/%s", t
->name
);
1291 lock
= lock_any_ref_for_update(path
, NULL
);
1292 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1293 die("Can't write %s", path
);
1297 static void dump_marks_helper(FILE *f
,
1303 for (k
= 0; k
< 1024; k
++) {
1304 if (m
->data
.sets
[k
])
1305 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1309 for (k
= 0; k
< 1024; k
++) {
1310 if (m
->data
.marked
[k
])
1311 fprintf(f
, ":%ju %s\n", base
+ k
,
1312 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1317 static void dump_marks(void)
1321 FILE *f
= fopen(mark_file
, "w");
1322 dump_marks_helper(f
, 0, marks
);
1327 static void read_next_command(void)
1329 read_line(&command_buf
, stdin
, '\n');
1332 static void cmd_mark(void)
1334 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1335 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1336 read_next_command();
1342 static void* cmd_data (size_t *size
)
1347 if (strncmp("data ", command_buf
.buf
, 5))
1348 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1350 if (!strncmp("<<", command_buf
.buf
+ 5, 2)) {
1351 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1352 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1354 buffer
= xmalloc(sz
);
1356 read_next_command();
1357 if (command_buf
.eof
)
1358 die("EOF in data (terminator '%s' not found)", term
);
1359 if (term_len
== command_buf
.len
1360 && !strcmp(term
, command_buf
.buf
))
1362 if (sz
< (length
+ command_buf
.len
)) {
1363 sz
= sz
* 3 / 2 + 16;
1364 if (sz
< (length
+ command_buf
.len
))
1365 sz
= length
+ command_buf
.len
;
1366 buffer
= xrealloc(buffer
, sz
);
1368 memcpy(buffer
+ length
,
1370 command_buf
.len
- 1);
1371 length
+= command_buf
.len
- 1;
1372 buffer
[length
++] = '\n';
1378 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1379 buffer
= xmalloc(length
);
1380 while (n
< length
) {
1381 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1382 if (!s
&& feof(stdin
))
1383 die("EOF in data (%lu bytes remaining)", length
- n
);
1388 if (fgetc(stdin
) != '\n')
1389 die("An lf did not trail the binary data as expected.");
1395 static void cmd_new_blob(void)
1400 read_next_command();
1404 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1408 static void unload_one_branch(void)
1410 while (cur_active_branches
1411 && cur_active_branches
>= max_active_branches
) {
1412 unsigned long min_commit
= ULONG_MAX
;
1413 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1415 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1416 if (e
->last_commit
< min_commit
) {
1418 min_commit
= e
->last_commit
;
1424 e
= p
->active_next_branch
;
1425 p
->active_next_branch
= e
->active_next_branch
;
1427 e
= active_branches
;
1428 active_branches
= e
->active_next_branch
;
1430 e
->active_next_branch
= NULL
;
1431 if (e
->branch_tree
.tree
) {
1432 release_tree_content_recursive(e
->branch_tree
.tree
);
1433 e
->branch_tree
.tree
= NULL
;
1435 cur_active_branches
--;
1439 static void load_branch(struct branch
*b
)
1441 load_tree(&b
->branch_tree
);
1442 b
->active_next_branch
= active_branches
;
1443 active_branches
= b
;
1444 cur_active_branches
++;
1445 branch_load_count
++;
1448 static void file_change_m(struct branch
*b
)
1450 const char *p
= command_buf
.buf
+ 2;
1453 struct object_entry
*oe
;
1454 unsigned char sha1
[20];
1458 p
= get_mode(p
, &mode
);
1460 die("Corrupt mode: %s", command_buf
.buf
);
1462 case S_IFREG
| 0644:
1463 case S_IFREG
| 0755:
1470 die("Corrupt mode: %s", command_buf
.buf
);
1475 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1476 hashcpy(sha1
, oe
->sha1
);
1479 if (get_sha1_hex(p
, sha1
))
1480 die("Invalid SHA1: %s", command_buf
.buf
);
1481 oe
= find_object(sha1
);
1485 die("Missing space after SHA1: %s", command_buf
.buf
);
1487 p_uq
= unquote_c_style(p
, &endp
);
1490 die("Garbage after path in: %s", command_buf
.buf
);
1495 if (oe
->type
!= OBJ_BLOB
)
1496 die("Not a blob (actually a %s): %s",
1497 command_buf
.buf
, type_names
[oe
->type
]);
1499 if (sha1_object_info(sha1
, type
, NULL
))
1500 die("Blob not found: %s", command_buf
.buf
);
1501 if (strcmp(blob_type
, type
))
1502 die("Not a blob (actually a %s): %s",
1503 command_buf
.buf
, type
);
1506 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1512 static void file_change_d(struct branch
*b
)
1514 const char *p
= command_buf
.buf
+ 2;
1518 p_uq
= unquote_c_style(p
, &endp
);
1521 die("Garbage after path in: %s", command_buf
.buf
);
1524 tree_content_remove(&b
->branch_tree
, p
);
1529 static void cmd_from(struct branch
*b
)
1531 const char *from
, *endp
;
1535 if (strncmp("from ", command_buf
.buf
, 5))
1539 die("Can't reinitailize branch %s", b
->name
);
1541 from
= strchr(command_buf
.buf
, ' ') + 1;
1542 str_uq
= unquote_c_style(from
, &endp
);
1545 die("Garbage after string in: %s", command_buf
.buf
);
1549 s
= lookup_branch(from
);
1551 die("Can't create a branch from itself: %s", b
->name
);
1553 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1554 hashcpy(b
->sha1
, s
->sha1
);
1555 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1556 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1557 } else if (*from
== ':') {
1558 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1559 struct object_entry
*oe
= find_mark(idnum
);
1562 if (oe
->type
!= OBJ_COMMIT
)
1563 die("Mark :%ju not a commit", idnum
);
1564 hashcpy(b
->sha1
, oe
->sha1
);
1565 buf
= gfi_unpack_entry(oe
, &size
);
1566 if (!buf
|| size
< 46)
1567 die("Not a valid commit: %s", from
);
1568 if (memcmp("tree ", buf
, 5)
1569 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1570 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1572 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1573 b
->branch_tree
.versions
[1].sha1
);
1574 } else if (!get_sha1(from
, b
->sha1
)) {
1575 if (is_null_sha1(b
->sha1
)) {
1576 hashclr(b
->branch_tree
.versions
[0].sha1
);
1577 hashclr(b
->branch_tree
.versions
[1].sha1
);
1582 buf
= read_object_with_reference(b
->sha1
,
1583 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1584 if (!buf
|| size
< 46)
1585 die("Not a valid commit: %s", from
);
1586 if (memcmp("tree ", buf
, 5)
1587 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1588 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1590 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1591 b
->branch_tree
.versions
[1].sha1
);
1594 die("Invalid ref name or SHA1 expression: %s", from
);
1596 read_next_command();
1599 static struct hash_list
* cmd_merge(unsigned int *count
)
1601 struct hash_list
*list
= NULL
, *n
, *e
;
1602 const char *from
, *endp
;
1607 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1608 from
= strchr(command_buf
.buf
, ' ') + 1;
1609 str_uq
= unquote_c_style(from
, &endp
);
1612 die("Garbage after string in: %s", command_buf
.buf
);
1616 n
= xmalloc(sizeof(*n
));
1617 s
= lookup_branch(from
);
1619 hashcpy(n
->sha1
, s
->sha1
);
1620 else if (*from
== ':') {
1621 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1622 struct object_entry
*oe
= find_mark(idnum
);
1623 if (oe
->type
!= OBJ_COMMIT
)
1624 die("Mark :%ju not a commit", idnum
);
1625 hashcpy(n
->sha1
, oe
->sha1
);
1626 } else if (get_sha1(from
, n
->sha1
))
1627 die("Invalid ref name or SHA1 expression: %s", from
);
1636 read_next_command();
1641 static void cmd_new_commit(void)
1649 char *author
= NULL
;
1650 char *committer
= NULL
;
1651 struct hash_list
*merge_list
= NULL
;
1652 unsigned int merge_count
;
1654 /* Obtain the branch name from the rest of our command */
1655 sp
= strchr(command_buf
.buf
, ' ') + 1;
1656 str_uq
= unquote_c_style(sp
, &endp
);
1659 die("Garbage after ref in: %s", command_buf
.buf
);
1662 b
= lookup_branch(sp
);
1668 read_next_command();
1670 if (!strncmp("author ", command_buf
.buf
, 7)) {
1671 author
= strdup(command_buf
.buf
);
1672 read_next_command();
1674 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1675 committer
= strdup(command_buf
.buf
);
1676 read_next_command();
1679 die("Expected committer but didn't get one");
1680 msg
= cmd_data(&msglen
);
1681 read_next_command();
1683 merge_list
= cmd_merge(&merge_count
);
1685 /* ensure the branch is active/loaded */
1686 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1687 unload_one_branch();
1693 if (1 == command_buf
.len
)
1695 else if (!strncmp("M ", command_buf
.buf
, 2))
1697 else if (!strncmp("D ", command_buf
.buf
, 2))
1700 die("Unsupported file_change: %s", command_buf
.buf
);
1701 read_next_command();
1704 /* build the tree and the commit */
1705 store_tree(&b
->branch_tree
);
1706 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1707 b
->branch_tree
.versions
[1].sha1
);
1708 size_dbuf(&new_data
, 97 + msglen
1711 ? strlen(author
) + strlen(committer
)
1712 : 2 * strlen(committer
)));
1713 sp
= new_data
.buffer
;
1714 sp
+= sprintf(sp
, "tree %s\n",
1715 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1716 if (!is_null_sha1(b
->sha1
))
1717 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1718 while (merge_list
) {
1719 struct hash_list
*next
= merge_list
->next
;
1720 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1725 sp
+= sprintf(sp
, "%s\n", author
);
1727 sp
+= sprintf(sp
, "author %s\n", committer
+ 10);
1728 sp
+= sprintf(sp
, "%s\n\n", committer
);
1729 memcpy(sp
, msg
, msglen
);
1736 if (!store_object(OBJ_COMMIT
,
1737 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1738 NULL
, b
->sha1
, next_mark
))
1739 b
->pack_id
= pack_id
;
1740 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1743 int need_dq
= quote_c_style(b
->name
, NULL
, NULL
, 0);
1744 fprintf(branch_log
, "commit ");
1746 fputc('"', branch_log
);
1747 quote_c_style(b
->name
, NULL
, branch_log
, 0);
1748 fputc('"', branch_log
);
1750 fprintf(branch_log
, "%s", b
->name
);
1751 fprintf(branch_log
," :%ju %s\n",next_mark
,sha1_to_hex(b
->sha1
));
1755 static void cmd_new_tag(void)
1766 uintmax_t from_mark
= 0;
1767 unsigned char sha1
[20];
1769 /* Obtain the new tag name from the rest of our command */
1770 sp
= strchr(command_buf
.buf
, ' ') + 1;
1771 str_uq
= unquote_c_style(sp
, &endp
);
1774 die("Garbage after tag name in: %s", command_buf
.buf
);
1777 t
= pool_alloc(sizeof(struct tag
));
1779 t
->name
= pool_strdup(sp
);
1781 last_tag
->next_tag
= t
;
1787 read_next_command();
1790 if (strncmp("from ", command_buf
.buf
, 5))
1791 die("Expected from command, got %s", command_buf
.buf
);
1793 from
= strchr(command_buf
.buf
, ' ') + 1;
1794 str_uq
= unquote_c_style(from
, &endp
);
1797 die("Garbage after string in: %s", command_buf
.buf
);
1801 s
= lookup_branch(from
);
1803 hashcpy(sha1
, s
->sha1
);
1804 } else if (*from
== ':') {
1805 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1806 struct object_entry
*oe
= find_mark(from_mark
);
1807 if (oe
->type
!= OBJ_COMMIT
)
1808 die("Mark :%ju not a commit", from_mark
);
1809 hashcpy(sha1
, oe
->sha1
);
1810 } else if (!get_sha1(from
, sha1
)) {
1814 buf
= read_object_with_reference(sha1
,
1815 type_names
[OBJ_COMMIT
], &size
, sha1
);
1816 if (!buf
|| size
< 46)
1817 die("Not a valid commit: %s", from
);
1820 die("Invalid ref name or SHA1 expression: %s", from
);
1824 read_next_command();
1827 if (strncmp("tagger ", command_buf
.buf
, 7))
1828 die("Expected tagger command, got %s", command_buf
.buf
);
1829 tagger
= strdup(command_buf
.buf
);
1831 /* tag payload/message */
1832 read_next_command();
1833 msg
= cmd_data(&msglen
);
1835 /* build the tag object */
1836 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1837 sp
= new_data
.buffer
;
1838 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1839 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1840 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1841 sp
+= sprintf(sp
, "%s\n\n", tagger
);
1842 memcpy(sp
, msg
, msglen
);
1847 if (store_object(OBJ_TAG
, new_data
.buffer
,
1848 sp
- (char*)new_data
.buffer
,
1850 t
->pack_id
= MAX_PACK_ID
;
1852 t
->pack_id
= pack_id
;
1855 int need_dq
= quote_c_style(t
->name
, NULL
, NULL
, 0);
1856 fprintf(branch_log
, "tag ");
1858 fputc('"', branch_log
);
1859 quote_c_style(t
->name
, NULL
, branch_log
, 0);
1860 fputc('"', branch_log
);
1862 fprintf(branch_log
, "%s", t
->name
);
1863 fprintf(branch_log
," :%ju %s\n",from_mark
,sha1_to_hex(t
->sha1
));
1867 static void cmd_reset_branch(void)
1874 /* Obtain the branch name from the rest of our command */
1875 sp
= strchr(command_buf
.buf
, ' ') + 1;
1876 str_uq
= unquote_c_style(sp
, &endp
);
1879 die("Garbage after ref in: %s", command_buf
.buf
);
1882 b
= lookup_branch(sp
);
1885 if (b
->branch_tree
.tree
) {
1886 release_tree_content_recursive(b
->branch_tree
.tree
);
1887 b
->branch_tree
.tree
= NULL
;
1894 read_next_command();
1898 static void cmd_checkpoint(void)
1902 read_next_command();
1905 static const char fast_import_usage
[] =
1906 "git-fast-import [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1908 int main(int argc
, const char **argv
)
1911 uintmax_t total_count
, duplicate_count
;
1914 git_config(git_default_config
);
1916 for (i
= 1; i
< argc
; i
++) {
1917 const char *a
= argv
[i
];
1919 if (*a
!= '-' || !strcmp(a
, "--"))
1921 else if (!strncmp(a
, "--max-pack-size=", 16))
1922 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1923 else if (!strncmp(a
, "--depth=", 8))
1924 max_depth
= strtoul(a
+ 8, NULL
, 0);
1925 else if (!strncmp(a
, "--active-branches=", 18))
1926 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
1927 else if (!strncmp(a
, "--export-marks=", 15))
1929 else if (!strncmp(a
, "--branch-log=", 13)) {
1930 branch_log
= fopen(a
+ 13, "w");
1932 die("Can't create %s: %s", a
+ 13, strerror(errno
));
1935 die("unknown option %s", a
);
1938 usage(fast_import_usage
);
1940 alloc_objects(object_entry_alloc
);
1941 strbuf_init(&command_buf
);
1943 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
1944 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
1945 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
1946 marks
= pool_calloc(1, sizeof(struct mark_set
));
1950 read_next_command();
1951 if (command_buf
.eof
)
1953 else if (!strcmp("blob", command_buf
.buf
))
1955 else if (!strncmp("commit ", command_buf
.buf
, 7))
1957 else if (!strncmp("tag ", command_buf
.buf
, 4))
1959 else if (!strncmp("reset ", command_buf
.buf
, 6))
1961 else if (!strcmp("checkpoint", command_buf
.buf
))
1964 die("Unsupported command: %s", command_buf
.buf
);
1976 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
1977 total_count
+= object_count_by_type
[i
];
1978 duplicate_count
= 0;
1979 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
1980 duplicate_count
+= duplicate_count_by_type
[i
];
1982 fprintf(stderr
, "%s statistics:\n", argv
[0]);
1983 fprintf(stderr
, "---------------------------------------------------------------------\n");
1984 fprintf(stderr
, "Alloc'd objects: %10ju\n", alloc_count
);
1985 fprintf(stderr
, "Total objects: %10ju (%10ju duplicates )\n", total_count
, duplicate_count
);
1986 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
]);
1987 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
]);
1988 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
]);
1989 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
]);
1990 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
1991 fprintf(stderr
, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
1992 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
1993 fprintf(stderr
, "Memory total: %10ju KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
1994 fprintf(stderr
, " pools: %10lu KiB\n", total_allocd
/1024);
1995 fprintf(stderr
, " objects: %10ju KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
1996 fprintf(stderr
, "---------------------------------------------------------------------\n");
1998 fprintf(stderr
, "---------------------------------------------------------------------\n");
1999 fprintf(stderr
, "\n");