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