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