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