2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
16 #include "string-list.h"
18 #include "parse-options.h"
22 static const char *fast_export_usage
[] = {
23 N_("git fast-export [rev-list-opts]"),
28 static enum { ABORT
, VERBATIM
, WARN
, WARN_STRIP
, STRIP
} signed_tag_mode
= ABORT
;
29 static enum { ERROR
, DROP
, REWRITE
} tag_of_filtered_mode
= ERROR
;
30 static int fake_missing_tagger
;
31 static int use_done_feature
;
34 static struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
35 static struct refspec
*refspecs
;
36 static int refspecs_nr
;
38 static int parse_opt_signed_tag_mode(const struct option
*opt
,
39 const char *arg
, int unset
)
41 if (unset
|| !strcmp(arg
, "abort"))
42 signed_tag_mode
= ABORT
;
43 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
44 signed_tag_mode
= VERBATIM
;
45 else if (!strcmp(arg
, "warn"))
46 signed_tag_mode
= WARN
;
47 else if (!strcmp(arg
, "warn-strip"))
48 signed_tag_mode
= WARN_STRIP
;
49 else if (!strcmp(arg
, "strip"))
50 signed_tag_mode
= STRIP
;
52 return error("Unknown signed-tags mode: %s", arg
);
56 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
57 const char *arg
, int unset
)
59 if (unset
|| !strcmp(arg
, "abort"))
60 tag_of_filtered_mode
= ERROR
;
61 else if (!strcmp(arg
, "drop"))
62 tag_of_filtered_mode
= DROP
;
63 else if (!strcmp(arg
, "rewrite"))
64 tag_of_filtered_mode
= REWRITE
;
66 return error("Unknown tag-of-filtered mode: %s", arg
);
70 static struct decoration idnums
;
71 static uint32_t last_idnum
;
73 static int has_unshown_parent(struct commit
*commit
)
75 struct commit_list
*parent
;
77 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
78 if (!(parent
->item
->object
.flags
& SHOWN
) &&
79 !(parent
->item
->object
.flags
& UNINTERESTING
))
84 /* Since intptr_t is C99, we do not use it here */
85 static inline uint32_t *mark_to_ptr(uint32_t mark
)
87 return ((uint32_t *)NULL
) + mark
;
90 static inline uint32_t ptr_to_mark(void * mark
)
92 return (uint32_t *)mark
- (uint32_t *)NULL
;
95 static inline void mark_object(struct object
*object
, uint32_t mark
)
97 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
100 static inline void mark_next_object(struct object
*object
)
102 mark_object(object
, ++last_idnum
);
105 static int get_object_mark(struct object
*object
)
107 void *decoration
= lookup_decoration(&idnums
, object
);
110 return ptr_to_mark(decoration
);
113 static void show_progress(void)
115 static int counter
= 0;
118 if ((++counter
% progress
) == 0)
119 printf("progress %d objects\n", counter
);
122 static void export_blob(const unsigned char *sha1
)
125 enum object_type type
;
127 struct object
*object
;
133 if (is_null_sha1(sha1
))
136 object
= lookup_object(sha1
);
137 if (object
&& object
->flags
& SHOWN
)
140 buf
= read_sha1_file(sha1
, &type
, &size
);
142 die ("Could not read blob %s", sha1_to_hex(sha1
));
143 if (check_sha1_signature(sha1
, buf
, size
, typename(type
)) < 0)
144 die("sha1 mismatch in blob %s", sha1_to_hex(sha1
));
145 object
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
147 die("Could not read blob %s", sha1_to_hex(sha1
));
149 mark_next_object(object
);
151 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
152 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
153 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1
));
158 object
->flags
|= SHOWN
;
163 static int depth_first(const void *a_
, const void *b_
)
165 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
166 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
167 const char *name_a
, *name_b
;
168 int len_a
, len_b
, len
;
171 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
172 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
174 len_a
= strlen(name_a
);
175 len_b
= strlen(name_b
);
176 len
= (len_a
< len_b
) ? len_a
: len_b
;
178 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
179 cmp
= memcmp(name_a
, name_b
, len
);
186 * Move 'R'ename entries last so that all references of the file
187 * appear in the output before it is renamed (e.g., when a file
188 * was copied and renamed in the same commit).
190 return (a
->status
== 'R') - (b
->status
== 'R');
193 static void print_path(const char *path
)
195 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
197 quote_c_style(path
, NULL
, stdout
, 0);
198 else if (strchr(path
, ' '))
199 printf("\"%s\"", path
);
204 static void show_filemodify(struct diff_queue_struct
*q
,
205 struct diff_options
*options
, void *data
)
210 * Handle files below a directory first, in case they are all deleted
211 * and the directory changes to a file or symlink.
213 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), depth_first
);
215 for (i
= 0; i
< q
->nr
; i
++) {
216 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
217 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
219 switch (q
->queue
[i
]->status
) {
220 case DIFF_STATUS_DELETED
:
222 print_path(spec
->path
);
226 case DIFF_STATUS_COPIED
:
227 case DIFF_STATUS_RENAMED
:
228 printf("%c ", q
->queue
[i
]->status
);
229 print_path(ospec
->path
);
231 print_path(spec
->path
);
234 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
235 ospec
->mode
== spec
->mode
)
239 case DIFF_STATUS_TYPE_CHANGED
:
240 case DIFF_STATUS_MODIFIED
:
241 case DIFF_STATUS_ADDED
:
243 * Links refer to objects in another repositories;
244 * output the SHA-1 verbatim.
246 if (no_data
|| S_ISGITLINK(spec
->mode
))
247 printf("M %06o %s ", spec
->mode
,
248 sha1_to_hex(spec
->sha1
));
250 struct object
*object
= lookup_object(spec
->sha1
);
251 printf("M %06o :%d ", spec
->mode
,
252 get_object_mark(object
));
254 print_path(spec
->path
);
259 die("Unexpected comparison status '%c' for %s, %s",
261 ospec
->path
? ospec
->path
: "none",
262 spec
->path
? spec
->path
: "none");
267 static const char *find_encoding(const char *begin
, const char *end
)
269 const char *needle
= "\nencoding ";
272 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
273 needle
, strlen(needle
));
275 return git_commit_encoding
;
276 bol
+= strlen(needle
);
277 eol
= strchrnul(bol
, '\n');
282 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
284 int saved_output_format
= rev
->diffopt
.output_format
;
285 const char *commit_buffer
;
286 const char *author
, *author_end
, *committer
, *committer_end
;
287 const char *encoding
, *message
;
288 char *reencoded
= NULL
;
289 struct commit_list
*p
;
292 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
294 parse_commit_or_die(commit
);
295 commit_buffer
= get_commit_buffer(commit
, NULL
);
296 author
= strstr(commit_buffer
, "\nauthor ");
298 die ("Could not find author in commit %s",
299 sha1_to_hex(commit
->object
.sha1
));
301 author_end
= strchrnul(author
, '\n');
302 committer
= strstr(author_end
, "\ncommitter ");
304 die ("Could not find committer in commit %s",
305 sha1_to_hex(commit
->object
.sha1
));
307 committer_end
= strchrnul(committer
, '\n');
308 message
= strstr(committer_end
, "\n\n");
309 encoding
= find_encoding(committer_end
, message
);
313 if (commit
->parents
&&
314 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
316 parse_commit_or_die(commit
->parents
->item
);
317 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
318 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
321 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
324 /* Export the referenced blobs, and remember the marks. */
325 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
326 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
327 export_blob(diff_queued_diff
.queue
[i
]->two
->sha1
);
329 mark_next_object(&commit
->object
);
330 if (!is_encoding_utf8(encoding
))
331 reencoded
= reencode_string(message
, "UTF-8", encoding
);
332 if (!commit
->parents
)
333 printf("reset %s\n", (const char*)commit
->util
);
334 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
335 (const char *)commit
->util
, last_idnum
,
336 (int)(author_end
- author
), author
,
337 (int)(committer_end
- committer
), committer
,
339 ? strlen(reencoded
) : message
340 ? strlen(message
) : 0),
341 reencoded
? reencoded
: message
? message
: "");
343 unuse_commit_buffer(commit
, commit_buffer
);
345 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
346 int mark
= get_object_mark(&p
->item
->object
);
350 printf("from :%d\n", mark
);
352 printf("merge :%d\n", mark
);
357 printf("deleteall\n");
358 log_tree_diff_flush(rev
);
359 rev
->diffopt
.output_format
= saved_output_format
;
366 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
368 struct commit
*commit
;
369 while (commits
->nr
) {
370 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
371 if (has_unshown_parent(commit
))
373 handle_commit(commit
, revs
);
378 static void handle_tag(const char *name
, struct tag
*tag
)
381 enum object_type type
;
383 const char *tagger
, *tagger_end
, *message
;
384 size_t message_size
= 0;
385 struct object
*tagged
;
389 /* Trees have no identifier in fast-export output, thus we have no way
390 * to output tags of trees, tags of tags of trees, etc. Simply omit
393 tagged
= tag
->tagged
;
394 while (tagged
->type
== OBJ_TAG
) {
395 tagged
= ((struct tag
*)tagged
)->tagged
;
397 if (tagged
->type
== OBJ_TREE
) {
398 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
399 sha1_to_hex(tag
->object
.sha1
));
403 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
405 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
406 message
= memmem(buf
, size
, "\n\n", 2);
409 message_size
= strlen(message
);
411 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
413 if (fake_missing_tagger
)
414 tagger
= "tagger Unspecified Tagger "
415 "<unspecified-tagger> 0 +0000";
418 tagger_end
= tagger
+ strlen(tagger
);
421 tagger_end
= strchrnul(tagger
, '\n');
424 /* handle signed tags */
426 const char *signature
= strstr(message
,
427 "\n-----BEGIN PGP SIGNATURE-----\n");
429 switch(signed_tag_mode
) {
431 die ("Encountered signed tag %s; use "
432 "--signed-tags=<mode> to handle it.",
433 sha1_to_hex(tag
->object
.sha1
));
435 warning ("Exporting signed tag %s",
436 sha1_to_hex(tag
->object
.sha1
));
441 warning ("Stripping signature from tag %s",
442 sha1_to_hex(tag
->object
.sha1
));
445 message_size
= signature
+ 1 - message
;
450 /* handle tag->tagged having been filtered out due to paths specified */
451 tagged
= tag
->tagged
;
452 tagged_mark
= get_object_mark(tagged
);
454 switch(tag_of_filtered_mode
) {
456 die ("Tag %s tags unexported object; use "
457 "--tag-of-filtered-object=<mode> to handle it.",
458 sha1_to_hex(tag
->object
.sha1
));
460 /* Ignore this tag altogether */
463 if (tagged
->type
!= OBJ_COMMIT
) {
464 die ("Tag %s tags unexported %s!",
465 sha1_to_hex(tag
->object
.sha1
),
466 typename(tagged
->type
));
468 p
= (struct commit
*)tagged
;
470 if (p
->parents
&& p
->parents
->next
)
472 if (p
->object
.flags
& UNINTERESTING
)
474 if (!(p
->object
.flags
& TREESAME
))
477 die ("Can't find replacement commit for tag %s\n",
478 sha1_to_hex(tag
->object
.sha1
));
479 p
= p
->parents
->item
;
481 tagged_mark
= get_object_mark(&p
->object
);
485 if (starts_with(name
, "refs/tags/"))
487 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
489 (int)(tagger_end
- tagger
), tagger
,
490 tagger
== tagger_end
? "" : "\n",
491 (int)message_size
, (int)message_size
, message
? message
: "");
494 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
496 switch (e
->item
->type
) {
498 return (struct commit
*)e
->item
;
500 struct tag
*tag
= (struct tag
*)e
->item
;
502 /* handle nested tags */
503 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
504 parse_object(tag
->object
.sha1
);
505 string_list_append(&extra_refs
, full_name
)->util
= tag
;
506 tag
= (struct tag
*)tag
->tagged
;
509 die("Tag %s points nowhere?", e
->name
);
510 return (struct commit
*)tag
;
518 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
522 for (i
= 0; i
< info
->nr
; i
++) {
523 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
524 unsigned char sha1
[20];
525 struct commit
*commit
;
528 if (e
->flags
& UNINTERESTING
)
531 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
536 private = apply_refspecs(refspecs
, refspecs_nr
, full_name
);
543 commit
= get_commit(e
, full_name
);
545 warning("%s: Unexpected object of type %s, skipping.",
547 typename(e
->item
->type
));
551 switch(commit
->object
.type
) {
555 export_blob(commit
->object
.sha1
);
557 default: /* OBJ_TAG (nested tags) is already handled */
558 warning("Tag points to object of unexpected type %s, skipping.",
559 typename(commit
->object
.type
));
564 * This ref will not be updated through a commit, lets make
565 * sure it gets properly updated eventually.
567 if (commit
->util
|| commit
->object
.flags
& SHOWN
)
568 string_list_append(&extra_refs
, full_name
)->util
= commit
;
570 commit
->util
= full_name
;
574 static void handle_reset(const char *name
, struct object
*object
)
576 int mark
= get_object_mark(object
);
579 printf("reset %s\nfrom :%d\n\n", name
,
580 get_object_mark(object
));
582 printf("reset %s\nfrom %s\n\n", name
,
583 sha1_to_hex(object
->sha1
));
586 static void handle_tags_and_duplicates(void)
590 for (i
= extra_refs
.nr
- 1; i
>= 0; i
--) {
591 const char *name
= extra_refs
.items
[i
].string
;
592 struct object
*object
= extra_refs
.items
[i
].util
;
593 switch (object
->type
) {
595 handle_tag(name
, (struct tag
*)object
);
598 /* create refs pointing to already seen commits */
599 handle_reset(name
, object
);
606 static void export_marks(char *file
)
610 struct object_decoration
*deco
= idnums
.hash
;
614 f
= fopen(file
, "w");
616 die_errno("Unable to open marks file %s for writing.", file
);
618 for (i
= 0; i
< idnums
.size
; i
++) {
619 if (deco
->base
&& deco
->base
->type
== 1) {
620 mark
= ptr_to_mark(deco
->decoration
);
621 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
622 sha1_to_hex(deco
->base
->sha1
)) < 0) {
633 error("Unable to write marks file %s.", file
);
636 static void import_marks(char *input_file
)
639 FILE *f
= fopen(input_file
, "r");
641 die_errno("cannot read '%s'", input_file
);
643 while (fgets(line
, sizeof(line
), f
)) {
645 char *line_end
, *mark_end
;
646 unsigned char sha1
[20];
647 struct object
*object
;
648 struct commit
*commit
;
649 enum object_type type
;
651 line_end
= strchr(line
, '\n');
652 if (line
[0] != ':' || !line_end
)
653 die("corrupt mark line: %s", line
);
656 mark
= strtoumax(line
+ 1, &mark_end
, 10);
657 if (!mark
|| mark_end
== line
+ 1
658 || *mark_end
!= ' ' || get_sha1_hex(mark_end
+ 1, sha1
))
659 die("corrupt mark line: %s", line
);
661 if (last_idnum
< mark
)
664 type
= sha1_object_info(sha1
, NULL
);
666 die("object not found: %s", sha1_to_hex(sha1
));
668 if (type
!= OBJ_COMMIT
)
672 commit
= lookup_commit(sha1
);
674 die("not a commit? can't happen: %s", sha1_to_hex(sha1
));
676 object
= &commit
->object
;
678 if (object
->flags
& SHOWN
)
679 error("Object %s already has a mark", sha1_to_hex(sha1
));
681 mark_object(object
, mark
);
683 object
->flags
|= SHOWN
;
688 static void handle_deletes(void)
691 for (i
= 0; i
< refspecs_nr
; i
++) {
692 struct refspec
*refspec
= &refspecs
[i
];
696 printf("reset %s\nfrom %s\n\n",
697 refspec
->dst
, sha1_to_hex(null_sha1
));
701 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
703 struct rev_info revs
;
704 struct object_array commits
= OBJECT_ARRAY_INIT
;
705 struct commit
*commit
;
706 char *export_filename
= NULL
, *import_filename
= NULL
;
707 uint32_t lastimportid
;
708 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
709 struct option options
[] = {
710 OPT_INTEGER(0, "progress", &progress
,
711 N_("show progress after <n> objects")),
712 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
713 N_("select handling of signed tags"),
714 parse_opt_signed_tag_mode
),
715 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
716 N_("select handling of tags that tag filtered objects"),
717 parse_opt_tag_of_filtered_mode
),
718 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
719 N_("Dump marks to this file")),
720 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
721 N_("Import marks from this file")),
722 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
723 N_("Fake a tagger when tags lack one")),
724 OPT_BOOL(0, "full-tree", &full_tree
,
725 N_("Output full tree for each commit")),
726 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
727 N_("Use the done feature to terminate the stream")),
728 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
729 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
730 N_("Apply refspec to exported refs")),
735 usage_with_options (fast_export_usage
, options
);
737 /* we handle encodings */
738 git_config(git_default_config
, NULL
);
740 init_revisions(&revs
, prefix
);
742 revs
.show_source
= 1;
743 revs
.rewrite_parents
= 1;
744 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
745 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
);
746 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
748 usage_with_options (fast_export_usage
, options
);
750 if (refspecs_list
.nr
) {
751 const char **refspecs_str
;
754 refspecs_str
= xmalloc(sizeof(*refspecs_str
) * refspecs_list
.nr
);
755 for (i
= 0; i
< refspecs_list
.nr
; i
++)
756 refspecs_str
[i
] = refspecs_list
.items
[i
].string
;
758 refspecs_nr
= refspecs_list
.nr
;
759 refspecs
= parse_fetch_refspec(refspecs_nr
, refspecs_str
);
761 string_list_clear(&refspecs_list
, 1);
765 if (use_done_feature
)
766 printf("feature done\n");
769 import_marks(import_filename
);
770 lastimportid
= last_idnum
;
772 if (import_filename
&& revs
.prune_data
.nr
)
775 get_tags_and_duplicates(&revs
.cmdline
);
777 if (prepare_revision_walk(&revs
))
778 die("revision walk setup failed");
779 revs
.diffopt
.format_callback
= show_filemodify
;
780 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
781 while ((commit
= get_revision(&revs
))) {
782 if (has_unshown_parent(commit
)) {
783 add_object_array(&commit
->object
, NULL
, &commits
);
786 handle_commit(commit
, &revs
);
787 handle_tail(&commits
, &revs
);
791 handle_tags_and_duplicates();
794 if (export_filename
&& lastimportid
!= last_idnum
)
795 export_marks(export_filename
);
797 if (use_done_feature
)
800 free_refspec(refspecs_nr
, refspecs
);