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