Merge branch 'jc/doc-sent-patch-now-what'
[alt-git.git] / shallow.c
blob5413719fd4ef16c130117440c4d4687f9c1c5525
1 #include "git-compat-util.h"
2 #include "hex.h"
3 #include "repository.h"
4 #include "tempfile.h"
5 #include "lockfile.h"
6 #include "object-store-ll.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "pkt-line.h"
10 #include "remote.h"
11 #include "refs.h"
12 #include "oid-array.h"
13 #include "path.h"
14 #include "diff.h"
15 #include "revision.h"
16 #include "commit-slab.h"
17 #include "list-objects.h"
18 #include "commit-reach.h"
19 #include "shallow.h"
20 #include "statinfo.h"
21 #include "trace.h"
23 void set_alternate_shallow_file(struct repository *r, const char *path, int override)
25 if (r->parsed_objects->is_shallow != -1)
26 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
27 if (r->parsed_objects->alternate_shallow_file && !override)
28 return;
29 free(r->parsed_objects->alternate_shallow_file);
30 r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
33 int register_shallow(struct repository *r, const struct object_id *oid)
35 struct commit_graft *graft =
36 xmalloc(sizeof(struct commit_graft));
37 struct commit *commit = lookup_commit(r, oid);
39 oidcpy(&graft->oid, oid);
40 graft->nr_parent = -1;
41 if (commit && commit->object.parsed)
42 commit->parents = NULL;
43 return register_commit_graft(r, graft, 0);
46 int unregister_shallow(const struct object_id *oid)
48 int pos = commit_graft_pos(the_repository, oid);
49 if (pos < 0)
50 return -1;
51 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
52 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
53 the_repository->parsed_objects->grafts + pos + 1,
54 the_repository->parsed_objects->grafts_nr - pos - 1);
55 the_repository->parsed_objects->grafts_nr--;
56 return 0;
59 int is_repository_shallow(struct repository *r)
61 FILE *fp;
62 char buf[1024];
63 const char *path = r->parsed_objects->alternate_shallow_file;
65 if (r->parsed_objects->is_shallow >= 0)
66 return r->parsed_objects->is_shallow;
68 if (!path)
69 path = git_path_shallow(r);
71 * fetch-pack sets '--shallow-file ""' as an indicator that no
72 * shallow file should be used. We could just open it and it
73 * will likely fail. But let's do an explicit check instead.
75 if (!*path || (fp = fopen(path, "r")) == NULL) {
76 stat_validity_clear(r->parsed_objects->shallow_stat);
77 r->parsed_objects->is_shallow = 0;
78 return r->parsed_objects->is_shallow;
80 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
81 r->parsed_objects->is_shallow = 1;
83 while (fgets(buf, sizeof(buf), fp)) {
84 struct object_id oid;
85 if (get_oid_hex(buf, &oid))
86 die("bad shallow line: %s", buf);
87 register_shallow(r, &oid);
89 fclose(fp);
90 return r->parsed_objects->is_shallow;
93 static void reset_repository_shallow(struct repository *r)
95 r->parsed_objects->is_shallow = -1;
96 stat_validity_clear(r->parsed_objects->shallow_stat);
97 reset_commit_grafts(r);
100 int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
102 int res = commit_lock_file(&lk->lock);
103 reset_repository_shallow(r);
106 * Update in-memory data structures with the new shallow information,
107 * including unparsing all commits that now have grafts.
109 is_repository_shallow(r);
111 return res;
114 void rollback_shallow_file(struct repository *r, struct shallow_lock *lk)
116 rollback_lock_file(&lk->lock);
117 reset_repository_shallow(r);
121 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
122 * supports a "valid" flag.
124 define_commit_slab(commit_depth, int *);
125 static void free_depth_in_slab(int **ptr)
127 FREE_AND_NULL(*ptr);
129 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
130 int shallow_flag, int not_shallow_flag)
132 int i = 0, cur_depth = 0;
133 struct commit_list *result = NULL;
134 struct object_array stack = OBJECT_ARRAY_INIT;
135 struct commit *commit = NULL;
136 struct commit_graft *graft;
137 struct commit_depth depths;
139 init_commit_depth(&depths);
140 while (commit || i < heads->nr || stack.nr) {
141 struct commit_list *p;
142 if (!commit) {
143 if (i < heads->nr) {
144 int **depth_slot;
145 commit = (struct commit *)
146 deref_tag(the_repository,
147 heads->objects[i++].item,
148 NULL, 0);
149 if (!commit || commit->object.type != OBJ_COMMIT) {
150 commit = NULL;
151 continue;
153 depth_slot = commit_depth_at(&depths, commit);
154 if (!*depth_slot)
155 *depth_slot = xmalloc(sizeof(int));
156 **depth_slot = 0;
157 cur_depth = 0;
158 } else {
159 commit = (struct commit *)
160 object_array_pop(&stack);
161 cur_depth = **commit_depth_at(&depths, commit);
164 parse_commit_or_die(commit);
165 cur_depth++;
166 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
167 (is_repository_shallow(the_repository) && !commit->parents &&
168 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
169 graft->nr_parent < 0)) {
170 commit_list_insert(commit, &result);
171 commit->object.flags |= shallow_flag;
172 commit = NULL;
173 continue;
175 commit->object.flags |= not_shallow_flag;
176 for (p = commit->parents, commit = NULL; p; p = p->next) {
177 int **depth_slot = commit_depth_at(&depths, p->item);
178 if (!*depth_slot) {
179 *depth_slot = xmalloc(sizeof(int));
180 **depth_slot = cur_depth;
181 } else {
182 if (cur_depth >= **depth_slot)
183 continue;
184 **depth_slot = cur_depth;
186 if (p->next)
187 add_object_array(&p->item->object,
188 NULL, &stack);
189 else {
190 commit = p->item;
191 cur_depth = **commit_depth_at(&depths, commit);
195 deep_clear_commit_depth(&depths, free_depth_in_slab);
197 return result;
200 static void show_commit(struct commit *commit, void *data)
202 commit_list_insert(commit, data);
206 * Given rev-list arguments, run rev-list. All reachable commits
207 * except border ones are marked with not_shallow_flag. Border commits
208 * are marked with shallow_flag. The list of border/shallow commits
209 * are also returned.
211 struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
212 int shallow_flag,
213 int not_shallow_flag)
215 struct commit_list *result = NULL, *p;
216 struct commit_list *not_shallow_list = NULL;
217 struct rev_info revs;
218 int both_flags = shallow_flag | not_shallow_flag;
221 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
222 * set at this point. But better be safe than sorry.
224 clear_object_flags(both_flags);
226 is_repository_shallow(the_repository); /* make sure shallows are read */
228 repo_init_revisions(the_repository, &revs, NULL);
229 save_commit_buffer = 0;
230 setup_revisions(ac, av, &revs, NULL);
232 if (prepare_revision_walk(&revs))
233 die("revision walk setup failed");
234 traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
236 if (!not_shallow_list)
237 die("no commits selected for shallow requests");
239 /* Mark all reachable commits as NOT_SHALLOW */
240 for (p = not_shallow_list; p; p = p->next)
241 p->item->object.flags |= not_shallow_flag;
244 * mark border commits SHALLOW + NOT_SHALLOW.
245 * We cannot clear NOT_SHALLOW right now. Imagine border
246 * commit A is processed first, then commit B, whose parent is
247 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
248 * itself is considered border at step 2, which is incorrect.
250 for (p = not_shallow_list; p; p = p->next) {
251 struct commit *c = p->item;
252 struct commit_list *parent;
254 if (repo_parse_commit(the_repository, c))
255 die("unable to parse commit %s",
256 oid_to_hex(&c->object.oid));
258 for (parent = c->parents; parent; parent = parent->next)
259 if (!(parent->item->object.flags & not_shallow_flag)) {
260 c->object.flags |= shallow_flag;
261 commit_list_insert(c, &result);
262 break;
265 free_commit_list(not_shallow_list);
268 * Now we can clean up NOT_SHALLOW on border commits. Having
269 * both flags set can confuse the caller.
271 for (p = result; p; p = p->next) {
272 struct object *o = &p->item->object;
273 if ((o->flags & both_flags) == both_flags)
274 o->flags &= ~not_shallow_flag;
276 release_revisions(&revs);
277 return result;
280 static void check_shallow_file_for_update(struct repository *r)
282 if (r->parsed_objects->is_shallow == -1)
283 BUG("shallow must be initialized by now");
285 if (!stat_validity_check(r->parsed_objects->shallow_stat,
286 git_path_shallow(r)))
287 die("shallow file has changed since we read it");
290 #define SEEN_ONLY 1
291 #define VERBOSE 2
292 #define QUICK 4
294 struct write_shallow_data {
295 struct strbuf *out;
296 int use_pack_protocol;
297 int count;
298 unsigned flags;
301 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
303 struct write_shallow_data *data = cb_data;
304 const char *hex = oid_to_hex(&graft->oid);
305 if (graft->nr_parent != -1)
306 return 0;
307 if (data->flags & QUICK) {
308 if (!repo_has_object_file(the_repository, &graft->oid))
309 return 0;
310 } else if (data->flags & SEEN_ONLY) {
311 struct commit *c = lookup_commit(the_repository, &graft->oid);
312 if (!c || !(c->object.flags & SEEN)) {
313 if (data->flags & VERBOSE)
314 printf("Removing %s from .git/shallow\n",
315 oid_to_hex(&c->object.oid));
316 return 0;
319 data->count++;
320 if (data->use_pack_protocol)
321 packet_buf_write(data->out, "shallow %s", hex);
322 else {
323 strbuf_addstr(data->out, hex);
324 strbuf_addch(data->out, '\n');
326 return 0;
329 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
330 const struct oid_array *extra,
331 unsigned flags)
333 struct write_shallow_data data;
334 int i;
335 data.out = out;
336 data.use_pack_protocol = use_pack_protocol;
337 data.count = 0;
338 data.flags = flags;
339 for_each_commit_graft(write_one_shallow, &data);
340 if (!extra)
341 return data.count;
342 for (i = 0; i < extra->nr; i++) {
343 strbuf_addstr(out, oid_to_hex(extra->oid + i));
344 strbuf_addch(out, '\n');
345 data.count++;
347 return data.count;
350 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
351 const struct oid_array *extra)
353 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
356 const char *setup_temporary_shallow(const struct oid_array *extra)
358 struct tempfile *temp;
359 struct strbuf sb = STRBUF_INIT;
361 if (write_shallow_commits(&sb, 0, extra)) {
362 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
364 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
365 close_tempfile_gently(temp) < 0)
366 die_errno("failed to write to %s",
367 get_tempfile_path(temp));
368 strbuf_release(&sb);
369 return get_tempfile_path(temp);
372 * is_repository_shallow() sees empty string as "no shallow
373 * file".
375 return "";
378 void setup_alternate_shallow(struct shallow_lock *shallow_lock,
379 const char **alternate_shallow_file,
380 const struct oid_array *extra)
382 struct strbuf sb = STRBUF_INIT;
383 int fd;
385 fd = hold_lock_file_for_update(&shallow_lock->lock,
386 git_path_shallow(the_repository),
387 LOCK_DIE_ON_ERROR);
388 check_shallow_file_for_update(the_repository);
389 if (write_shallow_commits(&sb, 0, extra)) {
390 if (write_in_full(fd, sb.buf, sb.len) < 0)
391 die_errno("failed to write to %s",
392 get_lock_file_path(&shallow_lock->lock));
393 *alternate_shallow_file = get_lock_file_path(&shallow_lock->lock);
394 } else
396 * is_repository_shallow() sees empty string as "no
397 * shallow file".
399 *alternate_shallow_file = "";
400 strbuf_release(&sb);
403 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
405 int fd = *(int *)cb;
406 if (graft->nr_parent == -1)
407 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
408 return 0;
411 void advertise_shallow_grafts(int fd)
413 if (!is_repository_shallow(the_repository))
414 return;
415 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
419 * mark_reachable_objects() should have been run prior to this and all
420 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
421 * in which case lines are excised from the shallow file if they refer to
422 * commits that do not exist (any longer).
424 void prune_shallow(unsigned options)
426 struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
427 struct strbuf sb = STRBUF_INIT;
428 unsigned flags = SEEN_ONLY;
429 int fd;
431 if (options & PRUNE_QUICK)
432 flags |= QUICK;
434 if (options & PRUNE_SHOW_ONLY) {
435 flags |= VERBOSE;
436 write_shallow_commits_1(&sb, 0, NULL, flags);
437 strbuf_release(&sb);
438 return;
440 fd = hold_lock_file_for_update(&shallow_lock.lock,
441 git_path_shallow(the_repository),
442 LOCK_DIE_ON_ERROR);
443 check_shallow_file_for_update(the_repository);
444 if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
445 if (write_in_full(fd, sb.buf, sb.len) < 0)
446 die_errno("failed to write to %s",
447 get_lock_file_path(&shallow_lock.lock));
448 commit_shallow_file(the_repository, &shallow_lock);
449 } else {
450 unlink(git_path_shallow(the_repository));
451 rollback_shallow_file(the_repository, &shallow_lock);
453 strbuf_release(&sb);
456 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
459 * Step 1, split sender shallow commits into "ours" and "theirs"
460 * Step 2, clean "ours" based on .git/shallow
462 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
464 int i;
465 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
466 memset(info, 0, sizeof(*info));
467 info->shallow = sa;
468 if (!sa)
469 return;
470 ALLOC_ARRAY(info->ours, sa->nr);
471 ALLOC_ARRAY(info->theirs, sa->nr);
472 for (i = 0; i < sa->nr; i++) {
473 if (repo_has_object_file(the_repository, sa->oid + i)) {
474 struct commit_graft *graft;
475 graft = lookup_commit_graft(the_repository,
476 &sa->oid[i]);
477 if (graft && graft->nr_parent < 0)
478 continue;
479 info->ours[info->nr_ours++] = i;
480 } else
481 info->theirs[info->nr_theirs++] = i;
485 void clear_shallow_info(struct shallow_info *info)
487 free(info->ours);
488 free(info->theirs);
491 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
493 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
495 struct object_id *oid = info->shallow->oid;
496 int i, dst;
497 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
498 for (i = dst = 0; i < info->nr_theirs; i++) {
499 if (i != dst)
500 info->theirs[dst] = info->theirs[i];
501 if (repo_has_object_file(the_repository, oid + info->theirs[i]))
502 dst++;
504 info->nr_theirs = dst;
507 define_commit_slab(ref_bitmap, uint32_t *);
509 #define POOL_SIZE (512 * 1024)
511 struct paint_info {
512 struct ref_bitmap ref_bitmap;
513 unsigned nr_bits;
514 char **pools;
515 char *free, *end;
516 unsigned pool_count;
519 static uint32_t *paint_alloc(struct paint_info *info)
521 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
522 unsigned size = nr * sizeof(uint32_t);
523 void *p;
524 if (!info->pool_count || size > info->end - info->free) {
525 if (size > POOL_SIZE)
526 BUG("pool size too small for %d in paint_alloc()",
527 size);
528 info->pool_count++;
529 REALLOC_ARRAY(info->pools, info->pool_count);
530 info->free = xmalloc(POOL_SIZE);
531 info->pools[info->pool_count - 1] = info->free;
532 info->end = info->free + POOL_SIZE;
534 p = info->free;
535 info->free += size;
536 return p;
540 * Given a commit SHA-1, walk down to parents until either SEEN,
541 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
542 * all walked commits.
544 static void paint_down(struct paint_info *info, const struct object_id *oid,
545 unsigned int id)
547 unsigned int i, nr;
548 struct commit_list *head = NULL;
549 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
550 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
551 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
553 uint32_t *tmp; /* to be freed before return */
554 uint32_t *bitmap;
556 if (!c)
557 return;
559 tmp = xmalloc(bitmap_size);
560 bitmap = paint_alloc(info);
561 memset(bitmap, 0, bitmap_size);
562 bitmap[id / 32] |= (1U << (id % 32));
563 commit_list_insert(c, &head);
564 while (head) {
565 struct commit_list *p;
566 struct commit *c = pop_commit(&head);
567 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
569 /* XXX check "UNINTERESTING" from pack bitmaps if available */
570 if (c->object.flags & (SEEN | UNINTERESTING))
571 continue;
572 else
573 c->object.flags |= SEEN;
575 if (!*refs)
576 *refs = bitmap;
577 else {
578 memcpy(tmp, *refs, bitmap_size);
579 for (i = 0; i < bitmap_nr; i++)
580 tmp[i] |= bitmap[i];
581 if (memcmp(tmp, *refs, bitmap_size)) {
582 *refs = paint_alloc(info);
583 memcpy(*refs, tmp, bitmap_size);
587 if (c->object.flags & BOTTOM)
588 continue;
590 if (repo_parse_commit(the_repository, c))
591 die("unable to parse commit %s",
592 oid_to_hex(&c->object.oid));
594 for (p = c->parents; p; p = p->next) {
595 if (p->item->object.flags & SEEN)
596 continue;
597 commit_list_insert(p->item, &head);
601 nr = get_max_object_index();
602 for (i = 0; i < nr; i++) {
603 struct object *o = get_indexed_object(i);
604 if (o && o->type == OBJ_COMMIT)
605 o->flags &= ~SEEN;
608 free(tmp);
611 static int mark_uninteresting(const char *refname UNUSED,
612 const struct object_id *oid,
613 int flags UNUSED,
614 void *cb_data UNUSED)
616 struct commit *commit = lookup_commit_reference_gently(the_repository,
617 oid, 1);
618 if (!commit)
619 return 0;
620 commit->object.flags |= UNINTERESTING;
621 mark_parents_uninteresting(NULL, commit);
622 return 0;
625 static void post_assign_shallow(struct shallow_info *info,
626 struct ref_bitmap *ref_bitmap,
627 int *ref_status);
629 * Step 6(+7), associate shallow commits with new refs
631 * info->ref must be initialized before calling this function.
633 * If used is not NULL, it's an array of info->shallow->nr
634 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
635 * m-th shallow commit from info->shallow.
637 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
638 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
639 * the ref needs some shallow commits from either info->ours or
640 * info->theirs.
642 void assign_shallow_commits_to_refs(struct shallow_info *info,
643 uint32_t **used, int *ref_status)
645 struct object_id *oid = info->shallow->oid;
646 struct oid_array *ref = info->ref;
647 unsigned int i, nr;
648 int *shallow, nr_shallow = 0;
649 struct paint_info pi;
651 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
652 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
653 for (i = 0; i < info->nr_ours; i++)
654 shallow[nr_shallow++] = info->ours[i];
655 for (i = 0; i < info->nr_theirs; i++)
656 shallow[nr_shallow++] = info->theirs[i];
659 * Prepare the commit graph to track what refs can reach what
660 * (new) shallow commits.
662 nr = get_max_object_index();
663 for (i = 0; i < nr; i++) {
664 struct object *o = get_indexed_object(i);
665 if (!o || o->type != OBJ_COMMIT)
666 continue;
668 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
671 memset(&pi, 0, sizeof(pi));
672 init_ref_bitmap(&pi.ref_bitmap);
673 pi.nr_bits = ref->nr;
676 * "--not --all" to cut short the traversal if new refs
677 * connect to old refs. If not (e.g. force ref updates) it'll
678 * have to go down to the current shallow commits.
680 head_ref(mark_uninteresting, NULL);
681 for_each_ref(mark_uninteresting, NULL);
683 /* Mark potential bottoms so we won't go out of bound */
684 for (i = 0; i < nr_shallow; i++) {
685 struct commit *c = lookup_commit(the_repository,
686 &oid[shallow[i]]);
687 c->object.flags |= BOTTOM;
690 for (i = 0; i < ref->nr; i++)
691 paint_down(&pi, ref->oid + i, i);
693 if (used) {
694 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
695 memset(used, 0, sizeof(*used) * info->shallow->nr);
696 for (i = 0; i < nr_shallow; i++) {
697 const struct commit *c = lookup_commit(the_repository,
698 &oid[shallow[i]]);
699 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
700 if (*map)
701 used[shallow[i]] = xmemdupz(*map, bitmap_size);
704 * unreachable shallow commits are not removed from
705 * "ours" and "theirs". The user is supposed to run
706 * step 7 on every ref separately and not trust "ours"
707 * and "theirs" any more.
709 } else
710 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
712 clear_ref_bitmap(&pi.ref_bitmap);
713 for (i = 0; i < pi.pool_count; i++)
714 free(pi.pools[i]);
715 free(pi.pools);
716 free(shallow);
719 struct commit_array {
720 struct commit **commits;
721 int nr, alloc;
724 static int add_ref(const char *refname UNUSED,
725 const struct object_id *oid,
726 int flags UNUSED,
727 void *cb_data)
729 struct commit_array *ca = cb_data;
730 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
731 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
732 oid, 1);
733 if (ca->commits[ca->nr])
734 ca->nr++;
735 return 0;
738 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
740 unsigned int i;
741 if (!ref_status)
742 return;
743 for (i = 0; i < nr; i++)
744 if (bitmap[i / 32] & (1U << (i % 32)))
745 ref_status[i]++;
749 * Step 7, reachability test on "ours" at commit level
751 static void post_assign_shallow(struct shallow_info *info,
752 struct ref_bitmap *ref_bitmap,
753 int *ref_status)
755 struct object_id *oid = info->shallow->oid;
756 struct commit *c;
757 uint32_t **bitmap;
758 int dst, i, j;
759 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
760 struct commit_array ca;
762 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
763 if (ref_status)
764 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
766 /* Remove unreachable shallow commits from "theirs" */
767 for (i = dst = 0; i < info->nr_theirs; i++) {
768 if (i != dst)
769 info->theirs[dst] = info->theirs[i];
770 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
771 bitmap = ref_bitmap_at(ref_bitmap, c);
772 if (!*bitmap)
773 continue;
774 for (j = 0; j < bitmap_nr; j++)
775 if (bitmap[0][j]) {
776 update_refstatus(ref_status, info->ref->nr, *bitmap);
777 dst++;
778 break;
781 info->nr_theirs = dst;
783 memset(&ca, 0, sizeof(ca));
784 head_ref(add_ref, &ca);
785 for_each_ref(add_ref, &ca);
787 /* Remove unreachable shallow commits from "ours" */
788 for (i = dst = 0; i < info->nr_ours; i++) {
789 if (i != dst)
790 info->ours[dst] = info->ours[i];
791 c = lookup_commit(the_repository, &oid[info->ours[i]]);
792 bitmap = ref_bitmap_at(ref_bitmap, c);
793 if (!*bitmap)
794 continue;
795 for (j = 0; j < bitmap_nr; j++)
796 if (bitmap[0][j] &&
797 /* Step 7, reachability test at commit level */
798 !repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits)) {
799 update_refstatus(ref_status, info->ref->nr, *bitmap);
800 dst++;
801 break;
804 info->nr_ours = dst;
806 free(ca.commits);
809 /* (Delayed) step 7, reachability test at commit level */
810 int delayed_reachability_test(struct shallow_info *si, int c)
812 if (si->need_reachability_test[c]) {
813 struct commit *commit = lookup_commit(the_repository,
814 &si->shallow->oid[c]);
816 if (!si->commits) {
817 struct commit_array ca;
819 memset(&ca, 0, sizeof(ca));
820 head_ref(add_ref, &ca);
821 for_each_ref(add_ref, &ca);
822 si->commits = ca.commits;
823 si->nr_commits = ca.nr;
826 si->reachable[c] = repo_in_merge_bases_many(the_repository,
827 commit,
828 si->nr_commits,
829 si->commits);
830 si->need_reachability_test[c] = 0;
832 return si->reachable[c];