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