builtin / fast-export.con commit commit: allow lookup_commit_graft to handle arbitrary repositories (b9dbddf)
   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 "config.h"
   9#include "refs.h"
  10#include "object-store.h"
  11#include "commit.h"
  12#include "object.h"
  13#include "tag.h"
  14#include "diff.h"
  15#include "diffcore.h"
  16#include "log-tree.h"
  17#include "revision.h"
  18#include "decorate.h"
  19#include "string-list.h"
  20#include "utf8.h"
  21#include "parse-options.h"
  22#include "quote.h"
  23#include "remote.h"
  24#include "blob.h"
  25
  26static const char *fast_export_usage[] = {
  27        N_("git fast-export [rev-list-opts]"),
  28        NULL
  29};
  30
  31static int progress;
  32static enum { ABORT, VERBATIM, WARN, WARN_STRIP, STRIP } signed_tag_mode = ABORT;
  33static enum { ERROR, DROP, REWRITE } tag_of_filtered_mode = ERROR;
  34static int fake_missing_tagger;
  35static int use_done_feature;
  36static int no_data;
  37static int full_tree;
  38static struct string_list extra_refs = STRING_LIST_INIT_NODUP;
  39static struct refspec *refspecs;
  40static int refspecs_nr;
  41static int anonymize;
  42
  43static int parse_opt_signed_tag_mode(const struct option *opt,
  44                                     const char *arg, int unset)
  45{
  46        if (unset || !strcmp(arg, "abort"))
  47                signed_tag_mode = ABORT;
  48        else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
  49                signed_tag_mode = VERBATIM;
  50        else if (!strcmp(arg, "warn"))
  51                signed_tag_mode = WARN;
  52        else if (!strcmp(arg, "warn-strip"))
  53                signed_tag_mode = WARN_STRIP;
  54        else if (!strcmp(arg, "strip"))
  55                signed_tag_mode = STRIP;
  56        else
  57                return error("Unknown signed-tags mode: %s", arg);
  58        return 0;
  59}
  60
  61static int parse_opt_tag_of_filtered_mode(const struct option *opt,
  62                                          const char *arg, int unset)
  63{
  64        if (unset || !strcmp(arg, "abort"))
  65                tag_of_filtered_mode = ERROR;
  66        else if (!strcmp(arg, "drop"))
  67                tag_of_filtered_mode = DROP;
  68        else if (!strcmp(arg, "rewrite"))
  69                tag_of_filtered_mode = REWRITE;
  70        else
  71                return error("Unknown tag-of-filtered mode: %s", arg);
  72        return 0;
  73}
  74
  75static struct decoration idnums;
  76static uint32_t last_idnum;
  77
  78static int has_unshown_parent(struct commit *commit)
  79{
  80        struct commit_list *parent;
  81
  82        for (parent = commit->parents; parent; parent = parent->next)
  83                if (!(parent->item->object.flags & SHOWN) &&
  84                    !(parent->item->object.flags & UNINTERESTING))
  85                        return 1;
  86        return 0;
  87}
  88
  89struct anonymized_entry {
  90        struct hashmap_entry hash;
  91        const char *orig;
  92        size_t orig_len;
  93        const char *anon;
  94        size_t anon_len;
  95};
  96
  97static int anonymized_entry_cmp(const void *unused_cmp_data,
  98                                const void *va, const void *vb,
  99                                const void *unused_keydata)
 100{
 101        const struct anonymized_entry *a = va, *b = vb;
 102        return a->orig_len != b->orig_len ||
 103                memcmp(a->orig, b->orig, a->orig_len);
 104}
 105
 106/*
 107 * Basically keep a cache of X->Y so that we can repeatedly replace
 108 * the same anonymized string with another. The actual generation
 109 * is farmed out to the generate function.
 110 */
 111static const void *anonymize_mem(struct hashmap *map,
 112                                 void *(*generate)(const void *, size_t *),
 113                                 const void *orig, size_t *len)
 114{
 115        struct anonymized_entry key, *ret;
 116
 117        if (!map->cmpfn)
 118                hashmap_init(map, anonymized_entry_cmp, NULL, 0);
 119
 120        hashmap_entry_init(&key, memhash(orig, *len));
 121        key.orig = orig;
 122        key.orig_len = *len;
 123        ret = hashmap_get(map, &key, NULL);
 124
 125        if (!ret) {
 126                ret = xmalloc(sizeof(*ret));
 127                hashmap_entry_init(&ret->hash, key.hash.hash);
 128                ret->orig = xstrdup(orig);
 129                ret->orig_len = *len;
 130                ret->anon = generate(orig, len);
 131                ret->anon_len = *len;
 132                hashmap_put(map, ret);
 133        }
 134
 135        *len = ret->anon_len;
 136        return ret->anon;
 137}
 138
 139/*
 140 * We anonymize each component of a path individually,
 141 * so that paths a/b and a/c will share a common root.
 142 * The paths are cached via anonymize_mem so that repeated
 143 * lookups for "a" will yield the same value.
 144 */
 145static void anonymize_path(struct strbuf *out, const char *path,
 146                           struct hashmap *map,
 147                           void *(*generate)(const void *, size_t *))
 148{
 149        while (*path) {
 150                const char *end_of_component = strchrnul(path, '/');
 151                size_t len = end_of_component - path;
 152                const char *c = anonymize_mem(map, generate, path, &len);
 153                strbuf_add(out, c, len);
 154                path = end_of_component;
 155                if (*path)
 156                        strbuf_addch(out, *path++);
 157        }
 158}
 159
 160/* Since intptr_t is C99, we do not use it here */
 161static inline uint32_t *mark_to_ptr(uint32_t mark)
 162{
 163        return ((uint32_t *)NULL) + mark;
 164}
 165
 166static inline uint32_t ptr_to_mark(void * mark)
 167{
 168        return (uint32_t *)mark - (uint32_t *)NULL;
 169}
 170
 171static inline void mark_object(struct object *object, uint32_t mark)
 172{
 173        add_decoration(&idnums, object, mark_to_ptr(mark));
 174}
 175
 176static inline void mark_next_object(struct object *object)
 177{
 178        mark_object(object, ++last_idnum);
 179}
 180
 181static int get_object_mark(struct object *object)
 182{
 183        void *decoration = lookup_decoration(&idnums, object);
 184        if (!decoration)
 185                return 0;
 186        return ptr_to_mark(decoration);
 187}
 188
 189static void show_progress(void)
 190{
 191        static int counter = 0;
 192        if (!progress)
 193                return;
 194        if ((++counter % progress) == 0)
 195                printf("progress %d objects\n", counter);
 196}
 197
 198/*
 199 * Ideally we would want some transformation of the blob data here
 200 * that is unreversible, but would still be the same size and have
 201 * the same data relationship to other blobs (so that we get the same
 202 * delta and packing behavior as the original). But the first and last
 203 * requirements there are probably mutually exclusive, so let's take
 204 * the easy way out for now, and just generate arbitrary content.
 205 *
 206 * There's no need to cache this result with anonymize_mem, since
 207 * we already handle blob content caching with marks.
 208 */
 209static char *anonymize_blob(unsigned long *size)
 210{
 211        static int counter;
 212        struct strbuf out = STRBUF_INIT;
 213        strbuf_addf(&out, "anonymous blob %d", counter++);
 214        *size = out.len;
 215        return strbuf_detach(&out, NULL);
 216}
 217
 218static void export_blob(const struct object_id *oid)
 219{
 220        unsigned long size;
 221        enum object_type type;
 222        char *buf;
 223        struct object *object;
 224        int eaten;
 225
 226        if (no_data)
 227                return;
 228
 229        if (is_null_oid(oid))
 230                return;
 231
 232        object = lookup_object(oid->hash);
 233        if (object && object->flags & SHOWN)
 234                return;
 235
 236        if (anonymize) {
 237                buf = anonymize_blob(&size);
 238                object = (struct object *)lookup_blob(oid);
 239                eaten = 0;
 240        } else {
 241                buf = read_object_file(oid, &type, &size);
 242                if (!buf)
 243                        die ("Could not read blob %s", oid_to_hex(oid));
 244                if (check_object_signature(oid, buf, size, type_name(type)) < 0)
 245                        die("sha1 mismatch in blob %s", oid_to_hex(oid));
 246                object = parse_object_buffer(oid, type, size, buf, &eaten);
 247        }
 248
 249        if (!object)
 250                die("Could not read blob %s", oid_to_hex(oid));
 251
 252        mark_next_object(object);
 253
 254        printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
 255        if (size && fwrite(buf, size, 1, stdout) != 1)
 256                die_errno ("Could not write blob '%s'", oid_to_hex(oid));
 257        printf("\n");
 258
 259        show_progress();
 260
 261        object->flags |= SHOWN;
 262        if (!eaten)
 263                free(buf);
 264}
 265
 266static int depth_first(const void *a_, const void *b_)
 267{
 268        const struct diff_filepair *a = *((const struct diff_filepair **)a_);
 269        const struct diff_filepair *b = *((const struct diff_filepair **)b_);
 270        const char *name_a, *name_b;
 271        int len_a, len_b, len;
 272        int cmp;
 273
 274        name_a = a->one ? a->one->path : a->two->path;
 275        name_b = b->one ? b->one->path : b->two->path;
 276
 277        len_a = strlen(name_a);
 278        len_b = strlen(name_b);
 279        len = (len_a < len_b) ? len_a : len_b;
 280
 281        /* strcmp will sort 'd' before 'd/e', we want 'd/e' before 'd' */
 282        cmp = memcmp(name_a, name_b, len);
 283        if (cmp)
 284                return cmp;
 285        cmp = len_b - len_a;
 286        if (cmp)
 287                return cmp;
 288        /*
 289         * Move 'R'ename entries last so that all references of the file
 290         * appear in the output before it is renamed (e.g., when a file
 291         * was copied and renamed in the same commit).
 292         */
 293        return (a->status == 'R') - (b->status == 'R');
 294}
 295
 296static void print_path_1(const char *path)
 297{
 298        int need_quote = quote_c_style(path, NULL, NULL, 0);
 299        if (need_quote)
 300                quote_c_style(path, NULL, stdout, 0);
 301        else if (strchr(path, ' '))
 302                printf("\"%s\"", path);
 303        else
 304                printf("%s", path);
 305}
 306
 307static void *anonymize_path_component(const void *path, size_t *len)
 308{
 309        static int counter;
 310        struct strbuf out = STRBUF_INIT;
 311        strbuf_addf(&out, "path%d", counter++);
 312        return strbuf_detach(&out, len);
 313}
 314
 315static void print_path(const char *path)
 316{
 317        if (!anonymize)
 318                print_path_1(path);
 319        else {
 320                static struct hashmap paths;
 321                static struct strbuf anon = STRBUF_INIT;
 322
 323                anonymize_path(&anon, path, &paths, anonymize_path_component);
 324                print_path_1(anon.buf);
 325                strbuf_reset(&anon);
 326        }
 327}
 328
 329static void *generate_fake_oid(const void *old, size_t *len)
 330{
 331        static uint32_t counter = 1; /* avoid null sha1 */
 332        unsigned char *out = xcalloc(GIT_SHA1_RAWSZ, 1);
 333        put_be32(out + GIT_SHA1_RAWSZ - 4, counter++);
 334        return out;
 335}
 336
 337static const unsigned char *anonymize_sha1(const struct object_id *oid)
 338{
 339        static struct hashmap sha1s;
 340        size_t len = GIT_SHA1_RAWSZ;
 341        return anonymize_mem(&sha1s, generate_fake_oid, oid, &len);
 342}
 343
 344static void show_filemodify(struct diff_queue_struct *q,
 345                            struct diff_options *options, void *data)
 346{
 347        int i;
 348        struct string_list *changed = data;
 349
 350        /*
 351         * Handle files below a directory first, in case they are all deleted
 352         * and the directory changes to a file or symlink.
 353         */
 354        QSORT(q->queue, q->nr, depth_first);
 355
 356        for (i = 0; i < q->nr; i++) {
 357                struct diff_filespec *ospec = q->queue[i]->one;
 358                struct diff_filespec *spec = q->queue[i]->two;
 359
 360                switch (q->queue[i]->status) {
 361                case DIFF_STATUS_DELETED:
 362                        printf("D ");
 363                        print_path(spec->path);
 364                        string_list_insert(changed, spec->path);
 365                        putchar('\n');
 366                        break;
 367
 368                case DIFF_STATUS_COPIED:
 369                case DIFF_STATUS_RENAMED:
 370                        /*
 371                         * If a change in the file corresponding to ospec->path
 372                         * has been observed, we cannot trust its contents
 373                         * because the diff is calculated based on the prior
 374                         * contents, not the current contents.  So, declare a
 375                         * copy or rename only if there was no change observed.
 376                         */
 377                        if (!string_list_has_string(changed, ospec->path)) {
 378                                printf("%c ", q->queue[i]->status);
 379                                print_path(ospec->path);
 380                                putchar(' ');
 381                                print_path(spec->path);
 382                                string_list_insert(changed, spec->path);
 383                                putchar('\n');
 384
 385                                if (!oidcmp(&ospec->oid, &spec->oid) &&
 386                                    ospec->mode == spec->mode)
 387                                        break;
 388                        }
 389                        /* fallthrough */
 390
 391                case DIFF_STATUS_TYPE_CHANGED:
 392                case DIFF_STATUS_MODIFIED:
 393                case DIFF_STATUS_ADDED:
 394                        /*
 395                         * Links refer to objects in another repositories;
 396                         * output the SHA-1 verbatim.
 397                         */
 398                        if (no_data || S_ISGITLINK(spec->mode))
 399                                printf("M %06o %s ", spec->mode,
 400                                       sha1_to_hex(anonymize ?
 401                                                   anonymize_sha1(&spec->oid) :
 402                                                   spec->oid.hash));
 403                        else {
 404                                struct object *object = lookup_object(spec->oid.hash);
 405                                printf("M %06o :%d ", spec->mode,
 406                                       get_object_mark(object));
 407                        }
 408                        print_path(spec->path);
 409                        string_list_insert(changed, spec->path);
 410                        putchar('\n');
 411                        break;
 412
 413                default:
 414                        die("Unexpected comparison status '%c' for %s, %s",
 415                                q->queue[i]->status,
 416                                ospec->path ? ospec->path : "none",
 417                                spec->path ? spec->path : "none");
 418                }
 419        }
 420}
 421
 422static const char *find_encoding(const char *begin, const char *end)
 423{
 424        const char *needle = "\nencoding ";
 425        char *bol, *eol;
 426
 427        bol = memmem(begin, end ? end - begin : strlen(begin),
 428                     needle, strlen(needle));
 429        if (!bol)
 430                return git_commit_encoding;
 431        bol += strlen(needle);
 432        eol = strchrnul(bol, '\n');
 433        *eol = '\0';
 434        return bol;
 435}
 436
 437static void *anonymize_ref_component(const void *old, size_t *len)
 438{
 439        static int counter;
 440        struct strbuf out = STRBUF_INIT;
 441        strbuf_addf(&out, "ref%d", counter++);
 442        return strbuf_detach(&out, len);
 443}
 444
 445static const char *anonymize_refname(const char *refname)
 446{
 447        /*
 448         * If any of these prefixes is found, we will leave it intact
 449         * so that tags remain tags and so forth.
 450         */
 451        static const char *prefixes[] = {
 452                "refs/heads/",
 453                "refs/tags/",
 454                "refs/remotes/",
 455                "refs/"
 456        };
 457        static struct hashmap refs;
 458        static struct strbuf anon = STRBUF_INIT;
 459        int i;
 460
 461        /*
 462         * We also leave "master" as a special case, since it does not reveal
 463         * anything interesting.
 464         */
 465        if (!strcmp(refname, "refs/heads/master"))
 466                return refname;
 467
 468        strbuf_reset(&anon);
 469        for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
 470                if (skip_prefix(refname, prefixes[i], &refname)) {
 471                        strbuf_addstr(&anon, prefixes[i]);
 472                        break;
 473                }
 474        }
 475
 476        anonymize_path(&anon, refname, &refs, anonymize_ref_component);
 477        return anon.buf;
 478}
 479
 480/*
 481 * We do not even bother to cache commit messages, as they are unlikely
 482 * to be repeated verbatim, and it is not that interesting when they are.
 483 */
 484static char *anonymize_commit_message(const char *old)
 485{
 486        static int counter;
 487        return xstrfmt("subject %d\n\nbody\n", counter++);
 488}
 489
 490static struct hashmap idents;
 491static void *anonymize_ident(const void *old, size_t *len)
 492{
 493        static int counter;
 494        struct strbuf out = STRBUF_INIT;
 495        strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
 496        counter++;
 497        return strbuf_detach(&out, len);
 498}
 499
 500/*
 501 * Our strategy here is to anonymize the names and email addresses,
 502 * but keep timestamps intact, as they influence things like traversal
 503 * order (and by themselves should not be too revealing).
 504 */
 505static void anonymize_ident_line(const char **beg, const char **end)
 506{
 507        static struct strbuf buffers[] = { STRBUF_INIT, STRBUF_INIT };
 508        static unsigned which_buffer;
 509
 510        struct strbuf *out;
 511        struct ident_split split;
 512        const char *end_of_header;
 513
 514        out = &buffers[which_buffer++];
 515        which_buffer %= ARRAY_SIZE(buffers);
 516        strbuf_reset(out);
 517
 518        /* skip "committer", "author", "tagger", etc */
 519        end_of_header = strchr(*beg, ' ');
 520        if (!end_of_header)
 521                die("BUG: malformed line fed to anonymize_ident_line: %.*s",
 522                    (int)(*end - *beg), *beg);
 523        end_of_header++;
 524        strbuf_add(out, *beg, end_of_header - *beg);
 525
 526        if (!split_ident_line(&split, end_of_header, *end - end_of_header) &&
 527            split.date_begin) {
 528                const char *ident;
 529                size_t len;
 530
 531                len = split.mail_end - split.name_begin;
 532                ident = anonymize_mem(&idents, anonymize_ident,
 533                                      split.name_begin, &len);
 534                strbuf_add(out, ident, len);
 535                strbuf_addch(out, ' ');
 536                strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
 537        } else {
 538                strbuf_addstr(out, "Malformed Ident <malformed@example.com> 0 -0000");
 539        }
 540
 541        *beg = out->buf;
 542        *end = out->buf + out->len;
 543}
 544
 545static void handle_commit(struct commit *commit, struct rev_info *rev,
 546                          struct string_list *paths_of_changed_objects)
 547{
 548        int saved_output_format = rev->diffopt.output_format;
 549        const char *commit_buffer;
 550        const char *author, *author_end, *committer, *committer_end;
 551        const char *encoding, *message;
 552        char *reencoded = NULL;
 553        struct commit_list *p;
 554        const char *refname;
 555        int i;
 556
 557        rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
 558
 559        parse_commit_or_die(commit);
 560        commit_buffer = get_commit_buffer(commit, NULL);
 561        author = strstr(commit_buffer, "\nauthor ");
 562        if (!author)
 563                die ("Could not find author in commit %s",
 564                     oid_to_hex(&commit->object.oid));
 565        author++;
 566        author_end = strchrnul(author, '\n');
 567        committer = strstr(author_end, "\ncommitter ");
 568        if (!committer)
 569                die ("Could not find committer in commit %s",
 570                     oid_to_hex(&commit->object.oid));
 571        committer++;
 572        committer_end = strchrnul(committer, '\n');
 573        message = strstr(committer_end, "\n\n");
 574        encoding = find_encoding(committer_end, message);
 575        if (message)
 576                message += 2;
 577
 578        if (commit->parents &&
 579            get_object_mark(&commit->parents->item->object) != 0 &&
 580            !full_tree) {
 581                parse_commit_or_die(commit->parents->item);
 582                diff_tree_oid(&commit->parents->item->tree->object.oid,
 583                              &commit->tree->object.oid, "", &rev->diffopt);
 584        }
 585        else
 586                diff_root_tree_oid(&commit->tree->object.oid,
 587                                   "", &rev->diffopt);
 588
 589        /* Export the referenced blobs, and remember the marks. */
 590        for (i = 0; i < diff_queued_diff.nr; i++)
 591                if (!S_ISGITLINK(diff_queued_diff.queue[i]->two->mode))
 592                        export_blob(&diff_queued_diff.queue[i]->two->oid);
 593
 594        refname = commit->util;
 595        if (anonymize) {
 596                refname = anonymize_refname(refname);
 597                anonymize_ident_line(&committer, &committer_end);
 598                anonymize_ident_line(&author, &author_end);
 599        }
 600
 601        mark_next_object(&commit->object);
 602        if (anonymize)
 603                reencoded = anonymize_commit_message(message);
 604        else if (!is_encoding_utf8(encoding))
 605                reencoded = reencode_string(message, "UTF-8", encoding);
 606        if (!commit->parents)
 607                printf("reset %s\n", refname);
 608        printf("commit %s\nmark :%"PRIu32"\n%.*s\n%.*s\ndata %u\n%s",
 609               refname, last_idnum,
 610               (int)(author_end - author), author,
 611               (int)(committer_end - committer), committer,
 612               (unsigned)(reencoded
 613                          ? strlen(reencoded) : message
 614                          ? strlen(message) : 0),
 615               reencoded ? reencoded : message ? message : "");
 616        free(reencoded);
 617        unuse_commit_buffer(commit, commit_buffer);
 618
 619        for (i = 0, p = commit->parents; p; p = p->next) {
 620                int mark = get_object_mark(&p->item->object);
 621                if (!mark)
 622                        continue;
 623                if (i == 0)
 624                        printf("from :%d\n", mark);
 625                else
 626                        printf("merge :%d\n", mark);
 627                i++;
 628        }
 629
 630        if (full_tree)
 631                printf("deleteall\n");
 632        log_tree_diff_flush(rev);
 633        string_list_clear(paths_of_changed_objects, 0);
 634        rev->diffopt.output_format = saved_output_format;
 635
 636        printf("\n");
 637
 638        show_progress();
 639}
 640
 641static void *anonymize_tag(const void *old, size_t *len)
 642{
 643        static int counter;
 644        struct strbuf out = STRBUF_INIT;
 645        strbuf_addf(&out, "tag message %d", counter++);
 646        return strbuf_detach(&out, len);
 647}
 648
 649static void handle_tail(struct object_array *commits, struct rev_info *revs,
 650                        struct string_list *paths_of_changed_objects)
 651{
 652        struct commit *commit;
 653        while (commits->nr) {
 654                commit = (struct commit *)object_array_pop(commits);
 655                if (has_unshown_parent(commit))
 656                        return;
 657                handle_commit(commit, revs, paths_of_changed_objects);
 658        }
 659}
 660
 661static void handle_tag(const char *name, struct tag *tag)
 662{
 663        unsigned long size;
 664        enum object_type type;
 665        char *buf;
 666        const char *tagger, *tagger_end, *message;
 667        size_t message_size = 0;
 668        struct object *tagged;
 669        int tagged_mark;
 670        struct commit *p;
 671
 672        /* Trees have no identifier in fast-export output, thus we have no way
 673         * to output tags of trees, tags of tags of trees, etc.  Simply omit
 674         * such tags.
 675         */
 676        tagged = tag->tagged;
 677        while (tagged->type == OBJ_TAG) {
 678                tagged = ((struct tag *)tagged)->tagged;
 679        }
 680        if (tagged->type == OBJ_TREE) {
 681                warning("Omitting tag %s,\nsince tags of trees (or tags of tags of trees, etc.) are not supported.",
 682                        oid_to_hex(&tag->object.oid));
 683                return;
 684        }
 685
 686        buf = read_object_file(&tag->object.oid, &type, &size);
 687        if (!buf)
 688                die ("Could not read tag %s", oid_to_hex(&tag->object.oid));
 689        message = memmem(buf, size, "\n\n", 2);
 690        if (message) {
 691                message += 2;
 692                message_size = strlen(message);
 693        }
 694        tagger = memmem(buf, message ? message - buf : size, "\ntagger ", 8);
 695        if (!tagger) {
 696                if (fake_missing_tagger)
 697                        tagger = "tagger Unspecified Tagger "
 698                                "<unspecified-tagger> 0 +0000";
 699                else
 700                        tagger = "";
 701                tagger_end = tagger + strlen(tagger);
 702        } else {
 703                tagger++;
 704                tagger_end = strchrnul(tagger, '\n');
 705                if (anonymize)
 706                        anonymize_ident_line(&tagger, &tagger_end);
 707        }
 708
 709        if (anonymize) {
 710                name = anonymize_refname(name);
 711                if (message) {
 712                        static struct hashmap tags;
 713                        message = anonymize_mem(&tags, anonymize_tag,
 714                                                message, &message_size);
 715                }
 716        }
 717
 718        /* handle signed tags */
 719        if (message) {
 720                const char *signature = strstr(message,
 721                                               "\n-----BEGIN PGP SIGNATURE-----\n");
 722                if (signature)
 723                        switch(signed_tag_mode) {
 724                        case ABORT:
 725                                die ("Encountered signed tag %s; use "
 726                                     "--signed-tags=<mode> to handle it.",
 727                                     oid_to_hex(&tag->object.oid));
 728                        case WARN:
 729                                warning ("Exporting signed tag %s",
 730                                         oid_to_hex(&tag->object.oid));
 731                                /* fallthru */
 732                        case VERBATIM:
 733                                break;
 734                        case WARN_STRIP:
 735                                warning ("Stripping signature from tag %s",
 736                                         oid_to_hex(&tag->object.oid));
 737                                /* fallthru */
 738                        case STRIP:
 739                                message_size = signature + 1 - message;
 740                                break;
 741                        }
 742        }
 743
 744        /* handle tag->tagged having been filtered out due to paths specified */
 745        tagged = tag->tagged;
 746        tagged_mark = get_object_mark(tagged);
 747        if (!tagged_mark) {
 748                switch(tag_of_filtered_mode) {
 749                case ABORT:
 750                        die ("Tag %s tags unexported object; use "
 751                             "--tag-of-filtered-object=<mode> to handle it.",
 752                             oid_to_hex(&tag->object.oid));
 753                case DROP:
 754                        /* Ignore this tag altogether */
 755                        free(buf);
 756                        return;
 757                case REWRITE:
 758                        if (tagged->type != OBJ_COMMIT) {
 759                                die ("Tag %s tags unexported %s!",
 760                                     oid_to_hex(&tag->object.oid),
 761                                     type_name(tagged->type));
 762                        }
 763                        p = (struct commit *)tagged;
 764                        for (;;) {
 765                                if (p->parents && p->parents->next)
 766                                        break;
 767                                if (p->object.flags & UNINTERESTING)
 768                                        break;
 769                                if (!(p->object.flags & TREESAME))
 770                                        break;
 771                                if (!p->parents)
 772                                        die ("Can't find replacement commit for tag %s\n",
 773                                             oid_to_hex(&tag->object.oid));
 774                                p = p->parents->item;
 775                        }
 776                        tagged_mark = get_object_mark(&p->object);
 777                }
 778        }
 779
 780        if (starts_with(name, "refs/tags/"))
 781                name += 10;
 782        printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
 783               name, tagged_mark,
 784               (int)(tagger_end - tagger), tagger,
 785               tagger == tagger_end ? "" : "\n",
 786               (int)message_size, (int)message_size, message ? message : "");
 787        free(buf);
 788}
 789
 790static struct commit *get_commit(struct rev_cmdline_entry *e, char *full_name)
 791{
 792        switch (e->item->type) {
 793        case OBJ_COMMIT:
 794                return (struct commit *)e->item;
 795        case OBJ_TAG: {
 796                struct tag *tag = (struct tag *)e->item;
 797
 798                /* handle nested tags */
 799                while (tag && tag->object.type == OBJ_TAG) {
 800                        parse_object(&tag->object.oid);
 801                        string_list_append(&extra_refs, full_name)->util = tag;
 802                        tag = (struct tag *)tag->tagged;
 803                }
 804                if (!tag)
 805                        die("Tag %s points nowhere?", e->name);
 806                return (struct commit *)tag;
 807                break;
 808        }
 809        default:
 810                return NULL;
 811        }
 812}
 813
 814static void get_tags_and_duplicates(struct rev_cmdline_info *info)
 815{
 816        int i;
 817
 818        for (i = 0; i < info->nr; i++) {
 819                struct rev_cmdline_entry *e = info->rev + i;
 820                struct object_id oid;
 821                struct commit *commit;
 822                char *full_name;
 823
 824                if (e->flags & UNINTERESTING)
 825                        continue;
 826
 827                if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1)
 828                        continue;
 829
 830                if (refspecs) {
 831                        char *private;
 832                        private = apply_refspecs(refspecs, refspecs_nr, full_name);
 833                        if (private) {
 834                                free(full_name);
 835                                full_name = private;
 836                        }
 837                }
 838
 839                commit = get_commit(e, full_name);
 840                if (!commit) {
 841                        warning("%s: Unexpected object of type %s, skipping.",
 842                                e->name,
 843                                type_name(e->item->type));
 844                        continue;
 845                }
 846
 847                switch(commit->object.type) {
 848                case OBJ_COMMIT:
 849                        break;
 850                case OBJ_BLOB:
 851                        export_blob(&commit->object.oid);
 852                        continue;
 853                default: /* OBJ_TAG (nested tags) is already handled */
 854                        warning("Tag points to object of unexpected type %s, skipping.",
 855                                type_name(commit->object.type));
 856                        continue;
 857                }
 858
 859                /*
 860                 * This ref will not be updated through a commit, lets make
 861                 * sure it gets properly updated eventually.
 862                 */
 863                if (commit->util || commit->object.flags & SHOWN)
 864                        string_list_append(&extra_refs, full_name)->util = commit;
 865                if (!commit->util)
 866                        commit->util = full_name;
 867        }
 868}
 869
 870static void handle_tags_and_duplicates(void)
 871{
 872        struct commit *commit;
 873        int i;
 874
 875        for (i = extra_refs.nr - 1; i >= 0; i--) {
 876                const char *name = extra_refs.items[i].string;
 877                struct object *object = extra_refs.items[i].util;
 878                switch (object->type) {
 879                case OBJ_TAG:
 880                        handle_tag(name, (struct tag *)object);
 881                        break;
 882                case OBJ_COMMIT:
 883                        if (anonymize)
 884                                name = anonymize_refname(name);
 885                        /* create refs pointing to already seen commits */
 886                        commit = (struct commit *)object;
 887                        printf("reset %s\nfrom :%d\n\n", name,
 888                               get_object_mark(&commit->object));
 889                        show_progress();
 890                        break;
 891                }
 892        }
 893}
 894
 895static void export_marks(char *file)
 896{
 897        unsigned int i;
 898        uint32_t mark;
 899        struct decoration_entry *deco = idnums.entries;
 900        FILE *f;
 901        int e = 0;
 902
 903        f = fopen_for_writing(file);
 904        if (!f)
 905                die_errno("Unable to open marks file %s for writing.", file);
 906
 907        for (i = 0; i < idnums.size; i++) {
 908                if (deco->base && deco->base->type == 1) {
 909                        mark = ptr_to_mark(deco->decoration);
 910                        if (fprintf(f, ":%"PRIu32" %s\n", mark,
 911                                oid_to_hex(&deco->base->oid)) < 0) {
 912                            e = 1;
 913                            break;
 914                        }
 915                }
 916                deco++;
 917        }
 918
 919        e |= ferror(f);
 920        e |= fclose(f);
 921        if (e)
 922                error("Unable to write marks file %s.", file);
 923}
 924
 925static void import_marks(char *input_file)
 926{
 927        char line[512];
 928        FILE *f = xfopen(input_file, "r");
 929
 930        while (fgets(line, sizeof(line), f)) {
 931                uint32_t mark;
 932                char *line_end, *mark_end;
 933                struct object_id oid;
 934                struct object *object;
 935                struct commit *commit;
 936                enum object_type type;
 937
 938                line_end = strchr(line, '\n');
 939                if (line[0] != ':' || !line_end)
 940                        die("corrupt mark line: %s", line);
 941                *line_end = '\0';
 942
 943                mark = strtoumax(line + 1, &mark_end, 10);
 944                if (!mark || mark_end == line + 1
 945                        || *mark_end != ' ' || get_oid_hex(mark_end + 1, &oid))
 946                        die("corrupt mark line: %s", line);
 947
 948                if (last_idnum < mark)
 949                        last_idnum = mark;
 950
 951                type = oid_object_info(the_repository, &oid, NULL);
 952                if (type < 0)
 953                        die("object not found: %s", oid_to_hex(&oid));
 954
 955                if (type != OBJ_COMMIT)
 956                        /* only commits */
 957                        continue;
 958
 959                commit = lookup_commit(&oid);
 960                if (!commit)
 961                        die("not a commit? can't happen: %s", oid_to_hex(&oid));
 962
 963                object = &commit->object;
 964
 965                if (object->flags & SHOWN)
 966                        error("Object %s already has a mark", oid_to_hex(&oid));
 967
 968                mark_object(object, mark);
 969
 970                object->flags |= SHOWN;
 971        }
 972        fclose(f);
 973}
 974
 975static void handle_deletes(void)
 976{
 977        int i;
 978        for (i = 0; i < refspecs_nr; i++) {
 979                struct refspec *refspec = &refspecs[i];
 980                if (*refspec->src)
 981                        continue;
 982
 983                printf("reset %s\nfrom %s\n\n",
 984                                refspec->dst, sha1_to_hex(null_sha1));
 985        }
 986}
 987
 988int cmd_fast_export(int argc, const char **argv, const char *prefix)
 989{
 990        struct rev_info revs;
 991        struct object_array commits = OBJECT_ARRAY_INIT;
 992        struct commit *commit;
 993        char *export_filename = NULL, *import_filename = NULL;
 994        uint32_t lastimportid;
 995        struct string_list refspecs_list = STRING_LIST_INIT_NODUP;
 996        struct string_list paths_of_changed_objects = STRING_LIST_INIT_DUP;
 997        struct option options[] = {
 998                OPT_INTEGER(0, "progress", &progress,
 999                            N_("show progress after <n> objects")),
1000                OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
1001                             N_("select handling of signed tags"),
1002                             parse_opt_signed_tag_mode),
1003                OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
1004                             N_("select handling of tags that tag filtered objects"),
1005                             parse_opt_tag_of_filtered_mode),
1006                OPT_STRING(0, "export-marks", &export_filename, N_("file"),
1007                             N_("Dump marks to this file")),
1008                OPT_STRING(0, "import-marks", &import_filename, N_("file"),
1009                             N_("Import marks from this file")),
1010                OPT_BOOL(0, "fake-missing-tagger", &fake_missing_tagger,
1011                         N_("Fake a tagger when tags lack one")),
1012                OPT_BOOL(0, "full-tree", &full_tree,
1013                         N_("Output full tree for each commit")),
1014                OPT_BOOL(0, "use-done-feature", &use_done_feature,
1015                             N_("Use the done feature to terminate the stream")),
1016                OPT_BOOL(0, "no-data", &no_data, N_("Skip output of blob data")),
1017                OPT_STRING_LIST(0, "refspec", &refspecs_list, N_("refspec"),
1018                             N_("Apply refspec to exported refs")),
1019                OPT_BOOL(0, "anonymize", &anonymize, N_("anonymize output")),
1020                OPT_END()
1021        };
1022
1023        if (argc == 1)
1024                usage_with_options (fast_export_usage, options);
1025
1026        /* we handle encodings */
1027        git_config(git_default_config, NULL);
1028
1029        init_revisions(&revs, prefix);
1030        revs.topo_order = 1;
1031        revs.show_source = 1;
1032        revs.rewrite_parents = 1;
1033        argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1034                        PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
1035        argc = setup_revisions(argc, argv, &revs, NULL);
1036        if (argc > 1)
1037                usage_with_options (fast_export_usage, options);
1038
1039        if (refspecs_list.nr) {
1040                const char **refspecs_str;
1041                int i;
1042
1043                ALLOC_ARRAY(refspecs_str, refspecs_list.nr);
1044                for (i = 0; i < refspecs_list.nr; i++)
1045                        refspecs_str[i] = refspecs_list.items[i].string;
1046
1047                refspecs_nr = refspecs_list.nr;
1048                refspecs = parse_fetch_refspec(refspecs_nr, refspecs_str);
1049
1050                string_list_clear(&refspecs_list, 1);
1051                free(refspecs_str);
1052        }
1053
1054        if (use_done_feature)
1055                printf("feature done\n");
1056
1057        if (import_filename)
1058                import_marks(import_filename);
1059        lastimportid = last_idnum;
1060
1061        if (import_filename && revs.prune_data.nr)
1062                full_tree = 1;
1063
1064        get_tags_and_duplicates(&revs.cmdline);
1065
1066        if (prepare_revision_walk(&revs))
1067                die("revision walk setup failed");
1068        revs.diffopt.format_callback = show_filemodify;
1069        revs.diffopt.format_callback_data = &paths_of_changed_objects;
1070        revs.diffopt.flags.recursive = 1;
1071        while ((commit = get_revision(&revs))) {
1072                if (has_unshown_parent(commit)) {
1073                        add_object_array(&commit->object, NULL, &commits);
1074                }
1075                else {
1076                        handle_commit(commit, &revs, &paths_of_changed_objects);
1077                        handle_tail(&commits, &revs, &paths_of_changed_objects);
1078                }
1079        }
1080
1081        handle_tags_and_duplicates();
1082        handle_deletes();
1083
1084        if (export_filename && lastimportid != last_idnum)
1085                export_marks(export_filename);
1086
1087        if (use_done_feature)
1088                printf("done\n");
1089
1090        free_refspec(refspecs_nr, refspecs);
1091
1092        return 0;
1093}