send-pack.con commit Merge branch 'jk/pack-name-cleanups' into maint (ba5e05f)
   1#include "builtin.h"
   2#include "commit.h"
   3#include "refs.h"
   4#include "pkt-line.h"
   5#include "sideband.h"
   6#include "run-command.h"
   7#include "remote.h"
   8#include "connect.h"
   9#include "send-pack.h"
  10#include "quote.h"
  11#include "transport.h"
  12#include "version.h"
  13#include "sha1-array.h"
  14#include "gpg-interface.h"
  15#include "cache.h"
  16
  17int option_parse_push_signed(const struct option *opt,
  18                             const char *arg, int unset)
  19{
  20        if (unset) {
  21                *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
  22                return 0;
  23        }
  24        switch (git_parse_maybe_bool(arg)) {
  25        case 1:
  26                *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
  27                return 0;
  28        case 0:
  29                *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
  30                return 0;
  31        }
  32        if (!strcasecmp("if-asked", arg)) {
  33                *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
  34                return 0;
  35        }
  36        die("bad %s argument: %s", opt->long_name, arg);
  37}
  38
  39static void feed_object(const unsigned char *sha1, FILE *fh, int negative)
  40{
  41        if (negative && !has_sha1_file(sha1))
  42                return;
  43
  44        if (negative)
  45                putc('^', fh);
  46        fputs(sha1_to_hex(sha1), fh);
  47        putc('\n', fh);
  48}
  49
  50/*
  51 * Make a pack stream and spit it out into file descriptor fd
  52 */
  53static int pack_objects(int fd, struct ref *refs, struct sha1_array *extra, struct send_pack_args *args)
  54{
  55        /*
  56         * The child becomes pack-objects --revs; we feed
  57         * the revision parameters to it via its stdin and
  58         * let its stdout go back to the other end.
  59         */
  60        const char *argv[] = {
  61                "pack-objects",
  62                "--all-progress-implied",
  63                "--revs",
  64                "--stdout",
  65                NULL,
  66                NULL,
  67                NULL,
  68                NULL,
  69                NULL,
  70                NULL,
  71        };
  72        struct child_process po = CHILD_PROCESS_INIT;
  73        FILE *po_in;
  74        int i;
  75        int rc;
  76
  77        i = 4;
  78        if (args->use_thin_pack)
  79                argv[i++] = "--thin";
  80        if (args->use_ofs_delta)
  81                argv[i++] = "--delta-base-offset";
  82        if (args->quiet || !args->progress)
  83                argv[i++] = "-q";
  84        if (args->progress)
  85                argv[i++] = "--progress";
  86        if (is_repository_shallow())
  87                argv[i++] = "--shallow";
  88        po.argv = argv;
  89        po.in = -1;
  90        po.out = args->stateless_rpc ? -1 : fd;
  91        po.git_cmd = 1;
  92        if (start_command(&po))
  93                die_errno("git pack-objects failed");
  94
  95        /*
  96         * We feed the pack-objects we just spawned with revision
  97         * parameters by writing to the pipe.
  98         */
  99        po_in = xfdopen(po.in, "w");
 100        for (i = 0; i < extra->nr; i++)
 101                feed_object(extra->sha1[i], po_in, 1);
 102
 103        while (refs) {
 104                if (!is_null_oid(&refs->old_oid))
 105                        feed_object(refs->old_oid.hash, po_in, 1);
 106                if (!is_null_oid(&refs->new_oid))
 107                        feed_object(refs->new_oid.hash, po_in, 0);
 108                refs = refs->next;
 109        }
 110
 111        fflush(po_in);
 112        if (ferror(po_in))
 113                die_errno("error writing to pack-objects");
 114        fclose(po_in);
 115
 116        if (args->stateless_rpc) {
 117                char *buf = xmalloc(LARGE_PACKET_MAX);
 118                while (1) {
 119                        ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
 120                        if (n <= 0)
 121                                break;
 122                        send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
 123                }
 124                free(buf);
 125                close(po.out);
 126                po.out = -1;
 127        }
 128
 129        rc = finish_command(&po);
 130        if (rc) {
 131                /*
 132                 * For a normal non-zero exit, we assume pack-objects wrote
 133                 * something useful to stderr. For death by signal, though,
 134                 * we should mention it to the user. The exception is SIGPIPE
 135                 * (141), because that's a normal occurence if the remote end
 136                 * hangs up (and we'll report that by trying to read the unpack
 137                 * status).
 138                 */
 139                if (rc > 128 && rc != 141)
 140                        error("pack-objects died of signal %d", rc - 128);
 141                return -1;
 142        }
 143        return 0;
 144}
 145
 146static int receive_unpack_status(int in)
 147{
 148        const char *line = packet_read_line(in, NULL);
 149        if (!skip_prefix(line, "unpack ", &line))
 150                return error(_("unable to parse remote unpack status: %s"), line);
 151        if (strcmp(line, "ok"))
 152                return error(_("remote unpack failed: %s"), line);
 153        return 0;
 154}
 155
 156static int receive_status(int in, struct ref *refs)
 157{
 158        struct ref *hint;
 159        int ret;
 160
 161        hint = NULL;
 162        ret = receive_unpack_status(in);
 163        while (1) {
 164                char *refname;
 165                char *msg;
 166                char *line = packet_read_line(in, NULL);
 167                if (!line)
 168                        break;
 169                if (!starts_with(line, "ok ") && !starts_with(line, "ng ")) {
 170                        error("invalid ref status from remote: %s", line);
 171                        ret = -1;
 172                        break;
 173                }
 174
 175                refname = line + 3;
 176                msg = strchr(refname, ' ');
 177                if (msg)
 178                        *msg++ = '\0';
 179
 180                /* first try searching at our hint, falling back to all refs */
 181                if (hint)
 182                        hint = find_ref_by_name(hint, refname);
 183                if (!hint)
 184                        hint = find_ref_by_name(refs, refname);
 185                if (!hint) {
 186                        warning("remote reported status on unknown ref: %s",
 187                                        refname);
 188                        continue;
 189                }
 190                if (hint->status != REF_STATUS_EXPECTING_REPORT) {
 191                        warning("remote reported status on unexpected ref: %s",
 192                                        refname);
 193                        continue;
 194                }
 195
 196                if (line[0] == 'o' && line[1] == 'k')
 197                        hint->status = REF_STATUS_OK;
 198                else {
 199                        hint->status = REF_STATUS_REMOTE_REJECT;
 200                        ret = -1;
 201                }
 202                hint->remote_status = xstrdup_or_null(msg);
 203                /* start our next search from the next ref */
 204                hint = hint->next;
 205        }
 206        return ret;
 207}
 208
 209static int sideband_demux(int in, int out, void *data)
 210{
 211        int *fd = data, ret;
 212#ifdef NO_PTHREADS
 213        close(fd[1]);
 214#endif
 215        ret = recv_sideband("send-pack", fd[0], out);
 216        close(out);
 217        return ret;
 218}
 219
 220static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
 221{
 222        struct strbuf *sb = cb;
 223        if (graft->nr_parent == -1)
 224                packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
 225        return 0;
 226}
 227
 228static void advertise_shallow_grafts_buf(struct strbuf *sb)
 229{
 230        if (!is_repository_shallow())
 231                return;
 232        for_each_commit_graft(advertise_shallow_grafts_cb, sb);
 233}
 234
 235#define CHECK_REF_NO_PUSH -1
 236#define CHECK_REF_STATUS_REJECTED -2
 237#define CHECK_REF_UPTODATE -3
 238static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
 239{
 240        if (!ref->peer_ref && !args->send_mirror)
 241                return CHECK_REF_NO_PUSH;
 242
 243        /* Check for statuses set by set_ref_status_for_push() */
 244        switch (ref->status) {
 245        case REF_STATUS_REJECT_NONFASTFORWARD:
 246        case REF_STATUS_REJECT_ALREADY_EXISTS:
 247        case REF_STATUS_REJECT_FETCH_FIRST:
 248        case REF_STATUS_REJECT_NEEDS_FORCE:
 249        case REF_STATUS_REJECT_STALE:
 250        case REF_STATUS_REJECT_NODELETE:
 251                return CHECK_REF_STATUS_REJECTED;
 252        case REF_STATUS_UPTODATE:
 253                return CHECK_REF_UPTODATE;
 254        default:
 255                return 0;
 256        }
 257}
 258
 259/*
 260 * the beginning of the next line, or the end of buffer.
 261 *
 262 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
 263 * convert many similar uses found by "git grep -A4 memchr".
 264 */
 265static const char *next_line(const char *line, size_t len)
 266{
 267        const char *nl = memchr(line, '\n', len);
 268        if (!nl)
 269                return line + len; /* incomplete line */
 270        return nl + 1;
 271}
 272
 273static int generate_push_cert(struct strbuf *req_buf,
 274                              const struct ref *remote_refs,
 275                              struct send_pack_args *args,
 276                              const char *cap_string,
 277                              const char *push_cert_nonce)
 278{
 279        const struct ref *ref;
 280        struct string_list_item *item;
 281        char *signing_key = xstrdup(get_signing_key());
 282        const char *cp, *np;
 283        struct strbuf cert = STRBUF_INIT;
 284        int update_seen = 0;
 285
 286        strbuf_addstr(&cert, "certificate version 0.1\n");
 287        strbuf_addf(&cert, "pusher %s ", signing_key);
 288        datestamp(&cert);
 289        strbuf_addch(&cert, '\n');
 290        if (args->url && *args->url) {
 291                char *anon_url = transport_anonymize_url(args->url);
 292                strbuf_addf(&cert, "pushee %s\n", anon_url);
 293                free(anon_url);
 294        }
 295        if (push_cert_nonce[0])
 296                strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
 297        if (args->push_options)
 298                for_each_string_list_item(item, args->push_options)
 299                        strbuf_addf(&cert, "push-option %s\n", item->string);
 300        strbuf_addstr(&cert, "\n");
 301
 302        for (ref = remote_refs; ref; ref = ref->next) {
 303                if (check_to_send_update(ref, args) < 0)
 304                        continue;
 305                update_seen = 1;
 306                strbuf_addf(&cert, "%s %s %s\n",
 307                            oid_to_hex(&ref->old_oid),
 308                            oid_to_hex(&ref->new_oid),
 309                            ref->name);
 310        }
 311        if (!update_seen)
 312                goto free_return;
 313
 314        if (sign_buffer(&cert, &cert, signing_key))
 315                die(_("failed to sign the push certificate"));
 316
 317        packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
 318        for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
 319                np = next_line(cp, cert.buf + cert.len - cp);
 320                packet_buf_write(req_buf,
 321                                 "%.*s", (int)(np - cp), cp);
 322        }
 323        packet_buf_write(req_buf, "push-cert-end\n");
 324
 325free_return:
 326        free(signing_key);
 327        strbuf_release(&cert);
 328        return update_seen;
 329}
 330
 331
 332static int atomic_push_failure(struct send_pack_args *args,
 333                               struct ref *remote_refs,
 334                               struct ref *failing_ref)
 335{
 336        struct ref *ref;
 337        /* Mark other refs as failed */
 338        for (ref = remote_refs; ref; ref = ref->next) {
 339                if (!ref->peer_ref && !args->send_mirror)
 340                        continue;
 341
 342                switch (ref->status) {
 343                case REF_STATUS_EXPECTING_REPORT:
 344                        ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
 345                        continue;
 346                default:
 347                        break; /* do nothing */
 348                }
 349        }
 350        return error("atomic push failed for ref %s. status: %d\n",
 351                     failing_ref->name, failing_ref->status);
 352}
 353
 354#define NONCE_LEN_LIMIT 256
 355
 356static void reject_invalid_nonce(const char *nonce, int len)
 357{
 358        int i = 0;
 359
 360        if (NONCE_LEN_LIMIT <= len)
 361                die("the receiving end asked to sign an invalid nonce <%.*s>",
 362                    len, nonce);
 363
 364        for (i = 0; i < len; i++) {
 365                int ch = nonce[i] & 0xFF;
 366                if (isalnum(ch) ||
 367                    ch == '-' || ch == '.' ||
 368                    ch == '/' || ch == '+' ||
 369                    ch == '=' || ch == '_')
 370                        continue;
 371                die("the receiving end asked to sign an invalid nonce <%.*s>",
 372                    len, nonce);
 373        }
 374}
 375
 376int send_pack(struct send_pack_args *args,
 377              int fd[], struct child_process *conn,
 378              struct ref *remote_refs,
 379              struct sha1_array *extra_have)
 380{
 381        int in = fd[0];
 382        int out = fd[1];
 383        struct strbuf req_buf = STRBUF_INIT;
 384        struct strbuf cap_buf = STRBUF_INIT;
 385        struct ref *ref;
 386        int need_pack_data = 0;
 387        int allow_deleting_refs = 0;
 388        int status_report = 0;
 389        int use_sideband = 0;
 390        int quiet_supported = 0;
 391        int agent_supported = 0;
 392        int use_atomic = 0;
 393        int atomic_supported = 0;
 394        int use_push_options = 0;
 395        int push_options_supported = 0;
 396        unsigned cmds_sent = 0;
 397        int ret;
 398        struct async demux;
 399        const char *push_cert_nonce = NULL;
 400
 401        /* Does the other end support the reporting? */
 402        if (server_supports("report-status"))
 403                status_report = 1;
 404        if (server_supports("delete-refs"))
 405                allow_deleting_refs = 1;
 406        if (server_supports("ofs-delta"))
 407                args->use_ofs_delta = 1;
 408        if (server_supports("side-band-64k"))
 409                use_sideband = 1;
 410        if (server_supports("quiet"))
 411                quiet_supported = 1;
 412        if (server_supports("agent"))
 413                agent_supported = 1;
 414        if (server_supports("no-thin"))
 415                args->use_thin_pack = 0;
 416        if (server_supports("atomic"))
 417                atomic_supported = 1;
 418        if (server_supports("push-options"))
 419                push_options_supported = 1;
 420
 421        if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
 422                int len;
 423                push_cert_nonce = server_feature_value("push-cert", &len);
 424                if (push_cert_nonce) {
 425                        reject_invalid_nonce(push_cert_nonce, len);
 426                        push_cert_nonce = xmemdupz(push_cert_nonce, len);
 427                } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
 428                        die(_("the receiving end does not support --signed push"));
 429                } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
 430                        warning(_("not sending a push certificate since the"
 431                                  " receiving end does not support --signed"
 432                                  " push"));
 433                }
 434        }
 435
 436        if (!remote_refs) {
 437                fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
 438                        "Perhaps you should specify a branch such as 'master'.\n");
 439                return 0;
 440        }
 441        if (args->atomic && !atomic_supported)
 442                die(_("the receiving end does not support --atomic push"));
 443
 444        use_atomic = atomic_supported && args->atomic;
 445
 446        if (args->push_options && !push_options_supported)
 447                die(_("the receiving end does not support push options"));
 448
 449        use_push_options = push_options_supported && args->push_options;
 450
 451        if (status_report)
 452                strbuf_addstr(&cap_buf, " report-status");
 453        if (use_sideband)
 454                strbuf_addstr(&cap_buf, " side-band-64k");
 455        if (quiet_supported && (args->quiet || !args->progress))
 456                strbuf_addstr(&cap_buf, " quiet");
 457        if (use_atomic)
 458                strbuf_addstr(&cap_buf, " atomic");
 459        if (use_push_options)
 460                strbuf_addstr(&cap_buf, " push-options");
 461        if (agent_supported)
 462                strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
 463
 464        /*
 465         * NEEDSWORK: why does delete-refs have to be so specific to
 466         * send-pack machinery that set_ref_status_for_push() cannot
 467         * set this bit for us???
 468         */
 469        for (ref = remote_refs; ref; ref = ref->next)
 470                if (ref->deletion && !allow_deleting_refs)
 471                        ref->status = REF_STATUS_REJECT_NODELETE;
 472
 473        if (!args->dry_run)
 474                advertise_shallow_grafts_buf(&req_buf);
 475
 476        if (!args->dry_run && push_cert_nonce)
 477                cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
 478                                               cap_buf.buf, push_cert_nonce);
 479
 480        /*
 481         * Clear the status for each ref and see if we need to send
 482         * the pack data.
 483         */
 484        for (ref = remote_refs; ref; ref = ref->next) {
 485                switch (check_to_send_update(ref, args)) {
 486                case 0: /* no error */
 487                        break;
 488                case CHECK_REF_STATUS_REJECTED:
 489                        /*
 490                         * When we know the server would reject a ref update if
 491                         * we were to send it and we're trying to send the refs
 492                         * atomically, abort the whole operation.
 493                         */
 494                        if (use_atomic)
 495                                return atomic_push_failure(args, remote_refs, ref);
 496                        /* Fallthrough for non atomic case. */
 497                default:
 498                        continue;
 499                }
 500                if (!ref->deletion)
 501                        need_pack_data = 1;
 502
 503                if (args->dry_run || !status_report)
 504                        ref->status = REF_STATUS_OK;
 505                else
 506                        ref->status = REF_STATUS_EXPECTING_REPORT;
 507        }
 508
 509        /*
 510         * Finally, tell the other end!
 511         */
 512        for (ref = remote_refs; ref; ref = ref->next) {
 513                char *old_hex, *new_hex;
 514
 515                if (args->dry_run || push_cert_nonce)
 516                        continue;
 517
 518                if (check_to_send_update(ref, args) < 0)
 519                        continue;
 520
 521                old_hex = oid_to_hex(&ref->old_oid);
 522                new_hex = oid_to_hex(&ref->new_oid);
 523                if (!cmds_sent) {
 524                        packet_buf_write(&req_buf,
 525                                         "%s %s %s%c%s",
 526                                         old_hex, new_hex, ref->name, 0,
 527                                         cap_buf.buf);
 528                        cmds_sent = 1;
 529                } else {
 530                        packet_buf_write(&req_buf, "%s %s %s",
 531                                         old_hex, new_hex, ref->name);
 532                }
 533        }
 534
 535        if (args->stateless_rpc) {
 536                if (!args->dry_run && (cmds_sent || is_repository_shallow())) {
 537                        packet_buf_flush(&req_buf);
 538                        send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
 539                }
 540        } else {
 541                write_or_die(out, req_buf.buf, req_buf.len);
 542                packet_flush(out);
 543        }
 544        strbuf_release(&req_buf);
 545        strbuf_release(&cap_buf);
 546
 547        if (use_push_options) {
 548                struct string_list_item *item;
 549                struct strbuf sb = STRBUF_INIT;
 550
 551                for_each_string_list_item(item, args->push_options)
 552                        packet_buf_write(&sb, "%s", item->string);
 553
 554                write_or_die(out, sb.buf, sb.len);
 555                packet_flush(out);
 556                strbuf_release(&sb);
 557        }
 558
 559        if (use_sideband && cmds_sent) {
 560                memset(&demux, 0, sizeof(demux));
 561                demux.proc = sideband_demux;
 562                demux.data = fd;
 563                demux.out = -1;
 564                demux.isolate_sigpipe = 1;
 565                if (start_async(&demux))
 566                        die("send-pack: unable to fork off sideband demultiplexer");
 567                in = demux.out;
 568        }
 569
 570        if (need_pack_data && cmds_sent) {
 571                if (pack_objects(out, remote_refs, extra_have, args) < 0) {
 572                        for (ref = remote_refs; ref; ref = ref->next)
 573                                ref->status = REF_STATUS_NONE;
 574                        if (args->stateless_rpc)
 575                                close(out);
 576                        if (git_connection_is_socket(conn))
 577                                shutdown(fd[0], SHUT_WR);
 578
 579                        /*
 580                         * Do not even bother with the return value; we know we
 581                         * are failing, and just want the error() side effects.
 582                         */
 583                        if (status_report)
 584                                receive_unpack_status(in);
 585
 586                        if (use_sideband) {
 587                                close(demux.out);
 588                                finish_async(&demux);
 589                        }
 590                        fd[1] = -1;
 591                        return -1;
 592                }
 593                if (!args->stateless_rpc)
 594                        /* Closed by pack_objects() via start_command() */
 595                        fd[1] = -1;
 596        }
 597        if (args->stateless_rpc && cmds_sent)
 598                packet_flush(out);
 599
 600        if (status_report && cmds_sent)
 601                ret = receive_status(in, remote_refs);
 602        else
 603                ret = 0;
 604        if (args->stateless_rpc)
 605                packet_flush(out);
 606
 607        if (use_sideband && cmds_sent) {
 608                close(demux.out);
 609                if (finish_async(&demux)) {
 610                        error("error in sideband demultiplexer");
 611                        ret = -1;
 612                }
 613        }
 614
 615        if (ret < 0)
 616                return ret;
 617
 618        if (args->porcelain)
 619                return 0;
 620
 621        for (ref = remote_refs; ref; ref = ref->next) {
 622                switch (ref->status) {
 623                case REF_STATUS_NONE:
 624                case REF_STATUS_UPTODATE:
 625                case REF_STATUS_OK:
 626                        break;
 627                default:
 628                        return -1;
 629                }
 630        }
 631        return 0;
 632}