2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
11 #include "object-store.h"
20 #include "string-list.h"
22 #include "parse-options.h"
26 #include "commit-slab.h"
28 static const char *fast_export_usage[] = {
29 N_("git fast-export [rev-list-opts]"),
34 static enum { SIGNED_TAG_ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = SIGNED_TAG_ABORT;
35 static enum { TAG_FILTERING_ABORT, DROP, REWRITE } tag_of_filtered_mode = TAG_FILTERING_ABORT;
36 static enum { REENCODE_ABORT, REENCODE_YES, REENCODE_NO } reencode_mode = REENCODE_ABORT;
37 static int fake_missing_tagger;
38 static int use_done_feature;
41 static int reference_excluded_commits;
42 static int show_original_ids;
44 static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
45 static struct string_list tag_refs = STRING_LIST_INIT_NODUP;
46 static struct refspec refspecs = REFSPEC_INIT_FETCH;
48 static struct hashmap anonymized_seeds;
49 static struct revision_sources revision_sources;
51 static int parse_opt_signed_tag_mode(const struct option *opt,
52 const char *arg, int unset)
54 if (unset || !strcmp(arg, "abort"))
55 signed_tag_mode = SIGNED_TAG_ABORT;
56 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
57 signed_tag_mode = VERBATIM;
58 else if (!strcmp(arg, "warn"))
59 signed_tag_mode = WARN;
60 else if (!strcmp(arg, "warn-strip"))
61 signed_tag_mode = WARN_STRIP;
62 else if (!strcmp(arg, "strip"))
63 signed_tag_mode = STRIP;
65 return error("Unknown signed-tags mode: %s", arg);
69 static int parse_opt_tag_of_filtered_mode(const struct option *opt,
70 const char *arg, int unset)
72 if (unset || !strcmp(arg, "abort"))
73 tag_of_filtered_mode = TAG_FILTERING_ABORT;
74 else if (!strcmp(arg, "drop"))
75 tag_of_filtered_mode = DROP;
76 else if (!strcmp(arg, "rewrite"))
77 tag_of_filtered_mode = REWRITE;
79 return error("Unknown tag-of-filtered mode: %s", arg);
83 static int parse_opt_reencode_mode(const struct option *opt,
84 const char *arg, int unset)
87 reencode_mode = REENCODE_ABORT;
91 switch (git_parse_maybe_bool(arg)) {
93 reencode_mode = REENCODE_NO;
96 reencode_mode = REENCODE_YES;
99 if (!strcasecmp(arg, "abort"))
100 reencode_mode = REENCODE_ABORT;
102 return error("Unknown reencoding mode: %s", arg);
108 static struct decoration idnums;
109 static uint32_t last_idnum;
111 static int has_unshown_parent(struct commit *commit)
113 struct commit_list *parent;
115 for (parent = commit->parents; parent; parent = parent->next)
116 if (!(parent->item->object.flags & SHOWN) &&
117 !(parent->item->object.flags & UNINTERESTING))
122 struct anonymized_entry {
123 struct hashmap_entry hash;
125 const char orig[FLEX_ARRAY];
128 struct anonymized_entry_key {
129 struct hashmap_entry hash;
134 static int anonymized_entry_cmp(const void *unused_cmp_data,
135 const struct hashmap_entry *eptr,
136 const struct hashmap_entry *entry_or_key,
139 const struct anonymized_entry *a, *b;
141 a = container_of(eptr, const struct anonymized_entry, hash);
143 const struct anonymized_entry_key *key = keydata;
144 int equal = !strncmp(a->orig, key->orig, key->orig_len) &&
145 !a->orig[key->orig_len];
149 b = container_of(entry_or_key, const struct anonymized_entry, hash);
150 return strcmp(a->orig, b->orig);
154 * Basically keep a cache of X->Y so that we can repeatedly replace
155 * the same anonymized string with another. The actual generation
156 * is farmed out to the generate function.
158 static const char *anonymize_str(struct hashmap *map,
159 char *(*generate)(void *),
160 const char *orig, size_t len,
163 struct anonymized_entry_key key;
164 struct anonymized_entry *ret;
167 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
169 hashmap_entry_init(&key.hash, memhash(orig, len));
173 /* First check if it's a token the user configured manually... */
174 if (anonymized_seeds.cmpfn)
175 ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
179 /* ...otherwise check if we've already seen it in this context... */
181 ret = hashmap_get_entry(map, &key, hash, &key);
183 /* ...and finally generate a new mapping if necessary */
185 FLEX_ALLOC_MEM(ret, orig, orig, len);
186 hashmap_entry_init(&ret->hash, key.hash.hash);
187 ret->anon = generate(data);
188 hashmap_put(map, &ret->hash);
195 * We anonymize each component of a path individually,
196 * so that paths a/b and a/c will share a common root.
197 * The paths are cached via anonymize_mem so that repeated
198 * lookups for "a" will yield the same value.
200 static void anonymize_path(struct strbuf *out, const char *path,
202 char *(*generate)(void *))
205 const char *end_of_component = strchrnul(path, '/');
206 size_t len = end_of_component - path;
207 const char *c = anonymize_str(map, generate, path, len, NULL);
208 strbuf_addstr(out, c);
209 path = end_of_component;
211 strbuf_addch(out, *path++);
215 static inline void *mark_to_ptr(uint32_t mark)
217 return (void *)(uintptr_t)mark;
220 static inline uint32_t ptr_to_mark(void * mark)
222 return (uint32_t)(uintptr_t)mark;
225 static inline void mark_object(struct object *object, uint32_t mark)
227 add_decoration(&idnums, object, mark_to_ptr(mark));
230 static inline void mark_next_object(struct object *object)
232 mark_object(object, ++last_idnum);
235 static int get_object_mark(struct object *object)
237 void *decoration = lookup_decoration(&idnums, object);
240 return ptr_to_mark(decoration);
243 static struct commit *rewrite_commit(struct commit *p)
246 if (p->parents && p->parents->next)
248 if (p->object.flags & UNINTERESTING)
250 if (!(p->object.flags & TREESAME))
254 p = p->parents->item;
259 static void show_progress(void)
261 static int counter = 0;
264 if ((++counter % progress) == 0)
265 printf("progress %d objects\n", counter);
269 * Ideally we would want some transformation of the blob data here
270 * that is unreversible, but would still be the same size and have
271 * the same data relationship to other blobs (so that we get the same
272 * delta and packing behavior as the original). But the first and last
273 * requirements there are probably mutually exclusive, so let's take
274 * the easy way out for now, and just generate arbitrary content.
276 * There's no need to cache this result with anonymize_mem, since
277 * we already handle blob content caching with marks.
279 static char *anonymize_blob(unsigned long *size)
282 struct strbuf out = STRBUF_INIT;
283 strbuf_addf(&out, "anonymous blob %d", counter++);
285 return strbuf_detach(&out, NULL);
288 static void export_blob(const struct object_id *oid)
291 enum object_type type;
293 struct object *object;
299 if (is_null_oid(oid))
302 object = lookup_object(the_repository, oid);
303 if (object && object->flags & SHOWN)
307 buf = anonymize_blob(&size);
308 object = (struct object *)lookup_blob(the_repository, oid);
311 buf = read_object_file(oid, &type, &size);
313 die("could not read blob %s", oid_to_hex(oid));
314 if (check_object_signature(the_repository, oid, buf, size,
315 type_name(type)) < 0)
316 die("oid mismatch in blob %s", oid_to_hex(oid));
317 object = parse_object_buffer(the_repository, oid, type,
322 die("Could not read blob %s", oid_to_hex(oid));
324 mark_next_object(object);
326 printf("blob\nmark :%"PRIu32"\n", last_idnum);
327 if (show_original_ids)
328 printf("original-oid %s\n", oid_to_hex(oid));
329 printf("data %"PRIuMAX"\n", (uintmax_t)size);
330 if (size && fwrite(buf, size, 1, stdout) != 1)
331 die_errno("could not write blob '%s'", oid_to_hex(oid));
336 object->flags |= SHOWN;
341 static int depth_first(const void *a_, const void *b_)
343 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
344 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
345 const char *name_a, *name_b;
346 int len_a, len_b, len;
349 name_a = a->one ? a->one->path : a->two->path;
350 name_b = b->one ? b->one->path : b->two->path;
352 len_a = strlen(name_a);
353 len_b = strlen(name_b);
354 len = (len_a < len_b) ? len_a : len_b;
356 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
357 cmp = memcmp(name_a, name_b, len);
364 * Move 'R'ename entries last so that all references of the file
365 * appear in the output before it is renamed (e.g., when a file
366 * was copied and renamed in the same commit).
368 return (a->status == 'R') - (b->status == 'R');
371 static void print_path_1(const char *path)
373 int need_quote = quote_c_style(path, NULL, NULL, 0);
375 quote_c_style(path, NULL, stdout, 0);
376 else if (strchr(path, ' '))
377 printf("\"%s\"", path);
382 static char *anonymize_path_component(void *data)
385 struct strbuf out = STRBUF_INIT;
386 strbuf_addf(&out, "path%d", counter++);
387 return strbuf_detach(&out, NULL);
390 static void print_path(const char *path)
395 static struct hashmap paths;
396 static struct strbuf anon = STRBUF_INIT;
398 anonymize_path(&anon, path, &paths, anonymize_path_component);
399 print_path_1(anon.buf);
404 static char *generate_fake_oid(void *data)
406 static uint32_t counter = 1; /* avoid null oid */
407 const unsigned hashsz = the_hash_algo->rawsz;
408 struct object_id oid;
409 char *hex = xmallocz(GIT_MAX_HEXSZ);
412 put_be32(oid.hash + hashsz - 4, counter++);
413 return oid_to_hex_r(hex, &oid);
416 static const char *anonymize_oid(const char *oid_hex)
418 static struct hashmap objs;
419 size_t len = strlen(oid_hex);
420 return anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);
423 static void show_filemodify(struct diff_queue_struct *q,
424 struct diff_options *options, void *data)
427 struct string_list *changed = data;
430 * Handle files below a directory first, in case they are all deleted
431 * and the directory changes to a file or symlink.
433 QSORT(q->queue, q->nr, depth_first);
435 for (i = 0; i < q->nr; i++) {
436 struct diff_filespec *ospec = q->queue[i]->one;
437 struct diff_filespec *spec = q->queue[i]->two;
439 switch (q->queue[i]->status) {
440 case DIFF_STATUS_DELETED:
442 print_path(spec->path);
443 string_list_insert(changed, spec->path);
447 case DIFF_STATUS_COPIED:
448 case DIFF_STATUS_RENAMED:
450 * If a change in the file corresponding to ospec->path
451 * has been observed, we cannot trust its contents
452 * because the diff is calculated based on the prior
453 * contents, not the current contents. So, declare a
454 * copy or rename only if there was no change observed.
456 if (!string_list_has_string(changed, ospec->path)) {
457 printf("%c ", q->queue[i]->status);
458 print_path(ospec->path);
460 print_path(spec->path);
461 string_list_insert(changed, spec->path);
464 if (oideq(&ospec->oid, &spec->oid) &&
465 ospec->mode == spec->mode)
470 case DIFF_STATUS_TYPE_CHANGED:
471 case DIFF_STATUS_MODIFIED:
472 case DIFF_STATUS_ADDED:
474 * Links refer to objects in another repositories;
475 * output the SHA-1 verbatim.
477 if (no_data || S_ISGITLINK(spec->mode))
478 printf("M %06o %s ", spec->mode,
480 anonymize_oid(oid_to_hex(&spec->oid)) :
481 oid_to_hex(&spec->oid));
483 struct object *object = lookup_object(the_repository,
485 printf("M %06o :%d ", spec->mode,
486 get_object_mark(object));
488 print_path(spec->path);
489 string_list_insert(changed, spec->path);
494 die("Unexpected comparison status '%c' for %s, %s",
496 ospec->path ? ospec->path : "none",
497 spec->path ? spec->path : "none");
502 static const char *find_encoding(const char *begin, const char *end)
504 const char *needle = "\nencoding ";
507 bol = memmem(begin, end ? end - begin : strlen(begin),
508 needle, strlen(needle));
511 bol += strlen(needle);
512 eol = strchrnul(bol, '\n');
517 static char *anonymize_ref_component(void *data)
520 struct strbuf out = STRBUF_INIT;
521 strbuf_addf(&out, "ref%d", counter++);
522 return strbuf_detach(&out, NULL);
525 static const char *anonymize_refname(const char *refname)
528 * If any of these prefixes is found, we will leave it intact
529 * so that tags remain tags and so forth.
531 static const char *prefixes[] = {
537 static struct hashmap refs;
538 static struct strbuf anon = STRBUF_INIT;
542 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
543 if (skip_prefix(refname, prefixes[i], &refname)) {
544 strbuf_addstr(&anon, prefixes[i]);
549 anonymize_path(&anon, refname, &refs, anonymize_ref_component);
554 * We do not even bother to cache commit messages, as they are unlikely
555 * to be repeated verbatim, and it is not that interesting when they are.
557 static char *anonymize_commit_message(const char *old)
560 return xstrfmt("subject %d\n\nbody\n", counter++);
563 static char *anonymize_ident(void *data)
566 struct strbuf out = STRBUF_INIT;
567 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
569 return strbuf_detach(&out, NULL);
573 * Our strategy here is to anonymize the names and email addresses,
574 * but keep timestamps intact, as they influence things like traversal
575 * order (and by themselves should not be too revealing).
577 static void anonymize_ident_line(const char **beg, const char **end)
579 static struct hashmap idents;
580 static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT };
581 static unsigned which_buffer;
584 struct ident_split split;
585 const char *end_of_header;
587 out = &buffers[which_buffer++];
588 which_buffer %= ARRAY_SIZE(buffers);
591 /* skip "committer", "author", "tagger", etc */
592 end_of_header = strchr(*beg, ' ');
594 BUG("malformed line fed to anonymize_ident_line: %.*s",
595 (int)(*end - *beg), *beg);
597 strbuf_add(out, *beg, end_of_header - *beg);
599 if (!split_ident_line(&split, end_of_header, *end - end_of_header) &&
604 len = split.mail_end - split.name_begin;
605 ident = anonymize_str(&idents, anonymize_ident,
606 split.name_begin, len, NULL);
607 strbuf_addstr(out, ident);
608 strbuf_addch(out, ' ');
609 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
611 strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000");
615 *end = out->buf + out->len;
618 static void handle_commit(struct commit *commit, struct rev_info *rev,
619 struct string_list *paths_of_changed_objects)
621 int saved_output_format = rev->diffopt.output_format;
622 const char *commit_buffer;
623 const char *author, *author_end, *committer, *committer_end;
624 const char *encoding, *message;
625 char *reencoded = NULL;
626 struct commit_list *p;
630 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
632 parse_commit_or_die(commit);
633 commit_buffer = get_commit_buffer(commit, NULL);
634 author = strstr(commit_buffer, "\nauthor ");
636 die("could not find author in commit %s",
637 oid_to_hex(&commit->object.oid));
639 author_end = strchrnul(author, '\n');
640 committer = strstr(author_end, "\ncommitter ");
642 die("could not find committer in commit %s",
643 oid_to_hex(&commit->object.oid));
645 committer_end = strchrnul(committer, '\n');
646 message = strstr(committer_end, "\n\n");
647 encoding = find_encoding(committer_end, message);
651 if (commit->parents &&
652 (get_object_mark(&commit->parents->item->object) != 0 ||
653 reference_excluded_commits) &&
655 parse_commit_or_die(commit->parents->item);
656 diff_tree_oid(get_commit_tree_oid(commit->parents->item),
657 get_commit_tree_oid(commit), "", &rev->diffopt);
660 diff_root_tree_oid(get_commit_tree_oid(commit),
663 /* Export the referenced blobs, and remember the marks. */
664 for (i = 0; i < diff_queued_diff.nr; i++)
665 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
666 export_blob(&diff_queued_diff.queue[i]->two->oid);
668 refname = *revision_sources_at(&revision_sources, commit);
670 * FIXME: string_list_remove() below for each ref is overall
671 * O(N^2). Compared to a history walk and diffing trees, this is
672 * just lost in the noise in practice. However, theoretically a
673 * repo may have enough refs for this to become slow.
675 string_list_remove(&extra_refs, refname, 0);
677 refname = anonymize_refname(refname);
678 anonymize_ident_line(&committer, &committer_end);
679 anonymize_ident_line(&author, &author_end);
682 mark_next_object(&commit->object);
684 reencoded = anonymize_commit_message(message);
685 } else if (encoding) {
686 switch(reencode_mode) {
688 reencoded = reencode_string(message, "UTF-8", encoding);
693 die("Encountered commit-specific encoding %s in commit "
694 "%s; use --reencode=[yes|no] to handle it",
695 encoding, oid_to_hex(&commit->object.oid));
698 if (!commit->parents)
699 printf("reset %s\n", refname);
700 printf("commit %s\nmark :%"PRIu32"\n", refname, last_idnum);
701 if (show_original_ids)
702 printf("original-oid %s\n", oid_to_hex(&commit->object.oid));
703 printf("%.*s\n%.*s\n",
704 (int)(author_end - author), author,
705 (int)(committer_end - committer), committer);
706 if (!reencoded && encoding)
707 printf("encoding %s\n", encoding);
708 printf("data %u\n%s",
710 ? strlen(reencoded) : message
711 ? strlen(message) : 0),
712 reencoded ? reencoded : message ? message : "");
714 unuse_commit_buffer(commit, commit_buffer);
716 for (i = 0, p = commit->parents; p; p = p->next) {
717 struct object *obj = &p->item->object;
718 int mark = get_object_mark(obj);
720 if (!mark && !reference_excluded_commits)
727 printf(":%d\n", mark);
731 anonymize_oid(oid_to_hex(&obj->oid)) :
732 oid_to_hex(&obj->oid));
737 printf("deleteall\n");
738 log_tree_diff_flush(rev);
739 string_list_clear(paths_of_changed_objects, 0);
740 rev->diffopt.output_format = saved_output_format;
747 static char *anonymize_tag(void *data)
750 struct strbuf out = STRBUF_INIT;
751 strbuf_addf(&out, "tag message %d", counter++);
752 return strbuf_detach(&out, NULL);
755 static void handle_tail(struct object_array *commits, struct rev_info *revs,
756 struct string_list *paths_of_changed_objects)
758 struct commit *commit;
759 while (commits->nr) {
760 commit = (struct commit *)object_array_pop(commits);
761 if (has_unshown_parent(commit)) {
762 /* Queue again, to be handled later */
763 add_object_array(&commit->object, NULL, commits);
766 handle_commit(commit, revs, paths_of_changed_objects);
770 static void handle_tag(const char *name, struct tag *tag)
773 enum object_type type;
775 const char *tagger, *tagger_end, *message;
776 size_t message_size = 0;
777 struct object *tagged;
781 /* Trees have no identifier in fast-export output, thus we have no way
782 * to output tags of trees, tags of tags of trees, etc. Simply omit
785 tagged = tag->tagged;
786 while (tagged->type == OBJ_TAG) {
787 tagged = ((struct tag *)tagged)->tagged;
789 if (tagged->type == OBJ_TREE) {
790 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
791 oid_to_hex(&tag->object.oid));
795 buf = read_object_file(&tag->object.oid, &type, &size);
797 die("could not read tag %s", oid_to_hex(&tag->object.oid));
798 message = memmem(buf, size, "\n\n", 2);
801 message_size = strlen(message);
803 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
805 if (fake_missing_tagger)
806 tagger = "tagger Unspecified Tagger "
807 "<unspecified-tagger> 0 +0000";
810 tagger_end = tagger + strlen(tagger);
813 tagger_end = strchrnul(tagger, '\n');
815 anonymize_ident_line(&tagger, &tagger_end);
819 name = anonymize_refname(name);
821 static struct hashmap tags;
822 message = anonymize_str(&tags, anonymize_tag,
823 message, message_size, NULL);
827 /* handle signed tags */
829 const char *signature = strstr(message,
830 "\n-----BEGIN PGP SIGNATURE-----\n");
832 switch(signed_tag_mode) {
833 case SIGNED_TAG_ABORT:
834 die("encountered signed tag %s; use "
835 "--signed-tags=<mode> to handle it",
836 oid_to_hex(&tag->object.oid));
838 warning("exporting signed tag %s",
839 oid_to_hex(&tag->object.oid));
844 warning("stripping signature from tag %s",
845 oid_to_hex(&tag->object.oid));
848 message_size = signature + 1 - message;
853 /* handle tag->tagged having been filtered out due to paths specified */
854 tagged = tag->tagged;
855 tagged_mark = get_object_mark(tagged);
857 switch(tag_of_filtered_mode) {
858 case TAG_FILTERING_ABORT:
859 die("tag %s tags unexported object; use "
860 "--tag-of-filtered-object=<mode> to handle it",
861 oid_to_hex(&tag->object.oid));
863 /* Ignore this tag altogether */
867 if (tagged->type == OBJ_TAG && !mark_tags) {
868 die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
869 } else if (tagged->type == OBJ_COMMIT) {
870 p = rewrite_commit((struct commit *)tagged);
872 printf("reset %s\nfrom %s\n\n",
873 name, oid_to_hex(&null_oid));
877 tagged_mark = get_object_mark(&p->object);
879 /* tagged->type is either OBJ_BLOB or OBJ_TAG */
880 tagged_mark = get_object_mark(tagged);
885 if (tagged->type == OBJ_TAG) {
886 printf("reset %s\nfrom %s\n\n",
887 name, oid_to_hex(&null_oid));
889 skip_prefix(name, "refs/tags/", &name);
890 printf("tag %s\n", name);
892 mark_next_object(&tag->object);
893 printf("mark :%"PRIu32"\n", last_idnum);
896 printf("from :%d\n", tagged_mark);
898 printf("from %s\n", oid_to_hex(&tagged->oid));
900 if (show_original_ids)
901 printf("original-oid %s\n", oid_to_hex(&tag->object.oid));
902 printf("%.*s%sdata %d\n%.*s\n",
903 (int)(tagger_end - tagger), tagger,
904 tagger == tagger_end ? "" : "\n",
905 (int)message_size, (int)message_size, message ? message : "");
909 static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
911 switch (e->item->type) {
913 return (struct commit *)e->item;
915 struct tag *tag = (struct tag *)e->item;
917 /* handle nested tags */
918 while (tag && tag->object.type == OBJ_TAG) {
919 parse_object(the_repository, &tag->object.oid);
920 string_list_append(&tag_refs, full_name)->util = tag;
921 tag = (struct tag *)tag->tagged;
924 die("Tag %s points nowhere?", e->name);
925 return (struct commit *)tag;
933 static void get_tags_and_duplicates(struct rev_cmdline_info *info)
937 for (i = 0; i < info->nr; i++) {
938 struct rev_cmdline_entry *e = info->rev + i;
939 struct object_id oid;
940 struct commit *commit;
943 if (e->flags & UNINTERESTING)
946 if (dwim_ref(e->name, strlen(e->name), &oid, &full_name, 0) != 1)
951 private = apply_refspecs(&refspecs, full_name);
958 commit = get_commit(e, full_name);
960 warning("%s: Unexpected object of type %s, skipping.",
962 type_name(e->item->type));
966 switch(commit->object.type) {
970 export_blob(&commit->object.oid);
972 default: /* OBJ_TAG (nested tags) is already handled */
973 warning("Tag points to object of unexpected type %s, skipping.",
974 type_name(commit->object.type));
979 * Make sure this ref gets properly updated eventually, whether
980 * through a commit or manually at the end.
982 if (e->item->type != OBJ_TAG)
983 string_list_append(&extra_refs, full_name)->util = commit;
985 if (!*revision_sources_at(&revision_sources, commit))
986 *revision_sources_at(&revision_sources, commit) = full_name;
989 string_list_sort(&extra_refs);
990 string_list_remove_duplicates(&extra_refs, 0);
993 static void handle_tags_and_duplicates(struct string_list *extras)
995 struct commit *commit;
998 for (i = extras->nr - 1; i >= 0; i--) {
999 const char *name = extras->items[i].string;
1000 struct object *object = extras->items[i].util;
1003 switch (object->type) {
1005 handle_tag(name, (struct tag *)object);
1009 name = anonymize_refname(name);
1010 /* create refs pointing to already seen commits */
1011 commit = rewrite_commit((struct commit *)object);
1014 * Neither this object nor any of its
1015 * ancestors touch any relevant paths, so
1016 * it has been filtered to nothing. Delete
1019 printf("reset %s\nfrom %s\n\n",
1020 name, oid_to_hex(&null_oid));
1024 mark = get_object_mark(&commit->object);
1027 * Getting here means we have a commit which
1028 * was excluded by a negative refspec (e.g.
1029 * fast-export ^HEAD HEAD). If we are
1030 * referencing excluded commits, set the ref
1031 * to the exact commit. Otherwise, the user
1032 * wants the branch exported but every commit
1033 * in its history to be deleted, which basically
1034 * just means deletion of the ref.
1036 if (!reference_excluded_commits) {
1037 /* delete the ref */
1038 printf("reset %s\nfrom %s\n\n",
1039 name, oid_to_hex(&null_oid));
1042 /* set ref to commit using oid, not mark */
1043 printf("reset %s\nfrom %s\n\n", name,
1044 oid_to_hex(&commit->object.oid));
1048 printf("reset %s\nfrom :%d\n\n", name, mark
1056 static void export_marks(char *file)
1060 struct decoration_entry *deco = idnums.entries;
1064 f = fopen_for_writing(file);
1066 die_errno("Unable to open marks file %s for writing.", file);
1068 for (i = 0; i < idnums.size; i++) {
1069 if (deco->base && deco->base->type == 1) {
1070 mark = ptr_to_mark(deco->decoration);
1071 if (fprintf(f, ":%"PRIu32" %s\n", mark,
1072 oid_to_hex(&deco->base->oid)) < 0) {
1083 error("Unable to write marks file %s.", file);
1086 static void import_marks(char *input_file, int check_exists)
1092 if (check_exists && stat(input_file, &sb))
1095 f = xfopen(input_file, "r");
1096 while (fgets(line, sizeof(line), f)) {
1098 char *line_end, *mark_end;
1099 struct object_id oid;
1100 struct object *object;
1101 struct commit *commit;
1102 enum object_type type;
1104 line_end = strchr(line, '\n');
1105 if (line[0] != ':' || !line_end)
1106 die("corrupt mark line: %s", line);
1109 mark = strtoumax(line + 1, &mark_end, 10);
1110 if (!mark || mark_end == line + 1
1111 || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
1112 die("corrupt mark line: %s", line);
1114 if (last_idnum < mark)
1117 type = oid_object_info(the_repository, &oid, NULL);
1119 die("object not found: %s", oid_to_hex(&oid));
1121 if (type != OBJ_COMMIT)
1125 commit = lookup_commit(the_repository, &oid);
1127 die("not a commit? can't happen: %s", oid_to_hex(&oid));
1129 object = &commit->object;
1131 if (object->flags & SHOWN)
1132 error("Object %s already has a mark", oid_to_hex(&oid));
1134 mark_object(object, mark);
1136 object->flags |= SHOWN;
1141 static void handle_deletes(void)
1144 for (i = 0; i < refspecs.nr; i++) {
1145 struct refspec_item *refspec = &refspecs.items[i];
1149 printf("reset %s\nfrom %s\n\n",
1150 refspec->dst, oid_to_hex(&null_oid));
1154 static char *anonymize_seed(void *data)
1156 return xstrdup(data);
1159 static int parse_opt_anonymize_map(const struct option *opt,
1160 const char *arg, int unset)
1162 struct hashmap *map = opt->value;
1163 const char *delim, *value;
1166 BUG_ON_OPT_NEG(unset);
1168 delim = strchr(arg, ':');
1170 keylen = delim - arg;
1173 keylen = strlen(arg);
1177 if (!keylen || !*value)
1178 return error(_("--anonymize-map token cannot be empty"));
1180 anonymize_str(map, anonymize_seed, arg, keylen, (void *)value);
1185 int cmd_fast_export(int argc, const char **argv, const char *prefix)
1187 struct rev_info revs;
1188 struct object_array commits = OBJECT_ARRAY_INIT;
1189 struct commit *commit;
1190 char *export_filename = NULL,
1191 *import_filename = NULL,
1192 *import_filename_if_exists = NULL;
1193 uint32_t lastimportid;
1194 struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
1195 struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
1196 struct option options[] = {
1197 OPT_INTEGER(0, "progress", &progress,
1198 N_("show progress after <n> objects")),
1199 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1200 N_("select handling of signed tags"),
1201 parse_opt_signed_tag_mode),
1202 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1203 N_("select handling of tags that tag filtered objects"),
1204 parse_opt_tag_of_filtered_mode),
1205 OPT_CALLBACK(0, "reencode", &reencode_mode, N_("mode"),
1206 N_("select handling of commit messages in an alternate encoding"),
1207 parse_opt_reencode_mode),
1208 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1209 N_("Dump marks to this file")),
1210 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1211 N_("Import marks from this file")),
1212 OPT_STRING(0, "import-marks-if-exists",
1213 &import_filename_if_exists,
1215 N_("Import marks from this file if it exists")),
1216 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1217 N_("Fake a tagger when tags lack one")),
1218 OPT_BOOL(0, "full-tree", &full_tree,
1219 N_("Output full tree for each commit")),
1220 OPT_BOOL(0, "use-done-feature", &use_done_feature,
1221 N_("Use the done feature to terminate the stream")),
1222 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
1223 OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1224 N_("Apply refspec to exported refs")),
1225 OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
1226 OPT_CALLBACK_F(0, "anonymize-map", &anonymized_seeds, N_("from:to"),
1227 N_("convert <from> to <to> in anonymized output"),
1228 PARSE_OPT_NONEG, parse_opt_anonymize_map),
1229 OPT_BOOL(0, "reference-excluded-parents",
1230 &reference_excluded_commits, N_("Reference parents which are not in fast-export stream by object id")),
1231 OPT_BOOL(0, "show-original-ids", &show_original_ids,
1232 N_("Show original object ids of blobs/commits")),
1233 OPT_BOOL(0, "mark-tags", &mark_tags,
1234 N_("Label tags with mark ids")),
1240 usage_with_options (fast_export_usage, options);
1242 /* we handle encodings */
1243 git_config(git_default_config, NULL);
1245 repo_init_revisions(the_repository, &revs, prefix);
1246 init_revision_sources(&revision_sources);
1247 revs.topo_order = 1;
1248 revs.sources = &revision_sources;
1249 revs.rewrite_parents = 1;
1250 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1251 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
1252 argc = setup_revisions(argc, argv, &revs, NULL);
1254 usage_with_options (fast_export_usage, options);
1256 if (anonymized_seeds.cmpfn && !anonymize)
1257 die(_("--anonymize-map without --anonymize does not make sense"));
1259 if (refspecs_list.nr) {
1262 for (i = 0; i < refspecs_list.nr; i++)
1263 refspec_append(&refspecs, refspecs_list.items[i].string);
1265 string_list_clear(&refspecs_list, 1);
1268 if (use_done_feature)
1269 printf("feature done\n");
1271 if (import_filename && import_filename_if_exists)
1272 die(_("Cannot pass both --import-marks and --import-marks-if-exists"));
1273 if (import_filename)
1274 import_marks(import_filename, 0);
1275 else if (import_filename_if_exists)
1276 import_marks(import_filename_if_exists, 1);
1277 lastimportid = last_idnum;
1279 if (import_filename && revs.prune_data.nr)
1282 get_tags_and_duplicates(&revs.cmdline);
1284 if (prepare_revision_walk(&revs))
1285 die("revision walk setup failed");
1286 revs.diffopt.format_callback = show_filemodify;
1287 revs.diffopt.format_callback_data = &paths_of_changed_objects;
1288 revs.diffopt.flags.recursive = 1;
1289 while ((commit = get_revision(&revs))) {
1290 if (has_unshown_parent(commit)) {
1291 add_object_array(&commit->object, NULL, &commits);
1294 handle_commit(commit, &revs, &paths_of_changed_objects);
1295 handle_tail(&commits, &revs, &paths_of_changed_objects);
1299 handle_tags_and_duplicates(&extra_refs);
1300 handle_tags_and_duplicates(&tag_refs);
1303 if (export_filename && lastimportid != last_idnum)
1304 export_marks(export_filename);
1306 if (use_done_feature)
1309 refspec_clear(&refspecs);