2 Format of STDIN stream:
13 new_blob ::= 'blob' lf
16 file_content ::= data;
18 new_commit ::= 'commit' sp ref_str lf
20 ('author' sp name '<' email '>' when lf)?
21 'committer' sp name '<' email '>' when lf
23 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
24 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
29 file_change ::= file_clr | file_del | file_obm | file_inm;
30 file_clr ::= 'deleteall' lf;
31 file_del ::= 'D' sp path_str lf;
32 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
33 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
36 new_tag ::= 'tag' sp tag_str lf
37 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
38 'tagger' sp name '<' email '>' when lf
42 reset_branch ::= 'reset' sp ref_str lf
43 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
46 checkpoint ::= 'checkpoint' lf
49 # note: the first idnum in a stream should be 1 and subsequent
50 # idnums should not have gaps between values as this will cause
51 # the stream parser to reserve space for the gapped values. An
52 # idnum can be updated in the future to a new object by issuing
53 # a new mark directive with the old idnum.
55 mark ::= 'mark' sp idnum lf;
56 data ::= (delimited_data | exact_data)
59 # note: delim may be any string but must not contain lf.
60 # data_line may contain any data but must not be exactly
62 delimited_data ::= 'data' sp '<<' delim lf
66 # note: declen indicates the length of binary_data in bytes.
67 # declen does not include the lf preceeding the binary data.
69 exact_data ::= 'data' sp declen lf
72 # note: quoted strings are C-style quoting supporting \c for
73 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
74 # is the signed byte value in octal. Note that the only
75 # characters which must actually be escaped to protect the
76 # stream formatting is: \, " and LF. Otherwise these values
80 sha1exp_str ::= sha1exp;
82 path_str ::= path | '"' quoted(path) '"' ;
83 mode ::= '100644' | '644'
88 declen ::= # unsigned 32 bit value, ascii base10 notation;
89 bigint ::= # unsigned integer value, ascii base10 notation;
90 binary_data ::= # file content, not interpreted;
92 when ::= raw_when | rfc2822_when;
93 raw_when ::= ts sp tz;
94 rfc2822_when ::= # Valid RFC 2822 date and time;
96 sp ::= # ASCII space character;
97 lf ::= # ASCII newline (LF) character;
99 # note: a colon (':') must precede the numerical value assigned to
100 # an idnum. This is to distinguish it from a ref or tag name as
101 # GIT does not permit ':' in ref or tag strings.
103 idnum ::= ':' bigint;
104 path ::= # GIT style file path, e.g. "a/b/c";
105 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
106 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
107 sha1exp ::= # Any valid GIT SHA1 expression;
108 hexsha1 ::= # SHA1 in hexadecimal format;
110 # note: name and email are UTF8 strings, however name must not
111 # contain '<' or lf and email must not contain any of the
112 # following: '<', '>', lf.
114 name ::= # valid GIT author/committer name;
115 email ::= # valid GIT author/committer email;
116 ts ::= # time since the epoch in seconds, ascii base10 notation;
117 tz ::= # GIT style timezone;
129 #include "csum-file.h"
133 #define PACK_ID_BITS 16
134 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
136 #if !defined(NO_C99_FORMAT)
138 #define UM10_FMT "%10ju"
140 /* Assumes unsigned long long exists. */
141 #define UM_FMT "%llu"
142 #define UM10_FMT "%10llu"
147 struct object_entry
*next
;
149 unsigned type
: TYPE_BITS
;
150 unsigned pack_id
: PACK_ID_BITS
;
151 unsigned char sha1
[20];
154 struct object_entry_pool
156 struct object_entry_pool
*next_pool
;
157 struct object_entry
*next_free
;
158 struct object_entry
*end
;
159 struct object_entry entries
[FLEX_ARRAY
]; /* more */
165 struct object_entry
*marked
[1024];
166 struct mark_set
*sets
[1024];
182 struct mem_pool
*next_pool
;
185 char space
[FLEX_ARRAY
]; /* more */
190 struct atom_str
*next_atom
;
191 unsigned short str_len
;
192 char str_dat
[FLEX_ARRAY
]; /* more */
198 struct tree_content
*tree
;
199 struct atom_str
* name
;
203 unsigned char sha1
[20];
209 unsigned int entry_capacity
; /* must match avail_tree_content */
210 unsigned int entry_count
;
211 unsigned int delta_depth
;
212 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
215 struct avail_tree_content
217 unsigned int entry_capacity
; /* must match tree_content */
218 struct avail_tree_content
*next_avail
;
223 struct branch
*table_next_branch
;
224 struct branch
*active_next_branch
;
226 struct tree_entry branch_tree
;
227 uintmax_t last_commit
;
228 unsigned int pack_id
;
229 unsigned char sha1
[20];
234 struct tag
*next_tag
;
236 unsigned int pack_id
;
237 unsigned char sha1
[20];
248 struct hash_list
*next
;
249 unsigned char sha1
[20];
258 /* Configured limits on output */
259 static unsigned long max_depth
= 10;
260 static unsigned long max_packsize
= (1LL << 32) - 1;
261 static int force_update
;
263 /* Stats and misc. counters */
264 static uintmax_t alloc_count
;
265 static uintmax_t marks_set_count
;
266 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
267 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
268 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
269 static unsigned long object_count
;
270 static unsigned long branch_count
;
271 static unsigned long branch_load_count
;
273 static FILE *pack_edges
;
276 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
277 static size_t total_allocd
;
278 static struct mem_pool
*mem_pool
;
280 /* Atom management */
281 static unsigned int atom_table_sz
= 4451;
282 static unsigned int atom_cnt
;
283 static struct atom_str
**atom_table
;
285 /* The .pack file being generated */
286 static unsigned int pack_id
;
287 static struct packed_git
*pack_data
;
288 static struct packed_git
**all_packs
;
289 static unsigned long pack_size
;
291 /* Table of objects we've written. */
292 static unsigned int object_entry_alloc
= 5000;
293 static struct object_entry_pool
*blocks
;
294 static struct object_entry
*object_table
[1 << 16];
295 static struct mark_set
*marks
;
296 static const char* mark_file
;
299 static struct last_object last_blob
;
301 /* Tree management */
302 static unsigned int tree_entry_alloc
= 1000;
303 static void *avail_tree_entry
;
304 static unsigned int avail_tree_table_sz
= 100;
305 static struct avail_tree_content
**avail_tree_table
;
306 static struct dbuf old_tree
;
307 static struct dbuf new_tree
;
310 static unsigned long max_active_branches
= 5;
311 static unsigned long cur_active_branches
;
312 static unsigned long branch_table_sz
= 1039;
313 static struct branch
**branch_table
;
314 static struct branch
*active_branches
;
317 static struct tag
*first_tag
;
318 static struct tag
*last_tag
;
320 /* Input stream parsing */
321 static whenspec_type whenspec
= WHENSPEC_RAW
;
322 static struct strbuf command_buf
;
323 static uintmax_t next_mark
;
324 static struct dbuf new_data
;
327 static void alloc_objects(unsigned int cnt
)
329 struct object_entry_pool
*b
;
331 b
= xmalloc(sizeof(struct object_entry_pool
)
332 + cnt
* sizeof(struct object_entry
));
333 b
->next_pool
= blocks
;
334 b
->next_free
= b
->entries
;
335 b
->end
= b
->entries
+ cnt
;
340 static struct object_entry
*new_object(unsigned char *sha1
)
342 struct object_entry
*e
;
344 if (blocks
->next_free
== blocks
->end
)
345 alloc_objects(object_entry_alloc
);
347 e
= blocks
->next_free
++;
348 hashcpy(e
->sha1
, sha1
);
352 static struct object_entry
*find_object(unsigned char *sha1
)
354 unsigned int h
= sha1
[0] << 8 | sha1
[1];
355 struct object_entry
*e
;
356 for (e
= object_table
[h
]; e
; e
= e
->next
)
357 if (!hashcmp(sha1
, e
->sha1
))
362 static struct object_entry
*insert_object(unsigned char *sha1
)
364 unsigned int h
= sha1
[0] << 8 | sha1
[1];
365 struct object_entry
*e
= object_table
[h
];
366 struct object_entry
*p
= NULL
;
369 if (!hashcmp(sha1
, e
->sha1
))
375 e
= new_object(sha1
);
385 static unsigned int hc_str(const char *s
, size_t len
)
393 static void *pool_alloc(size_t len
)
398 for (p
= mem_pool
; p
; p
= p
->next_pool
)
399 if ((p
->end
- p
->next_free
>= len
))
403 if (len
>= (mem_pool_alloc
/2)) {
407 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
408 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
409 p
->next_pool
= mem_pool
;
410 p
->next_free
= p
->space
;
411 p
->end
= p
->next_free
+ mem_pool_alloc
;
416 /* round out to a pointer alignment */
417 if (len
& (sizeof(void*) - 1))
418 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
423 static void *pool_calloc(size_t count
, size_t size
)
425 size_t len
= count
* size
;
426 void *r
= pool_alloc(len
);
431 static char *pool_strdup(const char *s
)
433 char *r
= pool_alloc(strlen(s
) + 1);
438 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
441 if (b
->capacity
>= maxlen
)
445 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
446 b
->buffer
= xmalloc(b
->capacity
);
449 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
451 struct mark_set
*s
= marks
;
452 while ((idnum
>> s
->shift
) >= 1024) {
453 s
= pool_calloc(1, sizeof(struct mark_set
));
454 s
->shift
= marks
->shift
+ 10;
455 s
->data
.sets
[0] = marks
;
459 uintmax_t i
= idnum
>> s
->shift
;
460 idnum
-= i
<< s
->shift
;
461 if (!s
->data
.sets
[i
]) {
462 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
463 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
467 if (!s
->data
.marked
[idnum
])
469 s
->data
.marked
[idnum
] = oe
;
472 static struct object_entry
*find_mark(uintmax_t idnum
)
474 uintmax_t orig_idnum
= idnum
;
475 struct mark_set
*s
= marks
;
476 struct object_entry
*oe
= NULL
;
477 if ((idnum
>> s
->shift
) < 1024) {
478 while (s
&& s
->shift
) {
479 uintmax_t i
= idnum
>> s
->shift
;
480 idnum
-= i
<< s
->shift
;
484 oe
= s
->data
.marked
[idnum
];
487 die("mark :" UM_FMT
" not declared", orig_idnum
);
491 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
493 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
496 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
497 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
500 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
502 strncpy(c
->str_dat
, s
, len
);
504 c
->next_atom
= atom_table
[hc
];
510 static struct branch
*lookup_branch(const char *name
)
512 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
515 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
516 if (!strcmp(name
, b
->name
))
521 static struct branch
*new_branch(const char *name
)
523 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
524 struct branch
* b
= lookup_branch(name
);
527 die("Invalid attempt to create duplicate branch: %s", name
);
528 if (check_ref_format(name
))
529 die("Branch name doesn't conform to GIT standards: %s", name
);
531 b
= pool_calloc(1, sizeof(struct branch
));
532 b
->name
= pool_strdup(name
);
533 b
->table_next_branch
= branch_table
[hc
];
534 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
535 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
536 b
->pack_id
= MAX_PACK_ID
;
537 branch_table
[hc
] = b
;
542 static unsigned int hc_entries(unsigned int cnt
)
544 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
545 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
548 static struct tree_content
*new_tree_content(unsigned int cnt
)
550 struct avail_tree_content
*f
, *l
= NULL
;
551 struct tree_content
*t
;
552 unsigned int hc
= hc_entries(cnt
);
554 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
555 if (f
->entry_capacity
>= cnt
)
560 l
->next_avail
= f
->next_avail
;
562 avail_tree_table
[hc
] = f
->next_avail
;
564 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
565 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
566 f
->entry_capacity
= cnt
;
569 t
= (struct tree_content
*)f
;
575 static void release_tree_entry(struct tree_entry
*e
);
576 static void release_tree_content(struct tree_content
*t
)
578 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
579 unsigned int hc
= hc_entries(f
->entry_capacity
);
580 f
->next_avail
= avail_tree_table
[hc
];
581 avail_tree_table
[hc
] = f
;
584 static void release_tree_content_recursive(struct tree_content
*t
)
587 for (i
= 0; i
< t
->entry_count
; i
++)
588 release_tree_entry(t
->entries
[i
]);
589 release_tree_content(t
);
592 static struct tree_content
*grow_tree_content(
593 struct tree_content
*t
,
596 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
597 r
->entry_count
= t
->entry_count
;
598 r
->delta_depth
= t
->delta_depth
;
599 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
600 release_tree_content(t
);
604 static struct tree_entry
*new_tree_entry(void)
606 struct tree_entry
*e
;
608 if (!avail_tree_entry
) {
609 unsigned int n
= tree_entry_alloc
;
610 total_allocd
+= n
* sizeof(struct tree_entry
);
611 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
613 *((void**)e
) = e
+ 1;
619 e
= avail_tree_entry
;
620 avail_tree_entry
= *((void**)e
);
624 static void release_tree_entry(struct tree_entry
*e
)
627 release_tree_content_recursive(e
->tree
);
628 *((void**)e
) = avail_tree_entry
;
629 avail_tree_entry
= e
;
632 static void start_packfile(void)
634 static char tmpfile
[PATH_MAX
];
635 struct packed_git
*p
;
636 struct pack_header hdr
;
639 snprintf(tmpfile
, sizeof(tmpfile
),
640 "%s/pack_XXXXXX", get_object_directory());
641 pack_fd
= mkstemp(tmpfile
);
643 die("Can't create %s: %s", tmpfile
, strerror(errno
));
644 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
645 strcpy(p
->pack_name
, tmpfile
);
646 p
->pack_fd
= pack_fd
;
648 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
649 hdr
.hdr_version
= htonl(2);
651 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
654 pack_size
= sizeof(hdr
);
657 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
658 all_packs
[pack_id
] = p
;
661 static void fixup_header_footer(void)
663 static const int buf_sz
= 128 * 1024;
664 int pack_fd
= pack_data
->pack_fd
;
666 struct pack_header hdr
;
669 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
670 die("Failed seeking to start: %s", strerror(errno
));
671 if (read_in_full(pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
672 die("Unable to reread header of %s", pack_data
->pack_name
);
673 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
674 die("Failed seeking to start: %s", strerror(errno
));
675 hdr
.hdr_entries
= htonl(object_count
);
676 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
679 SHA1_Update(&c
, &hdr
, sizeof(hdr
));
681 buf
= xmalloc(buf_sz
);
683 size_t n
= xread(pack_fd
, buf
, buf_sz
);
687 die("Failed to checksum %s", pack_data
->pack_name
);
688 SHA1_Update(&c
, buf
, n
);
692 SHA1_Final(pack_data
->sha1
, &c
);
693 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
697 static int oecmp (const void *a_
, const void *b_
)
699 struct object_entry
*a
= *((struct object_entry
**)a_
);
700 struct object_entry
*b
= *((struct object_entry
**)b_
);
701 return hashcmp(a
->sha1
, b
->sha1
);
704 static char *create_index(void)
706 static char tmpfile
[PATH_MAX
];
709 struct object_entry
**idx
, **c
, **last
, *e
;
710 struct object_entry_pool
*o
;
714 /* Build the sorted table of object IDs. */
715 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
717 for (o
= blocks
; o
; o
= o
->next_pool
)
718 for (e
= o
->next_free
; e
-- != o
->entries
;)
719 if (pack_id
== e
->pack_id
)
721 last
= idx
+ object_count
;
723 die("internal consistency error creating the index");
724 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
726 /* Generate the fan-out array. */
728 for (i
= 0; i
< 256; i
++) {
729 struct object_entry
**next
= c
;;
730 while (next
< last
) {
731 if ((*next
)->sha1
[0] != i
)
735 array
[i
] = htonl(next
- idx
);
739 snprintf(tmpfile
, sizeof(tmpfile
),
740 "%s/index_XXXXXX", get_object_directory());
741 idx_fd
= mkstemp(tmpfile
);
743 die("Can't create %s: %s", tmpfile
, strerror(errno
));
744 f
= sha1fd(idx_fd
, tmpfile
);
745 sha1write(f
, array
, 256 * sizeof(int));
747 for (c
= idx
; c
!= last
; c
++) {
748 uint32_t offset
= htonl((*c
)->offset
);
749 sha1write(f
, &offset
, 4);
750 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
751 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
753 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
754 sha1close(f
, NULL
, 1);
756 SHA1_Final(pack_data
->sha1
, &ctx
);
760 static char *keep_pack(char *curr_index_name
)
762 static char name
[PATH_MAX
];
763 static char *keep_msg
= "fast-import";
766 chmod(pack_data
->pack_name
, 0444);
767 chmod(curr_index_name
, 0444);
769 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
770 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
771 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
773 die("cannot create keep file");
774 write(keep_fd
, keep_msg
, strlen(keep_msg
));
777 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
778 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
779 if (move_temp_to_file(pack_data
->pack_name
, name
))
780 die("cannot store pack file");
782 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
783 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
784 if (move_temp_to_file(curr_index_name
, name
))
785 die("cannot store index file");
789 static void unkeep_all_packs(void)
791 static char name
[PATH_MAX
];
794 for (k
= 0; k
< pack_id
; k
++) {
795 struct packed_git
*p
= all_packs
[k
];
796 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
797 get_object_directory(), sha1_to_hex(p
->sha1
));
802 static void end_packfile(void)
804 struct packed_git
*old_p
= pack_data
, *new_p
;
812 fixup_header_footer();
813 idx_name
= keep_pack(create_index());
815 /* Register the packfile with core git's machinary. */
816 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
818 die("core git rejected index %s", idx_name
);
819 new_p
->windows
= old_p
->windows
;
820 all_packs
[pack_id
] = new_p
;
821 install_packed_git(new_p
);
823 /* Print the boundary */
825 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
826 for (i
= 0; i
< branch_table_sz
; i
++) {
827 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
828 if (b
->pack_id
== pack_id
)
829 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
832 for (t
= first_tag
; t
; t
= t
->next_tag
) {
833 if (t
->pack_id
== pack_id
)
834 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
836 fputc('\n', pack_edges
);
843 unlink(old_p
->pack_name
);
846 /* We can't carry a delta across packfiles. */
847 free(last_blob
.data
);
848 last_blob
.data
= NULL
;
850 last_blob
.offset
= 0;
854 static void cycle_packfile(void)
860 static size_t encode_header(
861 enum object_type type
,
868 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
869 die("bad type %d", type
);
871 c
= (type
<< 4) | (size
& 15);
883 static int store_object(
884 enum object_type type
,
887 struct last_object
*last
,
888 unsigned char *sha1out
,
892 struct object_entry
*e
;
893 unsigned char hdr
[96];
894 unsigned char sha1
[20];
895 unsigned long hdrlen
, deltalen
;
899 hdrlen
= sprintf((char*)hdr
,"%s %lu", type_names
[type
],
900 (unsigned long)datlen
) + 1;
902 SHA1_Update(&c
, hdr
, hdrlen
);
903 SHA1_Update(&c
, dat
, datlen
);
904 SHA1_Final(sha1
, &c
);
906 hashcpy(sha1out
, sha1
);
908 e
= insert_object(sha1
);
910 insert_mark(mark
, e
);
912 duplicate_count_by_type
[type
]++;
916 if (last
&& last
->data
&& last
->depth
< max_depth
) {
917 delta
= diff_delta(last
->data
, last
->len
,
920 if (delta
&& deltalen
>= datlen
) {
927 memset(&s
, 0, sizeof(s
));
928 deflateInit(&s
, zlib_compression_level
);
931 s
.avail_in
= deltalen
;
936 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
937 s
.next_out
= out
= xmalloc(s
.avail_out
);
938 while (deflate(&s
, Z_FINISH
) == Z_OK
)
942 /* Determine if we should auto-checkpoint. */
943 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
944 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
946 /* This new object needs to *not* have the current pack_id. */
947 e
->pack_id
= pack_id
+ 1;
950 /* We cannot carry a delta into the new pack. */
955 memset(&s
, 0, sizeof(s
));
956 deflateInit(&s
, zlib_compression_level
);
959 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
960 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
961 while (deflate(&s
, Z_FINISH
) == Z_OK
)
968 e
->pack_id
= pack_id
;
969 e
->offset
= pack_size
;
971 object_count_by_type
[type
]++;
974 unsigned long ofs
= e
->offset
- last
->offset
;
975 unsigned pos
= sizeof(hdr
) - 1;
977 delta_count_by_type
[type
]++;
980 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
981 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
984 hdr
[pos
] = ofs
& 127;
986 hdr
[--pos
] = 128 | (--ofs
& 127);
987 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
988 pack_size
+= sizeof(hdr
) - pos
;
992 hdrlen
= encode_header(type
, datlen
, hdr
);
993 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
997 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
998 pack_size
+= s
.total_out
;
1006 last
->offset
= e
->offset
;
1012 static void *gfi_unpack_entry(
1013 struct object_entry
*oe
,
1014 unsigned long *sizep
)
1016 static char type
[20];
1017 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1019 p
->pack_size
= pack_size
+ 20;
1020 return unpack_entry(p
, oe
->offset
, type
, sizep
);
1023 static const char *get_mode(const char *str
, uint16_t *modep
)
1028 while ((c
= *str
++) != ' ') {
1029 if (c
< '0' || c
> '7')
1031 mode
= (mode
<< 3) + (c
- '0');
1037 static void load_tree(struct tree_entry
*root
)
1039 unsigned char* sha1
= root
->versions
[1].sha1
;
1040 struct object_entry
*myoe
;
1041 struct tree_content
*t
;
1046 root
->tree
= t
= new_tree_content(8);
1047 if (is_null_sha1(sha1
))
1050 myoe
= find_object(sha1
);
1052 if (myoe
->type
!= OBJ_TREE
)
1053 die("Not a tree: %s", sha1_to_hex(sha1
));
1055 buf
= gfi_unpack_entry(myoe
, &size
);
1058 buf
= read_sha1_file(sha1
, type
, &size
);
1059 if (!buf
|| strcmp(type
, tree_type
))
1060 die("Can't load tree %s", sha1_to_hex(sha1
));
1064 while (c
!= (buf
+ size
)) {
1065 struct tree_entry
*e
= new_tree_entry();
1067 if (t
->entry_count
== t
->entry_capacity
)
1068 root
->tree
= t
= grow_tree_content(t
, 8);
1069 t
->entries
[t
->entry_count
++] = e
;
1072 c
= get_mode(c
, &e
->versions
[1].mode
);
1074 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1075 e
->versions
[0].mode
= e
->versions
[1].mode
;
1076 e
->name
= to_atom(c
, (unsigned short)strlen(c
));
1077 c
+= e
->name
->str_len
+ 1;
1078 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1079 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1085 static int tecmp0 (const void *_a
, const void *_b
)
1087 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1088 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1089 return base_name_compare(
1090 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1091 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1094 static int tecmp1 (const void *_a
, const void *_b
)
1096 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1097 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1098 return base_name_compare(
1099 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1100 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1103 static void mktree(struct tree_content
*t
,
1113 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1115 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1117 for (i
= 0; i
< t
->entry_count
; i
++) {
1118 if (t
->entries
[i
]->versions
[v
].mode
)
1119 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1122 size_dbuf(b
, maxlen
);
1124 for (i
= 0; i
< t
->entry_count
; i
++) {
1125 struct tree_entry
*e
= t
->entries
[i
];
1126 if (!e
->versions
[v
].mode
)
1128 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1130 strcpy(c
, e
->name
->str_dat
);
1131 c
+= e
->name
->str_len
+ 1;
1132 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1135 *szp
= c
- (char*)b
->buffer
;
1138 static void store_tree(struct tree_entry
*root
)
1140 struct tree_content
*t
= root
->tree
;
1141 unsigned int i
, j
, del
;
1142 unsigned long new_len
;
1143 struct last_object lo
;
1144 struct object_entry
*le
;
1146 if (!is_null_sha1(root
->versions
[1].sha1
))
1149 for (i
= 0; i
< t
->entry_count
; i
++) {
1150 if (t
->entries
[i
]->tree
)
1151 store_tree(t
->entries
[i
]);
1154 le
= find_object(root
->versions
[0].sha1
);
1155 if (!S_ISDIR(root
->versions
[0].mode
)
1157 || le
->pack_id
!= pack_id
) {
1161 mktree(t
, 0, &lo
.len
, &old_tree
);
1162 lo
.data
= old_tree
.buffer
;
1163 lo
.offset
= le
->offset
;
1164 lo
.depth
= t
->delta_depth
;
1168 mktree(t
, 1, &new_len
, &new_tree
);
1169 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1170 &lo
, root
->versions
[1].sha1
, 0);
1172 t
->delta_depth
= lo
.depth
;
1173 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1174 struct tree_entry
*e
= t
->entries
[i
];
1175 if (e
->versions
[1].mode
) {
1176 e
->versions
[0].mode
= e
->versions
[1].mode
;
1177 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1178 t
->entries
[j
++] = e
;
1180 release_tree_entry(e
);
1184 t
->entry_count
-= del
;
1187 static int tree_content_set(
1188 struct tree_entry
*root
,
1190 const unsigned char *sha1
,
1191 const uint16_t mode
)
1193 struct tree_content
*t
= root
->tree
;
1196 struct tree_entry
*e
;
1198 slash1
= strchr(p
, '/');
1204 for (i
= 0; i
< t
->entry_count
; i
++) {
1206 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1208 if (e
->versions
[1].mode
== mode
1209 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1211 e
->versions
[1].mode
= mode
;
1212 hashcpy(e
->versions
[1].sha1
, sha1
);
1214 release_tree_content_recursive(e
->tree
);
1217 hashclr(root
->versions
[1].sha1
);
1220 if (!S_ISDIR(e
->versions
[1].mode
)) {
1221 e
->tree
= new_tree_content(8);
1222 e
->versions
[1].mode
= S_IFDIR
;
1226 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1227 hashclr(root
->versions
[1].sha1
);
1234 if (t
->entry_count
== t
->entry_capacity
)
1235 root
->tree
= t
= grow_tree_content(t
, 8);
1236 e
= new_tree_entry();
1237 e
->name
= to_atom(p
, (unsigned short)n
);
1238 e
->versions
[0].mode
= 0;
1239 hashclr(e
->versions
[0].sha1
);
1240 t
->entries
[t
->entry_count
++] = e
;
1242 e
->tree
= new_tree_content(8);
1243 e
->versions
[1].mode
= S_IFDIR
;
1244 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1247 e
->versions
[1].mode
= mode
;
1248 hashcpy(e
->versions
[1].sha1
, sha1
);
1250 hashclr(root
->versions
[1].sha1
);
1254 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1256 struct tree_content
*t
= root
->tree
;
1259 struct tree_entry
*e
;
1261 slash1
= strchr(p
, '/');
1267 for (i
= 0; i
< t
->entry_count
; i
++) {
1269 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1270 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1274 if (tree_content_remove(e
, slash1
+ 1)) {
1275 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1276 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1277 hashclr(root
->versions
[1].sha1
);
1290 release_tree_content_recursive(e
->tree
);
1293 e
->versions
[1].mode
= 0;
1294 hashclr(e
->versions
[1].sha1
);
1295 hashclr(root
->versions
[1].sha1
);
1299 static int update_branch(struct branch
*b
)
1301 static const char *msg
= "fast-import";
1302 struct ref_lock
*lock
;
1303 unsigned char old_sha1
[20];
1305 if (read_ref(b
->name
, old_sha1
))
1307 lock
= lock_any_ref_for_update(b
->name
, old_sha1
);
1309 return error("Unable to lock %s", b
->name
);
1310 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1311 struct commit
*old_cmit
, *new_cmit
;
1313 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1314 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1315 if (!old_cmit
|| !new_cmit
) {
1317 return error("Branch %s is missing commits.", b
->name
);
1320 if (!in_merge_bases(old_cmit
, new_cmit
)) {
1322 warn("Not updating %s"
1323 " (new tip %s does not contain %s)",
1324 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1328 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1329 return error("Unable to update %s", b
->name
);
1333 static void dump_branches(void)
1338 for (i
= 0; i
< branch_table_sz
; i
++) {
1339 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1340 failure
|= update_branch(b
);
1344 static void dump_tags(void)
1346 static const char *msg
= "fast-import";
1348 struct ref_lock
*lock
;
1349 char ref_name
[PATH_MAX
];
1351 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1352 sprintf(ref_name
, "tags/%s", t
->name
);
1353 lock
= lock_ref_sha1(ref_name
, NULL
);
1354 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1355 failure
|= error("Unable to update %s", ref_name
);
1359 static void dump_marks_helper(FILE *f
,
1365 for (k
= 0; k
< 1024; k
++) {
1366 if (m
->data
.sets
[k
])
1367 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1371 for (k
= 0; k
< 1024; k
++) {
1372 if (m
->data
.marked
[k
])
1373 fprintf(f
, ":" UM_FMT
" %s\n", base
+ k
,
1374 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1379 static void dump_marks(void)
1383 FILE *f
= fopen(mark_file
, "w");
1385 dump_marks_helper(f
, 0, marks
);
1388 failure
|= error("Unable to write marks file %s: %s",
1389 mark_file
, strerror(errno
));
1393 static void read_next_command(void)
1395 read_line(&command_buf
, stdin
, '\n');
1398 static void cmd_mark(void)
1400 if (!strncmp("mark :", command_buf
.buf
, 6)) {
1401 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1402 read_next_command();
1408 static void *cmd_data (size_t *size
)
1413 if (strncmp("data ", command_buf
.buf
, 5))
1414 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1416 if (!strncmp("<<", command_buf
.buf
+ 5, 2)) {
1417 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1418 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1420 buffer
= xmalloc(sz
);
1422 read_next_command();
1423 if (command_buf
.eof
)
1424 die("EOF in data (terminator '%s' not found)", term
);
1425 if (term_len
== command_buf
.len
1426 && !strcmp(term
, command_buf
.buf
))
1428 if (sz
< (length
+ command_buf
.len
)) {
1429 sz
= sz
* 3 / 2 + 16;
1430 if (sz
< (length
+ command_buf
.len
))
1431 sz
= length
+ command_buf
.len
;
1432 buffer
= xrealloc(buffer
, sz
);
1434 memcpy(buffer
+ length
,
1436 command_buf
.len
- 1);
1437 length
+= command_buf
.len
- 1;
1438 buffer
[length
++] = '\n';
1444 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1445 buffer
= xmalloc(length
);
1446 while (n
< length
) {
1447 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1448 if (!s
&& feof(stdin
))
1449 die("EOF in data (%lu bytes remaining)",
1450 (unsigned long)(length
- n
));
1455 if (fgetc(stdin
) != '\n')
1456 die("An lf did not trail the binary data as expected.");
1462 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1464 const char *orig_src
= src
;
1467 strtoul(src
, &endp
, 10);
1468 if (endp
== src
|| *endp
!= ' ')
1472 if (*src
!= '-' && *src
!= '+')
1476 strtoul(src
+ 1, &endp
, 10);
1477 if (endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1480 strcpy(result
, orig_src
);
1484 static char *parse_ident(const char *buf
)
1490 gt
= strrchr(buf
, '>');
1492 die("Missing > in ident string: %s", buf
);
1495 die("Missing space after > in ident string: %s", buf
);
1497 name_len
= gt
- buf
;
1498 ident
= xmalloc(name_len
+ 24);
1499 strncpy(ident
, buf
, name_len
);
1503 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1504 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1506 case WHENSPEC_RFC2822
:
1507 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1508 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1511 if (strcmp("now", gt
))
1512 die("Date in ident must be 'now': %s", buf
);
1513 datestamp(ident
+ name_len
, 24);
1520 static void cmd_new_blob(void)
1525 read_next_command();
1529 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1533 static void unload_one_branch(void)
1535 while (cur_active_branches
1536 && cur_active_branches
>= max_active_branches
) {
1537 unsigned long min_commit
= ULONG_MAX
;
1538 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1540 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1541 if (e
->last_commit
< min_commit
) {
1543 min_commit
= e
->last_commit
;
1549 e
= p
->active_next_branch
;
1550 p
->active_next_branch
= e
->active_next_branch
;
1552 e
= active_branches
;
1553 active_branches
= e
->active_next_branch
;
1555 e
->active_next_branch
= NULL
;
1556 if (e
->branch_tree
.tree
) {
1557 release_tree_content_recursive(e
->branch_tree
.tree
);
1558 e
->branch_tree
.tree
= NULL
;
1560 cur_active_branches
--;
1564 static void load_branch(struct branch
*b
)
1566 load_tree(&b
->branch_tree
);
1567 b
->active_next_branch
= active_branches
;
1568 active_branches
= b
;
1569 cur_active_branches
++;
1570 branch_load_count
++;
1573 static void file_change_m(struct branch
*b
)
1575 const char *p
= command_buf
.buf
+ 2;
1578 struct object_entry
*oe
= oe
;
1579 unsigned char sha1
[20];
1580 uint16_t mode
, inline_data
= 0;
1583 p
= get_mode(p
, &mode
);
1585 die("Corrupt mode: %s", command_buf
.buf
);
1587 case S_IFREG
| 0644:
1588 case S_IFREG
| 0755:
1595 die("Corrupt mode: %s", command_buf
.buf
);
1600 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1601 hashcpy(sha1
, oe
->sha1
);
1603 } else if (!strncmp("inline", p
, 6)) {
1607 if (get_sha1_hex(p
, sha1
))
1608 die("Invalid SHA1: %s", command_buf
.buf
);
1609 oe
= find_object(sha1
);
1613 die("Missing space after SHA1: %s", command_buf
.buf
);
1615 p_uq
= unquote_c_style(p
, &endp
);
1618 die("Garbage after path in: %s", command_buf
.buf
);
1626 p
= p_uq
= xstrdup(p
);
1627 read_next_command();
1629 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1632 if (oe
->type
!= OBJ_BLOB
)
1633 die("Not a blob (actually a %s): %s",
1634 command_buf
.buf
, type_names
[oe
->type
]);
1636 if (sha1_object_info(sha1
, type
, NULL
))
1637 die("Blob not found: %s", command_buf
.buf
);
1638 if (strcmp(blob_type
, type
))
1639 die("Not a blob (actually a %s): %s",
1640 command_buf
.buf
, type
);
1643 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1647 static void file_change_d(struct branch
*b
)
1649 const char *p
= command_buf
.buf
+ 2;
1653 p_uq
= unquote_c_style(p
, &endp
);
1656 die("Garbage after path in: %s", command_buf
.buf
);
1659 tree_content_remove(&b
->branch_tree
, p
);
1663 static void file_change_deleteall(struct branch
*b
)
1665 release_tree_content_recursive(b
->branch_tree
.tree
);
1666 hashclr(b
->branch_tree
.versions
[0].sha1
);
1667 hashclr(b
->branch_tree
.versions
[1].sha1
);
1668 load_tree(&b
->branch_tree
);
1671 static void cmd_from(struct branch
*b
)
1676 if (strncmp("from ", command_buf
.buf
, 5))
1679 if (b
->branch_tree
.tree
) {
1680 release_tree_content_recursive(b
->branch_tree
.tree
);
1681 b
->branch_tree
.tree
= NULL
;
1684 from
= strchr(command_buf
.buf
, ' ') + 1;
1685 s
= lookup_branch(from
);
1687 die("Can't create a branch from itself: %s", b
->name
);
1689 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1690 hashcpy(b
->sha1
, s
->sha1
);
1691 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1692 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1693 } else if (*from
== ':') {
1694 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1695 struct object_entry
*oe
= find_mark(idnum
);
1698 if (oe
->type
!= OBJ_COMMIT
)
1699 die("Mark :" UM_FMT
" not a commit", idnum
);
1700 hashcpy(b
->sha1
, oe
->sha1
);
1701 buf
= gfi_unpack_entry(oe
, &size
);
1702 if (!buf
|| size
< 46)
1703 die("Not a valid commit: %s", from
);
1704 if (memcmp("tree ", buf
, 5)
1705 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1706 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1708 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1709 b
->branch_tree
.versions
[1].sha1
);
1710 } else if (!get_sha1(from
, b
->sha1
)) {
1711 if (is_null_sha1(b
->sha1
)) {
1712 hashclr(b
->branch_tree
.versions
[0].sha1
);
1713 hashclr(b
->branch_tree
.versions
[1].sha1
);
1718 buf
= read_object_with_reference(b
->sha1
,
1719 type_names
[OBJ_COMMIT
], &size
, b
->sha1
);
1720 if (!buf
|| size
< 46)
1721 die("Not a valid commit: %s", from
);
1722 if (memcmp("tree ", buf
, 5)
1723 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1724 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1726 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1727 b
->branch_tree
.versions
[1].sha1
);
1730 die("Invalid ref name or SHA1 expression: %s", from
);
1732 read_next_command();
1735 static struct hash_list
*cmd_merge(unsigned int *count
)
1737 struct hash_list
*list
= NULL
, *n
, *e
= e
;
1742 while (!strncmp("merge ", command_buf
.buf
, 6)) {
1743 from
= strchr(command_buf
.buf
, ' ') + 1;
1744 n
= xmalloc(sizeof(*n
));
1745 s
= lookup_branch(from
);
1747 hashcpy(n
->sha1
, s
->sha1
);
1748 else if (*from
== ':') {
1749 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1750 struct object_entry
*oe
= find_mark(idnum
);
1751 if (oe
->type
!= OBJ_COMMIT
)
1752 die("Mark :" UM_FMT
" not a commit", idnum
);
1753 hashcpy(n
->sha1
, oe
->sha1
);
1754 } else if (get_sha1(from
, n
->sha1
))
1755 die("Invalid ref name or SHA1 expression: %s", from
);
1764 read_next_command();
1769 static void cmd_new_commit(void)
1775 char *author
= NULL
;
1776 char *committer
= NULL
;
1777 struct hash_list
*merge_list
= NULL
;
1778 unsigned int merge_count
;
1780 /* Obtain the branch name from the rest of our command */
1781 sp
= strchr(command_buf
.buf
, ' ') + 1;
1782 b
= lookup_branch(sp
);
1786 read_next_command();
1788 if (!strncmp("author ", command_buf
.buf
, 7)) {
1789 author
= parse_ident(command_buf
.buf
+ 7);
1790 read_next_command();
1792 if (!strncmp("committer ", command_buf
.buf
, 10)) {
1793 committer
= parse_ident(command_buf
.buf
+ 10);
1794 read_next_command();
1797 die("Expected committer but didn't get one");
1798 msg
= cmd_data(&msglen
);
1799 read_next_command();
1801 merge_list
= cmd_merge(&merge_count
);
1803 /* ensure the branch is active/loaded */
1804 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1805 unload_one_branch();
1811 if (1 == command_buf
.len
)
1813 else if (!strncmp("M ", command_buf
.buf
, 2))
1815 else if (!strncmp("D ", command_buf
.buf
, 2))
1817 else if (!strcmp("deleteall", command_buf
.buf
))
1818 file_change_deleteall(b
);
1820 die("Unsupported file_change: %s", command_buf
.buf
);
1821 read_next_command();
1824 /* build the tree and the commit */
1825 store_tree(&b
->branch_tree
);
1826 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1827 b
->branch_tree
.versions
[1].sha1
);
1828 size_dbuf(&new_data
, 114 + msglen
1831 ? strlen(author
) + strlen(committer
)
1832 : 2 * strlen(committer
)));
1833 sp
= new_data
.buffer
;
1834 sp
+= sprintf(sp
, "tree %s\n",
1835 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1836 if (!is_null_sha1(b
->sha1
))
1837 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1838 while (merge_list
) {
1839 struct hash_list
*next
= merge_list
->next
;
1840 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1844 sp
+= sprintf(sp
, "author %s\n", author
? author
: committer
);
1845 sp
+= sprintf(sp
, "committer %s\n", committer
);
1847 memcpy(sp
, msg
, msglen
);
1853 if (!store_object(OBJ_COMMIT
,
1854 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1855 NULL
, b
->sha1
, next_mark
))
1856 b
->pack_id
= pack_id
;
1857 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1860 static void cmd_new_tag(void)
1869 uintmax_t from_mark
= 0;
1870 unsigned char sha1
[20];
1872 /* Obtain the new tag name from the rest of our command */
1873 sp
= strchr(command_buf
.buf
, ' ') + 1;
1874 t
= pool_alloc(sizeof(struct tag
));
1876 t
->name
= pool_strdup(sp
);
1878 last_tag
->next_tag
= t
;
1882 read_next_command();
1885 if (strncmp("from ", command_buf
.buf
, 5))
1886 die("Expected from command, got %s", command_buf
.buf
);
1887 from
= strchr(command_buf
.buf
, ' ') + 1;
1888 s
= lookup_branch(from
);
1890 hashcpy(sha1
, s
->sha1
);
1891 } else if (*from
== ':') {
1892 struct object_entry
*oe
;
1893 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1894 oe
= find_mark(from_mark
);
1895 if (oe
->type
!= OBJ_COMMIT
)
1896 die("Mark :" UM_FMT
" not a commit", from_mark
);
1897 hashcpy(sha1
, oe
->sha1
);
1898 } else if (!get_sha1(from
, sha1
)) {
1902 buf
= read_object_with_reference(sha1
,
1903 type_names
[OBJ_COMMIT
], &size
, sha1
);
1904 if (!buf
|| size
< 46)
1905 die("Not a valid commit: %s", from
);
1908 die("Invalid ref name or SHA1 expression: %s", from
);
1909 read_next_command();
1912 if (strncmp("tagger ", command_buf
.buf
, 7))
1913 die("Expected tagger command, got %s", command_buf
.buf
);
1914 tagger
= parse_ident(command_buf
.buf
+ 7);
1916 /* tag payload/message */
1917 read_next_command();
1918 msg
= cmd_data(&msglen
);
1920 /* build the tag object */
1921 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1922 sp
= new_data
.buffer
;
1923 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1924 sp
+= sprintf(sp
, "type %s\n", type_names
[OBJ_COMMIT
]);
1925 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1926 sp
+= sprintf(sp
, "tagger %s\n", tagger
);
1928 memcpy(sp
, msg
, msglen
);
1933 if (store_object(OBJ_TAG
, new_data
.buffer
,
1934 sp
- (char*)new_data
.buffer
,
1936 t
->pack_id
= MAX_PACK_ID
;
1938 t
->pack_id
= pack_id
;
1941 static void cmd_reset_branch(void)
1946 /* Obtain the branch name from the rest of our command */
1947 sp
= strchr(command_buf
.buf
, ' ') + 1;
1948 b
= lookup_branch(sp
);
1951 hashclr(b
->branch_tree
.versions
[0].sha1
);
1952 hashclr(b
->branch_tree
.versions
[1].sha1
);
1953 if (b
->branch_tree
.tree
) {
1954 release_tree_content_recursive(b
->branch_tree
.tree
);
1955 b
->branch_tree
.tree
= NULL
;
1960 read_next_command();
1964 static void cmd_checkpoint(void)
1972 read_next_command();
1975 static const char fast_import_usage
[] =
1976 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
1978 int main(int argc
, const char **argv
)
1980 int i
, show_stats
= 1;
1982 git_config(git_default_config
);
1984 for (i
= 1; i
< argc
; i
++) {
1985 const char *a
= argv
[i
];
1987 if (*a
!= '-' || !strcmp(a
, "--"))
1989 else if (!strncmp(a
, "--date-format=", 14)) {
1990 const char *fmt
= a
+ 14;
1991 if (!strcmp(fmt
, "raw"))
1992 whenspec
= WHENSPEC_RAW
;
1993 else if (!strcmp(fmt
, "rfc2822"))
1994 whenspec
= WHENSPEC_RFC2822
;
1995 else if (!strcmp(fmt
, "now"))
1996 whenspec
= WHENSPEC_NOW
;
1998 die("unknown --date-format argument %s", fmt
);
2000 else if (!strncmp(a
, "--max-pack-size=", 16))
2001 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
2002 else if (!strncmp(a
, "--depth=", 8))
2003 max_depth
= strtoul(a
+ 8, NULL
, 0);
2004 else if (!strncmp(a
, "--active-branches=", 18))
2005 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
2006 else if (!strncmp(a
, "--export-marks=", 15))
2008 else if (!strncmp(a
, "--export-pack-edges=", 20)) {
2011 pack_edges
= fopen(a
+ 20, "a");
2013 die("Cannot open %s: %s", a
+ 20, strerror(errno
));
2014 } else if (!strcmp(a
, "--force"))
2016 else if (!strcmp(a
, "--quiet"))
2018 else if (!strcmp(a
, "--stats"))
2021 die("unknown option %s", a
);
2024 usage(fast_import_usage
);
2026 alloc_objects(object_entry_alloc
);
2027 strbuf_init(&command_buf
);
2029 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2030 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2031 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2032 marks
= pool_calloc(1, sizeof(struct mark_set
));
2036 read_next_command();
2037 if (command_buf
.eof
)
2039 else if (!strcmp("blob", command_buf
.buf
))
2041 else if (!strncmp("commit ", command_buf
.buf
, 7))
2043 else if (!strncmp("tag ", command_buf
.buf
, 4))
2045 else if (!strncmp("reset ", command_buf
.buf
, 6))
2047 else if (!strcmp("checkpoint", command_buf
.buf
))
2050 die("Unsupported command: %s", command_buf
.buf
);
2063 uintmax_t total_count
= 0, duplicate_count
= 0;
2064 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2065 total_count
+= object_count_by_type
[i
];
2066 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2067 duplicate_count
+= duplicate_count_by_type
[i
];
2069 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2070 fprintf(stderr
, "---------------------------------------------------------------------\n");
2071 fprintf(stderr
, "Alloc'd objects: " UM10_FMT
"\n", alloc_count
);
2072 fprintf(stderr
, "Total objects: " UM10_FMT
" (" UM10_FMT
" duplicates )\n", total_count
, duplicate_count
);
2073 fprintf(stderr
, " blobs : " UM10_FMT
" (" UM10_FMT
" duplicates " UM10_FMT
" deltas)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
], delta_count_by_type
[OBJ_BLOB
]);
2074 fprintf(stderr
, " trees : " UM10_FMT
" (" UM10_FMT
" duplicates " UM10_FMT
" deltas)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
], delta_count_by_type
[OBJ_TREE
]);
2075 fprintf(stderr
, " commits: " UM10_FMT
" (" UM10_FMT
" duplicates " UM10_FMT
" deltas)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
], delta_count_by_type
[OBJ_COMMIT
]);
2076 fprintf(stderr
, " tags : " UM10_FMT
" (" UM10_FMT
" duplicates " UM10_FMT
" deltas)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
], delta_count_by_type
[OBJ_TAG
]);
2077 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2078 fprintf(stderr
, " marks: " UM10_FMT
" (" UM10_FMT
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2079 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2080 fprintf(stderr
, "Memory total: " UM10_FMT
" KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2081 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
2082 fprintf(stderr
, " objects: " UM10_FMT
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2083 fprintf(stderr
, "---------------------------------------------------------------------\n");
2085 fprintf(stderr
, "---------------------------------------------------------------------\n");
2086 fprintf(stderr
, "\n");
2089 return failure
? 1 : 0;