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