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
;
43 static struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
44 static struct string_list tag_refs
= STRING_LIST_INIT_NODUP
;
45 static struct refspec refspecs
= REFSPEC_INIT_FETCH
;
47 static struct revision_sources revision_sources
;
49 static int parse_opt_signed_tag_mode(const struct option
*opt
,
50 const char *arg
, int unset
)
52 if (unset
|| !strcmp(arg
, "abort"))
53 signed_tag_mode
= SIGNED_TAG_ABORT
;
54 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
55 signed_tag_mode
= VERBATIM
;
56 else if (!strcmp(arg
, "warn"))
57 signed_tag_mode
= WARN
;
58 else if (!strcmp(arg
, "warn-strip"))
59 signed_tag_mode
= WARN_STRIP
;
60 else if (!strcmp(arg
, "strip"))
61 signed_tag_mode
= STRIP
;
63 return error("Unknown signed-tags mode: %s", arg
);
67 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
68 const char *arg
, int unset
)
70 if (unset
|| !strcmp(arg
, "abort"))
71 tag_of_filtered_mode
= TAG_FILTERING_ABORT
;
72 else if (!strcmp(arg
, "drop"))
73 tag_of_filtered_mode
= DROP
;
74 else if (!strcmp(arg
, "rewrite"))
75 tag_of_filtered_mode
= REWRITE
;
77 return error("Unknown tag-of-filtered mode: %s", arg
);
81 static int parse_opt_reencode_mode(const struct option
*opt
,
82 const char *arg
, int unset
)
85 reencode_mode
= REENCODE_ABORT
;
89 switch (git_parse_maybe_bool(arg
)) {
91 reencode_mode
= REENCODE_NO
;
94 reencode_mode
= REENCODE_YES
;
97 if (!strcasecmp(arg
, "abort"))
98 reencode_mode
= REENCODE_ABORT
;
100 return error("Unknown reencoding mode: %s", arg
);
106 static struct decoration idnums
;
107 static uint32_t last_idnum
;
109 static int has_unshown_parent(struct commit
*commit
)
111 struct commit_list
*parent
;
113 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
114 if (!(parent
->item
->object
.flags
& SHOWN
) &&
115 !(parent
->item
->object
.flags
& UNINTERESTING
))
120 struct anonymized_entry
{
121 struct hashmap_entry hash
;
128 static int anonymized_entry_cmp(const void *unused_cmp_data
,
129 const void *va
, const void *vb
,
130 const void *unused_keydata
)
132 const struct anonymized_entry
*a
= va
, *b
= vb
;
133 return a
->orig_len
!= b
->orig_len
||
134 memcmp(a
->orig
, b
->orig
, a
->orig_len
);
138 * Basically keep a cache of X->Y so that we can repeatedly replace
139 * the same anonymized string with another. The actual generation
140 * is farmed out to the generate function.
142 static const void *anonymize_mem(struct hashmap
*map
,
143 void *(*generate
)(const void *, size_t *),
144 const void *orig
, size_t *len
)
146 struct anonymized_entry key
, *ret
;
149 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
151 hashmap_entry_init(&key
, memhash(orig
, *len
));
154 ret
= hashmap_get(map
, &key
, NULL
);
157 ret
= xmalloc(sizeof(*ret
));
158 hashmap_entry_init(&ret
->hash
, key
.hash
.hash
);
159 ret
->orig
= xstrdup(orig
);
160 ret
->orig_len
= *len
;
161 ret
->anon
= generate(orig
, len
);
162 ret
->anon_len
= *len
;
163 hashmap_put(map
, ret
);
166 *len
= ret
->anon_len
;
171 * We anonymize each component of a path individually,
172 * so that paths a/b and a/c will share a common root.
173 * The paths are cached via anonymize_mem so that repeated
174 * lookups for "a" will yield the same value.
176 static void anonymize_path(struct strbuf
*out
, const char *path
,
178 void *(*generate
)(const void *, size_t *))
181 const char *end_of_component
= strchrnul(path
, '/');
182 size_t len
= end_of_component
- path
;
183 const char *c
= anonymize_mem(map
, generate
, path
, &len
);
184 strbuf_add(out
, c
, len
);
185 path
= end_of_component
;
187 strbuf_addch(out
, *path
++);
191 static inline void *mark_to_ptr(uint32_t mark
)
193 return (void *)(uintptr_t)mark
;
196 static inline uint32_t ptr_to_mark(void * mark
)
198 return (uint32_t)(uintptr_t)mark
;
201 static inline void mark_object(struct object
*object
, uint32_t mark
)
203 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
206 static inline void mark_next_object(struct object
*object
)
208 mark_object(object
, ++last_idnum
);
211 static int get_object_mark(struct object
*object
)
213 void *decoration
= lookup_decoration(&idnums
, object
);
216 return ptr_to_mark(decoration
);
219 static struct commit
*rewrite_commit(struct commit
*p
)
222 if (p
->parents
&& p
->parents
->next
)
224 if (p
->object
.flags
& UNINTERESTING
)
226 if (!(p
->object
.flags
& TREESAME
))
230 p
= p
->parents
->item
;
235 static void show_progress(void)
237 static int counter
= 0;
240 if ((++counter
% progress
) == 0)
241 printf("progress %d objects\n", counter
);
245 * Ideally we would want some transformation of the blob data here
246 * that is unreversible, but would still be the same size and have
247 * the same data relationship to other blobs (so that we get the same
248 * delta and packing behavior as the original). But the first and last
249 * requirements there are probably mutually exclusive, so let's take
250 * the easy way out for now, and just generate arbitrary content.
252 * There's no need to cache this result with anonymize_mem, since
253 * we already handle blob content caching with marks.
255 static char *anonymize_blob(unsigned long *size
)
258 struct strbuf out
= STRBUF_INIT
;
259 strbuf_addf(&out
, "anonymous blob %d", counter
++);
261 return strbuf_detach(&out
, NULL
);
264 static void export_blob(const struct object_id
*oid
)
267 enum object_type type
;
269 struct object
*object
;
275 if (is_null_oid(oid
))
278 object
= lookup_object(the_repository
, oid
);
279 if (object
&& object
->flags
& SHOWN
)
283 buf
= anonymize_blob(&size
);
284 object
= (struct object
*)lookup_blob(the_repository
, oid
);
287 buf
= read_object_file(oid
, &type
, &size
);
289 die("could not read blob %s", oid_to_hex(oid
));
290 if (check_object_signature(oid
, buf
, size
, type_name(type
)) < 0)
291 die("oid mismatch in blob %s", oid_to_hex(oid
));
292 object
= parse_object_buffer(the_repository
, oid
, type
,
297 die("Could not read blob %s", oid_to_hex(oid
));
299 mark_next_object(object
);
301 printf("blob\nmark :%"PRIu32
"\n", last_idnum
);
302 if (show_original_ids
)
303 printf("original-oid %s\n", oid_to_hex(oid
));
304 printf("data %"PRIuMAX
"\n", (uintmax_t)size
);
305 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
306 die_errno("could not write blob '%s'", oid_to_hex(oid
));
311 object
->flags
|= SHOWN
;
316 static int depth_first(const void *a_
, const void *b_
)
318 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
319 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
320 const char *name_a
, *name_b
;
321 int len_a
, len_b
, len
;
324 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
325 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
327 len_a
= strlen(name_a
);
328 len_b
= strlen(name_b
);
329 len
= (len_a
< len_b
) ? len_a
: len_b
;
331 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
332 cmp
= memcmp(name_a
, name_b
, len
);
339 * Move 'R'ename entries last so that all references of the file
340 * appear in the output before it is renamed (e.g., when a file
341 * was copied and renamed in the same commit).
343 return (a
->status
== 'R') - (b
->status
== 'R');
346 static void print_path_1(const char *path
)
348 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
350 quote_c_style(path
, NULL
, stdout
, 0);
351 else if (strchr(path
, ' '))
352 printf("\"%s\"", path
);
357 static void *anonymize_path_component(const void *path
, size_t *len
)
360 struct strbuf out
= STRBUF_INIT
;
361 strbuf_addf(&out
, "path%d", counter
++);
362 return strbuf_detach(&out
, len
);
365 static void print_path(const char *path
)
370 static struct hashmap paths
;
371 static struct strbuf anon
= STRBUF_INIT
;
373 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
374 print_path_1(anon
.buf
);
379 static void *generate_fake_oid(const void *old
, size_t *len
)
381 static uint32_t counter
= 1; /* avoid null oid */
382 const unsigned hashsz
= the_hash_algo
->rawsz
;
383 unsigned char *out
= xcalloc(hashsz
, 1);
384 put_be32(out
+ hashsz
- 4, counter
++);
388 static const struct object_id
*anonymize_oid(const struct object_id
*oid
)
390 static struct hashmap objs
;
391 size_t len
= the_hash_algo
->rawsz
;
392 return anonymize_mem(&objs
, generate_fake_oid
, oid
, &len
);
395 static void show_filemodify(struct diff_queue_struct
*q
,
396 struct diff_options
*options
, void *data
)
399 struct string_list
*changed
= data
;
402 * Handle files below a directory first, in case they are all deleted
403 * and the directory changes to a file or symlink.
405 QSORT(q
->queue
, q
->nr
, depth_first
);
407 for (i
= 0; i
< q
->nr
; i
++) {
408 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
409 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
411 switch (q
->queue
[i
]->status
) {
412 case DIFF_STATUS_DELETED
:
414 print_path(spec
->path
);
415 string_list_insert(changed
, spec
->path
);
419 case DIFF_STATUS_COPIED
:
420 case DIFF_STATUS_RENAMED
:
422 * If a change in the file corresponding to ospec->path
423 * has been observed, we cannot trust its contents
424 * because the diff is calculated based on the prior
425 * contents, not the current contents. So, declare a
426 * copy or rename only if there was no change observed.
428 if (!string_list_has_string(changed
, ospec
->path
)) {
429 printf("%c ", q
->queue
[i
]->status
);
430 print_path(ospec
->path
);
432 print_path(spec
->path
);
433 string_list_insert(changed
, spec
->path
);
436 if (oideq(&ospec
->oid
, &spec
->oid
) &&
437 ospec
->mode
== spec
->mode
)
442 case DIFF_STATUS_TYPE_CHANGED
:
443 case DIFF_STATUS_MODIFIED
:
444 case DIFF_STATUS_ADDED
:
446 * Links refer to objects in another repositories;
447 * output the SHA-1 verbatim.
449 if (no_data
|| S_ISGITLINK(spec
->mode
))
450 printf("M %06o %s ", spec
->mode
,
451 oid_to_hex(anonymize
?
452 anonymize_oid(&spec
->oid
) :
455 struct object
*object
= lookup_object(the_repository
,
457 printf("M %06o :%d ", spec
->mode
,
458 get_object_mark(object
));
460 print_path(spec
->path
);
461 string_list_insert(changed
, spec
->path
);
466 die("Unexpected comparison status '%c' for %s, %s",
468 ospec
->path
? ospec
->path
: "none",
469 spec
->path
? spec
->path
: "none");
474 static const char *find_encoding(const char *begin
, const char *end
)
476 const char *needle
= "\nencoding ";
479 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
480 needle
, strlen(needle
));
483 bol
+= strlen(needle
);
484 eol
= strchrnul(bol
, '\n');
489 static void *anonymize_ref_component(const void *old
, size_t *len
)
492 struct strbuf out
= STRBUF_INIT
;
493 strbuf_addf(&out
, "ref%d", counter
++);
494 return strbuf_detach(&out
, len
);
497 static const char *anonymize_refname(const char *refname
)
500 * If any of these prefixes is found, we will leave it intact
501 * so that tags remain tags and so forth.
503 static const char *prefixes
[] = {
509 static struct hashmap refs
;
510 static struct strbuf anon
= STRBUF_INIT
;
514 * We also leave "master" as a special case, since it does not reveal
515 * anything interesting.
517 if (!strcmp(refname
, "refs/heads/master"))
521 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
522 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
523 strbuf_addstr(&anon
, prefixes
[i
]);
528 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
533 * We do not even bother to cache commit messages, as they are unlikely
534 * to be repeated verbatim, and it is not that interesting when they are.
536 static char *anonymize_commit_message(const char *old
)
539 return xstrfmt("subject %d\n\nbody\n", counter
++);
542 static struct hashmap idents
;
543 static void *anonymize_ident(const void *old
, size_t *len
)
546 struct strbuf out
= STRBUF_INIT
;
547 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
549 return strbuf_detach(&out
, len
);
553 * Our strategy here is to anonymize the names and email addresses,
554 * but keep timestamps intact, as they influence things like traversal
555 * order (and by themselves should not be too revealing).
557 static void anonymize_ident_line(const char **beg
, const char **end
)
559 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
560 static unsigned which_buffer
;
563 struct ident_split split
;
564 const char *end_of_header
;
566 out
= &buffers
[which_buffer
++];
567 which_buffer
%= ARRAY_SIZE(buffers
);
570 /* skip "committer", "author", "tagger", etc */
571 end_of_header
= strchr(*beg
, ' ');
573 BUG("malformed line fed to anonymize_ident_line: %.*s",
574 (int)(*end
- *beg
), *beg
);
576 strbuf_add(out
, *beg
, end_of_header
- *beg
);
578 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
583 len
= split
.mail_end
- split
.name_begin
;
584 ident
= anonymize_mem(&idents
, anonymize_ident
,
585 split
.name_begin
, &len
);
586 strbuf_add(out
, ident
, len
);
587 strbuf_addch(out
, ' ');
588 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
590 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
594 *end
= out
->buf
+ out
->len
;
597 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
,
598 struct string_list
*paths_of_changed_objects
)
600 int saved_output_format
= rev
->diffopt
.output_format
;
601 const char *commit_buffer
;
602 const char *author
, *author_end
, *committer
, *committer_end
;
603 const char *encoding
, *message
;
604 char *reencoded
= NULL
;
605 struct commit_list
*p
;
609 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
611 parse_commit_or_die(commit
);
612 commit_buffer
= get_commit_buffer(commit
, NULL
);
613 author
= strstr(commit_buffer
, "\nauthor ");
615 die("could not find author in commit %s",
616 oid_to_hex(&commit
->object
.oid
));
618 author_end
= strchrnul(author
, '\n');
619 committer
= strstr(author_end
, "\ncommitter ");
621 die("could not find committer in commit %s",
622 oid_to_hex(&commit
->object
.oid
));
624 committer_end
= strchrnul(committer
, '\n');
625 message
= strstr(committer_end
, "\n\n");
626 encoding
= find_encoding(committer_end
, message
);
630 if (commit
->parents
&&
631 (get_object_mark(&commit
->parents
->item
->object
) != 0 ||
632 reference_excluded_commits
) &&
634 parse_commit_or_die(commit
->parents
->item
);
635 diff_tree_oid(get_commit_tree_oid(commit
->parents
->item
),
636 get_commit_tree_oid(commit
), "", &rev
->diffopt
);
639 diff_root_tree_oid(get_commit_tree_oid(commit
),
642 /* Export the referenced blobs, and remember the marks. */
643 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
644 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
645 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
647 refname
= *revision_sources_at(&revision_sources
, commit
);
649 * FIXME: string_list_remove() below for each ref is overall
650 * O(N^2). Compared to a history walk and diffing trees, this is
651 * just lost in the noise in practice. However, theoretically a
652 * repo may have enough refs for this to become slow.
654 string_list_remove(&extra_refs
, refname
, 0);
656 refname
= anonymize_refname(refname
);
657 anonymize_ident_line(&committer
, &committer_end
);
658 anonymize_ident_line(&author
, &author_end
);
661 mark_next_object(&commit
->object
);
663 reencoded
= anonymize_commit_message(message
);
664 } else if (encoding
) {
665 switch(reencode_mode
) {
667 reencoded
= reencode_string(message
, "UTF-8", encoding
);
672 die("Encountered commit-specific encoding %s in commit "
673 "%s; use --reencode=[yes|no] to handle it",
674 encoding
, oid_to_hex(&commit
->object
.oid
));
677 if (!commit
->parents
)
678 printf("reset %s\n", refname
);
679 printf("commit %s\nmark :%"PRIu32
"\n", refname
, last_idnum
);
680 if (show_original_ids
)
681 printf("original-oid %s\n", oid_to_hex(&commit
->object
.oid
));
682 printf("%.*s\n%.*s\n",
683 (int)(author_end
- author
), author
,
684 (int)(committer_end
- committer
), committer
);
685 if (!reencoded
&& encoding
)
686 printf("encoding %s\n", encoding
);
687 printf("data %u\n%s",
689 ? strlen(reencoded
) : message
690 ? strlen(message
) : 0),
691 reencoded
? reencoded
: message
? message
: "");
693 unuse_commit_buffer(commit
, commit_buffer
);
695 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
696 struct object
*obj
= &p
->item
->object
;
697 int mark
= get_object_mark(obj
);
699 if (!mark
&& !reference_excluded_commits
)
706 printf(":%d\n", mark
);
708 printf("%s\n", oid_to_hex(anonymize
?
709 anonymize_oid(&obj
->oid
) :
715 printf("deleteall\n");
716 log_tree_diff_flush(rev
);
717 string_list_clear(paths_of_changed_objects
, 0);
718 rev
->diffopt
.output_format
= saved_output_format
;
725 static void *anonymize_tag(const void *old
, size_t *len
)
728 struct strbuf out
= STRBUF_INIT
;
729 strbuf_addf(&out
, "tag message %d", counter
++);
730 return strbuf_detach(&out
, len
);
733 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
,
734 struct string_list
*paths_of_changed_objects
)
736 struct commit
*commit
;
737 while (commits
->nr
) {
738 commit
= (struct commit
*)object_array_pop(commits
);
739 if (has_unshown_parent(commit
)) {
740 /* Queue again, to be handled later */
741 add_object_array(&commit
->object
, NULL
, commits
);
744 handle_commit(commit
, revs
, paths_of_changed_objects
);
748 static void handle_tag(const char *name
, struct tag
*tag
)
751 enum object_type type
;
753 const char *tagger
, *tagger_end
, *message
;
754 size_t message_size
= 0;
755 struct object
*tagged
;
759 /* Trees have no identifier in fast-export output, thus we have no way
760 * to output tags of trees, tags of tags of trees, etc. Simply omit
763 tagged
= tag
->tagged
;
764 while (tagged
->type
== OBJ_TAG
) {
765 tagged
= ((struct tag
*)tagged
)->tagged
;
767 if (tagged
->type
== OBJ_TREE
) {
768 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
769 oid_to_hex(&tag
->object
.oid
));
773 buf
= read_object_file(&tag
->object
.oid
, &type
, &size
);
775 die("could not read tag %s", oid_to_hex(&tag
->object
.oid
));
776 message
= memmem(buf
, size
, "\n\n", 2);
779 message_size
= strlen(message
);
781 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
783 if (fake_missing_tagger
)
784 tagger
= "tagger Unspecified Tagger "
785 "<unspecified-tagger> 0 +0000";
788 tagger_end
= tagger
+ strlen(tagger
);
791 tagger_end
= strchrnul(tagger
, '\n');
793 anonymize_ident_line(&tagger
, &tagger_end
);
797 name
= anonymize_refname(name
);
799 static struct hashmap tags
;
800 message
= anonymize_mem(&tags
, anonymize_tag
,
801 message
, &message_size
);
805 /* handle signed tags */
807 const char *signature
= strstr(message
,
808 "\n-----BEGIN PGP SIGNATURE-----\n");
810 switch(signed_tag_mode
) {
811 case SIGNED_TAG_ABORT
:
812 die("encountered signed tag %s; use "
813 "--signed-tags=<mode> to handle it",
814 oid_to_hex(&tag
->object
.oid
));
816 warning("exporting signed tag %s",
817 oid_to_hex(&tag
->object
.oid
));
822 warning("stripping signature from tag %s",
823 oid_to_hex(&tag
->object
.oid
));
826 message_size
= signature
+ 1 - message
;
831 /* handle tag->tagged having been filtered out due to paths specified */
832 tagged
= tag
->tagged
;
833 tagged_mark
= get_object_mark(tagged
);
835 switch(tag_of_filtered_mode
) {
836 case TAG_FILTERING_ABORT
:
837 die("tag %s tags unexported object; use "
838 "--tag-of-filtered-object=<mode> to handle it",
839 oid_to_hex(&tag
->object
.oid
));
841 /* Ignore this tag altogether */
845 if (tagged
->type
!= OBJ_COMMIT
) {
846 die("tag %s tags unexported %s!",
847 oid_to_hex(&tag
->object
.oid
),
848 type_name(tagged
->type
));
850 p
= rewrite_commit((struct commit
*)tagged
);
852 printf("reset %s\nfrom %s\n\n",
853 name
, oid_to_hex(&null_oid
));
857 tagged_mark
= get_object_mark(&p
->object
);
861 if (starts_with(name
, "refs/tags/"))
863 printf("tag %s\nfrom :%d\n", name
, tagged_mark
);
864 if (show_original_ids
)
865 printf("original-oid %s\n", oid_to_hex(&tag
->object
.oid
));
866 printf("%.*s%sdata %d\n%.*s\n",
867 (int)(tagger_end
- tagger
), tagger
,
868 tagger
== tagger_end
? "" : "\n",
869 (int)message_size
, (int)message_size
, message
? message
: "");
873 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
875 switch (e
->item
->type
) {
877 return (struct commit
*)e
->item
;
879 struct tag
*tag
= (struct tag
*)e
->item
;
881 /* handle nested tags */
882 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
883 parse_object(the_repository
, &tag
->object
.oid
);
884 string_list_append(&tag_refs
, full_name
)->util
= tag
;
885 tag
= (struct tag
*)tag
->tagged
;
888 die("Tag %s points nowhere?", e
->name
);
889 return (struct commit
*)tag
;
897 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
901 for (i
= 0; i
< info
->nr
; i
++) {
902 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
903 struct object_id oid
;
904 struct commit
*commit
;
907 if (e
->flags
& UNINTERESTING
)
910 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &full_name
) != 1)
915 private = apply_refspecs(&refspecs
, full_name
);
922 commit
= get_commit(e
, full_name
);
924 warning("%s: Unexpected object of type %s, skipping.",
926 type_name(e
->item
->type
));
930 switch(commit
->object
.type
) {
934 export_blob(&commit
->object
.oid
);
936 default: /* OBJ_TAG (nested tags) is already handled */
937 warning("Tag points to object of unexpected type %s, skipping.",
938 type_name(commit
->object
.type
));
943 * Make sure this ref gets properly updated eventually, whether
944 * through a commit or manually at the end.
946 if (e
->item
->type
!= OBJ_TAG
)
947 string_list_append(&extra_refs
, full_name
)->util
= commit
;
949 if (!*revision_sources_at(&revision_sources
, commit
))
950 *revision_sources_at(&revision_sources
, commit
) = full_name
;
953 string_list_sort(&extra_refs
);
954 string_list_remove_duplicates(&extra_refs
, 0);
957 static void handle_tags_and_duplicates(struct string_list
*extras
)
959 struct commit
*commit
;
962 for (i
= extras
->nr
- 1; i
>= 0; i
--) {
963 const char *name
= extras
->items
[i
].string
;
964 struct object
*object
= extras
->items
[i
].util
;
967 switch (object
->type
) {
969 handle_tag(name
, (struct tag
*)object
);
973 name
= anonymize_refname(name
);
974 /* create refs pointing to already seen commits */
975 commit
= rewrite_commit((struct commit
*)object
);
978 * Neither this object nor any of its
979 * ancestors touch any relevant paths, so
980 * it has been filtered to nothing. Delete
983 printf("reset %s\nfrom %s\n\n",
984 name
, oid_to_hex(&null_oid
));
988 mark
= get_object_mark(&commit
->object
);
991 * Getting here means we have a commit which
992 * was excluded by a negative refspec (e.g.
993 * fast-export ^master master). If we are
994 * referencing excluded commits, set the ref
995 * to the exact commit. Otherwise, the user
996 * wants the branch exported but every commit
997 * in its history to be deleted, which basically
998 * just means deletion of the ref.
1000 if (!reference_excluded_commits
) {
1001 /* delete the ref */
1002 printf("reset %s\nfrom %s\n\n",
1003 name
, oid_to_hex(&null_oid
));
1006 /* set ref to commit using oid, not mark */
1007 printf("reset %s\nfrom %s\n\n", name
,
1008 oid_to_hex(&commit
->object
.oid
));
1012 printf("reset %s\nfrom :%d\n\n", name
, mark
1020 static void export_marks(char *file
)
1024 struct decoration_entry
*deco
= idnums
.entries
;
1028 f
= fopen_for_writing(file
);
1030 die_errno("Unable to open marks file %s for writing.", file
);
1032 for (i
= 0; i
< idnums
.size
; i
++) {
1033 if (deco
->base
&& deco
->base
->type
== 1) {
1034 mark
= ptr_to_mark(deco
->decoration
);
1035 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
1036 oid_to_hex(&deco
->base
->oid
)) < 0) {
1047 error("Unable to write marks file %s.", file
);
1050 static void import_marks(char *input_file
)
1053 FILE *f
= xfopen(input_file
, "r");
1055 while (fgets(line
, sizeof(line
), f
)) {
1057 char *line_end
, *mark_end
;
1058 struct object_id oid
;
1059 struct object
*object
;
1060 struct commit
*commit
;
1061 enum object_type type
;
1063 line_end
= strchr(line
, '\n');
1064 if (line
[0] != ':' || !line_end
)
1065 die("corrupt mark line: %s", line
);
1068 mark
= strtoumax(line
+ 1, &mark_end
, 10);
1069 if (!mark
|| mark_end
== line
+ 1
1070 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
1071 die("corrupt mark line: %s", line
);
1073 if (last_idnum
< mark
)
1076 type
= oid_object_info(the_repository
, &oid
, NULL
);
1078 die("object not found: %s", oid_to_hex(&oid
));
1080 if (type
!= OBJ_COMMIT
)
1084 commit
= lookup_commit(the_repository
, &oid
);
1086 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
1088 object
= &commit
->object
;
1090 if (object
->flags
& SHOWN
)
1091 error("Object %s already has a mark", oid_to_hex(&oid
));
1093 mark_object(object
, mark
);
1095 object
->flags
|= SHOWN
;
1100 static void handle_deletes(void)
1103 for (i
= 0; i
< refspecs
.nr
; i
++) {
1104 struct refspec_item
*refspec
= &refspecs
.items
[i
];
1108 printf("reset %s\nfrom %s\n\n",
1109 refspec
->dst
, oid_to_hex(&null_oid
));
1113 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
1115 struct rev_info revs
;
1116 struct object_array commits
= OBJECT_ARRAY_INIT
;
1117 struct commit
*commit
;
1118 char *export_filename
= NULL
, *import_filename
= NULL
;
1119 uint32_t lastimportid
;
1120 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
1121 struct string_list paths_of_changed_objects
= STRING_LIST_INIT_DUP
;
1122 struct option options
[] = {
1123 OPT_INTEGER(0, "progress", &progress
,
1124 N_("show progress after <n> objects")),
1125 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
1126 N_("select handling of signed tags"),
1127 parse_opt_signed_tag_mode
),
1128 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
1129 N_("select handling of tags that tag filtered objects"),
1130 parse_opt_tag_of_filtered_mode
),
1131 OPT_CALLBACK(0, "reencode", &reencode_mode
, N_("mode"),
1132 N_("select handling of commit messages in an alternate encoding"),
1133 parse_opt_reencode_mode
),
1134 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
1135 N_("Dump marks to this file")),
1136 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
1137 N_("Import marks from this file")),
1138 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
1139 N_("Fake a tagger when tags lack one")),
1140 OPT_BOOL(0, "full-tree", &full_tree
,
1141 N_("Output full tree for each commit")),
1142 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
1143 N_("Use the done feature to terminate the stream")),
1144 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
1145 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1146 N_("Apply refspec to exported refs")),
1147 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1148 OPT_BOOL(0, "reference-excluded-parents",
1149 &reference_excluded_commits
, N_("Reference parents which are not in fast-export stream by object id")),
1150 OPT_BOOL(0, "show-original-ids", &show_original_ids
,
1151 N_("Show original object ids of blobs/commits")),
1157 usage_with_options (fast_export_usage
, options
);
1159 /* we handle encodings */
1160 git_config(git_default_config
, NULL
);
1162 repo_init_revisions(the_repository
, &revs
, prefix
);
1163 init_revision_sources(&revision_sources
);
1164 revs
.topo_order
= 1;
1165 revs
.sources
= &revision_sources
;
1166 revs
.rewrite_parents
= 1;
1167 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1168 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
);
1169 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1171 usage_with_options (fast_export_usage
, options
);
1173 if (refspecs_list
.nr
) {
1176 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1177 refspec_append(&refspecs
, refspecs_list
.items
[i
].string
);
1179 string_list_clear(&refspecs_list
, 1);
1182 if (use_done_feature
)
1183 printf("feature done\n");
1185 if (import_filename
)
1186 import_marks(import_filename
);
1187 lastimportid
= last_idnum
;
1189 if (import_filename
&& revs
.prune_data
.nr
)
1192 get_tags_and_duplicates(&revs
.cmdline
);
1194 if (prepare_revision_walk(&revs
))
1195 die("revision walk setup failed");
1196 revs
.diffopt
.format_callback
= show_filemodify
;
1197 revs
.diffopt
.format_callback_data
= &paths_of_changed_objects
;
1198 revs
.diffopt
.flags
.recursive
= 1;
1199 while ((commit
= get_revision(&revs
))) {
1200 if (has_unshown_parent(commit
)) {
1201 add_object_array(&commit
->object
, NULL
, &commits
);
1204 handle_commit(commit
, &revs
, &paths_of_changed_objects
);
1205 handle_tail(&commits
, &revs
, &paths_of_changed_objects
);
1209 handle_tags_and_duplicates(&extra_refs
);
1210 handle_tags_and_duplicates(&tag_refs
);
1213 if (export_filename
&& lastimportid
!= last_idnum
)
1214 export_marks(export_filename
);
1216 if (use_done_feature
)
1219 refspec_clear(&refspecs
);