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