2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
16 #include "string-list.h"
18 #include "parse-options.h"
21 static const char *fast_export_usage
[] = {
22 N_("git fast-export [rev-list-opts]"),
27 static enum { ABORT
, VERBATIM
, WARN
, WARN_STRIP
, STRIP
} signed_tag_mode
= ABORT
;
28 static enum { ERROR
, DROP
, REWRITE
} tag_of_filtered_mode
= ERROR
;
29 static int fake_missing_tagger
;
30 static int use_done_feature
;
33 static struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
35 static int parse_opt_signed_tag_mode(const struct option
*opt
,
36 const char *arg
, int unset
)
38 if (unset
|| !strcmp(arg
, "abort"))
39 signed_tag_mode
= ABORT
;
40 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
41 signed_tag_mode
= VERBATIM
;
42 else if (!strcmp(arg
, "warn"))
43 signed_tag_mode
= WARN
;
44 else if (!strcmp(arg
, "warn-strip"))
45 signed_tag_mode
= WARN_STRIP
;
46 else if (!strcmp(arg
, "strip"))
47 signed_tag_mode
= STRIP
;
49 return error("Unknown signed-tags mode: %s", arg
);
53 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
54 const char *arg
, int unset
)
56 if (unset
|| !strcmp(arg
, "abort"))
57 tag_of_filtered_mode
= ERROR
;
58 else if (!strcmp(arg
, "drop"))
59 tag_of_filtered_mode
= DROP
;
60 else if (!strcmp(arg
, "rewrite"))
61 tag_of_filtered_mode
= REWRITE
;
63 return error("Unknown tag-of-filtered mode: %s", arg
);
67 static struct decoration idnums
;
68 static uint32_t last_idnum
;
70 static int has_unshown_parent(struct commit
*commit
)
72 struct commit_list
*parent
;
74 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
75 if (!(parent
->item
->object
.flags
& SHOWN
) &&
76 !(parent
->item
->object
.flags
& UNINTERESTING
))
81 /* Since intptr_t is C99, we do not use it here */
82 static inline uint32_t *mark_to_ptr(uint32_t mark
)
84 return ((uint32_t *)NULL
) + mark
;
87 static inline uint32_t ptr_to_mark(void * mark
)
89 return (uint32_t *)mark
- (uint32_t *)NULL
;
92 static inline void mark_object(struct object
*object
, uint32_t mark
)
94 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
97 static inline void mark_next_object(struct object
*object
)
99 mark_object(object
, ++last_idnum
);
102 static int get_object_mark(struct object
*object
)
104 void *decoration
= lookup_decoration(&idnums
, object
);
107 return ptr_to_mark(decoration
);
110 static void show_progress(void)
112 static int counter
= 0;
115 if ((++counter
% progress
) == 0)
116 printf("progress %d objects\n", counter
);
119 static void export_blob(const unsigned char *sha1
)
122 enum object_type type
;
124 struct object
*object
;
130 if (is_null_sha1(sha1
))
133 object
= lookup_object(sha1
);
134 if (object
&& object
->flags
& SHOWN
)
137 buf
= read_sha1_file(sha1
, &type
, &size
);
139 die ("Could not read blob %s", sha1_to_hex(sha1
));
140 if (check_sha1_signature(sha1
, buf
, size
, typename(type
)) < 0)
141 die("sha1 mismatch in blob %s", sha1_to_hex(sha1
));
142 object
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
144 die("Could not read blob %s", sha1_to_hex(sha1
));
146 mark_next_object(object
);
148 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
149 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
150 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1
));
155 object
->flags
|= SHOWN
;
160 static int depth_first(const void *a_
, const void *b_
)
162 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
163 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
164 const char *name_a
, *name_b
;
165 int len_a
, len_b
, len
;
168 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
169 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
171 len_a
= strlen(name_a
);
172 len_b
= strlen(name_b
);
173 len
= (len_a
< len_b
) ? len_a
: len_b
;
175 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
176 cmp
= memcmp(name_a
, name_b
, len
);
183 * Move 'R'ename entries last so that all references of the file
184 * appear in the output before it is renamed (e.g., when a file
185 * was copied and renamed in the same commit).
187 return (a
->status
== 'R') - (b
->status
== 'R');
190 static void print_path(const char *path
)
192 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
194 quote_c_style(path
, NULL
, stdout
, 0);
195 else if (strchr(path
, ' '))
196 printf("\"%s\"", path
);
201 static void show_filemodify(struct diff_queue_struct
*q
,
202 struct diff_options
*options
, void *data
)
207 * Handle files below a directory first, in case they are all deleted
208 * and the directory changes to a file or symlink.
210 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), depth_first
);
212 for (i
= 0; i
< q
->nr
; i
++) {
213 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
214 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
216 switch (q
->queue
[i
]->status
) {
217 case DIFF_STATUS_DELETED
:
219 print_path(spec
->path
);
223 case DIFF_STATUS_COPIED
:
224 case DIFF_STATUS_RENAMED
:
225 printf("%c ", q
->queue
[i
]->status
);
226 print_path(ospec
->path
);
228 print_path(spec
->path
);
231 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
232 ospec
->mode
== spec
->mode
)
236 case DIFF_STATUS_TYPE_CHANGED
:
237 case DIFF_STATUS_MODIFIED
:
238 case DIFF_STATUS_ADDED
:
240 * Links refer to objects in another repositories;
241 * output the SHA-1 verbatim.
243 if (no_data
|| S_ISGITLINK(spec
->mode
))
244 printf("M %06o %s ", spec
->mode
,
245 sha1_to_hex(spec
->sha1
));
247 struct object
*object
= lookup_object(spec
->sha1
);
248 printf("M %06o :%d ", spec
->mode
,
249 get_object_mark(object
));
251 print_path(spec
->path
);
256 die("Unexpected comparison status '%c' for %s, %s",
258 ospec
->path
? ospec
->path
: "none",
259 spec
->path
? spec
->path
: "none");
264 static const char *find_encoding(const char *begin
, const char *end
)
266 const char *needle
= "\nencoding ";
269 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
270 needle
, strlen(needle
));
272 return git_commit_encoding
;
273 bol
+= strlen(needle
);
274 eol
= strchrnul(bol
, '\n');
279 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
281 int saved_output_format
= rev
->diffopt
.output_format
;
282 const char *commit_buffer
;
283 const char *author
, *author_end
, *committer
, *committer_end
;
284 const char *encoding
, *message
;
285 char *reencoded
= NULL
;
286 struct commit_list
*p
;
289 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
291 parse_commit_or_die(commit
);
292 commit_buffer
= get_commit_buffer(commit
, NULL
);
293 author
= strstr(commit_buffer
, "\nauthor ");
295 die ("Could not find author in commit %s",
296 sha1_to_hex(commit
->object
.sha1
));
298 author_end
= strchrnul(author
, '\n');
299 committer
= strstr(author_end
, "\ncommitter ");
301 die ("Could not find committer in commit %s",
302 sha1_to_hex(commit
->object
.sha1
));
304 committer_end
= strchrnul(committer
, '\n');
305 message
= strstr(committer_end
, "\n\n");
306 encoding
= find_encoding(committer_end
, message
);
310 if (commit
->parents
&&
311 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
313 parse_commit_or_die(commit
->parents
->item
);
314 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
315 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
318 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
321 /* Export the referenced blobs, and remember the marks. */
322 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
323 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
324 export_blob(diff_queued_diff
.queue
[i
]->two
->sha1
);
326 mark_next_object(&commit
->object
);
327 if (!is_encoding_utf8(encoding
))
328 reencoded
= reencode_string(message
, "UTF-8", encoding
);
329 if (!commit
->parents
)
330 printf("reset %s\n", (const char*)commit
->util
);
331 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
332 (const char *)commit
->util
, last_idnum
,
333 (int)(author_end
- author
), author
,
334 (int)(committer_end
- committer
), committer
,
336 ? strlen(reencoded
) : message
337 ? strlen(message
) : 0),
338 reencoded
? reencoded
: message
? message
: "");
340 unuse_commit_buffer(commit
, commit_buffer
);
342 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
343 int mark
= get_object_mark(&p
->item
->object
);
347 printf("from :%d\n", mark
);
349 printf("merge :%d\n", mark
);
354 printf("deleteall\n");
355 log_tree_diff_flush(rev
);
356 rev
->diffopt
.output_format
= saved_output_format
;
363 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
365 struct commit
*commit
;
366 while (commits
->nr
) {
367 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
368 if (has_unshown_parent(commit
))
370 handle_commit(commit
, revs
);
375 static void handle_tag(const char *name
, struct tag
*tag
)
378 enum object_type type
;
380 const char *tagger
, *tagger_end
, *message
;
381 size_t message_size
= 0;
382 struct object
*tagged
;
386 /* Trees have no identifier in fast-export output, thus we have no way
387 * to output tags of trees, tags of tags of trees, etc. Simply omit
390 tagged
= tag
->tagged
;
391 while (tagged
->type
== OBJ_TAG
) {
392 tagged
= ((struct tag
*)tagged
)->tagged
;
394 if (tagged
->type
== OBJ_TREE
) {
395 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
396 sha1_to_hex(tag
->object
.sha1
));
400 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
402 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
403 message
= memmem(buf
, size
, "\n\n", 2);
406 message_size
= strlen(message
);
408 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
410 if (fake_missing_tagger
)
411 tagger
= "tagger Unspecified Tagger "
412 "<unspecified-tagger> 0 +0000";
415 tagger_end
= tagger
+ strlen(tagger
);
418 tagger_end
= strchrnul(tagger
, '\n');
421 /* handle signed tags */
423 const char *signature
= strstr(message
,
424 "\n-----BEGIN PGP SIGNATURE-----\n");
426 switch(signed_tag_mode
) {
428 die ("Encountered signed tag %s; use "
429 "--signed-tags=<mode> to handle it.",
430 sha1_to_hex(tag
->object
.sha1
));
432 warning ("Exporting signed tag %s",
433 sha1_to_hex(tag
->object
.sha1
));
438 warning ("Stripping signature from tag %s",
439 sha1_to_hex(tag
->object
.sha1
));
442 message_size
= signature
+ 1 - message
;
447 /* handle tag->tagged having been filtered out due to paths specified */
448 tagged
= tag
->tagged
;
449 tagged_mark
= get_object_mark(tagged
);
451 switch(tag_of_filtered_mode
) {
453 die ("Tag %s tags unexported object; use "
454 "--tag-of-filtered-object=<mode> to handle it.",
455 sha1_to_hex(tag
->object
.sha1
));
457 /* Ignore this tag altogether */
460 if (tagged
->type
!= OBJ_COMMIT
) {
461 die ("Tag %s tags unexported %s!",
462 sha1_to_hex(tag
->object
.sha1
),
463 typename(tagged
->type
));
465 p
= (struct commit
*)tagged
;
467 if (p
->parents
&& p
->parents
->next
)
469 if (p
->object
.flags
& UNINTERESTING
)
471 if (!(p
->object
.flags
& TREESAME
))
474 die ("Can't find replacement commit for tag %s\n",
475 sha1_to_hex(tag
->object
.sha1
));
476 p
= p
->parents
->item
;
478 tagged_mark
= get_object_mark(&p
->object
);
482 if (starts_with(name
, "refs/tags/"))
484 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
486 (int)(tagger_end
- tagger
), tagger
,
487 tagger
== tagger_end
? "" : "\n",
488 (int)message_size
, (int)message_size
, message
? message
: "");
491 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
493 switch (e
->item
->type
) {
495 return (struct commit
*)e
->item
;
497 struct tag
*tag
= (struct tag
*)e
->item
;
499 /* handle nested tags */
500 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
501 parse_object(tag
->object
.sha1
);
502 string_list_append(&extra_refs
, full_name
)->util
= tag
;
503 tag
= (struct tag
*)tag
->tagged
;
506 die("Tag %s points nowhere?", e
->name
);
507 return (struct commit
*)tag
;
515 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
519 for (i
= 0; i
< info
->nr
; i
++) {
520 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
521 unsigned char sha1
[20];
522 struct commit
*commit
;
525 if (e
->flags
& UNINTERESTING
)
528 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
531 commit
= get_commit(e
, full_name
);
533 warning("%s: Unexpected object of type %s, skipping.",
535 typename(e
->item
->type
));
539 switch(commit
->object
.type
) {
543 export_blob(commit
->object
.sha1
);
545 default: /* OBJ_TAG (nested tags) is already handled */
546 warning("Tag points to object of unexpected type %s, skipping.",
547 typename(commit
->object
.type
));
552 * This ref will not be updated through a commit, lets make
553 * sure it gets properly updated eventually.
555 if (commit
->util
|| commit
->object
.flags
& SHOWN
)
556 string_list_append(&extra_refs
, full_name
)->util
= commit
;
558 commit
->util
= full_name
;
562 static void handle_tags_and_duplicates(void)
564 struct commit
*commit
;
567 for (i
= extra_refs
.nr
- 1; i
>= 0; i
--) {
568 const char *name
= extra_refs
.items
[i
].string
;
569 struct object
*object
= extra_refs
.items
[i
].util
;
570 switch (object
->type
) {
572 handle_tag(name
, (struct tag
*)object
);
575 /* create refs pointing to already seen commits */
576 commit
= (struct commit
*)object
;
577 printf("reset %s\nfrom :%d\n\n", name
,
578 get_object_mark(&commit
->object
));
585 static void export_marks(char *file
)
589 struct object_decoration
*deco
= idnums
.hash
;
593 f
= fopen(file
, "w");
595 die_errno("Unable to open marks file %s for writing.", file
);
597 for (i
= 0; i
< idnums
.size
; i
++) {
598 if (deco
->base
&& deco
->base
->type
== 1) {
599 mark
= ptr_to_mark(deco
->decoration
);
600 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
601 sha1_to_hex(deco
->base
->sha1
)) < 0) {
612 error("Unable to write marks file %s.", file
);
615 static void import_marks(char *input_file
)
618 FILE *f
= fopen(input_file
, "r");
620 die_errno("cannot read '%s'", input_file
);
622 while (fgets(line
, sizeof(line
), f
)) {
624 char *line_end
, *mark_end
;
625 unsigned char sha1
[20];
626 struct object
*object
;
627 struct commit
*commit
;
628 enum object_type type
;
630 line_end
= strchr(line
, '\n');
631 if (line
[0] != ':' || !line_end
)
632 die("corrupt mark line: %s", line
);
635 mark
= strtoumax(line
+ 1, &mark_end
, 10);
636 if (!mark
|| mark_end
== line
+ 1
637 || *mark_end
!= ' ' || get_sha1_hex(mark_end
+ 1, sha1
))
638 die("corrupt mark line: %s", line
);
640 if (last_idnum
< mark
)
643 type
= sha1_object_info(sha1
, NULL
);
645 die("object not found: %s", sha1_to_hex(sha1
));
647 if (type
!= OBJ_COMMIT
)
651 commit
= lookup_commit(sha1
);
653 die("not a commit? can't happen: %s", sha1_to_hex(sha1
));
655 object
= &commit
->object
;
657 if (object
->flags
& SHOWN
)
658 error("Object %s already has a mark", sha1_to_hex(sha1
));
660 mark_object(object
, mark
);
662 object
->flags
|= SHOWN
;
667 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
669 struct rev_info revs
;
670 struct object_array commits
= OBJECT_ARRAY_INIT
;
671 struct commit
*commit
;
672 char *export_filename
= NULL
, *import_filename
= NULL
;
673 uint32_t lastimportid
;
674 struct option options
[] = {
675 OPT_INTEGER(0, "progress", &progress
,
676 N_("show progress after <n> objects")),
677 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
678 N_("select handling of signed tags"),
679 parse_opt_signed_tag_mode
),
680 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
681 N_("select handling of tags that tag filtered objects"),
682 parse_opt_tag_of_filtered_mode
),
683 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
684 N_("Dump marks to this file")),
685 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
686 N_("Import marks from this file")),
687 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
688 N_("Fake a tagger when tags lack one")),
689 OPT_BOOL(0, "full-tree", &full_tree
,
690 N_("Output full tree for each commit")),
691 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
692 N_("Use the done feature to terminate the stream")),
693 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
698 usage_with_options (fast_export_usage
, options
);
700 /* we handle encodings */
701 git_config(git_default_config
, NULL
);
703 init_revisions(&revs
, prefix
);
705 revs
.show_source
= 1;
706 revs
.rewrite_parents
= 1;
707 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
708 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
, 0);
710 usage_with_options (fast_export_usage
, options
);
712 if (use_done_feature
)
713 printf("feature done\n");
716 import_marks(import_filename
);
717 lastimportid
= last_idnum
;
719 if (import_filename
&& revs
.prune_data
.nr
)
722 get_tags_and_duplicates(&revs
.cmdline
);
724 if (prepare_revision_walk(&revs
))
725 die("revision walk setup failed");
726 revs
.diffopt
.format_callback
= show_filemodify
;
727 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
728 while ((commit
= get_revision(&revs
))) {
729 if (has_unshown_parent(commit
)) {
730 add_object_array(&commit
->object
, NULL
, &commits
);
733 handle_commit(commit
, &revs
);
734 handle_tail(&commits
, &revs
);
738 handle_tags_and_duplicates();
740 if (export_filename
&& lastimportid
!= last_idnum
)
741 export_marks(export_filename
);
743 if (use_done_feature
)