builtin / receive-pack.con commit walker.c: use ref transaction for ref updates (b6b10bb)
   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;
 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        memset(&proc, 0, sizeof(proc));
 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        while (1) {
 292                const char *buf;
 293                size_t n;
 294                if (feed(feed_state, &buf, &n))
 295                        break;
 296                if (write_in_full(proc.in, buf, n) != n)
 297                        break;
 298        }
 299        close(proc.in);
 300        if (use_sideband)
 301                finish_async(&muxer);
 302        return finish_command(&proc);
 303}
 304
 305struct receive_hook_feed_state {
 306        struct command *cmd;
 307        int skip_broken;
 308        struct strbuf buf;
 309};
 310
 311static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
 312{
 313        struct receive_hook_feed_state *state = state_;
 314        struct command *cmd = state->cmd;
 315
 316        while (cmd &&
 317               state->skip_broken && (cmd->error_string || cmd->did_not_exist))
 318                cmd = cmd->next;
 319        if (!cmd)
 320                return -1; /* EOF */
 321        strbuf_reset(&state->buf);
 322        strbuf_addf(&state->buf, "%s %s %s\n",
 323                    sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
 324                    cmd->ref_name);
 325        state->cmd = cmd->next;
 326        if (bufp) {
 327                *bufp = state->buf.buf;
 328                *sizep = state->buf.len;
 329        }
 330        return 0;
 331}
 332
 333static int run_receive_hook(struct command *commands, const char *hook_name,
 334                            int skip_broken)
 335{
 336        struct receive_hook_feed_state state;
 337        int status;
 338
 339        strbuf_init(&state.buf, 0);
 340        state.cmd = commands;
 341        state.skip_broken = skip_broken;
 342        if (feed_receive_hook(&state, NULL, NULL))
 343                return 0;
 344        state.cmd = commands;
 345        status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
 346        strbuf_release(&state.buf);
 347        return status;
 348}
 349
 350static int run_update_hook(struct command *cmd)
 351{
 352        const char *argv[5];
 353        struct child_process proc;
 354        int code;
 355
 356        argv[0] = find_hook("update");
 357        if (!argv[0])
 358                return 0;
 359
 360        argv[1] = cmd->ref_name;
 361        argv[2] = sha1_to_hex(cmd->old_sha1);
 362        argv[3] = sha1_to_hex(cmd->new_sha1);
 363        argv[4] = NULL;
 364
 365        memset(&proc, 0, sizeof(proc));
 366        proc.no_stdin = 1;
 367        proc.stdout_to_stderr = 1;
 368        proc.err = use_sideband ? -1 : 0;
 369        proc.argv = argv;
 370
 371        code = start_command(&proc);
 372        if (code)
 373                return code;
 374        if (use_sideband)
 375                copy_to_sideband(proc.err, -1, NULL);
 376        return finish_command(&proc);
 377}
 378
 379static int is_ref_checked_out(const char *ref)
 380{
 381        if (is_bare_repository())
 382                return 0;
 383
 384        if (!head_name)
 385                return 0;
 386        return !strcmp(head_name, ref);
 387}
 388
 389static char *refuse_unconfigured_deny_msg[] = {
 390        "By default, updating the current branch in a non-bare repository",
 391        "is denied, because it will make the index and work tree inconsistent",
 392        "with what you pushed, and will require 'git reset --hard' to match",
 393        "the work tree to HEAD.",
 394        "",
 395        "You can set 'receive.denyCurrentBranch' configuration variable to",
 396        "'ignore' or 'warn' in the remote repository to allow pushing into",
 397        "its current branch; however, this is not recommended unless you",
 398        "arranged to update its work tree to match what you pushed in some",
 399        "other way.",
 400        "",
 401        "To squelch this message and still keep the default behaviour, set",
 402        "'receive.denyCurrentBranch' configuration variable to 'refuse'."
 403};
 404
 405static void refuse_unconfigured_deny(void)
 406{
 407        int i;
 408        for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
 409                rp_error("%s", refuse_unconfigured_deny_msg[i]);
 410}
 411
 412static char *refuse_unconfigured_deny_delete_current_msg[] = {
 413        "By default, deleting the current branch is denied, because the next",
 414        "'git clone' won't result in any file checked out, causing confusion.",
 415        "",
 416        "You can set 'receive.denyDeleteCurrent' configuration variable to",
 417        "'warn' or 'ignore' in the remote repository to allow deleting the",
 418        "current branch, with or without a warning message.",
 419        "",
 420        "To squelch this message, you can set it to 'refuse'."
 421};
 422
 423static void refuse_unconfigured_deny_delete_current(void)
 424{
 425        int i;
 426        for (i = 0;
 427             i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
 428             i++)
 429                rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
 430}
 431
 432static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
 433static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
 434{
 435        static struct lock_file shallow_lock;
 436        struct sha1_array extra = SHA1_ARRAY_INIT;
 437        const char *alt_file;
 438        uint32_t mask = 1 << (cmd->index % 32);
 439        int i;
 440
 441        trace_printf_key("GIT_TRACE_SHALLOW",
 442                         "shallow: update_shallow_ref %s\n", cmd->ref_name);
 443        for (i = 0; i < si->shallow->nr; i++)
 444                if (si->used_shallow[i] &&
 445                    (si->used_shallow[i][cmd->index / 32] & mask) &&
 446                    !delayed_reachability_test(si, i))
 447                        sha1_array_append(&extra, si->shallow->sha1[i]);
 448
 449        setup_alternate_shallow(&shallow_lock, &alt_file, &extra);
 450        if (check_shallow_connected(command_singleton_iterator,
 451                                    0, cmd, alt_file)) {
 452                rollback_lock_file(&shallow_lock);
 453                sha1_array_clear(&extra);
 454                return -1;
 455        }
 456
 457        commit_lock_file(&shallow_lock);
 458
 459        /*
 460         * Make sure setup_alternate_shallow() for the next ref does
 461         * not lose these new roots..
 462         */
 463        for (i = 0; i < extra.nr; i++)
 464                register_shallow(extra.sha1[i]);
 465
 466        si->shallow_ref[cmd->index] = 0;
 467        sha1_array_clear(&extra);
 468        return 0;
 469}
 470
 471static const char *update(struct command *cmd, struct shallow_info *si)
 472{
 473        const char *name = cmd->ref_name;
 474        struct strbuf namespaced_name_buf = STRBUF_INIT;
 475        const char *namespaced_name;
 476        unsigned char *old_sha1 = cmd->old_sha1;
 477        unsigned char *new_sha1 = cmd->new_sha1;
 478
 479        /* only refs/... are allowed */
 480        if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
 481                rp_error("refusing to create funny ref '%s' remotely", name);
 482                return "funny refname";
 483        }
 484
 485        strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
 486        namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
 487
 488        if (is_ref_checked_out(namespaced_name)) {
 489                switch (deny_current_branch) {
 490                case DENY_IGNORE:
 491                        break;
 492                case DENY_WARN:
 493                        rp_warning("updating the current branch");
 494                        break;
 495                case DENY_REFUSE:
 496                case DENY_UNCONFIGURED:
 497                        rp_error("refusing to update checked out branch: %s", name);
 498                        if (deny_current_branch == DENY_UNCONFIGURED)
 499                                refuse_unconfigured_deny();
 500                        return "branch is currently checked out";
 501                }
 502        }
 503
 504        if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
 505                error("unpack should have generated %s, "
 506                      "but I can't find it!", sha1_to_hex(new_sha1));
 507                return "bad pack";
 508        }
 509
 510        if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
 511                if (deny_deletes && starts_with(name, "refs/heads/")) {
 512                        rp_error("denying ref deletion for %s", name);
 513                        return "deletion prohibited";
 514                }
 515
 516                if (!strcmp(namespaced_name, head_name)) {
 517                        switch (deny_delete_current) {
 518                        case DENY_IGNORE:
 519                                break;
 520                        case DENY_WARN:
 521                                rp_warning("deleting the current branch");
 522                                break;
 523                        case DENY_REFUSE:
 524                        case DENY_UNCONFIGURED:
 525                                if (deny_delete_current == DENY_UNCONFIGURED)
 526                                        refuse_unconfigured_deny_delete_current();
 527                                rp_error("refusing to delete the current branch: %s", name);
 528                                return "deletion of the current branch prohibited";
 529                        }
 530                }
 531        }
 532
 533        if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
 534            !is_null_sha1(old_sha1) &&
 535            starts_with(name, "refs/heads/")) {
 536                struct object *old_object, *new_object;
 537                struct commit *old_commit, *new_commit;
 538
 539                old_object = parse_object(old_sha1);
 540                new_object = parse_object(new_sha1);
 541
 542                if (!old_object || !new_object ||
 543                    old_object->type != OBJ_COMMIT ||
 544                    new_object->type != OBJ_COMMIT) {
 545                        error("bad sha1 objects for %s", name);
 546                        return "bad ref";
 547                }
 548                old_commit = (struct commit *)old_object;
 549                new_commit = (struct commit *)new_object;
 550                if (!in_merge_bases(old_commit, new_commit)) {
 551                        rp_error("denying non-fast-forward %s"
 552                                 " (you should pull first)", name);
 553                        return "non-fast-forward";
 554                }
 555        }
 556        if (run_update_hook(cmd)) {
 557                rp_error("hook declined to update %s", name);
 558                return "hook declined";
 559        }
 560
 561        if (is_null_sha1(new_sha1)) {
 562                if (!parse_object(old_sha1)) {
 563                        old_sha1 = NULL;
 564                        if (ref_exists(name)) {
 565                                rp_warning("Allowing deletion of corrupt ref.");
 566                        } else {
 567                                rp_warning("Deleting a non-existent ref.");
 568                                cmd->did_not_exist = 1;
 569                        }
 570                }
 571                if (delete_ref(namespaced_name, old_sha1, 0)) {
 572                        rp_error("failed to delete %s", name);
 573                        return "failed to delete";
 574                }
 575                return NULL; /* good */
 576        }
 577        else {
 578                struct strbuf err = STRBUF_INIT;
 579                struct ref_transaction *transaction;
 580
 581                if (shallow_update && si->shallow_ref[cmd->index] &&
 582                    update_shallow_ref(cmd, si))
 583                        return "shallow error";
 584
 585                transaction = ref_transaction_begin(&err);
 586                if (!transaction ||
 587                    ref_transaction_update(transaction, namespaced_name,
 588                                           new_sha1, old_sha1, 0, 1, &err) ||
 589                    ref_transaction_commit(transaction, "push", &err)) {
 590                        ref_transaction_free(transaction);
 591
 592                        rp_error("%s", err.buf);
 593                        strbuf_release(&err);
 594                        return "failed to update ref";
 595                }
 596
 597                ref_transaction_free(transaction);
 598                strbuf_release(&err);
 599                return NULL; /* good */
 600        }
 601}
 602
 603static void run_update_post_hook(struct command *commands)
 604{
 605        struct command *cmd;
 606        int argc;
 607        const char **argv;
 608        struct child_process proc;
 609        char *hook;
 610
 611        hook = find_hook("post-update");
 612        for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
 613                if (cmd->error_string || cmd->did_not_exist)
 614                        continue;
 615                argc++;
 616        }
 617        if (!argc || !hook)
 618                return;
 619
 620        argv = xmalloc(sizeof(*argv) * (2 + argc));
 621        argv[0] = hook;
 622
 623        for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
 624                char *p;
 625                if (cmd->error_string || cmd->did_not_exist)
 626                        continue;
 627                p = xmalloc(strlen(cmd->ref_name) + 1);
 628                strcpy(p, cmd->ref_name);
 629                argv[argc] = p;
 630                argc++;
 631        }
 632        argv[argc] = NULL;
 633
 634        memset(&proc, 0, sizeof(proc));
 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;
 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        memset(&child, 0, sizeof(child));
 947        if (ntohl(hdr.hdr_entries) < unpack_limit) {
 948                argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
 949                if (quiet)
 950                        argv_array_push(&av, "-q");
 951                if (fsck_objects)
 952                        argv_array_push(&av, "--strict");
 953                child.argv = av.argv;
 954                child.no_stdout = 1;
 955                child.err = err_fd;
 956                child.git_cmd = 1;
 957                status = run_command(&child);
 958                if (status)
 959                        return "unpack-objects abnormal exit";
 960        } else {
 961                int s;
 962                char keep_arg[256];
 963
 964                s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
 965                if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
 966                        strcpy(keep_arg + s, "localhost");
 967
 968                argv_array_pushl(&av, "index-pack",
 969                                 "--stdin", hdr_arg, keep_arg, NULL);
 970                if (fsck_objects)
 971                        argv_array_push(&av, "--strict");
 972                if (fix_thin)
 973                        argv_array_push(&av, "--fix-thin");
 974                child.argv = av.argv;
 975                child.out = -1;
 976                child.err = err_fd;
 977                child.git_cmd = 1;
 978                status = start_command(&child);
 979                if (status)
 980                        return "index-pack fork failed";
 981                pack_lockfile = index_pack_lockfile(child.out);
 982                close(child.out);
 983                status = finish_command(&child);
 984                if (status)
 985                        return "index-pack abnormal exit";
 986                reprepare_packed_git();
 987        }
 988        return NULL;
 989}
 990
 991static const char *unpack_with_sideband(struct shallow_info *si)
 992{
 993        struct async muxer;
 994        const char *ret;
 995
 996        if (!use_sideband)
 997                return unpack(0, si);
 998
 999        memset(&muxer, 0, sizeof(muxer));
1000        muxer.proc = copy_to_sideband;
1001        muxer.in = -1;
1002        if (start_async(&muxer))
1003                return NULL;
1004
1005        ret = unpack(muxer.in, si);
1006
1007        finish_async(&muxer);
1008        return ret;
1009}
1010
1011static void prepare_shallow_update(struct command *commands,
1012                                   struct shallow_info *si)
1013{
1014        int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1015
1016        si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1017                                   si->shallow->nr);
1018        assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1019
1020        si->need_reachability_test =
1021                xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1022        si->reachable =
1023                xcalloc(si->shallow->nr, sizeof(*si->reachable));
1024        si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1025
1026        for (i = 0; i < si->nr_ours; i++)
1027                si->need_reachability_test[si->ours[i]] = 1;
1028
1029        for (i = 0; i < si->shallow->nr; i++) {
1030                if (!si->used_shallow[i])
1031                        continue;
1032                for (j = 0; j < bitmap_size; j++) {
1033                        if (!si->used_shallow[i][j])
1034                                continue;
1035                        si->need_reachability_test[i]++;
1036                        for (k = 0; k < 32; k++)
1037                                if (si->used_shallow[i][j] & (1 << k))
1038                                        si->shallow_ref[j * 32 + k]++;
1039                }
1040
1041                /*
1042                 * true for those associated with some refs and belong
1043                 * in "ours" list aka "step 7 not done yet"
1044                 */
1045                si->need_reachability_test[i] =
1046                        si->need_reachability_test[i] > 1;
1047        }
1048
1049        /*
1050         * keep hooks happy by forcing a temporary shallow file via
1051         * env variable because we can't add --shallow-file to every
1052         * command. check_everything_connected() will be done with
1053         * true .git/shallow though.
1054         */
1055        setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1056}
1057
1058static void update_shallow_info(struct command *commands,
1059                                struct shallow_info *si,
1060                                struct sha1_array *ref)
1061{
1062        struct command *cmd;
1063        int *ref_status;
1064        remove_nonexistent_theirs_shallow(si);
1065        if (!si->nr_ours && !si->nr_theirs) {
1066                shallow_update = 0;
1067                return;
1068        }
1069
1070        for (cmd = commands; cmd; cmd = cmd->next) {
1071                if (is_null_sha1(cmd->new_sha1))
1072                        continue;
1073                sha1_array_append(ref, cmd->new_sha1);
1074                cmd->index = ref->nr - 1;
1075        }
1076        si->ref = ref;
1077
1078        if (shallow_update) {
1079                prepare_shallow_update(commands, si);
1080                return;
1081        }
1082
1083        ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1084        assign_shallow_commits_to_refs(si, NULL, ref_status);
1085        for (cmd = commands; cmd; cmd = cmd->next) {
1086                if (is_null_sha1(cmd->new_sha1))
1087                        continue;
1088                if (ref_status[cmd->index]) {
1089                        cmd->error_string = "shallow update not allowed";
1090                        cmd->skip_update = 1;
1091                }
1092        }
1093        free(ref_status);
1094}
1095
1096static void report(struct command *commands, const char *unpack_status)
1097{
1098        struct command *cmd;
1099        struct strbuf buf = STRBUF_INIT;
1100
1101        packet_buf_write(&buf, "unpack %s\n",
1102                         unpack_status ? unpack_status : "ok");
1103        for (cmd = commands; cmd; cmd = cmd->next) {
1104                if (!cmd->error_string)
1105                        packet_buf_write(&buf, "ok %s\n",
1106                                         cmd->ref_name);
1107                else
1108                        packet_buf_write(&buf, "ng %s %s\n",
1109                                         cmd->ref_name, cmd->error_string);
1110        }
1111        packet_buf_flush(&buf);
1112
1113        if (use_sideband)
1114                send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1115        else
1116                write_or_die(1, buf.buf, buf.len);
1117        strbuf_release(&buf);
1118}
1119
1120static int delete_only(struct command *commands)
1121{
1122        struct command *cmd;
1123        for (cmd = commands; cmd; cmd = cmd->next) {
1124                if (!is_null_sha1(cmd->new_sha1))
1125                        return 0;
1126        }
1127        return 1;
1128}
1129
1130int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1131{
1132        int advertise_refs = 0;
1133        int stateless_rpc = 0;
1134        int i;
1135        char *dir = NULL;
1136        struct command *commands;
1137        struct sha1_array shallow = SHA1_ARRAY_INIT;
1138        struct sha1_array ref = SHA1_ARRAY_INIT;
1139        struct shallow_info si;
1140
1141        packet_trace_identity("receive-pack");
1142
1143        argv++;
1144        for (i = 1; i < argc; i++) {
1145                const char *arg = *argv++;
1146
1147                if (*arg == '-') {
1148                        if (!strcmp(arg, "--quiet")) {
1149                                quiet = 1;
1150                                continue;
1151                        }
1152
1153                        if (!strcmp(arg, "--advertise-refs")) {
1154                                advertise_refs = 1;
1155                                continue;
1156                        }
1157                        if (!strcmp(arg, "--stateless-rpc")) {
1158                                stateless_rpc = 1;
1159                                continue;
1160                        }
1161                        if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1162                                fix_thin = 0;
1163                                continue;
1164                        }
1165
1166                        usage(receive_pack_usage);
1167                }
1168                if (dir)
1169                        usage(receive_pack_usage);
1170                dir = xstrdup(arg);
1171        }
1172        if (!dir)
1173                usage(receive_pack_usage);
1174
1175        setup_path();
1176
1177        if (!enter_repo(dir, 0))
1178                die("'%s' does not appear to be a git repository", dir);
1179
1180        git_config(receive_pack_config, NULL);
1181
1182        if (0 <= transfer_unpack_limit)
1183                unpack_limit = transfer_unpack_limit;
1184        else if (0 <= receive_unpack_limit)
1185                unpack_limit = receive_unpack_limit;
1186
1187        if (advertise_refs || !stateless_rpc) {
1188                write_head_info();
1189        }
1190        if (advertise_refs)
1191                return 0;
1192
1193        if ((commands = read_head_info(&shallow)) != NULL) {
1194                const char *unpack_status = NULL;
1195
1196                prepare_shallow_info(&si, &shallow);
1197                if (!si.nr_ours && !si.nr_theirs)
1198                        shallow_update = 0;
1199                if (!delete_only(commands)) {
1200                        unpack_status = unpack_with_sideband(&si);
1201                        update_shallow_info(commands, &si, &ref);
1202                }
1203                execute_commands(commands, unpack_status, &si);
1204                if (pack_lockfile)
1205                        unlink_or_warn(pack_lockfile);
1206                if (report_status)
1207                        report(commands, unpack_status);
1208                run_receive_hook(commands, "post-receive", 1);
1209                run_update_post_hook(commands);
1210                if (auto_gc) {
1211                        const char *argv_gc_auto[] = {
1212                                "gc", "--auto", "--quiet", NULL,
1213                        };
1214                        int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1215                        run_command_v_opt(argv_gc_auto, opt);
1216                }
1217                if (auto_update_server_info)
1218                        update_server_info(0);
1219                clear_shallow_info(&si);
1220        }
1221        if (use_sideband)
1222                packet_flush(1);
1223        sha1_array_clear(&shallow);
1224        sha1_array_clear(&ref);
1225        return 0;
1226}