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