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