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