upload-pack.con commit allow cloning a repository "shallowly" (016e6cc)
   1#include <signal.h>
   2#include <sys/wait.h>
   3#include <sys/poll.h>
   4#include "cache.h"
   5#include "refs.h"
   6#include "pkt-line.h"
   7#include "sideband.h"
   8#include "tag.h"
   9#include "object.h"
  10#include "commit.h"
  11#include "exec_cmd.h"
  12#include "diff.h"
  13#include "revision.h"
  14#include "list-objects.h"
  15
  16static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
  17
  18/* bits #0..7 in revision.h, #8..10 in commit.c */
  19#define THEY_HAVE       (1u << 11)
  20#define OUR_REF         (1u << 12)
  21#define WANTED          (1u << 13)
  22#define COMMON_KNOWN    (1u << 14)
  23#define REACHABLE       (1u << 15)
  24
  25static unsigned long oldest_have;
  26
  27static int multi_ack, nr_our_refs;
  28static int use_thin_pack, use_ofs_delta;
  29static struct object_array have_obj;
  30static struct object_array want_obj;
  31static unsigned int timeout;
  32/* 0 for no sideband,
  33 * otherwise maximum packet size (up to 65520 bytes).
  34 */
  35static int use_sideband;
  36
  37static void reset_timeout(void)
  38{
  39        alarm(timeout);
  40}
  41
  42static int strip(char *line, int len)
  43{
  44        if (len && line[len-1] == '\n')
  45                line[--len] = 0;
  46        return len;
  47}
  48
  49static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
  50{
  51        if (use_sideband)
  52                return send_sideband(1, fd, data, sz, use_sideband);
  53        if (fd == 3)
  54                /* emergency quit */
  55                fd = 2;
  56        if (fd == 2) {
  57                xwrite(fd, data, sz);
  58                return sz;
  59        }
  60        return safe_write(fd, data, sz);
  61}
  62
  63FILE *pack_pipe = NULL;
  64static void show_commit(struct commit *commit)
  65{
  66        if (commit->object.flags & BOUNDARY)
  67                fputc('-', pack_pipe);
  68        if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0)
  69                die("broken output pipe");
  70        fputc('\n', pack_pipe);
  71        fflush(pack_pipe);
  72        free(commit->buffer);
  73        commit->buffer = NULL;
  74}
  75
  76static void show_object(struct object_array_entry *p)
  77{
  78        /* An object with name "foo\n0000000..." can be used to
  79         * confuse downstream git-pack-objects very badly.
  80         */
  81        const char *ep = strchr(p->name, '\n');
  82        if (ep) {
  83                fprintf(pack_pipe, "%s %.*s\n", sha1_to_hex(p->item->sha1),
  84                       (int) (ep - p->name),
  85                       p->name);
  86        }
  87        else
  88                fprintf(pack_pipe, "%s %s\n",
  89                                sha1_to_hex(p->item->sha1), p->name);
  90}
  91
  92static void show_edge(struct commit *commit)
  93{
  94        fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
  95}
  96
  97static void create_pack_file(void)
  98{
  99        /* Pipes between rev-list to pack-objects, pack-objects to us
 100         * and pack-objects error stream for progress bar.
 101         */
 102        int lp_pipe[2], pu_pipe[2], pe_pipe[2];
 103        pid_t pid_rev_list, pid_pack_objects;
 104        int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
 105        char data[8193], progress[128];
 106        char abort_msg[] = "aborting due to possible repository "
 107                "corruption on the remote side.";
 108        int buffered = -1;
 109
 110        if (pipe(lp_pipe) < 0)
 111                die("git-upload-pack: unable to create pipe");
 112        pid_rev_list = fork();
 113        if (pid_rev_list < 0)
 114                die("git-upload-pack: unable to fork git-rev-list");
 115
 116        if (!pid_rev_list) {
 117                int i;
 118                struct rev_info revs;
 119
 120                pack_pipe = fdopen(lp_pipe[1], "w");
 121
 122                if (create_full_pack)
 123                        use_thin_pack = 0; /* no point doing it */
 124                init_revisions(&revs, NULL);
 125                revs.tag_objects = 1;
 126                revs.tree_objects = 1;
 127                revs.blob_objects = 1;
 128                if (use_thin_pack)
 129                        revs.edge_hint = 1;
 130
 131                if (create_full_pack) {
 132                        const char *args[] = {"rev-list", "--all", NULL};
 133                        setup_revisions(2, args, &revs, NULL);
 134                } else {
 135                        for (i = 0; i < want_obj.nr; i++) {
 136                                struct object *o = want_obj.objects[i].item;
 137                                add_pending_object(&revs, o, NULL);
 138                        }
 139                        for (i = 0; i < have_obj.nr; i++) {
 140                                struct object *o = have_obj.objects[i].item;
 141                                o->flags |= UNINTERESTING;
 142                                add_pending_object(&revs, o, NULL);
 143                        }
 144                        setup_revisions(0, NULL, &revs, NULL);
 145                }
 146                prepare_revision_walk(&revs);
 147                mark_edges_uninteresting(revs.commits, &revs, show_edge);
 148                traverse_commit_list(&revs, show_commit, show_object);
 149                exit(0);
 150        }
 151
 152        if (pipe(pu_pipe) < 0)
 153                die("git-upload-pack: unable to create pipe");
 154        if (pipe(pe_pipe) < 0)
 155                die("git-upload-pack: unable to create pipe");
 156        pid_pack_objects = fork();
 157        if (pid_pack_objects < 0) {
 158                /* daemon sets things up to ignore TERM */
 159                kill(pid_rev_list, SIGKILL);
 160                die("git-upload-pack: unable to fork git-pack-objects");
 161        }
 162        if (!pid_pack_objects) {
 163                dup2(lp_pipe[0], 0);
 164                dup2(pu_pipe[1], 1);
 165                dup2(pe_pipe[1], 2);
 166
 167                close(lp_pipe[0]);
 168                close(lp_pipe[1]);
 169                close(pu_pipe[0]);
 170                close(pu_pipe[1]);
 171                close(pe_pipe[0]);
 172                close(pe_pipe[1]);
 173                execl_git_cmd("pack-objects", "--stdout", "--progress",
 174                              use_ofs_delta ? "--delta-base-offset" : NULL,
 175                              NULL);
 176                kill(pid_rev_list, SIGKILL);
 177                die("git-upload-pack: unable to exec git-pack-objects");
 178        }
 179
 180        close(lp_pipe[0]);
 181        close(lp_pipe[1]);
 182
 183        /* We read from pe_pipe[0] to capture stderr output for
 184         * progress bar, and pu_pipe[0] to capture the pack data.
 185         */
 186        close(pe_pipe[1]);
 187        close(pu_pipe[1]);
 188
 189        while (1) {
 190                const char *who;
 191                struct pollfd pfd[2];
 192                pid_t pid;
 193                int status;
 194                ssize_t sz;
 195                int pe, pu, pollsize;
 196
 197                reset_timeout();
 198
 199                pollsize = 0;
 200                pe = pu = -1;
 201
 202                if (0 <= pu_pipe[0]) {
 203                        pfd[pollsize].fd = pu_pipe[0];
 204                        pfd[pollsize].events = POLLIN;
 205                        pu = pollsize;
 206                        pollsize++;
 207                }
 208                if (0 <= pe_pipe[0]) {
 209                        pfd[pollsize].fd = pe_pipe[0];
 210                        pfd[pollsize].events = POLLIN;
 211                        pe = pollsize;
 212                        pollsize++;
 213                }
 214
 215                if (pollsize) {
 216                        if (poll(pfd, pollsize, -1) < 0) {
 217                                if (errno != EINTR) {
 218                                        error("poll failed, resuming: %s",
 219                                              strerror(errno));
 220                                        sleep(1);
 221                                }
 222                                continue;
 223                        }
 224                        if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
 225                                /* Data ready; we keep the last byte
 226                                 * to ourselves in case we detect
 227                                 * broken rev-list, so that we can
 228                                 * leave the stream corrupted.  This
 229                                 * is unfortunate -- unpack-objects
 230                                 * would happily accept a valid pack
 231                                 * data with trailing garbage, so
 232                                 * appending garbage after we pass all
 233                                 * the pack data is not good enough to
 234                                 * signal breakage to downstream.
 235                                 */
 236                                char *cp = data;
 237                                ssize_t outsz = 0;
 238                                if (0 <= buffered) {
 239                                        *cp++ = buffered;
 240                                        outsz++;
 241                                }
 242                                sz = read(pu_pipe[0], cp,
 243                                          sizeof(data) - outsz);
 244                                if (0 < sz)
 245                                                ;
 246                                else if (sz == 0) {
 247                                        close(pu_pipe[0]);
 248                                        pu_pipe[0] = -1;
 249                                }
 250                                else
 251                                        goto fail;
 252                                sz += outsz;
 253                                if (1 < sz) {
 254                                        buffered = data[sz-1] & 0xFF;
 255                                        sz--;
 256                                }
 257                                else
 258                                        buffered = -1;
 259                                sz = send_client_data(1, data, sz);
 260                                if (sz < 0)
 261                                        goto fail;
 262                        }
 263                        if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
 264                                /* Status ready; we ship that in the side-band
 265                                 * or dump to the standard error.
 266                                 */
 267                                sz = read(pe_pipe[0], progress,
 268                                          sizeof(progress));
 269                                if (0 < sz)
 270                                        send_client_data(2, progress, sz);
 271                                else if (sz == 0) {
 272                                        close(pe_pipe[0]);
 273                                        pe_pipe[0] = -1;
 274                                }
 275                                else
 276                                        goto fail;
 277                        }
 278                }
 279
 280                /* See if the children are still there */
 281                if (pid_rev_list || pid_pack_objects) {
 282                        pid = waitpid(-1, &status, WNOHANG);
 283                        if (!pid)
 284                                continue;
 285                        who = ((pid == pid_rev_list) ? "git-rev-list" :
 286                               (pid == pid_pack_objects) ? "git-pack-objects" :
 287                               NULL);
 288                        if (!who) {
 289                                if (pid < 0) {
 290                                        error("git-upload-pack: %s",
 291                                              strerror(errno));
 292                                        goto fail;
 293                                }
 294                                error("git-upload-pack: we weren't "
 295                                      "waiting for %d", pid);
 296                                continue;
 297                        }
 298                        if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
 299                                error("git-upload-pack: %s died with error.",
 300                                      who);
 301                                goto fail;
 302                        }
 303                        if (pid == pid_rev_list)
 304                                pid_rev_list = 0;
 305                        if (pid == pid_pack_objects)
 306                                pid_pack_objects = 0;
 307                        if (pid_rev_list || pid_pack_objects)
 308                                continue;
 309                }
 310
 311                /* both died happily */
 312                if (pollsize)
 313                        continue;
 314
 315                /* flush the data */
 316                if (0 <= buffered) {
 317                        data[0] = buffered;
 318                        sz = send_client_data(1, data, 1);
 319                        if (sz < 0)
 320                                goto fail;
 321                        fprintf(stderr, "flushed.\n");
 322                }
 323                if (use_sideband)
 324                        packet_flush(1);
 325                return;
 326        }
 327 fail:
 328        if (pid_pack_objects)
 329                kill(pid_pack_objects, SIGKILL);
 330        if (pid_rev_list)
 331                kill(pid_rev_list, SIGKILL);
 332        send_client_data(3, abort_msg, sizeof(abort_msg));
 333        die("git-upload-pack: %s", abort_msg);
 334}
 335
 336static int got_sha1(char *hex, unsigned char *sha1)
 337{
 338        struct object *o;
 339        int we_knew_they_have = 0;
 340
 341        if (get_sha1_hex(hex, sha1))
 342                die("git-upload-pack: expected SHA1 object, got '%s'", hex);
 343        if (!has_sha1_file(sha1))
 344                return -1;
 345
 346        o = lookup_object(sha1);
 347        if (!(o && o->parsed))
 348                o = parse_object(sha1);
 349        if (!o)
 350                die("oops (%s)", sha1_to_hex(sha1));
 351        if (o->type == OBJ_COMMIT) {
 352                struct commit_list *parents;
 353                struct commit *commit = (struct commit *)o;
 354                if (o->flags & THEY_HAVE)
 355                        we_knew_they_have = 1;
 356                else
 357                        o->flags |= THEY_HAVE;
 358                if (!oldest_have || (commit->date < oldest_have))
 359                        oldest_have = commit->date;
 360                for (parents = commit->parents;
 361                     parents;
 362                     parents = parents->next)
 363                        parents->item->object.flags |= THEY_HAVE;
 364        }
 365        if (!we_knew_they_have) {
 366                add_object_array(o, NULL, &have_obj);
 367                return 1;
 368        }
 369        return 0;
 370}
 371
 372static int reachable(struct commit *want)
 373{
 374        struct commit_list *work = NULL;
 375
 376        insert_by_date(want, &work);
 377        while (work) {
 378                struct commit_list *list = work->next;
 379                struct commit *commit = work->item;
 380                free(work);
 381                work = list;
 382
 383                if (commit->object.flags & THEY_HAVE) {
 384                        want->object.flags |= COMMON_KNOWN;
 385                        break;
 386                }
 387                if (!commit->object.parsed)
 388                        parse_object(commit->object.sha1);
 389                if (commit->object.flags & REACHABLE)
 390                        continue;
 391                commit->object.flags |= REACHABLE;
 392                if (commit->date < oldest_have)
 393                        continue;
 394                for (list = commit->parents; list; list = list->next) {
 395                        struct commit *parent = list->item;
 396                        if (!(parent->object.flags & REACHABLE))
 397                                insert_by_date(parent, &work);
 398                }
 399        }
 400        want->object.flags |= REACHABLE;
 401        clear_commit_marks(want, REACHABLE);
 402        free_commit_list(work);
 403        return (want->object.flags & COMMON_KNOWN);
 404}
 405
 406static int ok_to_give_up(void)
 407{
 408        int i;
 409
 410        if (!have_obj.nr)
 411                return 0;
 412
 413        for (i = 0; i < want_obj.nr; i++) {
 414                struct object *want = want_obj.objects[i].item;
 415
 416                if (want->flags & COMMON_KNOWN)
 417                        continue;
 418                want = deref_tag(want, "a want line", 0);
 419                if (!want || want->type != OBJ_COMMIT) {
 420                        /* no way to tell if this is reachable by
 421                         * looking at the ancestry chain alone, so
 422                         * leave a note to ourselves not to worry about
 423                         * this object anymore.
 424                         */
 425                        want_obj.objects[i].item->flags |= COMMON_KNOWN;
 426                        continue;
 427                }
 428                if (!reachable((struct commit *)want))
 429                        return 0;
 430        }
 431        return 1;
 432}
 433
 434static int get_common_commits(void)
 435{
 436        static char line[1000];
 437        unsigned char sha1[20];
 438        char hex[41], last_hex[41];
 439        int len;
 440
 441        track_object_refs = 0;
 442        save_commit_buffer = 0;
 443
 444        for(;;) {
 445                len = packet_read_line(0, line, sizeof(line));
 446                reset_timeout();
 447
 448                if (!len) {
 449                        if (have_obj.nr == 0 || multi_ack)
 450                                packet_write(1, "NAK\n");
 451                        continue;
 452                }
 453                len = strip(line, len);
 454                if (!strncmp(line, "have ", 5)) {
 455                        switch (got_sha1(line+5, sha1)) {
 456                        case -1: /* they have what we do not */
 457                                if (multi_ack && ok_to_give_up())
 458                                        packet_write(1, "ACK %s continue\n",
 459                                                     sha1_to_hex(sha1));
 460                                break;
 461                        default:
 462                                memcpy(hex, sha1_to_hex(sha1), 41);
 463                                if (multi_ack) {
 464                                        const char *msg = "ACK %s continue\n";
 465                                        packet_write(1, msg, hex);
 466                                        memcpy(last_hex, hex, 41);
 467                                }
 468                                else if (have_obj.nr == 1)
 469                                        packet_write(1, "ACK %s\n", hex);
 470                                break;
 471                        }
 472                        continue;
 473                }
 474                if (!strcmp(line, "done")) {
 475                        if (have_obj.nr > 0) {
 476                                if (multi_ack)
 477                                        packet_write(1, "ACK %s\n", last_hex);
 478                                return 0;
 479                        }
 480                        packet_write(1, "NAK\n");
 481                        return -1;
 482                }
 483                die("git-upload-pack: expected SHA1 list, got '%s'", line);
 484        }
 485}
 486
 487static void receive_needs(void)
 488{
 489        struct object_array shallows = {0, 0, NULL};
 490        static char line[1000];
 491        int len, depth = 0;
 492
 493        for (;;) {
 494                struct object *o;
 495                unsigned char sha1_buf[20];
 496                len = packet_read_line(0, line, sizeof(line));
 497                reset_timeout();
 498                if (!len)
 499                        break;
 500
 501                if (!strncmp("shallow ", line, 8)) {
 502                        unsigned char sha1[20];
 503                        struct object *object;
 504                        if (get_sha1(line + 8, sha1))
 505                                die("invalid shallow line: %s", line);
 506                        object = parse_object(sha1);
 507                        if (!object)
 508                                die("did not find object for %s", line);
 509                        add_object_array(object, NULL, &shallows);
 510                        continue;
 511                }
 512                if (!strncmp("deepen ", line, 7)) {
 513                        char *end;
 514                        depth = strtol(line + 7, &end, 0);
 515                        if (end == line + 7 || depth <= 0)
 516                                die("Invalid deepen: %s", line);
 517                        continue;
 518                }
 519                if (strncmp("want ", line, 5) ||
 520                    get_sha1_hex(line+5, sha1_buf))
 521                        die("git-upload-pack: protocol error, "
 522                            "expected to get sha, not '%s'", line);
 523                if (strstr(line+45, "multi_ack"))
 524                        multi_ack = 1;
 525                if (strstr(line+45, "thin-pack"))
 526                        use_thin_pack = 1;
 527                if (strstr(line+45, "ofs-delta"))
 528                        use_ofs_delta = 1;
 529                if (strstr(line+45, "side-band-64k"))
 530                        use_sideband = LARGE_PACKET_MAX;
 531                else if (strstr(line+45, "side-band"))
 532                        use_sideband = DEFAULT_PACKET_MAX;
 533
 534                /* We have sent all our refs already, and the other end
 535                 * should have chosen out of them; otherwise they are
 536                 * asking for nonsense.
 537                 *
 538                 * Hmph.  We may later want to allow "want" line that
 539                 * asks for something like "master~10" (symbolic)...
 540                 * would it make sense?  I don't know.
 541                 */
 542                o = lookup_object(sha1_buf);
 543                if (!o || !(o->flags & OUR_REF))
 544                        die("git-upload-pack: not our ref %s", line+5);
 545                if (!(o->flags & WANTED)) {
 546                        o->flags |= WANTED;
 547                        add_object_array(o, NULL, &want_obj);
 548                }
 549        }
 550        if (depth > 0) {
 551                struct commit_list *result, *backup;
 552                if (shallows.nr > 0)
 553                        die("Deepening a shallow repository not yet supported");
 554                backup = result = get_shallow_commits(&want_obj, depth);
 555                while (result) {
 556                        packet_write(1, "shallow %s",
 557                                        sha1_to_hex(result->item->object.sha1));
 558                        result = result->next;
 559                }
 560                free_commit_list(backup);
 561        }
 562        if (shallows.nr > 0) {
 563                int i;
 564                for (i = 0; i < shallows.nr; i++)
 565                        register_shallow(shallows.objects[i].item->sha1);
 566        }
 567}
 568
 569static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 570{
 571        static const char *capabilities = "multi_ack thin-pack side-band"
 572                " side-band-64k ofs-delta shallow";
 573        struct object *o = parse_object(sha1);
 574
 575        if (!o)
 576                die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
 577
 578        if (capabilities)
 579                packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
 580                        0, capabilities);
 581        else
 582                packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
 583        capabilities = NULL;
 584        if (!(o->flags & OUR_REF)) {
 585                o->flags |= OUR_REF;
 586                nr_our_refs++;
 587        }
 588        if (o->type == OBJ_TAG) {
 589                o = deref_tag(o, refname, 0);
 590                packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
 591        }
 592        return 0;
 593}
 594
 595static void upload_pack(void)
 596{
 597        reset_timeout();
 598        head_ref(send_ref, NULL);
 599        for_each_ref(send_ref, NULL);
 600        packet_flush(1);
 601        receive_needs();
 602        if (want_obj.nr) {
 603                get_common_commits();
 604                create_pack_file();
 605        }
 606}
 607
 608int main(int argc, char **argv)
 609{
 610        char *dir;
 611        int i;
 612        int strict = 0;
 613
 614        for (i = 1; i < argc; i++) {
 615                char *arg = argv[i];
 616
 617                if (arg[0] != '-')
 618                        break;
 619                if (!strcmp(arg, "--strict")) {
 620                        strict = 1;
 621                        continue;
 622                }
 623                if (!strncmp(arg, "--timeout=", 10)) {
 624                        timeout = atoi(arg+10);
 625                        continue;
 626                }
 627                if (!strcmp(arg, "--")) {
 628                        i++;
 629                        break;
 630                }
 631        }
 632        
 633        if (i != argc-1)
 634                usage(upload_pack_usage);
 635        dir = argv[i];
 636
 637        if (!enter_repo(dir, strict))
 638                die("'%s': unable to chdir or not a git archive", dir);
 639
 640        upload_pack();
 641        return 0;
 642}