builtin / receive-pack.con commit transfer.fsckobjects: unify fetch/receive.fsckobjects (dab76d3)
   1#include "cache.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 "transport.h"
  12
  13static const char receive_pack_usage[] = "git receive-pack <git-dir>";
  14
  15enum deny_action {
  16        DENY_UNCONFIGURED,
  17        DENY_IGNORE,
  18        DENY_WARN,
  19        DENY_REFUSE,
  20};
  21
  22static int deny_deletes;
  23static int deny_non_fast_forwards;
  24static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
  25static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
  26static int receive_fsck_objects = -1;
  27static int transfer_fsck_objects = -1;
  28static int receive_unpack_limit = -1;
  29static int transfer_unpack_limit = -1;
  30static int unpack_limit = 100;
  31static int report_status;
  32static int use_sideband;
  33static int prefer_ofs_delta = 1;
  34static int auto_update_server_info;
  35static int auto_gc = 1;
  36static const char *head_name;
  37static int sent_capabilities;
  38
  39static enum deny_action parse_deny_action(const char *var, const char *value)
  40{
  41        if (value) {
  42                if (!strcasecmp(value, "ignore"))
  43                        return DENY_IGNORE;
  44                if (!strcasecmp(value, "warn"))
  45                        return DENY_WARN;
  46                if (!strcasecmp(value, "refuse"))
  47                        return DENY_REFUSE;
  48        }
  49        if (git_config_bool(var, value))
  50                return DENY_REFUSE;
  51        return DENY_IGNORE;
  52}
  53
  54static int receive_pack_config(const char *var, const char *value, void *cb)
  55{
  56        if (strcmp(var, "receive.denydeletes") == 0) {
  57                deny_deletes = git_config_bool(var, value);
  58                return 0;
  59        }
  60
  61        if (strcmp(var, "receive.denynonfastforwards") == 0) {
  62                deny_non_fast_forwards = git_config_bool(var, value);
  63                return 0;
  64        }
  65
  66        if (strcmp(var, "receive.unpacklimit") == 0) {
  67                receive_unpack_limit = git_config_int(var, value);
  68                return 0;
  69        }
  70
  71        if (strcmp(var, "transfer.unpacklimit") == 0) {
  72                transfer_unpack_limit = git_config_int(var, value);
  73                return 0;
  74        }
  75
  76        if (strcmp(var, "receive.fsckobjects") == 0) {
  77                receive_fsck_objects = git_config_bool(var, value);
  78                return 0;
  79        }
  80
  81        if (strcmp(var, "transfer.fsckobjects") == 0) {
  82                transfer_fsck_objects = git_config_bool(var, value);
  83                return 0;
  84        }
  85
  86        if (!strcmp(var, "receive.denycurrentbranch")) {
  87                deny_current_branch = parse_deny_action(var, value);
  88                return 0;
  89        }
  90
  91        if (strcmp(var, "receive.denydeletecurrent") == 0) {
  92                deny_delete_current = parse_deny_action(var, value);
  93                return 0;
  94        }
  95
  96        if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
  97                prefer_ofs_delta = git_config_bool(var, value);
  98                return 0;
  99        }
 100
 101        if (strcmp(var, "receive.updateserverinfo") == 0) {
 102                auto_update_server_info = git_config_bool(var, value);
 103                return 0;
 104        }
 105
 106        if (strcmp(var, "receive.autogc") == 0) {
 107                auto_gc = git_config_bool(var, value);
 108                return 0;
 109        }
 110
 111        return git_default_config(var, value, cb);
 112}
 113
 114static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 115{
 116        if (sent_capabilities)
 117                packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
 118        else
 119                packet_write(1, "%s %s%c%s%s\n",
 120                             sha1_to_hex(sha1), path, 0,
 121                             " report-status delete-refs side-band-64k",
 122                             prefer_ofs_delta ? " ofs-delta" : "");
 123        sent_capabilities = 1;
 124        return 0;
 125}
 126
 127static void write_head_info(void)
 128{
 129        for_each_ref(show_ref, NULL);
 130        if (!sent_capabilities)
 131                show_ref("capabilities^{}", null_sha1, 0, NULL);
 132
 133}
 134
 135struct command {
 136        struct command *next;
 137        const char *error_string;
 138        unsigned char old_sha1[20];
 139        unsigned char new_sha1[20];
 140        char ref_name[FLEX_ARRAY]; /* more */
 141};
 142
 143static struct command *commands;
 144
 145static const char pre_receive_hook[] = "hooks/pre-receive";
 146static const char post_receive_hook[] = "hooks/post-receive";
 147
 148static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
 149static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
 150
 151static void report_message(const char *prefix, const char *err, va_list params)
 152{
 153        int sz = strlen(prefix);
 154        char msg[4096];
 155
 156        strncpy(msg, prefix, sz);
 157        sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
 158        if (sz > (sizeof(msg) - 1))
 159                sz = sizeof(msg) - 1;
 160        msg[sz++] = '\n';
 161
 162        if (use_sideband)
 163                send_sideband(1, 2, msg, sz, use_sideband);
 164        else
 165                xwrite(2, msg, sz);
 166}
 167
 168static void rp_warning(const char *err, ...)
 169{
 170        va_list params;
 171        va_start(params, err);
 172        report_message("warning: ", err, params);
 173        va_end(params);
 174}
 175
 176static void rp_error(const char *err, ...)
 177{
 178        va_list params;
 179        va_start(params, err);
 180        report_message("error: ", err, params);
 181        va_end(params);
 182}
 183
 184static int copy_to_sideband(int in, int out, void *arg)
 185{
 186        char data[128];
 187        while (1) {
 188                ssize_t sz = xread(in, data, sizeof(data));
 189                if (sz <= 0)
 190                        break;
 191                send_sideband(1, 2, data, sz, use_sideband);
 192        }
 193        close(in);
 194        return 0;
 195}
 196
 197static int run_receive_hook(const char *hook_name)
 198{
 199        static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
 200        struct command *cmd;
 201        struct child_process proc;
 202        struct async muxer;
 203        const char *argv[2];
 204        int have_input = 0, code;
 205
 206        for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
 207                if (!cmd->error_string)
 208                        have_input = 1;
 209        }
 210
 211        if (!have_input || access(hook_name, X_OK) < 0)
 212                return 0;
 213
 214        argv[0] = hook_name;
 215        argv[1] = NULL;
 216
 217        memset(&proc, 0, sizeof(proc));
 218        proc.argv = argv;
 219        proc.in = -1;
 220        proc.stdout_to_stderr = 1;
 221
 222        if (use_sideband) {
 223                memset(&muxer, 0, sizeof(muxer));
 224                muxer.proc = copy_to_sideband;
 225                muxer.in = -1;
 226                code = start_async(&muxer);
 227                if (code)
 228                        return code;
 229                proc.err = muxer.in;
 230        }
 231
 232        code = start_command(&proc);
 233        if (code) {
 234                if (use_sideband)
 235                        finish_async(&muxer);
 236                return code;
 237        }
 238
 239        for (cmd = commands; cmd; cmd = cmd->next) {
 240                if (!cmd->error_string) {
 241                        size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
 242                                sha1_to_hex(cmd->old_sha1),
 243                                sha1_to_hex(cmd->new_sha1),
 244                                cmd->ref_name);
 245                        if (write_in_full(proc.in, buf, n) != n)
 246                                break;
 247                }
 248        }
 249        close(proc.in);
 250        if (use_sideband)
 251                finish_async(&muxer);
 252        return finish_command(&proc);
 253}
 254
 255static int run_update_hook(struct command *cmd)
 256{
 257        static const char update_hook[] = "hooks/update";
 258        const char *argv[5];
 259        struct child_process proc;
 260        int code;
 261
 262        if (access(update_hook, X_OK) < 0)
 263                return 0;
 264
 265        argv[0] = update_hook;
 266        argv[1] = cmd->ref_name;
 267        argv[2] = sha1_to_hex(cmd->old_sha1);
 268        argv[3] = sha1_to_hex(cmd->new_sha1);
 269        argv[4] = NULL;
 270
 271        memset(&proc, 0, sizeof(proc));
 272        proc.no_stdin = 1;
 273        proc.stdout_to_stderr = 1;
 274        proc.err = use_sideband ? -1 : 0;
 275        proc.argv = argv;
 276
 277        code = start_command(&proc);
 278        if (code)
 279                return code;
 280        if (use_sideband)
 281                copy_to_sideband(proc.err, -1, NULL);
 282        return finish_command(&proc);
 283}
 284
 285static int is_ref_checked_out(const char *ref)
 286{
 287        if (is_bare_repository())
 288                return 0;
 289
 290        if (!head_name)
 291                return 0;
 292        return !strcmp(head_name, ref);
 293}
 294
 295static char *refuse_unconfigured_deny_msg[] = {
 296        "By default, updating the current branch in a non-bare repository",
 297        "is denied, because it will make the index and work tree inconsistent",
 298        "with what you pushed, and will require 'git reset --hard' to match",
 299        "the work tree to HEAD.",
 300        "",
 301        "You can set 'receive.denyCurrentBranch' configuration variable to",
 302        "'ignore' or 'warn' in the remote repository to allow pushing into",
 303        "its current branch; however, this is not recommended unless you",
 304        "arranged to update its work tree to match what you pushed in some",
 305        "other way.",
 306        "",
 307        "To squelch this message and still keep the default behaviour, set",
 308        "'receive.denyCurrentBranch' configuration variable to 'refuse'."
 309};
 310
 311static void refuse_unconfigured_deny(void)
 312{
 313        int i;
 314        for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
 315                rp_error("%s", refuse_unconfigured_deny_msg[i]);
 316}
 317
 318static char *refuse_unconfigured_deny_delete_current_msg[] = {
 319        "By default, deleting the current branch is denied, because the next",
 320        "'git clone' won't result in any file checked out, causing confusion.",
 321        "",
 322        "You can set 'receive.denyDeleteCurrent' configuration variable to",
 323        "'warn' or 'ignore' in the remote repository to allow deleting the",
 324        "current branch, with or without a warning message.",
 325        "",
 326        "To squelch this message, you can set it to 'refuse'."
 327};
 328
 329static void refuse_unconfigured_deny_delete_current(void)
 330{
 331        int i;
 332        for (i = 0;
 333             i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
 334             i++)
 335                rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
 336}
 337
 338static const char *update(struct command *cmd)
 339{
 340        const char *name = cmd->ref_name;
 341        unsigned char *old_sha1 = cmd->old_sha1;
 342        unsigned char *new_sha1 = cmd->new_sha1;
 343        struct ref_lock *lock;
 344
 345        /* only refs/... are allowed */
 346        if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
 347                rp_error("refusing to create funny ref '%s' remotely", name);
 348                return "funny refname";
 349        }
 350
 351        if (is_ref_checked_out(name)) {
 352                switch (deny_current_branch) {
 353                case DENY_IGNORE:
 354                        break;
 355                case DENY_WARN:
 356                        rp_warning("updating the current branch");
 357                        break;
 358                case DENY_REFUSE:
 359                case DENY_UNCONFIGURED:
 360                        rp_error("refusing to update checked out branch: %s", name);
 361                        if (deny_current_branch == DENY_UNCONFIGURED)
 362                                refuse_unconfigured_deny();
 363                        return "branch is currently checked out";
 364                }
 365        }
 366
 367        if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
 368                error("unpack should have generated %s, "
 369                      "but I can't find it!", sha1_to_hex(new_sha1));
 370                return "bad pack";
 371        }
 372
 373        if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
 374                if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
 375                        rp_error("denying ref deletion for %s", name);
 376                        return "deletion prohibited";
 377                }
 378
 379                if (!strcmp(name, head_name)) {
 380                        switch (deny_delete_current) {
 381                        case DENY_IGNORE:
 382                                break;
 383                        case DENY_WARN:
 384                                rp_warning("deleting the current branch");
 385                                break;
 386                        case DENY_REFUSE:
 387                        case DENY_UNCONFIGURED:
 388                                if (deny_delete_current == DENY_UNCONFIGURED)
 389                                        refuse_unconfigured_deny_delete_current();
 390                                rp_error("refusing to delete the current branch: %s", name);
 391                                return "deletion of the current branch prohibited";
 392                        }
 393                }
 394        }
 395
 396        if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
 397            !is_null_sha1(old_sha1) &&
 398            !prefixcmp(name, "refs/heads/")) {
 399                struct object *old_object, *new_object;
 400                struct commit *old_commit, *new_commit;
 401                struct commit_list *bases, *ent;
 402
 403                old_object = parse_object(old_sha1);
 404                new_object = parse_object(new_sha1);
 405
 406                if (!old_object || !new_object ||
 407                    old_object->type != OBJ_COMMIT ||
 408                    new_object->type != OBJ_COMMIT) {
 409                        error("bad sha1 objects for %s", name);
 410                        return "bad ref";
 411                }
 412                old_commit = (struct commit *)old_object;
 413                new_commit = (struct commit *)new_object;
 414                bases = get_merge_bases(old_commit, new_commit, 1);
 415                for (ent = bases; ent; ent = ent->next)
 416                        if (!hashcmp(old_sha1, ent->item->object.sha1))
 417                                break;
 418                free_commit_list(bases);
 419                if (!ent) {
 420                        rp_error("denying non-fast-forward %s"
 421                                 " (you should pull first)", name);
 422                        return "non-fast-forward";
 423                }
 424        }
 425        if (run_update_hook(cmd)) {
 426                rp_error("hook declined to update %s", name);
 427                return "hook declined";
 428        }
 429
 430        if (is_null_sha1(new_sha1)) {
 431                if (!parse_object(old_sha1)) {
 432                        rp_warning("Allowing deletion of corrupt ref.");
 433                        old_sha1 = NULL;
 434                }
 435                if (delete_ref(name, old_sha1, 0)) {
 436                        rp_error("failed to delete %s", name);
 437                        return "failed to delete";
 438                }
 439                return NULL; /* good */
 440        }
 441        else {
 442                lock = lock_any_ref_for_update(name, old_sha1, 0);
 443                if (!lock) {
 444                        rp_error("failed to lock %s", name);
 445                        return "failed to lock";
 446                }
 447                if (write_ref_sha1(lock, new_sha1, "push")) {
 448                        return "failed to write"; /* error() already called */
 449                }
 450                return NULL; /* good */
 451        }
 452}
 453
 454static char update_post_hook[] = "hooks/post-update";
 455
 456static void run_update_post_hook(struct command *cmd)
 457{
 458        struct command *cmd_p;
 459        int argc;
 460        const char **argv;
 461        struct child_process proc;
 462
 463        for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
 464                if (cmd_p->error_string)
 465                        continue;
 466                argc++;
 467        }
 468        if (!argc || access(update_post_hook, X_OK) < 0)
 469                return;
 470        argv = xmalloc(sizeof(*argv) * (2 + argc));
 471        argv[0] = update_post_hook;
 472
 473        for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
 474                char *p;
 475                if (cmd_p->error_string)
 476                        continue;
 477                p = xmalloc(strlen(cmd_p->ref_name) + 1);
 478                strcpy(p, cmd_p->ref_name);
 479                argv[argc] = p;
 480                argc++;
 481        }
 482        argv[argc] = NULL;
 483
 484        memset(&proc, 0, sizeof(proc));
 485        proc.no_stdin = 1;
 486        proc.stdout_to_stderr = 1;
 487        proc.err = use_sideband ? -1 : 0;
 488        proc.argv = argv;
 489
 490        if (!start_command(&proc)) {
 491                if (use_sideband)
 492                        copy_to_sideband(proc.err, -1, NULL);
 493                finish_command(&proc);
 494        }
 495}
 496
 497static void execute_commands(const char *unpacker_error)
 498{
 499        struct command *cmd = commands;
 500        unsigned char sha1[20];
 501
 502        if (unpacker_error) {
 503                while (cmd) {
 504                        cmd->error_string = "n/a (unpacker error)";
 505                        cmd = cmd->next;
 506                }
 507                return;
 508        }
 509
 510        if (run_receive_hook(pre_receive_hook)) {
 511                while (cmd) {
 512                        cmd->error_string = "pre-receive hook declined";
 513                        cmd = cmd->next;
 514                }
 515                return;
 516        }
 517
 518        head_name = resolve_ref("HEAD", sha1, 0, NULL);
 519
 520        while (cmd) {
 521                cmd->error_string = update(cmd);
 522                cmd = cmd->next;
 523        }
 524}
 525
 526static void read_head_info(void)
 527{
 528        struct command **p = &commands;
 529        for (;;) {
 530                static char line[1000];
 531                unsigned char old_sha1[20], new_sha1[20];
 532                struct command *cmd;
 533                char *refname;
 534                int len, reflen;
 535
 536                len = packet_read_line(0, line, sizeof(line));
 537                if (!len)
 538                        break;
 539                if (line[len-1] == '\n')
 540                        line[--len] = 0;
 541                if (len < 83 ||
 542                    line[40] != ' ' ||
 543                    line[81] != ' ' ||
 544                    get_sha1_hex(line, old_sha1) ||
 545                    get_sha1_hex(line + 41, new_sha1))
 546                        die("protocol error: expected old/new/ref, got '%s'",
 547                            line);
 548
 549                refname = line + 82;
 550                reflen = strlen(refname);
 551                if (reflen + 82 < len) {
 552                        if (strstr(refname + reflen + 1, "report-status"))
 553                                report_status = 1;
 554                        if (strstr(refname + reflen + 1, "side-band-64k"))
 555                                use_sideband = LARGE_PACKET_MAX;
 556                }
 557                cmd = xmalloc(sizeof(struct command) + len - 80);
 558                hashcpy(cmd->old_sha1, old_sha1);
 559                hashcpy(cmd->new_sha1, new_sha1);
 560                memcpy(cmd->ref_name, line + 82, len - 81);
 561                cmd->error_string = NULL;
 562                cmd->next = NULL;
 563                *p = cmd;
 564                p = &cmd->next;
 565        }
 566}
 567
 568static const char *parse_pack_header(struct pack_header *hdr)
 569{
 570        switch (read_pack_header(0, hdr)) {
 571        case PH_ERROR_EOF:
 572                return "eof before pack header was fully read";
 573
 574        case PH_ERROR_PACK_SIGNATURE:
 575                return "protocol error (pack signature mismatch detected)";
 576
 577        case PH_ERROR_PROTOCOL:
 578                return "protocol error (pack version unsupported)";
 579
 580        default:
 581                return "unknown error in parse_pack_header";
 582
 583        case 0:
 584                return NULL;
 585        }
 586}
 587
 588static const char *pack_lockfile;
 589
 590static const char *unpack(void)
 591{
 592        struct pack_header hdr;
 593        const char *hdr_err;
 594        char hdr_arg[38];
 595        int fsck_objects = (receive_fsck_objects >= 0
 596                            ? receive_fsck_objects
 597                            : transfer_fsck_objects >= 0
 598                            ? transfer_fsck_objects
 599                            : 0);
 600
 601        hdr_err = parse_pack_header(&hdr);
 602        if (hdr_err)
 603                return hdr_err;
 604        snprintf(hdr_arg, sizeof(hdr_arg),
 605                        "--pack_header=%"PRIu32",%"PRIu32,
 606                        ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
 607
 608        if (ntohl(hdr.hdr_entries) < unpack_limit) {
 609                int code, i = 0;
 610                const char *unpacker[4];
 611                unpacker[i++] = "unpack-objects";
 612                if (fsck_objects)
 613                        unpacker[i++] = "--strict";
 614                unpacker[i++] = hdr_arg;
 615                unpacker[i++] = NULL;
 616                code = run_command_v_opt(unpacker, RUN_GIT_CMD);
 617                if (!code)
 618                        return NULL;
 619                return "unpack-objects abnormal exit";
 620        } else {
 621                const char *keeper[7];
 622                int s, status, i = 0;
 623                char keep_arg[256];
 624                struct child_process ip;
 625
 626                s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
 627                if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
 628                        strcpy(keep_arg + s, "localhost");
 629
 630                keeper[i++] = "index-pack";
 631                keeper[i++] = "--stdin";
 632                if (fsck_objects)
 633                        keeper[i++] = "--strict";
 634                keeper[i++] = "--fix-thin";
 635                keeper[i++] = hdr_arg;
 636                keeper[i++] = keep_arg;
 637                keeper[i++] = NULL;
 638                memset(&ip, 0, sizeof(ip));
 639                ip.argv = keeper;
 640                ip.out = -1;
 641                ip.git_cmd = 1;
 642                status = start_command(&ip);
 643                if (status) {
 644                        return "index-pack fork failed";
 645                }
 646                pack_lockfile = index_pack_lockfile(ip.out);
 647                close(ip.out);
 648                status = finish_command(&ip);
 649                if (!status) {
 650                        reprepare_packed_git();
 651                        return NULL;
 652                }
 653                return "index-pack abnormal exit";
 654        }
 655}
 656
 657static void report(const char *unpack_status)
 658{
 659        struct command *cmd;
 660        struct strbuf buf = STRBUF_INIT;
 661
 662        packet_buf_write(&buf, "unpack %s\n",
 663                         unpack_status ? unpack_status : "ok");
 664        for (cmd = commands; cmd; cmd = cmd->next) {
 665                if (!cmd->error_string)
 666                        packet_buf_write(&buf, "ok %s\n",
 667                                         cmd->ref_name);
 668                else
 669                        packet_buf_write(&buf, "ng %s %s\n",
 670                                         cmd->ref_name, cmd->error_string);
 671        }
 672        packet_buf_flush(&buf);
 673
 674        if (use_sideband)
 675                send_sideband(1, 1, buf.buf, buf.len, use_sideband);
 676        else
 677                safe_write(1, buf.buf, buf.len);
 678        strbuf_release(&buf);
 679}
 680
 681static int delete_only(struct command *cmd)
 682{
 683        while (cmd) {
 684                if (!is_null_sha1(cmd->new_sha1))
 685                        return 0;
 686                cmd = cmd->next;
 687        }
 688        return 1;
 689}
 690
 691static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
 692{
 693        char *other;
 694        size_t len;
 695        struct remote *remote;
 696        struct transport *transport;
 697        const struct ref *extra;
 698
 699        e->name[-1] = '\0';
 700        other = xstrdup(make_absolute_path(e->base));
 701        e->name[-1] = '/';
 702        len = strlen(other);
 703
 704        while (other[len-1] == '/')
 705                other[--len] = '\0';
 706        if (len < 8 || memcmp(other + len - 8, "/objects", 8))
 707                return 0;
 708        /* Is this a git repository with refs? */
 709        memcpy(other + len - 8, "/refs", 6);
 710        if (!is_directory(other))
 711                return 0;
 712        other[len - 8] = '\0';
 713        remote = remote_get(other);
 714        transport = transport_get(remote, other);
 715        for (extra = transport_get_remote_refs(transport);
 716             extra;
 717             extra = extra->next) {
 718                add_extra_ref(".have", extra->old_sha1, 0);
 719        }
 720        transport_disconnect(transport);
 721        free(other);
 722        return 0;
 723}
 724
 725static void add_alternate_refs(void)
 726{
 727        foreach_alt_odb(add_refs_from_alternate, NULL);
 728}
 729
 730int cmd_receive_pack(int argc, const char **argv, const char *prefix)
 731{
 732        int advertise_refs = 0;
 733        int stateless_rpc = 0;
 734        int i;
 735        char *dir = NULL;
 736
 737        argv++;
 738        for (i = 1; i < argc; i++) {
 739                const char *arg = *argv++;
 740
 741                if (*arg == '-') {
 742                        if (!strcmp(arg, "--advertise-refs")) {
 743                                advertise_refs = 1;
 744                                continue;
 745                        }
 746                        if (!strcmp(arg, "--stateless-rpc")) {
 747                                stateless_rpc = 1;
 748                                continue;
 749                        }
 750
 751                        usage(receive_pack_usage);
 752                }
 753                if (dir)
 754                        usage(receive_pack_usage);
 755                dir = xstrdup(arg);
 756        }
 757        if (!dir)
 758                usage(receive_pack_usage);
 759
 760        setup_path();
 761
 762        if (!enter_repo(dir, 0))
 763                die("'%s' does not appear to be a git repository", dir);
 764
 765        if (is_repository_shallow())
 766                die("attempt to push into a shallow repository");
 767
 768        git_config(receive_pack_config, NULL);
 769
 770        if (0 <= transfer_unpack_limit)
 771                unpack_limit = transfer_unpack_limit;
 772        else if (0 <= receive_unpack_limit)
 773                unpack_limit = receive_unpack_limit;
 774
 775        if (advertise_refs || !stateless_rpc) {
 776                add_alternate_refs();
 777                write_head_info();
 778                clear_extra_refs();
 779
 780                /* EOF */
 781                packet_flush(1);
 782        }
 783        if (advertise_refs)
 784                return 0;
 785
 786        read_head_info();
 787        if (commands) {
 788                const char *unpack_status = NULL;
 789
 790                if (!delete_only(commands))
 791                        unpack_status = unpack();
 792                execute_commands(unpack_status);
 793                if (pack_lockfile)
 794                        unlink_or_warn(pack_lockfile);
 795                if (report_status)
 796                        report(unpack_status);
 797                run_receive_hook(post_receive_hook);
 798                run_update_post_hook(commands);
 799                if (auto_gc) {
 800                        const char *argv_gc_auto[] = {
 801                                "gc", "--auto", "--quiet", NULL,
 802                        };
 803                        run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
 804                }
 805                if (auto_update_server_info)
 806                        update_server_info(0);
 807        }
 808        if (use_sideband)
 809                packet_flush(1);
 810        return 0;
 811}