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;
29 static int use_done_feature;
33 static int parse_opt_signed_tag_mode(const struct option *opt,
34 const char *arg, int unset)
36 if (unset || !strcmp(arg, "abort"))
37 signed_tag_mode = ABORT;
38 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
39 signed_tag_mode = VERBATIM;
40 else if (!strcmp(arg, "warn"))
41 signed_tag_mode = WARN;
42 else if (!strcmp(arg, "strip"))
43 signed_tag_mode = STRIP;
45 return error("Unknown signed-tag mode: %s", arg);
49 static int parse_opt_tag_of_filtered_mode(const struct option *opt,
50 const char *arg, int unset)
52 if (unset || !strcmp(arg, "abort"))
53 tag_of_filtered_mode = ABORT;
54 else if (!strcmp(arg, "drop"))
55 tag_of_filtered_mode = DROP;
56 else if (!strcmp(arg, "rewrite"))
57 tag_of_filtered_mode = REWRITE;
59 return error("Unknown tag-of-filtered mode: %s", arg);
63 static struct decoration idnums;
64 static uint32_t last_idnum;
66 static int has_unshown_parent(struct commit *commit)
68 struct commit_list *parent;
70 for (parent = commit->parents; parent; parent = parent->next)
71 if (!(parent->item->object.flags & SHOWN) &&
72 !(parent->item->object.flags & UNINTERESTING))
77 /* Since intptr_t is C99, we do not use it here */
78 static inline uint32_t *mark_to_ptr(uint32_t mark)
80 return ((uint32_t *)NULL) + mark;
83 static inline uint32_t ptr_to_mark(void * mark)
85 return (uint32_t *)mark - (uint32_t *)NULL;
88 static inline void mark_object(struct object *object, uint32_t mark)
90 add_decoration(&idnums, object, mark_to_ptr(mark));
93 static inline void mark_next_object(struct object *object)
95 mark_object(object, ++last_idnum);
98 static int get_object_mark(struct object *object)
100 void *decoration = lookup_decoration(&idnums, object);
103 return ptr_to_mark(decoration);
106 static void show_progress(void)
108 static int counter = 0;
111 if ((++counter % progress) == 0)
112 printf("progress %d objects\n", counter);
115 static void handle_object(const unsigned char *sha1)
118 enum object_type type;
120 struct object *object;
125 if (is_null_sha1(sha1))
128 object = parse_object(sha1);
130 die ("Could not read blob %s", sha1_to_hex(sha1));
132 if (object->flags & SHOWN)
135 buf = read_sha1_file(sha1, &type, &size);
137 die ("Could not read blob %s", sha1_to_hex(sha1));
139 mark_next_object(object);
141 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
142 if (size && fwrite(buf, size, 1, stdout) != 1)
143 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1));
148 object->flags |= SHOWN;
152 static int depth_first(const void *a_, const void *b_)
154 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
155 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
156 const char *name_a, *name_b;
157 int len_a, len_b, len;
160 name_a = a->one ? a->one->path : a->two->path;
161 name_b = b->one ? b->one->path : b->two->path;
163 len_a = strlen(name_a);
164 len_b = strlen(name_b);
165 len = (len_a < len_b) ? len_a : len_b;
167 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
168 cmp = memcmp(name_a, name_b, len);
175 * Move 'R'ename entries last so that all references of the file
176 * appear in the output before it is renamed (e.g., when a file
177 * was copied and renamed in the same commit).
179 return (a->status == 'R') - (b->status == 'R');
182 static void show_filemodify(struct diff_queue_struct *q,
183 struct diff_options *options, void *data)
188 * Handle files below a directory first, in case they are all deleted
189 * and the directory changes to a file or symlink.
191 qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
193 for (i = 0; i < q->nr; i++) {
194 struct diff_filespec *ospec = q->queue[i]->one;
195 struct diff_filespec *spec = q->queue[i]->two;
197 switch (q->queue[i]->status) {
198 case DIFF_STATUS_DELETED:
199 printf("D %s\n", spec->path);
202 case DIFF_STATUS_COPIED:
203 case DIFF_STATUS_RENAMED:
204 printf("%c \"%s\" \"%s\"\n", q->queue[i]->status,
205 ospec->path, spec->path);
207 if (!hashcmp(ospec->sha1, spec->sha1) &&
208 ospec->mode == spec->mode)
212 case DIFF_STATUS_TYPE_CHANGED:
213 case DIFF_STATUS_MODIFIED:
214 case DIFF_STATUS_ADDED:
216 * Links refer to objects in another repositories;
217 * output the SHA-1 verbatim.
219 if (no_data || S_ISGITLINK(spec->mode))
220 printf("M %06o %s %s\n", spec->mode,
221 sha1_to_hex(spec->sha1), spec->path);
223 struct object *object = lookup_object(spec->sha1);
224 printf("M %06o :%d %s\n", spec->mode,
225 get_object_mark(object), spec->path);
230 die("Unexpected comparison status '%c' for %s, %s",
232 ospec->path ? ospec->path : "none",
233 spec->path ? spec->path : "none");
238 static const char *find_encoding(const char *begin, const char *end)
240 const char *needle = "\nencoding ";
243 bol = memmem(begin, end ? end - begin : strlen(begin),
244 needle, strlen(needle));
246 return git_commit_encoding;
247 bol += strlen(needle);
248 eol = strchrnul(bol, '\n');
253 static void handle_commit(struct commit *commit, struct rev_info *rev)
255 int saved_output_format = rev->diffopt.output_format;
256 const char *author, *author_end, *committer, *committer_end;
257 const char *encoding, *message;
258 char *reencoded = NULL;
259 struct commit_list *p;
262 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
264 parse_commit(commit);
265 author = strstr(commit->buffer, "\nauthor ");
267 die ("Could not find author in commit %s",
268 sha1_to_hex(commit->object.sha1));
270 author_end = strchrnul(author, '\n');
271 committer = strstr(author_end, "\ncommitter ");
273 die ("Could not find committer in commit %s",
274 sha1_to_hex(commit->object.sha1));
276 committer_end = strchrnul(committer, '\n');
277 message = strstr(committer_end, "\n\n");
278 encoding = find_encoding(committer_end, message);
282 if (commit->parents &&
283 get_object_mark(&commit->parents->item->object) != 0 &&
285 parse_commit(commit->parents->item);
286 diff_tree_sha1(commit->parents->item->tree->object.sha1,
287 commit->tree->object.sha1, "", &rev->diffopt);
290 diff_root_tree_sha1(commit->tree->object.sha1,
293 /* Export the referenced blobs, and remember the marks. */
294 for (i = 0; i < diff_queued_diff.nr; i++)
295 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
296 handle_object(diff_queued_diff.queue[i]->two->sha1);
298 mark_next_object(&commit->object);
299 if (!is_encoding_utf8(encoding))
300 reencoded = reencode_string(message, "UTF-8", encoding);
301 if (!commit->parents)
302 printf("reset %s\n", (const char*)commit->util);
303 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
304 (const char *)commit->util, last_idnum,
305 (int)(author_end - author), author,
306 (int)(committer_end - committer), committer,
308 ? strlen(reencoded) : message
309 ? strlen(message) : 0),
310 reencoded ? reencoded : message ? message : "");
313 for (i = 0, p = commit->parents; p; p = p->next) {
314 int mark = get_object_mark(&p->item->object);
318 printf("from :%d\n", mark);
320 printf("merge :%d\n", mark);
325 printf("deleteall\n");
326 log_tree_diff_flush(rev);
327 rev->diffopt.output_format = saved_output_format;
334 static void handle_tail(struct object_array *commits, struct rev_info *revs)
336 struct commit *commit;
337 while (commits->nr) {
338 commit = (struct commit *)commits->objects[commits->nr - 1].item;
339 if (has_unshown_parent(commit))
341 handle_commit(commit, revs);
346 static void handle_tag(const char *name, struct tag *tag)
349 enum object_type type;
351 const char *tagger, *tagger_end, *message;
352 size_t message_size = 0;
353 struct object *tagged;
357 /* Trees have no identifer in fast-export output, thus we have no way
358 * to output tags of trees, tags of tags of trees, etc. Simply omit
361 tagged = tag->tagged;
362 while (tagged->type == OBJ_TAG) {
363 tagged = ((struct tag *)tagged)->tagged;
365 if (tagged->type == OBJ_TREE) {
366 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
367 sha1_to_hex(tag->object.sha1));
371 buf = read_sha1_file(tag->object.sha1, &type, &size);
373 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
374 message = memmem(buf, size, "\n\n", 2);
377 message_size = strlen(message);
379 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
381 if (fake_missing_tagger)
382 tagger = "tagger Unspecified Tagger "
383 "<unspecified-tagger> 0 +0000";
386 tagger_end = tagger + strlen(tagger);
389 tagger_end = strchrnul(tagger, '\n');
392 /* handle signed tags */
394 const char *signature = strstr(message,
395 "\n-----BEGIN PGP SIGNATURE-----\n");
397 switch(signed_tag_mode) {
399 die ("Encountered signed tag %s; use "
400 "--signed-tag=<mode> to handle it.",
401 sha1_to_hex(tag->object.sha1));
403 warning ("Exporting signed tag %s",
404 sha1_to_hex(tag->object.sha1));
409 message_size = signature + 1 - message;
414 /* handle tag->tagged having been filtered out due to paths specified */
415 tagged = tag->tagged;
416 tagged_mark = get_object_mark(tagged);
418 switch(tag_of_filtered_mode) {
420 die ("Tag %s tags unexported object; use "
421 "--tag-of-filtered-object=<mode> to handle it.",
422 sha1_to_hex(tag->object.sha1));
424 /* Ignore this tag altogether */
427 if (tagged->type != OBJ_COMMIT) {
428 die ("Tag %s tags unexported %s!",
429 sha1_to_hex(tag->object.sha1),
430 typename(tagged->type));
432 p = (struct commit *)tagged;
434 if (p->parents && p->parents->next)
436 if (p->object.flags & UNINTERESTING)
438 if (!(p->object.flags & TREESAME))
441 die ("Can't find replacement commit for tag %s\n",
442 sha1_to_hex(tag->object.sha1));
443 p = p->parents->item;
445 tagged_mark = get_object_mark(&p->object);
449 if (!prefixcmp(name, "refs/tags/"))
451 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
453 (int)(tagger_end - tagger), tagger,
454 tagger == tagger_end ? "" : "\n",
455 (int)message_size, (int)message_size, message ? message : "");
458 static void get_tags_and_duplicates(struct object_array *pending,
459 struct string_list *extra_refs)
464 for (i = 0; i < pending->nr; i++) {
465 struct object_array_entry *e = pending->objects + i;
466 unsigned char sha1[20];
467 struct commit *commit = commit;
470 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
473 switch (e->item->type) {
475 commit = (struct commit *)e->item;
478 tag = (struct tag *)e->item;
480 /* handle nested tags */
481 while (tag && tag->object.type == OBJ_TAG) {
482 parse_object(tag->object.sha1);
483 string_list_append(extra_refs, full_name)->util = tag;
484 tag = (struct tag *)tag->tagged;
487 die ("Tag %s points nowhere?", e->name);
488 switch(tag->object.type) {
490 commit = (struct commit *)tag;
493 handle_object(tag->object.sha1);
495 default: /* OBJ_TAG (nested tags) is already handled */
496 warning("Tag points to object of unexpected type %s, skipping.",
497 typename(tag->object.type));
502 warning("%s: Unexpected object of type %s, skipping.",
504 typename(e->item->type));
508 /* more than one name for the same object */
509 string_list_append(extra_refs, full_name)->util = commit;
511 commit->util = full_name;
515 static void handle_tags_and_duplicates(struct string_list *extra_refs)
517 struct commit *commit;
520 for (i = extra_refs->nr - 1; i >= 0; i--) {
521 const char *name = extra_refs->items[i].string;
522 struct object *object = extra_refs->items[i].util;
523 switch (object->type) {
525 handle_tag(name, (struct tag *)object);
528 /* create refs pointing to already seen commits */
529 commit = (struct commit *)object;
530 printf("reset %s\nfrom :%d\n\n", name,
531 get_object_mark(&commit->object));
538 static void export_marks(char *file)
542 struct object_decoration *deco = idnums.hash;
546 f = fopen(file, "w");
548 die_errno("Unable to open marks file %s for writing.", file);
550 for (i = 0; i < idnums.size; i++) {
551 if (deco->base && deco->base->type == 1) {
552 mark = ptr_to_mark(deco->decoration);
553 if (fprintf(f, ":%"PRIu32" %s\n", mark,
554 sha1_to_hex(deco->base->sha1)) < 0) {
565 error("Unable to write marks file %s.", file);
568 static void import_marks(char *input_file)
571 FILE *f = fopen(input_file, "r");
573 die_errno("cannot read '%s'", input_file);
575 while (fgets(line, sizeof(line), f)) {
577 char *line_end, *mark_end;
578 unsigned char sha1[20];
579 struct object *object;
581 line_end = strchr(line, '\n');
582 if (line[0] != ':' || !line_end)
583 die("corrupt mark line: %s", line);
586 mark = strtoumax(line + 1, &mark_end, 10);
587 if (!mark || mark_end == line + 1
588 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
589 die("corrupt mark line: %s", line);
591 object = parse_object(sha1);
593 die ("Could not read blob %s", sha1_to_hex(sha1));
595 if (object->flags & SHOWN)
596 error("Object %s already has a mark", sha1);
598 mark_object(object, mark);
599 if (last_idnum < mark)
602 object->flags |= SHOWN;
607 int cmd_fast_export(int argc, const char **argv, const char *prefix)
609 struct rev_info revs;
610 struct object_array commits = OBJECT_ARRAY_INIT;
611 struct string_list extra_refs = STRING_LIST_INIT_NODUP;
612 struct commit *commit;
613 char *export_filename = NULL, *import_filename = NULL;
614 struct option options[] = {
615 OPT_INTEGER(0, "progress", &progress,
616 "show progress after <n> objects"),
617 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
618 "select handling of signed tags",
619 parse_opt_signed_tag_mode),
620 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, "mode",
621 "select handling of tags that tag filtered objects",
622 parse_opt_tag_of_filtered_mode),
623 OPT_STRING(0, "export-marks", &export_filename, "file",
624 "Dump marks to this file"),
625 OPT_STRING(0, "import-marks", &import_filename, "file",
626 "Import marks from this file"),
627 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
628 "Fake a tagger when tags lack one"),
629 OPT_BOOLEAN(0, "full-tree", &full_tree,
630 "Output full tree for each commit"),
631 OPT_BOOLEAN(0, "use-done-feature", &use_done_feature,
632 "Use the done feature to terminate the stream"),
633 { OPTION_NEGBIT, 0, "data", &no_data, NULL,
634 "Skip output of blob data",
635 PARSE_OPT_NOARG | PARSE_OPT_NEGHELP, NULL, 1 },
640 usage_with_options (fast_export_usage, options);
642 /* we handle encodings */
643 git_config(git_default_config, NULL);
645 init_revisions(&revs, prefix);
647 revs.show_source = 1;
648 revs.rewrite_parents = 1;
649 argc = setup_revisions(argc, argv, &revs, NULL);
650 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
652 usage_with_options (fast_export_usage, options);
654 if (use_done_feature)
655 printf("feature done\n");
658 import_marks(import_filename);
660 if (import_filename && revs.prune_data.nr)
663 get_tags_and_duplicates(&revs.pending, &extra_refs);
665 if (prepare_revision_walk(&revs))
666 die("revision walk setup failed");
667 revs.diffopt.format_callback = show_filemodify;
668 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
669 while ((commit = get_revision(&revs))) {
670 if (has_unshown_parent(commit)) {
671 add_object_array(&commit->object, NULL, &commits);
674 handle_commit(commit, &revs);
675 handle_tail(&commits, &revs);
679 handle_tags_and_duplicates(&extra_refs);
682 export_marks(export_filename);
684 if (use_done_feature)