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_rnm | file_obm | file_inm;
30 file_clr ::= 'deleteall' lf;
31 file_del ::= 'D' sp path_str lf;
32 file_rnm ::= 'R' sp path_str sp path_str lf;
33 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
34 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
37 new_tag ::= 'tag' sp tag_str lf
38 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
39 'tagger' sp name '<' email '>' when lf
43 reset_branch ::= 'reset' sp ref_str lf
44 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
47 checkpoint ::= 'checkpoint' lf
50 # note: the first idnum in a stream should be 1 and subsequent
51 # idnums should not have gaps between values as this will cause
52 # the stream parser to reserve space for the gapped values. An
53 # idnum can be updated in the future to a new object by issuing
54 # a new mark directive with the old idnum.
56 mark ::= 'mark' sp idnum lf;
57 data ::= (delimited_data | exact_data)
60 # note: delim may be any string but must not contain lf.
61 # data_line may contain any data but must not be exactly
63 delimited_data ::= 'data' sp '<<' delim lf
67 # note: declen indicates the length of binary_data in bytes.
68 # declen does not include the lf preceeding the binary data.
70 exact_data ::= 'data' sp declen lf
73 # note: quoted strings are C-style quoting supporting \c for
74 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
75 # is the signed byte value in octal. Note that the only
76 # characters which must actually be escaped to protect the
77 # stream formatting is: \, " and LF. Otherwise these values
81 sha1exp_str ::= sha1exp;
83 path_str ::= path | '"' quoted(path) '"' ;
84 mode ::= '100644' | '644'
89 declen ::= # unsigned 32 bit value, ascii base10 notation;
90 bigint ::= # unsigned integer value, ascii base10 notation;
91 binary_data ::= # file content, not interpreted;
93 when ::= raw_when | rfc2822_when;
94 raw_when ::= ts sp tz;
95 rfc2822_when ::= # Valid RFC 2822 date and time;
97 sp ::= # ASCII space character;
98 lf ::= # ASCII newline (LF) character;
100 # note: a colon (':') must precede the numerical value assigned to
101 # an idnum. This is to distinguish it from a ref or tag name as
102 # GIT does not permit ':' in ref or tag strings.
104 idnum ::= ':' bigint;
105 path ::= # GIT style file path, e.g. "a/b/c";
106 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
107 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
108 sha1exp ::= # Any valid GIT SHA1 expression;
109 hexsha1 ::= # SHA1 in hexadecimal format;
111 # note: name and email are UTF8 strings, however name must not
112 # contain '<' or lf and email must not contain any of the
113 # following: '<', '>', lf.
115 name ::= # valid GIT author/committer name;
116 email ::= # valid GIT author/committer email;
117 ts ::= # time since the epoch in seconds, ascii base10 notation;
118 tz ::= # GIT style timezone;
130 #include "csum-file.h"
134 #define PACK_ID_BITS 16
135 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
139 struct object_entry
*next
;
141 unsigned type
: TYPE_BITS
;
142 unsigned pack_id
: PACK_ID_BITS
;
143 unsigned char sha1
[20];
146 struct object_entry_pool
148 struct object_entry_pool
*next_pool
;
149 struct object_entry
*next_free
;
150 struct object_entry
*end
;
151 struct object_entry entries
[FLEX_ARRAY
]; /* more */
157 struct object_entry
*marked
[1024];
158 struct mark_set
*sets
[1024];
174 struct mem_pool
*next_pool
;
177 char space
[FLEX_ARRAY
]; /* more */
182 struct atom_str
*next_atom
;
183 unsigned short str_len
;
184 char str_dat
[FLEX_ARRAY
]; /* more */
190 struct tree_content
*tree
;
191 struct atom_str
* name
;
195 unsigned char sha1
[20];
201 unsigned int entry_capacity
; /* must match avail_tree_content */
202 unsigned int entry_count
;
203 unsigned int delta_depth
;
204 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
207 struct avail_tree_content
209 unsigned int entry_capacity
; /* must match tree_content */
210 struct avail_tree_content
*next_avail
;
215 struct branch
*table_next_branch
;
216 struct branch
*active_next_branch
;
218 struct tree_entry branch_tree
;
219 uintmax_t last_commit
;
221 unsigned pack_id
: PACK_ID_BITS
;
222 unsigned char sha1
[20];
227 struct tag
*next_tag
;
229 unsigned int pack_id
;
230 unsigned char sha1
[20];
241 struct hash_list
*next
;
242 unsigned char sha1
[20];
251 /* Configured limits on output */
252 static unsigned long max_depth
= 10;
253 static off_t max_packsize
= (1LL << 32) - 1;
254 static int force_update
;
256 /* Stats and misc. counters */
257 static uintmax_t alloc_count
;
258 static uintmax_t marks_set_count
;
259 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
260 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
261 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
262 static unsigned long object_count
;
263 static unsigned long branch_count
;
264 static unsigned long branch_load_count
;
266 static FILE *pack_edges
;
269 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
270 static size_t total_allocd
;
271 static struct mem_pool
*mem_pool
;
273 /* Atom management */
274 static unsigned int atom_table_sz
= 4451;
275 static unsigned int atom_cnt
;
276 static struct atom_str
**atom_table
;
278 /* The .pack file being generated */
279 static unsigned int pack_id
;
280 static struct packed_git
*pack_data
;
281 static struct packed_git
**all_packs
;
282 static unsigned long pack_size
;
284 /* Table of objects we've written. */
285 static unsigned int object_entry_alloc
= 5000;
286 static struct object_entry_pool
*blocks
;
287 static struct object_entry
*object_table
[1 << 16];
288 static struct mark_set
*marks
;
289 static const char* mark_file
;
292 static struct last_object last_blob
;
294 /* Tree management */
295 static unsigned int tree_entry_alloc
= 1000;
296 static void *avail_tree_entry
;
297 static unsigned int avail_tree_table_sz
= 100;
298 static struct avail_tree_content
**avail_tree_table
;
299 static struct dbuf old_tree
;
300 static struct dbuf new_tree
;
303 static unsigned long max_active_branches
= 5;
304 static unsigned long cur_active_branches
;
305 static unsigned long branch_table_sz
= 1039;
306 static struct branch
**branch_table
;
307 static struct branch
*active_branches
;
310 static struct tag
*first_tag
;
311 static struct tag
*last_tag
;
313 /* Input stream parsing */
314 static whenspec_type whenspec
= WHENSPEC_RAW
;
315 static struct strbuf command_buf
;
316 static uintmax_t next_mark
;
317 static struct dbuf new_data
;
320 static void alloc_objects(unsigned int cnt
)
322 struct object_entry_pool
*b
;
324 b
= xmalloc(sizeof(struct object_entry_pool
)
325 + cnt
* sizeof(struct object_entry
));
326 b
->next_pool
= blocks
;
327 b
->next_free
= b
->entries
;
328 b
->end
= b
->entries
+ cnt
;
333 static struct object_entry
*new_object(unsigned char *sha1
)
335 struct object_entry
*e
;
337 if (blocks
->next_free
== blocks
->end
)
338 alloc_objects(object_entry_alloc
);
340 e
= blocks
->next_free
++;
341 hashcpy(e
->sha1
, sha1
);
345 static struct object_entry
*find_object(unsigned char *sha1
)
347 unsigned int h
= sha1
[0] << 8 | sha1
[1];
348 struct object_entry
*e
;
349 for (e
= object_table
[h
]; e
; e
= e
->next
)
350 if (!hashcmp(sha1
, e
->sha1
))
355 static struct object_entry
*insert_object(unsigned char *sha1
)
357 unsigned int h
= sha1
[0] << 8 | sha1
[1];
358 struct object_entry
*e
= object_table
[h
];
359 struct object_entry
*p
= NULL
;
362 if (!hashcmp(sha1
, e
->sha1
))
368 e
= new_object(sha1
);
378 static unsigned int hc_str(const char *s
, size_t len
)
386 static void *pool_alloc(size_t len
)
391 for (p
= mem_pool
; p
; p
= p
->next_pool
)
392 if ((p
->end
- p
->next_free
>= len
))
396 if (len
>= (mem_pool_alloc
/2)) {
400 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
401 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
402 p
->next_pool
= mem_pool
;
403 p
->next_free
= p
->space
;
404 p
->end
= p
->next_free
+ mem_pool_alloc
;
409 /* round out to a pointer alignment */
410 if (len
& (sizeof(void*) - 1))
411 len
+= sizeof(void*) - (len
& (sizeof(void*) - 1));
416 static void *pool_calloc(size_t count
, size_t size
)
418 size_t len
= count
* size
;
419 void *r
= pool_alloc(len
);
424 static char *pool_strdup(const char *s
)
426 char *r
= pool_alloc(strlen(s
) + 1);
431 static void size_dbuf(struct dbuf
*b
, size_t maxlen
)
434 if (b
->capacity
>= maxlen
)
438 b
->capacity
= ((maxlen
/ 1024) + 1) * 1024;
439 b
->buffer
= xmalloc(b
->capacity
);
442 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
444 struct mark_set
*s
= marks
;
445 while ((idnum
>> s
->shift
) >= 1024) {
446 s
= pool_calloc(1, sizeof(struct mark_set
));
447 s
->shift
= marks
->shift
+ 10;
448 s
->data
.sets
[0] = marks
;
452 uintmax_t i
= idnum
>> s
->shift
;
453 idnum
-= i
<< s
->shift
;
454 if (!s
->data
.sets
[i
]) {
455 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
456 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
460 if (!s
->data
.marked
[idnum
])
462 s
->data
.marked
[idnum
] = oe
;
465 static struct object_entry
*find_mark(uintmax_t idnum
)
467 uintmax_t orig_idnum
= idnum
;
468 struct mark_set
*s
= marks
;
469 struct object_entry
*oe
= NULL
;
470 if ((idnum
>> s
->shift
) < 1024) {
471 while (s
&& s
->shift
) {
472 uintmax_t i
= idnum
>> s
->shift
;
473 idnum
-= i
<< s
->shift
;
477 oe
= s
->data
.marked
[idnum
];
480 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
484 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
486 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
489 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
490 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
493 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
495 strncpy(c
->str_dat
, s
, len
);
497 c
->next_atom
= atom_table
[hc
];
503 static struct branch
*lookup_branch(const char *name
)
505 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
508 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
509 if (!strcmp(name
, b
->name
))
514 static struct branch
*new_branch(const char *name
)
516 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
517 struct branch
* b
= lookup_branch(name
);
520 die("Invalid attempt to create duplicate branch: %s", name
);
521 if (check_ref_format(name
))
522 die("Branch name doesn't conform to GIT standards: %s", name
);
524 b
= pool_calloc(1, sizeof(struct branch
));
525 b
->name
= pool_strdup(name
);
526 b
->table_next_branch
= branch_table
[hc
];
527 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
528 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
530 b
->pack_id
= MAX_PACK_ID
;
531 branch_table
[hc
] = b
;
536 static unsigned int hc_entries(unsigned int cnt
)
538 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
539 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
542 static struct tree_content
*new_tree_content(unsigned int cnt
)
544 struct avail_tree_content
*f
, *l
= NULL
;
545 struct tree_content
*t
;
546 unsigned int hc
= hc_entries(cnt
);
548 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
549 if (f
->entry_capacity
>= cnt
)
554 l
->next_avail
= f
->next_avail
;
556 avail_tree_table
[hc
] = f
->next_avail
;
558 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
559 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
560 f
->entry_capacity
= cnt
;
563 t
= (struct tree_content
*)f
;
569 static void release_tree_entry(struct tree_entry
*e
);
570 static void release_tree_content(struct tree_content
*t
)
572 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
573 unsigned int hc
= hc_entries(f
->entry_capacity
);
574 f
->next_avail
= avail_tree_table
[hc
];
575 avail_tree_table
[hc
] = f
;
578 static void release_tree_content_recursive(struct tree_content
*t
)
581 for (i
= 0; i
< t
->entry_count
; i
++)
582 release_tree_entry(t
->entries
[i
]);
583 release_tree_content(t
);
586 static struct tree_content
*grow_tree_content(
587 struct tree_content
*t
,
590 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
591 r
->entry_count
= t
->entry_count
;
592 r
->delta_depth
= t
->delta_depth
;
593 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
594 release_tree_content(t
);
598 static struct tree_entry
*new_tree_entry(void)
600 struct tree_entry
*e
;
602 if (!avail_tree_entry
) {
603 unsigned int n
= tree_entry_alloc
;
604 total_allocd
+= n
* sizeof(struct tree_entry
);
605 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
607 *((void**)e
) = e
+ 1;
613 e
= avail_tree_entry
;
614 avail_tree_entry
= *((void**)e
);
618 static void release_tree_entry(struct tree_entry
*e
)
621 release_tree_content_recursive(e
->tree
);
622 *((void**)e
) = avail_tree_entry
;
623 avail_tree_entry
= e
;
626 static void start_packfile(void)
628 static char tmpfile
[PATH_MAX
];
629 struct packed_git
*p
;
630 struct pack_header hdr
;
633 snprintf(tmpfile
, sizeof(tmpfile
),
634 "%s/tmp_pack_XXXXXX", get_object_directory());
635 pack_fd
= mkstemp(tmpfile
);
637 die("Can't create %s: %s", tmpfile
, strerror(errno
));
638 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
639 strcpy(p
->pack_name
, tmpfile
);
640 p
->pack_fd
= pack_fd
;
642 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
643 hdr
.hdr_version
= htonl(2);
645 write_or_die(p
->pack_fd
, &hdr
, sizeof(hdr
));
648 pack_size
= sizeof(hdr
);
651 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
652 all_packs
[pack_id
] = p
;
655 static int oecmp (const void *a_
, const void *b_
)
657 struct object_entry
*a
= *((struct object_entry
**)a_
);
658 struct object_entry
*b
= *((struct object_entry
**)b_
);
659 return hashcmp(a
->sha1
, b
->sha1
);
662 static char *create_index(void)
664 static char tmpfile
[PATH_MAX
];
667 struct object_entry
**idx
, **c
, **last
, *e
;
668 struct object_entry_pool
*o
;
672 /* Build the sorted table of object IDs. */
673 idx
= xmalloc(object_count
* sizeof(struct object_entry
*));
675 for (o
= blocks
; o
; o
= o
->next_pool
)
676 for (e
= o
->next_free
; e
-- != o
->entries
;)
677 if (pack_id
== e
->pack_id
)
679 last
= idx
+ object_count
;
681 die("internal consistency error creating the index");
682 qsort(idx
, object_count
, sizeof(struct object_entry
*), oecmp
);
684 /* Generate the fan-out array. */
686 for (i
= 0; i
< 256; i
++) {
687 struct object_entry
**next
= c
;;
688 while (next
< last
) {
689 if ((*next
)->sha1
[0] != i
)
693 array
[i
] = htonl(next
- idx
);
697 snprintf(tmpfile
, sizeof(tmpfile
),
698 "%s/tmp_idx_XXXXXX", get_object_directory());
699 idx_fd
= mkstemp(tmpfile
);
701 die("Can't create %s: %s", tmpfile
, strerror(errno
));
702 f
= sha1fd(idx_fd
, tmpfile
);
703 sha1write(f
, array
, 256 * sizeof(int));
705 for (c
= idx
; c
!= last
; c
++) {
706 uint32_t offset
= htonl((*c
)->offset
);
707 sha1write(f
, &offset
, 4);
708 sha1write(f
, (*c
)->sha1
, sizeof((*c
)->sha1
));
709 SHA1_Update(&ctx
, (*c
)->sha1
, 20);
711 sha1write(f
, pack_data
->sha1
, sizeof(pack_data
->sha1
));
712 sha1close(f
, NULL
, 1);
714 SHA1_Final(pack_data
->sha1
, &ctx
);
718 static char *keep_pack(char *curr_index_name
)
720 static char name
[PATH_MAX
];
721 static const char *keep_msg
= "fast-import";
724 chmod(pack_data
->pack_name
, 0444);
725 chmod(curr_index_name
, 0444);
727 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
728 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
729 keep_fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
731 die("cannot create keep file");
732 write(keep_fd
, keep_msg
, strlen(keep_msg
));
735 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
736 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
737 if (move_temp_to_file(pack_data
->pack_name
, name
))
738 die("cannot store pack file");
740 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
741 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
742 if (move_temp_to_file(curr_index_name
, name
))
743 die("cannot store index file");
747 static void unkeep_all_packs(void)
749 static char name
[PATH_MAX
];
752 for (k
= 0; k
< pack_id
; k
++) {
753 struct packed_git
*p
= all_packs
[k
];
754 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
755 get_object_directory(), sha1_to_hex(p
->sha1
));
760 static void end_packfile(void)
762 struct packed_git
*old_p
= pack_data
, *new_p
;
770 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->sha1
,
771 pack_data
->pack_name
, object_count
);
772 close(pack_data
->pack_fd
);
773 idx_name
= keep_pack(create_index());
775 /* Register the packfile with core git's machinary. */
776 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
778 die("core git rejected index %s", idx_name
);
779 new_p
->windows
= old_p
->windows
;
780 all_packs
[pack_id
] = new_p
;
781 install_packed_git(new_p
);
783 /* Print the boundary */
785 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
786 for (i
= 0; i
< branch_table_sz
; i
++) {
787 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
788 if (b
->pack_id
== pack_id
)
789 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
792 for (t
= first_tag
; t
; t
= t
->next_tag
) {
793 if (t
->pack_id
== pack_id
)
794 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
796 fputc('\n', pack_edges
);
803 unlink(old_p
->pack_name
);
806 /* We can't carry a delta across packfiles. */
807 free(last_blob
.data
);
808 last_blob
.data
= NULL
;
810 last_blob
.offset
= 0;
814 static void cycle_packfile(void)
820 static size_t encode_header(
821 enum object_type type
,
828 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
829 die("bad type %d", type
);
831 c
= (type
<< 4) | (size
& 15);
843 static int store_object(
844 enum object_type type
,
847 struct last_object
*last
,
848 unsigned char *sha1out
,
852 struct object_entry
*e
;
853 unsigned char hdr
[96];
854 unsigned char sha1
[20];
855 unsigned long hdrlen
, deltalen
;
859 hdrlen
= sprintf((char*)hdr
,"%s %lu", typename(type
),
860 (unsigned long)datlen
) + 1;
862 SHA1_Update(&c
, hdr
, hdrlen
);
863 SHA1_Update(&c
, dat
, datlen
);
864 SHA1_Final(sha1
, &c
);
866 hashcpy(sha1out
, sha1
);
868 e
= insert_object(sha1
);
870 insert_mark(mark
, e
);
872 duplicate_count_by_type
[type
]++;
874 } else if (find_sha1_pack(sha1
, packed_git
)) {
876 e
->pack_id
= MAX_PACK_ID
;
877 e
->offset
= 1; /* just not zero! */
878 duplicate_count_by_type
[type
]++;
882 if (last
&& last
->data
&& last
->depth
< max_depth
) {
883 delta
= diff_delta(last
->data
, last
->len
,
886 if (delta
&& deltalen
>= datlen
) {
893 memset(&s
, 0, sizeof(s
));
894 deflateInit(&s
, zlib_compression_level
);
897 s
.avail_in
= deltalen
;
902 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
903 s
.next_out
= out
= xmalloc(s
.avail_out
);
904 while (deflate(&s
, Z_FINISH
) == Z_OK
)
908 /* Determine if we should auto-checkpoint. */
909 if ((pack_size
+ 60 + s
.total_out
) > max_packsize
910 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
912 /* This new object needs to *not* have the current pack_id. */
913 e
->pack_id
= pack_id
+ 1;
916 /* We cannot carry a delta into the new pack. */
921 memset(&s
, 0, sizeof(s
));
922 deflateInit(&s
, zlib_compression_level
);
925 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
926 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
927 while (deflate(&s
, Z_FINISH
) == Z_OK
)
934 e
->pack_id
= pack_id
;
935 e
->offset
= pack_size
;
937 object_count_by_type
[type
]++;
940 unsigned long ofs
= e
->offset
- last
->offset
;
941 unsigned pos
= sizeof(hdr
) - 1;
943 delta_count_by_type
[type
]++;
946 hdrlen
= encode_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
947 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
950 hdr
[pos
] = ofs
& 127;
952 hdr
[--pos
] = 128 | (--ofs
& 127);
953 write_or_die(pack_data
->pack_fd
, hdr
+ pos
, sizeof(hdr
) - pos
);
954 pack_size
+= sizeof(hdr
) - pos
;
958 hdrlen
= encode_header(type
, datlen
, hdr
);
959 write_or_die(pack_data
->pack_fd
, hdr
, hdrlen
);
963 write_or_die(pack_data
->pack_fd
, out
, s
.total_out
);
964 pack_size
+= s
.total_out
;
972 last
->offset
= e
->offset
;
978 static void *gfi_unpack_entry(
979 struct object_entry
*oe
,
980 unsigned long *sizep
)
982 enum object_type type
;
983 struct packed_git
*p
= all_packs
[oe
->pack_id
];
985 p
->pack_size
= pack_size
+ 20;
986 return unpack_entry(p
, oe
->offset
, &type
, sizep
);
989 static const char *get_mode(const char *str
, uint16_t *modep
)
994 while ((c
= *str
++) != ' ') {
995 if (c
< '0' || c
> '7')
997 mode
= (mode
<< 3) + (c
- '0');
1003 static void load_tree(struct tree_entry
*root
)
1005 unsigned char* sha1
= root
->versions
[1].sha1
;
1006 struct object_entry
*myoe
;
1007 struct tree_content
*t
;
1012 root
->tree
= t
= new_tree_content(8);
1013 if (is_null_sha1(sha1
))
1016 myoe
= find_object(sha1
);
1017 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1018 if (myoe
->type
!= OBJ_TREE
)
1019 die("Not a tree: %s", sha1_to_hex(sha1
));
1021 buf
= gfi_unpack_entry(myoe
, &size
);
1023 enum object_type type
;
1024 buf
= read_sha1_file(sha1
, &type
, &size
);
1025 if (!buf
|| type
!= OBJ_TREE
)
1026 die("Can't load tree %s", sha1_to_hex(sha1
));
1030 while (c
!= (buf
+ size
)) {
1031 struct tree_entry
*e
= new_tree_entry();
1033 if (t
->entry_count
== t
->entry_capacity
)
1034 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1035 t
->entries
[t
->entry_count
++] = e
;
1038 c
= get_mode(c
, &e
->versions
[1].mode
);
1040 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1041 e
->versions
[0].mode
= e
->versions
[1].mode
;
1042 e
->name
= to_atom(c
, strlen(c
));
1043 c
+= e
->name
->str_len
+ 1;
1044 hashcpy(e
->versions
[0].sha1
, (unsigned char*)c
);
1045 hashcpy(e
->versions
[1].sha1
, (unsigned char*)c
);
1051 static int tecmp0 (const void *_a
, const void *_b
)
1053 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1054 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1055 return base_name_compare(
1056 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1057 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1060 static int tecmp1 (const void *_a
, const void *_b
)
1062 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1063 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1064 return base_name_compare(
1065 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1066 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1069 static void mktree(struct tree_content
*t
,
1079 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1081 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1083 for (i
= 0; i
< t
->entry_count
; i
++) {
1084 if (t
->entries
[i
]->versions
[v
].mode
)
1085 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1088 size_dbuf(b
, maxlen
);
1090 for (i
= 0; i
< t
->entry_count
; i
++) {
1091 struct tree_entry
*e
= t
->entries
[i
];
1092 if (!e
->versions
[v
].mode
)
1094 c
+= sprintf(c
, "%o", (unsigned int)e
->versions
[v
].mode
);
1096 strcpy(c
, e
->name
->str_dat
);
1097 c
+= e
->name
->str_len
+ 1;
1098 hashcpy((unsigned char*)c
, e
->versions
[v
].sha1
);
1101 *szp
= c
- (char*)b
->buffer
;
1104 static void store_tree(struct tree_entry
*root
)
1106 struct tree_content
*t
= root
->tree
;
1107 unsigned int i
, j
, del
;
1108 unsigned long new_len
;
1109 struct last_object lo
;
1110 struct object_entry
*le
;
1112 if (!is_null_sha1(root
->versions
[1].sha1
))
1115 for (i
= 0; i
< t
->entry_count
; i
++) {
1116 if (t
->entries
[i
]->tree
)
1117 store_tree(t
->entries
[i
]);
1120 le
= find_object(root
->versions
[0].sha1
);
1121 if (!S_ISDIR(root
->versions
[0].mode
)
1123 || le
->pack_id
!= pack_id
) {
1128 mktree(t
, 0, &lo
.len
, &old_tree
);
1129 lo
.data
= old_tree
.buffer
;
1130 lo
.offset
= le
->offset
;
1131 lo
.depth
= t
->delta_depth
;
1135 mktree(t
, 1, &new_len
, &new_tree
);
1136 store_object(OBJ_TREE
, new_tree
.buffer
, new_len
,
1137 &lo
, root
->versions
[1].sha1
, 0);
1139 t
->delta_depth
= lo
.depth
;
1140 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1141 struct tree_entry
*e
= t
->entries
[i
];
1142 if (e
->versions
[1].mode
) {
1143 e
->versions
[0].mode
= e
->versions
[1].mode
;
1144 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1145 t
->entries
[j
++] = e
;
1147 release_tree_entry(e
);
1151 t
->entry_count
-= del
;
1154 static int tree_content_set(
1155 struct tree_entry
*root
,
1157 const unsigned char *sha1
,
1158 const uint16_t mode
,
1159 struct tree_content
*subtree
)
1161 struct tree_content
*t
= root
->tree
;
1164 struct tree_entry
*e
;
1166 slash1
= strchr(p
, '/');
1172 die("Empty path component found in input");
1173 if (!slash1
&& !S_ISDIR(mode
) && subtree
)
1174 die("Non-directories cannot have subtrees");
1176 for (i
= 0; i
< t
->entry_count
; i
++) {
1178 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1181 && e
->versions
[1].mode
== mode
1182 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1184 e
->versions
[1].mode
= mode
;
1185 hashcpy(e
->versions
[1].sha1
, sha1
);
1187 release_tree_content_recursive(e
->tree
);
1189 hashclr(root
->versions
[1].sha1
);
1192 if (!S_ISDIR(e
->versions
[1].mode
)) {
1193 e
->tree
= new_tree_content(8);
1194 e
->versions
[1].mode
= S_IFDIR
;
1198 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
)) {
1199 hashclr(root
->versions
[1].sha1
);
1206 if (t
->entry_count
== t
->entry_capacity
)
1207 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1208 e
= new_tree_entry();
1209 e
->name
= to_atom(p
, n
);
1210 e
->versions
[0].mode
= 0;
1211 hashclr(e
->versions
[0].sha1
);
1212 t
->entries
[t
->entry_count
++] = e
;
1214 e
->tree
= new_tree_content(8);
1215 e
->versions
[1].mode
= S_IFDIR
;
1216 tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
);
1219 e
->versions
[1].mode
= mode
;
1220 hashcpy(e
->versions
[1].sha1
, sha1
);
1222 hashclr(root
->versions
[1].sha1
);
1226 static int tree_content_remove(
1227 struct tree_entry
*root
,
1229 struct tree_entry
*backup_leaf
)
1231 struct tree_content
*t
= root
->tree
;
1234 struct tree_entry
*e
;
1236 slash1
= strchr(p
, '/');
1242 for (i
= 0; i
< t
->entry_count
; i
++) {
1244 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1245 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1249 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
)) {
1250 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1251 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1252 hashclr(root
->versions
[1].sha1
);
1266 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1268 release_tree_content_recursive(e
->tree
);
1270 e
->versions
[1].mode
= 0;
1271 hashclr(e
->versions
[1].sha1
);
1272 hashclr(root
->versions
[1].sha1
);
1276 static int update_branch(struct branch
*b
)
1278 static const char *msg
= "fast-import";
1279 struct ref_lock
*lock
;
1280 unsigned char old_sha1
[20];
1282 if (read_ref(b
->name
, old_sha1
))
1284 lock
= lock_any_ref_for_update(b
->name
, old_sha1
, 0);
1286 return error("Unable to lock %s", b
->name
);
1287 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1288 struct commit
*old_cmit
, *new_cmit
;
1290 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1291 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1292 if (!old_cmit
|| !new_cmit
) {
1294 return error("Branch %s is missing commits.", b
->name
);
1297 if (!in_merge_bases(old_cmit
, &new_cmit
, 1)) {
1299 warning("Not updating %s"
1300 " (new tip %s does not contain %s)",
1301 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1305 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1306 return error("Unable to update %s", b
->name
);
1310 static void dump_branches(void)
1315 for (i
= 0; i
< branch_table_sz
; i
++) {
1316 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1317 failure
|= update_branch(b
);
1321 static void dump_tags(void)
1323 static const char *msg
= "fast-import";
1325 struct ref_lock
*lock
;
1326 char ref_name
[PATH_MAX
];
1328 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1329 sprintf(ref_name
, "tags/%s", t
->name
);
1330 lock
= lock_ref_sha1(ref_name
, NULL
);
1331 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1332 failure
|= error("Unable to update %s", ref_name
);
1336 static void dump_marks_helper(FILE *f
,
1342 for (k
= 0; k
< 1024; k
++) {
1343 if (m
->data
.sets
[k
])
1344 dump_marks_helper(f
, (base
+ k
) << m
->shift
,
1348 for (k
= 0; k
< 1024; k
++) {
1349 if (m
->data
.marked
[k
])
1350 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1351 sha1_to_hex(m
->data
.marked
[k
]->sha1
));
1356 static void dump_marks(void)
1358 static struct lock_file mark_lock
;
1365 mark_fd
= hold_lock_file_for_update(&mark_lock
, mark_file
, 0);
1367 failure
|= error("Unable to write marks file %s: %s",
1368 mark_file
, strerror(errno
));
1372 f
= fdopen(mark_fd
, "w");
1374 rollback_lock_file(&mark_lock
);
1375 failure
|= error("Unable to write marks file %s: %s",
1376 mark_file
, strerror(errno
));
1380 dump_marks_helper(f
, 0, marks
);
1382 if (commit_lock_file(&mark_lock
))
1383 failure
|= error("Unable to write marks file %s: %s",
1384 mark_file
, strerror(errno
));
1387 static void read_next_command(void)
1389 read_line(&command_buf
, stdin
, '\n');
1392 static void cmd_mark(void)
1394 if (!prefixcmp(command_buf
.buf
, "mark :")) {
1395 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1396 read_next_command();
1402 static void *cmd_data (size_t *size
)
1407 if (prefixcmp(command_buf
.buf
, "data "))
1408 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1410 if (!prefixcmp(command_buf
.buf
+ 5, "<<")) {
1411 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1412 size_t sz
= 8192, term_len
= command_buf
.len
- 5 - 2;
1414 buffer
= xmalloc(sz
);
1416 read_next_command();
1417 if (command_buf
.eof
)
1418 die("EOF in data (terminator '%s' not found)", term
);
1419 if (term_len
== command_buf
.len
1420 && !strcmp(term
, command_buf
.buf
))
1422 if (sz
< (length
+ command_buf
.len
)) {
1423 sz
= sz
* 3 / 2 + 16;
1424 if (sz
< (length
+ command_buf
.len
))
1425 sz
= length
+ command_buf
.len
;
1426 buffer
= xrealloc(buffer
, sz
);
1428 memcpy(buffer
+ length
,
1430 command_buf
.len
- 1);
1431 length
+= command_buf
.len
- 1;
1432 buffer
[length
++] = '\n';
1438 length
= strtoul(command_buf
.buf
+ 5, NULL
, 10);
1439 buffer
= xmalloc(length
);
1440 while (n
< length
) {
1441 size_t s
= fread(buffer
+ n
, 1, length
- n
, stdin
);
1442 if (!s
&& feof(stdin
))
1443 die("EOF in data (%lu bytes remaining)",
1444 (unsigned long)(length
- n
));
1449 if (fgetc(stdin
) != '\n')
1450 die("An lf did not trail the binary data as expected.");
1456 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1458 const char *orig_src
= src
;
1461 strtoul(src
, &endp
, 10);
1462 if (endp
== src
|| *endp
!= ' ')
1466 if (*src
!= '-' && *src
!= '+')
1470 strtoul(src
+ 1, &endp
, 10);
1471 if (endp
== src
|| *endp
|| (endp
- orig_src
) >= maxlen
)
1474 strcpy(result
, orig_src
);
1478 static char *parse_ident(const char *buf
)
1484 gt
= strrchr(buf
, '>');
1486 die("Missing > in ident string: %s", buf
);
1489 die("Missing space after > in ident string: %s", buf
);
1491 name_len
= gt
- buf
;
1492 ident
= xmalloc(name_len
+ 24);
1493 strncpy(ident
, buf
, name_len
);
1497 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1498 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1500 case WHENSPEC_RFC2822
:
1501 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1502 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1505 if (strcmp("now", gt
))
1506 die("Date in ident must be 'now': %s", buf
);
1507 datestamp(ident
+ name_len
, 24);
1514 static void cmd_new_blob(void)
1519 read_next_command();
1523 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, NULL
, next_mark
))
1527 static void unload_one_branch(void)
1529 while (cur_active_branches
1530 && cur_active_branches
>= max_active_branches
) {
1531 uintmax_t min_commit
= ULONG_MAX
;
1532 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1534 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1535 if (e
->last_commit
< min_commit
) {
1537 min_commit
= e
->last_commit
;
1543 e
= p
->active_next_branch
;
1544 p
->active_next_branch
= e
->active_next_branch
;
1546 e
= active_branches
;
1547 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
);
1564 b
->active_next_branch
= active_branches
;
1565 active_branches
= b
;
1566 cur_active_branches
++;
1567 branch_load_count
++;
1571 static void file_change_m(struct branch
*b
)
1573 const char *p
= command_buf
.buf
+ 2;
1576 struct object_entry
*oe
= oe
;
1577 unsigned char sha1
[20];
1578 uint16_t mode
, inline_data
= 0;
1580 p
= get_mode(p
, &mode
);
1582 die("Corrupt mode: %s", command_buf
.buf
);
1584 case S_IFREG
| 0644:
1585 case S_IFREG
| 0755:
1592 die("Corrupt mode: %s", command_buf
.buf
);
1597 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
1598 hashcpy(sha1
, oe
->sha1
);
1600 } else if (!prefixcmp(p
, "inline")) {
1604 if (get_sha1_hex(p
, sha1
))
1605 die("Invalid SHA1: %s", command_buf
.buf
);
1606 oe
= find_object(sha1
);
1610 die("Missing space after SHA1: %s", command_buf
.buf
);
1612 p_uq
= unquote_c_style(p
, &endp
);
1615 die("Garbage after path in: %s", command_buf
.buf
);
1623 p
= p_uq
= xstrdup(p
);
1624 read_next_command();
1626 if (store_object(OBJ_BLOB
, d
, l
, &last_blob
, sha1
, 0))
1629 if (oe
->type
!= OBJ_BLOB
)
1630 die("Not a blob (actually a %s): %s",
1631 command_buf
.buf
, typename(oe
->type
));
1633 enum object_type type
= sha1_object_info(sha1
, NULL
);
1635 die("Blob not found: %s", command_buf
.buf
);
1636 if (type
!= OBJ_BLOB
)
1637 die("Not a blob (actually a %s): %s",
1638 typename(type
), command_buf
.buf
);
1641 tree_content_set(&b
->branch_tree
, p
, sha1
, S_IFREG
| mode
, NULL
);
1645 static void file_change_d(struct branch
*b
)
1647 const char *p
= command_buf
.buf
+ 2;
1651 p_uq
= unquote_c_style(p
, &endp
);
1654 die("Garbage after path in: %s", command_buf
.buf
);
1657 tree_content_remove(&b
->branch_tree
, p
, NULL
);
1661 static void file_change_r(struct branch
*b
)
1666 struct tree_entry leaf
;
1668 s
= command_buf
.buf
+ 2;
1669 s_uq
= unquote_c_style(s
, &endp
);
1672 die("Missing space after source: %s", command_buf
.buf
);
1675 endp
= strchr(s
, ' ');
1677 die("Missing space after source: %s", command_buf
.buf
);
1678 s_uq
= xmalloc(endp
- s
+ 1);
1679 memcpy(s_uq
, s
, endp
- s
);
1686 die("Missing dest: %s", command_buf
.buf
);
1689 d_uq
= unquote_c_style(d
, &endp
);
1692 die("Garbage after dest in: %s", command_buf
.buf
);
1696 memset(&leaf
, 0, sizeof(leaf
));
1697 tree_content_remove(&b
->branch_tree
, s
, &leaf
);
1698 if (!leaf
.versions
[1].mode
)
1699 die("Path %s not in branch", s
);
1700 tree_content_set(&b
->branch_tree
, d
,
1701 leaf
.versions
[1].sha1
,
1702 leaf
.versions
[1].mode
,
1709 static void file_change_deleteall(struct branch
*b
)
1711 release_tree_content_recursive(b
->branch_tree
.tree
);
1712 hashclr(b
->branch_tree
.versions
[0].sha1
);
1713 hashclr(b
->branch_tree
.versions
[1].sha1
);
1714 load_tree(&b
->branch_tree
);
1717 static void cmd_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
1719 if (!buf
|| size
< 46)
1720 die("Not a valid commit: %s", sha1_to_hex(b
->sha1
));
1721 if (memcmp("tree ", buf
, 5)
1722 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
1723 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
1724 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1725 b
->branch_tree
.versions
[1].sha1
);
1728 static void cmd_from_existing(struct branch
*b
)
1730 if (is_null_sha1(b
->sha1
)) {
1731 hashclr(b
->branch_tree
.versions
[0].sha1
);
1732 hashclr(b
->branch_tree
.versions
[1].sha1
);
1737 buf
= read_object_with_reference(b
->sha1
,
1738 commit_type
, &size
, b
->sha1
);
1739 cmd_from_commit(b
, buf
, size
);
1744 static void cmd_from(struct branch
*b
)
1749 if (prefixcmp(command_buf
.buf
, "from "))
1752 if (b
->branch_tree
.tree
) {
1753 release_tree_content_recursive(b
->branch_tree
.tree
);
1754 b
->branch_tree
.tree
= NULL
;
1757 from
= strchr(command_buf
.buf
, ' ') + 1;
1758 s
= lookup_branch(from
);
1760 die("Can't create a branch from itself: %s", b
->name
);
1762 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
1763 hashcpy(b
->sha1
, s
->sha1
);
1764 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
1765 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
1766 } else if (*from
== ':') {
1767 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1768 struct object_entry
*oe
= find_mark(idnum
);
1769 if (oe
->type
!= OBJ_COMMIT
)
1770 die("Mark :%" PRIuMAX
" not a commit", idnum
);
1771 hashcpy(b
->sha1
, oe
->sha1
);
1772 if (oe
->pack_id
!= MAX_PACK_ID
) {
1774 char *buf
= gfi_unpack_entry(oe
, &size
);
1775 cmd_from_commit(b
, buf
, size
);
1778 cmd_from_existing(b
);
1779 } else if (!get_sha1(from
, b
->sha1
))
1780 cmd_from_existing(b
);
1782 die("Invalid ref name or SHA1 expression: %s", from
);
1784 read_next_command();
1787 static struct hash_list
*cmd_merge(unsigned int *count
)
1789 struct hash_list
*list
= NULL
, *n
, *e
= e
;
1794 while (!prefixcmp(command_buf
.buf
, "merge ")) {
1795 from
= strchr(command_buf
.buf
, ' ') + 1;
1796 n
= xmalloc(sizeof(*n
));
1797 s
= lookup_branch(from
);
1799 hashcpy(n
->sha1
, s
->sha1
);
1800 else if (*from
== ':') {
1801 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
1802 struct object_entry
*oe
= find_mark(idnum
);
1803 if (oe
->type
!= OBJ_COMMIT
)
1804 die("Mark :%" PRIuMAX
" not a commit", idnum
);
1805 hashcpy(n
->sha1
, oe
->sha1
);
1806 } else if (!get_sha1(from
, n
->sha1
)) {
1808 char *buf
= read_object_with_reference(n
->sha1
,
1809 commit_type
, &size
, n
->sha1
);
1810 if (!buf
|| size
< 46)
1811 die("Not a valid commit: %s", from
);
1814 die("Invalid ref name or SHA1 expression: %s", from
);
1823 read_next_command();
1828 static void cmd_new_commit(void)
1834 char *author
= NULL
;
1835 char *committer
= NULL
;
1836 struct hash_list
*merge_list
= NULL
;
1837 unsigned int merge_count
;
1839 /* Obtain the branch name from the rest of our command */
1840 sp
= strchr(command_buf
.buf
, ' ') + 1;
1841 b
= lookup_branch(sp
);
1845 read_next_command();
1847 if (!prefixcmp(command_buf
.buf
, "author ")) {
1848 author
= parse_ident(command_buf
.buf
+ 7);
1849 read_next_command();
1851 if (!prefixcmp(command_buf
.buf
, "committer ")) {
1852 committer
= parse_ident(command_buf
.buf
+ 10);
1853 read_next_command();
1856 die("Expected committer but didn't get one");
1857 msg
= cmd_data(&msglen
);
1858 read_next_command();
1860 merge_list
= cmd_merge(&merge_count
);
1862 /* ensure the branch is active/loaded */
1863 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
1864 unload_one_branch();
1870 if (1 == command_buf
.len
)
1872 else if (!prefixcmp(command_buf
.buf
, "M "))
1874 else if (!prefixcmp(command_buf
.buf
, "D "))
1876 else if (!prefixcmp(command_buf
.buf
, "R "))
1878 else if (!strcmp("deleteall", command_buf
.buf
))
1879 file_change_deleteall(b
);
1881 die("Unsupported file_change: %s", command_buf
.buf
);
1882 read_next_command();
1885 /* build the tree and the commit */
1886 store_tree(&b
->branch_tree
);
1887 hashcpy(b
->branch_tree
.versions
[0].sha1
,
1888 b
->branch_tree
.versions
[1].sha1
);
1889 size_dbuf(&new_data
, 114 + msglen
1892 ? strlen(author
) + strlen(committer
)
1893 : 2 * strlen(committer
)));
1894 sp
= new_data
.buffer
;
1895 sp
+= sprintf(sp
, "tree %s\n",
1896 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
1897 if (!is_null_sha1(b
->sha1
))
1898 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(b
->sha1
));
1899 while (merge_list
) {
1900 struct hash_list
*next
= merge_list
->next
;
1901 sp
+= sprintf(sp
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
1905 sp
+= sprintf(sp
, "author %s\n", author
? author
: committer
);
1906 sp
+= sprintf(sp
, "committer %s\n", committer
);
1908 memcpy(sp
, msg
, msglen
);
1914 if (!store_object(OBJ_COMMIT
,
1915 new_data
.buffer
, sp
- (char*)new_data
.buffer
,
1916 NULL
, b
->sha1
, next_mark
))
1917 b
->pack_id
= pack_id
;
1918 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
1921 static void cmd_new_tag(void)
1930 uintmax_t from_mark
= 0;
1931 unsigned char sha1
[20];
1933 /* Obtain the new tag name from the rest of our command */
1934 sp
= strchr(command_buf
.buf
, ' ') + 1;
1935 t
= pool_alloc(sizeof(struct tag
));
1937 t
->name
= pool_strdup(sp
);
1939 last_tag
->next_tag
= t
;
1943 read_next_command();
1946 if (prefixcmp(command_buf
.buf
, "from "))
1947 die("Expected from command, got %s", command_buf
.buf
);
1948 from
= strchr(command_buf
.buf
, ' ') + 1;
1949 s
= lookup_branch(from
);
1951 hashcpy(sha1
, s
->sha1
);
1952 } else if (*from
== ':') {
1953 struct object_entry
*oe
;
1954 from_mark
= strtoumax(from
+ 1, NULL
, 10);
1955 oe
= find_mark(from_mark
);
1956 if (oe
->type
!= OBJ_COMMIT
)
1957 die("Mark :%" PRIuMAX
" not a commit", from_mark
);
1958 hashcpy(sha1
, oe
->sha1
);
1959 } else if (!get_sha1(from
, sha1
)) {
1963 buf
= read_object_with_reference(sha1
,
1964 commit_type
, &size
, sha1
);
1965 if (!buf
|| size
< 46)
1966 die("Not a valid commit: %s", from
);
1969 die("Invalid ref name or SHA1 expression: %s", from
);
1970 read_next_command();
1973 if (prefixcmp(command_buf
.buf
, "tagger "))
1974 die("Expected tagger command, got %s", command_buf
.buf
);
1975 tagger
= parse_ident(command_buf
.buf
+ 7);
1977 /* tag payload/message */
1978 read_next_command();
1979 msg
= cmd_data(&msglen
);
1981 /* build the tag object */
1982 size_dbuf(&new_data
, 67+strlen(t
->name
)+strlen(tagger
)+msglen
);
1983 sp
= new_data
.buffer
;
1984 sp
+= sprintf(sp
, "object %s\n", sha1_to_hex(sha1
));
1985 sp
+= sprintf(sp
, "type %s\n", commit_type
);
1986 sp
+= sprintf(sp
, "tag %s\n", t
->name
);
1987 sp
+= sprintf(sp
, "tagger %s\n", tagger
);
1989 memcpy(sp
, msg
, msglen
);
1994 if (store_object(OBJ_TAG
, new_data
.buffer
,
1995 sp
- (char*)new_data
.buffer
,
1997 t
->pack_id
= MAX_PACK_ID
;
1999 t
->pack_id
= pack_id
;
2002 static void cmd_reset_branch(void)
2007 /* Obtain the branch name from the rest of our command */
2008 sp
= strchr(command_buf
.buf
, ' ') + 1;
2009 b
= lookup_branch(sp
);
2012 hashclr(b
->branch_tree
.versions
[0].sha1
);
2013 hashclr(b
->branch_tree
.versions
[1].sha1
);
2014 if (b
->branch_tree
.tree
) {
2015 release_tree_content_recursive(b
->branch_tree
.tree
);
2016 b
->branch_tree
.tree
= NULL
;
2021 read_next_command();
2025 static void cmd_checkpoint(void)
2033 read_next_command();
2036 static void import_marks(const char *input_file
)
2039 FILE *f
= fopen(input_file
, "r");
2041 die("cannot read %s: %s", input_file
, strerror(errno
));
2042 while (fgets(line
, sizeof(line
), f
)) {
2045 unsigned char sha1
[20];
2046 struct object_entry
*e
;
2048 end
= strchr(line
, '\n');
2049 if (line
[0] != ':' || !end
)
2050 die("corrupt mark line: %s", line
);
2052 mark
= strtoumax(line
+ 1, &end
, 10);
2053 if (!mark
|| end
== line
+ 1
2054 || *end
!= ' ' || get_sha1(end
+ 1, sha1
))
2055 die("corrupt mark line: %s", line
);
2056 e
= find_object(sha1
);
2058 enum object_type type
= sha1_object_info(sha1
, NULL
);
2060 die("object not found: %s", sha1_to_hex(sha1
));
2061 e
= insert_object(sha1
);
2063 e
->pack_id
= MAX_PACK_ID
;
2064 e
->offset
= 1; /* just not zero! */
2066 insert_mark(mark
, e
);
2071 static const char fast_import_usage
[] =
2072 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
2074 int main(int argc
, const char **argv
)
2076 int i
, show_stats
= 1;
2078 git_config(git_default_config
);
2079 alloc_objects(object_entry_alloc
);
2080 strbuf_init(&command_buf
);
2081 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
2082 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
2083 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
2084 marks
= pool_calloc(1, sizeof(struct mark_set
));
2086 for (i
= 1; i
< argc
; i
++) {
2087 const char *a
= argv
[i
];
2089 if (*a
!= '-' || !strcmp(a
, "--"))
2091 else if (!prefixcmp(a
, "--date-format=")) {
2092 const char *fmt
= a
+ 14;
2093 if (!strcmp(fmt
, "raw"))
2094 whenspec
= WHENSPEC_RAW
;
2095 else if (!strcmp(fmt
, "rfc2822"))
2096 whenspec
= WHENSPEC_RFC2822
;
2097 else if (!strcmp(fmt
, "now"))
2098 whenspec
= WHENSPEC_NOW
;
2100 die("unknown --date-format argument %s", fmt
);
2102 else if (!prefixcmp(a
, "--max-pack-size="))
2103 max_packsize
= strtoumax(a
+ 16, NULL
, 0) * 1024 * 1024;
2104 else if (!prefixcmp(a
, "--depth="))
2105 max_depth
= strtoul(a
+ 8, NULL
, 0);
2106 else if (!prefixcmp(a
, "--active-branches="))
2107 max_active_branches
= strtoul(a
+ 18, NULL
, 0);
2108 else if (!prefixcmp(a
, "--import-marks="))
2109 import_marks(a
+ 15);
2110 else if (!prefixcmp(a
, "--export-marks="))
2112 else if (!prefixcmp(a
, "--export-pack-edges=")) {
2115 pack_edges
= fopen(a
+ 20, "a");
2117 die("Cannot open %s: %s", a
+ 20, strerror(errno
));
2118 } else if (!strcmp(a
, "--force"))
2120 else if (!strcmp(a
, "--quiet"))
2122 else if (!strcmp(a
, "--stats"))
2125 die("unknown option %s", a
);
2128 usage(fast_import_usage
);
2130 prepare_packed_git();
2133 read_next_command();
2134 if (command_buf
.eof
)
2136 else if (!strcmp("blob", command_buf
.buf
))
2138 else if (!prefixcmp(command_buf
.buf
, "commit "))
2140 else if (!prefixcmp(command_buf
.buf
, "tag "))
2142 else if (!prefixcmp(command_buf
.buf
, "reset "))
2144 else if (!strcmp("checkpoint", command_buf
.buf
))
2147 die("Unsupported command: %s", command_buf
.buf
);
2160 uintmax_t total_count
= 0, duplicate_count
= 0;
2161 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
2162 total_count
+= object_count_by_type
[i
];
2163 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
2164 duplicate_count
+= duplicate_count_by_type
[i
];
2166 fprintf(stderr
, "%s statistics:\n", argv
[0]);
2167 fprintf(stderr
, "---------------------------------------------------------------------\n");
2168 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
2169 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
2170 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
]);
2171 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
]);
2172 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
]);
2173 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
]);
2174 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
2175 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
2176 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
2177 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
2178 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
2179 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
2180 fprintf(stderr
, "---------------------------------------------------------------------\n");
2182 fprintf(stderr
, "---------------------------------------------------------------------\n");
2183 fprintf(stderr
, "\n");
2186 return failure
? 1 : 0;