]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fast-export.c
commit: record buffer length in cache
[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"
8#include "commit.h"
9#include "object.h"
10#include "tag.h"
11#include "diff.h"
12#include "diffcore.h"
13#include "log-tree.h"
14#include "revision.h"
15#include "decorate.h"
c455c87c 16#include "string-list.h"
f2dc849e
JS
17#include "utf8.h"
18#include "parse-options.h"
6280dfdc 19#include "quote.h"
f2dc849e
JS
20
21static const char *fast_export_usage[] = {
3b787b96 22 N_("git fast-export [rev-list-opts]"),
f2dc849e
JS
23 NULL
24};
25
26static int progress;
cd16c59b 27static enum { ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = ABORT;
d7a10c31 28static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR;
4e46a8d6 29static int fake_missing_tagger;
82670a5c 30static int use_done_feature;
79559f27 31static int no_data;
7f40ab09 32static int full_tree;
1d844ee7 33static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
f2dc849e
JS
34
35static int parse_opt_signed_tag_mode(const struct option *opt,
36 const char *arg, int unset)
37{
38 if (unset || !strcmp(arg, "abort"))
39 signed_tag_mode = ABORT;
ee4bc371
JS
40 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
41 signed_tag_mode = VERBATIM;
f2dc849e
JS
42 else if (!strcmp(arg, "warn"))
43 signed_tag_mode = WARN;
cd16c59b
JK
44 else if (!strcmp(arg, "warn-strip"))
45 signed_tag_mode = WARN_STRIP;
f2dc849e
JS
46 else if (!strcmp(arg, "strip"))
47 signed_tag_mode = STRIP;
48 else
04a74b6c 49 return error("Unknown signed-tags mode: %s", arg);
f2dc849e
JS
50 return 0;
51}
52
2d8ad469
EN
53static int parse_opt_tag_of_filtered_mode(const struct option *opt,
54 const char *arg, int unset)
55{
56 if (unset || !strcmp(arg, "abort"))
d7a10c31 57 tag_of_filtered_mode = ERROR;
2d8ad469
EN
58 else if (!strcmp(arg, "drop"))
59 tag_of_filtered_mode = DROP;
60 else if (!strcmp(arg, "rewrite"))
61 tag_of_filtered_mode = REWRITE;
62 else
63 return error("Unknown tag-of-filtered mode: %s", arg);
64 return 0;
65}
66
f2dc849e
JS
67static struct decoration idnums;
68static uint32_t last_idnum;
69
70static int has_unshown_parent(struct commit *commit)
71{
72 struct commit_list *parent;
73
74 for (parent = commit->parents; parent; parent = parent->next)
75 if (!(parent->item->object.flags & SHOWN) &&
76 !(parent->item->object.flags & UNINTERESTING))
77 return 1;
78 return 0;
79}
80
81/* Since intptr_t is C99, we do not use it here */
df6a7ff7 82static inline uint32_t *mark_to_ptr(uint32_t mark)
f2dc849e 83{
df6a7ff7
PB
84 return ((uint32_t *)NULL) + mark;
85}
86
87static inline uint32_t ptr_to_mark(void * mark)
88{
89 return (uint32_t *)mark - (uint32_t *)NULL;
90}
91
92static inline void mark_object(struct object *object, uint32_t mark)
93{
94 add_decoration(&idnums, object, mark_to_ptr(mark));
95}
96
97static inline void mark_next_object(struct object *object)
98{
99 mark_object(object, ++last_idnum);
f2dc849e
JS
100}
101
102static int get_object_mark(struct object *object)
103{
104 void *decoration = lookup_decoration(&idnums, object);
105 if (!decoration)
106 return 0;
df6a7ff7 107 return ptr_to_mark(decoration);
f2dc849e
JS
108}
109
110static void show_progress(void)
111{
112 static int counter = 0;
113 if (!progress)
114 return;
115 if ((++counter % progress) == 0)
116 printf("progress %d objects\n", counter);
117}
118
f9b54e26 119static void export_blob(const unsigned char *sha1)
f2dc849e
JS
120{
121 unsigned long size;
122 enum object_type type;
123 char *buf;
124 struct object *object;
30b939c3 125 int eaten;
f2dc849e 126
79559f27
GI
127 if (no_data)
128 return;
129
f2dc849e
JS
130 if (is_null_sha1(sha1))
131 return;
132
30b939c3
JK
133 object = lookup_object(sha1);
134 if (object && object->flags & SHOWN)
f2dc849e
JS
135 return;
136
137 buf = read_sha1_file(sha1, &type, &size);
138 if (!buf)
139 die ("Could not read blob %s", sha1_to_hex(sha1));
30b939c3
JK
140 if (check_sha1_signature(sha1, buf, size, typename(type)) < 0)
141 die("sha1 mismatch in blob %s", sha1_to_hex(sha1));
142 object = parse_object_buffer(sha1, type, size, buf, &eaten);
143 if (!object)
144 die("Could not read blob %s", sha1_to_hex(sha1));
f2dc849e 145
df6a7ff7 146 mark_next_object(object);
f2dc849e 147
6e1c2344 148 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
b0fe0d72 149 if (size && fwrite(buf, size, 1, stdout) != 1)
0721c314 150 die_errno ("Could not write blob '%s'", sha1_to_hex(sha1));
f2dc849e
JS
151 printf("\n");
152
153 show_progress();
154
155 object->flags |= SHOWN;
30b939c3
JK
156 if (!eaten)
157 free(buf);
f2dc849e
JS
158}
159
060df624
EN
160static int depth_first(const void *a_, const void *b_)
161{
162 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
163 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
164 const char *name_a, *name_b;
165 int len_a, len_b, len;
166 int cmp;
167
168 name_a = a->one ? a->one->path : a->two->path;
169 name_b = b->one ? b->one->path : b->two->path;
170
171 len_a = strlen(name_a);
172 len_b = strlen(name_b);
173 len = (len_a < len_b) ? len_a : len_b;
174
175 /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
176 cmp = memcmp(name_a, name_b, len);
177 if (cmp)
178 return cmp;
4ce6fb80
JS
179 cmp = len_b - len_a;
180 if (cmp)
181 return cmp;
182 /*
183 * Move 'R'ename entries last so that all references of the file
184 * appear in the output before it is renamed (e.g., when a file
185 * was copied and renamed in the same commit).
186 */
187 return (a->status == 'R') - (b->status == 'R');
060df624
EN
188}
189
6280dfdc
JK
190static void print_path(const char *path)
191{
192 int need_quote = quote_c_style(path, NULL, NULL, 0);
193 if (need_quote)
194 quote_c_style(path, NULL, stdout, 0);
ff59f6da
JS
195 else if (strchr(path, ' '))
196 printf("\"%s\"", path);
6280dfdc
JK
197 else
198 printf("%s", path);
199}
200
f2dc849e
JS
201static void show_filemodify(struct diff_queue_struct *q,
202 struct diff_options *options, void *data)
203{
204 int i;
060df624
EN
205
206 /*
207 * Handle files below a directory first, in case they are all deleted
208 * and the directory changes to a file or symlink.
209 */
210 qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
211
f2dc849e 212 for (i = 0; i < q->nr; i++) {
ae7c5dce 213 struct diff_filespec *ospec = q->queue[i]->one;
f2dc849e 214 struct diff_filespec *spec = q->queue[i]->two;
ae7c5dce
AG
215
216 switch (q->queue[i]->status) {
217 case DIFF_STATUS_DELETED:
6280dfdc
JK
218 printf("D ");
219 print_path(spec->path);
220 putchar('\n');
ae7c5dce
AG
221 break;
222
223 case DIFF_STATUS_COPIED:
224 case DIFF_STATUS_RENAMED:
6280dfdc
JK
225 printf("%c ", q->queue[i]->status);
226 print_path(ospec->path);
227 putchar(' ');
228 print_path(spec->path);
229 putchar('\n');
ae7c5dce
AG
230
231 if (!hashcmp(ospec->sha1, spec->sha1) &&
232 ospec->mode == spec->mode)
233 break;
234 /* fallthrough */
235
236 case DIFF_STATUS_TYPE_CHANGED:
237 case DIFF_STATUS_MODIFIED:
238 case DIFF_STATUS_ADDED:
03db4525
AG
239 /*
240 * Links refer to objects in another repositories;
241 * output the SHA-1 verbatim.
242 */
79559f27 243 if (no_data || S_ISGITLINK(spec->mode))
6280dfdc
JK
244 printf("M %06o %s ", spec->mode,
245 sha1_to_hex(spec->sha1));
03db4525
AG
246 else {
247 struct object *object = lookup_object(spec->sha1);
6280dfdc
JK
248 printf("M %06o :%d ", spec->mode,
249 get_object_mark(object));
03db4525 250 }
6280dfdc
JK
251 print_path(spec->path);
252 putchar('\n');
ae7c5dce
AG
253 break;
254
255 default:
256 die("Unexpected comparison status '%c' for %s, %s",
257 q->queue[i]->status,
258 ospec->path ? ospec->path : "none",
259 spec->path ? spec->path : "none");
f2dc849e
JS
260 }
261 }
262}
263
264static const char *find_encoding(const char *begin, const char *end)
265{
266 const char *needle = "\nencoding ";
267 char *bol, *eol;
268
269 bol = memmem(begin, end ? end - begin : strlen(begin),
270 needle, strlen(needle));
271 if (!bol)
272 return git_commit_encoding;
273 bol += strlen(needle);
274 eol = strchrnul(bol, '\n');
275 *eol = '\0';
276 return bol;
277}
278
279static void handle_commit(struct commit *commit, struct rev_info *rev)
280{
281 int saved_output_format = rev->diffopt.output_format;
bc6b8fc1 282 const char *commit_buffer;
f2dc849e
JS
283 const char *author, *author_end, *committer, *committer_end;
284 const char *encoding, *message;
285 char *reencoded = NULL;
286 struct commit_list *p;
287 int i;
288
289 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
290
683ff884 291 parse_commit_or_die(commit);
8597ea3a 292 commit_buffer = get_commit_buffer(commit, NULL);
bc6b8fc1 293 author = strstr(commit_buffer, "\nauthor ");
f2dc849e
JS
294 if (!author)
295 die ("Could not find author in commit %s",
296 sha1_to_hex(commit->object.sha1));
297 author++;
298 author_end = strchrnul(author, '\n');
299 committer = strstr(author_end, "\ncommitter ");
300 if (!committer)
301 die ("Could not find committer in commit %s",
302 sha1_to_hex(commit->object.sha1));
303 committer++;
304 committer_end = strchrnul(committer, '\n');
305 message = strstr(committer_end, "\n\n");
306 encoding = find_encoding(committer_end, message);
307 if (message)
308 message += 2;
309
ebeec7db 310 if (commit->parents &&
4087a02e
EN
311 get_object_mark(&commit->parents->item->object) != 0 &&
312 !full_tree) {
683ff884 313 parse_commit_or_die(commit->parents->item);
f2dc849e
JS
314 diff_tree_sha1(commit->parents->item->tree->object.sha1,
315 commit->tree->object.sha1, "", &rev->diffopt);
316 }
317 else
318 diff_root_tree_sha1(commit->tree->object.sha1,
319 "", &rev->diffopt);
320
03db4525 321 /* Export the referenced blobs, and remember the marks. */
f2dc849e 322 for (i = 0; i < diff_queued_diff.nr; i++)
03db4525 323 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
f9b54e26 324 export_blob(diff_queued_diff.queue[i]->two->sha1);
f2dc849e 325
df6a7ff7 326 mark_next_object(&commit->object);
f2dc849e
JS
327 if (!is_encoding_utf8(encoding))
328 reencoded = reencode_string(message, "UTF-8", encoding);
d8933f01
SP
329 if (!commit->parents)
330 printf("reset %s\n", (const char*)commit->util);
6e1c2344 331 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
f2dc849e
JS
332 (const char *)commit->util, last_idnum,
333 (int)(author_end - author), author,
334 (int)(committer_end - committer), committer,
335 (unsigned)(reencoded
336 ? strlen(reencoded) : message
337 ? strlen(message) : 0),
338 reencoded ? reencoded : message ? message : "");
8e0f7003 339 free(reencoded);
bc6b8fc1 340 unuse_commit_buffer(commit, commit_buffer);
f2dc849e
JS
341
342 for (i = 0, p = commit->parents; p; p = p->next) {
343 int mark = get_object_mark(&p->item->object);
344 if (!mark)
345 continue;
346 if (i == 0)
347 printf("from :%d\n", mark);
f2dc849e 348 else
5070b49e 349 printf("merge :%d\n", mark);
f2dc849e
JS
350 i++;
351 }
f2dc849e 352
4087a02e
EN
353 if (full_tree)
354 printf("deleteall\n");
f2dc849e
JS
355 log_tree_diff_flush(rev);
356 rev->diffopt.output_format = saved_output_format;
357
358 printf("\n");
359
360 show_progress();
361}
362
363static void handle_tail(struct object_array *commits, struct rev_info *revs)
364{
365 struct commit *commit;
366 while (commits->nr) {
367 commit = (struct commit *)commits->objects[commits->nr - 1].item;
368 if (has_unshown_parent(commit))
369 return;
370 handle_commit(commit, revs);
371 commits->nr--;
372 }
373}
374
375static void handle_tag(const char *name, struct tag *tag)
376{
377 unsigned long size;
378 enum object_type type;
379 char *buf;
380 const char *tagger, *tagger_end, *message;
381 size_t message_size = 0;
02c48cd6 382 struct object *tagged;
2d8ad469
EN
383 int tagged_mark;
384 struct commit *p;
02c48cd6 385
98e023de 386 /* Trees have no identifier in fast-export output, thus we have no way
02c48cd6
EN
387 * to output tags of trees, tags of tags of trees, etc. Simply omit
388 * such tags.
389 */
390 tagged = tag->tagged;
391 while (tagged->type == OBJ_TAG) {
392 tagged = ((struct tag *)tagged)->tagged;
393 }
394 if (tagged->type == OBJ_TREE) {
395 warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
396 sha1_to_hex(tag->object.sha1));
397 return;
398 }
f2dc849e
JS
399
400 buf = read_sha1_file(tag->object.sha1, &type, &size);
401 if (!buf)
402 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
403 message = memmem(buf, size, "\n\n", 2);
404 if (message) {
405 message += 2;
406 message_size = strlen(message);
407 }
408 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
4e46a8d6
JS
409 if (!tagger) {
410 if (fake_missing_tagger)
411 tagger = "tagger Unspecified Tagger "
412 "<unspecified-tagger> 0 +0000";
413 else
414 tagger = "";
415 tagger_end = tagger + strlen(tagger);
416 } else {
417 tagger++;
418 tagger_end = strchrnul(tagger, '\n');
419 }
f2dc849e
JS
420
421 /* handle signed tags */
422 if (message) {
423 const char *signature = strstr(message,
424 "\n-----BEGIN PGP SIGNATURE-----\n");
425 if (signature)
426 switch(signed_tag_mode) {
427 case ABORT:
428 die ("Encountered signed tag %s; use "
04a74b6c 429 "--signed-tags=<mode> to handle it.",
f2dc849e
JS
430 sha1_to_hex(tag->object.sha1));
431 case WARN:
432 warning ("Exporting signed tag %s",
433 sha1_to_hex(tag->object.sha1));
434 /* fallthru */
ee4bc371 435 case VERBATIM:
f2dc849e 436 break;
cd16c59b
JK
437 case WARN_STRIP:
438 warning ("Stripping signature from tag %s",
439 sha1_to_hex(tag->object.sha1));
440 /* fallthru */
f2dc849e
JS
441 case STRIP:
442 message_size = signature + 1 - message;
443 break;
444 }
445 }
446
2d8ad469
EN
447 /* handle tag->tagged having been filtered out due to paths specified */
448 tagged = tag->tagged;
449 tagged_mark = get_object_mark(tagged);
450 if (!tagged_mark) {
451 switch(tag_of_filtered_mode) {
452 case ABORT:
453 die ("Tag %s tags unexported object; use "
454 "--tag-of-filtered-object=<mode> to handle it.",
455 sha1_to_hex(tag->object.sha1));
456 case DROP:
457 /* Ignore this tag altogether */
458 return;
459 case REWRITE:
460 if (tagged->type != OBJ_COMMIT) {
461 die ("Tag %s tags unexported %s!",
462 sha1_to_hex(tag->object.sha1),
463 typename(tagged->type));
464 }
465 p = (struct commit *)tagged;
466 for (;;) {
467 if (p->parents && p->parents->next)
468 break;
469 if (p->object.flags & UNINTERESTING)
470 break;
471 if (!(p->object.flags & TREESAME))
472 break;
473 if (!p->parents)
474 die ("Can't find replacement commit for tag %s\n",
475 sha1_to_hex(tag->object.sha1));
476 p = p->parents->item;
477 }
478 tagged_mark = get_object_mark(&p->object);
479 }
480 }
481
59556548 482 if (starts_with(name, "refs/tags/"))
f2dc849e 483 name += 10;
4e46a8d6 484 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
2d8ad469 485 name, tagged_mark,
f2dc849e 486 (int)(tagger_end - tagger), tagger,
4e46a8d6 487 tagger == tagger_end ? "" : "\n",
f2dc849e
JS
488 (int)message_size, (int)message_size, message ? message : "");
489}
490
3e9b9cb1
FC
491static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
492{
493 switch (e->item->type) {
494 case OBJ_COMMIT:
495 return (struct commit *)e->item;
496 case OBJ_TAG: {
497 struct tag *tag = (struct tag *)e->item;
498
499 /* handle nested tags */
500 while (tag && tag->object.type == OBJ_TAG) {
501 parse_object(tag->object.sha1);
502 string_list_append(&extra_refs, full_name)->util = tag;
503 tag = (struct tag *)tag->tagged;
504 }
505 if (!tag)
506 die("Tag %s points nowhere?", e->name);
507 return (struct commit *)tag;
508 break;
509 }
510 default:
511 return NULL;
512 }
513}
514
1d844ee7 515static void get_tags_and_duplicates(struct rev_cmdline_info *info)
f2dc849e 516{
f2dc849e
JS
517 int i;
518
49266e8a
FC
519 for (i = 0; i < info->nr; i++) {
520 struct rev_cmdline_entry *e = info->rev + i;
f2dc849e 521 unsigned char sha1[20];
2d242de4 522 struct commit *commit;
f2dc849e
JS
523 char *full_name;
524
49266e8a
FC
525 if (e->flags & UNINTERESTING)
526 continue;
527
f2dc849e
JS
528 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
529 continue;
530
3e9b9cb1
FC
531 commit = get_commit(e, full_name);
532 if (!commit) {
2d07f6d4
EFL
533 warning("%s: Unexpected object of type %s, skipping.",
534 e->name,
535 typename(e->item->type));
536 continue;
f2dc849e 537 }
f28e7c90 538
3e9b9cb1
FC
539 switch(commit->object.type) {
540 case OBJ_COMMIT:
541 break;
542 case OBJ_BLOB:
543 export_blob(commit->object.sha1);
544 continue;
545 default: /* OBJ_TAG (nested tags) is already handled */
546 warning("Tag points to object of unexpected type %s, skipping.",
547 typename(commit->object.type));
548 continue;
549 }
550
f28e7c90
FC
551 /*
552 * This ref will not be updated through a commit, lets make
553 * sure it gets properly updated eventually.
554 */
555 if (commit->util || commit->object.flags & SHOWN)
1d844ee7 556 string_list_append(&extra_refs, full_name)->util = commit;
f28e7c90 557 if (!commit->util)
f2dc849e
JS
558 commit->util = full_name;
559 }
560}
561
1d844ee7 562static void handle_tags_and_duplicates(void)
f2dc849e
JS
563{
564 struct commit *commit;
565 int i;
566
1d844ee7
FC
567 for (i = extra_refs.nr - 1; i >= 0; i--) {
568 const char *name = extra_refs.items[i].string;
569 struct object *object = extra_refs.items[i].util;
f2dc849e
JS
570 switch (object->type) {
571 case OBJ_TAG:
572 handle_tag(name, (struct tag *)object);
573 break;
574 case OBJ_COMMIT:
575 /* create refs pointing to already seen commits */
576 commit = (struct commit *)object;
577 printf("reset %s\nfrom :%d\n\n", name,
578 get_object_mark(&commit->object));
579 show_progress();
580 break;
581 }
582 }
583}
584
df6a7ff7
PB
585static void export_marks(char *file)
586{
587 unsigned int i;
588 uint32_t mark;
589 struct object_decoration *deco = idnums.hash;
590 FILE *f;
96d69b55 591 int e = 0;
df6a7ff7
PB
592
593 f = fopen(file, "w");
594 if (!f)
bb6ad28c 595 die_errno("Unable to open marks file %s for writing.", file);
df6a7ff7 596
69913575
JH
597 for (i = 0; i < idnums.size; i++) {
598 if (deco->base && deco->base->type == 1) {
df6a7ff7 599 mark = ptr_to_mark(deco->decoration);
96d69b55
MA
600 if (fprintf(f, ":%"PRIu32" %s\n", mark,
601 sha1_to_hex(deco->base->sha1)) < 0) {
602 e = 1;
603 break;
604 }
df6a7ff7 605 }
69913575 606 deco++;
df6a7ff7
PB
607 }
608
96d69b55
MA
609 e |= ferror(f);
610 e |= fclose(f);
611 if (e)
df6a7ff7
PB
612 error("Unable to write marks file %s.", file);
613}
614
69913575 615static void import_marks(char *input_file)
df6a7ff7
PB
616{
617 char line[512];
618 FILE *f = fopen(input_file, "r");
619 if (!f)
d824cbba 620 die_errno("cannot read '%s'", input_file);
df6a7ff7
PB
621
622 while (fgets(line, sizeof(line), f)) {
623 uint32_t mark;
624 char *line_end, *mark_end;
625 unsigned char sha1[20];
626 struct object *object;
47bd9bf8 627 struct commit *commit;
e6812cfa 628 enum object_type type;
df6a7ff7
PB
629
630 line_end = strchr(line, '\n');
631 if (line[0] != ':' || !line_end)
632 die("corrupt mark line: %s", line);
69913575 633 *line_end = '\0';
df6a7ff7
PB
634
635 mark = strtoumax(line + 1, &mark_end, 10);
636 if (!mark || mark_end == line + 1
45c5d4a5 637 || *mark_end != ' ' || get_sha1_hex(mark_end + 1, sha1))
df6a7ff7
PB
638 die("corrupt mark line: %s", line);
639
c4458ecd
AP
640 if (last_idnum < mark)
641 last_idnum = mark;
642
e6812cfa
FC
643 type = sha1_object_info(sha1, NULL);
644 if (type < 0)
645 die("object not found: %s", sha1_to_hex(sha1));
646
647 if (type != OBJ_COMMIT)
648 /* only commits */
c4458ecd 649 continue;
df6a7ff7 650
47bd9bf8
FC
651 commit = lookup_commit(sha1);
652 if (!commit)
653 die("not a commit? can't happen: %s", sha1_to_hex(sha1));
654
655 object = &commit->object;
e6812cfa 656
df6a7ff7 657 if (object->flags & SHOWN)
43bc2302 658 error("Object %s already has a mark", sha1_to_hex(sha1));
df6a7ff7
PB
659
660 mark_object(object, mark);
df6a7ff7
PB
661
662 object->flags |= SHOWN;
663 }
664 fclose(f);
665}
666
f2dc849e
JS
667int cmd_fast_export(int argc, const char **argv, const char *prefix)
668{
669 struct rev_info revs;
3cd47459 670 struct object_array commits = OBJECT_ARRAY_INIT;
f2dc849e 671 struct commit *commit;
df6a7ff7 672 char *export_filename = NULL, *import_filename = NULL;
c4458ecd 673 uint32_t lastimportid;
f2dc849e
JS
674 struct option options[] = {
675 OPT_INTEGER(0, "progress", &progress,
3b787b96
NTND
676 N_("show progress after <n> objects")),
677 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
678 N_("select handling of signed tags"),
f2dc849e 679 parse_opt_signed_tag_mode),
3b787b96
NTND
680 OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
681 N_("select handling of tags that tag filtered objects"),
2d8ad469 682 parse_opt_tag_of_filtered_mode),
3b787b96
NTND
683 OPT_STRING(0, "export-marks", &export_filename, N_("file"),
684 N_("Dump marks to this file")),
685 OPT_STRING(0, "import-marks", &import_filename, N_("file"),
686 N_("Import marks from this file")),
d5d09d47
SB
687 OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
688 N_("Fake a tagger when tags lack one")),
689 OPT_BOOL(0, "full-tree", &full_tree,
690 N_("Output full tree for each commit")),
691 OPT_BOOL(0, "use-done-feature", &use_done_feature,
3b787b96
NTND
692 N_("Use the done feature to terminate the stream")),
693 OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
f2dc849e
JS
694 OPT_END()
695 };
696
dcfdbdf0
MV
697 if (argc == 1)
698 usage_with_options (fast_export_usage, options);
699
f2dc849e 700 /* we handle encodings */
ef90d6d4 701 git_config(git_default_config, NULL);
f2dc849e
JS
702
703 init_revisions(&revs, prefix);
668f3aa7 704 revs.topo_order = 1;
2374502c 705 revs.show_source = 1;
32164131 706 revs.rewrite_parents = 1;
f2dc849e 707 argc = setup_revisions(argc, argv, &revs, NULL);
37782920 708 argc = parse_options(argc, argv, prefix, options, fast_export_usage, 0);
f2dc849e
JS
709 if (argc > 1)
710 usage_with_options (fast_export_usage, options);
711
82670a5c
SR
712 if (use_done_feature)
713 printf("feature done\n");
714
df6a7ff7
PB
715 if (import_filename)
716 import_marks(import_filename);
c4458ecd 717 lastimportid = last_idnum;
df6a7ff7 718
afe069d1 719 if (import_filename && revs.prune_data.nr)
4087a02e
EN
720 full_tree = 1;
721
1d844ee7 722 get_tags_and_duplicates(&revs.cmdline);
f2dc849e 723
3d51e1b5
MK
724 if (prepare_revision_walk(&revs))
725 die("revision walk setup failed");
f2dc849e
JS
726 revs.diffopt.format_callback = show_filemodify;
727 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
728 while ((commit = get_revision(&revs))) {
729 if (has_unshown_parent(commit)) {
f2dc849e 730 add_object_array(&commit->object, NULL, &commits);
f2dc849e
JS
731 }
732 else {
733 handle_commit(commit, &revs);
734 handle_tail(&commits, &revs);
735 }
736 }
737
1d844ee7 738 handle_tags_and_duplicates();
f2dc849e 739
c4458ecd 740 if (export_filename && lastimportid != last_idnum)
df6a7ff7
PB
741 export_marks(export_filename);
742
82670a5c
SR
743 if (use_done_feature)
744 printf("done\n");
745
f2dc849e
JS
746 return 0;
747}