transport.con commit fetch-pack: perform a fetch using v2 (685fbd3)
   1#include "cache.h"
   2#include "config.h"
   3#include "transport.h"
   4#include "run-command.h"
   5#include "pkt-line.h"
   6#include "fetch-pack.h"
   7#include "remote.h"
   8#include "connect.h"
   9#include "send-pack.h"
  10#include "walker.h"
  11#include "bundle.h"
  12#include "dir.h"
  13#include "refs.h"
  14#include "branch.h"
  15#include "url.h"
  16#include "submodule.h"
  17#include "string-list.h"
  18#include "sha1-array.h"
  19#include "sigchain.h"
  20#include "transport-internal.h"
  21#include "protocol.h"
  22
  23static void set_upstreams(struct transport *transport, struct ref *refs,
  24        int pretend)
  25{
  26        struct ref *ref;
  27        for (ref = refs; ref; ref = ref->next) {
  28                const char *localname;
  29                const char *tmp;
  30                const char *remotename;
  31                int flag = 0;
  32                /*
  33                 * Check suitability for tracking. Must be successful /
  34                 * already up-to-date ref create/modify (not delete).
  35                 */
  36                if (ref->status != REF_STATUS_OK &&
  37                        ref->status != REF_STATUS_UPTODATE)
  38                        continue;
  39                if (!ref->peer_ref)
  40                        continue;
  41                if (is_null_oid(&ref->new_oid))
  42                        continue;
  43
  44                /* Follow symbolic refs (mainly for HEAD). */
  45                localname = ref->peer_ref->name;
  46                remotename = ref->name;
  47                tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
  48                                         NULL, &flag);
  49                if (tmp && flag & REF_ISSYMREF &&
  50                        starts_with(tmp, "refs/heads/"))
  51                        localname = tmp;
  52
  53                /* Both source and destination must be local branches. */
  54                if (!localname || !starts_with(localname, "refs/heads/"))
  55                        continue;
  56                if (!remotename || !starts_with(remotename, "refs/heads/"))
  57                        continue;
  58
  59                if (!pretend)
  60                        install_branch_config(BRANCH_CONFIG_VERBOSE,
  61                                localname + 11, transport->remote->name,
  62                                remotename);
  63                else
  64                        printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
  65                                localname + 11, remotename + 11,
  66                                transport->remote->name);
  67        }
  68}
  69
  70struct bundle_transport_data {
  71        int fd;
  72        struct bundle_header header;
  73};
  74
  75static struct ref *get_refs_from_bundle(struct transport *transport,
  76                                        int for_push,
  77                                        const struct argv_array *ref_prefixes)
  78{
  79        struct bundle_transport_data *data = transport->data;
  80        struct ref *result = NULL;
  81        int i;
  82
  83        if (for_push)
  84                return NULL;
  85
  86        if (data->fd > 0)
  87                close(data->fd);
  88        data->fd = read_bundle_header(transport->url, &data->header);
  89        if (data->fd < 0)
  90                die ("Could not read bundle '%s'.", transport->url);
  91        for (i = 0; i < data->header.references.nr; i++) {
  92                struct ref_list_entry *e = data->header.references.list + i;
  93                struct ref *ref = alloc_ref(e->name);
  94                oidcpy(&ref->old_oid, &e->oid);
  95                ref->next = result;
  96                result = ref;
  97        }
  98        return result;
  99}
 100
 101static int fetch_refs_from_bundle(struct transport *transport,
 102                               int nr_heads, struct ref **to_fetch)
 103{
 104        struct bundle_transport_data *data = transport->data;
 105        return unbundle(&data->header, data->fd,
 106                        transport->progress ? BUNDLE_VERBOSE : 0);
 107}
 108
 109static int close_bundle(struct transport *transport)
 110{
 111        struct bundle_transport_data *data = transport->data;
 112        if (data->fd > 0)
 113                close(data->fd);
 114        free(data);
 115        return 0;
 116}
 117
 118struct git_transport_data {
 119        struct git_transport_options options;
 120        struct child_process *conn;
 121        int fd[2];
 122        unsigned got_remote_heads : 1;
 123        enum protocol_version version;
 124        struct oid_array extra_have;
 125        struct oid_array shallow;
 126};
 127
 128static int set_git_option(struct git_transport_options *opts,
 129                          const char *name, const char *value)
 130{
 131        if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
 132                opts->uploadpack = value;
 133                return 0;
 134        } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
 135                opts->receivepack = value;
 136                return 0;
 137        } else if (!strcmp(name, TRANS_OPT_THIN)) {
 138                opts->thin = !!value;
 139                return 0;
 140        } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
 141                opts->followtags = !!value;
 142                return 0;
 143        } else if (!strcmp(name, TRANS_OPT_KEEP)) {
 144                opts->keep = !!value;
 145                return 0;
 146        } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
 147                opts->update_shallow = !!value;
 148                return 0;
 149        } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
 150                if (!value)
 151                        opts->depth = 0;
 152                else {
 153                        char *end;
 154                        opts->depth = strtol(value, &end, 0);
 155                        if (*end)
 156                                die(_("transport: invalid depth option '%s'"), value);
 157                }
 158                return 0;
 159        } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
 160                opts->deepen_since = value;
 161                return 0;
 162        } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
 163                opts->deepen_not = (const struct string_list *)value;
 164                return 0;
 165        } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
 166                opts->deepen_relative = !!value;
 167                return 0;
 168        }
 169        return 1;
 170}
 171
 172static int connect_setup(struct transport *transport, int for_push)
 173{
 174        struct git_transport_data *data = transport->data;
 175        int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
 176
 177        if (data->conn)
 178                return 0;
 179
 180        switch (transport->family) {
 181        case TRANSPORT_FAMILY_ALL: break;
 182        case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
 183        case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
 184        }
 185
 186        data->conn = git_connect(data->fd, transport->url,
 187                                 for_push ? data->options.receivepack :
 188                                 data->options.uploadpack,
 189                                 flags);
 190
 191        return 0;
 192}
 193
 194static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
 195                                        const struct argv_array *ref_prefixes)
 196{
 197        struct git_transport_data *data = transport->data;
 198        struct ref *refs = NULL;
 199        struct packet_reader reader;
 200
 201        connect_setup(transport, for_push);
 202
 203        packet_reader_init(&reader, data->fd[0], NULL, 0,
 204                           PACKET_READ_CHOMP_NEWLINE |
 205                           PACKET_READ_GENTLE_ON_EOF);
 206
 207        data->version = discover_version(&reader);
 208        switch (data->version) {
 209        case protocol_v2:
 210                get_remote_refs(data->fd[1], &reader, &refs, for_push,
 211                                ref_prefixes);
 212                break;
 213        case protocol_v1:
 214        case protocol_v0:
 215                get_remote_heads(&reader, &refs,
 216                                 for_push ? REF_NORMAL : 0,
 217                                 &data->extra_have,
 218                                 &data->shallow);
 219                break;
 220        case protocol_unknown_version:
 221                BUG("unknown protocol version");
 222        }
 223        data->got_remote_heads = 1;
 224
 225        return refs;
 226}
 227
 228static int fetch_refs_via_pack(struct transport *transport,
 229                               int nr_heads, struct ref **to_fetch)
 230{
 231        int ret = 0;
 232        struct git_transport_data *data = transport->data;
 233        struct ref *refs = NULL;
 234        char *dest = xstrdup(transport->url);
 235        struct fetch_pack_args args;
 236        struct ref *refs_tmp = NULL;
 237
 238        memset(&args, 0, sizeof(args));
 239        args.uploadpack = data->options.uploadpack;
 240        args.keep_pack = data->options.keep;
 241        args.lock_pack = 1;
 242        args.use_thin_pack = data->options.thin;
 243        args.include_tag = data->options.followtags;
 244        args.verbose = (transport->verbose > 1);
 245        args.quiet = (transport->verbose < 0);
 246        args.no_progress = !transport->progress;
 247        args.depth = data->options.depth;
 248        args.deepen_since = data->options.deepen_since;
 249        args.deepen_not = data->options.deepen_not;
 250        args.deepen_relative = data->options.deepen_relative;
 251        args.check_self_contained_and_connected =
 252                data->options.check_self_contained_and_connected;
 253        args.cloning = transport->cloning;
 254        args.update_shallow = data->options.update_shallow;
 255
 256        if (!data->got_remote_heads)
 257                refs_tmp = get_refs_via_connect(transport, 0, NULL);
 258
 259        switch (data->version) {
 260        case protocol_v2:
 261                refs = fetch_pack(&args, data->fd, data->conn,
 262                                  refs_tmp ? refs_tmp : transport->remote_refs,
 263                                  dest, to_fetch, nr_heads, &data->shallow,
 264                                  &transport->pack_lockfile, data->version);
 265                break;
 266        case protocol_v1:
 267        case protocol_v0:
 268                refs = fetch_pack(&args, data->fd, data->conn,
 269                                  refs_tmp ? refs_tmp : transport->remote_refs,
 270                                  dest, to_fetch, nr_heads, &data->shallow,
 271                                  &transport->pack_lockfile, data->version);
 272                break;
 273        case protocol_unknown_version:
 274                BUG("unknown protocol version");
 275        }
 276
 277        close(data->fd[0]);
 278        close(data->fd[1]);
 279        if (finish_connect(data->conn))
 280                ret = -1;
 281        data->conn = NULL;
 282        data->got_remote_heads = 0;
 283        data->options.self_contained_and_connected =
 284                args.self_contained_and_connected;
 285
 286        if (refs == NULL)
 287                ret = -1;
 288        if (report_unmatched_refs(to_fetch, nr_heads))
 289                ret = -1;
 290
 291        free_refs(refs_tmp);
 292        free_refs(refs);
 293        free(dest);
 294        return ret;
 295}
 296
 297static int push_had_errors(struct ref *ref)
 298{
 299        for (; ref; ref = ref->next) {
 300                switch (ref->status) {
 301                case REF_STATUS_NONE:
 302                case REF_STATUS_UPTODATE:
 303                case REF_STATUS_OK:
 304                        break;
 305                default:
 306                        return 1;
 307                }
 308        }
 309        return 0;
 310}
 311
 312int transport_refs_pushed(struct ref *ref)
 313{
 314        for (; ref; ref = ref->next) {
 315                switch(ref->status) {
 316                case REF_STATUS_NONE:
 317                case REF_STATUS_UPTODATE:
 318                        break;
 319                default:
 320                        return 1;
 321                }
 322        }
 323        return 0;
 324}
 325
 326void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
 327{
 328        struct refspec rs;
 329
 330        if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
 331                return;
 332
 333        rs.src = ref->name;
 334        rs.dst = NULL;
 335
 336        if (!remote_find_tracking(remote, &rs)) {
 337                if (verbose)
 338                        fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
 339                if (ref->deletion) {
 340                        delete_ref(NULL, rs.dst, NULL, 0);
 341                } else
 342                        update_ref("update by push", rs.dst, &ref->new_oid,
 343                                   NULL, 0, 0);
 344                free(rs.dst);
 345        }
 346}
 347
 348static void print_ref_status(char flag, const char *summary,
 349                             struct ref *to, struct ref *from, const char *msg,
 350                             int porcelain, int summary_width)
 351{
 352        if (porcelain) {
 353                if (from)
 354                        fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
 355                else
 356                        fprintf(stdout, "%c\t:%s\t", flag, to->name);
 357                if (msg)
 358                        fprintf(stdout, "%s (%s)\n", summary, msg);
 359                else
 360                        fprintf(stdout, "%s\n", summary);
 361        } else {
 362                fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
 363                if (from)
 364                        fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
 365                else
 366                        fputs(prettify_refname(to->name), stderr);
 367                if (msg) {
 368                        fputs(" (", stderr);
 369                        fputs(msg, stderr);
 370                        fputc(')', stderr);
 371                }
 372                fputc('\n', stderr);
 373        }
 374}
 375
 376static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
 377{
 378        if (ref->deletion)
 379                print_ref_status('-', "[deleted]", ref, NULL, NULL,
 380                                 porcelain, summary_width);
 381        else if (is_null_oid(&ref->old_oid))
 382                print_ref_status('*',
 383                        (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
 384                        "[new branch]"),
 385                        ref, ref->peer_ref, NULL, porcelain, summary_width);
 386        else {
 387                struct strbuf quickref = STRBUF_INIT;
 388                char type;
 389                const char *msg;
 390
 391                strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
 392                                         DEFAULT_ABBREV);
 393                if (ref->forced_update) {
 394                        strbuf_addstr(&quickref, "...");
 395                        type = '+';
 396                        msg = "forced update";
 397                } else {
 398                        strbuf_addstr(&quickref, "..");
 399                        type = ' ';
 400                        msg = NULL;
 401                }
 402                strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
 403                                         DEFAULT_ABBREV);
 404
 405                print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
 406                                 porcelain, summary_width);
 407                strbuf_release(&quickref);
 408        }
 409}
 410
 411static int print_one_push_status(struct ref *ref, const char *dest, int count,
 412                                 int porcelain, int summary_width)
 413{
 414        if (!count) {
 415                char *url = transport_anonymize_url(dest);
 416                fprintf(porcelain ? stdout : stderr, "To %s\n", url);
 417                free(url);
 418        }
 419
 420        switch(ref->status) {
 421        case REF_STATUS_NONE:
 422                print_ref_status('X', "[no match]", ref, NULL, NULL,
 423                                 porcelain, summary_width);
 424                break;
 425        case REF_STATUS_REJECT_NODELETE:
 426                print_ref_status('!', "[rejected]", ref, NULL,
 427                                 "remote does not support deleting refs",
 428                                 porcelain, summary_width);
 429                break;
 430        case REF_STATUS_UPTODATE:
 431                print_ref_status('=', "[up to date]", ref,
 432                                 ref->peer_ref, NULL, porcelain, summary_width);
 433                break;
 434        case REF_STATUS_REJECT_NONFASTFORWARD:
 435                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 436                                 "non-fast-forward", porcelain, summary_width);
 437                break;
 438        case REF_STATUS_REJECT_ALREADY_EXISTS:
 439                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 440                                 "already exists", porcelain, summary_width);
 441                break;
 442        case REF_STATUS_REJECT_FETCH_FIRST:
 443                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 444                                 "fetch first", porcelain, summary_width);
 445                break;
 446        case REF_STATUS_REJECT_NEEDS_FORCE:
 447                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 448                                 "needs force", porcelain, summary_width);
 449                break;
 450        case REF_STATUS_REJECT_STALE:
 451                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 452                                 "stale info", porcelain, summary_width);
 453                break;
 454        case REF_STATUS_REJECT_SHALLOW:
 455                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 456                                 "new shallow roots not allowed",
 457                                 porcelain, summary_width);
 458                break;
 459        case REF_STATUS_REMOTE_REJECT:
 460                print_ref_status('!', "[remote rejected]", ref,
 461                                 ref->deletion ? NULL : ref->peer_ref,
 462                                 ref->remote_status, porcelain, summary_width);
 463                break;
 464        case REF_STATUS_EXPECTING_REPORT:
 465                print_ref_status('!', "[remote failure]", ref,
 466                                 ref->deletion ? NULL : ref->peer_ref,
 467                                 "remote failed to report status",
 468                                 porcelain, summary_width);
 469                break;
 470        case REF_STATUS_ATOMIC_PUSH_FAILED:
 471                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 472                                 "atomic push failed", porcelain, summary_width);
 473                break;
 474        case REF_STATUS_OK:
 475                print_ok_ref_status(ref, porcelain, summary_width);
 476                break;
 477        }
 478
 479        return 1;
 480}
 481
 482static int measure_abbrev(const struct object_id *oid, int sofar)
 483{
 484        char hex[GIT_MAX_HEXSZ + 1];
 485        int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV);
 486
 487        return (w < sofar) ? sofar : w;
 488}
 489
 490int transport_summary_width(const struct ref *refs)
 491{
 492        int maxw = -1;
 493
 494        for (; refs; refs = refs->next) {
 495                maxw = measure_abbrev(&refs->old_oid, maxw);
 496                maxw = measure_abbrev(&refs->new_oid, maxw);
 497        }
 498        if (maxw < 0)
 499                maxw = FALLBACK_DEFAULT_ABBREV;
 500        return (2 * maxw + 3);
 501}
 502
 503void transport_print_push_status(const char *dest, struct ref *refs,
 504                                  int verbose, int porcelain, unsigned int *reject_reasons)
 505{
 506        struct ref *ref;
 507        int n = 0;
 508        char *head;
 509        int summary_width = transport_summary_width(refs);
 510
 511        head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
 512
 513        if (verbose) {
 514                for (ref = refs; ref; ref = ref->next)
 515                        if (ref->status == REF_STATUS_UPTODATE)
 516                                n += print_one_push_status(ref, dest, n,
 517                                                           porcelain, summary_width);
 518        }
 519
 520        for (ref = refs; ref; ref = ref->next)
 521                if (ref->status == REF_STATUS_OK)
 522                        n += print_one_push_status(ref, dest, n,
 523                                                   porcelain, summary_width);
 524
 525        *reject_reasons = 0;
 526        for (ref = refs; ref; ref = ref->next) {
 527                if (ref->status != REF_STATUS_NONE &&
 528                    ref->status != REF_STATUS_UPTODATE &&
 529                    ref->status != REF_STATUS_OK)
 530                        n += print_one_push_status(ref, dest, n,
 531                                                   porcelain, summary_width);
 532                if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
 533                        if (head != NULL && !strcmp(head, ref->name))
 534                                *reject_reasons |= REJECT_NON_FF_HEAD;
 535                        else
 536                                *reject_reasons |= REJECT_NON_FF_OTHER;
 537                } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
 538                        *reject_reasons |= REJECT_ALREADY_EXISTS;
 539                } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
 540                        *reject_reasons |= REJECT_FETCH_FIRST;
 541                } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
 542                        *reject_reasons |= REJECT_NEEDS_FORCE;
 543                }
 544        }
 545        free(head);
 546}
 547
 548void transport_verify_remote_names(int nr_heads, const char **heads)
 549{
 550        int i;
 551
 552        for (i = 0; i < nr_heads; i++) {
 553                const char *local = heads[i];
 554                const char *remote = strrchr(heads[i], ':');
 555
 556                if (*local == '+')
 557                        local++;
 558
 559                /* A matching refspec is okay.  */
 560                if (remote == local && remote[1] == '\0')
 561                        continue;
 562
 563                remote = remote ? (remote + 1) : local;
 564                if (check_refname_format(remote,
 565                                REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
 566                        die("remote part of refspec is not a valid name in %s",
 567                                heads[i]);
 568        }
 569}
 570
 571static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
 572{
 573        struct git_transport_data *data = transport->data;
 574        struct send_pack_args args;
 575        int ret = 0;
 576
 577        if (!data->got_remote_heads)
 578                get_refs_via_connect(transport, 1, NULL);
 579
 580        memset(&args, 0, sizeof(args));
 581        args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
 582        args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
 583        args.use_thin_pack = data->options.thin;
 584        args.verbose = (transport->verbose > 0);
 585        args.quiet = (transport->verbose < 0);
 586        args.progress = transport->progress;
 587        args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
 588        args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
 589        args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
 590        args.push_options = transport->push_options;
 591        args.url = transport->url;
 592
 593        if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
 594                args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
 595        else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
 596                args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
 597        else
 598                args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
 599
 600        switch (data->version) {
 601        case protocol_v2:
 602                die("support for protocol v2 not implemented yet");
 603                break;
 604        case protocol_v1:
 605        case protocol_v0:
 606                ret = send_pack(&args, data->fd, data->conn, remote_refs,
 607                                &data->extra_have);
 608                break;
 609        case protocol_unknown_version:
 610                BUG("unknown protocol version");
 611        }
 612
 613        close(data->fd[1]);
 614        close(data->fd[0]);
 615        ret |= finish_connect(data->conn);
 616        data->conn = NULL;
 617        data->got_remote_heads = 0;
 618
 619        return ret;
 620}
 621
 622static int connect_git(struct transport *transport, const char *name,
 623                       const char *executable, int fd[2])
 624{
 625        struct git_transport_data *data = transport->data;
 626        data->conn = git_connect(data->fd, transport->url,
 627                                 executable, 0);
 628        fd[0] = data->fd[0];
 629        fd[1] = data->fd[1];
 630        return 0;
 631}
 632
 633static int disconnect_git(struct transport *transport)
 634{
 635        struct git_transport_data *data = transport->data;
 636        if (data->conn) {
 637                if (data->got_remote_heads)
 638                        packet_flush(data->fd[1]);
 639                close(data->fd[0]);
 640                close(data->fd[1]);
 641                finish_connect(data->conn);
 642        }
 643
 644        free(data);
 645        return 0;
 646}
 647
 648static struct transport_vtable taken_over_vtable = {
 649        NULL,
 650        get_refs_via_connect,
 651        fetch_refs_via_pack,
 652        git_transport_push,
 653        NULL,
 654        disconnect_git
 655};
 656
 657void transport_take_over(struct transport *transport,
 658                         struct child_process *child)
 659{
 660        struct git_transport_data *data;
 661
 662        if (!transport->smart_options)
 663                die("BUG: taking over transport requires non-NULL "
 664                    "smart_options field.");
 665
 666        data = xcalloc(1, sizeof(*data));
 667        data->options = *transport->smart_options;
 668        data->conn = child;
 669        data->fd[0] = data->conn->out;
 670        data->fd[1] = data->conn->in;
 671        data->got_remote_heads = 0;
 672        transport->data = data;
 673
 674        transport->vtable = &taken_over_vtable;
 675        transport->smart_options = &(data->options);
 676
 677        transport->cannot_reuse = 1;
 678}
 679
 680static int is_file(const char *url)
 681{
 682        struct stat buf;
 683        if (stat(url, &buf))
 684                return 0;
 685        return S_ISREG(buf.st_mode);
 686}
 687
 688static int external_specification_len(const char *url)
 689{
 690        return strchr(url, ':') - url;
 691}
 692
 693static const struct string_list *protocol_whitelist(void)
 694{
 695        static int enabled = -1;
 696        static struct string_list allowed = STRING_LIST_INIT_DUP;
 697
 698        if (enabled < 0) {
 699                const char *v = getenv("GIT_ALLOW_PROTOCOL");
 700                if (v) {
 701                        string_list_split(&allowed, v, ':', -1);
 702                        string_list_sort(&allowed);
 703                        enabled = 1;
 704                } else {
 705                        enabled = 0;
 706                }
 707        }
 708
 709        return enabled ? &allowed : NULL;
 710}
 711
 712enum protocol_allow_config {
 713        PROTOCOL_ALLOW_NEVER = 0,
 714        PROTOCOL_ALLOW_USER_ONLY,
 715        PROTOCOL_ALLOW_ALWAYS
 716};
 717
 718static enum protocol_allow_config parse_protocol_config(const char *key,
 719                                                        const char *value)
 720{
 721        if (!strcasecmp(value, "always"))
 722                return PROTOCOL_ALLOW_ALWAYS;
 723        else if (!strcasecmp(value, "never"))
 724                return PROTOCOL_ALLOW_NEVER;
 725        else if (!strcasecmp(value, "user"))
 726                return PROTOCOL_ALLOW_USER_ONLY;
 727
 728        die("unknown value for config '%s': %s", key, value);
 729}
 730
 731static enum protocol_allow_config get_protocol_config(const char *type)
 732{
 733        char *key = xstrfmt("protocol.%s.allow", type);
 734        char *value;
 735
 736        /* first check the per-protocol config */
 737        if (!git_config_get_string(key, &value)) {
 738                enum protocol_allow_config ret =
 739                        parse_protocol_config(key, value);
 740                free(key);
 741                free(value);
 742                return ret;
 743        }
 744        free(key);
 745
 746        /* if defined, fallback to user-defined default for unknown protocols */
 747        if (!git_config_get_string("protocol.allow", &value)) {
 748                enum protocol_allow_config ret =
 749                        parse_protocol_config("protocol.allow", value);
 750                free(value);
 751                return ret;
 752        }
 753
 754        /* fallback to built-in defaults */
 755        /* known safe */
 756        if (!strcmp(type, "http") ||
 757            !strcmp(type, "https") ||
 758            !strcmp(type, "git") ||
 759            !strcmp(type, "ssh") ||
 760            !strcmp(type, "file"))
 761                return PROTOCOL_ALLOW_ALWAYS;
 762
 763        /* known scary; err on the side of caution */
 764        if (!strcmp(type, "ext"))
 765                return PROTOCOL_ALLOW_NEVER;
 766
 767        /* unknown; by default let them be used only directly by the user */
 768        return PROTOCOL_ALLOW_USER_ONLY;
 769}
 770
 771int is_transport_allowed(const char *type, int from_user)
 772{
 773        const struct string_list *whitelist = protocol_whitelist();
 774        if (whitelist)
 775                return string_list_has_string(whitelist, type);
 776
 777        switch (get_protocol_config(type)) {
 778        case PROTOCOL_ALLOW_ALWAYS:
 779                return 1;
 780        case PROTOCOL_ALLOW_NEVER:
 781                return 0;
 782        case PROTOCOL_ALLOW_USER_ONLY:
 783                if (from_user < 0)
 784                        from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
 785                return from_user;
 786        }
 787
 788        die("BUG: invalid protocol_allow_config type");
 789}
 790
 791void transport_check_allowed(const char *type)
 792{
 793        if (!is_transport_allowed(type, -1))
 794                die("transport '%s' not allowed", type);
 795}
 796
 797static struct transport_vtable bundle_vtable = {
 798        NULL,
 799        get_refs_from_bundle,
 800        fetch_refs_from_bundle,
 801        NULL,
 802        NULL,
 803        close_bundle
 804};
 805
 806static struct transport_vtable builtin_smart_vtable = {
 807        NULL,
 808        get_refs_via_connect,
 809        fetch_refs_via_pack,
 810        git_transport_push,
 811        connect_git,
 812        disconnect_git
 813};
 814
 815struct transport *transport_get(struct remote *remote, const char *url)
 816{
 817        const char *helper;
 818        struct transport *ret = xcalloc(1, sizeof(*ret));
 819
 820        ret->progress = isatty(2);
 821
 822        if (!remote)
 823                die("No remote provided to transport_get()");
 824
 825        ret->got_remote_refs = 0;
 826        ret->remote = remote;
 827        helper = remote->foreign_vcs;
 828
 829        if (!url && remote->url)
 830                url = remote->url[0];
 831        ret->url = url;
 832
 833        /* maybe it is a foreign URL? */
 834        if (url) {
 835                const char *p = url;
 836
 837                while (is_urlschemechar(p == url, *p))
 838                        p++;
 839                if (starts_with(p, "::"))
 840                        helper = xstrndup(url, p - url);
 841        }
 842
 843        if (helper) {
 844                transport_helper_init(ret, helper);
 845        } else if (starts_with(url, "rsync:")) {
 846                die("git-over-rsync is no longer supported");
 847        } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
 848                struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
 849                transport_check_allowed("file");
 850                ret->data = data;
 851                ret->vtable = &bundle_vtable;
 852                ret->smart_options = NULL;
 853        } else if (!is_url(url)
 854                || starts_with(url, "file://")
 855                || starts_with(url, "git://")
 856                || starts_with(url, "ssh://")
 857                || starts_with(url, "git+ssh://") /* deprecated - do not use */
 858                || starts_with(url, "ssh+git://") /* deprecated - do not use */
 859                ) {
 860                /*
 861                 * These are builtin smart transports; "allowed" transports
 862                 * will be checked individually in git_connect.
 863                 */
 864                struct git_transport_data *data = xcalloc(1, sizeof(*data));
 865                ret->data = data;
 866                ret->vtable = &builtin_smart_vtable;
 867                ret->smart_options = &(data->options);
 868
 869                data->conn = NULL;
 870                data->got_remote_heads = 0;
 871        } else {
 872                /* Unknown protocol in URL. Pass to external handler. */
 873                int len = external_specification_len(url);
 874                char *handler = xmemdupz(url, len);
 875                transport_helper_init(ret, handler);
 876        }
 877
 878        if (ret->smart_options) {
 879                ret->smart_options->thin = 1;
 880                ret->smart_options->uploadpack = "git-upload-pack";
 881                if (remote->uploadpack)
 882                        ret->smart_options->uploadpack = remote->uploadpack;
 883                ret->smart_options->receivepack = "git-receive-pack";
 884                if (remote->receivepack)
 885                        ret->smart_options->receivepack = remote->receivepack;
 886        }
 887
 888        return ret;
 889}
 890
 891int transport_set_option(struct transport *transport,
 892                         const char *name, const char *value)
 893{
 894        int git_reports = 1, protocol_reports = 1;
 895
 896        if (transport->smart_options)
 897                git_reports = set_git_option(transport->smart_options,
 898                                             name, value);
 899
 900        if (transport->vtable->set_option)
 901                protocol_reports = transport->vtable->set_option(transport,
 902                                                                 name, value);
 903
 904        /* If either report is 0, report 0 (success). */
 905        if (!git_reports || !protocol_reports)
 906                return 0;
 907        /* If either reports -1 (invalid value), report -1. */
 908        if ((git_reports == -1) || (protocol_reports == -1))
 909                return -1;
 910        /* Otherwise if both report unknown, report unknown. */
 911        return 1;
 912}
 913
 914void transport_set_verbosity(struct transport *transport, int verbosity,
 915        int force_progress)
 916{
 917        if (verbosity >= 1)
 918                transport->verbose = verbosity <= 3 ? verbosity : 3;
 919        if (verbosity < 0)
 920                transport->verbose = -1;
 921
 922        /**
 923         * Rules used to determine whether to report progress (processing aborts
 924         * when a rule is satisfied):
 925         *
 926         *   . Report progress, if force_progress is 1 (ie. --progress).
 927         *   . Don't report progress, if force_progress is 0 (ie. --no-progress).
 928         *   . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
 929         *   . Report progress if isatty(2) is 1.
 930         **/
 931        if (force_progress >= 0)
 932                transport->progress = !!force_progress;
 933        else
 934                transport->progress = verbosity >= 0 && isatty(2);
 935}
 936
 937static void die_with_unpushed_submodules(struct string_list *needs_pushing)
 938{
 939        int i;
 940
 941        fprintf(stderr, _("The following submodule paths contain changes that can\n"
 942                        "not be found on any remote:\n"));
 943        for (i = 0; i < needs_pushing->nr; i++)
 944                fprintf(stderr, "  %s\n", needs_pushing->items[i].string);
 945        fprintf(stderr, _("\nPlease try\n\n"
 946                          "     git push --recurse-submodules=on-demand\n\n"
 947                          "or cd to the path and use\n\n"
 948                          "     git push\n\n"
 949                          "to push them to a remote.\n\n"));
 950
 951        string_list_clear(needs_pushing, 0);
 952
 953        die(_("Aborting."));
 954}
 955
 956static int run_pre_push_hook(struct transport *transport,
 957                             struct ref *remote_refs)
 958{
 959        int ret = 0, x;
 960        struct ref *r;
 961        struct child_process proc = CHILD_PROCESS_INIT;
 962        struct strbuf buf;
 963        const char *argv[4];
 964
 965        if (!(argv[0] = find_hook("pre-push")))
 966                return 0;
 967
 968        argv[1] = transport->remote->name;
 969        argv[2] = transport->url;
 970        argv[3] = NULL;
 971
 972        proc.argv = argv;
 973        proc.in = -1;
 974
 975        if (start_command(&proc)) {
 976                finish_command(&proc);
 977                return -1;
 978        }
 979
 980        sigchain_push(SIGPIPE, SIG_IGN);
 981
 982        strbuf_init(&buf, 256);
 983
 984        for (r = remote_refs; r; r = r->next) {
 985                if (!r->peer_ref) continue;
 986                if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
 987                if (r->status == REF_STATUS_REJECT_STALE) continue;
 988                if (r->status == REF_STATUS_UPTODATE) continue;
 989
 990                strbuf_reset(&buf);
 991                strbuf_addf( &buf, "%s %s %s %s\n",
 992                         r->peer_ref->name, oid_to_hex(&r->new_oid),
 993                         r->name, oid_to_hex(&r->old_oid));
 994
 995                if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
 996                        /* We do not mind if a hook does not read all refs. */
 997                        if (errno != EPIPE)
 998                                ret = -1;
 999                        break;
1000                }
1001        }
1002
1003        strbuf_release(&buf);
1004
1005        x = close(proc.in);
1006        if (!ret)
1007                ret = x;
1008
1009        sigchain_pop(SIGPIPE);
1010
1011        x = finish_command(&proc);
1012        if (!ret)
1013                ret = x;
1014
1015        return ret;
1016}
1017
1018int transport_push(struct transport *transport,
1019                   int refspec_nr, const char **refspec, int flags,
1020                   unsigned int *reject_reasons)
1021{
1022        *reject_reasons = 0;
1023        transport_verify_remote_names(refspec_nr, refspec);
1024
1025        if (transport->vtable->push_refs) {
1026                struct ref *remote_refs;
1027                struct ref *local_refs = get_local_heads();
1028                int match_flags = MATCH_REFS_NONE;
1029                int verbose = (transport->verbose > 0);
1030                int quiet = (transport->verbose < 0);
1031                int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1032                int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1033                int push_ret, ret, err;
1034                struct refspec *tmp_rs;
1035                struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1036                int i;
1037
1038                if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
1039                        return -1;
1040
1041                tmp_rs = parse_push_refspec(refspec_nr, refspec);
1042                for (i = 0; i < refspec_nr; i++) {
1043                        const char *prefix = NULL;
1044
1045                        if (tmp_rs[i].dst)
1046                                prefix = tmp_rs[i].dst;
1047                        else if (tmp_rs[i].src && !tmp_rs[i].exact_sha1)
1048                                prefix = tmp_rs[i].src;
1049
1050                        if (prefix) {
1051                                const char *glob = strchr(prefix, '*');
1052                                if (glob)
1053                                        argv_array_pushf(&ref_prefixes, "%.*s",
1054                                                         (int)(glob - prefix),
1055                                                         prefix);
1056                                else
1057                                        expand_ref_prefix(&ref_prefixes, prefix);
1058                        }
1059                }
1060
1061                remote_refs = transport->vtable->get_refs_list(transport, 1,
1062                                                               &ref_prefixes);
1063
1064                argv_array_clear(&ref_prefixes);
1065                free_refspec(refspec_nr, tmp_rs);
1066
1067                if (flags & TRANSPORT_PUSH_ALL)
1068                        match_flags |= MATCH_REFS_ALL;
1069                if (flags & TRANSPORT_PUSH_MIRROR)
1070                        match_flags |= MATCH_REFS_MIRROR;
1071                if (flags & TRANSPORT_PUSH_PRUNE)
1072                        match_flags |= MATCH_REFS_PRUNE;
1073                if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1074                        match_flags |= MATCH_REFS_FOLLOW_TAGS;
1075
1076                if (match_push_refs(local_refs, &remote_refs,
1077                                    refspec_nr, refspec, match_flags)) {
1078                        return -1;
1079                }
1080
1081                if (transport->smart_options &&
1082                    transport->smart_options->cas &&
1083                    !is_empty_cas(transport->smart_options->cas))
1084                        apply_push_cas(transport->smart_options->cas,
1085                                       transport->remote, remote_refs);
1086
1087                set_ref_status_for_push(remote_refs,
1088                        flags & TRANSPORT_PUSH_MIRROR,
1089                        flags & TRANSPORT_PUSH_FORCE);
1090
1091                if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1092                        if (run_pre_push_hook(transport, remote_refs))
1093                                return -1;
1094
1095                if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1096                              TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1097                    !is_bare_repository()) {
1098                        struct ref *ref = remote_refs;
1099                        struct oid_array commits = OID_ARRAY_INIT;
1100
1101                        for (; ref; ref = ref->next)
1102                                if (!is_null_oid(&ref->new_oid))
1103                                        oid_array_append(&commits,
1104                                                          &ref->new_oid);
1105
1106                        if (!push_unpushed_submodules(&commits,
1107                                                      transport->remote,
1108                                                      refspec, refspec_nr,
1109                                                      transport->push_options,
1110                                                      pretend)) {
1111                                oid_array_clear(&commits);
1112                                die("Failed to push all needed submodules!");
1113                        }
1114                        oid_array_clear(&commits);
1115                }
1116
1117                if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1118                     ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1119                                TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1120                      !pretend)) && !is_bare_repository()) {
1121                        struct ref *ref = remote_refs;
1122                        struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1123                        struct oid_array commits = OID_ARRAY_INIT;
1124
1125                        for (; ref; ref = ref->next)
1126                                if (!is_null_oid(&ref->new_oid))
1127                                        oid_array_append(&commits,
1128                                                          &ref->new_oid);
1129
1130                        if (find_unpushed_submodules(&commits, transport->remote->name,
1131                                                &needs_pushing)) {
1132                                oid_array_clear(&commits);
1133                                die_with_unpushed_submodules(&needs_pushing);
1134                        }
1135                        string_list_clear(&needs_pushing, 0);
1136                        oid_array_clear(&commits);
1137                }
1138
1139                if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1140                        push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1141                else
1142                        push_ret = 0;
1143                err = push_had_errors(remote_refs);
1144                ret = push_ret | err;
1145
1146                if (!quiet || err)
1147                        transport_print_push_status(transport->url, remote_refs,
1148                                        verbose | porcelain, porcelain,
1149                                        reject_reasons);
1150
1151                if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1152                        set_upstreams(transport, remote_refs, pretend);
1153
1154                if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1155                               TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1156                        struct ref *ref;
1157                        for (ref = remote_refs; ref; ref = ref->next)
1158                                transport_update_tracking_ref(transport->remote, ref, verbose);
1159                }
1160
1161                if (porcelain && !push_ret)
1162                        puts("Done");
1163                else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1164                        fprintf(stderr, "Everything up-to-date\n");
1165
1166                return ret;
1167        }
1168        return 1;
1169}
1170
1171const struct ref *transport_get_remote_refs(struct transport *transport,
1172                                            const struct argv_array *ref_prefixes)
1173{
1174        if (!transport->got_remote_refs) {
1175                transport->remote_refs =
1176                        transport->vtable->get_refs_list(transport, 0,
1177                                                         ref_prefixes);
1178                transport->got_remote_refs = 1;
1179        }
1180
1181        return transport->remote_refs;
1182}
1183
1184int transport_fetch_refs(struct transport *transport, struct ref *refs)
1185{
1186        int rc;
1187        int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1188        struct ref **heads = NULL;
1189        struct ref *rm;
1190
1191        for (rm = refs; rm; rm = rm->next) {
1192                nr_refs++;
1193                if (rm->peer_ref &&
1194                    !is_null_oid(&rm->old_oid) &&
1195                    !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1196                        continue;
1197                ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1198                heads[nr_heads++] = rm;
1199        }
1200
1201        if (!nr_heads) {
1202                /*
1203                 * When deepening of a shallow repository is requested,
1204                 * then local and remote refs are likely to still be equal.
1205                 * Just feed them all to the fetch method in that case.
1206                 * This condition shouldn't be met in a non-deepening fetch
1207                 * (see builtin/fetch.c:quickfetch()).
1208                 */
1209                ALLOC_ARRAY(heads, nr_refs);
1210                for (rm = refs; rm; rm = rm->next)
1211                        heads[nr_heads++] = rm;
1212        }
1213
1214        rc = transport->vtable->fetch(transport, nr_heads, heads);
1215
1216        free(heads);
1217        return rc;
1218}
1219
1220void transport_unlock_pack(struct transport *transport)
1221{
1222        if (transport->pack_lockfile) {
1223                unlink_or_warn(transport->pack_lockfile);
1224                FREE_AND_NULL(transport->pack_lockfile);
1225        }
1226}
1227
1228int transport_connect(struct transport *transport, const char *name,
1229                      const char *exec, int fd[2])
1230{
1231        if (transport->vtable->connect)
1232                return transport->vtable->connect(transport, name, exec, fd);
1233        else
1234                die("Operation not supported by protocol");
1235}
1236
1237int transport_disconnect(struct transport *transport)
1238{
1239        int ret = 0;
1240        if (transport->vtable->disconnect)
1241                ret = transport->vtable->disconnect(transport);
1242        free(transport);
1243        return ret;
1244}
1245
1246/*
1247 * Strip username (and password) from a URL and return
1248 * it in a newly allocated string.
1249 */
1250char *transport_anonymize_url(const char *url)
1251{
1252        char *scheme_prefix, *anon_part;
1253        size_t anon_len, prefix_len = 0;
1254
1255        anon_part = strchr(url, '@');
1256        if (url_is_local_not_ssh(url) || !anon_part)
1257                goto literal_copy;
1258
1259        anon_len = strlen(++anon_part);
1260        scheme_prefix = strstr(url, "://");
1261        if (!scheme_prefix) {
1262                if (!strchr(anon_part, ':'))
1263                        /* cannot be "me@there:/path/name" */
1264                        goto literal_copy;
1265        } else {
1266                const char *cp;
1267                /* make sure scheme is reasonable */
1268                for (cp = url; cp < scheme_prefix; cp++) {
1269                        switch (*cp) {
1270                                /* RFC 1738 2.1 */
1271                        case '+': case '.': case '-':
1272                                break; /* ok */
1273                        default:
1274                                if (isalnum(*cp))
1275                                        break;
1276                                /* it isn't */
1277                                goto literal_copy;
1278                        }
1279                }
1280                /* @ past the first slash does not count */
1281                cp = strchr(scheme_prefix + 3, '/');
1282                if (cp && cp < anon_part)
1283                        goto literal_copy;
1284                prefix_len = scheme_prefix - url + 3;
1285        }
1286        return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1287                       (int)anon_len, anon_part);
1288literal_copy:
1289        return xstrdup(url);
1290}
1291
1292static void read_alternate_refs(const char *path,
1293                                alternate_ref_fn *cb,
1294                                void *data)
1295{
1296        struct child_process cmd = CHILD_PROCESS_INIT;
1297        struct strbuf line = STRBUF_INIT;
1298        FILE *fh;
1299
1300        cmd.git_cmd = 1;
1301        argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1302        argv_array_push(&cmd.args, "for-each-ref");
1303        argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1304        cmd.env = local_repo_env;
1305        cmd.out = -1;
1306
1307        if (start_command(&cmd))
1308                return;
1309
1310        fh = xfdopen(cmd.out, "r");
1311        while (strbuf_getline_lf(&line, fh) != EOF) {
1312                struct object_id oid;
1313
1314                if (get_oid_hex(line.buf, &oid) ||
1315                    line.buf[GIT_SHA1_HEXSZ] != ' ') {
1316                        warning("invalid line while parsing alternate refs: %s",
1317                                line.buf);
1318                        break;
1319                }
1320
1321                cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1322        }
1323
1324        fclose(fh);
1325        finish_command(&cmd);
1326}
1327
1328struct alternate_refs_data {
1329        alternate_ref_fn *fn;
1330        void *data;
1331};
1332
1333static int refs_from_alternate_cb(struct alternate_object_database *e,
1334                                  void *data)
1335{
1336        struct strbuf path = STRBUF_INIT;
1337        size_t base_len;
1338        struct alternate_refs_data *cb = data;
1339
1340        if (!strbuf_realpath(&path, e->path, 0))
1341                goto out;
1342        if (!strbuf_strip_suffix(&path, "/objects"))
1343                goto out;
1344        base_len = path.len;
1345
1346        /* Is this a git repository with refs? */
1347        strbuf_addstr(&path, "/refs");
1348        if (!is_directory(path.buf))
1349                goto out;
1350        strbuf_setlen(&path, base_len);
1351
1352        read_alternate_refs(path.buf, cb->fn, cb->data);
1353
1354out:
1355        strbuf_release(&path);
1356        return 0;
1357}
1358
1359void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1360{
1361        struct alternate_refs_data cb;
1362        cb.fn = fn;
1363        cb.data = data;
1364        foreach_alt_odb(refs_from_alternate_cb, &cb);
1365}