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