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