install-webdoc: keep installed RelNotes-*.txt
[git/jnareb-git.git] / builtin / fast-export.c
bloba9bbf8653d1a8fa704a38c06102ec3ace4a59a28
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"
20 static const char *fast_export_usage[] = {
21 "git fast-export [rev-list-opts]",
22 NULL
25 static int progress;
26 static enum { ABORT, VERBATIM, WARN, STRIP } signed_tag_mode = ABORT;
27 static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ABORT;
28 static int fake_missing_tagger;
29 static int no_data;
30 static int full_tree;
32 static int parse_opt_signed_tag_mode(const struct option *opt,
33 const char *arg, int unset)
35 if (unset || !strcmp(arg, "abort"))
36 signed_tag_mode = ABORT;
37 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
38 signed_tag_mode = VERBATIM;
39 else if (!strcmp(arg, "warn"))
40 signed_tag_mode = WARN;
41 else if (!strcmp(arg, "strip"))
42 signed_tag_mode = STRIP;
43 else
44 return error("Unknown signed-tag mode: %s", arg);
45 return 0;
48 static int parse_opt_tag_of_filtered_mode(const struct option *opt,
49 const char *arg, int unset)
51 if (unset || !strcmp(arg, "abort"))
52 tag_of_filtered_mode = ABORT;
53 else if (!strcmp(arg, "drop"))
54 tag_of_filtered_mode = DROP;
55 else if (!strcmp(arg, "rewrite"))
56 tag_of_filtered_mode = REWRITE;
57 else
58 return error("Unknown tag-of-filtered mode: %s", arg);
59 return 0;
62 static struct decoration idnums;
63 static uint32_t last_idnum;
65 static int has_unshown_parent(struct commit *commit)
67 struct commit_list *parent;
69 for (parent = commit->parents; parent; parent = parent->next)
70 if (!(parent->item->object.flags & SHOWN) &&
71 !(parent->item->object.flags & UNINTERESTING))
72 return 1;
73 return 0;
76 /* Since intptr_t is C99, we do not use it here */
77 static inline uint32_t *mark_to_ptr(uint32_t mark)
79 return ((uint32_t *)NULL) + mark;
82 static inline uint32_t ptr_to_mark(void * mark)
84 return (uint32_t *)mark - (uint32_t *)NULL;
87 static inline void mark_object(struct object *object, uint32_t mark)
89 add_decoration(&idnums, object, mark_to_ptr(mark));
92 static inline void mark_next_object(struct object *object)
94 mark_object(object, ++last_idnum);
97 static int get_object_mark(struct object *object)
99 void *decoration = lookup_decoration(&idnums, object);
100 if (!decoration)
101 return 0;
102 return ptr_to_mark(decoration);
105 static void show_progress(void)
107 static int counter = 0;
108 if (!progress)
109 return;
110 if ((++counter % progress) == 0)
111 printf("progress %d objects\n", counter);
114 static void handle_object(const unsigned char *sha1)
116 unsigned long size;
117 enum object_type type;
118 char *buf;
119 struct object *object;
121 if (no_data)
122 return;
124 if (is_null_sha1(sha1))
125 return;
127 object = parse_object(sha1);
128 if (!object)
129 die ("Could not read blob %s", sha1_to_hex(sha1));
131 if (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));
138 mark_next_object(object);
140 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
141 if (size && fwrite(buf, size, 1, stdout) != 1)
142 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1));
143 printf("\n");
145 show_progress();
147 object->flags |= SHOWN;
148 free(buf);
151 static int depth_first(const void *a_, const void *b_)
153 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
154 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
155 const char *name_a, *name_b;
156 int len_a, len_b, len;
157 int cmp;
159 name_a = a->one ? a->one->path : a->two->path;
160 name_b = b->one ? b->one->path : b->two->path;
162 len_a = strlen(name_a);
163 len_b = strlen(name_b);
164 len = (len_a < len_b) ? len_a : len_b;
166 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
167 cmp = memcmp(name_a, name_b, len);
168 if (cmp)
169 return cmp;
170 return (len_b - len_a);
173 static void show_filemodify(struct diff_queue_struct *q,
174 struct diff_options *options, void *data)
176 int i;
179 * Handle files below a directory first, in case they are all deleted
180 * and the directory changes to a file or symlink.
182 qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
184 for (i = 0; i < q->nr; i++) {
185 struct diff_filespec *ospec = q->queue[i]->one;
186 struct diff_filespec *spec = q->queue[i]->two;
188 switch (q->queue[i]->status) {
189 case DIFF_STATUS_DELETED:
190 printf("D %s\n", spec->path);
191 break;
193 case DIFF_STATUS_COPIED:
194 case DIFF_STATUS_RENAMED:
195 printf("%c \"%s\" \"%s\"\n", q->queue[i]->status,
196 ospec->path, spec->path);
198 if (!hashcmp(ospec->sha1, spec->sha1) &&
199 ospec->mode == spec->mode)
200 break;
201 /* fallthrough */
203 case DIFF_STATUS_TYPE_CHANGED:
204 case DIFF_STATUS_MODIFIED:
205 case DIFF_STATUS_ADDED:
207 * Links refer to objects in another repositories;
208 * output the SHA-1 verbatim.
210 if (no_data || S_ISGITLINK(spec->mode))
211 printf("M %06o %s %s\n", spec->mode,
212 sha1_to_hex(spec->sha1), spec->path);
213 else {
214 struct object *object = lookup_object(spec->sha1);
215 printf("M %06o :%d %s\n", spec->mode,
216 get_object_mark(object), spec->path);
218 break;
220 default:
221 die("Unexpected comparison status '%c' for %s, %s",
222 q->queue[i]->status,
223 ospec->path ? ospec->path : "none",
224 spec->path ? spec->path : "none");
229 static const char *find_encoding(const char *begin, const char *end)
231 const char *needle = "\nencoding ";
232 char *bol, *eol;
234 bol = memmem(begin, end ? end - begin : strlen(begin),
235 needle, strlen(needle));
236 if (!bol)
237 return git_commit_encoding;
238 bol += strlen(needle);
239 eol = strchrnul(bol, '\n');
240 *eol = '\0';
241 return bol;
244 static void handle_commit(struct commit *commit, struct rev_info *rev)
246 int saved_output_format = rev->diffopt.output_format;
247 const char *author, *author_end, *committer, *committer_end;
248 const char *encoding, *message;
249 char *reencoded = NULL;
250 struct commit_list *p;
251 int i;
253 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
255 parse_commit(commit);
256 author = strstr(commit->buffer, "\nauthor ");
257 if (!author)
258 die ("Could not find author in commit %s",
259 sha1_to_hex(commit->object.sha1));
260 author++;
261 author_end = strchrnul(author, '\n');
262 committer = strstr(author_end, "\ncommitter ");
263 if (!committer)
264 die ("Could not find committer in commit %s",
265 sha1_to_hex(commit->object.sha1));
266 committer++;
267 committer_end = strchrnul(committer, '\n');
268 message = strstr(committer_end, "\n\n");
269 encoding = find_encoding(committer_end, message);
270 if (message)
271 message += 2;
273 if (commit->parents &&
274 get_object_mark(&commit->parents->item->object) != 0 &&
275 !full_tree) {
276 parse_commit(commit->parents->item);
277 diff_tree_sha1(commit->parents->item->tree->object.sha1,
278 commit->tree->object.sha1, "", &rev->diffopt);
280 else
281 diff_root_tree_sha1(commit->tree->object.sha1,
282 "", &rev->diffopt);
284 /* Export the referenced blobs, and remember the marks. */
285 for (i = 0; i < diff_queued_diff.nr; i++)
286 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
287 handle_object(diff_queued_diff.queue[i]->two->sha1);
289 mark_next_object(&commit->object);
290 if (!is_encoding_utf8(encoding))
291 reencoded = reencode_string(message, "UTF-8", encoding);
292 if (!commit->parents)
293 printf("reset %s\n", (const char*)commit->util);
294 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
295 (const char *)commit->util, last_idnum,
296 (int)(author_end - author), author,
297 (int)(committer_end - committer), committer,
298 (unsigned)(reencoded
299 ? strlen(reencoded) : message
300 ? strlen(message) : 0),
301 reencoded ? reencoded : message ? message : "");
302 free(reencoded);
304 for (i = 0, p = commit->parents; p; p = p->next) {
305 int mark = get_object_mark(&p->item->object);
306 if (!mark)
307 continue;
308 if (i == 0)
309 printf("from :%d\n", mark);
310 else
311 printf("merge :%d\n", mark);
312 i++;
315 if (full_tree)
316 printf("deleteall\n");
317 log_tree_diff_flush(rev);
318 rev->diffopt.output_format = saved_output_format;
320 printf("\n");
322 show_progress();
325 static void handle_tail(struct object_array *commits, struct rev_info *revs)
327 struct commit *commit;
328 while (commits->nr) {
329 commit = (struct commit *)commits->objects[commits->nr - 1].item;
330 if (has_unshown_parent(commit))
331 return;
332 handle_commit(commit, revs);
333 commits->nr--;
337 static void handle_tag(const char *name, struct tag *tag)
339 unsigned long size;
340 enum object_type type;
341 char *buf;
342 const char *tagger, *tagger_end, *message;
343 size_t message_size = 0;
344 struct object *tagged;
345 int tagged_mark;
346 struct commit *p;
348 /* Trees have no identifer in fast-export output, thus we have no way
349 * to output tags of trees, tags of tags of trees, etc. Simply omit
350 * such tags.
352 tagged = tag->tagged;
353 while (tagged->type == OBJ_TAG) {
354 tagged = ((struct tag *)tagged)->tagged;
356 if (tagged->type == OBJ_TREE) {
357 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
358 sha1_to_hex(tag->object.sha1));
359 return;
362 buf = read_sha1_file(tag->object.sha1, &type, &size);
363 if (!buf)
364 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
365 message = memmem(buf, size, "\n\n", 2);
366 if (message) {
367 message += 2;
368 message_size = strlen(message);
370 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
371 if (!tagger) {
372 if (fake_missing_tagger)
373 tagger = "tagger Unspecified Tagger "
374 "<unspecified-tagger> 0 +0000";
375 else
376 tagger = "";
377 tagger_end = tagger + strlen(tagger);
378 } else {
379 tagger++;
380 tagger_end = strchrnul(tagger, '\n');
383 /* handle signed tags */
384 if (message) {
385 const char *signature = strstr(message,
386 "\n-----BEGIN PGP SIGNATURE-----\n");
387 if (signature)
388 switch(signed_tag_mode) {
389 case ABORT:
390 die ("Encountered signed tag %s; use "
391 "--signed-tag=<mode> to handle it.",
392 sha1_to_hex(tag->object.sha1));
393 case WARN:
394 warning ("Exporting signed tag %s",
395 sha1_to_hex(tag->object.sha1));
396 /* fallthru */
397 case VERBATIM:
398 break;
399 case STRIP:
400 message_size = signature + 1 - message;
401 break;
405 /* handle tag->tagged having been filtered out due to paths specified */
406 tagged = tag->tagged;
407 tagged_mark = get_object_mark(tagged);
408 if (!tagged_mark) {
409 switch(tag_of_filtered_mode) {
410 case ABORT:
411 die ("Tag %s tags unexported object; use "
412 "--tag-of-filtered-object=<mode> to handle it.",
413 sha1_to_hex(tag->object.sha1));
414 case DROP:
415 /* Ignore this tag altogether */
416 return;
417 case REWRITE:
418 if (tagged->type != OBJ_COMMIT) {
419 die ("Tag %s tags unexported %s!",
420 sha1_to_hex(tag->object.sha1),
421 typename(tagged->type));
423 p = (struct commit *)tagged;
424 for (;;) {
425 if (p->parents && p->parents->next)
426 break;
427 if (p->object.flags & UNINTERESTING)
428 break;
429 if (!(p->object.flags & TREESAME))
430 break;
431 if (!p->parents)
432 die ("Can't find replacement commit for tag %s\n",
433 sha1_to_hex(tag->object.sha1));
434 p = p->parents->item;
436 tagged_mark = get_object_mark(&p->object);
440 if (!prefixcmp(name, "refs/tags/"))
441 name += 10;
442 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
443 name, tagged_mark,
444 (int)(tagger_end - tagger), tagger,
445 tagger == tagger_end ? "" : "\n",
446 (int)message_size, (int)message_size, message ? message : "");
449 static void get_tags_and_duplicates(struct object_array *pending,
450 struct string_list *extra_refs)
452 struct tag *tag;
453 int i;
455 for (i = 0; i < pending->nr; i++) {
456 struct object_array_entry *e = pending->objects + i;
457 unsigned char sha1[20];
458 struct commit *commit = commit;
459 char *full_name;
461 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
462 continue;
464 switch (e->item->type) {
465 case OBJ_COMMIT:
466 commit = (struct commit *)e->item;
467 break;
468 case OBJ_TAG:
469 tag = (struct tag *)e->item;
471 /* handle nested tags */
472 while (tag && tag->object.type == OBJ_TAG) {
473 parse_object(tag->object.sha1);
474 string_list_append(extra_refs, full_name)->util = tag;
475 tag = (struct tag *)tag->tagged;
477 if (!tag)
478 die ("Tag %s points nowhere?", e->name);
479 switch(tag->object.type) {
480 case OBJ_COMMIT:
481 commit = (struct commit *)tag;
482 break;
483 case OBJ_BLOB:
484 handle_object(tag->object.sha1);
485 continue;
486 default: /* OBJ_TAG (nested tags) is already handled */
487 warning("Tag points to object of unexpected type %s, skipping.",
488 typename(tag->object.type));
489 continue;
491 break;
492 default:
493 warning("%s: Unexpected object of type %s, skipping.",
494 e->name,
495 typename(e->item->type));
496 continue;
498 if (commit->util)
499 /* more than one name for the same object */
500 string_list_append(extra_refs, full_name)->util = commit;
501 else
502 commit->util = full_name;
506 static void handle_tags_and_duplicates(struct string_list *extra_refs)
508 struct commit *commit;
509 int i;
511 for (i = extra_refs->nr - 1; i >= 0; i--) {
512 const char *name = extra_refs->items[i].string;
513 struct object *object = extra_refs->items[i].util;
514 switch (object->type) {
515 case OBJ_TAG:
516 handle_tag(name, (struct tag *)object);
517 break;
518 case OBJ_COMMIT:
519 /* create refs pointing to already seen commits */
520 commit = (struct commit *)object;
521 printf("reset %s\nfrom :%d\n\n", name,
522 get_object_mark(&commit->object));
523 show_progress();
524 break;
529 static void export_marks(char *file)
531 unsigned int i;
532 uint32_t mark;
533 struct object_decoration *deco = idnums.hash;
534 FILE *f;
535 int e = 0;
537 f = fopen(file, "w");
538 if (!f)
539 die_errno("Unable to open marks file %s for writing.", file);
541 for (i = 0; i < idnums.size; i++) {
542 if (deco->base && deco->base->type == 1) {
543 mark = ptr_to_mark(deco->decoration);
544 if (fprintf(f, ":%"PRIu32" %s\n", mark,
545 sha1_to_hex(deco->base->sha1)) < 0) {
546 e = 1;
547 break;
550 deco++;
553 e |= ferror(f);
554 e |= fclose(f);
555 if (e)
556 error("Unable to write marks file %s.", file);
559 static void import_marks(char *input_file)
561 char line[512];
562 FILE *f = fopen(input_file, "r");
563 if (!f)
564 die_errno("cannot read '%s'", input_file);
566 while (fgets(line, sizeof(line), f)) {
567 uint32_t mark;
568 char *line_end, *mark_end;
569 unsigned char sha1[20];
570 struct object *object;
572 line_end = strchr(line, '\n');
573 if (line[0] != ':' || !line_end)
574 die("corrupt mark line: %s", line);
575 *line_end = '\0';
577 mark = strtoumax(line + 1, &mark_end, 10);
578 if (!mark || mark_end == line + 1
579 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
580 die("corrupt mark line: %s", line);
582 object = parse_object(sha1);
583 if (!object)
584 die ("Could not read blob %s", sha1_to_hex(sha1));
586 if (object->flags & SHOWN)
587 error("Object %s already has a mark", sha1);
589 mark_object(object, mark);
590 if (last_idnum < mark)
591 last_idnum = mark;
593 object->flags |= SHOWN;
595 fclose(f);
598 int cmd_fast_export(int argc, const char **argv, const char *prefix)
600 struct rev_info revs;
601 struct object_array commits = OBJECT_ARRAY_INIT;
602 struct string_list extra_refs = STRING_LIST_INIT_NODUP;
603 struct commit *commit;
604 char *export_filename = NULL, *import_filename = NULL;
605 struct option options[] = {
606 OPT_INTEGER(0, "progress", &progress,
607 "show progress after <n> objects"),
608 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
609 "select handling of signed tags",
610 parse_opt_signed_tag_mode),
611 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, "mode",
612 "select handling of tags that tag filtered objects",
613 parse_opt_tag_of_filtered_mode),
614 OPT_STRING(0, "export-marks", &export_filename, "FILE",
615 "Dump marks to this file"),
616 OPT_STRING(0, "import-marks", &import_filename, "FILE",
617 "Import marks from this file"),
618 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
619 "Fake a tagger when tags lack one"),
620 OPT_BOOLEAN(0, "full-tree", &full_tree,
621 "Output full tree for each commit"),
622 { OPTION_NEGBIT, 0, "data", &no_data, NULL,
623 "Skip output of blob data",
624 PARSE_OPT_NOARG | PARSE_OPT_NEGHELP, NULL, 1 },
625 OPT_END()
628 if (argc == 1)
629 usage_with_options (fast_export_usage, options);
631 /* we handle encodings */
632 git_config(git_default_config, NULL);
634 init_revisions(&revs, prefix);
635 revs.topo_order = 1;
636 revs.show_source = 1;
637 revs.rewrite_parents = 1;
638 argc = setup_revisions(argc, argv, &revs, NULL);
639 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
640 if (argc > 1)
641 usage_with_options (fast_export_usage, options);
643 if (import_filename)
644 import_marks(import_filename);
646 if (import_filename && revs.prune_data)
647 full_tree = 1;
649 get_tags_and_duplicates(&revs.pending, &extra_refs);
651 if (prepare_revision_walk(&revs))
652 die("revision walk setup failed");
653 revs.diffopt.format_callback = show_filemodify;
654 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
655 while ((commit = get_revision(&revs))) {
656 if (has_unshown_parent(commit)) {
657 add_object_array(&commit->object, NULL, &commits);
659 else {
660 handle_commit(commit, &revs);
661 handle_tail(&commits, &revs);
665 handle_tags_and_duplicates(&extra_refs);
667 if (export_filename)
668 export_marks(export_filename);
670 return 0;