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