fast-export: do not load blob objects twice
[git/jnareb-git.git] / builtin / fast-export.c
blobed2916595fc386fe4127dc3fe9f68b85d15b3def
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, 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;
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, "strip"))
44 signed_tag_mode = STRIP;
45 else
46 return error("Unknown signed-tag mode: %s", arg);
47 return 0;
50 static int parse_opt_tag_of_filtered_mode(const struct option *opt,
51 const char *arg, int unset)
53 if (unset || !strcmp(arg, "abort"))
54 tag_of_filtered_mode = ERROR;
55 else if (!strcmp(arg, "drop"))
56 tag_of_filtered_mode = DROP;
57 else if (!strcmp(arg, "rewrite"))
58 tag_of_filtered_mode = REWRITE;
59 else
60 return error("Unknown tag-of-filtered mode: %s", arg);
61 return 0;
64 static struct decoration idnums;
65 static uint32_t last_idnum;
67 static int has_unshown_parent(struct commit *commit)
69 struct commit_list *parent;
71 for (parent = commit->parents; parent; parent = parent->next)
72 if (!(parent->item->object.flags & SHOWN) &&
73 !(parent->item->object.flags & UNINTERESTING))
74 return 1;
75 return 0;
78 /* Since intptr_t is C99, we do not use it here */
79 static inline uint32_t *mark_to_ptr(uint32_t mark)
81 return ((uint32_t *)NULL) + mark;
84 static inline uint32_t ptr_to_mark(void * mark)
86 return (uint32_t *)mark - (uint32_t *)NULL;
89 static inline void mark_object(struct object *object, uint32_t mark)
91 add_decoration(&idnums, object, mark_to_ptr(mark));
94 static inline void mark_next_object(struct object *object)
96 mark_object(object, ++last_idnum);
99 static int get_object_mark(struct object *object)
101 void *decoration = lookup_decoration(&idnums, object);
102 if (!decoration)
103 return 0;
104 return ptr_to_mark(decoration);
107 static void show_progress(void)
109 static int counter = 0;
110 if (!progress)
111 return;
112 if ((++counter % progress) == 0)
113 printf("progress %d objects\n", counter);
116 static void export_blob(const unsigned char *sha1)
118 unsigned long size;
119 enum object_type type;
120 char *buf;
121 struct object *object;
122 int eaten;
124 if (no_data)
125 return;
127 if (is_null_sha1(sha1))
128 return;
130 object = lookup_object(sha1);
131 if (object && object->flags & SHOWN)
132 return;
134 buf = read_sha1_file(sha1, &type, &size);
135 if (!buf)
136 die ("Could not read blob %s", sha1_to_hex(sha1));
137 if (check_sha1_signature(sha1, buf, size, typename(type)) < 0)
138 die("sha1 mismatch in blob %s", sha1_to_hex(sha1));
139 object = parse_object_buffer(sha1, type, size, buf, &eaten);
140 if (!object)
141 die("Could not read blob %s", sha1_to_hex(sha1));
143 mark_next_object(object);
145 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
146 if (size && fwrite(buf, size, 1, stdout) != 1)
147 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1));
148 printf("\n");
150 show_progress();
152 object->flags |= SHOWN;
153 if (!eaten)
154 free(buf);
157 static int depth_first(const void *a_, const void *b_)
159 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
160 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
161 const char *name_a, *name_b;
162 int len_a, len_b, len;
163 int cmp;
165 name_a = a->one ? a->one->path : a->two->path;
166 name_b = b->one ? b->one->path : b->two->path;
168 len_a = strlen(name_a);
169 len_b = strlen(name_b);
170 len = (len_a < len_b) ? len_a : len_b;
172 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
173 cmp = memcmp(name_a, name_b, len);
174 if (cmp)
175 return cmp;
176 cmp = len_b - len_a;
177 if (cmp)
178 return cmp;
180 * Move 'R'ename entries last so that all references of the file
181 * appear in the output before it is renamed (e.g., when a file
182 * was copied and renamed in the same commit).
184 return (a->status == 'R') - (b->status == 'R');
187 static void print_path(const char *path)
189 int need_quote = quote_c_style(path, NULL, NULL, 0);
190 if (need_quote)
191 quote_c_style(path, NULL, stdout, 0);
192 else if (strchr(path, ' '))
193 printf("\"%s\"", path);
194 else
195 printf("%s", path);
198 static void show_filemodify(struct diff_queue_struct *q,
199 struct diff_options *options, void *data)
201 int i;
204 * Handle files below a directory first, in case they are all deleted
205 * and the directory changes to a file or symlink.
207 qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
209 for (i = 0; i < q->nr; i++) {
210 struct diff_filespec *ospec = q->queue[i]->one;
211 struct diff_filespec *spec = q->queue[i]->two;
213 switch (q->queue[i]->status) {
214 case DIFF_STATUS_DELETED:
215 printf("D ");
216 print_path(spec->path);
217 putchar('\n');
218 break;
220 case DIFF_STATUS_COPIED:
221 case DIFF_STATUS_RENAMED:
222 printf("%c ", q->queue[i]->status);
223 print_path(ospec->path);
224 putchar(' ');
225 print_path(spec->path);
226 putchar('\n');
228 if (!hashcmp(ospec->sha1, spec->sha1) &&
229 ospec->mode == spec->mode)
230 break;
231 /* fallthrough */
233 case DIFF_STATUS_TYPE_CHANGED:
234 case DIFF_STATUS_MODIFIED:
235 case DIFF_STATUS_ADDED:
237 * Links refer to objects in another repositories;
238 * output the SHA-1 verbatim.
240 if (no_data || S_ISGITLINK(spec->mode))
241 printf("M %06o %s ", spec->mode,
242 sha1_to_hex(spec->sha1));
243 else {
244 struct object *object = lookup_object(spec->sha1);
245 printf("M %06o :%d ", spec->mode,
246 get_object_mark(object));
248 print_path(spec->path);
249 putchar('\n');
250 break;
252 default:
253 die("Unexpected comparison status '%c' for %s, %s",
254 q->queue[i]->status,
255 ospec->path ? ospec->path : "none",
256 spec->path ? spec->path : "none");
261 static const char *find_encoding(const char *begin, const char *end)
263 const char *needle = "\nencoding ";
264 char *bol, *eol;
266 bol = memmem(begin, end ? end - begin : strlen(begin),
267 needle, strlen(needle));
268 if (!bol)
269 return git_commit_encoding;
270 bol += strlen(needle);
271 eol = strchrnul(bol, '\n');
272 *eol = '\0';
273 return bol;
276 static void handle_commit(struct commit *commit, struct rev_info *rev)
278 int saved_output_format = rev->diffopt.output_format;
279 const char *author, *author_end, *committer, *committer_end;
280 const char *encoding, *message;
281 char *reencoded = NULL;
282 struct commit_list *p;
283 int i;
285 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
287 parse_commit(commit);
288 author = strstr(commit->buffer, "\nauthor ");
289 if (!author)
290 die ("Could not find author in commit %s",
291 sha1_to_hex(commit->object.sha1));
292 author++;
293 author_end = strchrnul(author, '\n');
294 committer = strstr(author_end, "\ncommitter ");
295 if (!committer)
296 die ("Could not find committer in commit %s",
297 sha1_to_hex(commit->object.sha1));
298 committer++;
299 committer_end = strchrnul(committer, '\n');
300 message = strstr(committer_end, "\n\n");
301 encoding = find_encoding(committer_end, message);
302 if (message)
303 message += 2;
305 if (commit->parents &&
306 get_object_mark(&commit->parents->item->object) != 0 &&
307 !full_tree) {
308 parse_commit(commit->parents->item);
309 diff_tree_sha1(commit->parents->item->tree->object.sha1,
310 commit->tree->object.sha1, "", &rev->diffopt);
312 else
313 diff_root_tree_sha1(commit->tree->object.sha1,
314 "", &rev->diffopt);
316 /* Export the referenced blobs, and remember the marks. */
317 for (i = 0; i < diff_queued_diff.nr; i++)
318 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
319 export_blob(diff_queued_diff.queue[i]->two->sha1);
321 mark_next_object(&commit->object);
322 if (!is_encoding_utf8(encoding))
323 reencoded = reencode_string(message, "UTF-8", encoding);
324 if (!commit->parents)
325 printf("reset %s\n", (const char*)commit->util);
326 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
327 (const char *)commit->util, last_idnum,
328 (int)(author_end - author), author,
329 (int)(committer_end - committer), committer,
330 (unsigned)(reencoded
331 ? strlen(reencoded) : message
332 ? strlen(message) : 0),
333 reencoded ? reencoded : message ? message : "");
334 free(reencoded);
336 for (i = 0, p = commit->parents; p; p = p->next) {
337 int mark = get_object_mark(&p->item->object);
338 if (!mark)
339 continue;
340 if (i == 0)
341 printf("from :%d\n", mark);
342 else
343 printf("merge :%d\n", mark);
344 i++;
347 if (full_tree)
348 printf("deleteall\n");
349 log_tree_diff_flush(rev);
350 rev->diffopt.output_format = saved_output_format;
352 printf("\n");
354 show_progress();
357 static void handle_tail(struct object_array *commits, struct rev_info *revs)
359 struct commit *commit;
360 while (commits->nr) {
361 commit = (struct commit *)commits->objects[commits->nr - 1].item;
362 if (has_unshown_parent(commit))
363 return;
364 handle_commit(commit, revs);
365 commits->nr--;
369 static void handle_tag(const char *name, struct tag *tag)
371 unsigned long size;
372 enum object_type type;
373 char *buf;
374 const char *tagger, *tagger_end, *message;
375 size_t message_size = 0;
376 struct object *tagged;
377 int tagged_mark;
378 struct commit *p;
380 /* Trees have no identifer in fast-export output, thus we have no way
381 * to output tags of trees, tags of tags of trees, etc. Simply omit
382 * such tags.
384 tagged = tag->tagged;
385 while (tagged->type == OBJ_TAG) {
386 tagged = ((struct tag *)tagged)->tagged;
388 if (tagged->type == OBJ_TREE) {
389 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
390 sha1_to_hex(tag->object.sha1));
391 return;
394 buf = read_sha1_file(tag->object.sha1, &type, &size);
395 if (!buf)
396 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
397 message = memmem(buf, size, "\n\n", 2);
398 if (message) {
399 message += 2;
400 message_size = strlen(message);
402 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
403 if (!tagger) {
404 if (fake_missing_tagger)
405 tagger = "tagger Unspecified Tagger "
406 "<unspecified-tagger> 0 +0000";
407 else
408 tagger = "";
409 tagger_end = tagger + strlen(tagger);
410 } else {
411 tagger++;
412 tagger_end = strchrnul(tagger, '\n');
415 /* handle signed tags */
416 if (message) {
417 const char *signature = strstr(message,
418 "\n-----BEGIN PGP SIGNATURE-----\n");
419 if (signature)
420 switch(signed_tag_mode) {
421 case ABORT:
422 die ("Encountered signed tag %s; use "
423 "--signed-tag=<mode> to handle it.",
424 sha1_to_hex(tag->object.sha1));
425 case WARN:
426 warning ("Exporting signed tag %s",
427 sha1_to_hex(tag->object.sha1));
428 /* fallthru */
429 case VERBATIM:
430 break;
431 case STRIP:
432 message_size = signature + 1 - message;
433 break;
437 /* handle tag->tagged having been filtered out due to paths specified */
438 tagged = tag->tagged;
439 tagged_mark = get_object_mark(tagged);
440 if (!tagged_mark) {
441 switch(tag_of_filtered_mode) {
442 case ABORT:
443 die ("Tag %s tags unexported object; use "
444 "--tag-of-filtered-object=<mode> to handle it.",
445 sha1_to_hex(tag->object.sha1));
446 case DROP:
447 /* Ignore this tag altogether */
448 return;
449 case REWRITE:
450 if (tagged->type != OBJ_COMMIT) {
451 die ("Tag %s tags unexported %s!",
452 sha1_to_hex(tag->object.sha1),
453 typename(tagged->type));
455 p = (struct commit *)tagged;
456 for (;;) {
457 if (p->parents && p->parents->next)
458 break;
459 if (p->object.flags & UNINTERESTING)
460 break;
461 if (!(p->object.flags & TREESAME))
462 break;
463 if (!p->parents)
464 die ("Can't find replacement commit for tag %s\n",
465 sha1_to_hex(tag->object.sha1));
466 p = p->parents->item;
468 tagged_mark = get_object_mark(&p->object);
472 if (!prefixcmp(name, "refs/tags/"))
473 name += 10;
474 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
475 name, tagged_mark,
476 (int)(tagger_end - tagger), tagger,
477 tagger == tagger_end ? "" : "\n",
478 (int)message_size, (int)message_size, message ? message : "");
481 static void get_tags_and_duplicates(struct object_array *pending,
482 struct string_list *extra_refs)
484 struct tag *tag;
485 int i;
487 for (i = 0; i < pending->nr; i++) {
488 struct object_array_entry *e = pending->objects + i;
489 unsigned char sha1[20];
490 struct commit *commit = commit;
491 char *full_name;
493 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
494 continue;
496 switch (e->item->type) {
497 case OBJ_COMMIT:
498 commit = (struct commit *)e->item;
499 break;
500 case OBJ_TAG:
501 tag = (struct tag *)e->item;
503 /* handle nested tags */
504 while (tag && tag->object.type == OBJ_TAG) {
505 parse_object(tag->object.sha1);
506 string_list_append(extra_refs, full_name)->util = tag;
507 tag = (struct tag *)tag->tagged;
509 if (!tag)
510 die ("Tag %s points nowhere?", e->name);
511 switch(tag->object.type) {
512 case OBJ_COMMIT:
513 commit = (struct commit *)tag;
514 break;
515 case OBJ_BLOB:
516 export_blob(tag->object.sha1);
517 continue;
518 default: /* OBJ_TAG (nested tags) is already handled */
519 warning("Tag points to object of unexpected type %s, skipping.",
520 typename(tag->object.type));
521 continue;
523 break;
524 default:
525 warning("%s: Unexpected object of type %s, skipping.",
526 e->name,
527 typename(e->item->type));
528 continue;
530 if (commit->util)
531 /* more than one name for the same object */
532 string_list_append(extra_refs, full_name)->util = commit;
533 else
534 commit->util = full_name;
538 static void handle_tags_and_duplicates(struct string_list *extra_refs)
540 struct commit *commit;
541 int i;
543 for (i = extra_refs->nr - 1; i >= 0; i--) {
544 const char *name = extra_refs->items[i].string;
545 struct object *object = extra_refs->items[i].util;
546 switch (object->type) {
547 case OBJ_TAG:
548 handle_tag(name, (struct tag *)object);
549 break;
550 case OBJ_COMMIT:
551 /* create refs pointing to already seen commits */
552 commit = (struct commit *)object;
553 printf("reset %s\nfrom :%d\n\n", name,
554 get_object_mark(&commit->object));
555 show_progress();
556 break;
561 static void export_marks(char *file)
563 unsigned int i;
564 uint32_t mark;
565 struct object_decoration *deco = idnums.hash;
566 FILE *f;
567 int e = 0;
569 f = fopen(file, "w");
570 if (!f)
571 die_errno("Unable to open marks file %s for writing.", file);
573 for (i = 0; i < idnums.size; i++) {
574 if (deco->base && deco->base->type == 1) {
575 mark = ptr_to_mark(deco->decoration);
576 if (fprintf(f, ":%"PRIu32" %s\n", mark,
577 sha1_to_hex(deco->base->sha1)) < 0) {
578 e = 1;
579 break;
582 deco++;
585 e |= ferror(f);
586 e |= fclose(f);
587 if (e)
588 error("Unable to write marks file %s.", file);
591 static void import_marks(char *input_file)
593 char line[512];
594 FILE *f = fopen(input_file, "r");
595 if (!f)
596 die_errno("cannot read '%s'", input_file);
598 while (fgets(line, sizeof(line), f)) {
599 uint32_t mark;
600 char *line_end, *mark_end;
601 unsigned char sha1[20];
602 struct object *object;
604 line_end = strchr(line, '\n');
605 if (line[0] != ':' || !line_end)
606 die("corrupt mark line: %s", line);
607 *line_end = '\0';
609 mark = strtoumax(line + 1, &mark_end, 10);
610 if (!mark || mark_end == line + 1
611 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
612 die("corrupt mark line: %s", line);
614 object = parse_object(sha1);
615 if (!object)
616 die ("Could not read blob %s", sha1_to_hex(sha1));
618 if (object->flags & SHOWN)
619 error("Object %s already has a mark", sha1_to_hex(sha1));
621 mark_object(object, mark);
622 if (last_idnum < mark)
623 last_idnum = mark;
625 object->flags |= SHOWN;
627 fclose(f);
630 int cmd_fast_export(int argc, const char **argv, const char *prefix)
632 struct rev_info revs;
633 struct object_array commits = OBJECT_ARRAY_INIT;
634 struct string_list extra_refs = STRING_LIST_INIT_NODUP;
635 struct commit *commit;
636 char *export_filename = NULL, *import_filename = NULL;
637 struct option options[] = {
638 OPT_INTEGER(0, "progress", &progress,
639 N_("show progress after <n> objects")),
640 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
641 N_("select handling of signed tags"),
642 parse_opt_signed_tag_mode),
643 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
644 N_("select handling of tags that tag filtered objects"),
645 parse_opt_tag_of_filtered_mode),
646 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
647 N_("Dump marks to this file")),
648 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
649 N_("Import marks from this file")),
650 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
651 N_("Fake a tagger when tags lack one")),
652 OPT_BOOLEAN(0, "full-tree", &full_tree,
653 N_("Output full tree for each commit")),
654 OPT_BOOLEAN(0, "use-done-feature", &use_done_feature,
655 N_("Use the done feature to terminate the stream")),
656 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
657 OPT_END()
660 if (argc == 1)
661 usage_with_options (fast_export_usage, options);
663 /* we handle encodings */
664 git_config(git_default_config, NULL);
666 init_revisions(&revs, prefix);
667 revs.topo_order = 1;
668 revs.show_source = 1;
669 revs.rewrite_parents = 1;
670 argc = setup_revisions(argc, argv, &revs, NULL);
671 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
672 if (argc > 1)
673 usage_with_options (fast_export_usage, options);
675 if (use_done_feature)
676 printf("feature done\n");
678 if (import_filename)
679 import_marks(import_filename);
681 if (import_filename && revs.prune_data.nr)
682 full_tree = 1;
684 get_tags_and_duplicates(&revs.pending, &extra_refs);
686 if (prepare_revision_walk(&revs))
687 die("revision walk setup failed");
688 revs.diffopt.format_callback = show_filemodify;
689 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
690 while ((commit = get_revision(&revs))) {
691 if (has_unshown_parent(commit)) {
692 add_object_array(&commit->object, NULL, &commits);
694 else {
695 handle_commit(commit, &revs);
696 handle_tail(&commits, &revs);
700 handle_tags_and_duplicates(&extra_refs);
702 if (export_filename)
703 export_marks(export_filename);
705 if (use_done_feature)
706 printf("done\n");
708 return 0;