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
;
30 static int parse_opt_signed_tag_mode(const struct option
*opt
,
31 const char *arg
, int unset
)
33 if (unset
|| !strcmp(arg
, "abort"))
34 signed_tag_mode
= ABORT
;
35 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
36 signed_tag_mode
= VERBATIM
;
37 else if (!strcmp(arg
, "warn"))
38 signed_tag_mode
= WARN
;
39 else if (!strcmp(arg
, "strip"))
40 signed_tag_mode
= STRIP
;
42 return error("Unknown signed-tag mode: %s", arg
);
46 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
47 const char *arg
, int unset
)
49 if (unset
|| !strcmp(arg
, "abort"))
50 tag_of_filtered_mode
= ABORT
;
51 else if (!strcmp(arg
, "drop"))
52 tag_of_filtered_mode
= DROP
;
53 else if (!strcmp(arg
, "rewrite"))
54 tag_of_filtered_mode
= REWRITE
;
56 return error("Unknown tag-of-filtered mode: %s", arg
);
60 static struct decoration idnums
;
61 static uint32_t last_idnum
;
63 static int has_unshown_parent(struct commit
*commit
)
65 struct commit_list
*parent
;
67 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
68 if (!(parent
->item
->object
.flags
& SHOWN
) &&
69 !(parent
->item
->object
.flags
& UNINTERESTING
))
74 /* Since intptr_t is C99, we do not use it here */
75 static inline uint32_t *mark_to_ptr(uint32_t mark
)
77 return ((uint32_t *)NULL
) + mark
;
80 static inline uint32_t ptr_to_mark(void * mark
)
82 return (uint32_t *)mark
- (uint32_t *)NULL
;
85 static inline void mark_object(struct object
*object
, uint32_t mark
)
87 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
90 static inline void mark_next_object(struct object
*object
)
92 mark_object(object
, ++last_idnum
);
95 static int get_object_mark(struct object
*object
)
97 void *decoration
= lookup_decoration(&idnums
, object
);
100 return ptr_to_mark(decoration
);
103 static void show_progress(void)
105 static int counter
= 0;
108 if ((++counter
% progress
) == 0)
109 printf("progress %d objects\n", counter
);
112 static void handle_object(const unsigned char *sha1
)
115 enum object_type type
;
117 struct object
*object
;
119 if (is_null_sha1(sha1
))
122 object
= parse_object(sha1
);
124 die ("Could not read blob %s", sha1_to_hex(sha1
));
126 if (object
->flags
& SHOWN
)
129 buf
= read_sha1_file(sha1
, &type
, &size
);
131 die ("Could not read blob %s", sha1_to_hex(sha1
));
133 mark_next_object(object
);
135 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
136 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
137 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1
));
142 object
->flags
|= SHOWN
;
146 static void show_filemodify(struct diff_queue_struct
*q
,
147 struct diff_options
*options
, void *data
)
150 for (i
= 0; i
< q
->nr
; i
++) {
151 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
152 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
154 switch (q
->queue
[i
]->status
) {
155 case DIFF_STATUS_DELETED
:
156 printf("D %s\n", spec
->path
);
159 case DIFF_STATUS_COPIED
:
160 case DIFF_STATUS_RENAMED
:
161 printf("%c \"%s\" \"%s\"\n", q
->queue
[i
]->status
,
162 ospec
->path
, spec
->path
);
164 if (!hashcmp(ospec
->sha1
, spec
->sha1
) &&
165 ospec
->mode
== spec
->mode
)
169 case DIFF_STATUS_TYPE_CHANGED
:
170 case DIFF_STATUS_MODIFIED
:
171 case DIFF_STATUS_ADDED
:
173 * Links refer to objects in another repositories;
174 * output the SHA-1 verbatim.
176 if (S_ISGITLINK(spec
->mode
))
177 printf("M %06o %s %s\n", spec
->mode
,
178 sha1_to_hex(spec
->sha1
), spec
->path
);
180 struct object
*object
= lookup_object(spec
->sha1
);
181 printf("M %06o :%d %s\n", spec
->mode
,
182 get_object_mark(object
), spec
->path
);
187 die("Unexpected comparison status '%c' for %s, %s",
189 ospec
->path
? ospec
->path
: "none",
190 spec
->path
? spec
->path
: "none");
195 static const char *find_encoding(const char *begin
, const char *end
)
197 const char *needle
= "\nencoding ";
200 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
201 needle
, strlen(needle
));
203 return git_commit_encoding
;
204 bol
+= strlen(needle
);
205 eol
= strchrnul(bol
, '\n');
210 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
212 int saved_output_format
= rev
->diffopt
.output_format
;
213 const char *author
, *author_end
, *committer
, *committer_end
;
214 const char *encoding
, *message
;
215 char *reencoded
= NULL
;
216 struct commit_list
*p
;
219 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
221 parse_commit(commit
);
222 author
= strstr(commit
->buffer
, "\nauthor ");
224 die ("Could not find author in commit %s",
225 sha1_to_hex(commit
->object
.sha1
));
227 author_end
= strchrnul(author
, '\n');
228 committer
= strstr(author_end
, "\ncommitter ");
230 die ("Could not find committer in commit %s",
231 sha1_to_hex(commit
->object
.sha1
));
233 committer_end
= strchrnul(committer
, '\n');
234 message
= strstr(committer_end
, "\n\n");
235 encoding
= find_encoding(committer_end
, message
);
239 if (commit
->parents
&&
240 get_object_mark(&commit
->parents
->item
->object
) != 0) {
241 parse_commit(commit
->parents
->item
);
242 diff_tree_sha1(commit
->parents
->item
->tree
->object
.sha1
,
243 commit
->tree
->object
.sha1
, "", &rev
->diffopt
);
246 diff_root_tree_sha1(commit
->tree
->object
.sha1
,
249 /* Export the referenced blobs, and remember the marks. */
250 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
251 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
252 handle_object(diff_queued_diff
.queue
[i
]->two
->sha1
);
254 mark_next_object(&commit
->object
);
255 if (!is_encoding_utf8(encoding
))
256 reencoded
= reencode_string(message
, "UTF-8", encoding
);
257 if (!commit
->parents
)
258 printf("reset %s\n", (const char*)commit
->util
);
259 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
260 (const char *)commit
->util
, last_idnum
,
261 (int)(author_end
- author
), author
,
262 (int)(committer_end
- committer
), committer
,
264 ? strlen(reencoded
) : message
265 ? strlen(message
) : 0),
266 reencoded
? reencoded
: message
? message
: "");
269 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
270 int mark
= get_object_mark(&p
->item
->object
);
274 printf("from :%d\n", mark
);
276 printf("merge :%d\n", mark
);
280 log_tree_diff_flush(rev
);
281 rev
->diffopt
.output_format
= saved_output_format
;
288 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
290 struct commit
*commit
;
291 while (commits
->nr
) {
292 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
293 if (has_unshown_parent(commit
))
295 handle_commit(commit
, revs
);
300 static void handle_tag(const char *name
, struct tag
*tag
)
303 enum object_type type
;
305 const char *tagger
, *tagger_end
, *message
;
306 size_t message_size
= 0;
307 struct object
*tagged
;
311 /* Trees have no identifer in fast-export output, thus we have no way
312 * to output tags of trees, tags of tags of trees, etc. Simply omit
315 tagged
= tag
->tagged
;
316 while (tagged
->type
== OBJ_TAG
) {
317 tagged
= ((struct tag
*)tagged
)->tagged
;
319 if (tagged
->type
== OBJ_TREE
) {
320 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
321 sha1_to_hex(tag
->object
.sha1
));
325 buf
= read_sha1_file(tag
->object
.sha1
, &type
, &size
);
327 die ("Could not read tag %s", sha1_to_hex(tag
->object
.sha1
));
328 message
= memmem(buf
, size
, "\n\n", 2);
331 message_size
= strlen(message
);
333 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
335 if (fake_missing_tagger
)
336 tagger
= "tagger Unspecified Tagger "
337 "<unspecified-tagger> 0 +0000";
340 tagger_end
= tagger
+ strlen(tagger
);
343 tagger_end
= strchrnul(tagger
, '\n');
346 /* handle signed tags */
348 const char *signature
= strstr(message
,
349 "\n-----BEGIN PGP SIGNATURE-----\n");
351 switch(signed_tag_mode
) {
353 die ("Encountered signed tag %s; use "
354 "--signed-tag=<mode> to handle it.",
355 sha1_to_hex(tag
->object
.sha1
));
357 warning ("Exporting signed tag %s",
358 sha1_to_hex(tag
->object
.sha1
));
363 message_size
= signature
+ 1 - message
;
368 /* handle tag->tagged having been filtered out due to paths specified */
369 tagged
= tag
->tagged
;
370 tagged_mark
= get_object_mark(tagged
);
372 switch(tag_of_filtered_mode
) {
374 die ("Tag %s tags unexported object; use "
375 "--tag-of-filtered-object=<mode> to handle it.",
376 sha1_to_hex(tag
->object
.sha1
));
378 /* Ignore this tag altogether */
381 if (tagged
->type
!= OBJ_COMMIT
) {
382 die ("Tag %s tags unexported %s!",
383 sha1_to_hex(tag
->object
.sha1
),
384 typename(tagged
->type
));
386 p
= (struct commit
*)tagged
;
388 if (p
->parents
&& p
->parents
->next
)
390 if (p
->object
.flags
& UNINTERESTING
)
392 if (!(p
->object
.flags
& TREESAME
))
395 die ("Can't find replacement commit for tag %s\n",
396 sha1_to_hex(tag
->object
.sha1
));
397 p
= p
->parents
->item
;
399 tagged_mark
= get_object_mark(&p
->object
);
403 if (!prefixcmp(name
, "refs/tags/"))
405 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
407 (int)(tagger_end
- tagger
), tagger
,
408 tagger
== tagger_end
? "" : "\n",
409 (int)message_size
, (int)message_size
, message
? message
: "");
412 static void get_tags_and_duplicates(struct object_array
*pending
,
413 struct string_list
*extra_refs
)
418 for (i
= 0; i
< pending
->nr
; i
++) {
419 struct object_array_entry
*e
= pending
->objects
+ i
;
420 unsigned char sha1
[20];
421 struct commit
*commit
= commit
;
424 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &full_name
) != 1)
427 switch (e
->item
->type
) {
429 commit
= (struct commit
*)e
->item
;
432 tag
= (struct tag
*)e
->item
;
434 /* handle nested tags */
435 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
436 parse_object(tag
->object
.sha1
);
437 string_list_append(full_name
, extra_refs
)->util
= tag
;
438 tag
= (struct tag
*)tag
->tagged
;
441 die ("Tag %s points nowhere?", e
->name
);
442 switch(tag
->object
.type
) {
444 commit
= (struct commit
*)tag
;
447 handle_object(tag
->object
.sha1
);
449 default: /* OBJ_TAG (nested tags) is already handled */
450 warning("Tag points to object of unexpected type %s, skipping.",
451 typename(tag
->object
.type
));
456 warning("%s: Unexpected object of type %s, skipping.",
458 typename(e
->item
->type
));
462 /* more than one name for the same object */
463 string_list_append(full_name
, extra_refs
)->util
= commit
;
465 commit
->util
= full_name
;
469 static void handle_tags_and_duplicates(struct string_list
*extra_refs
)
471 struct commit
*commit
;
474 for (i
= extra_refs
->nr
- 1; i
>= 0; i
--) {
475 const char *name
= extra_refs
->items
[i
].string
;
476 struct object
*object
= extra_refs
->items
[i
].util
;
477 switch (object
->type
) {
479 handle_tag(name
, (struct tag
*)object
);
482 /* create refs pointing to already seen commits */
483 commit
= (struct commit
*)object
;
484 printf("reset %s\nfrom :%d\n\n", name
,
485 get_object_mark(&commit
->object
));
492 static void export_marks(char *file
)
496 struct object_decoration
*deco
= idnums
.hash
;
500 f
= fopen(file
, "w");
502 error("Unable to open marks file %s for writing.", file
);
504 for (i
= 0; i
< idnums
.size
; i
++) {
505 if (deco
->base
&& deco
->base
->type
== 1) {
506 mark
= ptr_to_mark(deco
->decoration
);
507 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
508 sha1_to_hex(deco
->base
->sha1
)) < 0) {
519 error("Unable to write marks file %s.", file
);
522 static void import_marks(char *input_file
)
525 FILE *f
= fopen(input_file
, "r");
527 die_errno("cannot read '%s'", input_file
);
529 while (fgets(line
, sizeof(line
), f
)) {
531 char *line_end
, *mark_end
;
532 unsigned char sha1
[20];
533 struct object
*object
;
535 line_end
= strchr(line
, '\n');
536 if (line
[0] != ':' || !line_end
)
537 die("corrupt mark line: %s", line
);
540 mark
= strtoumax(line
+ 1, &mark_end
, 10);
541 if (!mark
|| mark_end
== line
+ 1
542 || *mark_end
!= ' ' || get_sha1(mark_end
+ 1, sha1
))
543 die("corrupt mark line: %s", line
);
545 object
= parse_object(sha1
);
547 die ("Could not read blob %s", sha1_to_hex(sha1
));
549 if (object
->flags
& SHOWN
)
550 error("Object %s already has a mark", sha1
);
552 mark_object(object
, mark
);
553 if (last_idnum
< mark
)
556 object
->flags
|= SHOWN
;
561 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
563 struct rev_info revs
;
564 struct object_array commits
= { 0, 0, NULL
};
565 struct string_list extra_refs
= { NULL
, 0, 0, 0 };
566 struct commit
*commit
;
567 char *export_filename
= NULL
, *import_filename
= NULL
;
568 struct option options
[] = {
569 OPT_INTEGER(0, "progress", &progress
,
570 "show progress after <n> objects"),
571 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, "mode",
572 "select handling of signed tags",
573 parse_opt_signed_tag_mode
),
574 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, "mode",
575 "select handling of tags that tag filtered objects",
576 parse_opt_tag_of_filtered_mode
),
577 OPT_STRING(0, "export-marks", &export_filename
, "FILE",
578 "Dump marks to this file"),
579 OPT_STRING(0, "import-marks", &import_filename
, "FILE",
580 "Import marks from this file"),
581 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger
,
582 "Fake a tagger when tags lack one"),
587 usage_with_options (fast_export_usage
, options
);
589 /* we handle encodings */
590 git_config(git_default_config
, NULL
);
592 init_revisions(&revs
, prefix
);
594 revs
.show_source
= 1;
595 revs
.rewrite_parents
= 1;
596 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
597 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
, 0);
599 usage_with_options (fast_export_usage
, options
);
602 import_marks(import_filename
);
604 get_tags_and_duplicates(&revs
.pending
, &extra_refs
);
606 if (prepare_revision_walk(&revs
))
607 die("revision walk setup failed");
608 revs
.diffopt
.format_callback
= show_filemodify
;
609 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
610 while ((commit
= get_revision(&revs
))) {
611 if (has_unshown_parent(commit
)) {
612 add_object_array(&commit
->object
, NULL
, &commits
);
615 handle_commit(commit
, &revs
);
616 handle_tail(&commits
, &revs
);
620 handle_tags_and_duplicates(&extra_refs
);
623 export_marks(export_filename
);