7d708fb8ad59d0f52333d590bee22515d56a76cd
   1#include "builtin.h"
   2#include "refs.h"
   3#include "pkt-line.h"
   4#include "commit.h"
   5#include "tag.h"
   6#include "exec_cmd.h"
   7#include "pack.h"
   8#include "sideband.h"
   9#include "fetch-pack.h"
  10#include "remote.h"
  11#include "run-command.h"
  12#include "transport.h"
  13
  14static int transfer_unpack_limit = -1;
  15static int fetch_unpack_limit = -1;
  16static int unpack_limit = 100;
  17static int prefer_ofs_delta = 1;
  18static int no_done;
  19static int fetch_fsck_objects = -1;
  20static int transfer_fsck_objects = -1;
  21static struct fetch_pack_args args = {
  22        /* .uploadpack = */ "git-upload-pack",
  23};
  24
  25static const char fetch_pack_usage[] =
  26"git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
  27"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
  28"[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
  29
  30#define COMPLETE        (1U << 0)
  31#define COMMON          (1U << 1)
  32#define COMMON_REF      (1U << 2)
  33#define SEEN            (1U << 3)
  34#define POPPED          (1U << 4)
  35
  36static int marked;
  37
  38/*
  39 * After sending this many "have"s if we do not get any new ACK , we
  40 * give up traversing our history.
  41 */
  42#define MAX_IN_VAIN 256
  43
  44static struct commit_list *rev_list;
  45static int non_common_revs, multi_ack, use_sideband;
  46
  47static void rev_list_push(struct commit *commit, int mark)
  48{
  49        if (!(commit->object.flags & mark)) {
  50                commit->object.flags |= mark;
  51
  52                if (!(commit->object.parsed))
  53                        if (parse_commit(commit))
  54                                return;
  55
  56                commit_list_insert_by_date(commit, &rev_list);
  57
  58                if (!(commit->object.flags & COMMON))
  59                        non_common_revs++;
  60        }
  61}
  62
  63static int rev_list_insert_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
  64{
  65        struct object *o = deref_tag(parse_object(sha1), refname, 0);
  66
  67        if (o && o->type == OBJ_COMMIT)
  68                rev_list_push((struct commit *)o, SEEN);
  69
  70        return 0;
  71}
  72
  73static int clear_marks(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
  74{
  75        struct object *o = deref_tag(parse_object(sha1), refname, 0);
  76
  77        if (o && o->type == OBJ_COMMIT)
  78                clear_commit_marks((struct commit *)o,
  79                                   COMMON | COMMON_REF | SEEN | POPPED);
  80        return 0;
  81}
  82
  83/*
  84   This function marks a rev and its ancestors as common.
  85   In some cases, it is desirable to mark only the ancestors (for example
  86   when only the server does not yet know that they are common).
  87*/
  88
  89static void mark_common(struct commit *commit,
  90                int ancestors_only, int dont_parse)
  91{
  92        if (commit != NULL && !(commit->object.flags & COMMON)) {
  93                struct object *o = (struct object *)commit;
  94
  95                if (!ancestors_only)
  96                        o->flags |= COMMON;
  97
  98                if (!(o->flags & SEEN))
  99                        rev_list_push(commit, SEEN);
 100                else {
 101                        struct commit_list *parents;
 102
 103                        if (!ancestors_only && !(o->flags & POPPED))
 104                                non_common_revs--;
 105                        if (!o->parsed && !dont_parse)
 106                                if (parse_commit(commit))
 107                                        return;
 108
 109                        for (parents = commit->parents;
 110                                        parents;
 111                                        parents = parents->next)
 112                                mark_common(parents->item, 0, dont_parse);
 113                }
 114        }
 115}
 116
 117/*
 118  Get the next rev to send, ignoring the common.
 119*/
 120
 121static const unsigned char *get_rev(void)
 122{
 123        struct commit *commit = NULL;
 124
 125        while (commit == NULL) {
 126                unsigned int mark;
 127                struct commit_list *parents;
 128
 129                if (rev_list == NULL || non_common_revs == 0)
 130                        return NULL;
 131
 132                commit = rev_list->item;
 133                if (!commit->object.parsed)
 134                        parse_commit(commit);
 135                parents = commit->parents;
 136
 137                commit->object.flags |= POPPED;
 138                if (!(commit->object.flags & COMMON))
 139                        non_common_revs--;
 140
 141                if (commit->object.flags & COMMON) {
 142                        /* do not send "have", and ignore ancestors */
 143                        commit = NULL;
 144                        mark = COMMON | SEEN;
 145                } else if (commit->object.flags & COMMON_REF)
 146                        /* send "have", and ignore ancestors */
 147                        mark = COMMON | SEEN;
 148                else
 149                        /* send "have", also for its ancestors */
 150                        mark = SEEN;
 151
 152                while (parents) {
 153                        if (!(parents->item->object.flags & SEEN))
 154                                rev_list_push(parents->item, mark);
 155                        if (mark & COMMON)
 156                                mark_common(parents->item, 1, 0);
 157                        parents = parents->next;
 158                }
 159
 160                rev_list = rev_list->next;
 161        }
 162
 163        return commit->object.sha1;
 164}
 165
 166enum ack_type {
 167        NAK = 0,
 168        ACK,
 169        ACK_continue,
 170        ACK_common,
 171        ACK_ready
 172};
 173
 174static void consume_shallow_list(int fd)
 175{
 176        if (args.stateless_rpc && args.depth > 0) {
 177                /* If we sent a depth we will get back "duplicate"
 178                 * shallow and unshallow commands every time there
 179                 * is a block of have lines exchanged.
 180                 */
 181                char line[1000];
 182                while (packet_read_line(fd, line, sizeof(line))) {
 183                        if (!prefixcmp(line, "shallow "))
 184                                continue;
 185                        if (!prefixcmp(line, "unshallow "))
 186                                continue;
 187                        die("git fetch-pack: expected shallow list");
 188                }
 189        }
 190}
 191
 192struct write_shallow_data {
 193        struct strbuf *out;
 194        int use_pack_protocol;
 195        int count;
 196};
 197
 198static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
 199{
 200        struct write_shallow_data *data = cb_data;
 201        const char *hex = sha1_to_hex(graft->sha1);
 202        data->count++;
 203        if (data->use_pack_protocol)
 204                packet_buf_write(data->out, "shallow %s", hex);
 205        else {
 206                strbuf_addstr(data->out, hex);
 207                strbuf_addch(data->out, '\n');
 208        }
 209        return 0;
 210}
 211
 212static int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
 213{
 214        struct write_shallow_data data;
 215        data.out = out;
 216        data.use_pack_protocol = use_pack_protocol;
 217        data.count = 0;
 218        for_each_commit_graft(write_one_shallow, &data);
 219        return data.count;
 220}
 221
 222static enum ack_type get_ack(int fd, unsigned char *result_sha1)
 223{
 224        static char line[1000];
 225        int len = packet_read_line(fd, line, sizeof(line));
 226
 227        if (!len)
 228                die("git fetch-pack: expected ACK/NAK, got EOF");
 229        if (line[len-1] == '\n')
 230                line[--len] = 0;
 231        if (!strcmp(line, "NAK"))
 232                return NAK;
 233        if (!prefixcmp(line, "ACK ")) {
 234                if (!get_sha1_hex(line+4, result_sha1)) {
 235                        if (strstr(line+45, "continue"))
 236                                return ACK_continue;
 237                        if (strstr(line+45, "common"))
 238                                return ACK_common;
 239                        if (strstr(line+45, "ready"))
 240                                return ACK_ready;
 241                        return ACK;
 242                }
 243        }
 244        die("git fetch_pack: expected ACK/NAK, got '%s'", line);
 245}
 246
 247static void send_request(int fd, struct strbuf *buf)
 248{
 249        if (args.stateless_rpc) {
 250                send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
 251                packet_flush(fd);
 252        } else
 253                safe_write(fd, buf->buf, buf->len);
 254}
 255
 256static void insert_one_alternate_ref(const struct ref *ref, void *unused)
 257{
 258        rev_list_insert_ref(NULL, ref->old_sha1, 0, NULL);
 259}
 260
 261#define INITIAL_FLUSH 16
 262#define PIPESAFE_FLUSH 32
 263#define LARGE_FLUSH 1024
 264
 265static int next_flush(int count)
 266{
 267        int flush_limit = args.stateless_rpc ? LARGE_FLUSH : PIPESAFE_FLUSH;
 268
 269        if (count < flush_limit)
 270                count <<= 1;
 271        else
 272                count += flush_limit;
 273        return count;
 274}
 275
 276static int find_common(int fd[2], unsigned char *result_sha1,
 277                       struct ref *refs)
 278{
 279        int fetching;
 280        int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
 281        const unsigned char *sha1;
 282        unsigned in_vain = 0;
 283        int got_continue = 0;
 284        int got_ready = 0;
 285        struct strbuf req_buf = STRBUF_INIT;
 286        size_t state_len = 0;
 287
 288        if (args.stateless_rpc && multi_ack == 1)
 289                die("--stateless-rpc requires multi_ack_detailed");
 290        if (marked)
 291                for_each_ref(clear_marks, NULL);
 292        marked = 1;
 293
 294        for_each_ref(rev_list_insert_ref, NULL);
 295        for_each_alternate_ref(insert_one_alternate_ref, NULL);
 296
 297        fetching = 0;
 298        for ( ; refs ; refs = refs->next) {
 299                unsigned char *remote = refs->old_sha1;
 300                const char *remote_hex;
 301                struct object *o;
 302
 303                /*
 304                 * If that object is complete (i.e. it is an ancestor of a
 305                 * local ref), we tell them we have it but do not have to
 306                 * tell them about its ancestors, which they already know
 307                 * about.
 308                 *
 309                 * We use lookup_object here because we are only
 310                 * interested in the case we *know* the object is
 311                 * reachable and we have already scanned it.
 312                 */
 313                if (((o = lookup_object(remote)) != NULL) &&
 314                                (o->flags & COMPLETE)) {
 315                        continue;
 316                }
 317
 318                remote_hex = sha1_to_hex(remote);
 319                if (!fetching) {
 320                        struct strbuf c = STRBUF_INIT;
 321                        if (multi_ack == 2)     strbuf_addstr(&c, " multi_ack_detailed");
 322                        if (multi_ack == 1)     strbuf_addstr(&c, " multi_ack");
 323                        if (no_done)            strbuf_addstr(&c, " no-done");
 324                        if (use_sideband == 2)  strbuf_addstr(&c, " side-band-64k");
 325                        if (use_sideband == 1)  strbuf_addstr(&c, " side-band");
 326                        if (args.use_thin_pack) strbuf_addstr(&c, " thin-pack");
 327                        if (args.no_progress)   strbuf_addstr(&c, " no-progress");
 328                        if (args.include_tag)   strbuf_addstr(&c, " include-tag");
 329                        if (prefer_ofs_delta)   strbuf_addstr(&c, " ofs-delta");
 330                        packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
 331                        strbuf_release(&c);
 332                } else
 333                        packet_buf_write(&req_buf, "want %s\n", remote_hex);
 334                fetching++;
 335        }
 336
 337        if (!fetching) {
 338                strbuf_release(&req_buf);
 339                packet_flush(fd[1]);
 340                return 1;
 341        }
 342
 343        if (is_repository_shallow())
 344                write_shallow_commits(&req_buf, 1);
 345        if (args.depth > 0)
 346                packet_buf_write(&req_buf, "deepen %d", args.depth);
 347        packet_buf_flush(&req_buf);
 348        state_len = req_buf.len;
 349
 350        if (args.depth > 0) {
 351                char line[1024];
 352                unsigned char sha1[20];
 353
 354                send_request(fd[1], &req_buf);
 355                while (packet_read_line(fd[0], line, sizeof(line))) {
 356                        if (!prefixcmp(line, "shallow ")) {
 357                                if (get_sha1_hex(line + 8, sha1))
 358                                        die("invalid shallow line: %s", line);
 359                                register_shallow(sha1);
 360                                continue;
 361                        }
 362                        if (!prefixcmp(line, "unshallow ")) {
 363                                if (get_sha1_hex(line + 10, sha1))
 364                                        die("invalid unshallow line: %s", line);
 365                                if (!lookup_object(sha1))
 366                                        die("object not found: %s", line);
 367                                /* make sure that it is parsed as shallow */
 368                                if (!parse_object(sha1))
 369                                        die("error in object: %s", line);
 370                                if (unregister_shallow(sha1))
 371                                        die("no shallow found: %s", line);
 372                                continue;
 373                        }
 374                        die("expected shallow/unshallow, got %s", line);
 375                }
 376        } else if (!args.stateless_rpc)
 377                send_request(fd[1], &req_buf);
 378
 379        if (!args.stateless_rpc) {
 380                /* If we aren't using the stateless-rpc interface
 381                 * we don't need to retain the headers.
 382                 */
 383                strbuf_setlen(&req_buf, 0);
 384                state_len = 0;
 385        }
 386
 387        flushes = 0;
 388        retval = -1;
 389        while ((sha1 = get_rev())) {
 390                packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
 391                if (args.verbose)
 392                        fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
 393                in_vain++;
 394                if (flush_at <= ++count) {
 395                        int ack;
 396
 397                        packet_buf_flush(&req_buf);
 398                        send_request(fd[1], &req_buf);
 399                        strbuf_setlen(&req_buf, state_len);
 400                        flushes++;
 401                        flush_at = next_flush(count);
 402
 403                        /*
 404                         * We keep one window "ahead" of the other side, and
 405                         * will wait for an ACK only on the next one
 406                         */
 407                        if (!args.stateless_rpc && count == INITIAL_FLUSH)
 408                                continue;
 409
 410                        consume_shallow_list(fd[0]);
 411                        do {
 412                                ack = get_ack(fd[0], result_sha1);
 413                                if (args.verbose && ack)
 414                                        fprintf(stderr, "got ack %d %s\n", ack,
 415                                                        sha1_to_hex(result_sha1));
 416                                switch (ack) {
 417                                case ACK:
 418                                        flushes = 0;
 419                                        multi_ack = 0;
 420                                        retval = 0;
 421                                        goto done;
 422                                case ACK_common:
 423                                case ACK_ready:
 424                                case ACK_continue: {
 425                                        struct commit *commit =
 426                                                lookup_commit(result_sha1);
 427                                        if (!commit)
 428                                                die("invalid commit %s", sha1_to_hex(result_sha1));
 429                                        if (args.stateless_rpc
 430                                         && ack == ACK_common
 431                                         && !(commit->object.flags & COMMON)) {
 432                                                /* We need to replay the have for this object
 433                                                 * on the next RPC request so the peer knows
 434                                                 * it is in common with us.
 435                                                 */
 436                                                const char *hex = sha1_to_hex(result_sha1);
 437                                                packet_buf_write(&req_buf, "have %s\n", hex);
 438                                                state_len = req_buf.len;
 439                                        }
 440                                        mark_common(commit, 0, 1);
 441                                        retval = 0;
 442                                        in_vain = 0;
 443                                        got_continue = 1;
 444                                        if (ack == ACK_ready) {
 445                                                rev_list = NULL;
 446                                                got_ready = 1;
 447                                        }
 448                                        break;
 449                                        }
 450                                }
 451                        } while (ack);
 452                        flushes--;
 453                        if (got_continue && MAX_IN_VAIN < in_vain) {
 454                                if (args.verbose)
 455                                        fprintf(stderr, "giving up\n");
 456                                break; /* give up */
 457                        }
 458                }
 459        }
 460done:
 461        if (!got_ready || !no_done) {
 462                packet_buf_write(&req_buf, "done\n");
 463                send_request(fd[1], &req_buf);
 464        }
 465        if (args.verbose)
 466                fprintf(stderr, "done\n");
 467        if (retval != 0) {
 468                multi_ack = 0;
 469                flushes++;
 470        }
 471        strbuf_release(&req_buf);
 472
 473        consume_shallow_list(fd[0]);
 474        while (flushes || multi_ack) {
 475                int ack = get_ack(fd[0], result_sha1);
 476                if (ack) {
 477                        if (args.verbose)
 478                                fprintf(stderr, "got ack (%d) %s\n", ack,
 479                                        sha1_to_hex(result_sha1));
 480                        if (ack == ACK)
 481                                return 0;
 482                        multi_ack = 1;
 483                        continue;
 484                }
 485                flushes--;
 486        }
 487        /* it is no error to fetch into a completely empty repo */
 488        return count ? retval : 0;
 489}
 490
 491static struct commit_list *complete;
 492
 493static int mark_complete(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 494{
 495        struct object *o = parse_object(sha1);
 496
 497        while (o && o->type == OBJ_TAG) {
 498                struct tag *t = (struct tag *) o;
 499                if (!t->tagged)
 500                        break; /* broken repository */
 501                o->flags |= COMPLETE;
 502                o = parse_object(t->tagged->sha1);
 503        }
 504        if (o && o->type == OBJ_COMMIT) {
 505                struct commit *commit = (struct commit *)o;
 506                if (!(commit->object.flags & COMPLETE)) {
 507                        commit->object.flags |= COMPLETE;
 508                        commit_list_insert_by_date(commit, &complete);
 509                }
 510        }
 511        return 0;
 512}
 513
 514static void mark_recent_complete_commits(unsigned long cutoff)
 515{
 516        while (complete && cutoff <= complete->item->date) {
 517                if (args.verbose)
 518                        fprintf(stderr, "Marking %s as complete\n",
 519                                sha1_to_hex(complete->item->object.sha1));
 520                pop_most_recent_commit(&complete, COMPLETE);
 521        }
 522}
 523
 524static void filter_refs(struct ref **refs, int nr_match, char **match)
 525{
 526        struct ref **return_refs;
 527        struct ref *newlist = NULL;
 528        struct ref **newtail = &newlist;
 529        struct ref *ref, *next;
 530        struct ref *fastarray[32];
 531
 532        if (nr_match && !args.fetch_all) {
 533                if (ARRAY_SIZE(fastarray) < nr_match)
 534                        return_refs = xcalloc(nr_match, sizeof(struct ref *));
 535                else {
 536                        return_refs = fastarray;
 537                        memset(return_refs, 0, sizeof(struct ref *) * nr_match);
 538                }
 539        }
 540        else
 541                return_refs = NULL;
 542
 543        for (ref = *refs; ref; ref = next) {
 544                next = ref->next;
 545                if (!memcmp(ref->name, "refs/", 5) &&
 546                    check_refname_format(ref->name + 5, 0))
 547                        ; /* trash */
 548                else if (args.fetch_all &&
 549                         (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
 550                        *newtail = ref;
 551                        ref->next = NULL;
 552                        newtail = &ref->next;
 553                        continue;
 554                }
 555                else {
 556                        int i;
 557                        for (i = 0; i < nr_match; i++) {
 558                                if (!strcmp(ref->name, match[i])) {
 559                                        match[i][0] = '\0';
 560                                        return_refs[i] = ref;
 561                                        break;
 562                                }
 563                        }
 564                        if (i < nr_match)
 565                                continue; /* we will link it later */
 566                }
 567                free(ref);
 568        }
 569
 570        if (!args.fetch_all) {
 571                int i;
 572                for (i = 0; i < nr_match; i++) {
 573                        ref = return_refs[i];
 574                        if (ref) {
 575                                *newtail = ref;
 576                                ref->next = NULL;
 577                                newtail = &ref->next;
 578                        }
 579                }
 580                if (return_refs != fastarray)
 581                        free(return_refs);
 582        }
 583        *refs = newlist;
 584}
 585
 586static void mark_alternate_complete(const struct ref *ref, void *unused)
 587{
 588        mark_complete(NULL, ref->old_sha1, 0, NULL);
 589}
 590
 591static int everything_local(struct ref **refs, int nr_match, char **match)
 592{
 593        struct ref *ref;
 594        int retval;
 595        unsigned long cutoff = 0;
 596
 597        save_commit_buffer = 0;
 598
 599        for (ref = *refs; ref; ref = ref->next) {
 600                struct object *o;
 601
 602                o = parse_object(ref->old_sha1);
 603                if (!o)
 604                        continue;
 605
 606                /* We already have it -- which may mean that we were
 607                 * in sync with the other side at some time after
 608                 * that (it is OK if we guess wrong here).
 609                 */
 610                if (o->type == OBJ_COMMIT) {
 611                        struct commit *commit = (struct commit *)o;
 612                        if (!cutoff || cutoff < commit->date)
 613                                cutoff = commit->date;
 614                }
 615        }
 616
 617        if (!args.depth) {
 618                for_each_ref(mark_complete, NULL);
 619                for_each_alternate_ref(mark_alternate_complete, NULL);
 620                if (cutoff)
 621                        mark_recent_complete_commits(cutoff);
 622        }
 623
 624        /*
 625         * Mark all complete remote refs as common refs.
 626         * Don't mark them common yet; the server has to be told so first.
 627         */
 628        for (ref = *refs; ref; ref = ref->next) {
 629                struct object *o = deref_tag(lookup_object(ref->old_sha1),
 630                                             NULL, 0);
 631
 632                if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
 633                        continue;
 634
 635                if (!(o->flags & SEEN)) {
 636                        rev_list_push((struct commit *)o, COMMON_REF | SEEN);
 637
 638                        mark_common((struct commit *)o, 1, 1);
 639                }
 640        }
 641
 642        filter_refs(refs, nr_match, match);
 643
 644        for (retval = 1, ref = *refs; ref ; ref = ref->next) {
 645                const unsigned char *remote = ref->old_sha1;
 646                unsigned char local[20];
 647                struct object *o;
 648
 649                o = lookup_object(remote);
 650                if (!o || !(o->flags & COMPLETE)) {
 651                        retval = 0;
 652                        if (!args.verbose)
 653                                continue;
 654                        fprintf(stderr,
 655                                "want %s (%s)\n", sha1_to_hex(remote),
 656                                ref->name);
 657                        continue;
 658                }
 659
 660                hashcpy(ref->new_sha1, local);
 661                if (!args.verbose)
 662                        continue;
 663                fprintf(stderr,
 664                        "already have %s (%s)\n", sha1_to_hex(remote),
 665                        ref->name);
 666        }
 667        return retval;
 668}
 669
 670static int sideband_demux(int in, int out, void *data)
 671{
 672        int *xd = data;
 673
 674        int ret = recv_sideband("fetch-pack", xd[0], out);
 675        close(out);
 676        return ret;
 677}
 678
 679static int get_pack(int xd[2], char **pack_lockfile)
 680{
 681        struct async demux;
 682        const char *argv[20];
 683        char keep_arg[256];
 684        char hdr_arg[256];
 685        const char **av;
 686        int do_keep = args.keep_pack;
 687        struct child_process cmd;
 688
 689        memset(&demux, 0, sizeof(demux));
 690        if (use_sideband) {
 691                /* xd[] is talking with upload-pack; subprocess reads from
 692                 * xd[0], spits out band#2 to stderr, and feeds us band#1
 693                 * through demux->out.
 694                 */
 695                demux.proc = sideband_demux;
 696                demux.data = xd;
 697                demux.out = -1;
 698                if (start_async(&demux))
 699                        die("fetch-pack: unable to fork off sideband"
 700                            " demultiplexer");
 701        }
 702        else
 703                demux.out = xd[0];
 704
 705        memset(&cmd, 0, sizeof(cmd));
 706        cmd.argv = argv;
 707        av = argv;
 708        *hdr_arg = 0;
 709        if (!args.keep_pack && unpack_limit) {
 710                struct pack_header header;
 711
 712                if (read_pack_header(demux.out, &header))
 713                        die("protocol error: bad pack header");
 714                snprintf(hdr_arg, sizeof(hdr_arg),
 715                         "--pack_header=%"PRIu32",%"PRIu32,
 716                         ntohl(header.hdr_version), ntohl(header.hdr_entries));
 717                if (ntohl(header.hdr_entries) < unpack_limit)
 718                        do_keep = 0;
 719                else
 720                        do_keep = 1;
 721        }
 722
 723        if (do_keep) {
 724                if (pack_lockfile)
 725                        cmd.out = -1;
 726                *av++ = "index-pack";
 727                *av++ = "--stdin";
 728                if (!args.quiet && !args.no_progress)
 729                        *av++ = "-v";
 730                if (args.use_thin_pack)
 731                        *av++ = "--fix-thin";
 732                if (args.lock_pack || unpack_limit) {
 733                        int s = sprintf(keep_arg,
 734                                        "--keep=fetch-pack %"PRIuMAX " on ", (uintmax_t) getpid());
 735                        if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
 736                                strcpy(keep_arg + s, "localhost");
 737                        *av++ = keep_arg;
 738                }
 739        }
 740        else {
 741                *av++ = "unpack-objects";
 742                if (args.quiet || args.no_progress)
 743                        *av++ = "-q";
 744        }
 745        if (*hdr_arg)
 746                *av++ = hdr_arg;
 747        if (fetch_fsck_objects >= 0
 748            ? fetch_fsck_objects
 749            : transfer_fsck_objects >= 0
 750            ? transfer_fsck_objects
 751            : 0)
 752                *av++ = "--strict";
 753        *av++ = NULL;
 754
 755        cmd.in = demux.out;
 756        cmd.git_cmd = 1;
 757        if (start_command(&cmd))
 758                die("fetch-pack: unable to fork off %s", argv[0]);
 759        if (do_keep && pack_lockfile) {
 760                *pack_lockfile = index_pack_lockfile(cmd.out);
 761                close(cmd.out);
 762        }
 763
 764        if (finish_command(&cmd))
 765                die("%s failed", argv[0]);
 766        if (use_sideband && finish_async(&demux))
 767                die("error in sideband demultiplexer");
 768        return 0;
 769}
 770
 771static struct ref *do_fetch_pack(int fd[2],
 772                const struct ref *orig_ref,
 773                int nr_match,
 774                char **match,
 775                char **pack_lockfile)
 776{
 777        struct ref *ref = copy_ref_list(orig_ref);
 778        unsigned char sha1[20];
 779
 780        sort_ref_list(&ref, ref_compare_name);
 781
 782        if (is_repository_shallow() && !server_supports("shallow"))
 783                die("Server does not support shallow clients");
 784        if (server_supports("multi_ack_detailed")) {
 785                if (args.verbose)
 786                        fprintf(stderr, "Server supports multi_ack_detailed\n");
 787                multi_ack = 2;
 788                if (server_supports("no-done")) {
 789                        if (args.verbose)
 790                                fprintf(stderr, "Server supports no-done\n");
 791                        if (args.stateless_rpc)
 792                                no_done = 1;
 793                }
 794        }
 795        else if (server_supports("multi_ack")) {
 796                if (args.verbose)
 797                        fprintf(stderr, "Server supports multi_ack\n");
 798                multi_ack = 1;
 799        }
 800        if (server_supports("side-band-64k")) {
 801                if (args.verbose)
 802                        fprintf(stderr, "Server supports side-band-64k\n");
 803                use_sideband = 2;
 804        }
 805        else if (server_supports("side-band")) {
 806                if (args.verbose)
 807                        fprintf(stderr, "Server supports side-band\n");
 808                use_sideband = 1;
 809        }
 810        if (server_supports("ofs-delta")) {
 811                if (args.verbose)
 812                        fprintf(stderr, "Server supports ofs-delta\n");
 813        } else
 814                prefer_ofs_delta = 0;
 815        if (everything_local(&ref, nr_match, match)) {
 816                packet_flush(fd[1]);
 817                goto all_done;
 818        }
 819        if (find_common(fd, sha1, ref) < 0)
 820                if (!args.keep_pack)
 821                        /* When cloning, it is not unusual to have
 822                         * no common commit.
 823                         */
 824                        warning("no common commits");
 825
 826        if (args.stateless_rpc)
 827                packet_flush(fd[1]);
 828        if (get_pack(fd, pack_lockfile))
 829                die("git fetch-pack: fetch failed.");
 830
 831 all_done:
 832        return ref;
 833}
 834
 835static int remove_duplicates(int nr_heads, char **heads)
 836{
 837        int src, dst;
 838
 839        if (!nr_heads)
 840                return 0;
 841
 842        for (src = dst = 1; src < nr_heads; src++)
 843                if (strcmp(heads[src], heads[dst-1]))
 844                        heads[dst++] = heads[src];
 845        return dst;
 846}
 847
 848static int fetch_pack_config(const char *var, const char *value, void *cb)
 849{
 850        if (strcmp(var, "fetch.unpacklimit") == 0) {
 851                fetch_unpack_limit = git_config_int(var, value);
 852                return 0;
 853        }
 854
 855        if (strcmp(var, "transfer.unpacklimit") == 0) {
 856                transfer_unpack_limit = git_config_int(var, value);
 857                return 0;
 858        }
 859
 860        if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
 861                prefer_ofs_delta = git_config_bool(var, value);
 862                return 0;
 863        }
 864
 865        if (!strcmp(var, "fetch.fsckobjects")) {
 866                fetch_fsck_objects = git_config_bool(var, value);
 867                return 0;
 868        }
 869
 870        if (!strcmp(var, "transfer.fsckobjects")) {
 871                transfer_fsck_objects = git_config_bool(var, value);
 872                return 0;
 873        }
 874
 875        return git_default_config(var, value, cb);
 876}
 877
 878static struct lock_file lock;
 879
 880static void fetch_pack_setup(void)
 881{
 882        static int did_setup;
 883        if (did_setup)
 884                return;
 885        git_config(fetch_pack_config, NULL);
 886        if (0 <= transfer_unpack_limit)
 887                unpack_limit = transfer_unpack_limit;
 888        else if (0 <= fetch_unpack_limit)
 889                unpack_limit = fetch_unpack_limit;
 890        did_setup = 1;
 891}
 892
 893int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
 894{
 895        int i, ret, nr_heads;
 896        struct ref *ref = NULL;
 897        char *dest = NULL, **heads;
 898        int fd[2];
 899        char *pack_lockfile = NULL;
 900        char **pack_lockfile_ptr = NULL;
 901        struct child_process *conn;
 902
 903        packet_trace_identity("fetch-pack");
 904
 905        nr_heads = 0;
 906        heads = NULL;
 907        for (i = 1; i < argc; i++) {
 908                const char *arg = argv[i];
 909
 910                if (*arg == '-') {
 911                        if (!prefixcmp(arg, "--upload-pack=")) {
 912                                args.uploadpack = arg + 14;
 913                                continue;
 914                        }
 915                        if (!prefixcmp(arg, "--exec=")) {
 916                                args.uploadpack = arg + 7;
 917                                continue;
 918                        }
 919                        if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
 920                                args.quiet = 1;
 921                                continue;
 922                        }
 923                        if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
 924                                args.lock_pack = args.keep_pack;
 925                                args.keep_pack = 1;
 926                                continue;
 927                        }
 928                        if (!strcmp("--thin", arg)) {
 929                                args.use_thin_pack = 1;
 930                                continue;
 931                        }
 932                        if (!strcmp("--include-tag", arg)) {
 933                                args.include_tag = 1;
 934                                continue;
 935                        }
 936                        if (!strcmp("--all", arg)) {
 937                                args.fetch_all = 1;
 938                                continue;
 939                        }
 940                        if (!strcmp("--stdin", arg)) {
 941                                args.stdin_refs = 1;
 942                                continue;
 943                        }
 944                        if (!strcmp("-v", arg)) {
 945                                args.verbose = 1;
 946                                continue;
 947                        }
 948                        if (!prefixcmp(arg, "--depth=")) {
 949                                args.depth = strtol(arg + 8, NULL, 0);
 950                                continue;
 951                        }
 952                        if (!strcmp("--no-progress", arg)) {
 953                                args.no_progress = 1;
 954                                continue;
 955                        }
 956                        if (!strcmp("--stateless-rpc", arg)) {
 957                                args.stateless_rpc = 1;
 958                                continue;
 959                        }
 960                        if (!strcmp("--lock-pack", arg)) {
 961                                args.lock_pack = 1;
 962                                pack_lockfile_ptr = &pack_lockfile;
 963                                continue;
 964                        }
 965                        usage(fetch_pack_usage);
 966                }
 967                dest = (char *)arg;
 968                heads = (char **)(argv + i + 1);
 969                nr_heads = argc - i - 1;
 970                break;
 971        }
 972        if (!dest)
 973                usage(fetch_pack_usage);
 974
 975        if (args.stdin_refs) {
 976                /*
 977                 * Copy refs from cmdline to new growable list, then
 978                 * append the refs from the standard input.
 979                 */
 980                int alloc_heads = nr_heads;
 981                int size = nr_heads * sizeof(*heads);
 982                heads = memcpy(xmalloc(size), heads, size);
 983                if (args.stateless_rpc) {
 984                        /* in stateless RPC mode we use pkt-line to read
 985                         * from stdin, until we get a flush packet
 986                         */
 987                        static char line[1000];
 988                        for (;;) {
 989                                int n = packet_read_line(0, line, sizeof(line));
 990                                if (!n)
 991                                        break;
 992                                if (line[n-1] == '\n')
 993                                        n--;
 994                                ALLOC_GROW(heads, nr_heads + 1, alloc_heads);
 995                                heads[nr_heads++] = xmemdupz(line, n);
 996                        }
 997                }
 998                else {
 999                        /* read from stdin one ref per line, until EOF */
1000                        struct strbuf line = STRBUF_INIT;
1001                        while (strbuf_getline(&line, stdin, '\n') != EOF) {
1002                                ALLOC_GROW(heads, nr_heads + 1, alloc_heads);
1003                                heads[nr_heads++] = strbuf_detach(&line, NULL);
1004                        }
1005                        strbuf_release(&line);
1006                }
1007        }
1008
1009        if (args.stateless_rpc) {
1010                conn = NULL;
1011                fd[0] = 0;
1012                fd[1] = 1;
1013        } else {
1014                conn = git_connect(fd, (char *)dest, args.uploadpack,
1015                                   args.verbose ? CONNECT_VERBOSE : 0);
1016        }
1017
1018        get_remote_heads(fd[0], &ref, 0, NULL);
1019
1020        ref = fetch_pack(&args, fd, conn, ref, dest,
1021                nr_heads, heads, pack_lockfile_ptr);
1022        if (pack_lockfile) {
1023                printf("lock %s\n", pack_lockfile);
1024                fflush(stdout);
1025        }
1026        close(fd[0]);
1027        close(fd[1]);
1028        if (finish_connect(conn))
1029                ref = NULL;
1030        ret = !ref;
1031
1032        if (!ret && nr_heads) {
1033                /* If the heads to pull were given, we should have
1034                 * consumed all of them by matching the remote.
1035                 * Otherwise, 'git fetch remote no-such-ref' would
1036                 * silently succeed without issuing an error.
1037                 */
1038                for (i = 0; i < nr_heads; i++)
1039                        if (heads[i] && heads[i][0]) {
1040                                error("no such remote ref %s", heads[i]);
1041                                ret = 1;
1042                        }
1043        }
1044        while (ref) {
1045                printf("%s %s\n",
1046                       sha1_to_hex(ref->old_sha1), ref->name);
1047                ref = ref->next;
1048        }
1049
1050        return ret;
1051}
1052
1053static int compare_heads(const void *a, const void *b)
1054{
1055        return strcmp(*(const char **)a, *(const char **)b);
1056}
1057
1058struct ref *fetch_pack(struct fetch_pack_args *my_args,
1059                       int fd[], struct child_process *conn,
1060                       const struct ref *ref,
1061                const char *dest,
1062                int nr_heads,
1063                char **heads,
1064                char **pack_lockfile)
1065{
1066        struct stat st;
1067        struct ref *ref_cpy;
1068
1069        fetch_pack_setup();
1070        if (&args != my_args)
1071                memcpy(&args, my_args, sizeof(args));
1072        if (args.depth > 0) {
1073                if (stat(git_path("shallow"), &st))
1074                        st.st_mtime = 0;
1075        }
1076
1077        if (heads && nr_heads) {
1078                nr_heads = remove_duplicates(nr_heads, heads);
1079                qsort(heads, nr_heads, sizeof(*heads), compare_heads);
1080        }
1081
1082        if (!ref) {
1083                packet_flush(fd[1]);
1084                die("no matching remote head");
1085        }
1086        ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile);
1087
1088        if (args.depth > 0) {
1089                struct cache_time mtime;
1090                struct strbuf sb = STRBUF_INIT;
1091                char *shallow = git_path("shallow");
1092                int fd;
1093
1094                mtime.sec = st.st_mtime;
1095                mtime.nsec = ST_MTIME_NSEC(st);
1096                if (stat(shallow, &st)) {
1097                        if (mtime.sec)
1098                                die("shallow file was removed during fetch");
1099                } else if (st.st_mtime != mtime.sec
1100#ifdef USE_NSEC
1101                                || ST_MTIME_NSEC(st) != mtime.nsec
1102#endif
1103                          )
1104                        die("shallow file was changed during fetch");
1105
1106                fd = hold_lock_file_for_update(&lock, shallow,
1107                                               LOCK_DIE_ON_ERROR);
1108                if (!write_shallow_commits(&sb, 0)
1109                 || write_in_full(fd, sb.buf, sb.len) != sb.len) {
1110                        unlink_or_warn(shallow);
1111                        rollback_lock_file(&lock);
1112                } else {
1113                        commit_lock_file(&lock);
1114                }
1115                strbuf_release(&sb);
1116        }
1117
1118        reprepare_packed_git();
1119        return ref_cpy;
1120}