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 N_("git fast-export [rev-list-opts]"),
27 static enum { ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = ABORT;
28 static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR;
29 static int fake_missing_tagger;
30 static int use_done_feature;
34 static int parse_opt_signed_tag_mode(const struct option *opt,
35 const char *arg, int unset)
37 if (unset || !strcmp(arg, "abort"))
38 signed_tag_mode = ABORT;
39 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
40 signed_tag_mode = VERBATIM;
41 else if (!strcmp(arg, "warn"))
42 signed_tag_mode = WARN;
43 else if (!strcmp(arg, "warn-strip"))
44 signed_tag_mode = WARN_STRIP;
45 else if (!strcmp(arg, "strip"))
46 signed_tag_mode = STRIP;
48 return error("Unknown signed-tags 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 export_blob(const unsigned char *sha1)
121 enum object_type type;
123 struct object *object;
129 if (is_null_sha1(sha1))
132 object = lookup_object(sha1);
133 if (object && object->flags & SHOWN)
136 buf = read_sha1_file(sha1, &type, &size);
138 die ("Could not read blob %s", sha1_to_hex(sha1));
139 if (check_sha1_signature(sha1, buf, size, typename(type)) < 0)
140 die("sha1 mismatch in blob %s", sha1_to_hex(sha1));
141 object = parse_object_buffer(sha1, type, size, buf, &eaten);
143 die("Could not read blob %s", sha1_to_hex(sha1));
145 mark_next_object(object);
147 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
148 if (size && fwrite(buf, size, 1, stdout) != 1)
149 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1));
154 object->flags |= SHOWN;
159 static int depth_first(const void *a_, const void *b_)
161 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
162 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
163 const char *name_a, *name_b;
164 int len_a, len_b, len;
167 name_a = a->one ? a->one->path : a->two->path;
168 name_b = b->one ? b->one->path : b->two->path;
170 len_a = strlen(name_a);
171 len_b = strlen(name_b);
172 len = (len_a < len_b) ? len_a : len_b;
174 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
175 cmp = memcmp(name_a, name_b, len);
182 * Move 'R'ename entries last so that all references of the file
183 * appear in the output before it is renamed (e.g., when a file
184 * was copied and renamed in the same commit).
186 return (a->status == 'R') - (b->status == 'R');
189 static void print_path(const char *path)
191 int need_quote = quote_c_style(path, NULL, NULL, 0);
193 quote_c_style(path, NULL, stdout, 0);
194 else if (strchr(path, ' '))
195 printf("\"%s\"", path);
200 static void show_filemodify(struct diff_queue_struct *q,
201 struct diff_options *options, void *data)
206 * Handle files below a directory first, in case they are all deleted
207 * and the directory changes to a file or symlink.
209 qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
211 for (i = 0; i < q->nr; i++) {
212 struct diff_filespec *ospec = q->queue[i]->one;
213 struct diff_filespec *spec = q->queue[i]->two;
215 switch (q->queue[i]->status) {
216 case DIFF_STATUS_DELETED:
218 print_path(spec->path);
222 case DIFF_STATUS_COPIED:
223 case DIFF_STATUS_RENAMED:
224 printf("%c ", q->queue[i]->status);
225 print_path(ospec->path);
227 print_path(spec->path);
230 if (!hashcmp(ospec->sha1, spec->sha1) &&
231 ospec->mode == spec->mode)
235 case DIFF_STATUS_TYPE_CHANGED:
236 case DIFF_STATUS_MODIFIED:
237 case DIFF_STATUS_ADDED:
239 * Links refer to objects in another repositories;
240 * output the SHA-1 verbatim.
242 if (no_data || S_ISGITLINK(spec->mode))
243 printf("M %06o %s ", spec->mode,
244 sha1_to_hex(spec->sha1));
246 struct object *object = lookup_object(spec->sha1);
247 printf("M %06o :%d ", spec->mode,
248 get_object_mark(object));
250 print_path(spec->path);
255 die("Unexpected comparison status '%c' for %s, %s",
257 ospec->path ? ospec->path : "none",
258 spec->path ? spec->path : "none");
263 static const char *find_encoding(const char *begin, const char *end)
265 const char *needle = "\nencoding ";
268 bol = memmem(begin, end ? end - begin : strlen(begin),
269 needle, strlen(needle));
271 return git_commit_encoding;
272 bol += strlen(needle);
273 eol = strchrnul(bol, '\n');
278 static void handle_commit(struct commit *commit, struct rev_info *rev)
280 int saved_output_format = rev->diffopt.output_format;
281 const char *author, *author_end, *committer, *committer_end;
282 const char *encoding, *message;
283 char *reencoded = NULL;
284 struct commit_list *p;
287 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
289 parse_commit(commit);
290 author = strstr(commit->buffer, "\nauthor ");
292 die ("Could not find author in commit %s",
293 sha1_to_hex(commit->object.sha1));
295 author_end = strchrnul(author, '\n');
296 committer = strstr(author_end, "\ncommitter ");
298 die ("Could not find committer in commit %s",
299 sha1_to_hex(commit->object.sha1));
301 committer_end = strchrnul(committer, '\n');
302 message = strstr(committer_end, "\n\n");
303 encoding = find_encoding(committer_end, message);
307 if (commit->parents &&
308 get_object_mark(&commit->parents->item->object) != 0 &&
310 parse_commit(commit->parents->item);
311 diff_tree_sha1(commit->parents->item->tree->object.sha1,
312 commit->tree->object.sha1, "", &rev->diffopt);
315 diff_root_tree_sha1(commit->tree->object.sha1,
318 /* Export the referenced blobs, and remember the marks. */
319 for (i = 0; i < diff_queued_diff.nr; i++)
320 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
321 export_blob(diff_queued_diff.queue[i]->two->sha1);
323 mark_next_object(&commit->object);
324 if (!is_encoding_utf8(encoding))
325 reencoded = reencode_string(message, "UTF-8", encoding);
326 if (!commit->parents)
327 printf("reset %s\n", (const char*)commit->util);
328 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
329 (const char *)commit->util, last_idnum,
330 (int)(author_end - author), author,
331 (int)(committer_end - committer), committer,
333 ? strlen(reencoded) : message
334 ? strlen(message) : 0),
335 reencoded ? reencoded : message ? message : "");
338 for (i = 0, p = commit->parents; p; p = p->next) {
339 int mark = get_object_mark(&p->item->object);
343 printf("from :%d\n", mark);
345 printf("merge :%d\n", mark);
350 printf("deleteall\n");
351 log_tree_diff_flush(rev);
352 rev->diffopt.output_format = saved_output_format;
359 static void handle_tail(struct object_array *commits, struct rev_info *revs)
361 struct commit *commit;
362 while (commits->nr) {
363 commit = (struct commit *)commits->objects[commits->nr - 1].item;
364 if (has_unshown_parent(commit))
366 handle_commit(commit, revs);
371 static void handle_tag(const char *name, struct tag *tag)
374 enum object_type type;
376 const char *tagger, *tagger_end, *message;
377 size_t message_size = 0;
378 struct object *tagged;
382 /* Trees have no identifier in fast-export output, thus we have no way
383 * to output tags of trees, tags of tags of trees, etc. Simply omit
386 tagged = tag->tagged;
387 while (tagged->type == OBJ_TAG) {
388 tagged = ((struct tag *)tagged)->tagged;
390 if (tagged->type == OBJ_TREE) {
391 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
392 sha1_to_hex(tag->object.sha1));
396 buf = read_sha1_file(tag->object.sha1, &type, &size);
398 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
399 message = memmem(buf, size, "\n\n", 2);
402 message_size = strlen(message);
404 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
406 if (fake_missing_tagger)
407 tagger = "tagger Unspecified Tagger "
408 "<unspecified-tagger> 0 +0000";
411 tagger_end = tagger + strlen(tagger);
414 tagger_end = strchrnul(tagger, '\n');
417 /* handle signed tags */
419 const char *signature = strstr(message,
420 "\n-----BEGIN PGP SIGNATURE-----\n");
422 switch(signed_tag_mode) {
424 die ("Encountered signed tag %s; use "
425 "--signed-tags=<mode> to handle it.",
426 sha1_to_hex(tag->object.sha1));
428 warning ("Exporting signed tag %s",
429 sha1_to_hex(tag->object.sha1));
434 warning ("Stripping signature from tag %s",
435 sha1_to_hex(tag->object.sha1));
438 message_size = signature + 1 - message;
443 /* handle tag->tagged having been filtered out due to paths specified */
444 tagged = tag->tagged;
445 tagged_mark = get_object_mark(tagged);
447 switch(tag_of_filtered_mode) {
449 die ("Tag %s tags unexported object; use "
450 "--tag-of-filtered-object=<mode> to handle it.",
451 sha1_to_hex(tag->object.sha1));
453 /* Ignore this tag altogether */
456 if (tagged->type != OBJ_COMMIT) {
457 die ("Tag %s tags unexported %s!",
458 sha1_to_hex(tag->object.sha1),
459 typename(tagged->type));
461 p = (struct commit *)tagged;
463 if (p->parents && p->parents->next)
465 if (p->object.flags & UNINTERESTING)
467 if (!(p->object.flags & TREESAME))
470 die ("Can't find replacement commit for tag %s\n",
471 sha1_to_hex(tag->object.sha1));
472 p = p->parents->item;
474 tagged_mark = get_object_mark(&p->object);
478 if (!prefixcmp(name, "refs/tags/"))
480 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
482 (int)(tagger_end - tagger), tagger,
483 tagger == tagger_end ? "" : "\n",
484 (int)message_size, (int)message_size, message ? message : "");
487 static void get_tags_and_duplicates(struct rev_cmdline_info *info,
488 struct string_list *extra_refs)
493 for (i = 0; i < info->nr; i++) {
494 struct rev_cmdline_entry *e = info->rev + i;
495 unsigned char sha1[20];
496 struct commit *commit;
499 if (e->flags & UNINTERESTING)
502 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
505 switch (e->item->type) {
507 commit = (struct commit *)e->item;
510 tag = (struct tag *)e->item;
512 /* handle nested tags */
513 while (tag && tag->object.type == OBJ_TAG) {
514 parse_object(tag->object.sha1);
515 string_list_append(extra_refs, full_name)->util = tag;
516 tag = (struct tag *)tag->tagged;
519 die ("Tag %s points nowhere?", e->name);
520 switch(tag->object.type) {
522 commit = (struct commit *)tag;
525 export_blob(tag->object.sha1);
527 default: /* OBJ_TAG (nested tags) is already handled */
528 warning("Tag points to object of unexpected type %s, skipping.",
529 typename(tag->object.type));
534 warning("%s: Unexpected object of type %s, skipping.",
536 typename(e->item->type));
541 * This ref will not be updated through a commit, lets make
542 * sure it gets properly updated eventually.
544 if (commit->util || commit->object.flags & SHOWN)
545 string_list_append(extra_refs, full_name)->util = commit;
547 commit->util = full_name;
551 static void handle_tags_and_duplicates(struct string_list *extra_refs)
553 struct commit *commit;
556 for (i = extra_refs->nr - 1; i >= 0; i--) {
557 const char *name = extra_refs->items[i].string;
558 struct object *object = extra_refs->items[i].util;
559 switch (object->type) {
561 handle_tag(name, (struct tag *)object);
564 /* create refs pointing to already seen commits */
565 commit = (struct commit *)object;
566 printf("reset %s\nfrom :%d\n\n", name,
567 get_object_mark(&commit->object));
574 static void export_marks(char *file)
578 struct object_decoration *deco = idnums.hash;
582 f = fopen(file, "w");
584 die_errno("Unable to open marks file %s for writing.", file);
586 for (i = 0; i < idnums.size; i++) {
587 if (deco->base && deco->base->type == 1) {
588 mark = ptr_to_mark(deco->decoration);
589 if (fprintf(f, ":%"PRIu32" %s\n", mark,
590 sha1_to_hex(deco->base->sha1)) < 0) {
601 error("Unable to write marks file %s.", file);
604 static void import_marks(char *input_file)
607 FILE *f = fopen(input_file, "r");
609 die_errno("cannot read '%s'", input_file);
611 while (fgets(line, sizeof(line), f)) {
613 char *line_end, *mark_end;
614 unsigned char sha1[20];
615 struct object *object;
616 struct commit *commit;
617 enum object_type type;
619 line_end = strchr(line, '\n');
620 if (line[0] != ':' || !line_end)
621 die("corrupt mark line: %s", line);
624 mark = strtoumax(line + 1, &mark_end, 10);
625 if (!mark || mark_end == line + 1
626 || *mark_end != ' ' || get_sha1_hex(mark_end + 1, sha1))
627 die("corrupt mark line: %s", line);
629 if (last_idnum < mark)
632 type = sha1_object_info(sha1, NULL);
634 die("object not found: %s", sha1_to_hex(sha1));
636 if (type != OBJ_COMMIT)
640 commit = lookup_commit(sha1);
642 die("not a commit? can't happen: %s", sha1_to_hex(sha1));
644 object = &commit->object;
646 if (object->flags & SHOWN)
647 error("Object %s already has a mark", sha1_to_hex(sha1));
649 mark_object(object, mark);
651 object->flags |= SHOWN;
656 int cmd_fast_export(int argc, const char **argv, const char *prefix)
658 struct rev_info revs;
659 struct object_array commits = OBJECT_ARRAY_INIT;
660 struct string_list extra_refs = STRING_LIST_INIT_NODUP;
661 struct commit *commit;
662 char *export_filename = NULL, *import_filename = NULL;
663 uint32_t lastimportid;
664 struct option options[] = {
665 OPT_INTEGER(0, "progress", &progress,
666 N_("show progress after <n> objects")),
667 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
668 N_("select handling of signed tags"),
669 parse_opt_signed_tag_mode),
670 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
671 N_("select handling of tags that tag filtered objects"),
672 parse_opt_tag_of_filtered_mode),
673 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
674 N_("Dump marks to this file")),
675 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
676 N_("Import marks from this file")),
677 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
678 N_("Fake a tagger when tags lack one")),
679 OPT_BOOLEAN(0, "full-tree", &full_tree,
680 N_("Output full tree for each commit")),
681 OPT_BOOLEAN(0, "use-done-feature", &use_done_feature,
682 N_("Use the done feature to terminate the stream")),
683 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
688 usage_with_options (fast_export_usage, options);
690 /* we handle encodings */
691 git_config(git_default_config, NULL);
693 init_revisions(&revs, prefix);
695 revs.show_source = 1;
696 revs.rewrite_parents = 1;
697 argc = setup_revisions(argc, argv, &revs, NULL);
698 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
700 usage_with_options (fast_export_usage, options);
702 if (use_done_feature)
703 printf("feature done\n");
706 import_marks(import_filename);
707 lastimportid = last_idnum;
709 if (import_filename && revs.prune_data.nr)
712 get_tags_and_duplicates(&revs.cmdline, &extra_refs);
714 if (prepare_revision_walk(&revs))
715 die("revision walk setup failed");
716 revs.diffopt.format_callback = show_filemodify;
717 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
718 while ((commit = get_revision(&revs))) {
719 if (has_unshown_parent(commit)) {
720 add_object_array(&commit->object, NULL, &commits);
723 handle_commit(commit, &revs);
724 handle_tail(&commits, &revs);
728 handle_tags_and_duplicates(&extra_refs);
730 if (export_filename && lastimportid != last_idnum)
731 export_marks(export_filename);
733 if (use_done_feature)