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