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