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