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