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