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