2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
16 #include "string-list.h"
18 #include "parse-options.h"
20 static const char *fast_export_usage[] = {
21 "git fast-export [rev-list-opts]",
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;
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;
44 return error("Unknown signed-tag mode: %s", arg);
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;
58 return error("Unknown tag-of-filtered mode: %s", arg);
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))
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);
102 return ptr_to_mark(decoration);
105 static void show_progress(void)
107 static int counter = 0;
110 if ((++counter % progress) == 0)
111 printf("progress %d objects\n", counter);
114 static void handle_object(const unsigned char *sha1)
117 enum object_type type;
119 struct object *object;
124 if (is_null_sha1(sha1))
127 object = parse_object(sha1);
129 die ("Could not read blob %s", sha1_to_hex(sha1));
131 if (object->flags & SHOWN)
134 buf = read_sha1_file(sha1, &type, &size);
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));
147 object->flags |= SHOWN;
151 static void show_filemodify(struct diff_queue_struct *q,
152 struct diff_options *options, void *data)
155 for (i = 0; i < q->nr; i++) {
156 struct diff_filespec *ospec = q->queue[i]->one;
157 struct diff_filespec *spec = q->queue[i]->two;
159 switch (q->queue[i]->status) {
160 case DIFF_STATUS_DELETED:
161 printf("D %s\n", spec->path);
164 case DIFF_STATUS_COPIED:
165 case DIFF_STATUS_RENAMED:
166 printf("%c \"%s\" \"%s\"\n", q->queue[i]->status,
167 ospec->path, spec->path);
169 if (!hashcmp(ospec->sha1, spec->sha1) &&
170 ospec->mode == spec->mode)
174 case DIFF_STATUS_TYPE_CHANGED:
175 case DIFF_STATUS_MODIFIED:
176 case DIFF_STATUS_ADDED:
178 * Links refer to objects in another repositories;
179 * output the SHA-1 verbatim.
181 if (no_data || S_ISGITLINK(spec->mode))
182 printf("M %06o %s %s\n", spec->mode,
183 sha1_to_hex(spec->sha1), spec->path);
185 struct object *object = lookup_object(spec->sha1);
186 printf("M %06o :%d %s\n", spec->mode,
187 get_object_mark(object), spec->path);
192 die("Unexpected comparison status '%c' for %s, %s",
194 ospec->path ? ospec->path : "none",
195 spec->path ? spec->path : "none");
200 static const char *find_encoding(const char *begin, const char *end)
202 const char *needle = "\nencoding ";
205 bol = memmem(begin, end ? end - begin : strlen(begin),
206 needle, strlen(needle));
208 return git_commit_encoding;
209 bol += strlen(needle);
210 eol = strchrnul(bol, '\n');
215 static void handle_commit(struct commit *commit, struct rev_info *rev)
217 int saved_output_format = rev->diffopt.output_format;
218 const char *author, *author_end, *committer, *committer_end;
219 const char *encoding, *message;
220 char *reencoded = NULL;
221 struct commit_list *p;
224 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
226 parse_commit(commit);
227 author = strstr(commit->buffer, "\nauthor ");
229 die ("Could not find author in commit %s",
230 sha1_to_hex(commit->object.sha1));
232 author_end = strchrnul(author, '\n');
233 committer = strstr(author_end, "\ncommitter ");
235 die ("Could not find committer in commit %s",
236 sha1_to_hex(commit->object.sha1));
238 committer_end = strchrnul(committer, '\n');
239 message = strstr(committer_end, "\n\n");
240 encoding = find_encoding(committer_end, message);
244 if (commit->parents &&
245 get_object_mark(&commit->parents->item->object) != 0 &&
247 parse_commit(commit->parents->item);
248 diff_tree_sha1(commit->parents->item->tree->object.sha1,
249 commit->tree->object.sha1, "", &rev->diffopt);
252 diff_root_tree_sha1(commit->tree->object.sha1,
255 /* Export the referenced blobs, and remember the marks. */
256 for (i = 0; i < diff_queued_diff.nr; i++)
257 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
258 handle_object(diff_queued_diff.queue[i]->two->sha1);
260 mark_next_object(&commit->object);
261 if (!is_encoding_utf8(encoding))
262 reencoded = reencode_string(message, "UTF-8", encoding);
263 if (!commit->parents)
264 printf("reset %s\n", (const char*)commit->util);
265 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
266 (const char *)commit->util, last_idnum,
267 (int)(author_end - author), author,
268 (int)(committer_end - committer), committer,
270 ? strlen(reencoded) : message
271 ? strlen(message) : 0),
272 reencoded ? reencoded : message ? message : "");
275 for (i = 0, p = commit->parents; p; p = p->next) {
276 int mark = get_object_mark(&p->item->object);
280 printf("from :%d\n", mark);
282 printf("merge :%d\n", mark);
287 printf("deleteall\n");
288 log_tree_diff_flush(rev);
289 rev->diffopt.output_format = saved_output_format;
296 static void handle_tail(struct object_array *commits, struct rev_info *revs)
298 struct commit *commit;
299 while (commits->nr) {
300 commit = (struct commit *)commits->objects[commits->nr - 1].item;
301 if (has_unshown_parent(commit))
303 handle_commit(commit, revs);
308 static void handle_tag(const char *name, struct tag *tag)
311 enum object_type type;
313 const char *tagger, *tagger_end, *message;
314 size_t message_size = 0;
315 struct object *tagged;
319 /* Trees have no identifer in fast-export output, thus we have no way
320 * to output tags of trees, tags of tags of trees, etc. Simply omit
323 tagged = tag->tagged;
324 while (tagged->type == OBJ_TAG) {
325 tagged = ((struct tag *)tagged)->tagged;
327 if (tagged->type == OBJ_TREE) {
328 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
329 sha1_to_hex(tag->object.sha1));
333 buf = read_sha1_file(tag->object.sha1, &type, &size);
335 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
336 message = memmem(buf, size, "\n\n", 2);
339 message_size = strlen(message);
341 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
343 if (fake_missing_tagger)
344 tagger = "tagger Unspecified Tagger "
345 "<unspecified-tagger> 0 +0000";
348 tagger_end = tagger + strlen(tagger);
351 tagger_end = strchrnul(tagger, '\n');
354 /* handle signed tags */
356 const char *signature = strstr(message,
357 "\n-----BEGIN PGP SIGNATURE-----\n");
359 switch(signed_tag_mode) {
361 die ("Encountered signed tag %s; use "
362 "--signed-tag=<mode> to handle it.",
363 sha1_to_hex(tag->object.sha1));
365 warning ("Exporting signed tag %s",
366 sha1_to_hex(tag->object.sha1));
371 message_size = signature + 1 - message;
376 /* handle tag->tagged having been filtered out due to paths specified */
377 tagged = tag->tagged;
378 tagged_mark = get_object_mark(tagged);
380 switch(tag_of_filtered_mode) {
382 die ("Tag %s tags unexported object; use "
383 "--tag-of-filtered-object=<mode> to handle it.",
384 sha1_to_hex(tag->object.sha1));
386 /* Ignore this tag altogether */
389 if (tagged->type != OBJ_COMMIT) {
390 die ("Tag %s tags unexported %s!",
391 sha1_to_hex(tag->object.sha1),
392 typename(tagged->type));
394 p = (struct commit *)tagged;
396 if (p->parents && p->parents->next)
398 if (p->object.flags & UNINTERESTING)
400 if (!(p->object.flags & TREESAME))
403 die ("Can't find replacement commit for tag %s\n",
404 sha1_to_hex(tag->object.sha1));
405 p = p->parents->item;
407 tagged_mark = get_object_mark(&p->object);
411 if (!prefixcmp(name, "refs/tags/"))
413 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
415 (int)(tagger_end - tagger), tagger,
416 tagger == tagger_end ? "" : "\n",
417 (int)message_size, (int)message_size, message ? message : "");
420 static void get_tags_and_duplicates(struct object_array *pending,
421 struct string_list *extra_refs)
426 for (i = 0; i < pending->nr; i++) {
427 struct object_array_entry *e = pending->objects + i;
428 unsigned char sha1[20];
429 struct commit *commit = commit;
432 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
435 switch (e->item->type) {
437 commit = (struct commit *)e->item;
440 tag = (struct tag *)e->item;
442 /* handle nested tags */
443 while (tag && tag->object.type == OBJ_TAG) {
444 parse_object(tag->object.sha1);
445 string_list_append(extra_refs, full_name)->util = tag;
446 tag = (struct tag *)tag->tagged;
449 die ("Tag %s points nowhere?", e->name);
450 switch(tag->object.type) {
452 commit = (struct commit *)tag;
455 handle_object(tag->object.sha1);
457 default: /* OBJ_TAG (nested tags) is already handled */
458 warning("Tag points to object of unexpected type %s, skipping.",
459 typename(tag->object.type));
464 warning("%s: Unexpected object of type %s, skipping.",
466 typename(e->item->type));
470 /* more than one name for the same object */
471 string_list_append(extra_refs, full_name)->util = commit;
473 commit->util = full_name;
477 static void handle_tags_and_duplicates(struct string_list *extra_refs)
479 struct commit *commit;
482 for (i = extra_refs->nr - 1; i >= 0; i--) {
483 const char *name = extra_refs->items[i].string;
484 struct object *object = extra_refs->items[i].util;
485 switch (object->type) {
487 handle_tag(name, (struct tag *)object);
490 /* create refs pointing to already seen commits */
491 commit = (struct commit *)object;
492 printf("reset %s\nfrom :%d\n\n", name,
493 get_object_mark(&commit->object));
500 static void export_marks(char *file)
504 struct object_decoration *deco = idnums.hash;
508 f = fopen(file, "w");
510 die_errno("Unable to open marks file %s for writing.", file);
512 for (i = 0; i < idnums.size; i++) {
513 if (deco->base && deco->base->type == 1) {
514 mark = ptr_to_mark(deco->decoration);
515 if (fprintf(f, ":%"PRIu32" %s\n", mark,
516 sha1_to_hex(deco->base->sha1)) < 0) {
527 error("Unable to write marks file %s.", file);
530 static void import_marks(char *input_file)
533 FILE *f = fopen(input_file, "r");
535 die_errno("cannot read '%s'", input_file);
537 while (fgets(line, sizeof(line), f)) {
539 char *line_end, *mark_end;
540 unsigned char sha1[20];
541 struct object *object;
543 line_end = strchr(line, '\n');
544 if (line[0] != ':' || !line_end)
545 die("corrupt mark line: %s", line);
548 mark = strtoumax(line + 1, &mark_end, 10);
549 if (!mark || mark_end == line + 1
550 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
551 die("corrupt mark line: %s", line);
553 object = parse_object(sha1);
555 die ("Could not read blob %s", sha1_to_hex(sha1));
557 if (object->flags & SHOWN)
558 error("Object %s already has a mark", sha1);
560 mark_object(object, mark);
561 if (last_idnum < mark)
564 object->flags |= SHOWN;
569 int cmd_fast_export(int argc, const char **argv, const char *prefix)
571 struct rev_info revs;
572 struct object_array commits = OBJECT_ARRAY_INIT;
573 struct string_list extra_refs = STRING_LIST_INIT_NODUP;
574 struct commit *commit;
575 char *export_filename = NULL, *import_filename = NULL;
576 struct option options[] = {
577 OPT_INTEGER(0, "progress", &progress,
578 "show progress after <n> objects"),
579 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
580 "select handling of signed tags",
581 parse_opt_signed_tag_mode),
582 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, "mode",
583 "select handling of tags that tag filtered objects",
584 parse_opt_tag_of_filtered_mode),
585 OPT_STRING(0, "export-marks", &export_filename, "FILE",
586 "Dump marks to this file"),
587 OPT_STRING(0, "import-marks", &import_filename, "FILE",
588 "Import marks from this file"),
589 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
590 "Fake a tagger when tags lack one"),
591 OPT_BOOLEAN(0, "full-tree", &full_tree,
592 "Output full tree for each commit"),
593 { OPTION_NEGBIT, 0, "data", &no_data, NULL,
594 "Skip output of blob data",
595 PARSE_OPT_NOARG | PARSE_OPT_NEGHELP, NULL, 1 },
600 usage_with_options (fast_export_usage, options);
602 /* we handle encodings */
603 git_config(git_default_config, NULL);
605 init_revisions(&revs, prefix);
607 revs.show_source = 1;
608 revs.rewrite_parents = 1;
609 argc = setup_revisions(argc, argv, &revs, NULL);
610 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
612 usage_with_options (fast_export_usage, options);
615 import_marks(import_filename);
617 if (import_filename && revs.prune_data)
620 get_tags_and_duplicates(&revs.pending, &extra_refs);
622 if (prepare_revision_walk(&revs))
623 die("revision walk setup failed");
624 revs.diffopt.format_callback = show_filemodify;
625 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
626 while ((commit = get_revision(&revs))) {
627 if (has_unshown_parent(commit)) {
628 add_object_array(&commit->object, NULL, &commits);
631 handle_commit(commit, &revs);
632 handle_tail(&commits, &revs);
636 handle_tags_and_duplicates(&extra_refs);
639 export_marks(export_filename);