fetch-pack.con commit fetch: implement fetch.fsck.* (1362df0)
   1#include "cache.h"
   2#include "repository.h"
   3#include "config.h"
   4#include "lockfile.h"
   5#include "refs.h"
   6#include "pkt-line.h"
   7#include "commit.h"
   8#include "tag.h"
   9#include "exec-cmd.h"
  10#include "pack.h"
  11#include "sideband.h"
  12#include "fetch-pack.h"
  13#include "remote.h"
  14#include "run-command.h"
  15#include "connect.h"
  16#include "transport.h"
  17#include "version.h"
  18#include "prio-queue.h"
  19#include "sha1-array.h"
  20#include "oidset.h"
  21#include "packfile.h"
  22#include "object-store.h"
  23#include "connected.h"
  24#include "fsck.h"
  25
  26static int transfer_unpack_limit = -1;
  27static int fetch_unpack_limit = -1;
  28static int unpack_limit = 100;
  29static int prefer_ofs_delta = 1;
  30static int no_done;
  31static int deepen_since_ok;
  32static int deepen_not_ok;
  33static int fetch_fsck_objects = -1;
  34static int transfer_fsck_objects = -1;
  35static int agent_supported;
  36static int server_supports_filtering;
  37static struct lock_file shallow_lock;
  38static const char *alternate_shallow_file;
  39static struct strbuf fsck_msg_types = STRBUF_INIT;
  40
  41/* Remember to update object flag allocation in object.h */
  42#define COMPLETE        (1U << 0)
  43#define COMMON          (1U << 1)
  44#define COMMON_REF      (1U << 2)
  45#define SEEN            (1U << 3)
  46#define POPPED          (1U << 4)
  47#define ALTERNATE       (1U << 5)
  48
  49static int marked;
  50
  51/*
  52 * After sending this many "have"s if we do not get any new ACK , we
  53 * give up traversing our history.
  54 */
  55#define MAX_IN_VAIN 256
  56
  57static struct prio_queue rev_list = { compare_commits_by_commit_date };
  58static int non_common_revs, multi_ack, use_sideband;
  59/* Allow specifying sha1 if it is a ref tip. */
  60#define ALLOW_TIP_SHA1  01
  61/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
  62#define ALLOW_REACHABLE_SHA1    02
  63static unsigned int allow_unadvertised_object_request;
  64
  65__attribute__((format (printf, 2, 3)))
  66static inline void print_verbose(const struct fetch_pack_args *args,
  67                                 const char *fmt, ...)
  68{
  69        va_list params;
  70
  71        if (!args->verbose)
  72                return;
  73
  74        va_start(params, fmt);
  75        vfprintf(stderr, fmt, params);
  76        va_end(params);
  77        fputc('\n', stderr);
  78}
  79
  80struct alternate_object_cache {
  81        struct object **items;
  82        size_t nr, alloc;
  83};
  84
  85static void cache_one_alternate(const char *refname,
  86                                const struct object_id *oid,
  87                                void *vcache)
  88{
  89        struct alternate_object_cache *cache = vcache;
  90        struct object *obj = parse_object(oid);
  91
  92        if (!obj || (obj->flags & ALTERNATE))
  93                return;
  94
  95        obj->flags |= ALTERNATE;
  96        ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
  97        cache->items[cache->nr++] = obj;
  98}
  99
 100static void for_each_cached_alternate(void (*cb)(struct object *))
 101{
 102        static int initialized;
 103        static struct alternate_object_cache cache;
 104        size_t i;
 105
 106        if (!initialized) {
 107                for_each_alternate_ref(cache_one_alternate, &cache);
 108                initialized = 1;
 109        }
 110
 111        for (i = 0; i < cache.nr; i++)
 112                cb(cache.items[i]);
 113}
 114
 115static void rev_list_push(struct commit *commit, int mark)
 116{
 117        if (!(commit->object.flags & mark)) {
 118                commit->object.flags |= mark;
 119
 120                if (parse_commit(commit))
 121                        return;
 122
 123                prio_queue_put(&rev_list, commit);
 124
 125                if (!(commit->object.flags & COMMON))
 126                        non_common_revs++;
 127        }
 128}
 129
 130static int rev_list_insert_ref(const char *refname, const struct object_id *oid)
 131{
 132        struct object *o = deref_tag(parse_object(oid), refname, 0);
 133
 134        if (o && o->type == OBJ_COMMIT)
 135                rev_list_push((struct commit *)o, SEEN);
 136
 137        return 0;
 138}
 139
 140static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
 141                                   int flag, void *cb_data)
 142{
 143        return rev_list_insert_ref(refname, oid);
 144}
 145
 146static int clear_marks(const char *refname, const struct object_id *oid,
 147                       int flag, void *cb_data)
 148{
 149        struct object *o = deref_tag(parse_object(oid), refname, 0);
 150
 151        if (o && o->type == OBJ_COMMIT)
 152                clear_commit_marks((struct commit *)o,
 153                                   COMMON | COMMON_REF | SEEN | POPPED);
 154        return 0;
 155}
 156
 157/*
 158   This function marks a rev and its ancestors as common.
 159   In some cases, it is desirable to mark only the ancestors (for example
 160   when only the server does not yet know that they are common).
 161*/
 162
 163static void mark_common(struct commit *commit,
 164                int ancestors_only, int dont_parse)
 165{
 166        if (commit != NULL && !(commit->object.flags & COMMON)) {
 167                struct object *o = (struct object *)commit;
 168
 169                if (!ancestors_only)
 170                        o->flags |= COMMON;
 171
 172                if (!(o->flags & SEEN))
 173                        rev_list_push(commit, SEEN);
 174                else {
 175                        struct commit_list *parents;
 176
 177                        if (!ancestors_only && !(o->flags & POPPED))
 178                                non_common_revs--;
 179                        if (!o->parsed && !dont_parse)
 180                                if (parse_commit(commit))
 181                                        return;
 182
 183                        for (parents = commit->parents;
 184                                        parents;
 185                                        parents = parents->next)
 186                                mark_common(parents->item, 0, dont_parse);
 187                }
 188        }
 189}
 190
 191/*
 192  Get the next rev to send, ignoring the common.
 193*/
 194
 195static const struct object_id *get_rev(void)
 196{
 197        struct commit *commit = NULL;
 198
 199        while (commit == NULL) {
 200                unsigned int mark;
 201                struct commit_list *parents;
 202
 203                if (rev_list.nr == 0 || non_common_revs == 0)
 204                        return NULL;
 205
 206                commit = prio_queue_get(&rev_list);
 207                parse_commit(commit);
 208                parents = commit->parents;
 209
 210                commit->object.flags |= POPPED;
 211                if (!(commit->object.flags & COMMON))
 212                        non_common_revs--;
 213
 214                if (commit->object.flags & COMMON) {
 215                        /* do not send "have", and ignore ancestors */
 216                        commit = NULL;
 217                        mark = COMMON | SEEN;
 218                } else if (commit->object.flags & COMMON_REF)
 219                        /* send "have", and ignore ancestors */
 220                        mark = COMMON | SEEN;
 221                else
 222                        /* send "have", also for its ancestors */
 223                        mark = SEEN;
 224
 225                while (parents) {
 226                        if (!(parents->item->object.flags & SEEN))
 227                                rev_list_push(parents->item, mark);
 228                        if (mark & COMMON)
 229                                mark_common(parents->item, 1, 0);
 230                        parents = parents->next;
 231                }
 232        }
 233
 234        return &commit->object.oid;
 235}
 236
 237enum ack_type {
 238        NAK = 0,
 239        ACK,
 240        ACK_continue,
 241        ACK_common,
 242        ACK_ready
 243};
 244
 245static void consume_shallow_list(struct fetch_pack_args *args, int fd)
 246{
 247        if (args->stateless_rpc && args->deepen) {
 248                /* If we sent a depth we will get back "duplicate"
 249                 * shallow and unshallow commands every time there
 250                 * is a block of have lines exchanged.
 251                 */
 252                char *line;
 253                while ((line = packet_read_line(fd, NULL))) {
 254                        if (starts_with(line, "shallow "))
 255                                continue;
 256                        if (starts_with(line, "unshallow "))
 257                                continue;
 258                        die(_("git fetch-pack: expected shallow list"));
 259                }
 260        }
 261}
 262
 263static enum ack_type get_ack(int fd, struct object_id *result_oid)
 264{
 265        int len;
 266        char *line = packet_read_line(fd, &len);
 267        const char *arg;
 268
 269        if (!line)
 270                die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
 271        if (!strcmp(line, "NAK"))
 272                return NAK;
 273        if (skip_prefix(line, "ACK ", &arg)) {
 274                if (!get_oid_hex(arg, result_oid)) {
 275                        arg += 40;
 276                        len -= arg - line;
 277                        if (len < 1)
 278                                return ACK;
 279                        if (strstr(arg, "continue"))
 280                                return ACK_continue;
 281                        if (strstr(arg, "common"))
 282                                return ACK_common;
 283                        if (strstr(arg, "ready"))
 284                                return ACK_ready;
 285                        return ACK;
 286                }
 287        }
 288        if (skip_prefix(line, "ERR ", &arg))
 289                die(_("remote error: %s"), arg);
 290        die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
 291}
 292
 293static void send_request(struct fetch_pack_args *args,
 294                         int fd, struct strbuf *buf)
 295{
 296        if (args->stateless_rpc) {
 297                send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
 298                packet_flush(fd);
 299        } else
 300                write_or_die(fd, buf->buf, buf->len);
 301}
 302
 303static void insert_one_alternate_object(struct object *obj)
 304{
 305        rev_list_insert_ref(NULL, &obj->oid);
 306}
 307
 308#define INITIAL_FLUSH 16
 309#define PIPESAFE_FLUSH 32
 310#define LARGE_FLUSH 16384
 311
 312static int next_flush(int stateless_rpc, int count)
 313{
 314        if (stateless_rpc) {
 315                if (count < LARGE_FLUSH)
 316                        count <<= 1;
 317                else
 318                        count = count * 11 / 10;
 319        } else {
 320                if (count < PIPESAFE_FLUSH)
 321                        count <<= 1;
 322                else
 323                        count += PIPESAFE_FLUSH;
 324        }
 325        return count;
 326}
 327
 328static int find_common(struct fetch_pack_args *args,
 329                       int fd[2], struct object_id *result_oid,
 330                       struct ref *refs)
 331{
 332        int fetching;
 333        int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
 334        const struct object_id *oid;
 335        unsigned in_vain = 0;
 336        int got_continue = 0;
 337        int got_ready = 0;
 338        struct strbuf req_buf = STRBUF_INIT;
 339        size_t state_len = 0;
 340
 341        if (args->stateless_rpc && multi_ack == 1)
 342                die(_("--stateless-rpc requires multi_ack_detailed"));
 343        if (marked)
 344                for_each_ref(clear_marks, NULL);
 345        marked = 1;
 346
 347        for_each_ref(rev_list_insert_ref_oid, NULL);
 348        for_each_cached_alternate(insert_one_alternate_object);
 349
 350        fetching = 0;
 351        for ( ; refs ; refs = refs->next) {
 352                struct object_id *remote = &refs->old_oid;
 353                const char *remote_hex;
 354                struct object *o;
 355
 356                /*
 357                 * If that object is complete (i.e. it is an ancestor of a
 358                 * local ref), we tell them we have it but do not have to
 359                 * tell them about its ancestors, which they already know
 360                 * about.
 361                 *
 362                 * We use lookup_object here because we are only
 363                 * interested in the case we *know* the object is
 364                 * reachable and we have already scanned it.
 365                 */
 366                if (((o = lookup_object(remote->hash)) != NULL) &&
 367                                (o->flags & COMPLETE)) {
 368                        continue;
 369                }
 370
 371                remote_hex = oid_to_hex(remote);
 372                if (!fetching) {
 373                        struct strbuf c = STRBUF_INIT;
 374                        if (multi_ack == 2)     strbuf_addstr(&c, " multi_ack_detailed");
 375                        if (multi_ack == 1)     strbuf_addstr(&c, " multi_ack");
 376                        if (no_done)            strbuf_addstr(&c, " no-done");
 377                        if (use_sideband == 2)  strbuf_addstr(&c, " side-band-64k");
 378                        if (use_sideband == 1)  strbuf_addstr(&c, " side-band");
 379                        if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
 380                        if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
 381                        if (args->no_progress)   strbuf_addstr(&c, " no-progress");
 382                        if (args->include_tag)   strbuf_addstr(&c, " include-tag");
 383                        if (prefer_ofs_delta)   strbuf_addstr(&c, " ofs-delta");
 384                        if (deepen_since_ok)    strbuf_addstr(&c, " deepen-since");
 385                        if (deepen_not_ok)      strbuf_addstr(&c, " deepen-not");
 386                        if (agent_supported)    strbuf_addf(&c, " agent=%s",
 387                                                            git_user_agent_sanitized());
 388                        if (args->filter_options.choice)
 389                                strbuf_addstr(&c, " filter");
 390                        packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
 391                        strbuf_release(&c);
 392                } else
 393                        packet_buf_write(&req_buf, "want %s\n", remote_hex);
 394                fetching++;
 395        }
 396
 397        if (!fetching) {
 398                strbuf_release(&req_buf);
 399                packet_flush(fd[1]);
 400                return 1;
 401        }
 402
 403        if (is_repository_shallow(the_repository))
 404                write_shallow_commits(&req_buf, 1, NULL);
 405        if (args->depth > 0)
 406                packet_buf_write(&req_buf, "deepen %d", args->depth);
 407        if (args->deepen_since) {
 408                timestamp_t max_age = approxidate(args->deepen_since);
 409                packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
 410        }
 411        if (args->deepen_not) {
 412                int i;
 413                for (i = 0; i < args->deepen_not->nr; i++) {
 414                        struct string_list_item *s = args->deepen_not->items + i;
 415                        packet_buf_write(&req_buf, "deepen-not %s", s->string);
 416                }
 417        }
 418        if (server_supports_filtering && args->filter_options.choice)
 419                packet_buf_write(&req_buf, "filter %s",
 420                                 args->filter_options.filter_spec);
 421        packet_buf_flush(&req_buf);
 422        state_len = req_buf.len;
 423
 424        if (args->deepen) {
 425                char *line;
 426                const char *arg;
 427                struct object_id oid;
 428
 429                send_request(args, fd[1], &req_buf);
 430                while ((line = packet_read_line(fd[0], NULL))) {
 431                        if (skip_prefix(line, "shallow ", &arg)) {
 432                                if (get_oid_hex(arg, &oid))
 433                                        die(_("invalid shallow line: %s"), line);
 434                                register_shallow(the_repository, &oid);
 435                                continue;
 436                        }
 437                        if (skip_prefix(line, "unshallow ", &arg)) {
 438                                if (get_oid_hex(arg, &oid))
 439                                        die(_("invalid unshallow line: %s"), line);
 440                                if (!lookup_object(oid.hash))
 441                                        die(_("object not found: %s"), line);
 442                                /* make sure that it is parsed as shallow */
 443                                if (!parse_object(&oid))
 444                                        die(_("error in object: %s"), line);
 445                                if (unregister_shallow(&oid))
 446                                        die(_("no shallow found: %s"), line);
 447                                continue;
 448                        }
 449                        die(_("expected shallow/unshallow, got %s"), line);
 450                }
 451        } else if (!args->stateless_rpc)
 452                send_request(args, fd[1], &req_buf);
 453
 454        if (!args->stateless_rpc) {
 455                /* If we aren't using the stateless-rpc interface
 456                 * we don't need to retain the headers.
 457                 */
 458                strbuf_setlen(&req_buf, 0);
 459                state_len = 0;
 460        }
 461
 462        flushes = 0;
 463        retval = -1;
 464        if (args->no_dependents)
 465                goto done;
 466        while ((oid = get_rev())) {
 467                packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
 468                print_verbose(args, "have %s", oid_to_hex(oid));
 469                in_vain++;
 470                if (flush_at <= ++count) {
 471                        int ack;
 472
 473                        packet_buf_flush(&req_buf);
 474                        send_request(args, fd[1], &req_buf);
 475                        strbuf_setlen(&req_buf, state_len);
 476                        flushes++;
 477                        flush_at = next_flush(args->stateless_rpc, count);
 478
 479                        /*
 480                         * We keep one window "ahead" of the other side, and
 481                         * will wait for an ACK only on the next one
 482                         */
 483                        if (!args->stateless_rpc && count == INITIAL_FLUSH)
 484                                continue;
 485
 486                        consume_shallow_list(args, fd[0]);
 487                        do {
 488                                ack = get_ack(fd[0], result_oid);
 489                                if (ack)
 490                                        print_verbose(args, _("got %s %d %s"), "ack",
 491                                                      ack, oid_to_hex(result_oid));
 492                                switch (ack) {
 493                                case ACK:
 494                                        flushes = 0;
 495                                        multi_ack = 0;
 496                                        retval = 0;
 497                                        goto done;
 498                                case ACK_common:
 499                                case ACK_ready:
 500                                case ACK_continue: {
 501                                        struct commit *commit =
 502                                                lookup_commit(result_oid);
 503                                        if (!commit)
 504                                                die(_("invalid commit %s"), oid_to_hex(result_oid));
 505                                        if (args->stateless_rpc
 506                                         && ack == ACK_common
 507                                         && !(commit->object.flags & COMMON)) {
 508                                                /* We need to replay the have for this object
 509                                                 * on the next RPC request so the peer knows
 510                                                 * it is in common with us.
 511                                                 */
 512                                                const char *hex = oid_to_hex(result_oid);
 513                                                packet_buf_write(&req_buf, "have %s\n", hex);
 514                                                state_len = req_buf.len;
 515                                                /*
 516                                                 * Reset in_vain because an ack
 517                                                 * for this commit has not been
 518                                                 * seen.
 519                                                 */
 520                                                in_vain = 0;
 521                                        } else if (!args->stateless_rpc
 522                                                   || ack != ACK_common)
 523                                                in_vain = 0;
 524                                        mark_common(commit, 0, 1);
 525                                        retval = 0;
 526                                        got_continue = 1;
 527                                        if (ack == ACK_ready) {
 528                                                clear_prio_queue(&rev_list);
 529                                                got_ready = 1;
 530                                        }
 531                                        break;
 532                                        }
 533                                }
 534                        } while (ack);
 535                        flushes--;
 536                        if (got_continue && MAX_IN_VAIN < in_vain) {
 537                                print_verbose(args, _("giving up"));
 538                                break; /* give up */
 539                        }
 540                }
 541        }
 542done:
 543        if (!got_ready || !no_done) {
 544                packet_buf_write(&req_buf, "done\n");
 545                send_request(args, fd[1], &req_buf);
 546        }
 547        print_verbose(args, _("done"));
 548        if (retval != 0) {
 549                multi_ack = 0;
 550                flushes++;
 551        }
 552        strbuf_release(&req_buf);
 553
 554        if (!got_ready || !no_done)
 555                consume_shallow_list(args, fd[0]);
 556        while (flushes || multi_ack) {
 557                int ack = get_ack(fd[0], result_oid);
 558                if (ack) {
 559                        print_verbose(args, _("got %s (%d) %s"), "ack",
 560                                      ack, oid_to_hex(result_oid));
 561                        if (ack == ACK)
 562                                return 0;
 563                        multi_ack = 1;
 564                        continue;
 565                }
 566                flushes--;
 567        }
 568        /* it is no error to fetch into a completely empty repo */
 569        return count ? retval : 0;
 570}
 571
 572static struct commit_list *complete;
 573
 574static int mark_complete(const struct object_id *oid)
 575{
 576        struct object *o = parse_object(oid);
 577
 578        while (o && o->type == OBJ_TAG) {
 579                struct tag *t = (struct tag *) o;
 580                if (!t->tagged)
 581                        break; /* broken repository */
 582                o->flags |= COMPLETE;
 583                o = parse_object(&t->tagged->oid);
 584        }
 585        if (o && o->type == OBJ_COMMIT) {
 586                struct commit *commit = (struct commit *)o;
 587                if (!(commit->object.flags & COMPLETE)) {
 588                        commit->object.flags |= COMPLETE;
 589                        commit_list_insert(commit, &complete);
 590                }
 591        }
 592        return 0;
 593}
 594
 595static int mark_complete_oid(const char *refname, const struct object_id *oid,
 596                             int flag, void *cb_data)
 597{
 598        return mark_complete(oid);
 599}
 600
 601static void mark_recent_complete_commits(struct fetch_pack_args *args,
 602                                         timestamp_t cutoff)
 603{
 604        while (complete && cutoff <= complete->item->date) {
 605                print_verbose(args, _("Marking %s as complete"),
 606                              oid_to_hex(&complete->item->object.oid));
 607                pop_most_recent_commit(&complete, COMPLETE);
 608        }
 609}
 610
 611static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
 612{
 613        for (; refs; refs = refs->next)
 614                oidset_insert(oids, &refs->old_oid);
 615}
 616
 617static int tip_oids_contain(struct oidset *tip_oids,
 618                            struct ref *unmatched, struct ref *newlist,
 619                            const struct object_id *id)
 620{
 621        /*
 622         * Note that this only looks at the ref lists the first time it's
 623         * called. This works out in filter_refs() because even though it may
 624         * add to "newlist" between calls, the additions will always be for
 625         * oids that are already in the set.
 626         */
 627        if (!tip_oids->map.map.tablesize) {
 628                add_refs_to_oidset(tip_oids, unmatched);
 629                add_refs_to_oidset(tip_oids, newlist);
 630        }
 631        return oidset_contains(tip_oids, id);
 632}
 633
 634static void filter_refs(struct fetch_pack_args *args,
 635                        struct ref **refs,
 636                        struct ref **sought, int nr_sought)
 637{
 638        struct ref *newlist = NULL;
 639        struct ref **newtail = &newlist;
 640        struct ref *unmatched = NULL;
 641        struct ref *ref, *next;
 642        struct oidset tip_oids = OIDSET_INIT;
 643        int i;
 644
 645        i = 0;
 646        for (ref = *refs; ref; ref = next) {
 647                int keep = 0;
 648                next = ref->next;
 649
 650                if (starts_with(ref->name, "refs/") &&
 651                    check_refname_format(ref->name, 0))
 652                        ; /* trash */
 653                else {
 654                        while (i < nr_sought) {
 655                                int cmp = strcmp(ref->name, sought[i]->name);
 656                                if (cmp < 0)
 657                                        break; /* definitely do not have it */
 658                                else if (cmp == 0) {
 659                                        keep = 1; /* definitely have it */
 660                                        sought[i]->match_status = REF_MATCHED;
 661                                }
 662                                i++;
 663                        }
 664
 665                        if (!keep && args->fetch_all &&
 666                            (!args->deepen || !starts_with(ref->name, "refs/tags/")))
 667                                keep = 1;
 668                }
 669
 670                if (keep) {
 671                        *newtail = ref;
 672                        ref->next = NULL;
 673                        newtail = &ref->next;
 674                } else {
 675                        ref->next = unmatched;
 676                        unmatched = ref;
 677                }
 678        }
 679
 680        /* Append unmatched requests to the list */
 681        for (i = 0; i < nr_sought; i++) {
 682                struct object_id oid;
 683                const char *p;
 684
 685                ref = sought[i];
 686                if (ref->match_status != REF_NOT_MATCHED)
 687                        continue;
 688                if (parse_oid_hex(ref->name, &oid, &p) ||
 689                    *p != '\0' ||
 690                    oidcmp(&oid, &ref->old_oid))
 691                        continue;
 692
 693                if ((allow_unadvertised_object_request &
 694                     (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
 695                    tip_oids_contain(&tip_oids, unmatched, newlist,
 696                                     &ref->old_oid)) {
 697                        ref->match_status = REF_MATCHED;
 698                        *newtail = copy_ref(ref);
 699                        newtail = &(*newtail)->next;
 700                } else {
 701                        ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
 702                }
 703        }
 704
 705        oidset_clear(&tip_oids);
 706        for (ref = unmatched; ref; ref = next) {
 707                next = ref->next;
 708                free(ref);
 709        }
 710
 711        *refs = newlist;
 712}
 713
 714static void mark_alternate_complete(struct object *obj)
 715{
 716        mark_complete(&obj->oid);
 717}
 718
 719struct loose_object_iter {
 720        struct oidset *loose_object_set;
 721        struct ref *refs;
 722};
 723
 724/*
 725 *  If the number of refs is not larger than the number of loose objects,
 726 *  this function stops inserting.
 727 */
 728static int add_loose_objects_to_set(const struct object_id *oid,
 729                                    const char *path,
 730                                    void *data)
 731{
 732        struct loose_object_iter *iter = data;
 733        oidset_insert(iter->loose_object_set, oid);
 734        if (iter->refs == NULL)
 735                return 1;
 736
 737        iter->refs = iter->refs->next;
 738        return 0;
 739}
 740
 741static int everything_local(struct fetch_pack_args *args,
 742                            struct ref **refs,
 743                            struct ref **sought, int nr_sought)
 744{
 745        struct ref *ref;
 746        int retval;
 747        int old_save_commit_buffer = save_commit_buffer;
 748        timestamp_t cutoff = 0;
 749        struct oidset loose_oid_set = OIDSET_INIT;
 750        int use_oidset = 0;
 751        struct loose_object_iter iter = {&loose_oid_set, *refs};
 752
 753        /* Enumerate all loose objects or know refs are not so many. */
 754        use_oidset = !for_each_loose_object(add_loose_objects_to_set,
 755                                            &iter, 0);
 756
 757        save_commit_buffer = 0;
 758
 759        for (ref = *refs; ref; ref = ref->next) {
 760                struct object *o;
 761                unsigned int flags = OBJECT_INFO_QUICK;
 762
 763                if (use_oidset &&
 764                    !oidset_contains(&loose_oid_set, &ref->old_oid)) {
 765                        /*
 766                         * I know this does not exist in the loose form,
 767                         * so check if it exists in a non-loose form.
 768                         */
 769                        flags |= OBJECT_INFO_IGNORE_LOOSE;
 770                }
 771
 772                if (!has_object_file_with_flags(&ref->old_oid, flags))
 773                        continue;
 774                o = parse_object(&ref->old_oid);
 775                if (!o)
 776                        continue;
 777
 778                /* We already have it -- which may mean that we were
 779                 * in sync with the other side at some time after
 780                 * that (it is OK if we guess wrong here).
 781                 */
 782                if (o->type == OBJ_COMMIT) {
 783                        struct commit *commit = (struct commit *)o;
 784                        if (!cutoff || cutoff < commit->date)
 785                                cutoff = commit->date;
 786                }
 787        }
 788
 789        oidset_clear(&loose_oid_set);
 790
 791        if (!args->no_dependents) {
 792                if (!args->deepen) {
 793                        for_each_ref(mark_complete_oid, NULL);
 794                        for_each_cached_alternate(mark_alternate_complete);
 795                        commit_list_sort_by_date(&complete);
 796                        if (cutoff)
 797                                mark_recent_complete_commits(args, cutoff);
 798                }
 799
 800                /*
 801                 * Mark all complete remote refs as common refs.
 802                 * Don't mark them common yet; the server has to be told so first.
 803                 */
 804                for (ref = *refs; ref; ref = ref->next) {
 805                        struct object *o = deref_tag(lookup_object(ref->old_oid.hash),
 806                                                     NULL, 0);
 807
 808                        if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
 809                                continue;
 810
 811                        if (!(o->flags & SEEN)) {
 812                                rev_list_push((struct commit *)o, COMMON_REF | SEEN);
 813
 814                                mark_common((struct commit *)o, 1, 1);
 815                        }
 816                }
 817        }
 818
 819        filter_refs(args, refs, sought, nr_sought);
 820
 821        for (retval = 1, ref = *refs; ref ; ref = ref->next) {
 822                const struct object_id *remote = &ref->old_oid;
 823                struct object *o;
 824
 825                o = lookup_object(remote->hash);
 826                if (!o || !(o->flags & COMPLETE)) {
 827                        retval = 0;
 828                        print_verbose(args, "want %s (%s)", oid_to_hex(remote),
 829                                      ref->name);
 830                        continue;
 831                }
 832                print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
 833                              ref->name);
 834        }
 835
 836        save_commit_buffer = old_save_commit_buffer;
 837
 838        return retval;
 839}
 840
 841static int sideband_demux(int in, int out, void *data)
 842{
 843        int *xd = data;
 844        int ret;
 845
 846        ret = recv_sideband("fetch-pack", xd[0], out);
 847        close(out);
 848        return ret;
 849}
 850
 851static int get_pack(struct fetch_pack_args *args,
 852                    int xd[2], char **pack_lockfile)
 853{
 854        struct async demux;
 855        int do_keep = args->keep_pack;
 856        const char *cmd_name;
 857        struct pack_header header;
 858        int pass_header = 0;
 859        struct child_process cmd = CHILD_PROCESS_INIT;
 860        int ret;
 861
 862        memset(&demux, 0, sizeof(demux));
 863        if (use_sideband) {
 864                /* xd[] is talking with upload-pack; subprocess reads from
 865                 * xd[0], spits out band#2 to stderr, and feeds us band#1
 866                 * through demux->out.
 867                 */
 868                demux.proc = sideband_demux;
 869                demux.data = xd;
 870                demux.out = -1;
 871                demux.isolate_sigpipe = 1;
 872                if (start_async(&demux))
 873                        die(_("fetch-pack: unable to fork off sideband demultiplexer"));
 874        }
 875        else
 876                demux.out = xd[0];
 877
 878        if (!args->keep_pack && unpack_limit) {
 879
 880                if (read_pack_header(demux.out, &header))
 881                        die(_("protocol error: bad pack header"));
 882                pass_header = 1;
 883                if (ntohl(header.hdr_entries) < unpack_limit)
 884                        do_keep = 0;
 885                else
 886                        do_keep = 1;
 887        }
 888
 889        if (alternate_shallow_file) {
 890                argv_array_push(&cmd.args, "--shallow-file");
 891                argv_array_push(&cmd.args, alternate_shallow_file);
 892        }
 893
 894        if (do_keep || args->from_promisor) {
 895                if (pack_lockfile)
 896                        cmd.out = -1;
 897                cmd_name = "index-pack";
 898                argv_array_push(&cmd.args, cmd_name);
 899                argv_array_push(&cmd.args, "--stdin");
 900                if (!args->quiet && !args->no_progress)
 901                        argv_array_push(&cmd.args, "-v");
 902                if (args->use_thin_pack)
 903                        argv_array_push(&cmd.args, "--fix-thin");
 904                if (do_keep && (args->lock_pack || unpack_limit)) {
 905                        char hostname[HOST_NAME_MAX + 1];
 906                        if (xgethostname(hostname, sizeof(hostname)))
 907                                xsnprintf(hostname, sizeof(hostname), "localhost");
 908                        argv_array_pushf(&cmd.args,
 909                                        "--keep=fetch-pack %"PRIuMAX " on %s",
 910                                        (uintmax_t)getpid(), hostname);
 911                }
 912                if (args->check_self_contained_and_connected)
 913                        argv_array_push(&cmd.args, "--check-self-contained-and-connected");
 914                if (args->from_promisor)
 915                        argv_array_push(&cmd.args, "--promisor");
 916        }
 917        else {
 918                cmd_name = "unpack-objects";
 919                argv_array_push(&cmd.args, cmd_name);
 920                if (args->quiet || args->no_progress)
 921                        argv_array_push(&cmd.args, "-q");
 922                args->check_self_contained_and_connected = 0;
 923        }
 924
 925        if (pass_header)
 926                argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
 927                                 ntohl(header.hdr_version),
 928                                 ntohl(header.hdr_entries));
 929        if (fetch_fsck_objects >= 0
 930            ? fetch_fsck_objects
 931            : transfer_fsck_objects >= 0
 932            ? transfer_fsck_objects
 933            : 0) {
 934                if (args->from_promisor)
 935                        /*
 936                         * We cannot use --strict in index-pack because it
 937                         * checks both broken objects and links, but we only
 938                         * want to check for broken objects.
 939                         */
 940                        argv_array_push(&cmd.args, "--fsck-objects");
 941                else
 942                        argv_array_pushf(&cmd.args, "--strict%s",
 943                                         fsck_msg_types.buf);
 944        }
 945
 946        cmd.in = demux.out;
 947        cmd.git_cmd = 1;
 948        if (start_command(&cmd))
 949                die(_("fetch-pack: unable to fork off %s"), cmd_name);
 950        if (do_keep && pack_lockfile) {
 951                *pack_lockfile = index_pack_lockfile(cmd.out);
 952                close(cmd.out);
 953        }
 954
 955        if (!use_sideband)
 956                /* Closed by start_command() */
 957                xd[0] = -1;
 958
 959        ret = finish_command(&cmd);
 960        if (!ret || (args->check_self_contained_and_connected && ret == 1))
 961                args->self_contained_and_connected =
 962                        args->check_self_contained_and_connected &&
 963                        ret == 0;
 964        else
 965                die(_("%s failed"), cmd_name);
 966        if (use_sideband && finish_async(&demux))
 967                die(_("error in sideband demultiplexer"));
 968        return 0;
 969}
 970
 971static int cmp_ref_by_name(const void *a_, const void *b_)
 972{
 973        const struct ref *a = *((const struct ref **)a_);
 974        const struct ref *b = *((const struct ref **)b_);
 975        return strcmp(a->name, b->name);
 976}
 977
 978static struct ref *do_fetch_pack(struct fetch_pack_args *args,
 979                                 int fd[2],
 980                                 const struct ref *orig_ref,
 981                                 struct ref **sought, int nr_sought,
 982                                 struct shallow_info *si,
 983                                 char **pack_lockfile)
 984{
 985        struct ref *ref = copy_ref_list(orig_ref);
 986        struct object_id oid;
 987        const char *agent_feature;
 988        int agent_len;
 989
 990        sort_ref_list(&ref, ref_compare_name);
 991        QSORT(sought, nr_sought, cmp_ref_by_name);
 992
 993        if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
 994                die(_("Server does not support shallow clients"));
 995        if (args->depth > 0 || args->deepen_since || args->deepen_not)
 996                args->deepen = 1;
 997        if (server_supports("multi_ack_detailed")) {
 998                print_verbose(args, _("Server supports multi_ack_detailed"));
 999                multi_ack = 2;
1000                if (server_supports("no-done")) {
1001                        print_verbose(args, _("Server supports no-done"));
1002                        if (args->stateless_rpc)
1003                                no_done = 1;
1004                }
1005        }
1006        else if (server_supports("multi_ack")) {
1007                print_verbose(args, _("Server supports multi_ack"));
1008                multi_ack = 1;
1009        }
1010        if (server_supports("side-band-64k")) {
1011                print_verbose(args, _("Server supports side-band-64k"));
1012                use_sideband = 2;
1013        }
1014        else if (server_supports("side-band")) {
1015                print_verbose(args, _("Server supports side-band"));
1016                use_sideband = 1;
1017        }
1018        if (server_supports("allow-tip-sha1-in-want")) {
1019                print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
1020                allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1021        }
1022        if (server_supports("allow-reachable-sha1-in-want")) {
1023                print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
1024                allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1025        }
1026        if (!server_supports("thin-pack"))
1027                args->use_thin_pack = 0;
1028        if (!server_supports("no-progress"))
1029                args->no_progress = 0;
1030        if (!server_supports("include-tag"))
1031                args->include_tag = 0;
1032        if (server_supports("ofs-delta"))
1033                print_verbose(args, _("Server supports ofs-delta"));
1034        else
1035                prefer_ofs_delta = 0;
1036
1037        if (server_supports("filter")) {
1038                server_supports_filtering = 1;
1039                print_verbose(args, _("Server supports filter"));
1040        } else if (args->filter_options.choice) {
1041                warning("filtering not recognized by server, ignoring");
1042        }
1043
1044        if ((agent_feature = server_feature_value("agent", &agent_len))) {
1045                agent_supported = 1;
1046                if (agent_len)
1047                        print_verbose(args, _("Server version is %.*s"),
1048                                      agent_len, agent_feature);
1049        }
1050        if (server_supports("deepen-since"))
1051                deepen_since_ok = 1;
1052        else if (args->deepen_since)
1053                die(_("Server does not support --shallow-since"));
1054        if (server_supports("deepen-not"))
1055                deepen_not_ok = 1;
1056        else if (args->deepen_not)
1057                die(_("Server does not support --shallow-exclude"));
1058        if (!server_supports("deepen-relative") && args->deepen_relative)
1059                die(_("Server does not support --deepen"));
1060
1061        if (everything_local(args, &ref, sought, nr_sought)) {
1062                packet_flush(fd[1]);
1063                goto all_done;
1064        }
1065        if (find_common(args, fd, &oid, ref) < 0)
1066                if (!args->keep_pack)
1067                        /* When cloning, it is not unusual to have
1068                         * no common commit.
1069                         */
1070                        warning(_("no common commits"));
1071
1072        if (args->stateless_rpc)
1073                packet_flush(fd[1]);
1074        if (args->deepen)
1075                setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1076                                        NULL);
1077        else if (si->nr_ours || si->nr_theirs)
1078                alternate_shallow_file = setup_temporary_shallow(si->shallow);
1079        else
1080                alternate_shallow_file = NULL;
1081        if (get_pack(args, fd, pack_lockfile))
1082                die(_("git fetch-pack: fetch failed."));
1083
1084 all_done:
1085        return ref;
1086}
1087
1088static void add_shallow_requests(struct strbuf *req_buf,
1089                                 const struct fetch_pack_args *args)
1090{
1091        if (is_repository_shallow(the_repository))
1092                write_shallow_commits(req_buf, 1, NULL);
1093        if (args->depth > 0)
1094                packet_buf_write(req_buf, "deepen %d", args->depth);
1095        if (args->deepen_since) {
1096                timestamp_t max_age = approxidate(args->deepen_since);
1097                packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1098        }
1099        if (args->deepen_not) {
1100                int i;
1101                for (i = 0; i < args->deepen_not->nr; i++) {
1102                        struct string_list_item *s = args->deepen_not->items + i;
1103                        packet_buf_write(req_buf, "deepen-not %s", s->string);
1104                }
1105        }
1106}
1107
1108static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1109{
1110        int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1111
1112        for ( ; wants ; wants = wants->next) {
1113                const struct object_id *remote = &wants->old_oid;
1114                struct object *o;
1115
1116                /*
1117                 * If that object is complete (i.e. it is an ancestor of a
1118                 * local ref), we tell them we have it but do not have to
1119                 * tell them about its ancestors, which they already know
1120                 * about.
1121                 *
1122                 * We use lookup_object here because we are only
1123                 * interested in the case we *know* the object is
1124                 * reachable and we have already scanned it.
1125                 */
1126                if (((o = lookup_object(remote->hash)) != NULL) &&
1127                    (o->flags & COMPLETE)) {
1128                        continue;
1129                }
1130
1131                if (!use_ref_in_want || wants->exact_oid)
1132                        packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1133                else
1134                        packet_buf_write(req_buf, "want-ref %s\n", wants->name);
1135        }
1136}
1137
1138static void add_common(struct strbuf *req_buf, struct oidset *common)
1139{
1140        struct oidset_iter iter;
1141        const struct object_id *oid;
1142        oidset_iter_init(common, &iter);
1143
1144        while ((oid = oidset_iter_next(&iter))) {
1145                packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1146        }
1147}
1148
1149static int add_haves(struct strbuf *req_buf, int *haves_to_send, int *in_vain)
1150{
1151        int ret = 0;
1152        int haves_added = 0;
1153        const struct object_id *oid;
1154
1155        while ((oid = get_rev())) {
1156                packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1157                if (++haves_added >= *haves_to_send)
1158                        break;
1159        }
1160
1161        *in_vain += haves_added;
1162        if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1163                /* Send Done */
1164                packet_buf_write(req_buf, "done\n");
1165                ret = 1;
1166        }
1167
1168        /* Increase haves to send on next round */
1169        *haves_to_send = next_flush(1, *haves_to_send);
1170
1171        return ret;
1172}
1173
1174static int send_fetch_request(int fd_out, const struct fetch_pack_args *args,
1175                              const struct ref *wants, struct oidset *common,
1176                              int *haves_to_send, int *in_vain)
1177{
1178        int ret = 0;
1179        struct strbuf req_buf = STRBUF_INIT;
1180
1181        if (server_supports_v2("fetch", 1))
1182                packet_buf_write(&req_buf, "command=fetch");
1183        if (server_supports_v2("agent", 0))
1184                packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
1185        if (args->server_options && args->server_options->nr &&
1186            server_supports_v2("server-option", 1)) {
1187                int i;
1188                for (i = 0; i < args->server_options->nr; i++)
1189                        packet_write_fmt(fd_out, "server-option=%s",
1190                                         args->server_options->items[i].string);
1191        }
1192
1193        packet_buf_delim(&req_buf);
1194        if (args->use_thin_pack)
1195                packet_buf_write(&req_buf, "thin-pack");
1196        if (args->no_progress)
1197                packet_buf_write(&req_buf, "no-progress");
1198        if (args->include_tag)
1199                packet_buf_write(&req_buf, "include-tag");
1200        if (prefer_ofs_delta)
1201                packet_buf_write(&req_buf, "ofs-delta");
1202
1203        /* Add shallow-info and deepen request */
1204        if (server_supports_feature("fetch", "shallow", 0))
1205                add_shallow_requests(&req_buf, args);
1206        else if (is_repository_shallow(the_repository) || args->deepen)
1207                die(_("Server does not support shallow requests"));
1208
1209        /* Add filter */
1210        if (server_supports_feature("fetch", "filter", 0) &&
1211            args->filter_options.choice) {
1212                print_verbose(args, _("Server supports filter"));
1213                packet_buf_write(&req_buf, "filter %s",
1214                                 args->filter_options.filter_spec);
1215        } else if (args->filter_options.choice) {
1216                warning("filtering not recognized by server, ignoring");
1217        }
1218
1219        /* add wants */
1220        add_wants(wants, &req_buf);
1221
1222        if (args->no_dependents) {
1223                packet_buf_write(&req_buf, "done");
1224                ret = 1;
1225        } else {
1226                /* Add all of the common commits we've found in previous rounds */
1227                add_common(&req_buf, common);
1228
1229                /* Add initial haves */
1230                ret = add_haves(&req_buf, haves_to_send, in_vain);
1231        }
1232
1233        /* Send request */
1234        packet_buf_flush(&req_buf);
1235        write_or_die(fd_out, req_buf.buf, req_buf.len);
1236
1237        strbuf_release(&req_buf);
1238        return ret;
1239}
1240
1241/*
1242 * Processes a section header in a server's response and checks if it matches
1243 * `section`.  If the value of `peek` is 1, the header line will be peeked (and
1244 * not consumed); if 0, the line will be consumed and the function will die if
1245 * the section header doesn't match what was expected.
1246 */
1247static int process_section_header(struct packet_reader *reader,
1248                                  const char *section, int peek)
1249{
1250        int ret;
1251
1252        if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1253                die("error reading section header '%s'", section);
1254
1255        ret = !strcmp(reader->line, section);
1256
1257        if (!peek) {
1258                if (!ret)
1259                        die("expected '%s', received '%s'",
1260                            section, reader->line);
1261                packet_reader_read(reader);
1262        }
1263
1264        return ret;
1265}
1266
1267static int process_acks(struct packet_reader *reader, struct oidset *common)
1268{
1269        /* received */
1270        int received_ready = 0;
1271        int received_ack = 0;
1272
1273        process_section_header(reader, "acknowledgments", 0);
1274        while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1275                const char *arg;
1276
1277                if (!strcmp(reader->line, "NAK"))
1278                        continue;
1279
1280                if (skip_prefix(reader->line, "ACK ", &arg)) {
1281                        struct object_id oid;
1282                        if (!get_oid_hex(arg, &oid)) {
1283                                struct commit *commit;
1284                                oidset_insert(common, &oid);
1285                                commit = lookup_commit(&oid);
1286                                mark_common(commit, 0, 1);
1287                        }
1288                        continue;
1289                }
1290
1291                if (!strcmp(reader->line, "ready")) {
1292                        clear_prio_queue(&rev_list);
1293                        received_ready = 1;
1294                        continue;
1295                }
1296
1297                die("unexpected acknowledgment line: '%s'", reader->line);
1298        }
1299
1300        if (reader->status != PACKET_READ_FLUSH &&
1301            reader->status != PACKET_READ_DELIM)
1302                die("error processing acks: %d", reader->status);
1303
1304        /* return 0 if no common, 1 if there are common, or 2 if ready */
1305        return received_ready ? 2 : (received_ack ? 1 : 0);
1306}
1307
1308static void receive_shallow_info(struct fetch_pack_args *args,
1309                                 struct packet_reader *reader)
1310{
1311        process_section_header(reader, "shallow-info", 0);
1312        while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1313                const char *arg;
1314                struct object_id oid;
1315
1316                if (skip_prefix(reader->line, "shallow ", &arg)) {
1317                        if (get_oid_hex(arg, &oid))
1318                                die(_("invalid shallow line: %s"), reader->line);
1319                        register_shallow(the_repository, &oid);
1320                        continue;
1321                }
1322                if (skip_prefix(reader->line, "unshallow ", &arg)) {
1323                        if (get_oid_hex(arg, &oid))
1324                                die(_("invalid unshallow line: %s"), reader->line);
1325                        if (!lookup_object(oid.hash))
1326                                die(_("object not found: %s"), reader->line);
1327                        /* make sure that it is parsed as shallow */
1328                        if (!parse_object(&oid))
1329                                die(_("error in object: %s"), reader->line);
1330                        if (unregister_shallow(&oid))
1331                                die(_("no shallow found: %s"), reader->line);
1332                        continue;
1333                }
1334                die(_("expected shallow/unshallow, got %s"), reader->line);
1335        }
1336
1337        if (reader->status != PACKET_READ_FLUSH &&
1338            reader->status != PACKET_READ_DELIM)
1339                die("error processing shallow info: %d", reader->status);
1340
1341        setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1342        args->deepen = 1;
1343}
1344
1345static void receive_wanted_refs(struct packet_reader *reader, struct ref *refs)
1346{
1347        process_section_header(reader, "wanted-refs", 0);
1348        while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1349                struct object_id oid;
1350                const char *end;
1351                struct ref *r = NULL;
1352
1353                if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
1354                        die("expected wanted-ref, got '%s'", reader->line);
1355
1356                for (r = refs; r; r = r->next) {
1357                        if (!strcmp(end, r->name)) {
1358                                oidcpy(&r->old_oid, &oid);
1359                                break;
1360                        }
1361                }
1362
1363                if (!r)
1364                        die("unexpected wanted-ref: '%s'", reader->line);
1365        }
1366
1367        if (reader->status != PACKET_READ_DELIM)
1368                die("error processing wanted refs: %d", reader->status);
1369}
1370
1371enum fetch_state {
1372        FETCH_CHECK_LOCAL = 0,
1373        FETCH_SEND_REQUEST,
1374        FETCH_PROCESS_ACKS,
1375        FETCH_GET_PACK,
1376        FETCH_DONE,
1377};
1378
1379static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1380                                    int fd[2],
1381                                    const struct ref *orig_ref,
1382                                    struct ref **sought, int nr_sought,
1383                                    char **pack_lockfile)
1384{
1385        struct ref *ref = copy_ref_list(orig_ref);
1386        enum fetch_state state = FETCH_CHECK_LOCAL;
1387        struct oidset common = OIDSET_INIT;
1388        struct packet_reader reader;
1389        int in_vain = 0;
1390        int haves_to_send = INITIAL_FLUSH;
1391        packet_reader_init(&reader, fd[0], NULL, 0,
1392                           PACKET_READ_CHOMP_NEWLINE);
1393
1394        while (state != FETCH_DONE) {
1395                switch (state) {
1396                case FETCH_CHECK_LOCAL:
1397                        sort_ref_list(&ref, ref_compare_name);
1398                        QSORT(sought, nr_sought, cmp_ref_by_name);
1399
1400                        /* v2 supports these by default */
1401                        allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1402                        use_sideband = 2;
1403                        if (args->depth > 0 || args->deepen_since || args->deepen_not)
1404                                args->deepen = 1;
1405
1406                        if (marked)
1407                                for_each_ref(clear_marks, NULL);
1408                        marked = 1;
1409
1410                        for_each_ref(rev_list_insert_ref_oid, NULL);
1411                        for_each_cached_alternate(insert_one_alternate_object);
1412
1413                        /* Filter 'ref' by 'sought' and those that aren't local */
1414                        if (everything_local(args, &ref, sought, nr_sought))
1415                                state = FETCH_DONE;
1416                        else
1417                                state = FETCH_SEND_REQUEST;
1418                        break;
1419                case FETCH_SEND_REQUEST:
1420                        if (send_fetch_request(fd[1], args, ref, &common,
1421                                               &haves_to_send, &in_vain))
1422                                state = FETCH_GET_PACK;
1423                        else
1424                                state = FETCH_PROCESS_ACKS;
1425                        break;
1426                case FETCH_PROCESS_ACKS:
1427                        /* Process ACKs/NAKs */
1428                        switch (process_acks(&reader, &common)) {
1429                        case 2:
1430                                state = FETCH_GET_PACK;
1431                                break;
1432                        case 1:
1433                                in_vain = 0;
1434                                /* fallthrough */
1435                        default:
1436                                state = FETCH_SEND_REQUEST;
1437                                break;
1438                        }
1439                        break;
1440                case FETCH_GET_PACK:
1441                        /* Check for shallow-info section */
1442                        if (process_section_header(&reader, "shallow-info", 1))
1443                                receive_shallow_info(args, &reader);
1444
1445                        if (process_section_header(&reader, "wanted-refs", 1))
1446                                receive_wanted_refs(&reader, ref);
1447
1448                        /* get the pack */
1449                        process_section_header(&reader, "packfile", 0);
1450                        if (get_pack(args, fd, pack_lockfile))
1451                                die(_("git fetch-pack: fetch failed."));
1452
1453                        state = FETCH_DONE;
1454                        break;
1455                case FETCH_DONE:
1456                        continue;
1457                }
1458        }
1459
1460        oidset_clear(&common);
1461        return ref;
1462}
1463
1464static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1465{
1466        if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1467                const char *path;
1468
1469                if (git_config_pathname(&path, var, value))
1470                        return 1;
1471                strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1472                        fsck_msg_types.len ? ',' : '=', path);
1473                free((char *)path);
1474                return 0;
1475        }
1476
1477        if (skip_prefix(var, "fetch.fsck.", &var)) {
1478                if (is_valid_msg_type(var, value))
1479                        strbuf_addf(&fsck_msg_types, "%c%s=%s",
1480                                fsck_msg_types.len ? ',' : '=', var, value);
1481                else
1482                        warning("Skipping unknown msg id '%s'", var);
1483                return 0;
1484        }
1485
1486        return git_default_config(var, value, cb);
1487}
1488
1489static void fetch_pack_config(void)
1490{
1491        git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1492        git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1493        git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1494        git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1495        git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
1496
1497        git_config(fetch_pack_config_cb, NULL);
1498}
1499
1500static void fetch_pack_setup(void)
1501{
1502        static int did_setup;
1503        if (did_setup)
1504                return;
1505        fetch_pack_config();
1506        if (0 <= transfer_unpack_limit)
1507                unpack_limit = transfer_unpack_limit;
1508        else if (0 <= fetch_unpack_limit)
1509                unpack_limit = fetch_unpack_limit;
1510        did_setup = 1;
1511}
1512
1513static int remove_duplicates_in_refs(struct ref **ref, int nr)
1514{
1515        struct string_list names = STRING_LIST_INIT_NODUP;
1516        int src, dst;
1517
1518        for (src = dst = 0; src < nr; src++) {
1519                struct string_list_item *item;
1520                item = string_list_insert(&names, ref[src]->name);
1521                if (item->util)
1522                        continue; /* already have it */
1523                item->util = ref[src];
1524                if (src != dst)
1525                        ref[dst] = ref[src];
1526                dst++;
1527        }
1528        for (src = dst; src < nr; src++)
1529                ref[src] = NULL;
1530        string_list_clear(&names, 0);
1531        return dst;
1532}
1533
1534static void update_shallow(struct fetch_pack_args *args,
1535                           struct ref *refs,
1536                           struct shallow_info *si)
1537{
1538        struct oid_array ref = OID_ARRAY_INIT;
1539        int *status;
1540        int i;
1541        struct ref *r;
1542
1543        if (args->deepen && alternate_shallow_file) {
1544                if (*alternate_shallow_file == '\0') { /* --unshallow */
1545                        unlink_or_warn(git_path_shallow(the_repository));
1546                        rollback_lock_file(&shallow_lock);
1547                } else
1548                        commit_lock_file(&shallow_lock);
1549                return;
1550        }
1551
1552        if (!si->shallow || !si->shallow->nr)
1553                return;
1554
1555        if (args->cloning) {
1556                /*
1557                 * remote is shallow, but this is a clone, there are
1558                 * no objects in repo to worry about. Accept any
1559                 * shallow points that exist in the pack (iow in repo
1560                 * after get_pack() and reprepare_packed_git())
1561                 */
1562                struct oid_array extra = OID_ARRAY_INIT;
1563                struct object_id *oid = si->shallow->oid;
1564                for (i = 0; i < si->shallow->nr; i++)
1565                        if (has_object_file(&oid[i]))
1566                                oid_array_append(&extra, &oid[i]);
1567                if (extra.nr) {
1568                        setup_alternate_shallow(&shallow_lock,
1569                                                &alternate_shallow_file,
1570                                                &extra);
1571                        commit_lock_file(&shallow_lock);
1572                }
1573                oid_array_clear(&extra);
1574                return;
1575        }
1576
1577        if (!si->nr_ours && !si->nr_theirs)
1578                return;
1579
1580        remove_nonexistent_theirs_shallow(si);
1581        if (!si->nr_ours && !si->nr_theirs)
1582                return;
1583        for (r = refs; r; r = r->next)
1584                oid_array_append(&ref, &r->old_oid);
1585        si->ref = &ref;
1586
1587        if (args->update_shallow) {
1588                /*
1589                 * remote is also shallow, .git/shallow may be updated
1590                 * so all refs can be accepted. Make sure we only add
1591                 * shallow roots that are actually reachable from new
1592                 * refs.
1593                 */
1594                struct oid_array extra = OID_ARRAY_INIT;
1595                struct object_id *oid = si->shallow->oid;
1596                assign_shallow_commits_to_refs(si, NULL, NULL);
1597                if (!si->nr_ours && !si->nr_theirs) {
1598                        oid_array_clear(&ref);
1599                        return;
1600                }
1601                for (i = 0; i < si->nr_ours; i++)
1602                        oid_array_append(&extra, &oid[si->ours[i]]);
1603                for (i = 0; i < si->nr_theirs; i++)
1604                        oid_array_append(&extra, &oid[si->theirs[i]]);
1605                setup_alternate_shallow(&shallow_lock,
1606                                        &alternate_shallow_file,
1607                                        &extra);
1608                commit_lock_file(&shallow_lock);
1609                oid_array_clear(&extra);
1610                oid_array_clear(&ref);
1611                return;
1612        }
1613
1614        /*
1615         * remote is also shallow, check what ref is safe to update
1616         * without updating .git/shallow
1617         */
1618        status = xcalloc(ref.nr, sizeof(*status));
1619        assign_shallow_commits_to_refs(si, NULL, status);
1620        if (si->nr_ours || si->nr_theirs) {
1621                for (r = refs, i = 0; r; r = r->next, i++)
1622                        if (status[i])
1623                                r->status = REF_STATUS_REJECT_SHALLOW;
1624        }
1625        free(status);
1626        oid_array_clear(&ref);
1627}
1628
1629static int iterate_ref_map(void *cb_data, struct object_id *oid)
1630{
1631        struct ref **rm = cb_data;
1632        struct ref *ref = *rm;
1633
1634        if (!ref)
1635                return -1; /* end of the list */
1636        *rm = ref->next;
1637        oidcpy(oid, &ref->old_oid);
1638        return 0;
1639}
1640
1641struct ref *fetch_pack(struct fetch_pack_args *args,
1642                       int fd[], struct child_process *conn,
1643                       const struct ref *ref,
1644                       const char *dest,
1645                       struct ref **sought, int nr_sought,
1646                       struct oid_array *shallow,
1647                       char **pack_lockfile,
1648                       enum protocol_version version)
1649{
1650        struct ref *ref_cpy;
1651        struct shallow_info si;
1652
1653        fetch_pack_setup();
1654        if (nr_sought)
1655                nr_sought = remove_duplicates_in_refs(sought, nr_sought);
1656
1657        if (!ref) {
1658                packet_flush(fd[1]);
1659                die(_("no matching remote head"));
1660        }
1661        prepare_shallow_info(&si, shallow);
1662        if (version == protocol_v2)
1663                ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1664                                           pack_lockfile);
1665        else
1666                ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1667                                        &si, pack_lockfile);
1668        reprepare_packed_git(the_repository);
1669
1670        if (!args->cloning && args->deepen) {
1671                struct check_connected_options opt = CHECK_CONNECTED_INIT;
1672                struct ref *iterator = ref_cpy;
1673                opt.shallow_file = alternate_shallow_file;
1674                if (args->deepen)
1675                        opt.is_deepening_fetch = 1;
1676                if (check_connected(iterate_ref_map, &iterator, &opt)) {
1677                        error(_("remote did not send all necessary objects"));
1678                        free_refs(ref_cpy);
1679                        ref_cpy = NULL;
1680                        rollback_lock_file(&shallow_lock);
1681                        goto cleanup;
1682                }
1683                args->connectivity_checked = 1;
1684        }
1685
1686        update_shallow(args, ref_cpy, &si);
1687cleanup:
1688        clear_shallow_info(&si);
1689        return ref_cpy;
1690}
1691
1692int report_unmatched_refs(struct ref **sought, int nr_sought)
1693{
1694        int i, ret = 0;
1695
1696        for (i = 0; i < nr_sought; i++) {
1697                if (!sought[i])
1698                        continue;
1699                switch (sought[i]->match_status) {
1700                case REF_MATCHED:
1701                        continue;
1702                case REF_NOT_MATCHED:
1703                        error(_("no such remote ref %s"), sought[i]->name);
1704                        break;
1705                case REF_UNADVERTISED_NOT_ALLOWED:
1706                        error(_("Server does not allow request for unadvertised object %s"),
1707                              sought[i]->name);
1708                        break;
1709                }
1710                ret = 1;
1711        }
1712        return ret;
1713}