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 { ABORT
, VERBATIM
, WARN
, WARN_STRIP
, STRIP
} signed_tag_mode
= ABORT
;
35 static enum { ERROR
, DROP
, REWRITE
} tag_of_filtered_mode
= ERROR
;
36 static int fake_missing_tagger
;
37 static int use_done_feature
;
40 static struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
41 static struct refspec refspecs
= REFSPEC_INIT_FETCH
;
43 static struct revision_sources revision_sources
;
45 static int parse_opt_signed_tag_mode(const struct option
*opt
,
46 const char *arg
, int unset
)
48 if (unset
|| !strcmp(arg
, "abort"))
49 signed_tag_mode
= ABORT
;
50 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
51 signed_tag_mode
= VERBATIM
;
52 else if (!strcmp(arg
, "warn"))
53 signed_tag_mode
= WARN
;
54 else if (!strcmp(arg
, "warn-strip"))
55 signed_tag_mode
= WARN_STRIP
;
56 else if (!strcmp(arg
, "strip"))
57 signed_tag_mode
= STRIP
;
59 return error("Unknown signed-tags mode: %s", arg
);
63 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
64 const char *arg
, int unset
)
66 if (unset
|| !strcmp(arg
, "abort"))
67 tag_of_filtered_mode
= ERROR
;
68 else if (!strcmp(arg
, "drop"))
69 tag_of_filtered_mode
= DROP
;
70 else if (!strcmp(arg
, "rewrite"))
71 tag_of_filtered_mode
= REWRITE
;
73 return error("Unknown tag-of-filtered mode: %s", arg
);
77 static struct decoration idnums
;
78 static uint32_t last_idnum
;
80 static int has_unshown_parent(struct commit
*commit
)
82 struct commit_list
*parent
;
84 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
85 if (!(parent
->item
->object
.flags
& SHOWN
) &&
86 !(parent
->item
->object
.flags
& UNINTERESTING
))
91 struct anonymized_entry
{
92 struct hashmap_entry hash
;
99 static int anonymized_entry_cmp(const void *unused_cmp_data
,
100 const void *va
, const void *vb
,
101 const void *unused_keydata
)
103 const struct anonymized_entry
*a
= va
, *b
= vb
;
104 return a
->orig_len
!= b
->orig_len
||
105 memcmp(a
->orig
, b
->orig
, a
->orig_len
);
109 * Basically keep a cache of X->Y so that we can repeatedly replace
110 * the same anonymized string with another. The actual generation
111 * is farmed out to the generate function.
113 static const void *anonymize_mem(struct hashmap
*map
,
114 void *(*generate
)(const void *, size_t *),
115 const void *orig
, size_t *len
)
117 struct anonymized_entry key
, *ret
;
120 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
122 hashmap_entry_init(&key
, memhash(orig
, *len
));
125 ret
= hashmap_get(map
, &key
, NULL
);
128 ret
= xmalloc(sizeof(*ret
));
129 hashmap_entry_init(&ret
->hash
, key
.hash
.hash
);
130 ret
->orig
= xstrdup(orig
);
131 ret
->orig_len
= *len
;
132 ret
->anon
= generate(orig
, len
);
133 ret
->anon_len
= *len
;
134 hashmap_put(map
, ret
);
137 *len
= ret
->anon_len
;
142 * We anonymize each component of a path individually,
143 * so that paths a/b and a/c will share a common root.
144 * The paths are cached via anonymize_mem so that repeated
145 * lookups for "a" will yield the same value.
147 static void anonymize_path(struct strbuf
*out
, const char *path
,
149 void *(*generate
)(const void *, size_t *))
152 const char *end_of_component
= strchrnul(path
, '/');
153 size_t len
= end_of_component
- path
;
154 const char *c
= anonymize_mem(map
, generate
, path
, &len
);
155 strbuf_add(out
, c
, len
);
156 path
= end_of_component
;
158 strbuf_addch(out
, *path
++);
162 static inline void *mark_to_ptr(uint32_t mark
)
164 return (void *)(uintptr_t)mark
;
167 static inline uint32_t ptr_to_mark(void * mark
)
169 return (uint32_t)(uintptr_t)mark
;
172 static inline void mark_object(struct object
*object
, uint32_t mark
)
174 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
177 static inline void mark_next_object(struct object
*object
)
179 mark_object(object
, ++last_idnum
);
182 static int get_object_mark(struct object
*object
)
184 void *decoration
= lookup_decoration(&idnums
, object
);
187 return ptr_to_mark(decoration
);
190 static void show_progress(void)
192 static int counter
= 0;
195 if ((++counter
% progress
) == 0)
196 printf("progress %d objects\n", counter
);
200 * Ideally we would want some transformation of the blob data here
201 * that is unreversible, but would still be the same size and have
202 * the same data relationship to other blobs (so that we get the same
203 * delta and packing behavior as the original). But the first and last
204 * requirements there are probably mutually exclusive, so let's take
205 * the easy way out for now, and just generate arbitrary content.
207 * There's no need to cache this result with anonymize_mem, since
208 * we already handle blob content caching with marks.
210 static char *anonymize_blob(unsigned long *size
)
213 struct strbuf out
= STRBUF_INIT
;
214 strbuf_addf(&out
, "anonymous blob %d", counter
++);
216 return strbuf_detach(&out
, NULL
);
219 static void export_blob(const struct object_id
*oid
)
222 enum object_type type
;
224 struct object
*object
;
230 if (is_null_oid(oid
))
233 object
= lookup_object(the_repository
, oid
->hash
);
234 if (object
&& object
->flags
& SHOWN
)
238 buf
= anonymize_blob(&size
);
239 object
= (struct object
*)lookup_blob(the_repository
, oid
);
242 buf
= read_object_file(oid
, &type
, &size
);
244 die("could not read blob %s", oid_to_hex(oid
));
245 if (check_object_signature(oid
, buf
, size
, type_name(type
)) < 0)
246 die("sha1 mismatch in blob %s", oid_to_hex(oid
));
247 object
= parse_object_buffer(the_repository
, oid
, type
,
252 die("Could not read blob %s", oid_to_hex(oid
));
254 mark_next_object(object
);
256 printf("blob\nmark :%"PRIu32
"\ndata %"PRIuMAX
"\n", last_idnum
, (uintmax_t)size
);
257 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
258 die_errno("could not write blob '%s'", oid_to_hex(oid
));
263 object
->flags
|= SHOWN
;
268 static int depth_first(const void *a_
, const void *b_
)
270 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
271 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
272 const char *name_a
, *name_b
;
273 int len_a
, len_b
, len
;
276 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
277 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
279 len_a
= strlen(name_a
);
280 len_b
= strlen(name_b
);
281 len
= (len_a
< len_b
) ? len_a
: len_b
;
283 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
284 cmp
= memcmp(name_a
, name_b
, len
);
291 * Move 'R'ename entries last so that all references of the file
292 * appear in the output before it is renamed (e.g., when a file
293 * was copied and renamed in the same commit).
295 return (a
->status
== 'R') - (b
->status
== 'R');
298 static void print_path_1(const char *path
)
300 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
302 quote_c_style(path
, NULL
, stdout
, 0);
303 else if (strchr(path
, ' '))
304 printf("\"%s\"", path
);
309 static void *anonymize_path_component(const void *path
, size_t *len
)
312 struct strbuf out
= STRBUF_INIT
;
313 strbuf_addf(&out
, "path%d", counter
++);
314 return strbuf_detach(&out
, len
);
317 static void print_path(const char *path
)
322 static struct hashmap paths
;
323 static struct strbuf anon
= STRBUF_INIT
;
325 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
326 print_path_1(anon
.buf
);
331 static void *generate_fake_oid(const void *old
, size_t *len
)
333 static uint32_t counter
= 1; /* avoid null sha1 */
334 unsigned char *out
= xcalloc(GIT_SHA1_RAWSZ
, 1);
335 put_be32(out
+ GIT_SHA1_RAWSZ
- 4, counter
++);
339 static const unsigned char *anonymize_sha1(const struct object_id
*oid
)
341 static struct hashmap sha1s
;
342 size_t len
= GIT_SHA1_RAWSZ
;
343 return anonymize_mem(&sha1s
, generate_fake_oid
, oid
, &len
);
346 static void show_filemodify(struct diff_queue_struct
*q
,
347 struct diff_options
*options
, void *data
)
350 struct string_list
*changed
= data
;
353 * Handle files below a directory first, in case they are all deleted
354 * and the directory changes to a file or symlink.
356 QSORT(q
->queue
, q
->nr
, depth_first
);
358 for (i
= 0; i
< q
->nr
; i
++) {
359 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
360 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
362 switch (q
->queue
[i
]->status
) {
363 case DIFF_STATUS_DELETED
:
365 print_path(spec
->path
);
366 string_list_insert(changed
, spec
->path
);
370 case DIFF_STATUS_COPIED
:
371 case DIFF_STATUS_RENAMED
:
373 * If a change in the file corresponding to ospec->path
374 * has been observed, we cannot trust its contents
375 * because the diff is calculated based on the prior
376 * contents, not the current contents. So, declare a
377 * copy or rename only if there was no change observed.
379 if (!string_list_has_string(changed
, ospec
->path
)) {
380 printf("%c ", q
->queue
[i
]->status
);
381 print_path(ospec
->path
);
383 print_path(spec
->path
);
384 string_list_insert(changed
, spec
->path
);
387 if (oideq(&ospec
->oid
, &spec
->oid
) &&
388 ospec
->mode
== spec
->mode
)
393 case DIFF_STATUS_TYPE_CHANGED
:
394 case DIFF_STATUS_MODIFIED
:
395 case DIFF_STATUS_ADDED
:
397 * Links refer to objects in another repositories;
398 * output the SHA-1 verbatim.
400 if (no_data
|| S_ISGITLINK(spec
->mode
))
401 printf("M %06o %s ", spec
->mode
,
402 sha1_to_hex(anonymize
?
403 anonymize_sha1(&spec
->oid
) :
406 struct object
*object
= lookup_object(the_repository
,
408 printf("M %06o :%d ", spec
->mode
,
409 get_object_mark(object
));
411 print_path(spec
->path
);
412 string_list_insert(changed
, spec
->path
);
417 die("Unexpected comparison status '%c' for %s, %s",
419 ospec
->path
? ospec
->path
: "none",
420 spec
->path
? spec
->path
: "none");
425 static const char *find_encoding(const char *begin
, const char *end
)
427 const char *needle
= "\nencoding ";
430 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
431 needle
, strlen(needle
));
433 return git_commit_encoding
;
434 bol
+= strlen(needle
);
435 eol
= strchrnul(bol
, '\n');
440 static void *anonymize_ref_component(const void *old
, size_t *len
)
443 struct strbuf out
= STRBUF_INIT
;
444 strbuf_addf(&out
, "ref%d", counter
++);
445 return strbuf_detach(&out
, len
);
448 static const char *anonymize_refname(const char *refname
)
451 * If any of these prefixes is found, we will leave it intact
452 * so that tags remain tags and so forth.
454 static const char *prefixes
[] = {
460 static struct hashmap refs
;
461 static struct strbuf anon
= STRBUF_INIT
;
465 * We also leave "master" as a special case, since it does not reveal
466 * anything interesting.
468 if (!strcmp(refname
, "refs/heads/master"))
472 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
473 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
474 strbuf_addstr(&anon
, prefixes
[i
]);
479 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
484 * We do not even bother to cache commit messages, as they are unlikely
485 * to be repeated verbatim, and it is not that interesting when they are.
487 static char *anonymize_commit_message(const char *old
)
490 return xstrfmt("subject %d\n\nbody\n", counter
++);
493 static struct hashmap idents
;
494 static void *anonymize_ident(const void *old
, size_t *len
)
497 struct strbuf out
= STRBUF_INIT
;
498 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
500 return strbuf_detach(&out
, len
);
504 * Our strategy here is to anonymize the names and email addresses,
505 * but keep timestamps intact, as they influence things like traversal
506 * order (and by themselves should not be too revealing).
508 static void anonymize_ident_line(const char **beg
, const char **end
)
510 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
511 static unsigned which_buffer
;
514 struct ident_split split
;
515 const char *end_of_header
;
517 out
= &buffers
[which_buffer
++];
518 which_buffer
%= ARRAY_SIZE(buffers
);
521 /* skip "committer", "author", "tagger", etc */
522 end_of_header
= strchr(*beg
, ' ');
524 BUG("malformed line fed to anonymize_ident_line: %.*s",
525 (int)(*end
- *beg
), *beg
);
527 strbuf_add(out
, *beg
, end_of_header
- *beg
);
529 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
534 len
= split
.mail_end
- split
.name_begin
;
535 ident
= anonymize_mem(&idents
, anonymize_ident
,
536 split
.name_begin
, &len
);
537 strbuf_add(out
, ident
, len
);
538 strbuf_addch(out
, ' ');
539 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
541 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
545 *end
= out
->buf
+ out
->len
;
548 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
,
549 struct string_list
*paths_of_changed_objects
)
551 int saved_output_format
= rev
->diffopt
.output_format
;
552 const char *commit_buffer
;
553 const char *author
, *author_end
, *committer
, *committer_end
;
554 const char *encoding
, *message
;
555 char *reencoded
= NULL
;
556 struct commit_list
*p
;
560 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
562 parse_commit_or_die(commit
);
563 commit_buffer
= get_commit_buffer(commit
, NULL
);
564 author
= strstr(commit_buffer
, "\nauthor ");
566 die("could not find author in commit %s",
567 oid_to_hex(&commit
->object
.oid
));
569 author_end
= strchrnul(author
, '\n');
570 committer
= strstr(author_end
, "\ncommitter ");
572 die("could not find committer in commit %s",
573 oid_to_hex(&commit
->object
.oid
));
575 committer_end
= strchrnul(committer
, '\n');
576 message
= strstr(committer_end
, "\n\n");
577 encoding
= find_encoding(committer_end
, message
);
581 if (commit
->parents
&&
582 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
584 parse_commit_or_die(commit
->parents
->item
);
585 diff_tree_oid(get_commit_tree_oid(commit
->parents
->item
),
586 get_commit_tree_oid(commit
), "", &rev
->diffopt
);
589 diff_root_tree_oid(get_commit_tree_oid(commit
),
592 /* Export the referenced blobs, and remember the marks. */
593 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
594 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
595 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
597 refname
= *revision_sources_at(&revision_sources
, commit
);
599 refname
= anonymize_refname(refname
);
600 anonymize_ident_line(&committer
, &committer_end
);
601 anonymize_ident_line(&author
, &author_end
);
604 mark_next_object(&commit
->object
);
606 reencoded
= anonymize_commit_message(message
);
607 else if (!is_encoding_utf8(encoding
))
608 reencoded
= reencode_string(message
, "UTF-8", encoding
);
609 if (!commit
->parents
)
610 printf("reset %s\n", refname
);
611 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
613 (int)(author_end
- author
), author
,
614 (int)(committer_end
- committer
), committer
,
616 ? strlen(reencoded
) : message
617 ? strlen(message
) : 0),
618 reencoded
? reencoded
: message
? message
: "");
620 unuse_commit_buffer(commit
, commit_buffer
);
622 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
623 int mark
= get_object_mark(&p
->item
->object
);
627 printf("from :%d\n", mark
);
629 printf("merge :%d\n", mark
);
634 printf("deleteall\n");
635 log_tree_diff_flush(rev
);
636 string_list_clear(paths_of_changed_objects
, 0);
637 rev
->diffopt
.output_format
= saved_output_format
;
644 static void *anonymize_tag(const void *old
, size_t *len
)
647 struct strbuf out
= STRBUF_INIT
;
648 strbuf_addf(&out
, "tag message %d", counter
++);
649 return strbuf_detach(&out
, len
);
652 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
,
653 struct string_list
*paths_of_changed_objects
)
655 struct commit
*commit
;
656 while (commits
->nr
) {
657 commit
= (struct commit
*)object_array_pop(commits
);
658 if (has_unshown_parent(commit
)) {
659 /* Queue again, to be handled later */
660 add_object_array(&commit
->object
, NULL
, commits
);
663 handle_commit(commit
, revs
, paths_of_changed_objects
);
667 static void handle_tag(const char *name
, struct tag
*tag
)
670 enum object_type type
;
672 const char *tagger
, *tagger_end
, *message
;
673 size_t message_size
= 0;
674 struct object
*tagged
;
678 /* Trees have no identifier in fast-export output, thus we have no way
679 * to output tags of trees, tags of tags of trees, etc. Simply omit
682 tagged
= tag
->tagged
;
683 while (tagged
->type
== OBJ_TAG
) {
684 tagged
= ((struct tag
*)tagged
)->tagged
;
686 if (tagged
->type
== OBJ_TREE
) {
687 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
688 oid_to_hex(&tag
->object
.oid
));
692 buf
= read_object_file(&tag
->object
.oid
, &type
, &size
);
694 die("could not read tag %s", oid_to_hex(&tag
->object
.oid
));
695 message
= memmem(buf
, size
, "\n\n", 2);
698 message_size
= strlen(message
);
700 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
702 if (fake_missing_tagger
)
703 tagger
= "tagger Unspecified Tagger "
704 "<unspecified-tagger> 0 +0000";
707 tagger_end
= tagger
+ strlen(tagger
);
710 tagger_end
= strchrnul(tagger
, '\n');
712 anonymize_ident_line(&tagger
, &tagger_end
);
716 name
= anonymize_refname(name
);
718 static struct hashmap tags
;
719 message
= anonymize_mem(&tags
, anonymize_tag
,
720 message
, &message_size
);
724 /* handle signed tags */
726 const char *signature
= strstr(message
,
727 "\n-----BEGIN PGP SIGNATURE-----\n");
729 switch(signed_tag_mode
) {
731 die("encountered signed tag %s; use "
732 "--signed-tags=<mode> to handle it",
733 oid_to_hex(&tag
->object
.oid
));
735 warning("exporting signed tag %s",
736 oid_to_hex(&tag
->object
.oid
));
741 warning("stripping signature from tag %s",
742 oid_to_hex(&tag
->object
.oid
));
745 message_size
= signature
+ 1 - message
;
750 /* handle tag->tagged having been filtered out due to paths specified */
751 tagged
= tag
->tagged
;
752 tagged_mark
= get_object_mark(tagged
);
754 switch(tag_of_filtered_mode
) {
756 die("tag %s tags unexported object; use "
757 "--tag-of-filtered-object=<mode> to handle it",
758 oid_to_hex(&tag
->object
.oid
));
760 /* Ignore this tag altogether */
764 if (tagged
->type
!= OBJ_COMMIT
) {
765 die("tag %s tags unexported %s!",
766 oid_to_hex(&tag
->object
.oid
),
767 type_name(tagged
->type
));
769 p
= (struct commit
*)tagged
;
771 if (p
->parents
&& p
->parents
->next
)
773 if (p
->object
.flags
& UNINTERESTING
)
775 if (!(p
->object
.flags
& TREESAME
))
778 die("can't find replacement commit for tag %s",
779 oid_to_hex(&tag
->object
.oid
));
780 p
= p
->parents
->item
;
782 tagged_mark
= get_object_mark(&p
->object
);
786 if (starts_with(name
, "refs/tags/"))
788 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
790 (int)(tagger_end
- tagger
), tagger
,
791 tagger
== tagger_end
? "" : "\n",
792 (int)message_size
, (int)message_size
, message
? message
: "");
796 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
798 switch (e
->item
->type
) {
800 return (struct commit
*)e
->item
;
802 struct tag
*tag
= (struct tag
*)e
->item
;
804 /* handle nested tags */
805 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
806 parse_object(the_repository
, &tag
->object
.oid
);
807 string_list_append(&extra_refs
, full_name
)->util
= tag
;
808 tag
= (struct tag
*)tag
->tagged
;
811 die("Tag %s points nowhere?", e
->name
);
812 return (struct commit
*)tag
;
820 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
824 for (i
= 0; i
< info
->nr
; i
++) {
825 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
826 struct object_id oid
;
827 struct commit
*commit
;
830 if (e
->flags
& UNINTERESTING
)
833 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &full_name
) != 1)
838 private = apply_refspecs(&refspecs
, full_name
);
845 commit
= get_commit(e
, full_name
);
847 warning("%s: Unexpected object of type %s, skipping.",
849 type_name(e
->item
->type
));
853 switch(commit
->object
.type
) {
857 export_blob(&commit
->object
.oid
);
859 default: /* OBJ_TAG (nested tags) is already handled */
860 warning("Tag points to object of unexpected type %s, skipping.",
861 type_name(commit
->object
.type
));
866 * This ref will not be updated through a commit, lets make
867 * sure it gets properly updated eventually.
869 if (*revision_sources_at(&revision_sources
, commit
) ||
870 commit
->object
.flags
& SHOWN
)
871 string_list_append(&extra_refs
, full_name
)->util
= commit
;
872 if (!*revision_sources_at(&revision_sources
, commit
))
873 *revision_sources_at(&revision_sources
, commit
) = full_name
;
877 static void handle_tags_and_duplicates(void)
879 struct commit
*commit
;
882 for (i
= extra_refs
.nr
- 1; i
>= 0; i
--) {
883 const char *name
= extra_refs
.items
[i
].string
;
884 struct object
*object
= extra_refs
.items
[i
].util
;
885 switch (object
->type
) {
887 handle_tag(name
, (struct tag
*)object
);
891 name
= anonymize_refname(name
);
892 /* create refs pointing to already seen commits */
893 commit
= (struct commit
*)object
;
894 printf("reset %s\nfrom :%d\n\n", name
,
895 get_object_mark(&commit
->object
));
902 static void export_marks(char *file
)
906 struct decoration_entry
*deco
= idnums
.entries
;
910 f
= fopen_for_writing(file
);
912 die_errno("Unable to open marks file %s for writing.", file
);
914 for (i
= 0; i
< idnums
.size
; i
++) {
915 if (deco
->base
&& deco
->base
->type
== 1) {
916 mark
= ptr_to_mark(deco
->decoration
);
917 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
918 oid_to_hex(&deco
->base
->oid
)) < 0) {
929 error("Unable to write marks file %s.", file
);
932 static void import_marks(char *input_file
)
935 FILE *f
= xfopen(input_file
, "r");
937 while (fgets(line
, sizeof(line
), f
)) {
939 char *line_end
, *mark_end
;
940 struct object_id oid
;
941 struct object
*object
;
942 struct commit
*commit
;
943 enum object_type type
;
945 line_end
= strchr(line
, '\n');
946 if (line
[0] != ':' || !line_end
)
947 die("corrupt mark line: %s", line
);
950 mark
= strtoumax(line
+ 1, &mark_end
, 10);
951 if (!mark
|| mark_end
== line
+ 1
952 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
953 die("corrupt mark line: %s", line
);
955 if (last_idnum
< mark
)
958 type
= oid_object_info(the_repository
, &oid
, NULL
);
960 die("object not found: %s", oid_to_hex(&oid
));
962 if (type
!= OBJ_COMMIT
)
966 commit
= lookup_commit(the_repository
, &oid
);
968 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
970 object
= &commit
->object
;
972 if (object
->flags
& SHOWN
)
973 error("Object %s already has a mark", oid_to_hex(&oid
));
975 mark_object(object
, mark
);
977 object
->flags
|= SHOWN
;
982 static void handle_deletes(void)
985 for (i
= 0; i
< refspecs
.nr
; i
++) {
986 struct refspec_item
*refspec
= &refspecs
.items
[i
];
990 printf("reset %s\nfrom %s\n\n",
991 refspec
->dst
, sha1_to_hex(null_sha1
));
995 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
997 struct rev_info revs
;
998 struct object_array commits
= OBJECT_ARRAY_INIT
;
999 struct commit
*commit
;
1000 char *export_filename
= NULL
, *import_filename
= NULL
;
1001 uint32_t lastimportid
;
1002 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
1003 struct string_list paths_of_changed_objects
= STRING_LIST_INIT_DUP
;
1004 struct option options
[] = {
1005 OPT_INTEGER(0, "progress", &progress
,
1006 N_("show progress after <n> objects")),
1007 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
1008 N_("select handling of signed tags"),
1009 parse_opt_signed_tag_mode
),
1010 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
1011 N_("select handling of tags that tag filtered objects"),
1012 parse_opt_tag_of_filtered_mode
),
1013 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
1014 N_("Dump marks to this file")),
1015 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
1016 N_("Import marks from this file")),
1017 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
1018 N_("Fake a tagger when tags lack one")),
1019 OPT_BOOL(0, "full-tree", &full_tree
,
1020 N_("Output full tree for each commit")),
1021 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
1022 N_("Use the done feature to terminate the stream")),
1023 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
1024 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1025 N_("Apply refspec to exported refs")),
1026 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1031 usage_with_options (fast_export_usage
, options
);
1033 /* we handle encodings */
1034 git_config(git_default_config
, NULL
);
1036 repo_init_revisions(the_repository
, &revs
, prefix
);
1037 init_revision_sources(&revision_sources
);
1038 revs
.topo_order
= 1;
1039 revs
.sources
= &revision_sources
;
1040 revs
.rewrite_parents
= 1;
1041 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1042 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
);
1043 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1045 usage_with_options (fast_export_usage
, options
);
1047 if (refspecs_list
.nr
) {
1050 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1051 refspec_append(&refspecs
, refspecs_list
.items
[i
].string
);
1053 string_list_clear(&refspecs_list
, 1);
1056 if (use_done_feature
)
1057 printf("feature done\n");
1059 if (import_filename
)
1060 import_marks(import_filename
);
1061 lastimportid
= last_idnum
;
1063 if (import_filename
&& revs
.prune_data
.nr
)
1066 get_tags_and_duplicates(&revs
.cmdline
);
1068 if (prepare_revision_walk(&revs
))
1069 die("revision walk setup failed");
1070 revs
.diffopt
.format_callback
= show_filemodify
;
1071 revs
.diffopt
.format_callback_data
= &paths_of_changed_objects
;
1072 revs
.diffopt
.flags
.recursive
= 1;
1073 while ((commit
= get_revision(&revs
))) {
1074 if (has_unshown_parent(commit
)) {
1075 add_object_array(&commit
->object
, NULL
, &commits
);
1078 handle_commit(commit
, &revs
, &paths_of_changed_objects
);
1079 handle_tail(&commits
, &revs
, &paths_of_changed_objects
);
1083 handle_tags_and_duplicates();
1086 if (export_filename
&& lastimportid
!= last_idnum
)
1087 export_marks(export_filename
);
1089 if (use_done_feature
)
1092 refspec_clear(&refspecs
);