2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
18 #include "string-list.h"
20 #include "parse-options.h"
25 static const char *fast_export_usage
[] = {
26 N_("git fast-export [rev-list-opts]"),
31 static enum { ABORT
, VERBATIM
, WARN
, WARN_STRIP
, STRIP
} signed_tag_mode
= ABORT
;
32 static enum { ERROR
, DROP
, REWRITE
} tag_of_filtered_mode
= ERROR
;
33 static int fake_missing_tagger
;
34 static int use_done_feature
;
37 static struct string_list extra_refs
= STRING_LIST_INIT_NODUP
;
38 static struct refspec
*refspecs
;
39 static int refspecs_nr
;
42 static int parse_opt_signed_tag_mode(const struct option
*opt
,
43 const char *arg
, int unset
)
45 if (unset
|| !strcmp(arg
, "abort"))
46 signed_tag_mode
= ABORT
;
47 else if (!strcmp(arg
, "verbatim") || !strcmp(arg
, "ignore"))
48 signed_tag_mode
= VERBATIM
;
49 else if (!strcmp(arg
, "warn"))
50 signed_tag_mode
= WARN
;
51 else if (!strcmp(arg
, "warn-strip"))
52 signed_tag_mode
= WARN_STRIP
;
53 else if (!strcmp(arg
, "strip"))
54 signed_tag_mode
= STRIP
;
56 return error("Unknown signed-tags mode: %s", arg
);
60 static int parse_opt_tag_of_filtered_mode(const struct option
*opt
,
61 const char *arg
, int unset
)
63 if (unset
|| !strcmp(arg
, "abort"))
64 tag_of_filtered_mode
= ERROR
;
65 else if (!strcmp(arg
, "drop"))
66 tag_of_filtered_mode
= DROP
;
67 else if (!strcmp(arg
, "rewrite"))
68 tag_of_filtered_mode
= REWRITE
;
70 return error("Unknown tag-of-filtered mode: %s", arg
);
74 static struct decoration idnums
;
75 static uint32_t last_idnum
;
77 static int has_unshown_parent(struct commit
*commit
)
79 struct commit_list
*parent
;
81 for (parent
= commit
->parents
; parent
; parent
= parent
->next
)
82 if (!(parent
->item
->object
.flags
& SHOWN
) &&
83 !(parent
->item
->object
.flags
& UNINTERESTING
))
88 struct anonymized_entry
{
89 struct hashmap_entry hash
;
96 static int anonymized_entry_cmp(const void *unused_cmp_data
,
97 const void *va
, const void *vb
,
98 const void *unused_keydata
)
100 const struct anonymized_entry
*a
= va
, *b
= vb
;
101 return a
->orig_len
!= b
->orig_len
||
102 memcmp(a
->orig
, b
->orig
, a
->orig_len
);
106 * Basically keep a cache of X->Y so that we can repeatedly replace
107 * the same anonymized string with another. The actual generation
108 * is farmed out to the generate function.
110 static const void *anonymize_mem(struct hashmap
*map
,
111 void *(*generate
)(const void *, size_t *),
112 const void *orig
, size_t *len
)
114 struct anonymized_entry key
, *ret
;
117 hashmap_init(map
, anonymized_entry_cmp
, NULL
, 0);
119 hashmap_entry_init(&key
, memhash(orig
, *len
));
122 ret
= hashmap_get(map
, &key
, NULL
);
125 ret
= xmalloc(sizeof(*ret
));
126 hashmap_entry_init(&ret
->hash
, key
.hash
.hash
);
127 ret
->orig
= xstrdup(orig
);
128 ret
->orig_len
= *len
;
129 ret
->anon
= generate(orig
, len
);
130 ret
->anon_len
= *len
;
131 hashmap_put(map
, ret
);
134 *len
= ret
->anon_len
;
139 * We anonymize each component of a path individually,
140 * so that paths a/b and a/c will share a common root.
141 * The paths are cached via anonymize_mem so that repeated
142 * lookups for "a" will yield the same value.
144 static void anonymize_path(struct strbuf
*out
, const char *path
,
146 void *(*generate
)(const void *, size_t *))
149 const char *end_of_component
= strchrnul(path
, '/');
150 size_t len
= end_of_component
- path
;
151 const char *c
= anonymize_mem(map
, generate
, path
, &len
);
152 strbuf_add(out
, c
, len
);
153 path
= end_of_component
;
155 strbuf_addch(out
, *path
++);
159 /* Since intptr_t is C99, we do not use it here */
160 static inline uint32_t *mark_to_ptr(uint32_t mark
)
162 return ((uint32_t *)NULL
) + mark
;
165 static inline uint32_t ptr_to_mark(void * mark
)
167 return (uint32_t *)mark
- (uint32_t *)NULL
;
170 static inline void mark_object(struct object
*object
, uint32_t mark
)
172 add_decoration(&idnums
, object
, mark_to_ptr(mark
));
175 static inline void mark_next_object(struct object
*object
)
177 mark_object(object
, ++last_idnum
);
180 static int get_object_mark(struct object
*object
)
182 void *decoration
= lookup_decoration(&idnums
, object
);
185 return ptr_to_mark(decoration
);
188 static void show_progress(void)
190 static int counter
= 0;
193 if ((++counter
% progress
) == 0)
194 printf("progress %d objects\n", counter
);
198 * Ideally we would want some transformation of the blob data here
199 * that is unreversible, but would still be the same size and have
200 * the same data relationship to other blobs (so that we get the same
201 * delta and packing behavior as the original). But the first and last
202 * requirements there are probably mutually exclusive, so let's take
203 * the easy way out for now, and just generate arbitrary content.
205 * There's no need to cache this result with anonymize_mem, since
206 * we already handle blob content caching with marks.
208 static char *anonymize_blob(unsigned long *size
)
211 struct strbuf out
= STRBUF_INIT
;
212 strbuf_addf(&out
, "anonymous blob %d", counter
++);
214 return strbuf_detach(&out
, NULL
);
217 static void export_blob(const struct object_id
*oid
)
220 enum object_type type
;
222 struct object
*object
;
228 if (is_null_oid(oid
))
231 object
= lookup_object(oid
->hash
);
232 if (object
&& object
->flags
& SHOWN
)
236 buf
= anonymize_blob(&size
);
237 object
= (struct object
*)lookup_blob(oid
);
240 buf
= read_sha1_file(oid
->hash
, &type
, &size
);
242 die ("Could not read blob %s", oid_to_hex(oid
));
243 if (check_sha1_signature(oid
->hash
, buf
, size
, typename(type
)) < 0)
244 die("sha1 mismatch in blob %s", oid_to_hex(oid
));
245 object
= parse_object_buffer(oid
, type
, size
, buf
, &eaten
);
249 die("Could not read blob %s", oid_to_hex(oid
));
251 mark_next_object(object
);
253 printf("blob\nmark :%"PRIu32
"\ndata %lu\n", last_idnum
, size
);
254 if (size
&& fwrite(buf
, size
, 1, stdout
) != 1)
255 die_errno ("Could not write blob '%s'", oid_to_hex(oid
));
260 object
->flags
|= SHOWN
;
265 static int depth_first(const void *a_
, const void *b_
)
267 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
268 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
269 const char *name_a
, *name_b
;
270 int len_a
, len_b
, len
;
273 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
274 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
276 len_a
= strlen(name_a
);
277 len_b
= strlen(name_b
);
278 len
= (len_a
< len_b
) ? len_a
: len_b
;
280 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
281 cmp
= memcmp(name_a
, name_b
, len
);
288 * Move 'R'ename entries last so that all references of the file
289 * appear in the output before it is renamed (e.g., when a file
290 * was copied and renamed in the same commit).
292 return (a
->status
== 'R') - (b
->status
== 'R');
295 static void print_path_1(const char *path
)
297 int need_quote
= quote_c_style(path
, NULL
, NULL
, 0);
299 quote_c_style(path
, NULL
, stdout
, 0);
300 else if (strchr(path
, ' '))
301 printf("\"%s\"", path
);
306 static void *anonymize_path_component(const void *path
, size_t *len
)
309 struct strbuf out
= STRBUF_INIT
;
310 strbuf_addf(&out
, "path%d", counter
++);
311 return strbuf_detach(&out
, len
);
314 static void print_path(const char *path
)
319 static struct hashmap paths
;
320 static struct strbuf anon
= STRBUF_INIT
;
322 anonymize_path(&anon
, path
, &paths
, anonymize_path_component
);
323 print_path_1(anon
.buf
);
328 static void *generate_fake_oid(const void *old
, size_t *len
)
330 static uint32_t counter
= 1; /* avoid null sha1 */
331 unsigned char *out
= xcalloc(GIT_SHA1_RAWSZ
, 1);
332 put_be32(out
+ GIT_SHA1_RAWSZ
- 4, counter
++);
336 static const unsigned char *anonymize_sha1(const struct object_id
*oid
)
338 static struct hashmap sha1s
;
339 size_t len
= GIT_SHA1_RAWSZ
;
340 return anonymize_mem(&sha1s
, generate_fake_oid
, oid
, &len
);
343 static void show_filemodify(struct diff_queue_struct
*q
,
344 struct diff_options
*options
, void *data
)
349 * Handle files below a directory first, in case they are all deleted
350 * and the directory changes to a file or symlink.
352 QSORT(q
->queue
, q
->nr
, depth_first
);
354 for (i
= 0; i
< q
->nr
; i
++) {
355 struct diff_filespec
*ospec
= q
->queue
[i
]->one
;
356 struct diff_filespec
*spec
= q
->queue
[i
]->two
;
358 switch (q
->queue
[i
]->status
) {
359 case DIFF_STATUS_DELETED
:
361 print_path(spec
->path
);
365 case DIFF_STATUS_COPIED
:
366 case DIFF_STATUS_RENAMED
:
367 printf("%c ", q
->queue
[i
]->status
);
368 print_path(ospec
->path
);
370 print_path(spec
->path
);
373 if (!oidcmp(&ospec
->oid
, &spec
->oid
) &&
374 ospec
->mode
== spec
->mode
)
378 case DIFF_STATUS_TYPE_CHANGED
:
379 case DIFF_STATUS_MODIFIED
:
380 case DIFF_STATUS_ADDED
:
382 * Links refer to objects in another repositories;
383 * output the SHA-1 verbatim.
385 if (no_data
|| S_ISGITLINK(spec
->mode
))
386 printf("M %06o %s ", spec
->mode
,
387 sha1_to_hex(anonymize
?
388 anonymize_sha1(&spec
->oid
) :
391 struct object
*object
= lookup_object(spec
->oid
.hash
);
392 printf("M %06o :%d ", spec
->mode
,
393 get_object_mark(object
));
395 print_path(spec
->path
);
400 die("Unexpected comparison status '%c' for %s, %s",
402 ospec
->path
? ospec
->path
: "none",
403 spec
->path
? spec
->path
: "none");
408 static const char *find_encoding(const char *begin
, const char *end
)
410 const char *needle
= "\nencoding ";
413 bol
= memmem(begin
, end
? end
- begin
: strlen(begin
),
414 needle
, strlen(needle
));
416 return git_commit_encoding
;
417 bol
+= strlen(needle
);
418 eol
= strchrnul(bol
, '\n');
423 static void *anonymize_ref_component(const void *old
, size_t *len
)
426 struct strbuf out
= STRBUF_INIT
;
427 strbuf_addf(&out
, "ref%d", counter
++);
428 return strbuf_detach(&out
, len
);
431 static const char *anonymize_refname(const char *refname
)
434 * If any of these prefixes is found, we will leave it intact
435 * so that tags remain tags and so forth.
437 static const char *prefixes
[] = {
443 static struct hashmap refs
;
444 static struct strbuf anon
= STRBUF_INIT
;
448 * We also leave "master" as a special case, since it does not reveal
449 * anything interesting.
451 if (!strcmp(refname
, "refs/heads/master"))
455 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
456 if (skip_prefix(refname
, prefixes
[i
], &refname
)) {
457 strbuf_addstr(&anon
, prefixes
[i
]);
462 anonymize_path(&anon
, refname
, &refs
, anonymize_ref_component
);
467 * We do not even bother to cache commit messages, as they are unlikely
468 * to be repeated verbatim, and it is not that interesting when they are.
470 static char *anonymize_commit_message(const char *old
)
473 return xstrfmt("subject %d\n\nbody\n", counter
++);
476 static struct hashmap idents
;
477 static void *anonymize_ident(const void *old
, size_t *len
)
480 struct strbuf out
= STRBUF_INIT
;
481 strbuf_addf(&out
, "User %d <user%d@example.com>", counter
, counter
);
483 return strbuf_detach(&out
, len
);
487 * Our strategy here is to anonymize the names and email addresses,
488 * but keep timestamps intact, as they influence things like traversal
489 * order (and by themselves should not be too revealing).
491 static void anonymize_ident_line(const char **beg
, const char **end
)
493 static struct strbuf buffers
[] = { STRBUF_INIT
, STRBUF_INIT
};
494 static unsigned which_buffer
;
497 struct ident_split split
;
498 const char *end_of_header
;
500 out
= &buffers
[which_buffer
++];
501 which_buffer
%= ARRAY_SIZE(buffers
);
504 /* skip "committer", "author", "tagger", etc */
505 end_of_header
= strchr(*beg
, ' ');
507 die("BUG: malformed line fed to anonymize_ident_line: %.*s",
508 (int)(*end
- *beg
), *beg
);
510 strbuf_add(out
, *beg
, end_of_header
- *beg
);
512 if (!split_ident_line(&split
, end_of_header
, *end
- end_of_header
) &&
517 len
= split
.mail_end
- split
.name_begin
;
518 ident
= anonymize_mem(&idents
, anonymize_ident
,
519 split
.name_begin
, &len
);
520 strbuf_add(out
, ident
, len
);
521 strbuf_addch(out
, ' ');
522 strbuf_add(out
, split
.date_begin
, split
.tz_end
- split
.date_begin
);
524 strbuf_addstr(out
, "Malformed Ident <malformed@example.com> 0 -0000");
528 *end
= out
->buf
+ out
->len
;
531 static void handle_commit(struct commit
*commit
, struct rev_info
*rev
)
533 int saved_output_format
= rev
->diffopt
.output_format
;
534 const char *commit_buffer
;
535 const char *author
, *author_end
, *committer
, *committer_end
;
536 const char *encoding
, *message
;
537 char *reencoded
= NULL
;
538 struct commit_list
*p
;
542 rev
->diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
544 parse_commit_or_die(commit
);
545 commit_buffer
= get_commit_buffer(commit
, NULL
);
546 author
= strstr(commit_buffer
, "\nauthor ");
548 die ("Could not find author in commit %s",
549 oid_to_hex(&commit
->object
.oid
));
551 author_end
= strchrnul(author
, '\n');
552 committer
= strstr(author_end
, "\ncommitter ");
554 die ("Could not find committer in commit %s",
555 oid_to_hex(&commit
->object
.oid
));
557 committer_end
= strchrnul(committer
, '\n');
558 message
= strstr(committer_end
, "\n\n");
559 encoding
= find_encoding(committer_end
, message
);
563 if (commit
->parents
&&
564 get_object_mark(&commit
->parents
->item
->object
) != 0 &&
566 parse_commit_or_die(commit
->parents
->item
);
567 diff_tree_oid(&commit
->parents
->item
->tree
->object
.oid
,
568 &commit
->tree
->object
.oid
, "", &rev
->diffopt
);
571 diff_root_tree_oid(&commit
->tree
->object
.oid
,
574 /* Export the referenced blobs, and remember the marks. */
575 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
576 if (!S_ISGITLINK(diff_queued_diff
.queue
[i
]->two
->mode
))
577 export_blob(&diff_queued_diff
.queue
[i
]->two
->oid
);
579 refname
= commit
->util
;
581 refname
= anonymize_refname(refname
);
582 anonymize_ident_line(&committer
, &committer_end
);
583 anonymize_ident_line(&author
, &author_end
);
586 mark_next_object(&commit
->object
);
588 reencoded
= anonymize_commit_message(message
);
589 else if (!is_encoding_utf8(encoding
))
590 reencoded
= reencode_string(message
, "UTF-8", encoding
);
591 if (!commit
->parents
)
592 printf("reset %s\n", refname
);
593 printf("commit %s\nmark :%"PRIu32
"\n%.*s\n%.*s\ndata %u\n%s",
595 (int)(author_end
- author
), author
,
596 (int)(committer_end
- committer
), committer
,
598 ? strlen(reencoded
) : message
599 ? strlen(message
) : 0),
600 reencoded
? reencoded
: message
? message
: "");
602 unuse_commit_buffer(commit
, commit_buffer
);
604 for (i
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
605 int mark
= get_object_mark(&p
->item
->object
);
609 printf("from :%d\n", mark
);
611 printf("merge :%d\n", mark
);
616 printf("deleteall\n");
617 log_tree_diff_flush(rev
);
618 rev
->diffopt
.output_format
= saved_output_format
;
625 static void *anonymize_tag(const void *old
, size_t *len
)
628 struct strbuf out
= STRBUF_INIT
;
629 strbuf_addf(&out
, "tag message %d", counter
++);
630 return strbuf_detach(&out
, len
);
633 static void handle_tail(struct object_array
*commits
, struct rev_info
*revs
)
635 struct commit
*commit
;
636 while (commits
->nr
) {
637 commit
= (struct commit
*)commits
->objects
[commits
->nr
- 1].item
;
638 if (has_unshown_parent(commit
))
640 handle_commit(commit
, revs
);
645 static void handle_tag(const char *name
, struct tag
*tag
)
648 enum object_type type
;
650 const char *tagger
, *tagger_end
, *message
;
651 size_t message_size
= 0;
652 struct object
*tagged
;
656 /* Trees have no identifier in fast-export output, thus we have no way
657 * to output tags of trees, tags of tags of trees, etc. Simply omit
660 tagged
= tag
->tagged
;
661 while (tagged
->type
== OBJ_TAG
) {
662 tagged
= ((struct tag
*)tagged
)->tagged
;
664 if (tagged
->type
== OBJ_TREE
) {
665 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
666 oid_to_hex(&tag
->object
.oid
));
670 buf
= read_sha1_file(tag
->object
.oid
.hash
, &type
, &size
);
672 die ("Could not read tag %s", oid_to_hex(&tag
->object
.oid
));
673 message
= memmem(buf
, size
, "\n\n", 2);
676 message_size
= strlen(message
);
678 tagger
= memmem(buf
, message
? message
- buf
: size
, "\ntagger ", 8);
680 if (fake_missing_tagger
)
681 tagger
= "tagger Unspecified Tagger "
682 "<unspecified-tagger> 0 +0000";
685 tagger_end
= tagger
+ strlen(tagger
);
688 tagger_end
= strchrnul(tagger
, '\n');
690 anonymize_ident_line(&tagger
, &tagger_end
);
694 name
= anonymize_refname(name
);
696 static struct hashmap tags
;
697 message
= anonymize_mem(&tags
, anonymize_tag
,
698 message
, &message_size
);
702 /* handle signed tags */
704 const char *signature
= strstr(message
,
705 "\n-----BEGIN PGP SIGNATURE-----\n");
707 switch(signed_tag_mode
) {
709 die ("Encountered signed tag %s; use "
710 "--signed-tags=<mode> to handle it.",
711 oid_to_hex(&tag
->object
.oid
));
713 warning ("Exporting signed tag %s",
714 oid_to_hex(&tag
->object
.oid
));
719 warning ("Stripping signature from tag %s",
720 oid_to_hex(&tag
->object
.oid
));
723 message_size
= signature
+ 1 - message
;
728 /* handle tag->tagged having been filtered out due to paths specified */
729 tagged
= tag
->tagged
;
730 tagged_mark
= get_object_mark(tagged
);
732 switch(tag_of_filtered_mode
) {
734 die ("Tag %s tags unexported object; use "
735 "--tag-of-filtered-object=<mode> to handle it.",
736 oid_to_hex(&tag
->object
.oid
));
738 /* Ignore this tag altogether */
742 if (tagged
->type
!= OBJ_COMMIT
) {
743 die ("Tag %s tags unexported %s!",
744 oid_to_hex(&tag
->object
.oid
),
745 typename(tagged
->type
));
747 p
= (struct commit
*)tagged
;
749 if (p
->parents
&& p
->parents
->next
)
751 if (p
->object
.flags
& UNINTERESTING
)
753 if (!(p
->object
.flags
& TREESAME
))
756 die ("Can't find replacement commit for tag %s\n",
757 oid_to_hex(&tag
->object
.oid
));
758 p
= p
->parents
->item
;
760 tagged_mark
= get_object_mark(&p
->object
);
764 if (starts_with(name
, "refs/tags/"))
766 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
768 (int)(tagger_end
- tagger
), tagger
,
769 tagger
== tagger_end
? "" : "\n",
770 (int)message_size
, (int)message_size
, message
? message
: "");
774 static struct commit
*get_commit(struct rev_cmdline_entry
*e
, char *full_name
)
776 switch (e
->item
->type
) {
778 return (struct commit
*)e
->item
;
780 struct tag
*tag
= (struct tag
*)e
->item
;
782 /* handle nested tags */
783 while (tag
&& tag
->object
.type
== OBJ_TAG
) {
784 parse_object(&tag
->object
.oid
);
785 string_list_append(&extra_refs
, full_name
)->util
= tag
;
786 tag
= (struct tag
*)tag
->tagged
;
789 die("Tag %s points nowhere?", e
->name
);
790 return (struct commit
*)tag
;
798 static void get_tags_and_duplicates(struct rev_cmdline_info
*info
)
802 for (i
= 0; i
< info
->nr
; i
++) {
803 struct rev_cmdline_entry
*e
= info
->rev
+ i
;
804 struct object_id oid
;
805 struct commit
*commit
;
808 if (e
->flags
& UNINTERESTING
)
811 if (dwim_ref(e
->name
, strlen(e
->name
), oid
.hash
, &full_name
) != 1)
816 private = apply_refspecs(refspecs
, refspecs_nr
, full_name
);
823 commit
= get_commit(e
, full_name
);
825 warning("%s: Unexpected object of type %s, skipping.",
827 typename(e
->item
->type
));
831 switch(commit
->object
.type
) {
835 export_blob(&commit
->object
.oid
);
837 default: /* OBJ_TAG (nested tags) is already handled */
838 warning("Tag points to object of unexpected type %s, skipping.",
839 typename(commit
->object
.type
));
844 * This ref will not be updated through a commit, lets make
845 * sure it gets properly updated eventually.
847 if (commit
->util
|| commit
->object
.flags
& SHOWN
)
848 string_list_append(&extra_refs
, full_name
)->util
= commit
;
850 commit
->util
= full_name
;
854 static void handle_tags_and_duplicates(void)
856 struct commit
*commit
;
859 for (i
= extra_refs
.nr
- 1; i
>= 0; i
--) {
860 const char *name
= extra_refs
.items
[i
].string
;
861 struct object
*object
= extra_refs
.items
[i
].util
;
862 switch (object
->type
) {
864 handle_tag(name
, (struct tag
*)object
);
868 name
= anonymize_refname(name
);
869 /* create refs pointing to already seen commits */
870 commit
= (struct commit
*)object
;
871 printf("reset %s\nfrom :%d\n\n", name
,
872 get_object_mark(&commit
->object
));
879 static void export_marks(char *file
)
883 struct object_decoration
*deco
= idnums
.hash
;
887 f
= fopen_for_writing(file
);
889 die_errno("Unable to open marks file %s for writing.", file
);
891 for (i
= 0; i
< idnums
.size
; i
++) {
892 if (deco
->base
&& deco
->base
->type
== 1) {
893 mark
= ptr_to_mark(deco
->decoration
);
894 if (fprintf(f
, ":%"PRIu32
" %s\n", mark
,
895 oid_to_hex(&deco
->base
->oid
)) < 0) {
906 error("Unable to write marks file %s.", file
);
909 static void import_marks(char *input_file
)
912 FILE *f
= xfopen(input_file
, "r");
914 while (fgets(line
, sizeof(line
), f
)) {
916 char *line_end
, *mark_end
;
917 struct object_id oid
;
918 struct object
*object
;
919 struct commit
*commit
;
920 enum object_type type
;
922 line_end
= strchr(line
, '\n');
923 if (line
[0] != ':' || !line_end
)
924 die("corrupt mark line: %s", line
);
927 mark
= strtoumax(line
+ 1, &mark_end
, 10);
928 if (!mark
|| mark_end
== line
+ 1
929 || *mark_end
!= ' ' || get_oid_hex(mark_end
+ 1, &oid
))
930 die("corrupt mark line: %s", line
);
932 if (last_idnum
< mark
)
935 type
= sha1_object_info(oid
.hash
, NULL
);
937 die("object not found: %s", oid_to_hex(&oid
));
939 if (type
!= OBJ_COMMIT
)
943 commit
= lookup_commit(&oid
);
945 die("not a commit? can't happen: %s", oid_to_hex(&oid
));
947 object
= &commit
->object
;
949 if (object
->flags
& SHOWN
)
950 error("Object %s already has a mark", oid_to_hex(&oid
));
952 mark_object(object
, mark
);
954 object
->flags
|= SHOWN
;
959 static void handle_deletes(void)
962 for (i
= 0; i
< refspecs_nr
; i
++) {
963 struct refspec
*refspec
= &refspecs
[i
];
967 printf("reset %s\nfrom %s\n\n",
968 refspec
->dst
, sha1_to_hex(null_sha1
));
972 int cmd_fast_export(int argc
, const char **argv
, const char *prefix
)
974 struct rev_info revs
;
975 struct object_array commits
= OBJECT_ARRAY_INIT
;
976 struct commit
*commit
;
977 char *export_filename
= NULL
, *import_filename
= NULL
;
978 uint32_t lastimportid
;
979 struct string_list refspecs_list
= STRING_LIST_INIT_NODUP
;
980 struct option options
[] = {
981 OPT_INTEGER(0, "progress", &progress
,
982 N_("show progress after <n> objects")),
983 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode
, N_("mode"),
984 N_("select handling of signed tags"),
985 parse_opt_signed_tag_mode
),
986 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode
, N_("mode"),
987 N_("select handling of tags that tag filtered objects"),
988 parse_opt_tag_of_filtered_mode
),
989 OPT_STRING(0, "export-marks", &export_filename
, N_("file"),
990 N_("Dump marks to this file")),
991 OPT_STRING(0, "import-marks", &import_filename
, N_("file"),
992 N_("Import marks from this file")),
993 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger
,
994 N_("Fake a tagger when tags lack one")),
995 OPT_BOOL(0, "full-tree", &full_tree
,
996 N_("Output full tree for each commit")),
997 OPT_BOOL(0, "use-done-feature", &use_done_feature
,
998 N_("Use the done feature to terminate the stream")),
999 OPT_BOOL(0, "no-data", &no_data
, N_("Skip output of blob data")),
1000 OPT_STRING_LIST(0, "refspec", &refspecs_list
, N_("refspec"),
1001 N_("Apply refspec to exported refs")),
1002 OPT_BOOL(0, "anonymize", &anonymize
, N_("anonymize output")),
1007 usage_with_options (fast_export_usage
, options
);
1009 /* we handle encodings */
1010 git_config(git_default_config
, NULL
);
1012 init_revisions(&revs
, prefix
);
1013 revs
.topo_order
= 1;
1014 revs
.show_source
= 1;
1015 revs
.rewrite_parents
= 1;
1016 argc
= parse_options(argc
, argv
, prefix
, options
, fast_export_usage
,
1017 PARSE_OPT_KEEP_ARGV0
| PARSE_OPT_KEEP_UNKNOWN
);
1018 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
1020 usage_with_options (fast_export_usage
, options
);
1022 if (refspecs_list
.nr
) {
1023 const char **refspecs_str
;
1026 ALLOC_ARRAY(refspecs_str
, refspecs_list
.nr
);
1027 for (i
= 0; i
< refspecs_list
.nr
; i
++)
1028 refspecs_str
[i
] = refspecs_list
.items
[i
].string
;
1030 refspecs_nr
= refspecs_list
.nr
;
1031 refspecs
= parse_fetch_refspec(refspecs_nr
, refspecs_str
);
1033 string_list_clear(&refspecs_list
, 1);
1037 if (use_done_feature
)
1038 printf("feature done\n");
1040 if (import_filename
)
1041 import_marks(import_filename
);
1042 lastimportid
= last_idnum
;
1044 if (import_filename
&& revs
.prune_data
.nr
)
1047 get_tags_and_duplicates(&revs
.cmdline
);
1049 if (prepare_revision_walk(&revs
))
1050 die("revision walk setup failed");
1051 revs
.diffopt
.format_callback
= show_filemodify
;
1052 DIFF_OPT_SET(&revs
.diffopt
, RECURSIVE
);
1053 while ((commit
= get_revision(&revs
))) {
1054 if (has_unshown_parent(commit
)) {
1055 add_object_array(&commit
->object
, NULL
, &commits
);
1058 handle_commit(commit
, &revs
);
1059 handle_tail(&commits
, &revs
);
1063 handle_tags_and_duplicates();
1066 if (export_filename
&& lastimportid
!= last_idnum
)
1067 export_marks(export_filename
);
1069 if (use_done_feature
)
1072 free_refspec(refspecs_nr
, refspecs
);