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