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