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 "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
= ABORT
;
29 static int fake_missing_tagger
;
33 static int parse_opt_signed_tag_mode(const struct option
*opt
,
34 const char *arg
, int unset
)
36 if (unset
|| !strcmp(arg
, "abort"))
37 signed_tag_mode
= ABORT
;
38 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
39 signed_tag_mode
= VERBATIM
;
40 else if (!strcmp(arg
, "warn"))
41 signed_tag_mode
= WARN
;
42 else if (!strcmp(arg
, "strip"))
43 signed_tag_mode
= STRIP
;
45 return error("Unknown signed-tag mode: %s", arg
);
49 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
50 const char *arg
, int unset
)
52 if (unset
|| !strcmp(arg
, "abort"))
53 tag_of_filtered_mode
= ABORT
;
54 else if (!strcmp(arg
, "drop"))
55 tag_of_filtered_mode
= DROP
;
56 else if (!strcmp(arg
, "rewrite"))
57 tag_of_filtered_mode
= REWRITE
;
59 return error("Unknown tag-of-filtered mode: %s", arg
);
63 static struct decoration idnums
;
64 static uint32_t last_idnum
;
66 static int has_unshown_parent(struct commit
*commit
)
68 struct commit_list
*parent
;
70 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
71 if (!(parent
->item
->object
.flags
& SHOWN
) &&
72 !(parent
->item
->object
.flags
& UNINTERESTING
))
77 /* Since intptr_t is C99, we do not use it here */
78 static inline uint32_t *mark_to_ptr(uint32_t mark
)
80 return ((uint32_t *)NULL
) + mark
;
83 static inline uint32_t ptr_to_mark(void * mark
)
85 return (uint32_t *)mark
- (uint32_t *)NULL
;
88 static inline void mark_object(struct object
*object
, uint32_t mark
)
90 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
93 static inline void mark_next_object(struct object
*object
)
95 mark_object(object
, ++last_idnum
);
98 static int get_object_mark(struct object
*object
)
100 void *decoration
= lookup_decoration(&idnums
, object
);
103 return ptr_to_mark(decoration
);
106 static void show_progress(void)
108 static int counter
= 0;
111 if ((++counter
% progress
) == 0)
112 printf("progress %d objects\n", counter
);
115 static void handle_object(const unsigned char *sha1
)
118 enum object_type type
;
120 struct object
*object
;
125 if (is_null_sha1(sha1
))
128 object
= parse_object(sha1
);
130 die ("Could not read blob %s", sha1_to_hex(sha1
));
132 if (object
->flags
& SHOWN
)
135 buf
= read_sha1_file(sha1
, &type
, &size
);
137 die ("Could not read blob %s", sha1_to_hex(sha1
));
139 mark_next_object(object
);
141 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
142 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
143 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1
));
148 object
->flags
|= SHOWN
;
152 static int depth_first(const void *a_
, const void *b_
)
154 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
155 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
156 const char *name_a
, *name_b
;
157 int len_a
, len_b
, len
;
160 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
161 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
163 len_a
= strlen(name_a
);
164 len_b
= strlen(name_b
);
165 len
= (len_a
< len_b
) ? len_a
: len_b
;
167 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
168 cmp
= memcmp(name_a
, name_b
, len
);
175 * Move 'R'ename entries last so that all references of the file
176 * appear in the output before it is renamed (e.g., when a file
177 * was copied and renamed in the same commit).
179 return (a
->status
== 'R') - (b
->status
== 'R');
182 static void print_path(const char *path
)
184 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
186 quote_c_style(path
, NULL
, stdout
, 0);
191 static void show_filemodify(struct diff_queue_struct
*q
,
192 struct diff_options
*options
, void *data
)
197 * Handle files below a directory first, in case they are all deleted
198 * and the directory changes to a file or symlink.
200 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), depth_first
);
202 for (i
= 0; i
< q
->nr
; i
++) {
203 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
204 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
206 switch (q
->queue
[i
]->status
) {
207 case DIFF_STATUS_DELETED
:
209 print_path(spec
->path
);
213 case DIFF_STATUS_COPIED
:
214 case DIFF_STATUS_RENAMED
:
215 printf("%c ", q
->queue
[i
]->status
);
216 print_path(ospec
->path
);
218 print_path(spec
->path
);
221 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
222 ospec
->mode
== spec
->mode
)
226 case DIFF_STATUS_TYPE_CHANGED
:
227 case DIFF_STATUS_MODIFIED
:
228 case DIFF_STATUS_ADDED
:
230 * Links refer to objects in another repositories;
231 * output the SHA-1 verbatim.
233 if (no_data
|| S_ISGITLINK(spec
->mode
))
234 printf("M %06o %s ", spec
->mode
,
235 sha1_to_hex(spec
->sha1
));
237 struct object
*object
= lookup_object(spec
->sha1
);
238 printf("M %06o :%d ", spec
->mode
,
239 get_object_mark(object
));
241 print_path(spec
->path
);
246 die("Unexpected comparison status '%c' for %s, %s",
248 ospec
->path
? ospec
->path
: "none",
249 spec
->path
? spec
->path
: "none");
254 static const char *find_encoding(const char *begin
, const char *end
)
256 const char *needle
= "\nencoding ";
259 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
260 needle
, strlen(needle
));
262 return git_commit_encoding
;
263 bol
+= strlen(needle
);
264 eol
= strchrnul(bol
, '\n');
269 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
271 int saved_output_format
= rev
->diffopt
.output_format
;
272 const char *author
, *author_end
, *committer
, *committer_end
;
273 const char *encoding
, *message
;
274 char *reencoded
= NULL
;
275 struct commit_list
*p
;
278 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
280 parse_commit(commit
);
281 author
= strstr(commit
->buffer
, "\nauthor ");
283 die ("Could not find author in commit %s",
284 sha1_to_hex(commit
->object
.sha1
));
286 author_end
= strchrnul(author
, '\n');
287 committer
= strstr(author_end
, "\ncommitter ");
289 die ("Could not find committer in commit %s",
290 sha1_to_hex(commit
->object
.sha1
));
292 committer_end
= strchrnul(committer
, '\n');
293 message
= strstr(committer_end
, "\n\n");
294 encoding
= find_encoding(committer_end
, message
);
298 if (commit
->parents
&&
299 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
301 parse_commit(commit
->parents
->item
);
302 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
303 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
306 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
309 /* Export the referenced blobs, and remember the marks. */
310 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
311 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
312 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
314 mark_next_object(&commit
->object
);
315 if (!is_encoding_utf8(encoding
))
316 reencoded
= reencode_string(message
, "UTF-8", encoding
);
317 if (!commit
->parents
)
318 printf("reset %s\n", (const char*)commit
->util
);
319 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
320 (const char *)commit
->util
, last_idnum
,
321 (int)(author_end
- author
), author
,
322 (int)(committer_end
- committer
), committer
,
324 ? strlen(reencoded
) : message
325 ? strlen(message
) : 0),
326 reencoded
? reencoded
: message
? message
: "");
329 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
330 int mark
= get_object_mark(&p
->item
->object
);
334 printf("from :%d\n", mark
);
336 printf("merge :%d\n", mark
);
341 printf("deleteall\n");
342 log_tree_diff_flush(rev
);
343 rev
->diffopt
.output_format
= saved_output_format
;
350 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
352 struct commit
*commit
;
353 while (commits
->nr
) {
354 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
355 if (has_unshown_parent(commit
))
357 handle_commit(commit
, revs
);
362 static void handle_tag(const char *name
, struct tag
*tag
)
365 enum object_type type
;
367 const char *tagger
, *tagger_end
, *message
;
368 size_t message_size
= 0;
369 struct object
*tagged
;
373 /* Trees have no identifer in fast-export output, thus we have no way
374 * to output tags of trees, tags of tags of trees, etc. Simply omit
377 tagged
= tag
->tagged
;
378 while (tagged
->type
== OBJ_TAG
) {
379 tagged
= ((struct tag
*)tagged
)->tagged
;
381 if (tagged
->type
== OBJ_TREE
) {
382 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
383 sha1_to_hex(tag
->object
.sha1
));
387 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
389 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
390 message
= memmem(buf
, size
, "\n\n", 2);
393 message_size
= strlen(message
);
395 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
397 if (fake_missing_tagger
)
398 tagger
= "tagger Unspecified Tagger "
399 "<unspecified-tagger> 0 +0000";
402 tagger_end
= tagger
+ strlen(tagger
);
405 tagger_end
= strchrnul(tagger
, '\n');
408 /* handle signed tags */
410 const char *signature
= strstr(message
,
411 "\n-----BEGIN PGP SIGNATURE-----\n");
413 switch(signed_tag_mode
) {
415 die ("Encountered signed tag %s; use "
416 "--signed-tag=<mode> to handle it.",
417 sha1_to_hex(tag
->object
.sha1
));
419 warning ("Exporting signed tag %s",
420 sha1_to_hex(tag
->object
.sha1
));
425 message_size
= signature
+ 1 - message
;
430 /* handle tag->tagged having been filtered out due to paths specified */
431 tagged
= tag
->tagged
;
432 tagged_mark
= get_object_mark(tagged
);
434 switch(tag_of_filtered_mode
) {
436 die ("Tag %s tags unexported object; use "
437 "--tag-of-filtered-object=<mode> to handle it.",
438 sha1_to_hex(tag
->object
.sha1
));
440 /* Ignore this tag altogether */
443 if (tagged
->type
!= OBJ_COMMIT
) {
444 die ("Tag %s tags unexported %s!",
445 sha1_to_hex(tag
->object
.sha1
),
446 typename(tagged
->type
));
448 p
= (struct commit
*)tagged
;
450 if (p
->parents
&& p
->parents
->next
)
452 if (p
->object
.flags
& UNINTERESTING
)
454 if (!(p
->object
.flags
& TREESAME
))
457 die ("Can't find replacement commit for tag %s\n",
458 sha1_to_hex(tag
->object
.sha1
));
459 p
= p
->parents
->item
;
461 tagged_mark
= get_object_mark(&p
->object
);
465 if (!prefixcmp(name
, "refs/tags/"))
467 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
469 (int)(tagger_end
- tagger
), tagger
,
470 tagger
== tagger_end
? "" : "\n",
471 (int)message_size
, (int)message_size
, message
? message
: "");
474 static void get_tags_and_duplicates(struct object_array
*pending
,
475 struct string_list
*extra_refs
)
480 for (i
= 0; i
< pending
->nr
; i
++) {
481 struct object_array_entry
*e
= pending
->objects
+ i
;
482 unsigned char sha1
[20];
483 struct commit
*commit
= commit
;
486 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
489 switch (e
->item
->type
) {
491 commit
= (struct commit
*)e
->item
;
494 tag
= (struct tag
*)e
->item
;
496 /* handle nested tags */
497 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
498 parse_object(tag
->object
.sha1
);
499 string_list_append(extra_refs
, full_name
)->util
= tag
;
500 tag
= (struct tag
*)tag
->tagged
;
503 die ("Tag %s points nowhere?", e
->name
);
504 switch(tag
->object
.type
) {
506 commit
= (struct commit
*)tag
;
509 handle_object(tag
->object
.sha1
);
511 default: /* OBJ_TAG (nested tags) is already handled */
512 warning("Tag points to object of unexpected type %s, skipping.",
513 typename(tag
->object
.type
));
518 warning("%s: Unexpected object of type %s, skipping.",
520 typename(e
->item
->type
));
524 /* more than one name for the same object */
525 string_list_append(extra_refs
, full_name
)->util
= commit
;
527 commit
->util
= full_name
;
531 static void handle_tags_and_duplicates(struct string_list
*extra_refs
)
533 struct commit
*commit
;
536 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
537 const char *name
= extra_refs
->items
[i
].string
;
538 struct object
*object
= extra_refs
->items
[i
].util
;
539 switch (object
->type
) {
541 handle_tag(name
, (struct tag
*)object
);
544 /* create refs pointing to already seen commits */
545 commit
= (struct commit
*)object
;
546 printf("reset %s\nfrom :%d\n\n", name
,
547 get_object_mark(&commit
->object
));
554 static void export_marks(char *file
)
558 struct object_decoration
*deco
= idnums
.hash
;
562 f
= fopen(file
, "w");
564 die_errno("Unable to open marks file %s for writing.", file
);
566 for (i
= 0; i
< idnums
.size
; i
++) {
567 if (deco
->base
&& deco
->base
->type
== 1) {
568 mark
= ptr_to_mark(deco
->decoration
);
569 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
570 sha1_to_hex(deco
->base
->sha1
)) < 0) {
581 error("Unable to write marks file %s.", file
);
584 static void import_marks(char *input_file
)
587 FILE *f
= fopen(input_file
, "r");
589 die_errno("cannot read '%s'", input_file
);
591 while (fgets(line
, sizeof(line
), f
)) {
593 char *line_end
, *mark_end
;
594 unsigned char sha1
[20];
595 struct object
*object
;
597 line_end
= strchr(line
, '\n');
598 if (line
[0] != ':' || !line_end
)
599 die("corrupt mark line: %s", line
);
602 mark
= strtoumax(line
+ 1, &mark_end
, 10);
603 if (!mark
|| mark_end
== line
+ 1
604 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
605 die("corrupt mark line: %s", line
);
607 object
= parse_object(sha1
);
609 die ("Could not read blob %s", sha1_to_hex(sha1
));
611 if (object
->flags
& SHOWN
)
612 error("Object %s already has a mark", sha1
);
614 mark_object(object
, mark
);
615 if (last_idnum
< mark
)
618 object
->flags
|= SHOWN
;
623 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
625 struct rev_info revs
;
626 struct object_array commits
= OBJECT_ARRAY_INIT
;
627 struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
628 struct commit
*commit
;
629 char *export_filename
= NULL
, *import_filename
= NULL
;
630 struct option options
[] = {
631 OPT_INTEGER(0, "progress", &progress
,
632 "show progress after <n> objects"),
633 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, "mode",
634 "select handling of signed tags",
635 parse_opt_signed_tag_mode
),
636 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, "mode",
637 "select handling of tags that tag filtered objects",
638 parse_opt_tag_of_filtered_mode
),
639 OPT_STRING(0, "export-marks", &export_filename
, "file",
640 "Dump marks to this file"),
641 OPT_STRING(0, "import-marks", &import_filename
, "file",
642 "Import marks from this file"),
643 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger
,
644 "Fake a tagger when tags lack one"),
645 OPT_BOOLEAN(0, "full-tree", &full_tree
,
646 "Output full tree for each commit"),
647 { OPTION_NEGBIT
, 0, "data", &no_data
, NULL
,
648 "Skip output of blob data",
649 PARSE_OPT_NOARG
| PARSE_OPT_NEGHELP
, NULL
, 1 },
654 usage_with_options (fast_export_usage
, options
);
656 /* we handle encodings */
657 git_config(git_default_config
, NULL
);
659 init_revisions(&revs
, prefix
);
661 revs
.show_source
= 1;
662 revs
.rewrite_parents
= 1;
663 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
664 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
, 0);
666 usage_with_options (fast_export_usage
, options
);
669 import_marks(import_filename
);
671 if (import_filename
&& revs
.prune_data
.nr
)
674 get_tags_and_duplicates(&revs
.pending
, &extra_refs
);
676 if (prepare_revision_walk(&revs
))
677 die("revision walk setup failed");
678 revs
.diffopt
.format_callback
= show_filemodify
;
679 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
680 while ((commit
= get_revision(&revs
))) {
681 if (has_unshown_parent(commit
)) {
682 add_object_array(&commit
->object
, NULL
, &commits
);
685 handle_commit(commit
, &revs
);
686 handle_tail(&commits
, &revs
);
690 handle_tags_and_duplicates(&extra_refs
);
693 export_marks(export_filename
);