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