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