builtin-fast-export.c: turn error into warning
[git/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"
19
20static const char *fast_export_usage[] = {
1b1dd23f 21 "git fast-export [rev-list-opts]",
f2dc849e
JS
22 NULL
23};
24
25static int progress;
ee4bc371 26static enum { VERBATIM, WARN, STRIP, ABORT } signed_tag_mode = ABORT;
4e46a8d6 27static int fake_missing_tagger;
f2dc849e
JS
28
29static int parse_opt_signed_tag_mode(const struct option *opt,
30 const char *arg, int unset)
31{
32 if (unset || !strcmp(arg, "abort"))
33 signed_tag_mode = ABORT;
ee4bc371
JS
34 else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
35 signed_tag_mode = VERBATIM;
f2dc849e
JS
36 else if (!strcmp(arg, "warn"))
37 signed_tag_mode = WARN;
38 else if (!strcmp(arg, "strip"))
39 signed_tag_mode = STRIP;
40 else
41 return error("Unknown signed-tag mode: %s", arg);
42 return 0;
43}
44
45static struct decoration idnums;
46static uint32_t last_idnum;
47
48static int has_unshown_parent(struct commit *commit)
49{
50 struct commit_list *parent;
51
52 for (parent = commit->parents; parent; parent = parent->next)
53 if (!(parent->item->object.flags & SHOWN) &&
54 !(parent->item->object.flags & UNINTERESTING))
55 return 1;
56 return 0;
57}
58
59/* Since intptr_t is C99, we do not use it here */
df6a7ff7 60static inline uint32_t *mark_to_ptr(uint32_t mark)
f2dc849e 61{
df6a7ff7
PB
62 return ((uint32_t *)NULL) + mark;
63}
64
65static inline uint32_t ptr_to_mark(void * mark)
66{
67 return (uint32_t *)mark - (uint32_t *)NULL;
68}
69
70static inline void mark_object(struct object *object, uint32_t mark)
71{
72 add_decoration(&idnums, object, mark_to_ptr(mark));
73}
74
75static inline void mark_next_object(struct object *object)
76{
77 mark_object(object, ++last_idnum);
f2dc849e
JS
78}
79
80static int get_object_mark(struct object *object)
81{
82 void *decoration = lookup_decoration(&idnums, object);
83 if (!decoration)
84 return 0;
df6a7ff7 85 return ptr_to_mark(decoration);
f2dc849e
JS
86}
87
88static void show_progress(void)
89{
90 static int counter = 0;
91 if (!progress)
92 return;
93 if ((++counter % progress) == 0)
94 printf("progress %d objects\n", counter);
95}
96
97static void handle_object(const unsigned char *sha1)
98{
99 unsigned long size;
100 enum object_type type;
101 char *buf;
102 struct object *object;
103
104 if (is_null_sha1(sha1))
105 return;
106
107 object = parse_object(sha1);
108 if (!object)
109 die ("Could not read blob %s", sha1_to_hex(sha1));
110
111 if (object->flags & SHOWN)
112 return;
113
114 buf = read_sha1_file(sha1, &type, &size);
115 if (!buf)
116 die ("Could not read blob %s", sha1_to_hex(sha1));
117
df6a7ff7 118 mark_next_object(object);
f2dc849e 119
6e1c2344 120 printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
b0fe0d72 121 if (size && fwrite(buf, size, 1, stdout) != 1)
f2dc849e
JS
122 die ("Could not write blob %s", sha1_to_hex(sha1));
123 printf("\n");
124
125 show_progress();
126
127 object->flags |= SHOWN;
128 free(buf);
129}
130
131static void show_filemodify(struct diff_queue_struct *q,
132 struct diff_options *options, void *data)
133{
134 int i;
135 for (i = 0; i < q->nr; i++) {
ae7c5dce 136 struct diff_filespec *ospec = q->queue[i]->one;
f2dc849e 137 struct diff_filespec *spec = q->queue[i]->two;
ae7c5dce
AG
138
139 switch (q->queue[i]->status) {
140 case DIFF_STATUS_DELETED:
f2dc849e 141 printf("D %s\n", spec->path);
ae7c5dce
AG
142 break;
143
144 case DIFF_STATUS_COPIED:
145 case DIFF_STATUS_RENAMED:
146 printf("%c \"%s\" \"%s\"\n", q->queue[i]->status,
147 ospec->path, spec->path);
148
149 if (!hashcmp(ospec->sha1, spec->sha1) &&
150 ospec->mode == spec->mode)
151 break;
152 /* fallthrough */
153
154 case DIFF_STATUS_TYPE_CHANGED:
155 case DIFF_STATUS_MODIFIED:
156 case DIFF_STATUS_ADDED:
03db4525
AG
157 /*
158 * Links refer to objects in another repositories;
159 * output the SHA-1 verbatim.
160 */
161 if (S_ISGITLINK(spec->mode))
162 printf("M %06o %s %s\n", spec->mode,
163 sha1_to_hex(spec->sha1), spec->path);
164 else {
165 struct object *object = lookup_object(spec->sha1);
166 printf("M %06o :%d %s\n", spec->mode,
167 get_object_mark(object), spec->path);
168 }
ae7c5dce
AG
169 break;
170
171 default:
172 die("Unexpected comparison status '%c' for %s, %s",
173 q->queue[i]->status,
174 ospec->path ? ospec->path : "none",
175 spec->path ? spec->path : "none");
f2dc849e
JS
176 }
177 }
178}
179
180static const char *find_encoding(const char *begin, const char *end)
181{
182 const char *needle = "\nencoding ";
183 char *bol, *eol;
184
185 bol = memmem(begin, end ? end - begin : strlen(begin),
186 needle, strlen(needle));
187 if (!bol)
188 return git_commit_encoding;
189 bol += strlen(needle);
190 eol = strchrnul(bol, '\n');
191 *eol = '\0';
192 return bol;
193}
194
195static void handle_commit(struct commit *commit, struct rev_info *rev)
196{
197 int saved_output_format = rev->diffopt.output_format;
198 const char *author, *author_end, *committer, *committer_end;
199 const char *encoding, *message;
200 char *reencoded = NULL;
201 struct commit_list *p;
202 int i;
203
204 rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
205
206 parse_commit(commit);
207 author = strstr(commit->buffer, "\nauthor ");
208 if (!author)
209 die ("Could not find author in commit %s",
210 sha1_to_hex(commit->object.sha1));
211 author++;
212 author_end = strchrnul(author, '\n');
213 committer = strstr(author_end, "\ncommitter ");
214 if (!committer)
215 die ("Could not find committer in commit %s",
216 sha1_to_hex(commit->object.sha1));
217 committer++;
218 committer_end = strchrnul(committer, '\n');
219 message = strstr(committer_end, "\n\n");
220 encoding = find_encoding(committer_end, message);
221 if (message)
222 message += 2;
223
224 if (commit->parents) {
225 parse_commit(commit->parents->item);
226 diff_tree_sha1(commit->parents->item->tree->object.sha1,
227 commit->tree->object.sha1, "", &rev->diffopt);
228 }
229 else
230 diff_root_tree_sha1(commit->tree->object.sha1,
231 "", &rev->diffopt);
232
03db4525 233 /* Export the referenced blobs, and remember the marks. */
f2dc849e 234 for (i = 0; i < diff_queued_diff.nr; i++)
03db4525
AG
235 if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
236 handle_object(diff_queued_diff.queue[i]->two->sha1);
f2dc849e 237
df6a7ff7 238 mark_next_object(&commit->object);
f2dc849e
JS
239 if (!is_encoding_utf8(encoding))
240 reencoded = reencode_string(message, "UTF-8", encoding);
d8933f01
SP
241 if (!commit->parents)
242 printf("reset %s\n", (const char*)commit->util);
6e1c2344 243 printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
f2dc849e
JS
244 (const char *)commit->util, last_idnum,
245 (int)(author_end - author), author,
246 (int)(committer_end - committer), committer,
247 (unsigned)(reencoded
248 ? strlen(reencoded) : message
249 ? strlen(message) : 0),
250 reencoded ? reencoded : message ? message : "");
8e0f7003 251 free(reencoded);
f2dc849e
JS
252
253 for (i = 0, p = commit->parents; p; p = p->next) {
254 int mark = get_object_mark(&p->item->object);
255 if (!mark)
256 continue;
257 if (i == 0)
258 printf("from :%d\n", mark);
f2dc849e 259 else
5070b49e 260 printf("merge :%d\n", mark);
f2dc849e
JS
261 i++;
262 }
f2dc849e
JS
263
264 log_tree_diff_flush(rev);
265 rev->diffopt.output_format = saved_output_format;
266
267 printf("\n");
268
269 show_progress();
270}
271
272static void handle_tail(struct object_array *commits, struct rev_info *revs)
273{
274 struct commit *commit;
275 while (commits->nr) {
276 commit = (struct commit *)commits->objects[commits->nr - 1].item;
277 if (has_unshown_parent(commit))
278 return;
279 handle_commit(commit, revs);
280 commits->nr--;
281 }
282}
283
284static void handle_tag(const char *name, struct tag *tag)
285{
286 unsigned long size;
287 enum object_type type;
288 char *buf;
289 const char *tagger, *tagger_end, *message;
290 size_t message_size = 0;
291
292 buf = read_sha1_file(tag->object.sha1, &type, &size);
293 if (!buf)
294 die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
295 message = memmem(buf, size, "\n\n", 2);
296 if (message) {
297 message += 2;
298 message_size = strlen(message);
299 }
300 tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
4e46a8d6
JS
301 if (!tagger) {
302 if (fake_missing_tagger)
303 tagger = "tagger Unspecified Tagger "
304 "<unspecified-tagger> 0 +0000";
305 else
306 tagger = "";
307 tagger_end = tagger + strlen(tagger);
308 } else {
309 tagger++;
310 tagger_end = strchrnul(tagger, '\n');
311 }
f2dc849e
JS
312
313 /* handle signed tags */
314 if (message) {
315 const char *signature = strstr(message,
316 "\n-----BEGIN PGP SIGNATURE-----\n");
317 if (signature)
318 switch(signed_tag_mode) {
319 case ABORT:
320 die ("Encountered signed tag %s; use "
321 "--signed-tag=<mode> to handle it.",
322 sha1_to_hex(tag->object.sha1));
323 case WARN:
324 warning ("Exporting signed tag %s",
325 sha1_to_hex(tag->object.sha1));
326 /* fallthru */
ee4bc371 327 case VERBATIM:
f2dc849e
JS
328 break;
329 case STRIP:
330 message_size = signature + 1 - message;
331 break;
332 }
333 }
334
335 if (!prefixcmp(name, "refs/tags/"))
336 name += 10;
4e46a8d6 337 printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
f2dc849e
JS
338 name, get_object_mark(tag->tagged),
339 (int)(tagger_end - tagger), tagger,
4e46a8d6 340 tagger == tagger_end ? "" : "\n",
f2dc849e
JS
341 (int)message_size, (int)message_size, message ? message : "");
342}
343
344static void get_tags_and_duplicates(struct object_array *pending,
c455c87c 345 struct string_list *extra_refs)
f2dc849e
JS
346{
347 struct tag *tag;
348 int i;
349
350 for (i = 0; i < pending->nr; i++) {
351 struct object_array_entry *e = pending->objects + i;
352 unsigned char sha1[20];
353 struct commit *commit = commit;
354 char *full_name;
355
356 if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
357 continue;
358
359 switch (e->item->type) {
360 case OBJ_COMMIT:
361 commit = (struct commit *)e->item;
362 break;
363 case OBJ_TAG:
364 tag = (struct tag *)e->item;
365 while (tag && tag->object.type == OBJ_TAG) {
2075ffb5 366 string_list_append(full_name, extra_refs)->util = tag;
f2dc849e
JS
367 tag = (struct tag *)tag->tagged;
368 }
369 if (!tag)
370 die ("Tag %s points nowhere?", e->name);
371 switch(tag->object.type) {
372 case OBJ_COMMIT:
373 commit = (struct commit *)tag;
374 break;
375 case OBJ_BLOB:
376 handle_object(tag->object.sha1);
377 continue;
378 }
379 break;
380 default:
2d07f6d4
EFL
381 warning("%s: Unexpected object of type %s, skipping.",
382 e->name,
383 typename(e->item->type));
384 continue;
f2dc849e
JS
385 }
386 if (commit->util)
387 /* more than one name for the same object */
2075ffb5 388 string_list_append(full_name, extra_refs)->util = commit;
f2dc849e
JS
389 else
390 commit->util = full_name;
391 }
392}
393
c455c87c 394static void handle_tags_and_duplicates(struct string_list *extra_refs)
f2dc849e
JS
395{
396 struct commit *commit;
397 int i;
398
399 for (i = extra_refs->nr - 1; i >= 0; i--) {
c455c87c 400 const char *name = extra_refs->items[i].string;
f2dc849e
JS
401 struct object *object = extra_refs->items[i].util;
402 switch (object->type) {
403 case OBJ_TAG:
404 handle_tag(name, (struct tag *)object);
405 break;
406 case OBJ_COMMIT:
407 /* create refs pointing to already seen commits */
408 commit = (struct commit *)object;
409 printf("reset %s\nfrom :%d\n\n", name,
410 get_object_mark(&commit->object));
411 show_progress();
412 break;
413 }
414 }
415}
416
df6a7ff7
PB
417static void export_marks(char *file)
418{
419 unsigned int i;
420 uint32_t mark;
421 struct object_decoration *deco = idnums.hash;
422 FILE *f;
423
424 f = fopen(file, "w");
425 if (!f)
426 error("Unable to open marks file %s for writing", file);
427
69913575
JH
428 for (i = 0; i < idnums.size; i++) {
429 if (deco->base && deco->base->type == 1) {
df6a7ff7 430 mark = ptr_to_mark(deco->decoration);
d47fb8b0
RJ
431 fprintf(f, ":%"PRIu32" %s\n", mark,
432 sha1_to_hex(deco->base->sha1));
df6a7ff7 433 }
69913575 434 deco++;
df6a7ff7
PB
435 }
436
437 if (ferror(f) || fclose(f))
438 error("Unable to write marks file %s.", file);
439}
440
69913575 441static void import_marks(char *input_file)
df6a7ff7
PB
442{
443 char line[512];
444 FILE *f = fopen(input_file, "r");
445 if (!f)
446 die("cannot read %s: %s", input_file, strerror(errno));
447
448 while (fgets(line, sizeof(line), f)) {
449 uint32_t mark;
450 char *line_end, *mark_end;
451 unsigned char sha1[20];
452 struct object *object;
453
454 line_end = strchr(line, '\n');
455 if (line[0] != ':' || !line_end)
456 die("corrupt mark line: %s", line);
69913575 457 *line_end = '\0';
df6a7ff7
PB
458
459 mark = strtoumax(line + 1, &mark_end, 10);
460 if (!mark || mark_end == line + 1
461 || *mark_end != ' ' || get_sha1(mark_end + 1, sha1))
462 die("corrupt mark line: %s", line);
463
464 object = parse_object(sha1);
465 if (!object)
466 die ("Could not read blob %s", sha1_to_hex(sha1));
467
468 if (object->flags & SHOWN)
469 error("Object %s already has a mark", sha1);
470
471 mark_object(object, mark);
472 if (last_idnum < mark)
473 last_idnum = mark;
474
475 object->flags |= SHOWN;
476 }
477 fclose(f);
478}
479
f2dc849e
JS
480int cmd_fast_export(int argc, const char **argv, const char *prefix)
481{
482 struct rev_info revs;
483 struct object_array commits = { 0, 0, NULL };
c455c87c 484 struct string_list extra_refs = { NULL, 0, 0, 0 };
f2dc849e 485 struct commit *commit;
df6a7ff7 486 char *export_filename = NULL, *import_filename = NULL;
f2dc849e
JS
487 struct option options[] = {
488 OPT_INTEGER(0, "progress", &progress,
489 "show progress after <n> objects"),
490 OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
491 "select handling of signed tags",
492 parse_opt_signed_tag_mode),
df6a7ff7
PB
493 OPT_STRING(0, "export-marks", &export_filename, "FILE",
494 "Dump marks to this file"),
495 OPT_STRING(0, "import-marks", &import_filename, "FILE",
496 "Import marks from this file"),
4e46a8d6
JS
497 OPT_BOOLEAN(0, "fake-missing-tagger", &fake_missing_tagger,
498 "Fake a tagger when tags lack one"),
f2dc849e
JS
499 OPT_END()
500 };
501
dcfdbdf0
MV
502 if (argc == 1)
503 usage_with_options (fast_export_usage, options);
504
f2dc849e 505 /* we handle encodings */
ef90d6d4 506 git_config(git_default_config, NULL);
f2dc849e
JS
507
508 init_revisions(&revs, prefix);
509 argc = setup_revisions(argc, argv, &revs, NULL);
510 argc = parse_options(argc, argv, options, fast_export_usage, 0);
511 if (argc > 1)
512 usage_with_options (fast_export_usage, options);
513
df6a7ff7
PB
514 if (import_filename)
515 import_marks(import_filename);
516
f2dc849e
JS
517 get_tags_and_duplicates(&revs.pending, &extra_refs);
518
784f8aff 519 revs.topo_order = 1;
3d51e1b5
MK
520 if (prepare_revision_walk(&revs))
521 die("revision walk setup failed");
f2dc849e
JS
522 revs.diffopt.format_callback = show_filemodify;
523 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
524 while ((commit = get_revision(&revs))) {
525 if (has_unshown_parent(commit)) {
526 struct commit_list *parent = commit->parents;
527 add_object_array(&commit->object, NULL, &commits);
528 for (; parent; parent = parent->next)
529 if (!parent->item->util)
530 parent->item->util = commit->util;
531 }
532 else {
533 handle_commit(commit, &revs);
534 handle_tail(&commits, &revs);
535 }
536 }
537
538 handle_tags_and_duplicates(&extra_refs);
539
df6a7ff7
PB
540 if (export_filename)
541 export_marks(export_filename);
542
f2dc849e
JS
543 return 0;
544}