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