docs: introduce document to announce breaking changes
[git/gitster.git] / shallow.c
blob7ff50dd0da45e00ac3de5e03af473ff361d33b02
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 "refs.h"
11 #include "oid-array.h"
12 #include "path.h"
13 #include "diff.h"
14 #include "revision.h"
15 #include "commit-slab.h"
16 #include "list-objects.h"
17 #include "commit-reach.h"
18 #include "shallow.h"
19 #include "statinfo.h"
20 #include "trace.h"
22 void set_alternate_shallow_file(struct repository *r, const char *path, int override)
24 if (r->parsed_objects->is_shallow != -1)
25 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
26 if (r->parsed_objects->alternate_shallow_file && !override)
27 return;
28 free(r->parsed_objects->alternate_shallow_file);
29 r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
32 int register_shallow(struct repository *r, const struct object_id *oid)
34 struct commit_graft *graft =
35 xmalloc(sizeof(struct commit_graft));
36 struct commit *commit = lookup_commit(r, oid);
38 oidcpy(&graft->oid, oid);
39 graft->nr_parent = -1;
40 if (commit && commit->object.parsed) {
41 free_commit_list(commit->parents);
42 commit->parents = NULL;
44 return register_commit_graft(r, graft, 0);
47 int unregister_shallow(const struct object_id *oid)
49 int pos = commit_graft_pos(the_repository, oid);
50 if (pos < 0)
51 return -1;
52 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
53 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
54 the_repository->parsed_objects->grafts + pos + 1,
55 the_repository->parsed_objects->grafts_nr - pos - 1);
56 the_repository->parsed_objects->grafts_nr--;
57 return 0;
60 int is_repository_shallow(struct repository *r)
62 FILE *fp;
63 char buf[1024];
64 const char *path = r->parsed_objects->alternate_shallow_file;
66 if (r->parsed_objects->is_shallow >= 0)
67 return r->parsed_objects->is_shallow;
69 if (!path)
70 path = git_path_shallow(r);
72 * fetch-pack sets '--shallow-file ""' as an indicator that no
73 * shallow file should be used. We could just open it and it
74 * will likely fail. But let's do an explicit check instead.
76 if (!*path || (fp = fopen(path, "r")) == NULL) {
77 stat_validity_clear(r->parsed_objects->shallow_stat);
78 r->parsed_objects->is_shallow = 0;
79 return r->parsed_objects->is_shallow;
81 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
82 r->parsed_objects->is_shallow = 1;
84 while (fgets(buf, sizeof(buf), fp)) {
85 struct object_id oid;
86 if (get_oid_hex(buf, &oid))
87 die("bad shallow line: %s", buf);
88 register_shallow(r, &oid);
90 fclose(fp);
91 return r->parsed_objects->is_shallow;
94 static void reset_repository_shallow(struct repository *r)
96 r->parsed_objects->is_shallow = -1;
97 stat_validity_clear(r->parsed_objects->shallow_stat);
98 reset_commit_grafts(r);
101 int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
103 int res = commit_lock_file(&lk->lock);
104 reset_repository_shallow(r);
107 * Update in-memory data structures with the new shallow information,
108 * including unparsing all commits that now have grafts.
110 is_repository_shallow(r);
112 return res;
115 void rollback_shallow_file(struct repository *r, struct shallow_lock *lk)
117 rollback_lock_file(&lk->lock);
118 reset_repository_shallow(r);
122 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
123 * supports a "valid" flag.
125 define_commit_slab(commit_depth, int *);
126 static void free_depth_in_slab(int **ptr)
128 FREE_AND_NULL(*ptr);
130 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
131 int shallow_flag, int not_shallow_flag)
133 int i = 0, cur_depth = 0;
134 struct commit_list *result = NULL;
135 struct object_array stack = OBJECT_ARRAY_INIT;
136 struct commit *commit = NULL;
137 struct commit_graft *graft;
138 struct commit_depth depths;
140 init_commit_depth(&depths);
141 while (commit || i < heads->nr || stack.nr) {
142 struct commit_list *p;
143 if (!commit) {
144 if (i < heads->nr) {
145 int **depth_slot;
146 commit = (struct commit *)
147 deref_tag(the_repository,
148 heads->objects[i++].item,
149 NULL, 0);
150 if (!commit || commit->object.type != OBJ_COMMIT) {
151 commit = NULL;
152 continue;
154 depth_slot = commit_depth_at(&depths, commit);
155 if (!*depth_slot)
156 *depth_slot = xmalloc(sizeof(int));
157 **depth_slot = 0;
158 cur_depth = 0;
159 } else {
160 commit = (struct commit *)
161 object_array_pop(&stack);
162 cur_depth = **commit_depth_at(&depths, commit);
165 parse_commit_or_die(commit);
166 cur_depth++;
167 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
168 (is_repository_shallow(the_repository) && !commit->parents &&
169 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
170 graft->nr_parent < 0)) {
171 commit_list_insert(commit, &result);
172 commit->object.flags |= shallow_flag;
173 commit = NULL;
174 continue;
176 commit->object.flags |= not_shallow_flag;
177 for (p = commit->parents, commit = NULL; p; p = p->next) {
178 int **depth_slot = commit_depth_at(&depths, p->item);
179 if (!*depth_slot) {
180 *depth_slot = xmalloc(sizeof(int));
181 **depth_slot = cur_depth;
182 } else {
183 if (cur_depth >= **depth_slot)
184 continue;
185 **depth_slot = cur_depth;
187 if (p->next)
188 add_object_array(&p->item->object,
189 NULL, &stack);
190 else {
191 commit = p->item;
192 cur_depth = **commit_depth_at(&depths, commit);
196 deep_clear_commit_depth(&depths, free_depth_in_slab);
198 return result;
201 static void show_commit(struct commit *commit, void *data)
203 commit_list_insert(commit, data);
207 * Given rev-list arguments, run rev-list. All reachable commits
208 * except border ones are marked with not_shallow_flag. Border commits
209 * are marked with shallow_flag. The list of border/shallow commits
210 * are also returned.
212 struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
213 int shallow_flag,
214 int not_shallow_flag)
216 struct commit_list *result = NULL, *p;
217 struct commit_list *not_shallow_list = NULL;
218 struct rev_info revs;
219 int both_flags = shallow_flag | not_shallow_flag;
222 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
223 * set at this point. But better be safe than sorry.
225 clear_object_flags(both_flags);
227 is_repository_shallow(the_repository); /* make sure shallows are read */
229 repo_init_revisions(the_repository, &revs, NULL);
230 save_commit_buffer = 0;
231 setup_revisions(ac, av, &revs, NULL);
233 if (prepare_revision_walk(&revs))
234 die("revision walk setup failed");
235 traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
237 if (!not_shallow_list)
238 die("no commits selected for shallow requests");
240 /* Mark all reachable commits as NOT_SHALLOW */
241 for (p = not_shallow_list; p; p = p->next)
242 p->item->object.flags |= not_shallow_flag;
245 * mark border commits SHALLOW + NOT_SHALLOW.
246 * We cannot clear NOT_SHALLOW right now. Imagine border
247 * commit A is processed first, then commit B, whose parent is
248 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
249 * itself is considered border at step 2, which is incorrect.
251 for (p = not_shallow_list; p; p = p->next) {
252 struct commit *c = p->item;
253 struct commit_list *parent;
255 if (repo_parse_commit(the_repository, c))
256 die("unable to parse commit %s",
257 oid_to_hex(&c->object.oid));
259 for (parent = c->parents; parent; parent = parent->next)
260 if (!(parent->item->object.flags & not_shallow_flag)) {
261 c->object.flags |= shallow_flag;
262 commit_list_insert(c, &result);
263 break;
266 free_commit_list(not_shallow_list);
269 * Now we can clean up NOT_SHALLOW on border commits. Having
270 * both flags set can confuse the caller.
272 for (p = result; p; p = p->next) {
273 struct object *o = &p->item->object;
274 if ((o->flags & both_flags) == both_flags)
275 o->flags &= ~not_shallow_flag;
277 release_revisions(&revs);
278 return result;
281 static void check_shallow_file_for_update(struct repository *r)
283 if (r->parsed_objects->is_shallow == -1)
284 BUG("shallow must be initialized by now");
286 if (!stat_validity_check(r->parsed_objects->shallow_stat,
287 git_path_shallow(r)))
288 die("shallow file has changed since we read it");
291 #define SEEN_ONLY 1
292 #define VERBOSE 2
293 #define QUICK 4
295 struct write_shallow_data {
296 struct strbuf *out;
297 int use_pack_protocol;
298 int count;
299 unsigned flags;
302 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
304 struct write_shallow_data *data = cb_data;
305 const char *hex = oid_to_hex(&graft->oid);
306 if (graft->nr_parent != -1)
307 return 0;
308 if (data->flags & QUICK) {
309 if (!repo_has_object_file(the_repository, &graft->oid))
310 return 0;
311 } else if (data->flags & SEEN_ONLY) {
312 struct commit *c = lookup_commit(the_repository, &graft->oid);
313 if (!c || !(c->object.flags & SEEN)) {
314 if (data->flags & VERBOSE)
315 printf("Removing %s from .git/shallow\n",
316 oid_to_hex(&c->object.oid));
317 return 0;
320 data->count++;
321 if (data->use_pack_protocol)
322 packet_buf_write(data->out, "shallow %s", hex);
323 else {
324 strbuf_addstr(data->out, hex);
325 strbuf_addch(data->out, '\n');
327 return 0;
330 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
331 const struct oid_array *extra,
332 unsigned flags)
334 struct write_shallow_data data;
335 int i;
336 data.out = out;
337 data.use_pack_protocol = use_pack_protocol;
338 data.count = 0;
339 data.flags = flags;
340 for_each_commit_graft(write_one_shallow, &data);
341 if (!extra)
342 return data.count;
343 for (i = 0; i < extra->nr; i++) {
344 strbuf_addstr(out, oid_to_hex(extra->oid + i));
345 strbuf_addch(out, '\n');
346 data.count++;
348 return data.count;
351 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
352 const struct oid_array *extra)
354 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
357 const char *setup_temporary_shallow(const struct oid_array *extra)
359 struct tempfile *temp;
360 struct strbuf sb = STRBUF_INIT;
362 if (write_shallow_commits(&sb, 0, extra)) {
363 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
365 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
366 close_tempfile_gently(temp) < 0)
367 die_errno("failed to write to %s",
368 get_tempfile_path(temp));
369 strbuf_release(&sb);
370 return get_tempfile_path(temp);
373 * is_repository_shallow() sees empty string as "no shallow
374 * file".
376 return "";
379 void setup_alternate_shallow(struct shallow_lock *shallow_lock,
380 const char **alternate_shallow_file,
381 const struct oid_array *extra)
383 struct strbuf sb = STRBUF_INIT;
384 int fd;
386 fd = hold_lock_file_for_update(&shallow_lock->lock,
387 git_path_shallow(the_repository),
388 LOCK_DIE_ON_ERROR);
389 check_shallow_file_for_update(the_repository);
390 if (write_shallow_commits(&sb, 0, extra)) {
391 if (write_in_full(fd, sb.buf, sb.len) < 0)
392 die_errno("failed to write to %s",
393 get_lock_file_path(&shallow_lock->lock));
394 *alternate_shallow_file = get_lock_file_path(&shallow_lock->lock);
395 } else
397 * is_repository_shallow() sees empty string as "no
398 * shallow file".
400 *alternate_shallow_file = "";
401 strbuf_release(&sb);
404 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
406 int fd = *(int *)cb;
407 if (graft->nr_parent == -1)
408 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
409 return 0;
412 void advertise_shallow_grafts(int fd)
414 if (!is_repository_shallow(the_repository))
415 return;
416 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
420 * mark_reachable_objects() should have been run prior to this and all
421 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
422 * in which case lines are excised from the shallow file if they refer to
423 * commits that do not exist (any longer).
425 void prune_shallow(unsigned options)
427 struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
428 struct strbuf sb = STRBUF_INIT;
429 unsigned flags = SEEN_ONLY;
430 int fd;
432 if (options & PRUNE_QUICK)
433 flags |= QUICK;
435 if (options & PRUNE_SHOW_ONLY) {
436 flags |= VERBOSE;
437 write_shallow_commits_1(&sb, 0, NULL, flags);
438 strbuf_release(&sb);
439 return;
441 fd = hold_lock_file_for_update(&shallow_lock.lock,
442 git_path_shallow(the_repository),
443 LOCK_DIE_ON_ERROR);
444 check_shallow_file_for_update(the_repository);
445 if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
446 if (write_in_full(fd, sb.buf, sb.len) < 0)
447 die_errno("failed to write to %s",
448 get_lock_file_path(&shallow_lock.lock));
449 commit_shallow_file(the_repository, &shallow_lock);
450 } else {
451 unlink(git_path_shallow(the_repository));
452 rollback_shallow_file(the_repository, &shallow_lock);
454 strbuf_release(&sb);
457 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
460 * Step 1, split sender shallow commits into "ours" and "theirs"
461 * Step 2, clean "ours" based on .git/shallow
463 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
465 int i;
466 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
467 memset(info, 0, sizeof(*info));
468 info->shallow = sa;
469 if (!sa)
470 return;
471 ALLOC_ARRAY(info->ours, sa->nr);
472 ALLOC_ARRAY(info->theirs, sa->nr);
473 for (i = 0; i < sa->nr; i++) {
474 if (repo_has_object_file(the_repository, sa->oid + i)) {
475 struct commit_graft *graft;
476 graft = lookup_commit_graft(the_repository,
477 &sa->oid[i]);
478 if (graft && graft->nr_parent < 0)
479 continue;
480 info->ours[info->nr_ours++] = i;
481 } else
482 info->theirs[info->nr_theirs++] = i;
486 void clear_shallow_info(struct shallow_info *info)
488 free(info->ours);
489 free(info->theirs);
492 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
494 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
496 struct object_id *oid = info->shallow->oid;
497 int i, dst;
498 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
499 for (i = dst = 0; i < info->nr_theirs; i++) {
500 if (i != dst)
501 info->theirs[dst] = info->theirs[i];
502 if (repo_has_object_file(the_repository, oid + info->theirs[i]))
503 dst++;
505 info->nr_theirs = dst;
508 define_commit_slab(ref_bitmap, uint32_t *);
510 #define POOL_SIZE (512 * 1024)
512 struct paint_info {
513 struct ref_bitmap ref_bitmap;
514 unsigned nr_bits;
515 char **pools;
516 char *free, *end;
517 unsigned pool_count;
520 static uint32_t *paint_alloc(struct paint_info *info)
522 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
523 unsigned size = nr * sizeof(uint32_t);
524 void *p;
525 if (!info->pool_count || size > info->end - info->free) {
526 if (size > POOL_SIZE)
527 BUG("pool size too small for %d in paint_alloc()",
528 size);
529 info->pool_count++;
530 REALLOC_ARRAY(info->pools, info->pool_count);
531 info->free = xmalloc(POOL_SIZE);
532 info->pools[info->pool_count - 1] = info->free;
533 info->end = info->free + POOL_SIZE;
535 p = info->free;
536 info->free += size;
537 return p;
541 * Given a commit SHA-1, walk down to parents until either SEEN,
542 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
543 * all walked commits.
545 static void paint_down(struct paint_info *info, const struct object_id *oid,
546 unsigned int id)
548 unsigned int i, nr;
549 struct commit_list *head = NULL;
550 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
551 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
552 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
554 uint32_t *tmp; /* to be freed before return */
555 uint32_t *bitmap;
557 if (!c)
558 return;
560 tmp = xmalloc(bitmap_size);
561 bitmap = paint_alloc(info);
562 memset(bitmap, 0, bitmap_size);
563 bitmap[id / 32] |= (1U << (id % 32));
564 commit_list_insert(c, &head);
565 while (head) {
566 struct commit_list *p;
567 struct commit *c = pop_commit(&head);
568 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
570 /* XXX check "UNINTERESTING" from pack bitmaps if available */
571 if (c->object.flags & (SEEN | UNINTERESTING))
572 continue;
573 else
574 c->object.flags |= SEEN;
576 if (!*refs)
577 *refs = bitmap;
578 else {
579 memcpy(tmp, *refs, bitmap_size);
580 for (i = 0; i < bitmap_nr; i++)
581 tmp[i] |= bitmap[i];
582 if (memcmp(tmp, *refs, bitmap_size)) {
583 *refs = paint_alloc(info);
584 memcpy(*refs, tmp, bitmap_size);
588 if (c->object.flags & BOTTOM)
589 continue;
591 if (repo_parse_commit(the_repository, c))
592 die("unable to parse commit %s",
593 oid_to_hex(&c->object.oid));
595 for (p = c->parents; p; p = p->next) {
596 if (p->item->object.flags & SEEN)
597 continue;
598 commit_list_insert(p->item, &head);
602 nr = get_max_object_index();
603 for (i = 0; i < nr; i++) {
604 struct object *o = get_indexed_object(i);
605 if (o && o->type == OBJ_COMMIT)
606 o->flags &= ~SEEN;
609 free(tmp);
612 static int mark_uninteresting(const char *refname UNUSED,
613 const struct object_id *oid,
614 int flags UNUSED,
615 void *cb_data UNUSED)
617 struct commit *commit = lookup_commit_reference_gently(the_repository,
618 oid, 1);
619 if (!commit)
620 return 0;
621 commit->object.flags |= UNINTERESTING;
622 mark_parents_uninteresting(NULL, commit);
623 return 0;
626 static void post_assign_shallow(struct shallow_info *info,
627 struct ref_bitmap *ref_bitmap,
628 int *ref_status);
630 * Step 6(+7), associate shallow commits with new refs
632 * info->ref must be initialized before calling this function.
634 * If used is not NULL, it's an array of info->shallow->nr
635 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
636 * m-th shallow commit from info->shallow.
638 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
639 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
640 * the ref needs some shallow commits from either info->ours or
641 * info->theirs.
643 void assign_shallow_commits_to_refs(struct shallow_info *info,
644 uint32_t **used, int *ref_status)
646 struct object_id *oid = info->shallow->oid;
647 struct oid_array *ref = info->ref;
648 unsigned int i, nr;
649 int *shallow, nr_shallow = 0;
650 struct paint_info pi;
652 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
653 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
654 for (i = 0; i < info->nr_ours; i++)
655 shallow[nr_shallow++] = info->ours[i];
656 for (i = 0; i < info->nr_theirs; i++)
657 shallow[nr_shallow++] = info->theirs[i];
660 * Prepare the commit graph to track what refs can reach what
661 * (new) shallow commits.
663 nr = get_max_object_index();
664 for (i = 0; i < nr; i++) {
665 struct object *o = get_indexed_object(i);
666 if (!o || o->type != OBJ_COMMIT)
667 continue;
669 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
672 memset(&pi, 0, sizeof(pi));
673 init_ref_bitmap(&pi.ref_bitmap);
674 pi.nr_bits = ref->nr;
677 * "--not --all" to cut short the traversal if new refs
678 * connect to old refs. If not (e.g. force ref updates) it'll
679 * have to go down to the current shallow commits.
681 head_ref(mark_uninteresting, NULL);
682 for_each_ref(mark_uninteresting, NULL);
684 /* Mark potential bottoms so we won't go out of bound */
685 for (i = 0; i < nr_shallow; i++) {
686 struct commit *c = lookup_commit(the_repository,
687 &oid[shallow[i]]);
688 c->object.flags |= BOTTOM;
691 for (i = 0; i < ref->nr; i++)
692 paint_down(&pi, ref->oid + i, i);
694 if (used) {
695 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
696 memset(used, 0, sizeof(*used) * info->shallow->nr);
697 for (i = 0; i < nr_shallow; i++) {
698 const struct commit *c = lookup_commit(the_repository,
699 &oid[shallow[i]]);
700 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
701 if (*map)
702 used[shallow[i]] = xmemdupz(*map, bitmap_size);
705 * unreachable shallow commits are not removed from
706 * "ours" and "theirs". The user is supposed to run
707 * step 7 on every ref separately and not trust "ours"
708 * and "theirs" any more.
710 } else
711 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
713 clear_ref_bitmap(&pi.ref_bitmap);
714 for (i = 0; i < pi.pool_count; i++)
715 free(pi.pools[i]);
716 free(pi.pools);
717 free(shallow);
720 struct commit_array {
721 struct commit **commits;
722 int nr, alloc;
725 static int add_ref(const char *refname UNUSED,
726 const struct object_id *oid,
727 int flags UNUSED,
728 void *cb_data)
730 struct commit_array *ca = cb_data;
731 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
732 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
733 oid, 1);
734 if (ca->commits[ca->nr])
735 ca->nr++;
736 return 0;
739 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
741 unsigned int i;
742 if (!ref_status)
743 return;
744 for (i = 0; i < nr; i++)
745 if (bitmap[i / 32] & (1U << (i % 32)))
746 ref_status[i]++;
750 * Step 7, reachability test on "ours" at commit level
752 static void post_assign_shallow(struct shallow_info *info,
753 struct ref_bitmap *ref_bitmap,
754 int *ref_status)
756 struct object_id *oid = info->shallow->oid;
757 struct commit *c;
758 uint32_t **bitmap;
759 int dst, i, j;
760 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
761 struct commit_array ca;
763 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
764 if (ref_status)
765 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
767 /* Remove unreachable shallow commits from "theirs" */
768 for (i = dst = 0; i < info->nr_theirs; i++) {
769 if (i != dst)
770 info->theirs[dst] = info->theirs[i];
771 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
772 bitmap = ref_bitmap_at(ref_bitmap, c);
773 if (!*bitmap)
774 continue;
775 for (j = 0; j < bitmap_nr; j++)
776 if (bitmap[0][j]) {
777 update_refstatus(ref_status, info->ref->nr, *bitmap);
778 dst++;
779 break;
782 info->nr_theirs = dst;
784 memset(&ca, 0, sizeof(ca));
785 head_ref(add_ref, &ca);
786 for_each_ref(add_ref, &ca);
788 /* Remove unreachable shallow commits from "ours" */
789 for (i = dst = 0; i < info->nr_ours; i++) {
790 if (i != dst)
791 info->ours[dst] = info->ours[i];
792 c = lookup_commit(the_repository, &oid[info->ours[i]]);
793 bitmap = ref_bitmap_at(ref_bitmap, c);
794 if (!*bitmap)
795 continue;
796 for (j = 0; j < bitmap_nr; j++)
797 if (bitmap[0][j]) {
798 /* Step 7, reachability test at commit level */
799 int ret = repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits, 1);
800 if (ret < 0)
801 exit(128);
802 if (!ret) {
803 update_refstatus(ref_status, info->ref->nr, *bitmap);
804 dst++;
805 break;
809 info->nr_ours = dst;
811 free(ca.commits);
814 /* (Delayed) step 7, reachability test at commit level */
815 int delayed_reachability_test(struct shallow_info *si, int c)
817 if (si->need_reachability_test[c]) {
818 struct commit *commit = lookup_commit(the_repository,
819 &si->shallow->oid[c]);
821 if (!si->commits) {
822 struct commit_array ca;
824 memset(&ca, 0, sizeof(ca));
825 head_ref(add_ref, &ca);
826 for_each_ref(add_ref, &ca);
827 si->commits = ca.commits;
828 si->nr_commits = ca.nr;
831 si->reachable[c] = repo_in_merge_bases_many(the_repository,
832 commit,
833 si->nr_commits,
834 si->commits,
836 if (si->reachable[c] < 0)
837 exit(128);
838 si->need_reachability_test[c] = 0;
840 return si->reachable[c];