2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
11 #include "object-store.h"
20 #include "string-list.h"
22 #include "parse-options.h"
26 #include "commit-slab.h"
28 static const char *fast_export_usage
[] = {
29 N_("git fast-export [rev-list-opts]"),
34 static enum { SIGNED_TAG_ABORT
, VERBATIM
, WARN
, WARN_STRIP
, STRIP
} signed_tag_mode
= SIGNED_TAG_ABORT
;
35 static enum { TAG_FILTERING_ABORT
, DROP
, REWRITE
} tag_of_filtered_mode
= TAG_FILTERING_ABORT
;
36 static enum { REENCODE_ABORT
, REENCODE_YES
, REENCODE_NO
} reencode_mode
= REENCODE_ABORT
;
37 static int fake_missing_tagger
;
38 static int use_done_feature
;
41 static int reference_excluded_commits
;
42 static int show_original_ids
;
44 static struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
45 static struct string_list tag_refs
= STRING_LIST_INIT_NODUP
;
46 static struct refspec refspecs
= REFSPEC_INIT_FETCH
;
48 static struct revision_sources revision_sources
;
50 static int parse_opt_signed_tag_mode(const struct option
*opt
,
51 const char *arg
, int unset
)
53 if (unset
|| !strcmp(arg
, "abort"))
54 signed_tag_mode
= SIGNED_TAG_ABORT
;
55 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
56 signed_tag_mode
= VERBATIM
;
57 else if (!strcmp(arg
, "warn"))
58 signed_tag_mode
= WARN
;
59 else if (!strcmp(arg
, "warn-strip"))
60 signed_tag_mode
= WARN_STRIP
;
61 else if (!strcmp(arg
, "strip"))
62 signed_tag_mode
= STRIP
;
64 return error("Unknown signed-tags mode: %s", arg
);
68 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
69 const char *arg
, int unset
)
71 if (unset
|| !strcmp(arg
, "abort"))
72 tag_of_filtered_mode
= TAG_FILTERING_ABORT
;
73 else if (!strcmp(arg
, "drop"))
74 tag_of_filtered_mode
= DROP
;
75 else if (!strcmp(arg
, "rewrite"))
76 tag_of_filtered_mode
= REWRITE
;
78 return error("Unknown tag-of-filtered mode: %s", arg
);
82 static int parse_opt_reencode_mode(const struct option
*opt
,
83 const char *arg
, int unset
)
86 reencode_mode
= REENCODE_ABORT
;
90 switch (git_parse_maybe_bool(arg
)) {
92 reencode_mode
= REENCODE_NO
;
95 reencode_mode
= REENCODE_YES
;
98 if (!strcasecmp(arg
, "abort"))
99 reencode_mode
= REENCODE_ABORT
;
101 return error("Unknown reencoding mode: %s", arg
);
107 static struct decoration idnums
;
108 static uint32_t last_idnum
;
110 static int has_unshown_parent(struct commit
*commit
)
112 struct commit_list
*parent
;
114 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
115 if (!(parent
->item
->object
.flags
& SHOWN
) &&
116 !(parent
->item
->object
.flags
& UNINTERESTING
))
121 struct anonymized_entry
{
122 struct hashmap_entry hash
;
129 static int anonymized_entry_cmp(const void *unused_cmp_data
,
130 const void *va
, const void *vb
,
131 const void *unused_keydata
)
133 const struct anonymized_entry
*a
= va
, *b
= vb
;
134 return a
->orig_len
!= b
->orig_len
||
135 memcmp(a
->orig
, b
->orig
, a
->orig_len
);
139 * Basically keep a cache of X->Y so that we can repeatedly replace
140 * the same anonymized string with another. The actual generation
141 * is farmed out to the generate function.
143 static const void *anonymize_mem(struct hashmap
*map
,
144 void *(*generate
)(const void *, size_t *),
145 const void *orig
, size_t *len
)
147 struct anonymized_entry key
, *ret
;
150 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
152 hashmap_entry_init(&key
, memhash(orig
, *len
));
155 ret
= hashmap_get(map
, &key
, NULL
);
158 ret
= xmalloc(sizeof(*ret
));
159 hashmap_entry_init(&ret
->hash
, key
.hash
.hash
);
160 ret
->orig
= xstrdup(orig
);
161 ret
->orig_len
= *len
;
162 ret
->anon
= generate(orig
, len
);
163 ret
->anon_len
= *len
;
164 hashmap_put(map
, ret
);
167 *len
= ret
->anon_len
;
172 * We anonymize each component of a path individually,
173 * so that paths a/b and a/c will share a common root.
174 * The paths are cached via anonymize_mem so that repeated
175 * lookups for "a" will yield the same value.
177 static void anonymize_path(struct strbuf
*out
, const char *path
,
179 void *(*generate
)(const void *, size_t *))
182 const char *end_of_component
= strchrnul(path
, '/');
183 size_t len
= end_of_component
- path
;
184 const char *c
= anonymize_mem(map
, generate
, path
, &len
);
185 strbuf_add(out
, c
, len
);
186 path
= end_of_component
;
188 strbuf_addch(out
, *path
++);
192 static inline void *mark_to_ptr(uint32_t mark
)
194 return (void *)(uintptr_t)mark
;
197 static inline uint32_t ptr_to_mark(void * mark
)
199 return (uint32_t)(uintptr_t)mark
;
202 static inline void mark_object(struct object
*object
, uint32_t mark
)
204 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
207 static inline void mark_next_object(struct object
*object
)
209 mark_object(object
, ++last_idnum
);
212 static int get_object_mark(struct object
*object
)
214 void *decoration
= lookup_decoration(&idnums
, object
);
217 return ptr_to_mark(decoration
);
220 static struct commit
*rewrite_commit(struct commit
*p
)
223 if (p
->parents
&& p
->parents
->next
)
225 if (p
->object
.flags
& UNINTERESTING
)
227 if (!(p
->object
.flags
& TREESAME
))
231 p
= p
->parents
->item
;
236 static void show_progress(void)
238 static int counter
= 0;
241 if ((++counter
% progress
) == 0)
242 printf("progress %d objects\n", counter
);
246 * Ideally we would want some transformation of the blob data here
247 * that is unreversible, but would still be the same size and have
248 * the same data relationship to other blobs (so that we get the same
249 * delta and packing behavior as the original). But the first and last
250 * requirements there are probably mutually exclusive, so let's take
251 * the easy way out for now, and just generate arbitrary content.
253 * There's no need to cache this result with anonymize_mem, since
254 * we already handle blob content caching with marks.
256 static char *anonymize_blob(unsigned long *size
)
259 struct strbuf out
= STRBUF_INIT
;
260 strbuf_addf(&out
, "anonymous blob %d", counter
++);
262 return strbuf_detach(&out
, NULL
);
265 static void export_blob(const struct object_id
*oid
)
268 enum object_type type
;
270 struct object
*object
;
276 if (is_null_oid(oid
))
279 object
= lookup_object(the_repository
, oid
);
280 if (object
&& object
->flags
& SHOWN
)
284 buf
= anonymize_blob(&size
);
285 object
= (struct object
*)lookup_blob(the_repository
, oid
);
288 buf
= read_object_file(oid
, &type
, &size
);
290 die("could not read blob %s", oid_to_hex(oid
));
291 if (check_object_signature(oid
, buf
, size
, type_name(type
)) < 0)
292 die("oid mismatch in blob %s", oid_to_hex(oid
));
293 object
= parse_object_buffer(the_repository
, oid
, type
,
298 die("Could not read blob %s", oid_to_hex(oid
));
300 mark_next_object(object
);
302 printf("blob\nmark :%"PRIu32
"\n", last_idnum
);
303 if (show_original_ids
)
304 printf("original-oid %s\n", oid_to_hex(oid
));
305 printf("data %"PRIuMAX
"\n", (uintmax_t)size
);
306 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
307 die_errno("could not write blob '%s'", oid_to_hex(oid
));
312 object
->flags
|= SHOWN
;
317 static int depth_first(const void *a_
, const void *b_
)
319 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
320 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
321 const char *name_a
, *name_b
;
322 int len_a
, len_b
, len
;
325 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
326 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
328 len_a
= strlen(name_a
);
329 len_b
= strlen(name_b
);
330 len
= (len_a
< len_b
) ? len_a
: len_b
;
332 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
333 cmp
= memcmp(name_a
, name_b
, len
);
340 * Move 'R'ename entries last so that all references of the file
341 * appear in the output before it is renamed (e.g., when a file
342 * was copied and renamed in the same commit).
344 return (a
->status
== 'R') - (b
->status
== 'R');
347 static void print_path_1(const char *path
)
349 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
351 quote_c_style(path
, NULL
, stdout
, 0);
352 else if (strchr(path
, ' '))
353 printf("\"%s\"", path
);
358 static void *anonymize_path_component(const void *path
, size_t *len
)
361 struct strbuf out
= STRBUF_INIT
;
362 strbuf_addf(&out
, "path%d", counter
++);
363 return strbuf_detach(&out
, len
);
366 static void print_path(const char *path
)
371 static struct hashmap paths
;
372 static struct strbuf anon
= STRBUF_INIT
;
374 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
375 print_path_1(anon
.buf
);
380 static void *generate_fake_oid(const void *old
, size_t *len
)
382 static uint32_t counter
= 1; /* avoid null oid */
383 const unsigned hashsz
= the_hash_algo
->rawsz
;
384 unsigned char *out
= xcalloc(hashsz
, 1);
385 put_be32(out
+ hashsz
- 4, counter
++);
389 static const struct object_id
*anonymize_oid(const struct object_id
*oid
)
391 static struct hashmap objs
;
392 size_t len
= the_hash_algo
->rawsz
;
393 return anonymize_mem(&objs
, generate_fake_oid
, oid
, &len
);
396 static void show_filemodify(struct diff_queue_struct
*q
,
397 struct diff_options
*options
, void *data
)
400 struct string_list
*changed
= data
;
403 * Handle files below a directory first, in case they are all deleted
404 * and the directory changes to a file or symlink.
406 QSORT(q
->queue
, q
->nr
, depth_first
);
408 for (i
= 0; i
< q
->nr
; i
++) {
409 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
410 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
412 switch (q
->queue
[i
]->status
) {
413 case DIFF_STATUS_DELETED
:
415 print_path(spec
->path
);
416 string_list_insert(changed
, spec
->path
);
420 case DIFF_STATUS_COPIED
:
421 case DIFF_STATUS_RENAMED
:
423 * If a change in the file corresponding to ospec->path
424 * has been observed, we cannot trust its contents
425 * because the diff is calculated based on the prior
426 * contents, not the current contents. So, declare a
427 * copy or rename only if there was no change observed.
429 if (!string_list_has_string(changed
, ospec
->path
)) {
430 printf("%c ", q
->queue
[i
]->status
);
431 print_path(ospec
->path
);
433 print_path(spec
->path
);
434 string_list_insert(changed
, spec
->path
);
437 if (oideq(&ospec
->oid
, &spec
->oid
) &&
438 ospec
->mode
== spec
->mode
)
443 case DIFF_STATUS_TYPE_CHANGED
:
444 case DIFF_STATUS_MODIFIED
:
445 case DIFF_STATUS_ADDED
:
447 * Links refer to objects in another repositories;
448 * output the SHA-1 verbatim.
450 if (no_data
|| S_ISGITLINK(spec
->mode
))
451 printf("M %06o %s ", spec
->mode
,
452 oid_to_hex(anonymize
?
453 anonymize_oid(&spec
->oid
) :
456 struct object
*object
= lookup_object(the_repository
,
458 printf("M %06o :%d ", spec
->mode
,
459 get_object_mark(object
));
461 print_path(spec
->path
);
462 string_list_insert(changed
, spec
->path
);
467 die("Unexpected comparison status '%c' for %s, %s",
469 ospec
->path
? ospec
->path
: "none",
470 spec
->path
? spec
->path
: "none");
475 static const char *find_encoding(const char *begin
, const char *end
)
477 const char *needle
= "\nencoding ";
480 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
481 needle
, strlen(needle
));
484 bol
+= strlen(needle
);
485 eol
= strchrnul(bol
, '\n');
490 static void *anonymize_ref_component(const void *old
, size_t *len
)
493 struct strbuf out
= STRBUF_INIT
;
494 strbuf_addf(&out
, "ref%d", counter
++);
495 return strbuf_detach(&out
, len
);
498 static const char *anonymize_refname(const char *refname
)
501 * If any of these prefixes is found, we will leave it intact
502 * so that tags remain tags and so forth.
504 static const char *prefixes
[] = {
510 static struct hashmap refs
;
511 static struct strbuf anon
= STRBUF_INIT
;
515 * We also leave "master" as a special case, since it does not reveal
516 * anything interesting.
518 if (!strcmp(refname
, "refs/heads/master"))
522 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
523 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
524 strbuf_addstr(&anon
, prefixes
[i
]);
529 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
534 * We do not even bother to cache commit messages, as they are unlikely
535 * to be repeated verbatim, and it is not that interesting when they are.
537 static char *anonymize_commit_message(const char *old
)
540 return xstrfmt("subject %d\n\nbody\n", counter
++);
543 static struct hashmap idents
;
544 static void *anonymize_ident(const void *old
, size_t *len
)
547 struct strbuf out
= STRBUF_INIT
;
548 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
550 return strbuf_detach(&out
, len
);
554 * Our strategy here is to anonymize the names and email addresses,
555 * but keep timestamps intact, as they influence things like traversal
556 * order (and by themselves should not be too revealing).
558 static void anonymize_ident_line(const char **beg
, const char **end
)
560 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
561 static unsigned which_buffer
;
564 struct ident_split split
;
565 const char *end_of_header
;
567 out
= &buffers
[which_buffer
++];
568 which_buffer
%= ARRAY_SIZE(buffers
);
571 /* skip "committer", "author", "tagger", etc */
572 end_of_header
= strchr(*beg
, ' ');
574 BUG("malformed line fed to anonymize_ident_line: %.*s",
575 (int)(*end
- *beg
), *beg
);
577 strbuf_add(out
, *beg
, end_of_header
- *beg
);
579 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
584 len
= split
.mail_end
- split
.name_begin
;
585 ident
= anonymize_mem(&idents
, anonymize_ident
,
586 split
.name_begin
, &len
);
587 strbuf_add(out
, ident
, len
);
588 strbuf_addch(out
, ' ');
589 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
591 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
595 *end
= out
->buf
+ out
->len
;
598 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
,
599 struct string_list
*paths_of_changed_objects
)
601 int saved_output_format
= rev
->diffopt
.output_format
;
602 const char *commit_buffer
;
603 const char *author
, *author_end
, *committer
, *committer_end
;
604 const char *encoding
, *message
;
605 char *reencoded
= NULL
;
606 struct commit_list
*p
;
610 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
612 parse_commit_or_die(commit
);
613 commit_buffer
= get_commit_buffer(commit
, NULL
);
614 author
= strstr(commit_buffer
, "\nauthor ");
616 die("could not find author in commit %s",
617 oid_to_hex(&commit
->object
.oid
));
619 author_end
= strchrnul(author
, '\n');
620 committer
= strstr(author_end
, "\ncommitter ");
622 die("could not find committer in commit %s",
623 oid_to_hex(&commit
->object
.oid
));
625 committer_end
= strchrnul(committer
, '\n');
626 message
= strstr(committer_end
, "\n\n");
627 encoding
= find_encoding(committer_end
, message
);
631 if (commit
->parents
&&
632 (get_object_mark(&commit
->parents
->item
->object
) != 0 ||
633 reference_excluded_commits
) &&
635 parse_commit_or_die(commit
->parents
->item
);
636 diff_tree_oid(get_commit_tree_oid(commit
->parents
->item
),
637 get_commit_tree_oid(commit
), "", &rev
->diffopt
);
640 diff_root_tree_oid(get_commit_tree_oid(commit
),
643 /* Export the referenced blobs, and remember the marks. */
644 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
645 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
646 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
648 refname
= *revision_sources_at(&revision_sources
, commit
);
650 * FIXME: string_list_remove() below for each ref is overall
651 * O(N^2). Compared to a history walk and diffing trees, this is
652 * just lost in the noise in practice. However, theoretically a
653 * repo may have enough refs for this to become slow.
655 string_list_remove(&extra_refs
, refname
, 0);
657 refname
= anonymize_refname(refname
);
658 anonymize_ident_line(&committer
, &committer_end
);
659 anonymize_ident_line(&author
, &author_end
);
662 mark_next_object(&commit
->object
);
664 reencoded
= anonymize_commit_message(message
);
665 } else if (encoding
) {
666 switch(reencode_mode
) {
668 reencoded
= reencode_string(message
, "UTF-8", encoding
);
673 die("Encountered commit-specific encoding %s in commit "
674 "%s; use --reencode=[yes|no] to handle it",
675 encoding
, oid_to_hex(&commit
->object
.oid
));
678 if (!commit
->parents
)
679 printf("reset %s\n", refname
);
680 printf("commit %s\nmark :%"PRIu32
"\n", refname
, last_idnum
);
681 if (show_original_ids
)
682 printf("original-oid %s\n", oid_to_hex(&commit
->object
.oid
));
683 printf("%.*s\n%.*s\n",
684 (int)(author_end
- author
), author
,
685 (int)(committer_end
- committer
), committer
);
686 if (!reencoded
&& encoding
)
687 printf("encoding %s\n", encoding
);
688 printf("data %u\n%s",
690 ? strlen(reencoded
) : message
691 ? strlen(message
) : 0),
692 reencoded
? reencoded
: message
? message
: "");
694 unuse_commit_buffer(commit
, commit_buffer
);
696 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
697 struct object
*obj
= &p
->item
->object
;
698 int mark
= get_object_mark(obj
);
700 if (!mark
&& !reference_excluded_commits
)
707 printf(":%d\n", mark
);
709 printf("%s\n", oid_to_hex(anonymize
?
710 anonymize_oid(&obj
->oid
) :
716 printf("deleteall\n");
717 log_tree_diff_flush(rev
);
718 string_list_clear(paths_of_changed_objects
, 0);
719 rev
->diffopt
.output_format
= saved_output_format
;
726 static void *anonymize_tag(const void *old
, size_t *len
)
729 struct strbuf out
= STRBUF_INIT
;
730 strbuf_addf(&out
, "tag message %d", counter
++);
731 return strbuf_detach(&out
, len
);
734 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
,
735 struct string_list
*paths_of_changed_objects
)
737 struct commit
*commit
;
738 while (commits
->nr
) {
739 commit
= (struct commit
*)object_array_pop(commits
);
740 if (has_unshown_parent(commit
)) {
741 /* Queue again, to be handled later */
742 add_object_array(&commit
->object
, NULL
, commits
);
745 handle_commit(commit
, revs
, paths_of_changed_objects
);
749 static void handle_tag(const char *name
, struct tag
*tag
)
752 enum object_type type
;
754 const char *tagger
, *tagger_end
, *message
;
755 size_t message_size
= 0;
756 struct object
*tagged
;
760 /* Trees have no identifier in fast-export output, thus we have no way
761 * to output tags of trees, tags of tags of trees, etc. Simply omit
764 tagged
= tag
->tagged
;
765 while (tagged
->type
== OBJ_TAG
) {
766 tagged
= ((struct tag
*)tagged
)->tagged
;
768 if (tagged
->type
== OBJ_TREE
) {
769 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
770 oid_to_hex(&tag
->object
.oid
));
774 buf
= read_object_file(&tag
->object
.oid
, &type
, &size
);
776 die("could not read tag %s", oid_to_hex(&tag
->object
.oid
));
777 message
= memmem(buf
, size
, "\n\n", 2);
780 message_size
= strlen(message
);
782 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
784 if (fake_missing_tagger
)
785 tagger
= "tagger Unspecified Tagger "
786 "<unspecified-tagger> 0 +0000";
789 tagger_end
= tagger
+ strlen(tagger
);
792 tagger_end
= strchrnul(tagger
, '\n');
794 anonymize_ident_line(&tagger
, &tagger_end
);
798 name
= anonymize_refname(name
);
800 static struct hashmap tags
;
801 message
= anonymize_mem(&tags
, anonymize_tag
,
802 message
, &message_size
);
806 /* handle signed tags */
808 const char *signature
= strstr(message
,
809 "\n-----BEGIN PGP SIGNATURE-----\n");
811 switch(signed_tag_mode
) {
812 case SIGNED_TAG_ABORT
:
813 die("encountered signed tag %s; use "
814 "--signed-tags=<mode> to handle it",
815 oid_to_hex(&tag
->object
.oid
));
817 warning("exporting signed tag %s",
818 oid_to_hex(&tag
->object
.oid
));
823 warning("stripping signature from tag %s",
824 oid_to_hex(&tag
->object
.oid
));
827 message_size
= signature
+ 1 - message
;
832 /* handle tag->tagged having been filtered out due to paths specified */
833 tagged
= tag
->tagged
;
834 tagged_mark
= get_object_mark(tagged
);
836 switch(tag_of_filtered_mode
) {
837 case TAG_FILTERING_ABORT
:
838 die("tag %s tags unexported object; use "
839 "--tag-of-filtered-object=<mode> to handle it",
840 oid_to_hex(&tag
->object
.oid
));
842 /* Ignore this tag altogether */
846 if (tagged
->type
== OBJ_TAG
&& !mark_tags
) {
847 die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
848 } else if (tagged
->type
== OBJ_COMMIT
) {
849 p
= rewrite_commit((struct commit
*)tagged
);
851 printf("reset %s\nfrom %s\n\n",
852 name
, oid_to_hex(&null_oid
));
856 tagged_mark
= get_object_mark(&p
->object
);
858 /* tagged->type is either OBJ_BLOB or OBJ_TAG */
859 tagged_mark
= get_object_mark(tagged
);
864 if (tagged
->type
== OBJ_TAG
) {
865 printf("reset %s\nfrom %s\n\n",
866 name
, oid_to_hex(&null_oid
));
868 if (starts_with(name
, "refs/tags/"))
870 printf("tag %s\n", name
);
872 mark_next_object(&tag
->object
);
873 printf("mark :%"PRIu32
"\n", last_idnum
);
876 printf("from :%d\n", tagged_mark
);
878 printf("from %s\n", oid_to_hex(&tagged
->oid
));
880 if (show_original_ids
)
881 printf("original-oid %s\n", oid_to_hex(&tag
->object
.oid
));
882 printf("%.*s%sdata %d\n%.*s\n",
883 (int)(tagger_end
- tagger
), tagger
,
884 tagger
== tagger_end
? "" : "\n",
885 (int)message_size
, (int)message_size
, message
? message
: "");
889 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
891 switch (e
->item
->type
) {
893 return (struct commit
*)e
->item
;
895 struct tag
*tag
= (struct tag
*)e
->item
;
897 /* handle nested tags */
898 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
899 parse_object(the_repository
, &tag
->object
.oid
);
900 string_list_append(&tag_refs
, full_name
)->util
= tag
;
901 tag
= (struct tag
*)tag
->tagged
;
904 die("Tag %s points nowhere?", e
->name
);
905 return (struct commit
*)tag
;
913 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
917 for (i
= 0; i
< info
->nr
; i
++) {
918 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
919 struct object_id oid
;
920 struct commit
*commit
;
923 if (e
->flags
& UNINTERESTING
)
926 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &full_name
) != 1)
931 private = apply_refspecs(&refspecs
, full_name
);
938 commit
= get_commit(e
, full_name
);
940 warning("%s: Unexpected object of type %s, skipping.",
942 type_name(e
->item
->type
));
946 switch(commit
->object
.type
) {
950 export_blob(&commit
->object
.oid
);
952 default: /* OBJ_TAG (nested tags) is already handled */
953 warning("Tag points to object of unexpected type %s, skipping.",
954 type_name(commit
->object
.type
));
959 * Make sure this ref gets properly updated eventually, whether
960 * through a commit or manually at the end.
962 if (e
->item
->type
!= OBJ_TAG
)
963 string_list_append(&extra_refs
, full_name
)->util
= commit
;
965 if (!*revision_sources_at(&revision_sources
, commit
))
966 *revision_sources_at(&revision_sources
, commit
) = full_name
;
969 string_list_sort(&extra_refs
);
970 string_list_remove_duplicates(&extra_refs
, 0);
973 static void handle_tags_and_duplicates(struct string_list
*extras
)
975 struct commit
*commit
;
978 for (i
= extras
->nr
- 1; i
>= 0; i
--) {
979 const char *name
= extras
->items
[i
].string
;
980 struct object
*object
= extras
->items
[i
].util
;
983 switch (object
->type
) {
985 handle_tag(name
, (struct tag
*)object
);
989 name
= anonymize_refname(name
);
990 /* create refs pointing to already seen commits */
991 commit
= rewrite_commit((struct commit
*)object
);
994 * Neither this object nor any of its
995 * ancestors touch any relevant paths, so
996 * it has been filtered to nothing. Delete
999 printf("reset %s\nfrom %s\n\n",
1000 name
, oid_to_hex(&null_oid
));
1004 mark
= get_object_mark(&commit
->object
);
1007 * Getting here means we have a commit which
1008 * was excluded by a negative refspec (e.g.
1009 * fast-export ^master master). If we are
1010 * referencing excluded commits, set the ref
1011 * to the exact commit. Otherwise, the user
1012 * wants the branch exported but every commit
1013 * in its history to be deleted, which basically
1014 * just means deletion of the ref.
1016 if (!reference_excluded_commits
) {
1017 /* delete the ref */
1018 printf("reset %s\nfrom %s\n\n",
1019 name
, oid_to_hex(&null_oid
));
1022 /* set ref to commit using oid, not mark */
1023 printf("reset %s\nfrom %s\n\n", name
,
1024 oid_to_hex(&commit
->object
.oid
));
1028 printf("reset %s\nfrom :%d\n\n", name
, mark
1036 static void export_marks(char *file
)
1040 struct decoration_entry
*deco
= idnums
.entries
;
1044 f
= fopen_for_writing(file
);
1046 die_errno("Unable to open marks file %s for writing.", file
);
1048 for (i
= 0; i
< idnums
.size
; i
++) {
1049 if (deco
->base
&& deco
->base
->type
== 1) {
1050 mark
= ptr_to_mark(deco
->decoration
);
1051 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
1052 oid_to_hex(&deco
->base
->oid
)) < 0) {
1063 error("Unable to write marks file %s.", file
);
1066 static void import_marks(char *input_file
, int check_exists
)
1072 if (check_exists
&& stat(input_file
, &sb
))
1075 f
= xfopen(input_file
, "r");
1076 while (fgets(line
, sizeof(line
), f
)) {
1078 char *line_end
, *mark_end
;
1079 struct object_id oid
;
1080 struct object
*object
;
1081 struct commit
*commit
;
1082 enum object_type type
;
1084 line_end
= strchr(line
, '\n');
1085 if (line
[0] != ':' || !line_end
)
1086 die("corrupt mark line: %s", line
);
1089 mark
= strtoumax(line
+ 1, &mark_end
, 10);
1090 if (!mark
|| mark_end
== line
+ 1
1091 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
1092 die("corrupt mark line: %s", line
);
1094 if (last_idnum
< mark
)
1097 type
= oid_object_info(the_repository
, &oid
, NULL
);
1099 die("object not found: %s", oid_to_hex(&oid
));
1101 if (type
!= OBJ_COMMIT
)
1105 commit
= lookup_commit(the_repository
, &oid
);
1107 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
1109 object
= &commit
->object
;
1111 if (object
->flags
& SHOWN
)
1112 error("Object %s already has a mark", oid_to_hex(&oid
));
1114 mark_object(object
, mark
);
1116 object
->flags
|= SHOWN
;
1121 static void handle_deletes(void)
1124 for (i
= 0; i
< refspecs
.nr
; i
++) {
1125 struct refspec_item
*refspec
= &refspecs
.items
[i
];
1129 printf("reset %s\nfrom %s\n\n",
1130 refspec
->dst
, oid_to_hex(&null_oid
));
1134 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
1136 struct rev_info revs
;
1137 struct object_array commits
= OBJECT_ARRAY_INIT
;
1138 struct commit
*commit
;
1139 char *export_filename
= NULL
,
1140 *import_filename
= NULL
,
1141 *import_filename_if_exists
= NULL
;
1142 uint32_t lastimportid
;
1143 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
1144 struct string_list paths_of_changed_objects
= STRING_LIST_INIT_DUP
;
1145 struct option options
[] = {
1146 OPT_INTEGER(0, "progress", &progress
,
1147 N_("show progress after <n> objects")),
1148 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
1149 N_("select handling of signed tags"),
1150 parse_opt_signed_tag_mode
),
1151 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
1152 N_("select handling of tags that tag filtered objects"),
1153 parse_opt_tag_of_filtered_mode
),
1154 OPT_CALLBACK(0, "reencode", &reencode_mode
, N_("mode"),
1155 N_("select handling of commit messages in an alternate encoding"),
1156 parse_opt_reencode_mode
),
1157 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
1158 N_("Dump marks to this file")),
1159 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
1160 N_("Import marks from this file")),
1161 OPT_STRING(0, "import-marks-if-exists",
1162 &import_filename_if_exists
,
1164 N_("Import marks from this file if it exists")),
1165 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
1166 N_("Fake a tagger when tags lack one")),
1167 OPT_BOOL(0, "full-tree", &full_tree
,
1168 N_("Output full tree for each commit")),
1169 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
1170 N_("Use the done feature to terminate the stream")),
1171 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
1172 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1173 N_("Apply refspec to exported refs")),
1174 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1175 OPT_BOOL(0, "reference-excluded-parents",
1176 &reference_excluded_commits
, N_("Reference parents which are not in fast-export stream by object id")),
1177 OPT_BOOL(0, "show-original-ids", &show_original_ids
,
1178 N_("Show original object ids of blobs/commits")),
1179 OPT_BOOL(0, "mark-tags", &mark_tags
,
1180 N_("Label tags with mark ids")),
1186 usage_with_options (fast_export_usage
, options
);
1188 /* we handle encodings */
1189 git_config(git_default_config
, NULL
);
1191 repo_init_revisions(the_repository
, &revs
, prefix
);
1192 init_revision_sources(&revision_sources
);
1193 revs
.topo_order
= 1;
1194 revs
.sources
= &revision_sources
;
1195 revs
.rewrite_parents
= 1;
1196 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1197 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
);
1198 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1200 usage_with_options (fast_export_usage
, options
);
1202 if (refspecs_list
.nr
) {
1205 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1206 refspec_append(&refspecs
, refspecs_list
.items
[i
].string
);
1208 string_list_clear(&refspecs_list
, 1);
1211 if (use_done_feature
)
1212 printf("feature done\n");
1214 if (import_filename
&& import_filename_if_exists
)
1215 die(_("Cannot pass both --import-marks and --import-marks-if-exists"));
1216 if (import_filename
)
1217 import_marks(import_filename
, 0);
1218 else if (import_filename_if_exists
)
1219 import_marks(import_filename_if_exists
, 1);
1220 lastimportid
= last_idnum
;
1222 if (import_filename
&& revs
.prune_data
.nr
)
1225 get_tags_and_duplicates(&revs
.cmdline
);
1227 if (prepare_revision_walk(&revs
))
1228 die("revision walk setup failed");
1229 revs
.diffopt
.format_callback
= show_filemodify
;
1230 revs
.diffopt
.format_callback_data
= &paths_of_changed_objects
;
1231 revs
.diffopt
.flags
.recursive
= 1;
1232 while ((commit
= get_revision(&revs
))) {
1233 if (has_unshown_parent(commit
)) {
1234 add_object_array(&commit
->object
, NULL
, &commits
);
1237 handle_commit(commit
, &revs
, &paths_of_changed_objects
);
1238 handle_tail(&commits
, &revs
, &paths_of_changed_objects
);
1242 handle_tags_and_duplicates(&extra_refs
);
1243 handle_tags_and_duplicates(&tag_refs
);
1246 if (export_filename
&& lastimportid
!= last_idnum
)
1247 export_marks(export_filename
);
1249 if (use_done_feature
)
1252 refspec_clear(&refspecs
);