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