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
, 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
;
34 static int parse_opt_signed_tag_mode(const struct option
*opt
,
35 const char *arg
, int unset
)
37 if (unset
|| !strcmp(arg
, "abort"))
38 signed_tag_mode
= ABORT
;
39 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
40 signed_tag_mode
= VERBATIM
;
41 else if (!strcmp(arg
, "warn"))
42 signed_tag_mode
= WARN
;
43 else if (!strcmp(arg
, "strip"))
44 signed_tag_mode
= STRIP
;
46 return error("Unknown signed-tags mode: %s", arg
);
50 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
51 const char *arg
, int unset
)
53 if (unset
|| !strcmp(arg
, "abort"))
54 tag_of_filtered_mode
= ERROR
;
55 else if (!strcmp(arg
, "drop"))
56 tag_of_filtered_mode
= DROP
;
57 else if (!strcmp(arg
, "rewrite"))
58 tag_of_filtered_mode
= REWRITE
;
60 return error("Unknown tag-of-filtered mode: %s", arg
);
64 static struct decoration idnums
;
65 static uint32_t last_idnum
;
67 static int has_unshown_parent(struct commit
*commit
)
69 struct commit_list
*parent
;
71 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
72 if (!(parent
->item
->object
.flags
& SHOWN
) &&
73 !(parent
->item
->object
.flags
& UNINTERESTING
))
78 /* Since intptr_t is C99, we do not use it here */
79 static inline uint32_t *mark_to_ptr(uint32_t mark
)
81 return ((uint32_t *)NULL
) + mark
;
84 static inline uint32_t ptr_to_mark(void * mark
)
86 return (uint32_t *)mark
- (uint32_t *)NULL
;
89 static inline void mark_object(struct object
*object
, uint32_t mark
)
91 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
94 static inline void mark_next_object(struct object
*object
)
96 mark_object(object
, ++last_idnum
);
99 static int get_object_mark(struct object
*object
)
101 void *decoration
= lookup_decoration(&idnums
, object
);
104 return ptr_to_mark(decoration
);
107 static void show_progress(void)
109 static int counter
= 0;
112 if ((++counter
% progress
) == 0)
113 printf("progress %d objects\n", counter
);
116 static void export_blob(const unsigned char *sha1
)
119 enum object_type type
;
121 struct object
*object
;
127 if (is_null_sha1(sha1
))
130 object
= lookup_object(sha1
);
131 if (object
&& object
->flags
& SHOWN
)
134 buf
= read_sha1_file(sha1
, &type
, &size
);
136 die ("Could not read blob %s", sha1_to_hex(sha1
));
137 if (check_sha1_signature(sha1
, buf
, size
, typename(type
)) < 0)
138 die("sha1 mismatch in blob %s", sha1_to_hex(sha1
));
139 object
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
141 die("Could not read blob %s", sha1_to_hex(sha1
));
143 mark_next_object(object
);
145 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
146 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
147 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1
));
152 object
->flags
|= SHOWN
;
157 static int depth_first(const void *a_
, const void *b_
)
159 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
160 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
161 const char *name_a
, *name_b
;
162 int len_a
, len_b
, len
;
165 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
166 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
168 len_a
= strlen(name_a
);
169 len_b
= strlen(name_b
);
170 len
= (len_a
< len_b
) ? len_a
: len_b
;
172 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
173 cmp
= memcmp(name_a
, name_b
, len
);
180 * Move 'R'ename entries last so that all references of the file
181 * appear in the output before it is renamed (e.g., when a file
182 * was copied and renamed in the same commit).
184 return (a
->status
== 'R') - (b
->status
== 'R');
187 static void print_path(const char *path
)
189 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
191 quote_c_style(path
, NULL
, stdout
, 0);
192 else if (strchr(path
, ' '))
193 printf("\"%s\"", path
);
198 static void show_filemodify(struct diff_queue_struct
*q
,
199 struct diff_options
*options
, void *data
)
204 * Handle files below a directory first, in case they are all deleted
205 * and the directory changes to a file or symlink.
207 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), depth_first
);
209 for (i
= 0; i
< q
->nr
; i
++) {
210 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
211 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
213 switch (q
->queue
[i
]->status
) {
214 case DIFF_STATUS_DELETED
:
216 print_path(spec
->path
);
220 case DIFF_STATUS_COPIED
:
221 case DIFF_STATUS_RENAMED
:
222 printf("%c ", q
->queue
[i
]->status
);
223 print_path(ospec
->path
);
225 print_path(spec
->path
);
228 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
229 ospec
->mode
== spec
->mode
)
233 case DIFF_STATUS_TYPE_CHANGED
:
234 case DIFF_STATUS_MODIFIED
:
235 case DIFF_STATUS_ADDED
:
237 * Links refer to objects in another repositories;
238 * output the SHA-1 verbatim.
240 if (no_data
|| S_ISGITLINK(spec
->mode
))
241 printf("M %06o %s ", spec
->mode
,
242 sha1_to_hex(spec
->sha1
));
244 struct object
*object
= lookup_object(spec
->sha1
);
245 printf("M %06o :%d ", spec
->mode
,
246 get_object_mark(object
));
248 print_path(spec
->path
);
253 die("Unexpected comparison status '%c' for %s, %s",
255 ospec
->path
? ospec
->path
: "none",
256 spec
->path
? spec
->path
: "none");
261 static const char *find_encoding(const char *begin
, const char *end
)
263 const char *needle
= "\nencoding ";
266 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
267 needle
, strlen(needle
));
269 return git_commit_encoding
;
270 bol
+= strlen(needle
);
271 eol
= strchrnul(bol
, '\n');
276 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
278 int saved_output_format
= rev
->diffopt
.output_format
;
279 const char *author
, *author_end
, *committer
, *committer_end
;
280 const char *encoding
, *message
;
281 char *reencoded
= NULL
;
282 struct commit_list
*p
;
285 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
287 parse_commit(commit
);
288 author
= strstr(commit
->buffer
, "\nauthor ");
290 die ("Could not find author in commit %s",
291 sha1_to_hex(commit
->object
.sha1
));
293 author_end
= strchrnul(author
, '\n');
294 committer
= strstr(author_end
, "\ncommitter ");
296 die ("Could not find committer in commit %s",
297 sha1_to_hex(commit
->object
.sha1
));
299 committer_end
= strchrnul(committer
, '\n');
300 message
= strstr(committer_end
, "\n\n");
301 encoding
= find_encoding(committer_end
, message
);
305 if (commit
->parents
&&
306 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
308 parse_commit(commit
->parents
->item
);
309 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
310 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
313 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
316 /* Export the referenced blobs, and remember the marks. */
317 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
318 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
319 export_blob(diff_queued_diff
.queue
[i
]->two
->sha1
);
321 mark_next_object(&commit
->object
);
322 if (!is_encoding_utf8(encoding
))
323 reencoded
= reencode_string(message
, "UTF-8", encoding
);
324 if (!commit
->parents
)
325 printf("reset %s\n", (const char*)commit
->util
);
326 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
327 (const char *)commit
->util
, last_idnum
,
328 (int)(author_end
- author
), author
,
329 (int)(committer_end
- committer
), committer
,
331 ? strlen(reencoded
) : message
332 ? strlen(message
) : 0),
333 reencoded
? reencoded
: message
? message
: "");
336 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
337 int mark
= get_object_mark(&p
->item
->object
);
341 printf("from :%d\n", mark
);
343 printf("merge :%d\n", mark
);
348 printf("deleteall\n");
349 log_tree_diff_flush(rev
);
350 rev
->diffopt
.output_format
= saved_output_format
;
357 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
359 struct commit
*commit
;
360 while (commits
->nr
) {
361 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
362 if (has_unshown_parent(commit
))
364 handle_commit(commit
, revs
);
369 static void handle_tag(const char *name
, struct tag
*tag
)
372 enum object_type type
;
374 const char *tagger
, *tagger_end
, *message
;
375 size_t message_size
= 0;
376 struct object
*tagged
;
380 /* Trees have no identifer in fast-export output, thus we have no way
381 * to output tags of trees, tags of tags of trees, etc. Simply omit
384 tagged
= tag
->tagged
;
385 while (tagged
->type
== OBJ_TAG
) {
386 tagged
= ((struct tag
*)tagged
)->tagged
;
388 if (tagged
->type
== OBJ_TREE
) {
389 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
390 sha1_to_hex(tag
->object
.sha1
));
394 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
396 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
397 message
= memmem(buf
, size
, "\n\n", 2);
400 message_size
= strlen(message
);
402 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
404 if (fake_missing_tagger
)
405 tagger
= "tagger Unspecified Tagger "
406 "<unspecified-tagger> 0 +0000";
409 tagger_end
= tagger
+ strlen(tagger
);
412 tagger_end
= strchrnul(tagger
, '\n');
415 /* handle signed tags */
417 const char *signature
= strstr(message
,
418 "\n-----BEGIN PGP SIGNATURE-----\n");
420 switch(signed_tag_mode
) {
422 die ("Encountered signed tag %s; use "
423 "--signed-tags=<mode> to handle it.",
424 sha1_to_hex(tag
->object
.sha1
));
426 warning ("Exporting signed tag %s",
427 sha1_to_hex(tag
->object
.sha1
));
432 message_size
= signature
+ 1 - message
;
437 /* handle tag->tagged having been filtered out due to paths specified */
438 tagged
= tag
->tagged
;
439 tagged_mark
= get_object_mark(tagged
);
441 switch(tag_of_filtered_mode
) {
443 die ("Tag %s tags unexported object; use "
444 "--tag-of-filtered-object=<mode> to handle it.",
445 sha1_to_hex(tag
->object
.sha1
));
447 /* Ignore this tag altogether */
450 if (tagged
->type
!= OBJ_COMMIT
) {
451 die ("Tag %s tags unexported %s!",
452 sha1_to_hex(tag
->object
.sha1
),
453 typename(tagged
->type
));
455 p
= (struct commit
*)tagged
;
457 if (p
->parents
&& p
->parents
->next
)
459 if (p
->object
.flags
& UNINTERESTING
)
461 if (!(p
->object
.flags
& TREESAME
))
464 die ("Can't find replacement commit for tag %s\n",
465 sha1_to_hex(tag
->object
.sha1
));
466 p
= p
->parents
->item
;
468 tagged_mark
= get_object_mark(&p
->object
);
472 if (!prefixcmp(name
, "refs/tags/"))
474 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
476 (int)(tagger_end
- tagger
), tagger
,
477 tagger
== tagger_end
? "" : "\n",
478 (int)message_size
, (int)message_size
, message
? message
: "");
481 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
,
482 struct string_list
*extra_refs
)
487 for (i
= 0; i
< info
->nr
; i
++) {
488 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
489 unsigned char sha1
[20];
490 struct commit
*commit
;
493 if (e
->flags
& UNINTERESTING
)
496 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
499 switch (e
->item
->type
) {
501 commit
= (struct commit
*)e
->item
;
504 tag
= (struct tag
*)e
->item
;
506 /* handle nested tags */
507 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
508 parse_object(tag
->object
.sha1
);
509 string_list_append(extra_refs
, full_name
)->util
= tag
;
510 tag
= (struct tag
*)tag
->tagged
;
513 die ("Tag %s points nowhere?", e
->name
);
514 switch(tag
->object
.type
) {
516 commit
= (struct commit
*)tag
;
519 export_blob(tag
->object
.sha1
);
521 default: /* OBJ_TAG (nested tags) is already handled */
522 warning("Tag points to object of unexpected type %s, skipping.",
523 typename(tag
->object
.type
));
528 warning("%s: Unexpected object of type %s, skipping.",
530 typename(e
->item
->type
));
535 * This ref will not be updated through a commit, lets make
536 * sure it gets properly updated eventually.
538 if (commit
->util
|| commit
->object
.flags
& SHOWN
)
539 string_list_append(extra_refs
, full_name
)->util
= commit
;
541 commit
->util
= full_name
;
545 static void handle_tags_and_duplicates(struct string_list
*extra_refs
)
547 struct commit
*commit
;
550 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
551 const char *name
= extra_refs
->items
[i
].string
;
552 struct object
*object
= extra_refs
->items
[i
].util
;
553 switch (object
->type
) {
555 handle_tag(name
, (struct tag
*)object
);
558 /* create refs pointing to already seen commits */
559 commit
= (struct commit
*)object
;
560 printf("reset %s\nfrom :%d\n\n", name
,
561 get_object_mark(&commit
->object
));
568 static void export_marks(char *file
)
572 struct object_decoration
*deco
= idnums
.hash
;
576 f
= fopen(file
, "w");
578 die_errno("Unable to open marks file %s for writing.", file
);
580 for (i
= 0; i
< idnums
.size
; i
++) {
581 if (deco
->base
&& deco
->base
->type
== 1) {
582 mark
= ptr_to_mark(deco
->decoration
);
583 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
584 sha1_to_hex(deco
->base
->sha1
)) < 0) {
595 error("Unable to write marks file %s.", file
);
598 static void import_marks(char *input_file
)
601 FILE *f
= fopen(input_file
, "r");
603 die_errno("cannot read '%s'", input_file
);
605 while (fgets(line
, sizeof(line
), f
)) {
607 char *line_end
, *mark_end
;
608 unsigned char sha1
[20];
609 struct object
*object
;
611 line_end
= strchr(line
, '\n');
612 if (line
[0] != ':' || !line_end
)
613 die("corrupt mark line: %s", line
);
616 mark
= strtoumax(line
+ 1, &mark_end
, 10);
617 if (!mark
|| mark_end
== line
+ 1
618 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
619 die("corrupt mark line: %s", line
);
621 if (last_idnum
< mark
)
624 object
= parse_object(sha1
);
628 if (object
->flags
& SHOWN
)
629 error("Object %s already has a mark", sha1_to_hex(sha1
));
631 if (object
->type
!= OBJ_COMMIT
)
635 mark_object(object
, mark
);
637 object
->flags
|= SHOWN
;
642 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
644 struct rev_info revs
;
645 struct object_array commits
= OBJECT_ARRAY_INIT
;
646 struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
647 struct commit
*commit
;
648 char *export_filename
= NULL
, *import_filename
= NULL
;
649 uint32_t lastimportid
;
650 struct option options
[] = {
651 OPT_INTEGER(0, "progress", &progress
,
652 N_("show progress after <n> objects")),
653 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
654 N_("select handling of signed tags"),
655 parse_opt_signed_tag_mode
),
656 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
657 N_("select handling of tags that tag filtered objects"),
658 parse_opt_tag_of_filtered_mode
),
659 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
660 N_("Dump marks to this file")),
661 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
662 N_("Import marks from this file")),
663 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger
,
664 N_("Fake a tagger when tags lack one")),
665 OPT_BOOLEAN(0, "full-tree", &full_tree
,
666 N_("Output full tree for each commit")),
667 OPT_BOOLEAN(0, "use-done-feature", &use_done_feature
,
668 N_("Use the done feature to terminate the stream")),
669 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
674 usage_with_options (fast_export_usage
, options
);
676 /* we handle encodings */
677 git_config(git_default_config
, NULL
);
679 init_revisions(&revs
, prefix
);
681 revs
.show_source
= 1;
682 revs
.rewrite_parents
= 1;
683 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
684 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
, 0);
686 usage_with_options (fast_export_usage
, options
);
688 if (use_done_feature
)
689 printf("feature done\n");
692 import_marks(import_filename
);
693 lastimportid
= last_idnum
;
695 if (import_filename
&& revs
.prune_data
.nr
)
698 get_tags_and_duplicates(&revs
.cmdline
, &extra_refs
);
700 if (prepare_revision_walk(&revs
))
701 die("revision walk setup failed");
702 revs
.diffopt
.format_callback
= show_filemodify
;
703 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
704 while ((commit
= get_revision(&revs
))) {
705 if (has_unshown_parent(commit
)) {
706 add_object_array(&commit
->object
, NULL
, &commits
);
709 handle_commit(commit
, &revs
);
710 handle_tail(&commits
, &revs
);
714 handle_tags_and_duplicates(&extra_refs
);
716 if (export_filename
&& lastimportid
!= last_idnum
)
717 export_marks(export_filename
);
719 if (use_done_feature
)