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