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