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