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