1/*
2 * "git fast-export" builtin command
3 *
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"
27
28static const char *fast_export_usage[] = {
29 N_("git fast-export [rev-list-opts]"),
30 NULL
31};
32
33static int progress;
34static enum { SIGNED_TAG_ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = SIGNED_TAG_ABORT;
35static enum { TAG_FILTERING_ABORT, DROP, REWRITE } tag_of_filtered_mode = TAG_FILTERING_ABORT;
36static int fake_missing_tagger;
37static int use_done_feature;
38static int no_data;
39static int full_tree;
40static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
41static struct refspec refspecs = REFSPEC_INIT_FETCH;
42static int anonymize;
43static struct revision_sources revision_sources;
44
45static int parse_opt_signed_tag_mode(const struct option *opt,
46 const char *arg, int unset)
47{
48 if (unset || !strcmp(arg, "abort"))
49 signed_tag_mode = SIGNED_TAG_ABORT;
50 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
51 signed_tag_mode = VERBATIM;
52 else if (!strcmp(arg, "warn"))
53 signed_tag_mode = WARN;
54 else if (!strcmp(arg, "warn-strip"))
55 signed_tag_mode = WARN_STRIP;
56 else if (!strcmp(arg, "strip"))
57 signed_tag_mode = STRIP;
58 else
59 return error("Unknown signed-tags mode: %s", arg);
60 return 0;
61}
62
63static int parse_opt_tag_of_filtered_mode(const struct option *opt,
64 const char *arg, int unset)
65{
66 if (unset || !strcmp(arg, "abort"))
67 tag_of_filtered_mode = TAG_FILTERING_ABORT;
68 else if (!strcmp(arg, "drop"))
69 tag_of_filtered_mode = DROP;
70 else if (!strcmp(arg, "rewrite"))
71 tag_of_filtered_mode = REWRITE;
72 else
73 return error("Unknown tag-of-filtered mode: %s", arg);
74 return 0;
75}
76
77static struct decoration idnums;
78static uint32_t last_idnum;
79
80static int has_unshown_parent(struct commit *commit)
81{
82 struct commit_list *parent;
83
84 for (parent = commit->parents; parent; parent = parent->next)
85 if (!(parent->item->object.flags & SHOWN) &&
86 !(parent->item->object.flags & UNINTERESTING))
87 return 1;
88 return 0;
89}
90
91struct anonymized_entry {
92 struct hashmap_entry hash;
93 const char *orig;
94 size_t orig_len;
95 const char *anon;
96 size_t anon_len;
97};
98
99static int anonymized_entry_cmp(const void *unused_cmp_data,
100 const void *va, const void *vb,
101 const void *unused_keydata)
102{
103 const struct anonymized_entry *a = va, *b = vb;
104 return a->orig_len != b->orig_len ||
105 memcmp(a->orig, b->orig, a->orig_len);
106}
107
108/*
109 * Basically keep a cache of X->Y so that we can repeatedly replace
110 * the same anonymized string with another. The actual generation
111 * is farmed out to the generate function.
112 */
113static const void *anonymize_mem(struct hashmap *map,
114 void *(*generate)(const void *, size_t *),
115 const void *orig, size_t *len)
116{
117 struct anonymized_entry key, *ret;
118
119 if (!map->cmpfn)
120 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
121
122 hashmap_entry_init(&key, memhash(orig, *len));
123 key.orig = orig;
124 key.orig_len = *len;
125 ret = hashmap_get(map, &key, NULL);
126
127 if (!ret) {
128 ret = xmalloc(sizeof(*ret));
129 hashmap_entry_init(&ret->hash, key.hash.hash);
130 ret->orig = xstrdup(orig);
131 ret->orig_len = *len;
132 ret->anon = generate(orig, len);
133 ret->anon_len = *len;
134 hashmap_put(map, ret);
135 }
136
137 *len = ret->anon_len;
138 return ret->anon;
139}
140
141/*
142 * We anonymize each component of a path individually,
143 * so that paths a/b and a/c will share a common root.
144 * The paths are cached via anonymize_mem so that repeated
145 * lookups for "a" will yield the same value.
146 */
147static void anonymize_path(struct strbuf *out, const char *path,
148 struct hashmap *map,
149 void *(*generate)(const void *, size_t *))
150{
151 while (*path) {
152 const char *end_of_component = strchrnul(path, '/');
153 size_t len = end_of_component - path;
154 const char *c = anonymize_mem(map, generate, path, &len);
155 strbuf_add(out, c, len);
156 path = end_of_component;
157 if (*path)
158 strbuf_addch(out, *path++);
159 }
160}
161
162static inline void *mark_to_ptr(uint32_t mark)
163{
164 return (void *)(uintptr_t)mark;
165}
166
167static inline uint32_t ptr_to_mark(void * mark)
168{
169 return (uint32_t)(uintptr_t)mark;
170}
171
172static inline void mark_object(struct object *object, uint32_t mark)
173{
174 add_decoration(&idnums, object, mark_to_ptr(mark));
175}
176
177static inline void mark_next_object(struct object *object)
178{
179 mark_object(object, ++last_idnum);
180}
181
182static int get_object_mark(struct object *object)
183{
184 void *decoration = lookup_decoration(&idnums, object);
185 if (!decoration)
186 return 0;
187 return ptr_to_mark(decoration);
188}
189
190static struct commit *rewrite_commit(struct commit *p)
191{
192 for (;;) {
193 if (p->parents && p->parents->next)
194 break;
195 if (p->object.flags & UNINTERESTING)
196 break;
197 if (!(p->object.flags & TREESAME))
198 break;
199 if (!p->parents)
200 return NULL;
201 p = p->parents->item;
202 }
203 return p;
204}
205
206static void show_progress(void)
207{
208 static int counter = 0;
209 if (!progress)
210 return;
211 if ((++counter % progress) == 0)
212 printf("progress %d objects\n", counter);
213}
214
215/*
216 * Ideally we would want some transformation of the blob data here
217 * that is unreversible, but would still be the same size and have
218 * the same data relationship to other blobs (so that we get the same
219 * delta and packing behavior as the original). But the first and last
220 * requirements there are probably mutually exclusive, so let's take
221 * the easy way out for now, and just generate arbitrary content.
222 *
223 * There's no need to cache this result with anonymize_mem, since
224 * we already handle blob content caching with marks.
225 */
226static char *anonymize_blob(unsigned long *size)
227{
228 static int counter;
229 struct strbuf out = STRBUF_INIT;
230 strbuf_addf(&out, "anonymous blob %d", counter++);
231 *size = out.len;
232 return strbuf_detach(&out, NULL);
233}
234
235static void export_blob(const struct object_id *oid)
236{
237 unsigned long size;
238 enum object_type type;
239 char *buf;
240 struct object *object;
241 int eaten;
242
243 if (no_data)
244 return;
245
246 if (is_null_oid(oid))
247 return;
248
249 object = lookup_object(the_repository, oid->hash);
250 if (object && object->flags & SHOWN)
251 return;
252
253 if (anonymize) {
254 buf = anonymize_blob(&size);
255 object = (struct object *)lookup_blob(the_repository, oid);
256 eaten = 0;
257 } else {
258 buf = read_object_file(oid, &type, &size);
259 if (!buf)
260 die("could not read blob %s", oid_to_hex(oid));
261 if (check_object_signature(oid, buf, size, type_name(type)) < 0)
262 die("oid mismatch in blob %s", oid_to_hex(oid));
263 object = parse_object_buffer(the_repository, oid, type,
264 size, buf, &eaten);
265 }
266
267 if (!object)
268 die("Could not read blob %s", oid_to_hex(oid));
269
270 mark_next_object(object);
271
272 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
273 if (size && fwrite(buf, size, 1, stdout) != 1)
274 die_errno("could not write blob '%s'", oid_to_hex(oid));
275 printf("\n");
276
277 show_progress();
278
279 object->flags |= SHOWN;
280 if (!eaten)
281 free(buf);
282}
283
284static int depth_first(const void *a_, const void *b_)
285{
286 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
287 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
288 const char *name_a, *name_b;
289 int len_a, len_b, len;
290 int cmp;
291
292 name_a = a->one ? a->one->path : a->two->path;
293 name_b = b->one ? b->one->path : b->two->path;
294
295 len_a = strlen(name_a);
296 len_b = strlen(name_b);
297 len = (len_a < len_b) ? len_a : len_b;
298
299 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
300 cmp = memcmp(name_a, name_b, len);
301 if (cmp)
302 return cmp;
303 cmp = len_b - len_a;
304 if (cmp)
305 return cmp;
306 /*
307 * Move 'R'ename entries last so that all references of the file
308 * appear in the output before it is renamed (e.g., when a file
309 * was copied and renamed in the same commit).
310 */
311 return (a->status == 'R') - (b->status == 'R');
312}
313
314static void print_path_1(const char *path)
315{
316 int need_quote = quote_c_style(path, NULL, NULL, 0);
317 if (need_quote)
318 quote_c_style(path, NULL, stdout, 0);
319 else if (strchr(path, ' '))
320 printf("\"%s\"", path);
321 else
322 printf("%s", path);
323}
324
325static void *anonymize_path_component(const void *path, size_t *len)
326{
327 static int counter;
328 struct strbuf out = STRBUF_INIT;
329 strbuf_addf(&out, "path%d", counter++);
330 return strbuf_detach(&out, len);
331}
332
333static void print_path(const char *path)
334{
335 if (!anonymize)
336 print_path_1(path);
337 else {
338 static struct hashmap paths;
339 static struct strbuf anon = STRBUF_INIT;
340
341 anonymize_path(&anon, path, &paths, anonymize_path_component);
342 print_path_1(anon.buf);
343 strbuf_reset(&anon);
344 }
345}
346
347static void *generate_fake_oid(const void *old, size_t *len)
348{
349 static uint32_t counter = 1; /* avoid null oid */
350 const unsigned hashsz = the_hash_algo->rawsz;
351 unsigned char *out = xcalloc(hashsz, 1);
352 put_be32(out + hashsz - 4, counter++);
353 return out;
354}
355
356static const struct object_id *anonymize_oid(const struct object_id *oid)
357{
358 static struct hashmap objs;
359 size_t len = the_hash_algo->rawsz;
360 return anonymize_mem(&objs, generate_fake_oid, oid, &len);
361}
362
363static void show_filemodify(struct diff_queue_struct *q,
364 struct diff_options *options, void *data)
365{
366 int i;
367 struct string_list *changed = data;
368
369 /*
370 * Handle files below a directory first, in case they are all deleted
371 * and the directory changes to a file or symlink.
372 */
373 QSORT(q->queue, q->nr, depth_first);
374
375 for (i = 0; i < q->nr; i++) {
376 struct diff_filespec *ospec = q->queue[i]->one;
377 struct diff_filespec *spec = q->queue[i]->two;
378
379 switch (q->queue[i]->status) {
380 case DIFF_STATUS_DELETED:
381 printf("D ");
382 print_path(spec->path);
383 string_list_insert(changed, spec->path);
384 putchar('\n');
385 break;
386
387 case DIFF_STATUS_COPIED:
388 case DIFF_STATUS_RENAMED:
389 /*
390 * If a change in the file corresponding to ospec->path
391 * has been observed, we cannot trust its contents
392 * because the diff is calculated based on the prior
393 * contents, not the current contents. So, declare a
394 * copy or rename only if there was no change observed.
395 */
396 if (!string_list_has_string(changed, ospec->path)) {
397 printf("%c ", q->queue[i]->status);
398 print_path(ospec->path);
399 putchar(' ');
400 print_path(spec->path);
401 string_list_insert(changed, spec->path);
402 putchar('\n');
403
404 if (oideq(&ospec->oid, &spec->oid) &&
405 ospec->mode == spec->mode)
406 break;
407 }
408 /* fallthrough */
409
410 case DIFF_STATUS_TYPE_CHANGED:
411 case DIFF_STATUS_MODIFIED:
412 case DIFF_STATUS_ADDED:
413 /*
414 * Links refer to objects in another repositories;
415 * output the SHA-1 verbatim.
416 */
417 if (no_data || S_ISGITLINK(spec->mode))
418 printf("M %06o %s ", spec->mode,
419 oid_to_hex(anonymize ?
420 anonymize_oid(&spec->oid) :
421 &spec->oid));
422 else {
423 struct object *object = lookup_object(the_repository,
424 spec->oid.hash);
425 printf("M %06o :%d ", spec->mode,
426 get_object_mark(object));
427 }
428 print_path(spec->path);
429 string_list_insert(changed, spec->path);
430 putchar('\n');
431 break;
432
433 default:
434 die("Unexpected comparison status '%c' for %s, %s",
435 q->queue[i]->status,
436 ospec->path ? ospec->path : "none",
437 spec->path ? spec->path : "none");
438 }
439 }
440}
441
442static const char *find_encoding(const char *begin, const char *end)
443{
444 const char *needle = "\nencoding ";
445 char *bol, *eol;
446
447 bol = memmem(begin, end ? end - begin : strlen(begin),
448 needle, strlen(needle));
449 if (!bol)
450 return git_commit_encoding;
451 bol += strlen(needle);
452 eol = strchrnul(bol, '\n');
453 *eol = '\0';
454 return bol;
455}
456
457static void *anonymize_ref_component(const void *old, size_t *len)
458{
459 static int counter;
460 struct strbuf out = STRBUF_INIT;
461 strbuf_addf(&out, "ref%d", counter++);
462 return strbuf_detach(&out, len);
463}
464
465static const char *anonymize_refname(const char *refname)
466{
467 /*
468 * If any of these prefixes is found, we will leave it intact
469 * so that tags remain tags and so forth.
470 */
471 static const char *prefixes[] = {
472 "refs/heads/",
473 "refs/tags/",
474 "refs/remotes/",
475 "refs/"
476 };
477 static struct hashmap refs;
478 static struct strbuf anon = STRBUF_INIT;
479 int i;
480
481 /*
482 * We also leave "master" as a special case, since it does not reveal
483 * anything interesting.
484 */
485 if (!strcmp(refname, "refs/heads/master"))
486 return refname;
487
488 strbuf_reset(&anon);
489 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
490 if (skip_prefix(refname, prefixes[i], &refname)) {
491 strbuf_addstr(&anon, prefixes[i]);
492 break;
493 }
494 }
495
496 anonymize_path(&anon, refname, &refs, anonymize_ref_component);
497 return anon.buf;
498}
499
500/*
501 * We do not even bother to cache commit messages, as they are unlikely
502 * to be repeated verbatim, and it is not that interesting when they are.
503 */
504static char *anonymize_commit_message(const char *old)
505{
506 static int counter;
507 return xstrfmt("subject %d\n\nbody\n", counter++);
508}
509
510static struct hashmap idents;
511static void *anonymize_ident(const void *old, size_t *len)
512{
513 static int counter;
514 struct strbuf out = STRBUF_INIT;
515 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
516 counter++;
517 return strbuf_detach(&out, len);
518}
519
520/*
521 * Our strategy here is to anonymize the names and email addresses,
522 * but keep timestamps intact, as they influence things like traversal
523 * order (and by themselves should not be too revealing).
524 */
525static void anonymize_ident_line(const char **beg, const char **end)
526{
527 static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT };
528 static unsigned which_buffer;
529
530 struct strbuf *out;
531 struct ident_split split;
532 const char *end_of_header;
533
534 out = &buffers[which_buffer++];
535 which_buffer %= ARRAY_SIZE(buffers);
536 strbuf_reset(out);
537
538 /* skip "committer", "author", "tagger", etc */
539 end_of_header = strchr(*beg, ' ');
540 if (!end_of_header)
541 BUG("malformed line fed to anonymize_ident_line: %.*s",
542 (int)(*end - *beg), *beg);
543 end_of_header++;
544 strbuf_add(out, *beg, end_of_header - *beg);
545
546 if (!split_ident_line(&split, end_of_header, *end - end_of_header) &&
547 split.date_begin) {
548 const char *ident;
549 size_t len;
550
551 len = split.mail_end - split.name_begin;
552 ident = anonymize_mem(&idents, anonymize_ident,
553 split.name_begin, &len);
554 strbuf_add(out, ident, len);
555 strbuf_addch(out, ' ');
556 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
557 } else {
558 strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000");
559 }
560
561 *beg = out->buf;
562 *end = out->buf + out->len;
563}
564
565static void handle_commit(struct commit *commit, struct rev_info *rev,
566 struct string_list *paths_of_changed_objects)
567{
568 int saved_output_format = rev->diffopt.output_format;
569 const char *commit_buffer;
570 const char *author, *author_end, *committer, *committer_end;
571 const char *encoding, *message;
572 char *reencoded = NULL;
573 struct commit_list *p;
574 const char *refname;
575 int i;
576
577 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
578
579 parse_commit_or_die(commit);
580 commit_buffer = get_commit_buffer(commit, NULL);
581 author = strstr(commit_buffer, "\nauthor ");
582 if (!author)
583 die("could not find author in commit %s",
584 oid_to_hex(&commit->object.oid));
585 author++;
586 author_end = strchrnul(author, '\n');
587 committer = strstr(author_end, "\ncommitter ");
588 if (!committer)
589 die("could not find committer in commit %s",
590 oid_to_hex(&commit->object.oid));
591 committer++;
592 committer_end = strchrnul(committer, '\n');
593 message = strstr(committer_end, "\n\n");
594 encoding = find_encoding(committer_end, message);
595 if (message)
596 message += 2;
597
598 if (commit->parents &&
599 get_object_mark(&commit->parents->item->object) != 0 &&
600 !full_tree) {
601 parse_commit_or_die(commit->parents->item);
602 diff_tree_oid(get_commit_tree_oid(commit->parents->item),
603 get_commit_tree_oid(commit), "", &rev->diffopt);
604 }
605 else
606 diff_root_tree_oid(get_commit_tree_oid(commit),
607 "", &rev->diffopt);
608
609 /* Export the referenced blobs, and remember the marks. */
610 for (i = 0; i < diff_queued_diff.nr; i++)
611 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
612 export_blob(&diff_queued_diff.queue[i]->two->oid);
613
614 refname = *revision_sources_at(&revision_sources, commit);
615 if (anonymize) {
616 refname = anonymize_refname(refname);
617 anonymize_ident_line(&committer, &committer_end);
618 anonymize_ident_line(&author, &author_end);
619 }
620
621 mark_next_object(&commit->object);
622 if (anonymize)
623 reencoded = anonymize_commit_message(message);
624 else if (!is_encoding_utf8(encoding))
625 reencoded = reencode_string(message, "UTF-8", encoding);
626 if (!commit->parents)
627 printf("reset %s\n", refname);
628 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
629 refname, last_idnum,
630 (int)(author_end - author), author,
631 (int)(committer_end - committer), committer,
632 (unsigned)(reencoded
633 ? strlen(reencoded) : message
634 ? strlen(message) : 0),
635 reencoded ? reencoded : message ? message : "");
636 free(reencoded);
637 unuse_commit_buffer(commit, commit_buffer);
638
639 for (i = 0, p = commit->parents; p; p = p->next) {
640 int mark = get_object_mark(&p->item->object);
641 if (!mark)
642 continue;
643 if (i == 0)
644 printf("from :%d\n", mark);
645 else
646 printf("merge :%d\n", mark);
647 i++;
648 }
649
650 if (full_tree)
651 printf("deleteall\n");
652 log_tree_diff_flush(rev);
653 string_list_clear(paths_of_changed_objects, 0);
654 rev->diffopt.output_format = saved_output_format;
655
656 printf("\n");
657
658 show_progress();
659}
660
661static void *anonymize_tag(const void *old, size_t *len)
662{
663 static int counter;
664 struct strbuf out = STRBUF_INIT;
665 strbuf_addf(&out, "tag message %d", counter++);
666 return strbuf_detach(&out, len);
667}
668
669static void handle_tail(struct object_array *commits, struct rev_info *revs,
670 struct string_list *paths_of_changed_objects)
671{
672 struct commit *commit;
673 while (commits->nr) {
674 commit = (struct commit *)object_array_pop(commits);
675 if (has_unshown_parent(commit)) {
676 /* Queue again, to be handled later */
677 add_object_array(&commit->object, NULL, commits);
678 return;
679 }
680 handle_commit(commit, revs, paths_of_changed_objects);
681 }
682}
683
684static void handle_tag(const char *name, struct tag *tag)
685{
686 unsigned long size;
687 enum object_type type;
688 char *buf;
689 const char *tagger, *tagger_end, *message;
690 size_t message_size = 0;
691 struct object *tagged;
692 int tagged_mark;
693 struct commit *p;
694
695 /* Trees have no identifier in fast-export output, thus we have no way
696 * to output tags of trees, tags of tags of trees, etc. Simply omit
697 * such tags.
698 */
699 tagged = tag->tagged;
700 while (tagged->type == OBJ_TAG) {
701 tagged = ((struct tag *)tagged)->tagged;
702 }
703 if (tagged->type == OBJ_TREE) {
704 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
705 oid_to_hex(&tag->object.oid));
706 return;
707 }
708
709 buf = read_object_file(&tag->object.oid, &type, &size);
710 if (!buf)
711 die("could not read tag %s", oid_to_hex(&tag->object.oid));
712 message = memmem(buf, size, "\n\n", 2);
713 if (message) {
714 message += 2;
715 message_size = strlen(message);
716 }
717 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
718 if (!tagger) {
719 if (fake_missing_tagger)
720 tagger = "tagger Unspecified Tagger "
721 "<unspecified-tagger> 0 +0000";
722 else
723 tagger = "";
724 tagger_end = tagger + strlen(tagger);
725 } else {
726 tagger++;
727 tagger_end = strchrnul(tagger, '\n');
728 if (anonymize)
729 anonymize_ident_line(&tagger, &tagger_end);
730 }
731
732 if (anonymize) {
733 name = anonymize_refname(name);
734 if (message) {
735 static struct hashmap tags;
736 message = anonymize_mem(&tags, anonymize_tag,
737 message, &message_size);
738 }
739 }
740
741 /* handle signed tags */
742 if (message) {
743 const char *signature = strstr(message,
744 "\n-----BEGIN PGP SIGNATURE-----\n");
745 if (signature)
746 switch(signed_tag_mode) {
747 case SIGNED_TAG_ABORT:
748 die("encountered signed tag %s; use "
749 "--signed-tags=<mode> to handle it",
750 oid_to_hex(&tag->object.oid));
751 case WARN:
752 warning("exporting signed tag %s",
753 oid_to_hex(&tag->object.oid));
754 /* fallthru */
755 case VERBATIM:
756 break;
757 case WARN_STRIP:
758 warning("stripping signature from tag %s",
759 oid_to_hex(&tag->object.oid));
760 /* fallthru */
761 case STRIP:
762 message_size = signature + 1 - message;
763 break;
764 }
765 }
766
767 /* handle tag->tagged having been filtered out due to paths specified */
768 tagged = tag->tagged;
769 tagged_mark = get_object_mark(tagged);
770 if (!tagged_mark) {
771 switch(tag_of_filtered_mode) {
772 case TAG_FILTERING_ABORT:
773 die("tag %s tags unexported object; use "
774 "--tag-of-filtered-object=<mode> to handle it",
775 oid_to_hex(&tag->object.oid));
776 case DROP:
777 /* Ignore this tag altogether */
778 free(buf);
779 return;
780 case REWRITE:
781 if (tagged->type != OBJ_COMMIT) {
782 die("tag %s tags unexported %s!",
783 oid_to_hex(&tag->object.oid),
784 type_name(tagged->type));
785 }
786 p = rewrite_commit((struct commit *)tagged);
787 if (!p) {
788 printf("reset %s\nfrom %s\n\n",
789 name, oid_to_hex(&null_oid));
790 free(buf);
791 return;
792 }
793 tagged_mark = get_object_mark(&p->object);
794 }
795 }
796
797 if (starts_with(name, "refs/tags/"))
798 name += 10;
799 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
800 name, tagged_mark,
801 (int)(tagger_end - tagger), tagger,
802 tagger == tagger_end ? "" : "\n",
803 (int)message_size, (int)message_size, message ? message : "");
804 free(buf);
805}
806
807static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
808{
809 switch (e->item->type) {
810 case OBJ_COMMIT:
811 return (struct commit *)e->item;
812 case OBJ_TAG: {
813 struct tag *tag = (struct tag *)e->item;
814
815 /* handle nested tags */
816 while (tag && tag->object.type == OBJ_TAG) {
817 parse_object(the_repository, &tag->object.oid);
818 string_list_append(&extra_refs, full_name)->util = tag;
819 tag = (struct tag *)tag->tagged;
820 }
821 if (!tag)
822 die("Tag %s points nowhere?", e->name);
823 return (struct commit *)tag;
824 break;
825 }
826 default:
827 return NULL;
828 }
829}
830
831static void get_tags_and_duplicates(struct rev_cmdline_info *info)
832{
833 int i;
834
835 for (i = 0; i < info->nr; i++) {
836 struct rev_cmdline_entry *e = info->rev + i;
837 struct object_id oid;
838 struct commit *commit;
839 char *full_name;
840
841 if (e->flags & UNINTERESTING)
842 continue;
843
844 if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1)
845 continue;
846
847 if (refspecs.nr) {
848 char *private;
849 private = apply_refspecs(&refspecs, full_name);
850 if (private) {
851 free(full_name);
852 full_name = private;
853 }
854 }
855
856 commit = get_commit(e, full_name);
857 if (!commit) {
858 warning("%s: Unexpected object of type %s, skipping.",
859 e->name,
860 type_name(e->item->type));
861 continue;
862 }
863
864 switch(commit->object.type) {
865 case OBJ_COMMIT:
866 break;
867 case OBJ_BLOB:
868 export_blob(&commit->object.oid);
869 continue;
870 default: /* OBJ_TAG (nested tags) is already handled */
871 warning("Tag points to object of unexpected type %s, skipping.",
872 type_name(commit->object.type));
873 continue;
874 }
875
876 /*
877 * This ref will not be updated through a commit, lets make
878 * sure it gets properly updated eventually.
879 */
880 if (*revision_sources_at(&revision_sources, commit) ||
881 commit->object.flags & SHOWN)
882 string_list_append(&extra_refs, full_name)->util = commit;
883 if (!*revision_sources_at(&revision_sources, commit))
884 *revision_sources_at(&revision_sources, commit) = full_name;
885 }
886}
887
888static void handle_tags_and_duplicates(void)
889{
890 struct commit *commit;
891 int i;
892
893 for (i = extra_refs.nr - 1; i >= 0; i--) {
894 const char *name = extra_refs.items[i].string;
895 struct object *object = extra_refs.items[i].util;
896 switch (object->type) {
897 case OBJ_TAG:
898 handle_tag(name, (struct tag *)object);
899 break;
900 case OBJ_COMMIT:
901 if (anonymize)
902 name = anonymize_refname(name);
903 /* create refs pointing to already seen commits */
904 commit = rewrite_commit((struct commit *)object);
905 if (!commit) {
906 /*
907 * Neither this object nor any of its
908 * ancestors touch any relevant paths, so
909 * it has been filtered to nothing. Delete
910 * it.
911 */
912 printf("reset %s\nfrom %s\n\n",
913 name, oid_to_hex(&null_oid));
914 continue;
915 }
916 printf("reset %s\nfrom :%d\n\n", name,
917 get_object_mark(&commit->object));
918 show_progress();
919 break;
920 }
921 }
922}
923
924static void export_marks(char *file)
925{
926 unsigned int i;
927 uint32_t mark;
928 struct decoration_entry *deco = idnums.entries;
929 FILE *f;
930 int e = 0;
931
932 f = fopen_for_writing(file);
933 if (!f)
934 die_errno("Unable to open marks file %s for writing.", file);
935
936 for (i = 0; i < idnums.size; i++) {
937 if (deco->base && deco->base->type == 1) {
938 mark = ptr_to_mark(deco->decoration);
939 if (fprintf(f, ":%"PRIu32" %s\n", mark,
940 oid_to_hex(&deco->base->oid)) < 0) {
941 e = 1;
942 break;
943 }
944 }
945 deco++;
946 }
947
948 e |= ferror(f);
949 e |= fclose(f);
950 if (e)
951 error("Unable to write marks file %s.", file);
952}
953
954static void import_marks(char *input_file)
955{
956 char line[512];
957 FILE *f = xfopen(input_file, "r");
958
959 while (fgets(line, sizeof(line), f)) {
960 uint32_t mark;
961 char *line_end, *mark_end;
962 struct object_id oid;
963 struct object *object;
964 struct commit *commit;
965 enum object_type type;
966
967 line_end = strchr(line, '\n');
968 if (line[0] != ':' || !line_end)
969 die("corrupt mark line: %s", line);
970 *line_end = '\0';
971
972 mark = strtoumax(line + 1, &mark_end, 10);
973 if (!mark || mark_end == line + 1
974 || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
975 die("corrupt mark line: %s", line);
976
977 if (last_idnum < mark)
978 last_idnum = mark;
979
980 type = oid_object_info(the_repository, &oid, NULL);
981 if (type < 0)
982 die("object not found: %s", oid_to_hex(&oid));
983
984 if (type != OBJ_COMMIT)
985 /* only commits */
986 continue;
987
988 commit = lookup_commit(the_repository, &oid);
989 if (!commit)
990 die("not a commit? can't happen: %s", oid_to_hex(&oid));
991
992 object = &commit->object;
993
994 if (object->flags & SHOWN)
995 error("Object %s already has a mark", oid_to_hex(&oid));
996
997 mark_object(object, mark);
998
999 object->flags |= SHOWN;
1000 }
1001 fclose(f);
1002}
1003
1004static void handle_deletes(void)
1005{
1006 int i;
1007 for (i = 0; i < refspecs.nr; i++) {
1008 struct refspec_item *refspec = &refspecs.items[i];
1009 if (*refspec->src)
1010 continue;
1011
1012 printf("reset %s\nfrom %s\n\n",
1013 refspec->dst, oid_to_hex(&null_oid));
1014 }
1015}
1016
1017int cmd_fast_export(int argc, const char **argv, const char *prefix)
1018{
1019 struct rev_info revs;
1020 struct object_array commits = OBJECT_ARRAY_INIT;
1021 struct commit *commit;
1022 char *export_filename = NULL, *import_filename = NULL;
1023 uint32_t lastimportid;
1024 struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
1025 struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
1026 struct option options[] = {
1027 OPT_INTEGER(0, "progress", &progress,
1028 N_("show progress after <n> objects")),
1029 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1030 N_("select handling of signed tags"),
1031 parse_opt_signed_tag_mode),
1032 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1033 N_("select handling of tags that tag filtered objects"),
1034 parse_opt_tag_of_filtered_mode),
1035 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1036 N_("Dump marks to this file")),
1037 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1038 N_("Import marks from this file")),
1039 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1040 N_("Fake a tagger when tags lack one")),
1041 OPT_BOOL(0, "full-tree", &full_tree,
1042 N_("Output full tree for each commit")),
1043 OPT_BOOL(0, "use-done-feature", &use_done_feature,
1044 N_("Use the done feature to terminate the stream")),
1045 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
1046 OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1047 N_("Apply refspec to exported refs")),
1048 OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
1049 OPT_END()
1050 };
1051
1052 if (argc == 1)
1053 usage_with_options (fast_export_usage, options);
1054
1055 /* we handle encodings */
1056 git_config(git_default_config, NULL);
1057
1058 repo_init_revisions(the_repository, &revs, prefix);
1059 init_revision_sources(&revision_sources);
1060 revs.topo_order = 1;
1061 revs.sources = &revision_sources;
1062 revs.rewrite_parents = 1;
1063 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1064 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
1065 argc = setup_revisions(argc, argv, &revs, NULL);
1066 if (argc > 1)
1067 usage_with_options (fast_export_usage, options);
1068
1069 if (refspecs_list.nr) {
1070 int i;
1071
1072 for (i = 0; i < refspecs_list.nr; i++)
1073 refspec_append(&refspecs, refspecs_list.items[i].string);
1074
1075 string_list_clear(&refspecs_list, 1);
1076 }
1077
1078 if (use_done_feature)
1079 printf("feature done\n");
1080
1081 if (import_filename)
1082 import_marks(import_filename);
1083 lastimportid = last_idnum;
1084
1085 if (import_filename && revs.prune_data.nr)
1086 full_tree = 1;
1087
1088 get_tags_and_duplicates(&revs.cmdline);
1089
1090 if (prepare_revision_walk(&revs))
1091 die("revision walk setup failed");
1092 revs.diffopt.format_callback = show_filemodify;
1093 revs.diffopt.format_callback_data = &paths_of_changed_objects;
1094 revs.diffopt.flags.recursive = 1;
1095 while ((commit = get_revision(&revs))) {
1096 if (has_unshown_parent(commit)) {
1097 add_object_array(&commit->object, NULL, &commits);
1098 }
1099 else {
1100 handle_commit(commit, &revs, &paths_of_changed_objects);
1101 handle_tail(&commits, &revs, &paths_of_changed_objects);
1102 }
1103 }
1104
1105 handle_tags_and_duplicates();
1106 handle_deletes();
1107
1108 if (export_filename && lastimportid != last_idnum)
1109 export_marks(export_filename);
1110
1111 if (use_done_feature)
1112 printf("done\n");
1113
1114 refspec_clear(&refspecs);
1115
1116 return 0;
1117}