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 N_("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);
190 else if (strchr(path
, ' '))
191 printf("\"%s\"", path
);
196 static void show_filemodify(struct diff_queue_struct
*q
,
197 struct diff_options
*options
, void *data
)
202 * Handle files below a directory first, in case they are all deleted
203 * and the directory changes to a file or symlink.
205 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), depth_first
);
207 for (i
= 0; i
< q
->nr
; i
++) {
208 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
209 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
211 switch (q
->queue
[i
]->status
) {
212 case DIFF_STATUS_DELETED
:
214 print_path(spec
->path
);
218 case DIFF_STATUS_COPIED
:
219 case DIFF_STATUS_RENAMED
:
220 printf("%c ", q
->queue
[i
]->status
);
221 print_path(ospec
->path
);
223 print_path(spec
->path
);
226 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
227 ospec
->mode
== spec
->mode
)
231 case DIFF_STATUS_TYPE_CHANGED
:
232 case DIFF_STATUS_MODIFIED
:
233 case DIFF_STATUS_ADDED
:
235 * Links refer to objects in another repositories;
236 * output the SHA-1 verbatim.
238 if (no_data
|| S_ISGITLINK(spec
->mode
))
239 printf("M %06o %s ", spec
->mode
,
240 sha1_to_hex(spec
->sha1
));
242 struct object
*object
= lookup_object(spec
->sha1
);
243 printf("M %06o :%d ", spec
->mode
,
244 get_object_mark(object
));
246 print_path(spec
->path
);
251 die("Unexpected comparison status '%c' for %s, %s",
253 ospec
->path
? ospec
->path
: "none",
254 spec
->path
? spec
->path
: "none");
259 static const char *find_encoding(const char *begin
, const char *end
)
261 const char *needle
= "\nencoding ";
264 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
265 needle
, strlen(needle
));
267 return git_commit_encoding
;
268 bol
+= strlen(needle
);
269 eol
= strchrnul(bol
, '\n');
274 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
276 int saved_output_format
= rev
->diffopt
.output_format
;
277 const char *author
, *author_end
, *committer
, *committer_end
;
278 const char *encoding
, *message
;
279 char *reencoded
= NULL
;
280 struct commit_list
*p
;
283 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
285 parse_commit(commit
);
286 author
= strstr(commit
->buffer
, "\nauthor ");
288 die ("Could not find author in commit %s",
289 sha1_to_hex(commit
->object
.sha1
));
291 author_end
= strchrnul(author
, '\n');
292 committer
= strstr(author_end
, "\ncommitter ");
294 die ("Could not find committer in commit %s",
295 sha1_to_hex(commit
->object
.sha1
));
297 committer_end
= strchrnul(committer
, '\n');
298 message
= strstr(committer_end
, "\n\n");
299 encoding
= find_encoding(committer_end
, message
);
303 if (commit
->parents
&&
304 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
306 parse_commit(commit
->parents
->item
);
307 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
308 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
311 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
314 /* Export the referenced blobs, and remember the marks. */
315 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
316 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
317 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
319 mark_next_object(&commit
->object
);
320 if (!is_encoding_utf8(encoding
))
321 reencoded
= reencode_string(message
, "UTF-8", encoding
);
322 if (!commit
->parents
)
323 printf("reset %s\n", (const char*)commit
->util
);
324 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
325 (const char *)commit
->util
, last_idnum
,
326 (int)(author_end
- author
), author
,
327 (int)(committer_end
- committer
), committer
,
329 ? strlen(reencoded
) : message
330 ? strlen(message
) : 0),
331 reencoded
? reencoded
: message
? message
: "");
334 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
335 int mark
= get_object_mark(&p
->item
->object
);
339 printf("from :%d\n", mark
);
341 printf("merge :%d\n", mark
);
346 printf("deleteall\n");
347 log_tree_diff_flush(rev
);
348 rev
->diffopt
.output_format
= saved_output_format
;
355 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
357 struct commit
*commit
;
358 while (commits
->nr
) {
359 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
360 if (has_unshown_parent(commit
))
362 handle_commit(commit
, revs
);
367 static void handle_tag(const char *name
, struct tag
*tag
)
370 enum object_type type
;
372 const char *tagger
, *tagger_end
, *message
;
373 size_t message_size
= 0;
374 struct object
*tagged
;
378 /* Trees have no identifer in fast-export output, thus we have no way
379 * to output tags of trees, tags of tags of trees, etc. Simply omit
382 tagged
= tag
->tagged
;
383 while (tagged
->type
== OBJ_TAG
) {
384 tagged
= ((struct tag
*)tagged
)->tagged
;
386 if (tagged
->type
== OBJ_TREE
) {
387 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
388 sha1_to_hex(tag
->object
.sha1
));
392 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
394 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
395 message
= memmem(buf
, size
, "\n\n", 2);
398 message_size
= strlen(message
);
400 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
402 if (fake_missing_tagger
)
403 tagger
= "tagger Unspecified Tagger "
404 "<unspecified-tagger> 0 +0000";
407 tagger_end
= tagger
+ strlen(tagger
);
410 tagger_end
= strchrnul(tagger
, '\n');
413 /* handle signed tags */
415 const char *signature
= strstr(message
,
416 "\n-----BEGIN PGP SIGNATURE-----\n");
418 switch(signed_tag_mode
) {
420 die ("Encountered signed tag %s; use "
421 "--signed-tag=<mode> to handle it.",
422 sha1_to_hex(tag
->object
.sha1
));
424 warning ("Exporting signed tag %s",
425 sha1_to_hex(tag
->object
.sha1
));
430 message_size
= signature
+ 1 - message
;
435 /* handle tag->tagged having been filtered out due to paths specified */
436 tagged
= tag
->tagged
;
437 tagged_mark
= get_object_mark(tagged
);
439 switch(tag_of_filtered_mode
) {
441 die ("Tag %s tags unexported object; use "
442 "--tag-of-filtered-object=<mode> to handle it.",
443 sha1_to_hex(tag
->object
.sha1
));
445 /* Ignore this tag altogether */
448 if (tagged
->type
!= OBJ_COMMIT
) {
449 die ("Tag %s tags unexported %s!",
450 sha1_to_hex(tag
->object
.sha1
),
451 typename(tagged
->type
));
453 p
= (struct commit
*)tagged
;
455 if (p
->parents
&& p
->parents
->next
)
457 if (p
->object
.flags
& UNINTERESTING
)
459 if (!(p
->object
.flags
& TREESAME
))
462 die ("Can't find replacement commit for tag %s\n",
463 sha1_to_hex(tag
->object
.sha1
));
464 p
= p
->parents
->item
;
466 tagged_mark
= get_object_mark(&p
->object
);
470 if (!prefixcmp(name
, "refs/tags/"))
472 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
474 (int)(tagger_end
- tagger
), tagger
,
475 tagger
== tagger_end
? "" : "\n",
476 (int)message_size
, (int)message_size
, message
? message
: "");
479 static void get_tags_and_duplicates(struct object_array
*pending
,
480 struct string_list
*refs
,
481 struct string_list
*extra_refs
)
486 for (i
= 0; i
< pending
->nr
; i
++) {
487 struct object_array_entry
*e
= pending
->objects
+ i
;
488 unsigned char sha1
[20];
489 struct commit
*commit
= commit
;
492 if ((e
->flags
& UNINTERESTING
) || dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
495 switch (e
->item
->type
) {
497 commit
= (struct commit
*)e
->item
;
500 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 switch(tag
->object
.type
) {
512 commit
= (struct commit
*)tag
;
515 handle_object(tag
->object
.sha1
);
517 default: /* OBJ_TAG (nested tags) is already handled */
518 warning("Tag points to object of unexpected type %s, skipping.",
519 typename(tag
->object
.type
));
524 warning("%s: Unexpected object of type %s, skipping.",
526 typename(e
->item
->type
));
530 /* more than one name for the same object */
531 string_list_append(extra_refs
, full_name
)->util
= commit
;
533 commit
->util
= full_name
;
534 /* we might need to set this ref explicitly */
535 string_list_append(refs
, full_name
)->util
= commit
;
540 static void handle_reset(const char *name
, struct object
*object
)
542 int mark
= get_object_mark(object
);
545 printf("reset %s\nfrom :%d\n\n", name
,
546 get_object_mark(object
));
548 printf("reset %s\nfrom %s\n\n", name
,
549 sha1_to_hex(object
->sha1
));
552 static void handle_tags_and_duplicates(struct string_list
*refs
, struct string_list
*extra_refs
)
556 /* even if no commits were exported, we need to export the ref */
557 for (i
= refs
->nr
- 1; i
>= 0; i
--) {
558 const char *name
= refs
->items
[i
].string
;
559 struct object
*object
= refs
->items
[i
].util
;
561 if (!(object
->flags
& REF_HANDLED
)) {
562 if (object
->type
& OBJ_TAG
)
563 handle_tag(name
, (struct tag
*)object
);
565 if (!prefixcmp(name
, "refs/tags/") &&
566 (tag_of_filtered_mode
!= REWRITE
||
567 !get_object_mark(object
)))
569 handle_reset(name
, object
);
570 object
->flags
|= REF_HANDLED
;
575 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
576 const char *name
= extra_refs
->items
[i
].string
;
577 struct object
*object
= extra_refs
->items
[i
].util
;
578 switch (object
->type
) {
580 handle_tag(name
, (struct tag
*)object
);
583 /* create refs pointing to already seen commits */
584 handle_reset(name
, object
);
591 static void export_marks(char *file
)
595 struct object_decoration
*deco
= idnums
.hash
;
599 f
= fopen(file
, "w");
601 die_errno("Unable to open marks file %s for writing.", file
);
603 for (i
= 0; i
< idnums
.size
; i
++) {
604 if (deco
->base
&& deco
->base
->type
== 1) {
605 mark
= ptr_to_mark(deco
->decoration
);
606 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
607 sha1_to_hex(deco
->base
->sha1
)) < 0) {
618 error("Unable to write marks file %s.", file
);
621 static void import_marks(char *input_file
)
624 FILE *f
= fopen(input_file
, "r");
626 die_errno("cannot read '%s'", input_file
);
628 while (fgets(line
, sizeof(line
), f
)) {
630 char *line_end
, *mark_end
;
631 unsigned char sha1
[20];
632 struct object
*object
;
634 line_end
= strchr(line
, '\n');
635 if (line
[0] != ':' || !line_end
)
636 die("corrupt mark line: %s", line
);
639 mark
= strtoumax(line
+ 1, &mark_end
, 10);
640 if (!mark
|| mark_end
== line
+ 1
641 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
642 die("corrupt mark line: %s", line
);
644 object
= parse_object(sha1
);
646 die ("Could not read blob %s", sha1_to_hex(sha1
));
648 if (object
->flags
& SHOWN
)
649 error("Object %s already has a mark", sha1_to_hex(sha1
));
651 mark_object(object
, mark
);
652 if (last_idnum
< mark
)
655 object
->flags
|= SHOWN
;
660 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
662 struct rev_info revs
;
663 struct object_array commits
= OBJECT_ARRAY_INIT
;
664 struct string_list refs
= STRING_LIST_INIT_NODUP
, extra_refs
= STRING_LIST_INIT_NODUP
;
665 struct commit
*commit
;
666 char *export_filename
= NULL
, *import_filename
= NULL
;
667 struct option options
[] = {
668 OPT_INTEGER(0, "progress", &progress
,
669 N_("show progress after <n> objects")),
670 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
671 N_("select handling of signed tags"),
672 parse_opt_signed_tag_mode
),
673 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
674 N_("select handling of tags that tag filtered objects"),
675 parse_opt_tag_of_filtered_mode
),
676 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
677 N_("Dump marks to this file")),
678 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
679 N_("Import marks from this file")),
680 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger
,
681 N_("Fake a tagger when tags lack one")),
682 OPT_BOOLEAN(0, "full-tree", &full_tree
,
683 N_("Output full tree for each commit")),
684 OPT_BOOLEAN(0, "use-done-feature", &use_done_feature
,
685 N_("Use the done feature to terminate the stream")),
686 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
691 usage_with_options (fast_export_usage
, options
);
693 /* we handle encodings */
694 git_config(git_default_config
, NULL
);
696 init_revisions(&revs
, prefix
);
698 revs
.show_source
= 1;
699 revs
.rewrite_parents
= 1;
700 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
701 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
, 0);
703 usage_with_options (fast_export_usage
, options
);
705 if (use_done_feature
)
706 printf("feature done\n");
709 import_marks(import_filename
);
711 if (import_filename
&& revs
.prune_data
.nr
)
714 get_tags_and_duplicates(&revs
.pending
, &refs
, &extra_refs
);
716 if (prepare_revision_walk(&revs
))
717 die("revision walk setup failed");
718 revs
.diffopt
.format_callback
= show_filemodify
;
719 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
720 while ((commit
= get_revision(&revs
))) {
721 if (has_unshown_parent(commit
)) {
722 add_object_array(&commit
->object
, NULL
, &commits
);
725 handle_commit(commit
, &revs
);
726 commit
->object
.flags
|= REF_HANDLED
;
727 handle_tail(&commits
, &revs
);
731 handle_tags_and_duplicates(&refs
, &extra_refs
);
734 export_marks(export_filename
);
736 if (use_done_feature
)