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