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