transport.con commit Merge branch 'gb/maint-am-patch-format-error-message' (e7734c6)
   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 "send-pack.h"
   7#include "walker.h"
   8#include "bundle.h"
   9#include "dir.h"
  10#include "refs.h"
  11#include "branch.h"
  12#include "url.h"
  13#include "submodule.h"
  14
  15/* rsync support */
  16
  17/*
  18 * We copy packed-refs and refs/ into a temporary file, then read the
  19 * loose refs recursively (sorting whenever possible), and then inserting
  20 * those packed refs that are not yet in the list (not validating, but
  21 * assuming that the file is sorted).
  22 *
  23 * Appears refactoring this from refs.c is too cumbersome.
  24 */
  25
  26static int str_cmp(const void *a, const void *b)
  27{
  28        const char *s1 = a;
  29        const char *s2 = b;
  30
  31        return strcmp(s1, s2);
  32}
  33
  34/* path->buf + name_offset is expected to point to "refs/" */
  35
  36static int read_loose_refs(struct strbuf *path, int name_offset,
  37                struct ref **tail)
  38{
  39        DIR *dir = opendir(path->buf);
  40        struct dirent *de;
  41        struct {
  42                char **entries;
  43                int nr, alloc;
  44        } list;
  45        int i, pathlen;
  46
  47        if (!dir)
  48                return -1;
  49
  50        memset (&list, 0, sizeof(list));
  51
  52        while ((de = readdir(dir))) {
  53                if (is_dot_or_dotdot(de->d_name))
  54                        continue;
  55                ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
  56                list.entries[list.nr++] = xstrdup(de->d_name);
  57        }
  58        closedir(dir);
  59
  60        /* sort the list */
  61
  62        qsort(list.entries, list.nr, sizeof(char *), str_cmp);
  63
  64        pathlen = path->len;
  65        strbuf_addch(path, '/');
  66
  67        for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
  68                strbuf_addstr(path, list.entries[i]);
  69                if (read_loose_refs(path, name_offset, tail)) {
  70                        int fd = open(path->buf, O_RDONLY);
  71                        char buffer[40];
  72                        struct ref *next;
  73
  74                        if (fd < 0)
  75                                continue;
  76                        next = alloc_ref(path->buf + name_offset);
  77                        if (read_in_full(fd, buffer, 40) != 40 ||
  78                                        get_sha1_hex(buffer, next->old_sha1)) {
  79                                close(fd);
  80                                free(next);
  81                                continue;
  82                        }
  83                        close(fd);
  84                        (*tail)->next = next;
  85                        *tail = next;
  86                }
  87        }
  88        strbuf_setlen(path, pathlen);
  89
  90        for (i = 0; i < list.nr; i++)
  91                free(list.entries[i]);
  92        free(list.entries);
  93
  94        return 0;
  95}
  96
  97/* insert the packed refs for which no loose refs were found */
  98
  99static void insert_packed_refs(const char *packed_refs, struct ref **list)
 100{
 101        FILE *f = fopen(packed_refs, "r");
 102        static char buffer[PATH_MAX];
 103
 104        if (!f)
 105                return;
 106
 107        for (;;) {
 108                int cmp = cmp, len;
 109
 110                if (!fgets(buffer, sizeof(buffer), f)) {
 111                        fclose(f);
 112                        return;
 113                }
 114
 115                if (hexval(buffer[0]) > 0xf)
 116                        continue;
 117                len = strlen(buffer);
 118                if (len && buffer[len - 1] == '\n')
 119                        buffer[--len] = '\0';
 120                if (len < 41)
 121                        continue;
 122                while ((*list)->next &&
 123                                (cmp = strcmp(buffer + 41,
 124                                      (*list)->next->name)) > 0)
 125                        list = &(*list)->next;
 126                if (!(*list)->next || cmp < 0) {
 127                        struct ref *next = alloc_ref(buffer + 41);
 128                        buffer[40] = '\0';
 129                        if (get_sha1_hex(buffer, next->old_sha1)) {
 130                                warning ("invalid SHA-1: %s", buffer);
 131                                free(next);
 132                                continue;
 133                        }
 134                        next->next = (*list)->next;
 135                        (*list)->next = next;
 136                        list = &(*list)->next;
 137                }
 138        }
 139}
 140
 141static void set_upstreams(struct transport *transport, struct ref *refs,
 142        int pretend)
 143{
 144        struct ref *ref;
 145        for (ref = refs; ref; ref = ref->next) {
 146                const char *localname;
 147                const char *tmp;
 148                const char *remotename;
 149                unsigned char sha[20];
 150                int flag = 0;
 151                /*
 152                 * Check suitability for tracking. Must be successful /
 153                 * already up-to-date ref create/modify (not delete).
 154                 */
 155                if (ref->status != REF_STATUS_OK &&
 156                        ref->status != REF_STATUS_UPTODATE)
 157                        continue;
 158                if (!ref->peer_ref)
 159                        continue;
 160                if (is_null_sha1(ref->new_sha1))
 161                        continue;
 162
 163                /* Follow symbolic refs (mainly for HEAD). */
 164                localname = ref->peer_ref->name;
 165                remotename = ref->name;
 166                tmp = resolve_ref(localname, sha, 1, &flag);
 167                if (tmp && flag & REF_ISSYMREF &&
 168                        !prefixcmp(tmp, "refs/heads/"))
 169                        localname = tmp;
 170
 171                /* Both source and destination must be local branches. */
 172                if (!localname || prefixcmp(localname, "refs/heads/"))
 173                        continue;
 174                if (!remotename || prefixcmp(remotename, "refs/heads/"))
 175                        continue;
 176
 177                if (!pretend)
 178                        install_branch_config(BRANCH_CONFIG_VERBOSE,
 179                                localname + 11, transport->remote->name,
 180                                remotename);
 181                else
 182                        printf("Would set upstream of '%s' to '%s' of '%s'\n",
 183                                localname + 11, remotename + 11,
 184                                transport->remote->name);
 185        }
 186}
 187
 188static const char *rsync_url(const char *url)
 189{
 190        return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
 191}
 192
 193static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
 194{
 195        struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
 196        struct ref dummy = {NULL}, *tail = &dummy;
 197        struct child_process rsync;
 198        const char *args[5];
 199        int temp_dir_len;
 200
 201        if (for_push)
 202                return NULL;
 203
 204        /* copy the refs to the temporary directory */
 205
 206        strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
 207        if (!mkdtemp(temp_dir.buf))
 208                die_errno ("Could not make temporary directory");
 209        temp_dir_len = temp_dir.len;
 210
 211        strbuf_addstr(&buf, rsync_url(transport->url));
 212        strbuf_addstr(&buf, "/refs");
 213
 214        memset(&rsync, 0, sizeof(rsync));
 215        rsync.argv = args;
 216        rsync.stdout_to_stderr = 1;
 217        args[0] = "rsync";
 218        args[1] = (transport->verbose > 0) ? "-rv" : "-r";
 219        args[2] = buf.buf;
 220        args[3] = temp_dir.buf;
 221        args[4] = NULL;
 222
 223        if (run_command(&rsync))
 224                die ("Could not run rsync to get refs");
 225
 226        strbuf_reset(&buf);
 227        strbuf_addstr(&buf, rsync_url(transport->url));
 228        strbuf_addstr(&buf, "/packed-refs");
 229
 230        args[2] = buf.buf;
 231
 232        if (run_command(&rsync))
 233                die ("Could not run rsync to get refs");
 234
 235        /* read the copied refs */
 236
 237        strbuf_addstr(&temp_dir, "/refs");
 238        read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
 239        strbuf_setlen(&temp_dir, temp_dir_len);
 240
 241        tail = &dummy;
 242        strbuf_addstr(&temp_dir, "/packed-refs");
 243        insert_packed_refs(temp_dir.buf, &tail);
 244        strbuf_setlen(&temp_dir, temp_dir_len);
 245
 246        if (remove_dir_recursively(&temp_dir, 0))
 247                warning ("Error removing temporary directory %s.",
 248                                temp_dir.buf);
 249
 250        strbuf_release(&buf);
 251        strbuf_release(&temp_dir);
 252
 253        return dummy.next;
 254}
 255
 256static int fetch_objs_via_rsync(struct transport *transport,
 257                                int nr_objs, struct ref **to_fetch)
 258{
 259        struct strbuf buf = STRBUF_INIT;
 260        struct child_process rsync;
 261        const char *args[8];
 262        int result;
 263
 264        strbuf_addstr(&buf, rsync_url(transport->url));
 265        strbuf_addstr(&buf, "/objects/");
 266
 267        memset(&rsync, 0, sizeof(rsync));
 268        rsync.argv = args;
 269        rsync.stdout_to_stderr = 1;
 270        args[0] = "rsync";
 271        args[1] = (transport->verbose > 0) ? "-rv" : "-r";
 272        args[2] = "--ignore-existing";
 273        args[3] = "--exclude";
 274        args[4] = "info";
 275        args[5] = buf.buf;
 276        args[6] = get_object_directory();
 277        args[7] = NULL;
 278
 279        /* NEEDSWORK: handle one level of alternates */
 280        result = run_command(&rsync);
 281
 282        strbuf_release(&buf);
 283
 284        return result;
 285}
 286
 287static int write_one_ref(const char *name, const unsigned char *sha1,
 288                int flags, void *data)
 289{
 290        struct strbuf *buf = data;
 291        int len = buf->len;
 292        FILE *f;
 293
 294        /* when called via for_each_ref(), flags is non-zero */
 295        if (flags && prefixcmp(name, "refs/heads/") &&
 296                        prefixcmp(name, "refs/tags/"))
 297                return 0;
 298
 299        strbuf_addstr(buf, name);
 300        if (safe_create_leading_directories(buf->buf) ||
 301                        !(f = fopen(buf->buf, "w")) ||
 302                        fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
 303                        fclose(f))
 304                return error("problems writing temporary file %s", buf->buf);
 305        strbuf_setlen(buf, len);
 306        return 0;
 307}
 308
 309static int write_refs_to_temp_dir(struct strbuf *temp_dir,
 310                int refspec_nr, const char **refspec)
 311{
 312        int i;
 313
 314        for (i = 0; i < refspec_nr; i++) {
 315                unsigned char sha1[20];
 316                char *ref;
 317
 318                if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
 319                        return error("Could not get ref %s", refspec[i]);
 320
 321                if (write_one_ref(ref, sha1, 0, temp_dir)) {
 322                        free(ref);
 323                        return -1;
 324                }
 325                free(ref);
 326        }
 327        return 0;
 328}
 329
 330static int rsync_transport_push(struct transport *transport,
 331                int refspec_nr, const char **refspec, int flags)
 332{
 333        struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
 334        int result = 0, i;
 335        struct child_process rsync;
 336        const char *args[10];
 337
 338        if (flags & TRANSPORT_PUSH_MIRROR)
 339                return error("rsync transport does not support mirror mode");
 340
 341        /* first push the objects */
 342
 343        strbuf_addstr(&buf, rsync_url(transport->url));
 344        strbuf_addch(&buf, '/');
 345
 346        memset(&rsync, 0, sizeof(rsync));
 347        rsync.argv = args;
 348        rsync.stdout_to_stderr = 1;
 349        i = 0;
 350        args[i++] = "rsync";
 351        args[i++] = "-a";
 352        if (flags & TRANSPORT_PUSH_DRY_RUN)
 353                args[i++] = "--dry-run";
 354        if (transport->verbose > 0)
 355                args[i++] = "-v";
 356        args[i++] = "--ignore-existing";
 357        args[i++] = "--exclude";
 358        args[i++] = "info";
 359        args[i++] = get_object_directory();
 360        args[i++] = buf.buf;
 361        args[i++] = NULL;
 362
 363        if (run_command(&rsync))
 364                return error("Could not push objects to %s",
 365                                rsync_url(transport->url));
 366
 367        /* copy the refs to the temporary directory; they could be packed. */
 368
 369        strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
 370        if (!mkdtemp(temp_dir.buf))
 371                die_errno ("Could not make temporary directory");
 372        strbuf_addch(&temp_dir, '/');
 373
 374        if (flags & TRANSPORT_PUSH_ALL) {
 375                if (for_each_ref(write_one_ref, &temp_dir))
 376                        return -1;
 377        } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
 378                return -1;
 379
 380        i = 2;
 381        if (flags & TRANSPORT_PUSH_DRY_RUN)
 382                args[i++] = "--dry-run";
 383        if (!(flags & TRANSPORT_PUSH_FORCE))
 384                args[i++] = "--ignore-existing";
 385        args[i++] = temp_dir.buf;
 386        args[i++] = rsync_url(transport->url);
 387        args[i++] = NULL;
 388        if (run_command(&rsync))
 389                result = error("Could not push to %s",
 390                                rsync_url(transport->url));
 391
 392        if (remove_dir_recursively(&temp_dir, 0))
 393                warning ("Could not remove temporary directory %s.",
 394                                temp_dir.buf);
 395
 396        strbuf_release(&buf);
 397        strbuf_release(&temp_dir);
 398
 399        return result;
 400}
 401
 402struct bundle_transport_data {
 403        int fd;
 404        struct bundle_header header;
 405};
 406
 407static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
 408{
 409        struct bundle_transport_data *data = transport->data;
 410        struct ref *result = NULL;
 411        int i;
 412
 413        if (for_push)
 414                return NULL;
 415
 416        if (data->fd > 0)
 417                close(data->fd);
 418        data->fd = read_bundle_header(transport->url, &data->header);
 419        if (data->fd < 0)
 420                die ("Could not read bundle '%s'.", transport->url);
 421        for (i = 0; i < data->header.references.nr; i++) {
 422                struct ref_list_entry *e = data->header.references.list + i;
 423                struct ref *ref = alloc_ref(e->name);
 424                hashcpy(ref->old_sha1, e->sha1);
 425                ref->next = result;
 426                result = ref;
 427        }
 428        return result;
 429}
 430
 431static int fetch_refs_from_bundle(struct transport *transport,
 432                               int nr_heads, struct ref **to_fetch)
 433{
 434        struct bundle_transport_data *data = transport->data;
 435        return unbundle(&data->header, data->fd);
 436}
 437
 438static int close_bundle(struct transport *transport)
 439{
 440        struct bundle_transport_data *data = transport->data;
 441        if (data->fd > 0)
 442                close(data->fd);
 443        free(data);
 444        return 0;
 445}
 446
 447struct git_transport_data {
 448        struct git_transport_options options;
 449        struct child_process *conn;
 450        int fd[2];
 451        unsigned got_remote_heads : 1;
 452        struct extra_have_objects extra_have;
 453};
 454
 455static int set_git_option(struct git_transport_options *opts,
 456                          const char *name, const char *value)
 457{
 458        if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
 459                opts->uploadpack = value;
 460                return 0;
 461        } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
 462                opts->receivepack = value;
 463                return 0;
 464        } else if (!strcmp(name, TRANS_OPT_THIN)) {
 465                opts->thin = !!value;
 466                return 0;
 467        } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
 468                opts->followtags = !!value;
 469                return 0;
 470        } else if (!strcmp(name, TRANS_OPT_KEEP)) {
 471                opts->keep = !!value;
 472                return 0;
 473        } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
 474                if (!value)
 475                        opts->depth = 0;
 476                else
 477                        opts->depth = atoi(value);
 478                return 0;
 479        }
 480        return 1;
 481}
 482
 483static int connect_setup(struct transport *transport, int for_push, int verbose)
 484{
 485        struct git_transport_data *data = transport->data;
 486        struct strbuf sb = STRBUF_INIT;
 487
 488        if (data->conn)
 489                return 0;
 490
 491        strbuf_addstr(&sb, for_push ? data->options.receivepack :
 492                                 data->options.uploadpack);
 493        if (for_push && transport->verbose < 0)
 494                strbuf_addstr(&sb, " --quiet");
 495        data->conn = git_connect(data->fd, transport->url, sb.buf,
 496                                 verbose ? CONNECT_VERBOSE : 0);
 497        strbuf_release(&sb);
 498
 499        return 0;
 500}
 501
 502static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
 503{
 504        struct git_transport_data *data = transport->data;
 505        struct ref *refs;
 506
 507        connect_setup(transport, for_push, 0);
 508        get_remote_heads(data->fd[0], &refs, 0, NULL,
 509                         for_push ? REF_NORMAL : 0, &data->extra_have);
 510        data->got_remote_heads = 1;
 511
 512        return refs;
 513}
 514
 515static int fetch_refs_via_pack(struct transport *transport,
 516                               int nr_heads, struct ref **to_fetch)
 517{
 518        struct git_transport_data *data = transport->data;
 519        char **heads = xmalloc(nr_heads * sizeof(*heads));
 520        char **origh = xmalloc(nr_heads * sizeof(*origh));
 521        const struct ref *refs;
 522        char *dest = xstrdup(transport->url);
 523        struct fetch_pack_args args;
 524        int i;
 525        struct ref *refs_tmp = NULL;
 526
 527        memset(&args, 0, sizeof(args));
 528        args.uploadpack = data->options.uploadpack;
 529        args.keep_pack = data->options.keep;
 530        args.lock_pack = 1;
 531        args.use_thin_pack = data->options.thin;
 532        args.include_tag = data->options.followtags;
 533        args.verbose = (transport->verbose > 0);
 534        args.quiet = (transport->verbose < 0);
 535        args.no_progress = !transport->progress;
 536        args.depth = data->options.depth;
 537
 538        for (i = 0; i < nr_heads; i++)
 539                origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
 540
 541        if (!data->got_remote_heads) {
 542                connect_setup(transport, 0, 0);
 543                get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
 544                data->got_remote_heads = 1;
 545        }
 546
 547        refs = fetch_pack(&args, data->fd, data->conn,
 548                          refs_tmp ? refs_tmp : transport->remote_refs,
 549                          dest, nr_heads, heads, &transport->pack_lockfile);
 550        close(data->fd[0]);
 551        close(data->fd[1]);
 552        if (finish_connect(data->conn))
 553                refs = NULL;
 554        data->conn = NULL;
 555        data->got_remote_heads = 0;
 556
 557        free_refs(refs_tmp);
 558
 559        for (i = 0; i < nr_heads; i++)
 560                free(origh[i]);
 561        free(origh);
 562        free(heads);
 563        free(dest);
 564        return (refs ? 0 : -1);
 565}
 566
 567static int push_had_errors(struct ref *ref)
 568{
 569        for (; ref; ref = ref->next) {
 570                switch (ref->status) {
 571                case REF_STATUS_NONE:
 572                case REF_STATUS_UPTODATE:
 573                case REF_STATUS_OK:
 574                        break;
 575                default:
 576                        return 1;
 577                }
 578        }
 579        return 0;
 580}
 581
 582int transport_refs_pushed(struct ref *ref)
 583{
 584        for (; ref; ref = ref->next) {
 585                switch(ref->status) {
 586                case REF_STATUS_NONE:
 587                case REF_STATUS_UPTODATE:
 588                        break;
 589                default:
 590                        return 1;
 591                }
 592        }
 593        return 0;
 594}
 595
 596void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
 597{
 598        struct refspec rs;
 599
 600        if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
 601                return;
 602
 603        rs.src = ref->name;
 604        rs.dst = NULL;
 605
 606        if (!remote_find_tracking(remote, &rs)) {
 607                if (verbose)
 608                        fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
 609                if (ref->deletion) {
 610                        delete_ref(rs.dst, NULL, 0);
 611                } else
 612                        update_ref("update by push", rs.dst,
 613                                        ref->new_sha1, NULL, 0, 0);
 614                free(rs.dst);
 615        }
 616}
 617
 618static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
 619{
 620        if (porcelain) {
 621                if (from)
 622                        fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
 623                else
 624                        fprintf(stdout, "%c\t:%s\t", flag, to->name);
 625                if (msg)
 626                        fprintf(stdout, "%s (%s)\n", summary, msg);
 627                else
 628                        fprintf(stdout, "%s\n", summary);
 629        } else {
 630                fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
 631                if (from)
 632                        fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
 633                else
 634                        fputs(prettify_refname(to->name), stderr);
 635                if (msg) {
 636                        fputs(" (", stderr);
 637                        fputs(msg, stderr);
 638                        fputc(')', stderr);
 639                }
 640                fputc('\n', stderr);
 641        }
 642}
 643
 644static const char *status_abbrev(unsigned char sha1[20])
 645{
 646        return find_unique_abbrev(sha1, DEFAULT_ABBREV);
 647}
 648
 649static void print_ok_ref_status(struct ref *ref, int porcelain)
 650{
 651        if (ref->deletion)
 652                print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
 653        else if (is_null_sha1(ref->old_sha1))
 654                print_ref_status('*',
 655                        (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
 656                        "[new branch]"),
 657                        ref, ref->peer_ref, NULL, porcelain);
 658        else {
 659                char quickref[84];
 660                char type;
 661                const char *msg;
 662
 663                strcpy(quickref, status_abbrev(ref->old_sha1));
 664                if (ref->nonfastforward) {
 665                        strcat(quickref, "...");
 666                        type = '+';
 667                        msg = "forced update";
 668                } else {
 669                        strcat(quickref, "..");
 670                        type = ' ';
 671                        msg = NULL;
 672                }
 673                strcat(quickref, status_abbrev(ref->new_sha1));
 674
 675                print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
 676        }
 677}
 678
 679static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
 680{
 681        if (!count)
 682                fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
 683
 684        switch(ref->status) {
 685        case REF_STATUS_NONE:
 686                print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
 687                break;
 688        case REF_STATUS_REJECT_NODELETE:
 689                print_ref_status('!', "[rejected]", ref, NULL,
 690                                                 "remote does not support deleting refs", porcelain);
 691                break;
 692        case REF_STATUS_UPTODATE:
 693                print_ref_status('=', "[up to date]", ref,
 694                                                 ref->peer_ref, NULL, porcelain);
 695                break;
 696        case REF_STATUS_REJECT_NONFASTFORWARD:
 697                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 698                                                 "non-fast-forward", porcelain);
 699                break;
 700        case REF_STATUS_REMOTE_REJECT:
 701                print_ref_status('!', "[remote rejected]", ref,
 702                                                 ref->deletion ? NULL : ref->peer_ref,
 703                                                 ref->remote_status, porcelain);
 704                break;
 705        case REF_STATUS_EXPECTING_REPORT:
 706                print_ref_status('!', "[remote failure]", ref,
 707                                                 ref->deletion ? NULL : ref->peer_ref,
 708                                                 "remote failed to report status", porcelain);
 709                break;
 710        case REF_STATUS_OK:
 711                print_ok_ref_status(ref, porcelain);
 712                break;
 713        }
 714
 715        return 1;
 716}
 717
 718void transport_print_push_status(const char *dest, struct ref *refs,
 719                                  int verbose, int porcelain, int *nonfastforward)
 720{
 721        struct ref *ref;
 722        int n = 0;
 723
 724        if (verbose) {
 725                for (ref = refs; ref; ref = ref->next)
 726                        if (ref->status == REF_STATUS_UPTODATE)
 727                                n += print_one_push_status(ref, dest, n, porcelain);
 728        }
 729
 730        for (ref = refs; ref; ref = ref->next)
 731                if (ref->status == REF_STATUS_OK)
 732                        n += print_one_push_status(ref, dest, n, porcelain);
 733
 734        *nonfastforward = 0;
 735        for (ref = refs; ref; ref = ref->next) {
 736                if (ref->status != REF_STATUS_NONE &&
 737                    ref->status != REF_STATUS_UPTODATE &&
 738                    ref->status != REF_STATUS_OK)
 739                        n += print_one_push_status(ref, dest, n, porcelain);
 740                if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
 741                        *nonfastforward = 1;
 742        }
 743}
 744
 745void transport_verify_remote_names(int nr_heads, const char **heads)
 746{
 747        int i;
 748
 749        for (i = 0; i < nr_heads; i++) {
 750                const char *local = heads[i];
 751                const char *remote = strrchr(heads[i], ':');
 752
 753                if (*local == '+')
 754                        local++;
 755
 756                /* A matching refspec is okay.  */
 757                if (remote == local && remote[1] == '\0')
 758                        continue;
 759
 760                remote = remote ? (remote + 1) : local;
 761                switch (check_ref_format(remote)) {
 762                case 0: /* ok */
 763                case CHECK_REF_FORMAT_ONELEVEL:
 764                        /* ok but a single level -- that is fine for
 765                         * a match pattern.
 766                         */
 767                case CHECK_REF_FORMAT_WILDCARD:
 768                        /* ok but ends with a pattern-match character */
 769                        continue;
 770                }
 771                die("remote part of refspec is not a valid name in %s",
 772                    heads[i]);
 773        }
 774}
 775
 776static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
 777{
 778        struct git_transport_data *data = transport->data;
 779        struct send_pack_args args;
 780        int ret;
 781
 782        if (!data->got_remote_heads) {
 783                struct ref *tmp_refs;
 784                connect_setup(transport, 1, 0);
 785
 786                get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
 787                                 NULL);
 788                data->got_remote_heads = 1;
 789        }
 790
 791        memset(&args, 0, sizeof(args));
 792        args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
 793        args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
 794        args.use_thin_pack = data->options.thin;
 795        args.verbose = (transport->verbose > 0);
 796        args.quiet = (transport->verbose < 0);
 797        args.progress = transport->progress;
 798        args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
 799        args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
 800
 801        ret = send_pack(&args, data->fd, data->conn, remote_refs,
 802                        &data->extra_have);
 803
 804        close(data->fd[1]);
 805        close(data->fd[0]);
 806        ret |= finish_connect(data->conn);
 807        data->conn = NULL;
 808        data->got_remote_heads = 0;
 809
 810        return ret;
 811}
 812
 813static int connect_git(struct transport *transport, const char *name,
 814                       const char *executable, int fd[2])
 815{
 816        struct git_transport_data *data = transport->data;
 817        data->conn = git_connect(data->fd, transport->url,
 818                                 executable, 0);
 819        fd[0] = data->fd[0];
 820        fd[1] = data->fd[1];
 821        return 0;
 822}
 823
 824static int disconnect_git(struct transport *transport)
 825{
 826        struct git_transport_data *data = transport->data;
 827        if (data->conn) {
 828                if (data->got_remote_heads)
 829                        packet_flush(data->fd[1]);
 830                close(data->fd[0]);
 831                close(data->fd[1]);
 832                finish_connect(data->conn);
 833        }
 834
 835        free(data);
 836        return 0;
 837}
 838
 839void transport_take_over(struct transport *transport,
 840                         struct child_process *child)
 841{
 842        struct git_transport_data *data;
 843
 844        if (!transport->smart_options)
 845                die("Bug detected: Taking over transport requires non-NULL "
 846                    "smart_options field.");
 847
 848        data = xcalloc(1, sizeof(*data));
 849        data->options = *transport->smart_options;
 850        data->conn = child;
 851        data->fd[0] = data->conn->out;
 852        data->fd[1] = data->conn->in;
 853        data->got_remote_heads = 0;
 854        transport->data = data;
 855
 856        transport->set_option = NULL;
 857        transport->get_refs_list = get_refs_via_connect;
 858        transport->fetch = fetch_refs_via_pack;
 859        transport->push = NULL;
 860        transport->push_refs = git_transport_push;
 861        transport->disconnect = disconnect_git;
 862        transport->smart_options = &(data->options);
 863}
 864
 865static int is_local(const char *url)
 866{
 867        const char *colon = strchr(url, ':');
 868        const char *slash = strchr(url, '/');
 869        return !colon || (slash && slash < colon) ||
 870                has_dos_drive_prefix(url);
 871}
 872
 873static int is_file(const char *url)
 874{
 875        struct stat buf;
 876        if (stat(url, &buf))
 877                return 0;
 878        return S_ISREG(buf.st_mode);
 879}
 880
 881static int external_specification_len(const char *url)
 882{
 883        return strchr(url, ':') - url;
 884}
 885
 886struct transport *transport_get(struct remote *remote, const char *url)
 887{
 888        const char *helper;
 889        struct transport *ret = xcalloc(1, sizeof(*ret));
 890
 891        ret->progress = isatty(2);
 892
 893        if (!remote)
 894                die("No remote provided to transport_get()");
 895
 896        ret->got_remote_refs = 0;
 897        ret->remote = remote;
 898        helper = remote->foreign_vcs;
 899
 900        if (!url && remote->url)
 901                url = remote->url[0];
 902        ret->url = url;
 903
 904        /* maybe it is a foreign URL? */
 905        if (url) {
 906                const char *p = url;
 907
 908                while (is_urlschemechar(p == url, *p))
 909                        p++;
 910                if (!prefixcmp(p, "::"))
 911                        helper = xstrndup(url, p - url);
 912        }
 913
 914        if (helper) {
 915                transport_helper_init(ret, helper);
 916        } else if (!prefixcmp(url, "rsync:")) {
 917                ret->get_refs_list = get_refs_via_rsync;
 918                ret->fetch = fetch_objs_via_rsync;
 919                ret->push = rsync_transport_push;
 920                ret->smart_options = NULL;
 921        } else if (is_local(url) && is_file(url)) {
 922                struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
 923                ret->data = data;
 924                ret->get_refs_list = get_refs_from_bundle;
 925                ret->fetch = fetch_refs_from_bundle;
 926                ret->disconnect = close_bundle;
 927                ret->smart_options = NULL;
 928        } else if (!is_url(url)
 929                || !prefixcmp(url, "file://")
 930                || !prefixcmp(url, "git://")
 931                || !prefixcmp(url, "ssh://")
 932                || !prefixcmp(url, "git+ssh://")
 933                || !prefixcmp(url, "ssh+git://")) {
 934                /* These are builtin smart transports. */
 935                struct git_transport_data *data = xcalloc(1, sizeof(*data));
 936                ret->data = data;
 937                ret->set_option = NULL;
 938                ret->get_refs_list = get_refs_via_connect;
 939                ret->fetch = fetch_refs_via_pack;
 940                ret->push_refs = git_transport_push;
 941                ret->connect = connect_git;
 942                ret->disconnect = disconnect_git;
 943                ret->smart_options = &(data->options);
 944
 945                data->conn = NULL;
 946                data->got_remote_heads = 0;
 947        } else {
 948                /* Unknown protocol in URL. Pass to external handler. */
 949                int len = external_specification_len(url);
 950                char *handler = xmalloc(len + 1);
 951                handler[len] = 0;
 952                strncpy(handler, url, len);
 953                transport_helper_init(ret, handler);
 954        }
 955
 956        if (ret->smart_options) {
 957                ret->smart_options->thin = 1;
 958                ret->smart_options->uploadpack = "git-upload-pack";
 959                if (remote->uploadpack)
 960                        ret->smart_options->uploadpack = remote->uploadpack;
 961                ret->smart_options->receivepack = "git-receive-pack";
 962                if (remote->receivepack)
 963                        ret->smart_options->receivepack = remote->receivepack;
 964        }
 965
 966        return ret;
 967}
 968
 969int transport_set_option(struct transport *transport,
 970                         const char *name, const char *value)
 971{
 972        int git_reports = 1, protocol_reports = 1;
 973
 974        if (transport->smart_options)
 975                git_reports = set_git_option(transport->smart_options,
 976                                             name, value);
 977
 978        if (transport->set_option)
 979                protocol_reports = transport->set_option(transport, name,
 980                                                        value);
 981
 982        /* If either report is 0, report 0 (success). */
 983        if (!git_reports || !protocol_reports)
 984                return 0;
 985        /* If either reports -1 (invalid value), report -1. */
 986        if ((git_reports == -1) || (protocol_reports == -1))
 987                return -1;
 988        /* Otherwise if both report unknown, report unknown. */
 989        return 1;
 990}
 991
 992void transport_set_verbosity(struct transport *transport, int verbosity,
 993        int force_progress)
 994{
 995        if (verbosity >= 2)
 996                transport->verbose = verbosity <= 3 ? verbosity : 3;
 997        if (verbosity < 0)
 998                transport->verbose = -1;
 999
1000        /**
1001         * Rules used to determine whether to report progress (processing aborts
1002         * when a rule is satisfied):
1003         *
1004         *   1. Report progress, if force_progress is 1 (ie. --progress).
1005         *   2. Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1006         *   3. Report progress if isatty(2) is 1.
1007         **/
1008        transport->progress = force_progress || (verbosity >= 0 && isatty(2));
1009}
1010
1011int transport_push(struct transport *transport,
1012                   int refspec_nr, const char **refspec, int flags,
1013                   int *nonfastforward)
1014{
1015        *nonfastforward = 0;
1016        transport_verify_remote_names(refspec_nr, refspec);
1017
1018        if (transport->push) {
1019                /* Maybe FIXME. But no important transport uses this case. */
1020                if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1021                        die("This transport does not support using --set-upstream");
1022
1023                return transport->push(transport, refspec_nr, refspec, flags);
1024        } else if (transport->push_refs) {
1025                struct ref *remote_refs =
1026                        transport->get_refs_list(transport, 1);
1027                struct ref *local_refs = get_local_heads();
1028                int match_flags = MATCH_REFS_NONE;
1029                int verbose = (transport->verbose > 0);
1030                int quiet = (transport->verbose < 0);
1031                int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1032                int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1033                int push_ret, ret, err;
1034
1035                if (flags & TRANSPORT_PUSH_ALL)
1036                        match_flags |= MATCH_REFS_ALL;
1037                if (flags & TRANSPORT_PUSH_MIRROR)
1038                        match_flags |= MATCH_REFS_MIRROR;
1039
1040                if (match_refs(local_refs, &remote_refs,
1041                               refspec_nr, refspec, match_flags)) {
1042                        return -1;
1043                }
1044
1045                set_ref_status_for_push(remote_refs,
1046                        flags & TRANSPORT_PUSH_MIRROR,
1047                        flags & TRANSPORT_PUSH_FORCE);
1048
1049                if ((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) && !is_bare_repository()) {
1050                        struct ref *ref = remote_refs;
1051                        for (; ref; ref = ref->next)
1052                                if (!is_null_sha1(ref->new_sha1) &&
1053                                    check_submodule_needs_pushing(ref->new_sha1,transport->remote->name))
1054                                        die("There are unpushed submodules, aborting.");
1055                }
1056
1057                push_ret = transport->push_refs(transport, remote_refs, flags);
1058                err = push_had_errors(remote_refs);
1059                ret = push_ret | err;
1060
1061                if (!quiet || err)
1062                        transport_print_push_status(transport->url, remote_refs,
1063                                        verbose | porcelain, porcelain,
1064                                        nonfastforward);
1065
1066                if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1067                        set_upstreams(transport, remote_refs, pretend);
1068
1069                if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1070                        struct ref *ref;
1071                        for (ref = remote_refs; ref; ref = ref->next)
1072                                transport_update_tracking_ref(transport->remote, ref, verbose);
1073                }
1074
1075                if (porcelain && !push_ret)
1076                        puts("Done");
1077                else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1078                        fprintf(stderr, "Everything up-to-date\n");
1079
1080                return ret;
1081        }
1082        return 1;
1083}
1084
1085const struct ref *transport_get_remote_refs(struct transport *transport)
1086{
1087        if (!transport->got_remote_refs) {
1088                transport->remote_refs = transport->get_refs_list(transport, 0);
1089                transport->got_remote_refs = 1;
1090        }
1091
1092        return transport->remote_refs;
1093}
1094
1095int transport_fetch_refs(struct transport *transport, struct ref *refs)
1096{
1097        int rc;
1098        int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1099        struct ref **heads = NULL;
1100        struct ref *rm;
1101
1102        for (rm = refs; rm; rm = rm->next) {
1103                nr_refs++;
1104                if (rm->peer_ref &&
1105                    !is_null_sha1(rm->old_sha1) &&
1106                    !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1107                        continue;
1108                ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1109                heads[nr_heads++] = rm;
1110        }
1111
1112        if (!nr_heads) {
1113                /*
1114                 * When deepening of a shallow repository is requested,
1115                 * then local and remote refs are likely to still be equal.
1116                 * Just feed them all to the fetch method in that case.
1117                 * This condition shouldn't be met in a non-deepening fetch
1118                 * (see builtin-fetch.c:quickfetch()).
1119                 */
1120                heads = xmalloc(nr_refs * sizeof(*heads));
1121                for (rm = refs; rm; rm = rm->next)
1122                        heads[nr_heads++] = rm;
1123        }
1124
1125        rc = transport->fetch(transport, nr_heads, heads);
1126
1127        free(heads);
1128        return rc;
1129}
1130
1131void transport_unlock_pack(struct transport *transport)
1132{
1133        if (transport->pack_lockfile) {
1134                unlink_or_warn(transport->pack_lockfile);
1135                free(transport->pack_lockfile);
1136                transport->pack_lockfile = NULL;
1137        }
1138}
1139
1140int transport_connect(struct transport *transport, const char *name,
1141                      const char *exec, int fd[2])
1142{
1143        if (transport->connect)
1144                return transport->connect(transport, name, exec, fd);
1145        else
1146                die("Operation not supported by protocol");
1147}
1148
1149int transport_disconnect(struct transport *transport)
1150{
1151        int ret = 0;
1152        if (transport->disconnect)
1153                ret = transport->disconnect(transport);
1154        free(transport);
1155        return ret;
1156}
1157
1158/*
1159 * Strip username (and password) from an url and return
1160 * it in a newly allocated string.
1161 */
1162char *transport_anonymize_url(const char *url)
1163{
1164        char *anon_url, *scheme_prefix, *anon_part;
1165        size_t anon_len, prefix_len = 0;
1166
1167        anon_part = strchr(url, '@');
1168        if (is_local(url) || !anon_part)
1169                goto literal_copy;
1170
1171        anon_len = strlen(++anon_part);
1172        scheme_prefix = strstr(url, "://");
1173        if (!scheme_prefix) {
1174                if (!strchr(anon_part, ':'))
1175                        /* cannot be "me@there:/path/name" */
1176                        goto literal_copy;
1177        } else {
1178                const char *cp;
1179                /* make sure scheme is reasonable */
1180                for (cp = url; cp < scheme_prefix; cp++) {
1181                        switch (*cp) {
1182                                /* RFC 1738 2.1 */
1183                        case '+': case '.': case '-':
1184                                break; /* ok */
1185                        default:
1186                                if (isalnum(*cp))
1187                                        break;
1188                                /* it isn't */
1189                                goto literal_copy;
1190                        }
1191                }
1192                /* @ past the first slash does not count */
1193                cp = strchr(scheme_prefix + 3, '/');
1194                if (cp && cp < anon_part)
1195                        goto literal_copy;
1196                prefix_len = scheme_prefix - url + 3;
1197        }
1198        anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1199        memcpy(anon_url, url, prefix_len);
1200        memcpy(anon_url + prefix_len, anon_part, anon_len);
1201        return anon_url;
1202literal_copy:
1203        return xstrdup(url);
1204}
1205
1206struct alternate_refs_data {
1207        alternate_ref_fn *fn;
1208        void *data;
1209};
1210
1211static int refs_from_alternate_cb(struct alternate_object_database *e,
1212                                  void *data)
1213{
1214        char *other;
1215        size_t len;
1216        struct remote *remote;
1217        struct transport *transport;
1218        const struct ref *extra;
1219        struct alternate_refs_data *cb = data;
1220
1221        e->name[-1] = '\0';
1222        other = xstrdup(real_path(e->base));
1223        e->name[-1] = '/';
1224        len = strlen(other);
1225
1226        while (other[len-1] == '/')
1227                other[--len] = '\0';
1228        if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1229                return 0;
1230        /* Is this a git repository with refs? */
1231        memcpy(other + len - 8, "/refs", 6);
1232        if (!is_directory(other))
1233                return 0;
1234        other[len - 8] = '\0';
1235        remote = remote_get(other);
1236        transport = transport_get(remote, other);
1237        for (extra = transport_get_remote_refs(transport);
1238             extra;
1239             extra = extra->next)
1240                cb->fn(extra, cb->data);
1241        transport_disconnect(transport);
1242        free(other);
1243        return 0;
1244}
1245
1246void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1247{
1248        struct alternate_refs_data cb;
1249        cb.fn = fn;
1250        cb.data = data;
1251        foreach_alt_odb(refs_from_alternate_cb, &cb);
1252}