]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fast-export.c
fast-export: tighten anonymize_mem() interface to handle only strings
[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;
123 const char *orig;
124 size_t orig_len;
125 const char *anon;
126 size_t anon_len;
127};
128
7663cdc8 129static int anonymized_entry_cmp(const void *unused_cmp_data,
939af16e
EW
130 const struct hashmap_entry *eptr,
131 const struct hashmap_entry *entry_or_key,
7663cdc8 132 const void *unused_keydata)
a8722750 133{
939af16e
EW
134 const struct anonymized_entry *a, *b;
135
136 a = container_of(eptr, const struct anonymized_entry, hash);
137 b = container_of(entry_or_key, const struct anonymized_entry, hash);
138
a8722750
JK
139 return a->orig_len != b->orig_len ||
140 memcmp(a->orig, b->orig, a->orig_len);
141}
142
143/*
144 * Basically keep a cache of X->Y so that we can repeatedly replace
145 * the same anonymized string with another. The actual generation
146 * is farmed out to the generate function.
147 */
7f407594
JK
148static const char *anonymize_str(struct hashmap *map,
149 char *(*generate)(const char *, size_t),
150 const char *orig, size_t len)
a8722750
JK
151{
152 struct anonymized_entry key, *ret;
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;
404ab78e 160 ret = hashmap_get_entry(map, &key, hash, NULL);
a8722750
JK
161
162 if (!ret) {
163 ret = xmalloc(sizeof(*ret));
164 hashmap_entry_init(&ret->hash, key.hash.hash);
7f407594
JK
165 ret->orig = xmemdupz(orig, len);
166 ret->orig_len = len;
a8722750 167 ret->anon = generate(orig, len);
7f407594 168 ret->anon_len = strlen(ret->anon);
26b455f2 169 hashmap_put(map, &ret->hash);
a8722750
JK
170 }
171
a8722750
JK
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,
7f407594 183 char *(*generate)(const char *, size_t))
a8722750
JK
184{
185 while (*path) {
186 const char *end_of_component = strchrnul(path, '/');
187 size_t len = end_of_component - path;
7f407594
JK
188 const char *c = anonymize_str(map, generate, path, len);
189 strbuf_addstr(out, c);
a8722750
JK
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));
b98d1885
MT
295 if (check_object_signature(the_repository, oid, buf, size,
296 type_name(type)) < 0)
843b9e6d 297 die("oid mismatch in blob %s", oid_to_hex(oid));
1ec5bfd2
SB
298 object = parse_object_buffer(the_repository, oid, type,
299 size, buf, &eaten);
a8722750
JK
300 }
301
30b939c3 302 if (!object)
273f8ee8 303 die("Could not read blob %s", oid_to_hex(oid));
f2dc849e 304
df6a7ff7 305 mark_next_object(object);
f2dc849e 306
a965bb31
EN
307 printf("blob\nmark :%"PRIu32"\n", last_idnum);
308 if (show_original_ids)
309 printf("original-oid %s\n", oid_to_hex(oid));
4d597532 310 printf("data %"PRIuMAX"\n", (uintmax_t)size);
b0fe0d72 311 if (size && fwrite(buf, size, 1, stdout) != 1)
1a07e59c 312 die_errno("could not write blob '%s'", oid_to_hex(oid));
f2dc849e
JS
313 printf("\n");
314
315 show_progress();
316
317 object->flags |= SHOWN;
30b939c3
JK
318 if (!eaten)
319 free(buf);
f2dc849e
JS
320}
321
060df624
EN
322static int depth_first(const void *a_, const void *b_)
323{
324 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
325 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
326 const char *name_a, *name_b;
327 int len_a, len_b, len;
328 int cmp;
329
330 name_a = a->one ? a->one->path : a->two->path;
331 name_b = b->one ? b->one->path : b->two->path;
332
333 len_a = strlen(name_a);
334 len_b = strlen(name_b);
335 len = (len_a < len_b) ? len_a : len_b;
336
337 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
338 cmp = memcmp(name_a, name_b, len);
339 if (cmp)
340 return cmp;
4ce6fb80
JS
341 cmp = len_b - len_a;
342 if (cmp)
343 return cmp;
344 /*
345 * Move 'R'ename entries last so that all references of the file
346 * appear in the output before it is renamed (e.g., when a file
347 * was copied and renamed in the same commit).
348 */
349 return (a->status == 'R') - (b->status == 'R');
060df624
EN
350}
351
a8722750 352static void print_path_1(const char *path)
6280dfdc
JK
353{
354 int need_quote = quote_c_style(path, NULL, NULL, 0);
355 if (need_quote)
356 quote_c_style(path, NULL, stdout, 0);
ff59f6da
JS
357 else if (strchr(path, ' '))
358 printf("\"%s\"", path);
6280dfdc
JK
359 else
360 printf("%s", path);
361}
362
7f407594 363static char *anonymize_path_component(const char *path, size_t len)
a8722750
JK
364{
365 static int counter;
366 struct strbuf out = STRBUF_INIT;
367 strbuf_addf(&out, "path%d", counter++);
7f407594 368 return strbuf_detach(&out, NULL);
a8722750
JK
369}
370
371static void print_path(const char *path)
372{
373 if (!anonymize)
374 print_path_1(path);
375 else {
376 static struct hashmap paths;
377 static struct strbuf anon = STRBUF_INIT;
378
379 anonymize_path(&anon, path, &paths, anonymize_path_component);
380 print_path_1(anon.buf);
381 strbuf_reset(&anon);
382 }
383}
384
7f407594 385static char *generate_fake_oid(const char *old, size_t len)
a8722750 386{
843b9e6d
EN
387 static uint32_t counter = 1; /* avoid null oid */
388 const unsigned hashsz = the_hash_algo->rawsz;
750bb325
JK
389 struct object_id oid;
390 char *hex = xmallocz(GIT_MAX_HEXSZ);
391
392 oidclr(&oid);
393 put_be32(oid.hash + hashsz - 4, counter++);
394 return oid_to_hex_r(hex, &oid);
a8722750
JK
395}
396
750bb325 397static const char *anonymize_oid(const char *oid_hex)
a8722750 398{
843b9e6d 399 static struct hashmap objs;
750bb325 400 size_t len = strlen(oid_hex);
7f407594 401 return anonymize_str(&objs, generate_fake_oid, oid_hex, len);
a8722750
JK
402}
403
f2dc849e
JS
404static void show_filemodify(struct diff_queue_struct *q,
405 struct diff_options *options, void *data)
406{
407 int i;
b3e8ca89 408 struct string_list *changed = data;
060df624
EN
409
410 /*
411 * Handle files below a directory first, in case they are all deleted
412 * and the directory changes to a file or symlink.
413 */
9ed0d8d6 414 QSORT(q->queue, q->nr, depth_first);
060df624 415
f2dc849e 416 for (i = 0; i < q->nr; i++) {
ae7c5dce 417 struct diff_filespec *ospec = q->queue[i]->one;
f2dc849e 418 struct diff_filespec *spec = q->queue[i]->two;
ae7c5dce
AG
419
420 switch (q->queue[i]->status) {
421 case DIFF_STATUS_DELETED:
6280dfdc
JK
422 printf("D ");
423 print_path(spec->path);
b3e8ca89 424 string_list_insert(changed, spec->path);
6280dfdc 425 putchar('\n');
ae7c5dce
AG
426 break;
427
428 case DIFF_STATUS_COPIED:
429 case DIFF_STATUS_RENAMED:
b3e8ca89
JT
430 /*
431 * If a change in the file corresponding to ospec->path
432 * has been observed, we cannot trust its contents
433 * because the diff is calculated based on the prior
434 * contents, not the current contents. So, declare a
435 * copy or rename only if there was no change observed.
436 */
437 if (!string_list_has_string(changed, ospec->path)) {
438 printf("%c ", q->queue[i]->status);
439 print_path(ospec->path);
440 putchar(' ');
441 print_path(spec->path);
442 string_list_insert(changed, spec->path);
443 putchar('\n');
444
4a7e27e9 445 if (oideq(&ospec->oid, &spec->oid) &&
b3e8ca89
JT
446 ospec->mode == spec->mode)
447 break;
448 }
ae7c5dce
AG
449 /* fallthrough */
450
451 case DIFF_STATUS_TYPE_CHANGED:
452 case DIFF_STATUS_MODIFIED:
453 case DIFF_STATUS_ADDED:
03db4525
AG
454 /*
455 * Links refer to objects in another repositories;
456 * output the SHA-1 verbatim.
457 */
79559f27 458 if (no_data || S_ISGITLINK(spec->mode))
6280dfdc 459 printf("M %06o %s ", spec->mode,
750bb325
JK
460 anonymize ?
461 anonymize_oid(oid_to_hex(&spec->oid)) :
462 oid_to_hex(&spec->oid));
03db4525 463 else {
5abddd1e 464 struct object *object = lookup_object(the_repository,
d0229abd 465 &spec->oid);
6280dfdc
JK
466 printf("M %06o :%d ", spec->mode,
467 get_object_mark(object));
03db4525 468 }
6280dfdc 469 print_path(spec->path);
b3e8ca89 470 string_list_insert(changed, spec->path);
6280dfdc 471 putchar('\n');
ae7c5dce
AG
472 break;
473
474 default:
475 die("Unexpected comparison status '%c' for %s, %s",
476 q->queue[i]->status,
477 ospec->path ? ospec->path : "none",
478 spec->path ? spec->path : "none");
f2dc849e
JS
479 }
480 }
481}
482
483static const char *find_encoding(const char *begin, const char *end)
484{
485 const char *needle = "\nencoding ";
486 char *bol, *eol;
487
488 bol = memmem(begin, end ? end - begin : strlen(begin),
489 needle, strlen(needle));
490 if (!bol)
57a8be2c 491 return NULL;
f2dc849e
JS
492 bol += strlen(needle);
493 eol = strchrnul(bol, '\n');
494 *eol = '\0';
495 return bol;
496}
497
7f407594 498static char *anonymize_ref_component(const char *old, size_t len)
a8722750
JK
499{
500 static int counter;
501 struct strbuf out = STRBUF_INIT;
502 strbuf_addf(&out, "ref%d", counter++);
7f407594 503 return strbuf_detach(&out, NULL);
a8722750
JK
504}
505
506static const char *anonymize_refname(const char *refname)
507{
508 /*
509 * If any of these prefixes is found, we will leave it intact
510 * so that tags remain tags and so forth.
511 */
512 static const char *prefixes[] = {
513 "refs/heads/",
514 "refs/tags/",
515 "refs/remotes/",
516 "refs/"
517 };
518 static struct hashmap refs;
519 static struct strbuf anon = STRBUF_INIT;
520 int i;
521
522 /*
523 * We also leave "master" as a special case, since it does not reveal
524 * anything interesting.
525 */
526 if (!strcmp(refname, "refs/heads/master"))
527 return refname;
528
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
551static struct hashmap idents;
7f407594 552static char *anonymize_ident(const char *old, size_t len)
a8722750
JK
553{
554 static int counter;
555 struct strbuf out = STRBUF_INIT;
556 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
557 counter++;
7f407594 558 return strbuf_detach(&out, NULL);
a8722750
JK
559}
560
561/*
562 * Our strategy here is to anonymize the names and email addresses,
563 * but keep timestamps intact, as they influence things like traversal
564 * order (and by themselves should not be too revealing).
565 */
566static void anonymize_ident_line(const char **beg, const char **end)
567{
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
JK
593 ident = anonymize_str(&idents, anonymize_ident,
594 split.name_begin, len);
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);
8597ea3a 621 commit_buffer = get_commit_buffer(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);
bc6b8fc1 702 unuse_commit_buffer(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
7f407594 735static char *anonymize_tag(const char *old, size_t len)
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
b3e8ca89
JT
743static void handle_tail(struct object_array *commits, struct rev_info *revs,
744 struct string_list *paths_of_changed_objects)
f2dc849e
JS
745{
746 struct commit *commit;
747 while (commits->nr) {
71992039 748 commit = (struct commit *)object_array_pop(commits);
be011bbe
749 if (has_unshown_parent(commit)) {
750 /* Queue again, to be handled later */
751 add_object_array(&commit->object, NULL, commits);
f2dc849e 752 return;
be011bbe 753 }
b3e8ca89 754 handle_commit(commit, revs, paths_of_changed_objects);
f2dc849e
JS
755 }
756}
757
758static void handle_tag(const char *name, struct tag *tag)
759{
760 unsigned long size;
761 enum object_type type;
762 char *buf;
763 const char *tagger, *tagger_end, *message;
764 size_t message_size = 0;
02c48cd6 765 struct object *tagged;
2d8ad469
EN
766 int tagged_mark;
767 struct commit *p;
02c48cd6 768
98e023de 769 /* Trees have no identifier in fast-export output, thus we have no way
02c48cd6
EN
770 * to output tags of trees, tags of tags of trees, etc. Simply omit
771 * such tags.
772 */
773 tagged = tag->tagged;
774 while (tagged->type == OBJ_TAG) {
775 tagged = ((struct tag *)tagged)->tagged;
776 }
777 if (tagged->type == OBJ_TREE) {
778 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
f2fd0760 779 oid_to_hex(&tag->object.oid));
02c48cd6
EN
780 return;
781 }
f2dc849e 782
b4f5aca4 783 buf = read_object_file(&tag->object.oid, &type, &size);
f2dc849e 784 if (!buf)
1a07e59c 785 die("could not read tag %s", oid_to_hex(&tag->object.oid));
f2dc849e
JS
786 message = memmem(buf, size, "\n\n", 2);
787 if (message) {
788 message += 2;
789 message_size = strlen(message);
790 }
791 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
4e46a8d6
JS
792 if (!tagger) {
793 if (fake_missing_tagger)
794 tagger = "tagger Unspecified Tagger "
795 "<unspecified-tagger> 0 +0000";
796 else
797 tagger = "";
798 tagger_end = tagger + strlen(tagger);
799 } else {
800 tagger++;
801 tagger_end = strchrnul(tagger, '\n');
a8722750
JK
802 if (anonymize)
803 anonymize_ident_line(&tagger, &tagger_end);
804 }
805
806 if (anonymize) {
807 name = anonymize_refname(name);
808 if (message) {
809 static struct hashmap tags;
7f407594
JK
810 message = anonymize_str(&tags, anonymize_tag,
811 message, message_size);
a8722750 812 }
4e46a8d6 813 }
f2dc849e
JS
814
815 /* handle signed tags */
816 if (message) {
817 const char *signature = strstr(message,
818 "\n-----BEGIN PGP SIGNATURE-----\n");
819 if (signature)
820 switch(signed_tag_mode) {
b93b81e7 821 case SIGNED_TAG_ABORT:
1a07e59c
NTND
822 die("encountered signed tag %s; use "
823 "--signed-tags=<mode> to handle it",
824 oid_to_hex(&tag->object.oid));
f2dc849e 825 case WARN:
1a07e59c
NTND
826 warning("exporting signed tag %s",
827 oid_to_hex(&tag->object.oid));
f2dc849e 828 /* fallthru */
ee4bc371 829 case VERBATIM:
f2dc849e 830 break;
cd16c59b 831 case WARN_STRIP:
1a07e59c
NTND
832 warning("stripping signature from tag %s",
833 oid_to_hex(&tag->object.oid));
cd16c59b 834 /* fallthru */
f2dc849e
JS
835 case STRIP:
836 message_size = signature + 1 - message;
837 break;
838 }
839 }
840
2d8ad469
EN
841 /* handle tag->tagged having been filtered out due to paths specified */
842 tagged = tag->tagged;
843 tagged_mark = get_object_mark(tagged);
844 if (!tagged_mark) {
845 switch(tag_of_filtered_mode) {
b93b81e7 846 case TAG_FILTERING_ABORT:
1a07e59c
NTND
847 die("tag %s tags unexported object; use "
848 "--tag-of-filtered-object=<mode> to handle it",
849 oid_to_hex(&tag->object.oid));
2d8ad469
EN
850 case DROP:
851 /* Ignore this tag altogether */
1efb1e9a 852 free(buf);
2d8ad469
EN
853 return;
854 case REWRITE:
941790d7
EN
855 if (tagged->type == OBJ_TAG && !mark_tags) {
856 die(_("Error: Cannot export nested tags unless --mark-tags is specified."));
857 } else if (tagged->type == OBJ_COMMIT) {
858 p = rewrite_commit((struct commit *)tagged);
859 if (!p) {
860 printf("reset %s\nfrom %s\n\n",
861 name, oid_to_hex(&null_oid));
862 free(buf);
863 return;
864 }
865 tagged_mark = get_object_mark(&p->object);
866 } else {
867 /* tagged->type is either OBJ_BLOB or OBJ_TAG */
868 tagged_mark = get_object_mark(tagged);
2d8ad469 869 }
2d8ad469
EN
870 }
871 }
872
941790d7
EN
873 if (tagged->type == OBJ_TAG) {
874 printf("reset %s\nfrom %s\n\n",
875 name, oid_to_hex(&null_oid));
876 }
145136a9 877 skip_prefix(name, "refs/tags/", &name);
af2abd87 878 printf("tag %s\n", name);
a1638cfe
EN
879 if (mark_tags) {
880 mark_next_object(&tag->object);
881 printf("mark :%"PRIu32"\n", last_idnum);
882 }
af2abd87
EN
883 if (tagged_mark)
884 printf("from :%d\n", tagged_mark);
885 else
886 printf("from %s\n", oid_to_hex(&tagged->oid));
887
a965bb31
EN
888 if (show_original_ids)
889 printf("original-oid %s\n", oid_to_hex(&tag->object.oid));
890 printf("%.*s%sdata %d\n%.*s\n",
f2dc849e 891 (int)(tagger_end - tagger), tagger,
4e46a8d6 892 tagger == tagger_end ? "" : "\n",
f2dc849e 893 (int)message_size, (int)message_size, message ? message : "");
1efb1e9a 894 free(buf);
f2dc849e
JS
895}
896
3e9b9cb1
FC
897static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
898{
899 switch (e->item->type) {
900 case OBJ_COMMIT:
901 return (struct commit *)e->item;
902 case OBJ_TAG: {
903 struct tag *tag = (struct tag *)e->item;
904
905 /* handle nested tags */
906 while (tag && tag->object.type == OBJ_TAG) {
109cd76d 907 parse_object(the_repository, &tag->object.oid);
fdf31b63 908 string_list_append(&tag_refs, full_name)->util = tag;
3e9b9cb1
FC
909 tag = (struct tag *)tag->tagged;
910 }
911 if (!tag)
912 die("Tag %s points nowhere?", e->name);
913 return (struct commit *)tag;
914 break;
915 }
916 default:
917 return NULL;
918 }
919}
920
1d844ee7 921static void get_tags_and_duplicates(struct rev_cmdline_info *info)
f2dc849e 922{
f2dc849e
JS
923 int i;
924
49266e8a
FC
925 for (i = 0; i < info->nr; i++) {
926 struct rev_cmdline_entry *e = info->rev + i;
273f8ee8 927 struct object_id oid;
2d242de4 928 struct commit *commit;
f2dc849e
JS
929 char *full_name;
930
49266e8a
FC
931 if (e->flags & UNINTERESTING)
932 continue;
933
cca5fa64 934 if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1)
f2dc849e
JS
935 continue;
936
16eefc8e 937 if (refspecs.nr) {
03e9010c 938 char *private;
d000414e 939 private = apply_refspecs(&refspecs, full_name);
03e9010c
FC
940 if (private) {
941 free(full_name);
942 full_name = private;
943 }
944 }
945
3e9b9cb1
FC
946 commit = get_commit(e, full_name);
947 if (!commit) {
2d07f6d4
EFL
948 warning("%s: Unexpected object of type %s, skipping.",
949 e->name,
debca9d2 950 type_name(e->item->type));
2d07f6d4 951 continue;
f2dc849e 952 }
f28e7c90 953
3e9b9cb1
FC
954 switch(commit->object.type) {
955 case OBJ_COMMIT:
956 break;
957 case OBJ_BLOB:
273f8ee8 958 export_blob(&commit->object.oid);
3e9b9cb1
FC
959 continue;
960 default: /* OBJ_TAG (nested tags) is already handled */
961 warning("Tag points to object of unexpected type %s, skipping.",
debca9d2 962 type_name(commit->object.type));
3e9b9cb1
FC
963 continue;
964 }
965
f28e7c90 966 /*
fdf31b63
EN
967 * Make sure this ref gets properly updated eventually, whether
968 * through a commit or manually at the end.
f28e7c90 969 */
fdf31b63 970 if (e->item->type != OBJ_TAG)
1d844ee7 971 string_list_append(&extra_refs, full_name)->util = commit;
fdf31b63 972
87be2523
NTND
973 if (!*revision_sources_at(&revision_sources, commit))
974 *revision_sources_at(&revision_sources, commit) = full_name;
f2dc849e 975 }
fdf31b63
EN
976
977 string_list_sort(&extra_refs);
978 string_list_remove_duplicates(&extra_refs, 0);
f2dc849e
JS
979}
980
fdf31b63 981static void handle_tags_and_duplicates(struct string_list *extras)
f2dc849e
JS
982{
983 struct commit *commit;
984 int i;
985
fdf31b63
EN
986 for (i = extras->nr - 1; i >= 0; i--) {
987 const char *name = extras->items[i].string;
988 struct object *object = extras->items[i].util;
989 int mark;
990
f2dc849e
JS
991 switch (object->type) {
992 case OBJ_TAG:
993 handle_tag(name, (struct tag *)object);
994 break;
995 case OBJ_COMMIT:
a8722750
JK
996 if (anonymize)
997 name = anonymize_refname(name);
f2dc849e 998 /* create refs pointing to already seen commits */
cd13762d
EN
999 commit = rewrite_commit((struct commit *)object);
1000 if (!commit) {
1001 /*
1002 * Neither this object nor any of its
1003 * ancestors touch any relevant paths, so
1004 * it has been filtered to nothing. Delete
1005 * it.
1006 */
1007 printf("reset %s\nfrom %s\n\n",
1008 name, oid_to_hex(&null_oid));
1009 continue;
1010 }
fdf31b63
EN
1011
1012 mark = get_object_mark(&commit->object);
1013 if (!mark) {
1014 /*
1015 * Getting here means we have a commit which
1016 * was excluded by a negative refspec (e.g.
530ca19c
EN
1017 * fast-export ^master master). If we are
1018 * referencing excluded commits, set the ref
1019 * to the exact commit. Otherwise, the user
fdf31b63 1020 * wants the branch exported but every commit
530ca19c
EN
1021 * in its history to be deleted, which basically
1022 * just means deletion of the ref.
fdf31b63 1023 */
530ca19c
EN
1024 if (!reference_excluded_commits) {
1025 /* delete the ref */
1026 printf("reset %s\nfrom %s\n\n",
1027 name, oid_to_hex(&null_oid));
1028 continue;
1029 }
1030 /* set ref to commit using oid, not mark */
1031 printf("reset %s\nfrom %s\n\n", name,
1032 oid_to_hex(&commit->object.oid));
fdf31b63
EN
1033 continue;
1034 }
1035
1036 printf("reset %s\nfrom :%d\n\n", name, mark
1037 );
f2dc849e
JS
1038 show_progress();
1039 break;
1040 }
1041 }
1042}
1043
df6a7ff7
PB
1044static void export_marks(char *file)
1045{
1046 unsigned int i;
1047 uint32_t mark;
ddd3e312 1048 struct decoration_entry *deco = idnums.entries;
df6a7ff7 1049 FILE *f;
96d69b55 1050 int e = 0;
df6a7ff7 1051
ea56518d 1052 f = fopen_for_writing(file);
df6a7ff7 1053 if (!f)
bb6ad28c 1054 die_errno("Unable to open marks file %s for writing.", file);
df6a7ff7 1055
69913575
JH
1056 for (i = 0; i < idnums.size; i++) {
1057 if (deco->base && deco->base->type == 1) {
df6a7ff7 1058 mark = ptr_to_mark(deco->decoration);
96d69b55 1059 if (fprintf(f, ":%"PRIu32" %s\n", mark,
f2fd0760 1060 oid_to_hex(&deco->base->oid)) < 0) {
96d69b55
MA
1061 e = 1;
1062 break;
1063 }
df6a7ff7 1064 }
69913575 1065 deco++;
df6a7ff7
PB
1066 }
1067
96d69b55
MA
1068 e |= ferror(f);
1069 e |= fclose(f);
1070 if (e)
df6a7ff7
PB
1071 error("Unable to write marks file %s.", file);
1072}
1073
208d6924 1074static void import_marks(char *input_file, int check_exists)
df6a7ff7
PB
1075{
1076 char line[512];
208d6924
EN
1077 FILE *f;
1078 struct stat sb;
1079
1080 if (check_exists && stat(input_file, &sb))
1081 return;
df6a7ff7 1082
208d6924 1083 f = xfopen(input_file, "r");
df6a7ff7
PB
1084 while (fgets(line, sizeof(line), f)) {
1085 uint32_t mark;
1086 char *line_end, *mark_end;
273f8ee8 1087 struct object_id oid;
df6a7ff7 1088 struct object *object;
47bd9bf8 1089 struct commit *commit;
e6812cfa 1090 enum object_type type;
df6a7ff7
PB
1091
1092 line_end = strchr(line, '\n');
1093 if (line[0] != ':' || !line_end)
1094 die("corrupt mark line: %s", line);
69913575 1095 *line_end = '\0';
df6a7ff7
PB
1096
1097 mark = strtoumax(line + 1, &mark_end, 10);
1098 if (!mark || mark_end == line + 1
273f8ee8 1099 || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
df6a7ff7
PB
1100 die("corrupt mark line: %s", line);
1101
c4458ecd
AP
1102 if (last_idnum < mark)
1103 last_idnum = mark;
1104
0df8e965 1105 type = oid_object_info(the_repository, &oid, NULL);
e6812cfa 1106 if (type < 0)
273f8ee8 1107 die("object not found: %s", oid_to_hex(&oid));
e6812cfa
FC
1108
1109 if (type != OBJ_COMMIT)
1110 /* only commits */
c4458ecd 1111 continue;
df6a7ff7 1112
c1f5eb49 1113 commit = lookup_commit(the_repository, &oid);
47bd9bf8 1114 if (!commit)
273f8ee8 1115 die("not a commit? can't happen: %s", oid_to_hex(&oid));
47bd9bf8
FC
1116
1117 object = &commit->object;
e6812cfa 1118
df6a7ff7 1119 if (object->flags & SHOWN)
273f8ee8 1120 error("Object %s already has a mark", oid_to_hex(&oid));
df6a7ff7
PB
1121
1122 mark_object(object, mark);
df6a7ff7
PB
1123
1124 object->flags |= SHOWN;
1125 }
1126 fclose(f);
1127}
1128
60ed2643
FC
1129static void handle_deletes(void)
1130{
1131 int i;
16eefc8e
BW
1132 for (i = 0; i < refspecs.nr; i++) {
1133 struct refspec_item *refspec = &refspecs.items[i];
60ed2643
FC
1134 if (*refspec->src)
1135 continue;
1136
1137 printf("reset %s\nfrom %s\n\n",
843b9e6d 1138 refspec->dst, oid_to_hex(&null_oid));
60ed2643
FC
1139 }
1140}
1141
f2dc849e
JS
1142int cmd_fast_export(int argc, const char **argv, const char *prefix)
1143{
1144 struct rev_info revs;
3cd47459 1145 struct object_array commits = OBJECT_ARRAY_INIT;
f2dc849e 1146 struct commit *commit;
208d6924
EN
1147 char *export_filename = NULL,
1148 *import_filename = NULL,
1149 *import_filename_if_exists = NULL;
c4458ecd 1150 uint32_t lastimportid;
03e9010c 1151 struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
b3e8ca89 1152 struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
f2dc849e
JS
1153 struct option options[] = {
1154 OPT_INTEGER(0, "progress", &progress,
3b787b96
NTND
1155 N_("show progress after <n> objects")),
1156 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1157 N_("select handling of signed tags"),
f2dc849e 1158 parse_opt_signed_tag_mode),
3b787b96
NTND
1159 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1160 N_("select handling of tags that tag filtered objects"),
2d8ad469 1161 parse_opt_tag_of_filtered_mode),
e80001f8
EN
1162 OPT_CALLBACK(0, "reencode", &reencode_mode, N_("mode"),
1163 N_("select handling of commit messages in an alternate encoding"),
1164 parse_opt_reencode_mode),
3b787b96
NTND
1165 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1166 N_("Dump marks to this file")),
1167 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1168 N_("Import marks from this file")),
208d6924
EN
1169 OPT_STRING(0, "import-marks-if-exists",
1170 &import_filename_if_exists,
1171 N_("file"),
1172 N_("Import marks from this file if it exists")),
d5d09d47
SB
1173 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1174 N_("Fake a tagger when tags lack one")),
1175 OPT_BOOL(0, "full-tree", &full_tree,
1176 N_("Output full tree for each commit")),
1177 OPT_BOOL(0, "use-done-feature", &use_done_feature,
3b787b96
NTND
1178 N_("Use the done feature to terminate the stream")),
1179 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
03e9010c
FC
1180 OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1181 N_("Apply refspec to exported refs")),
a8722750 1182 OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
530ca19c
EN
1183 OPT_BOOL(0, "reference-excluded-parents",
1184 &reference_excluded_commits, N_("Reference parents which are not in fast-export stream by object id")),
a965bb31
EN
1185 OPT_BOOL(0, "show-original-ids", &show_original_ids,
1186 N_("Show original object ids of blobs/commits")),
a1638cfe
EN
1187 OPT_BOOL(0, "mark-tags", &mark_tags,
1188 N_("Label tags with mark ids")),
530ca19c 1189
f2dc849e
JS
1190 OPT_END()
1191 };
1192
dcfdbdf0
MV
1193 if (argc == 1)
1194 usage_with_options (fast_export_usage, options);
1195
f2dc849e 1196 /* we handle encodings */
ef90d6d4 1197 git_config(git_default_config, NULL);
f2dc849e 1198
2abf3503 1199 repo_init_revisions(the_repository, &revs, prefix);
87be2523 1200 init_revision_sources(&revision_sources);
668f3aa7 1201 revs.topo_order = 1;
87be2523 1202 revs.sources = &revision_sources;
32164131 1203 revs.rewrite_parents = 1;
8b2f86a7
FC
1204 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1205 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
f2dc849e 1206 argc = setup_revisions(argc, argv, &revs, NULL);
f2dc849e
JS
1207 if (argc > 1)
1208 usage_with_options (fast_export_usage, options);
1209
03e9010c 1210 if (refspecs_list.nr) {
03e9010c
FC
1211 int i;
1212
03e9010c 1213 for (i = 0; i < refspecs_list.nr; i++)
16eefc8e 1214 refspec_append(&refspecs, refspecs_list.items[i].string);
03e9010c
FC
1215
1216 string_list_clear(&refspecs_list, 1);
03e9010c
FC
1217 }
1218
82670a5c
SR
1219 if (use_done_feature)
1220 printf("feature done\n");
1221
208d6924
EN
1222 if (import_filename && import_filename_if_exists)
1223 die(_("Cannot pass both --import-marks and --import-marks-if-exists"));
df6a7ff7 1224 if (import_filename)
208d6924
EN
1225 import_marks(import_filename, 0);
1226 else if (import_filename_if_exists)
1227 import_marks(import_filename_if_exists, 1);
c4458ecd 1228 lastimportid = last_idnum;
df6a7ff7 1229
afe069d1 1230 if (import_filename && revs.prune_data.nr)
4087a02e
EN
1231 full_tree = 1;
1232
1d844ee7 1233 get_tags_and_duplicates(&revs.cmdline);
f2dc849e 1234
3d51e1b5
MK
1235 if (prepare_revision_walk(&revs))
1236 die("revision walk setup failed");
f2dc849e 1237 revs.diffopt.format_callback = show_filemodify;
b3e8ca89 1238 revs.diffopt.format_callback_data = &paths_of_changed_objects;
0d1e0e78 1239 revs.diffopt.flags.recursive = 1;
f2dc849e
JS
1240 while ((commit = get_revision(&revs))) {
1241 if (has_unshown_parent(commit)) {
f2dc849e 1242 add_object_array(&commit->object, NULL, &commits);
f2dc849e
JS
1243 }
1244 else {
b3e8ca89
JT
1245 handle_commit(commit, &revs, &paths_of_changed_objects);
1246 handle_tail(&commits, &revs, &paths_of_changed_objects);
f2dc849e
JS
1247 }
1248 }
1249
fdf31b63
EN
1250 handle_tags_and_duplicates(&extra_refs);
1251 handle_tags_and_duplicates(&tag_refs);
60ed2643 1252 handle_deletes();
f2dc849e 1253
c4458ecd 1254 if (export_filename && lastimportid != last_idnum)
df6a7ff7
PB
1255 export_marks(export_filename);
1256
82670a5c
SR
1257 if (use_done_feature)
1258 printf("done\n");
1259
16eefc8e 1260 refspec_clear(&refspecs);
03e9010c 1261
f2dc849e
JS
1262 return 0;
1263}