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