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