Merge branch 'en/directory-renames-nothanks-doc-update'
[git/raj.git] / builtin / fast-export.c
blob9e283482efcfa6de0376cc9306061cea149b12df
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 "object-store.h"
12 #include "commit.h"
13 #include "object.h"
14 #include "tag.h"
15 #include "diff.h"
16 #include "diffcore.h"
17 #include "log-tree.h"
18 #include "revision.h"
19 #include "decorate.h"
20 #include "string-list.h"
21 #include "utf8.h"
22 #include "parse-options.h"
23 #include "quote.h"
24 #include "remote.h"
25 #include "blob.h"
26 #include "commit-slab.h"
28 static const char *fast_export_usage[] = {
29 N_("git fast-export [rev-list-opts]"),
30 NULL
33 static int progress;
34 static enum { SIGNED_TAG_ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = SIGNED_TAG_ABORT;
35 static enum { TAG_FILTERING_ABORT, DROP, REWRITE } tag_of_filtered_mode = TAG_FILTERING_ABORT;
36 static int fake_missing_tagger;
37 static int use_done_feature;
38 static int no_data;
39 static int full_tree;
40 static int reference_excluded_commits;
41 static int show_original_ids;
42 static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
43 static struct string_list tag_refs = STRING_LIST_INIT_NODUP;
44 static struct refspec refspecs = REFSPEC_INIT_FETCH;
45 static int anonymize;
46 static struct revision_sources revision_sources;
48 static int parse_opt_signed_tag_mode(const struct option *opt,
49 const char *arg, int unset)
51 if (unset || !strcmp(arg, "abort"))
52 signed_tag_mode = SIGNED_TAG_ABORT;
53 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
54 signed_tag_mode = VERBATIM;
55 else if (!strcmp(arg, "warn"))
56 signed_tag_mode = WARN;
57 else if (!strcmp(arg, "warn-strip"))
58 signed_tag_mode = WARN_STRIP;
59 else if (!strcmp(arg, "strip"))
60 signed_tag_mode = STRIP;
61 else
62 return error("Unknown signed-tags mode: %s", arg);
63 return 0;
66 static int parse_opt_tag_of_filtered_mode(const struct option *opt,
67 const char *arg, int unset)
69 if (unset || !strcmp(arg, "abort"))
70 tag_of_filtered_mode = TAG_FILTERING_ABORT;
71 else if (!strcmp(arg, "drop"))
72 tag_of_filtered_mode = DROP;
73 else if (!strcmp(arg, "rewrite"))
74 tag_of_filtered_mode = REWRITE;
75 else
76 return error("Unknown tag-of-filtered mode: %s", arg);
77 return 0;
80 static struct decoration idnums;
81 static uint32_t last_idnum;
83 static int has_unshown_parent(struct commit *commit)
85 struct commit_list *parent;
87 for (parent = commit->parents; parent; parent = parent->next)
88 if (!(parent->item->object.flags & SHOWN) &&
89 !(parent->item->object.flags & UNINTERESTING))
90 return 1;
91 return 0;
94 struct anonymized_entry {
95 struct hashmap_entry hash;
96 const char *orig;
97 size_t orig_len;
98 const char *anon;
99 size_t anon_len;
102 static int anonymized_entry_cmp(const void *unused_cmp_data,
103 const void *va, const void *vb,
104 const void *unused_keydata)
106 const struct anonymized_entry *a = va, *b = vb;
107 return a->orig_len != b->orig_len ||
108 memcmp(a->orig, b->orig, a->orig_len);
112 * Basically keep a cache of X->Y so that we can repeatedly replace
113 * the same anonymized string with another. The actual generation
114 * is farmed out to the generate function.
116 static const void *anonymize_mem(struct hashmap *map,
117 void *(*generate)(const void *, size_t *),
118 const void *orig, size_t *len)
120 struct anonymized_entry key, *ret;
122 if (!map->cmpfn)
123 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
125 hashmap_entry_init(&key, memhash(orig, *len));
126 key.orig = orig;
127 key.orig_len = *len;
128 ret = hashmap_get(map, &key, NULL);
130 if (!ret) {
131 ret = xmalloc(sizeof(*ret));
132 hashmap_entry_init(&ret->hash, key.hash.hash);
133 ret->orig = xstrdup(orig);
134 ret->orig_len = *len;
135 ret->anon = generate(orig, len);
136 ret->anon_len = *len;
137 hashmap_put(map, ret);
140 *len = ret->anon_len;
141 return ret->anon;
145 * We anonymize each component of a path individually,
146 * so that paths a/b and a/c will share a common root.
147 * The paths are cached via anonymize_mem so that repeated
148 * lookups for "a" will yield the same value.
150 static void anonymize_path(struct strbuf *out, const char *path,
151 struct hashmap *map,
152 void *(*generate)(const void *, size_t *))
154 while (*path) {
155 const char *end_of_component = strchrnul(path, '/');
156 size_t len = end_of_component - path;
157 const char *c = anonymize_mem(map, generate, path, &len);
158 strbuf_add(out, c, len);
159 path = end_of_component;
160 if (*path)
161 strbuf_addch(out, *path++);
165 static inline void *mark_to_ptr(uint32_t mark)
167 return (void *)(uintptr_t)mark;
170 static inline uint32_t ptr_to_mark(void * mark)
172 return (uint32_t)(uintptr_t)mark;
175 static inline void mark_object(struct object *object, uint32_t mark)
177 add_decoration(&idnums, object, mark_to_ptr(mark));
180 static inline void mark_next_object(struct object *object)
182 mark_object(object, ++last_idnum);
185 static int get_object_mark(struct object *object)
187 void *decoration = lookup_decoration(&idnums, object);
188 if (!decoration)
189 return 0;
190 return ptr_to_mark(decoration);
193 static struct commit *rewrite_commit(struct commit *p)
195 for (;;) {
196 if (p->parents && p->parents->next)
197 break;
198 if (p->object.flags & UNINTERESTING)
199 break;
200 if (!(p->object.flags & TREESAME))
201 break;
202 if (!p->parents)
203 return NULL;
204 p = p->parents->item;
206 return p;
209 static void show_progress(void)
211 static int counter = 0;
212 if (!progress)
213 return;
214 if ((++counter % progress) == 0)
215 printf("progress %d objects\n", counter);
219 * Ideally we would want some transformation of the blob data here
220 * that is unreversible, but would still be the same size and have
221 * the same data relationship to other blobs (so that we get the same
222 * delta and packing behavior as the original). But the first and last
223 * requirements there are probably mutually exclusive, so let's take
224 * the easy way out for now, and just generate arbitrary content.
226 * There's no need to cache this result with anonymize_mem, since
227 * we already handle blob content caching with marks.
229 static char *anonymize_blob(unsigned long *size)
231 static int counter;
232 struct strbuf out = STRBUF_INIT;
233 strbuf_addf(&out, "anonymous blob %d", counter++);
234 *size = out.len;
235 return strbuf_detach(&out, NULL);
238 static void export_blob(const struct object_id *oid)
240 unsigned long size;
241 enum object_type type;
242 char *buf;
243 struct object *object;
244 int eaten;
246 if (no_data)
247 return;
249 if (is_null_oid(oid))
250 return;
252 object = lookup_object(the_repository, oid->hash);
253 if (object && object->flags & SHOWN)
254 return;
256 if (anonymize) {
257 buf = anonymize_blob(&size);
258 object = (struct object *)lookup_blob(the_repository, oid);
259 eaten = 0;
260 } else {
261 buf = read_object_file(oid, &type, &size);
262 if (!buf)
263 die("could not read blob %s", oid_to_hex(oid));
264 if (check_object_signature(oid, buf, size, type_name(type)) < 0)
265 die("oid mismatch in blob %s", oid_to_hex(oid));
266 object = parse_object_buffer(the_repository, oid, type,
267 size, buf, &eaten);
270 if (!object)
271 die("Could not read blob %s", oid_to_hex(oid));
273 mark_next_object(object);
275 printf("blob\nmark :%"PRIu32"\n", last_idnum);
276 if (show_original_ids)
277 printf("original-oid %s\n", oid_to_hex(oid));
278 printf("data %"PRIuMAX"\n", (uintmax_t)size);
279 if (size && fwrite(buf, size, 1, stdout) != 1)
280 die_errno("could not write blob '%s'", oid_to_hex(oid));
281 printf("\n");
283 show_progress();
285 object->flags |= SHOWN;
286 if (!eaten)
287 free(buf);
290 static int depth_first(const void *a_, const void *b_)
292 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
293 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
294 const char *name_a, *name_b;
295 int len_a, len_b, len;
296 int cmp;
298 name_a = a->one ? a->one->path : a->two->path;
299 name_b = b->one ? b->one->path : b->two->path;
301 len_a = strlen(name_a);
302 len_b = strlen(name_b);
303 len = (len_a < len_b) ? len_a : len_b;
305 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
306 cmp = memcmp(name_a, name_b, len);
307 if (cmp)
308 return cmp;
309 cmp = len_b - len_a;
310 if (cmp)
311 return cmp;
313 * Move 'R'ename entries last so that all references of the file
314 * appear in the output before it is renamed (e.g., when a file
315 * was copied and renamed in the same commit).
317 return (a->status == 'R') - (b->status == 'R');
320 static void print_path_1(const char *path)
322 int need_quote = quote_c_style(path, NULL, NULL, 0);
323 if (need_quote)
324 quote_c_style(path, NULL, stdout, 0);
325 else if (strchr(path, ' '))
326 printf("\"%s\"", path);
327 else
328 printf("%s", path);
331 static void *anonymize_path_component(const void *path, size_t *len)
333 static int counter;
334 struct strbuf out = STRBUF_INIT;
335 strbuf_addf(&out, "path%d", counter++);
336 return strbuf_detach(&out, len);
339 static void print_path(const char *path)
341 if (!anonymize)
342 print_path_1(path);
343 else {
344 static struct hashmap paths;
345 static struct strbuf anon = STRBUF_INIT;
347 anonymize_path(&anon, path, &paths, anonymize_path_component);
348 print_path_1(anon.buf);
349 strbuf_reset(&anon);
353 static void *generate_fake_oid(const void *old, size_t *len)
355 static uint32_t counter = 1; /* avoid null oid */
356 const unsigned hashsz = the_hash_algo->rawsz;
357 unsigned char *out = xcalloc(hashsz, 1);
358 put_be32(out + hashsz - 4, counter++);
359 return out;
362 static const struct object_id *anonymize_oid(const struct object_id *oid)
364 static struct hashmap objs;
365 size_t len = the_hash_algo->rawsz;
366 return anonymize_mem(&objs, generate_fake_oid, oid, &len);
369 static void show_filemodify(struct diff_queue_struct *q,
370 struct diff_options *options, void *data)
372 int i;
373 struct string_list *changed = data;
376 * Handle files below a directory first, in case they are all deleted
377 * and the directory changes to a file or symlink.
379 QSORT(q->queue, q->nr, depth_first);
381 for (i = 0; i < q->nr; i++) {
382 struct diff_filespec *ospec = q->queue[i]->one;
383 struct diff_filespec *spec = q->queue[i]->two;
385 switch (q->queue[i]->status) {
386 case DIFF_STATUS_DELETED:
387 printf("D ");
388 print_path(spec->path);
389 string_list_insert(changed, spec->path);
390 putchar('\n');
391 break;
393 case DIFF_STATUS_COPIED:
394 case DIFF_STATUS_RENAMED:
396 * If a change in the file corresponding to ospec->path
397 * has been observed, we cannot trust its contents
398 * because the diff is calculated based on the prior
399 * contents, not the current contents. So, declare a
400 * copy or rename only if there was no change observed.
402 if (!string_list_has_string(changed, ospec->path)) {
403 printf("%c ", q->queue[i]->status);
404 print_path(ospec->path);
405 putchar(' ');
406 print_path(spec->path);
407 string_list_insert(changed, spec->path);
408 putchar('\n');
410 if (oideq(&ospec->oid, &spec->oid) &&
411 ospec->mode == spec->mode)
412 break;
414 /* fallthrough */
416 case DIFF_STATUS_TYPE_CHANGED:
417 case DIFF_STATUS_MODIFIED:
418 case DIFF_STATUS_ADDED:
420 * Links refer to objects in another repositories;
421 * output the SHA-1 verbatim.
423 if (no_data || S_ISGITLINK(spec->mode))
424 printf("M %06o %s ", spec->mode,
425 oid_to_hex(anonymize ?
426 anonymize_oid(&spec->oid) :
427 &spec->oid));
428 else {
429 struct object *object = lookup_object(the_repository,
430 spec->oid.hash);
431 printf("M %06o :%d ", spec->mode,
432 get_object_mark(object));
434 print_path(spec->path);
435 string_list_insert(changed, spec->path);
436 putchar('\n');
437 break;
439 default:
440 die("Unexpected comparison status '%c' for %s, %s",
441 q->queue[i]->status,
442 ospec->path ? ospec->path : "none",
443 spec->path ? spec->path : "none");
448 static const char *find_encoding(const char *begin, const char *end)
450 const char *needle = "\nencoding ";
451 char *bol, *eol;
453 bol = memmem(begin, end ? end - begin : strlen(begin),
454 needle, strlen(needle));
455 if (!bol)
456 return git_commit_encoding;
457 bol += strlen(needle);
458 eol = strchrnul(bol, '\n');
459 *eol = '\0';
460 return bol;
463 static void *anonymize_ref_component(const void *old, size_t *len)
465 static int counter;
466 struct strbuf out = STRBUF_INIT;
467 strbuf_addf(&out, "ref%d", counter++);
468 return strbuf_detach(&out, len);
471 static const char *anonymize_refname(const char *refname)
474 * If any of these prefixes is found, we will leave it intact
475 * so that tags remain tags and so forth.
477 static const char *prefixes[] = {
478 "refs/heads/",
479 "refs/tags/",
480 "refs/remotes/",
481 "refs/"
483 static struct hashmap refs;
484 static struct strbuf anon = STRBUF_INIT;
485 int i;
488 * We also leave "master" as a special case, since it does not reveal
489 * anything interesting.
491 if (!strcmp(refname, "refs/heads/master"))
492 return refname;
494 strbuf_reset(&anon);
495 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
496 if (skip_prefix(refname, prefixes[i], &refname)) {
497 strbuf_addstr(&anon, prefixes[i]);
498 break;
502 anonymize_path(&anon, refname, &refs, anonymize_ref_component);
503 return anon.buf;
507 * We do not even bother to cache commit messages, as they are unlikely
508 * to be repeated verbatim, and it is not that interesting when they are.
510 static char *anonymize_commit_message(const char *old)
512 static int counter;
513 return xstrfmt("subject %d\n\nbody\n", counter++);
516 static struct hashmap idents;
517 static void *anonymize_ident(const void *old, size_t *len)
519 static int counter;
520 struct strbuf out = STRBUF_INIT;
521 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
522 counter++;
523 return strbuf_detach(&out, len);
527 * Our strategy here is to anonymize the names and email addresses,
528 * but keep timestamps intact, as they influence things like traversal
529 * order (and by themselves should not be too revealing).
531 static void anonymize_ident_line(const char **beg, const char **end)
533 static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT };
534 static unsigned which_buffer;
536 struct strbuf *out;
537 struct ident_split split;
538 const char *end_of_header;
540 out = &buffers[which_buffer++];
541 which_buffer %= ARRAY_SIZE(buffers);
542 strbuf_reset(out);
544 /* skip "committer", "author", "tagger", etc */
545 end_of_header = strchr(*beg, ' ');
546 if (!end_of_header)
547 BUG("malformed line fed to anonymize_ident_line: %.*s",
548 (int)(*end - *beg), *beg);
549 end_of_header++;
550 strbuf_add(out, *beg, end_of_header - *beg);
552 if (!split_ident_line(&split, end_of_header, *end - end_of_header) &&
553 split.date_begin) {
554 const char *ident;
555 size_t len;
557 len = split.mail_end - split.name_begin;
558 ident = anonymize_mem(&idents, anonymize_ident,
559 split.name_begin, &len);
560 strbuf_add(out, ident, len);
561 strbuf_addch(out, ' ');
562 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
563 } else {
564 strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000");
567 *beg = out->buf;
568 *end = out->buf + out->len;
571 static void handle_commit(struct commit *commit, struct rev_info *rev,
572 struct string_list *paths_of_changed_objects)
574 int saved_output_format = rev->diffopt.output_format;
575 const char *commit_buffer;
576 const char *author, *author_end, *committer, *committer_end;
577 const char *encoding, *message;
578 char *reencoded = NULL;
579 struct commit_list *p;
580 const char *refname;
581 int i;
583 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
585 parse_commit_or_die(commit);
586 commit_buffer = get_commit_buffer(commit, NULL);
587 author = strstr(commit_buffer, "\nauthor ");
588 if (!author)
589 die("could not find author in commit %s",
590 oid_to_hex(&commit->object.oid));
591 author++;
592 author_end = strchrnul(author, '\n');
593 committer = strstr(author_end, "\ncommitter ");
594 if (!committer)
595 die("could not find committer in commit %s",
596 oid_to_hex(&commit->object.oid));
597 committer++;
598 committer_end = strchrnul(committer, '\n');
599 message = strstr(committer_end, "\n\n");
600 encoding = find_encoding(committer_end, message);
601 if (message)
602 message += 2;
604 if (commit->parents &&
605 (get_object_mark(&commit->parents->item->object) != 0 ||
606 reference_excluded_commits) &&
607 !full_tree) {
608 parse_commit_or_die(commit->parents->item);
609 diff_tree_oid(get_commit_tree_oid(commit->parents->item),
610 get_commit_tree_oid(commit), "", &rev->diffopt);
612 else
613 diff_root_tree_oid(get_commit_tree_oid(commit),
614 "", &rev->diffopt);
616 /* Export the referenced blobs, and remember the marks. */
617 for (i = 0; i < diff_queued_diff.nr; i++)
618 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
619 export_blob(&diff_queued_diff.queue[i]->two->oid);
621 refname = *revision_sources_at(&revision_sources, commit);
623 * FIXME: string_list_remove() below for each ref is overall
624 * O(N^2). Compared to a history walk and diffing trees, this is
625 * just lost in the noise in practice. However, theoretically a
626 * repo may have enough refs for this to become slow.
628 string_list_remove(&extra_refs, refname, 0);
629 if (anonymize) {
630 refname = anonymize_refname(refname);
631 anonymize_ident_line(&committer, &committer_end);
632 anonymize_ident_line(&author, &author_end);
635 mark_next_object(&commit->object);
636 if (anonymize)
637 reencoded = anonymize_commit_message(message);
638 else if (!is_encoding_utf8(encoding))
639 reencoded = reencode_string(message, "UTF-8", encoding);
640 if (!commit->parents)
641 printf("reset %s\n", refname);
642 printf("commit %s\nmark :%"PRIu32"\n", refname, last_idnum);
643 if (show_original_ids)
644 printf("original-oid %s\n", oid_to_hex(&commit->object.oid));
645 printf("%.*s\n%.*s\ndata %u\n%s",
646 (int)(author_end - author), author,
647 (int)(committer_end - committer), committer,
648 (unsigned)(reencoded
649 ? strlen(reencoded) : message
650 ? strlen(message) : 0),
651 reencoded ? reencoded : message ? message : "");
652 free(reencoded);
653 unuse_commit_buffer(commit, commit_buffer);
655 for (i = 0, p = commit->parents; p; p = p->next) {
656 struct object *obj = &p->item->object;
657 int mark = get_object_mark(obj);
659 if (!mark && !reference_excluded_commits)
660 continue;
661 if (i == 0)
662 printf("from ");
663 else
664 printf("merge ");
665 if (mark)
666 printf(":%d\n", mark);
667 else
668 printf("%s\n", oid_to_hex(anonymize ?
669 anonymize_oid(&obj->oid) :
670 &obj->oid));
671 i++;
674 if (full_tree)
675 printf("deleteall\n");
676 log_tree_diff_flush(rev);
677 string_list_clear(paths_of_changed_objects, 0);
678 rev->diffopt.output_format = saved_output_format;
680 printf("\n");
682 show_progress();
685 static void *anonymize_tag(const void *old, size_t *len)
687 static int counter;
688 struct strbuf out = STRBUF_INIT;
689 strbuf_addf(&out, "tag message %d", counter++);
690 return strbuf_detach(&out, len);
693 static void handle_tail(struct object_array *commits, struct rev_info *revs,
694 struct string_list *paths_of_changed_objects)
696 struct commit *commit;
697 while (commits->nr) {
698 commit = (struct commit *)object_array_pop(commits);
699 if (has_unshown_parent(commit)) {
700 /* Queue again, to be handled later */
701 add_object_array(&commit->object, NULL, commits);
702 return;
704 handle_commit(commit, revs, paths_of_changed_objects);
708 static void handle_tag(const char *name, struct tag *tag)
710 unsigned long size;
711 enum object_type type;
712 char *buf;
713 const char *tagger, *tagger_end, *message;
714 size_t message_size = 0;
715 struct object *tagged;
716 int tagged_mark;
717 struct commit *p;
719 /* Trees have no identifier in fast-export output, thus we have no way
720 * to output tags of trees, tags of tags of trees, etc. Simply omit
721 * such tags.
723 tagged = tag->tagged;
724 while (tagged->type == OBJ_TAG) {
725 tagged = ((struct tag *)tagged)->tagged;
727 if (tagged->type == OBJ_TREE) {
728 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
729 oid_to_hex(&tag->object.oid));
730 return;
733 buf = read_object_file(&tag->object.oid, &type, &size);
734 if (!buf)
735 die("could not read tag %s", oid_to_hex(&tag->object.oid));
736 message = memmem(buf, size, "\n\n", 2);
737 if (message) {
738 message += 2;
739 message_size = strlen(message);
741 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
742 if (!tagger) {
743 if (fake_missing_tagger)
744 tagger = "tagger Unspecified Tagger "
745 "<unspecified-tagger> 0 +0000";
746 else
747 tagger = "";
748 tagger_end = tagger + strlen(tagger);
749 } else {
750 tagger++;
751 tagger_end = strchrnul(tagger, '\n');
752 if (anonymize)
753 anonymize_ident_line(&tagger, &tagger_end);
756 if (anonymize) {
757 name = anonymize_refname(name);
758 if (message) {
759 static struct hashmap tags;
760 message = anonymize_mem(&tags, anonymize_tag,
761 message, &message_size);
765 /* handle signed tags */
766 if (message) {
767 const char *signature = strstr(message,
768 "\n-----BEGIN PGP SIGNATURE-----\n");
769 if (signature)
770 switch(signed_tag_mode) {
771 case SIGNED_TAG_ABORT:
772 die("encountered signed tag %s; use "
773 "--signed-tags=<mode> to handle it",
774 oid_to_hex(&tag->object.oid));
775 case WARN:
776 warning("exporting signed tag %s",
777 oid_to_hex(&tag->object.oid));
778 /* fallthru */
779 case VERBATIM:
780 break;
781 case WARN_STRIP:
782 warning("stripping signature from tag %s",
783 oid_to_hex(&tag->object.oid));
784 /* fallthru */
785 case STRIP:
786 message_size = signature + 1 - message;
787 break;
791 /* handle tag->tagged having been filtered out due to paths specified */
792 tagged = tag->tagged;
793 tagged_mark = get_object_mark(tagged);
794 if (!tagged_mark) {
795 switch(tag_of_filtered_mode) {
796 case TAG_FILTERING_ABORT:
797 die("tag %s tags unexported object; use "
798 "--tag-of-filtered-object=<mode> to handle it",
799 oid_to_hex(&tag->object.oid));
800 case DROP:
801 /* Ignore this tag altogether */
802 free(buf);
803 return;
804 case REWRITE:
805 if (tagged->type != OBJ_COMMIT) {
806 die("tag %s tags unexported %s!",
807 oid_to_hex(&tag->object.oid),
808 type_name(tagged->type));
810 p = rewrite_commit((struct commit *)tagged);
811 if (!p) {
812 printf("reset %s\nfrom %s\n\n",
813 name, oid_to_hex(&null_oid));
814 free(buf);
815 return;
817 tagged_mark = get_object_mark(&p->object);
821 if (starts_with(name, "refs/tags/"))
822 name += 10;
823 printf("tag %s\nfrom :%d\n", name, tagged_mark);
824 if (show_original_ids)
825 printf("original-oid %s\n", oid_to_hex(&tag->object.oid));
826 printf("%.*s%sdata %d\n%.*s\n",
827 (int)(tagger_end - tagger), tagger,
828 tagger == tagger_end ? "" : "\n",
829 (int)message_size, (int)message_size, message ? message : "");
830 free(buf);
833 static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
835 switch (e->item->type) {
836 case OBJ_COMMIT:
837 return (struct commit *)e->item;
838 case OBJ_TAG: {
839 struct tag *tag = (struct tag *)e->item;
841 /* handle nested tags */
842 while (tag && tag->object.type == OBJ_TAG) {
843 parse_object(the_repository, &tag->object.oid);
844 string_list_append(&tag_refs, full_name)->util = tag;
845 tag = (struct tag *)tag->tagged;
847 if (!tag)
848 die("Tag %s points nowhere?", e->name);
849 return (struct commit *)tag;
850 break;
852 default:
853 return NULL;
857 static void get_tags_and_duplicates(struct rev_cmdline_info *info)
859 int i;
861 for (i = 0; i < info->nr; i++) {
862 struct rev_cmdline_entry *e = info->rev + i;
863 struct object_id oid;
864 struct commit *commit;
865 char *full_name;
867 if (e->flags & UNINTERESTING)
868 continue;
870 if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1)
871 continue;
873 if (refspecs.nr) {
874 char *private;
875 private = apply_refspecs(&refspecs, full_name);
876 if (private) {
877 free(full_name);
878 full_name = private;
882 commit = get_commit(e, full_name);
883 if (!commit) {
884 warning("%s: Unexpected object of type %s, skipping.",
885 e->name,
886 type_name(e->item->type));
887 continue;
890 switch(commit->object.type) {
891 case OBJ_COMMIT:
892 break;
893 case OBJ_BLOB:
894 export_blob(&commit->object.oid);
895 continue;
896 default: /* OBJ_TAG (nested tags) is already handled */
897 warning("Tag points to object of unexpected type %s, skipping.",
898 type_name(commit->object.type));
899 continue;
903 * Make sure this ref gets properly updated eventually, whether
904 * through a commit or manually at the end.
906 if (e->item->type != OBJ_TAG)
907 string_list_append(&extra_refs, full_name)->util = commit;
909 if (!*revision_sources_at(&revision_sources, commit))
910 *revision_sources_at(&revision_sources, commit) = full_name;
913 string_list_sort(&extra_refs);
914 string_list_remove_duplicates(&extra_refs, 0);
917 static void handle_tags_and_duplicates(struct string_list *extras)
919 struct commit *commit;
920 int i;
922 for (i = extras->nr - 1; i >= 0; i--) {
923 const char *name = extras->items[i].string;
924 struct object *object = extras->items[i].util;
925 int mark;
927 switch (object->type) {
928 case OBJ_TAG:
929 handle_tag(name, (struct tag *)object);
930 break;
931 case OBJ_COMMIT:
932 if (anonymize)
933 name = anonymize_refname(name);
934 /* create refs pointing to already seen commits */
935 commit = rewrite_commit((struct commit *)object);
936 if (!commit) {
938 * Neither this object nor any of its
939 * ancestors touch any relevant paths, so
940 * it has been filtered to nothing. Delete
941 * it.
943 printf("reset %s\nfrom %s\n\n",
944 name, oid_to_hex(&null_oid));
945 continue;
948 mark = get_object_mark(&commit->object);
949 if (!mark) {
951 * Getting here means we have a commit which
952 * was excluded by a negative refspec (e.g.
953 * fast-export ^master master). If we are
954 * referencing excluded commits, set the ref
955 * to the exact commit. Otherwise, the user
956 * wants the branch exported but every commit
957 * in its history to be deleted, which basically
958 * just means deletion of the ref.
960 if (!reference_excluded_commits) {
961 /* delete the ref */
962 printf("reset %s\nfrom %s\n\n",
963 name, oid_to_hex(&null_oid));
964 continue;
966 /* set ref to commit using oid, not mark */
967 printf("reset %s\nfrom %s\n\n", name,
968 oid_to_hex(&commit->object.oid));
969 continue;
972 printf("reset %s\nfrom :%d\n\n", name, mark
974 show_progress();
975 break;
980 static void export_marks(char *file)
982 unsigned int i;
983 uint32_t mark;
984 struct decoration_entry *deco = idnums.entries;
985 FILE *f;
986 int e = 0;
988 f = fopen_for_writing(file);
989 if (!f)
990 die_errno("Unable to open marks file %s for writing.", file);
992 for (i = 0; i < idnums.size; i++) {
993 if (deco->base && deco->base->type == 1) {
994 mark = ptr_to_mark(deco->decoration);
995 if (fprintf(f, ":%"PRIu32" %s\n", mark,
996 oid_to_hex(&deco->base->oid)) < 0) {
997 e = 1;
998 break;
1001 deco++;
1004 e |= ferror(f);
1005 e |= fclose(f);
1006 if (e)
1007 error("Unable to write marks file %s.", file);
1010 static void import_marks(char *input_file)
1012 char line[512];
1013 FILE *f = xfopen(input_file, "r");
1015 while (fgets(line, sizeof(line), f)) {
1016 uint32_t mark;
1017 char *line_end, *mark_end;
1018 struct object_id oid;
1019 struct object *object;
1020 struct commit *commit;
1021 enum object_type type;
1023 line_end = strchr(line, '\n');
1024 if (line[0] != ':' || !line_end)
1025 die("corrupt mark line: %s", line);
1026 *line_end = '\0';
1028 mark = strtoumax(line + 1, &mark_end, 10);
1029 if (!mark || mark_end == line + 1
1030 || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
1031 die("corrupt mark line: %s", line);
1033 if (last_idnum < mark)
1034 last_idnum = mark;
1036 type = oid_object_info(the_repository, &oid, NULL);
1037 if (type < 0)
1038 die("object not found: %s", oid_to_hex(&oid));
1040 if (type != OBJ_COMMIT)
1041 /* only commits */
1042 continue;
1044 commit = lookup_commit(the_repository, &oid);
1045 if (!commit)
1046 die("not a commit? can't happen: %s", oid_to_hex(&oid));
1048 object = &commit->object;
1050 if (object->flags & SHOWN)
1051 error("Object %s already has a mark", oid_to_hex(&oid));
1053 mark_object(object, mark);
1055 object->flags |= SHOWN;
1057 fclose(f);
1060 static void handle_deletes(void)
1062 int i;
1063 for (i = 0; i < refspecs.nr; i++) {
1064 struct refspec_item *refspec = &refspecs.items[i];
1065 if (*refspec->src)
1066 continue;
1068 printf("reset %s\nfrom %s\n\n",
1069 refspec->dst, oid_to_hex(&null_oid));
1073 int cmd_fast_export(int argc, const char **argv, const char *prefix)
1075 struct rev_info revs;
1076 struct object_array commits = OBJECT_ARRAY_INIT;
1077 struct commit *commit;
1078 char *export_filename = NULL, *import_filename = NULL;
1079 uint32_t lastimportid;
1080 struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
1081 struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
1082 struct option options[] = {
1083 OPT_INTEGER(0, "progress", &progress,
1084 N_("show progress after <n> objects")),
1085 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1086 N_("select handling of signed tags"),
1087 parse_opt_signed_tag_mode),
1088 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1089 N_("select handling of tags that tag filtered objects"),
1090 parse_opt_tag_of_filtered_mode),
1091 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1092 N_("Dump marks to this file")),
1093 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1094 N_("Import marks from this file")),
1095 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1096 N_("Fake a tagger when tags lack one")),
1097 OPT_BOOL(0, "full-tree", &full_tree,
1098 N_("Output full tree for each commit")),
1099 OPT_BOOL(0, "use-done-feature", &use_done_feature,
1100 N_("Use the done feature to terminate the stream")),
1101 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
1102 OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1103 N_("Apply refspec to exported refs")),
1104 OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
1105 OPT_BOOL(0, "reference-excluded-parents",
1106 &reference_excluded_commits, N_("Reference parents which are not in fast-export stream by object id")),
1107 OPT_BOOL(0, "show-original-ids", &show_original_ids,
1108 N_("Show original object ids of blobs/commits")),
1110 OPT_END()
1113 if (argc == 1)
1114 usage_with_options (fast_export_usage, options);
1116 /* we handle encodings */
1117 git_config(git_default_config, NULL);
1119 repo_init_revisions(the_repository, &revs, prefix);
1120 init_revision_sources(&revision_sources);
1121 revs.topo_order = 1;
1122 revs.sources = &revision_sources;
1123 revs.rewrite_parents = 1;
1124 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1125 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
1126 argc = setup_revisions(argc, argv, &revs, NULL);
1127 if (argc > 1)
1128 usage_with_options (fast_export_usage, options);
1130 if (refspecs_list.nr) {
1131 int i;
1133 for (i = 0; i < refspecs_list.nr; i++)
1134 refspec_append(&refspecs, refspecs_list.items[i].string);
1136 string_list_clear(&refspecs_list, 1);
1139 if (use_done_feature)
1140 printf("feature done\n");
1142 if (import_filename)
1143 import_marks(import_filename);
1144 lastimportid = last_idnum;
1146 if (import_filename && revs.prune_data.nr)
1147 full_tree = 1;
1149 get_tags_and_duplicates(&revs.cmdline);
1151 if (prepare_revision_walk(&revs))
1152 die("revision walk setup failed");
1153 revs.diffopt.format_callback = show_filemodify;
1154 revs.diffopt.format_callback_data = &paths_of_changed_objects;
1155 revs.diffopt.flags.recursive = 1;
1156 while ((commit = get_revision(&revs))) {
1157 if (has_unshown_parent(commit)) {
1158 add_object_array(&commit->object, NULL, &commits);
1160 else {
1161 handle_commit(commit, &revs, &paths_of_changed_objects);
1162 handle_tail(&commits, &revs, &paths_of_changed_objects);
1166 handle_tags_and_duplicates(&extra_refs);
1167 handle_tags_and_duplicates(&tag_refs);
1168 handle_deletes();
1170 if (export_filename && lastimportid != last_idnum)
1171 export_marks(export_filename);
1173 if (use_done_feature)
1174 printf("done\n");
1176 refspec_clear(&refspecs);
1178 return 0;