upload-pack.con commit list-objects-filter-options: make filter_spec a string_list (cf9ceb5)
   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 int allow_sideband_all;
  74
  75static void reset_timeout(void)
  76{
  77        alarm(timeout);
  78}
  79
  80static void send_client_data(int fd, const char *data, ssize_t sz)
  81{
  82        if (use_sideband) {
  83                send_sideband(1, fd, data, sz, use_sideband);
  84                return;
  85        }
  86        if (fd == 3)
  87                /* emergency quit */
  88                fd = 2;
  89        if (fd == 2) {
  90                /* XXX: are we happy to lose stuff here? */
  91                xwrite(fd, data, sz);
  92                return;
  93        }
  94        write_or_die(fd, data, sz);
  95}
  96
  97static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
  98{
  99        FILE *fp = cb_data;
 100        if (graft->nr_parent == -1)
 101                fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
 102        return 0;
 103}
 104
 105static void create_pack_file(const struct object_array *have_obj,
 106                             const struct object_array *want_obj)
 107{
 108        struct child_process pack_objects = CHILD_PROCESS_INIT;
 109        char data[8193], progress[128];
 110        char abort_msg[] = "aborting due to possible repository "
 111                "corruption on the remote side.";
 112        int buffered = -1;
 113        ssize_t sz;
 114        int i;
 115        FILE *pipe_fd;
 116
 117        if (!pack_objects_hook)
 118                pack_objects.git_cmd = 1;
 119        else {
 120                argv_array_push(&pack_objects.args, pack_objects_hook);
 121                argv_array_push(&pack_objects.args, "git");
 122                pack_objects.use_shell = 1;
 123        }
 124
 125        if (shallow_nr) {
 126                argv_array_push(&pack_objects.args, "--shallow-file");
 127                argv_array_push(&pack_objects.args, "");
 128        }
 129        argv_array_push(&pack_objects.args, "pack-objects");
 130        argv_array_push(&pack_objects.args, "--revs");
 131        if (use_thin_pack)
 132                argv_array_push(&pack_objects.args, "--thin");
 133
 134        argv_array_push(&pack_objects.args, "--stdout");
 135        if (shallow_nr)
 136                argv_array_push(&pack_objects.args, "--shallow");
 137        if (!no_progress)
 138                argv_array_push(&pack_objects.args, "--progress");
 139        if (use_ofs_delta)
 140                argv_array_push(&pack_objects.args, "--delta-base-offset");
 141        if (use_include_tag)
 142                argv_array_push(&pack_objects.args, "--include-tag");
 143        if (filter_options.choice) {
 144                const char *spec =
 145                        expand_list_objects_filter_spec(&filter_options);
 146                if (pack_objects.use_shell) {
 147                        struct strbuf buf = STRBUF_INIT;
 148                        sq_quote_buf(&buf, spec);
 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                                         spec);
 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 packet_reader *reader,
 361                              struct object_array *have_obj,
 362                              struct object_array *want_obj)
 363{
 364        struct object_id oid;
 365        char last_hex[GIT_MAX_HEXSZ + 1];
 366        int got_common = 0;
 367        int got_other = 0;
 368        int sent_ready = 0;
 369
 370        save_commit_buffer = 0;
 371
 372        for (;;) {
 373                const char *arg;
 374
 375                reset_timeout();
 376
 377                if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
 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(reader->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(reader->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'", reader->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                          struct packet_writer *writer)
 596{
 597        int i;
 598
 599        /*
 600         * In the normal in-process case without
 601         * uploadpack.allowReachableSHA1InWant,
 602         * non-tip requests can never happen.
 603         */
 604        if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
 605                goto error;
 606        if (!has_unreachable(want_obj))
 607                /* All the non-tip ones are ancestors of what we advertised */
 608                return;
 609
 610error:
 611        /* Pick one of them (we know there at least is one) */
 612        for (i = 0; i < want_obj->nr; i++) {
 613                struct object *o = want_obj->objects[i].item;
 614                if (!is_our_ref(o)) {
 615                        packet_writer_error(writer,
 616                                            "upload-pack: not our ref %s",
 617                                            oid_to_hex(&o->oid));
 618                        die("git upload-pack: not our ref %s",
 619                            oid_to_hex(&o->oid));
 620                }
 621        }
 622}
 623
 624static void send_shallow(struct packet_writer *writer,
 625                         struct commit_list *result)
 626{
 627        while (result) {
 628                struct object *object = &result->item->object;
 629                if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
 630                        packet_writer_write(writer, "shallow %s",
 631                                            oid_to_hex(&object->oid));
 632                        register_shallow(the_repository, &object->oid);
 633                        shallow_nr++;
 634                }
 635                result = result->next;
 636        }
 637}
 638
 639static void send_unshallow(struct packet_writer *writer,
 640                           const struct object_array *shallows,
 641                           struct object_array *want_obj)
 642{
 643        int i;
 644
 645        for (i = 0; i < shallows->nr; i++) {
 646                struct object *object = shallows->objects[i].item;
 647                if (object->flags & NOT_SHALLOW) {
 648                        struct commit_list *parents;
 649                        packet_writer_write(writer, "unshallow %s",
 650                                            oid_to_hex(&object->oid));
 651                        object->flags &= ~CLIENT_SHALLOW;
 652                        /*
 653                         * We want to _register_ "object" as shallow, but we
 654                         * also need to traverse object's parents to deepen a
 655                         * shallow clone. Unregister it for now so we can
 656                         * parse and add the parents to the want list, then
 657                         * re-register it.
 658                         */
 659                        unregister_shallow(&object->oid);
 660                        object->parsed = 0;
 661                        parse_commit_or_die((struct commit *)object);
 662                        parents = ((struct commit *)object)->parents;
 663                        while (parents) {
 664                                add_object_array(&parents->item->object,
 665                                                 NULL, want_obj);
 666                                parents = parents->next;
 667                        }
 668                        add_object_array(object, NULL, &extra_edge_obj);
 669                }
 670                /* make sure commit traversal conforms to client */
 671                register_shallow(the_repository, &object->oid);
 672        }
 673}
 674
 675static int check_ref(const char *refname_full, const struct object_id *oid,
 676                     int flag, void *cb_data);
 677static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
 678                   struct object_array *shallows, struct object_array *want_obj)
 679{
 680        if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
 681                int i;
 682
 683                for (i = 0; i < shallows->nr; i++) {
 684                        struct object *object = shallows->objects[i].item;
 685                        object->flags |= NOT_SHALLOW;
 686                }
 687        } else if (deepen_relative) {
 688                struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
 689                struct commit_list *result;
 690
 691                /*
 692                 * Checking for reachable shallows requires that our refs be
 693                 * marked with OUR_REF.
 694                 */
 695                head_ref_namespaced(check_ref, NULL);
 696                for_each_namespaced_ref(check_ref, NULL);
 697
 698                get_reachable_list(shallows, &reachable_shallows);
 699                result = get_shallow_commits(&reachable_shallows,
 700                                             depth + 1,
 701                                             SHALLOW, NOT_SHALLOW);
 702                send_shallow(writer, result);
 703                free_commit_list(result);
 704                object_array_clear(&reachable_shallows);
 705        } else {
 706                struct commit_list *result;
 707
 708                result = get_shallow_commits(want_obj, depth,
 709                                             SHALLOW, NOT_SHALLOW);
 710                send_shallow(writer, result);
 711                free_commit_list(result);
 712        }
 713
 714        send_unshallow(writer, shallows, want_obj);
 715}
 716
 717static void deepen_by_rev_list(struct packet_writer *writer, int ac,
 718                               const char **av,
 719                               struct object_array *shallows,
 720                               struct object_array *want_obj)
 721{
 722        struct commit_list *result;
 723
 724        close_commit_graph(the_repository);
 725        result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
 726        send_shallow(writer, result);
 727        free_commit_list(result);
 728        send_unshallow(writer, shallows, want_obj);
 729}
 730
 731/* Returns 1 if a shallow list is sent or 0 otherwise */
 732static int send_shallow_list(struct packet_writer *writer,
 733                             int depth, int deepen_rev_list,
 734                             timestamp_t deepen_since,
 735                             struct string_list *deepen_not,
 736                             int deepen_relative,
 737                             struct object_array *shallows,
 738                             struct object_array *want_obj)
 739{
 740        int ret = 0;
 741
 742        if (depth > 0 && deepen_rev_list)
 743                die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
 744        if (depth > 0) {
 745                deepen(writer, depth, deepen_relative, shallows, want_obj);
 746                ret = 1;
 747        } else if (deepen_rev_list) {
 748                struct argv_array av = ARGV_ARRAY_INIT;
 749                int i;
 750
 751                argv_array_push(&av, "rev-list");
 752                if (deepen_since)
 753                        argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
 754                if (deepen_not->nr) {
 755                        argv_array_push(&av, "--not");
 756                        for (i = 0; i < deepen_not->nr; i++) {
 757                                struct string_list_item *s = deepen_not->items + i;
 758                                argv_array_push(&av, s->string);
 759                        }
 760                        argv_array_push(&av, "--not");
 761                }
 762                for (i = 0; i < want_obj->nr; i++) {
 763                        struct object *o = want_obj->objects[i].item;
 764                        argv_array_push(&av, oid_to_hex(&o->oid));
 765                }
 766                deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
 767                argv_array_clear(&av);
 768                ret = 1;
 769        } else {
 770                if (shallows->nr > 0) {
 771                        int i;
 772                        for (i = 0; i < shallows->nr; i++)
 773                                register_shallow(the_repository,
 774                                                 &shallows->objects[i].item->oid);
 775                }
 776        }
 777
 778        shallow_nr += shallows->nr;
 779        return ret;
 780}
 781
 782static int process_shallow(const char *line, struct object_array *shallows)
 783{
 784        const char *arg;
 785        if (skip_prefix(line, "shallow ", &arg)) {
 786                struct object_id oid;
 787                struct object *object;
 788                if (get_oid_hex(arg, &oid))
 789                        die("invalid shallow line: %s", line);
 790                object = parse_object(the_repository, &oid);
 791                if (!object)
 792                        return 1;
 793                if (object->type != OBJ_COMMIT)
 794                        die("invalid shallow object %s", oid_to_hex(&oid));
 795                if (!(object->flags & CLIENT_SHALLOW)) {
 796                        object->flags |= CLIENT_SHALLOW;
 797                        add_object_array(object, NULL, shallows);
 798                }
 799                return 1;
 800        }
 801
 802        return 0;
 803}
 804
 805static int process_deepen(const char *line, int *depth)
 806{
 807        const char *arg;
 808        if (skip_prefix(line, "deepen ", &arg)) {
 809                char *end = NULL;
 810                *depth = (int)strtol(arg, &end, 0);
 811                if (!end || *end || *depth <= 0)
 812                        die("Invalid deepen: %s", line);
 813                return 1;
 814        }
 815
 816        return 0;
 817}
 818
 819static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
 820{
 821        const char *arg;
 822        if (skip_prefix(line, "deepen-since ", &arg)) {
 823                char *end = NULL;
 824                *deepen_since = parse_timestamp(arg, &end, 0);
 825                if (!end || *end || !deepen_since ||
 826                    /* revisions.c's max_age -1 is special */
 827                    *deepen_since == -1)
 828                        die("Invalid deepen-since: %s", line);
 829                *deepen_rev_list = 1;
 830                return 1;
 831        }
 832        return 0;
 833}
 834
 835static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
 836{
 837        const char *arg;
 838        if (skip_prefix(line, "deepen-not ", &arg)) {
 839                char *ref = NULL;
 840                struct object_id oid;
 841                if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
 842                        die("git upload-pack: ambiguous deepen-not: %s", line);
 843                string_list_append(deepen_not, ref);
 844                free(ref);
 845                *deepen_rev_list = 1;
 846                return 1;
 847        }
 848        return 0;
 849}
 850
 851static void receive_needs(struct packet_reader *reader, struct object_array *want_obj)
 852{
 853        struct object_array shallows = OBJECT_ARRAY_INIT;
 854        struct string_list deepen_not = STRING_LIST_INIT_DUP;
 855        int depth = 0;
 856        int has_non_tip = 0;
 857        timestamp_t deepen_since = 0;
 858        int deepen_rev_list = 0;
 859        int deepen_relative = 0;
 860        struct packet_writer writer;
 861
 862        shallow_nr = 0;
 863        packet_writer_init(&writer, 1);
 864        for (;;) {
 865                struct object *o;
 866                const char *features;
 867                struct object_id oid_buf;
 868                const char *arg;
 869
 870                reset_timeout();
 871                if (packet_reader_read(reader) != PACKET_READ_NORMAL)
 872                        break;
 873
 874                if (process_shallow(reader->line, &shallows))
 875                        continue;
 876                if (process_deepen(reader->line, &depth))
 877                        continue;
 878                if (process_deepen_since(reader->line, &deepen_since, &deepen_rev_list))
 879                        continue;
 880                if (process_deepen_not(reader->line, &deepen_not, &deepen_rev_list))
 881                        continue;
 882
 883                if (skip_prefix(reader->line, "filter ", &arg)) {
 884                        if (!filter_capability_requested)
 885                                die("git upload-pack: filtering capability not negotiated");
 886                        parse_list_objects_filter(&filter_options, arg);
 887                        continue;
 888                }
 889
 890                if (!skip_prefix(reader->line, "want ", &arg) ||
 891                    parse_oid_hex(arg, &oid_buf, &features))
 892                        die("git upload-pack: protocol error, "
 893                            "expected to get object ID, not '%s'", reader->line);
 894
 895                if (parse_feature_request(features, "deepen-relative"))
 896                        deepen_relative = 1;
 897                if (parse_feature_request(features, "multi_ack_detailed"))
 898                        multi_ack = 2;
 899                else if (parse_feature_request(features, "multi_ack"))
 900                        multi_ack = 1;
 901                if (parse_feature_request(features, "no-done"))
 902                        no_done = 1;
 903                if (parse_feature_request(features, "thin-pack"))
 904                        use_thin_pack = 1;
 905                if (parse_feature_request(features, "ofs-delta"))
 906                        use_ofs_delta = 1;
 907                if (parse_feature_request(features, "side-band-64k"))
 908                        use_sideband = LARGE_PACKET_MAX;
 909                else if (parse_feature_request(features, "side-band"))
 910                        use_sideband = DEFAULT_PACKET_MAX;
 911                if (parse_feature_request(features, "no-progress"))
 912                        no_progress = 1;
 913                if (parse_feature_request(features, "include-tag"))
 914                        use_include_tag = 1;
 915                if (allow_filter && parse_feature_request(features, "filter"))
 916                        filter_capability_requested = 1;
 917
 918                o = parse_object(the_repository, &oid_buf);
 919                if (!o) {
 920                        packet_writer_error(&writer,
 921                                            "upload-pack: not our ref %s",
 922                                            oid_to_hex(&oid_buf));
 923                        die("git upload-pack: not our ref %s",
 924                            oid_to_hex(&oid_buf));
 925                }
 926                if (!(o->flags & WANTED)) {
 927                        o->flags |= WANTED;
 928                        if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
 929                              || is_our_ref(o)))
 930                                has_non_tip = 1;
 931                        add_object_array(o, NULL, want_obj);
 932                }
 933        }
 934
 935        /*
 936         * We have sent all our refs already, and the other end
 937         * should have chosen out of them. When we are operating
 938         * in the stateless RPC mode, however, their choice may
 939         * have been based on the set of older refs advertised
 940         * by another process that handled the initial request.
 941         */
 942        if (has_non_tip)
 943                check_non_tip(want_obj, &writer);
 944
 945        if (!use_sideband && daemon_mode)
 946                no_progress = 1;
 947
 948        if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
 949                return;
 950
 951        if (send_shallow_list(&writer, depth, deepen_rev_list, deepen_since,
 952                              &deepen_not, deepen_relative, &shallows,
 953                              want_obj))
 954                packet_flush(1);
 955        object_array_clear(&shallows);
 956}
 957
 958/* return non-zero if the ref is hidden, otherwise 0 */
 959static int mark_our_ref(const char *refname, const char *refname_full,
 960                        const struct object_id *oid)
 961{
 962        struct object *o = lookup_unknown_object(oid->hash);
 963
 964        if (ref_is_hidden(refname, refname_full)) {
 965                o->flags |= HIDDEN_REF;
 966                return 1;
 967        }
 968        o->flags |= OUR_REF;
 969        return 0;
 970}
 971
 972static int check_ref(const char *refname_full, const struct object_id *oid,
 973                     int flag, void *cb_data)
 974{
 975        const char *refname = strip_namespace(refname_full);
 976
 977        mark_our_ref(refname, refname_full, oid);
 978        return 0;
 979}
 980
 981static void format_symref_info(struct strbuf *buf, struct string_list *symref)
 982{
 983        struct string_list_item *item;
 984
 985        if (!symref->nr)
 986                return;
 987        for_each_string_list_item(item, symref)
 988                strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
 989}
 990
 991static int send_ref(const char *refname, const struct object_id *oid,
 992                    int flag, void *cb_data)
 993{
 994        static const char *capabilities = "multi_ack thin-pack side-band"
 995                " side-band-64k ofs-delta shallow deepen-since deepen-not"
 996                " deepen-relative no-progress include-tag multi_ack_detailed";
 997        const char *refname_nons = strip_namespace(refname);
 998        struct object_id peeled;
 999
1000        if (mark_our_ref(refname_nons, refname, oid))
1001                return 0;
1002
1003        if (capabilities) {
1004                struct strbuf symref_info = STRBUF_INIT;
1005
1006                format_symref_info(&symref_info, cb_data);
1007                packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
1008                             oid_to_hex(oid), refname_nons,
1009                             0, capabilities,
1010                             (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1011                                     " allow-tip-sha1-in-want" : "",
1012                             (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1013                                     " allow-reachable-sha1-in-want" : "",
1014                             stateless_rpc ? " no-done" : "",
1015                             symref_info.buf,
1016                             allow_filter ? " filter" : "",
1017                             git_user_agent_sanitized());
1018                strbuf_release(&symref_info);
1019        } else {
1020                packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
1021        }
1022        capabilities = NULL;
1023        if (!peel_ref(refname, &peeled))
1024                packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
1025        return 0;
1026}
1027
1028static int find_symref(const char *refname, const struct object_id *oid,
1029                       int flag, void *cb_data)
1030{
1031        const char *symref_target;
1032        struct string_list_item *item;
1033
1034        if ((flag & REF_ISSYMREF) == 0)
1035                return 0;
1036        symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
1037        if (!symref_target || (flag & REF_ISSYMREF) == 0)
1038                die("'%s' is a symref but it is not?", refname);
1039        item = string_list_append(cb_data, strip_namespace(refname));
1040        item->util = xstrdup(strip_namespace(symref_target));
1041        return 0;
1042}
1043
1044static int upload_pack_config(const char *var, const char *value, void *unused)
1045{
1046        if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1047                if (git_config_bool(var, value))
1048                        allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1049                else
1050                        allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1051        } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1052                if (git_config_bool(var, value))
1053                        allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1054                else
1055                        allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1056        } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1057                if (git_config_bool(var, value))
1058                        allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1059                else
1060                        allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1061        } else if (!strcmp("uploadpack.keepalive", var)) {
1062                keepalive = git_config_int(var, value);
1063                if (!keepalive)
1064                        keepalive = -1;
1065        } else if (!strcmp("uploadpack.allowfilter", var)) {
1066                allow_filter = git_config_bool(var, value);
1067        } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1068                allow_ref_in_want = git_config_bool(var, value);
1069        } else if (!strcmp("uploadpack.allowsidebandall", var)) {
1070                allow_sideband_all = git_config_bool(var, value);
1071        } else if (!strcmp("core.precomposeunicode", var)) {
1072                precomposed_unicode = git_config_bool(var, value);
1073        }
1074
1075        if (current_config_scope() != CONFIG_SCOPE_REPO) {
1076                if (!strcmp("uploadpack.packobjectshook", var))
1077                        return git_config_string(&pack_objects_hook, var, value);
1078        }
1079
1080        return parse_hide_refs_config(var, value, "uploadpack");
1081}
1082
1083void upload_pack(struct upload_pack_options *options)
1084{
1085        struct string_list symref = STRING_LIST_INIT_DUP;
1086        struct object_array want_obj = OBJECT_ARRAY_INIT;
1087        struct packet_reader reader;
1088
1089        stateless_rpc = options->stateless_rpc;
1090        timeout = options->timeout;
1091        daemon_mode = options->daemon_mode;
1092
1093        git_config(upload_pack_config, NULL);
1094
1095        head_ref_namespaced(find_symref, &symref);
1096
1097        if (options->advertise_refs || !stateless_rpc) {
1098                reset_timeout();
1099                head_ref_namespaced(send_ref, &symref);
1100                for_each_namespaced_ref(send_ref, &symref);
1101                advertise_shallow_grafts(1);
1102                packet_flush(1);
1103        } else {
1104                head_ref_namespaced(check_ref, NULL);
1105                for_each_namespaced_ref(check_ref, NULL);
1106        }
1107        string_list_clear(&symref, 1);
1108        if (options->advertise_refs)
1109                return;
1110
1111        packet_reader_init(&reader, 0, NULL, 0,
1112                           PACKET_READ_CHOMP_NEWLINE |
1113                           PACKET_READ_DIE_ON_ERR_PACKET);
1114
1115        receive_needs(&reader, &want_obj);
1116        if (want_obj.nr) {
1117                struct object_array have_obj = OBJECT_ARRAY_INIT;
1118                get_common_commits(&reader, &have_obj, &want_obj);
1119                create_pack_file(&have_obj, &want_obj);
1120        }
1121}
1122
1123struct upload_pack_data {
1124        struct object_array wants;
1125        struct string_list wanted_refs;
1126        struct oid_array haves;
1127
1128        struct object_array shallows;
1129        struct string_list deepen_not;
1130        int depth;
1131        timestamp_t deepen_since;
1132        int deepen_rev_list;
1133        int deepen_relative;
1134
1135        struct packet_writer writer;
1136
1137        unsigned stateless_rpc : 1;
1138
1139        unsigned use_thin_pack : 1;
1140        unsigned use_ofs_delta : 1;
1141        unsigned no_progress : 1;
1142        unsigned use_include_tag : 1;
1143        unsigned done : 1;
1144};
1145
1146static void upload_pack_data_init(struct upload_pack_data *data)
1147{
1148        struct object_array wants = OBJECT_ARRAY_INIT;
1149        struct string_list wanted_refs = STRING_LIST_INIT_DUP;
1150        struct oid_array haves = OID_ARRAY_INIT;
1151        struct object_array shallows = OBJECT_ARRAY_INIT;
1152        struct string_list deepen_not = STRING_LIST_INIT_DUP;
1153
1154        memset(data, 0, sizeof(*data));
1155        data->wants = wants;
1156        data->wanted_refs = wanted_refs;
1157        data->haves = haves;
1158        data->shallows = shallows;
1159        data->deepen_not = deepen_not;
1160        packet_writer_init(&data->writer, 1);
1161}
1162
1163static void upload_pack_data_clear(struct upload_pack_data *data)
1164{
1165        object_array_clear(&data->wants);
1166        string_list_clear(&data->wanted_refs, 1);
1167        oid_array_clear(&data->haves);
1168        object_array_clear(&data->shallows);
1169        string_list_clear(&data->deepen_not, 0);
1170}
1171
1172static int parse_want(struct packet_writer *writer, const char *line,
1173                      struct object_array *want_obj)
1174{
1175        const char *arg;
1176        if (skip_prefix(line, "want ", &arg)) {
1177                struct object_id oid;
1178                struct object *o;
1179
1180                if (get_oid_hex(arg, &oid))
1181                        die("git upload-pack: protocol error, "
1182                            "expected to get oid, not '%s'", line);
1183
1184                o = parse_object(the_repository, &oid);
1185                if (!o) {
1186                        packet_writer_error(writer,
1187                                            "upload-pack: not our ref %s",
1188                                            oid_to_hex(&oid));
1189                        die("git upload-pack: not our ref %s",
1190                            oid_to_hex(&oid));
1191                }
1192
1193                if (!(o->flags & WANTED)) {
1194                        o->flags |= WANTED;
1195                        add_object_array(o, NULL, want_obj);
1196                }
1197
1198                return 1;
1199        }
1200
1201        return 0;
1202}
1203
1204static int parse_want_ref(struct packet_writer *writer, const char *line,
1205                          struct string_list *wanted_refs,
1206                          struct object_array *want_obj)
1207{
1208        const char *arg;
1209        if (skip_prefix(line, "want-ref ", &arg)) {
1210                struct object_id oid;
1211                struct string_list_item *item;
1212                struct object *o;
1213
1214                if (read_ref(arg, &oid)) {
1215                        packet_writer_error(writer, "unknown ref %s", arg);
1216                        die("unknown ref %s", arg);
1217                }
1218
1219                item = string_list_append(wanted_refs, arg);
1220                item->util = oiddup(&oid);
1221
1222                o = parse_object_or_die(&oid, arg);
1223                if (!(o->flags & WANTED)) {
1224                        o->flags |= WANTED;
1225                        add_object_array(o, NULL, want_obj);
1226                }
1227
1228                return 1;
1229        }
1230
1231        return 0;
1232}
1233
1234static int parse_have(const char *line, struct oid_array *haves)
1235{
1236        const char *arg;
1237        if (skip_prefix(line, "have ", &arg)) {
1238                struct object_id oid;
1239
1240                if (get_oid_hex(arg, &oid))
1241                        die("git upload-pack: expected SHA1 object, got '%s'", arg);
1242                oid_array_append(haves, &oid);
1243                return 1;
1244        }
1245
1246        return 0;
1247}
1248
1249static void process_args(struct packet_reader *request,
1250                         struct upload_pack_data *data,
1251                         struct object_array *want_obj)
1252{
1253        while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1254                const char *arg = request->line;
1255                const char *p;
1256
1257                /* process want */
1258                if (parse_want(&data->writer, arg, want_obj))
1259                        continue;
1260                if (allow_ref_in_want &&
1261                    parse_want_ref(&data->writer, arg, &data->wanted_refs,
1262                                   want_obj))
1263                        continue;
1264                /* process have line */
1265                if (parse_have(arg, &data->haves))
1266                        continue;
1267
1268                /* process args like thin-pack */
1269                if (!strcmp(arg, "thin-pack")) {
1270                        use_thin_pack = 1;
1271                        continue;
1272                }
1273                if (!strcmp(arg, "ofs-delta")) {
1274                        use_ofs_delta = 1;
1275                        continue;
1276                }
1277                if (!strcmp(arg, "no-progress")) {
1278                        no_progress = 1;
1279                        continue;
1280                }
1281                if (!strcmp(arg, "include-tag")) {
1282                        use_include_tag = 1;
1283                        continue;
1284                }
1285                if (!strcmp(arg, "done")) {
1286                        data->done = 1;
1287                        continue;
1288                }
1289
1290                /* Shallow related arguments */
1291                if (process_shallow(arg, &data->shallows))
1292                        continue;
1293                if (process_deepen(arg, &data->depth))
1294                        continue;
1295                if (process_deepen_since(arg, &data->deepen_since,
1296                                         &data->deepen_rev_list))
1297                        continue;
1298                if (process_deepen_not(arg, &data->deepen_not,
1299                                       &data->deepen_rev_list))
1300                        continue;
1301                if (!strcmp(arg, "deepen-relative")) {
1302                        data->deepen_relative = 1;
1303                        continue;
1304                }
1305
1306                if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1307                        parse_list_objects_filter(&filter_options, p);
1308                        continue;
1309                }
1310
1311                if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1312                     allow_sideband_all) &&
1313                    !strcmp(arg, "sideband-all")) {
1314                        data->writer.use_sideband = 1;
1315                        continue;
1316                }
1317
1318                /* ignore unknown lines maybe? */
1319                die("unexpected line: '%s'", arg);
1320        }
1321}
1322
1323static int process_haves(struct oid_array *haves, struct oid_array *common,
1324                         struct object_array *have_obj)
1325{
1326        int i;
1327
1328        /* Process haves */
1329        for (i = 0; i < haves->nr; i++) {
1330                const struct object_id *oid = &haves->oid[i];
1331                struct object *o;
1332                int we_knew_they_have = 0;
1333
1334                if (!has_object_file(oid))
1335                        continue;
1336
1337                oid_array_append(common, oid);
1338
1339                o = parse_object(the_repository, oid);
1340                if (!o)
1341                        die("oops (%s)", oid_to_hex(oid));
1342                if (o->type == OBJ_COMMIT) {
1343                        struct commit_list *parents;
1344                        struct commit *commit = (struct commit *)o;
1345                        if (o->flags & THEY_HAVE)
1346                                we_knew_they_have = 1;
1347                        else
1348                                o->flags |= THEY_HAVE;
1349                        if (!oldest_have || (commit->date < oldest_have))
1350                                oldest_have = commit->date;
1351                        for (parents = commit->parents;
1352                             parents;
1353                             parents = parents->next)
1354                                parents->item->object.flags |= THEY_HAVE;
1355                }
1356                if (!we_knew_they_have)
1357                        add_object_array(o, NULL, have_obj);
1358        }
1359
1360        return 0;
1361}
1362
1363static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1364                     const struct object_array *have_obj,
1365                     struct object_array *want_obj)
1366{
1367        int i;
1368
1369        packet_writer_write(writer, "acknowledgments\n");
1370
1371        /* Send Acks */
1372        if (!acks->nr)
1373                packet_writer_write(writer, "NAK\n");
1374
1375        for (i = 0; i < acks->nr; i++) {
1376                packet_writer_write(writer, "ACK %s\n",
1377                                    oid_to_hex(&acks->oid[i]));
1378        }
1379
1380        if (ok_to_give_up(have_obj, want_obj)) {
1381                /* Send Ready */
1382                packet_writer_write(writer, "ready\n");
1383                return 1;
1384        }
1385
1386        return 0;
1387}
1388
1389static int process_haves_and_send_acks(struct upload_pack_data *data,
1390                                       struct object_array *have_obj,
1391                                       struct object_array *want_obj)
1392{
1393        struct oid_array common = OID_ARRAY_INIT;
1394        int ret = 0;
1395
1396        process_haves(&data->haves, &common, have_obj);
1397        if (data->done) {
1398                ret = 1;
1399        } else if (send_acks(&data->writer, &common, have_obj, want_obj)) {
1400                packet_writer_delim(&data->writer);
1401                ret = 1;
1402        } else {
1403                /* Add Flush */
1404                packet_writer_flush(&data->writer);
1405                ret = 0;
1406        }
1407
1408        oid_array_clear(&data->haves);
1409        oid_array_clear(&common);
1410        return ret;
1411}
1412
1413static void send_wanted_ref_info(struct upload_pack_data *data)
1414{
1415        const struct string_list_item *item;
1416
1417        if (!data->wanted_refs.nr)
1418                return;
1419
1420        packet_writer_write(&data->writer, "wanted-refs\n");
1421
1422        for_each_string_list_item(item, &data->wanted_refs) {
1423                packet_writer_write(&data->writer, "%s %s\n",
1424                                    oid_to_hex(item->util),
1425                                    item->string);
1426        }
1427
1428        packet_writer_delim(&data->writer);
1429}
1430
1431static void send_shallow_info(struct upload_pack_data *data,
1432                              struct object_array *want_obj)
1433{
1434        /* No shallow info needs to be sent */
1435        if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
1436            !is_repository_shallow(the_repository))
1437                return;
1438
1439        packet_writer_write(&data->writer, "shallow-info\n");
1440
1441        if (!send_shallow_list(&data->writer, data->depth,
1442                               data->deepen_rev_list,
1443                               data->deepen_since, &data->deepen_not,
1444                               data->deepen_relative,
1445                               &data->shallows, want_obj) &&
1446            is_repository_shallow(the_repository))
1447                deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1448                       &data->shallows, want_obj);
1449
1450        packet_delim(1);
1451}
1452
1453enum fetch_state {
1454        FETCH_PROCESS_ARGS = 0,
1455        FETCH_SEND_ACKS,
1456        FETCH_SEND_PACK,
1457        FETCH_DONE,
1458};
1459
1460int upload_pack_v2(struct repository *r, struct argv_array *keys,
1461                   struct packet_reader *request)
1462{
1463        enum fetch_state state = FETCH_PROCESS_ARGS;
1464        struct upload_pack_data data;
1465        struct object_array have_obj = OBJECT_ARRAY_INIT;
1466        struct object_array want_obj = OBJECT_ARRAY_INIT;
1467
1468        clear_object_flags(ALL_FLAGS);
1469
1470        git_config(upload_pack_config, NULL);
1471
1472        upload_pack_data_init(&data);
1473        use_sideband = LARGE_PACKET_MAX;
1474
1475        while (state != FETCH_DONE) {
1476                switch (state) {
1477                case FETCH_PROCESS_ARGS:
1478                        process_args(request, &data, &want_obj);
1479
1480                        if (!want_obj.nr) {
1481                                /*
1482                                 * Request didn't contain any 'want' lines,
1483                                 * guess they didn't want anything.
1484                                 */
1485                                state = FETCH_DONE;
1486                        } else if (data.haves.nr) {
1487                                /*
1488                                 * Request had 'have' lines, so lets ACK them.
1489                                 */
1490                                state = FETCH_SEND_ACKS;
1491                        } else {
1492                                /*
1493                                 * Request had 'want's but no 'have's so we can
1494                                 * immedietly go to construct and send a pack.
1495                                 */
1496                                state = FETCH_SEND_PACK;
1497                        }
1498                        break;
1499                case FETCH_SEND_ACKS:
1500                        if (process_haves_and_send_acks(&data, &have_obj,
1501                                                        &want_obj))
1502                                state = FETCH_SEND_PACK;
1503                        else
1504                                state = FETCH_DONE;
1505                        break;
1506                case FETCH_SEND_PACK:
1507                        send_wanted_ref_info(&data);
1508                        send_shallow_info(&data, &want_obj);
1509
1510                        packet_writer_write(&data.writer, "packfile\n");
1511                        create_pack_file(&have_obj, &want_obj);
1512                        state = FETCH_DONE;
1513                        break;
1514                case FETCH_DONE:
1515                        continue;
1516                }
1517        }
1518
1519        upload_pack_data_clear(&data);
1520        object_array_clear(&have_obj);
1521        object_array_clear(&want_obj);
1522        return 0;
1523}
1524
1525int upload_pack_advertise(struct repository *r,
1526                          struct strbuf *value)
1527{
1528        if (value) {
1529                int allow_filter_value;
1530                int allow_ref_in_want;
1531                int allow_sideband_all_value;
1532
1533                strbuf_addstr(value, "shallow");
1534
1535                if (!repo_config_get_bool(the_repository,
1536                                         "uploadpack.allowfilter",
1537                                         &allow_filter_value) &&
1538                    allow_filter_value)
1539                        strbuf_addstr(value, " filter");
1540
1541                if (!repo_config_get_bool(the_repository,
1542                                         "uploadpack.allowrefinwant",
1543                                         &allow_ref_in_want) &&
1544                    allow_ref_in_want)
1545                        strbuf_addstr(value, " ref-in-want");
1546
1547                if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1548                    (!repo_config_get_bool(the_repository,
1549                                           "uploadpack.allowsidebandall",
1550                                           &allow_sideband_all_value) &&
1551                     allow_sideband_all_value))
1552                        strbuf_addstr(value, " sideband-all");
1553        }
1554
1555        return 1;
1556}