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)
137 #define PRIuMAX "llu"
142 struct object_entry
*next
;
144 unsigned type
: TYPE_BITS
;
145 unsigned pack_id
: PACK_ID_BITS
;
146 unsigned char sha1
[20];
149 struct object_entry_pool
151 struct object_entry_pool
*next_pool
;
152 struct object_entry
*next_free
;
153 struct object_entry
*end
;
154 struct object_entry entries
[FLEX_ARRAY
]; /* more */
160 struct object_entry
*marked
[1024];
161 struct mark_set
*sets
[1024];
177 struct mem_pool
*next_pool
;
180 char space
[FLEX_ARRAY
]; /* more */
185 struct atom_str
*next_atom
;
186 unsigned short str_len
;
187 char str_dat
[FLEX_ARRAY
]; /* more */
193 struct tree_content
*tree
;
194 struct atom_str
* name
;
198 unsigned char sha1
[20];
204 unsigned int entry_capacity
; /* must match avail_tree_content */
205 unsigned int entry_count
;
206 unsigned int delta_depth
;
207 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
210 struct avail_tree_content
212 unsigned int entry_capacity
; /* must match tree_content */
213 struct avail_tree_content
*next_avail
;
218 struct branch
*table_next_branch
;
219 struct branch
*active_next_branch
;
221 struct tree_entry branch_tree
;
222 uintmax_t last_commit
;
223 unsigned int pack_id
;
224 unsigned char sha1
[20];
229 struct tag
*next_tag
;
231 unsigned int pack_id
;
232 unsigned char sha1
[20];
243 struct hash_list
*next
;
244 unsigned char sha1
[20];
253 /* Configured limits on output */
254 static unsigned long max_depth
= 10;
255 static unsigned long max_packsize
= (1LL << 32) - 1;
256 static int force_update
;
258 /* Stats and misc. counters */
259 static uintmax_t alloc_count
;
260 static uintmax_t marks_set_count
;
261 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
262 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
263 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
264 static unsigned long object_count
;
265 static unsigned long branch_count
;
266 static unsigned long branch_load_count
;
268 static FILE *pack_edges
;
271 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
272 static size_t total_allocd
;
273 static struct mem_pool
*mem_pool
;
275 /* Atom management */
276 static unsigned int atom_table_sz
= 4451;
277 static unsigned int atom_cnt
;
278 static struct atom_str
**atom_table
;
280 /* The .pack file being generated */
281 static unsigned int pack_id
;
282 static struct packed_git
*pack_data
;
283 static struct packed_git
**all_packs
;
284 static unsigned long pack_size
;
286 /* Table of objects we've written. */
287 static unsigned int object_entry_alloc
= 5000;
288 static struct object_entry_pool
*blocks
;
289 static struct object_entry
*object_table
[1 << 16];
290 static struct mark_set
*marks
;
291 static const char* mark_file
;
294 static struct last_object last_blob
;
296 /* Tree management */
297 static unsigned int tree_entry_alloc
= 1000;
298 static void *avail_tree_entry
;
299 static unsigned int avail_tree_table_sz
= 100;
300 static struct avail_tree_content
**avail_tree_table
;
301 static struct dbuf old_tree
;
302 static struct dbuf new_tree
;
305 static unsigned long max_active_branches
= 5;
306 static unsigned long cur_active_branches
;
307 static unsigned long branch_table_sz
= 1039;
308 static struct branch
**branch_table
;
309 static struct branch
*active_branches
;
312 static struct tag
*first_tag
;
313 static struct tag
*last_tag
;
315 /* Input stream parsing */
316 static whenspec_type whenspec
= WHENSPEC_RAW
;
317 static struct strbuf command_buf
;
318 static uintmax_t next_mark
;
319 static struct dbuf new_data
;
322 static void alloc_objects(unsigned int cnt
)
324 struct object_entry_pool
*b
;
326 b
= xmalloc(sizeof(struct object_entry_pool
)
327 + cnt
* sizeof(struct object_entry
));
328 b
->next_pool
= blocks
;
329 b
->next_free
= b
->entries
;
330 b
->end
= b
->entries
+ cnt
;
335 static struct object_entry
*new_object(unsigned char *sha1
)
337 struct object_entry
*e
;
339 if (blocks
->next_free
== blocks
->end
)
340 alloc_objects(object_entry_alloc
);
342 e
= blocks
->next_free
++;
343 hashcpy(e
->sha1
, sha1
);
347 static struct object_entry
*find_object(unsigned char *sha1
)
349 unsigned int h
= sha1
[0] << 8 | sha1
[1];
350 struct object_entry
*e
;
351 for (e
= object_table
[h
]; e
; e
= e
->next
)
352 if (!hashcmp(sha1
, e
->sha1
))
357 static struct object_entry
*insert_object(unsigned char *sha1
)
359 unsigned int h
= sha1
[0] << 8 | sha1
[1];
360 struct object_entry
*e
= object_table
[h
];
361 struct object_entry
*p
= NULL
;
364 if (!hashcmp(sha1
, e
->sha1
))
370 e
= new_object(sha1
);
380 static unsigned int hc_str(const char *s
, size_t len
)
388 static void *pool_alloc(size_t len
)
393 for (p
= mem_pool
; p
; p
= p
->next_pool
)
394 if ((p
->end
- p
->next_free
>= len
))
398 if (len
>= (mem_pool_alloc
/2)) {
402 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
403 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
404 p
->next_pool
= mem_pool
;
405 p
->next_free
= p
->space
;
406 p
->end
= p
->next_free
+ mem_pool_alloc
;
411 /* round out to a pointer alignment */
412 if (len
& (sizeof(void*) - 1))
413 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
418 static void *pool_calloc(size_t count
, size_t size
)
420 size_t len
= count
* size
;
421 void *r
= pool_alloc(len
);
426 static char *pool_strdup(const char *s
)
428 char *r
= pool_alloc(strlen(s
) + 1);
433 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
436 if (b
->capacity
>= maxlen
)
440 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
441 b
->buffer
= xmalloc(b
->capacity
);
444 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
446 struct mark_set
*s
= marks
;
447 while ((idnum
>> s
->shift
) >= 1024) {
448 s
= pool_calloc(1, sizeof(struct mark_set
));
449 s
->shift
= marks
->shift
+ 10;
450 s
->data
.sets
[0] = marks
;
454 uintmax_t i
= idnum
>> s
->shift
;
455 idnum
-= i
<< s
->shift
;
456 if (!s
->data
.sets
[i
]) {
457 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
458 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
462 if (!s
->data
.marked
[idnum
])
464 s
->data
.marked
[idnum
] = oe
;
467 static struct object_entry
*find_mark(uintmax_t idnum
)
469 uintmax_t orig_idnum
= idnum
;
470 struct mark_set
*s
= marks
;
471 struct object_entry
*oe
= NULL
;
472 if ((idnum
>> s
->shift
) < 1024) {
473 while (s
&& s
->shift
) {
474 uintmax_t i
= idnum
>> s
->shift
;
475 idnum
-= i
<< s
->shift
;
479 oe
= s
->data
.marked
[idnum
];
482 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
486 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
488 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
491 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
492 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
495 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
497 strncpy(c
->str_dat
, s
, len
);
499 c
->next_atom
= atom_table
[hc
];
505 static struct branch
*lookup_branch(const char *name
)
507 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
510 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
511 if (!strcmp(name
, b
->name
))
516 static struct branch
*new_branch(const char *name
)
518 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
519 struct branch
* b
= lookup_branch(name
);
522 die("Invalid attempt to create duplicate branch: %s", name
);
523 if (check_ref_format(name
))
524 die("Branch name doesn't conform to GIT standards: %s", name
);
526 b
= pool_calloc(1, sizeof(struct branch
));
527 b
->name
= pool_strdup(name
);
528 b
->table_next_branch
= branch_table
[hc
];
529 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
530 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
531 b
->pack_id
= MAX_PACK_ID
;
532 branch_table
[hc
] = b
;
537 static unsigned int hc_entries(unsigned int cnt
)
539 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
540 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
543 static struct tree_content
*new_tree_content(unsigned int cnt
)
545 struct avail_tree_content
*f
, *l
= NULL
;
546 struct tree_content
*t
;
547 unsigned int hc
= hc_entries(cnt
);
549 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
550 if (f
->entry_capacity
>= cnt
)
555 l
->next_avail
= f
->next_avail
;
557 avail_tree_table
[hc
] = f
->next_avail
;
559 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
560 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
561 f
->entry_capacity
= cnt
;
564 t
= (struct tree_content
*)f
;
570 static void release_tree_entry(struct tree_entry
*e
);
571 static void release_tree_content(struct tree_content
*t
)
573 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
574 unsigned int hc
= hc_entries(f
->entry_capacity
);
575 f
->next_avail
= avail_tree_table
[hc
];
576 avail_tree_table
[hc
] = f
;
579 static void release_tree_content_recursive(struct tree_content
*t
)
582 for (i
= 0; i
< t
->entry_count
; i
++)
583 release_tree_entry(t
->entries
[i
]);
584 release_tree_content(t
);
587 static struct tree_content
*grow_tree_content(
588 struct tree_content
*t
,
591 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
592 r
->entry_count
= t
->entry_count
;
593 r
->delta_depth
= t
->delta_depth
;
594 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
595 release_tree_content(t
);
599 static struct tree_entry
*new_tree_entry(void)
601 struct tree_entry
*e
;
603 if (!avail_tree_entry
) {
604 unsigned int n
= tree_entry_alloc
;
605 total_allocd
+= n
* sizeof(struct tree_entry
);
606 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
608 *((void**)e
) = e
+ 1;
614 e
= avail_tree_entry
;
615 avail_tree_entry
= *((void**)e
);
619 static void release_tree_entry(struct tree_entry
*e
)
622 release_tree_content_recursive(e
->tree
);
623 *((void**)e
) = avail_tree_entry
;
624 avail_tree_entry
= e
;
627 static void start_packfile(void)
629 static char tmpfile
[PATH_MAX
];
630 struct packed_git
*p
;
631 struct pack_header hdr
;
634 snprintf(tmpfile
, sizeof(tmpfile
),
635 "%s/pack_XXXXXX", get_object_directory());
636 pack_fd
= mkstemp(tmpfile
);
638 die("Can't create %s: %s", tmpfile
, strerror(errno
));
639 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
640 strcpy(p
->pack_name
, tmpfile
);
641 p
->pack_fd
= pack_fd
;
643 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
644 hdr
.hdr_version
= htonl(2);
646 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
649 pack_size
= sizeof(hdr
);
652 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
653 all_packs
[pack_id
] = p
;
656 static void fixup_header_footer(void)
658 static const int buf_sz
= 128 * 1024;
659 int pack_fd
= pack_data
->pack_fd
;
661 struct pack_header hdr
;
664 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
665 die("Failed seeking to start: %s", strerror(errno
));
666 if (read_in_full(pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
667 die("Unable to reread header of %s", pack_data
->pack_name
);
668 if (lseek(pack_fd
, 0, SEEK_SET
) != 0)
669 die("Failed seeking to start: %s", strerror(errno
));
670 hdr
.hdr_entries
= htonl(object_count
);
671 write_or_die(pack_fd
, &hdr
, sizeof(hdr
));
674 SHA1_Update(&c
, &hdr
, sizeof(hdr
));
676 buf
= xmalloc(buf_sz
);
678 size_t n
= xread(pack_fd
, buf
, buf_sz
);
682 die("Failed to checksum %s", pack_data
->pack_name
);
683 SHA1_Update(&c
, buf
, n
);
687 SHA1_Final(pack_data
->sha1
, &c
);
688 write_or_die(pack_fd
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
692 static int oecmp (const void *a_
, const void *b_
)
694 struct object_entry
*a
= *((struct object_entry
**)a_
);
695 struct object_entry
*b
= *((struct object_entry
**)b_
);
696 return hashcmp(a
->sha1
, b
->sha1
);
699 static char *create_index(void)
701 static char tmpfile
[PATH_MAX
];
704 struct object_entry
**idx
, **c
, **last
, *e
;
705 struct object_entry_pool
*o
;
709 /* Build the sorted table of object IDs. */
710 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
712 for (o
= blocks
; o
; o
= o
->next_pool
)
713 for (e
= o
->next_free
; e
-- != o
->entries
;)
714 if (pack_id
== e
->pack_id
)
716 last
= idx
+ object_count
;
718 die("internal consistency error creating the index");
719 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
721 /* Generate the fan-out array. */
723 for (i
= 0; i
< 256; i
++) {
724 struct object_entry
**next
= c
;;
725 while (next
< last
) {
726 if ((*next
)->sha1
[0] != i
)
730 array
[i
] = htonl(next
- idx
);
734 snprintf(tmpfile
, sizeof(tmpfile
),
735 "%s/index_XXXXXX", get_object_directory());
736 idx_fd
= mkstemp(tmpfile
);
738 die("Can't create %s: %s", tmpfile
, strerror(errno
));
739 f
= sha1fd(idx_fd
, tmpfile
);
740 sha1write(f
, array
, 256 * sizeof(int));
742 for (c
= idx
; c
!= last
; c
++) {
743 uint32_t offset
= htonl((*c
)->offset
);
744 sha1write(f
, &offset
, 4);
745 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
746 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
748 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
749 sha1close(f
, NULL
, 1);
751 SHA1_Final(pack_data
->sha1
, &ctx
);
755 static char *keep_pack(char *curr_index_name
)
757 static char name
[PATH_MAX
];
758 static char *keep_msg
= "fast-import";
761 chmod(pack_data
->pack_name
, 0444);
762 chmod(curr_index_name
, 0444);
764 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
765 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
766 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
768 die("cannot create keep file");
769 write(keep_fd
, keep_msg
, strlen(keep_msg
));
772 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
773 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
774 if (move_temp_to_file(pack_data
->pack_name
, name
))
775 die("cannot store pack file");
777 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
778 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
779 if (move_temp_to_file(curr_index_name
, name
))
780 die("cannot store index file");
784 static void unkeep_all_packs(void)
786 static char name
[PATH_MAX
];
789 for (k
= 0; k
< pack_id
; k
++) {
790 struct packed_git
*p
= all_packs
[k
];
791 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
792 get_object_directory(), sha1_to_hex(p
->sha1
));
797 static void end_packfile(void)
799 struct packed_git
*old_p
= pack_data
, *new_p
;
807 fixup_header_footer();
808 idx_name
= keep_pack(create_index());
810 /* Register the packfile with core git's machinary. */
811 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
813 die("core git rejected index %s", idx_name
);
814 new_p
->windows
= old_p
->windows
;
815 all_packs
[pack_id
] = new_p
;
816 install_packed_git(new_p
);
818 /* Print the boundary */
820 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
821 for (i
= 0; i
< branch_table_sz
; i
++) {
822 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
823 if (b
->pack_id
== pack_id
)
824 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
827 for (t
= first_tag
; t
; t
= t
->next_tag
) {
828 if (t
->pack_id
== pack_id
)
829 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
831 fputc('\n', pack_edges
);
838 unlink(old_p
->pack_name
);
841 /* We can't carry a delta across packfiles. */
842 free(last_blob
.data
);
843 last_blob
.data
= NULL
;
845 last_blob
.offset
= 0;
849 static void cycle_packfile(void)
855 static size_t encode_header(
856 enum object_type type
,
863 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
864 die("bad type %d", type
);
866 c
= (type
<< 4) | (size
& 15);
878 static int store_object(
879 enum object_type type
,
882 struct last_object
*last
,
883 unsigned char *sha1out
,
887 struct object_entry
*e
;
888 unsigned char hdr
[96];
889 unsigned char sha1
[20];
890 unsigned long hdrlen
, deltalen
;
894 hdrlen
= sprintf((char*)hdr
,"%s %lu", typename(type
),
895 (unsigned long)datlen
) + 1;
897 SHA1_Update(&c
, hdr
, hdrlen
);
898 SHA1_Update(&c
, dat
, datlen
);
899 SHA1_Final(sha1
, &c
);
901 hashcpy(sha1out
, sha1
);
903 e
= insert_object(sha1
);
905 insert_mark(mark
, e
);
907 duplicate_count_by_type
[type
]++;
911 if (last
&& last
->data
&& last
->depth
< max_depth
) {
912 delta
= diff_delta(last
->data
, last
->len
,
915 if (delta
&& deltalen
>= datlen
) {
922 memset(&s
, 0, sizeof(s
));
923 deflateInit(&s
, zlib_compression_level
);
926 s
.avail_in
= deltalen
;
931 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
932 s
.next_out
= out
= xmalloc(s
.avail_out
);
933 while (deflate(&s
, Z_FINISH
) == Z_OK
)
937 /* Determine if we should auto-checkpoint. */
938 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
939 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
941 /* This new object needs to *not* have the current pack_id. */
942 e
->pack_id
= pack_id
+ 1;
945 /* We cannot carry a delta into the new pack. */
950 memset(&s
, 0, sizeof(s
));
951 deflateInit(&s
, zlib_compression_level
);
954 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
955 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
956 while (deflate(&s
, Z_FINISH
) == Z_OK
)
963 e
->pack_id
= pack_id
;
964 e
->offset
= pack_size
;
966 object_count_by_type
[type
]++;
969 unsigned long ofs
= e
->offset
- last
->offset
;
970 unsigned pos
= sizeof(hdr
) - 1;
972 delta_count_by_type
[type
]++;
975 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
976 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
979 hdr
[pos
] = ofs
& 127;
981 hdr
[--pos
] = 128 | (--ofs
& 127);
982 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
983 pack_size
+= sizeof(hdr
) - pos
;
987 hdrlen
= encode_header(type
, datlen
, hdr
);
988 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
992 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
993 pack_size
+= s
.total_out
;
1001 last
->offset
= e
->offset
;
1007 static void *gfi_unpack_entry(
1008 struct object_entry
*oe
,
1009 unsigned long *sizep
)
1011 enum object_type type
;
1012 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1014 p
->pack_size
= pack_size
+ 20;
1015 return unpack_entry(p
, oe
->offset
, &type
, sizep
);
1018 static const char *get_mode(const char *str
, uint16_t *modep
)
1023 while ((c
= *str
++) != ' ') {
1024 if (c
< '0' || c
> '7')
1026 mode
= (mode
<< 3) + (c
- '0');
1032 static void load_tree(struct tree_entry
*root
)
1034 unsigned char* sha1
= root
->versions
[1].sha1
;
1035 struct object_entry
*myoe
;
1036 struct tree_content
*t
;
1041 root
->tree
= t
= new_tree_content(8);
1042 if (is_null_sha1(sha1
))
1045 myoe
= find_object(sha1
);
1047 if (myoe
->type
!= OBJ_TREE
)
1048 die("Not a tree: %s", sha1_to_hex(sha1
));
1050 buf
= gfi_unpack_entry(myoe
, &size
);
1052 enum object_type type
;
1053 buf
= read_sha1_file(sha1
, &type
, &size
);
1054 if (!buf
|| type
!= OBJ_TREE
)
1055 die("Can't load tree %s", sha1_to_hex(sha1
));
1059 while (c
!= (buf
+ size
)) {
1060 struct tree_entry
*e
= new_tree_entry();
1062 if (t
->entry_count
== t
->entry_capacity
)
1063 root
->tree
= t
= grow_tree_content(t
, 8);
1064 t
->entries
[t
->entry_count
++] = e
;
1067 c
= get_mode(c
, &e
->versions
[1].mode
);
1069 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1070 e
->versions
[0].mode
= e
->versions
[1].mode
;
1071 e
->name
= to_atom(c
, (unsigned short)strlen(c
));
1072 c
+= e
->name
->str_len
+ 1;
1073 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1074 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1080 static int tecmp0 (const void *_a
, const void *_b
)
1082 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1083 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1084 return base_name_compare(
1085 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1086 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1089 static int tecmp1 (const void *_a
, const void *_b
)
1091 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1092 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1093 return base_name_compare(
1094 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1095 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1098 static void mktree(struct tree_content
*t
,
1108 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1110 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1112 for (i
= 0; i
< t
->entry_count
; i
++) {
1113 if (t
->entries
[i
]->versions
[v
].mode
)
1114 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1117 size_dbuf(b
, maxlen
);
1119 for (i
= 0; i
< t
->entry_count
; i
++) {
1120 struct tree_entry
*e
= t
->entries
[i
];
1121 if (!e
->versions
[v
].mode
)
1123 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1125 strcpy(c
, e
->name
->str_dat
);
1126 c
+= e
->name
->str_len
+ 1;
1127 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1130 *szp
= c
- (char*)b
->buffer
;
1133 static void store_tree(struct tree_entry
*root
)
1135 struct tree_content
*t
= root
->tree
;
1136 unsigned int i
, j
, del
;
1137 unsigned long new_len
;
1138 struct last_object lo
;
1139 struct object_entry
*le
;
1141 if (!is_null_sha1(root
->versions
[1].sha1
))
1144 for (i
= 0; i
< t
->entry_count
; i
++) {
1145 if (t
->entries
[i
]->tree
)
1146 store_tree(t
->entries
[i
]);
1149 le
= find_object(root
->versions
[0].sha1
);
1150 if (!S_ISDIR(root
->versions
[0].mode
)
1152 || le
->pack_id
!= pack_id
) {
1156 mktree(t
, 0, &lo
.len
, &old_tree
);
1157 lo
.data
= old_tree
.buffer
;
1158 lo
.offset
= le
->offset
;
1159 lo
.depth
= t
->delta_depth
;
1163 mktree(t
, 1, &new_len
, &new_tree
);
1164 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1165 &lo
, root
->versions
[1].sha1
, 0);
1167 t
->delta_depth
= lo
.depth
;
1168 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1169 struct tree_entry
*e
= t
->entries
[i
];
1170 if (e
->versions
[1].mode
) {
1171 e
->versions
[0].mode
= e
->versions
[1].mode
;
1172 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1173 t
->entries
[j
++] = e
;
1175 release_tree_entry(e
);
1179 t
->entry_count
-= del
;
1182 static int tree_content_set(
1183 struct tree_entry
*root
,
1185 const unsigned char *sha1
,
1186 const uint16_t mode
)
1188 struct tree_content
*t
= root
->tree
;
1191 struct tree_entry
*e
;
1193 slash1
= strchr(p
, '/');
1199 for (i
= 0; i
< t
->entry_count
; i
++) {
1201 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1203 if (e
->versions
[1].mode
== mode
1204 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1206 e
->versions
[1].mode
= mode
;
1207 hashcpy(e
->versions
[1].sha1
, sha1
);
1209 release_tree_content_recursive(e
->tree
);
1212 hashclr(root
->versions
[1].sha1
);
1215 if (!S_ISDIR(e
->versions
[1].mode
)) {
1216 e
->tree
= new_tree_content(8);
1217 e
->versions
[1].mode
= S_IFDIR
;
1221 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
)) {
1222 hashclr(root
->versions
[1].sha1
);
1229 if (t
->entry_count
== t
->entry_capacity
)
1230 root
->tree
= t
= grow_tree_content(t
, 8);
1231 e
= new_tree_entry();
1232 e
->name
= to_atom(p
, (unsigned short)n
);
1233 e
->versions
[0].mode
= 0;
1234 hashclr(e
->versions
[0].sha1
);
1235 t
->entries
[t
->entry_count
++] = e
;
1237 e
->tree
= new_tree_content(8);
1238 e
->versions
[1].mode
= S_IFDIR
;
1239 tree_content_set(e
, slash1
+ 1, sha1
, mode
);
1242 e
->versions
[1].mode
= mode
;
1243 hashcpy(e
->versions
[1].sha1
, sha1
);
1245 hashclr(root
->versions
[1].sha1
);
1249 static int tree_content_remove(struct tree_entry
*root
, const char *p
)
1251 struct tree_content
*t
= root
->tree
;
1254 struct tree_entry
*e
;
1256 slash1
= strchr(p
, '/');
1262 for (i
= 0; i
< t
->entry_count
; i
++) {
1264 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1265 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1269 if (tree_content_remove(e
, slash1
+ 1)) {
1270 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1271 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1272 hashclr(root
->versions
[1].sha1
);
1285 release_tree_content_recursive(e
->tree
);
1288 e
->versions
[1].mode
= 0;
1289 hashclr(e
->versions
[1].sha1
);
1290 hashclr(root
->versions
[1].sha1
);
1294 static int update_branch(struct branch
*b
)
1296 static const char *msg
= "fast-import";
1297 struct ref_lock
*lock
;
1298 unsigned char old_sha1
[20];
1300 if (read_ref(b
->name
, old_sha1
))
1302 lock
= lock_any_ref_for_update(b
->name
, old_sha1
);
1304 return error("Unable to lock %s", b
->name
);
1305 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1306 struct commit
*old_cmit
, *new_cmit
;
1308 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1309 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1310 if (!old_cmit
|| !new_cmit
) {
1312 return error("Branch %s is missing commits.", b
->name
);
1315 if (!in_merge_bases(old_cmit
, &new_cmit
, 1)) {
1317 warn("Not updating %s"
1318 " (new tip %s does not contain %s)",
1319 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1323 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1324 return error("Unable to update %s", b
->name
);
1328 static void dump_branches(void)
1333 for (i
= 0; i
< branch_table_sz
; i
++) {
1334 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1335 failure
|= update_branch(b
);
1339 static void dump_tags(void)
1341 static const char *msg
= "fast-import";
1343 struct ref_lock
*lock
;
1344 char ref_name
[PATH_MAX
];
1346 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1347 sprintf(ref_name
, "tags/%s", t
->name
);
1348 lock
= lock_ref_sha1(ref_name
, NULL
);
1349 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1350 failure
|= error("Unable to update %s", ref_name
);
1354 static void dump_marks_helper(FILE *f
,
1360 for (k
= 0; k
< 1024; k
++) {
1361 if (m
->data
.sets
[k
])
1362 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1366 for (k
= 0; k
< 1024; k
++) {
1367 if (m
->data
.marked
[k
])
1368 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1369 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1374 static void dump_marks(void)
1378 FILE *f
= fopen(mark_file
, "w");
1380 dump_marks_helper(f
, 0, marks
);
1383 failure
|= error("Unable to write marks file %s: %s",
1384 mark_file
, strerror(errno
));
1388 static void read_next_command(void)
1390 read_line(&command_buf
, stdin
, '\n');
1393 static void cmd_mark(void)
1395 if (!prefixcmp(command_buf
.buf
, "mark :")) {
1396 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1397 read_next_command();
1403 static void *cmd_data (size_t *size
)
1408 if (prefixcmp(command_buf
.buf
, "data "))
1409 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1411 if (!prefixcmp(command_buf
.buf
+ 5, "<<")) {
1412 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1413 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1415 buffer
= xmalloc(sz
);
1417 read_next_command();
1418 if (command_buf
.eof
)
1419 die("EOF in data (terminator '%s' not found)", term
);
1420 if (term_len
== command_buf
.len
1421 && !strcmp(term
, command_buf
.buf
))
1423 if (sz
< (length
+ command_buf
.len
)) {
1424 sz
= sz
* 3 / 2 + 16;
1425 if (sz
< (length
+ command_buf
.len
))
1426 sz
= length
+ command_buf
.len
;
1427 buffer
= xrealloc(buffer
, sz
);
1429 memcpy(buffer
+ length
,
1431 command_buf
.len
- 1);
1432 length
+= command_buf
.len
- 1;
1433 buffer
[length
++] = '\n';
1439 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1440 buffer
= xmalloc(length
);
1441 while (n
< length
) {
1442 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1443 if (!s
&& feof(stdin
))
1444 die("EOF in data (%lu bytes remaining)",
1445 (unsigned long)(length
- n
));
1450 if (fgetc(stdin
) != '\n')
1451 die("An lf did not trail the binary data as expected.");
1457 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1459 const char *orig_src
= src
;
1462 strtoul(src
, &endp
, 10);
1463 if (endp
== src
|| *endp
!= ' ')
1467 if (*src
!= '-' && *src
!= '+')
1471 strtoul(src
+ 1, &endp
, 10);
1472 if (endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1475 strcpy(result
, orig_src
);
1479 static char *parse_ident(const char *buf
)
1485 gt
= strrchr(buf
, '>');
1487 die("Missing > in ident string: %s", buf
);
1490 die("Missing space after > in ident string: %s", buf
);
1492 name_len
= gt
- buf
;
1493 ident
= xmalloc(name_len
+ 24);
1494 strncpy(ident
, buf
, name_len
);
1498 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1499 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1501 case WHENSPEC_RFC2822
:
1502 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1503 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1506 if (strcmp("now", gt
))
1507 die("Date in ident must be 'now': %s", buf
);
1508 datestamp(ident
+ name_len
, 24);
1515 static void cmd_new_blob(void)
1520 read_next_command();
1524 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1528 static void unload_one_branch(void)
1530 while (cur_active_branches
1531 && cur_active_branches
>= max_active_branches
) {
1532 unsigned long min_commit
= ULONG_MAX
;
1533 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1535 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1536 if (e
->last_commit
< min_commit
) {
1538 min_commit
= e
->last_commit
;
1544 e
= p
->active_next_branch
;
1545 p
->active_next_branch
= e
->active_next_branch
;
1547 e
= active_branches
;
1548 active_branches
= e
->active_next_branch
;
1550 e
->active_next_branch
= NULL
;
1551 if (e
->branch_tree
.tree
) {
1552 release_tree_content_recursive(e
->branch_tree
.tree
);
1553 e
->branch_tree
.tree
= NULL
;
1555 cur_active_branches
--;
1559 static void load_branch(struct branch
*b
)
1561 load_tree(&b
->branch_tree
);
1562 b
->active_next_branch
= active_branches
;
1563 active_branches
= b
;
1564 cur_active_branches
++;
1565 branch_load_count
++;
1568 static void file_change_m(struct branch
*b
)
1570 const char *p
= command_buf
.buf
+ 2;
1573 struct object_entry
*oe
= oe
;
1574 unsigned char sha1
[20];
1575 uint16_t mode
, inline_data
= 0;
1577 p
= get_mode(p
, &mode
);
1579 die("Corrupt mode: %s", command_buf
.buf
);
1581 case S_IFREG
| 0644:
1582 case S_IFREG
| 0755:
1589 die("Corrupt mode: %s", command_buf
.buf
);
1594 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1595 hashcpy(sha1
, oe
->sha1
);
1597 } else if (!prefixcmp(p
, "inline")) {
1601 if (get_sha1_hex(p
, sha1
))
1602 die("Invalid SHA1: %s", command_buf
.buf
);
1603 oe
= find_object(sha1
);
1607 die("Missing space after SHA1: %s", command_buf
.buf
);
1609 p_uq
= unquote_c_style(p
, &endp
);
1612 die("Garbage after path in: %s", command_buf
.buf
);
1620 p
= p_uq
= xstrdup(p
);
1621 read_next_command();
1623 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1626 if (oe
->type
!= OBJ_BLOB
)
1627 die("Not a blob (actually a %s): %s",
1628 command_buf
.buf
, typename(oe
->type
));
1630 enum object_type type
= sha1_object_info(sha1
, NULL
);
1632 die("Blob not found: %s", command_buf
.buf
);
1633 if (type
!= OBJ_BLOB
)
1634 die("Not a blob (actually a %s): %s",
1635 typename(type
), command_buf
.buf
);
1638 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
);
1642 static void file_change_d(struct branch
*b
)
1644 const char *p
= command_buf
.buf
+ 2;
1648 p_uq
= unquote_c_style(p
, &endp
);
1651 die("Garbage after path in: %s", command_buf
.buf
);
1654 tree_content_remove(&b
->branch_tree
, p
);
1658 static void file_change_deleteall(struct branch
*b
)
1660 release_tree_content_recursive(b
->branch_tree
.tree
);
1661 hashclr(b
->branch_tree
.versions
[0].sha1
);
1662 hashclr(b
->branch_tree
.versions
[1].sha1
);
1663 load_tree(&b
->branch_tree
);
1666 static void cmd_from(struct branch
*b
)
1671 if (prefixcmp(command_buf
.buf
, "from "))
1674 if (b
->branch_tree
.tree
) {
1675 release_tree_content_recursive(b
->branch_tree
.tree
);
1676 b
->branch_tree
.tree
= NULL
;
1679 from
= strchr(command_buf
.buf
, ' ') + 1;
1680 s
= lookup_branch(from
);
1682 die("Can't create a branch from itself: %s", b
->name
);
1684 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1685 hashcpy(b
->sha1
, s
->sha1
);
1686 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1687 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1688 } else if (*from
== ':') {
1689 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1690 struct object_entry
*oe
= find_mark(idnum
);
1693 if (oe
->type
!= OBJ_COMMIT
)
1694 die("Mark :%" PRIuMAX
" not a commit", idnum
);
1695 hashcpy(b
->sha1
, oe
->sha1
);
1696 buf
= gfi_unpack_entry(oe
, &size
);
1697 if (!buf
|| size
< 46)
1698 die("Not a valid commit: %s", from
);
1699 if (memcmp("tree ", buf
, 5)
1700 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1701 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1703 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1704 b
->branch_tree
.versions
[1].sha1
);
1705 } else if (!get_sha1(from
, b
->sha1
)) {
1706 if (is_null_sha1(b
->sha1
)) {
1707 hashclr(b
->branch_tree
.versions
[0].sha1
);
1708 hashclr(b
->branch_tree
.versions
[1].sha1
);
1713 buf
= read_object_with_reference(b
->sha1
,
1714 commit_type
, &size
, b
->sha1
);
1715 if (!buf
|| size
< 46)
1716 die("Not a valid commit: %s", from
);
1717 if (memcmp("tree ", buf
, 5)
1718 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1719 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1721 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1722 b
->branch_tree
.versions
[1].sha1
);
1725 die("Invalid ref name or SHA1 expression: %s", from
);
1727 read_next_command();
1730 static struct hash_list
*cmd_merge(unsigned int *count
)
1732 struct hash_list
*list
= NULL
, *n
, *e
= e
;
1737 while (!prefixcmp(command_buf
.buf
, "merge ")) {
1738 from
= strchr(command_buf
.buf
, ' ') + 1;
1739 n
= xmalloc(sizeof(*n
));
1740 s
= lookup_branch(from
);
1742 hashcpy(n
->sha1
, s
->sha1
);
1743 else if (*from
== ':') {
1744 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1745 struct object_entry
*oe
= find_mark(idnum
);
1746 if (oe
->type
!= OBJ_COMMIT
)
1747 die("Mark :%" PRIuMAX
" not a commit", idnum
);
1748 hashcpy(n
->sha1
, oe
->sha1
);
1749 } else if (get_sha1(from
, n
->sha1
))
1750 die("Invalid ref name or SHA1 expression: %s", from
);
1759 read_next_command();
1764 static void cmd_new_commit(void)
1770 char *author
= NULL
;
1771 char *committer
= NULL
;
1772 struct hash_list
*merge_list
= NULL
;
1773 unsigned int merge_count
;
1775 /* Obtain the branch name from the rest of our command */
1776 sp
= strchr(command_buf
.buf
, ' ') + 1;
1777 b
= lookup_branch(sp
);
1781 read_next_command();
1783 if (!prefixcmp(command_buf
.buf
, "author ")) {
1784 author
= parse_ident(command_buf
.buf
+ 7);
1785 read_next_command();
1787 if (!prefixcmp(command_buf
.buf
, "committer ")) {
1788 committer
= parse_ident(command_buf
.buf
+ 10);
1789 read_next_command();
1792 die("Expected committer but didn't get one");
1793 msg
= cmd_data(&msglen
);
1794 read_next_command();
1796 merge_list
= cmd_merge(&merge_count
);
1798 /* ensure the branch is active/loaded */
1799 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1800 unload_one_branch();
1806 if (1 == command_buf
.len
)
1808 else if (!prefixcmp(command_buf
.buf
, "M "))
1810 else if (!prefixcmp(command_buf
.buf
, "D "))
1812 else if (!strcmp("deleteall", command_buf
.buf
))
1813 file_change_deleteall(b
);
1815 die("Unsupported file_change: %s", command_buf
.buf
);
1816 read_next_command();
1819 /* build the tree and the commit */
1820 store_tree(&b
->branch_tree
);
1821 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1822 b
->branch_tree
.versions
[1].sha1
);
1823 size_dbuf(&new_data
, 114 + msglen
1826 ? strlen(author
) + strlen(committer
)
1827 : 2 * strlen(committer
)));
1828 sp
= new_data
.buffer
;
1829 sp
+= sprintf(sp
, "tree %s\n",
1830 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1831 if (!is_null_sha1(b
->sha1
))
1832 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1833 while (merge_list
) {
1834 struct hash_list
*next
= merge_list
->next
;
1835 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1839 sp
+= sprintf(sp
, "author %s\n", author
? author
: committer
);
1840 sp
+= sprintf(sp
, "committer %s\n", committer
);
1842 memcpy(sp
, msg
, msglen
);
1848 if (!store_object(OBJ_COMMIT
,
1849 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1850 NULL
, b
->sha1
, next_mark
))
1851 b
->pack_id
= pack_id
;
1852 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1855 static void cmd_new_tag(void)
1864 uintmax_t from_mark
= 0;
1865 unsigned char sha1
[20];
1867 /* Obtain the new tag name from the rest of our command */
1868 sp
= strchr(command_buf
.buf
, ' ') + 1;
1869 t
= pool_alloc(sizeof(struct tag
));
1871 t
->name
= pool_strdup(sp
);
1873 last_tag
->next_tag
= t
;
1877 read_next_command();
1880 if (prefixcmp(command_buf
.buf
, "from "))
1881 die("Expected from command, got %s", command_buf
.buf
);
1882 from
= strchr(command_buf
.buf
, ' ') + 1;
1883 s
= lookup_branch(from
);
1885 hashcpy(sha1
, s
->sha1
);
1886 } else if (*from
== ':') {
1887 struct object_entry
*oe
;
1888 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1889 oe
= find_mark(from_mark
);
1890 if (oe
->type
!= OBJ_COMMIT
)
1891 die("Mark :%" PRIuMAX
" not a commit", from_mark
);
1892 hashcpy(sha1
, oe
->sha1
);
1893 } else if (!get_sha1(from
, sha1
)) {
1897 buf
= read_object_with_reference(sha1
,
1898 commit_type
, &size
, sha1
);
1899 if (!buf
|| size
< 46)
1900 die("Not a valid commit: %s", from
);
1903 die("Invalid ref name or SHA1 expression: %s", from
);
1904 read_next_command();
1907 if (prefixcmp(command_buf
.buf
, "tagger "))
1908 die("Expected tagger command, got %s", command_buf
.buf
);
1909 tagger
= parse_ident(command_buf
.buf
+ 7);
1911 /* tag payload/message */
1912 read_next_command();
1913 msg
= cmd_data(&msglen
);
1915 /* build the tag object */
1916 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1917 sp
= new_data
.buffer
;
1918 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1919 sp
+= sprintf(sp
, "type %s\n", commit_type
);
1920 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1921 sp
+= sprintf(sp
, "tagger %s\n", tagger
);
1923 memcpy(sp
, msg
, msglen
);
1928 if (store_object(OBJ_TAG
, new_data
.buffer
,
1929 sp
- (char*)new_data
.buffer
,
1931 t
->pack_id
= MAX_PACK_ID
;
1933 t
->pack_id
= pack_id
;
1936 static void cmd_reset_branch(void)
1941 /* Obtain the branch name from the rest of our command */
1942 sp
= strchr(command_buf
.buf
, ' ') + 1;
1943 b
= lookup_branch(sp
);
1946 hashclr(b
->branch_tree
.versions
[0].sha1
);
1947 hashclr(b
->branch_tree
.versions
[1].sha1
);
1948 if (b
->branch_tree
.tree
) {
1949 release_tree_content_recursive(b
->branch_tree
.tree
);
1950 b
->branch_tree
.tree
= NULL
;
1955 read_next_command();
1959 static void cmd_checkpoint(void)
1967 read_next_command();
1970 static const char fast_import_usage
[] =
1971 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
1973 int main(int argc
, const char **argv
)
1975 int i
, show_stats
= 1;
1977 git_config(git_default_config
);
1979 for (i
= 1; i
< argc
; i
++) {
1980 const char *a
= argv
[i
];
1982 if (*a
!= '-' || !strcmp(a
, "--"))
1984 else if (!prefixcmp(a
, "--date-format=")) {
1985 const char *fmt
= a
+ 14;
1986 if (!strcmp(fmt
, "raw"))
1987 whenspec
= WHENSPEC_RAW
;
1988 else if (!strcmp(fmt
, "rfc2822"))
1989 whenspec
= WHENSPEC_RFC2822
;
1990 else if (!strcmp(fmt
, "now"))
1991 whenspec
= WHENSPEC_NOW
;
1993 die("unknown --date-format argument %s", fmt
);
1995 else if (!prefixcmp(a
, "--max-pack-size="))
1996 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
1997 else if (!prefixcmp(a
, "--depth="))
1998 max_depth
= strtoul(a
+ 8, NULL
, 0);
1999 else if (!prefixcmp(a
, "--active-branches="))
2000 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
2001 else if (!prefixcmp(a
, "--export-marks="))
2003 else if (!prefixcmp(a
, "--export-pack-edges=")) {
2006 pack_edges
= fopen(a
+ 20, "a");
2008 die("Cannot open %s: %s", a
+ 20, strerror(errno
));
2009 } else if (!strcmp(a
, "--force"))
2011 else if (!strcmp(a
, "--quiet"))
2013 else if (!strcmp(a
, "--stats"))
2016 die("unknown option %s", a
);
2019 usage(fast_import_usage
);
2021 alloc_objects(object_entry_alloc
);
2022 strbuf_init(&command_buf
);
2024 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2025 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2026 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2027 marks
= pool_calloc(1, sizeof(struct mark_set
));
2031 read_next_command();
2032 if (command_buf
.eof
)
2034 else if (!strcmp("blob", command_buf
.buf
))
2036 else if (!prefixcmp(command_buf
.buf
, "commit "))
2038 else if (!prefixcmp(command_buf
.buf
, "tag "))
2040 else if (!prefixcmp(command_buf
.buf
, "reset "))
2042 else if (!strcmp("checkpoint", command_buf
.buf
))
2045 die("Unsupported command: %s", command_buf
.buf
);
2058 uintmax_t total_count
= 0, duplicate_count
= 0;
2059 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2060 total_count
+= object_count_by_type
[i
];
2061 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2062 duplicate_count
+= duplicate_count_by_type
[i
];
2064 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2065 fprintf(stderr
, "---------------------------------------------------------------------\n");
2066 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
2067 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
2068 fprintf(stderr
, " blobs : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
], delta_count_by_type
[OBJ_BLOB
]);
2069 fprintf(stderr
, " trees : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
], delta_count_by_type
[OBJ_TREE
]);
2070 fprintf(stderr
, " commits: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
], delta_count_by_type
[OBJ_COMMIT
]);
2071 fprintf(stderr
, " tags : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
], delta_count_by_type
[OBJ_TAG
]);
2072 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2073 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2074 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2075 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2076 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
2077 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2078 fprintf(stderr
, "---------------------------------------------------------------------\n");
2080 fprintf(stderr
, "---------------------------------------------------------------------\n");
2081 fprintf(stderr
, "\n");
2084 return failure
? 1 : 0;