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