upload-pack.con commit Merge branch 'bc/http-backend-allow-405' (26e53f8)
   1#include "cache.h"
   2#include "refs.h"
   3#include "pkt-line.h"
   4#include "sideband.h"
   5#include "tag.h"
   6#include "object.h"
   7#include "commit.h"
   8#include "exec_cmd.h"
   9#include "diff.h"
  10#include "revision.h"
  11#include "list-objects.h"
  12#include "run-command.h"
  13#include "connect.h"
  14#include "sigchain.h"
  15#include "version.h"
  16#include "string-list.h"
  17
  18static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
  19
  20/* bits #0..7 in revision.h, #8..10 in commit.c */
  21#define THEY_HAVE       (1u << 11)
  22#define OUR_REF         (1u << 12)
  23#define WANTED          (1u << 13)
  24#define COMMON_KNOWN    (1u << 14)
  25#define REACHABLE       (1u << 15)
  26
  27#define SHALLOW         (1u << 16)
  28#define NOT_SHALLOW     (1u << 17)
  29#define CLIENT_SHALLOW  (1u << 18)
  30#define HIDDEN_REF      (1u << 19)
  31
  32static unsigned long oldest_have;
  33
  34static int multi_ack;
  35static int no_done;
  36static int use_thin_pack, use_ofs_delta, use_include_tag;
  37static int no_progress, daemon_mode;
  38static int allow_tip_sha1_in_want;
  39static int shallow_nr;
  40static struct object_array have_obj;
  41static struct object_array want_obj;
  42static struct object_array extra_edge_obj;
  43static unsigned int timeout;
  44/* 0 for no sideband,
  45 * otherwise maximum packet size (up to 65520 bytes).
  46 */
  47static int use_sideband;
  48static int advertise_refs;
  49static int stateless_rpc;
  50
  51static void reset_timeout(void)
  52{
  53        alarm(timeout);
  54}
  55
  56static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
  57{
  58        if (use_sideband)
  59                return send_sideband(1, fd, data, sz, use_sideband);
  60        if (fd == 3)
  61                /* emergency quit */
  62                fd = 2;
  63        if (fd == 2) {
  64                /* XXX: are we happy to lose stuff here? */
  65                xwrite(fd, data, sz);
  66                return sz;
  67        }
  68        write_or_die(fd, data, sz);
  69        return sz;
  70}
  71
  72static void create_pack_file(void)
  73{
  74        struct child_process pack_objects;
  75        char data[8193], progress[128];
  76        char abort_msg[] = "aborting due to possible repository "
  77                "corruption on the remote side.";
  78        int buffered = -1;
  79        ssize_t sz;
  80        const char *argv[12];
  81        int i, arg = 0;
  82        FILE *pipe_fd;
  83        char *shallow_file = NULL;
  84
  85        if (shallow_nr) {
  86                shallow_file = setup_temporary_shallow();
  87                argv[arg++] = "--shallow-file";
  88                argv[arg++] = shallow_file;
  89        }
  90        argv[arg++] = "pack-objects";
  91        argv[arg++] = "--revs";
  92        if (use_thin_pack)
  93                argv[arg++] = "--thin";
  94
  95        argv[arg++] = "--stdout";
  96        if (!no_progress)
  97                argv[arg++] = "--progress";
  98        if (use_ofs_delta)
  99                argv[arg++] = "--delta-base-offset";
 100        if (use_include_tag)
 101                argv[arg++] = "--include-tag";
 102        argv[arg++] = NULL;
 103
 104        memset(&pack_objects, 0, sizeof(pack_objects));
 105        pack_objects.in = -1;
 106        pack_objects.out = -1;
 107        pack_objects.err = -1;
 108        pack_objects.git_cmd = 1;
 109        pack_objects.argv = argv;
 110
 111        if (start_command(&pack_objects))
 112                die("git upload-pack: unable to fork git-pack-objects");
 113
 114        pipe_fd = xfdopen(pack_objects.in, "w");
 115
 116        for (i = 0; i < want_obj.nr; i++)
 117                fprintf(pipe_fd, "%s\n",
 118                        sha1_to_hex(want_obj.objects[i].item->sha1));
 119        fprintf(pipe_fd, "--not\n");
 120        for (i = 0; i < have_obj.nr; i++)
 121                fprintf(pipe_fd, "%s\n",
 122                        sha1_to_hex(have_obj.objects[i].item->sha1));
 123        for (i = 0; i < extra_edge_obj.nr; i++)
 124                fprintf(pipe_fd, "%s\n",
 125                        sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
 126        fprintf(pipe_fd, "\n");
 127        fflush(pipe_fd);
 128        fclose(pipe_fd);
 129
 130        /* We read from pack_objects.err to capture stderr output for
 131         * progress bar, and pack_objects.out to capture the pack data.
 132         */
 133
 134        while (1) {
 135                struct pollfd pfd[2];
 136                int pe, pu, pollsize;
 137
 138                reset_timeout();
 139
 140                pollsize = 0;
 141                pe = pu = -1;
 142
 143                if (0 <= pack_objects.out) {
 144                        pfd[pollsize].fd = pack_objects.out;
 145                        pfd[pollsize].events = POLLIN;
 146                        pu = pollsize;
 147                        pollsize++;
 148                }
 149                if (0 <= pack_objects.err) {
 150                        pfd[pollsize].fd = pack_objects.err;
 151                        pfd[pollsize].events = POLLIN;
 152                        pe = pollsize;
 153                        pollsize++;
 154                }
 155
 156                if (!pollsize)
 157                        break;
 158
 159                if (poll(pfd, pollsize, -1) < 0) {
 160                        if (errno != EINTR) {
 161                                error("poll failed, resuming: %s",
 162                                      strerror(errno));
 163                                sleep(1);
 164                        }
 165                        continue;
 166                }
 167                if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
 168                        /* Status ready; we ship that in the side-band
 169                         * or dump to the standard error.
 170                         */
 171                        sz = xread(pack_objects.err, progress,
 172                                  sizeof(progress));
 173                        if (0 < sz)
 174                                send_client_data(2, progress, sz);
 175                        else if (sz == 0) {
 176                                close(pack_objects.err);
 177                                pack_objects.err = -1;
 178                        }
 179                        else
 180                                goto fail;
 181                        /* give priority to status messages */
 182                        continue;
 183                }
 184                if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
 185                        /* Data ready; we keep the last byte to ourselves
 186                         * in case we detect broken rev-list, so that we
 187                         * can leave the stream corrupted.  This is
 188                         * unfortunate -- unpack-objects would happily
 189                         * accept a valid packdata with trailing garbage,
 190                         * so appending garbage after we pass all the
 191                         * pack data is not good enough to signal
 192                         * breakage to downstream.
 193                         */
 194                        char *cp = data;
 195                        ssize_t outsz = 0;
 196                        if (0 <= buffered) {
 197                                *cp++ = buffered;
 198                                outsz++;
 199                        }
 200                        sz = xread(pack_objects.out, cp,
 201                                  sizeof(data) - outsz);
 202                        if (0 < sz)
 203                                ;
 204                        else if (sz == 0) {
 205                                close(pack_objects.out);
 206                                pack_objects.out = -1;
 207                        }
 208                        else
 209                                goto fail;
 210                        sz += outsz;
 211                        if (1 < sz) {
 212                                buffered = data[sz-1] & 0xFF;
 213                                sz--;
 214                        }
 215                        else
 216                                buffered = -1;
 217                        sz = send_client_data(1, data, sz);
 218                        if (sz < 0)
 219                                goto fail;
 220                }
 221        }
 222
 223        if (finish_command(&pack_objects)) {
 224                error("git upload-pack: git-pack-objects died with error.");
 225                goto fail;
 226        }
 227        if (shallow_file) {
 228                if (*shallow_file)
 229                        unlink(shallow_file);
 230                free(shallow_file);
 231        }
 232
 233        /* flush the data */
 234        if (0 <= buffered) {
 235                data[0] = buffered;
 236                sz = send_client_data(1, data, 1);
 237                if (sz < 0)
 238                        goto fail;
 239                fprintf(stderr, "flushed.\n");
 240        }
 241        if (use_sideband)
 242                packet_flush(1);
 243        return;
 244
 245 fail:
 246        send_client_data(3, abort_msg, sizeof(abort_msg));
 247        die("git upload-pack: %s", abort_msg);
 248}
 249
 250static int got_sha1(char *hex, unsigned char *sha1)
 251{
 252        struct object *o;
 253        int we_knew_they_have = 0;
 254
 255        if (get_sha1_hex(hex, sha1))
 256                die("git upload-pack: expected SHA1 object, got '%s'", hex);
 257        if (!has_sha1_file(sha1))
 258                return -1;
 259
 260        o = parse_object(sha1);
 261        if (!o)
 262                die("oops (%s)", sha1_to_hex(sha1));
 263        if (o->type == OBJ_COMMIT) {
 264                struct commit_list *parents;
 265                struct commit *commit = (struct commit *)o;
 266                if (o->flags & THEY_HAVE)
 267                        we_knew_they_have = 1;
 268                else
 269                        o->flags |= THEY_HAVE;
 270                if (!oldest_have || (commit->date < oldest_have))
 271                        oldest_have = commit->date;
 272                for (parents = commit->parents;
 273                     parents;
 274                     parents = parents->next)
 275                        parents->item->object.flags |= THEY_HAVE;
 276        }
 277        if (!we_knew_they_have) {
 278                add_object_array(o, NULL, &have_obj);
 279                return 1;
 280        }
 281        return 0;
 282}
 283
 284static int reachable(struct commit *want)
 285{
 286        struct commit_list *work = NULL;
 287
 288        commit_list_insert_by_date(want, &work);
 289        while (work) {
 290                struct commit_list *list = work->next;
 291                struct commit *commit = work->item;
 292                free(work);
 293                work = list;
 294
 295                if (commit->object.flags & THEY_HAVE) {
 296                        want->object.flags |= COMMON_KNOWN;
 297                        break;
 298                }
 299                if (!commit->object.parsed)
 300                        parse_object(commit->object.sha1);
 301                if (commit->object.flags & REACHABLE)
 302                        continue;
 303                commit->object.flags |= REACHABLE;
 304                if (commit->date < oldest_have)
 305                        continue;
 306                for (list = commit->parents; list; list = list->next) {
 307                        struct commit *parent = list->item;
 308                        if (!(parent->object.flags & REACHABLE))
 309                                commit_list_insert_by_date(parent, &work);
 310                }
 311        }
 312        want->object.flags |= REACHABLE;
 313        clear_commit_marks(want, REACHABLE);
 314        free_commit_list(work);
 315        return (want->object.flags & COMMON_KNOWN);
 316}
 317
 318static int ok_to_give_up(void)
 319{
 320        int i;
 321
 322        if (!have_obj.nr)
 323                return 0;
 324
 325        for (i = 0; i < want_obj.nr; i++) {
 326                struct object *want = want_obj.objects[i].item;
 327
 328                if (want->flags & COMMON_KNOWN)
 329                        continue;
 330                want = deref_tag(want, "a want line", 0);
 331                if (!want || want->type != OBJ_COMMIT) {
 332                        /* no way to tell if this is reachable by
 333                         * looking at the ancestry chain alone, so
 334                         * leave a note to ourselves not to worry about
 335                         * this object anymore.
 336                         */
 337                        want_obj.objects[i].item->flags |= COMMON_KNOWN;
 338                        continue;
 339                }
 340                if (!reachable((struct commit *)want))
 341                        return 0;
 342        }
 343        return 1;
 344}
 345
 346static int get_common_commits(void)
 347{
 348        unsigned char sha1[20];
 349        char last_hex[41];
 350        int got_common = 0;
 351        int got_other = 0;
 352        int sent_ready = 0;
 353
 354        save_commit_buffer = 0;
 355
 356        for (;;) {
 357                char *line = packet_read_line(0, NULL);
 358                reset_timeout();
 359
 360                if (!line) {
 361                        if (multi_ack == 2 && got_common
 362                            && !got_other && ok_to_give_up()) {
 363                                sent_ready = 1;
 364                                packet_write(1, "ACK %s ready\n", last_hex);
 365                        }
 366                        if (have_obj.nr == 0 || multi_ack)
 367                                packet_write(1, "NAK\n");
 368
 369                        if (no_done && sent_ready) {
 370                                packet_write(1, "ACK %s\n", last_hex);
 371                                return 0;
 372                        }
 373                        if (stateless_rpc)
 374                                exit(0);
 375                        got_common = 0;
 376                        got_other = 0;
 377                        continue;
 378                }
 379                if (!prefixcmp(line, "have ")) {
 380                        switch (got_sha1(line+5, sha1)) {
 381                        case -1: /* they have what we do not */
 382                                got_other = 1;
 383                                if (multi_ack && ok_to_give_up()) {
 384                                        const char *hex = sha1_to_hex(sha1);
 385                                        if (multi_ack == 2) {
 386                                                sent_ready = 1;
 387                                                packet_write(1, "ACK %s ready\n", hex);
 388                                        } else
 389                                                packet_write(1, "ACK %s continue\n", hex);
 390                                }
 391                                break;
 392                        default:
 393                                got_common = 1;
 394                                memcpy(last_hex, sha1_to_hex(sha1), 41);
 395                                if (multi_ack == 2)
 396                                        packet_write(1, "ACK %s common\n", last_hex);
 397                                else if (multi_ack)
 398                                        packet_write(1, "ACK %s continue\n", last_hex);
 399                                else if (have_obj.nr == 1)
 400                                        packet_write(1, "ACK %s\n", last_hex);
 401                                break;
 402                        }
 403                        continue;
 404                }
 405                if (!strcmp(line, "done")) {
 406                        if (have_obj.nr > 0) {
 407                                if (multi_ack)
 408                                        packet_write(1, "ACK %s\n", last_hex);
 409                                return 0;
 410                        }
 411                        packet_write(1, "NAK\n");
 412                        return -1;
 413                }
 414                die("git upload-pack: expected SHA1 list, got '%s'", line);
 415        }
 416}
 417
 418static int is_our_ref(struct object *o)
 419{
 420        return o->flags &
 421                ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
 422}
 423
 424static void check_non_tip(void)
 425{
 426        static const char *argv[] = {
 427                "rev-list", "--stdin", NULL,
 428        };
 429        static struct child_process cmd;
 430        struct object *o;
 431        char namebuf[42]; /* ^ + SHA-1 + LF */
 432        int i;
 433
 434        /* In the normal in-process case non-tip request can never happen */
 435        if (!stateless_rpc)
 436                goto error;
 437
 438        cmd.argv = argv;
 439        cmd.git_cmd = 1;
 440        cmd.no_stderr = 1;
 441        cmd.in = -1;
 442        cmd.out = -1;
 443
 444        if (start_command(&cmd))
 445                goto error;
 446
 447        /*
 448         * If rev-list --stdin encounters an unknown commit, it
 449         * terminates, which will cause SIGPIPE in the write loop
 450         * below.
 451         */
 452        sigchain_push(SIGPIPE, SIG_IGN);
 453
 454        namebuf[0] = '^';
 455        namebuf[41] = '\n';
 456        for (i = get_max_object_index(); 0 < i; ) {
 457                o = get_indexed_object(--i);
 458                if (!o)
 459                        continue;
 460                if (!is_our_ref(o))
 461                        continue;
 462                memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
 463                if (write_in_full(cmd.in, namebuf, 42) < 0)
 464                        goto error;
 465        }
 466        namebuf[40] = '\n';
 467        for (i = 0; i < want_obj.nr; i++) {
 468                o = want_obj.objects[i].item;
 469                if (is_our_ref(o))
 470                        continue;
 471                memcpy(namebuf, sha1_to_hex(o->sha1), 40);
 472                if (write_in_full(cmd.in, namebuf, 41) < 0)
 473                        goto error;
 474        }
 475        close(cmd.in);
 476
 477        sigchain_pop(SIGPIPE);
 478
 479        /*
 480         * The commits out of the rev-list are not ancestors of
 481         * our ref.
 482         */
 483        i = read_in_full(cmd.out, namebuf, 1);
 484        if (i)
 485                goto error;
 486        close(cmd.out);
 487
 488        /*
 489         * rev-list may have died by encountering a bad commit
 490         * in the history, in which case we do want to bail out
 491         * even when it showed no commit.
 492         */
 493        if (finish_command(&cmd))
 494                goto error;
 495
 496        /* All the non-tip ones are ancestors of what we advertised */
 497        return;
 498
 499error:
 500        /* Pick one of them (we know there at least is one) */
 501        for (i = 0; i < want_obj.nr; i++) {
 502                o = want_obj.objects[i].item;
 503                if (!is_our_ref(o))
 504                        die("git upload-pack: not our ref %s",
 505                            sha1_to_hex(o->sha1));
 506        }
 507}
 508
 509static void receive_needs(void)
 510{
 511        struct object_array shallows = OBJECT_ARRAY_INIT;
 512        int depth = 0;
 513        int has_non_tip = 0;
 514
 515        shallow_nr = 0;
 516        for (;;) {
 517                struct object *o;
 518                const char *features;
 519                unsigned char sha1_buf[20];
 520                char *line = packet_read_line(0, NULL);
 521                reset_timeout();
 522                if (!line)
 523                        break;
 524
 525                if (!prefixcmp(line, "shallow ")) {
 526                        unsigned char sha1[20];
 527                        struct object *object;
 528                        if (get_sha1_hex(line + 8, sha1))
 529                                die("invalid shallow line: %s", line);
 530                        object = parse_object(sha1);
 531                        if (!object)
 532                                continue;
 533                        if (object->type != OBJ_COMMIT)
 534                                die("invalid shallow object %s", sha1_to_hex(sha1));
 535                        if (!(object->flags & CLIENT_SHALLOW)) {
 536                                object->flags |= CLIENT_SHALLOW;
 537                                add_object_array(object, NULL, &shallows);
 538                        }
 539                        continue;
 540                }
 541                if (!prefixcmp(line, "deepen ")) {
 542                        char *end;
 543                        depth = strtol(line + 7, &end, 0);
 544                        if (end == line + 7 || depth <= 0)
 545                                die("Invalid deepen: %s", line);
 546                        continue;
 547                }
 548                if (prefixcmp(line, "want ") ||
 549                    get_sha1_hex(line+5, sha1_buf))
 550                        die("git upload-pack: protocol error, "
 551                            "expected to get sha, not '%s'", line);
 552
 553                features = line + 45;
 554
 555                if (parse_feature_request(features, "multi_ack_detailed"))
 556                        multi_ack = 2;
 557                else if (parse_feature_request(features, "multi_ack"))
 558                        multi_ack = 1;
 559                if (parse_feature_request(features, "no-done"))
 560                        no_done = 1;
 561                if (parse_feature_request(features, "thin-pack"))
 562                        use_thin_pack = 1;
 563                if (parse_feature_request(features, "ofs-delta"))
 564                        use_ofs_delta = 1;
 565                if (parse_feature_request(features, "side-band-64k"))
 566                        use_sideband = LARGE_PACKET_MAX;
 567                else if (parse_feature_request(features, "side-band"))
 568                        use_sideband = DEFAULT_PACKET_MAX;
 569                if (parse_feature_request(features, "no-progress"))
 570                        no_progress = 1;
 571                if (parse_feature_request(features, "include-tag"))
 572                        use_include_tag = 1;
 573
 574                o = parse_object(sha1_buf);
 575                if (!o)
 576                        die("git upload-pack: not our ref %s",
 577                            sha1_to_hex(sha1_buf));
 578                if (!(o->flags & WANTED)) {
 579                        o->flags |= WANTED;
 580                        if (!is_our_ref(o))
 581                                has_non_tip = 1;
 582                        add_object_array(o, NULL, &want_obj);
 583                }
 584        }
 585
 586        /*
 587         * We have sent all our refs already, and the other end
 588         * should have chosen out of them. When we are operating
 589         * in the stateless RPC mode, however, their choice may
 590         * have been based on the set of older refs advertised
 591         * by another process that handled the initial request.
 592         */
 593        if (has_non_tip)
 594                check_non_tip();
 595
 596        if (!use_sideband && daemon_mode)
 597                no_progress = 1;
 598
 599        if (depth == 0 && shallows.nr == 0)
 600                return;
 601        if (depth > 0) {
 602                struct commit_list *result = NULL, *backup = NULL;
 603                int i;
 604                if (depth == INFINITE_DEPTH)
 605                        for (i = 0; i < shallows.nr; i++) {
 606                                struct object *object = shallows.objects[i].item;
 607                                object->flags |= NOT_SHALLOW;
 608                        }
 609                else
 610                        backup = result =
 611                                get_shallow_commits(&want_obj, depth,
 612                                                    SHALLOW, NOT_SHALLOW);
 613                while (result) {
 614                        struct object *object = &result->item->object;
 615                        if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
 616                                packet_write(1, "shallow %s",
 617                                                sha1_to_hex(object->sha1));
 618                                register_shallow(object->sha1);
 619                                shallow_nr++;
 620                        }
 621                        result = result->next;
 622                }
 623                free_commit_list(backup);
 624                for (i = 0; i < shallows.nr; i++) {
 625                        struct object *object = shallows.objects[i].item;
 626                        if (object->flags & NOT_SHALLOW) {
 627                                struct commit_list *parents;
 628                                packet_write(1, "unshallow %s",
 629                                        sha1_to_hex(object->sha1));
 630                                object->flags &= ~CLIENT_SHALLOW;
 631                                /* make sure the real parents are parsed */
 632                                unregister_shallow(object->sha1);
 633                                object->parsed = 0;
 634                                if (parse_commit((struct commit *)object))
 635                                        die("invalid commit");
 636                                parents = ((struct commit *)object)->parents;
 637                                while (parents) {
 638                                        add_object_array(&parents->item->object,
 639                                                        NULL, &want_obj);
 640                                        parents = parents->next;
 641                                }
 642                                add_object_array(object, NULL, &extra_edge_obj);
 643                        }
 644                        /* make sure commit traversal conforms to client */
 645                        register_shallow(object->sha1);
 646                }
 647                packet_flush(1);
 648        } else
 649                if (shallows.nr > 0) {
 650                        int i;
 651                        for (i = 0; i < shallows.nr; i++)
 652                                register_shallow(shallows.objects[i].item->sha1);
 653                }
 654
 655        shallow_nr += shallows.nr;
 656        free(shallows.objects);
 657}
 658
 659/* return non-zero if the ref is hidden, otherwise 0 */
 660static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 661{
 662        struct object *o = lookup_unknown_object(sha1);
 663
 664        if (ref_is_hidden(refname)) {
 665                o->flags |= HIDDEN_REF;
 666                return 1;
 667        }
 668        if (!o)
 669                die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
 670        o->flags |= OUR_REF;
 671        return 0;
 672}
 673
 674static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 675{
 676        static const char *capabilities = "multi_ack thin-pack side-band"
 677                " side-band-64k ofs-delta shallow no-progress"
 678                " include-tag multi_ack_detailed";
 679        const char *refname_nons = strip_namespace(refname);
 680        unsigned char peeled[20];
 681
 682        if (mark_our_ref(refname, sha1, flag, cb_data))
 683                return 0;
 684
 685        if (capabilities)
 686                packet_write(1, "%s %s%c%s%s%s agent=%s\n",
 687                             sha1_to_hex(sha1), refname_nons,
 688                             0, capabilities,
 689                             allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
 690                             stateless_rpc ? " no-done" : "",
 691                             git_user_agent_sanitized());
 692        else
 693                packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
 694        capabilities = NULL;
 695        if (!peel_ref(refname, peeled))
 696                packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
 697        return 0;
 698}
 699
 700static void upload_pack(void)
 701{
 702        if (advertise_refs || !stateless_rpc) {
 703                reset_timeout();
 704                head_ref_namespaced(send_ref, NULL);
 705                for_each_namespaced_ref(send_ref, NULL);
 706                packet_flush(1);
 707        } else {
 708                head_ref_namespaced(mark_our_ref, NULL);
 709                for_each_namespaced_ref(mark_our_ref, NULL);
 710        }
 711        if (advertise_refs)
 712                return;
 713
 714        receive_needs();
 715        if (want_obj.nr) {
 716                get_common_commits();
 717                create_pack_file();
 718        }
 719}
 720
 721static int upload_pack_config(const char *var, const char *value, void *unused)
 722{
 723        if (!strcmp("uploadpack.allowtipsha1inwant", var))
 724                allow_tip_sha1_in_want = git_config_bool(var, value);
 725        return parse_hide_refs_config(var, value, "uploadpack");
 726}
 727
 728int main(int argc, char **argv)
 729{
 730        char *dir;
 731        int i;
 732        int strict = 0;
 733
 734        git_setup_gettext();
 735
 736        packet_trace_identity("upload-pack");
 737        git_extract_argv0_path(argv[0]);
 738        read_replace_refs = 0;
 739
 740        for (i = 1; i < argc; i++) {
 741                char *arg = argv[i];
 742
 743                if (arg[0] != '-')
 744                        break;
 745                if (!strcmp(arg, "--advertise-refs")) {
 746                        advertise_refs = 1;
 747                        continue;
 748                }
 749                if (!strcmp(arg, "--stateless-rpc")) {
 750                        stateless_rpc = 1;
 751                        continue;
 752                }
 753                if (!strcmp(arg, "--strict")) {
 754                        strict = 1;
 755                        continue;
 756                }
 757                if (!prefixcmp(arg, "--timeout=")) {
 758                        timeout = atoi(arg+10);
 759                        daemon_mode = 1;
 760                        continue;
 761                }
 762                if (!strcmp(arg, "--")) {
 763                        i++;
 764                        break;
 765                }
 766        }
 767
 768        if (i != argc-1)
 769                usage(upload_pack_usage);
 770
 771        setup_path();
 772
 773        dir = argv[i];
 774
 775        if (!enter_repo(dir, strict))
 776                die("'%s' does not appear to be a git repository", dir);
 777        if (is_repository_shallow())
 778                die("attempt to fetch/clone from a shallow repository");
 779        git_config(upload_pack_config, NULL);
 780        upload_pack();
 781        return 0;
 782}