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