2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
16 #include "string-list.h"
18 #include "parse-options.h"
20 static const char *fast_export_usage
[] = {
21 "git fast-export [rev-list-opts]",
26 static enum { ABORT
, VERBATIM
, WARN
, STRIP
} signed_tag_mode
= ABORT
;
27 static enum { ERROR
, DROP
, REWRITE
} tag_of_filtered_mode
= ABORT
;
28 static int fake_missing_tagger
;
32 static int parse_opt_signed_tag_mode(const struct option
*opt
,
33 const char *arg
, int unset
)
35 if (unset
|| !strcmp(arg
, "abort"))
36 signed_tag_mode
= ABORT
;
37 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
38 signed_tag_mode
= VERBATIM
;
39 else if (!strcmp(arg
, "warn"))
40 signed_tag_mode
= WARN
;
41 else if (!strcmp(arg
, "strip"))
42 signed_tag_mode
= STRIP
;
44 return error("Unknown signed-tag mode: %s", arg
);
48 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
49 const char *arg
, int unset
)
51 if (unset
|| !strcmp(arg
, "abort"))
52 tag_of_filtered_mode
= ABORT
;
53 else if (!strcmp(arg
, "drop"))
54 tag_of_filtered_mode
= DROP
;
55 else if (!strcmp(arg
, "rewrite"))
56 tag_of_filtered_mode
= REWRITE
;
58 return error("Unknown tag-of-filtered mode: %s", arg
);
62 static struct decoration idnums
;
63 static uint32_t last_idnum
;
65 static int has_unshown_parent(struct commit
*commit
)
67 struct commit_list
*parent
;
69 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
70 if (!(parent
->item
->object
.flags
& SHOWN
) &&
71 !(parent
->item
->object
.flags
& UNINTERESTING
))
76 /* Since intptr_t is C99, we do not use it here */
77 static inline uint32_t *mark_to_ptr(uint32_t mark
)
79 return ((uint32_t *)NULL
) + mark
;
82 static inline uint32_t ptr_to_mark(void * mark
)
84 return (uint32_t *)mark
- (uint32_t *)NULL
;
87 static inline void mark_object(struct object
*object
, uint32_t mark
)
89 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
92 static inline void mark_next_object(struct object
*object
)
94 mark_object(object
, ++last_idnum
);
97 static int get_object_mark(struct object
*object
)
99 void *decoration
= lookup_decoration(&idnums
, object
);
102 return ptr_to_mark(decoration
);
105 static void show_progress(void)
107 static int counter
= 0;
110 if ((++counter
% progress
) == 0)
111 printf("progress %d objects\n", counter
);
114 static void handle_object(const unsigned char *sha1
)
117 enum object_type type
;
119 struct object
*object
;
124 if (is_null_sha1(sha1
))
127 object
= parse_object(sha1
);
129 die ("Could not read blob %s", sha1_to_hex(sha1
));
131 if (object
->flags
& SHOWN
)
134 buf
= read_sha1_file(sha1
, &type
, &size
);
136 die ("Could not read blob %s", sha1_to_hex(sha1
));
138 mark_next_object(object
);
140 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
141 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
142 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1
));
147 object
->flags
|= SHOWN
;
151 static int depth_first(const void *a_
, const void *b_
)
153 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
154 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
155 const char *name_a
, *name_b
;
156 int len_a
, len_b
, len
;
159 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
160 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
162 len_a
= strlen(name_a
);
163 len_b
= strlen(name_b
);
164 len
= (len_a
< len_b
) ? len_a
: len_b
;
166 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
167 cmp
= memcmp(name_a
, name_b
, len
);
174 * Move 'R'ename entries last so that all references of the file
175 * appear in the output before it is renamed (e.g., when a file
176 * was copied and renamed in the same commit).
178 return (a
->status
== 'R') - (b
->status
== 'R');
181 static void show_filemodify(struct diff_queue_struct
*q
,
182 struct diff_options
*options
, void *data
)
187 * Handle files below a directory first, in case they are all deleted
188 * and the directory changes to a file or symlink.
190 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), depth_first
);
192 for (i
= 0; i
< q
->nr
; i
++) {
193 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
194 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
196 switch (q
->queue
[i
]->status
) {
197 case DIFF_STATUS_DELETED
:
198 printf("D %s\n", spec
->path
);
201 case DIFF_STATUS_COPIED
:
202 case DIFF_STATUS_RENAMED
:
203 printf("%c \"%s\" \"%s\"\n", q
->queue
[i
]->status
,
204 ospec
->path
, spec
->path
);
206 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
207 ospec
->mode
== spec
->mode
)
211 case DIFF_STATUS_TYPE_CHANGED
:
212 case DIFF_STATUS_MODIFIED
:
213 case DIFF_STATUS_ADDED
:
215 * Links refer to objects in another repositories;
216 * output the SHA-1 verbatim.
218 if (no_data
|| S_ISGITLINK(spec
->mode
))
219 printf("M %06o %s %s\n", spec
->mode
,
220 sha1_to_hex(spec
->sha1
), spec
->path
);
222 struct object
*object
= lookup_object(spec
->sha1
);
223 printf("M %06o :%d %s\n", spec
->mode
,
224 get_object_mark(object
), spec
->path
);
229 die("Unexpected comparison status '%c' for %s, %s",
231 ospec
->path
? ospec
->path
: "none",
232 spec
->path
? spec
->path
: "none");
237 static const char *find_encoding(const char *begin
, const char *end
)
239 const char *needle
= "\nencoding ";
242 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
243 needle
, strlen(needle
));
245 return git_commit_encoding
;
246 bol
+= strlen(needle
);
247 eol
= strchrnul(bol
, '\n');
252 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
254 int saved_output_format
= rev
->diffopt
.output_format
;
255 const char *author
, *author_end
, *committer
, *committer_end
;
256 const char *encoding
, *message
;
257 char *reencoded
= NULL
;
258 struct commit_list
*p
;
261 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
263 parse_commit(commit
);
264 author
= strstr(commit
->buffer
, "\nauthor ");
266 die ("Could not find author in commit %s",
267 sha1_to_hex(commit
->object
.sha1
));
269 author_end
= strchrnul(author
, '\n');
270 committer
= strstr(author_end
, "\ncommitter ");
272 die ("Could not find committer in commit %s",
273 sha1_to_hex(commit
->object
.sha1
));
275 committer_end
= strchrnul(committer
, '\n');
276 message
= strstr(committer_end
, "\n\n");
277 encoding
= find_encoding(committer_end
, message
);
281 if (commit
->parents
&&
282 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
284 parse_commit(commit
->parents
->item
);
285 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
286 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
289 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
292 /* Export the referenced blobs, and remember the marks. */
293 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
294 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
295 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
297 mark_next_object(&commit
->object
);
298 if (!is_encoding_utf8(encoding
))
299 reencoded
= reencode_string(message
, "UTF-8", encoding
);
300 if (!commit
->parents
)
301 printf("reset %s\n", (const char*)commit
->util
);
302 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
303 (const char *)commit
->util
, last_idnum
,
304 (int)(author_end
- author
), author
,
305 (int)(committer_end
- committer
), committer
,
307 ? strlen(reencoded
) : message
308 ? strlen(message
) : 0),
309 reencoded
? reencoded
: message
? message
: "");
312 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
313 int mark
= get_object_mark(&p
->item
->object
);
317 printf("from :%d\n", mark
);
319 printf("merge :%d\n", mark
);
324 printf("deleteall\n");
325 log_tree_diff_flush(rev
);
326 rev
->diffopt
.output_format
= saved_output_format
;
333 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
335 struct commit
*commit
;
336 while (commits
->nr
) {
337 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
338 if (has_unshown_parent(commit
))
340 handle_commit(commit
, revs
);
345 static void handle_tag(const char *name
, struct tag
*tag
)
348 enum object_type type
;
350 const char *tagger
, *tagger_end
, *message
;
351 size_t message_size
= 0;
352 struct object
*tagged
;
356 /* Trees have no identifer in fast-export output, thus we have no way
357 * to output tags of trees, tags of tags of trees, etc. Simply omit
360 tagged
= tag
->tagged
;
361 while (tagged
->type
== OBJ_TAG
) {
362 tagged
= ((struct tag
*)tagged
)->tagged
;
364 if (tagged
->type
== OBJ_TREE
) {
365 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
366 sha1_to_hex(tag
->object
.sha1
));
370 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
372 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
373 message
= memmem(buf
, size
, "\n\n", 2);
376 message_size
= strlen(message
);
378 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
380 if (fake_missing_tagger
)
381 tagger
= "tagger Unspecified Tagger "
382 "<unspecified-tagger> 0 +0000";
385 tagger_end
= tagger
+ strlen(tagger
);
388 tagger_end
= strchrnul(tagger
, '\n');
391 /* handle signed tags */
393 const char *signature
= strstr(message
,
394 "\n-----BEGIN PGP SIGNATURE-----\n");
396 switch(signed_tag_mode
) {
398 die ("Encountered signed tag %s; use "
399 "--signed-tag=<mode> to handle it.",
400 sha1_to_hex(tag
->object
.sha1
));
402 warning ("Exporting signed tag %s",
403 sha1_to_hex(tag
->object
.sha1
));
408 message_size
= signature
+ 1 - message
;
413 /* handle tag->tagged having been filtered out due to paths specified */
414 tagged
= tag
->tagged
;
415 tagged_mark
= get_object_mark(tagged
);
417 switch(tag_of_filtered_mode
) {
419 die ("Tag %s tags unexported object; use "
420 "--tag-of-filtered-object=<mode> to handle it.",
421 sha1_to_hex(tag
->object
.sha1
));
423 /* Ignore this tag altogether */
426 if (tagged
->type
!= OBJ_COMMIT
) {
427 die ("Tag %s tags unexported %s!",
428 sha1_to_hex(tag
->object
.sha1
),
429 typename(tagged
->type
));
431 p
= (struct commit
*)tagged
;
433 if (p
->parents
&& p
->parents
->next
)
435 if (p
->object
.flags
& UNINTERESTING
)
437 if (!(p
->object
.flags
& TREESAME
))
440 die ("Can't find replacement commit for tag %s\n",
441 sha1_to_hex(tag
->object
.sha1
));
442 p
= p
->parents
->item
;
444 tagged_mark
= get_object_mark(&p
->object
);
448 if (!prefixcmp(name
, "refs/tags/"))
450 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
452 (int)(tagger_end
- tagger
), tagger
,
453 tagger
== tagger_end
? "" : "\n",
454 (int)message_size
, (int)message_size
, message
? message
: "");
457 static void get_tags_and_duplicates(struct object_array
*pending
,
458 struct string_list
*extra_refs
)
463 for (i
= 0; i
< pending
->nr
; i
++) {
464 struct object_array_entry
*e
= pending
->objects
+ i
;
465 unsigned char sha1
[20];
466 struct commit
*commit
= commit
;
469 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
472 switch (e
->item
->type
) {
474 commit
= (struct commit
*)e
->item
;
477 tag
= (struct tag
*)e
->item
;
479 /* handle nested tags */
480 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
481 parse_object(tag
->object
.sha1
);
482 string_list_append(extra_refs
, full_name
)->util
= tag
;
483 tag
= (struct tag
*)tag
->tagged
;
486 die ("Tag %s points nowhere?", e
->name
);
487 switch(tag
->object
.type
) {
489 commit
= (struct commit
*)tag
;
492 handle_object(tag
->object
.sha1
);
494 default: /* OBJ_TAG (nested tags) is already handled */
495 warning("Tag points to object of unexpected type %s, skipping.",
496 typename(tag
->object
.type
));
501 warning("%s: Unexpected object of type %s, skipping.",
503 typename(e
->item
->type
));
507 /* more than one name for the same object */
508 string_list_append(extra_refs
, full_name
)->util
= commit
;
510 commit
->util
= full_name
;
514 static void handle_tags_and_duplicates(struct string_list
*extra_refs
)
516 struct commit
*commit
;
519 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
520 const char *name
= extra_refs
->items
[i
].string
;
521 struct object
*object
= extra_refs
->items
[i
].util
;
522 switch (object
->type
) {
524 handle_tag(name
, (struct tag
*)object
);
527 /* create refs pointing to already seen commits */
528 commit
= (struct commit
*)object
;
529 printf("reset %s\nfrom :%d\n\n", name
,
530 get_object_mark(&commit
->object
));
537 static void export_marks(char *file
)
541 struct object_decoration
*deco
= idnums
.hash
;
545 f
= fopen(file
, "w");
547 die_errno("Unable to open marks file %s for writing.", file
);
549 for (i
= 0; i
< idnums
.size
; i
++) {
550 if (deco
->base
&& deco
->base
->type
== 1) {
551 mark
= ptr_to_mark(deco
->decoration
);
552 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
553 sha1_to_hex(deco
->base
->sha1
)) < 0) {
564 error("Unable to write marks file %s.", file
);
567 static void import_marks(char *input_file
)
570 FILE *f
= fopen(input_file
, "r");
572 die_errno("cannot read '%s'", input_file
);
574 while (fgets(line
, sizeof(line
), f
)) {
576 char *line_end
, *mark_end
;
577 unsigned char sha1
[20];
578 struct object
*object
;
580 line_end
= strchr(line
, '\n');
581 if (line
[0] != ':' || !line_end
)
582 die("corrupt mark line: %s", line
);
585 mark
= strtoumax(line
+ 1, &mark_end
, 10);
586 if (!mark
|| mark_end
== line
+ 1
587 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
588 die("corrupt mark line: %s", line
);
590 object
= parse_object(sha1
);
592 die ("Could not read blob %s", sha1_to_hex(sha1
));
594 if (object
->flags
& SHOWN
)
595 error("Object %s already has a mark", sha1
);
597 mark_object(object
, mark
);
598 if (last_idnum
< mark
)
601 object
->flags
|= SHOWN
;
606 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
608 struct rev_info revs
;
609 struct object_array commits
= OBJECT_ARRAY_INIT
;
610 struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
611 struct commit
*commit
;
612 char *export_filename
= NULL
, *import_filename
= NULL
;
613 struct option options
[] = {
614 OPT_INTEGER(0, "progress", &progress
,
615 "show progress after <n> objects"),
616 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, "mode",
617 "select handling of signed tags",
618 parse_opt_signed_tag_mode
),
619 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, "mode",
620 "select handling of tags that tag filtered objects",
621 parse_opt_tag_of_filtered_mode
),
622 OPT_STRING(0, "export-marks", &export_filename
, "FILE",
623 "Dump marks to this file"),
624 OPT_STRING(0, "import-marks", &import_filename
, "FILE",
625 "Import marks from this file"),
626 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger
,
627 "Fake a tagger when tags lack one"),
628 OPT_BOOLEAN(0, "full-tree", &full_tree
,
629 "Output full tree for each commit"),
630 { OPTION_NEGBIT
, 0, "data", &no_data
, NULL
,
631 "Skip output of blob data",
632 PARSE_OPT_NOARG
| PARSE_OPT_NEGHELP
, NULL
, 1 },
637 usage_with_options (fast_export_usage
, options
);
639 /* we handle encodings */
640 git_config(git_default_config
, NULL
);
642 init_revisions(&revs
, prefix
);
644 revs
.show_source
= 1;
645 revs
.rewrite_parents
= 1;
646 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
647 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
, 0);
649 usage_with_options (fast_export_usage
, options
);
652 import_marks(import_filename
);
654 if (import_filename
&& revs
.prune_data
.nr
)
657 get_tags_and_duplicates(&revs
.pending
, &extra_refs
);
659 if (prepare_revision_walk(&revs
))
660 die("revision walk setup failed");
661 revs
.diffopt
.format_callback
= show_filemodify
;
662 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
663 while ((commit
= get_revision(&revs
))) {
664 if (has_unshown_parent(commit
)) {
665 add_object_array(&commit
->object
, NULL
, &commits
);
668 handle_commit(commit
, &revs
);
669 handle_tail(&commits
, &revs
);
673 handle_tags_and_duplicates(&extra_refs
);
676 export_marks(export_filename
);