2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
6 #define USE_THE_REPOSITORY_VARIABLE
13 #include "object-file.h"
14 #include "object-store-ll.h"
23 #include "string-list.h"
25 #include "parse-options.h"
30 static const char *fast_export_usage
[] = {
31 N_("git fast-export [<rev-list-opts>]"),
36 static enum signed_tag_mode
{ SIGNED_TAG_ABORT
, VERBATIM
, WARN
, WARN_STRIP
, STRIP
} signed_tag_mode
= SIGNED_TAG_ABORT
;
37 static enum tag_of_filtered_mode
{ TAG_FILTERING_ABORT
, DROP
, REWRITE
} tag_of_filtered_mode
= TAG_FILTERING_ABORT
;
38 static enum reencode_mode
{ 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_DUP
;
47 static struct string_list tag_refs
= STRING_LIST_INIT_DUP
;
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 enum signed_tag_mode
*val
= opt
->value
;
58 if (unset
|| !strcmp(arg
, "abort"))
59 *val
= SIGNED_TAG_ABORT
;
60 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
62 else if (!strcmp(arg
, "warn"))
64 else if (!strcmp(arg
, "warn-strip"))
66 else if (!strcmp(arg
, "strip"))
69 return error("Unknown signed-tags mode: %s", arg
);
73 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
74 const char *arg
, int unset
)
76 enum tag_of_filtered_mode
*val
= opt
->value
;
78 if (unset
|| !strcmp(arg
, "abort"))
79 *val
= TAG_FILTERING_ABORT
;
80 else if (!strcmp(arg
, "drop"))
82 else if (!strcmp(arg
, "rewrite"))
85 return error("Unknown tag-of-filtered mode: %s", arg
);
89 static int parse_opt_reencode_mode(const struct option
*opt
,
90 const char *arg
, int unset
)
92 enum reencode_mode
*val
= opt
->value
;
95 *val
= REENCODE_ABORT
;
99 switch (git_parse_maybe_bool(arg
)) {
107 if (!strcasecmp(arg
, "abort"))
108 *val
= REENCODE_ABORT
;
110 return error("Unknown reencoding mode: %s", arg
);
116 static struct decoration idnums
;
117 static uint32_t last_idnum
;
118 struct anonymized_entry
{
119 struct hashmap_entry hash
;
121 const char orig
[FLEX_ARRAY
];
124 struct anonymized_entry_key
{
125 struct hashmap_entry hash
;
130 static int anonymized_entry_cmp(const void *cmp_data UNUSED
,
131 const struct hashmap_entry
*eptr
,
132 const struct hashmap_entry
*entry_or_key
,
135 const struct anonymized_entry
*a
, *b
;
137 a
= container_of(eptr
, const struct anonymized_entry
, hash
);
139 const struct anonymized_entry_key
*key
= keydata
;
140 int equal
= !xstrncmpz(a
->orig
, key
->orig
, key
->orig_len
);
144 b
= container_of(entry_or_key
, const struct anonymized_entry
, hash
);
145 return strcmp(a
->orig
, b
->orig
);
148 static struct anonymized_entry
*add_anonymized_entry(struct hashmap
*map
,
150 const char *orig
, size_t len
,
153 struct anonymized_entry
*ret
, *old
;
156 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
158 FLEX_ALLOC_MEM(ret
, orig
, orig
, len
);
159 hashmap_entry_init(&ret
->hash
, hash
);
161 old
= hashmap_put_entry(map
, ret
, hash
);
172 * Basically keep a cache of X->Y so that we can repeatedly replace
173 * the same anonymized string with another. The actual generation
174 * is farmed out to the generate function.
176 static const char *anonymize_str(struct hashmap
*map
,
177 char *(*generate
)(void),
178 const char *orig
, size_t len
)
180 struct anonymized_entry_key key
;
181 struct anonymized_entry
*ret
;
183 hashmap_entry_init(&key
.hash
, memhash(orig
, len
));
187 /* First check if it's a token the user configured manually... */
188 ret
= hashmap_get_entry(&anonymized_seeds
, &key
, hash
, &key
);
190 /* ...otherwise check if we've already seen it in this context... */
192 ret
= hashmap_get_entry(map
, &key
, hash
, &key
);
194 /* ...and finally generate a new mapping if necessary */
196 ret
= add_anonymized_entry(map
, key
.hash
.hash
,
197 orig
, len
, generate());
203 * We anonymize each component of a path individually,
204 * so that paths a/b and a/c will share a common root.
205 * The paths are cached via anonymize_mem so that repeated
206 * lookups for "a" will yield the same value.
208 static void anonymize_path(struct strbuf
*out
, const char *path
,
210 char *(*generate
)(void))
213 const char *end_of_component
= strchrnul(path
, '/');
214 size_t len
= end_of_component
- path
;
215 const char *c
= anonymize_str(map
, generate
, path
, len
);
216 strbuf_addstr(out
, c
);
217 path
= end_of_component
;
219 strbuf_addch(out
, *path
++);
223 static inline void *mark_to_ptr(uint32_t mark
)
225 return (void *)(uintptr_t)mark
;
228 static inline uint32_t ptr_to_mark(void * mark
)
230 return (uint32_t)(uintptr_t)mark
;
233 static inline void mark_object(struct object
*object
, uint32_t mark
)
235 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
238 static inline void mark_next_object(struct object
*object
)
240 mark_object(object
, ++last_idnum
);
243 static int get_object_mark(struct object
*object
)
245 void *decoration
= lookup_decoration(&idnums
, object
);
248 return ptr_to_mark(decoration
);
251 static struct commit
*rewrite_commit(struct commit
*p
)
254 if (p
->parents
&& p
->parents
->next
)
256 if (p
->object
.flags
& UNINTERESTING
)
258 if (!(p
->object
.flags
& TREESAME
))
262 p
= p
->parents
->item
;
267 static void show_progress(void)
269 static int counter
= 0;
272 if ((++counter
% progress
) == 0)
273 printf("progress %d objects\n", counter
);
277 * Ideally we would want some transformation of the blob data here
278 * that is unreversible, but would still be the same size and have
279 * the same data relationship to other blobs (so that we get the same
280 * delta and packing behavior as the original). But the first and last
281 * requirements there are probably mutually exclusive, so let's take
282 * the easy way out for now, and just generate arbitrary content.
284 * There's no need to cache this result with anonymize_mem, since
285 * we already handle blob content caching with marks.
287 static char *anonymize_blob(unsigned long *size
)
290 struct strbuf out
= STRBUF_INIT
;
291 strbuf_addf(&out
, "anonymous blob %d", counter
++);
293 return strbuf_detach(&out
, NULL
);
296 static void export_blob(const struct object_id
*oid
)
299 enum object_type type
;
301 struct object
*object
;
307 if (is_null_oid(oid
))
310 object
= lookup_object(the_repository
, oid
);
311 if (object
&& object
->flags
& SHOWN
)
315 buf
= anonymize_blob(&size
);
316 object
= (struct object
*)lookup_blob(the_repository
, oid
);
319 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
321 die("could not read blob %s", oid_to_hex(oid
));
322 if (check_object_signature(the_repository
, oid
, buf
, size
,
324 die("oid mismatch in blob %s", oid_to_hex(oid
));
325 object
= parse_object_buffer(the_repository
, oid
, type
,
330 die("Could not read blob %s", oid_to_hex(oid
));
332 mark_next_object(object
);
334 printf("blob\nmark :%"PRIu32
"\n", last_idnum
);
335 if (show_original_ids
)
336 printf("original-oid %s\n", oid_to_hex(oid
));
337 printf("data %"PRIuMAX
"\n", (uintmax_t)size
);
338 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
339 die_errno("could not write blob '%s'", oid_to_hex(oid
));
344 object
->flags
|= SHOWN
;
349 static int depth_first(const void *a_
, const void *b_
)
351 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
352 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
353 const char *name_a
, *name_b
;
354 int len_a
, len_b
, len
;
357 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
358 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
360 len_a
= strlen(name_a
);
361 len_b
= strlen(name_b
);
362 len
= (len_a
< len_b
) ? len_a
: len_b
;
364 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
365 cmp
= memcmp(name_a
, name_b
, len
);
372 * Move 'R'ename entries last so that all references of the file
373 * appear in the output before it is renamed (e.g., when a file
374 * was copied and renamed in the same commit).
376 return (a
->status
== 'R') - (b
->status
== 'R');
379 static void print_path_1(const char *path
)
381 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
383 quote_c_style(path
, NULL
, stdout
, 0);
384 else if (strchr(path
, ' '))
385 printf("\"%s\"", path
);
390 static char *anonymize_path_component(void)
393 struct strbuf out
= STRBUF_INIT
;
394 strbuf_addf(&out
, "path%d", counter
++);
395 return strbuf_detach(&out
, NULL
);
398 static void print_path(const char *path
)
403 static struct hashmap paths
;
404 static struct strbuf anon
= STRBUF_INIT
;
406 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
407 print_path_1(anon
.buf
);
412 static char *generate_fake_oid(void)
414 static uint32_t counter
= 1; /* avoid null oid */
415 const unsigned hashsz
= the_hash_algo
->rawsz
;
416 struct object_id oid
;
417 char *hex
= xmallocz(GIT_MAX_HEXSZ
);
419 oidclr(&oid
, the_repository
->hash_algo
);
420 put_be32(oid
.hash
+ hashsz
- 4, counter
++);
421 return oid_to_hex_r(hex
, &oid
);
424 static const char *anonymize_oid(const char *oid_hex
)
426 static struct hashmap objs
;
427 size_t len
= strlen(oid_hex
);
428 return anonymize_str(&objs
, generate_fake_oid
, oid_hex
, len
);
431 static void show_filemodify(struct diff_queue_struct
*q
,
432 struct diff_options
*options UNUSED
, void *data
)
435 struct string_list
*changed
= data
;
438 * Handle files below a directory first, in case they are all deleted
439 * and the directory changes to a file or symlink.
441 QSORT(q
->queue
, q
->nr
, depth_first
);
443 for (i
= 0; i
< q
->nr
; i
++) {
444 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
445 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
447 switch (q
->queue
[i
]->status
) {
448 case DIFF_STATUS_DELETED
:
450 print_path(spec
->path
);
451 string_list_insert(changed
, spec
->path
);
455 case DIFF_STATUS_COPIED
:
456 case DIFF_STATUS_RENAMED
:
458 * If a change in the file corresponding to ospec->path
459 * has been observed, we cannot trust its contents
460 * because the diff is calculated based on the prior
461 * contents, not the current contents. So, declare a
462 * copy or rename only if there was no change observed.
464 if (!string_list_has_string(changed
, ospec
->path
)) {
465 printf("%c ", q
->queue
[i
]->status
);
466 print_path(ospec
->path
);
468 print_path(spec
->path
);
469 string_list_insert(changed
, spec
->path
);
472 if (oideq(&ospec
->oid
, &spec
->oid
) &&
473 ospec
->mode
== spec
->mode
)
478 case DIFF_STATUS_TYPE_CHANGED
:
479 case DIFF_STATUS_MODIFIED
:
480 case DIFF_STATUS_ADDED
:
482 * Links refer to objects in another repositories;
483 * output the SHA-1 verbatim.
485 if (no_data
|| S_ISGITLINK(spec
->mode
))
486 printf("M %06o %s ", spec
->mode
,
488 anonymize_oid(oid_to_hex(&spec
->oid
)) :
489 oid_to_hex(&spec
->oid
));
491 struct object
*object
= lookup_object(the_repository
,
493 printf("M %06o :%d ", spec
->mode
,
494 get_object_mark(object
));
496 print_path(spec
->path
);
497 string_list_insert(changed
, spec
->path
);
502 die("Unexpected comparison status '%c' for %s, %s",
504 ospec
->path
? ospec
->path
: "none",
505 spec
->path
? spec
->path
: "none");
510 static const char *find_encoding(const char *begin
, const char *end
)
512 const char *needle
= "\nencoding ";
515 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
516 needle
, strlen(needle
));
519 bol
+= strlen(needle
);
520 eol
= strchrnul(bol
, '\n');
525 static char *anonymize_ref_component(void)
528 struct strbuf out
= STRBUF_INIT
;
529 strbuf_addf(&out
, "ref%d", counter
++);
530 return strbuf_detach(&out
, NULL
);
533 static const char *anonymize_refname(const char *refname
)
536 * If any of these prefixes is found, we will leave it intact
537 * so that tags remain tags and so forth.
539 static const char *prefixes
[] = {
545 static struct hashmap refs
;
546 static struct strbuf anon
= STRBUF_INIT
;
550 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
551 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
552 strbuf_addstr(&anon
, prefixes
[i
]);
557 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
562 * We do not even bother to cache commit messages, as they are unlikely
563 * to be repeated verbatim, and it is not that interesting when they are.
565 static char *anonymize_commit_message(void)
568 return xstrfmt("subject %d\n\nbody\n", counter
++);
571 static char *anonymize_ident(void)
574 struct strbuf out
= STRBUF_INIT
;
575 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
577 return strbuf_detach(&out
, NULL
);
581 * Our strategy here is to anonymize the names and email addresses,
582 * but keep timestamps intact, as they influence things like traversal
583 * order (and by themselves should not be too revealing).
585 static void anonymize_ident_line(const char **beg
, const char **end
)
587 static struct hashmap idents
;
588 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
589 static unsigned which_buffer
;
592 struct ident_split split
;
593 const char *end_of_header
;
595 out
= &buffers
[which_buffer
++];
596 which_buffer
%= ARRAY_SIZE(buffers
);
599 /* skip "committer", "author", "tagger", etc */
600 end_of_header
= strchr(*beg
, ' ');
602 BUG("malformed line fed to anonymize_ident_line: %.*s",
603 (int)(*end
- *beg
), *beg
);
605 strbuf_add(out
, *beg
, end_of_header
- *beg
);
607 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
612 len
= split
.mail_end
- split
.name_begin
;
613 ident
= anonymize_str(&idents
, anonymize_ident
,
614 split
.name_begin
, len
);
615 strbuf_addstr(out
, ident
);
616 strbuf_addch(out
, ' ');
617 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
619 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
623 *end
= out
->buf
+ out
->len
;
626 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
,
627 struct string_list
*paths_of_changed_objects
)
629 int saved_output_format
= rev
->diffopt
.output_format
;
630 const char *commit_buffer
;
631 const char *author
, *author_end
, *committer
, *committer_end
;
632 const char *encoding
, *message
;
633 char *reencoded
= NULL
;
634 struct commit_list
*p
;
638 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
640 parse_commit_or_die(commit
);
641 commit_buffer
= repo_get_commit_buffer(the_repository
, commit
, NULL
);
642 author
= strstr(commit_buffer
, "\nauthor ");
644 die("could not find author in commit %s",
645 oid_to_hex(&commit
->object
.oid
));
647 author_end
= strchrnul(author
, '\n');
648 committer
= strstr(author_end
, "\ncommitter ");
650 die("could not find committer in commit %s",
651 oid_to_hex(&commit
->object
.oid
));
653 committer_end
= strchrnul(committer
, '\n');
654 message
= strstr(committer_end
, "\n\n");
655 encoding
= find_encoding(committer_end
, message
);
659 if (commit
->parents
&&
660 (get_object_mark(&commit
->parents
->item
->object
) != 0 ||
661 reference_excluded_commits
) &&
663 parse_commit_or_die(commit
->parents
->item
);
664 diff_tree_oid(get_commit_tree_oid(commit
->parents
->item
),
665 get_commit_tree_oid(commit
), "", &rev
->diffopt
);
668 diff_root_tree_oid(get_commit_tree_oid(commit
),
671 /* Export the referenced blobs, and remember the marks. */
672 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
673 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
674 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
676 refname
= *revision_sources_at(&revision_sources
, commit
);
678 * FIXME: string_list_remove() below for each ref is overall
679 * O(N^2). Compared to a history walk and diffing trees, this is
680 * just lost in the noise in practice. However, theoretically a
681 * repo may have enough refs for this to become slow.
683 string_list_remove(&extra_refs
, refname
, 0);
685 refname
= anonymize_refname(refname
);
686 anonymize_ident_line(&committer
, &committer_end
);
687 anonymize_ident_line(&author
, &author_end
);
690 mark_next_object(&commit
->object
);
692 reencoded
= anonymize_commit_message();
693 } else if (encoding
) {
694 switch(reencode_mode
) {
696 reencoded
= reencode_string(message
, "UTF-8", encoding
);
701 die("Encountered commit-specific encoding %s in commit "
702 "%s; use --reencode=[yes|no] to handle it",
703 encoding
, oid_to_hex(&commit
->object
.oid
));
706 if (!commit
->parents
)
707 printf("reset %s\n", refname
);
708 printf("commit %s\nmark :%"PRIu32
"\n", refname
, last_idnum
);
709 if (show_original_ids
)
710 printf("original-oid %s\n", oid_to_hex(&commit
->object
.oid
));
711 printf("%.*s\n%.*s\n",
712 (int)(author_end
- author
), author
,
713 (int)(committer_end
- committer
), committer
);
714 if (!reencoded
&& encoding
)
715 printf("encoding %s\n", encoding
);
716 printf("data %u\n%s",
718 ? strlen(reencoded
) : message
719 ? strlen(message
) : 0),
720 reencoded
? reencoded
: message
? message
: "");
722 repo_unuse_commit_buffer(the_repository
, commit
, commit_buffer
);
724 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
725 struct object
*obj
= &p
->item
->object
;
726 int mark
= get_object_mark(obj
);
728 if (!mark
&& !reference_excluded_commits
)
735 printf(":%d\n", mark
);
739 anonymize_oid(oid_to_hex(&obj
->oid
)) :
740 oid_to_hex(&obj
->oid
));
745 printf("deleteall\n");
746 log_tree_diff_flush(rev
);
747 string_list_clear(paths_of_changed_objects
, 0);
748 rev
->diffopt
.output_format
= saved_output_format
;
755 static char *anonymize_tag(void)
758 struct strbuf out
= STRBUF_INIT
;
759 strbuf_addf(&out
, "tag message %d", counter
++);
760 return strbuf_detach(&out
, NULL
);
764 static void handle_tag(const char *name
, struct tag
*tag
)
767 enum object_type type
;
769 const char *tagger
, *tagger_end
, *message
;
770 size_t message_size
= 0;
771 struct object
*tagged
;
775 /* Trees have no identifier in fast-export output, thus we have no way
776 * to output tags of trees, tags of tags of trees, etc. Simply omit
779 tagged
= tag
->tagged
;
780 while (tagged
->type
== OBJ_TAG
) {
781 tagged
= ((struct tag
*)tagged
)->tagged
;
783 if (tagged
->type
== OBJ_TREE
) {
784 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
785 oid_to_hex(&tag
->object
.oid
));
789 buf
= repo_read_object_file(the_repository
, &tag
->object
.oid
, &type
,
792 die("could not read tag %s", oid_to_hex(&tag
->object
.oid
));
793 message
= memmem(buf
, size
, "\n\n", 2);
796 message_size
= strlen(message
);
798 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
800 if (fake_missing_tagger
)
801 tagger
= "tagger Unspecified Tagger "
802 "<unspecified-tagger> 0 +0000";
805 tagger_end
= tagger
+ strlen(tagger
);
808 tagger_end
= strchrnul(tagger
, '\n');
810 anonymize_ident_line(&tagger
, &tagger_end
);
814 name
= anonymize_refname(name
);
816 static struct hashmap tags
;
817 message
= anonymize_str(&tags
, anonymize_tag
,
818 message
, message_size
);
819 message_size
= strlen(message
);
823 /* handle signed tags */
825 const char *signature
= strstr(message
,
826 "\n-----BEGIN PGP SIGNATURE-----\n");
828 switch(signed_tag_mode
) {
829 case SIGNED_TAG_ABORT
:
830 die("encountered signed tag %s; use "
831 "--signed-tags=<mode> to handle it",
832 oid_to_hex(&tag
->object
.oid
));
834 warning("exporting signed tag %s",
835 oid_to_hex(&tag
->object
.oid
));
840 warning("stripping signature from tag %s",
841 oid_to_hex(&tag
->object
.oid
));
844 message_size
= signature
+ 1 - message
;
849 /* handle tag->tagged having been filtered out due to paths specified */
850 tagged
= tag
->tagged
;
851 tagged_mark
= get_object_mark(tagged
);
853 switch(tag_of_filtered_mode
) {
854 case TAG_FILTERING_ABORT
:
855 die("tag %s tags unexported object; use "
856 "--tag-of-filtered-object=<mode> to handle it",
857 oid_to_hex(&tag
->object
.oid
));
859 /* Ignore this tag altogether */
863 if (tagged
->type
== OBJ_TAG
&& !mark_tags
) {
864 die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
865 } else if (tagged
->type
== OBJ_COMMIT
) {
866 p
= rewrite_commit((struct commit
*)tagged
);
868 printf("reset %s\nfrom %s\n\n",
869 name
, oid_to_hex(null_oid()));
873 tagged_mark
= get_object_mark(&p
->object
);
875 /* tagged->type is either OBJ_BLOB or OBJ_TAG */
876 tagged_mark
= get_object_mark(tagged
);
881 if (tagged
->type
== OBJ_TAG
) {
882 printf("reset %s\nfrom %s\n\n",
883 name
, oid_to_hex(null_oid()));
885 skip_prefix(name
, "refs/tags/", &name
);
886 printf("tag %s\n", name
);
888 mark_next_object(&tag
->object
);
889 printf("mark :%"PRIu32
"\n", last_idnum
);
892 printf("from :%d\n", tagged_mark
);
894 printf("from %s\n", oid_to_hex(&tagged
->oid
));
896 if (show_original_ids
)
897 printf("original-oid %s\n", oid_to_hex(&tag
->object
.oid
));
898 printf("%.*s%sdata %d\n%.*s\n",
899 (int)(tagger_end
- tagger
), tagger
,
900 tagger
== tagger_end
? "" : "\n",
901 (int)message_size
, (int)message_size
, message
? message
: "");
905 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, const char *full_name
)
907 switch (e
->item
->type
) {
909 return (struct commit
*)e
->item
;
911 struct tag
*tag
= (struct tag
*)e
->item
;
913 /* handle nested tags */
914 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
915 parse_object(the_repository
, &tag
->object
.oid
);
916 string_list_append(&tag_refs
, full_name
)->util
= tag
;
917 tag
= (struct tag
*)tag
->tagged
;
920 die("Tag %s points nowhere?", e
->name
);
921 return (struct commit
*)tag
;
928 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
932 for (i
= 0; i
< info
->nr
; i
++) {
933 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
934 struct object_id oid
;
935 struct commit
*commit
;
936 char *full_name
= NULL
;
938 if (e
->flags
& UNINTERESTING
)
941 if (repo_dwim_ref(the_repository
, e
->name
, strlen(e
->name
),
942 &oid
, &full_name
, 0) != 1) {
949 private = apply_refspecs(&refspecs
, full_name
);
956 commit
= get_commit(e
, full_name
);
958 warning("%s: Unexpected object of type %s, skipping.",
960 type_name(e
->item
->type
));
965 switch(commit
->object
.type
) {
969 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
));
980 * Make sure this ref gets properly updated eventually, whether
981 * through a commit or manually at the end.
983 if (e
->item
->type
!= OBJ_TAG
)
984 string_list_append(&extra_refs
, full_name
)->util
= commit
;
986 if (!*revision_sources_at(&revision_sources
, commit
))
987 *revision_sources_at(&revision_sources
, commit
) = full_name
;
992 string_list_sort(&extra_refs
);
993 string_list_remove_duplicates(&extra_refs
, 0);
996 static void handle_tags_and_duplicates(struct string_list
*extras
)
998 struct commit
*commit
;
1001 for (i
= extras
->nr
- 1; i
>= 0; i
--) {
1002 const char *name
= extras
->items
[i
].string
;
1003 struct object
*object
= extras
->items
[i
].util
;
1006 switch (object
->type
) {
1008 handle_tag(name
, (struct tag
*)object
);
1012 name
= anonymize_refname(name
);
1013 /* create refs pointing to already seen commits */
1014 commit
= rewrite_commit((struct commit
*)object
);
1017 * Neither this object nor any of its
1018 * ancestors touch any relevant paths, so
1019 * it has been filtered to nothing. Delete
1022 printf("reset %s\nfrom %s\n\n",
1023 name
, oid_to_hex(null_oid()));
1027 mark
= get_object_mark(&commit
->object
);
1030 * Getting here means we have a commit which
1031 * was excluded by a negative refspec (e.g.
1032 * fast-export ^HEAD HEAD). If we are
1033 * referencing excluded commits, set the ref
1034 * to the exact commit. Otherwise, the user
1035 * wants the branch exported but every commit
1036 * in its history to be deleted, which basically
1037 * just means deletion of the ref.
1039 if (!reference_excluded_commits
) {
1040 /* delete the ref */
1041 printf("reset %s\nfrom %s\n\n",
1042 name
, oid_to_hex(null_oid()));
1045 /* set ref to commit using oid, not mark */
1046 printf("reset %s\nfrom %s\n\n", name
,
1047 oid_to_hex(&commit
->object
.oid
));
1051 printf("reset %s\nfrom :%d\n\n", name
, mark
1059 static void export_marks(char *file
)
1063 struct decoration_entry
*deco
= idnums
.entries
;
1067 f
= fopen_for_writing(file
);
1069 die_errno("Unable to open marks file %s for writing.", file
);
1071 for (i
= 0; i
< idnums
.size
; i
++) {
1072 if (deco
->base
&& deco
->base
->type
== 1) {
1073 mark
= ptr_to_mark(deco
->decoration
);
1074 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
1075 oid_to_hex(&deco
->base
->oid
)) < 0) {
1086 error("Unable to write marks file %s.", file
);
1089 static void import_marks(char *input_file
, int check_exists
)
1095 if (check_exists
&& stat(input_file
, &sb
))
1098 f
= xfopen(input_file
, "r");
1099 while (fgets(line
, sizeof(line
), f
)) {
1101 char *line_end
, *mark_end
;
1102 struct object_id oid
;
1103 struct object
*object
;
1104 struct commit
*commit
;
1105 enum object_type type
;
1107 line_end
= strchr(line
, '\n');
1108 if (line
[0] != ':' || !line_end
)
1109 die("corrupt mark line: %s", line
);
1112 mark
= strtoumax(line
+ 1, &mark_end
, 10);
1113 if (!mark
|| mark_end
== line
+ 1
1114 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
1115 die("corrupt mark line: %s", line
);
1117 if (last_idnum
< mark
)
1120 type
= oid_object_info(the_repository
, &oid
, NULL
);
1122 die("object not found: %s", oid_to_hex(&oid
));
1124 if (type
!= OBJ_COMMIT
)
1128 commit
= lookup_commit(the_repository
, &oid
);
1130 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
1132 object
= &commit
->object
;
1134 if (object
->flags
& SHOWN
)
1135 error("Object %s already has a mark", oid_to_hex(&oid
));
1137 mark_object(object
, mark
);
1139 object
->flags
|= SHOWN
;
1144 static void handle_deletes(void)
1147 for (i
= 0; i
< refspecs
.nr
; i
++) {
1148 struct refspec_item
*refspec
= &refspecs
.items
[i
];
1152 printf("reset %s\nfrom %s\n\n",
1153 refspec
->dst
, oid_to_hex(null_oid()));
1157 static int parse_opt_anonymize_map(const struct option
*opt
,
1158 const char *arg
, int unset
)
1160 struct hashmap
*map
= opt
->value
;
1161 const char *delim
, *value
;
1164 BUG_ON_OPT_NEG(unset
);
1166 delim
= strchr(arg
, ':');
1168 keylen
= delim
- arg
;
1171 keylen
= strlen(arg
);
1175 if (!keylen
|| !*value
)
1176 return error(_("--anonymize-map token cannot be empty"));
1178 add_anonymized_entry(map
, memhash(arg
, keylen
), arg
, keylen
,
1184 int cmd_fast_export(int argc
,
1187 struct repository
*repo UNUSED
)
1189 struct rev_info revs
;
1190 struct commit
*commit
;
1191 char *export_filename
= NULL
,
1192 *import_filename
= NULL
,
1193 *import_filename_if_exists
= NULL
;
1194 uint32_t lastimportid
;
1195 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
1196 struct string_list paths_of_changed_objects
= STRING_LIST_INIT_DUP
;
1197 struct option options
[] = {
1198 OPT_INTEGER(0, "progress", &progress
,
1199 N_("show progress after <n> objects")),
1200 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
1201 N_("select handling of signed tags"),
1202 parse_opt_signed_tag_mode
),
1203 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
1204 N_("select handling of tags that tag filtered objects"),
1205 parse_opt_tag_of_filtered_mode
),
1206 OPT_CALLBACK(0, "reencode", &reencode_mode
, N_("mode"),
1207 N_("select handling of commit messages in an alternate encoding"),
1208 parse_opt_reencode_mode
),
1209 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
1210 N_("dump marks to this file")),
1211 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
1212 N_("import marks from this file")),
1213 OPT_STRING(0, "import-marks-if-exists",
1214 &import_filename_if_exists
,
1216 N_("import marks from this file if it exists")),
1217 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
1218 N_("fake a tagger when tags lack one")),
1219 OPT_BOOL(0, "full-tree", &full_tree
,
1220 N_("output full tree for each commit")),
1221 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
1222 N_("use the done feature to terminate the stream")),
1223 OPT_BOOL(0, "no-data", &no_data
, N_("skip output of blob data")),
1224 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1225 N_("apply refspec to exported refs")),
1226 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1227 OPT_CALLBACK_F(0, "anonymize-map", &anonymized_seeds
, N_("from:to"),
1228 N_("convert <from> to <to> in anonymized output"),
1229 PARSE_OPT_NONEG
, parse_opt_anonymize_map
),
1230 OPT_BOOL(0, "reference-excluded-parents",
1231 &reference_excluded_commits
, N_("reference parents which are not in fast-export stream by object id")),
1232 OPT_BOOL(0, "show-original-ids", &show_original_ids
,
1233 N_("show original object ids of blobs/commits")),
1234 OPT_BOOL(0, "mark-tags", &mark_tags
,
1235 N_("label tags with mark ids")),
1241 usage_with_options (fast_export_usage
, options
);
1243 /* we handle encodings */
1244 git_config(git_default_config
, NULL
);
1246 repo_init_revisions(the_repository
, &revs
, prefix
);
1247 init_revision_sources(&revision_sources
);
1248 revs
.topo_order
= 1;
1249 revs
.sources
= &revision_sources
;
1250 revs
.rewrite_parents
= 1;
1251 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1252 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
);
1253 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1255 usage_with_options (fast_export_usage
, options
);
1257 if (anonymized_seeds
.cmpfn
&& !anonymize
)
1258 die(_("the option '%s' requires '%s'"), "--anonymize-map", "--anonymize");
1260 if (refspecs_list
.nr
) {
1263 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1264 refspec_append(&refspecs
, refspecs_list
.items
[i
].string
);
1266 string_list_clear(&refspecs_list
, 1);
1269 if (use_done_feature
)
1270 printf("feature done\n");
1272 if (import_filename
&& import_filename_if_exists
)
1273 die(_("options '%s' and '%s' cannot be used together"), "--import-marks", "--import-marks-if-exists");
1274 if (import_filename
)
1275 import_marks(import_filename
, 0);
1276 else if (import_filename_if_exists
)
1277 import_marks(import_filename_if_exists
, 1);
1278 lastimportid
= last_idnum
;
1280 if (import_filename
&& revs
.prune_data
.nr
)
1283 get_tags_and_duplicates(&revs
.cmdline
);
1285 if (prepare_revision_walk(&revs
))
1286 die("revision walk setup failed");
1289 revs
.diffopt
.format_callback
= show_filemodify
;
1290 revs
.diffopt
.format_callback_data
= &paths_of_changed_objects
;
1291 revs
.diffopt
.flags
.recursive
= 1;
1293 revs
.diffopt
.no_free
= 1;
1294 while ((commit
= get_revision(&revs
)))
1295 handle_commit(commit
, &revs
, &paths_of_changed_objects
);
1296 revs
.diffopt
.no_free
= 0;
1298 handle_tags_and_duplicates(&extra_refs
);
1299 handle_tags_and_duplicates(&tag_refs
);
1302 if (export_filename
&& lastimportid
!= last_idnum
)
1303 export_marks(export_filename
);
1305 if (use_done_feature
)
1308 refspec_clear(&refspecs
);
1309 release_revisions(&revs
);