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