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_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
);
143 static struct anonymized_entry
*add_anonymized_entry(struct hashmap
*map
,
145 const char *orig
, size_t len
,
148 struct anonymized_entry
*ret
, *old
;
151 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
153 FLEX_ALLOC_MEM(ret
, orig
, orig
, len
);
154 hashmap_entry_init(&ret
->hash
, hash
);
156 old
= hashmap_put_entry(map
, ret
, hash
);
167 * Basically keep a cache of X->Y so that we can repeatedly replace
168 * the same anonymized string with another. The actual generation
169 * is farmed out to the generate function.
171 static const char *anonymize_str(struct hashmap
*map
,
172 char *(*generate
)(void),
173 const char *orig
, size_t len
)
175 struct anonymized_entry_key key
;
176 struct anonymized_entry
*ret
;
178 hashmap_entry_init(&key
.hash
, memhash(orig
, len
));
182 /* First check if it's a token the user configured manually... */
183 ret
= hashmap_get_entry(&anonymized_seeds
, &key
, hash
, &key
);
185 /* ...otherwise check if we've already seen it in this context... */
187 ret
= hashmap_get_entry(map
, &key
, hash
, &key
);
189 /* ...and finally generate a new mapping if necessary */
191 ret
= add_anonymized_entry(map
, key
.hash
.hash
,
192 orig
, len
, generate());
198 * We anonymize each component of a path individually,
199 * so that paths a/b and a/c will share a common root.
200 * The paths are cached via anonymize_mem so that repeated
201 * lookups for "a" will yield the same value.
203 static void anonymize_path(struct strbuf
*out
, const char *path
,
205 char *(*generate
)(void))
208 const char *end_of_component
= strchrnul(path
, '/');
209 size_t len
= end_of_component
- path
;
210 const char *c
= anonymize_str(map
, generate
, path
, len
);
211 strbuf_addstr(out
, c
);
212 path
= end_of_component
;
214 strbuf_addch(out
, *path
++);
218 static inline void *mark_to_ptr(uint32_t mark
)
220 return (void *)(uintptr_t)mark
;
223 static inline uint32_t ptr_to_mark(void * mark
)
225 return (uint32_t)(uintptr_t)mark
;
228 static inline void mark_object(struct object
*object
, uint32_t mark
)
230 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
233 static inline void mark_next_object(struct object
*object
)
235 mark_object(object
, ++last_idnum
);
238 static int get_object_mark(struct object
*object
)
240 void *decoration
= lookup_decoration(&idnums
, object
);
243 return ptr_to_mark(decoration
);
246 static struct commit
*rewrite_commit(struct commit
*p
)
249 if (p
->parents
&& p
->parents
->next
)
251 if (p
->object
.flags
& UNINTERESTING
)
253 if (!(p
->object
.flags
& TREESAME
))
257 p
= p
->parents
->item
;
262 static void show_progress(void)
264 static int counter
= 0;
267 if ((++counter
% progress
) == 0)
268 printf("progress %d objects\n", counter
);
272 * Ideally we would want some transformation of the blob data here
273 * that is unreversible, but would still be the same size and have
274 * the same data relationship to other blobs (so that we get the same
275 * delta and packing behavior as the original). But the first and last
276 * requirements there are probably mutually exclusive, so let's take
277 * the easy way out for now, and just generate arbitrary content.
279 * There's no need to cache this result with anonymize_mem, since
280 * we already handle blob content caching with marks.
282 static char *anonymize_blob(unsigned long *size
)
285 struct strbuf out
= STRBUF_INIT
;
286 strbuf_addf(&out
, "anonymous blob %d", counter
++);
288 return strbuf_detach(&out
, NULL
);
291 static void export_blob(const struct object_id
*oid
)
294 enum object_type type
;
296 struct object
*object
;
302 if (is_null_oid(oid
))
305 object
= lookup_object(the_repository
, oid
);
306 if (object
&& object
->flags
& SHOWN
)
310 buf
= anonymize_blob(&size
);
311 object
= (struct object
*)lookup_blob(the_repository
, oid
);
314 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
316 die("could not read blob %s", oid_to_hex(oid
));
317 if (check_object_signature(the_repository
, oid
, buf
, size
,
319 die("oid mismatch in blob %s", oid_to_hex(oid
));
320 object
= parse_object_buffer(the_repository
, oid
, type
,
325 die("Could not read blob %s", oid_to_hex(oid
));
327 mark_next_object(object
);
329 printf("blob\nmark :%"PRIu32
"\n", last_idnum
);
330 if (show_original_ids
)
331 printf("original-oid %s\n", oid_to_hex(oid
));
332 printf("data %"PRIuMAX
"\n", (uintmax_t)size
);
333 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
334 die_errno("could not write blob '%s'", oid_to_hex(oid
));
339 object
->flags
|= SHOWN
;
344 static int depth_first(const void *a_
, const void *b_
)
346 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
347 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
348 const char *name_a
, *name_b
;
349 int len_a
, len_b
, len
;
352 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
353 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
355 len_a
= strlen(name_a
);
356 len_b
= strlen(name_b
);
357 len
= (len_a
< len_b
) ? len_a
: len_b
;
359 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
360 cmp
= memcmp(name_a
, name_b
, len
);
367 * Move 'R'ename entries last so that all references of the file
368 * appear in the output before it is renamed (e.g., when a file
369 * was copied and renamed in the same commit).
371 return (a
->status
== 'R') - (b
->status
== 'R');
374 static void print_path_1(const char *path
)
376 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
378 quote_c_style(path
, NULL
, stdout
, 0);
379 else if (strchr(path
, ' '))
380 printf("\"%s\"", path
);
385 static char *anonymize_path_component(void)
388 struct strbuf out
= STRBUF_INIT
;
389 strbuf_addf(&out
, "path%d", counter
++);
390 return strbuf_detach(&out
, NULL
);
393 static void print_path(const char *path
)
398 static struct hashmap paths
;
399 static struct strbuf anon
= STRBUF_INIT
;
401 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
402 print_path_1(anon
.buf
);
407 static char *generate_fake_oid(void)
409 static uint32_t counter
= 1; /* avoid null oid */
410 const unsigned hashsz
= the_hash_algo
->rawsz
;
411 struct object_id oid
;
412 char *hex
= xmallocz(GIT_MAX_HEXSZ
);
415 put_be32(oid
.hash
+ hashsz
- 4, counter
++);
416 return oid_to_hex_r(hex
, &oid
);
419 static const char *anonymize_oid(const char *oid_hex
)
421 static struct hashmap objs
;
422 size_t len
= strlen(oid_hex
);
423 return anonymize_str(&objs
, generate_fake_oid
, oid_hex
, len
);
426 static void show_filemodify(struct diff_queue_struct
*q
,
427 struct diff_options
*options UNUSED
, void *data
)
430 struct string_list
*changed
= data
;
433 * Handle files below a directory first, in case they are all deleted
434 * and the directory changes to a file or symlink.
436 QSORT(q
->queue
, q
->nr
, depth_first
);
438 for (i
= 0; i
< q
->nr
; i
++) {
439 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
440 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
442 switch (q
->queue
[i
]->status
) {
443 case DIFF_STATUS_DELETED
:
445 print_path(spec
->path
);
446 string_list_insert(changed
, spec
->path
);
450 case DIFF_STATUS_COPIED
:
451 case DIFF_STATUS_RENAMED
:
453 * If a change in the file corresponding to ospec->path
454 * has been observed, we cannot trust its contents
455 * because the diff is calculated based on the prior
456 * contents, not the current contents. So, declare a
457 * copy or rename only if there was no change observed.
459 if (!string_list_has_string(changed
, ospec
->path
)) {
460 printf("%c ", q
->queue
[i
]->status
);
461 print_path(ospec
->path
);
463 print_path(spec
->path
);
464 string_list_insert(changed
, spec
->path
);
467 if (oideq(&ospec
->oid
, &spec
->oid
) &&
468 ospec
->mode
== spec
->mode
)
473 case DIFF_STATUS_TYPE_CHANGED
:
474 case DIFF_STATUS_MODIFIED
:
475 case DIFF_STATUS_ADDED
:
477 * Links refer to objects in another repositories;
478 * output the SHA-1 verbatim.
480 if (no_data
|| S_ISGITLINK(spec
->mode
))
481 printf("M %06o %s ", spec
->mode
,
483 anonymize_oid(oid_to_hex(&spec
->oid
)) :
484 oid_to_hex(&spec
->oid
));
486 struct object
*object
= lookup_object(the_repository
,
488 printf("M %06o :%d ", spec
->mode
,
489 get_object_mark(object
));
491 print_path(spec
->path
);
492 string_list_insert(changed
, spec
->path
);
497 die("Unexpected comparison status '%c' for %s, %s",
499 ospec
->path
? ospec
->path
: "none",
500 spec
->path
? spec
->path
: "none");
505 static const char *find_encoding(const char *begin
, const char *end
)
507 const char *needle
= "\nencoding ";
510 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
511 needle
, strlen(needle
));
514 bol
+= strlen(needle
);
515 eol
= strchrnul(bol
, '\n');
520 static char *anonymize_ref_component(void)
523 struct strbuf out
= STRBUF_INIT
;
524 strbuf_addf(&out
, "ref%d", counter
++);
525 return strbuf_detach(&out
, NULL
);
528 static const char *anonymize_refname(const char *refname
)
531 * If any of these prefixes is found, we will leave it intact
532 * so that tags remain tags and so forth.
534 static const char *prefixes
[] = {
540 static struct hashmap refs
;
541 static struct strbuf anon
= STRBUF_INIT
;
545 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
546 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
547 strbuf_addstr(&anon
, prefixes
[i
]);
552 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
557 * We do not even bother to cache commit messages, as they are unlikely
558 * to be repeated verbatim, and it is not that interesting when they are.
560 static char *anonymize_commit_message(void)
563 return xstrfmt("subject %d\n\nbody\n", counter
++);
566 static char *anonymize_ident(void)
569 struct strbuf out
= STRBUF_INIT
;
570 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
572 return strbuf_detach(&out
, NULL
);
576 * Our strategy here is to anonymize the names and email addresses,
577 * but keep timestamps intact, as they influence things like traversal
578 * order (and by themselves should not be too revealing).
580 static void anonymize_ident_line(const char **beg
, const char **end
)
582 static struct hashmap idents
;
583 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
584 static unsigned which_buffer
;
587 struct ident_split split
;
588 const char *end_of_header
;
590 out
= &buffers
[which_buffer
++];
591 which_buffer
%= ARRAY_SIZE(buffers
);
594 /* skip "committer", "author", "tagger", etc */
595 end_of_header
= strchr(*beg
, ' ');
597 BUG("malformed line fed to anonymize_ident_line: %.*s",
598 (int)(*end
- *beg
), *beg
);
600 strbuf_add(out
, *beg
, end_of_header
- *beg
);
602 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
607 len
= split
.mail_end
- split
.name_begin
;
608 ident
= anonymize_str(&idents
, anonymize_ident
,
609 split
.name_begin
, len
);
610 strbuf_addstr(out
, ident
);
611 strbuf_addch(out
, ' ');
612 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
614 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
618 *end
= out
->buf
+ out
->len
;
621 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
,
622 struct string_list
*paths_of_changed_objects
)
624 int saved_output_format
= rev
->diffopt
.output_format
;
625 const char *commit_buffer
;
626 const char *author
, *author_end
, *committer
, *committer_end
;
627 const char *encoding
, *message
;
628 char *reencoded
= NULL
;
629 struct commit_list
*p
;
633 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
635 parse_commit_or_die(commit
);
636 commit_buffer
= repo_get_commit_buffer(the_repository
, commit
, NULL
);
637 author
= strstr(commit_buffer
, "\nauthor ");
639 die("could not find author in commit %s",
640 oid_to_hex(&commit
->object
.oid
));
642 author_end
= strchrnul(author
, '\n');
643 committer
= strstr(author_end
, "\ncommitter ");
645 die("could not find committer in commit %s",
646 oid_to_hex(&commit
->object
.oid
));
648 committer_end
= strchrnul(committer
, '\n');
649 message
= strstr(committer_end
, "\n\n");
650 encoding
= find_encoding(committer_end
, message
);
654 if (commit
->parents
&&
655 (get_object_mark(&commit
->parents
->item
->object
) != 0 ||
656 reference_excluded_commits
) &&
658 parse_commit_or_die(commit
->parents
->item
);
659 diff_tree_oid(get_commit_tree_oid(commit
->parents
->item
),
660 get_commit_tree_oid(commit
), "", &rev
->diffopt
);
663 diff_root_tree_oid(get_commit_tree_oid(commit
),
666 /* Export the referenced blobs, and remember the marks. */
667 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
668 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
669 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
671 refname
= *revision_sources_at(&revision_sources
, commit
);
673 * FIXME: string_list_remove() below for each ref is overall
674 * O(N^2). Compared to a history walk and diffing trees, this is
675 * just lost in the noise in practice. However, theoretically a
676 * repo may have enough refs for this to become slow.
678 string_list_remove(&extra_refs
, refname
, 0);
680 refname
= anonymize_refname(refname
);
681 anonymize_ident_line(&committer
, &committer_end
);
682 anonymize_ident_line(&author
, &author_end
);
685 mark_next_object(&commit
->object
);
687 reencoded
= anonymize_commit_message();
688 } else if (encoding
) {
689 switch(reencode_mode
) {
691 reencoded
= reencode_string(message
, "UTF-8", encoding
);
696 die("Encountered commit-specific encoding %s in commit "
697 "%s; use --reencode=[yes|no] to handle it",
698 encoding
, oid_to_hex(&commit
->object
.oid
));
701 if (!commit
->parents
)
702 printf("reset %s\n", refname
);
703 printf("commit %s\nmark :%"PRIu32
"\n", refname
, last_idnum
);
704 if (show_original_ids
)
705 printf("original-oid %s\n", oid_to_hex(&commit
->object
.oid
));
706 printf("%.*s\n%.*s\n",
707 (int)(author_end
- author
), author
,
708 (int)(committer_end
- committer
), committer
);
709 if (!reencoded
&& encoding
)
710 printf("encoding %s\n", encoding
);
711 printf("data %u\n%s",
713 ? strlen(reencoded
) : message
714 ? strlen(message
) : 0),
715 reencoded
? reencoded
: message
? message
: "");
717 repo_unuse_commit_buffer(the_repository
, commit
, commit_buffer
);
719 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
720 struct object
*obj
= &p
->item
->object
;
721 int mark
= get_object_mark(obj
);
723 if (!mark
&& !reference_excluded_commits
)
730 printf(":%d\n", mark
);
734 anonymize_oid(oid_to_hex(&obj
->oid
)) :
735 oid_to_hex(&obj
->oid
));
740 printf("deleteall\n");
741 log_tree_diff_flush(rev
);
742 string_list_clear(paths_of_changed_objects
, 0);
743 rev
->diffopt
.output_format
= saved_output_format
;
750 static char *anonymize_tag(void)
753 struct strbuf out
= STRBUF_INIT
;
754 strbuf_addf(&out
, "tag message %d", counter
++);
755 return strbuf_detach(&out
, NULL
);
759 static void handle_tag(const char *name
, struct tag
*tag
)
762 enum object_type type
;
764 const char *tagger
, *tagger_end
, *message
;
765 size_t message_size
= 0;
766 struct object
*tagged
;
770 /* Trees have no identifier in fast-export output, thus we have no way
771 * to output tags of trees, tags of tags of trees, etc. Simply omit
774 tagged
= tag
->tagged
;
775 while (tagged
->type
== OBJ_TAG
) {
776 tagged
= ((struct tag
*)tagged
)->tagged
;
778 if (tagged
->type
== OBJ_TREE
) {
779 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
780 oid_to_hex(&tag
->object
.oid
));
784 buf
= repo_read_object_file(the_repository
, &tag
->object
.oid
, &type
,
787 die("could not read tag %s", oid_to_hex(&tag
->object
.oid
));
788 message
= memmem(buf
, size
, "\n\n", 2);
791 message_size
= strlen(message
);
793 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
795 if (fake_missing_tagger
)
796 tagger
= "tagger Unspecified Tagger "
797 "<unspecified-tagger> 0 +0000";
800 tagger_end
= tagger
+ strlen(tagger
);
803 tagger_end
= strchrnul(tagger
, '\n');
805 anonymize_ident_line(&tagger
, &tagger_end
);
809 name
= anonymize_refname(name
);
811 static struct hashmap tags
;
812 message
= anonymize_str(&tags
, anonymize_tag
,
813 message
, message_size
);
814 message_size
= strlen(message
);
818 /* handle signed tags */
820 const char *signature
= strstr(message
,
821 "\n-----BEGIN PGP SIGNATURE-----\n");
823 switch(signed_tag_mode
) {
824 case SIGNED_TAG_ABORT
:
825 die("encountered signed tag %s; use "
826 "--signed-tags=<mode> to handle it",
827 oid_to_hex(&tag
->object
.oid
));
829 warning("exporting signed tag %s",
830 oid_to_hex(&tag
->object
.oid
));
835 warning("stripping signature from tag %s",
836 oid_to_hex(&tag
->object
.oid
));
839 message_size
= signature
+ 1 - message
;
844 /* handle tag->tagged having been filtered out due to paths specified */
845 tagged
= tag
->tagged
;
846 tagged_mark
= get_object_mark(tagged
);
848 switch(tag_of_filtered_mode
) {
849 case TAG_FILTERING_ABORT
:
850 die("tag %s tags unexported object; use "
851 "--tag-of-filtered-object=<mode> to handle it",
852 oid_to_hex(&tag
->object
.oid
));
854 /* Ignore this tag altogether */
858 if (tagged
->type
== OBJ_TAG
&& !mark_tags
) {
859 die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
860 } else if (tagged
->type
== OBJ_COMMIT
) {
861 p
= rewrite_commit((struct commit
*)tagged
);
863 printf("reset %s\nfrom %s\n\n",
864 name
, oid_to_hex(null_oid()));
868 tagged_mark
= get_object_mark(&p
->object
);
870 /* tagged->type is either OBJ_BLOB or OBJ_TAG */
871 tagged_mark
= get_object_mark(tagged
);
876 if (tagged
->type
== OBJ_TAG
) {
877 printf("reset %s\nfrom %s\n\n",
878 name
, oid_to_hex(null_oid()));
880 skip_prefix(name
, "refs/tags/", &name
);
881 printf("tag %s\n", name
);
883 mark_next_object(&tag
->object
);
884 printf("mark :%"PRIu32
"\n", last_idnum
);
887 printf("from :%d\n", tagged_mark
);
889 printf("from %s\n", oid_to_hex(&tagged
->oid
));
891 if (show_original_ids
)
892 printf("original-oid %s\n", oid_to_hex(&tag
->object
.oid
));
893 printf("%.*s%sdata %d\n%.*s\n",
894 (int)(tagger_end
- tagger
), tagger
,
895 tagger
== tagger_end
? "" : "\n",
896 (int)message_size
, (int)message_size
, message
? message
: "");
900 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
902 switch (e
->item
->type
) {
904 return (struct commit
*)e
->item
;
906 struct tag
*tag
= (struct tag
*)e
->item
;
908 /* handle nested tags */
909 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
910 parse_object(the_repository
, &tag
->object
.oid
);
911 string_list_append(&tag_refs
, full_name
)->util
= tag
;
912 tag
= (struct tag
*)tag
->tagged
;
915 die("Tag %s points nowhere?", e
->name
);
916 return (struct commit
*)tag
;
923 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
927 for (i
= 0; i
< info
->nr
; i
++) {
928 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
929 struct object_id oid
;
930 struct commit
*commit
;
933 if (e
->flags
& UNINTERESTING
)
936 if (repo_dwim_ref(the_repository
, e
->name
, strlen(e
->name
),
937 &oid
, &full_name
, 0) != 1)
942 private = apply_refspecs(&refspecs
, full_name
);
949 commit
= get_commit(e
, full_name
);
951 warning("%s: Unexpected object of type %s, skipping.",
953 type_name(e
->item
->type
));
957 switch(commit
->object
.type
) {
961 export_blob(&commit
->object
.oid
);
963 default: /* OBJ_TAG (nested tags) is already handled */
964 warning("Tag points to object of unexpected type %s, skipping.",
965 type_name(commit
->object
.type
));
970 * Make sure this ref gets properly updated eventually, whether
971 * through a commit or manually at the end.
973 if (e
->item
->type
!= OBJ_TAG
)
974 string_list_append(&extra_refs
, full_name
)->util
= commit
;
976 if (!*revision_sources_at(&revision_sources
, commit
))
977 *revision_sources_at(&revision_sources
, commit
) = full_name
;
980 string_list_sort(&extra_refs
);
981 string_list_remove_duplicates(&extra_refs
, 0);
984 static void handle_tags_and_duplicates(struct string_list
*extras
)
986 struct commit
*commit
;
989 for (i
= extras
->nr
- 1; i
>= 0; i
--) {
990 const char *name
= extras
->items
[i
].string
;
991 struct object
*object
= extras
->items
[i
].util
;
994 switch (object
->type
) {
996 handle_tag(name
, (struct tag
*)object
);
1000 name
= anonymize_refname(name
);
1001 /* create refs pointing to already seen commits */
1002 commit
= rewrite_commit((struct commit
*)object
);
1005 * Neither this object nor any of its
1006 * ancestors touch any relevant paths, so
1007 * it has been filtered to nothing. Delete
1010 printf("reset %s\nfrom %s\n\n",
1011 name
, oid_to_hex(null_oid()));
1015 mark
= get_object_mark(&commit
->object
);
1018 * Getting here means we have a commit which
1019 * was excluded by a negative refspec (e.g.
1020 * fast-export ^HEAD HEAD). If we are
1021 * referencing excluded commits, set the ref
1022 * to the exact commit. Otherwise, the user
1023 * wants the branch exported but every commit
1024 * in its history to be deleted, which basically
1025 * just means deletion of the ref.
1027 if (!reference_excluded_commits
) {
1028 /* delete the ref */
1029 printf("reset %s\nfrom %s\n\n",
1030 name
, oid_to_hex(null_oid()));
1033 /* set ref to commit using oid, not mark */
1034 printf("reset %s\nfrom %s\n\n", name
,
1035 oid_to_hex(&commit
->object
.oid
));
1039 printf("reset %s\nfrom :%d\n\n", name
, mark
1047 static void export_marks(char *file
)
1051 struct decoration_entry
*deco
= idnums
.entries
;
1055 f
= fopen_for_writing(file
);
1057 die_errno("Unable to open marks file %s for writing.", file
);
1059 for (i
= 0; i
< idnums
.size
; i
++) {
1060 if (deco
->base
&& deco
->base
->type
== 1) {
1061 mark
= ptr_to_mark(deco
->decoration
);
1062 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
1063 oid_to_hex(&deco
->base
->oid
)) < 0) {
1074 error("Unable to write marks file %s.", file
);
1077 static void import_marks(char *input_file
, int check_exists
)
1083 if (check_exists
&& stat(input_file
, &sb
))
1086 f
= xfopen(input_file
, "r");
1087 while (fgets(line
, sizeof(line
), f
)) {
1089 char *line_end
, *mark_end
;
1090 struct object_id oid
;
1091 struct object
*object
;
1092 struct commit
*commit
;
1093 enum object_type type
;
1095 line_end
= strchr(line
, '\n');
1096 if (line
[0] != ':' || !line_end
)
1097 die("corrupt mark line: %s", line
);
1100 mark
= strtoumax(line
+ 1, &mark_end
, 10);
1101 if (!mark
|| mark_end
== line
+ 1
1102 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
1103 die("corrupt mark line: %s", line
);
1105 if (last_idnum
< mark
)
1108 type
= oid_object_info(the_repository
, &oid
, NULL
);
1110 die("object not found: %s", oid_to_hex(&oid
));
1112 if (type
!= OBJ_COMMIT
)
1116 commit
= lookup_commit(the_repository
, &oid
);
1118 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
1120 object
= &commit
->object
;
1122 if (object
->flags
& SHOWN
)
1123 error("Object %s already has a mark", oid_to_hex(&oid
));
1125 mark_object(object
, mark
);
1127 object
->flags
|= SHOWN
;
1132 static void handle_deletes(void)
1135 for (i
= 0; i
< refspecs
.nr
; i
++) {
1136 struct refspec_item
*refspec
= &refspecs
.items
[i
];
1140 printf("reset %s\nfrom %s\n\n",
1141 refspec
->dst
, oid_to_hex(null_oid()));
1145 static int parse_opt_anonymize_map(const struct option
*opt
,
1146 const char *arg
, int unset
)
1148 struct hashmap
*map
= opt
->value
;
1149 const char *delim
, *value
;
1152 BUG_ON_OPT_NEG(unset
);
1154 delim
= strchr(arg
, ':');
1156 keylen
= delim
- arg
;
1159 keylen
= strlen(arg
);
1163 if (!keylen
|| !*value
)
1164 return error(_("--anonymize-map token cannot be empty"));
1166 add_anonymized_entry(map
, memhash(arg
, keylen
), arg
, keylen
,
1172 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
1174 struct rev_info revs
;
1175 struct commit
*commit
;
1176 char *export_filename
= NULL
,
1177 *import_filename
= NULL
,
1178 *import_filename_if_exists
= NULL
;
1179 uint32_t lastimportid
;
1180 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
1181 struct string_list paths_of_changed_objects
= STRING_LIST_INIT_DUP
;
1182 struct option options
[] = {
1183 OPT_INTEGER(0, "progress", &progress
,
1184 N_("show progress after <n> objects")),
1185 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
1186 N_("select handling of signed tags"),
1187 parse_opt_signed_tag_mode
),
1188 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
1189 N_("select handling of tags that tag filtered objects"),
1190 parse_opt_tag_of_filtered_mode
),
1191 OPT_CALLBACK(0, "reencode", &reencode_mode
, N_("mode"),
1192 N_("select handling of commit messages in an alternate encoding"),
1193 parse_opt_reencode_mode
),
1194 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
1195 N_("dump marks to this file")),
1196 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
1197 N_("import marks from this file")),
1198 OPT_STRING(0, "import-marks-if-exists",
1199 &import_filename_if_exists
,
1201 N_("import marks from this file if it exists")),
1202 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
1203 N_("fake a tagger when tags lack one")),
1204 OPT_BOOL(0, "full-tree", &full_tree
,
1205 N_("output full tree for each commit")),
1206 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
1207 N_("use the done feature to terminate the stream")),
1208 OPT_BOOL(0, "no-data", &no_data
, N_("skip output of blob data")),
1209 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1210 N_("apply refspec to exported refs")),
1211 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1212 OPT_CALLBACK_F(0, "anonymize-map", &anonymized_seeds
, N_("from:to"),
1213 N_("convert <from> to <to> in anonymized output"),
1214 PARSE_OPT_NONEG
, parse_opt_anonymize_map
),
1215 OPT_BOOL(0, "reference-excluded-parents",
1216 &reference_excluded_commits
, N_("reference parents which are not in fast-export stream by object id")),
1217 OPT_BOOL(0, "show-original-ids", &show_original_ids
,
1218 N_("show original object ids of blobs/commits")),
1219 OPT_BOOL(0, "mark-tags", &mark_tags
,
1220 N_("label tags with mark ids")),
1226 usage_with_options (fast_export_usage
, options
);
1228 /* we handle encodings */
1229 git_config(git_default_config
, NULL
);
1231 repo_init_revisions(the_repository
, &revs
, prefix
);
1232 init_revision_sources(&revision_sources
);
1233 revs
.topo_order
= 1;
1234 revs
.sources
= &revision_sources
;
1235 revs
.rewrite_parents
= 1;
1236 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1237 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN_OPT
);
1238 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1240 usage_with_options (fast_export_usage
, options
);
1242 if (anonymized_seeds
.cmpfn
&& !anonymize
)
1243 die(_("the option '%s' requires '%s'"), "--anonymize-map", "--anonymize");
1245 if (refspecs_list
.nr
) {
1248 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1249 refspec_append(&refspecs
, refspecs_list
.items
[i
].string
);
1251 string_list_clear(&refspecs_list
, 1);
1254 if (use_done_feature
)
1255 printf("feature done\n");
1257 if (import_filename
&& import_filename_if_exists
)
1258 die(_("options '%s' and '%s' cannot be used together"), "--import-marks", "--import-marks-if-exists");
1259 if (import_filename
)
1260 import_marks(import_filename
, 0);
1261 else if (import_filename_if_exists
)
1262 import_marks(import_filename_if_exists
, 1);
1263 lastimportid
= last_idnum
;
1265 if (import_filename
&& revs
.prune_data
.nr
)
1268 get_tags_and_duplicates(&revs
.cmdline
);
1270 if (prepare_revision_walk(&revs
))
1271 die("revision walk setup failed");
1274 revs
.diffopt
.format_callback
= show_filemodify
;
1275 revs
.diffopt
.format_callback_data
= &paths_of_changed_objects
;
1276 revs
.diffopt
.flags
.recursive
= 1;
1277 revs
.diffopt
.no_free
= 1;
1278 while ((commit
= get_revision(&revs
)))
1279 handle_commit(commit
, &revs
, &paths_of_changed_objects
);
1281 handle_tags_and_duplicates(&extra_refs
);
1282 handle_tags_and_duplicates(&tag_refs
);
1285 if (export_filename
&& lastimportid
!= last_idnum
)
1286 export_marks(export_filename
);
1288 if (use_done_feature
)
1291 refspec_clear(&refspecs
);
1292 release_revisions(&revs
);