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