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
);
170 return (len_b
- len_a
);
173 static void show_filemodify(struct diff_queue_struct
*q
,
174 struct diff_options
*options
, void *data
)
179 * Handle files below a directory first, in case they are all deleted
180 * and the directory changes to a file or symlink.
182 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), depth_first
);
184 for (i
= 0; i
< q
->nr
; i
++) {
185 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
186 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
188 switch (q
->queue
[i
]->status
) {
189 case DIFF_STATUS_DELETED
:
190 printf("D %s\n", spec
->path
);
193 case DIFF_STATUS_COPIED
:
194 case DIFF_STATUS_RENAMED
:
195 printf("%c \"%s\" \"%s\"\n", q
->queue
[i
]->status
,
196 ospec
->path
, spec
->path
);
198 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
199 ospec
->mode
== spec
->mode
)
203 case DIFF_STATUS_TYPE_CHANGED
:
204 case DIFF_STATUS_MODIFIED
:
205 case DIFF_STATUS_ADDED
:
207 * Links refer to objects in another repositories;
208 * output the SHA-1 verbatim.
210 if (no_data
|| S_ISGITLINK(spec
->mode
))
211 printf("M %06o %s %s\n", spec
->mode
,
212 sha1_to_hex(spec
->sha1
), spec
->path
);
214 struct object
*object
= lookup_object(spec
->sha1
);
215 printf("M %06o :%d %s\n", spec
->mode
,
216 get_object_mark(object
), spec
->path
);
221 die("Unexpected comparison status '%c' for %s, %s",
223 ospec
->path
? ospec
->path
: "none",
224 spec
->path
? spec
->path
: "none");
229 static const char *find_encoding(const char *begin
, const char *end
)
231 const char *needle
= "\nencoding ";
234 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
235 needle
, strlen(needle
));
237 return git_commit_encoding
;
238 bol
+= strlen(needle
);
239 eol
= strchrnul(bol
, '\n');
244 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
246 int saved_output_format
= rev
->diffopt
.output_format
;
247 const char *author
, *author_end
, *committer
, *committer_end
;
248 const char *encoding
, *message
;
249 char *reencoded
= NULL
;
250 struct commit_list
*p
;
253 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
255 parse_commit(commit
);
256 author
= strstr(commit
->buffer
, "\nauthor ");
258 die ("Could not find author in commit %s",
259 sha1_to_hex(commit
->object
.sha1
));
261 author_end
= strchrnul(author
, '\n');
262 committer
= strstr(author_end
, "\ncommitter ");
264 die ("Could not find committer in commit %s",
265 sha1_to_hex(commit
->object
.sha1
));
267 committer_end
= strchrnul(committer
, '\n');
268 message
= strstr(committer_end
, "\n\n");
269 encoding
= find_encoding(committer_end
, message
);
273 if (commit
->parents
&&
274 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
276 parse_commit(commit
->parents
->item
);
277 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
278 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
281 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
284 /* Export the referenced blobs, and remember the marks. */
285 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
286 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
287 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
289 mark_next_object(&commit
->object
);
290 if (!is_encoding_utf8(encoding
))
291 reencoded
= reencode_string(message
, "UTF-8", encoding
);
292 if (!commit
->parents
)
293 printf("reset %s\n", (const char*)commit
->util
);
294 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
295 (const char *)commit
->util
, last_idnum
,
296 (int)(author_end
- author
), author
,
297 (int)(committer_end
- committer
), committer
,
299 ? strlen(reencoded
) : message
300 ? strlen(message
) : 0),
301 reencoded
? reencoded
: message
? message
: "");
304 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
305 int mark
= get_object_mark(&p
->item
->object
);
309 printf("from :%d\n", mark
);
311 printf("merge :%d\n", mark
);
316 printf("deleteall\n");
317 log_tree_diff_flush(rev
);
318 rev
->diffopt
.output_format
= saved_output_format
;
325 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
327 struct commit
*commit
;
328 while (commits
->nr
) {
329 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
330 if (has_unshown_parent(commit
))
332 handle_commit(commit
, revs
);
337 static void handle_tag(const char *name
, struct tag
*tag
)
340 enum object_type type
;
342 const char *tagger
, *tagger_end
, *message
;
343 size_t message_size
= 0;
344 struct object
*tagged
;
348 /* Trees have no identifer in fast-export output, thus we have no way
349 * to output tags of trees, tags of tags of trees, etc. Simply omit
352 tagged
= tag
->tagged
;
353 while (tagged
->type
== OBJ_TAG
) {
354 tagged
= ((struct tag
*)tagged
)->tagged
;
356 if (tagged
->type
== OBJ_TREE
) {
357 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
358 sha1_to_hex(tag
->object
.sha1
));
362 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
364 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
365 message
= memmem(buf
, size
, "\n\n", 2);
368 message_size
= strlen(message
);
370 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
372 if (fake_missing_tagger
)
373 tagger
= "tagger Unspecified Tagger "
374 "<unspecified-tagger> 0 +0000";
377 tagger_end
= tagger
+ strlen(tagger
);
380 tagger_end
= strchrnul(tagger
, '\n');
383 /* handle signed tags */
385 const char *signature
= strstr(message
,
386 "\n-----BEGIN PGP SIGNATURE-----\n");
388 switch(signed_tag_mode
) {
390 die ("Encountered signed tag %s; use "
391 "--signed-tag=<mode> to handle it.",
392 sha1_to_hex(tag
->object
.sha1
));
394 warning ("Exporting signed tag %s",
395 sha1_to_hex(tag
->object
.sha1
));
400 message_size
= signature
+ 1 - message
;
405 /* handle tag->tagged having been filtered out due to paths specified */
406 tagged
= tag
->tagged
;
407 tagged_mark
= get_object_mark(tagged
);
409 switch(tag_of_filtered_mode
) {
411 die ("Tag %s tags unexported object; use "
412 "--tag-of-filtered-object=<mode> to handle it.",
413 sha1_to_hex(tag
->object
.sha1
));
415 /* Ignore this tag altogether */
418 if (tagged
->type
!= OBJ_COMMIT
) {
419 die ("Tag %s tags unexported %s!",
420 sha1_to_hex(tag
->object
.sha1
),
421 typename(tagged
->type
));
423 p
= (struct commit
*)tagged
;
425 if (p
->parents
&& p
->parents
->next
)
427 if (p
->object
.flags
& UNINTERESTING
)
429 if (!(p
->object
.flags
& TREESAME
))
432 die ("Can't find replacement commit for tag %s\n",
433 sha1_to_hex(tag
->object
.sha1
));
434 p
= p
->parents
->item
;
436 tagged_mark
= get_object_mark(&p
->object
);
440 if (!prefixcmp(name
, "refs/tags/"))
442 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
444 (int)(tagger_end
- tagger
), tagger
,
445 tagger
== tagger_end
? "" : "\n",
446 (int)message_size
, (int)message_size
, message
? message
: "");
449 static void get_tags_and_duplicates(struct object_array
*pending
,
450 struct string_list
*extra_refs
)
455 for (i
= 0; i
< pending
->nr
; i
++) {
456 struct object_array_entry
*e
= pending
->objects
+ i
;
457 unsigned char sha1
[20];
458 struct commit
*commit
= commit
;
461 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
464 switch (e
->item
->type
) {
466 commit
= (struct commit
*)e
->item
;
469 tag
= (struct tag
*)e
->item
;
471 /* handle nested tags */
472 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
473 parse_object(tag
->object
.sha1
);
474 string_list_append(extra_refs
, full_name
)->util
= tag
;
475 tag
= (struct tag
*)tag
->tagged
;
478 die ("Tag %s points nowhere?", e
->name
);
479 switch(tag
->object
.type
) {
481 commit
= (struct commit
*)tag
;
484 handle_object(tag
->object
.sha1
);
486 default: /* OBJ_TAG (nested tags) is already handled */
487 warning("Tag points to object of unexpected type %s, skipping.",
488 typename(tag
->object
.type
));
493 warning("%s: Unexpected object of type %s, skipping.",
495 typename(e
->item
->type
));
499 /* more than one name for the same object */
500 string_list_append(extra_refs
, full_name
)->util
= commit
;
502 commit
->util
= full_name
;
506 static void handle_tags_and_duplicates(struct string_list
*extra_refs
)
508 struct commit
*commit
;
511 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
512 const char *name
= extra_refs
->items
[i
].string
;
513 struct object
*object
= extra_refs
->items
[i
].util
;
514 switch (object
->type
) {
516 handle_tag(name
, (struct tag
*)object
);
519 /* create refs pointing to already seen commits */
520 commit
= (struct commit
*)object
;
521 printf("reset %s\nfrom :%d\n\n", name
,
522 get_object_mark(&commit
->object
));
529 static void export_marks(char *file
)
533 struct object_decoration
*deco
= idnums
.hash
;
537 f
= fopen(file
, "w");
539 die_errno("Unable to open marks file %s for writing.", file
);
541 for (i
= 0; i
< idnums
.size
; i
++) {
542 if (deco
->base
&& deco
->base
->type
== 1) {
543 mark
= ptr_to_mark(deco
->decoration
);
544 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
545 sha1_to_hex(deco
->base
->sha1
)) < 0) {
556 error("Unable to write marks file %s.", file
);
559 static void import_marks(char *input_file
)
562 FILE *f
= fopen(input_file
, "r");
564 die_errno("cannot read '%s'", input_file
);
566 while (fgets(line
, sizeof(line
), f
)) {
568 char *line_end
, *mark_end
;
569 unsigned char sha1
[20];
570 struct object
*object
;
572 line_end
= strchr(line
, '\n');
573 if (line
[0] != ':' || !line_end
)
574 die("corrupt mark line: %s", line
);
577 mark
= strtoumax(line
+ 1, &mark_end
, 10);
578 if (!mark
|| mark_end
== line
+ 1
579 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
580 die("corrupt mark line: %s", line
);
582 object
= parse_object(sha1
);
584 die ("Could not read blob %s", sha1_to_hex(sha1
));
586 if (object
->flags
& SHOWN
)
587 error("Object %s already has a mark", sha1
);
589 mark_object(object
, mark
);
590 if (last_idnum
< mark
)
593 object
->flags
|= SHOWN
;
598 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
600 struct rev_info revs
;
601 struct object_array commits
= OBJECT_ARRAY_INIT
;
602 struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
603 struct commit
*commit
;
604 char *export_filename
= NULL
, *import_filename
= NULL
;
605 struct option options
[] = {
606 OPT_INTEGER(0, "progress", &progress
,
607 "show progress after <n> objects"),
608 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, "mode",
609 "select handling of signed tags",
610 parse_opt_signed_tag_mode
),
611 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, "mode",
612 "select handling of tags that tag filtered objects",
613 parse_opt_tag_of_filtered_mode
),
614 OPT_STRING(0, "export-marks", &export_filename
, "FILE",
615 "Dump marks to this file"),
616 OPT_STRING(0, "import-marks", &import_filename
, "FILE",
617 "Import marks from this file"),
618 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger
,
619 "Fake a tagger when tags lack one"),
620 OPT_BOOLEAN(0, "full-tree", &full_tree
,
621 "Output full tree for each commit"),
622 { OPTION_NEGBIT
, 0, "data", &no_data
, NULL
,
623 "Skip output of blob data",
624 PARSE_OPT_NOARG
| PARSE_OPT_NEGHELP
, NULL
, 1 },
629 usage_with_options (fast_export_usage
, options
);
631 /* we handle encodings */
632 git_config(git_default_config
, NULL
);
634 init_revisions(&revs
, prefix
);
636 revs
.show_source
= 1;
637 revs
.rewrite_parents
= 1;
638 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
639 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
, 0);
641 usage_with_options (fast_export_usage
, options
);
644 import_marks(import_filename
);
646 if (import_filename
&& revs
.prune_data
)
649 get_tags_and_duplicates(&revs
.pending
, &extra_refs
);
651 if (prepare_revision_walk(&revs
))
652 die("revision walk setup failed");
653 revs
.diffopt
.format_callback
= show_filemodify
;
654 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
655 while ((commit
= get_revision(&revs
))) {
656 if (has_unshown_parent(commit
)) {
657 add_object_array(&commit
->object
, NULL
, &commits
);
660 handle_commit(commit
, &revs
);
661 handle_tail(&commits
, &revs
);
665 handle_tags_and_duplicates(&extra_refs
);
668 export_marks(export_filename
);