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