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