French translation: copy -> copie.
[git/dscho.git] / builtin / fast-export.c
blob9914d5efc460c12a57295f0903c4b8c5a38d12f5
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 #define REF_HANDLED (ALL_REV_FLAGS + 1)
23 static const char *fast_export_usage[] = {
24 N_("git fast-export [rev-list-opts]"),
25 NULL
28 static int progress;
29 static enum { ABORT, VERBATIM, WARN, STRIP } signed_tag_mode = ABORT;
30 static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR;
31 static int fake_missing_tagger;
32 static int use_done_feature;
33 static int no_data;
34 static int full_tree;
36 static int parse_opt_signed_tag_mode(const struct option *opt,
37 const char *arg, int unset)
39 if (unset || !strcmp(arg, "abort"))
40 signed_tag_mode = ABORT;
41 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
42 signed_tag_mode = VERBATIM;
43 else if (!strcmp(arg, "warn"))
44 signed_tag_mode = WARN;
45 else if (!strcmp(arg, "strip"))
46 signed_tag_mode = STRIP;
47 else
48 return error("Unknown signed-tag mode: %s", arg);
49 return 0;
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;
61 else
62 return error("Unknown tag-of-filtered mode: %s", arg);
63 return 0;
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))
76 return 1;
77 return 0;
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);
104 if (!decoration)
105 return 0;
106 return ptr_to_mark(decoration);
109 static void show_progress(void)
111 static int counter = 0;
112 if (!progress)
113 return;
114 if ((++counter % progress) == 0)
115 printf("progress %d objects\n", counter);
118 static void handle_object(const unsigned char *sha1)
120 unsigned long size;
121 enum object_type type;
122 char *buf;
123 struct object *object;
125 if (no_data)
126 return;
128 if (is_null_sha1(sha1))
129 return;
131 object = parse_object(sha1);
132 if (!object)
133 die ("Could not read blob %s", sha1_to_hex(sha1));
135 if (object->flags & SHOWN)
136 return;
138 buf = read_sha1_file(sha1, &type, &size);
139 if (!buf)
140 die ("Could not read blob %s", sha1_to_hex(sha1));
142 mark_next_object(object);
144 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
145 if (size && fwrite(buf, size, 1, stdout) != 1)
146 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1));
147 printf("\n");
149 show_progress();
151 object->flags |= SHOWN;
152 free(buf);
155 static int depth_first(const void *a_, const void *b_)
157 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
158 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
159 const char *name_a, *name_b;
160 int len_a, len_b, len;
161 int cmp;
163 name_a = a->one ? a->one->path : a->two->path;
164 name_b = b->one ? b->one->path : b->two->path;
166 len_a = strlen(name_a);
167 len_b = strlen(name_b);
168 len = (len_a < len_b) ? len_a : len_b;
170 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
171 cmp = memcmp(name_a, name_b, len);
172 if (cmp)
173 return cmp;
174 cmp = len_b - len_a;
175 if (cmp)
176 return cmp;
178 * Move 'R'ename entries last so that all references of the file
179 * appear in the output before it is renamed (e.g., when a file
180 * was copied and renamed in the same commit).
182 return (a->status == 'R') - (b->status == 'R');
185 static void print_path(const char *path)
187 int need_quote = quote_c_style(path, NULL, NULL, 0);
188 if (need_quote)
189 quote_c_style(path, NULL, stdout, 0);
190 else if (strchr(path, ' '))
191 printf("\"%s\"", path);
192 else
193 printf("%s", path);
196 static void show_filemodify(struct diff_queue_struct *q,
197 struct diff_options *options, void *data)
199 int i;
202 * Handle files below a directory first, in case they are all deleted
203 * and the directory changes to a file or symlink.
205 qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
207 for (i = 0; i < q->nr; i++) {
208 struct diff_filespec *ospec = q->queue[i]->one;
209 struct diff_filespec *spec = q->queue[i]->two;
211 switch (q->queue[i]->status) {
212 case DIFF_STATUS_DELETED:
213 printf("D ");
214 print_path(spec->path);
215 putchar('\n');
216 break;
218 case DIFF_STATUS_COPIED:
219 case DIFF_STATUS_RENAMED:
220 printf("%c ", q->queue[i]->status);
221 print_path(ospec->path);
222 putchar(' ');
223 print_path(spec->path);
224 putchar('\n');
226 if (!hashcmp(ospec->sha1, spec->sha1) &&
227 ospec->mode == spec->mode)
228 break;
229 /* fallthrough */
231 case DIFF_STATUS_TYPE_CHANGED:
232 case DIFF_STATUS_MODIFIED:
233 case DIFF_STATUS_ADDED:
235 * Links refer to objects in another repositories;
236 * output the SHA-1 verbatim.
238 if (no_data || S_ISGITLINK(spec->mode))
239 printf("M %06o %s ", spec->mode,
240 sha1_to_hex(spec->sha1));
241 else {
242 struct object *object = lookup_object(spec->sha1);
243 printf("M %06o :%d ", spec->mode,
244 get_object_mark(object));
246 print_path(spec->path);
247 putchar('\n');
248 break;
250 default:
251 die("Unexpected comparison status '%c' for %s, %s",
252 q->queue[i]->status,
253 ospec->path ? ospec->path : "none",
254 spec->path ? spec->path : "none");
259 static const char *find_encoding(const char *begin, const char *end)
261 const char *needle = "\nencoding ";
262 char *bol, *eol;
264 bol = memmem(begin, end ? end - begin : strlen(begin),
265 needle, strlen(needle));
266 if (!bol)
267 return git_commit_encoding;
268 bol += strlen(needle);
269 eol = strchrnul(bol, '\n');
270 *eol = '\0';
271 return bol;
274 static void handle_commit(struct commit *commit, struct rev_info *rev)
276 int saved_output_format = rev->diffopt.output_format;
277 const char *author, *author_end, *committer, *committer_end;
278 const char *encoding, *message;
279 char *reencoded = NULL;
280 struct commit_list *p;
281 int i;
283 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
285 parse_commit(commit);
286 author = strstr(commit->buffer, "\nauthor ");
287 if (!author)
288 die ("Could not find author in commit %s",
289 sha1_to_hex(commit->object.sha1));
290 author++;
291 author_end = strchrnul(author, '\n');
292 committer = strstr(author_end, "\ncommitter ");
293 if (!committer)
294 die ("Could not find committer in commit %s",
295 sha1_to_hex(commit->object.sha1));
296 committer++;
297 committer_end = strchrnul(committer, '\n');
298 message = strstr(committer_end, "\n\n");
299 encoding = find_encoding(committer_end, message);
300 if (message)
301 message += 2;
303 if (commit->parents &&
304 get_object_mark(&commit->parents->item->object) != 0 &&
305 !full_tree) {
306 parse_commit(commit->parents->item);
307 diff_tree_sha1(commit->parents->item->tree->object.sha1,
308 commit->tree->object.sha1, "", &rev->diffopt);
310 else
311 diff_root_tree_sha1(commit->tree->object.sha1,
312 "", &rev->diffopt);
314 /* Export the referenced blobs, and remember the marks. */
315 for (i = 0; i < diff_queued_diff.nr; i++)
316 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
317 handle_object(diff_queued_diff.queue[i]->two->sha1);
319 mark_next_object(&commit->object);
320 if (!is_encoding_utf8(encoding))
321 reencoded = reencode_string(message, "UTF-8", encoding);
322 if (!commit->parents)
323 printf("reset %s\n", (const char*)commit->util);
324 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
325 (const char *)commit->util, last_idnum,
326 (int)(author_end - author), author,
327 (int)(committer_end - committer), committer,
328 (unsigned)(reencoded
329 ? strlen(reencoded) : message
330 ? strlen(message) : 0),
331 reencoded ? reencoded : message ? message : "");
332 free(reencoded);
334 for (i = 0, p = commit->parents; p; p = p->next) {
335 int mark = get_object_mark(&p->item->object);
336 if (!mark)
337 continue;
338 if (i == 0)
339 printf("from :%d\n", mark);
340 else
341 printf("merge :%d\n", mark);
342 i++;
345 if (full_tree)
346 printf("deleteall\n");
347 log_tree_diff_flush(rev);
348 rev->diffopt.output_format = saved_output_format;
350 printf("\n");
352 show_progress();
355 static void handle_tail(struct object_array *commits, struct rev_info *revs)
357 struct commit *commit;
358 while (commits->nr) {
359 commit = (struct commit *)commits->objects[commits->nr - 1].item;
360 if (has_unshown_parent(commit))
361 return;
362 handle_commit(commit, revs);
363 commits->nr--;
367 static void handle_tag(const char *name, struct tag *tag)
369 unsigned long size;
370 enum object_type type;
371 char *buf;
372 const char *tagger, *tagger_end, *message;
373 size_t message_size = 0;
374 struct object *tagged;
375 int tagged_mark;
376 struct commit *p;
378 /* Trees have no identifer in fast-export output, thus we have no way
379 * to output tags of trees, tags of tags of trees, etc. Simply omit
380 * such tags.
382 tagged = tag->tagged;
383 while (tagged->type == OBJ_TAG) {
384 tagged = ((struct tag *)tagged)->tagged;
386 if (tagged->type == OBJ_TREE) {
387 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
388 sha1_to_hex(tag->object.sha1));
389 return;
392 buf = read_sha1_file(tag->object.sha1, &type, &size);
393 if (!buf)
394 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
395 message = memmem(buf, size, "\n\n", 2);
396 if (message) {
397 message += 2;
398 message_size = strlen(message);
400 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
401 if (!tagger) {
402 if (fake_missing_tagger)
403 tagger = "tagger Unspecified Tagger "
404 "<unspecified-tagger> 0 +0000";
405 else
406 tagger = "";
407 tagger_end = tagger + strlen(tagger);
408 } else {
409 tagger++;
410 tagger_end = strchrnul(tagger, '\n');
413 /* handle signed tags */
414 if (message) {
415 const char *signature = strstr(message,
416 "\n-----BEGIN PGP SIGNATURE-----\n");
417 if (signature)
418 switch(signed_tag_mode) {
419 case ABORT:
420 die ("Encountered signed tag %s; use "
421 "--signed-tag=<mode> to handle it.",
422 sha1_to_hex(tag->object.sha1));
423 case WARN:
424 warning ("Exporting signed tag %s",
425 sha1_to_hex(tag->object.sha1));
426 /* fallthru */
427 case VERBATIM:
428 break;
429 case STRIP:
430 message_size = signature + 1 - message;
431 break;
435 /* handle tag->tagged having been filtered out due to paths specified */
436 tagged = tag->tagged;
437 tagged_mark = get_object_mark(tagged);
438 if (!tagged_mark) {
439 switch(tag_of_filtered_mode) {
440 case ABORT:
441 die ("Tag %s tags unexported object; use "
442 "--tag-of-filtered-object=<mode> to handle it.",
443 sha1_to_hex(tag->object.sha1));
444 case DROP:
445 /* Ignore this tag altogether */
446 return;
447 case REWRITE:
448 if (tagged->type != OBJ_COMMIT) {
449 die ("Tag %s tags unexported %s!",
450 sha1_to_hex(tag->object.sha1),
451 typename(tagged->type));
453 p = (struct commit *)tagged;
454 for (;;) {
455 if (p->parents && p->parents->next)
456 break;
457 if (p->object.flags & UNINTERESTING)
458 break;
459 if (!(p->object.flags & TREESAME))
460 break;
461 if (!p->parents)
462 die ("Can't find replacement commit for tag %s\n",
463 sha1_to_hex(tag->object.sha1));
464 p = p->parents->item;
466 tagged_mark = get_object_mark(&p->object);
470 if (!prefixcmp(name, "refs/tags/"))
471 name += 10;
472 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
473 name, tagged_mark,
474 (int)(tagger_end - tagger), tagger,
475 tagger == tagger_end ? "" : "\n",
476 (int)message_size, (int)message_size, message ? message : "");
479 static void get_tags_and_duplicates(struct object_array *pending,
480 struct string_list *refs,
481 struct string_list *extra_refs)
483 struct tag *tag;
484 int i;
486 for (i = 0; i < pending->nr; i++) {
487 struct object_array_entry *e = pending->objects + i;
488 unsigned char sha1[20];
489 struct commit *commit = commit;
490 char *full_name;
492 if ((e->flags & UNINTERESTING) || dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
493 continue;
495 switch (e->item->type) {
496 case OBJ_COMMIT:
497 commit = (struct commit *)e->item;
498 break;
499 case OBJ_TAG:
500 tag = (struct tag *)e->item;
502 /* handle nested tags */
503 while (tag && tag->object.type == OBJ_TAG) {
504 parse_object(tag->object.sha1);
505 string_list_append(extra_refs, full_name)->util = tag;
506 tag = (struct tag *)tag->tagged;
508 if (!tag)
509 die ("Tag %s points nowhere?", e->name);
510 switch(tag->object.type) {
511 case OBJ_COMMIT:
512 commit = (struct commit *)tag;
513 break;
514 case OBJ_BLOB:
515 handle_object(tag->object.sha1);
516 continue;
517 default: /* OBJ_TAG (nested tags) is already handled */
518 warning("Tag points to object of unexpected type %s, skipping.",
519 typename(tag->object.type));
520 continue;
522 break;
523 default:
524 warning("%s: Unexpected object of type %s, skipping.",
525 e->name,
526 typename(e->item->type));
527 continue;
529 if (commit->util)
530 /* more than one name for the same object */
531 string_list_append(extra_refs, full_name)->util = commit;
532 else {
533 commit->util = full_name;
534 /* we might need to set this ref explicitly */
535 string_list_append(refs, full_name)->util = commit;
540 static void handle_reset(const char *name, struct object *object)
542 int mark = get_object_mark(object);
544 if (mark)
545 printf("reset %s\nfrom :%d\n\n", name,
546 get_object_mark(object));
547 else
548 printf("reset %s\nfrom %s\n\n", name,
549 sha1_to_hex(object->sha1));
552 static void handle_tags_and_duplicates(struct string_list *refs, struct string_list *extra_refs)
554 int i;
556 /* even if no commits were exported, we need to export the ref */
557 for (i = refs->nr - 1; i >= 0; i--) {
558 const char *name = refs->items[i].string;
559 struct object *object = refs->items[i].util;
561 if (!(object->flags & REF_HANDLED)) {
562 if (object->type & OBJ_TAG)
563 handle_tag(name, (struct tag *)object);
564 else {
565 if (!prefixcmp(name, "refs/tags/") &&
566 (tag_of_filtered_mode != REWRITE ||
567 !get_object_mark(object)))
568 continue;
569 handle_reset(name, object);
570 object->flags |= REF_HANDLED;
575 for (i = extra_refs->nr - 1; i >= 0; i--) {
576 const char *name = extra_refs->items[i].string;
577 struct object *object = extra_refs->items[i].util;
578 switch (object->type) {
579 case OBJ_TAG:
580 handle_tag(name, (struct tag *)object);
581 break;
582 case OBJ_COMMIT:
583 /* create refs pointing to already seen commits */
584 handle_reset(name, object);
585 show_progress();
586 break;
591 static void export_marks(char *file)
593 unsigned int i;
594 uint32_t mark;
595 struct object_decoration *deco = idnums.hash;
596 FILE *f;
597 int e = 0;
599 f = fopen(file, "w");
600 if (!f)
601 die_errno("Unable to open marks file %s for writing.", file);
603 for (i = 0; i < idnums.size; i++) {
604 if (deco->base && deco->base->type == 1) {
605 mark = ptr_to_mark(deco->decoration);
606 if (fprintf(f, ":%"PRIu32" %s\n", mark,
607 sha1_to_hex(deco->base->sha1)) < 0) {
608 e = 1;
609 break;
612 deco++;
615 e |= ferror(f);
616 e |= fclose(f);
617 if (e)
618 error("Unable to write marks file %s.", file);
621 static void import_marks(char *input_file)
623 char line[512];
624 FILE *f = fopen(input_file, "r");
625 if (!f)
626 die_errno("cannot read '%s'", input_file);
628 while (fgets(line, sizeof(line), f)) {
629 uint32_t mark;
630 char *line_end, *mark_end;
631 unsigned char sha1[20];
632 struct object *object;
634 line_end = strchr(line, '\n');
635 if (line[0] != ':' || !line_end)
636 die("corrupt mark line: %s", line);
637 *line_end = '\0';
639 mark = strtoumax(line + 1, &mark_end, 10);
640 if (!mark || mark_end == line + 1
641 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
642 die("corrupt mark line: %s", line);
644 object = parse_object(sha1);
645 if (!object)
646 die ("Could not read blob %s", sha1_to_hex(sha1));
648 if (object->flags & SHOWN)
649 error("Object %s already has a mark", sha1_to_hex(sha1));
651 mark_object(object, mark);
652 if (last_idnum < mark)
653 last_idnum = mark;
655 object->flags |= SHOWN;
657 fclose(f);
660 int cmd_fast_export(int argc, const char **argv, const char *prefix)
662 struct rev_info revs;
663 struct object_array commits = OBJECT_ARRAY_INIT;
664 struct string_list refs = STRING_LIST_INIT_NODUP, extra_refs = STRING_LIST_INIT_NODUP;
665 struct commit *commit;
666 char *export_filename = NULL, *import_filename = NULL;
667 struct option options[] = {
668 OPT_INTEGER(0, "progress", &progress,
669 N_("show progress after <n> objects")),
670 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
671 N_("select handling of signed tags"),
672 parse_opt_signed_tag_mode),
673 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
674 N_("select handling of tags that tag filtered objects"),
675 parse_opt_tag_of_filtered_mode),
676 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
677 N_("Dump marks to this file")),
678 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
679 N_("Import marks from this file")),
680 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
681 N_("Fake a tagger when tags lack one")),
682 OPT_BOOLEAN(0, "full-tree", &full_tree,
683 N_("Output full tree for each commit")),
684 OPT_BOOLEAN(0, "use-done-feature", &use_done_feature,
685 N_("Use the done feature to terminate the stream")),
686 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
687 OPT_END()
690 if (argc == 1)
691 usage_with_options (fast_export_usage, options);
693 /* we handle encodings */
694 git_config(git_default_config, NULL);
696 init_revisions(&revs, prefix);
697 revs.topo_order = 1;
698 revs.show_source = 1;
699 revs.rewrite_parents = 1;
700 argc = setup_revisions(argc, argv, &revs, NULL);
701 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
702 if (argc > 1)
703 usage_with_options (fast_export_usage, options);
705 if (use_done_feature)
706 printf("feature done\n");
708 if (import_filename)
709 import_marks(import_filename);
711 if (import_filename && revs.prune_data.nr)
712 full_tree = 1;
714 get_tags_and_duplicates(&revs.pending, &refs, &extra_refs);
716 if (prepare_revision_walk(&revs))
717 die("revision walk setup failed");
718 revs.diffopt.format_callback = show_filemodify;
719 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
720 while ((commit = get_revision(&revs))) {
721 if (has_unshown_parent(commit)) {
722 add_object_array(&commit->object, NULL, &commits);
724 else {
725 handle_commit(commit, &revs);
726 commit->object.flags |= REF_HANDLED;
727 handle_tail(&commits, &revs);
731 handle_tags_and_duplicates(&refs, &extra_refs);
733 if (export_filename)
734 export_marks(export_filename);
736 if (use_done_feature)
737 printf("done\n");
739 return 0;