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