fc8ec9c3fac5cfb8082c4c14af770f87610e66fb
   1#include "builtin.h"
   2#include "lockfile.h"
   3#include "pack.h"
   4#include "refs.h"
   5#include "pkt-line.h"
   6#include "sideband.h"
   7#include "run-command.h"
   8#include "exec_cmd.h"
   9#include "commit.h"
  10#include "object.h"
  11#include "remote.h"
  12#include "connect.h"
  13#include "transport.h"
  14#include "string-list.h"
  15#include "sha1-array.h"
  16#include "connected.h"
  17#include "argv-array.h"
  18#include "version.h"
  19#include "tag.h"
  20#include "gpg-interface.h"
  21#include "sigchain.h"
  22
  23static const char receive_pack_usage[] = "git receive-pack <git-dir>";
  24
  25enum deny_action {
  26        DENY_UNCONFIGURED,
  27        DENY_IGNORE,
  28        DENY_WARN,
  29        DENY_REFUSE,
  30        DENY_UPDATE_INSTEAD
  31};
  32
  33static int deny_deletes;
  34static int deny_non_fast_forwards;
  35static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
  36static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
  37static int receive_fsck_objects = -1;
  38static int transfer_fsck_objects = -1;
  39static int receive_unpack_limit = -1;
  40static int transfer_unpack_limit = -1;
  41static int unpack_limit = 100;
  42static int report_status;
  43static int use_sideband;
  44static int quiet;
  45static int prefer_ofs_delta = 1;
  46static int auto_update_server_info;
  47static int auto_gc = 1;
  48static int fix_thin = 1;
  49static int stateless_rpc;
  50static const char *service_dir;
  51static const char *head_name;
  52static void *head_name_to_free;
  53static int sent_capabilities;
  54static int shallow_update;
  55static const char *alt_shallow_file;
  56static struct strbuf push_cert = STRBUF_INIT;
  57static unsigned char push_cert_sha1[20];
  58static struct signature_check sigcheck;
  59static const char *push_cert_nonce;
  60static const char *cert_nonce_seed;
  61
  62static const char *NONCE_UNSOLICITED = "UNSOLICITED";
  63static const char *NONCE_BAD = "BAD";
  64static const char *NONCE_MISSING = "MISSING";
  65static const char *NONCE_OK = "OK";
  66static const char *NONCE_SLOP = "SLOP";
  67static const char *nonce_status;
  68static long nonce_stamp_slop;
  69static unsigned long nonce_stamp_slop_limit;
  70
  71static enum deny_action parse_deny_action(const char *var, const char *value)
  72{
  73        if (value) {
  74                if (!strcasecmp(value, "ignore"))
  75                        return DENY_IGNORE;
  76                if (!strcasecmp(value, "warn"))
  77                        return DENY_WARN;
  78                if (!strcasecmp(value, "refuse"))
  79                        return DENY_REFUSE;
  80                if (!strcasecmp(value, "updateinstead"))
  81                        return DENY_UPDATE_INSTEAD;
  82        }
  83        if (git_config_bool(var, value))
  84                return DENY_REFUSE;
  85        return DENY_IGNORE;
  86}
  87
  88static int receive_pack_config(const char *var, const char *value, void *cb)
  89{
  90        int status = parse_hide_refs_config(var, value, "receive");
  91
  92        if (status)
  93                return status;
  94
  95        if (strcmp(var, "receive.denydeletes") == 0) {
  96                deny_deletes = git_config_bool(var, value);
  97                return 0;
  98        }
  99
 100        if (strcmp(var, "receive.denynonfastforwards") == 0) {
 101                deny_non_fast_forwards = git_config_bool(var, value);
 102                return 0;
 103        }
 104
 105        if (strcmp(var, "receive.unpacklimit") == 0) {
 106                receive_unpack_limit = git_config_int(var, value);
 107                return 0;
 108        }
 109
 110        if (strcmp(var, "transfer.unpacklimit") == 0) {
 111                transfer_unpack_limit = git_config_int(var, value);
 112                return 0;
 113        }
 114
 115        if (strcmp(var, "receive.fsckobjects") == 0) {
 116                receive_fsck_objects = git_config_bool(var, value);
 117                return 0;
 118        }
 119
 120        if (strcmp(var, "transfer.fsckobjects") == 0) {
 121                transfer_fsck_objects = git_config_bool(var, value);
 122                return 0;
 123        }
 124
 125        if (!strcmp(var, "receive.denycurrentbranch")) {
 126                deny_current_branch = parse_deny_action(var, value);
 127                return 0;
 128        }
 129
 130        if (strcmp(var, "receive.denydeletecurrent") == 0) {
 131                deny_delete_current = parse_deny_action(var, value);
 132                return 0;
 133        }
 134
 135        if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
 136                prefer_ofs_delta = git_config_bool(var, value);
 137                return 0;
 138        }
 139
 140        if (strcmp(var, "receive.updateserverinfo") == 0) {
 141                auto_update_server_info = git_config_bool(var, value);
 142                return 0;
 143        }
 144
 145        if (strcmp(var, "receive.autogc") == 0) {
 146                auto_gc = git_config_bool(var, value);
 147                return 0;
 148        }
 149
 150        if (strcmp(var, "receive.shallowupdate") == 0) {
 151                shallow_update = git_config_bool(var, value);
 152                return 0;
 153        }
 154
 155        if (strcmp(var, "receive.certnonceseed") == 0)
 156                return git_config_string(&cert_nonce_seed, var, value);
 157
 158        if (strcmp(var, "receive.certnonceslop") == 0) {
 159                nonce_stamp_slop_limit = git_config_ulong(var, value);
 160                return 0;
 161        }
 162
 163        return git_default_config(var, value, cb);
 164}
 165
 166static void show_ref(const char *path, const unsigned char *sha1)
 167{
 168        if (ref_is_hidden(path))
 169                return;
 170
 171        if (sent_capabilities) {
 172                packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
 173        } else {
 174                struct strbuf cap = STRBUF_INIT;
 175
 176                strbuf_addstr(&cap,
 177                              "report-status delete-refs side-band-64k quiet");
 178                if (prefer_ofs_delta)
 179                        strbuf_addstr(&cap, " ofs-delta");
 180                if (push_cert_nonce)
 181                        strbuf_addf(&cap, " push-cert=%s", push_cert_nonce);
 182                strbuf_addf(&cap, " agent=%s", git_user_agent_sanitized());
 183                packet_write(1, "%s %s%c%s\n",
 184                             sha1_to_hex(sha1), path, 0, cap.buf);
 185                strbuf_release(&cap);
 186                sent_capabilities = 1;
 187        }
 188}
 189
 190static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
 191{
 192        path = strip_namespace(path);
 193        /*
 194         * Advertise refs outside our current namespace as ".have"
 195         * refs, so that the client can use them to minimize data
 196         * transfer but will otherwise ignore them. This happens to
 197         * cover ".have" that are thrown in by add_one_alternate_ref()
 198         * to mark histories that are complete in our alternates as
 199         * well.
 200         */
 201        if (!path)
 202                path = ".have";
 203        show_ref(path, sha1);
 204        return 0;
 205}
 206
 207static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
 208{
 209        show_ref(".have", sha1);
 210}
 211
 212static void collect_one_alternate_ref(const struct ref *ref, void *data)
 213{
 214        struct sha1_array *sa = data;
 215        sha1_array_append(sa, ref->old_sha1);
 216}
 217
 218static void write_head_info(void)
 219{
 220        struct sha1_array sa = SHA1_ARRAY_INIT;
 221        for_each_alternate_ref(collect_one_alternate_ref, &sa);
 222        sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
 223        sha1_array_clear(&sa);
 224        for_each_ref(show_ref_cb, NULL);
 225        if (!sent_capabilities)
 226                show_ref("capabilities^{}", null_sha1);
 227
 228        advertise_shallow_grafts(1);
 229
 230        /* EOF */
 231        packet_flush(1);
 232}
 233
 234struct command {
 235        struct command *next;
 236        const char *error_string;
 237        unsigned int skip_update:1,
 238                     did_not_exist:1;
 239        int index;
 240        unsigned char old_sha1[20];
 241        unsigned char new_sha1[20];
 242        char ref_name[FLEX_ARRAY]; /* more */
 243};
 244
 245static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
 246static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
 247
 248static void report_message(const char *prefix, const char *err, va_list params)
 249{
 250        int sz = strlen(prefix);
 251        char msg[4096];
 252
 253        strncpy(msg, prefix, sz);
 254        sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
 255        if (sz > (sizeof(msg) - 1))
 256                sz = sizeof(msg) - 1;
 257        msg[sz++] = '\n';
 258
 259        if (use_sideband)
 260                send_sideband(1, 2, msg, sz, use_sideband);
 261        else
 262                xwrite(2, msg, sz);
 263}
 264
 265static void rp_warning(const char *err, ...)
 266{
 267        va_list params;
 268        va_start(params, err);
 269        report_message("warning: ", err, params);
 270        va_end(params);
 271}
 272
 273static void rp_error(const char *err, ...)
 274{
 275        va_list params;
 276        va_start(params, err);
 277        report_message("error: ", err, params);
 278        va_end(params);
 279}
 280
 281static int copy_to_sideband(int in, int out, void *arg)
 282{
 283        char data[128];
 284        while (1) {
 285                ssize_t sz = xread(in, data, sizeof(data));
 286                if (sz <= 0)
 287                        break;
 288                send_sideband(1, 2, data, sz, use_sideband);
 289        }
 290        close(in);
 291        return 0;
 292}
 293
 294#define HMAC_BLOCK_SIZE 64
 295
 296static void hmac_sha1(unsigned char *out,
 297                      const char *key_in, size_t key_len,
 298                      const char *text, size_t text_len)
 299{
 300        unsigned char key[HMAC_BLOCK_SIZE];
 301        unsigned char k_ipad[HMAC_BLOCK_SIZE];
 302        unsigned char k_opad[HMAC_BLOCK_SIZE];
 303        int i;
 304        git_SHA_CTX ctx;
 305
 306        /* RFC 2104 2. (1) */
 307        memset(key, '\0', HMAC_BLOCK_SIZE);
 308        if (HMAC_BLOCK_SIZE < key_len) {
 309                git_SHA1_Init(&ctx);
 310                git_SHA1_Update(&ctx, key_in, key_len);
 311                git_SHA1_Final(key, &ctx);
 312        } else {
 313                memcpy(key, key_in, key_len);
 314        }
 315
 316        /* RFC 2104 2. (2) & (5) */
 317        for (i = 0; i < sizeof(key); i++) {
 318                k_ipad[i] = key[i] ^ 0x36;
 319                k_opad[i] = key[i] ^ 0x5c;
 320        }
 321
 322        /* RFC 2104 2. (3) & (4) */
 323        git_SHA1_Init(&ctx);
 324        git_SHA1_Update(&ctx, k_ipad, sizeof(k_ipad));
 325        git_SHA1_Update(&ctx, text, text_len);
 326        git_SHA1_Final(out, &ctx);
 327
 328        /* RFC 2104 2. (6) & (7) */
 329        git_SHA1_Init(&ctx);
 330        git_SHA1_Update(&ctx, k_opad, sizeof(k_opad));
 331        git_SHA1_Update(&ctx, out, 20);
 332        git_SHA1_Final(out, &ctx);
 333}
 334
 335static char *prepare_push_cert_nonce(const char *path, unsigned long stamp)
 336{
 337        struct strbuf buf = STRBUF_INIT;
 338        unsigned char sha1[20];
 339
 340        strbuf_addf(&buf, "%s:%lu", path, stamp);
 341        hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));;
 342        strbuf_release(&buf);
 343
 344        /* RFC 2104 5. HMAC-SHA1-80 */
 345        strbuf_addf(&buf, "%lu-%.*s", stamp, 20, sha1_to_hex(sha1));
 346        return strbuf_detach(&buf, NULL);
 347}
 348
 349/*
 350 * NEEDSWORK: reuse find_commit_header() from jk/commit-author-parsing
 351 * after dropping "_commit" from its name and possibly moving it out
 352 * of commit.c
 353 */
 354static char *find_header(const char *msg, size_t len, const char *key)
 355{
 356        int key_len = strlen(key);
 357        const char *line = msg;
 358
 359        while (line && line < msg + len) {
 360                const char *eol = strchrnul(line, '\n');
 361
 362                if ((msg + len <= eol) || line == eol)
 363                        return NULL;
 364                if (line + key_len < eol &&
 365                    !memcmp(line, key, key_len) && line[key_len] == ' ') {
 366                        int offset = key_len + 1;
 367                        return xmemdupz(line + offset, (eol - line) - offset);
 368                }
 369                line = *eol ? eol + 1 : NULL;
 370        }
 371        return NULL;
 372}
 373
 374static const char *check_nonce(const char *buf, size_t len)
 375{
 376        char *nonce = find_header(buf, len, "nonce");
 377        unsigned long stamp, ostamp;
 378        char *bohmac, *expect = NULL;
 379        const char *retval = NONCE_BAD;
 380
 381        if (!nonce) {
 382                retval = NONCE_MISSING;
 383                goto leave;
 384        } else if (!push_cert_nonce) {
 385                retval = NONCE_UNSOLICITED;
 386                goto leave;
 387        } else if (!strcmp(push_cert_nonce, nonce)) {
 388                retval = NONCE_OK;
 389                goto leave;
 390        }
 391
 392        if (!stateless_rpc) {
 393                /* returned nonce MUST match what we gave out earlier */
 394                retval = NONCE_BAD;
 395                goto leave;
 396        }
 397
 398        /*
 399         * In stateless mode, we may be receiving a nonce issued by
 400         * another instance of the server that serving the same
 401         * repository, and the timestamps may not match, but the
 402         * nonce-seed and dir should match, so we can recompute and
 403         * report the time slop.
 404         *
 405         * In addition, when a nonce issued by another instance has
 406         * timestamp within receive.certnonceslop seconds, we pretend
 407         * as if we issued that nonce when reporting to the hook.
 408         */
 409
 410        /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */
 411        if (*nonce <= '0' || '9' < *nonce) {
 412                retval = NONCE_BAD;
 413                goto leave;
 414        }
 415        stamp = strtoul(nonce, &bohmac, 10);
 416        if (bohmac == nonce || bohmac[0] != '-') {
 417                retval = NONCE_BAD;
 418                goto leave;
 419        }
 420
 421        expect = prepare_push_cert_nonce(service_dir, stamp);
 422        if (strcmp(expect, nonce)) {
 423                /* Not what we would have signed earlier */
 424                retval = NONCE_BAD;
 425                goto leave;
 426        }
 427
 428        /*
 429         * By how many seconds is this nonce stale?  Negative value
 430         * would mean it was issued by another server with its clock
 431         * skewed in the future.
 432         */
 433        ostamp = strtoul(push_cert_nonce, NULL, 10);
 434        nonce_stamp_slop = (long)ostamp - (long)stamp;
 435
 436        if (nonce_stamp_slop_limit &&
 437            abs(nonce_stamp_slop) <= nonce_stamp_slop_limit) {
 438                /*
 439                 * Pretend as if the received nonce (which passes the
 440                 * HMAC check, so it is not a forged by third-party)
 441                 * is what we issued.
 442                 */
 443                free((void *)push_cert_nonce);
 444                push_cert_nonce = xstrdup(nonce);
 445                retval = NONCE_OK;
 446        } else {
 447                retval = NONCE_SLOP;
 448        }
 449
 450leave:
 451        free(nonce);
 452        free(expect);
 453        return retval;
 454}
 455
 456static void prepare_push_cert_sha1(struct child_process *proc)
 457{
 458        static int already_done;
 459
 460        if (!push_cert.len)
 461                return;
 462
 463        if (!already_done) {
 464                struct strbuf gpg_output = STRBUF_INIT;
 465                struct strbuf gpg_status = STRBUF_INIT;
 466                int bogs /* beginning_of_gpg_sig */;
 467
 468                already_done = 1;
 469                if (write_sha1_file(push_cert.buf, push_cert.len, "blob", push_cert_sha1))
 470                        hashclr(push_cert_sha1);
 471
 472                memset(&sigcheck, '\0', sizeof(sigcheck));
 473                sigcheck.result = 'N';
 474
 475                bogs = parse_signature(push_cert.buf, push_cert.len);
 476                if (verify_signed_buffer(push_cert.buf, bogs,
 477                                         push_cert.buf + bogs, push_cert.len - bogs,
 478                                         &gpg_output, &gpg_status) < 0) {
 479                        ; /* error running gpg */
 480                } else {
 481                        sigcheck.payload = push_cert.buf;
 482                        sigcheck.gpg_output = gpg_output.buf;
 483                        sigcheck.gpg_status = gpg_status.buf;
 484                        parse_gpg_output(&sigcheck);
 485                }
 486
 487                strbuf_release(&gpg_output);
 488                strbuf_release(&gpg_status);
 489                nonce_status = check_nonce(push_cert.buf, bogs);
 490        }
 491        if (!is_null_sha1(push_cert_sha1)) {
 492                argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT=%s",
 493                                 sha1_to_hex(push_cert_sha1));
 494                argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_SIGNER=%s",
 495                                 sigcheck.signer ? sigcheck.signer : "");
 496                argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_KEY=%s",
 497                                 sigcheck.key ? sigcheck.key : "");
 498                argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_STATUS=%c",
 499                                 sigcheck.result);
 500                if (push_cert_nonce) {
 501                        argv_array_pushf(&proc->env_array,
 502                                         "GIT_PUSH_CERT_NONCE=%s",
 503                                         push_cert_nonce);
 504                        argv_array_pushf(&proc->env_array,
 505                                         "GIT_PUSH_CERT_NONCE_STATUS=%s",
 506                                         nonce_status);
 507                        if (nonce_status == NONCE_SLOP)
 508                                argv_array_pushf(&proc->env_array,
 509                                                 "GIT_PUSH_CERT_NONCE_SLOP=%ld",
 510                                                 nonce_stamp_slop);
 511                }
 512        }
 513}
 514
 515typedef int (*feed_fn)(void *, const char **, size_t *);
 516static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
 517{
 518        struct child_process proc = CHILD_PROCESS_INIT;
 519        struct async muxer;
 520        const char *argv[2];
 521        int code;
 522
 523        argv[0] = find_hook(hook_name);
 524        if (!argv[0])
 525                return 0;
 526
 527        argv[1] = NULL;
 528
 529        proc.argv = argv;
 530        proc.in = -1;
 531        proc.stdout_to_stderr = 1;
 532
 533        if (use_sideband) {
 534                memset(&muxer, 0, sizeof(muxer));
 535                muxer.proc = copy_to_sideband;
 536                muxer.in = -1;
 537                code = start_async(&muxer);
 538                if (code)
 539                        return code;
 540                proc.err = muxer.in;
 541        }
 542
 543        prepare_push_cert_sha1(&proc);
 544
 545        code = start_command(&proc);
 546        if (code) {
 547                if (use_sideband)
 548                        finish_async(&muxer);
 549                return code;
 550        }
 551
 552        sigchain_push(SIGPIPE, SIG_IGN);
 553
 554        while (1) {
 555                const char *buf;
 556                size_t n;
 557                if (feed(feed_state, &buf, &n))
 558                        break;
 559                if (write_in_full(proc.in, buf, n) != n)
 560                        break;
 561        }
 562        close(proc.in);
 563        if (use_sideband)
 564                finish_async(&muxer);
 565
 566        sigchain_pop(SIGPIPE);
 567
 568        return finish_command(&proc);
 569}
 570
 571struct receive_hook_feed_state {
 572        struct command *cmd;
 573        int skip_broken;
 574        struct strbuf buf;
 575};
 576
 577static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
 578{
 579        struct receive_hook_feed_state *state = state_;
 580        struct command *cmd = state->cmd;
 581
 582        while (cmd &&
 583               state->skip_broken && (cmd->error_string || cmd->did_not_exist))
 584                cmd = cmd->next;
 585        if (!cmd)
 586                return -1; /* EOF */
 587        strbuf_reset(&state->buf);
 588        strbuf_addf(&state->buf, "%s %s %s\n",
 589                    sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
 590                    cmd->ref_name);
 591        state->cmd = cmd->next;
 592        if (bufp) {
 593                *bufp = state->buf.buf;
 594                *sizep = state->buf.len;
 595        }
 596        return 0;
 597}
 598
 599static int run_receive_hook(struct command *commands, const char *hook_name,
 600                            int skip_broken)
 601{
 602        struct receive_hook_feed_state state;
 603        int status;
 604
 605        strbuf_init(&state.buf, 0);
 606        state.cmd = commands;
 607        state.skip_broken = skip_broken;
 608        if (feed_receive_hook(&state, NULL, NULL))
 609                return 0;
 610        state.cmd = commands;
 611        status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
 612        strbuf_release(&state.buf);
 613        return status;
 614}
 615
 616static int run_update_hook(struct command *cmd)
 617{
 618        const char *argv[5];
 619        struct child_process proc = CHILD_PROCESS_INIT;
 620        int code;
 621
 622        argv[0] = find_hook("update");
 623        if (!argv[0])
 624                return 0;
 625
 626        argv[1] = cmd->ref_name;
 627        argv[2] = sha1_to_hex(cmd->old_sha1);
 628        argv[3] = sha1_to_hex(cmd->new_sha1);
 629        argv[4] = NULL;
 630
 631        proc.no_stdin = 1;
 632        proc.stdout_to_stderr = 1;
 633        proc.err = use_sideband ? -1 : 0;
 634        proc.argv = argv;
 635
 636        code = start_command(&proc);
 637        if (code)
 638                return code;
 639        if (use_sideband)
 640                copy_to_sideband(proc.err, -1, NULL);
 641        return finish_command(&proc);
 642}
 643
 644static int is_ref_checked_out(const char *ref)
 645{
 646        if (is_bare_repository())
 647                return 0;
 648
 649        if (!head_name)
 650                return 0;
 651        return !strcmp(head_name, ref);
 652}
 653
 654static char *refuse_unconfigured_deny_msg[] = {
 655        "By default, updating the current branch in a non-bare repository",
 656        "is denied, because it will make the index and work tree inconsistent",
 657        "with what you pushed, and will require 'git reset --hard' to match",
 658        "the work tree to HEAD.",
 659        "",
 660        "You can set 'receive.denyCurrentBranch' configuration variable to",
 661        "'ignore' or 'warn' in the remote repository to allow pushing into",
 662        "its current branch; however, this is not recommended unless you",
 663        "arranged to update its work tree to match what you pushed in some",
 664        "other way.",
 665        "",
 666        "To squelch this message and still keep the default behaviour, set",
 667        "'receive.denyCurrentBranch' configuration variable to 'refuse'."
 668};
 669
 670static void refuse_unconfigured_deny(void)
 671{
 672        int i;
 673        for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
 674                rp_error("%s", refuse_unconfigured_deny_msg[i]);
 675}
 676
 677static char *refuse_unconfigured_deny_delete_current_msg[] = {
 678        "By default, deleting the current branch is denied, because the next",
 679        "'git clone' won't result in any file checked out, causing confusion.",
 680        "",
 681        "You can set 'receive.denyDeleteCurrent' configuration variable to",
 682        "'warn' or 'ignore' in the remote repository to allow deleting the",
 683        "current branch, with or without a warning message.",
 684        "",
 685        "To squelch this message, you can set it to 'refuse'."
 686};
 687
 688static void refuse_unconfigured_deny_delete_current(void)
 689{
 690        int i;
 691        for (i = 0;
 692             i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
 693             i++)
 694                rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
 695}
 696
 697static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
 698static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
 699{
 700        static struct lock_file shallow_lock;
 701        struct sha1_array extra = SHA1_ARRAY_INIT;
 702        const char *alt_file;
 703        uint32_t mask = 1 << (cmd->index % 32);
 704        int i;
 705
 706        trace_printf_key(&trace_shallow,
 707                         "shallow: update_shallow_ref %s\n", cmd->ref_name);
 708        for (i = 0; i < si->shallow->nr; i++)
 709                if (si->used_shallow[i] &&
 710                    (si->used_shallow[i][cmd->index / 32] & mask) &&
 711                    !delayed_reachability_test(si, i))
 712                        sha1_array_append(&extra, si->shallow->sha1[i]);
 713
 714        setup_alternate_shallow(&shallow_lock, &alt_file, &extra);
 715        if (check_shallow_connected(command_singleton_iterator,
 716                                    0, cmd, alt_file)) {
 717                rollback_lock_file(&shallow_lock);
 718                sha1_array_clear(&extra);
 719                return -1;
 720        }
 721
 722        commit_lock_file(&shallow_lock);
 723
 724        /*
 725         * Make sure setup_alternate_shallow() for the next ref does
 726         * not lose these new roots..
 727         */
 728        for (i = 0; i < extra.nr; i++)
 729                register_shallow(extra.sha1[i]);
 730
 731        si->shallow_ref[cmd->index] = 0;
 732        sha1_array_clear(&extra);
 733        return 0;
 734}
 735
 736static const char *push_to_deploy(unsigned char *sha1,
 737                                  struct argv_array *env,
 738                                  const char *work_tree)
 739{
 740        const char *update_refresh[] = {
 741                "update-index", "-q", "--ignore-submodules", "--refresh", NULL
 742        };
 743        const char *diff_files[] = {
 744                "diff-files", "--quiet", "--ignore-submodules", "--", NULL
 745        };
 746        const char *diff_index[] = {
 747                "diff-index", "--quiet", "--cached", "--ignore-submodules",
 748                "HEAD", "--", NULL
 749        };
 750        const char *read_tree[] = {
 751                "read-tree", "-u", "-m", NULL, NULL
 752        };
 753        struct child_process child = CHILD_PROCESS_INIT;
 754
 755        child.argv = update_refresh;
 756        child.env = env->argv;
 757        child.dir = work_tree;
 758        child.no_stdin = 1;
 759        child.stdout_to_stderr = 1;
 760        child.git_cmd = 1;
 761        if (run_command(&child))
 762                return "Up-to-date check failed";
 763
 764        /* run_command() does not clean up completely; reinitialize */
 765        child_process_init(&child);
 766        child.argv = diff_files;
 767        child.env = env->argv;
 768        child.dir = work_tree;
 769        child.no_stdin = 1;
 770        child.stdout_to_stderr = 1;
 771        child.git_cmd = 1;
 772        if (run_command(&child))
 773                return "Working directory has unstaged changes";
 774
 775        child_process_init(&child);
 776        child.argv = diff_index;
 777        child.env = env->argv;
 778        child.no_stdin = 1;
 779        child.no_stdout = 1;
 780        child.stdout_to_stderr = 0;
 781        child.git_cmd = 1;
 782        if (run_command(&child))
 783                return "Working directory has staged changes";
 784
 785        read_tree[3] = sha1_to_hex(sha1);
 786        child_process_init(&child);
 787        child.argv = read_tree;
 788        child.env = env->argv;
 789        child.dir = work_tree;
 790        child.no_stdin = 1;
 791        child.no_stdout = 1;
 792        child.stdout_to_stderr = 0;
 793        child.git_cmd = 1;
 794        if (run_command(&child))
 795                return "Could not update working tree to new HEAD";
 796
 797        return NULL;
 798}
 799
 800static const char *push_to_checkout_hook = "push-to-checkout";
 801
 802static const char *push_to_checkout(unsigned char *sha1,
 803                                    struct argv_array *env,
 804                                    const char *work_tree)
 805{
 806        argv_array_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
 807        if (run_hook_le(env->argv, push_to_checkout_hook,
 808                        sha1_to_hex(sha1), NULL))
 809                return "push-to-checkout hook declined";
 810        else
 811                return NULL;
 812}
 813
 814static const char *update_worktree(unsigned char *sha1)
 815{
 816        const char *retval;
 817        const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
 818        struct argv_array env = ARGV_ARRAY_INIT;
 819
 820        if (is_bare_repository())
 821                return "denyCurrentBranch = updateInstead needs a worktree";
 822
 823        argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
 824
 825        if (!find_hook(push_to_checkout_hook))
 826                retval = push_to_deploy(sha1, &env, work_tree);
 827        else
 828                retval = push_to_checkout(sha1, &env, work_tree);
 829
 830        argv_array_clear(&env);
 831        return retval;
 832}
 833
 834static const char *update(struct command *cmd, struct shallow_info *si)
 835{
 836        const char *name = cmd->ref_name;
 837        struct strbuf namespaced_name_buf = STRBUF_INIT;
 838        const char *namespaced_name, *ret;
 839        unsigned char *old_sha1 = cmd->old_sha1;
 840        unsigned char *new_sha1 = cmd->new_sha1;
 841
 842        /* only refs/... are allowed */
 843        if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
 844                rp_error("refusing to create funny ref '%s' remotely", name);
 845                return "funny refname";
 846        }
 847
 848        strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
 849        namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
 850
 851        if (is_ref_checked_out(namespaced_name)) {
 852                switch (deny_current_branch) {
 853                case DENY_IGNORE:
 854                        break;
 855                case DENY_WARN:
 856                        rp_warning("updating the current branch");
 857                        break;
 858                case DENY_REFUSE:
 859                case DENY_UNCONFIGURED:
 860                        rp_error("refusing to update checked out branch: %s", name);
 861                        if (deny_current_branch == DENY_UNCONFIGURED)
 862                                refuse_unconfigured_deny();
 863                        return "branch is currently checked out";
 864                case DENY_UPDATE_INSTEAD:
 865                        ret = update_worktree(new_sha1);
 866                        if (ret)
 867                                return ret;
 868                        break;
 869                }
 870        }
 871
 872        if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
 873                error("unpack should have generated %s, "
 874                      "but I can't find it!", sha1_to_hex(new_sha1));
 875                return "bad pack";
 876        }
 877
 878        if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
 879                if (deny_deletes && starts_with(name, "refs/heads/")) {
 880                        rp_error("denying ref deletion for %s", name);
 881                        return "deletion prohibited";
 882                }
 883
 884                if (!strcmp(namespaced_name, head_name)) {
 885                        switch (deny_delete_current) {
 886                        case DENY_IGNORE:
 887                                break;
 888                        case DENY_WARN:
 889                                rp_warning("deleting the current branch");
 890                                break;
 891                        case DENY_REFUSE:
 892                        case DENY_UNCONFIGURED:
 893                        case DENY_UPDATE_INSTEAD:
 894                                if (deny_delete_current == DENY_UNCONFIGURED)
 895                                        refuse_unconfigured_deny_delete_current();
 896                                rp_error("refusing to delete the current branch: %s", name);
 897                                return "deletion of the current branch prohibited";
 898                        default:
 899                                return "Invalid denyDeleteCurrent setting";
 900                        }
 901                }
 902        }
 903
 904        if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
 905            !is_null_sha1(old_sha1) &&
 906            starts_with(name, "refs/heads/")) {
 907                struct object *old_object, *new_object;
 908                struct commit *old_commit, *new_commit;
 909
 910                old_object = parse_object(old_sha1);
 911                new_object = parse_object(new_sha1);
 912
 913                if (!old_object || !new_object ||
 914                    old_object->type != OBJ_COMMIT ||
 915                    new_object->type != OBJ_COMMIT) {
 916                        error("bad sha1 objects for %s", name);
 917                        return "bad ref";
 918                }
 919                old_commit = (struct commit *)old_object;
 920                new_commit = (struct commit *)new_object;
 921                if (!in_merge_bases(old_commit, new_commit)) {
 922                        rp_error("denying non-fast-forward %s"
 923                                 " (you should pull first)", name);
 924                        return "non-fast-forward";
 925                }
 926        }
 927        if (run_update_hook(cmd)) {
 928                rp_error("hook declined to update %s", name);
 929                return "hook declined";
 930        }
 931
 932        if (is_null_sha1(new_sha1)) {
 933                if (!parse_object(old_sha1)) {
 934                        old_sha1 = NULL;
 935                        if (ref_exists(name)) {
 936                                rp_warning("Allowing deletion of corrupt ref.");
 937                        } else {
 938                                rp_warning("Deleting a non-existent ref.");
 939                                cmd->did_not_exist = 1;
 940                        }
 941                }
 942                if (delete_ref(namespaced_name, old_sha1, 0)) {
 943                        rp_error("failed to delete %s", name);
 944                        return "failed to delete";
 945                }
 946                return NULL; /* good */
 947        }
 948        else {
 949                struct strbuf err = STRBUF_INIT;
 950                struct ref_transaction *transaction;
 951
 952                if (shallow_update && si->shallow_ref[cmd->index] &&
 953                    update_shallow_ref(cmd, si))
 954                        return "shallow error";
 955
 956                transaction = ref_transaction_begin(&err);
 957                if (!transaction ||
 958                    ref_transaction_update(transaction, namespaced_name,
 959                                           new_sha1, old_sha1, 0, 1, "push",
 960                                           &err) ||
 961                    ref_transaction_commit(transaction, &err)) {
 962                        ref_transaction_free(transaction);
 963
 964                        rp_error("%s", err.buf);
 965                        strbuf_release(&err);
 966                        return "failed to update ref";
 967                }
 968
 969                ref_transaction_free(transaction);
 970                strbuf_release(&err);
 971                return NULL; /* good */
 972        }
 973}
 974
 975static void run_update_post_hook(struct command *commands)
 976{
 977        struct command *cmd;
 978        int argc;
 979        const char **argv;
 980        struct child_process proc = CHILD_PROCESS_INIT;
 981        char *hook;
 982
 983        hook = find_hook("post-update");
 984        for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
 985                if (cmd->error_string || cmd->did_not_exist)
 986                        continue;
 987                argc++;
 988        }
 989        if (!argc || !hook)
 990                return;
 991
 992        argv = xmalloc(sizeof(*argv) * (2 + argc));
 993        argv[0] = hook;
 994
 995        for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
 996                if (cmd->error_string || cmd->did_not_exist)
 997                        continue;
 998                argv[argc] = xstrdup(cmd->ref_name);
 999                argc++;
1000        }
1001        argv[argc] = NULL;
1002
1003        proc.no_stdin = 1;
1004        proc.stdout_to_stderr = 1;
1005        proc.err = use_sideband ? -1 : 0;
1006        proc.argv = argv;
1007
1008        if (!start_command(&proc)) {
1009                if (use_sideband)
1010                        copy_to_sideband(proc.err, -1, NULL);
1011                finish_command(&proc);
1012        }
1013}
1014
1015static void check_aliased_update(struct command *cmd, struct string_list *list)
1016{
1017        struct strbuf buf = STRBUF_INIT;
1018        const char *dst_name;
1019        struct string_list_item *item;
1020        struct command *dst_cmd;
1021        unsigned char sha1[20];
1022        char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
1023        int flag;
1024
1025        strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
1026        dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag);
1027        strbuf_release(&buf);
1028
1029        if (!(flag & REF_ISSYMREF))
1030                return;
1031
1032        dst_name = strip_namespace(dst_name);
1033        if (!dst_name) {
1034                rp_error("refusing update to broken symref '%s'", cmd->ref_name);
1035                cmd->skip_update = 1;
1036                cmd->error_string = "broken symref";
1037                return;
1038        }
1039
1040        if ((item = string_list_lookup(list, dst_name)) == NULL)
1041                return;
1042
1043        cmd->skip_update = 1;
1044
1045        dst_cmd = (struct command *) item->util;
1046
1047        if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
1048            !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
1049                return;
1050
1051        dst_cmd->skip_update = 1;
1052
1053        strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
1054        strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
1055        strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
1056        strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
1057        rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
1058                 " its target '%s' (%s..%s)",
1059                 cmd->ref_name, cmd_oldh, cmd_newh,
1060                 dst_cmd->ref_name, dst_oldh, dst_newh);
1061
1062        cmd->error_string = dst_cmd->error_string =
1063                "inconsistent aliased update";
1064}
1065
1066static void check_aliased_updates(struct command *commands)
1067{
1068        struct command *cmd;
1069        struct string_list ref_list = STRING_LIST_INIT_NODUP;
1070
1071        for (cmd = commands; cmd; cmd = cmd->next) {
1072                struct string_list_item *item =
1073                        string_list_append(&ref_list, cmd->ref_name);
1074                item->util = (void *)cmd;
1075        }
1076        sort_string_list(&ref_list);
1077
1078        for (cmd = commands; cmd; cmd = cmd->next) {
1079                if (!cmd->error_string)
1080                        check_aliased_update(cmd, &ref_list);
1081        }
1082
1083        string_list_clear(&ref_list, 0);
1084}
1085
1086static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
1087{
1088        struct command **cmd_list = cb_data;
1089        struct command *cmd = *cmd_list;
1090
1091        if (!cmd || is_null_sha1(cmd->new_sha1))
1092                return -1; /* end of list */
1093        *cmd_list = NULL; /* this returns only one */
1094        hashcpy(sha1, cmd->new_sha1);
1095        return 0;
1096}
1097
1098static void set_connectivity_errors(struct command *commands,
1099                                    struct shallow_info *si)
1100{
1101        struct command *cmd;
1102
1103        for (cmd = commands; cmd; cmd = cmd->next) {
1104                struct command *singleton = cmd;
1105                if (shallow_update && si->shallow_ref[cmd->index])
1106                        /* to be checked in update_shallow_ref() */
1107                        continue;
1108                if (!check_everything_connected(command_singleton_iterator,
1109                                                0, &singleton))
1110                        continue;
1111                cmd->error_string = "missing necessary objects";
1112        }
1113}
1114
1115struct iterate_data {
1116        struct command *cmds;
1117        struct shallow_info *si;
1118};
1119
1120static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
1121{
1122        struct iterate_data *data = cb_data;
1123        struct command **cmd_list = &data->cmds;
1124        struct command *cmd = *cmd_list;
1125
1126        for (; cmd; cmd = cmd->next) {
1127                if (shallow_update && data->si->shallow_ref[cmd->index])
1128                        /* to be checked in update_shallow_ref() */
1129                        continue;
1130                if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
1131                        hashcpy(sha1, cmd->new_sha1);
1132                        *cmd_list = cmd->next;
1133                        return 0;
1134                }
1135        }
1136        *cmd_list = NULL;
1137        return -1; /* end of list */
1138}
1139
1140static void reject_updates_to_hidden(struct command *commands)
1141{
1142        struct command *cmd;
1143
1144        for (cmd = commands; cmd; cmd = cmd->next) {
1145                if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
1146                        continue;
1147                if (is_null_sha1(cmd->new_sha1))
1148                        cmd->error_string = "deny deleting a hidden ref";
1149                else
1150                        cmd->error_string = "deny updating a hidden ref";
1151        }
1152}
1153
1154static void execute_commands(struct command *commands,
1155                             const char *unpacker_error,
1156                             struct shallow_info *si)
1157{
1158        int checked_connectivity;
1159        struct command *cmd;
1160        unsigned char sha1[20];
1161        struct iterate_data data;
1162
1163        if (unpacker_error) {
1164                for (cmd = commands; cmd; cmd = cmd->next)
1165                        cmd->error_string = "unpacker error";
1166                return;
1167        }
1168
1169        data.cmds = commands;
1170        data.si = si;
1171        if (check_everything_connected(iterate_receive_command_list, 0, &data))
1172                set_connectivity_errors(commands, si);
1173
1174        reject_updates_to_hidden(commands);
1175
1176        if (run_receive_hook(commands, "pre-receive", 0)) {
1177                for (cmd = commands; cmd; cmd = cmd->next) {
1178                        if (!cmd->error_string)
1179                                cmd->error_string = "pre-receive hook declined";
1180                }
1181                return;
1182        }
1183
1184        check_aliased_updates(commands);
1185
1186        free(head_name_to_free);
1187        head_name = head_name_to_free = resolve_refdup("HEAD", 0, sha1, NULL);
1188
1189        checked_connectivity = 1;
1190        for (cmd = commands; cmd; cmd = cmd->next) {
1191                if (cmd->error_string)
1192                        continue;
1193
1194                if (cmd->skip_update)
1195                        continue;
1196
1197                cmd->error_string = update(cmd, si);
1198                if (shallow_update && !cmd->error_string &&
1199                    si->shallow_ref[cmd->index]) {
1200                        error("BUG: connectivity check has not been run on ref %s",
1201                              cmd->ref_name);
1202                        checked_connectivity = 0;
1203                }
1204        }
1205
1206        if (shallow_update && !checked_connectivity)
1207                error("BUG: run 'git fsck' for safety.\n"
1208                      "If there are errors, try to remove "
1209                      "the reported refs above");
1210}
1211
1212static struct command **queue_command(struct command **tail,
1213                                      const char *line,
1214                                      int linelen)
1215{
1216        unsigned char old_sha1[20], new_sha1[20];
1217        struct command *cmd;
1218        const char *refname;
1219        int reflen;
1220
1221        if (linelen < 83 ||
1222            line[40] != ' ' ||
1223            line[81] != ' ' ||
1224            get_sha1_hex(line, old_sha1) ||
1225            get_sha1_hex(line + 41, new_sha1))
1226                die("protocol error: expected old/new/ref, got '%s'", line);
1227
1228        refname = line + 82;
1229        reflen = linelen - 82;
1230        cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
1231        hashcpy(cmd->old_sha1, old_sha1);
1232        hashcpy(cmd->new_sha1, new_sha1);
1233        memcpy(cmd->ref_name, refname, reflen);
1234        cmd->ref_name[reflen] = '\0';
1235        *tail = cmd;
1236        return &cmd->next;
1237}
1238
1239static void queue_commands_from_cert(struct command **tail,
1240                                     struct strbuf *push_cert)
1241{
1242        const char *boc, *eoc;
1243
1244        if (*tail)
1245                die("protocol error: got both push certificate and unsigned commands");
1246
1247        boc = strstr(push_cert->buf, "\n\n");
1248        if (!boc)
1249                die("malformed push certificate %.*s", 100, push_cert->buf);
1250        else
1251                boc += 2;
1252        eoc = push_cert->buf + parse_signature(push_cert->buf, push_cert->len);
1253
1254        while (boc < eoc) {
1255                const char *eol = memchr(boc, '\n', eoc - boc);
1256                tail = queue_command(tail, boc, eol ? eol - boc : eoc - eol);
1257                boc = eol ? eol + 1 : eoc;
1258        }
1259}
1260
1261static struct command *read_head_info(struct sha1_array *shallow)
1262{
1263        struct command *commands = NULL;
1264        struct command **p = &commands;
1265        for (;;) {
1266                char *line;
1267                int len, linelen;
1268
1269                line = packet_read_line(0, &len);
1270                if (!line)
1271                        break;
1272
1273                if (len == 48 && starts_with(line, "shallow ")) {
1274                        unsigned char sha1[20];
1275                        if (get_sha1_hex(line + 8, sha1))
1276                                die("protocol error: expected shallow sha, got '%s'",
1277                                    line + 8);
1278                        sha1_array_append(shallow, sha1);
1279                        continue;
1280                }
1281
1282                linelen = strlen(line);
1283                if (linelen < len) {
1284                        const char *feature_list = line + linelen + 1;
1285                        if (parse_feature_request(feature_list, "report-status"))
1286                                report_status = 1;
1287                        if (parse_feature_request(feature_list, "side-band-64k"))
1288                                use_sideband = LARGE_PACKET_MAX;
1289                        if (parse_feature_request(feature_list, "quiet"))
1290                                quiet = 1;
1291                }
1292
1293                if (!strcmp(line, "push-cert")) {
1294                        int true_flush = 0;
1295                        char certbuf[1024];
1296
1297                        for (;;) {
1298                                len = packet_read(0, NULL, NULL,
1299                                                  certbuf, sizeof(certbuf), 0);
1300                                if (!len) {
1301                                        true_flush = 1;
1302                                        break;
1303                                }
1304                                if (!strcmp(certbuf, "push-cert-end\n"))
1305                                        break; /* end of cert */
1306                                strbuf_addstr(&push_cert, certbuf);
1307                        }
1308
1309                        if (true_flush)
1310                                break;
1311                        continue;
1312                }
1313
1314                p = queue_command(p, line, linelen);
1315        }
1316
1317        if (push_cert.len)
1318                queue_commands_from_cert(p, &push_cert);
1319
1320        return commands;
1321}
1322
1323static const char *parse_pack_header(struct pack_header *hdr)
1324{
1325        switch (read_pack_header(0, hdr)) {
1326        case PH_ERROR_EOF:
1327                return "eof before pack header was fully read";
1328
1329        case PH_ERROR_PACK_SIGNATURE:
1330                return "protocol error (pack signature mismatch detected)";
1331
1332        case PH_ERROR_PROTOCOL:
1333                return "protocol error (pack version unsupported)";
1334
1335        default:
1336                return "unknown error in parse_pack_header";
1337
1338        case 0:
1339                return NULL;
1340        }
1341}
1342
1343static const char *pack_lockfile;
1344
1345static const char *unpack(int err_fd, struct shallow_info *si)
1346{
1347        struct pack_header hdr;
1348        const char *hdr_err;
1349        int status;
1350        char hdr_arg[38];
1351        struct child_process child = CHILD_PROCESS_INIT;
1352        int fsck_objects = (receive_fsck_objects >= 0
1353                            ? receive_fsck_objects
1354                            : transfer_fsck_objects >= 0
1355                            ? transfer_fsck_objects
1356                            : 0);
1357
1358        hdr_err = parse_pack_header(&hdr);
1359        if (hdr_err) {
1360                if (err_fd > 0)
1361                        close(err_fd);
1362                return hdr_err;
1363        }
1364        snprintf(hdr_arg, sizeof(hdr_arg),
1365                        "--pack_header=%"PRIu32",%"PRIu32,
1366                        ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
1367
1368        if (si->nr_ours || si->nr_theirs) {
1369                alt_shallow_file = setup_temporary_shallow(si->shallow);
1370                argv_array_push(&child.args, "--shallow-file");
1371                argv_array_push(&child.args, alt_shallow_file);
1372        }
1373
1374        if (ntohl(hdr.hdr_entries) < unpack_limit) {
1375                argv_array_pushl(&child.args, "unpack-objects", hdr_arg, NULL);
1376                if (quiet)
1377                        argv_array_push(&child.args, "-q");
1378                if (fsck_objects)
1379                        argv_array_push(&child.args, "--strict");
1380                child.no_stdout = 1;
1381                child.err = err_fd;
1382                child.git_cmd = 1;
1383                status = run_command(&child);
1384                if (status)
1385                        return "unpack-objects abnormal exit";
1386        } else {
1387                int s;
1388                char keep_arg[256];
1389
1390                s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
1391                if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
1392                        strcpy(keep_arg + s, "localhost");
1393
1394                argv_array_pushl(&child.args, "index-pack",
1395                                 "--stdin", hdr_arg, keep_arg, NULL);
1396                if (fsck_objects)
1397                        argv_array_push(&child.args, "--strict");
1398                if (fix_thin)
1399                        argv_array_push(&child.args, "--fix-thin");
1400                child.out = -1;
1401                child.err = err_fd;
1402                child.git_cmd = 1;
1403                status = start_command(&child);
1404                if (status)
1405                        return "index-pack fork failed";
1406                pack_lockfile = index_pack_lockfile(child.out);
1407                close(child.out);
1408                status = finish_command(&child);
1409                if (status)
1410                        return "index-pack abnormal exit";
1411                reprepare_packed_git();
1412        }
1413        return NULL;
1414}
1415
1416static const char *unpack_with_sideband(struct shallow_info *si)
1417{
1418        struct async muxer;
1419        const char *ret;
1420
1421        if (!use_sideband)
1422                return unpack(0, si);
1423
1424        memset(&muxer, 0, sizeof(muxer));
1425        muxer.proc = copy_to_sideband;
1426        muxer.in = -1;
1427        if (start_async(&muxer))
1428                return NULL;
1429
1430        ret = unpack(muxer.in, si);
1431
1432        finish_async(&muxer);
1433        return ret;
1434}
1435
1436static void prepare_shallow_update(struct command *commands,
1437                                   struct shallow_info *si)
1438{
1439        int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1440
1441        si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1442                                   si->shallow->nr);
1443        assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1444
1445        si->need_reachability_test =
1446                xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1447        si->reachable =
1448                xcalloc(si->shallow->nr, sizeof(*si->reachable));
1449        si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1450
1451        for (i = 0; i < si->nr_ours; i++)
1452                si->need_reachability_test[si->ours[i]] = 1;
1453
1454        for (i = 0; i < si->shallow->nr; i++) {
1455                if (!si->used_shallow[i])
1456                        continue;
1457                for (j = 0; j < bitmap_size; j++) {
1458                        if (!si->used_shallow[i][j])
1459                                continue;
1460                        si->need_reachability_test[i]++;
1461                        for (k = 0; k < 32; k++)
1462                                if (si->used_shallow[i][j] & (1 << k))
1463                                        si->shallow_ref[j * 32 + k]++;
1464                }
1465
1466                /*
1467                 * true for those associated with some refs and belong
1468                 * in "ours" list aka "step 7 not done yet"
1469                 */
1470                si->need_reachability_test[i] =
1471                        si->need_reachability_test[i] > 1;
1472        }
1473
1474        /*
1475         * keep hooks happy by forcing a temporary shallow file via
1476         * env variable because we can't add --shallow-file to every
1477         * command. check_everything_connected() will be done with
1478         * true .git/shallow though.
1479         */
1480        setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1481}
1482
1483static void update_shallow_info(struct command *commands,
1484                                struct shallow_info *si,
1485                                struct sha1_array *ref)
1486{
1487        struct command *cmd;
1488        int *ref_status;
1489        remove_nonexistent_theirs_shallow(si);
1490        if (!si->nr_ours && !si->nr_theirs) {
1491                shallow_update = 0;
1492                return;
1493        }
1494
1495        for (cmd = commands; cmd; cmd = cmd->next) {
1496                if (is_null_sha1(cmd->new_sha1))
1497                        continue;
1498                sha1_array_append(ref, cmd->new_sha1);
1499                cmd->index = ref->nr - 1;
1500        }
1501        si->ref = ref;
1502
1503        if (shallow_update) {
1504                prepare_shallow_update(commands, si);
1505                return;
1506        }
1507
1508        ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1509        assign_shallow_commits_to_refs(si, NULL, ref_status);
1510        for (cmd = commands; cmd; cmd = cmd->next) {
1511                if (is_null_sha1(cmd->new_sha1))
1512                        continue;
1513                if (ref_status[cmd->index]) {
1514                        cmd->error_string = "shallow update not allowed";
1515                        cmd->skip_update = 1;
1516                }
1517        }
1518        free(ref_status);
1519}
1520
1521static void report(struct command *commands, const char *unpack_status)
1522{
1523        struct command *cmd;
1524        struct strbuf buf = STRBUF_INIT;
1525
1526        packet_buf_write(&buf, "unpack %s\n",
1527                         unpack_status ? unpack_status : "ok");
1528        for (cmd = commands; cmd; cmd = cmd->next) {
1529                if (!cmd->error_string)
1530                        packet_buf_write(&buf, "ok %s\n",
1531                                         cmd->ref_name);
1532                else
1533                        packet_buf_write(&buf, "ng %s %s\n",
1534                                         cmd->ref_name, cmd->error_string);
1535        }
1536        packet_buf_flush(&buf);
1537
1538        if (use_sideband)
1539                send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1540        else
1541                write_or_die(1, buf.buf, buf.len);
1542        strbuf_release(&buf);
1543}
1544
1545static int delete_only(struct command *commands)
1546{
1547        struct command *cmd;
1548        for (cmd = commands; cmd; cmd = cmd->next) {
1549                if (!is_null_sha1(cmd->new_sha1))
1550                        return 0;
1551        }
1552        return 1;
1553}
1554
1555int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1556{
1557        int advertise_refs = 0;
1558        int i;
1559        struct command *commands;
1560        struct sha1_array shallow = SHA1_ARRAY_INIT;
1561        struct sha1_array ref = SHA1_ARRAY_INIT;
1562        struct shallow_info si;
1563
1564        packet_trace_identity("receive-pack");
1565
1566        argv++;
1567        for (i = 1; i < argc; i++) {
1568                const char *arg = *argv++;
1569
1570                if (*arg == '-') {
1571                        if (!strcmp(arg, "--quiet")) {
1572                                quiet = 1;
1573                                continue;
1574                        }
1575
1576                        if (!strcmp(arg, "--advertise-refs")) {
1577                                advertise_refs = 1;
1578                                continue;
1579                        }
1580                        if (!strcmp(arg, "--stateless-rpc")) {
1581                                stateless_rpc = 1;
1582                                continue;
1583                        }
1584                        if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1585                                fix_thin = 0;
1586                                continue;
1587                        }
1588
1589                        usage(receive_pack_usage);
1590                }
1591                if (service_dir)
1592                        usage(receive_pack_usage);
1593                service_dir = arg;
1594        }
1595        if (!service_dir)
1596                usage(receive_pack_usage);
1597
1598        setup_path();
1599
1600        if (!enter_repo(service_dir, 0))
1601                die("'%s' does not appear to be a git repository", service_dir);
1602
1603        git_config(receive_pack_config, NULL);
1604        if (cert_nonce_seed)
1605                push_cert_nonce = prepare_push_cert_nonce(service_dir, time(NULL));
1606
1607        if (0 <= transfer_unpack_limit)
1608                unpack_limit = transfer_unpack_limit;
1609        else if (0 <= receive_unpack_limit)
1610                unpack_limit = receive_unpack_limit;
1611
1612        if (advertise_refs || !stateless_rpc) {
1613                write_head_info();
1614        }
1615        if (advertise_refs)
1616                return 0;
1617
1618        if ((commands = read_head_info(&shallow)) != NULL) {
1619                const char *unpack_status = NULL;
1620
1621                prepare_shallow_info(&si, &shallow);
1622                if (!si.nr_ours && !si.nr_theirs)
1623                        shallow_update = 0;
1624                if (!delete_only(commands)) {
1625                        unpack_status = unpack_with_sideband(&si);
1626                        update_shallow_info(commands, &si, &ref);
1627                }
1628                execute_commands(commands, unpack_status, &si);
1629                if (pack_lockfile)
1630                        unlink_or_warn(pack_lockfile);
1631                if (report_status)
1632                        report(commands, unpack_status);
1633                run_receive_hook(commands, "post-receive", 1);
1634                run_update_post_hook(commands);
1635                if (auto_gc) {
1636                        const char *argv_gc_auto[] = {
1637                                "gc", "--auto", "--quiet", NULL,
1638                        };
1639                        int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1640                        run_command_v_opt(argv_gc_auto, opt);
1641                }
1642                if (auto_update_server_info)
1643                        update_server_info(0);
1644                clear_shallow_info(&si);
1645        }
1646        if (use_sideband)
1647                packet_flush(1);
1648        sha1_array_clear(&shallow);
1649        sha1_array_clear(&ref);
1650        free((void *)push_cert_nonce);
1651        return 0;
1652}