2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
12 #include "object-file.h"
13 #include "object-store-ll.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_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_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 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
= !strncmp(a
->orig
, key
->orig
, key
->orig_len
) &&
141 !a
->orig
[key
->orig_len
];
145 b
= container_of(entry_or_key
, const struct anonymized_entry
, hash
);
146 return strcmp(a
->orig
, b
->orig
);
149 static struct anonymized_entry
*add_anonymized_entry(struct hashmap
*map
,
151 const char *orig
, size_t len
,
154 struct anonymized_entry
*ret
, *old
;
157 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
159 FLEX_ALLOC_MEM(ret
, orig
, orig
, len
);
160 hashmap_entry_init(&ret
->hash
, hash
);
162 old
= hashmap_put_entry(map
, ret
, hash
);
173 * Basically keep a cache of X->Y so that we can repeatedly replace
174 * the same anonymized string with another. The actual generation
175 * is farmed out to the generate function.
177 static const char *anonymize_str(struct hashmap
*map
,
178 char *(*generate
)(void),
179 const char *orig
, size_t len
)
181 struct anonymized_entry_key key
;
182 struct anonymized_entry
*ret
;
184 hashmap_entry_init(&key
.hash
, memhash(orig
, len
));
188 /* First check if it's a token the user configured manually... */
189 ret
= hashmap_get_entry(&anonymized_seeds
, &key
, hash
, &key
);
191 /* ...otherwise check if we've already seen it in this context... */
193 ret
= hashmap_get_entry(map
, &key
, hash
, &key
);
195 /* ...and finally generate a new mapping if necessary */
197 ret
= add_anonymized_entry(map
, key
.hash
.hash
,
198 orig
, len
, generate());
204 * We anonymize each component of a path individually,
205 * so that paths a/b and a/c will share a common root.
206 * The paths are cached via anonymize_mem so that repeated
207 * lookups for "a" will yield the same value.
209 static void anonymize_path(struct strbuf
*out
, const char *path
,
211 char *(*generate
)(void))
214 const char *end_of_component
= strchrnul(path
, '/');
215 size_t len
= end_of_component
- path
;
216 const char *c
= anonymize_str(map
, generate
, path
, len
);
217 strbuf_addstr(out
, c
);
218 path
= end_of_component
;
220 strbuf_addch(out
, *path
++);
224 static inline void *mark_to_ptr(uint32_t mark
)
226 return (void *)(uintptr_t)mark
;
229 static inline uint32_t ptr_to_mark(void * mark
)
231 return (uint32_t)(uintptr_t)mark
;
234 static inline void mark_object(struct object
*object
, uint32_t mark
)
236 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
239 static inline void mark_next_object(struct object
*object
)
241 mark_object(object
, ++last_idnum
);
244 static int get_object_mark(struct object
*object
)
246 void *decoration
= lookup_decoration(&idnums
, object
);
249 return ptr_to_mark(decoration
);
252 static struct commit
*rewrite_commit(struct commit
*p
)
255 if (p
->parents
&& p
->parents
->next
)
257 if (p
->object
.flags
& UNINTERESTING
)
259 if (!(p
->object
.flags
& TREESAME
))
263 p
= p
->parents
->item
;
268 static void show_progress(void)
270 static int counter
= 0;
273 if ((++counter
% progress
) == 0)
274 printf("progress %d objects\n", counter
);
278 * Ideally we would want some transformation of the blob data here
279 * that is unreversible, but would still be the same size and have
280 * the same data relationship to other blobs (so that we get the same
281 * delta and packing behavior as the original). But the first and last
282 * requirements there are probably mutually exclusive, so let's take
283 * the easy way out for now, and just generate arbitrary content.
285 * There's no need to cache this result with anonymize_mem, since
286 * we already handle blob content caching with marks.
288 static char *anonymize_blob(unsigned long *size
)
291 struct strbuf out
= STRBUF_INIT
;
292 strbuf_addf(&out
, "anonymous blob %d", counter
++);
294 return strbuf_detach(&out
, NULL
);
297 static void export_blob(const struct object_id
*oid
)
300 enum object_type type
;
302 struct object
*object
;
308 if (is_null_oid(oid
))
311 object
= lookup_object(the_repository
, oid
);
312 if (object
&& object
->flags
& SHOWN
)
316 buf
= anonymize_blob(&size
);
317 object
= (struct object
*)lookup_blob(the_repository
, oid
);
320 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
322 die("could not read blob %s", oid_to_hex(oid
));
323 if (check_object_signature(the_repository
, oid
, buf
, size
,
325 die("oid mismatch in blob %s", oid_to_hex(oid
));
326 object
= parse_object_buffer(the_repository
, oid
, type
,
331 die("Could not read blob %s", oid_to_hex(oid
));
333 mark_next_object(object
);
335 printf("blob\nmark :%"PRIu32
"\n", last_idnum
);
336 if (show_original_ids
)
337 printf("original-oid %s\n", oid_to_hex(oid
));
338 printf("data %"PRIuMAX
"\n", (uintmax_t)size
);
339 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
340 die_errno("could not write blob '%s'", oid_to_hex(oid
));
345 object
->flags
|= SHOWN
;
350 static int depth_first(const void *a_
, const void *b_
)
352 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
353 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
354 const char *name_a
, *name_b
;
355 int len_a
, len_b
, len
;
358 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
359 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
361 len_a
= strlen(name_a
);
362 len_b
= strlen(name_b
);
363 len
= (len_a
< len_b
) ? len_a
: len_b
;
365 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
366 cmp
= memcmp(name_a
, name_b
, len
);
373 * Move 'R'ename entries last so that all references of the file
374 * appear in the output before it is renamed (e.g., when a file
375 * was copied and renamed in the same commit).
377 return (a
->status
== 'R') - (b
->status
== 'R');
380 static void print_path_1(const char *path
)
382 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
384 quote_c_style(path
, NULL
, stdout
, 0);
385 else if (strchr(path
, ' '))
386 printf("\"%s\"", path
);
391 static char *anonymize_path_component(void)
394 struct strbuf out
= STRBUF_INIT
;
395 strbuf_addf(&out
, "path%d", counter
++);
396 return strbuf_detach(&out
, NULL
);
399 static void print_path(const char *path
)
404 static struct hashmap paths
;
405 static struct strbuf anon
= STRBUF_INIT
;
407 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
408 print_path_1(anon
.buf
);
413 static char *generate_fake_oid(void)
415 static uint32_t counter
= 1; /* avoid null oid */
416 const unsigned hashsz
= the_hash_algo
->rawsz
;
417 struct object_id oid
;
418 char *hex
= xmallocz(GIT_MAX_HEXSZ
);
421 put_be32(oid
.hash
+ hashsz
- 4, counter
++);
422 return oid_to_hex_r(hex
, &oid
);
425 static const char *anonymize_oid(const char *oid_hex
)
427 static struct hashmap objs
;
428 size_t len
= strlen(oid_hex
);
429 return anonymize_str(&objs
, generate_fake_oid
, oid_hex
, len
);
432 static void show_filemodify(struct diff_queue_struct
*q
,
433 struct diff_options
*options UNUSED
, void *data
)
436 struct string_list
*changed
= data
;
439 * Handle files below a directory first, in case they are all deleted
440 * and the directory changes to a file or symlink.
442 QSORT(q
->queue
, q
->nr
, depth_first
);
444 for (i
= 0; i
< q
->nr
; i
++) {
445 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
446 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
448 switch (q
->queue
[i
]->status
) {
449 case DIFF_STATUS_DELETED
:
451 print_path(spec
->path
);
452 string_list_insert(changed
, spec
->path
);
456 case DIFF_STATUS_COPIED
:
457 case DIFF_STATUS_RENAMED
:
459 * If a change in the file corresponding to ospec->path
460 * has been observed, we cannot trust its contents
461 * because the diff is calculated based on the prior
462 * contents, not the current contents. So, declare a
463 * copy or rename only if there was no change observed.
465 if (!string_list_has_string(changed
, ospec
->path
)) {
466 printf("%c ", q
->queue
[i
]->status
);
467 print_path(ospec
->path
);
469 print_path(spec
->path
);
470 string_list_insert(changed
, spec
->path
);
473 if (oideq(&ospec
->oid
, &spec
->oid
) &&
474 ospec
->mode
== spec
->mode
)
479 case DIFF_STATUS_TYPE_CHANGED
:
480 case DIFF_STATUS_MODIFIED
:
481 case DIFF_STATUS_ADDED
:
483 * Links refer to objects in another repositories;
484 * output the SHA-1 verbatim.
486 if (no_data
|| S_ISGITLINK(spec
->mode
))
487 printf("M %06o %s ", spec
->mode
,
489 anonymize_oid(oid_to_hex(&spec
->oid
)) :
490 oid_to_hex(&spec
->oid
));
492 struct object
*object
= lookup_object(the_repository
,
494 printf("M %06o :%d ", spec
->mode
,
495 get_object_mark(object
));
497 print_path(spec
->path
);
498 string_list_insert(changed
, spec
->path
);
503 die("Unexpected comparison status '%c' for %s, %s",
505 ospec
->path
? ospec
->path
: "none",
506 spec
->path
? spec
->path
: "none");
511 static const char *find_encoding(const char *begin
, const char *end
)
513 const char *needle
= "\nencoding ";
516 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
517 needle
, strlen(needle
));
520 bol
+= strlen(needle
);
521 eol
= strchrnul(bol
, '\n');
526 static char *anonymize_ref_component(void)
529 struct strbuf out
= STRBUF_INIT
;
530 strbuf_addf(&out
, "ref%d", counter
++);
531 return strbuf_detach(&out
, NULL
);
534 static const char *anonymize_refname(const char *refname
)
537 * If any of these prefixes is found, we will leave it intact
538 * so that tags remain tags and so forth.
540 static const char *prefixes
[] = {
546 static struct hashmap refs
;
547 static struct strbuf anon
= STRBUF_INIT
;
551 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
552 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
553 strbuf_addstr(&anon
, prefixes
[i
]);
558 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
563 * We do not even bother to cache commit messages, as they are unlikely
564 * to be repeated verbatim, and it is not that interesting when they are.
566 static char *anonymize_commit_message(void)
569 return xstrfmt("subject %d\n\nbody\n", counter
++);
572 static char *anonymize_ident(void)
575 struct strbuf out
= STRBUF_INIT
;
576 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
578 return strbuf_detach(&out
, NULL
);
582 * Our strategy here is to anonymize the names and email addresses,
583 * but keep timestamps intact, as they influence things like traversal
584 * order (and by themselves should not be too revealing).
586 static void anonymize_ident_line(const char **beg
, const char **end
)
588 static struct hashmap idents
;
589 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
590 static unsigned which_buffer
;
593 struct ident_split split
;
594 const char *end_of_header
;
596 out
= &buffers
[which_buffer
++];
597 which_buffer
%= ARRAY_SIZE(buffers
);
600 /* skip "committer", "author", "tagger", etc */
601 end_of_header
= strchr(*beg
, ' ');
603 BUG("malformed line fed to anonymize_ident_line: %.*s",
604 (int)(*end
- *beg
), *beg
);
606 strbuf_add(out
, *beg
, end_of_header
- *beg
);
608 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
613 len
= split
.mail_end
- split
.name_begin
;
614 ident
= anonymize_str(&idents
, anonymize_ident
,
615 split
.name_begin
, len
);
616 strbuf_addstr(out
, ident
);
617 strbuf_addch(out
, ' ');
618 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
620 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
624 *end
= out
->buf
+ out
->len
;
627 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
,
628 struct string_list
*paths_of_changed_objects
)
630 int saved_output_format
= rev
->diffopt
.output_format
;
631 const char *commit_buffer
;
632 const char *author
, *author_end
, *committer
, *committer_end
;
633 const char *encoding
, *message
;
634 char *reencoded
= NULL
;
635 struct commit_list
*p
;
639 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
641 parse_commit_or_die(commit
);
642 commit_buffer
= repo_get_commit_buffer(the_repository
, commit
, NULL
);
643 author
= strstr(commit_buffer
, "\nauthor ");
645 die("could not find author in commit %s",
646 oid_to_hex(&commit
->object
.oid
));
648 author_end
= strchrnul(author
, '\n');
649 committer
= strstr(author_end
, "\ncommitter ");
651 die("could not find committer in commit %s",
652 oid_to_hex(&commit
->object
.oid
));
654 committer_end
= strchrnul(committer
, '\n');
655 message
= strstr(committer_end
, "\n\n");
656 encoding
= find_encoding(committer_end
, message
);
660 if (commit
->parents
&&
661 (get_object_mark(&commit
->parents
->item
->object
) != 0 ||
662 reference_excluded_commits
) &&
664 parse_commit_or_die(commit
->parents
->item
);
665 diff_tree_oid(get_commit_tree_oid(commit
->parents
->item
),
666 get_commit_tree_oid(commit
), "", &rev
->diffopt
);
669 diff_root_tree_oid(get_commit_tree_oid(commit
),
672 /* Export the referenced blobs, and remember the marks. */
673 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
674 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
675 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
677 refname
= *revision_sources_at(&revision_sources
, commit
);
679 * FIXME: string_list_remove() below for each ref is overall
680 * O(N^2). Compared to a history walk and diffing trees, this is
681 * just lost in the noise in practice. However, theoretically a
682 * repo may have enough refs for this to become slow.
684 string_list_remove(&extra_refs
, refname
, 0);
686 refname
= anonymize_refname(refname
);
687 anonymize_ident_line(&committer
, &committer_end
);
688 anonymize_ident_line(&author
, &author_end
);
691 mark_next_object(&commit
->object
);
693 reencoded
= anonymize_commit_message();
694 } else if (encoding
) {
695 switch(reencode_mode
) {
697 reencoded
= reencode_string(message
, "UTF-8", encoding
);
702 die("Encountered commit-specific encoding %s in commit "
703 "%s; use --reencode=[yes|no] to handle it",
704 encoding
, oid_to_hex(&commit
->object
.oid
));
707 if (!commit
->parents
)
708 printf("reset %s\n", refname
);
709 printf("commit %s\nmark :%"PRIu32
"\n", refname
, last_idnum
);
710 if (show_original_ids
)
711 printf("original-oid %s\n", oid_to_hex(&commit
->object
.oid
));
712 printf("%.*s\n%.*s\n",
713 (int)(author_end
- author
), author
,
714 (int)(committer_end
- committer
), committer
);
715 if (!reencoded
&& encoding
)
716 printf("encoding %s\n", encoding
);
717 printf("data %u\n%s",
719 ? strlen(reencoded
) : message
720 ? strlen(message
) : 0),
721 reencoded
? reencoded
: message
? message
: "");
723 repo_unuse_commit_buffer(the_repository
, commit
, commit_buffer
);
725 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
726 struct object
*obj
= &p
->item
->object
;
727 int mark
= get_object_mark(obj
);
729 if (!mark
&& !reference_excluded_commits
)
736 printf(":%d\n", mark
);
740 anonymize_oid(oid_to_hex(&obj
->oid
)) :
741 oid_to_hex(&obj
->oid
));
746 printf("deleteall\n");
747 log_tree_diff_flush(rev
);
748 string_list_clear(paths_of_changed_objects
, 0);
749 rev
->diffopt
.output_format
= saved_output_format
;
756 static char *anonymize_tag(void)
759 struct strbuf out
= STRBUF_INIT
;
760 strbuf_addf(&out
, "tag message %d", counter
++);
761 return strbuf_detach(&out
, NULL
);
765 static void handle_tag(const char *name
, struct tag
*tag
)
768 enum object_type type
;
770 const char *tagger
, *tagger_end
, *message
;
771 size_t message_size
= 0;
772 struct object
*tagged
;
776 /* Trees have no identifier in fast-export output, thus we have no way
777 * to output tags of trees, tags of tags of trees, etc. Simply omit
780 tagged
= tag
->tagged
;
781 while (tagged
->type
== OBJ_TAG
) {
782 tagged
= ((struct tag
*)tagged
)->tagged
;
784 if (tagged
->type
== OBJ_TREE
) {
785 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
786 oid_to_hex(&tag
->object
.oid
));
790 buf
= repo_read_object_file(the_repository
, &tag
->object
.oid
, &type
,
793 die("could not read tag %s", oid_to_hex(&tag
->object
.oid
));
794 message
= memmem(buf
, size
, "\n\n", 2);
797 message_size
= strlen(message
);
799 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
801 if (fake_missing_tagger
)
802 tagger
= "tagger Unspecified Tagger "
803 "<unspecified-tagger> 0 +0000";
806 tagger_end
= tagger
+ strlen(tagger
);
809 tagger_end
= strchrnul(tagger
, '\n');
811 anonymize_ident_line(&tagger
, &tagger_end
);
815 name
= anonymize_refname(name
);
817 static struct hashmap tags
;
818 message
= anonymize_str(&tags
, anonymize_tag
,
819 message
, message_size
);
820 message_size
= strlen(message
);
824 /* handle signed tags */
826 const char *signature
= strstr(message
,
827 "\n-----BEGIN PGP SIGNATURE-----\n");
829 switch(signed_tag_mode
) {
830 case SIGNED_TAG_ABORT
:
831 die("encountered signed tag %s; use "
832 "--signed-tags=<mode> to handle it",
833 oid_to_hex(&tag
->object
.oid
));
835 warning("exporting signed tag %s",
836 oid_to_hex(&tag
->object
.oid
));
841 warning("stripping signature from tag %s",
842 oid_to_hex(&tag
->object
.oid
));
845 message_size
= signature
+ 1 - message
;
850 /* handle tag->tagged having been filtered out due to paths specified */
851 tagged
= tag
->tagged
;
852 tagged_mark
= get_object_mark(tagged
);
854 switch(tag_of_filtered_mode
) {
855 case TAG_FILTERING_ABORT
:
856 die("tag %s tags unexported object; use "
857 "--tag-of-filtered-object=<mode> to handle it",
858 oid_to_hex(&tag
->object
.oid
));
860 /* Ignore this tag altogether */
864 if (tagged
->type
== OBJ_TAG
&& !mark_tags
) {
865 die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
866 } else if (tagged
->type
== OBJ_COMMIT
) {
867 p
= rewrite_commit((struct commit
*)tagged
);
869 printf("reset %s\nfrom %s\n\n",
870 name
, oid_to_hex(null_oid()));
874 tagged_mark
= get_object_mark(&p
->object
);
876 /* tagged->type is either OBJ_BLOB or OBJ_TAG */
877 tagged_mark
= get_object_mark(tagged
);
882 if (tagged
->type
== OBJ_TAG
) {
883 printf("reset %s\nfrom %s\n\n",
884 name
, oid_to_hex(null_oid()));
886 skip_prefix(name
, "refs/tags/", &name
);
887 printf("tag %s\n", name
);
889 mark_next_object(&tag
->object
);
890 printf("mark :%"PRIu32
"\n", last_idnum
);
893 printf("from :%d\n", tagged_mark
);
895 printf("from %s\n", oid_to_hex(&tagged
->oid
));
897 if (show_original_ids
)
898 printf("original-oid %s\n", oid_to_hex(&tag
->object
.oid
));
899 printf("%.*s%sdata %d\n%.*s\n",
900 (int)(tagger_end
- tagger
), tagger
,
901 tagger
== tagger_end
? "" : "\n",
902 (int)message_size
, (int)message_size
, message
? message
: "");
906 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
908 switch (e
->item
->type
) {
910 return (struct commit
*)e
->item
;
912 struct tag
*tag
= (struct tag
*)e
->item
;
914 /* handle nested tags */
915 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
916 parse_object(the_repository
, &tag
->object
.oid
);
917 string_list_append(&tag_refs
, full_name
)->util
= tag
;
918 tag
= (struct tag
*)tag
->tagged
;
921 die("Tag %s points nowhere?", e
->name
);
922 return (struct commit
*)tag
;
929 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
933 for (i
= 0; i
< info
->nr
; i
++) {
934 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
935 struct object_id oid
;
936 struct commit
*commit
;
939 if (e
->flags
& UNINTERESTING
)
942 if (repo_dwim_ref(the_repository
, e
->name
, strlen(e
->name
),
943 &oid
, &full_name
, 0) != 1)
948 private = apply_refspecs(&refspecs
, full_name
);
955 commit
= get_commit(e
, full_name
);
957 warning("%s: Unexpected object of type %s, skipping.",
959 type_name(e
->item
->type
));
963 switch(commit
->object
.type
) {
967 export_blob(&commit
->object
.oid
);
969 default: /* OBJ_TAG (nested tags) is already handled */
970 warning("Tag points to object of unexpected type %s, skipping.",
971 type_name(commit
->object
.type
));
976 * Make sure this ref gets properly updated eventually, whether
977 * through a commit or manually at the end.
979 if (e
->item
->type
!= OBJ_TAG
)
980 string_list_append(&extra_refs
, full_name
)->util
= commit
;
982 if (!*revision_sources_at(&revision_sources
, commit
))
983 *revision_sources_at(&revision_sources
, commit
) = full_name
;
986 string_list_sort(&extra_refs
);
987 string_list_remove_duplicates(&extra_refs
, 0);
990 static void handle_tags_and_duplicates(struct string_list
*extras
)
992 struct commit
*commit
;
995 for (i
= extras
->nr
- 1; i
>= 0; i
--) {
996 const char *name
= extras
->items
[i
].string
;
997 struct object
*object
= extras
->items
[i
].util
;
1000 switch (object
->type
) {
1002 handle_tag(name
, (struct tag
*)object
);
1006 name
= anonymize_refname(name
);
1007 /* create refs pointing to already seen commits */
1008 commit
= rewrite_commit((struct commit
*)object
);
1011 * Neither this object nor any of its
1012 * ancestors touch any relevant paths, so
1013 * it has been filtered to nothing. Delete
1016 printf("reset %s\nfrom %s\n\n",
1017 name
, oid_to_hex(null_oid()));
1021 mark
= get_object_mark(&commit
->object
);
1024 * Getting here means we have a commit which
1025 * was excluded by a negative refspec (e.g.
1026 * fast-export ^HEAD HEAD). If we are
1027 * referencing excluded commits, set the ref
1028 * to the exact commit. Otherwise, the user
1029 * wants the branch exported but every commit
1030 * in its history to be deleted, which basically
1031 * just means deletion of the ref.
1033 if (!reference_excluded_commits
) {
1034 /* delete the ref */
1035 printf("reset %s\nfrom %s\n\n",
1036 name
, oid_to_hex(null_oid()));
1039 /* set ref to commit using oid, not mark */
1040 printf("reset %s\nfrom %s\n\n", name
,
1041 oid_to_hex(&commit
->object
.oid
));
1045 printf("reset %s\nfrom :%d\n\n", name
, mark
1053 static void export_marks(char *file
)
1057 struct decoration_entry
*deco
= idnums
.entries
;
1061 f
= fopen_for_writing(file
);
1063 die_errno("Unable to open marks file %s for writing.", file
);
1065 for (i
= 0; i
< idnums
.size
; i
++) {
1066 if (deco
->base
&& deco
->base
->type
== 1) {
1067 mark
= ptr_to_mark(deco
->decoration
);
1068 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
1069 oid_to_hex(&deco
->base
->oid
)) < 0) {
1080 error("Unable to write marks file %s.", file
);
1083 static void import_marks(char *input_file
, int check_exists
)
1089 if (check_exists
&& stat(input_file
, &sb
))
1092 f
= xfopen(input_file
, "r");
1093 while (fgets(line
, sizeof(line
), f
)) {
1095 char *line_end
, *mark_end
;
1096 struct object_id oid
;
1097 struct object
*object
;
1098 struct commit
*commit
;
1099 enum object_type type
;
1101 line_end
= strchr(line
, '\n');
1102 if (line
[0] != ':' || !line_end
)
1103 die("corrupt mark line: %s", line
);
1106 mark
= strtoumax(line
+ 1, &mark_end
, 10);
1107 if (!mark
|| mark_end
== line
+ 1
1108 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
1109 die("corrupt mark line: %s", line
);
1111 if (last_idnum
< mark
)
1114 type
= oid_object_info(the_repository
, &oid
, NULL
);
1116 die("object not found: %s", oid_to_hex(&oid
));
1118 if (type
!= OBJ_COMMIT
)
1122 commit
= lookup_commit(the_repository
, &oid
);
1124 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
1126 object
= &commit
->object
;
1128 if (object
->flags
& SHOWN
)
1129 error("Object %s already has a mark", oid_to_hex(&oid
));
1131 mark_object(object
, mark
);
1133 object
->flags
|= SHOWN
;
1138 static void handle_deletes(void)
1141 for (i
= 0; i
< refspecs
.nr
; i
++) {
1142 struct refspec_item
*refspec
= &refspecs
.items
[i
];
1146 printf("reset %s\nfrom %s\n\n",
1147 refspec
->dst
, oid_to_hex(null_oid()));
1151 static int parse_opt_anonymize_map(const struct option
*opt
,
1152 const char *arg
, int unset
)
1154 struct hashmap
*map
= opt
->value
;
1155 const char *delim
, *value
;
1158 BUG_ON_OPT_NEG(unset
);
1160 delim
= strchr(arg
, ':');
1162 keylen
= delim
- arg
;
1165 keylen
= strlen(arg
);
1169 if (!keylen
|| !*value
)
1170 return error(_("--anonymize-map token cannot be empty"));
1172 add_anonymized_entry(map
, memhash(arg
, keylen
), arg
, keylen
,
1178 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
1180 struct rev_info revs
;
1181 struct commit
*commit
;
1182 char *export_filename
= NULL
,
1183 *import_filename
= NULL
,
1184 *import_filename_if_exists
= NULL
;
1185 uint32_t lastimportid
;
1186 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
1187 struct string_list paths_of_changed_objects
= STRING_LIST_INIT_DUP
;
1188 struct option options
[] = {
1189 OPT_INTEGER(0, "progress", &progress
,
1190 N_("show progress after <n> objects")),
1191 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
1192 N_("select handling of signed tags"),
1193 parse_opt_signed_tag_mode
),
1194 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
1195 N_("select handling of tags that tag filtered objects"),
1196 parse_opt_tag_of_filtered_mode
),
1197 OPT_CALLBACK(0, "reencode", &reencode_mode
, N_("mode"),
1198 N_("select handling of commit messages in an alternate encoding"),
1199 parse_opt_reencode_mode
),
1200 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
1201 N_("dump marks to this file")),
1202 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
1203 N_("import marks from this file")),
1204 OPT_STRING(0, "import-marks-if-exists",
1205 &import_filename_if_exists
,
1207 N_("import marks from this file if it exists")),
1208 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
1209 N_("fake a tagger when tags lack one")),
1210 OPT_BOOL(0, "full-tree", &full_tree
,
1211 N_("output full tree for each commit")),
1212 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
1213 N_("use the done feature to terminate the stream")),
1214 OPT_BOOL(0, "no-data", &no_data
, N_("skip output of blob data")),
1215 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1216 N_("apply refspec to exported refs")),
1217 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1218 OPT_CALLBACK_F(0, "anonymize-map", &anonymized_seeds
, N_("from:to"),
1219 N_("convert <from> to <to> in anonymized output"),
1220 PARSE_OPT_NONEG
, parse_opt_anonymize_map
),
1221 OPT_BOOL(0, "reference-excluded-parents",
1222 &reference_excluded_commits
, N_("reference parents which are not in fast-export stream by object id")),
1223 OPT_BOOL(0, "show-original-ids", &show_original_ids
,
1224 N_("show original object ids of blobs/commits")),
1225 OPT_BOOL(0, "mark-tags", &mark_tags
,
1226 N_("label tags with mark ids")),
1232 usage_with_options (fast_export_usage
, options
);
1234 /* we handle encodings */
1235 git_config(git_default_config
, NULL
);
1237 repo_init_revisions(the_repository
, &revs
, prefix
);
1238 init_revision_sources(&revision_sources
);
1239 revs
.topo_order
= 1;
1240 revs
.sources
= &revision_sources
;
1241 revs
.rewrite_parents
= 1;
1242 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1243 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
);
1244 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1246 usage_with_options (fast_export_usage
, options
);
1248 if (anonymized_seeds
.cmpfn
&& !anonymize
)
1249 die(_("the option '%s' requires '%s'"), "--anonymize-map", "--anonymize");
1251 if (refspecs_list
.nr
) {
1254 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1255 refspec_append(&refspecs
, refspecs_list
.items
[i
].string
);
1257 string_list_clear(&refspecs_list
, 1);
1260 if (use_done_feature
)
1261 printf("feature done\n");
1263 if (import_filename
&& import_filename_if_exists
)
1264 die(_("options '%s' and '%s' cannot be used together"), "--import-marks", "--import-marks-if-exists");
1265 if (import_filename
)
1266 import_marks(import_filename
, 0);
1267 else if (import_filename_if_exists
)
1268 import_marks(import_filename_if_exists
, 1);
1269 lastimportid
= last_idnum
;
1271 if (import_filename
&& revs
.prune_data
.nr
)
1274 get_tags_and_duplicates(&revs
.cmdline
);
1276 if (prepare_revision_walk(&revs
))
1277 die("revision walk setup failed");
1280 revs
.diffopt
.format_callback
= show_filemodify
;
1281 revs
.diffopt
.format_callback_data
= &paths_of_changed_objects
;
1282 revs
.diffopt
.flags
.recursive
= 1;
1283 revs
.diffopt
.no_free
= 1;
1284 while ((commit
= get_revision(&revs
)))
1285 handle_commit(commit
, &revs
, &paths_of_changed_objects
);
1287 handle_tags_and_duplicates(&extra_refs
);
1288 handle_tags_and_duplicates(&tag_refs
);
1291 if (export_filename
&& lastimportid
!= last_idnum
)
1292 export_marks(export_filename
);
1294 if (use_done_feature
)
1297 refspec_clear(&refspecs
);
1298 release_revisions(&revs
);