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