2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
16 #include "string-list.h"
18 #include "parse-options.h"
21 #define REF_HANDLED (ALL_REV_FLAGS + 1)
23 static const char *fast_export_usage
[] = {
24 "git fast-export [rev-list-opts]",
29 static enum { ABORT
, VERBATIM
, WARN
, STRIP
} signed_tag_mode
= ABORT
;
30 static enum { ERROR
, DROP
, REWRITE
} tag_of_filtered_mode
= ERROR
;
31 static int fake_missing_tagger
;
32 static int use_done_feature
;
36 static int parse_opt_signed_tag_mode(const struct option
*opt
,
37 const char *arg
, int unset
)
39 if (unset
|| !strcmp(arg
, "abort"))
40 signed_tag_mode
= ABORT
;
41 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
42 signed_tag_mode
= VERBATIM
;
43 else if (!strcmp(arg
, "warn"))
44 signed_tag_mode
= WARN
;
45 else if (!strcmp(arg
, "strip"))
46 signed_tag_mode
= STRIP
;
48 return error("Unknown signed-tag mode: %s", arg
);
52 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
53 const char *arg
, int unset
)
55 if (unset
|| !strcmp(arg
, "abort"))
56 tag_of_filtered_mode
= ERROR
;
57 else if (!strcmp(arg
, "drop"))
58 tag_of_filtered_mode
= DROP
;
59 else if (!strcmp(arg
, "rewrite"))
60 tag_of_filtered_mode
= REWRITE
;
62 return error("Unknown tag-of-filtered mode: %s", arg
);
66 static struct decoration idnums
;
67 static uint32_t last_idnum
;
69 static int has_unshown_parent(struct commit
*commit
)
71 struct commit_list
*parent
;
73 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
74 if (!(parent
->item
->object
.flags
& SHOWN
) &&
75 !(parent
->item
->object
.flags
& UNINTERESTING
))
80 /* Since intptr_t is C99, we do not use it here */
81 static inline uint32_t *mark_to_ptr(uint32_t mark
)
83 return ((uint32_t *)NULL
) + mark
;
86 static inline uint32_t ptr_to_mark(void * mark
)
88 return (uint32_t *)mark
- (uint32_t *)NULL
;
91 static inline void mark_object(struct object
*object
, uint32_t mark
)
93 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
96 static inline void mark_next_object(struct object
*object
)
98 mark_object(object
, ++last_idnum
);
101 static int get_object_mark(struct object
*object
)
103 void *decoration
= lookup_decoration(&idnums
, object
);
106 return ptr_to_mark(decoration
);
109 static void show_progress(void)
111 static int counter
= 0;
114 if ((++counter
% progress
) == 0)
115 printf("progress %d objects\n", counter
);
118 static void handle_object(const unsigned char *sha1
)
121 enum object_type type
;
123 struct object
*object
;
128 if (is_null_sha1(sha1
))
131 object
= parse_object(sha1
);
133 die ("Could not read blob %s", sha1_to_hex(sha1
));
135 if (object
->flags
& SHOWN
)
138 buf
= read_sha1_file(sha1
, &type
, &size
);
140 die ("Could not read blob %s", sha1_to_hex(sha1
));
142 mark_next_object(object
);
144 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
145 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
146 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1
));
151 object
->flags
|= SHOWN
;
155 static int depth_first(const void *a_
, const void *b_
)
157 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
158 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
159 const char *name_a
, *name_b
;
160 int len_a
, len_b
, len
;
163 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
164 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
166 len_a
= strlen(name_a
);
167 len_b
= strlen(name_b
);
168 len
= (len_a
< len_b
) ? len_a
: len_b
;
170 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
171 cmp
= memcmp(name_a
, name_b
, len
);
178 * Move 'R'ename entries last so that all references of the file
179 * appear in the output before it is renamed (e.g., when a file
180 * was copied and renamed in the same commit).
182 return (a
->status
== 'R') - (b
->status
== 'R');
185 static void print_path(const char *path
)
187 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
189 quote_c_style(path
, NULL
, stdout
, 0);
194 static void show_filemodify(struct diff_queue_struct
*q
,
195 struct diff_options
*options
, void *data
)
200 * Handle files below a directory first, in case they are all deleted
201 * and the directory changes to a file or symlink.
203 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), depth_first
);
205 for (i
= 0; i
< q
->nr
; i
++) {
206 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
207 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
209 switch (q
->queue
[i
]->status
) {
210 case DIFF_STATUS_DELETED
:
212 print_path(spec
->path
);
216 case DIFF_STATUS_COPIED
:
217 case DIFF_STATUS_RENAMED
:
218 printf("%c ", q
->queue
[i
]->status
);
219 print_path(ospec
->path
);
221 print_path(spec
->path
);
224 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
225 ospec
->mode
== spec
->mode
)
229 case DIFF_STATUS_TYPE_CHANGED
:
230 case DIFF_STATUS_MODIFIED
:
231 case DIFF_STATUS_ADDED
:
233 * Links refer to objects in another repositories;
234 * output the SHA-1 verbatim.
236 if (no_data
|| S_ISGITLINK(spec
->mode
))
237 printf("M %06o %s ", spec
->mode
,
238 sha1_to_hex(spec
->sha1
));
240 struct object
*object
= lookup_object(spec
->sha1
);
241 printf("M %06o :%d ", spec
->mode
,
242 get_object_mark(object
));
244 print_path(spec
->path
);
249 die("Unexpected comparison status '%c' for %s, %s",
251 ospec
->path
? ospec
->path
: "none",
252 spec
->path
? spec
->path
: "none");
257 static const char *find_encoding(const char *begin
, const char *end
)
259 const char *needle
= "\nencoding ";
262 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
263 needle
, strlen(needle
));
265 return git_commit_encoding
;
266 bol
+= strlen(needle
);
267 eol
= strchrnul(bol
, '\n');
272 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
274 int saved_output_format
= rev
->diffopt
.output_format
;
275 const char *author
, *author_end
, *committer
, *committer_end
;
276 const char *encoding
, *message
;
277 char *reencoded
= NULL
;
278 struct commit_list
*p
;
281 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
283 parse_commit(commit
);
284 author
= strstr(commit
->buffer
, "\nauthor ");
286 die ("Could not find author in commit %s",
287 sha1_to_hex(commit
->object
.sha1
));
289 author_end
= strchrnul(author
, '\n');
290 committer
= strstr(author_end
, "\ncommitter ");
292 die ("Could not find committer in commit %s",
293 sha1_to_hex(commit
->object
.sha1
));
295 committer_end
= strchrnul(committer
, '\n');
296 message
= strstr(committer_end
, "\n\n");
297 encoding
= find_encoding(committer_end
, message
);
301 if (commit
->parents
&&
302 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
304 parse_commit(commit
->parents
->item
);
305 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
306 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
309 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
312 /* Export the referenced blobs, and remember the marks. */
313 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
314 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
315 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
317 mark_next_object(&commit
->object
);
318 if (!is_encoding_utf8(encoding
))
319 reencoded
= reencode_string(message
, "UTF-8", encoding
);
320 if (!commit
->parents
)
321 printf("reset %s\n", (const char*)commit
->util
);
322 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
323 (const char *)commit
->util
, last_idnum
,
324 (int)(author_end
- author
), author
,
325 (int)(committer_end
- committer
), committer
,
327 ? strlen(reencoded
) : message
328 ? strlen(message
) : 0),
329 reencoded
? reencoded
: message
? message
: "");
332 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
333 int mark
= get_object_mark(&p
->item
->object
);
337 printf("from :%d\n", mark
);
339 printf("merge :%d\n", mark
);
344 printf("deleteall\n");
345 log_tree_diff_flush(rev
);
346 rev
->diffopt
.output_format
= saved_output_format
;
353 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
355 struct commit
*commit
;
356 while (commits
->nr
) {
357 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
358 if (has_unshown_parent(commit
))
360 handle_commit(commit
, revs
);
365 static void handle_tag(const char *name
, struct tag
*tag
)
368 enum object_type type
;
370 const char *tagger
, *tagger_end
, *message
;
371 size_t message_size
= 0;
372 struct object
*tagged
;
376 /* Trees have no identifer in fast-export output, thus we have no way
377 * to output tags of trees, tags of tags of trees, etc. Simply omit
380 tagged
= tag
->tagged
;
381 while (tagged
->type
== OBJ_TAG
) {
382 tagged
= ((struct tag
*)tagged
)->tagged
;
384 if (tagged
->type
== OBJ_TREE
) {
385 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
386 sha1_to_hex(tag
->object
.sha1
));
390 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
392 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
393 message
= memmem(buf
, size
, "\n\n", 2);
396 message_size
= strlen(message
);
398 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
400 if (fake_missing_tagger
)
401 tagger
= "tagger Unspecified Tagger "
402 "<unspecified-tagger> 0 +0000";
405 tagger_end
= tagger
+ strlen(tagger
);
408 tagger_end
= strchrnul(tagger
, '\n');
411 /* handle signed tags */
413 const char *signature
= strstr(message
,
414 "\n-----BEGIN PGP SIGNATURE-----\n");
416 switch(signed_tag_mode
) {
418 die ("Encountered signed tag %s; use "
419 "--signed-tag=<mode> to handle it.",
420 sha1_to_hex(tag
->object
.sha1
));
422 warning ("Exporting signed tag %s",
423 sha1_to_hex(tag
->object
.sha1
));
428 message_size
= signature
+ 1 - message
;
433 /* handle tag->tagged having been filtered out due to paths specified */
434 tagged
= tag
->tagged
;
435 tagged_mark
= get_object_mark(tagged
);
437 switch(tag_of_filtered_mode
) {
439 die ("Tag %s tags unexported object; use "
440 "--tag-of-filtered-object=<mode> to handle it.",
441 sha1_to_hex(tag
->object
.sha1
));
443 /* Ignore this tag altogether */
446 if (tagged
->type
!= OBJ_COMMIT
) {
447 die ("Tag %s tags unexported %s!",
448 sha1_to_hex(tag
->object
.sha1
),
449 typename(tagged
->type
));
451 p
= (struct commit
*)tagged
;
453 if (p
->parents
&& p
->parents
->next
)
455 if (p
->object
.flags
& UNINTERESTING
)
457 if (!(p
->object
.flags
& TREESAME
))
460 die ("Can't find replacement commit for tag %s\n",
461 sha1_to_hex(tag
->object
.sha1
));
462 p
= p
->parents
->item
;
464 tagged_mark
= get_object_mark(&p
->object
);
468 if (!prefixcmp(name
, "refs/tags/"))
470 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
472 (int)(tagger_end
- tagger
), tagger
,
473 tagger
== tagger_end
? "" : "\n",
474 (int)message_size
, (int)message_size
, message
? message
: "");
477 static void get_tags_and_duplicates(struct object_array
*pending
,
478 struct string_list
*refs
,
479 struct string_list
*extra_refs
)
484 for (i
= 0; i
< pending
->nr
; i
++) {
485 struct object_array_entry
*e
= pending
->objects
+ i
;
486 unsigned char sha1
[20];
487 struct commit
*commit
= commit
;
490 if ((e
->flags
& UNINTERESTING
) || dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
493 switch (e
->item
->type
) {
495 commit
= (struct commit
*)e
->item
;
498 tag
= (struct tag
*)e
->item
;
500 /* handle nested tags */
501 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
502 parse_object(tag
->object
.sha1
);
503 string_list_append(extra_refs
, full_name
)->util
= tag
;
504 tag
= (struct tag
*)tag
->tagged
;
507 die ("Tag %s points nowhere?", e
->name
);
508 switch(tag
->object
.type
) {
510 commit
= (struct commit
*)tag
;
513 handle_object(tag
->object
.sha1
);
515 default: /* OBJ_TAG (nested tags) is already handled */
516 warning("Tag points to object of unexpected type %s, skipping.",
517 typename(tag
->object
.type
));
522 warning("%s: Unexpected object of type %s, skipping.",
524 typename(e
->item
->type
));
528 /* more than one name for the same object */
529 string_list_append(extra_refs
, full_name
)->util
= commit
;
531 commit
->util
= full_name
;
532 /* we might need to set this ref explicitly */
533 string_list_append(refs
, full_name
)->util
= commit
;
538 static void handle_reset(const char *name
, struct object
*object
)
540 int mark
= get_object_mark(object
);
543 printf("reset %s\nfrom :%d\n\n", name
,
544 get_object_mark(object
));
546 printf("reset %s\nfrom %s\n\n", name
,
547 sha1_to_hex(object
->sha1
));
550 static void handle_tags_and_duplicates(struct string_list
*refs
, struct string_list
*extra_refs
)
554 /* even if no commits were exported, we need to export the ref */
555 for (i
= refs
->nr
- 1; i
>= 0; i
--) {
556 const char *name
= refs
->items
[i
].string
;
557 struct object
*object
= refs
->items
[i
].util
;
559 if (!(object
->flags
& REF_HANDLED
)) {
560 if (object
->type
& OBJ_TAG
)
561 handle_tag(name
, (struct tag
*)object
);
563 if (!prefixcmp(name
, "refs/tags/") &&
564 (tag_of_filtered_mode
!= REWRITE
||
565 !get_object_mark(object
)))
567 handle_reset(name
, object
);
568 object
->flags
|= REF_HANDLED
;
573 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
574 const char *name
= extra_refs
->items
[i
].string
;
575 struct object
*object
= extra_refs
->items
[i
].util
;
576 switch (object
->type
) {
578 handle_tag(name
, (struct tag
*)object
);
581 /* create refs pointing to already seen commits */
582 handle_reset(name
, object
);
589 static void export_marks(char *file
)
593 struct object_decoration
*deco
= idnums
.hash
;
597 f
= fopen(file
, "w");
599 die_errno("Unable to open marks file %s for writing.", file
);
601 for (i
= 0; i
< idnums
.size
; i
++) {
602 if (deco
->base
&& deco
->base
->type
== 1) {
603 mark
= ptr_to_mark(deco
->decoration
);
604 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
605 sha1_to_hex(deco
->base
->sha1
)) < 0) {
616 error("Unable to write marks file %s.", file
);
619 static void import_marks(char *input_file
)
622 FILE *f
= fopen(input_file
, "r");
624 die_errno("cannot read '%s'", input_file
);
626 while (fgets(line
, sizeof(line
), f
)) {
628 char *line_end
, *mark_end
;
629 unsigned char sha1
[20];
630 struct object
*object
;
632 line_end
= strchr(line
, '\n');
633 if (line
[0] != ':' || !line_end
)
634 die("corrupt mark line: %s", line
);
637 mark
= strtoumax(line
+ 1, &mark_end
, 10);
638 if (!mark
|| mark_end
== line
+ 1
639 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
640 die("corrupt mark line: %s", line
);
642 object
= parse_object(sha1
);
644 die ("Could not read blob %s", sha1_to_hex(sha1
));
646 if (object
->flags
& SHOWN
)
647 error("Object %s already has a mark", sha1_to_hex(sha1
));
649 mark_object(object
, mark
);
650 if (last_idnum
< mark
)
653 object
->flags
|= SHOWN
;
658 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
660 struct rev_info revs
;
661 struct object_array commits
= OBJECT_ARRAY_INIT
;
662 struct string_list refs
= STRING_LIST_INIT_NODUP
, extra_refs
= STRING_LIST_INIT_NODUP
;
663 struct commit
*commit
;
664 char *export_filename
= NULL
, *import_filename
= NULL
;
665 struct option options
[] = {
666 OPT_INTEGER(0, "progress", &progress
,
667 "show progress after <n> objects"),
668 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, "mode",
669 "select handling of signed tags",
670 parse_opt_signed_tag_mode
),
671 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, "mode",
672 "select handling of tags that tag filtered objects",
673 parse_opt_tag_of_filtered_mode
),
674 OPT_STRING(0, "export-marks", &export_filename
, "file",
675 "Dump marks to this file"),
676 OPT_STRING(0, "import-marks", &import_filename
, "file",
677 "Import marks from this file"),
678 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger
,
679 "Fake a tagger when tags lack one"),
680 OPT_BOOLEAN(0, "full-tree", &full_tree
,
681 "Output full tree for each commit"),
682 OPT_BOOLEAN(0, "use-done-feature", &use_done_feature
,
683 "Use the done feature to terminate the stream"),
684 OPT_BOOL(0, "no-data", &no_data
, "Skip output of blob data"),
689 usage_with_options (fast_export_usage
, options
);
691 /* we handle encodings */
692 git_config(git_default_config
, NULL
);
694 init_revisions(&revs
, prefix
);
696 revs
.show_source
= 1;
697 revs
.rewrite_parents
= 1;
698 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
699 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
, 0);
701 usage_with_options (fast_export_usage
, options
);
703 if (use_done_feature
)
704 printf("feature done\n");
707 import_marks(import_filename
);
709 if (import_filename
&& revs
.prune_data
.nr
)
712 get_tags_and_duplicates(&revs
.pending
, &refs
, &extra_refs
);
714 if (prepare_revision_walk(&revs
))
715 die("revision walk setup failed");
716 revs
.diffopt
.format_callback
= show_filemodify
;
717 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
718 while ((commit
= get_revision(&revs
))) {
719 if (has_unshown_parent(commit
)) {
720 add_object_array(&commit
->object
, NULL
, &commits
);
723 handle_commit(commit
, &revs
);
724 commit
->object
.flags
|= REF_HANDLED
;
725 handle_tail(&commits
, &revs
);
729 handle_tags_and_duplicates(&refs
, &extra_refs
);
732 export_marks(export_filename
);
734 if (use_done_feature
)