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