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