Merge branch 'km/doc-workflows-typofix'
[git.git] / builtin / fast-export.c
blob6c9768742fd4faecb3719e9c87997df3d490502a
1 /*
2 * "git fast-export" builtin command
4 * Copyright (C) 2007 Johannes E. Schindelin
5 */
6 #include "builtin.h"
7 #include "cache.h"
8 #include "config.h"
9 #include "refs.h"
10 #include "refspec.h"
11 #include "commit.h"
12 #include "object.h"
13 #include "tag.h"
14 #include "diff.h"
15 #include "diffcore.h"
16 #include "log-tree.h"
17 #include "revision.h"
18 #include "decorate.h"
19 #include "string-list.h"
20 #include "utf8.h"
21 #include "parse-options.h"
22 #include "quote.h"
23 #include "remote.h"
24 #include "blob.h"
26 static const char *fast_export_usage[] = {
27 N_("git fast-export [rev-list-opts]"),
28 NULL
31 static int progress;
32 static enum { ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = ABORT;
33 static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR;
34 static int fake_missing_tagger;
35 static int use_done_feature;
36 static int no_data;
37 static int full_tree;
38 static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
39 static struct refspec refspecs = REFSPEC_INIT_FETCH;
40 static int anonymize;
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;
55 else
56 return error("Unknown signed-tags mode: %s", arg);
57 return 0;
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;
69 else
70 return error("Unknown tag-of-filtered mode: %s", arg);
71 return 0;
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))
84 return 1;
85 return 0;
88 struct anonymized_entry {
89 struct hashmap_entry hash;
90 const char *orig;
91 size_t orig_len;
92 const char *anon;
93 size_t anon_len;
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;
116 if (!map->cmpfn)
117 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
119 hashmap_entry_init(&key, memhash(orig, *len));
120 key.orig = orig;
121 key.orig_len = *len;
122 ret = hashmap_get(map, &key, NULL);
124 if (!ret) {
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;
135 return ret->anon;
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,
145 struct hashmap *map,
146 void *(*generate)(const void *, size_t *))
148 while (*path) {
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;
154 if (*path)
155 strbuf_addch(out, *path++);
159 static inline void *mark_to_ptr(uint32_t mark)
161 return (void *)(uintptr_t)mark;
164 static inline uint32_t ptr_to_mark(void * mark)
166 return (uint32_t)(uintptr_t)mark;
169 static inline void mark_object(struct object *object, uint32_t mark)
171 add_decoration(&idnums, object, mark_to_ptr(mark));
174 static inline void mark_next_object(struct object *object)
176 mark_object(object, ++last_idnum);
179 static int get_object_mark(struct object *object)
181 void *decoration = lookup_decoration(&idnums, object);
182 if (!decoration)
183 return 0;
184 return ptr_to_mark(decoration);
187 static void show_progress(void)
189 static int counter = 0;
190 if (!progress)
191 return;
192 if ((++counter % progress) == 0)
193 printf("progress %d objects\n", counter);
197 * Ideally we would want some transformation of the blob data here
198 * that is unreversible, but would still be the same size and have
199 * the same data relationship to other blobs (so that we get the same
200 * delta and packing behavior as the original). But the first and last
201 * requirements there are probably mutually exclusive, so let's take
202 * the easy way out for now, and just generate arbitrary content.
204 * There's no need to cache this result with anonymize_mem, since
205 * we already handle blob content caching with marks.
207 static char *anonymize_blob(unsigned long *size)
209 static int counter;
210 struct strbuf out = STRBUF_INIT;
211 strbuf_addf(&out, "anonymous blob %d", counter++);
212 *size = out.len;
213 return strbuf_detach(&out, NULL);
216 static void export_blob(const struct object_id *oid)
218 unsigned long size;
219 enum object_type type;
220 char *buf;
221 struct object *object;
222 int eaten;
224 if (no_data)
225 return;
227 if (is_null_oid(oid))
228 return;
230 object = lookup_object(oid->hash);
231 if (object && object->flags & SHOWN)
232 return;
234 if (anonymize) {
235 buf = anonymize_blob(&size);
236 object = (struct object *)lookup_blob(oid);
237 eaten = 0;
238 } else {
239 buf = read_object_file(oid, &type, &size);
240 if (!buf)
241 die ("Could not read blob %s", oid_to_hex(oid));
242 if (check_object_signature(oid, buf, size, type_name(type)) < 0)
243 die("sha1 mismatch in blob %s", oid_to_hex(oid));
244 object = parse_object_buffer(oid, type, size, buf, &eaten);
247 if (!object)
248 die("Could not read blob %s", oid_to_hex(oid));
250 mark_next_object(object);
252 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
253 if (size && fwrite(buf, size, 1, stdout) != 1)
254 die_errno ("Could not write blob '%s'", oid_to_hex(oid));
255 printf("\n");
257 show_progress();
259 object->flags |= SHOWN;
260 if (!eaten)
261 free(buf);
264 static int depth_first(const void *a_, const void *b_)
266 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
267 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
268 const char *name_a, *name_b;
269 int len_a, len_b, len;
270 int cmp;
272 name_a = a->one ? a->one->path : a->two->path;
273 name_b = b->one ? b->one->path : b->two->path;
275 len_a = strlen(name_a);
276 len_b = strlen(name_b);
277 len = (len_a < len_b) ? len_a : len_b;
279 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
280 cmp = memcmp(name_a, name_b, len);
281 if (cmp)
282 return cmp;
283 cmp = len_b - len_a;
284 if (cmp)
285 return cmp;
287 * Move 'R'ename entries last so that all references of the file
288 * appear in the output before it is renamed (e.g., when a file
289 * was copied and renamed in the same commit).
291 return (a->status == 'R') - (b->status == 'R');
294 static void print_path_1(const char *path)
296 int need_quote = quote_c_style(path, NULL, NULL, 0);
297 if (need_quote)
298 quote_c_style(path, NULL, stdout, 0);
299 else if (strchr(path, ' '))
300 printf("\"%s\"", path);
301 else
302 printf("%s", path);
305 static void *anonymize_path_component(const void *path, size_t *len)
307 static int counter;
308 struct strbuf out = STRBUF_INIT;
309 strbuf_addf(&out, "path%d", counter++);
310 return strbuf_detach(&out, len);
313 static void print_path(const char *path)
315 if (!anonymize)
316 print_path_1(path);
317 else {
318 static struct hashmap paths;
319 static struct strbuf anon = STRBUF_INIT;
321 anonymize_path(&anon, path, &paths, anonymize_path_component);
322 print_path_1(anon.buf);
323 strbuf_reset(&anon);
327 static void *generate_fake_oid(const void *old, size_t *len)
329 static uint32_t counter = 1; /* avoid null sha1 */
330 unsigned char *out = xcalloc(GIT_SHA1_RAWSZ, 1);
331 put_be32(out + GIT_SHA1_RAWSZ - 4, counter++);
332 return out;
335 static const unsigned char *anonymize_sha1(const struct object_id *oid)
337 static struct hashmap sha1s;
338 size_t len = GIT_SHA1_RAWSZ;
339 return anonymize_mem(&sha1s, generate_fake_oid, oid, &len);
342 static void show_filemodify(struct diff_queue_struct *q,
343 struct diff_options *options, void *data)
345 int i;
346 struct string_list *changed = 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:
360 printf("D ");
361 print_path(spec->path);
362 string_list_insert(changed, spec->path);
363 putchar('\n');
364 break;
366 case DIFF_STATUS_COPIED:
367 case DIFF_STATUS_RENAMED:
369 * If a change in the file corresponding to ospec->path
370 * has been observed, we cannot trust its contents
371 * because the diff is calculated based on the prior
372 * contents, not the current contents. So, declare a
373 * copy or rename only if there was no change observed.
375 if (!string_list_has_string(changed, ospec->path)) {
376 printf("%c ", q->queue[i]->status);
377 print_path(ospec->path);
378 putchar(' ');
379 print_path(spec->path);
380 string_list_insert(changed, spec->path);
381 putchar('\n');
383 if (!oidcmp(&ospec->oid, &spec->oid) &&
384 ospec->mode == spec->mode)
385 break;
387 /* fallthrough */
389 case DIFF_STATUS_TYPE_CHANGED:
390 case DIFF_STATUS_MODIFIED:
391 case DIFF_STATUS_ADDED:
393 * Links refer to objects in another repositories;
394 * output the SHA-1 verbatim.
396 if (no_data || S_ISGITLINK(spec->mode))
397 printf("M %06o %s ", spec->mode,
398 sha1_to_hex(anonymize ?
399 anonymize_sha1(&spec->oid) :
400 spec->oid.hash));
401 else {
402 struct object *object = lookup_object(spec->oid.hash);
403 printf("M %06o :%d ", spec->mode,
404 get_object_mark(object));
406 print_path(spec->path);
407 string_list_insert(changed, spec->path);
408 putchar('\n');
409 break;
411 default:
412 die("Unexpected comparison status '%c' for %s, %s",
413 q->queue[i]->status,
414 ospec->path ? ospec->path : "none",
415 spec->path ? spec->path : "none");
420 static const char *find_encoding(const char *begin, const char *end)
422 const char *needle = "\nencoding ";
423 char *bol, *eol;
425 bol = memmem(begin, end ? end - begin : strlen(begin),
426 needle, strlen(needle));
427 if (!bol)
428 return git_commit_encoding;
429 bol += strlen(needle);
430 eol = strchrnul(bol, '\n');
431 *eol = '\0';
432 return bol;
435 static void *anonymize_ref_component(const void *old, size_t *len)
437 static int counter;
438 struct strbuf out = STRBUF_INIT;
439 strbuf_addf(&out, "ref%d", counter++);
440 return strbuf_detach(&out, len);
443 static const char *anonymize_refname(const char *refname)
446 * If any of these prefixes is found, we will leave it intact
447 * so that tags remain tags and so forth.
449 static const char *prefixes[] = {
450 "refs/heads/",
451 "refs/tags/",
452 "refs/remotes/",
453 "refs/"
455 static struct hashmap refs;
456 static struct strbuf anon = STRBUF_INIT;
457 int i;
460 * We also leave "master" as a special case, since it does not reveal
461 * anything interesting.
463 if (!strcmp(refname, "refs/heads/master"))
464 return refname;
466 strbuf_reset(&anon);
467 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
468 if (skip_prefix(refname, prefixes[i], &refname)) {
469 strbuf_addstr(&anon, prefixes[i]);
470 break;
474 anonymize_path(&anon, refname, &refs, anonymize_ref_component);
475 return anon.buf;
479 * We do not even bother to cache commit messages, as they are unlikely
480 * to be repeated verbatim, and it is not that interesting when they are.
482 static char *anonymize_commit_message(const char *old)
484 static int counter;
485 return xstrfmt("subject %d\n\nbody\n", counter++);
488 static struct hashmap idents;
489 static void *anonymize_ident(const void *old, size_t *len)
491 static int counter;
492 struct strbuf out = STRBUF_INIT;
493 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
494 counter++;
495 return strbuf_detach(&out, len);
499 * Our strategy here is to anonymize the names and email addresses,
500 * but keep timestamps intact, as they influence things like traversal
501 * order (and by themselves should not be too revealing).
503 static void anonymize_ident_line(const char **beg, const char **end)
505 static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT };
506 static unsigned which_buffer;
508 struct strbuf *out;
509 struct ident_split split;
510 const char *end_of_header;
512 out = &buffers[which_buffer++];
513 which_buffer %= ARRAY_SIZE(buffers);
514 strbuf_reset(out);
516 /* skip "committer", "author", "tagger", etc */
517 end_of_header = strchr(*beg, ' ');
518 if (!end_of_header)
519 BUG("malformed line fed to anonymize_ident_line: %.*s",
520 (int)(*end - *beg), *beg);
521 end_of_header++;
522 strbuf_add(out, *beg, end_of_header - *beg);
524 if (!split_ident_line(&split, end_of_header, *end - end_of_header) &&
525 split.date_begin) {
526 const char *ident;
527 size_t len;
529 len = split.mail_end - split.name_begin;
530 ident = anonymize_mem(&idents, anonymize_ident,
531 split.name_begin, &len);
532 strbuf_add(out, ident, len);
533 strbuf_addch(out, ' ');
534 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
535 } else {
536 strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000");
539 *beg = out->buf;
540 *end = out->buf + out->len;
543 static void handle_commit(struct commit *commit, struct rev_info *rev,
544 struct string_list *paths_of_changed_objects)
546 int saved_output_format = rev->diffopt.output_format;
547 const char *commit_buffer;
548 const char *author, *author_end, *committer, *committer_end;
549 const char *encoding, *message;
550 char *reencoded = NULL;
551 struct commit_list *p;
552 const char *refname;
553 int i;
555 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
557 parse_commit_or_die(commit);
558 commit_buffer = get_commit_buffer(commit, NULL);
559 author = strstr(commit_buffer, "\nauthor ");
560 if (!author)
561 die ("Could not find author in commit %s",
562 oid_to_hex(&commit->object.oid));
563 author++;
564 author_end = strchrnul(author, '\n');
565 committer = strstr(author_end, "\ncommitter ");
566 if (!committer)
567 die ("Could not find committer in commit %s",
568 oid_to_hex(&commit->object.oid));
569 committer++;
570 committer_end = strchrnul(committer, '\n');
571 message = strstr(committer_end, "\n\n");
572 encoding = find_encoding(committer_end, message);
573 if (message)
574 message += 2;
576 if (commit->parents &&
577 get_object_mark(&commit->parents->item->object) != 0 &&
578 !full_tree) {
579 parse_commit_or_die(commit->parents->item);
580 diff_tree_oid(get_commit_tree_oid(commit->parents->item),
581 get_commit_tree_oid(commit), "", &rev->diffopt);
583 else
584 diff_root_tree_oid(get_commit_tree_oid(commit),
585 "", &rev->diffopt);
587 /* Export the referenced blobs, and remember the marks. */
588 for (i = 0; i < diff_queued_diff.nr; i++)
589 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
590 export_blob(&diff_queued_diff.queue[i]->two->oid);
592 refname = commit->util;
593 if (anonymize) {
594 refname = anonymize_refname(refname);
595 anonymize_ident_line(&committer, &committer_end);
596 anonymize_ident_line(&author, &author_end);
599 mark_next_object(&commit->object);
600 if (anonymize)
601 reencoded = anonymize_commit_message(message);
602 else if (!is_encoding_utf8(encoding))
603 reencoded = reencode_string(message, "UTF-8", encoding);
604 if (!commit->parents)
605 printf("reset %s\n", refname);
606 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
607 refname, last_idnum,
608 (int)(author_end - author), author,
609 (int)(committer_end - committer), committer,
610 (unsigned)(reencoded
611 ? strlen(reencoded) : message
612 ? strlen(message) : 0),
613 reencoded ? reencoded : message ? message : "");
614 free(reencoded);
615 unuse_commit_buffer(commit, commit_buffer);
617 for (i = 0, p = commit->parents; p; p = p->next) {
618 int mark = get_object_mark(&p->item->object);
619 if (!mark)
620 continue;
621 if (i == 0)
622 printf("from :%d\n", mark);
623 else
624 printf("merge :%d\n", mark);
625 i++;
628 if (full_tree)
629 printf("deleteall\n");
630 log_tree_diff_flush(rev);
631 string_list_clear(paths_of_changed_objects, 0);
632 rev->diffopt.output_format = saved_output_format;
634 printf("\n");
636 show_progress();
639 static void *anonymize_tag(const void *old, size_t *len)
641 static int counter;
642 struct strbuf out = STRBUF_INIT;
643 strbuf_addf(&out, "tag message %d", counter++);
644 return strbuf_detach(&out, len);
647 static void handle_tail(struct object_array *commits, struct rev_info *revs,
648 struct string_list *paths_of_changed_objects)
650 struct commit *commit;
651 while (commits->nr) {
652 commit = (struct commit *)object_array_pop(commits);
653 if (has_unshown_parent(commit)) {
654 /* Queue again, to be handled later */
655 add_object_array(&commit->object, NULL, commits);
656 return;
658 handle_commit(commit, revs, paths_of_changed_objects);
662 static void handle_tag(const char *name, struct tag *tag)
664 unsigned long size;
665 enum object_type type;
666 char *buf;
667 const char *tagger, *tagger_end, *message;
668 size_t message_size = 0;
669 struct object *tagged;
670 int tagged_mark;
671 struct commit *p;
673 /* Trees have no identifier in fast-export output, thus we have no way
674 * to output tags of trees, tags of tags of trees, etc. Simply omit
675 * such tags.
677 tagged = tag->tagged;
678 while (tagged->type == OBJ_TAG) {
679 tagged = ((struct tag *)tagged)->tagged;
681 if (tagged->type == OBJ_TREE) {
682 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
683 oid_to_hex(&tag->object.oid));
684 return;
687 buf = read_object_file(&tag->object.oid, &type, &size);
688 if (!buf)
689 die ("Could not read tag %s", oid_to_hex(&tag->object.oid));
690 message = memmem(buf, size, "\n\n", 2);
691 if (message) {
692 message += 2;
693 message_size = strlen(message);
695 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
696 if (!tagger) {
697 if (fake_missing_tagger)
698 tagger = "tagger Unspecified Tagger "
699 "<unspecified-tagger> 0 +0000";
700 else
701 tagger = "";
702 tagger_end = tagger + strlen(tagger);
703 } else {
704 tagger++;
705 tagger_end = strchrnul(tagger, '\n');
706 if (anonymize)
707 anonymize_ident_line(&tagger, &tagger_end);
710 if (anonymize) {
711 name = anonymize_refname(name);
712 if (message) {
713 static struct hashmap tags;
714 message = anonymize_mem(&tags, anonymize_tag,
715 message, &message_size);
719 /* handle signed tags */
720 if (message) {
721 const char *signature = strstr(message,
722 "\n-----BEGIN PGP SIGNATURE-----\n");
723 if (signature)
724 switch(signed_tag_mode) {
725 case ABORT:
726 die ("Encountered signed tag %s; use "
727 "--signed-tags=<mode> to handle it.",
728 oid_to_hex(&tag->object.oid));
729 case WARN:
730 warning ("Exporting signed tag %s",
731 oid_to_hex(&tag->object.oid));
732 /* fallthru */
733 case VERBATIM:
734 break;
735 case WARN_STRIP:
736 warning ("Stripping signature from tag %s",
737 oid_to_hex(&tag->object.oid));
738 /* fallthru */
739 case STRIP:
740 message_size = signature + 1 - message;
741 break;
745 /* handle tag->tagged having been filtered out due to paths specified */
746 tagged = tag->tagged;
747 tagged_mark = get_object_mark(tagged);
748 if (!tagged_mark) {
749 switch(tag_of_filtered_mode) {
750 case ABORT:
751 die ("Tag %s tags unexported object; use "
752 "--tag-of-filtered-object=<mode> to handle it.",
753 oid_to_hex(&tag->object.oid));
754 case DROP:
755 /* Ignore this tag altogether */
756 free(buf);
757 return;
758 case REWRITE:
759 if (tagged->type != OBJ_COMMIT) {
760 die ("Tag %s tags unexported %s!",
761 oid_to_hex(&tag->object.oid),
762 type_name(tagged->type));
764 p = (struct commit *)tagged;
765 for (;;) {
766 if (p->parents && p->parents->next)
767 break;
768 if (p->object.flags & UNINTERESTING)
769 break;
770 if (!(p->object.flags & TREESAME))
771 break;
772 if (!p->parents)
773 die ("Can't find replacement commit for tag %s\n",
774 oid_to_hex(&tag->object.oid));
775 p = p->parents->item;
777 tagged_mark = get_object_mark(&p->object);
781 if (starts_with(name, "refs/tags/"))
782 name += 10;
783 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
784 name, tagged_mark,
785 (int)(tagger_end - tagger), tagger,
786 tagger == tagger_end ? "" : "\n",
787 (int)message_size, (int)message_size, message ? message : "");
788 free(buf);
791 static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
793 switch (e->item->type) {
794 case OBJ_COMMIT:
795 return (struct commit *)e->item;
796 case OBJ_TAG: {
797 struct tag *tag = (struct tag *)e->item;
799 /* handle nested tags */
800 while (tag && tag->object.type == OBJ_TAG) {
801 parse_object(&tag->object.oid);
802 string_list_append(&extra_refs, full_name)->util = tag;
803 tag = (struct tag *)tag->tagged;
805 if (!tag)
806 die("Tag %s points nowhere?", e->name);
807 return (struct commit *)tag;
808 break;
810 default:
811 return NULL;
815 static void get_tags_and_duplicates(struct rev_cmdline_info *info)
817 int i;
819 for (i = 0; i < info->nr; i++) {
820 struct rev_cmdline_entry *e = info->rev + i;
821 struct object_id oid;
822 struct commit *commit;
823 char *full_name;
825 if (e->flags & UNINTERESTING)
826 continue;
828 if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1)
829 continue;
831 if (refspecs.nr) {
832 char *private;
833 private = apply_refspecs(&refspecs, full_name);
834 if (private) {
835 free(full_name);
836 full_name = private;
840 commit = get_commit(e, full_name);
841 if (!commit) {
842 warning("%s: Unexpected object of type %s, skipping.",
843 e->name,
844 type_name(e->item->type));
845 continue;
848 switch(commit->object.type) {
849 case OBJ_COMMIT:
850 break;
851 case OBJ_BLOB:
852 export_blob(&commit->object.oid);
853 continue;
854 default: /* OBJ_TAG (nested tags) is already handled */
855 warning("Tag points to object of unexpected type %s, skipping.",
856 type_name(commit->object.type));
857 continue;
861 * This ref will not be updated through a commit, lets make
862 * sure it gets properly updated eventually.
864 if (commit->util || commit->object.flags & SHOWN)
865 string_list_append(&extra_refs, full_name)->util = commit;
866 if (!commit->util)
867 commit->util = full_name;
871 static void handle_tags_and_duplicates(void)
873 struct commit *commit;
874 int i;
876 for (i = extra_refs.nr - 1; i >= 0; i--) {
877 const char *name = extra_refs.items[i].string;
878 struct object *object = extra_refs.items[i].util;
879 switch (object->type) {
880 case OBJ_TAG:
881 handle_tag(name, (struct tag *)object);
882 break;
883 case OBJ_COMMIT:
884 if (anonymize)
885 name = anonymize_refname(name);
886 /* create refs pointing to already seen commits */
887 commit = (struct commit *)object;
888 printf("reset %s\nfrom :%d\n\n", name,
889 get_object_mark(&commit->object));
890 show_progress();
891 break;
896 static void export_marks(char *file)
898 unsigned int i;
899 uint32_t mark;
900 struct decoration_entry *deco = idnums.entries;
901 FILE *f;
902 int e = 0;
904 f = fopen_for_writing(file);
905 if (!f)
906 die_errno("Unable to open marks file %s for writing.", file);
908 for (i = 0; i < idnums.size; i++) {
909 if (deco->base && deco->base->type == 1) {
910 mark = ptr_to_mark(deco->decoration);
911 if (fprintf(f, ":%"PRIu32" %s\n", mark,
912 oid_to_hex(&deco->base->oid)) < 0) {
913 e = 1;
914 break;
917 deco++;
920 e |= ferror(f);
921 e |= fclose(f);
922 if (e)
923 error("Unable to write marks file %s.", file);
926 static void import_marks(char *input_file)
928 char line[512];
929 FILE *f = xfopen(input_file, "r");
931 while (fgets(line, sizeof(line), f)) {
932 uint32_t mark;
933 char *line_end, *mark_end;
934 struct object_id oid;
935 struct object *object;
936 struct commit *commit;
937 enum object_type type;
939 line_end = strchr(line, '\n');
940 if (line[0] != ':' || !line_end)
941 die("corrupt mark line: %s", line);
942 *line_end = '\0';
944 mark = strtoumax(line + 1, &mark_end, 10);
945 if (!mark || mark_end == line + 1
946 || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
947 die("corrupt mark line: %s", line);
949 if (last_idnum < mark)
950 last_idnum = mark;
952 type = oid_object_info(the_repository, &oid, NULL);
953 if (type < 0)
954 die("object not found: %s", oid_to_hex(&oid));
956 if (type != OBJ_COMMIT)
957 /* only commits */
958 continue;
960 commit = lookup_commit(&oid);
961 if (!commit)
962 die("not a commit? can't happen: %s", oid_to_hex(&oid));
964 object = &commit->object;
966 if (object->flags & SHOWN)
967 error("Object %s already has a mark", oid_to_hex(&oid));
969 mark_object(object, mark);
971 object->flags |= SHOWN;
973 fclose(f);
976 static void handle_deletes(void)
978 int i;
979 for (i = 0; i < refspecs.nr; i++) {
980 struct refspec_item *refspec = &refspecs.items[i];
981 if (*refspec->src)
982 continue;
984 printf("reset %s\nfrom %s\n\n",
985 refspec->dst, sha1_to_hex(null_sha1));
989 int cmd_fast_export(int argc, const char **argv, const char *prefix)
991 struct rev_info revs;
992 struct object_array commits = OBJECT_ARRAY_INIT;
993 struct commit *commit;
994 char *export_filename = NULL, *import_filename = NULL;
995 uint32_t lastimportid;
996 struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
997 struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
998 struct option options[] = {
999 OPT_INTEGER(0, "progress", &progress,
1000 N_("show progress after <n> objects")),
1001 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1002 N_("select handling of signed tags"),
1003 parse_opt_signed_tag_mode),
1004 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1005 N_("select handling of tags that tag filtered objects"),
1006 parse_opt_tag_of_filtered_mode),
1007 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1008 N_("Dump marks to this file")),
1009 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1010 N_("Import marks from this file")),
1011 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1012 N_("Fake a tagger when tags lack one")),
1013 OPT_BOOL(0, "full-tree", &full_tree,
1014 N_("Output full tree for each commit")),
1015 OPT_BOOL(0, "use-done-feature", &use_done_feature,
1016 N_("Use the done feature to terminate the stream")),
1017 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
1018 OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1019 N_("Apply refspec to exported refs")),
1020 OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
1021 OPT_END()
1024 if (argc == 1)
1025 usage_with_options (fast_export_usage, options);
1027 /* we handle encodings */
1028 git_config(git_default_config, NULL);
1030 init_revisions(&revs, prefix);
1031 revs.topo_order = 1;
1032 revs.show_source = 1;
1033 revs.rewrite_parents = 1;
1034 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1035 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
1036 argc = setup_revisions(argc, argv, &revs, NULL);
1037 if (argc > 1)
1038 usage_with_options (fast_export_usage, options);
1040 if (refspecs_list.nr) {
1041 int i;
1043 for (i = 0; i < refspecs_list.nr; i++)
1044 refspec_append(&refspecs, refspecs_list.items[i].string);
1046 string_list_clear(&refspecs_list, 1);
1049 if (use_done_feature)
1050 printf("feature done\n");
1052 if (import_filename)
1053 import_marks(import_filename);
1054 lastimportid = last_idnum;
1056 if (import_filename && revs.prune_data.nr)
1057 full_tree = 1;
1059 get_tags_and_duplicates(&revs.cmdline);
1061 if (prepare_revision_walk(&revs))
1062 die("revision walk setup failed");
1063 revs.diffopt.format_callback = show_filemodify;
1064 revs.diffopt.format_callback_data = &paths_of_changed_objects;
1065 revs.diffopt.flags.recursive = 1;
1066 while ((commit = get_revision(&revs))) {
1067 if (has_unshown_parent(commit)) {
1068 add_object_array(&commit->object, NULL, &commits);
1070 else {
1071 handle_commit(commit, &revs, &paths_of_changed_objects);
1072 handle_tail(&commits, &revs, &paths_of_changed_objects);
1076 handle_tags_and_duplicates();
1077 handle_deletes();
1079 if (export_filename && lastimportid != last_idnum)
1080 export_marks(export_filename);
1082 if (use_done_feature)
1083 printf("done\n");
1085 refspec_clear(&refspecs);
1087 return 0;