Merge branch 'ab/remove-implicit-use-of-the-repository' into en/header-split-cache-h
[alt-git.git] / builtin / fast-export.c
blob0bb779deb698ce4bcfda9d58455db16d14346e60
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 "gettext.h"
10 #include "hex.h"
11 #include "refs.h"
12 #include "refspec.h"
13 #include "object-store.h"
14 #include "commit.h"
15 #include "object.h"
16 #include "tag.h"
17 #include "diff.h"
18 #include "diffcore.h"
19 #include "log-tree.h"
20 #include "revision.h"
21 #include "decorate.h"
22 #include "string-list.h"
23 #include "utf8.h"
24 #include "parse-options.h"
25 #include "quote.h"
26 #include "remote.h"
27 #include "blob.h"
28 #include "commit-slab.h"
30 static const char *fast_export_usage[] = {
31 N_("git fast-export [<rev-list-opts>]"),
32 NULL
35 static int progress;
36 static enum { SIGNED_TAG_ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = SIGNED_TAG_ABORT;
37 static enum { TAG_FILTERING_ABORT, DROP, REWRITE } tag_of_filtered_mode = TAG_FILTERING_ABORT;
38 static enum { REENCODE_ABORT, REENCODE_YES, REENCODE_NO } reencode_mode = REENCODE_ABORT;
39 static int fake_missing_tagger;
40 static int use_done_feature;
41 static int no_data;
42 static int full_tree;
43 static int reference_excluded_commits;
44 static int show_original_ids;
45 static int mark_tags;
46 static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
47 static struct string_list tag_refs = STRING_LIST_INIT_NODUP;
48 static struct refspec refspecs = REFSPEC_INIT_FETCH;
49 static int anonymize;
50 static struct hashmap anonymized_seeds;
51 static struct revision_sources revision_sources;
53 static int parse_opt_signed_tag_mode(const struct option *opt,
54 const char *arg, int unset)
56 if (unset || !strcmp(arg, "abort"))
57 signed_tag_mode = SIGNED_TAG_ABORT;
58 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
59 signed_tag_mode = VERBATIM;
60 else if (!strcmp(arg, "warn"))
61 signed_tag_mode = WARN;
62 else if (!strcmp(arg, "warn-strip"))
63 signed_tag_mode = WARN_STRIP;
64 else if (!strcmp(arg, "strip"))
65 signed_tag_mode = STRIP;
66 else
67 return error("Unknown signed-tags mode: %s", arg);
68 return 0;
71 static int parse_opt_tag_of_filtered_mode(const struct option *opt,
72 const char *arg, int unset)
74 if (unset || !strcmp(arg, "abort"))
75 tag_of_filtered_mode = TAG_FILTERING_ABORT;
76 else if (!strcmp(arg, "drop"))
77 tag_of_filtered_mode = DROP;
78 else if (!strcmp(arg, "rewrite"))
79 tag_of_filtered_mode = REWRITE;
80 else
81 return error("Unknown tag-of-filtered mode: %s", arg);
82 return 0;
85 static int parse_opt_reencode_mode(const struct option *opt,
86 const char *arg, int unset)
88 if (unset) {
89 reencode_mode = REENCODE_ABORT;
90 return 0;
93 switch (git_parse_maybe_bool(arg)) {
94 case 0:
95 reencode_mode = REENCODE_NO;
96 break;
97 case 1:
98 reencode_mode = REENCODE_YES;
99 break;
100 default:
101 if (!strcasecmp(arg, "abort"))
102 reencode_mode = REENCODE_ABORT;
103 else
104 return error("Unknown reencoding mode: %s", arg);
107 return 0;
110 static struct decoration idnums;
111 static uint32_t last_idnum;
112 struct anonymized_entry {
113 struct hashmap_entry hash;
114 const char *anon;
115 const char orig[FLEX_ARRAY];
118 struct anonymized_entry_key {
119 struct hashmap_entry hash;
120 const char *orig;
121 size_t orig_len;
124 static int anonymized_entry_cmp(const void *cmp_data UNUSED,
125 const struct hashmap_entry *eptr,
126 const struct hashmap_entry *entry_or_key,
127 const void *keydata)
129 const struct anonymized_entry *a, *b;
131 a = container_of(eptr, const struct anonymized_entry, hash);
132 if (keydata) {
133 const struct anonymized_entry_key *key = keydata;
134 int equal = !strncmp(a->orig, key->orig, key->orig_len) &&
135 !a->orig[key->orig_len];
136 return !equal;
139 b = container_of(entry_or_key, const struct anonymized_entry, hash);
140 return strcmp(a->orig, b->orig);
144 * Basically keep a cache of X->Y so that we can repeatedly replace
145 * the same anonymized string with another. The actual generation
146 * is farmed out to the generate function.
148 static const char *anonymize_str(struct hashmap *map,
149 char *(*generate)(void *),
150 const char *orig, size_t len,
151 void *data)
153 struct anonymized_entry_key key;
154 struct anonymized_entry *ret;
156 if (!map->cmpfn)
157 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
159 hashmap_entry_init(&key.hash, memhash(orig, len));
160 key.orig = orig;
161 key.orig_len = len;
163 /* First check if it's a token the user configured manually... */
164 if (anonymized_seeds.cmpfn)
165 ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
166 else
167 ret = NULL;
169 /* ...otherwise check if we've already seen it in this context... */
170 if (!ret)
171 ret = hashmap_get_entry(map, &key, hash, &key);
173 /* ...and finally generate a new mapping if necessary */
174 if (!ret) {
175 FLEX_ALLOC_MEM(ret, orig, orig, len);
176 hashmap_entry_init(&ret->hash, key.hash.hash);
177 ret->anon = generate(data);
178 hashmap_put(map, &ret->hash);
181 return ret->anon;
185 * We anonymize each component of a path individually,
186 * so that paths a/b and a/c will share a common root.
187 * The paths are cached via anonymize_mem so that repeated
188 * lookups for "a" will yield the same value.
190 static void anonymize_path(struct strbuf *out, const char *path,
191 struct hashmap *map,
192 char *(*generate)(void *))
194 while (*path) {
195 const char *end_of_component = strchrnul(path, '/');
196 size_t len = end_of_component - path;
197 const char *c = anonymize_str(map, generate, path, len, NULL);
198 strbuf_addstr(out, c);
199 path = end_of_component;
200 if (*path)
201 strbuf_addch(out, *path++);
205 static inline void *mark_to_ptr(uint32_t mark)
207 return (void *)(uintptr_t)mark;
210 static inline uint32_t ptr_to_mark(void * mark)
212 return (uint32_t)(uintptr_t)mark;
215 static inline void mark_object(struct object *object, uint32_t mark)
217 add_decoration(&idnums, object, mark_to_ptr(mark));
220 static inline void mark_next_object(struct object *object)
222 mark_object(object, ++last_idnum);
225 static int get_object_mark(struct object *object)
227 void *decoration = lookup_decoration(&idnums, object);
228 if (!decoration)
229 return 0;
230 return ptr_to_mark(decoration);
233 static struct commit *rewrite_commit(struct commit *p)
235 for (;;) {
236 if (p->parents && p->parents->next)
237 break;
238 if (p->object.flags & UNINTERESTING)
239 break;
240 if (!(p->object.flags & TREESAME))
241 break;
242 if (!p->parents)
243 return NULL;
244 p = p->parents->item;
246 return p;
249 static void show_progress(void)
251 static int counter = 0;
252 if (!progress)
253 return;
254 if ((++counter % progress) == 0)
255 printf("progress %d objects\n", counter);
259 * Ideally we would want some transformation of the blob data here
260 * that is unreversible, but would still be the same size and have
261 * the same data relationship to other blobs (so that we get the same
262 * delta and packing behavior as the original). But the first and last
263 * requirements there are probably mutually exclusive, so let's take
264 * the easy way out for now, and just generate arbitrary content.
266 * There's no need to cache this result with anonymize_mem, since
267 * we already handle blob content caching with marks.
269 static char *anonymize_blob(unsigned long *size)
271 static int counter;
272 struct strbuf out = STRBUF_INIT;
273 strbuf_addf(&out, "anonymous blob %d", counter++);
274 *size = out.len;
275 return strbuf_detach(&out, NULL);
278 static void export_blob(const struct object_id *oid)
280 unsigned long size;
281 enum object_type type;
282 char *buf;
283 struct object *object;
284 int eaten;
286 if (no_data)
287 return;
289 if (is_null_oid(oid))
290 return;
292 object = lookup_object(the_repository, oid);
293 if (object && object->flags & SHOWN)
294 return;
296 if (anonymize) {
297 buf = anonymize_blob(&size);
298 object = (struct object *)lookup_blob(the_repository, oid);
299 eaten = 0;
300 } else {
301 buf = repo_read_object_file(the_repository, oid, &type, &size);
302 if (!buf)
303 die("could not read blob %s", oid_to_hex(oid));
304 if (check_object_signature(the_repository, oid, buf, size,
305 type) < 0)
306 die("oid mismatch in blob %s", oid_to_hex(oid));
307 object = parse_object_buffer(the_repository, oid, type,
308 size, buf, &eaten);
311 if (!object)
312 die("Could not read blob %s", oid_to_hex(oid));
314 mark_next_object(object);
316 printf("blob\nmark :%"PRIu32"\n", last_idnum);
317 if (show_original_ids)
318 printf("original-oid %s\n", oid_to_hex(oid));
319 printf("data %"PRIuMAX"\n", (uintmax_t)size);
320 if (size && fwrite(buf, size, 1, stdout) != 1)
321 die_errno("could not write blob '%s'", oid_to_hex(oid));
322 printf("\n");
324 show_progress();
326 object->flags |= SHOWN;
327 if (!eaten)
328 free(buf);
331 static int depth_first(const void *a_, const void *b_)
333 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
334 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
335 const char *name_a, *name_b;
336 int len_a, len_b, len;
337 int cmp;
339 name_a = a->one ? a->one->path : a->two->path;
340 name_b = b->one ? b->one->path : b->two->path;
342 len_a = strlen(name_a);
343 len_b = strlen(name_b);
344 len = (len_a < len_b) ? len_a : len_b;
346 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
347 cmp = memcmp(name_a, name_b, len);
348 if (cmp)
349 return cmp;
350 cmp = len_b - len_a;
351 if (cmp)
352 return cmp;
354 * Move 'R'ename entries last so that all references of the file
355 * appear in the output before it is renamed (e.g., when a file
356 * was copied and renamed in the same commit).
358 return (a->status == 'R') - (b->status == 'R');
361 static void print_path_1(const char *path)
363 int need_quote = quote_c_style(path, NULL, NULL, 0);
364 if (need_quote)
365 quote_c_style(path, NULL, stdout, 0);
366 else if (strchr(path, ' '))
367 printf("\"%s\"", path);
368 else
369 printf("%s", path);
372 static char *anonymize_path_component(void *data)
374 static int counter;
375 struct strbuf out = STRBUF_INIT;
376 strbuf_addf(&out, "path%d", counter++);
377 return strbuf_detach(&out, NULL);
380 static void print_path(const char *path)
382 if (!anonymize)
383 print_path_1(path);
384 else {
385 static struct hashmap paths;
386 static struct strbuf anon = STRBUF_INIT;
388 anonymize_path(&anon, path, &paths, anonymize_path_component);
389 print_path_1(anon.buf);
390 strbuf_reset(&anon);
394 static char *generate_fake_oid(void *data)
396 static uint32_t counter = 1; /* avoid null oid */
397 const unsigned hashsz = the_hash_algo->rawsz;
398 struct object_id oid;
399 char *hex = xmallocz(GIT_MAX_HEXSZ);
401 oidclr(&oid);
402 put_be32(oid.hash + hashsz - 4, counter++);
403 return oid_to_hex_r(hex, &oid);
406 static const char *anonymize_oid(const char *oid_hex)
408 static struct hashmap objs;
409 size_t len = strlen(oid_hex);
410 return anonymize_str(&objs, generate_fake_oid, oid_hex, len, NULL);
413 static void show_filemodify(struct diff_queue_struct *q,
414 struct diff_options *options UNUSED, void *data)
416 int i;
417 struct string_list *changed = data;
420 * Handle files below a directory first, in case they are all deleted
421 * and the directory changes to a file or symlink.
423 QSORT(q->queue, q->nr, depth_first);
425 for (i = 0; i < q->nr; i++) {
426 struct diff_filespec *ospec = q->queue[i]->one;
427 struct diff_filespec *spec = q->queue[i]->two;
429 switch (q->queue[i]->status) {
430 case DIFF_STATUS_DELETED:
431 printf("D ");
432 print_path(spec->path);
433 string_list_insert(changed, spec->path);
434 putchar('\n');
435 break;
437 case DIFF_STATUS_COPIED:
438 case DIFF_STATUS_RENAMED:
440 * If a change in the file corresponding to ospec->path
441 * has been observed, we cannot trust its contents
442 * because the diff is calculated based on the prior
443 * contents, not the current contents. So, declare a
444 * copy or rename only if there was no change observed.
446 if (!string_list_has_string(changed, ospec->path)) {
447 printf("%c ", q->queue[i]->status);
448 print_path(ospec->path);
449 putchar(' ');
450 print_path(spec->path);
451 string_list_insert(changed, spec->path);
452 putchar('\n');
454 if (oideq(&ospec->oid, &spec->oid) &&
455 ospec->mode == spec->mode)
456 break;
458 /* fallthrough */
460 case DIFF_STATUS_TYPE_CHANGED:
461 case DIFF_STATUS_MODIFIED:
462 case DIFF_STATUS_ADDED:
464 * Links refer to objects in another repositories;
465 * output the SHA-1 verbatim.
467 if (no_data || S_ISGITLINK(spec->mode))
468 printf("M %06o %s ", spec->mode,
469 anonymize ?
470 anonymize_oid(oid_to_hex(&spec->oid)) :
471 oid_to_hex(&spec->oid));
472 else {
473 struct object *object = lookup_object(the_repository,
474 &spec->oid);
475 printf("M %06o :%d ", spec->mode,
476 get_object_mark(object));
478 print_path(spec->path);
479 string_list_insert(changed, spec->path);
480 putchar('\n');
481 break;
483 default:
484 die("Unexpected comparison status '%c' for %s, %s",
485 q->queue[i]->status,
486 ospec->path ? ospec->path : "none",
487 spec->path ? spec->path : "none");
492 static const char *find_encoding(const char *begin, const char *end)
494 const char *needle = "\nencoding ";
495 char *bol, *eol;
497 bol = memmem(begin, end ? end - begin : strlen(begin),
498 needle, strlen(needle));
499 if (!bol)
500 return NULL;
501 bol += strlen(needle);
502 eol = strchrnul(bol, '\n');
503 *eol = '\0';
504 return bol;
507 static char *anonymize_ref_component(void *data)
509 static int counter;
510 struct strbuf out = STRBUF_INIT;
511 strbuf_addf(&out, "ref%d", counter++);
512 return strbuf_detach(&out, NULL);
515 static const char *anonymize_refname(const char *refname)
518 * If any of these prefixes is found, we will leave it intact
519 * so that tags remain tags and so forth.
521 static const char *prefixes[] = {
522 "refs/heads/",
523 "refs/tags/",
524 "refs/remotes/",
525 "refs/"
527 static struct hashmap refs;
528 static struct strbuf anon = STRBUF_INIT;
529 int i;
531 strbuf_reset(&anon);
532 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
533 if (skip_prefix(refname, prefixes[i], &refname)) {
534 strbuf_addstr(&anon, prefixes[i]);
535 break;
539 anonymize_path(&anon, refname, &refs, anonymize_ref_component);
540 return anon.buf;
544 * We do not even bother to cache commit messages, as they are unlikely
545 * to be repeated verbatim, and it is not that interesting when they are.
547 static char *anonymize_commit_message(const char *old)
549 static int counter;
550 return xstrfmt("subject %d\n\nbody\n", counter++);
553 static char *anonymize_ident(void *data)
555 static int counter;
556 struct strbuf out = STRBUF_INIT;
557 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
558 counter++;
559 return strbuf_detach(&out, NULL);
563 * Our strategy here is to anonymize the names and email addresses,
564 * but keep timestamps intact, as they influence things like traversal
565 * order (and by themselves should not be too revealing).
567 static void anonymize_ident_line(const char **beg, const char **end)
569 static struct hashmap idents;
570 static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT };
571 static unsigned which_buffer;
573 struct strbuf *out;
574 struct ident_split split;
575 const char *end_of_header;
577 out = &buffers[which_buffer++];
578 which_buffer %= ARRAY_SIZE(buffers);
579 strbuf_reset(out);
581 /* skip "committer", "author", "tagger", etc */
582 end_of_header = strchr(*beg, ' ');
583 if (!end_of_header)
584 BUG("malformed line fed to anonymize_ident_line: %.*s",
585 (int)(*end - *beg), *beg);
586 end_of_header++;
587 strbuf_add(out, *beg, end_of_header - *beg);
589 if (!split_ident_line(&split, end_of_header, *end - end_of_header) &&
590 split.date_begin) {
591 const char *ident;
592 size_t len;
594 len = split.mail_end - split.name_begin;
595 ident = anonymize_str(&idents, anonymize_ident,
596 split.name_begin, len, NULL);
597 strbuf_addstr(out, ident);
598 strbuf_addch(out, ' ');
599 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
600 } else {
601 strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000");
604 *beg = out->buf;
605 *end = out->buf + out->len;
608 static void handle_commit(struct commit *commit, struct rev_info *rev,
609 struct string_list *paths_of_changed_objects)
611 int saved_output_format = rev->diffopt.output_format;
612 const char *commit_buffer;
613 const char *author, *author_end, *committer, *committer_end;
614 const char *encoding, *message;
615 char *reencoded = NULL;
616 struct commit_list *p;
617 const char *refname;
618 int i;
620 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
622 parse_commit_or_die(commit);
623 commit_buffer = repo_get_commit_buffer(the_repository, commit, NULL);
624 author = strstr(commit_buffer, "\nauthor ");
625 if (!author)
626 die("could not find author in commit %s",
627 oid_to_hex(&commit->object.oid));
628 author++;
629 author_end = strchrnul(author, '\n');
630 committer = strstr(author_end, "\ncommitter ");
631 if (!committer)
632 die("could not find committer in commit %s",
633 oid_to_hex(&commit->object.oid));
634 committer++;
635 committer_end = strchrnul(committer, '\n');
636 message = strstr(committer_end, "\n\n");
637 encoding = find_encoding(committer_end, message);
638 if (message)
639 message += 2;
641 if (commit->parents &&
642 (get_object_mark(&commit->parents->item->object) != 0 ||
643 reference_excluded_commits) &&
644 !full_tree) {
645 parse_commit_or_die(commit->parents->item);
646 diff_tree_oid(get_commit_tree_oid(commit->parents->item),
647 get_commit_tree_oid(commit), "", &rev->diffopt);
649 else
650 diff_root_tree_oid(get_commit_tree_oid(commit),
651 "", &rev->diffopt);
653 /* Export the referenced blobs, and remember the marks. */
654 for (i = 0; i < diff_queued_diff.nr; i++)
655 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
656 export_blob(&diff_queued_diff.queue[i]->two->oid);
658 refname = *revision_sources_at(&revision_sources, commit);
660 * FIXME: string_list_remove() below for each ref is overall
661 * O(N^2). Compared to a history walk and diffing trees, this is
662 * just lost in the noise in practice. However, theoretically a
663 * repo may have enough refs for this to become slow.
665 string_list_remove(&extra_refs, refname, 0);
666 if (anonymize) {
667 refname = anonymize_refname(refname);
668 anonymize_ident_line(&committer, &committer_end);
669 anonymize_ident_line(&author, &author_end);
672 mark_next_object(&commit->object);
673 if (anonymize) {
674 reencoded = anonymize_commit_message(message);
675 } else if (encoding) {
676 switch(reencode_mode) {
677 case REENCODE_YES:
678 reencoded = reencode_string(message, "UTF-8", encoding);
679 break;
680 case REENCODE_NO:
681 break;
682 case REENCODE_ABORT:
683 die("Encountered commit-specific encoding %s in commit "
684 "%s; use --reencode=[yes|no] to handle it",
685 encoding, oid_to_hex(&commit->object.oid));
688 if (!commit->parents)
689 printf("reset %s\n", refname);
690 printf("commit %s\nmark :%"PRIu32"\n", refname, last_idnum);
691 if (show_original_ids)
692 printf("original-oid %s\n", oid_to_hex(&commit->object.oid));
693 printf("%.*s\n%.*s\n",
694 (int)(author_end - author), author,
695 (int)(committer_end - committer), committer);
696 if (!reencoded && encoding)
697 printf("encoding %s\n", encoding);
698 printf("data %u\n%s",
699 (unsigned)(reencoded
700 ? strlen(reencoded) : message
701 ? strlen(message) : 0),
702 reencoded ? reencoded : message ? message : "");
703 free(reencoded);
704 repo_unuse_commit_buffer(the_repository, commit, commit_buffer);
706 for (i = 0, p = commit->parents; p; p = p->next) {
707 struct object *obj = &p->item->object;
708 int mark = get_object_mark(obj);
710 if (!mark && !reference_excluded_commits)
711 continue;
712 if (i == 0)
713 printf("from ");
714 else
715 printf("merge ");
716 if (mark)
717 printf(":%d\n", mark);
718 else
719 printf("%s\n",
720 anonymize ?
721 anonymize_oid(oid_to_hex(&obj->oid)) :
722 oid_to_hex(&obj->oid));
723 i++;
726 if (full_tree)
727 printf("deleteall\n");
728 log_tree_diff_flush(rev);
729 string_list_clear(paths_of_changed_objects, 0);
730 rev->diffopt.output_format = saved_output_format;
732 printf("\n");
734 show_progress();
737 static char *anonymize_tag(void *data)
739 static int counter;
740 struct strbuf out = STRBUF_INIT;
741 strbuf_addf(&out, "tag message %d", counter++);
742 return strbuf_detach(&out, NULL);
746 static void handle_tag(const char *name, struct tag *tag)
748 unsigned long size;
749 enum object_type type;
750 char *buf;
751 const char *tagger, *tagger_end, *message;
752 size_t message_size = 0;
753 struct object *tagged;
754 int tagged_mark;
755 struct commit *p;
757 /* Trees have no identifier in fast-export output, thus we have no way
758 * to output tags of trees, tags of tags of trees, etc. Simply omit
759 * such tags.
761 tagged = tag->tagged;
762 while (tagged->type == OBJ_TAG) {
763 tagged = ((struct tag *)tagged)->tagged;
765 if (tagged->type == OBJ_TREE) {
766 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
767 oid_to_hex(&tag->object.oid));
768 return;
771 buf = repo_read_object_file(the_repository, &tag->object.oid, &type,
772 &size);
773 if (!buf)
774 die("could not read tag %s", oid_to_hex(&tag->object.oid));
775 message = memmem(buf, size, "\n\n", 2);
776 if (message) {
777 message += 2;
778 message_size = strlen(message);
780 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
781 if (!tagger) {
782 if (fake_missing_tagger)
783 tagger = "tagger Unspecified Tagger "
784 "<unspecified-tagger> 0 +0000";
785 else
786 tagger = "";
787 tagger_end = tagger + strlen(tagger);
788 } else {
789 tagger++;
790 tagger_end = strchrnul(tagger, '\n');
791 if (anonymize)
792 anonymize_ident_line(&tagger, &tagger_end);
795 if (anonymize) {
796 name = anonymize_refname(name);
797 if (message) {
798 static struct hashmap tags;
799 message = anonymize_str(&tags, anonymize_tag,
800 message, message_size, NULL);
801 message_size = strlen(message);
805 /* handle signed tags */
806 if (message) {
807 const char *signature = strstr(message,
808 "\n-----BEGIN PGP SIGNATURE-----\n");
809 if (signature)
810 switch(signed_tag_mode) {
811 case SIGNED_TAG_ABORT:
812 die("encountered signed tag %s; use "
813 "--signed-tags=<mode> to handle it",
814 oid_to_hex(&tag->object.oid));
815 case WARN:
816 warning("exporting signed tag %s",
817 oid_to_hex(&tag->object.oid));
818 /* fallthru */
819 case VERBATIM:
820 break;
821 case WARN_STRIP:
822 warning("stripping signature from tag %s",
823 oid_to_hex(&tag->object.oid));
824 /* fallthru */
825 case STRIP:
826 message_size = signature + 1 - message;
827 break;
831 /* handle tag->tagged having been filtered out due to paths specified */
832 tagged = tag->tagged;
833 tagged_mark = get_object_mark(tagged);
834 if (!tagged_mark) {
835 switch(tag_of_filtered_mode) {
836 case TAG_FILTERING_ABORT:
837 die("tag %s tags unexported object; use "
838 "--tag-of-filtered-object=<mode> to handle it",
839 oid_to_hex(&tag->object.oid));
840 case DROP:
841 /* Ignore this tag altogether */
842 free(buf);
843 return;
844 case REWRITE:
845 if (tagged->type == OBJ_TAG && !mark_tags) {
846 die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
847 } else if (tagged->type == OBJ_COMMIT) {
848 p = rewrite_commit((struct commit *)tagged);
849 if (!p) {
850 printf("reset %s\nfrom %s\n\n",
851 name, oid_to_hex(null_oid()));
852 free(buf);
853 return;
855 tagged_mark = get_object_mark(&p->object);
856 } else {
857 /* tagged->type is either OBJ_BLOB or OBJ_TAG */
858 tagged_mark = get_object_mark(tagged);
863 if (tagged->type == OBJ_TAG) {
864 printf("reset %s\nfrom %s\n\n",
865 name, oid_to_hex(null_oid()));
867 skip_prefix(name, "refs/tags/", &name);
868 printf("tag %s\n", name);
869 if (mark_tags) {
870 mark_next_object(&tag->object);
871 printf("mark :%"PRIu32"\n", last_idnum);
873 if (tagged_mark)
874 printf("from :%d\n", tagged_mark);
875 else
876 printf("from %s\n", oid_to_hex(&tagged->oid));
878 if (show_original_ids)
879 printf("original-oid %s\n", oid_to_hex(&tag->object.oid));
880 printf("%.*s%sdata %d\n%.*s\n",
881 (int)(tagger_end - tagger), tagger,
882 tagger == tagger_end ? "" : "\n",
883 (int)message_size, (int)message_size, message ? message : "");
884 free(buf);
887 static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
889 switch (e->item->type) {
890 case OBJ_COMMIT:
891 return (struct commit *)e->item;
892 case OBJ_TAG: {
893 struct tag *tag = (struct tag *)e->item;
895 /* handle nested tags */
896 while (tag && tag->object.type == OBJ_TAG) {
897 parse_object(the_repository, &tag->object.oid);
898 string_list_append(&tag_refs, full_name)->util = tag;
899 tag = (struct tag *)tag->tagged;
901 if (!tag)
902 die("Tag %s points nowhere?", e->name);
903 return (struct commit *)tag;
905 default:
906 return NULL;
910 static void get_tags_and_duplicates(struct rev_cmdline_info *info)
912 int i;
914 for (i = 0; i < info->nr; i++) {
915 struct rev_cmdline_entry *e = info->rev + i;
916 struct object_id oid;
917 struct commit *commit;
918 char *full_name;
920 if (e->flags & UNINTERESTING)
921 continue;
923 if (repo_dwim_ref(the_repository, e->name, strlen(e->name),
924 &oid, &full_name, 0) != 1)
925 continue;
927 if (refspecs.nr) {
928 char *private;
929 private = apply_refspecs(&refspecs, full_name);
930 if (private) {
931 free(full_name);
932 full_name = private;
936 commit = get_commit(e, full_name);
937 if (!commit) {
938 warning("%s: Unexpected object of type %s, skipping.",
939 e->name,
940 type_name(e->item->type));
941 continue;
944 switch(commit->object.type) {
945 case OBJ_COMMIT:
946 break;
947 case OBJ_BLOB:
948 export_blob(&commit->object.oid);
949 continue;
950 default: /* OBJ_TAG (nested tags) is already handled */
951 warning("Tag points to object of unexpected type %s, skipping.",
952 type_name(commit->object.type));
953 continue;
957 * Make sure this ref gets properly updated eventually, whether
958 * through a commit or manually at the end.
960 if (e->item->type != OBJ_TAG)
961 string_list_append(&extra_refs, full_name)->util = commit;
963 if (!*revision_sources_at(&revision_sources, commit))
964 *revision_sources_at(&revision_sources, commit) = full_name;
967 string_list_sort(&extra_refs);
968 string_list_remove_duplicates(&extra_refs, 0);
971 static void handle_tags_and_duplicates(struct string_list *extras)
973 struct commit *commit;
974 int i;
976 for (i = extras->nr - 1; i >= 0; i--) {
977 const char *name = extras->items[i].string;
978 struct object *object = extras->items[i].util;
979 int mark;
981 switch (object->type) {
982 case OBJ_TAG:
983 handle_tag(name, (struct tag *)object);
984 break;
985 case OBJ_COMMIT:
986 if (anonymize)
987 name = anonymize_refname(name);
988 /* create refs pointing to already seen commits */
989 commit = rewrite_commit((struct commit *)object);
990 if (!commit) {
992 * Neither this object nor any of its
993 * ancestors touch any relevant paths, so
994 * it has been filtered to nothing. Delete
995 * it.
997 printf("reset %s\nfrom %s\n\n",
998 name, oid_to_hex(null_oid()));
999 continue;
1002 mark = get_object_mark(&commit->object);
1003 if (!mark) {
1005 * Getting here means we have a commit which
1006 * was excluded by a negative refspec (e.g.
1007 * fast-export ^HEAD HEAD). If we are
1008 * referencing excluded commits, set the ref
1009 * to the exact commit. Otherwise, the user
1010 * wants the branch exported but every commit
1011 * in its history to be deleted, which basically
1012 * just means deletion of the ref.
1014 if (!reference_excluded_commits) {
1015 /* delete the ref */
1016 printf("reset %s\nfrom %s\n\n",
1017 name, oid_to_hex(null_oid()));
1018 continue;
1020 /* set ref to commit using oid, not mark */
1021 printf("reset %s\nfrom %s\n\n", name,
1022 oid_to_hex(&commit->object.oid));
1023 continue;
1026 printf("reset %s\nfrom :%d\n\n", name, mark
1028 show_progress();
1029 break;
1034 static void export_marks(char *file)
1036 unsigned int i;
1037 uint32_t mark;
1038 struct decoration_entry *deco = idnums.entries;
1039 FILE *f;
1040 int e = 0;
1042 f = fopen_for_writing(file);
1043 if (!f)
1044 die_errno("Unable to open marks file %s for writing.", file);
1046 for (i = 0; i < idnums.size; i++) {
1047 if (deco->base && deco->base->type == 1) {
1048 mark = ptr_to_mark(deco->decoration);
1049 if (fprintf(f, ":%"PRIu32" %s\n", mark,
1050 oid_to_hex(&deco->base->oid)) < 0) {
1051 e = 1;
1052 break;
1055 deco++;
1058 e |= ferror(f);
1059 e |= fclose(f);
1060 if (e)
1061 error("Unable to write marks file %s.", file);
1064 static void import_marks(char *input_file, int check_exists)
1066 char line[512];
1067 FILE *f;
1068 struct stat sb;
1070 if (check_exists && stat(input_file, &sb))
1071 return;
1073 f = xfopen(input_file, "r");
1074 while (fgets(line, sizeof(line), f)) {
1075 uint32_t mark;
1076 char *line_end, *mark_end;
1077 struct object_id oid;
1078 struct object *object;
1079 struct commit *commit;
1080 enum object_type type;
1082 line_end = strchr(line, '\n');
1083 if (line[0] != ':' || !line_end)
1084 die("corrupt mark line: %s", line);
1085 *line_end = '\0';
1087 mark = strtoumax(line + 1, &mark_end, 10);
1088 if (!mark || mark_end == line + 1
1089 || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
1090 die("corrupt mark line: %s", line);
1092 if (last_idnum < mark)
1093 last_idnum = mark;
1095 type = oid_object_info(the_repository, &oid, NULL);
1096 if (type < 0)
1097 die("object not found: %s", oid_to_hex(&oid));
1099 if (type != OBJ_COMMIT)
1100 /* only commits */
1101 continue;
1103 commit = lookup_commit(the_repository, &oid);
1104 if (!commit)
1105 die("not a commit? can't happen: %s", oid_to_hex(&oid));
1107 object = &commit->object;
1109 if (object->flags & SHOWN)
1110 error("Object %s already has a mark", oid_to_hex(&oid));
1112 mark_object(object, mark);
1114 object->flags |= SHOWN;
1116 fclose(f);
1119 static void handle_deletes(void)
1121 int i;
1122 for (i = 0; i < refspecs.nr; i++) {
1123 struct refspec_item *refspec = &refspecs.items[i];
1124 if (*refspec->src)
1125 continue;
1127 printf("reset %s\nfrom %s\n\n",
1128 refspec->dst, oid_to_hex(null_oid()));
1132 static char *anonymize_seed(void *data)
1134 return xstrdup(data);
1137 static int parse_opt_anonymize_map(const struct option *opt,
1138 const char *arg, int unset)
1140 struct hashmap *map = opt->value;
1141 const char *delim, *value;
1142 size_t keylen;
1144 BUG_ON_OPT_NEG(unset);
1146 delim = strchr(arg, ':');
1147 if (delim) {
1148 keylen = delim - arg;
1149 value = delim + 1;
1150 } else {
1151 keylen = strlen(arg);
1152 value = arg;
1155 if (!keylen || !*value)
1156 return error(_("--anonymize-map token cannot be empty"));
1158 anonymize_str(map, anonymize_seed, arg, keylen, (void *)value);
1160 return 0;
1163 int cmd_fast_export(int argc, const char **argv, const char *prefix)
1165 struct rev_info revs;
1166 struct commit *commit;
1167 char *export_filename = NULL,
1168 *import_filename = NULL,
1169 *import_filename_if_exists = NULL;
1170 uint32_t lastimportid;
1171 struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
1172 struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
1173 struct option options[] = {
1174 OPT_INTEGER(0, "progress", &progress,
1175 N_("show progress after <n> objects")),
1176 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1177 N_("select handling of signed tags"),
1178 parse_opt_signed_tag_mode),
1179 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1180 N_("select handling of tags that tag filtered objects"),
1181 parse_opt_tag_of_filtered_mode),
1182 OPT_CALLBACK(0, "reencode", &reencode_mode, N_("mode"),
1183 N_("select handling of commit messages in an alternate encoding"),
1184 parse_opt_reencode_mode),
1185 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1186 N_("dump marks to this file")),
1187 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1188 N_("import marks from this file")),
1189 OPT_STRING(0, "import-marks-if-exists",
1190 &import_filename_if_exists,
1191 N_("file"),
1192 N_("import marks from this file if it exists")),
1193 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1194 N_("fake a tagger when tags lack one")),
1195 OPT_BOOL(0, "full-tree", &full_tree,
1196 N_("output full tree for each commit")),
1197 OPT_BOOL(0, "use-done-feature", &use_done_feature,
1198 N_("use the done feature to terminate the stream")),
1199 OPT_BOOL(0, "no-data", &no_data, N_("skip output of blob data")),
1200 OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1201 N_("apply refspec to exported refs")),
1202 OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
1203 OPT_CALLBACK_F(0, "anonymize-map", &anonymized_seeds, N_("from:to"),
1204 N_("convert <from> to <to> in anonymized output"),
1205 PARSE_OPT_NONEG, parse_opt_anonymize_map),
1206 OPT_BOOL(0, "reference-excluded-parents",
1207 &reference_excluded_commits, N_("reference parents which are not in fast-export stream by object id")),
1208 OPT_BOOL(0, "show-original-ids", &show_original_ids,
1209 N_("show original object ids of blobs/commits")),
1210 OPT_BOOL(0, "mark-tags", &mark_tags,
1211 N_("label tags with mark ids")),
1213 OPT_END()
1216 if (argc == 1)
1217 usage_with_options (fast_export_usage, options);
1219 /* we handle encodings */
1220 git_config(git_default_config, NULL);
1222 repo_init_revisions(the_repository, &revs, prefix);
1223 init_revision_sources(&revision_sources);
1224 revs.topo_order = 1;
1225 revs.sources = &revision_sources;
1226 revs.rewrite_parents = 1;
1227 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1228 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);
1229 argc = setup_revisions(argc, argv, &revs, NULL);
1230 if (argc > 1)
1231 usage_with_options (fast_export_usage, options);
1233 if (anonymized_seeds.cmpfn && !anonymize)
1234 die(_("the option '%s' requires '%s'"), "--anonymize-map", "--anonymize");
1236 if (refspecs_list.nr) {
1237 int i;
1239 for (i = 0; i < refspecs_list.nr; i++)
1240 refspec_append(&refspecs, refspecs_list.items[i].string);
1242 string_list_clear(&refspecs_list, 1);
1245 if (use_done_feature)
1246 printf("feature done\n");
1248 if (import_filename && import_filename_if_exists)
1249 die(_("options '%s' and '%s' cannot be used together"), "--import-marks", "--import-marks-if-exists");
1250 if (import_filename)
1251 import_marks(import_filename, 0);
1252 else if (import_filename_if_exists)
1253 import_marks(import_filename_if_exists, 1);
1254 lastimportid = last_idnum;
1256 if (import_filename && revs.prune_data.nr)
1257 full_tree = 1;
1259 get_tags_and_duplicates(&revs.cmdline);
1261 if (prepare_revision_walk(&revs))
1262 die("revision walk setup failed");
1264 revs.reverse = 1;
1265 revs.diffopt.format_callback = show_filemodify;
1266 revs.diffopt.format_callback_data = &paths_of_changed_objects;
1267 revs.diffopt.flags.recursive = 1;
1268 revs.diffopt.no_free = 1;
1269 while ((commit = get_revision(&revs)))
1270 handle_commit(commit, &revs, &paths_of_changed_objects);
1272 handle_tags_and_duplicates(&extra_refs);
1273 handle_tags_and_duplicates(&tag_refs);
1274 handle_deletes();
1276 if (export_filename && lastimportid != last_idnum)
1277 export_marks(export_filename);
1279 if (use_done_feature)
1280 printf("done\n");
1282 refspec_clear(&refspecs);
1283 release_revisions(&revs);
1285 return 0;