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 int fake_missing_tagger;
37 static int use_done_feature;
40 static int reference_excluded_commits;
41 static int show_original_ids;
42 static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
43 static struct string_list tag_refs = STRING_LIST_INIT_NODUP;
44 static struct refspec refspecs = REFSPEC_INIT_FETCH;
46 static struct revision_sources revision_sources;
48 static int parse_opt_signed_tag_mode(const struct option *opt,
49 const char *arg, int unset)
51 if (unset || !strcmp(arg, "abort"))
52 signed_tag_mode = SIGNED_TAG_ABORT;
53 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
54 signed_tag_mode = VERBATIM;
55 else if (!strcmp(arg, "warn"))
56 signed_tag_mode = WARN;
57 else if (!strcmp(arg, "warn-strip"))
58 signed_tag_mode = WARN_STRIP;
59 else if (!strcmp(arg, "strip"))
60 signed_tag_mode = STRIP;
62 return error("Unknown signed-tags mode: %s", arg);
66 static int parse_opt_tag_of_filtered_mode(const struct option *opt,
67 const char *arg, int unset)
69 if (unset || !strcmp(arg, "abort"))
70 tag_of_filtered_mode = TAG_FILTERING_ABORT;
71 else if (!strcmp(arg, "drop"))
72 tag_of_filtered_mode = DROP;
73 else if (!strcmp(arg, "rewrite"))
74 tag_of_filtered_mode = REWRITE;
76 return error("Unknown tag-of-filtered mode: %s", arg);
80 static struct decoration idnums;
81 static uint32_t last_idnum;
83 static int has_unshown_parent(struct commit *commit)
85 struct commit_list *parent;
87 for (parent = commit->parents; parent; parent = parent->next)
88 if (!(parent->item->object.flags & SHOWN) &&
89 !(parent->item->object.flags & UNINTERESTING))
94 struct anonymized_entry {
95 struct hashmap_entry hash;
102 static int anonymized_entry_cmp(const void *unused_cmp_data,
103 const void *va, const void *vb,
104 const void *unused_keydata)
106 const struct anonymized_entry *a = va, *b = vb;
107 return a->orig_len != b->orig_len ||
108 memcmp(a->orig, b->orig, a->orig_len);
112 * Basically keep a cache of X->Y so that we can repeatedly replace
113 * the same anonymized string with another. The actual generation
114 * is farmed out to the generate function.
116 static const void *anonymize_mem(struct hashmap *map,
117 void *(*generate)(const void *, size_t *),
118 const void *orig, size_t *len)
120 struct anonymized_entry key, *ret;
123 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
125 hashmap_entry_init(&key, memhash(orig, *len));
128 ret = hashmap_get(map, &key, NULL);
131 ret = xmalloc(sizeof(*ret));
132 hashmap_entry_init(&ret->hash, key.hash.hash);
133 ret->orig = xstrdup(orig);
134 ret->orig_len = *len;
135 ret->anon = generate(orig, len);
136 ret->anon_len = *len;
137 hashmap_put(map, ret);
140 *len = ret->anon_len;
145 * We anonymize each component of a path individually,
146 * so that paths a/b and a/c will share a common root.
147 * The paths are cached via anonymize_mem so that repeated
148 * lookups for "a" will yield the same value.
150 static void anonymize_path(struct strbuf *out, const char *path,
152 void *(*generate)(const void *, size_t *))
155 const char *end_of_component = strchrnul(path, '/');
156 size_t len = end_of_component - path;
157 const char *c = anonymize_mem(map, generate, path, &len);
158 strbuf_add(out, c, len);
159 path = end_of_component;
161 strbuf_addch(out, *path++);
165 static inline void *mark_to_ptr(uint32_t mark)
167 return (void *)(uintptr_t)mark;
170 static inline uint32_t ptr_to_mark(void * mark)
172 return (uint32_t)(uintptr_t)mark;
175 static inline void mark_object(struct object *object, uint32_t mark)
177 add_decoration(&idnums, object, mark_to_ptr(mark));
180 static inline void mark_next_object(struct object *object)
182 mark_object(object, ++last_idnum);
185 static int get_object_mark(struct object *object)
187 void *decoration = lookup_decoration(&idnums, object);
190 return ptr_to_mark(decoration);
193 static struct commit *rewrite_commit(struct commit *p)
196 if (p->parents && p->parents->next)
198 if (p->object.flags & UNINTERESTING)
200 if (!(p->object.flags & TREESAME))
204 p = p->parents->item;
209 static void show_progress(void)
211 static int counter = 0;
214 if ((++counter % progress) == 0)
215 printf("progress %d objects\n", counter);
219 * Ideally we would want some transformation of the blob data here
220 * that is unreversible, but would still be the same size and have
221 * the same data relationship to other blobs (so that we get the same
222 * delta and packing behavior as the original). But the first and last
223 * requirements there are probably mutually exclusive, so let's take
224 * the easy way out for now, and just generate arbitrary content.
226 * There's no need to cache this result with anonymize_mem, since
227 * we already handle blob content caching with marks.
229 static char *anonymize_blob(unsigned long *size)
232 struct strbuf out = STRBUF_INIT;
233 strbuf_addf(&out, "anonymous blob %d", counter++);
235 return strbuf_detach(&out, NULL);
238 static void export_blob(const struct object_id *oid)
241 enum object_type type;
243 struct object *object;
249 if (is_null_oid(oid))
252 object = lookup_object(the_repository, oid->hash);
253 if (object && object->flags & SHOWN)
257 buf = anonymize_blob(&size);
258 object = (struct object *)lookup_blob(the_repository, oid);
261 buf = read_object_file(oid, &type, &size);
263 die("could not read blob %s", oid_to_hex(oid));
264 if (check_object_signature(oid, buf, size, type_name(type)) < 0)
265 die("oid mismatch in blob %s", oid_to_hex(oid));
266 object = parse_object_buffer(the_repository, oid, type,
271 die("Could not read blob %s", oid_to_hex(oid));
273 mark_next_object(object);
275 printf("blob\nmark :%"PRIu32"\n", last_idnum);
276 if (show_original_ids)
277 printf("original-oid %s\n", oid_to_hex(oid));
278 printf("data %"PRIuMAX"\n", (uintmax_t)size);
279 if (size && fwrite(buf, size, 1, stdout) != 1)
280 die_errno("could not write blob '%s'", oid_to_hex(oid));
285 object->flags |= SHOWN;
290 static int depth_first(const void *a_, const void *b_)
292 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
293 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
294 const char *name_a, *name_b;
295 int len_a, len_b, len;
298 name_a = a->one ? a->one->path : a->two->path;
299 name_b = b->one ? b->one->path : b->two->path;
301 len_a = strlen(name_a);
302 len_b = strlen(name_b);
303 len = (len_a < len_b) ? len_a : len_b;
305 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
306 cmp = memcmp(name_a, name_b, len);
313 * Move 'R'ename entries last so that all references of the file
314 * appear in the output before it is renamed (e.g., when a file
315 * was copied and renamed in the same commit).
317 return (a->status == 'R') - (b->status == 'R');
320 static void print_path_1(const char *path)
322 int need_quote = quote_c_style(path, NULL, NULL, 0);
324 quote_c_style(path, NULL, stdout, 0);
325 else if (strchr(path, ' '))
326 printf("\"%s\"", path);
331 static void *anonymize_path_component(const void *path, size_t *len)
334 struct strbuf out = STRBUF_INIT;
335 strbuf_addf(&out, "path%d", counter++);
336 return strbuf_detach(&out, len);
339 static void print_path(const char *path)
344 static struct hashmap paths;
345 static struct strbuf anon = STRBUF_INIT;
347 anonymize_path(&anon, path, &paths, anonymize_path_component);
348 print_path_1(anon.buf);
353 static void *generate_fake_oid(const void *old, size_t *len)
355 static uint32_t counter = 1; /* avoid null oid */
356 const unsigned hashsz = the_hash_algo->rawsz;
357 unsigned char *out = xcalloc(hashsz, 1);
358 put_be32(out + hashsz - 4, counter++);
362 static const struct object_id *anonymize_oid(const struct object_id *oid)
364 static struct hashmap objs;
365 size_t len = the_hash_algo->rawsz;
366 return anonymize_mem(&objs, generate_fake_oid, oid, &len);
369 static void show_filemodify(struct diff_queue_struct *q,
370 struct diff_options *options, void *data)
373 struct string_list *changed = data;
376 * Handle files below a directory first, in case they are all deleted
377 * and the directory changes to a file or symlink.
379 QSORT(q->queue, q->nr, depth_first);
381 for (i = 0; i < q->nr; i++) {
382 struct diff_filespec *ospec = q->queue[i]->one;
383 struct diff_filespec *spec = q->queue[i]->two;
385 switch (q->queue[i]->status) {
386 case DIFF_STATUS_DELETED:
388 print_path(spec->path);
389 string_list_insert(changed, spec->path);
393 case DIFF_STATUS_COPIED:
394 case DIFF_STATUS_RENAMED:
396 * If a change in the file corresponding to ospec->path
397 * has been observed, we cannot trust its contents
398 * because the diff is calculated based on the prior
399 * contents, not the current contents. So, declare a
400 * copy or rename only if there was no change observed.
402 if (!string_list_has_string(changed, ospec->path)) {
403 printf("%c ", q->queue[i]->status);
404 print_path(ospec->path);
406 print_path(spec->path);
407 string_list_insert(changed, spec->path);
410 if (oideq(&ospec->oid, &spec->oid) &&
411 ospec->mode == spec->mode)
416 case DIFF_STATUS_TYPE_CHANGED:
417 case DIFF_STATUS_MODIFIED:
418 case DIFF_STATUS_ADDED:
420 * Links refer to objects in another repositories;
421 * output the SHA-1 verbatim.
423 if (no_data || S_ISGITLINK(spec->mode))
424 printf("M %06o %s ", spec->mode,
425 oid_to_hex(anonymize ?
426 anonymize_oid(&spec->oid) :
429 struct object *object = lookup_object(the_repository,
431 printf("M %06o :%d ", spec->mode,
432 get_object_mark(object));
434 print_path(spec->path);
435 string_list_insert(changed, spec->path);
440 die("Unexpected comparison status '%c' for %s, %s",
442 ospec->path ? ospec->path : "none",
443 spec->path ? spec->path : "none");
448 static const char *find_encoding(const char *begin, const char *end)
450 const char *needle = "\nencoding ";
453 bol = memmem(begin, end ? end - begin : strlen(begin),
454 needle, strlen(needle));
456 return git_commit_encoding;
457 bol += strlen(needle);
458 eol = strchrnul(bol, '\n');
463 static void *anonymize_ref_component(const void *old, size_t *len)
466 struct strbuf out = STRBUF_INIT;
467 strbuf_addf(&out, "ref%d", counter++);
468 return strbuf_detach(&out, len);
471 static const char *anonymize_refname(const char *refname)
474 * If any of these prefixes is found, we will leave it intact
475 * so that tags remain tags and so forth.
477 static const char *prefixes[] = {
483 static struct hashmap refs;
484 static struct strbuf anon = STRBUF_INIT;
488 * We also leave "master" as a special case, since it does not reveal
489 * anything interesting.
491 if (!strcmp(refname, "refs/heads/master"))
495 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
496 if (skip_prefix(refname, prefixes[i], &refname)) {
497 strbuf_addstr(&anon, prefixes[i]);
502 anonymize_path(&anon, refname, &refs, anonymize_ref_component);
507 * We do not even bother to cache commit messages, as they are unlikely
508 * to be repeated verbatim, and it is not that interesting when they are.
510 static char *anonymize_commit_message(const char *old)
513 return xstrfmt("subject %d\n\nbody\n", counter++);
516 static struct hashmap idents;
517 static void *anonymize_ident(const void *old, size_t *len)
520 struct strbuf out = STRBUF_INIT;
521 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
523 return strbuf_detach(&out, len);
527 * Our strategy here is to anonymize the names and email addresses,
528 * but keep timestamps intact, as they influence things like traversal
529 * order (and by themselves should not be too revealing).
531 static void anonymize_ident_line(const char **beg, const char **end)
533 static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT };
534 static unsigned which_buffer;
537 struct ident_split split;
538 const char *end_of_header;
540 out = &buffers[which_buffer++];
541 which_buffer %= ARRAY_SIZE(buffers);
544 /* skip "committer", "author", "tagger", etc */
545 end_of_header = strchr(*beg, ' ');
547 BUG("malformed line fed to anonymize_ident_line: %.*s",
548 (int)(*end - *beg), *beg);
550 strbuf_add(out, *beg, end_of_header - *beg);
552 if (!split_ident_line(&split, end_of_header, *end - end_of_header) &&
557 len = split.mail_end - split.name_begin;
558 ident = anonymize_mem(&idents, anonymize_ident,
559 split.name_begin, &len);
560 strbuf_add(out, ident, len);
561 strbuf_addch(out, ' ');
562 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
564 strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000");
568 *end = out->buf + out->len;
571 static void handle_commit(struct commit *commit, struct rev_info *rev,
572 struct string_list *paths_of_changed_objects)
574 int saved_output_format = rev->diffopt.output_format;
575 const char *commit_buffer;
576 const char *author, *author_end, *committer, *committer_end;
577 const char *encoding, *message;
578 char *reencoded = NULL;
579 struct commit_list *p;
583 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
585 parse_commit_or_die(commit);
586 commit_buffer = get_commit_buffer(commit, NULL);
587 author = strstr(commit_buffer, "\nauthor ");
589 die("could not find author in commit %s",
590 oid_to_hex(&commit->object.oid));
592 author_end = strchrnul(author, '\n');
593 committer = strstr(author_end, "\ncommitter ");
595 die("could not find committer in commit %s",
596 oid_to_hex(&commit->object.oid));
598 committer_end = strchrnul(committer, '\n');
599 message = strstr(committer_end, "\n\n");
600 encoding = find_encoding(committer_end, message);
604 if (commit->parents &&
605 (get_object_mark(&commit->parents->item->object) != 0 ||
606 reference_excluded_commits) &&
608 parse_commit_or_die(commit->parents->item);
609 diff_tree_oid(get_commit_tree_oid(commit->parents->item),
610 get_commit_tree_oid(commit), "", &rev->diffopt);
613 diff_root_tree_oid(get_commit_tree_oid(commit),
616 /* Export the referenced blobs, and remember the marks. */
617 for (i = 0; i < diff_queued_diff.nr; i++)
618 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
619 export_blob(&diff_queued_diff.queue[i]->two->oid);
621 refname = *revision_sources_at(&revision_sources, commit);
623 * FIXME: string_list_remove() below for each ref is overall
624 * O(N^2). Compared to a history walk and diffing trees, this is
625 * just lost in the noise in practice. However, theoretically a
626 * repo may have enough refs for this to become slow.
628 string_list_remove(&extra_refs, refname, 0);
630 refname = anonymize_refname(refname);
631 anonymize_ident_line(&committer, &committer_end);
632 anonymize_ident_line(&author, &author_end);
635 mark_next_object(&commit->object);
637 reencoded = anonymize_commit_message(message);
638 else if (!is_encoding_utf8(encoding))
639 reencoded = reencode_string(message, "UTF-8", encoding);
640 if (!commit->parents)
641 printf("reset %s\n", refname);
642 printf("commit %s\nmark :%"PRIu32"\n", refname, last_idnum);
643 if (show_original_ids)
644 printf("original-oid %s\n", oid_to_hex(&commit->object.oid));
645 printf("%.*s\n%.*s\ndata %u\n%s",
646 (int)(author_end - author), author,
647 (int)(committer_end - committer), committer,
649 ? strlen(reencoded) : message
650 ? strlen(message) : 0),
651 reencoded ? reencoded : message ? message : "");
653 unuse_commit_buffer(commit, commit_buffer);
655 for (i = 0, p = commit->parents; p; p = p->next) {
656 struct object *obj = &p->item->object;
657 int mark = get_object_mark(obj);
659 if (!mark && !reference_excluded_commits)
666 printf(":%d\n", mark);
668 printf("%s\n", oid_to_hex(anonymize ?
669 anonymize_oid(&obj->oid) :
675 printf("deleteall\n");
676 log_tree_diff_flush(rev);
677 string_list_clear(paths_of_changed_objects, 0);
678 rev->diffopt.output_format = saved_output_format;
685 static void *anonymize_tag(const void *old, size_t *len)
688 struct strbuf out = STRBUF_INIT;
689 strbuf_addf(&out, "tag message %d", counter++);
690 return strbuf_detach(&out, len);
693 static void handle_tail(struct object_array *commits, struct rev_info *revs,
694 struct string_list *paths_of_changed_objects)
696 struct commit *commit;
697 while (commits->nr) {
698 commit = (struct commit *)object_array_pop(commits);
699 if (has_unshown_parent(commit)) {
700 /* Queue again, to be handled later */
701 add_object_array(&commit->object, NULL, commits);
704 handle_commit(commit, revs, paths_of_changed_objects);
708 static void handle_tag(const char *name, struct tag *tag)
711 enum object_type type;
713 const char *tagger, *tagger_end, *message;
714 size_t message_size = 0;
715 struct object *tagged;
719 /* Trees have no identifier in fast-export output, thus we have no way
720 * to output tags of trees, tags of tags of trees, etc. Simply omit
723 tagged = tag->tagged;
724 while (tagged->type == OBJ_TAG) {
725 tagged = ((struct tag *)tagged)->tagged;
727 if (tagged->type == OBJ_TREE) {
728 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
729 oid_to_hex(&tag->object.oid));
733 buf = read_object_file(&tag->object.oid, &type, &size);
735 die("could not read tag %s", oid_to_hex(&tag->object.oid));
736 message = memmem(buf, size, "\n\n", 2);
739 message_size = strlen(message);
741 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
743 if (fake_missing_tagger)
744 tagger = "tagger Unspecified Tagger "
745 "<unspecified-tagger> 0 +0000";
748 tagger_end = tagger + strlen(tagger);
751 tagger_end = strchrnul(tagger, '\n');
753 anonymize_ident_line(&tagger, &tagger_end);
757 name = anonymize_refname(name);
759 static struct hashmap tags;
760 message = anonymize_mem(&tags, anonymize_tag,
761 message, &message_size);
765 /* handle signed tags */
767 const char *signature = strstr(message,
768 "\n-----BEGIN PGP SIGNATURE-----\n");
770 switch(signed_tag_mode) {
771 case SIGNED_TAG_ABORT:
772 die("encountered signed tag %s; use "
773 "--signed-tags=<mode> to handle it",
774 oid_to_hex(&tag->object.oid));
776 warning("exporting signed tag %s",
777 oid_to_hex(&tag->object.oid));
782 warning("stripping signature from tag %s",
783 oid_to_hex(&tag->object.oid));
786 message_size = signature + 1 - message;
791 /* handle tag->tagged having been filtered out due to paths specified */
792 tagged = tag->tagged;
793 tagged_mark = get_object_mark(tagged);
795 switch(tag_of_filtered_mode) {
796 case TAG_FILTERING_ABORT:
797 die("tag %s tags unexported object; use "
798 "--tag-of-filtered-object=<mode> to handle it",
799 oid_to_hex(&tag->object.oid));
801 /* Ignore this tag altogether */
805 if (tagged->type != OBJ_COMMIT) {
806 die("tag %s tags unexported %s!",
807 oid_to_hex(&tag->object.oid),
808 type_name(tagged->type));
810 p = rewrite_commit((struct commit *)tagged);
812 printf("reset %s\nfrom %s\n\n",
813 name, oid_to_hex(&null_oid));
817 tagged_mark = get_object_mark(&p->object);
821 if (starts_with(name, "refs/tags/"))
823 printf("tag %s\nfrom :%d\n", name, tagged_mark);
824 if (show_original_ids)
825 printf("original-oid %s\n", oid_to_hex(&tag->object.oid));
826 printf("%.*s%sdata %d\n%.*s\n",
827 (int)(tagger_end - tagger), tagger,
828 tagger == tagger_end ? "" : "\n",
829 (int)message_size, (int)message_size, message ? message : "");
833 static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
835 switch (e->item->type) {
837 return (struct commit *)e->item;
839 struct tag *tag = (struct tag *)e->item;
841 /* handle nested tags */
842 while (tag && tag->object.type == OBJ_TAG) {
843 parse_object(the_repository, &tag->object.oid);
844 string_list_append(&tag_refs, full_name)->util = tag;
845 tag = (struct tag *)tag->tagged;
848 die("Tag %s points nowhere?", e->name);
849 return (struct commit *)tag;
857 static void get_tags_and_duplicates(struct rev_cmdline_info *info)
861 for (i = 0; i < info->nr; i++) {
862 struct rev_cmdline_entry *e = info->rev + i;
863 struct object_id oid;
864 struct commit *commit;
867 if (e->flags & UNINTERESTING)
870 if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1)
875 private = apply_refspecs(&refspecs, full_name);
882 commit = get_commit(e, full_name);
884 warning("%s: Unexpected object of type %s, skipping.",
886 type_name(e->item->type));
890 switch(commit->object.type) {
894 export_blob(&commit->object.oid);
896 default: /* OBJ_TAG (nested tags) is already handled */
897 warning("Tag points to object of unexpected type %s, skipping.",
898 type_name(commit->object.type));
903 * Make sure this ref gets properly updated eventually, whether
904 * through a commit or manually at the end.
906 if (e->item->type != OBJ_TAG)
907 string_list_append(&extra_refs, full_name)->util = commit;
909 if (!*revision_sources_at(&revision_sources, commit))
910 *revision_sources_at(&revision_sources, commit) = full_name;
913 string_list_sort(&extra_refs);
914 string_list_remove_duplicates(&extra_refs, 0);
917 static void handle_tags_and_duplicates(struct string_list *extras)
919 struct commit *commit;
922 for (i = extras->nr - 1; i >= 0; i--) {
923 const char *name = extras->items[i].string;
924 struct object *object = extras->items[i].util;
927 switch (object->type) {
929 handle_tag(name, (struct tag *)object);
933 name = anonymize_refname(name);
934 /* create refs pointing to already seen commits */
935 commit = rewrite_commit((struct commit *)object);
938 * Neither this object nor any of its
939 * ancestors touch any relevant paths, so
940 * it has been filtered to nothing. Delete
943 printf("reset %s\nfrom %s\n\n",
944 name, oid_to_hex(&null_oid));
948 mark = get_object_mark(&commit->object);
951 * Getting here means we have a commit which
952 * was excluded by a negative refspec (e.g.
953 * fast-export ^master master). If we are
954 * referencing excluded commits, set the ref
955 * to the exact commit. Otherwise, the user
956 * wants the branch exported but every commit
957 * in its history to be deleted, which basically
958 * just means deletion of the ref.
960 if (!reference_excluded_commits) {
962 printf("reset %s\nfrom %s\n\n",
963 name, oid_to_hex(&null_oid));
966 /* set ref to commit using oid, not mark */
967 printf("reset %s\nfrom %s\n\n", name,
968 oid_to_hex(&commit->object.oid));
972 printf("reset %s\nfrom :%d\n\n", name, mark
980 static void export_marks(char *file)
984 struct decoration_entry *deco = idnums.entries;
988 f = fopen_for_writing(file);
990 die_errno("Unable to open marks file %s for writing.", file);
992 for (i = 0; i < idnums.size; i++) {
993 if (deco->base && deco->base->type == 1) {
994 mark = ptr_to_mark(deco->decoration);
995 if (fprintf(f, ":%"PRIu32" %s\n", mark,
996 oid_to_hex(&deco->base->oid)) < 0) {
1007 error("Unable to write marks file %s.", file);
1010 static void import_marks(char *input_file)
1013 FILE *f = xfopen(input_file, "r");
1015 while (fgets(line, sizeof(line), f)) {
1017 char *line_end, *mark_end;
1018 struct object_id oid;
1019 struct object *object;
1020 struct commit *commit;
1021 enum object_type type;
1023 line_end = strchr(line, '\n');
1024 if (line[0] != ':' || !line_end)
1025 die("corrupt mark line: %s", line);
1028 mark = strtoumax(line + 1, &mark_end, 10);
1029 if (!mark || mark_end == line + 1
1030 || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
1031 die("corrupt mark line: %s", line);
1033 if (last_idnum < mark)
1036 type = oid_object_info(the_repository, &oid, NULL);
1038 die("object not found: %s", oid_to_hex(&oid));
1040 if (type != OBJ_COMMIT)
1044 commit = lookup_commit(the_repository, &oid);
1046 die("not a commit? can't happen: %s", oid_to_hex(&oid));
1048 object = &commit->object;
1050 if (object->flags & SHOWN)
1051 error("Object %s already has a mark", oid_to_hex(&oid));
1053 mark_object(object, mark);
1055 object->flags |= SHOWN;
1060 static void handle_deletes(void)
1063 for (i = 0; i < refspecs.nr; i++) {
1064 struct refspec_item *refspec = &refspecs.items[i];
1068 printf("reset %s\nfrom %s\n\n",
1069 refspec->dst, oid_to_hex(&null_oid));
1073 int cmd_fast_export(int argc, const char **argv, const char *prefix)
1075 struct rev_info revs;
1076 struct object_array commits = OBJECT_ARRAY_INIT;
1077 struct commit *commit;
1078 char *export_filename = NULL, *import_filename = NULL;
1079 uint32_t lastimportid;
1080 struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
1081 struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
1082 struct option options[] = {
1083 OPT_INTEGER(0, "progress", &progress,
1084 N_("show progress after <n> objects")),
1085 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1086 N_("select handling of signed tags"),
1087 parse_opt_signed_tag_mode),
1088 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1089 N_("select handling of tags that tag filtered objects"),
1090 parse_opt_tag_of_filtered_mode),
1091 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1092 N_("Dump marks to this file")),
1093 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1094 N_("Import marks from this file")),
1095 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1096 N_("Fake a tagger when tags lack one")),
1097 OPT_BOOL(0, "full-tree", &full_tree,
1098 N_("Output full tree for each commit")),
1099 OPT_BOOL(0, "use-done-feature", &use_done_feature,
1100 N_("Use the done feature to terminate the stream")),
1101 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
1102 OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1103 N_("Apply refspec to exported refs")),
1104 OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
1105 OPT_BOOL(0, "reference-excluded-parents",
1106 &reference_excluded_commits, N_("Reference parents which are not in fast-export stream by object id")),
1107 OPT_BOOL(0, "show-original-ids", &show_original_ids,
1108 N_("Show original object ids of blobs/commits")),
1114 usage_with_options (fast_export_usage, options);
1116 /* we handle encodings */
1117 git_config(git_default_config, NULL);
1119 repo_init_revisions(the_repository, &revs, prefix);
1120 init_revision_sources(&revision_sources);
1121 revs.topo_order = 1;
1122 revs.sources = &revision_sources;
1123 revs.rewrite_parents = 1;
1124 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1125 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
1126 argc = setup_revisions(argc, argv, &revs, NULL);
1128 usage_with_options (fast_export_usage, options);
1130 if (refspecs_list.nr) {
1133 for (i = 0; i < refspecs_list.nr; i++)
1134 refspec_append(&refspecs, refspecs_list.items[i].string);
1136 string_list_clear(&refspecs_list, 1);
1139 if (use_done_feature)
1140 printf("feature done\n");
1142 if (import_filename)
1143 import_marks(import_filename);
1144 lastimportid = last_idnum;
1146 if (import_filename && revs.prune_data.nr)
1149 get_tags_and_duplicates(&revs.cmdline);
1151 if (prepare_revision_walk(&revs))
1152 die("revision walk setup failed");
1153 revs.diffopt.format_callback = show_filemodify;
1154 revs.diffopt.format_callback_data = &paths_of_changed_objects;
1155 revs.diffopt.flags.recursive = 1;
1156 while ((commit = get_revision(&revs))) {
1157 if (has_unshown_parent(commit)) {
1158 add_object_array(&commit->object, NULL, &commits);
1161 handle_commit(commit, &revs, &paths_of_changed_objects);
1162 handle_tail(&commits, &revs, &paths_of_changed_objects);
1166 handle_tags_and_duplicates(&extra_refs);
1167 handle_tags_and_duplicates(&tag_refs);
1170 if (export_filename && lastimportid != last_idnum)
1171 export_marks(export_filename);
1173 if (use_done_feature)
1176 refspec_clear(&refspecs);