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