test: fix t7001 cp to use POSIX options
[git.git] / builtin / fast-export.c
blob78250eab08abf692344277e01cb94fc5f5c2f807
1 /*
2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
5 */
6 #include "builtin.h"
7 #include "cache.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "tag.h"
11 #include "diff.h"
12 #include "diffcore.h"
13 #include "log-tree.h"
14 #include "revision.h"
15 #include "decorate.h"
16 #include "string-list.h"
17 #include "utf8.h"
18 #include "parse-options.h"
19 #include "quote.h"
21 static const char *fast_export_usage[] = {
22 N_("git fast-export [rev-list-opts]"),
23 NULL
26 static int progress;
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;
31 static int no_data;
32 static int full_tree;
33 static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
35 static int parse_opt_signed_tag_mode(const struct option *opt,
36 const char *arg, int unset)
38 if (unset || !strcmp(arg, "abort"))
39 signed_tag_mode = ABORT;
40 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
41 signed_tag_mode = VERBATIM;
42 else if (!strcmp(arg, "warn"))
43 signed_tag_mode = WARN;
44 else if (!strcmp(arg, "warn-strip"))
45 signed_tag_mode = WARN_STRIP;
46 else if (!strcmp(arg, "strip"))
47 signed_tag_mode = STRIP;
48 else
49 return error("Unknown signed-tags mode: %s", arg);
50 return 0;
53 static int parse_opt_tag_of_filtered_mode(const struct option *opt,
54 const char *arg, int unset)
56 if (unset || !strcmp(arg, "abort"))
57 tag_of_filtered_mode = ERROR;
58 else if (!strcmp(arg, "drop"))
59 tag_of_filtered_mode = DROP;
60 else if (!strcmp(arg, "rewrite"))
61 tag_of_filtered_mode = REWRITE;
62 else
63 return error("Unknown tag-of-filtered mode: %s", arg);
64 return 0;
67 static struct decoration idnums;
68 static uint32_t last_idnum;
70 static int has_unshown_parent(struct commit *commit)
72 struct commit_list *parent;
74 for (parent = commit->parents; parent; parent = parent->next)
75 if (!(parent->item->object.flags & SHOWN) &&
76 !(parent->item->object.flags & UNINTERESTING))
77 return 1;
78 return 0;
81 /* Since intptr_t is C99, we do not use it here */
82 static inline uint32_t *mark_to_ptr(uint32_t mark)
84 return ((uint32_t *)NULL) + mark;
87 static inline uint32_t ptr_to_mark(void * mark)
89 return (uint32_t *)mark - (uint32_t *)NULL;
92 static inline void mark_object(struct object *object, uint32_t mark)
94 add_decoration(&idnums, object, mark_to_ptr(mark));
97 static inline void mark_next_object(struct object *object)
99 mark_object(object, ++last_idnum);
102 static int get_object_mark(struct object *object)
104 void *decoration = lookup_decoration(&idnums, object);
105 if (!decoration)
106 return 0;
107 return ptr_to_mark(decoration);
110 static void show_progress(void)
112 static int counter = 0;
113 if (!progress)
114 return;
115 if ((++counter % progress) == 0)
116 printf("progress %d objects\n", counter);
119 static void export_blob(const unsigned char *sha1)
121 unsigned long size;
122 enum object_type type;
123 char *buf;
124 struct object *object;
125 int eaten;
127 if (no_data)
128 return;
130 if (is_null_sha1(sha1))
131 return;
133 object = lookup_object(sha1);
134 if (object && object->flags & SHOWN)
135 return;
137 buf = read_sha1_file(sha1, &type, &size);
138 if (!buf)
139 die ("Could not read blob %s", sha1_to_hex(sha1));
140 if (check_sha1_signature(sha1, buf, size, typename(type)) < 0)
141 die("sha1 mismatch in blob %s", sha1_to_hex(sha1));
142 object = parse_object_buffer(sha1, type, size, buf, &eaten);
143 if (!object)
144 die("Could not read blob %s", sha1_to_hex(sha1));
146 mark_next_object(object);
148 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
149 if (size && fwrite(buf, size, 1, stdout) != 1)
150 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1));
151 printf("\n");
153 show_progress();
155 object->flags |= SHOWN;
156 if (!eaten)
157 free(buf);
160 static int depth_first(const void *a_, const void *b_)
162 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
163 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
164 const char *name_a, *name_b;
165 int len_a, len_b, len;
166 int cmp;
168 name_a = a->one ? a->one->path : a->two->path;
169 name_b = b->one ? b->one->path : b->two->path;
171 len_a = strlen(name_a);
172 len_b = strlen(name_b);
173 len = (len_a < len_b) ? len_a : len_b;
175 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
176 cmp = memcmp(name_a, name_b, len);
177 if (cmp)
178 return cmp;
179 cmp = len_b - len_a;
180 if (cmp)
181 return cmp;
183 * Move 'R'ename entries last so that all references of the file
184 * appear in the output before it is renamed (e.g., when a file
185 * was copied and renamed in the same commit).
187 return (a->status == 'R') - (b->status == 'R');
190 static void print_path(const char *path)
192 int need_quote = quote_c_style(path, NULL, NULL, 0);
193 if (need_quote)
194 quote_c_style(path, NULL, stdout, 0);
195 else if (strchr(path, ' '))
196 printf("\"%s\"", path);
197 else
198 printf("%s", path);
201 static void show_filemodify(struct diff_queue_struct *q,
202 struct diff_options *options, void *data)
204 int i;
207 * Handle files below a directory first, in case they are all deleted
208 * and the directory changes to a file or symlink.
210 qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
212 for (i = 0; i < q->nr; i++) {
213 struct diff_filespec *ospec = q->queue[i]->one;
214 struct diff_filespec *spec = q->queue[i]->two;
216 switch (q->queue[i]->status) {
217 case DIFF_STATUS_DELETED:
218 printf("D ");
219 print_path(spec->path);
220 putchar('\n');
221 break;
223 case DIFF_STATUS_COPIED:
224 case DIFF_STATUS_RENAMED:
225 printf("%c ", q->queue[i]->status);
226 print_path(ospec->path);
227 putchar(' ');
228 print_path(spec->path);
229 putchar('\n');
231 if (!hashcmp(ospec->sha1, spec->sha1) &&
232 ospec->mode == spec->mode)
233 break;
234 /* fallthrough */
236 case DIFF_STATUS_TYPE_CHANGED:
237 case DIFF_STATUS_MODIFIED:
238 case DIFF_STATUS_ADDED:
240 * Links refer to objects in another repositories;
241 * output the SHA-1 verbatim.
243 if (no_data || S_ISGITLINK(spec->mode))
244 printf("M %06o %s ", spec->mode,
245 sha1_to_hex(spec->sha1));
246 else {
247 struct object *object = lookup_object(spec->sha1);
248 printf("M %06o :%d ", spec->mode,
249 get_object_mark(object));
251 print_path(spec->path);
252 putchar('\n');
253 break;
255 default:
256 die("Unexpected comparison status '%c' for %s, %s",
257 q->queue[i]->status,
258 ospec->path ? ospec->path : "none",
259 spec->path ? spec->path : "none");
264 static const char *find_encoding(const char *begin, const char *end)
266 const char *needle = "\nencoding ";
267 char *bol, *eol;
269 bol = memmem(begin, end ? end - begin : strlen(begin),
270 needle, strlen(needle));
271 if (!bol)
272 return git_commit_encoding;
273 bol += strlen(needle);
274 eol = strchrnul(bol, '\n');
275 *eol = '\0';
276 return bol;
279 static void handle_commit(struct commit *commit, struct rev_info *rev)
281 int saved_output_format = rev->diffopt.output_format;
282 const char *author, *author_end, *committer, *committer_end;
283 const char *encoding, *message;
284 char *reencoded = NULL;
285 struct commit_list *p;
286 int i;
288 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
290 parse_commit(commit);
291 author = strstr(commit->buffer, "\nauthor ");
292 if (!author)
293 die ("Could not find author in commit %s",
294 sha1_to_hex(commit->object.sha1));
295 author++;
296 author_end = strchrnul(author, '\n');
297 committer = strstr(author_end, "\ncommitter ");
298 if (!committer)
299 die ("Could not find committer in commit %s",
300 sha1_to_hex(commit->object.sha1));
301 committer++;
302 committer_end = strchrnul(committer, '\n');
303 message = strstr(committer_end, "\n\n");
304 encoding = find_encoding(committer_end, message);
305 if (message)
306 message += 2;
308 if (commit->parents &&
309 get_object_mark(&commit->parents->item->object) != 0 &&
310 !full_tree) {
311 parse_commit(commit->parents->item);
312 diff_tree_sha1(commit->parents->item->tree->object.sha1,
313 commit->tree->object.sha1, "", &rev->diffopt);
315 else
316 diff_root_tree_sha1(commit->tree->object.sha1,
317 "", &rev->diffopt);
319 /* Export the referenced blobs, and remember the marks. */
320 for (i = 0; i < diff_queued_diff.nr; i++)
321 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
322 export_blob(diff_queued_diff.queue[i]->two->sha1);
324 mark_next_object(&commit->object);
325 if (!is_encoding_utf8(encoding))
326 reencoded = reencode_string(message, "UTF-8", encoding);
327 if (!commit->parents)
328 printf("reset %s\n", (const char*)commit->util);
329 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
330 (const char *)commit->util, last_idnum,
331 (int)(author_end - author), author,
332 (int)(committer_end - committer), committer,
333 (unsigned)(reencoded
334 ? strlen(reencoded) : message
335 ? strlen(message) : 0),
336 reencoded ? reencoded : message ? message : "");
337 free(reencoded);
339 for (i = 0, p = commit->parents; p; p = p->next) {
340 int mark = get_object_mark(&p->item->object);
341 if (!mark)
342 continue;
343 if (i == 0)
344 printf("from :%d\n", mark);
345 else
346 printf("merge :%d\n", mark);
347 i++;
350 if (full_tree)
351 printf("deleteall\n");
352 log_tree_diff_flush(rev);
353 rev->diffopt.output_format = saved_output_format;
355 printf("\n");
357 show_progress();
360 static void handle_tail(struct object_array *commits, struct rev_info *revs)
362 struct commit *commit;
363 while (commits->nr) {
364 commit = (struct commit *)commits->objects[commits->nr - 1].item;
365 if (has_unshown_parent(commit))
366 return;
367 handle_commit(commit, revs);
368 commits->nr--;
372 static void handle_tag(const char *name, struct tag *tag)
374 unsigned long size;
375 enum object_type type;
376 char *buf;
377 const char *tagger, *tagger_end, *message;
378 size_t message_size = 0;
379 struct object *tagged;
380 int tagged_mark;
381 struct commit *p;
383 /* Trees have no identifier in fast-export output, thus we have no way
384 * to output tags of trees, tags of tags of trees, etc. Simply omit
385 * such tags.
387 tagged = tag->tagged;
388 while (tagged->type == OBJ_TAG) {
389 tagged = ((struct tag *)tagged)->tagged;
391 if (tagged->type == OBJ_TREE) {
392 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
393 sha1_to_hex(tag->object.sha1));
394 return;
397 buf = read_sha1_file(tag->object.sha1, &type, &size);
398 if (!buf)
399 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
400 message = memmem(buf, size, "\n\n", 2);
401 if (message) {
402 message += 2;
403 message_size = strlen(message);
405 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
406 if (!tagger) {
407 if (fake_missing_tagger)
408 tagger = "tagger Unspecified Tagger "
409 "<unspecified-tagger> 0 +0000";
410 else
411 tagger = "";
412 tagger_end = tagger + strlen(tagger);
413 } else {
414 tagger++;
415 tagger_end = strchrnul(tagger, '\n');
418 /* handle signed tags */
419 if (message) {
420 const char *signature = strstr(message,
421 "\n-----BEGIN PGP SIGNATURE-----\n");
422 if (signature)
423 switch(signed_tag_mode) {
424 case ABORT:
425 die ("Encountered signed tag %s; use "
426 "--signed-tags=<mode> to handle it.",
427 sha1_to_hex(tag->object.sha1));
428 case WARN:
429 warning ("Exporting signed tag %s",
430 sha1_to_hex(tag->object.sha1));
431 /* fallthru */
432 case VERBATIM:
433 break;
434 case WARN_STRIP:
435 warning ("Stripping signature from tag %s",
436 sha1_to_hex(tag->object.sha1));
437 /* fallthru */
438 case STRIP:
439 message_size = signature + 1 - message;
440 break;
444 /* handle tag->tagged having been filtered out due to paths specified */
445 tagged = tag->tagged;
446 tagged_mark = get_object_mark(tagged);
447 if (!tagged_mark) {
448 switch(tag_of_filtered_mode) {
449 case ABORT:
450 die ("Tag %s tags unexported object; use "
451 "--tag-of-filtered-object=<mode> to handle it.",
452 sha1_to_hex(tag->object.sha1));
453 case DROP:
454 /* Ignore this tag altogether */
455 return;
456 case REWRITE:
457 if (tagged->type != OBJ_COMMIT) {
458 die ("Tag %s tags unexported %s!",
459 sha1_to_hex(tag->object.sha1),
460 typename(tagged->type));
462 p = (struct commit *)tagged;
463 for (;;) {
464 if (p->parents && p->parents->next)
465 break;
466 if (p->object.flags & UNINTERESTING)
467 break;
468 if (!(p->object.flags & TREESAME))
469 break;
470 if (!p->parents)
471 die ("Can't find replacement commit for tag %s\n",
472 sha1_to_hex(tag->object.sha1));
473 p = p->parents->item;
475 tagged_mark = get_object_mark(&p->object);
479 if (!prefixcmp(name, "refs/tags/"))
480 name += 10;
481 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
482 name, tagged_mark,
483 (int)(tagger_end - tagger), tagger,
484 tagger == tagger_end ? "" : "\n",
485 (int)message_size, (int)message_size, message ? message : "");
488 static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
490 switch (e->item->type) {
491 case OBJ_COMMIT:
492 return (struct commit *)e->item;
493 case OBJ_TAG: {
494 struct tag *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;
502 if (!tag)
503 die("Tag %s points nowhere?", e->name);
504 return (struct commit *)tag;
505 break;
507 default:
508 return NULL;
512 static void get_tags_and_duplicates(struct rev_cmdline_info *info)
514 int i;
516 for (i = 0; i < info->nr; i++) {
517 struct rev_cmdline_entry *e = info->rev + i;
518 unsigned char sha1[20];
519 struct commit *commit;
520 char *full_name;
522 if (e->flags & UNINTERESTING)
523 continue;
525 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
526 continue;
528 commit = get_commit(e, full_name);
529 if (!commit) {
530 warning("%s: Unexpected object of type %s, skipping.",
531 e->name,
532 typename(e->item->type));
533 continue;
536 switch(commit->object.type) {
537 case OBJ_COMMIT:
538 break;
539 case OBJ_BLOB:
540 export_blob(commit->object.sha1);
541 continue;
542 default: /* OBJ_TAG (nested tags) is already handled */
543 warning("Tag points to object of unexpected type %s, skipping.",
544 typename(commit->object.type));
545 continue;
549 * This ref will not be updated through a commit, lets make
550 * sure it gets properly updated eventually.
552 if (commit->util || commit->object.flags & SHOWN)
553 string_list_append(&extra_refs, full_name)->util = commit;
554 if (!commit->util)
555 commit->util = full_name;
559 static void handle_tags_and_duplicates(void)
561 struct commit *commit;
562 int i;
564 for (i = extra_refs.nr - 1; i >= 0; i--) {
565 const char *name = extra_refs.items[i].string;
566 struct object *object = extra_refs.items[i].util;
567 switch (object->type) {
568 case OBJ_TAG:
569 handle_tag(name, (struct tag *)object);
570 break;
571 case OBJ_COMMIT:
572 /* create refs pointing to already seen commits */
573 commit = (struct commit *)object;
574 printf("reset %s\nfrom :%d\n\n", name,
575 get_object_mark(&commit->object));
576 show_progress();
577 break;
582 static void export_marks(char *file)
584 unsigned int i;
585 uint32_t mark;
586 struct object_decoration *deco = idnums.hash;
587 FILE *f;
588 int e = 0;
590 f = fopen(file, "w");
591 if (!f)
592 die_errno("Unable to open marks file %s for writing.", file);
594 for (i = 0; i < idnums.size; i++) {
595 if (deco->base && deco->base->type == 1) {
596 mark = ptr_to_mark(deco->decoration);
597 if (fprintf(f, ":%"PRIu32" %s\n", mark,
598 sha1_to_hex(deco->base->sha1)) < 0) {
599 e = 1;
600 break;
603 deco++;
606 e |= ferror(f);
607 e |= fclose(f);
608 if (e)
609 error("Unable to write marks file %s.", file);
612 static void import_marks(char *input_file)
614 char line[512];
615 FILE *f = fopen(input_file, "r");
616 if (!f)
617 die_errno("cannot read '%s'", input_file);
619 while (fgets(line, sizeof(line), f)) {
620 uint32_t mark;
621 char *line_end, *mark_end;
622 unsigned char sha1[20];
623 struct object *object;
624 struct commit *commit;
625 enum object_type type;
627 line_end = strchr(line, '\n');
628 if (line[0] != ':' || !line_end)
629 die("corrupt mark line: %s", line);
630 *line_end = '\0';
632 mark = strtoumax(line + 1, &mark_end, 10);
633 if (!mark || mark_end == line + 1
634 || *mark_end != ' ' || get_sha1_hex(mark_end + 1, sha1))
635 die("corrupt mark line: %s", line);
637 if (last_idnum < mark)
638 last_idnum = mark;
640 type = sha1_object_info(sha1, NULL);
641 if (type < 0)
642 die("object not found: %s", sha1_to_hex(sha1));
644 if (type != OBJ_COMMIT)
645 /* only commits */
646 continue;
648 commit = lookup_commit(sha1);
649 if (!commit)
650 die("not a commit? can't happen: %s", sha1_to_hex(sha1));
652 object = &commit->object;
654 if (object->flags & SHOWN)
655 error("Object %s already has a mark", sha1_to_hex(sha1));
657 mark_object(object, mark);
659 object->flags |= SHOWN;
661 fclose(f);
664 int cmd_fast_export(int argc, const char **argv, const char *prefix)
666 struct rev_info revs;
667 struct object_array commits = OBJECT_ARRAY_INIT;
668 struct commit *commit;
669 char *export_filename = NULL, *import_filename = NULL;
670 uint32_t lastimportid;
671 struct option options[] = {
672 OPT_INTEGER(0, "progress", &progress,
673 N_("show progress after <n> objects")),
674 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
675 N_("select handling of signed tags"),
676 parse_opt_signed_tag_mode),
677 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
678 N_("select handling of tags that tag filtered objects"),
679 parse_opt_tag_of_filtered_mode),
680 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
681 N_("Dump marks to this file")),
682 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
683 N_("Import marks from this file")),
684 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
685 N_("Fake a tagger when tags lack one")),
686 OPT_BOOL(0, "full-tree", &full_tree,
687 N_("Output full tree for each commit")),
688 OPT_BOOL(0, "use-done-feature", &use_done_feature,
689 N_("Use the done feature to terminate the stream")),
690 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
691 OPT_END()
694 if (argc == 1)
695 usage_with_options (fast_export_usage, options);
697 /* we handle encodings */
698 git_config(git_default_config, NULL);
700 init_revisions(&revs, prefix);
701 revs.topo_order = 1;
702 revs.show_source = 1;
703 revs.rewrite_parents = 1;
704 argc = setup_revisions(argc, argv, &revs, NULL);
705 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
706 if (argc > 1)
707 usage_with_options (fast_export_usage, options);
709 if (use_done_feature)
710 printf("feature done\n");
712 if (import_filename)
713 import_marks(import_filename);
714 lastimportid = last_idnum;
716 if (import_filename && revs.prune_data.nr)
717 full_tree = 1;
719 get_tags_and_duplicates(&revs.cmdline);
721 if (prepare_revision_walk(&revs))
722 die("revision walk setup failed");
723 revs.diffopt.format_callback = show_filemodify;
724 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
725 while ((commit = get_revision(&revs))) {
726 if (has_unshown_parent(commit)) {
727 add_object_array(&commit->object, NULL, &commits);
729 else {
730 handle_commit(commit, &revs);
731 handle_tail(&commits, &revs);
735 handle_tags_and_duplicates();
737 if (export_filename && lastimportid != last_idnum)
738 export_marks(export_filename);
740 if (use_done_feature)
741 printf("done\n");
743 return 0;