upload-pack.con commit Merge branch 'rs/oidset-on-khash' (82d0a8c)
   1#include "cache.h"
   2#include "config.h"
   3#include "refs.h"
   4#include "pkt-line.h"
   5#include "sideband.h"
   6#include "repository.h"
   7#include "object-store.h"
   8#include "tag.h"
   9#include "object.h"
  10#include "commit.h"
  11#include "diff.h"
  12#include "revision.h"
  13#include "list-objects.h"
  14#include "list-objects-filter.h"
  15#include "list-objects-filter-options.h"
  16#include "run-command.h"
  17#include "connect.h"
  18#include "sigchain.h"
  19#include "version.h"
  20#include "string-list.h"
  21#include "argv-array.h"
  22#include "prio-queue.h"
  23#include "protocol.h"
  24#include "quote.h"
  25#include "upload-pack.h"
  26#include "serve.h"
  27#include "commit-graph.h"
  28#include "commit-reach.h"
  29
  30/* Remember to update object flag allocation in object.h */
  31#define THEY_HAVE       (1u << 11)
  32#define OUR_REF         (1u << 12)
  33#define WANTED          (1u << 13)
  34#define COMMON_KNOWN    (1u << 14)
  35
  36#define SHALLOW         (1u << 16)
  37#define NOT_SHALLOW     (1u << 17)
  38#define CLIENT_SHALLOW  (1u << 18)
  39#define HIDDEN_REF      (1u << 19)
  40
  41static timestamp_t oldest_have;
  42
  43static int deepen_relative;
  44static int multi_ack;
  45static int no_done;
  46static int use_thin_pack, use_ofs_delta, use_include_tag;
  47static int no_progress, daemon_mode;
  48/* Allow specifying sha1 if it is a ref tip. */
  49#define ALLOW_TIP_SHA1  01
  50/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
  51#define ALLOW_REACHABLE_SHA1    02
  52/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
  53#define ALLOW_ANY_SHA1  07
  54static unsigned int allow_unadvertised_object_request;
  55static int shallow_nr;
  56static struct object_array have_obj;
  57static struct object_array want_obj;
  58static struct object_array extra_edge_obj;
  59static unsigned int timeout;
  60static int keepalive = 5;
  61/* 0 for no sideband,
  62 * otherwise maximum packet size (up to 65520 bytes).
  63 */
  64static int use_sideband;
  65static int stateless_rpc;
  66static const char *pack_objects_hook;
  67
  68static int filter_capability_requested;
  69static int allow_filter;
  70static int allow_ref_in_want;
  71static struct list_objects_filter_options filter_options;
  72
  73static void reset_timeout(void)
  74{
  75        alarm(timeout);
  76}
  77
  78static void send_client_data(int fd, const char *data, ssize_t sz)
  79{
  80        if (use_sideband) {
  81                send_sideband(1, fd, data, sz, use_sideband);
  82                return;
  83        }
  84        if (fd == 3)
  85                /* emergency quit */
  86                fd = 2;
  87        if (fd == 2) {
  88                /* XXX: are we happy to lose stuff here? */
  89                xwrite(fd, data, sz);
  90                return;
  91        }
  92        write_or_die(fd, data, sz);
  93}
  94
  95static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
  96{
  97        FILE *fp = cb_data;
  98        if (graft->nr_parent == -1)
  99                fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
 100        return 0;
 101}
 102
 103static void create_pack_file(void)
 104{
 105        struct child_process pack_objects = CHILD_PROCESS_INIT;
 106        char data[8193], progress[128];
 107        char abort_msg[] = "aborting due to possible repository "
 108                "corruption on the remote side.";
 109        int buffered = -1;
 110        ssize_t sz;
 111        int i;
 112        FILE *pipe_fd;
 113
 114        if (!pack_objects_hook)
 115                pack_objects.git_cmd = 1;
 116        else {
 117                argv_array_push(&pack_objects.args, pack_objects_hook);
 118                argv_array_push(&pack_objects.args, "git");
 119                pack_objects.use_shell = 1;
 120        }
 121
 122        if (shallow_nr) {
 123                argv_array_push(&pack_objects.args, "--shallow-file");
 124                argv_array_push(&pack_objects.args, "");
 125        }
 126        argv_array_push(&pack_objects.args, "pack-objects");
 127        argv_array_push(&pack_objects.args, "--revs");
 128        if (use_thin_pack)
 129                argv_array_push(&pack_objects.args, "--thin");
 130
 131        argv_array_push(&pack_objects.args, "--stdout");
 132        if (shallow_nr)
 133                argv_array_push(&pack_objects.args, "--shallow");
 134        if (!no_progress)
 135                argv_array_push(&pack_objects.args, "--progress");
 136        if (use_ofs_delta)
 137                argv_array_push(&pack_objects.args, "--delta-base-offset");
 138        if (use_include_tag)
 139                argv_array_push(&pack_objects.args, "--include-tag");
 140        if (filter_options.filter_spec) {
 141                if (pack_objects.use_shell) {
 142                        struct strbuf buf = STRBUF_INIT;
 143                        sq_quote_buf(&buf, filter_options.filter_spec);
 144                        argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
 145                        strbuf_release(&buf);
 146                } else {
 147                        argv_array_pushf(&pack_objects.args, "--filter=%s",
 148                                         filter_options.filter_spec);
 149                }
 150        }
 151
 152        pack_objects.in = -1;
 153        pack_objects.out = -1;
 154        pack_objects.err = -1;
 155
 156        if (start_command(&pack_objects))
 157                die("git upload-pack: unable to fork git-pack-objects");
 158
 159        pipe_fd = xfdopen(pack_objects.in, "w");
 160
 161        if (shallow_nr)
 162                for_each_commit_graft(write_one_shallow, pipe_fd);
 163
 164        for (i = 0; i < want_obj.nr; i++)
 165                fprintf(pipe_fd, "%s\n",
 166                        oid_to_hex(&want_obj.objects[i].item->oid));
 167        fprintf(pipe_fd, "--not\n");
 168        for (i = 0; i < have_obj.nr; i++)
 169                fprintf(pipe_fd, "%s\n",
 170                        oid_to_hex(&have_obj.objects[i].item->oid));
 171        for (i = 0; i < extra_edge_obj.nr; i++)
 172                fprintf(pipe_fd, "%s\n",
 173                        oid_to_hex(&extra_edge_obj.objects[i].item->oid));
 174        fprintf(pipe_fd, "\n");
 175        fflush(pipe_fd);
 176        fclose(pipe_fd);
 177
 178        /* We read from pack_objects.err to capture stderr output for
 179         * progress bar, and pack_objects.out to capture the pack data.
 180         */
 181
 182        while (1) {
 183                struct pollfd pfd[2];
 184                int pe, pu, pollsize;
 185                int ret;
 186
 187                reset_timeout();
 188
 189                pollsize = 0;
 190                pe = pu = -1;
 191
 192                if (0 <= pack_objects.out) {
 193                        pfd[pollsize].fd = pack_objects.out;
 194                        pfd[pollsize].events = POLLIN;
 195                        pu = pollsize;
 196                        pollsize++;
 197                }
 198                if (0 <= pack_objects.err) {
 199                        pfd[pollsize].fd = pack_objects.err;
 200                        pfd[pollsize].events = POLLIN;
 201                        pe = pollsize;
 202                        pollsize++;
 203                }
 204
 205                if (!pollsize)
 206                        break;
 207
 208                ret = poll(pfd, pollsize,
 209                        keepalive < 0 ? -1 : 1000 * keepalive);
 210
 211                if (ret < 0) {
 212                        if (errno != EINTR) {
 213                                error_errno("poll failed, resuming");
 214                                sleep(1);
 215                        }
 216                        continue;
 217                }
 218                if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
 219                        /* Status ready; we ship that in the side-band
 220                         * or dump to the standard error.
 221                         */
 222                        sz = xread(pack_objects.err, progress,
 223                                  sizeof(progress));
 224                        if (0 < sz)
 225                                send_client_data(2, progress, sz);
 226                        else if (sz == 0) {
 227                                close(pack_objects.err);
 228                                pack_objects.err = -1;
 229                        }
 230                        else
 231                                goto fail;
 232                        /* give priority to status messages */
 233                        continue;
 234                }
 235                if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
 236                        /* Data ready; we keep the last byte to ourselves
 237                         * in case we detect broken rev-list, so that we
 238                         * can leave the stream corrupted.  This is
 239                         * unfortunate -- unpack-objects would happily
 240                         * accept a valid packdata with trailing garbage,
 241                         * so appending garbage after we pass all the
 242                         * pack data is not good enough to signal
 243                         * breakage to downstream.
 244                         */
 245                        char *cp = data;
 246                        ssize_t outsz = 0;
 247                        if (0 <= buffered) {
 248                                *cp++ = buffered;
 249                                outsz++;
 250                        }
 251                        sz = xread(pack_objects.out, cp,
 252                                  sizeof(data) - outsz);
 253                        if (0 < sz)
 254                                ;
 255                        else if (sz == 0) {
 256                                close(pack_objects.out);
 257                                pack_objects.out = -1;
 258                        }
 259                        else
 260                                goto fail;
 261                        sz += outsz;
 262                        if (1 < sz) {
 263                                buffered = data[sz-1] & 0xFF;
 264                                sz--;
 265                        }
 266                        else
 267                                buffered = -1;
 268                        send_client_data(1, data, sz);
 269                }
 270
 271                /*
 272                 * We hit the keepalive timeout without saying anything; send
 273                 * an empty message on the data sideband just to let the other
 274                 * side know we're still working on it, but don't have any data
 275                 * yet.
 276                 *
 277                 * If we don't have a sideband channel, there's no room in the
 278                 * protocol to say anything, so those clients are just out of
 279                 * luck.
 280                 */
 281                if (!ret && use_sideband) {
 282                        static const char buf[] = "0005\1";
 283                        write_or_die(1, buf, 5);
 284                }
 285        }
 286
 287        if (finish_command(&pack_objects)) {
 288                error("git upload-pack: git-pack-objects died with error.");
 289                goto fail;
 290        }
 291
 292        /* flush the data */
 293        if (0 <= buffered) {
 294                data[0] = buffered;
 295                send_client_data(1, data, 1);
 296                fprintf(stderr, "flushed.\n");
 297        }
 298        if (use_sideband)
 299                packet_flush(1);
 300        return;
 301
 302 fail:
 303        send_client_data(3, abort_msg, sizeof(abort_msg));
 304        die("git upload-pack: %s", abort_msg);
 305}
 306
 307static int got_oid(const char *hex, struct object_id *oid)
 308{
 309        struct object *o;
 310        int we_knew_they_have = 0;
 311
 312        if (get_oid_hex(hex, oid))
 313                die("git upload-pack: expected SHA1 object, got '%s'", hex);
 314        if (!has_object_file(oid))
 315                return -1;
 316
 317        o = parse_object(the_repository, oid);
 318        if (!o)
 319                die("oops (%s)", oid_to_hex(oid));
 320        if (o->type == OBJ_COMMIT) {
 321                struct commit_list *parents;
 322                struct commit *commit = (struct commit *)o;
 323                if (o->flags & THEY_HAVE)
 324                        we_knew_they_have = 1;
 325                else
 326                        o->flags |= THEY_HAVE;
 327                if (!oldest_have || (commit->date < oldest_have))
 328                        oldest_have = commit->date;
 329                for (parents = commit->parents;
 330                     parents;
 331                     parents = parents->next)
 332                        parents->item->object.flags |= THEY_HAVE;
 333        }
 334        if (!we_knew_they_have) {
 335                add_object_array(o, NULL, &have_obj);
 336                return 1;
 337        }
 338        return 0;
 339}
 340
 341static int ok_to_give_up(void)
 342{
 343        uint32_t min_generation = GENERATION_NUMBER_ZERO;
 344
 345        if (!have_obj.nr)
 346                return 0;
 347
 348        return can_all_from_reach_with_flag(&want_obj, THEY_HAVE,
 349                                            COMMON_KNOWN, oldest_have,
 350                                            min_generation);
 351}
 352
 353static int get_common_commits(void)
 354{
 355        struct object_id oid;
 356        char last_hex[GIT_MAX_HEXSZ + 1];
 357        int got_common = 0;
 358        int got_other = 0;
 359        int sent_ready = 0;
 360
 361        save_commit_buffer = 0;
 362
 363        for (;;) {
 364                char *line = packet_read_line(0, NULL);
 365                const char *arg;
 366
 367                reset_timeout();
 368
 369                if (!line) {
 370                        if (multi_ack == 2 && got_common
 371                            && !got_other && ok_to_give_up()) {
 372                                sent_ready = 1;
 373                                packet_write_fmt(1, "ACK %s ready\n", last_hex);
 374                        }
 375                        if (have_obj.nr == 0 || multi_ack)
 376                                packet_write_fmt(1, "NAK\n");
 377
 378                        if (no_done && sent_ready) {
 379                                packet_write_fmt(1, "ACK %s\n", last_hex);
 380                                return 0;
 381                        }
 382                        if (stateless_rpc)
 383                                exit(0);
 384                        got_common = 0;
 385                        got_other = 0;
 386                        continue;
 387                }
 388                if (skip_prefix(line, "have ", &arg)) {
 389                        switch (got_oid(arg, &oid)) {
 390                        case -1: /* they have what we do not */
 391                                got_other = 1;
 392                                if (multi_ack && ok_to_give_up()) {
 393                                        const char *hex = oid_to_hex(&oid);
 394                                        if (multi_ack == 2) {
 395                                                sent_ready = 1;
 396                                                packet_write_fmt(1, "ACK %s ready\n", hex);
 397                                        } else
 398                                                packet_write_fmt(1, "ACK %s continue\n", hex);
 399                                }
 400                                break;
 401                        default:
 402                                got_common = 1;
 403                                oid_to_hex_r(last_hex, &oid);
 404                                if (multi_ack == 2)
 405                                        packet_write_fmt(1, "ACK %s common\n", last_hex);
 406                                else if (multi_ack)
 407                                        packet_write_fmt(1, "ACK %s continue\n", last_hex);
 408                                else if (have_obj.nr == 1)
 409                                        packet_write_fmt(1, "ACK %s\n", last_hex);
 410                                break;
 411                        }
 412                        continue;
 413                }
 414                if (!strcmp(line, "done")) {
 415                        if (have_obj.nr > 0) {
 416                                if (multi_ack)
 417                                        packet_write_fmt(1, "ACK %s\n", last_hex);
 418                                return 0;
 419                        }
 420                        packet_write_fmt(1, "NAK\n");
 421                        return -1;
 422                }
 423                die("git upload-pack: expected SHA1 list, got '%s'", line);
 424        }
 425}
 426
 427static int is_our_ref(struct object *o)
 428{
 429        int allow_hidden_ref = (allow_unadvertised_object_request &
 430                        (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
 431        return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
 432}
 433
 434/*
 435 * on successful case, it's up to the caller to close cmd->out
 436 */
 437static int do_reachable_revlist(struct child_process *cmd,
 438                                struct object_array *src,
 439                                struct object_array *reachable)
 440{
 441        static const char *argv[] = {
 442                "rev-list", "--stdin", NULL,
 443        };
 444        struct object *o;
 445        char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
 446        int i;
 447
 448        cmd->argv = argv;
 449        cmd->git_cmd = 1;
 450        cmd->no_stderr = 1;
 451        cmd->in = -1;
 452        cmd->out = -1;
 453
 454        /*
 455         * If the next rev-list --stdin encounters an unknown commit,
 456         * it terminates, which will cause SIGPIPE in the write loop
 457         * below.
 458         */
 459        sigchain_push(SIGPIPE, SIG_IGN);
 460
 461        if (start_command(cmd))
 462                goto error;
 463
 464        namebuf[0] = '^';
 465        namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
 466        for (i = get_max_object_index(); 0 < i; ) {
 467                o = get_indexed_object(--i);
 468                if (!o)
 469                        continue;
 470                if (reachable && o->type == OBJ_COMMIT)
 471                        o->flags &= ~TMP_MARK;
 472                if (!is_our_ref(o))
 473                        continue;
 474                memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
 475                if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
 476                        goto error;
 477        }
 478        namebuf[GIT_SHA1_HEXSZ] = '\n';
 479        for (i = 0; i < src->nr; i++) {
 480                o = src->objects[i].item;
 481                if (is_our_ref(o)) {
 482                        if (reachable)
 483                                add_object_array(o, NULL, reachable);
 484                        continue;
 485                }
 486                if (reachable && o->type == OBJ_COMMIT)
 487                        o->flags |= TMP_MARK;
 488                memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
 489                if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
 490                        goto error;
 491        }
 492        close(cmd->in);
 493        cmd->in = -1;
 494        sigchain_pop(SIGPIPE);
 495
 496        return 0;
 497
 498error:
 499        sigchain_pop(SIGPIPE);
 500
 501        if (cmd->in >= 0)
 502                close(cmd->in);
 503        if (cmd->out >= 0)
 504                close(cmd->out);
 505        return -1;
 506}
 507
 508static int get_reachable_list(struct object_array *src,
 509                              struct object_array *reachable)
 510{
 511        struct child_process cmd = CHILD_PROCESS_INIT;
 512        int i;
 513        struct object *o;
 514        char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
 515        const unsigned hexsz = the_hash_algo->hexsz;
 516
 517        if (do_reachable_revlist(&cmd, src, reachable) < 0)
 518                return -1;
 519
 520        while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
 521                struct object_id sha1;
 522                const char *p;
 523
 524                if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
 525                        break;
 526
 527                o = lookup_object(the_repository, sha1.hash);
 528                if (o && o->type == OBJ_COMMIT) {
 529                        o->flags &= ~TMP_MARK;
 530                }
 531        }
 532        for (i = get_max_object_index(); 0 < i; i--) {
 533                o = get_indexed_object(i - 1);
 534                if (o && o->type == OBJ_COMMIT &&
 535                    (o->flags & TMP_MARK)) {
 536                        add_object_array(o, NULL, reachable);
 537                                o->flags &= ~TMP_MARK;
 538                }
 539        }
 540        close(cmd.out);
 541
 542        if (finish_command(&cmd))
 543                return -1;
 544
 545        return 0;
 546}
 547
 548static int has_unreachable(struct object_array *src)
 549{
 550        struct child_process cmd = CHILD_PROCESS_INIT;
 551        char buf[1];
 552        int i;
 553
 554        if (do_reachable_revlist(&cmd, src, NULL) < 0)
 555                return 1;
 556
 557        /*
 558         * The commits out of the rev-list are not ancestors of
 559         * our ref.
 560         */
 561        i = read_in_full(cmd.out, buf, 1);
 562        if (i)
 563                goto error;
 564        close(cmd.out);
 565        cmd.out = -1;
 566
 567        /*
 568         * rev-list may have died by encountering a bad commit
 569         * in the history, in which case we do want to bail out
 570         * even when it showed no commit.
 571         */
 572        if (finish_command(&cmd))
 573                goto error;
 574
 575        /* All the non-tip ones are ancestors of what we advertised */
 576        return 0;
 577
 578error:
 579        sigchain_pop(SIGPIPE);
 580        if (cmd.out >= 0)
 581                close(cmd.out);
 582        return 1;
 583}
 584
 585static void check_non_tip(void)
 586{
 587        int i;
 588
 589        /*
 590         * In the normal in-process case without
 591         * uploadpack.allowReachableSHA1InWant,
 592         * non-tip requests can never happen.
 593         */
 594        if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
 595                goto error;
 596        if (!has_unreachable(&want_obj))
 597                /* All the non-tip ones are ancestors of what we advertised */
 598                return;
 599
 600error:
 601        /* Pick one of them (we know there at least is one) */
 602        for (i = 0; i < want_obj.nr; i++) {
 603                struct object *o = want_obj.objects[i].item;
 604                if (!is_our_ref(o))
 605                        die("git upload-pack: not our ref %s",
 606                            oid_to_hex(&o->oid));
 607        }
 608}
 609
 610static void send_shallow(struct commit_list *result)
 611{
 612        while (result) {
 613                struct object *object = &result->item->object;
 614                if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
 615                        packet_write_fmt(1, "shallow %s",
 616                                         oid_to_hex(&object->oid));
 617                        register_shallow(the_repository, &object->oid);
 618                        shallow_nr++;
 619                }
 620                result = result->next;
 621        }
 622}
 623
 624static void send_unshallow(const struct object_array *shallows)
 625{
 626        int i;
 627
 628        for (i = 0; i < shallows->nr; i++) {
 629                struct object *object = shallows->objects[i].item;
 630                if (object->flags & NOT_SHALLOW) {
 631                        struct commit_list *parents;
 632                        packet_write_fmt(1, "unshallow %s",
 633                                         oid_to_hex(&object->oid));
 634                        object->flags &= ~CLIENT_SHALLOW;
 635                        /*
 636                         * We want to _register_ "object" as shallow, but we
 637                         * also need to traverse object's parents to deepen a
 638                         * shallow clone. Unregister it for now so we can
 639                         * parse and add the parents to the want list, then
 640                         * re-register it.
 641                         */
 642                        unregister_shallow(&object->oid);
 643                        object->parsed = 0;
 644                        parse_commit_or_die((struct commit *)object);
 645                        parents = ((struct commit *)object)->parents;
 646                        while (parents) {
 647                                add_object_array(&parents->item->object,
 648                                                 NULL, &want_obj);
 649                                parents = parents->next;
 650                        }
 651                        add_object_array(object, NULL, &extra_edge_obj);
 652                }
 653                /* make sure commit traversal conforms to client */
 654                register_shallow(the_repository, &object->oid);
 655        }
 656}
 657
 658static void deepen(int depth, int deepen_relative,
 659                   struct object_array *shallows)
 660{
 661        if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
 662                int i;
 663
 664                for (i = 0; i < shallows->nr; i++) {
 665                        struct object *object = shallows->objects[i].item;
 666                        object->flags |= NOT_SHALLOW;
 667                }
 668        } else if (deepen_relative) {
 669                struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
 670                struct commit_list *result;
 671
 672                get_reachable_list(shallows, &reachable_shallows);
 673                result = get_shallow_commits(&reachable_shallows,
 674                                             depth + 1,
 675                                             SHALLOW, NOT_SHALLOW);
 676                send_shallow(result);
 677                free_commit_list(result);
 678                object_array_clear(&reachable_shallows);
 679        } else {
 680                struct commit_list *result;
 681
 682                result = get_shallow_commits(&want_obj, depth,
 683                                             SHALLOW, NOT_SHALLOW);
 684                send_shallow(result);
 685                free_commit_list(result);
 686        }
 687
 688        send_unshallow(shallows);
 689}
 690
 691static void deepen_by_rev_list(int ac, const char **av,
 692                               struct object_array *shallows)
 693{
 694        struct commit_list *result;
 695
 696        close_commit_graph(the_repository);
 697        result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
 698        send_shallow(result);
 699        free_commit_list(result);
 700        send_unshallow(shallows);
 701}
 702
 703/* Returns 1 if a shallow list is sent or 0 otherwise */
 704static int send_shallow_list(int depth, int deepen_rev_list,
 705                             timestamp_t deepen_since,
 706                             struct string_list *deepen_not,
 707                             struct object_array *shallows)
 708{
 709        int ret = 0;
 710
 711        if (depth > 0 && deepen_rev_list)
 712                die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
 713        if (depth > 0) {
 714                deepen(depth, deepen_relative, shallows);
 715                ret = 1;
 716        } else if (deepen_rev_list) {
 717                struct argv_array av = ARGV_ARRAY_INIT;
 718                int i;
 719
 720                argv_array_push(&av, "rev-list");
 721                if (deepen_since)
 722                        argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
 723                if (deepen_not->nr) {
 724                        argv_array_push(&av, "--not");
 725                        for (i = 0; i < deepen_not->nr; i++) {
 726                                struct string_list_item *s = deepen_not->items + i;
 727                                argv_array_push(&av, s->string);
 728                        }
 729                        argv_array_push(&av, "--not");
 730                }
 731                for (i = 0; i < want_obj.nr; i++) {
 732                        struct object *o = want_obj.objects[i].item;
 733                        argv_array_push(&av, oid_to_hex(&o->oid));
 734                }
 735                deepen_by_rev_list(av.argc, av.argv, shallows);
 736                argv_array_clear(&av);
 737                ret = 1;
 738        } else {
 739                if (shallows->nr > 0) {
 740                        int i;
 741                        for (i = 0; i < shallows->nr; i++)
 742                                register_shallow(the_repository,
 743                                                 &shallows->objects[i].item->oid);
 744                }
 745        }
 746
 747        shallow_nr += shallows->nr;
 748        return ret;
 749}
 750
 751static int process_shallow(const char *line, struct object_array *shallows)
 752{
 753        const char *arg;
 754        if (skip_prefix(line, "shallow ", &arg)) {
 755                struct object_id oid;
 756                struct object *object;
 757                if (get_oid_hex(arg, &oid))
 758                        die("invalid shallow line: %s", line);
 759                object = parse_object(the_repository, &oid);
 760                if (!object)
 761                        return 1;
 762                if (object->type != OBJ_COMMIT)
 763                        die("invalid shallow object %s", oid_to_hex(&oid));
 764                if (!(object->flags & CLIENT_SHALLOW)) {
 765                        object->flags |= CLIENT_SHALLOW;
 766                        add_object_array(object, NULL, shallows);
 767                }
 768                return 1;
 769        }
 770
 771        return 0;
 772}
 773
 774static int process_deepen(const char *line, int *depth)
 775{
 776        const char *arg;
 777        if (skip_prefix(line, "deepen ", &arg)) {
 778                char *end = NULL;
 779                *depth = (int)strtol(arg, &end, 0);
 780                if (!end || *end || *depth <= 0)
 781                        die("Invalid deepen: %s", line);
 782                return 1;
 783        }
 784
 785        return 0;
 786}
 787
 788static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
 789{
 790        const char *arg;
 791        if (skip_prefix(line, "deepen-since ", &arg)) {
 792                char *end = NULL;
 793                *deepen_since = parse_timestamp(arg, &end, 0);
 794                if (!end || *end || !deepen_since ||
 795                    /* revisions.c's max_age -1 is special */
 796                    *deepen_since == -1)
 797                        die("Invalid deepen-since: %s", line);
 798                *deepen_rev_list = 1;
 799                return 1;
 800        }
 801        return 0;
 802}
 803
 804static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
 805{
 806        const char *arg;
 807        if (skip_prefix(line, "deepen-not ", &arg)) {
 808                char *ref = NULL;
 809                struct object_id oid;
 810                if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
 811                        die("git upload-pack: ambiguous deepen-not: %s", line);
 812                string_list_append(deepen_not, ref);
 813                free(ref);
 814                *deepen_rev_list = 1;
 815                return 1;
 816        }
 817        return 0;
 818}
 819
 820static void receive_needs(void)
 821{
 822        struct object_array shallows = OBJECT_ARRAY_INIT;
 823        struct string_list deepen_not = STRING_LIST_INIT_DUP;
 824        int depth = 0;
 825        int has_non_tip = 0;
 826        timestamp_t deepen_since = 0;
 827        int deepen_rev_list = 0;
 828
 829        shallow_nr = 0;
 830        for (;;) {
 831                struct object *o;
 832                const char *features;
 833                struct object_id oid_buf;
 834                char *line = packet_read_line(0, NULL);
 835                const char *arg;
 836
 837                reset_timeout();
 838                if (!line)
 839                        break;
 840
 841                if (process_shallow(line, &shallows))
 842                        continue;
 843                if (process_deepen(line, &depth))
 844                        continue;
 845                if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
 846                        continue;
 847                if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
 848                        continue;
 849
 850                if (skip_prefix(line, "filter ", &arg)) {
 851                        if (!filter_capability_requested)
 852                                die("git upload-pack: filtering capability not negotiated");
 853                        parse_list_objects_filter(&filter_options, arg);
 854                        continue;
 855                }
 856
 857                if (!skip_prefix(line, "want ", &arg) ||
 858                    parse_oid_hex(arg, &oid_buf, &features))
 859                        die("git upload-pack: protocol error, "
 860                            "expected to get object ID, not '%s'", line);
 861
 862                if (parse_feature_request(features, "deepen-relative"))
 863                        deepen_relative = 1;
 864                if (parse_feature_request(features, "multi_ack_detailed"))
 865                        multi_ack = 2;
 866                else if (parse_feature_request(features, "multi_ack"))
 867                        multi_ack = 1;
 868                if (parse_feature_request(features, "no-done"))
 869                        no_done = 1;
 870                if (parse_feature_request(features, "thin-pack"))
 871                        use_thin_pack = 1;
 872                if (parse_feature_request(features, "ofs-delta"))
 873                        use_ofs_delta = 1;
 874                if (parse_feature_request(features, "side-band-64k"))
 875                        use_sideband = LARGE_PACKET_MAX;
 876                else if (parse_feature_request(features, "side-band"))
 877                        use_sideband = DEFAULT_PACKET_MAX;
 878                if (parse_feature_request(features, "no-progress"))
 879                        no_progress = 1;
 880                if (parse_feature_request(features, "include-tag"))
 881                        use_include_tag = 1;
 882                if (allow_filter && parse_feature_request(features, "filter"))
 883                        filter_capability_requested = 1;
 884
 885                o = parse_object(the_repository, &oid_buf);
 886                if (!o) {
 887                        packet_write_fmt(1,
 888                                         "ERR upload-pack: not our ref %s",
 889                                         oid_to_hex(&oid_buf));
 890                        die("git upload-pack: not our ref %s",
 891                            oid_to_hex(&oid_buf));
 892                }
 893                if (!(o->flags & WANTED)) {
 894                        o->flags |= WANTED;
 895                        if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
 896                              || is_our_ref(o)))
 897                                has_non_tip = 1;
 898                        add_object_array(o, NULL, &want_obj);
 899                }
 900        }
 901
 902        /*
 903         * We have sent all our refs already, and the other end
 904         * should have chosen out of them. When we are operating
 905         * in the stateless RPC mode, however, their choice may
 906         * have been based on the set of older refs advertised
 907         * by another process that handled the initial request.
 908         */
 909        if (has_non_tip)
 910                check_non_tip();
 911
 912        if (!use_sideband && daemon_mode)
 913                no_progress = 1;
 914
 915        if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
 916                return;
 917
 918        if (send_shallow_list(depth, deepen_rev_list, deepen_since,
 919                              &deepen_not, &shallows))
 920                packet_flush(1);
 921        object_array_clear(&shallows);
 922}
 923
 924/* return non-zero if the ref is hidden, otherwise 0 */
 925static int mark_our_ref(const char *refname, const char *refname_full,
 926                        const struct object_id *oid)
 927{
 928        struct object *o = lookup_unknown_object(oid->hash);
 929
 930        if (ref_is_hidden(refname, refname_full)) {
 931                o->flags |= HIDDEN_REF;
 932                return 1;
 933        }
 934        o->flags |= OUR_REF;
 935        return 0;
 936}
 937
 938static int check_ref(const char *refname_full, const struct object_id *oid,
 939                     int flag, void *cb_data)
 940{
 941        const char *refname = strip_namespace(refname_full);
 942
 943        mark_our_ref(refname, refname_full, oid);
 944        return 0;
 945}
 946
 947static void format_symref_info(struct strbuf *buf, struct string_list *symref)
 948{
 949        struct string_list_item *item;
 950
 951        if (!symref->nr)
 952                return;
 953        for_each_string_list_item(item, symref)
 954                strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
 955}
 956
 957static int send_ref(const char *refname, const struct object_id *oid,
 958                    int flag, void *cb_data)
 959{
 960        static const char *capabilities = "multi_ack thin-pack side-band"
 961                " side-band-64k ofs-delta shallow deepen-since deepen-not"
 962                " deepen-relative no-progress include-tag multi_ack_detailed";
 963        const char *refname_nons = strip_namespace(refname);
 964        struct object_id peeled;
 965
 966        if (mark_our_ref(refname_nons, refname, oid))
 967                return 0;
 968
 969        if (capabilities) {
 970                struct strbuf symref_info = STRBUF_INIT;
 971
 972                format_symref_info(&symref_info, cb_data);
 973                packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
 974                             oid_to_hex(oid), refname_nons,
 975                             0, capabilities,
 976                             (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
 977                                     " allow-tip-sha1-in-want" : "",
 978                             (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
 979                                     " allow-reachable-sha1-in-want" : "",
 980                             stateless_rpc ? " no-done" : "",
 981                             symref_info.buf,
 982                             allow_filter ? " filter" : "",
 983                             git_user_agent_sanitized());
 984                strbuf_release(&symref_info);
 985        } else {
 986                packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
 987        }
 988        capabilities = NULL;
 989        if (!peel_ref(refname, &peeled))
 990                packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
 991        return 0;
 992}
 993
 994static int find_symref(const char *refname, const struct object_id *oid,
 995                       int flag, void *cb_data)
 996{
 997        const char *symref_target;
 998        struct string_list_item *item;
 999
1000        if ((flag & REF_ISSYMREF) == 0)
1001                return 0;
1002        symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1003        if (!symref_target || (flag & REF_ISSYMREF) == 0)
1004                die("'%s' is a symref but it is not?", refname);
1005        item = string_list_append(cb_data, refname);
1006        item->util = xstrdup(symref_target);
1007        return 0;
1008}
1009
1010static int upload_pack_config(const char *var, const char *value, void *unused)
1011{
1012        if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1013                if (git_config_bool(var, value))
1014                        allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1015                else
1016                        allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1017        } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1018                if (git_config_bool(var, value))
1019                        allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1020                else
1021                        allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1022        } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1023                if (git_config_bool(var, value))
1024                        allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1025                else
1026                        allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1027        } else if (!strcmp("uploadpack.keepalive", var)) {
1028                keepalive = git_config_int(var, value);
1029                if (!keepalive)
1030                        keepalive = -1;
1031        } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1032                if (!strcmp("uploadpack.packobjectshook", var))
1033                        return git_config_string(&pack_objects_hook, var, value);
1034        } else if (!strcmp("uploadpack.allowfilter", var)) {
1035                allow_filter = git_config_bool(var, value);
1036        } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1037                allow_ref_in_want = git_config_bool(var, value);
1038        }
1039        return parse_hide_refs_config(var, value, "uploadpack");
1040}
1041
1042void upload_pack(struct upload_pack_options *options)
1043{
1044        struct string_list symref = STRING_LIST_INIT_DUP;
1045
1046        stateless_rpc = options->stateless_rpc;
1047        timeout = options->timeout;
1048        daemon_mode = options->daemon_mode;
1049
1050        git_config(upload_pack_config, NULL);
1051
1052        head_ref_namespaced(find_symref, &symref);
1053
1054        if (options->advertise_refs || !stateless_rpc) {
1055                reset_timeout();
1056                head_ref_namespaced(send_ref, &symref);
1057                for_each_namespaced_ref(send_ref, &symref);
1058                advertise_shallow_grafts(1);
1059                packet_flush(1);
1060        } else {
1061                head_ref_namespaced(check_ref, NULL);
1062                for_each_namespaced_ref(check_ref, NULL);
1063        }
1064        string_list_clear(&symref, 1);
1065        if (options->advertise_refs)
1066                return;
1067
1068        receive_needs();
1069        if (want_obj.nr) {
1070                get_common_commits();
1071                create_pack_file();
1072        }
1073}
1074
1075struct upload_pack_data {
1076        struct object_array wants;
1077        struct string_list wanted_refs;
1078        struct oid_array haves;
1079
1080        struct object_array shallows;
1081        struct string_list deepen_not;
1082        int depth;
1083        timestamp_t deepen_since;
1084        int deepen_rev_list;
1085        int deepen_relative;
1086
1087        unsigned stateless_rpc : 1;
1088
1089        unsigned use_thin_pack : 1;
1090        unsigned use_ofs_delta : 1;
1091        unsigned no_progress : 1;
1092        unsigned use_include_tag : 1;
1093        unsigned done : 1;
1094};
1095
1096static void upload_pack_data_init(struct upload_pack_data *data)
1097{
1098        struct object_array wants = OBJECT_ARRAY_INIT;
1099        struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1100        struct oid_array haves = OID_ARRAY_INIT;
1101        struct object_array shallows = OBJECT_ARRAY_INIT;
1102        struct string_list deepen_not = STRING_LIST_INIT_DUP;
1103
1104        memset(data, 0, sizeof(*data));
1105        data->wants = wants;
1106        data->wanted_refs = wanted_refs;
1107        data->haves = haves;
1108        data->shallows = shallows;
1109        data->deepen_not = deepen_not;
1110}
1111
1112static void upload_pack_data_clear(struct upload_pack_data *data)
1113{
1114        object_array_clear(&data->wants);
1115        string_list_clear(&data->wanted_refs, 1);
1116        oid_array_clear(&data->haves);
1117        object_array_clear(&data->shallows);
1118        string_list_clear(&data->deepen_not, 0);
1119}
1120
1121static int parse_want(const char *line)
1122{
1123        const char *arg;
1124        if (skip_prefix(line, "want ", &arg)) {
1125                struct object_id oid;
1126                struct object *o;
1127
1128                if (get_oid_hex(arg, &oid))
1129                        die("git upload-pack: protocol error, "
1130                            "expected to get oid, not '%s'", line);
1131
1132                o = parse_object(the_repository, &oid);
1133                if (!o) {
1134                        packet_write_fmt(1,
1135                                         "ERR upload-pack: not our ref %s",
1136                                         oid_to_hex(&oid));
1137                        die("git upload-pack: not our ref %s",
1138                            oid_to_hex(&oid));
1139                }
1140
1141                if (!(o->flags & WANTED)) {
1142                        o->flags |= WANTED;
1143                        add_object_array(o, NULL, &want_obj);
1144                }
1145
1146                return 1;
1147        }
1148
1149        return 0;
1150}
1151
1152static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1153{
1154        const char *arg;
1155        if (skip_prefix(line, "want-ref ", &arg)) {
1156                struct object_id oid;
1157                struct string_list_item *item;
1158                struct object *o;
1159
1160                if (read_ref(arg, &oid)) {
1161                        packet_write_fmt(1, "ERR unknown ref %s", arg);
1162                        die("unknown ref %s", arg);
1163                }
1164
1165                item = string_list_append(wanted_refs, arg);
1166                item->util = oiddup(&oid);
1167
1168                o = parse_object_or_die(&oid, arg);
1169                if (!(o->flags & WANTED)) {
1170                        o->flags |= WANTED;
1171                        add_object_array(o, NULL, &want_obj);
1172                }
1173
1174                return 1;
1175        }
1176
1177        return 0;
1178}
1179
1180static int parse_have(const char *line, struct oid_array *haves)
1181{
1182        const char *arg;
1183        if (skip_prefix(line, "have ", &arg)) {
1184                struct object_id oid;
1185
1186                if (get_oid_hex(arg, &oid))
1187                        die("git upload-pack: expected SHA1 object, got '%s'", arg);
1188                oid_array_append(haves, &oid);
1189                return 1;
1190        }
1191
1192        return 0;
1193}
1194
1195static void process_args(struct packet_reader *request,
1196                         struct upload_pack_data *data)
1197{
1198        while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1199                const char *arg = request->line;
1200                const char *p;
1201
1202                /* process want */
1203                if (parse_want(arg))
1204                        continue;
1205                if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1206                        continue;
1207                /* process have line */
1208                if (parse_have(arg, &data->haves))
1209                        continue;
1210
1211                /* process args like thin-pack */
1212                if (!strcmp(arg, "thin-pack")) {
1213                        use_thin_pack = 1;
1214                        continue;
1215                }
1216                if (!strcmp(arg, "ofs-delta")) {
1217                        use_ofs_delta = 1;
1218                        continue;
1219                }
1220                if (!strcmp(arg, "no-progress")) {
1221                        no_progress = 1;
1222                        continue;
1223                }
1224                if (!strcmp(arg, "include-tag")) {
1225                        use_include_tag = 1;
1226                        continue;
1227                }
1228                if (!strcmp(arg, "done")) {
1229                        data->done = 1;
1230                        continue;
1231                }
1232
1233                /* Shallow related arguments */
1234                if (process_shallow(arg, &data->shallows))
1235                        continue;
1236                if (process_deepen(arg, &data->depth))
1237                        continue;
1238                if (process_deepen_since(arg, &data->deepen_since,
1239                                         &data->deepen_rev_list))
1240                        continue;
1241                if (process_deepen_not(arg, &data->deepen_not,
1242                                       &data->deepen_rev_list))
1243                        continue;
1244                if (!strcmp(arg, "deepen-relative")) {
1245                        data->deepen_relative = 1;
1246                        continue;
1247                }
1248
1249                if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1250                        parse_list_objects_filter(&filter_options, p);
1251                        continue;
1252                }
1253
1254                /* ignore unknown lines maybe? */
1255                die("unexpected line: '%s'", arg);
1256        }
1257}
1258
1259static int process_haves(struct oid_array *haves, struct oid_array *common)
1260{
1261        int i;
1262
1263        /* Process haves */
1264        for (i = 0; i < haves->nr; i++) {
1265                const struct object_id *oid = &haves->oid[i];
1266                struct object *o;
1267                int we_knew_they_have = 0;
1268
1269                if (!has_object_file(oid))
1270                        continue;
1271
1272                oid_array_append(common, oid);
1273
1274                o = parse_object(the_repository, oid);
1275                if (!o)
1276                        die("oops (%s)", oid_to_hex(oid));
1277                if (o->type == OBJ_COMMIT) {
1278                        struct commit_list *parents;
1279                        struct commit *commit = (struct commit *)o;
1280                        if (o->flags & THEY_HAVE)
1281                                we_knew_they_have = 1;
1282                        else
1283                                o->flags |= THEY_HAVE;
1284                        if (!oldest_have || (commit->date < oldest_have))
1285                                oldest_have = commit->date;
1286                        for (parents = commit->parents;
1287                             parents;
1288                             parents = parents->next)
1289                                parents->item->object.flags |= THEY_HAVE;
1290                }
1291                if (!we_knew_they_have)
1292                        add_object_array(o, NULL, &have_obj);
1293        }
1294
1295        return 0;
1296}
1297
1298static int send_acks(struct oid_array *acks, struct strbuf *response)
1299{
1300        int i;
1301
1302        packet_buf_write(response, "acknowledgments\n");
1303
1304        /* Send Acks */
1305        if (!acks->nr)
1306                packet_buf_write(response, "NAK\n");
1307
1308        for (i = 0; i < acks->nr; i++) {
1309                packet_buf_write(response, "ACK %s\n",
1310                                 oid_to_hex(&acks->oid[i]));
1311        }
1312
1313        if (ok_to_give_up()) {
1314                /* Send Ready */
1315                packet_buf_write(response, "ready\n");
1316                return 1;
1317        }
1318
1319        return 0;
1320}
1321
1322static int process_haves_and_send_acks(struct upload_pack_data *data)
1323{
1324        struct oid_array common = OID_ARRAY_INIT;
1325        struct strbuf response = STRBUF_INIT;
1326        int ret = 0;
1327
1328        process_haves(&data->haves, &common);
1329        if (data->done) {
1330                ret = 1;
1331        } else if (send_acks(&common, &response)) {
1332                packet_buf_delim(&response);
1333                ret = 1;
1334        } else {
1335                /* Add Flush */
1336                packet_buf_flush(&response);
1337                ret = 0;
1338        }
1339
1340        /* Send response */
1341        write_or_die(1, response.buf, response.len);
1342        strbuf_release(&response);
1343
1344        oid_array_clear(&data->haves);
1345        oid_array_clear(&common);
1346        return ret;
1347}
1348
1349static void send_wanted_ref_info(struct upload_pack_data *data)
1350{
1351        const struct string_list_item *item;
1352
1353        if (!data->wanted_refs.nr)
1354                return;
1355
1356        packet_write_fmt(1, "wanted-refs\n");
1357
1358        for_each_string_list_item(item, &data->wanted_refs) {
1359                packet_write_fmt(1, "%s %s\n",
1360                                 oid_to_hex(item->util),
1361                                 item->string);
1362        }
1363
1364        packet_delim(1);
1365}
1366
1367static void send_shallow_info(struct upload_pack_data *data)
1368{
1369        /* No shallow info needs to be sent */
1370        if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1371            !is_repository_shallow(the_repository))
1372                return;
1373
1374        packet_write_fmt(1, "shallow-info\n");
1375
1376        if (!send_shallow_list(data->depth, data->deepen_rev_list,
1377                               data->deepen_since, &data->deepen_not,
1378                               &data->shallows) &&
1379            is_repository_shallow(the_repository))
1380                deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1381
1382        packet_delim(1);
1383}
1384
1385enum fetch_state {
1386        FETCH_PROCESS_ARGS = 0,
1387        FETCH_SEND_ACKS,
1388        FETCH_SEND_PACK,
1389        FETCH_DONE,
1390};
1391
1392int upload_pack_v2(struct repository *r, struct argv_array *keys,
1393                   struct packet_reader *request)
1394{
1395        enum fetch_state state = FETCH_PROCESS_ARGS;
1396        struct upload_pack_data data;
1397
1398        git_config(upload_pack_config, NULL);
1399
1400        upload_pack_data_init(&data);
1401        use_sideband = LARGE_PACKET_MAX;
1402
1403        while (state != FETCH_DONE) {
1404                switch (state) {
1405                case FETCH_PROCESS_ARGS:
1406                        process_args(request, &data);
1407
1408                        if (!want_obj.nr) {
1409                                /*
1410                                 * Request didn't contain any 'want' lines,
1411                                 * guess they didn't want anything.
1412                                 */
1413                                state = FETCH_DONE;
1414                        } else if (data.haves.nr) {
1415                                /*
1416                                 * Request had 'have' lines, so lets ACK them.
1417                                 */
1418                                state = FETCH_SEND_ACKS;
1419                        } else {
1420                                /*
1421                                 * Request had 'want's but no 'have's so we can
1422                                 * immedietly go to construct and send a pack.
1423                                 */
1424                                state = FETCH_SEND_PACK;
1425                        }
1426                        break;
1427                case FETCH_SEND_ACKS:
1428                        if (process_haves_and_send_acks(&data))
1429                                state = FETCH_SEND_PACK;
1430                        else
1431                                state = FETCH_DONE;
1432                        break;
1433                case FETCH_SEND_PACK:
1434                        send_wanted_ref_info(&data);
1435                        send_shallow_info(&data);
1436
1437                        packet_write_fmt(1, "packfile\n");
1438                        create_pack_file();
1439                        state = FETCH_DONE;
1440                        break;
1441                case FETCH_DONE:
1442                        continue;
1443                }
1444        }
1445
1446        upload_pack_data_clear(&data);
1447        return 0;
1448}
1449
1450int upload_pack_advertise(struct repository *r,
1451                          struct strbuf *value)
1452{
1453        if (value) {
1454                int allow_filter_value;
1455                int allow_ref_in_want;
1456
1457                strbuf_addstr(value, "shallow");
1458
1459                if (!repo_config_get_bool(the_repository,
1460                                         "uploadpack.allowfilter",
1461                                         &allow_filter_value) &&
1462                    allow_filter_value)
1463                        strbuf_addstr(value, " filter");
1464
1465                if (!repo_config_get_bool(the_repository,
1466                                         "uploadpack.allowrefinwant",
1467                                         &allow_ref_in_want) &&
1468                    allow_ref_in_want)
1469                        strbuf_addstr(value, " ref-in-want");
1470        }
1471
1472        return 1;
1473}