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