2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
19 #include "string-list.h"
21 #include "parse-options.h"
25 #include "commit-slab.h"
27 static const char *fast_export_usage
[] = {
28 N_("git fast-export [rev-list-opts]"),
33 static enum { ABORT
, VERBATIM
, WARN
, WARN_STRIP
, STRIP
} signed_tag_mode
= ABORT
;
34 static enum { ERROR
, DROP
, REWRITE
} tag_of_filtered_mode
= ERROR
;
35 static int fake_missing_tagger
;
36 static int use_done_feature
;
39 static struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
40 static struct refspec refspecs
= REFSPEC_INIT_FETCH
;
42 static struct revision_sources revision_sources
;
44 static int parse_opt_signed_tag_mode(const struct option
*opt
,
45 const char *arg
, int unset
)
47 if (unset
|| !strcmp(arg
, "abort"))
48 signed_tag_mode
= ABORT
;
49 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
50 signed_tag_mode
= VERBATIM
;
51 else if (!strcmp(arg
, "warn"))
52 signed_tag_mode
= WARN
;
53 else if (!strcmp(arg
, "warn-strip"))
54 signed_tag_mode
= WARN_STRIP
;
55 else if (!strcmp(arg
, "strip"))
56 signed_tag_mode
= STRIP
;
58 return error("Unknown signed-tags mode: %s", arg
);
62 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
63 const char *arg
, int unset
)
65 if (unset
|| !strcmp(arg
, "abort"))
66 tag_of_filtered_mode
= ERROR
;
67 else if (!strcmp(arg
, "drop"))
68 tag_of_filtered_mode
= DROP
;
69 else if (!strcmp(arg
, "rewrite"))
70 tag_of_filtered_mode
= REWRITE
;
72 return error("Unknown tag-of-filtered mode: %s", arg
);
76 static struct decoration idnums
;
77 static uint32_t last_idnum
;
79 static int has_unshown_parent(struct commit
*commit
)
81 struct commit_list
*parent
;
83 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
84 if (!(parent
->item
->object
.flags
& SHOWN
) &&
85 !(parent
->item
->object
.flags
& UNINTERESTING
))
90 struct anonymized_entry
{
91 struct hashmap_entry hash
;
98 static int anonymized_entry_cmp(const void *unused_cmp_data
,
99 const void *va
, const void *vb
,
100 const void *unused_keydata
)
102 const struct anonymized_entry
*a
= va
, *b
= vb
;
103 return a
->orig_len
!= b
->orig_len
||
104 memcmp(a
->orig
, b
->orig
, a
->orig_len
);
108 * Basically keep a cache of X->Y so that we can repeatedly replace
109 * the same anonymized string with another. The actual generation
110 * is farmed out to the generate function.
112 static const void *anonymize_mem(struct hashmap
*map
,
113 void *(*generate
)(const void *, size_t *),
114 const void *orig
, size_t *len
)
116 struct anonymized_entry key
, *ret
;
119 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
121 hashmap_entry_init(&key
, memhash(orig
, *len
));
124 ret
= hashmap_get(map
, &key
, NULL
);
127 ret
= xmalloc(sizeof(*ret
));
128 hashmap_entry_init(&ret
->hash
, key
.hash
.hash
);
129 ret
->orig
= xstrdup(orig
);
130 ret
->orig_len
= *len
;
131 ret
->anon
= generate(orig
, len
);
132 ret
->anon_len
= *len
;
133 hashmap_put(map
, ret
);
136 *len
= ret
->anon_len
;
141 * We anonymize each component of a path individually,
142 * so that paths a/b and a/c will share a common root.
143 * The paths are cached via anonymize_mem so that repeated
144 * lookups for "a" will yield the same value.
146 static void anonymize_path(struct strbuf
*out
, const char *path
,
148 void *(*generate
)(const void *, size_t *))
151 const char *end_of_component
= strchrnul(path
, '/');
152 size_t len
= end_of_component
- path
;
153 const char *c
= anonymize_mem(map
, generate
, path
, &len
);
154 strbuf_add(out
, c
, len
);
155 path
= end_of_component
;
157 strbuf_addch(out
, *path
++);
161 static inline void *mark_to_ptr(uint32_t mark
)
163 return (void *)(uintptr_t)mark
;
166 static inline uint32_t ptr_to_mark(void * mark
)
168 return (uint32_t)(uintptr_t)mark
;
171 static inline void mark_object(struct object
*object
, uint32_t mark
)
173 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
176 static inline void mark_next_object(struct object
*object
)
178 mark_object(object
, ++last_idnum
);
181 static int get_object_mark(struct object
*object
)
183 void *decoration
= lookup_decoration(&idnums
, object
);
186 return ptr_to_mark(decoration
);
189 static void show_progress(void)
191 static int counter
= 0;
194 if ((++counter
% progress
) == 0)
195 printf("progress %d objects\n", counter
);
199 * Ideally we would want some transformation of the blob data here
200 * that is unreversible, but would still be the same size and have
201 * the same data relationship to other blobs (so that we get the same
202 * delta and packing behavior as the original). But the first and last
203 * requirements there are probably mutually exclusive, so let's take
204 * the easy way out for now, and just generate arbitrary content.
206 * There's no need to cache this result with anonymize_mem, since
207 * we already handle blob content caching with marks.
209 static char *anonymize_blob(unsigned long *size
)
212 struct strbuf out
= STRBUF_INIT
;
213 strbuf_addf(&out
, "anonymous blob %d", counter
++);
215 return strbuf_detach(&out
, NULL
);
218 static void export_blob(const struct object_id
*oid
)
221 enum object_type type
;
223 struct object
*object
;
229 if (is_null_oid(oid
))
232 object
= lookup_object(oid
->hash
);
233 if (object
&& object
->flags
& SHOWN
)
237 buf
= anonymize_blob(&size
);
238 object
= (struct object
*)lookup_blob(oid
);
241 buf
= read_object_file(oid
, &type
, &size
);
243 die ("Could not read blob %s", oid_to_hex(oid
));
244 if (check_object_signature(oid
, buf
, size
, type_name(type
)) < 0)
245 die("sha1 mismatch in blob %s", oid_to_hex(oid
));
246 object
= parse_object_buffer(oid
, type
, size
, buf
, &eaten
);
250 die("Could not read blob %s", oid_to_hex(oid
));
252 mark_next_object(object
);
254 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
255 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
256 die_errno ("Could not write blob '%s'", oid_to_hex(oid
));
261 object
->flags
|= SHOWN
;
266 static int depth_first(const void *a_
, const void *b_
)
268 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
269 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
270 const char *name_a
, *name_b
;
271 int len_a
, len_b
, len
;
274 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
275 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
277 len_a
= strlen(name_a
);
278 len_b
= strlen(name_b
);
279 len
= (len_a
< len_b
) ? len_a
: len_b
;
281 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
282 cmp
= memcmp(name_a
, name_b
, len
);
289 * Move 'R'ename entries last so that all references of the file
290 * appear in the output before it is renamed (e.g., when a file
291 * was copied and renamed in the same commit).
293 return (a
->status
== 'R') - (b
->status
== 'R');
296 static void print_path_1(const char *path
)
298 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
300 quote_c_style(path
, NULL
, stdout
, 0);
301 else if (strchr(path
, ' '))
302 printf("\"%s\"", path
);
307 static void *anonymize_path_component(const void *path
, size_t *len
)
310 struct strbuf out
= STRBUF_INIT
;
311 strbuf_addf(&out
, "path%d", counter
++);
312 return strbuf_detach(&out
, len
);
315 static void print_path(const char *path
)
320 static struct hashmap paths
;
321 static struct strbuf anon
= STRBUF_INIT
;
323 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
324 print_path_1(anon
.buf
);
329 static void *generate_fake_oid(const void *old
, size_t *len
)
331 static uint32_t counter
= 1; /* avoid null sha1 */
332 unsigned char *out
= xcalloc(GIT_SHA1_RAWSZ
, 1);
333 put_be32(out
+ GIT_SHA1_RAWSZ
- 4, counter
++);
337 static const unsigned char *anonymize_sha1(const struct object_id
*oid
)
339 static struct hashmap sha1s
;
340 size_t len
= GIT_SHA1_RAWSZ
;
341 return anonymize_mem(&sha1s
, generate_fake_oid
, oid
, &len
);
344 static void show_filemodify(struct diff_queue_struct
*q
,
345 struct diff_options
*options
, void *data
)
348 struct string_list
*changed
= data
;
351 * Handle files below a directory first, in case they are all deleted
352 * and the directory changes to a file or symlink.
354 QSORT(q
->queue
, q
->nr
, depth_first
);
356 for (i
= 0; i
< q
->nr
; i
++) {
357 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
358 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
360 switch (q
->queue
[i
]->status
) {
361 case DIFF_STATUS_DELETED
:
363 print_path(spec
->path
);
364 string_list_insert(changed
, spec
->path
);
368 case DIFF_STATUS_COPIED
:
369 case DIFF_STATUS_RENAMED
:
371 * If a change in the file corresponding to ospec->path
372 * has been observed, we cannot trust its contents
373 * because the diff is calculated based on the prior
374 * contents, not the current contents. So, declare a
375 * copy or rename only if there was no change observed.
377 if (!string_list_has_string(changed
, ospec
->path
)) {
378 printf("%c ", q
->queue
[i
]->status
);
379 print_path(ospec
->path
);
381 print_path(spec
->path
);
382 string_list_insert(changed
, spec
->path
);
385 if (!oidcmp(&ospec
->oid
, &spec
->oid
) &&
386 ospec
->mode
== spec
->mode
)
391 case DIFF_STATUS_TYPE_CHANGED
:
392 case DIFF_STATUS_MODIFIED
:
393 case DIFF_STATUS_ADDED
:
395 * Links refer to objects in another repositories;
396 * output the SHA-1 verbatim.
398 if (no_data
|| S_ISGITLINK(spec
->mode
))
399 printf("M %06o %s ", spec
->mode
,
400 sha1_to_hex(anonymize
?
401 anonymize_sha1(&spec
->oid
) :
404 struct object
*object
= lookup_object(spec
->oid
.hash
);
405 printf("M %06o :%d ", spec
->mode
,
406 get_object_mark(object
));
408 print_path(spec
->path
);
409 string_list_insert(changed
, spec
->path
);
414 die("Unexpected comparison status '%c' for %s, %s",
416 ospec
->path
? ospec
->path
: "none",
417 spec
->path
? spec
->path
: "none");
422 static const char *find_encoding(const char *begin
, const char *end
)
424 const char *needle
= "\nencoding ";
427 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
428 needle
, strlen(needle
));
430 return git_commit_encoding
;
431 bol
+= strlen(needle
);
432 eol
= strchrnul(bol
, '\n');
437 static void *anonymize_ref_component(const void *old
, size_t *len
)
440 struct strbuf out
= STRBUF_INIT
;
441 strbuf_addf(&out
, "ref%d", counter
++);
442 return strbuf_detach(&out
, len
);
445 static const char *anonymize_refname(const char *refname
)
448 * If any of these prefixes is found, we will leave it intact
449 * so that tags remain tags and so forth.
451 static const char *prefixes
[] = {
457 static struct hashmap refs
;
458 static struct strbuf anon
= STRBUF_INIT
;
462 * We also leave "master" as a special case, since it does not reveal
463 * anything interesting.
465 if (!strcmp(refname
, "refs/heads/master"))
469 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
470 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
471 strbuf_addstr(&anon
, prefixes
[i
]);
476 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
481 * We do not even bother to cache commit messages, as they are unlikely
482 * to be repeated verbatim, and it is not that interesting when they are.
484 static char *anonymize_commit_message(const char *old
)
487 return xstrfmt("subject %d\n\nbody\n", counter
++);
490 static struct hashmap idents
;
491 static void *anonymize_ident(const void *old
, size_t *len
)
494 struct strbuf out
= STRBUF_INIT
;
495 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
497 return strbuf_detach(&out
, len
);
501 * Our strategy here is to anonymize the names and email addresses,
502 * but keep timestamps intact, as they influence things like traversal
503 * order (and by themselves should not be too revealing).
505 static void anonymize_ident_line(const char **beg
, const char **end
)
507 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
508 static unsigned which_buffer
;
511 struct ident_split split
;
512 const char *end_of_header
;
514 out
= &buffers
[which_buffer
++];
515 which_buffer
%= ARRAY_SIZE(buffers
);
518 /* skip "committer", "author", "tagger", etc */
519 end_of_header
= strchr(*beg
, ' ');
521 BUG("malformed line fed to anonymize_ident_line: %.*s",
522 (int)(*end
- *beg
), *beg
);
524 strbuf_add(out
, *beg
, end_of_header
- *beg
);
526 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
531 len
= split
.mail_end
- split
.name_begin
;
532 ident
= anonymize_mem(&idents
, anonymize_ident
,
533 split
.name_begin
, &len
);
534 strbuf_add(out
, ident
, len
);
535 strbuf_addch(out
, ' ');
536 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
538 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
542 *end
= out
->buf
+ out
->len
;
545 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
,
546 struct string_list
*paths_of_changed_objects
)
548 int saved_output_format
= rev
->diffopt
.output_format
;
549 const char *commit_buffer
;
550 const char *author
, *author_end
, *committer
, *committer_end
;
551 const char *encoding
, *message
;
552 char *reencoded
= NULL
;
553 struct commit_list
*p
;
557 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
559 parse_commit_or_die(commit
);
560 commit_buffer
= get_commit_buffer(commit
, NULL
);
561 author
= strstr(commit_buffer
, "\nauthor ");
563 die ("Could not find author in commit %s",
564 oid_to_hex(&commit
->object
.oid
));
566 author_end
= strchrnul(author
, '\n');
567 committer
= strstr(author_end
, "\ncommitter ");
569 die ("Could not find committer in commit %s",
570 oid_to_hex(&commit
->object
.oid
));
572 committer_end
= strchrnul(committer
, '\n');
573 message
= strstr(committer_end
, "\n\n");
574 encoding
= find_encoding(committer_end
, message
);
578 if (commit
->parents
&&
579 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
581 parse_commit_or_die(commit
->parents
->item
);
582 diff_tree_oid(get_commit_tree_oid(commit
->parents
->item
),
583 get_commit_tree_oid(commit
), "", &rev
->diffopt
);
586 diff_root_tree_oid(get_commit_tree_oid(commit
),
589 /* Export the referenced blobs, and remember the marks. */
590 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
591 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
592 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
594 refname
= *revision_sources_at(&revision_sources
, commit
);
596 refname
= anonymize_refname(refname
);
597 anonymize_ident_line(&committer
, &committer_end
);
598 anonymize_ident_line(&author
, &author_end
);
601 mark_next_object(&commit
->object
);
603 reencoded
= anonymize_commit_message(message
);
604 else if (!is_encoding_utf8(encoding
))
605 reencoded
= reencode_string(message
, "UTF-8", encoding
);
606 if (!commit
->parents
)
607 printf("reset %s\n", refname
);
608 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
610 (int)(author_end
- author
), author
,
611 (int)(committer_end
- committer
), committer
,
613 ? strlen(reencoded
) : message
614 ? strlen(message
) : 0),
615 reencoded
? reencoded
: message
? message
: "");
617 unuse_commit_buffer(commit
, commit_buffer
);
619 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
620 int mark
= get_object_mark(&p
->item
->object
);
624 printf("from :%d\n", mark
);
626 printf("merge :%d\n", mark
);
631 printf("deleteall\n");
632 log_tree_diff_flush(rev
);
633 string_list_clear(paths_of_changed_objects
, 0);
634 rev
->diffopt
.output_format
= saved_output_format
;
641 static void *anonymize_tag(const void *old
, size_t *len
)
644 struct strbuf out
= STRBUF_INIT
;
645 strbuf_addf(&out
, "tag message %d", counter
++);
646 return strbuf_detach(&out
, len
);
649 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
,
650 struct string_list
*paths_of_changed_objects
)
652 struct commit
*commit
;
653 while (commits
->nr
) {
654 commit
= (struct commit
*)object_array_pop(commits
);
655 if (has_unshown_parent(commit
)) {
656 /* Queue again, to be handled later */
657 add_object_array(&commit
->object
, NULL
, commits
);
660 handle_commit(commit
, revs
, paths_of_changed_objects
);
664 static void handle_tag(const char *name
, struct tag
*tag
)
667 enum object_type type
;
669 const char *tagger
, *tagger_end
, *message
;
670 size_t message_size
= 0;
671 struct object
*tagged
;
675 /* Trees have no identifier in fast-export output, thus we have no way
676 * to output tags of trees, tags of tags of trees, etc. Simply omit
679 tagged
= tag
->tagged
;
680 while (tagged
->type
== OBJ_TAG
) {
681 tagged
= ((struct tag
*)tagged
)->tagged
;
683 if (tagged
->type
== OBJ_TREE
) {
684 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
685 oid_to_hex(&tag
->object
.oid
));
689 buf
= read_object_file(&tag
->object
.oid
, &type
, &size
);
691 die ("Could not read tag %s", oid_to_hex(&tag
->object
.oid
));
692 message
= memmem(buf
, size
, "\n\n", 2);
695 message_size
= strlen(message
);
697 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
699 if (fake_missing_tagger
)
700 tagger
= "tagger Unspecified Tagger "
701 "<unspecified-tagger> 0 +0000";
704 tagger_end
= tagger
+ strlen(tagger
);
707 tagger_end
= strchrnul(tagger
, '\n');
709 anonymize_ident_line(&tagger
, &tagger_end
);
713 name
= anonymize_refname(name
);
715 static struct hashmap tags
;
716 message
= anonymize_mem(&tags
, anonymize_tag
,
717 message
, &message_size
);
721 /* handle signed tags */
723 const char *signature
= strstr(message
,
724 "\n-----BEGIN PGP SIGNATURE-----\n");
726 switch(signed_tag_mode
) {
728 die ("Encountered signed tag %s; use "
729 "--signed-tags=<mode> to handle it.",
730 oid_to_hex(&tag
->object
.oid
));
732 warning ("Exporting signed tag %s",
733 oid_to_hex(&tag
->object
.oid
));
738 warning ("Stripping signature from tag %s",
739 oid_to_hex(&tag
->object
.oid
));
742 message_size
= signature
+ 1 - message
;
747 /* handle tag->tagged having been filtered out due to paths specified */
748 tagged
= tag
->tagged
;
749 tagged_mark
= get_object_mark(tagged
);
751 switch(tag_of_filtered_mode
) {
753 die ("Tag %s tags unexported object; use "
754 "--tag-of-filtered-object=<mode> to handle it.",
755 oid_to_hex(&tag
->object
.oid
));
757 /* Ignore this tag altogether */
761 if (tagged
->type
!= OBJ_COMMIT
) {
762 die ("Tag %s tags unexported %s!",
763 oid_to_hex(&tag
->object
.oid
),
764 type_name(tagged
->type
));
766 p
= (struct commit
*)tagged
;
768 if (p
->parents
&& p
->parents
->next
)
770 if (p
->object
.flags
& UNINTERESTING
)
772 if (!(p
->object
.flags
& TREESAME
))
775 die ("Can't find replacement commit for tag %s\n",
776 oid_to_hex(&tag
->object
.oid
));
777 p
= p
->parents
->item
;
779 tagged_mark
= get_object_mark(&p
->object
);
783 if (starts_with(name
, "refs/tags/"))
785 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
787 (int)(tagger_end
- tagger
), tagger
,
788 tagger
== tagger_end
? "" : "\n",
789 (int)message_size
, (int)message_size
, message
? message
: "");
793 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
795 switch (e
->item
->type
) {
797 return (struct commit
*)e
->item
;
799 struct tag
*tag
= (struct tag
*)e
->item
;
801 /* handle nested tags */
802 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
803 parse_object(&tag
->object
.oid
);
804 string_list_append(&extra_refs
, full_name
)->util
= tag
;
805 tag
= (struct tag
*)tag
->tagged
;
808 die("Tag %s points nowhere?", e
->name
);
809 return (struct commit
*)tag
;
817 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
821 for (i
= 0; i
< info
->nr
; i
++) {
822 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
823 struct object_id oid
;
824 struct commit
*commit
;
827 if (e
->flags
& UNINTERESTING
)
830 if (dwim_ref(e
->name
, strlen(e
->name
), &oid
, &full_name
) != 1)
835 private = apply_refspecs(&refspecs
, full_name
);
842 commit
= get_commit(e
, full_name
);
844 warning("%s: Unexpected object of type %s, skipping.",
846 type_name(e
->item
->type
));
850 switch(commit
->object
.type
) {
854 export_blob(&commit
->object
.oid
);
856 default: /* OBJ_TAG (nested tags) is already handled */
857 warning("Tag points to object of unexpected type %s, skipping.",
858 type_name(commit
->object
.type
));
863 * This ref will not be updated through a commit, lets make
864 * sure it gets properly updated eventually.
866 if (*revision_sources_at(&revision_sources
, commit
) ||
867 commit
->object
.flags
& SHOWN
)
868 string_list_append(&extra_refs
, full_name
)->util
= commit
;
869 if (!*revision_sources_at(&revision_sources
, commit
))
870 *revision_sources_at(&revision_sources
, commit
) = full_name
;
874 static void handle_tags_and_duplicates(void)
876 struct commit
*commit
;
879 for (i
= extra_refs
.nr
- 1; i
>= 0; i
--) {
880 const char *name
= extra_refs
.items
[i
].string
;
881 struct object
*object
= extra_refs
.items
[i
].util
;
882 switch (object
->type
) {
884 handle_tag(name
, (struct tag
*)object
);
888 name
= anonymize_refname(name
);
889 /* create refs pointing to already seen commits */
890 commit
= (struct commit
*)object
;
891 printf("reset %s\nfrom :%d\n\n", name
,
892 get_object_mark(&commit
->object
));
899 static void export_marks(char *file
)
903 struct decoration_entry
*deco
= idnums
.entries
;
907 f
= fopen_for_writing(file
);
909 die_errno("Unable to open marks file %s for writing.", file
);
911 for (i
= 0; i
< idnums
.size
; i
++) {
912 if (deco
->base
&& deco
->base
->type
== 1) {
913 mark
= ptr_to_mark(deco
->decoration
);
914 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
915 oid_to_hex(&deco
->base
->oid
)) < 0) {
926 error("Unable to write marks file %s.", file
);
929 static void import_marks(char *input_file
)
932 FILE *f
= xfopen(input_file
, "r");
934 while (fgets(line
, sizeof(line
), f
)) {
936 char *line_end
, *mark_end
;
937 struct object_id oid
;
938 struct object
*object
;
939 struct commit
*commit
;
940 enum object_type type
;
942 line_end
= strchr(line
, '\n');
943 if (line
[0] != ':' || !line_end
)
944 die("corrupt mark line: %s", line
);
947 mark
= strtoumax(line
+ 1, &mark_end
, 10);
948 if (!mark
|| mark_end
== line
+ 1
949 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
950 die("corrupt mark line: %s", line
);
952 if (last_idnum
< mark
)
955 type
= oid_object_info(the_repository
, &oid
, NULL
);
957 die("object not found: %s", oid_to_hex(&oid
));
959 if (type
!= OBJ_COMMIT
)
963 commit
= lookup_commit(&oid
);
965 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
967 object
= &commit
->object
;
969 if (object
->flags
& SHOWN
)
970 error("Object %s already has a mark", oid_to_hex(&oid
));
972 mark_object(object
, mark
);
974 object
->flags
|= SHOWN
;
979 static void handle_deletes(void)
982 for (i
= 0; i
< refspecs
.nr
; i
++) {
983 struct refspec_item
*refspec
= &refspecs
.items
[i
];
987 printf("reset %s\nfrom %s\n\n",
988 refspec
->dst
, sha1_to_hex(null_sha1
));
992 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
994 struct rev_info revs
;
995 struct object_array commits
= OBJECT_ARRAY_INIT
;
996 struct commit
*commit
;
997 char *export_filename
= NULL
, *import_filename
= NULL
;
998 uint32_t lastimportid
;
999 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
1000 struct string_list paths_of_changed_objects
= STRING_LIST_INIT_DUP
;
1001 struct option options
[] = {
1002 OPT_INTEGER(0, "progress", &progress
,
1003 N_("show progress after <n> objects")),
1004 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
1005 N_("select handling of signed tags"),
1006 parse_opt_signed_tag_mode
),
1007 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
1008 N_("select handling of tags that tag filtered objects"),
1009 parse_opt_tag_of_filtered_mode
),
1010 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
1011 N_("Dump marks to this file")),
1012 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
1013 N_("Import marks from this file")),
1014 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
1015 N_("Fake a tagger when tags lack one")),
1016 OPT_BOOL(0, "full-tree", &full_tree
,
1017 N_("Output full tree for each commit")),
1018 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
1019 N_("Use the done feature to terminate the stream")),
1020 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
1021 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1022 N_("Apply refspec to exported refs")),
1023 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1028 usage_with_options (fast_export_usage
, options
);
1030 /* we handle encodings */
1031 git_config(git_default_config
, NULL
);
1033 init_revisions(&revs
, prefix
);
1034 init_revision_sources(&revision_sources
);
1035 revs
.topo_order
= 1;
1036 revs
.sources
= &revision_sources
;
1037 revs
.rewrite_parents
= 1;
1038 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1039 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
);
1040 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1042 usage_with_options (fast_export_usage
, options
);
1044 if (refspecs_list
.nr
) {
1047 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1048 refspec_append(&refspecs
, refspecs_list
.items
[i
].string
);
1050 string_list_clear(&refspecs_list
, 1);
1053 if (use_done_feature
)
1054 printf("feature done\n");
1056 if (import_filename
)
1057 import_marks(import_filename
);
1058 lastimportid
= last_idnum
;
1060 if (import_filename
&& revs
.prune_data
.nr
)
1063 get_tags_and_duplicates(&revs
.cmdline
);
1065 if (prepare_revision_walk(&revs
))
1066 die("revision walk setup failed");
1067 revs
.diffopt
.format_callback
= show_filemodify
;
1068 revs
.diffopt
.format_callback_data
= &paths_of_changed_objects
;
1069 revs
.diffopt
.flags
.recursive
= 1;
1070 while ((commit
= get_revision(&revs
))) {
1071 if (has_unshown_parent(commit
)) {
1072 add_object_array(&commit
->object
, NULL
, &commits
);
1075 handle_commit(commit
, &revs
, &paths_of_changed_objects
);
1076 handle_tail(&commits
, &revs
, &paths_of_changed_objects
);
1080 handle_tags_and_duplicates();
1083 if (export_filename
&& lastimportid
!= last_idnum
)
1084 export_marks(export_filename
);
1086 if (use_done_feature
)
1089 refspec_clear(&refspecs
);