2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
13 #include "object-store.h"
22 #include "string-list.h"
24 #include "parse-options.h"
28 #include "commit-slab.h"
30 static const char *fast_export_usage
[] = {
31 N_("git fast-export [<rev-list-opts>]"),
36 static enum { SIGNED_TAG_ABORT
, VERBATIM
, WARN
, WARN_STRIP
, STRIP
} signed_tag_mode
= SIGNED_TAG_ABORT
;
37 static enum { TAG_FILTERING_ABORT
, DROP
, REWRITE
} tag_of_filtered_mode
= TAG_FILTERING_ABORT
;
38 static enum { REENCODE_ABORT
, REENCODE_YES
, REENCODE_NO
} reencode_mode
= REENCODE_ABORT
;
39 static int fake_missing_tagger
;
40 static int use_done_feature
;
43 static int reference_excluded_commits
;
44 static int show_original_ids
;
46 static struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
47 static struct string_list tag_refs
= STRING_LIST_INIT_NODUP
;
48 static struct refspec refspecs
= REFSPEC_INIT_FETCH
;
50 static struct hashmap anonymized_seeds
;
51 static struct revision_sources revision_sources
;
53 static int parse_opt_signed_tag_mode(const struct option
*opt
,
54 const char *arg
, int unset
)
56 if (unset
|| !strcmp(arg
, "abort"))
57 signed_tag_mode
= SIGNED_TAG_ABORT
;
58 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
59 signed_tag_mode
= VERBATIM
;
60 else if (!strcmp(arg
, "warn"))
61 signed_tag_mode
= WARN
;
62 else if (!strcmp(arg
, "warn-strip"))
63 signed_tag_mode
= WARN_STRIP
;
64 else if (!strcmp(arg
, "strip"))
65 signed_tag_mode
= STRIP
;
67 return error("Unknown signed-tags mode: %s", arg
);
71 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
72 const char *arg
, int unset
)
74 if (unset
|| !strcmp(arg
, "abort"))
75 tag_of_filtered_mode
= TAG_FILTERING_ABORT
;
76 else if (!strcmp(arg
, "drop"))
77 tag_of_filtered_mode
= DROP
;
78 else if (!strcmp(arg
, "rewrite"))
79 tag_of_filtered_mode
= REWRITE
;
81 return error("Unknown tag-of-filtered mode: %s", arg
);
85 static int parse_opt_reencode_mode(const struct option
*opt
,
86 const char *arg
, int unset
)
89 reencode_mode
= REENCODE_ABORT
;
93 switch (git_parse_maybe_bool(arg
)) {
95 reencode_mode
= REENCODE_NO
;
98 reencode_mode
= REENCODE_YES
;
101 if (!strcasecmp(arg
, "abort"))
102 reencode_mode
= REENCODE_ABORT
;
104 return error("Unknown reencoding mode: %s", arg
);
110 static struct decoration idnums
;
111 static uint32_t last_idnum
;
112 struct anonymized_entry
{
113 struct hashmap_entry hash
;
115 const char orig
[FLEX_ARRAY
];
118 struct anonymized_entry_key
{
119 struct hashmap_entry hash
;
124 static int anonymized_entry_cmp(const void *cmp_data UNUSED
,
125 const struct hashmap_entry
*eptr
,
126 const struct hashmap_entry
*entry_or_key
,
129 const struct anonymized_entry
*a
, *b
;
131 a
= container_of(eptr
, const struct anonymized_entry
, hash
);
133 const struct anonymized_entry_key
*key
= keydata
;
134 int equal
= !strncmp(a
->orig
, key
->orig
, key
->orig_len
) &&
135 !a
->orig
[key
->orig_len
];
139 b
= container_of(entry_or_key
, const struct anonymized_entry
, hash
);
140 return strcmp(a
->orig
, b
->orig
);
144 * Basically keep a cache of X->Y so that we can repeatedly replace
145 * the same anonymized string with another. The actual generation
146 * is farmed out to the generate function.
148 static const char *anonymize_str(struct hashmap
*map
,
149 char *(*generate
)(void *),
150 const char *orig
, size_t len
,
153 struct anonymized_entry_key key
;
154 struct anonymized_entry
*ret
;
157 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
159 hashmap_entry_init(&key
.hash
, memhash(orig
, len
));
163 /* First check if it's a token the user configured manually... */
164 if (anonymized_seeds
.cmpfn
)
165 ret
= hashmap_get_entry(&anonymized_seeds
, &key
, hash
, &key
);
169 /* ...otherwise check if we've already seen it in this context... */
171 ret
= hashmap_get_entry(map
, &key
, hash
, &key
);
173 /* ...and finally generate a new mapping if necessary */
175 FLEX_ALLOC_MEM(ret
, orig
, orig
, len
);
176 hashmap_entry_init(&ret
->hash
, key
.hash
.hash
);
177 ret
->anon
= generate(data
);
178 hashmap_put(map
, &ret
->hash
);
185 * We anonymize each component of a path individually,
186 * so that paths a/b and a/c will share a common root.
187 * The paths are cached via anonymize_mem so that repeated
188 * lookups for "a" will yield the same value.
190 static void anonymize_path(struct strbuf
*out
, const char *path
,
192 char *(*generate
)(void *))
195 const char *end_of_component
= strchrnul(path
, '/');
196 size_t len
= end_of_component
- path
;
197 const char *c
= anonymize_str(map
, generate
, path
, len
, NULL
);
198 strbuf_addstr(out
, c
);
199 path
= end_of_component
;
201 strbuf_addch(out
, *path
++);
205 static inline void *mark_to_ptr(uint32_t mark
)
207 return (void *)(uintptr_t)mark
;
210 static inline uint32_t ptr_to_mark(void * mark
)
212 return (uint32_t)(uintptr_t)mark
;
215 static inline void mark_object(struct object
*object
, uint32_t mark
)
217 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
220 static inline void mark_next_object(struct object
*object
)
222 mark_object(object
, ++last_idnum
);
225 static int get_object_mark(struct object
*object
)
227 void *decoration
= lookup_decoration(&idnums
, object
);
230 return ptr_to_mark(decoration
);
233 static struct commit
*rewrite_commit(struct commit
*p
)
236 if (p
->parents
&& p
->parents
->next
)
238 if (p
->object
.flags
& UNINTERESTING
)
240 if (!(p
->object
.flags
& TREESAME
))
244 p
= p
->parents
->item
;
249 static void show_progress(void)
251 static int counter
= 0;
254 if ((++counter
% progress
) == 0)
255 printf("progress %d objects\n", counter
);
259 * Ideally we would want some transformation of the blob data here
260 * that is unreversible, but would still be the same size and have
261 * the same data relationship to other blobs (so that we get the same
262 * delta and packing behavior as the original). But the first and last
263 * requirements there are probably mutually exclusive, so let's take
264 * the easy way out for now, and just generate arbitrary content.
266 * There's no need to cache this result with anonymize_mem, since
267 * we already handle blob content caching with marks.
269 static char *anonymize_blob(unsigned long *size
)
272 struct strbuf out
= STRBUF_INIT
;
273 strbuf_addf(&out
, "anonymous blob %d", counter
++);
275 return strbuf_detach(&out
, NULL
);
278 static void export_blob(const struct object_id
*oid
)
281 enum object_type type
;
283 struct object
*object
;
289 if (is_null_oid(oid
))
292 object
= lookup_object(the_repository
, oid
);
293 if (object
&& object
->flags
& SHOWN
)
297 buf
= anonymize_blob(&size
);
298 object
= (struct object
*)lookup_blob(the_repository
, oid
);
301 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
303 die("could not read blob %s", oid_to_hex(oid
));
304 if (check_object_signature(the_repository
, oid
, buf
, size
,
306 die("oid mismatch in blob %s", oid_to_hex(oid
));
307 object
= parse_object_buffer(the_repository
, oid
, type
,
312 die("Could not read blob %s", oid_to_hex(oid
));
314 mark_next_object(object
);
316 printf("blob\nmark :%"PRIu32
"\n", last_idnum
);
317 if (show_original_ids
)
318 printf("original-oid %s\n", oid_to_hex(oid
));
319 printf("data %"PRIuMAX
"\n", (uintmax_t)size
);
320 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
321 die_errno("could not write blob '%s'", oid_to_hex(oid
));
326 object
->flags
|= SHOWN
;
331 static int depth_first(const void *a_
, const void *b_
)
333 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
334 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
335 const char *name_a
, *name_b
;
336 int len_a
, len_b
, len
;
339 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
340 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
342 len_a
= strlen(name_a
);
343 len_b
= strlen(name_b
);
344 len
= (len_a
< len_b
) ? len_a
: len_b
;
346 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
347 cmp
= memcmp(name_a
, name_b
, len
);
354 * Move 'R'ename entries last so that all references of the file
355 * appear in the output before it is renamed (e.g., when a file
356 * was copied and renamed in the same commit).
358 return (a
->status
== 'R') - (b
->status
== 'R');
361 static void print_path_1(const char *path
)
363 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
365 quote_c_style(path
, NULL
, stdout
, 0);
366 else if (strchr(path
, ' '))
367 printf("\"%s\"", path
);
372 static char *anonymize_path_component(void *data
)
375 struct strbuf out
= STRBUF_INIT
;
376 strbuf_addf(&out
, "path%d", counter
++);
377 return strbuf_detach(&out
, NULL
);
380 static void print_path(const char *path
)
385 static struct hashmap paths
;
386 static struct strbuf anon
= STRBUF_INIT
;
388 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
389 print_path_1(anon
.buf
);
394 static char *generate_fake_oid(void *data
)
396 static uint32_t counter
= 1; /* avoid null oid */
397 const unsigned hashsz
= the_hash_algo
->rawsz
;
398 struct object_id oid
;
399 char *hex
= xmallocz(GIT_MAX_HEXSZ
);
402 put_be32(oid
.hash
+ hashsz
- 4, counter
++);
403 return oid_to_hex_r(hex
, &oid
);
406 static const char *anonymize_oid(const char *oid_hex
)
408 static struct hashmap objs
;
409 size_t len
= strlen(oid_hex
);
410 return anonymize_str(&objs
, generate_fake_oid
, oid_hex
, len
, NULL
);
413 static void show_filemodify(struct diff_queue_struct
*q
,
414 struct diff_options
*options UNUSED
, void *data
)
417 struct string_list
*changed
= data
;
420 * Handle files below a directory first, in case they are all deleted
421 * and the directory changes to a file or symlink.
423 QSORT(q
->queue
, q
->nr
, depth_first
);
425 for (i
= 0; i
< q
->nr
; i
++) {
426 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
427 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
429 switch (q
->queue
[i
]->status
) {
430 case DIFF_STATUS_DELETED
:
432 print_path(spec
->path
);
433 string_list_insert(changed
, spec
->path
);
437 case DIFF_STATUS_COPIED
:
438 case DIFF_STATUS_RENAMED
:
440 * If a change in the file corresponding to ospec->path
441 * has been observed, we cannot trust its contents
442 * because the diff is calculated based on the prior
443 * contents, not the current contents. So, declare a
444 * copy or rename only if there was no change observed.
446 if (!string_list_has_string(changed
, ospec
->path
)) {
447 printf("%c ", q
->queue
[i
]->status
);
448 print_path(ospec
->path
);
450 print_path(spec
->path
);
451 string_list_insert(changed
, spec
->path
);
454 if (oideq(&ospec
->oid
, &spec
->oid
) &&
455 ospec
->mode
== spec
->mode
)
460 case DIFF_STATUS_TYPE_CHANGED
:
461 case DIFF_STATUS_MODIFIED
:
462 case DIFF_STATUS_ADDED
:
464 * Links refer to objects in another repositories;
465 * output the SHA-1 verbatim.
467 if (no_data
|| S_ISGITLINK(spec
->mode
))
468 printf("M %06o %s ", spec
->mode
,
470 anonymize_oid(oid_to_hex(&spec
->oid
)) :
471 oid_to_hex(&spec
->oid
));
473 struct object
*object
= lookup_object(the_repository
,
475 printf("M %06o :%d ", spec
->mode
,
476 get_object_mark(object
));
478 print_path(spec
->path
);
479 string_list_insert(changed
, spec
->path
);
484 die("Unexpected comparison status '%c' for %s, %s",
486 ospec
->path
? ospec
->path
: "none",
487 spec
->path
? spec
->path
: "none");
492 static const char *find_encoding(const char *begin
, const char *end
)
494 const char *needle
= "\nencoding ";
497 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
498 needle
, strlen(needle
));
501 bol
+= strlen(needle
);
502 eol
= strchrnul(bol
, '\n');
507 static char *anonymize_ref_component(void *data
)
510 struct strbuf out
= STRBUF_INIT
;
511 strbuf_addf(&out
, "ref%d", counter
++);
512 return strbuf_detach(&out
, NULL
);
515 static const char *anonymize_refname(const char *refname
)
518 * If any of these prefixes is found, we will leave it intact
519 * so that tags remain tags and so forth.
521 static const char *prefixes
[] = {
527 static struct hashmap refs
;
528 static struct strbuf anon
= STRBUF_INIT
;
532 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
533 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
534 strbuf_addstr(&anon
, prefixes
[i
]);
539 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
544 * We do not even bother to cache commit messages, as they are unlikely
545 * to be repeated verbatim, and it is not that interesting when they are.
547 static char *anonymize_commit_message(const char *old
)
550 return xstrfmt("subject %d\n\nbody\n", counter
++);
553 static char *anonymize_ident(void *data
)
556 struct strbuf out
= STRBUF_INIT
;
557 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
559 return strbuf_detach(&out
, NULL
);
563 * Our strategy here is to anonymize the names and email addresses,
564 * but keep timestamps intact, as they influence things like traversal
565 * order (and by themselves should not be too revealing).
567 static void anonymize_ident_line(const char **beg
, const char **end
)
569 static struct hashmap idents
;
570 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
571 static unsigned which_buffer
;
574 struct ident_split split
;
575 const char *end_of_header
;
577 out
= &buffers
[which_buffer
++];
578 which_buffer
%= ARRAY_SIZE(buffers
);
581 /* skip "committer", "author", "tagger", etc */
582 end_of_header
= strchr(*beg
, ' ');
584 BUG("malformed line fed to anonymize_ident_line: %.*s",
585 (int)(*end
- *beg
), *beg
);
587 strbuf_add(out
, *beg
, end_of_header
- *beg
);
589 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
594 len
= split
.mail_end
- split
.name_begin
;
595 ident
= anonymize_str(&idents
, anonymize_ident
,
596 split
.name_begin
, len
, NULL
);
597 strbuf_addstr(out
, ident
);
598 strbuf_addch(out
, ' ');
599 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
601 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
605 *end
= out
->buf
+ out
->len
;
608 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
,
609 struct string_list
*paths_of_changed_objects
)
611 int saved_output_format
= rev
->diffopt
.output_format
;
612 const char *commit_buffer
;
613 const char *author
, *author_end
, *committer
, *committer_end
;
614 const char *encoding
, *message
;
615 char *reencoded
= NULL
;
616 struct commit_list
*p
;
620 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
622 parse_commit_or_die(commit
);
623 commit_buffer
= repo_get_commit_buffer(the_repository
, commit
, NULL
);
624 author
= strstr(commit_buffer
, "\nauthor ");
626 die("could not find author in commit %s",
627 oid_to_hex(&commit
->object
.oid
));
629 author_end
= strchrnul(author
, '\n');
630 committer
= strstr(author_end
, "\ncommitter ");
632 die("could not find committer in commit %s",
633 oid_to_hex(&commit
->object
.oid
));
635 committer_end
= strchrnul(committer
, '\n');
636 message
= strstr(committer_end
, "\n\n");
637 encoding
= find_encoding(committer_end
, message
);
641 if (commit
->parents
&&
642 (get_object_mark(&commit
->parents
->item
->object
) != 0 ||
643 reference_excluded_commits
) &&
645 parse_commit_or_die(commit
->parents
->item
);
646 diff_tree_oid(get_commit_tree_oid(commit
->parents
->item
),
647 get_commit_tree_oid(commit
), "", &rev
->diffopt
);
650 diff_root_tree_oid(get_commit_tree_oid(commit
),
653 /* Export the referenced blobs, and remember the marks. */
654 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
655 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
656 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
658 refname
= *revision_sources_at(&revision_sources
, commit
);
660 * FIXME: string_list_remove() below for each ref is overall
661 * O(N^2). Compared to a history walk and diffing trees, this is
662 * just lost in the noise in practice. However, theoretically a
663 * repo may have enough refs for this to become slow.
665 string_list_remove(&extra_refs
, refname
, 0);
667 refname
= anonymize_refname(refname
);
668 anonymize_ident_line(&committer
, &committer_end
);
669 anonymize_ident_line(&author
, &author_end
);
672 mark_next_object(&commit
->object
);
674 reencoded
= anonymize_commit_message(message
);
675 } else if (encoding
) {
676 switch(reencode_mode
) {
678 reencoded
= reencode_string(message
, "UTF-8", encoding
);
683 die("Encountered commit-specific encoding %s in commit "
684 "%s; use --reencode=[yes|no] to handle it",
685 encoding
, oid_to_hex(&commit
->object
.oid
));
688 if (!commit
->parents
)
689 printf("reset %s\n", refname
);
690 printf("commit %s\nmark :%"PRIu32
"\n", refname
, last_idnum
);
691 if (show_original_ids
)
692 printf("original-oid %s\n", oid_to_hex(&commit
->object
.oid
));
693 printf("%.*s\n%.*s\n",
694 (int)(author_end
- author
), author
,
695 (int)(committer_end
- committer
), committer
);
696 if (!reencoded
&& encoding
)
697 printf("encoding %s\n", encoding
);
698 printf("data %u\n%s",
700 ? strlen(reencoded
) : message
701 ? strlen(message
) : 0),
702 reencoded
? reencoded
: message
? message
: "");
704 repo_unuse_commit_buffer(the_repository
, commit
, commit_buffer
);
706 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
707 struct object
*obj
= &p
->item
->object
;
708 int mark
= get_object_mark(obj
);
710 if (!mark
&& !reference_excluded_commits
)
717 printf(":%d\n", mark
);
721 anonymize_oid(oid_to_hex(&obj
->oid
)) :
722 oid_to_hex(&obj
->oid
));
727 printf("deleteall\n");
728 log_tree_diff_flush(rev
);
729 string_list_clear(paths_of_changed_objects
, 0);
730 rev
->diffopt
.output_format
= saved_output_format
;
737 static char *anonymize_tag(void *data
)
740 struct strbuf out
= STRBUF_INIT
;
741 strbuf_addf(&out
, "tag message %d", counter
++);
742 return strbuf_detach(&out
, NULL
);
746 static void handle_tag(const char *name
, struct tag
*tag
)
749 enum object_type type
;
751 const char *tagger
, *tagger_end
, *message
;
752 size_t message_size
= 0;
753 struct object
*tagged
;
757 /* Trees have no identifier in fast-export output, thus we have no way
758 * to output tags of trees, tags of tags of trees, etc. Simply omit
761 tagged
= tag
->tagged
;
762 while (tagged
->type
== OBJ_TAG
) {
763 tagged
= ((struct tag
*)tagged
)->tagged
;
765 if (tagged
->type
== OBJ_TREE
) {
766 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
767 oid_to_hex(&tag
->object
.oid
));
771 buf
= repo_read_object_file(the_repository
, &tag
->object
.oid
, &type
,
774 die("could not read tag %s", oid_to_hex(&tag
->object
.oid
));
775 message
= memmem(buf
, size
, "\n\n", 2);
778 message_size
= strlen(message
);
780 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
782 if (fake_missing_tagger
)
783 tagger
= "tagger Unspecified Tagger "
784 "<unspecified-tagger> 0 +0000";
787 tagger_end
= tagger
+ strlen(tagger
);
790 tagger_end
= strchrnul(tagger
, '\n');
792 anonymize_ident_line(&tagger
, &tagger_end
);
796 name
= anonymize_refname(name
);
798 static struct hashmap tags
;
799 message
= anonymize_str(&tags
, anonymize_tag
,
800 message
, message_size
, NULL
);
801 message_size
= strlen(message
);
805 /* handle signed tags */
807 const char *signature
= strstr(message
,
808 "\n-----BEGIN PGP SIGNATURE-----\n");
810 switch(signed_tag_mode
) {
811 case SIGNED_TAG_ABORT
:
812 die("encountered signed tag %s; use "
813 "--signed-tags=<mode> to handle it",
814 oid_to_hex(&tag
->object
.oid
));
816 warning("exporting signed tag %s",
817 oid_to_hex(&tag
->object
.oid
));
822 warning("stripping signature from tag %s",
823 oid_to_hex(&tag
->object
.oid
));
826 message_size
= signature
+ 1 - message
;
831 /* handle tag->tagged having been filtered out due to paths specified */
832 tagged
= tag
->tagged
;
833 tagged_mark
= get_object_mark(tagged
);
835 switch(tag_of_filtered_mode
) {
836 case TAG_FILTERING_ABORT
:
837 die("tag %s tags unexported object; use "
838 "--tag-of-filtered-object=<mode> to handle it",
839 oid_to_hex(&tag
->object
.oid
));
841 /* Ignore this tag altogether */
845 if (tagged
->type
== OBJ_TAG
&& !mark_tags
) {
846 die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
847 } else if (tagged
->type
== OBJ_COMMIT
) {
848 p
= rewrite_commit((struct commit
*)tagged
);
850 printf("reset %s\nfrom %s\n\n",
851 name
, oid_to_hex(null_oid()));
855 tagged_mark
= get_object_mark(&p
->object
);
857 /* tagged->type is either OBJ_BLOB or OBJ_TAG */
858 tagged_mark
= get_object_mark(tagged
);
863 if (tagged
->type
== OBJ_TAG
) {
864 printf("reset %s\nfrom %s\n\n",
865 name
, oid_to_hex(null_oid()));
867 skip_prefix(name
, "refs/tags/", &name
);
868 printf("tag %s\n", name
);
870 mark_next_object(&tag
->object
);
871 printf("mark :%"PRIu32
"\n", last_idnum
);
874 printf("from :%d\n", tagged_mark
);
876 printf("from %s\n", oid_to_hex(&tagged
->oid
));
878 if (show_original_ids
)
879 printf("original-oid %s\n", oid_to_hex(&tag
->object
.oid
));
880 printf("%.*s%sdata %d\n%.*s\n",
881 (int)(tagger_end
- tagger
), tagger
,
882 tagger
== tagger_end
? "" : "\n",
883 (int)message_size
, (int)message_size
, message
? message
: "");
887 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
889 switch (e
->item
->type
) {
891 return (struct commit
*)e
->item
;
893 struct tag
*tag
= (struct tag
*)e
->item
;
895 /* handle nested tags */
896 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
897 parse_object(the_repository
, &tag
->object
.oid
);
898 string_list_append(&tag_refs
, full_name
)->util
= tag
;
899 tag
= (struct tag
*)tag
->tagged
;
902 die("Tag %s points nowhere?", e
->name
);
903 return (struct commit
*)tag
;
910 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
914 for (i
= 0; i
< info
->nr
; i
++) {
915 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
916 struct object_id oid
;
917 struct commit
*commit
;
920 if (e
->flags
& UNINTERESTING
)
923 if (repo_dwim_ref(the_repository
, e
->name
, strlen(e
->name
),
924 &oid
, &full_name
, 0) != 1)
929 private = apply_refspecs(&refspecs
, full_name
);
936 commit
= get_commit(e
, full_name
);
938 warning("%s: Unexpected object of type %s, skipping.",
940 type_name(e
->item
->type
));
944 switch(commit
->object
.type
) {
948 export_blob(&commit
->object
.oid
);
950 default: /* OBJ_TAG (nested tags) is already handled */
951 warning("Tag points to object of unexpected type %s, skipping.",
952 type_name(commit
->object
.type
));
957 * Make sure this ref gets properly updated eventually, whether
958 * through a commit or manually at the end.
960 if (e
->item
->type
!= OBJ_TAG
)
961 string_list_append(&extra_refs
, full_name
)->util
= commit
;
963 if (!*revision_sources_at(&revision_sources
, commit
))
964 *revision_sources_at(&revision_sources
, commit
) = full_name
;
967 string_list_sort(&extra_refs
);
968 string_list_remove_duplicates(&extra_refs
, 0);
971 static void handle_tags_and_duplicates(struct string_list
*extras
)
973 struct commit
*commit
;
976 for (i
= extras
->nr
- 1; i
>= 0; i
--) {
977 const char *name
= extras
->items
[i
].string
;
978 struct object
*object
= extras
->items
[i
].util
;
981 switch (object
->type
) {
983 handle_tag(name
, (struct tag
*)object
);
987 name
= anonymize_refname(name
);
988 /* create refs pointing to already seen commits */
989 commit
= rewrite_commit((struct commit
*)object
);
992 * Neither this object nor any of its
993 * ancestors touch any relevant paths, so
994 * it has been filtered to nothing. Delete
997 printf("reset %s\nfrom %s\n\n",
998 name
, oid_to_hex(null_oid()));
1002 mark
= get_object_mark(&commit
->object
);
1005 * Getting here means we have a commit which
1006 * was excluded by a negative refspec (e.g.
1007 * fast-export ^HEAD HEAD). If we are
1008 * referencing excluded commits, set the ref
1009 * to the exact commit. Otherwise, the user
1010 * wants the branch exported but every commit
1011 * in its history to be deleted, which basically
1012 * just means deletion of the ref.
1014 if (!reference_excluded_commits
) {
1015 /* delete the ref */
1016 printf("reset %s\nfrom %s\n\n",
1017 name
, oid_to_hex(null_oid()));
1020 /* set ref to commit using oid, not mark */
1021 printf("reset %s\nfrom %s\n\n", name
,
1022 oid_to_hex(&commit
->object
.oid
));
1026 printf("reset %s\nfrom :%d\n\n", name
, mark
1034 static void export_marks(char *file
)
1038 struct decoration_entry
*deco
= idnums
.entries
;
1042 f
= fopen_for_writing(file
);
1044 die_errno("Unable to open marks file %s for writing.", file
);
1046 for (i
= 0; i
< idnums
.size
; i
++) {
1047 if (deco
->base
&& deco
->base
->type
== 1) {
1048 mark
= ptr_to_mark(deco
->decoration
);
1049 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
1050 oid_to_hex(&deco
->base
->oid
)) < 0) {
1061 error("Unable to write marks file %s.", file
);
1064 static void import_marks(char *input_file
, int check_exists
)
1070 if (check_exists
&& stat(input_file
, &sb
))
1073 f
= xfopen(input_file
, "r");
1074 while (fgets(line
, sizeof(line
), f
)) {
1076 char *line_end
, *mark_end
;
1077 struct object_id oid
;
1078 struct object
*object
;
1079 struct commit
*commit
;
1080 enum object_type type
;
1082 line_end
= strchr(line
, '\n');
1083 if (line
[0] != ':' || !line_end
)
1084 die("corrupt mark line: %s", line
);
1087 mark
= strtoumax(line
+ 1, &mark_end
, 10);
1088 if (!mark
|| mark_end
== line
+ 1
1089 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
1090 die("corrupt mark line: %s", line
);
1092 if (last_idnum
< mark
)
1095 type
= oid_object_info(the_repository
, &oid
, NULL
);
1097 die("object not found: %s", oid_to_hex(&oid
));
1099 if (type
!= OBJ_COMMIT
)
1103 commit
= lookup_commit(the_repository
, &oid
);
1105 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
1107 object
= &commit
->object
;
1109 if (object
->flags
& SHOWN
)
1110 error("Object %s already has a mark", oid_to_hex(&oid
));
1112 mark_object(object
, mark
);
1114 object
->flags
|= SHOWN
;
1119 static void handle_deletes(void)
1122 for (i
= 0; i
< refspecs
.nr
; i
++) {
1123 struct refspec_item
*refspec
= &refspecs
.items
[i
];
1127 printf("reset %s\nfrom %s\n\n",
1128 refspec
->dst
, oid_to_hex(null_oid()));
1132 static char *anonymize_seed(void *data
)
1134 return xstrdup(data
);
1137 static int parse_opt_anonymize_map(const struct option
*opt
,
1138 const char *arg
, int unset
)
1140 struct hashmap
*map
= opt
->value
;
1141 const char *delim
, *value
;
1144 BUG_ON_OPT_NEG(unset
);
1146 delim
= strchr(arg
, ':');
1148 keylen
= delim
- arg
;
1151 keylen
= strlen(arg
);
1155 if (!keylen
|| !*value
)
1156 return error(_("--anonymize-map token cannot be empty"));
1158 anonymize_str(map
, anonymize_seed
, arg
, keylen
, (void *)value
);
1163 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
1165 struct rev_info revs
;
1166 struct commit
*commit
;
1167 char *export_filename
= NULL
,
1168 *import_filename
= NULL
,
1169 *import_filename_if_exists
= NULL
;
1170 uint32_t lastimportid
;
1171 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
1172 struct string_list paths_of_changed_objects
= STRING_LIST_INIT_DUP
;
1173 struct option options
[] = {
1174 OPT_INTEGER(0, "progress", &progress
,
1175 N_("show progress after <n> objects")),
1176 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
1177 N_("select handling of signed tags"),
1178 parse_opt_signed_tag_mode
),
1179 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
1180 N_("select handling of tags that tag filtered objects"),
1181 parse_opt_tag_of_filtered_mode
),
1182 OPT_CALLBACK(0, "reencode", &reencode_mode
, N_("mode"),
1183 N_("select handling of commit messages in an alternate encoding"),
1184 parse_opt_reencode_mode
),
1185 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
1186 N_("dump marks to this file")),
1187 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
1188 N_("import marks from this file")),
1189 OPT_STRING(0, "import-marks-if-exists",
1190 &import_filename_if_exists
,
1192 N_("import marks from this file if it exists")),
1193 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
1194 N_("fake a tagger when tags lack one")),
1195 OPT_BOOL(0, "full-tree", &full_tree
,
1196 N_("output full tree for each commit")),
1197 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
1198 N_("use the done feature to terminate the stream")),
1199 OPT_BOOL(0, "no-data", &no_data
, N_("skip output of blob data")),
1200 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1201 N_("apply refspec to exported refs")),
1202 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1203 OPT_CALLBACK_F(0, "anonymize-map", &anonymized_seeds
, N_("from:to"),
1204 N_("convert <from> to <to> in anonymized output"),
1205 PARSE_OPT_NONEG
, parse_opt_anonymize_map
),
1206 OPT_BOOL(0, "reference-excluded-parents",
1207 &reference_excluded_commits
, N_("reference parents which are not in fast-export stream by object id")),
1208 OPT_BOOL(0, "show-original-ids", &show_original_ids
,
1209 N_("show original object ids of blobs/commits")),
1210 OPT_BOOL(0, "mark-tags", &mark_tags
,
1211 N_("label tags with mark ids")),
1217 usage_with_options (fast_export_usage
, options
);
1219 /* we handle encodings */
1220 git_config(git_default_config
, NULL
);
1222 repo_init_revisions(the_repository
, &revs
, prefix
);
1223 init_revision_sources(&revision_sources
);
1224 revs
.topo_order
= 1;
1225 revs
.sources
= &revision_sources
;
1226 revs
.rewrite_parents
= 1;
1227 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1228 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
);
1229 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1231 usage_with_options (fast_export_usage
, options
);
1233 if (anonymized_seeds
.cmpfn
&& !anonymize
)
1234 die(_("the option '%s' requires '%s'"), "--anonymize-map", "--anonymize");
1236 if (refspecs_list
.nr
) {
1239 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1240 refspec_append(&refspecs
, refspecs_list
.items
[i
].string
);
1242 string_list_clear(&refspecs_list
, 1);
1245 if (use_done_feature
)
1246 printf("feature done\n");
1248 if (import_filename
&& import_filename_if_exists
)
1249 die(_("options '%s' and '%s' cannot be used together"), "--import-marks", "--import-marks-if-exists");
1250 if (import_filename
)
1251 import_marks(import_filename
, 0);
1252 else if (import_filename_if_exists
)
1253 import_marks(import_filename_if_exists
, 1);
1254 lastimportid
= last_idnum
;
1256 if (import_filename
&& revs
.prune_data
.nr
)
1259 get_tags_and_duplicates(&revs
.cmdline
);
1261 if (prepare_revision_walk(&revs
))
1262 die("revision walk setup failed");
1265 revs
.diffopt
.format_callback
= show_filemodify
;
1266 revs
.diffopt
.format_callback_data
= &paths_of_changed_objects
;
1267 revs
.diffopt
.flags
.recursive
= 1;
1268 revs
.diffopt
.no_free
= 1;
1269 while ((commit
= get_revision(&revs
)))
1270 handle_commit(commit
, &revs
, &paths_of_changed_objects
);
1272 handle_tags_and_duplicates(&extra_refs
);
1273 handle_tags_and_duplicates(&tag_refs
);
1276 if (export_filename
&& lastimportid
!= last_idnum
)
1277 export_marks(export_filename
);
1279 if (use_done_feature
)
1282 refspec_clear(&refspecs
);
1283 release_revisions(&revs
);