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