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