builtin-fast-export.con commit builtin-fast-export: Only output a single parent per line (5070b49)
   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 "path-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 { VERBATIM, WARN, STRIP, ABORT } signed_tag_mode = ABORT;
  27
  28static int parse_opt_signed_tag_mode(const struct option *opt,
  29                                     const char *arg, int unset)
  30{
  31        if (unset || !strcmp(arg, "abort"))
  32                signed_tag_mode = ABORT;
  33        else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
  34                signed_tag_mode = VERBATIM;
  35        else if (!strcmp(arg, "warn"))
  36                signed_tag_mode = WARN;
  37        else if (!strcmp(arg, "strip"))
  38                signed_tag_mode = STRIP;
  39        else
  40                return error("Unknown signed-tag mode: %s", arg);
  41        return 0;
  42}
  43
  44static struct decoration idnums;
  45static uint32_t last_idnum;
  46
  47static int has_unshown_parent(struct commit *commit)
  48{
  49        struct commit_list *parent;
  50
  51        for (parent = commit->parents; parent; parent = parent->next)
  52                if (!(parent->item->object.flags & SHOWN) &&
  53                    !(parent->item->object.flags & UNINTERESTING))
  54                        return 1;
  55        return 0;
  56}
  57
  58/* Since intptr_t is C99, we do not use it here */
  59static void mark_object(struct object *object)
  60{
  61        last_idnum++;
  62        add_decoration(&idnums, object, ((uint32_t *)NULL) + last_idnum);
  63}
  64
  65static int get_object_mark(struct object *object)
  66{
  67        void *decoration = lookup_decoration(&idnums, object);
  68        if (!decoration)
  69                return 0;
  70        return (uint32_t *)decoration - (uint32_t *)NULL;
  71}
  72
  73static void show_progress(void)
  74{
  75        static int counter = 0;
  76        if (!progress)
  77                return;
  78        if ((++counter % progress) == 0)
  79                printf("progress %d objects\n", counter);
  80}
  81
  82static void handle_object(const unsigned char *sha1)
  83{
  84        unsigned long size;
  85        enum object_type type;
  86        char *buf;
  87        struct object *object;
  88
  89        if (is_null_sha1(sha1))
  90                return;
  91
  92        object = parse_object(sha1);
  93        if (!object)
  94                die ("Could not read blob %s", sha1_to_hex(sha1));
  95
  96        if (object->flags & SHOWN)
  97                return;
  98
  99        buf = read_sha1_file(sha1, &type, &size);
 100        if (!buf)
 101                die ("Could not read blob %s", sha1_to_hex(sha1));
 102
 103        mark_object(object);
 104
 105        printf("blob\nmark :%d\ndata %lu\n", last_idnum, size);
 106        if (size && fwrite(buf, size, 1, stdout) != 1)
 107                die ("Could not write blob %s", sha1_to_hex(sha1));
 108        printf("\n");
 109
 110        show_progress();
 111
 112        object->flags |= SHOWN;
 113        free(buf);
 114}
 115
 116static void show_filemodify(struct diff_queue_struct *q,
 117                            struct diff_options *options, void *data)
 118{
 119        int i;
 120        for (i = 0; i < q->nr; i++) {
 121                struct diff_filespec *spec = q->queue[i]->two;
 122                if (is_null_sha1(spec->sha1))
 123                        printf("D %s\n", spec->path);
 124                else {
 125                        struct object *object = lookup_object(spec->sha1);
 126                        printf("M %06o :%d %s\n", spec->mode,
 127                               get_object_mark(object), spec->path);
 128                }
 129        }
 130}
 131
 132static const char *find_encoding(const char *begin, const char *end)
 133{
 134        const char *needle = "\nencoding ";
 135        char *bol, *eol;
 136
 137        bol = memmem(begin, end ? end - begin : strlen(begin),
 138                     needle, strlen(needle));
 139        if (!bol)
 140                return git_commit_encoding;
 141        bol += strlen(needle);
 142        eol = strchrnul(bol, '\n');
 143        *eol = '\0';
 144        return bol;
 145}
 146
 147static void handle_commit(struct commit *commit, struct rev_info *rev)
 148{
 149        int saved_output_format = rev->diffopt.output_format;
 150        const char *author, *author_end, *committer, *committer_end;
 151        const char *encoding, *message;
 152        char *reencoded = NULL;
 153        struct commit_list *p;
 154        int i;
 155
 156        rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
 157
 158        parse_commit(commit);
 159        author = strstr(commit->buffer, "\nauthor ");
 160        if (!author)
 161                die ("Could not find author in commit %s",
 162                     sha1_to_hex(commit->object.sha1));
 163        author++;
 164        author_end = strchrnul(author, '\n');
 165        committer = strstr(author_end, "\ncommitter ");
 166        if (!committer)
 167                die ("Could not find committer in commit %s",
 168                     sha1_to_hex(commit->object.sha1));
 169        committer++;
 170        committer_end = strchrnul(committer, '\n');
 171        message = strstr(committer_end, "\n\n");
 172        encoding = find_encoding(committer_end, message);
 173        if (message)
 174                message += 2;
 175
 176        if (commit->parents) {
 177                parse_commit(commit->parents->item);
 178                diff_tree_sha1(commit->parents->item->tree->object.sha1,
 179                               commit->tree->object.sha1, "", &rev->diffopt);
 180        }
 181        else
 182                diff_root_tree_sha1(commit->tree->object.sha1,
 183                                    "", &rev->diffopt);
 184
 185        for (i = 0; i < diff_queued_diff.nr; i++)
 186                handle_object(diff_queued_diff.queue[i]->two->sha1);
 187
 188        mark_object(&commit->object);
 189        if (!is_encoding_utf8(encoding))
 190                reencoded = reencode_string(message, "UTF-8", encoding);
 191        printf("commit %s\nmark :%d\n%.*s\n%.*s\ndata %u\n%s",
 192               (const char *)commit->util, last_idnum,
 193               (int)(author_end - author), author,
 194               (int)(committer_end - committer), committer,
 195               (unsigned)(reencoded
 196                          ? strlen(reencoded) : message
 197                          ? strlen(message) : 0),
 198               reencoded ? reencoded : message ? message : "");
 199        if (reencoded)
 200                free(reencoded);
 201
 202        for (i = 0, p = commit->parents; p; p = p->next) {
 203                int mark = get_object_mark(&p->item->object);
 204                if (!mark)
 205                        continue;
 206                if (i == 0)
 207                        printf("from :%d\n", mark);
 208                else
 209                        printf("merge :%d\n", mark);
 210                i++;
 211        }
 212
 213        log_tree_diff_flush(rev);
 214        rev->diffopt.output_format = saved_output_format;
 215
 216        printf("\n");
 217
 218        show_progress();
 219}
 220
 221static void handle_tail(struct object_array *commits, struct rev_info *revs)
 222{
 223        struct commit *commit;
 224        while (commits->nr) {
 225                commit = (struct commit *)commits->objects[commits->nr - 1].item;
 226                if (has_unshown_parent(commit))
 227                        return;
 228                handle_commit(commit, revs);
 229                commits->nr--;
 230        }
 231}
 232
 233static void handle_tag(const char *name, struct tag *tag)
 234{
 235        unsigned long size;
 236        enum object_type type;
 237        char *buf;
 238        const char *tagger, *tagger_end, *message;
 239        size_t message_size = 0;
 240
 241        buf = read_sha1_file(tag->object.sha1, &type, &size);
 242        if (!buf)
 243                die ("Could not read tag %s", sha1_to_hex(tag->object.sha1));
 244        message = memmem(buf, size, "\n\n", 2);
 245        if (message) {
 246                message += 2;
 247                message_size = strlen(message);
 248        }
 249        tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
 250        if (!tagger)
 251                die ("No tagger for tag %s", sha1_to_hex(tag->object.sha1));
 252        tagger++;
 253        tagger_end = strchrnul(tagger, '\n');
 254
 255        /* handle signed tags */
 256        if (message) {
 257                const char *signature = strstr(message,
 258                                               "\n-----BEGIN PGP SIGNATURE-----\n");
 259                if (signature)
 260                        switch(signed_tag_mode) {
 261                        case ABORT:
 262                                die ("Encountered signed tag %s; use "
 263                                     "--signed-tag=<mode> to handle it.",
 264                                     sha1_to_hex(tag->object.sha1));
 265                        case WARN:
 266                                warning ("Exporting signed tag %s",
 267                                         sha1_to_hex(tag->object.sha1));
 268                                /* fallthru */
 269                        case VERBATIM:
 270                                break;
 271                        case STRIP:
 272                                message_size = signature + 1 - message;
 273                                break;
 274                        }
 275        }
 276
 277        if (!prefixcmp(name, "refs/tags/"))
 278                name += 10;
 279        printf("tag %s\nfrom :%d\n%.*s\ndata %d\n%.*s\n",
 280               name, get_object_mark(tag->tagged),
 281               (int)(tagger_end - tagger), tagger,
 282               (int)message_size, (int)message_size, message ? message : "");
 283}
 284
 285static void get_tags_and_duplicates(struct object_array *pending,
 286                                    struct path_list *extra_refs)
 287{
 288        struct tag *tag;
 289        int i;
 290
 291        for (i = 0; i < pending->nr; i++) {
 292                struct object_array_entry *e = pending->objects + i;
 293                unsigned char sha1[20];
 294                struct commit *commit = commit;
 295                char *full_name;
 296
 297                if (dwim_ref(e->name, strlen(e->name), sha1, &full_name) != 1)
 298                        continue;
 299
 300                switch (e->item->type) {
 301                case OBJ_COMMIT:
 302                        commit = (struct commit *)e->item;
 303                        break;
 304                case OBJ_TAG:
 305                        tag = (struct tag *)e->item;
 306                        while (tag && tag->object.type == OBJ_TAG) {
 307                                path_list_insert(full_name, extra_refs)->util = tag;
 308                                tag = (struct tag *)tag->tagged;
 309                        }
 310                        if (!tag)
 311                                die ("Tag %s points nowhere?", e->name);
 312                        switch(tag->object.type) {
 313                        case OBJ_COMMIT:
 314                                commit = (struct commit *)tag;
 315                                break;
 316                        case OBJ_BLOB:
 317                                handle_object(tag->object.sha1);
 318                                continue;
 319                        }
 320                        break;
 321                default:
 322                        die ("Unexpected object of type %s",
 323                             typename(e->item->type));
 324                }
 325                if (commit->util)
 326                        /* more than one name for the same object */
 327                        path_list_insert(full_name, extra_refs)->util = commit;
 328                else
 329                        commit->util = full_name;
 330        }
 331}
 332
 333static void handle_tags_and_duplicates(struct path_list *extra_refs)
 334{
 335        struct commit *commit;
 336        int i;
 337
 338        for (i = extra_refs->nr - 1; i >= 0; i--) {
 339                const char *name = extra_refs->items[i].path;
 340                struct object *object = extra_refs->items[i].util;
 341                switch (object->type) {
 342                case OBJ_TAG:
 343                        handle_tag(name, (struct tag *)object);
 344                        break;
 345                case OBJ_COMMIT:
 346                        /* create refs pointing to already seen commits */
 347                        commit = (struct commit *)object;
 348                        printf("reset %s\nfrom :%d\n\n", name,
 349                               get_object_mark(&commit->object));
 350                        show_progress();
 351                        break;
 352                }
 353        }
 354}
 355
 356int cmd_fast_export(int argc, const char **argv, const char *prefix)
 357{
 358        struct rev_info revs;
 359        struct object_array commits = { 0, 0, NULL };
 360        struct path_list extra_refs = { NULL, 0, 0, 0 };
 361        struct commit *commit;
 362        struct option options[] = {
 363                OPT_INTEGER(0, "progress", &progress,
 364                            "show progress after <n> objects"),
 365                OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
 366                             "select handling of signed tags",
 367                             parse_opt_signed_tag_mode),
 368                OPT_END()
 369        };
 370
 371        /* we handle encodings */
 372        git_config(git_default_config);
 373
 374        init_revisions(&revs, prefix);
 375        argc = setup_revisions(argc, argv, &revs, NULL);
 376        argc = parse_options(argc, argv, options, fast_export_usage, 0);
 377        if (argc > 1)
 378                usage_with_options (fast_export_usage, options);
 379
 380        get_tags_and_duplicates(&revs.pending, &extra_refs);
 381
 382        prepare_revision_walk(&revs);
 383        revs.diffopt.format_callback = show_filemodify;
 384        DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
 385        while ((commit = get_revision(&revs))) {
 386                if (has_unshown_parent(commit)) {
 387                        struct commit_list *parent = commit->parents;
 388                        add_object_array(&commit->object, NULL, &commits);
 389                        for (; parent; parent = parent->next)
 390                                if (!parent->item->util)
 391                                        parent->item->util = commit->util;
 392                }
 393                else {
 394                        handle_commit(commit, &revs);
 395                        handle_tail(&commits, &revs);
 396                }
 397        }
 398
 399        handle_tags_and_duplicates(&extra_refs);
 400
 401        return 0;
 402}