Merge branch 'jk/bisect-prn-unsigned'
[git/mingw/j6t.git] / builtin / fast-export.c
blobf44b76cb330250f969a50fd684465dc5c1cb4ecc
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 rev_cmdline_info *info,
482 struct string_list *extra_refs)
484 struct tag *tag;
485 int i;
487 for (i = 0; i < info->nr; i++) {
488 struct rev_cmdline_entry *e = info->rev + i;
489 unsigned char sha1[20];
490 struct commit *commit;
491 char *full_name;
493 if (e->flags & UNINTERESTING)
494 continue;
496 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
497 continue;
499 switch (e->item->type) {
500 case OBJ_COMMIT:
501 commit = (struct commit *)e->item;
502 break;
503 case OBJ_TAG:
504 tag = (struct tag *)e->item;
506 /* handle nested tags */
507 while (tag && tag->object.type == OBJ_TAG) {
508 parse_object(tag->object.sha1);
509 string_list_append(extra_refs, full_name)->util = tag;
510 tag = (struct tag *)tag->tagged;
512 if (!tag)
513 die ("Tag %s points nowhere?", e->name);
514 switch(tag->object.type) {
515 case OBJ_COMMIT:
516 commit = (struct commit *)tag;
517 break;
518 case OBJ_BLOB:
519 export_blob(tag->object.sha1);
520 continue;
521 default: /* OBJ_TAG (nested tags) is already handled */
522 warning("Tag points to object of unexpected type %s, skipping.",
523 typename(tag->object.type));
524 continue;
526 break;
527 default:
528 warning("%s: Unexpected object of type %s, skipping.",
529 e->name,
530 typename(e->item->type));
531 continue;
535 * This ref will not be updated through a commit, lets make
536 * sure it gets properly updated eventually.
538 if (commit->util || commit->object.flags & SHOWN)
539 string_list_append(extra_refs, full_name)->util = commit;
540 if (!commit->util)
541 commit->util = full_name;
545 static void handle_tags_and_duplicates(struct string_list *extra_refs)
547 struct commit *commit;
548 int i;
550 for (i = extra_refs->nr - 1; i >= 0; i--) {
551 const char *name = extra_refs->items[i].string;
552 struct object *object = extra_refs->items[i].util;
553 switch (object->type) {
554 case OBJ_TAG:
555 handle_tag(name, (struct tag *)object);
556 break;
557 case OBJ_COMMIT:
558 /* create refs pointing to already seen commits */
559 commit = (struct commit *)object;
560 printf("reset %s\nfrom :%d\n\n", name,
561 get_object_mark(&commit->object));
562 show_progress();
563 break;
568 static void export_marks(char *file)
570 unsigned int i;
571 uint32_t mark;
572 struct object_decoration *deco = idnums.hash;
573 FILE *f;
574 int e = 0;
576 f = fopen(file, "w");
577 if (!f)
578 die_errno("Unable to open marks file %s for writing.", file);
580 for (i = 0; i < idnums.size; i++) {
581 if (deco->base && deco->base->type == 1) {
582 mark = ptr_to_mark(deco->decoration);
583 if (fprintf(f, ":%"PRIu32" %s\n", mark,
584 sha1_to_hex(deco->base->sha1)) < 0) {
585 e = 1;
586 break;
589 deco++;
592 e |= ferror(f);
593 e |= fclose(f);
594 if (e)
595 error("Unable to write marks file %s.", file);
598 static void import_marks(char *input_file)
600 char line[512];
601 FILE *f = fopen(input_file, "r");
602 if (!f)
603 die_errno("cannot read '%s'", input_file);
605 while (fgets(line, sizeof(line), f)) {
606 uint32_t mark;
607 char *line_end, *mark_end;
608 unsigned char sha1[20];
609 struct object *object;
611 line_end = strchr(line, '\n');
612 if (line[0] != ':' || !line_end)
613 die("corrupt mark line: %s", line);
614 *line_end = '\0';
616 mark = strtoumax(line + 1, &mark_end, 10);
617 if (!mark || mark_end == line + 1
618 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
619 die("corrupt mark line: %s", line);
621 if (last_idnum < mark)
622 last_idnum = mark;
624 object = parse_object(sha1);
625 if (!object)
626 continue;
628 if (object->flags & SHOWN)
629 error("Object %s already has a mark", sha1_to_hex(sha1));
631 if (object->type != OBJ_COMMIT)
632 /* only commits */
633 continue;
635 mark_object(object, mark);
637 object->flags |= SHOWN;
639 fclose(f);
642 int cmd_fast_export(int argc, const char **argv, const char *prefix)
644 struct rev_info revs;
645 struct object_array commits = OBJECT_ARRAY_INIT;
646 struct string_list extra_refs = STRING_LIST_INIT_NODUP;
647 struct commit *commit;
648 char *export_filename = NULL, *import_filename = NULL;
649 uint32_t lastimportid;
650 struct option options[] = {
651 OPT_INTEGER(0, "progress", &progress,
652 N_("show progress after <n> objects")),
653 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
654 N_("select handling of signed tags"),
655 parse_opt_signed_tag_mode),
656 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
657 N_("select handling of tags that tag filtered objects"),
658 parse_opt_tag_of_filtered_mode),
659 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
660 N_("Dump marks to this file")),
661 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
662 N_("Import marks from this file")),
663 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
664 N_("Fake a tagger when tags lack one")),
665 OPT_BOOLEAN(0, "full-tree", &full_tree,
666 N_("Output full tree for each commit")),
667 OPT_BOOLEAN(0, "use-done-feature", &use_done_feature,
668 N_("Use the done feature to terminate the stream")),
669 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
670 OPT_END()
673 if (argc == 1)
674 usage_with_options (fast_export_usage, options);
676 /* we handle encodings */
677 git_config(git_default_config, NULL);
679 init_revisions(&revs, prefix);
680 revs.topo_order = 1;
681 revs.show_source = 1;
682 revs.rewrite_parents = 1;
683 argc = setup_revisions(argc, argv, &revs, NULL);
684 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
685 if (argc > 1)
686 usage_with_options (fast_export_usage, options);
688 if (use_done_feature)
689 printf("feature done\n");
691 if (import_filename)
692 import_marks(import_filename);
693 lastimportid = last_idnum;
695 if (import_filename && revs.prune_data.nr)
696 full_tree = 1;
698 get_tags_and_duplicates(&revs.cmdline, &extra_refs);
700 if (prepare_revision_walk(&revs))
701 die("revision walk setup failed");
702 revs.diffopt.format_callback = show_filemodify;
703 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
704 while ((commit = get_revision(&revs))) {
705 if (has_unshown_parent(commit)) {
706 add_object_array(&commit->object, NULL, &commits);
708 else {
709 handle_commit(commit, &revs);
710 handle_tail(&commits, &revs);
714 handle_tags_and_duplicates(&extra_refs);
716 if (export_filename && lastimportid != last_idnum)
717 export_marks(export_filename);
719 if (use_done_feature)
720 printf("done\n");
722 return 0;