upload-pack.con commit fetch: support filters (acb0c57)
   1#include "cache.h"
   2#include "config.h"
   3#include "refs.h"
   4#include "pkt-line.h"
   5#include "sideband.h"
   6#include "tag.h"
   7#include "object.h"
   8#include "commit.h"
   9#include "exec_cmd.h"
  10#include "diff.h"
  11#include "revision.h"
  12#include "list-objects.h"
  13#include "list-objects-filter.h"
  14#include "list-objects-filter-options.h"
  15#include "run-command.h"
  16#include "connect.h"
  17#include "sigchain.h"
  18#include "version.h"
  19#include "string-list.h"
  20#include "parse-options.h"
  21#include "argv-array.h"
  22#include "prio-queue.h"
  23#include "quote.h"
  24
  25static const char * const upload_pack_usage[] = {
  26        N_("git upload-pack [<options>] <dir>"),
  27        NULL
  28};
  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#define REACHABLE       (1u << 15)
  36
  37#define SHALLOW         (1u << 16)
  38#define NOT_SHALLOW     (1u << 17)
  39#define CLIENT_SHALLOW  (1u << 18)
  40#define HIDDEN_REF      (1u << 19)
  41
  42static timestamp_t oldest_have;
  43
  44static int deepen_relative;
  45static int multi_ack;
  46static int no_done;
  47static int use_thin_pack, use_ofs_delta, use_include_tag;
  48static int no_progress, daemon_mode;
  49/* Allow specifying sha1 if it is a ref tip. */
  50#define ALLOW_TIP_SHA1  01
  51/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
  52#define ALLOW_REACHABLE_SHA1    02
  53/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
  54#define ALLOW_ANY_SHA1  07
  55static unsigned int allow_unadvertised_object_request;
  56static int shallow_nr;
  57static struct object_array have_obj;
  58static struct object_array want_obj;
  59static struct object_array extra_edge_obj;
  60static unsigned int timeout;
  61static int keepalive = 5;
  62/* 0 for no sideband,
  63 * otherwise maximum packet size (up to 65520 bytes).
  64 */
  65static int use_sideband;
  66static int advertise_refs;
  67static int stateless_rpc;
  68static const char *pack_objects_hook;
  69
  70static int filter_capability_requested;
  71static int filter_advertise;
  72static struct list_objects_filter_options filter_options;
  73
  74static void reset_timeout(void)
  75{
  76        alarm(timeout);
  77}
  78
  79static void send_client_data(int fd, const char *data, ssize_t sz)
  80{
  81        if (use_sideband) {
  82                send_sideband(1, fd, data, sz, use_sideband);
  83                return;
  84        }
  85        if (fd == 3)
  86                /* emergency quit */
  87                fd = 2;
  88        if (fd == 2) {
  89                /* XXX: are we happy to lose stuff here? */
  90                xwrite(fd, data, sz);
  91                return;
  92        }
  93        write_or_die(fd, data, sz);
  94}
  95
  96static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
  97{
  98        FILE *fp = cb_data;
  99        if (graft->nr_parent == -1)
 100                fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
 101        return 0;
 102}
 103
 104static void create_pack_file(void)
 105{
 106        struct child_process pack_objects = CHILD_PROCESS_INIT;
 107        char data[8193], progress[128];
 108        char abort_msg[] = "aborting due to possible repository "
 109                "corruption on the remote side.";
 110        int buffered = -1;
 111        ssize_t sz;
 112        int i;
 113        FILE *pipe_fd;
 114
 115        if (!pack_objects_hook)
 116                pack_objects.git_cmd = 1;
 117        else {
 118                argv_array_push(&pack_objects.args, pack_objects_hook);
 119                argv_array_push(&pack_objects.args, "git");
 120                pack_objects.use_shell = 1;
 121        }
 122
 123        if (shallow_nr) {
 124                argv_array_push(&pack_objects.args, "--shallow-file");
 125                argv_array_push(&pack_objects.args, "");
 126        }
 127        argv_array_push(&pack_objects.args, "pack-objects");
 128        argv_array_push(&pack_objects.args, "--revs");
 129        if (use_thin_pack)
 130                argv_array_push(&pack_objects.args, "--thin");
 131
 132        argv_array_push(&pack_objects.args, "--stdout");
 133        if (shallow_nr)
 134                argv_array_push(&pack_objects.args, "--shallow");
 135        if (!no_progress)
 136                argv_array_push(&pack_objects.args, "--progress");
 137        if (use_ofs_delta)
 138                argv_array_push(&pack_objects.args, "--delta-base-offset");
 139        if (use_include_tag)
 140                argv_array_push(&pack_objects.args, "--include-tag");
 141        if (filter_options.filter_spec) {
 142                if (pack_objects.use_shell) {
 143                        struct strbuf buf = STRBUF_INIT;
 144                        sq_quote_buf(&buf, filter_options.filter_spec);
 145                        argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
 146                        strbuf_release(&buf);
 147                } else {
 148                        argv_array_pushf(&pack_objects.args, "--filter=%s",
 149                                         filter_options.filter_spec);
 150                }
 151        }
 152
 153        pack_objects.in = -1;
 154        pack_objects.out = -1;
 155        pack_objects.err = -1;
 156
 157        if (start_command(&pack_objects))
 158                die("git upload-pack: unable to fork git-pack-objects");
 159
 160        pipe_fd = xfdopen(pack_objects.in, "w");
 161
 162        if (shallow_nr)
 163                for_each_commit_graft(write_one_shallow, pipe_fd);
 164
 165        for (i = 0; i < want_obj.nr; i++)
 166                fprintf(pipe_fd, "%s\n",
 167                        oid_to_hex(&want_obj.objects[i].item->oid));
 168        fprintf(pipe_fd, "--not\n");
 169        for (i = 0; i < have_obj.nr; i++)
 170                fprintf(pipe_fd, "%s\n",
 171                        oid_to_hex(&have_obj.objects[i].item->oid));
 172        for (i = 0; i < extra_edge_obj.nr; i++)
 173                fprintf(pipe_fd, "%s\n",
 174                        oid_to_hex(&extra_edge_obj.objects[i].item->oid));
 175        fprintf(pipe_fd, "\n");
 176        fflush(pipe_fd);
 177        fclose(pipe_fd);
 178
 179        /* We read from pack_objects.err to capture stderr output for
 180         * progress bar, and pack_objects.out to capture the pack data.
 181         */
 182
 183        while (1) {
 184                struct pollfd pfd[2];
 185                int pe, pu, pollsize;
 186                int ret;
 187
 188                reset_timeout();
 189
 190                pollsize = 0;
 191                pe = pu = -1;
 192
 193                if (0 <= pack_objects.out) {
 194                        pfd[pollsize].fd = pack_objects.out;
 195                        pfd[pollsize].events = POLLIN;
 196                        pu = pollsize;
 197                        pollsize++;
 198                }
 199                if (0 <= pack_objects.err) {
 200                        pfd[pollsize].fd = pack_objects.err;
 201                        pfd[pollsize].events = POLLIN;
 202                        pe = pollsize;
 203                        pollsize++;
 204                }
 205
 206                if (!pollsize)
 207                        break;
 208
 209                ret = poll(pfd, pollsize,
 210                        keepalive < 0 ? -1 : 1000 * keepalive);
 211
 212                if (ret < 0) {
 213                        if (errno != EINTR) {
 214                                error_errno("poll failed, resuming");
 215                                sleep(1);
 216                        }
 217                        continue;
 218                }
 219                if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
 220                        /* Status ready; we ship that in the side-band
 221                         * or dump to the standard error.
 222                         */
 223                        sz = xread(pack_objects.err, progress,
 224                                  sizeof(progress));
 225                        if (0 < sz)
 226                                send_client_data(2, progress, sz);
 227                        else if (sz == 0) {
 228                                close(pack_objects.err);
 229                                pack_objects.err = -1;
 230                        }
 231                        else
 232                                goto fail;
 233                        /* give priority to status messages */
 234                        continue;
 235                }
 236                if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
 237                        /* Data ready; we keep the last byte to ourselves
 238                         * in case we detect broken rev-list, so that we
 239                         * can leave the stream corrupted.  This is
 240                         * unfortunate -- unpack-objects would happily
 241                         * accept a valid packdata with trailing garbage,
 242                         * so appending garbage after we pass all the
 243                         * pack data is not good enough to signal
 244                         * breakage to downstream.
 245                         */
 246                        char *cp = data;
 247                        ssize_t outsz = 0;
 248                        if (0 <= buffered) {
 249                                *cp++ = buffered;
 250                                outsz++;
 251                        }
 252                        sz = xread(pack_objects.out, cp,
 253                                  sizeof(data) - outsz);
 254                        if (0 < sz)
 255                                ;
 256                        else if (sz == 0) {
 257                                close(pack_objects.out);
 258                                pack_objects.out = -1;
 259                        }
 260                        else
 261                                goto fail;
 262                        sz += outsz;
 263                        if (1 < sz) {
 264                                buffered = data[sz-1] & 0xFF;
 265                                sz--;
 266                        }
 267                        else
 268                                buffered = -1;
 269                        send_client_data(1, data, sz);
 270                }
 271
 272                /*
 273                 * We hit the keepalive timeout without saying anything; send
 274                 * an empty message on the data sideband just to let the other
 275                 * side know we're still working on it, but don't have any data
 276                 * yet.
 277                 *
 278                 * If we don't have a sideband channel, there's no room in the
 279                 * protocol to say anything, so those clients are just out of
 280                 * luck.
 281                 */
 282                if (!ret && use_sideband) {
 283                        static const char buf[] = "0005\1";
 284                        write_or_die(1, buf, 5);
 285                }
 286        }
 287
 288        if (finish_command(&pack_objects)) {
 289                error("git upload-pack: git-pack-objects died with error.");
 290                goto fail;
 291        }
 292
 293        /* flush the data */
 294        if (0 <= buffered) {
 295                data[0] = buffered;
 296                send_client_data(1, data, 1);
 297                fprintf(stderr, "flushed.\n");
 298        }
 299        if (use_sideband)
 300                packet_flush(1);
 301        return;
 302
 303 fail:
 304        send_client_data(3, abort_msg, sizeof(abort_msg));
 305        die("git upload-pack: %s", abort_msg);
 306}
 307
 308static int got_oid(const char *hex, struct object_id *oid)
 309{
 310        struct object *o;
 311        int we_knew_they_have = 0;
 312
 313        if (get_oid_hex(hex, oid))
 314                die("git upload-pack: expected SHA1 object, got '%s'", hex);
 315        if (!has_object_file(oid))
 316                return -1;
 317
 318        o = parse_object(oid);
 319        if (!o)
 320                die("oops (%s)", oid_to_hex(oid));
 321        if (o->type == OBJ_COMMIT) {
 322                struct commit_list *parents;
 323                struct commit *commit = (struct commit *)o;
 324                if (o->flags & THEY_HAVE)
 325                        we_knew_they_have = 1;
 326                else
 327                        o->flags |= THEY_HAVE;
 328                if (!oldest_have || (commit->date < oldest_have))
 329                        oldest_have = commit->date;
 330                for (parents = commit->parents;
 331                     parents;
 332                     parents = parents->next)
 333                        parents->item->object.flags |= THEY_HAVE;
 334        }
 335        if (!we_knew_they_have) {
 336                add_object_array(o, NULL, &have_obj);
 337                return 1;
 338        }
 339        return 0;
 340}
 341
 342static int reachable(struct commit *want)
 343{
 344        struct prio_queue work = { compare_commits_by_commit_date };
 345
 346        prio_queue_put(&work, want);
 347        while (work.nr) {
 348                struct commit_list *list;
 349                struct commit *commit = prio_queue_get(&work);
 350
 351                if (commit->object.flags & THEY_HAVE) {
 352                        want->object.flags |= COMMON_KNOWN;
 353                        break;
 354                }
 355                if (!commit->object.parsed)
 356                        parse_object(&commit->object.oid);
 357                if (commit->object.flags & REACHABLE)
 358                        continue;
 359                commit->object.flags |= REACHABLE;
 360                if (commit->date < oldest_have)
 361                        continue;
 362                for (list = commit->parents; list; list = list->next) {
 363                        struct commit *parent = list->item;
 364                        if (!(parent->object.flags & REACHABLE))
 365                                prio_queue_put(&work, parent);
 366                }
 367        }
 368        want->object.flags |= REACHABLE;
 369        clear_commit_marks(want, REACHABLE);
 370        clear_prio_queue(&work);
 371        return (want->object.flags & COMMON_KNOWN);
 372}
 373
 374static int ok_to_give_up(void)
 375{
 376        int i;
 377
 378        if (!have_obj.nr)
 379                return 0;
 380
 381        for (i = 0; i < want_obj.nr; i++) {
 382                struct object *want = want_obj.objects[i].item;
 383
 384                if (want->flags & COMMON_KNOWN)
 385                        continue;
 386                want = deref_tag(want, "a want line", 0);
 387                if (!want || want->type != OBJ_COMMIT) {
 388                        /* no way to tell if this is reachable by
 389                         * looking at the ancestry chain alone, so
 390                         * leave a note to ourselves not to worry about
 391                         * this object anymore.
 392                         */
 393                        want_obj.objects[i].item->flags |= COMMON_KNOWN;
 394                        continue;
 395                }
 396                if (!reachable((struct commit *)want))
 397                        return 0;
 398        }
 399        return 1;
 400}
 401
 402static int get_common_commits(void)
 403{
 404        struct object_id oid;
 405        char last_hex[GIT_MAX_HEXSZ + 1];
 406        int got_common = 0;
 407        int got_other = 0;
 408        int sent_ready = 0;
 409
 410        save_commit_buffer = 0;
 411
 412        for (;;) {
 413                char *line = packet_read_line(0, NULL);
 414                const char *arg;
 415
 416                reset_timeout();
 417
 418                if (!line) {
 419                        if (multi_ack == 2 && got_common
 420                            && !got_other && ok_to_give_up()) {
 421                                sent_ready = 1;
 422                                packet_write_fmt(1, "ACK %s ready\n", last_hex);
 423                        }
 424                        if (have_obj.nr == 0 || multi_ack)
 425                                packet_write_fmt(1, "NAK\n");
 426
 427                        if (no_done && sent_ready) {
 428                                packet_write_fmt(1, "ACK %s\n", last_hex);
 429                                return 0;
 430                        }
 431                        if (stateless_rpc)
 432                                exit(0);
 433                        got_common = 0;
 434                        got_other = 0;
 435                        continue;
 436                }
 437                if (skip_prefix(line, "have ", &arg)) {
 438                        switch (got_oid(arg, &oid)) {
 439                        case -1: /* they have what we do not */
 440                                got_other = 1;
 441                                if (multi_ack && ok_to_give_up()) {
 442                                        const char *hex = oid_to_hex(&oid);
 443                                        if (multi_ack == 2) {
 444                                                sent_ready = 1;
 445                                                packet_write_fmt(1, "ACK %s ready\n", hex);
 446                                        } else
 447                                                packet_write_fmt(1, "ACK %s continue\n", hex);
 448                                }
 449                                break;
 450                        default:
 451                                got_common = 1;
 452                                memcpy(last_hex, oid_to_hex(&oid), 41);
 453                                if (multi_ack == 2)
 454                                        packet_write_fmt(1, "ACK %s common\n", last_hex);
 455                                else if (multi_ack)
 456                                        packet_write_fmt(1, "ACK %s continue\n", last_hex);
 457                                else if (have_obj.nr == 1)
 458                                        packet_write_fmt(1, "ACK %s\n", last_hex);
 459                                break;
 460                        }
 461                        continue;
 462                }
 463                if (!strcmp(line, "done")) {
 464                        if (have_obj.nr > 0) {
 465                                if (multi_ack)
 466                                        packet_write_fmt(1, "ACK %s\n", last_hex);
 467                                return 0;
 468                        }
 469                        packet_write_fmt(1, "NAK\n");
 470                        return -1;
 471                }
 472                die("git upload-pack: expected SHA1 list, got '%s'", line);
 473        }
 474}
 475
 476static int is_our_ref(struct object *o)
 477{
 478        int allow_hidden_ref = (allow_unadvertised_object_request &
 479                        (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
 480        return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
 481}
 482
 483/*
 484 * on successful case, it's up to the caller to close cmd->out
 485 */
 486static int do_reachable_revlist(struct child_process *cmd,
 487                                struct object_array *src,
 488                                struct object_array *reachable)
 489{
 490        static const char *argv[] = {
 491                "rev-list", "--stdin", NULL,
 492        };
 493        struct object *o;
 494        char namebuf[42]; /* ^ + SHA-1 + LF */
 495        int i;
 496
 497        cmd->argv = argv;
 498        cmd->git_cmd = 1;
 499        cmd->no_stderr = 1;
 500        cmd->in = -1;
 501        cmd->out = -1;
 502
 503        /*
 504         * If the next rev-list --stdin encounters an unknown commit,
 505         * it terminates, which will cause SIGPIPE in the write loop
 506         * below.
 507         */
 508        sigchain_push(SIGPIPE, SIG_IGN);
 509
 510        if (start_command(cmd))
 511                goto error;
 512
 513        namebuf[0] = '^';
 514        namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
 515        for (i = get_max_object_index(); 0 < i; ) {
 516                o = get_indexed_object(--i);
 517                if (!o)
 518                        continue;
 519                if (reachable && o->type == OBJ_COMMIT)
 520                        o->flags &= ~TMP_MARK;
 521                if (!is_our_ref(o))
 522                        continue;
 523                memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
 524                if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
 525                        goto error;
 526        }
 527        namebuf[GIT_SHA1_HEXSZ] = '\n';
 528        for (i = 0; i < src->nr; i++) {
 529                o = src->objects[i].item;
 530                if (is_our_ref(o)) {
 531                        if (reachable)
 532                                add_object_array(o, NULL, reachable);
 533                        continue;
 534                }
 535                if (reachable && o->type == OBJ_COMMIT)
 536                        o->flags |= TMP_MARK;
 537                memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
 538                if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
 539                        goto error;
 540        }
 541        close(cmd->in);
 542        cmd->in = -1;
 543        sigchain_pop(SIGPIPE);
 544
 545        return 0;
 546
 547error:
 548        sigchain_pop(SIGPIPE);
 549
 550        if (cmd->in >= 0)
 551                close(cmd->in);
 552        if (cmd->out >= 0)
 553                close(cmd->out);
 554        return -1;
 555}
 556
 557static int get_reachable_list(struct object_array *src,
 558                              struct object_array *reachable)
 559{
 560        struct child_process cmd = CHILD_PROCESS_INIT;
 561        int i;
 562        struct object *o;
 563        char namebuf[42]; /* ^ + SHA-1 + LF */
 564
 565        if (do_reachable_revlist(&cmd, src, reachable) < 0)
 566                return -1;
 567
 568        while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
 569                struct object_id sha1;
 570
 571                if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
 572                        break;
 573
 574                o = lookup_object(sha1.hash);
 575                if (o && o->type == OBJ_COMMIT) {
 576                        o->flags &= ~TMP_MARK;
 577                }
 578        }
 579        for (i = get_max_object_index(); 0 < i; i--) {
 580                o = get_indexed_object(i - 1);
 581                if (o && o->type == OBJ_COMMIT &&
 582                    (o->flags & TMP_MARK)) {
 583                        add_object_array(o, NULL, reachable);
 584                                o->flags &= ~TMP_MARK;
 585                }
 586        }
 587        close(cmd.out);
 588
 589        if (finish_command(&cmd))
 590                return -1;
 591
 592        return 0;
 593}
 594
 595static int has_unreachable(struct object_array *src)
 596{
 597        struct child_process cmd = CHILD_PROCESS_INIT;
 598        char buf[1];
 599        int i;
 600
 601        if (do_reachable_revlist(&cmd, src, NULL) < 0)
 602                return 1;
 603
 604        /*
 605         * The commits out of the rev-list are not ancestors of
 606         * our ref.
 607         */
 608        i = read_in_full(cmd.out, buf, 1);
 609        if (i)
 610                goto error;
 611        close(cmd.out);
 612        cmd.out = -1;
 613
 614        /*
 615         * rev-list may have died by encountering a bad commit
 616         * in the history, in which case we do want to bail out
 617         * even when it showed no commit.
 618         */
 619        if (finish_command(&cmd))
 620                goto error;
 621
 622        /* All the non-tip ones are ancestors of what we advertised */
 623        return 0;
 624
 625error:
 626        sigchain_pop(SIGPIPE);
 627        if (cmd.out >= 0)
 628                close(cmd.out);
 629        return 1;
 630}
 631
 632static void check_non_tip(void)
 633{
 634        int i;
 635
 636        /*
 637         * In the normal in-process case without
 638         * uploadpack.allowReachableSHA1InWant,
 639         * non-tip requests can never happen.
 640         */
 641        if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
 642                goto error;
 643        if (!has_unreachable(&want_obj))
 644                /* All the non-tip ones are ancestors of what we advertised */
 645                return;
 646
 647error:
 648        /* Pick one of them (we know there at least is one) */
 649        for (i = 0; i < want_obj.nr; i++) {
 650                struct object *o = want_obj.objects[i].item;
 651                if (!is_our_ref(o))
 652                        die("git upload-pack: not our ref %s",
 653                            oid_to_hex(&o->oid));
 654        }
 655}
 656
 657static void send_shallow(struct commit_list *result)
 658{
 659        while (result) {
 660                struct object *object = &result->item->object;
 661                if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
 662                        packet_write_fmt(1, "shallow %s",
 663                                         oid_to_hex(&object->oid));
 664                        register_shallow(&object->oid);
 665                        shallow_nr++;
 666                }
 667                result = result->next;
 668        }
 669}
 670
 671static void send_unshallow(const struct object_array *shallows)
 672{
 673        int i;
 674
 675        for (i = 0; i < shallows->nr; i++) {
 676                struct object *object = shallows->objects[i].item;
 677                if (object->flags & NOT_SHALLOW) {
 678                        struct commit_list *parents;
 679                        packet_write_fmt(1, "unshallow %s",
 680                                         oid_to_hex(&object->oid));
 681                        object->flags &= ~CLIENT_SHALLOW;
 682                        /*
 683                         * We want to _register_ "object" as shallow, but we
 684                         * also need to traverse object's parents to deepen a
 685                         * shallow clone. Unregister it for now so we can
 686                         * parse and add the parents to the want list, then
 687                         * re-register it.
 688                         */
 689                        unregister_shallow(&object->oid);
 690                        object->parsed = 0;
 691                        parse_commit_or_die((struct commit *)object);
 692                        parents = ((struct commit *)object)->parents;
 693                        while (parents) {
 694                                add_object_array(&parents->item->object,
 695                                                 NULL, &want_obj);
 696                                parents = parents->next;
 697                        }
 698                        add_object_array(object, NULL, &extra_edge_obj);
 699                }
 700                /* make sure commit traversal conforms to client */
 701                register_shallow(&object->oid);
 702        }
 703}
 704
 705static void deepen(int depth, int deepen_relative,
 706                   struct object_array *shallows)
 707{
 708        if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
 709                int i;
 710
 711                for (i = 0; i < shallows->nr; i++) {
 712                        struct object *object = shallows->objects[i].item;
 713                        object->flags |= NOT_SHALLOW;
 714                }
 715        } else if (deepen_relative) {
 716                struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
 717                struct commit_list *result;
 718
 719                get_reachable_list(shallows, &reachable_shallows);
 720                result = get_shallow_commits(&reachable_shallows,
 721                                             depth + 1,
 722                                             SHALLOW, NOT_SHALLOW);
 723                send_shallow(result);
 724                free_commit_list(result);
 725                object_array_clear(&reachable_shallows);
 726        } else {
 727                struct commit_list *result;
 728
 729                result = get_shallow_commits(&want_obj, depth,
 730                                             SHALLOW, NOT_SHALLOW);
 731                send_shallow(result);
 732                free_commit_list(result);
 733        }
 734
 735        send_unshallow(shallows);
 736        packet_flush(1);
 737}
 738
 739static void deepen_by_rev_list(int ac, const char **av,
 740                               struct object_array *shallows)
 741{
 742        struct commit_list *result;
 743
 744        result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
 745        send_shallow(result);
 746        free_commit_list(result);
 747        send_unshallow(shallows);
 748        packet_flush(1);
 749}
 750
 751static void receive_needs(void)
 752{
 753        struct object_array shallows = OBJECT_ARRAY_INIT;
 754        struct string_list deepen_not = STRING_LIST_INIT_DUP;
 755        int depth = 0;
 756        int has_non_tip = 0;
 757        timestamp_t deepen_since = 0;
 758        int deepen_rev_list = 0;
 759
 760        shallow_nr = 0;
 761        for (;;) {
 762                struct object *o;
 763                const char *features;
 764                struct object_id oid_buf;
 765                char *line = packet_read_line(0, NULL);
 766                const char *arg;
 767
 768                reset_timeout();
 769                if (!line)
 770                        break;
 771
 772                if (skip_prefix(line, "shallow ", &arg)) {
 773                        struct object_id oid;
 774                        struct object *object;
 775                        if (get_oid_hex(arg, &oid))
 776                                die("invalid shallow line: %s", line);
 777                        object = parse_object(&oid);
 778                        if (!object)
 779                                continue;
 780                        if (object->type != OBJ_COMMIT)
 781                                die("invalid shallow object %s", oid_to_hex(&oid));
 782                        if (!(object->flags & CLIENT_SHALLOW)) {
 783                                object->flags |= CLIENT_SHALLOW;
 784                                add_object_array(object, NULL, &shallows);
 785                        }
 786                        continue;
 787                }
 788                if (skip_prefix(line, "deepen ", &arg)) {
 789                        char *end = NULL;
 790                        depth = strtol(arg, &end, 0);
 791                        if (!end || *end || depth <= 0)
 792                                die("Invalid deepen: %s", line);
 793                        continue;
 794                }
 795                if (skip_prefix(line, "deepen-since ", &arg)) {
 796                        char *end = NULL;
 797                        deepen_since = parse_timestamp(arg, &end, 0);
 798                        if (!end || *end || !deepen_since ||
 799                            /* revisions.c's max_age -1 is special */
 800                            deepen_since == -1)
 801                                die("Invalid deepen-since: %s", line);
 802                        deepen_rev_list = 1;
 803                        continue;
 804                }
 805                if (skip_prefix(line, "deepen-not ", &arg)) {
 806                        char *ref = NULL;
 807                        struct object_id oid;
 808                        if (expand_ref(arg, strlen(arg), oid.hash, &ref) != 1)
 809                                die("git upload-pack: ambiguous deepen-not: %s", line);
 810                        string_list_append(&deepen_not, ref);
 811                        free(ref);
 812                        deepen_rev_list = 1;
 813                        continue;
 814                }
 815                if (skip_prefix(line, "filter ", &arg)) {
 816                        if (!filter_capability_requested)
 817                                die("git upload-pack: filtering capability not negotiated");
 818                        parse_list_objects_filter(&filter_options, arg);
 819                        continue;
 820                }
 821                if (!skip_prefix(line, "want ", &arg) ||
 822                    get_oid_hex(arg, &oid_buf))
 823                        die("git upload-pack: protocol error, "
 824                            "expected to get sha, not '%s'", line);
 825
 826                features = arg + 40;
 827
 828                if (parse_feature_request(features, "deepen-relative"))
 829                        deepen_relative = 1;
 830                if (parse_feature_request(features, "multi_ack_detailed"))
 831                        multi_ack = 2;
 832                else if (parse_feature_request(features, "multi_ack"))
 833                        multi_ack = 1;
 834                if (parse_feature_request(features, "no-done"))
 835                        no_done = 1;
 836                if (parse_feature_request(features, "thin-pack"))
 837                        use_thin_pack = 1;
 838                if (parse_feature_request(features, "ofs-delta"))
 839                        use_ofs_delta = 1;
 840                if (parse_feature_request(features, "side-band-64k"))
 841                        use_sideband = LARGE_PACKET_MAX;
 842                else if (parse_feature_request(features, "side-band"))
 843                        use_sideband = DEFAULT_PACKET_MAX;
 844                if (parse_feature_request(features, "no-progress"))
 845                        no_progress = 1;
 846                if (parse_feature_request(features, "include-tag"))
 847                        use_include_tag = 1;
 848                if (parse_feature_request(features, "filter"))
 849                        filter_capability_requested = 1;
 850
 851                o = parse_object(&oid_buf);
 852                if (!o) {
 853                        packet_write_fmt(1,
 854                                         "ERR upload-pack: not our ref %s",
 855                                         oid_to_hex(&oid_buf));
 856                        die("git upload-pack: not our ref %s",
 857                            oid_to_hex(&oid_buf));
 858                }
 859                if (!(o->flags & WANTED)) {
 860                        o->flags |= WANTED;
 861                        if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
 862                              || is_our_ref(o)))
 863                                has_non_tip = 1;
 864                        add_object_array(o, NULL, &want_obj);
 865                }
 866        }
 867
 868        /*
 869         * We have sent all our refs already, and the other end
 870         * should have chosen out of them. When we are operating
 871         * in the stateless RPC mode, however, their choice may
 872         * have been based on the set of older refs advertised
 873         * by another process that handled the initial request.
 874         */
 875        if (has_non_tip)
 876                check_non_tip();
 877
 878        if (!use_sideband && daemon_mode)
 879                no_progress = 1;
 880
 881        if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
 882                return;
 883        if (depth > 0 && deepen_rev_list)
 884                die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
 885        if (depth > 0)
 886                deepen(depth, deepen_relative, &shallows);
 887        else if (deepen_rev_list) {
 888                struct argv_array av = ARGV_ARRAY_INIT;
 889                int i;
 890
 891                argv_array_push(&av, "rev-list");
 892                if (deepen_since)
 893                        argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
 894                if (deepen_not.nr) {
 895                        argv_array_push(&av, "--not");
 896                        for (i = 0; i < deepen_not.nr; i++) {
 897                                struct string_list_item *s = deepen_not.items + i;
 898                                argv_array_push(&av, s->string);
 899                        }
 900                        argv_array_push(&av, "--not");
 901                }
 902                for (i = 0; i < want_obj.nr; i++) {
 903                        struct object *o = want_obj.objects[i].item;
 904                        argv_array_push(&av, oid_to_hex(&o->oid));
 905                }
 906                deepen_by_rev_list(av.argc, av.argv, &shallows);
 907                argv_array_clear(&av);
 908        }
 909        else
 910                if (shallows.nr > 0) {
 911                        int i;
 912                        for (i = 0; i < shallows.nr; i++)
 913                                register_shallow(&shallows.objects[i].item->oid);
 914                }
 915
 916        shallow_nr += shallows.nr;
 917        object_array_clear(&shallows);
 918}
 919
 920/* return non-zero if the ref is hidden, otherwise 0 */
 921static int mark_our_ref(const char *refname, const char *refname_full,
 922                        const struct object_id *oid)
 923{
 924        struct object *o = lookup_unknown_object(oid->hash);
 925
 926        if (ref_is_hidden(refname, refname_full)) {
 927                o->flags |= HIDDEN_REF;
 928                return 1;
 929        }
 930        o->flags |= OUR_REF;
 931        return 0;
 932}
 933
 934static int check_ref(const char *refname_full, const struct object_id *oid,
 935                     int flag, void *cb_data)
 936{
 937        const char *refname = strip_namespace(refname_full);
 938
 939        mark_our_ref(refname, refname_full, oid);
 940        return 0;
 941}
 942
 943static void format_symref_info(struct strbuf *buf, struct string_list *symref)
 944{
 945        struct string_list_item *item;
 946
 947        if (!symref->nr)
 948                return;
 949        for_each_string_list_item(item, symref)
 950                strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
 951}
 952
 953static int send_ref(const char *refname, const struct object_id *oid,
 954                    int flag, void *cb_data)
 955{
 956        static const char *capabilities = "multi_ack thin-pack side-band"
 957                " side-band-64k ofs-delta shallow deepen-since deepen-not"
 958                " deepen-relative no-progress include-tag multi_ack_detailed";
 959        const char *refname_nons = strip_namespace(refname);
 960        struct object_id peeled;
 961
 962        if (mark_our_ref(refname_nons, refname, oid))
 963                return 0;
 964
 965        if (capabilities) {
 966                struct strbuf symref_info = STRBUF_INIT;
 967
 968                format_symref_info(&symref_info, cb_data);
 969                packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
 970                             oid_to_hex(oid), refname_nons,
 971                             0, capabilities,
 972                             (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
 973                                     " allow-tip-sha1-in-want" : "",
 974                             (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
 975                                     " allow-reachable-sha1-in-want" : "",
 976                             stateless_rpc ? " no-done" : "",
 977                             symref_info.buf,
 978                             filter_advertise ? " filter" : "",
 979                             git_user_agent_sanitized());
 980                strbuf_release(&symref_info);
 981        } else {
 982                packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
 983        }
 984        capabilities = NULL;
 985        if (!peel_ref(refname, peeled.hash))
 986                packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
 987        return 0;
 988}
 989
 990static int find_symref(const char *refname, const struct object_id *oid,
 991                       int flag, void *cb_data)
 992{
 993        const char *symref_target;
 994        struct string_list_item *item;
 995
 996        if ((flag & REF_ISSYMREF) == 0)
 997                return 0;
 998        symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
 999        if (!symref_target || (flag & REF_ISSYMREF) == 0)
1000                die("'%s' is a symref but it is not?", refname);
1001        item = string_list_append(cb_data, refname);
1002        item->util = xstrdup(symref_target);
1003        return 0;
1004}
1005
1006static void upload_pack(void)
1007{
1008        struct string_list symref = STRING_LIST_INIT_DUP;
1009
1010        head_ref_namespaced(find_symref, &symref);
1011
1012        if (advertise_refs || !stateless_rpc) {
1013                reset_timeout();
1014                head_ref_namespaced(send_ref, &symref);
1015                for_each_namespaced_ref(send_ref, &symref);
1016                advertise_shallow_grafts(1);
1017                packet_flush(1);
1018        } else {
1019                head_ref_namespaced(check_ref, NULL);
1020                for_each_namespaced_ref(check_ref, NULL);
1021        }
1022        string_list_clear(&symref, 1);
1023        if (advertise_refs)
1024                return;
1025
1026        receive_needs();
1027        if (want_obj.nr) {
1028                get_common_commits();
1029                create_pack_file();
1030        }
1031}
1032
1033static int upload_pack_config(const char *var, const char *value, void *unused)
1034{
1035        if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1036                if (git_config_bool(var, value))
1037                        allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1038                else
1039                        allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
1040        } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1041                if (git_config_bool(var, value))
1042                        allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1043                else
1044                        allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
1045        } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1046                if (git_config_bool(var, value))
1047                        allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1048                else
1049                        allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
1050        } else if (!strcmp("uploadpack.keepalive", var)) {
1051                keepalive = git_config_int(var, value);
1052                if (!keepalive)
1053                        keepalive = -1;
1054        } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1055                if (!strcmp("uploadpack.packobjectshook", var))
1056                        return git_config_string(&pack_objects_hook, var, value);
1057        } else if (!strcmp("uploadpack.allowfilter", var)) {
1058                filter_advertise = git_config_bool(var, value);
1059        }
1060        return parse_hide_refs_config(var, value, "uploadpack");
1061}
1062
1063int cmd_main(int argc, const char **argv)
1064{
1065        const char *dir;
1066        int strict = 0;
1067        struct option options[] = {
1068                OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1069                         N_("quit after a single request/response exchange")),
1070                OPT_BOOL(0, "advertise-refs", &advertise_refs,
1071                         N_("exit immediately after initial ref advertisement")),
1072                OPT_BOOL(0, "strict", &strict,
1073                         N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1074                OPT_INTEGER(0, "timeout", &timeout,
1075                            N_("interrupt transfer after <n> seconds of inactivity")),
1076                OPT_END()
1077        };
1078
1079        packet_trace_identity("upload-pack");
1080        check_replace_refs = 0;
1081
1082        argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
1083
1084        if (argc != 1)
1085                usage_with_options(upload_pack_usage, options);
1086
1087        if (timeout)
1088                daemon_mode = 1;
1089
1090        setup_path();
1091
1092        dir = argv[0];
1093
1094        if (!enter_repo(dir, strict))
1095                die("'%s' does not appear to be a git repository", dir);
1096
1097        git_config(upload_pack_config, NULL);
1098        upload_pack();
1099        return 0;
1100}