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