shallow.con commit commit: add repository argument to register_commit_graft (3f5787f)
   1#include "cache.h"
   2#include "repository.h"
   3#include "tempfile.h"
   4#include "lockfile.h"
   5#include "object-store.h"
   6#include "commit.h"
   7#include "tag.h"
   8#include "pkt-line.h"
   9#include "remote.h"
  10#include "refs.h"
  11#include "sha1-array.h"
  12#include "diff.h"
  13#include "revision.h"
  14#include "commit-slab.h"
  15#include "revision.h"
  16#include "list-objects.h"
  17
  18static int is_shallow = -1;
  19static struct stat_validity shallow_stat;
  20static char *alternate_shallow_file;
  21
  22void set_alternate_shallow_file(const char *path, int override)
  23{
  24        if (is_shallow != -1)
  25                die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
  26        if (alternate_shallow_file && !override)
  27                return;
  28        free(alternate_shallow_file);
  29        alternate_shallow_file = xstrdup_or_null(path);
  30}
  31
  32int register_shallow(const struct object_id *oid)
  33{
  34        struct commit_graft *graft =
  35                xmalloc(sizeof(struct commit_graft));
  36        struct commit *commit = lookup_commit(oid);
  37
  38        oidcpy(&graft->oid, oid);
  39        graft->nr_parent = -1;
  40        if (commit && commit->object.parsed)
  41                commit->parents = NULL;
  42        return register_commit_graft(the_repository, graft, 0);
  43}
  44
  45int is_repository_shallow(void)
  46{
  47        FILE *fp;
  48        char buf[1024];
  49        const char *path = alternate_shallow_file;
  50
  51        if (is_shallow >= 0)
  52                return is_shallow;
  53
  54        if (!path)
  55                path = git_path_shallow();
  56        /*
  57         * fetch-pack sets '--shallow-file ""' as an indicator that no
  58         * shallow file should be used. We could just open it and it
  59         * will likely fail. But let's do an explicit check instead.
  60         */
  61        if (!*path || (fp = fopen(path, "r")) == NULL) {
  62                stat_validity_clear(&shallow_stat);
  63                is_shallow = 0;
  64                return is_shallow;
  65        }
  66        stat_validity_update(&shallow_stat, fileno(fp));
  67        is_shallow = 1;
  68
  69        while (fgets(buf, sizeof(buf), fp)) {
  70                struct object_id oid;
  71                if (get_oid_hex(buf, &oid))
  72                        die("bad shallow line: %s", buf);
  73                register_shallow(&oid);
  74        }
  75        fclose(fp);
  76        return is_shallow;
  77}
  78
  79struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
  80                int shallow_flag, int not_shallow_flag)
  81{
  82        int i = 0, cur_depth = 0;
  83        struct commit_list *result = NULL;
  84        struct object_array stack = OBJECT_ARRAY_INIT;
  85        struct commit *commit = NULL;
  86        struct commit_graft *graft;
  87
  88        while (commit || i < heads->nr || stack.nr) {
  89                struct commit_list *p;
  90                if (!commit) {
  91                        if (i < heads->nr) {
  92                                commit = (struct commit *)
  93                                        deref_tag(heads->objects[i++].item, NULL, 0);
  94                                if (!commit || commit->object.type != OBJ_COMMIT) {
  95                                        commit = NULL;
  96                                        continue;
  97                                }
  98                                if (!commit->util)
  99                                        commit->util = xmalloc(sizeof(int));
 100                                *(int *)commit->util = 0;
 101                                cur_depth = 0;
 102                        } else {
 103                                commit = (struct commit *)
 104                                        object_array_pop(&stack);
 105                                cur_depth = *(int *)commit->util;
 106                        }
 107                }
 108                parse_commit_or_die(commit);
 109                cur_depth++;
 110                if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
 111                    (is_repository_shallow() && !commit->parents &&
 112                     (graft = lookup_commit_graft(&commit->object.oid)) != NULL &&
 113                     graft->nr_parent < 0)) {
 114                        commit_list_insert(commit, &result);
 115                        commit->object.flags |= shallow_flag;
 116                        commit = NULL;
 117                        continue;
 118                }
 119                commit->object.flags |= not_shallow_flag;
 120                for (p = commit->parents, commit = NULL; p; p = p->next) {
 121                        if (!p->item->util) {
 122                                int *pointer = xmalloc(sizeof(int));
 123                                p->item->util = pointer;
 124                                *pointer =  cur_depth;
 125                        } else {
 126                                int *pointer = p->item->util;
 127                                if (cur_depth >= *pointer)
 128                                        continue;
 129                                *pointer = cur_depth;
 130                        }
 131                        if (p->next)
 132                                add_object_array(&p->item->object,
 133                                                NULL, &stack);
 134                        else {
 135                                commit = p->item;
 136                                cur_depth = *(int *)commit->util;
 137                        }
 138                }
 139        }
 140
 141        return result;
 142}
 143
 144static void show_commit(struct commit *commit, void *data)
 145{
 146        commit_list_insert(commit, data);
 147}
 148
 149/*
 150 * Given rev-list arguments, run rev-list. All reachable commits
 151 * except border ones are marked with not_shallow_flag. Border commits
 152 * are marked with shallow_flag. The list of border/shallow commits
 153 * are also returned.
 154 */
 155struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
 156                                                    int shallow_flag,
 157                                                    int not_shallow_flag)
 158{
 159        struct commit_list *result = NULL, *p;
 160        struct commit_list *not_shallow_list = NULL;
 161        struct rev_info revs;
 162        int both_flags = shallow_flag | not_shallow_flag;
 163
 164        /*
 165         * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
 166         * set at this point. But better be safe than sorry.
 167         */
 168        clear_object_flags(both_flags);
 169
 170        is_repository_shallow(); /* make sure shallows are read */
 171
 172        init_revisions(&revs, NULL);
 173        save_commit_buffer = 0;
 174        setup_revisions(ac, av, &revs, NULL);
 175
 176        if (prepare_revision_walk(&revs))
 177                die("revision walk setup failed");
 178        traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
 179
 180        /* Mark all reachable commits as NOT_SHALLOW */
 181        for (p = not_shallow_list; p; p = p->next)
 182                p->item->object.flags |= not_shallow_flag;
 183
 184        /*
 185         * mark border commits SHALLOW + NOT_SHALLOW.
 186         * We cannot clear NOT_SHALLOW right now. Imagine border
 187         * commit A is processed first, then commit B, whose parent is
 188         * A, later. If NOT_SHALLOW on A is cleared at step 1, B
 189         * itself is considered border at step 2, which is incorrect.
 190         */
 191        for (p = not_shallow_list; p; p = p->next) {
 192                struct commit *c = p->item;
 193                struct commit_list *parent;
 194
 195                if (parse_commit(c))
 196                        die("unable to parse commit %s",
 197                            oid_to_hex(&c->object.oid));
 198
 199                for (parent = c->parents; parent; parent = parent->next)
 200                        if (!(parent->item->object.flags & not_shallow_flag)) {
 201                                c->object.flags |= shallow_flag;
 202                                commit_list_insert(c, &result);
 203                                break;
 204                        }
 205        }
 206        free_commit_list(not_shallow_list);
 207
 208        /*
 209         * Now we can clean up NOT_SHALLOW on border commits. Having
 210         * both flags set can confuse the caller.
 211         */
 212        for (p = result; p; p = p->next) {
 213                struct object *o = &p->item->object;
 214                if ((o->flags & both_flags) == both_flags)
 215                        o->flags &= ~not_shallow_flag;
 216        }
 217        return result;
 218}
 219
 220static void check_shallow_file_for_update(void)
 221{
 222        if (is_shallow == -1)
 223                die("BUG: shallow must be initialized by now");
 224
 225        if (!stat_validity_check(&shallow_stat, git_path_shallow()))
 226                die("shallow file has changed since we read it");
 227}
 228
 229#define SEEN_ONLY 1
 230#define VERBOSE   2
 231
 232struct write_shallow_data {
 233        struct strbuf *out;
 234        int use_pack_protocol;
 235        int count;
 236        unsigned flags;
 237};
 238
 239static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
 240{
 241        struct write_shallow_data *data = cb_data;
 242        const char *hex = oid_to_hex(&graft->oid);
 243        if (graft->nr_parent != -1)
 244                return 0;
 245        if (data->flags & SEEN_ONLY) {
 246                struct commit *c = lookup_commit(&graft->oid);
 247                if (!c || !(c->object.flags & SEEN)) {
 248                        if (data->flags & VERBOSE)
 249                                printf("Removing %s from .git/shallow\n",
 250                                       oid_to_hex(&c->object.oid));
 251                        return 0;
 252                }
 253        }
 254        data->count++;
 255        if (data->use_pack_protocol)
 256                packet_buf_write(data->out, "shallow %s", hex);
 257        else {
 258                strbuf_addstr(data->out, hex);
 259                strbuf_addch(data->out, '\n');
 260        }
 261        return 0;
 262}
 263
 264static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
 265                                   const struct oid_array *extra,
 266                                   unsigned flags)
 267{
 268        struct write_shallow_data data;
 269        int i;
 270        data.out = out;
 271        data.use_pack_protocol = use_pack_protocol;
 272        data.count = 0;
 273        data.flags = flags;
 274        for_each_commit_graft(write_one_shallow, &data);
 275        if (!extra)
 276                return data.count;
 277        for (i = 0; i < extra->nr; i++) {
 278                strbuf_addstr(out, oid_to_hex(extra->oid + i));
 279                strbuf_addch(out, '\n');
 280                data.count++;
 281        }
 282        return data.count;
 283}
 284
 285int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
 286                          const struct oid_array *extra)
 287{
 288        return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
 289}
 290
 291const char *setup_temporary_shallow(const struct oid_array *extra)
 292{
 293        struct tempfile *temp;
 294        struct strbuf sb = STRBUF_INIT;
 295
 296        if (write_shallow_commits(&sb, 0, extra)) {
 297                temp = xmks_tempfile(git_path("shallow_XXXXXX"));
 298
 299                if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
 300                    close_tempfile_gently(temp) < 0)
 301                        die_errno("failed to write to %s",
 302                                  get_tempfile_path(temp));
 303                strbuf_release(&sb);
 304                return get_tempfile_path(temp);
 305        }
 306        /*
 307         * is_repository_shallow() sees empty string as "no shallow
 308         * file".
 309         */
 310        return "";
 311}
 312
 313void setup_alternate_shallow(struct lock_file *shallow_lock,
 314                             const char **alternate_shallow_file,
 315                             const struct oid_array *extra)
 316{
 317        struct strbuf sb = STRBUF_INIT;
 318        int fd;
 319
 320        fd = hold_lock_file_for_update(shallow_lock, git_path_shallow(),
 321                                       LOCK_DIE_ON_ERROR);
 322        check_shallow_file_for_update();
 323        if (write_shallow_commits(&sb, 0, extra)) {
 324                if (write_in_full(fd, sb.buf, sb.len) < 0)
 325                        die_errno("failed to write to %s",
 326                                  get_lock_file_path(shallow_lock));
 327                *alternate_shallow_file = get_lock_file_path(shallow_lock);
 328        } else
 329                /*
 330                 * is_repository_shallow() sees empty string as "no
 331                 * shallow file".
 332                 */
 333                *alternate_shallow_file = "";
 334        strbuf_release(&sb);
 335}
 336
 337static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
 338{
 339        int fd = *(int *)cb;
 340        if (graft->nr_parent == -1)
 341                packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
 342        return 0;
 343}
 344
 345void advertise_shallow_grafts(int fd)
 346{
 347        if (!is_repository_shallow())
 348                return;
 349        for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
 350}
 351
 352/*
 353 * mark_reachable_objects() should have been run prior to this and all
 354 * reachable commits marked as "SEEN".
 355 */
 356void prune_shallow(int show_only)
 357{
 358        static struct lock_file shallow_lock;
 359        struct strbuf sb = STRBUF_INIT;
 360        int fd;
 361
 362        if (show_only) {
 363                write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
 364                strbuf_release(&sb);
 365                return;
 366        }
 367        fd = hold_lock_file_for_update(&shallow_lock, git_path_shallow(),
 368                                       LOCK_DIE_ON_ERROR);
 369        check_shallow_file_for_update();
 370        if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
 371                if (write_in_full(fd, sb.buf, sb.len) < 0)
 372                        die_errno("failed to write to %s",
 373                                  get_lock_file_path(&shallow_lock));
 374                commit_lock_file(&shallow_lock);
 375        } else {
 376                unlink(git_path_shallow());
 377                rollback_lock_file(&shallow_lock);
 378        }
 379        strbuf_release(&sb);
 380}
 381
 382struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
 383
 384/*
 385 * Step 1, split sender shallow commits into "ours" and "theirs"
 386 * Step 2, clean "ours" based on .git/shallow
 387 */
 388void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
 389{
 390        int i;
 391        trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
 392        memset(info, 0, sizeof(*info));
 393        info->shallow = sa;
 394        if (!sa)
 395                return;
 396        ALLOC_ARRAY(info->ours, sa->nr);
 397        ALLOC_ARRAY(info->theirs, sa->nr);
 398        for (i = 0; i < sa->nr; i++) {
 399                if (has_object_file(sa->oid + i)) {
 400                        struct commit_graft *graft;
 401                        graft = lookup_commit_graft(&sa->oid[i]);
 402                        if (graft && graft->nr_parent < 0)
 403                                continue;
 404                        info->ours[info->nr_ours++] = i;
 405                } else
 406                        info->theirs[info->nr_theirs++] = i;
 407        }
 408}
 409
 410void clear_shallow_info(struct shallow_info *info)
 411{
 412        free(info->ours);
 413        free(info->theirs);
 414}
 415
 416/* Step 4, remove non-existent ones in "theirs" after getting the pack */
 417
 418void remove_nonexistent_theirs_shallow(struct shallow_info *info)
 419{
 420        struct object_id *oid = info->shallow->oid;
 421        int i, dst;
 422        trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
 423        for (i = dst = 0; i < info->nr_theirs; i++) {
 424                if (i != dst)
 425                        info->theirs[dst] = info->theirs[i];
 426                if (has_object_file(oid + info->theirs[i]))
 427                        dst++;
 428        }
 429        info->nr_theirs = dst;
 430}
 431
 432define_commit_slab(ref_bitmap, uint32_t *);
 433
 434#define POOL_SIZE (512 * 1024)
 435
 436struct paint_info {
 437        struct ref_bitmap ref_bitmap;
 438        unsigned nr_bits;
 439        char **pools;
 440        char *free, *end;
 441        unsigned pool_count;
 442};
 443
 444static uint32_t *paint_alloc(struct paint_info *info)
 445{
 446        unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
 447        unsigned size = nr * sizeof(uint32_t);
 448        void *p;
 449        if (!info->pool_count || size > info->end - info->free) {
 450                if (size > POOL_SIZE)
 451                        die("BUG: pool size too small for %d in paint_alloc()",
 452                            size);
 453                info->pool_count++;
 454                REALLOC_ARRAY(info->pools, info->pool_count);
 455                info->free = xmalloc(POOL_SIZE);
 456                info->pools[info->pool_count - 1] = info->free;
 457                info->end = info->free + POOL_SIZE;
 458        }
 459        p = info->free;
 460        info->free += size;
 461        return p;
 462}
 463
 464/*
 465 * Given a commit SHA-1, walk down to parents until either SEEN,
 466 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
 467 * all walked commits.
 468 */
 469static void paint_down(struct paint_info *info, const struct object_id *oid,
 470                       unsigned int id)
 471{
 472        unsigned int i, nr;
 473        struct commit_list *head = NULL;
 474        int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
 475        size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
 476        struct commit *c = lookup_commit_reference_gently(oid, 1);
 477        uint32_t *tmp; /* to be freed before return */
 478        uint32_t *bitmap;
 479
 480        if (!c)
 481                return;
 482
 483        tmp = xmalloc(bitmap_size);
 484        bitmap = paint_alloc(info);
 485        memset(bitmap, 0, bitmap_size);
 486        bitmap[id / 32] |= (1U << (id % 32));
 487        commit_list_insert(c, &head);
 488        while (head) {
 489                struct commit_list *p;
 490                struct commit *c = pop_commit(&head);
 491                uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
 492
 493                /* XXX check "UNINTERESTING" from pack bitmaps if available */
 494                if (c->object.flags & (SEEN | UNINTERESTING))
 495                        continue;
 496                else
 497                        c->object.flags |= SEEN;
 498
 499                if (*refs == NULL)
 500                        *refs = bitmap;
 501                else {
 502                        memcpy(tmp, *refs, bitmap_size);
 503                        for (i = 0; i < bitmap_nr; i++)
 504                                tmp[i] |= bitmap[i];
 505                        if (memcmp(tmp, *refs, bitmap_size)) {
 506                                *refs = paint_alloc(info);
 507                                memcpy(*refs, tmp, bitmap_size);
 508                        }
 509                }
 510
 511                if (c->object.flags & BOTTOM)
 512                        continue;
 513
 514                if (parse_commit(c))
 515                        die("unable to parse commit %s",
 516                            oid_to_hex(&c->object.oid));
 517
 518                for (p = c->parents; p; p = p->next) {
 519                        if (p->item->object.flags & SEEN)
 520                                continue;
 521                        commit_list_insert(p->item, &head);
 522                }
 523        }
 524
 525        nr = get_max_object_index();
 526        for (i = 0; i < nr; i++) {
 527                struct object *o = get_indexed_object(i);
 528                if (o && o->type == OBJ_COMMIT)
 529                        o->flags &= ~SEEN;
 530        }
 531
 532        free(tmp);
 533}
 534
 535static int mark_uninteresting(const char *refname, const struct object_id *oid,
 536                              int flags, void *cb_data)
 537{
 538        struct commit *commit = lookup_commit_reference_gently(oid, 1);
 539        if (!commit)
 540                return 0;
 541        commit->object.flags |= UNINTERESTING;
 542        mark_parents_uninteresting(commit);
 543        return 0;
 544}
 545
 546static void post_assign_shallow(struct shallow_info *info,
 547                                struct ref_bitmap *ref_bitmap,
 548                                int *ref_status);
 549/*
 550 * Step 6(+7), associate shallow commits with new refs
 551 *
 552 * info->ref must be initialized before calling this function.
 553 *
 554 * If used is not NULL, it's an array of info->shallow->nr
 555 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
 556 * m-th shallow commit from info->shallow.
 557 *
 558 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
 559 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
 560 * the ref needs some shallow commits from either info->ours or
 561 * info->theirs.
 562 */
 563void assign_shallow_commits_to_refs(struct shallow_info *info,
 564                                    uint32_t **used, int *ref_status)
 565{
 566        struct object_id *oid = info->shallow->oid;
 567        struct oid_array *ref = info->ref;
 568        unsigned int i, nr;
 569        int *shallow, nr_shallow = 0;
 570        struct paint_info pi;
 571
 572        trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
 573        ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
 574        for (i = 0; i < info->nr_ours; i++)
 575                shallow[nr_shallow++] = info->ours[i];
 576        for (i = 0; i < info->nr_theirs; i++)
 577                shallow[nr_shallow++] = info->theirs[i];
 578
 579        /*
 580         * Prepare the commit graph to track what refs can reach what
 581         * (new) shallow commits.
 582         */
 583        nr = get_max_object_index();
 584        for (i = 0; i < nr; i++) {
 585                struct object *o = get_indexed_object(i);
 586                if (!o || o->type != OBJ_COMMIT)
 587                        continue;
 588
 589                o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
 590        }
 591
 592        memset(&pi, 0, sizeof(pi));
 593        init_ref_bitmap(&pi.ref_bitmap);
 594        pi.nr_bits = ref->nr;
 595
 596        /*
 597         * "--not --all" to cut short the traversal if new refs
 598         * connect to old refs. If not (e.g. force ref updates) it'll
 599         * have to go down to the current shallow commits.
 600         */
 601        head_ref(mark_uninteresting, NULL);
 602        for_each_ref(mark_uninteresting, NULL);
 603
 604        /* Mark potential bottoms so we won't go out of bound */
 605        for (i = 0; i < nr_shallow; i++) {
 606                struct commit *c = lookup_commit(&oid[shallow[i]]);
 607                c->object.flags |= BOTTOM;
 608        }
 609
 610        for (i = 0; i < ref->nr; i++)
 611                paint_down(&pi, ref->oid + i, i);
 612
 613        if (used) {
 614                int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
 615                memset(used, 0, sizeof(*used) * info->shallow->nr);
 616                for (i = 0; i < nr_shallow; i++) {
 617                        const struct commit *c = lookup_commit(&oid[shallow[i]]);
 618                        uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
 619                        if (*map)
 620                                used[shallow[i]] = xmemdupz(*map, bitmap_size);
 621                }
 622                /*
 623                 * unreachable shallow commits are not removed from
 624                 * "ours" and "theirs". The user is supposed to run
 625                 * step 7 on every ref separately and not trust "ours"
 626                 * and "theirs" any more.
 627                 */
 628        } else
 629                post_assign_shallow(info, &pi.ref_bitmap, ref_status);
 630
 631        clear_ref_bitmap(&pi.ref_bitmap);
 632        for (i = 0; i < pi.pool_count; i++)
 633                free(pi.pools[i]);
 634        free(pi.pools);
 635        free(shallow);
 636}
 637
 638struct commit_array {
 639        struct commit **commits;
 640        int nr, alloc;
 641};
 642
 643static int add_ref(const char *refname, const struct object_id *oid,
 644                   int flags, void *cb_data)
 645{
 646        struct commit_array *ca = cb_data;
 647        ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
 648        ca->commits[ca->nr] = lookup_commit_reference_gently(oid, 1);
 649        if (ca->commits[ca->nr])
 650                ca->nr++;
 651        return 0;
 652}
 653
 654static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
 655{
 656        unsigned int i;
 657        if (!ref_status)
 658                return;
 659        for (i = 0; i < nr; i++)
 660                if (bitmap[i / 32] & (1U << (i % 32)))
 661                        ref_status[i]++;
 662}
 663
 664/*
 665 * Step 7, reachability test on "ours" at commit level
 666 */
 667static void post_assign_shallow(struct shallow_info *info,
 668                                struct ref_bitmap *ref_bitmap,
 669                                int *ref_status)
 670{
 671        struct object_id *oid = info->shallow->oid;
 672        struct commit *c;
 673        uint32_t **bitmap;
 674        int dst, i, j;
 675        int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
 676        struct commit_array ca;
 677
 678        trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
 679        if (ref_status)
 680                memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
 681
 682        /* Remove unreachable shallow commits from "theirs" */
 683        for (i = dst = 0; i < info->nr_theirs; i++) {
 684                if (i != dst)
 685                        info->theirs[dst] = info->theirs[i];
 686                c = lookup_commit(&oid[info->theirs[i]]);
 687                bitmap = ref_bitmap_at(ref_bitmap, c);
 688                if (!*bitmap)
 689                        continue;
 690                for (j = 0; j < bitmap_nr; j++)
 691                        if (bitmap[0][j]) {
 692                                update_refstatus(ref_status, info->ref->nr, *bitmap);
 693                                dst++;
 694                                break;
 695                        }
 696        }
 697        info->nr_theirs = dst;
 698
 699        memset(&ca, 0, sizeof(ca));
 700        head_ref(add_ref, &ca);
 701        for_each_ref(add_ref, &ca);
 702
 703        /* Remove unreachable shallow commits from "ours" */
 704        for (i = dst = 0; i < info->nr_ours; i++) {
 705                if (i != dst)
 706                        info->ours[dst] = info->ours[i];
 707                c = lookup_commit(&oid[info->ours[i]]);
 708                bitmap = ref_bitmap_at(ref_bitmap, c);
 709                if (!*bitmap)
 710                        continue;
 711                for (j = 0; j < bitmap_nr; j++)
 712                        if (bitmap[0][j] &&
 713                            /* Step 7, reachability test at commit level */
 714                            !in_merge_bases_many(c, ca.nr, ca.commits)) {
 715                                update_refstatus(ref_status, info->ref->nr, *bitmap);
 716                                dst++;
 717                                break;
 718                        }
 719        }
 720        info->nr_ours = dst;
 721
 722        free(ca.commits);
 723}
 724
 725/* (Delayed) step 7, reachability test at commit level */
 726int delayed_reachability_test(struct shallow_info *si, int c)
 727{
 728        if (si->need_reachability_test[c]) {
 729                struct commit *commit = lookup_commit(&si->shallow->oid[c]);
 730
 731                if (!si->commits) {
 732                        struct commit_array ca;
 733
 734                        memset(&ca, 0, sizeof(ca));
 735                        head_ref(add_ref, &ca);
 736                        for_each_ref(add_ref, &ca);
 737                        si->commits = ca.commits;
 738                        si->nr_commits = ca.nr;
 739                }
 740
 741                si->reachable[c] = in_merge_bases_many(commit,
 742                                                       si->nr_commits,
 743                                                       si->commits);
 744                si->need_reachability_test[c] = 0;
 745        }
 746        return si->reachable[c];
 747}