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