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