Merge branch 'dh/encoding-trace-optim'
[git/gitster.git] / shallow.c
blob31a6ca40fe2287e37092dc2251d1f6b8515455e0
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "hex.h"
5 #include "repository.h"
6 #include "tempfile.h"
7 #include "lockfile.h"
8 #include "object-store-ll.h"
9 #include "commit.h"
10 #include "tag.h"
11 #include "pkt-line.h"
12 #include "refs.h"
13 #include "oid-array.h"
14 #include "path.h"
15 #include "diff.h"
16 #include "revision.h"
17 #include "commit-slab.h"
18 #include "list-objects.h"
19 #include "commit-reach.h"
20 #include "shallow.h"
21 #include "statinfo.h"
22 #include "trace.h"
24 void set_alternate_shallow_file(struct repository *r, const char *path, int override)
26 if (r->parsed_objects->is_shallow != -1)
27 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
28 if (r->parsed_objects->alternate_shallow_file && !override)
29 return;
30 free(r->parsed_objects->alternate_shallow_file);
31 r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
34 int register_shallow(struct repository *r, const struct object_id *oid)
36 struct commit_graft *graft =
37 xmalloc(sizeof(struct commit_graft));
38 struct commit *commit = lookup_commit(r, oid);
40 oidcpy(&graft->oid, oid);
41 graft->nr_parent = -1;
42 if (commit && commit->object.parsed) {
43 free_commit_list(commit->parents);
44 commit->parents = NULL;
46 return register_commit_graft(r, graft, 0);
49 int unregister_shallow(const struct object_id *oid)
51 int pos = commit_graft_pos(the_repository, oid);
52 if (pos < 0)
53 return -1;
54 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
55 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
56 the_repository->parsed_objects->grafts + pos + 1,
57 the_repository->parsed_objects->grafts_nr - pos - 1);
58 the_repository->parsed_objects->grafts_nr--;
59 return 0;
62 int is_repository_shallow(struct repository *r)
64 FILE *fp;
65 char buf[1024];
66 const char *path = r->parsed_objects->alternate_shallow_file;
68 if (r->parsed_objects->is_shallow >= 0)
69 return r->parsed_objects->is_shallow;
71 if (!path)
72 path = git_path_shallow(r);
74 * fetch-pack sets '--shallow-file ""' as an indicator that no
75 * shallow file should be used. We could just open it and it
76 * will likely fail. But let's do an explicit check instead.
78 if (!*path || (fp = fopen(path, "r")) == NULL) {
79 stat_validity_clear(r->parsed_objects->shallow_stat);
80 r->parsed_objects->is_shallow = 0;
81 return r->parsed_objects->is_shallow;
83 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
84 r->parsed_objects->is_shallow = 1;
86 while (fgets(buf, sizeof(buf), fp)) {
87 struct object_id oid;
88 if (get_oid_hex(buf, &oid))
89 die("bad shallow line: %s", buf);
90 register_shallow(r, &oid);
92 fclose(fp);
93 return r->parsed_objects->is_shallow;
96 static void reset_repository_shallow(struct repository *r)
98 r->parsed_objects->is_shallow = -1;
99 stat_validity_clear(r->parsed_objects->shallow_stat);
100 reset_commit_grafts(r);
103 int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
105 int res = commit_lock_file(&lk->lock);
106 reset_repository_shallow(r);
109 * Update in-memory data structures with the new shallow information,
110 * including unparsing all commits that now have grafts.
112 is_repository_shallow(r);
114 return res;
117 void rollback_shallow_file(struct repository *r, struct shallow_lock *lk)
119 rollback_lock_file(&lk->lock);
120 reset_repository_shallow(r);
124 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
125 * supports a "valid" flag.
127 define_commit_slab(commit_depth, int *);
128 static void free_depth_in_slab(int **ptr)
130 FREE_AND_NULL(*ptr);
132 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
133 int shallow_flag, int not_shallow_flag)
135 int i = 0, cur_depth = 0;
136 struct commit_list *result = NULL;
137 struct object_array stack = OBJECT_ARRAY_INIT;
138 struct commit *commit = NULL;
139 struct commit_graft *graft;
140 struct commit_depth depths;
142 init_commit_depth(&depths);
143 while (commit || i < heads->nr || stack.nr) {
144 struct commit_list *p;
145 if (!commit) {
146 if (i < heads->nr) {
147 int **depth_slot;
148 commit = (struct commit *)
149 deref_tag(the_repository,
150 heads->objects[i++].item,
151 NULL, 0);
152 if (!commit || commit->object.type != OBJ_COMMIT) {
153 commit = NULL;
154 continue;
156 depth_slot = commit_depth_at(&depths, commit);
157 if (!*depth_slot)
158 *depth_slot = xmalloc(sizeof(int));
159 **depth_slot = 0;
160 cur_depth = 0;
161 } else {
162 commit = (struct commit *)
163 object_array_pop(&stack);
164 cur_depth = **commit_depth_at(&depths, commit);
167 parse_commit_or_die(commit);
168 cur_depth++;
169 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
170 (is_repository_shallow(the_repository) && !commit->parents &&
171 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
172 graft->nr_parent < 0)) {
173 commit_list_insert(commit, &result);
174 commit->object.flags |= shallow_flag;
175 commit = NULL;
176 continue;
178 commit->object.flags |= not_shallow_flag;
179 for (p = commit->parents, commit = NULL; p; p = p->next) {
180 int **depth_slot = commit_depth_at(&depths, p->item);
181 if (!*depth_slot) {
182 *depth_slot = xmalloc(sizeof(int));
183 **depth_slot = cur_depth;
184 } else {
185 if (cur_depth >= **depth_slot)
186 continue;
187 **depth_slot = cur_depth;
189 if (p->next)
190 add_object_array(&p->item->object,
191 NULL, &stack);
192 else {
193 commit = p->item;
194 cur_depth = **commit_depth_at(&depths, commit);
198 deep_clear_commit_depth(&depths, free_depth_in_slab);
200 return result;
203 static void show_commit(struct commit *commit, void *data)
205 commit_list_insert(commit, data);
209 * Given rev-list arguments, run rev-list. All reachable commits
210 * except border ones are marked with not_shallow_flag. Border commits
211 * are marked with shallow_flag. The list of border/shallow commits
212 * are also returned.
214 struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
215 int shallow_flag,
216 int not_shallow_flag)
218 struct commit_list *result = NULL, *p;
219 struct commit_list *not_shallow_list = NULL;
220 struct rev_info revs;
221 int both_flags = shallow_flag | not_shallow_flag;
224 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
225 * set at this point. But better be safe than sorry.
227 clear_object_flags(both_flags);
229 is_repository_shallow(the_repository); /* make sure shallows are read */
231 repo_init_revisions(the_repository, &revs, NULL);
232 save_commit_buffer = 0;
233 setup_revisions(ac, av, &revs, NULL);
235 if (prepare_revision_walk(&revs))
236 die("revision walk setup failed");
237 traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
239 if (!not_shallow_list)
240 die("no commits selected for shallow requests");
242 /* Mark all reachable commits as NOT_SHALLOW */
243 for (p = not_shallow_list; p; p = p->next)
244 p->item->object.flags |= not_shallow_flag;
247 * mark border commits SHALLOW + NOT_SHALLOW.
248 * We cannot clear NOT_SHALLOW right now. Imagine border
249 * commit A is processed first, then commit B, whose parent is
250 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
251 * itself is considered border at step 2, which is incorrect.
253 for (p = not_shallow_list; p; p = p->next) {
254 struct commit *c = p->item;
255 struct commit_list *parent;
257 if (repo_parse_commit(the_repository, c))
258 die("unable to parse commit %s",
259 oid_to_hex(&c->object.oid));
261 for (parent = c->parents; parent; parent = parent->next)
262 if (!(parent->item->object.flags & not_shallow_flag)) {
263 c->object.flags |= shallow_flag;
264 commit_list_insert(c, &result);
265 break;
268 free_commit_list(not_shallow_list);
271 * Now we can clean up NOT_SHALLOW on border commits. Having
272 * both flags set can confuse the caller.
274 for (p = result; p; p = p->next) {
275 struct object *o = &p->item->object;
276 if ((o->flags & both_flags) == both_flags)
277 o->flags &= ~not_shallow_flag;
279 release_revisions(&revs);
280 return result;
283 static void check_shallow_file_for_update(struct repository *r)
285 if (r->parsed_objects->is_shallow == -1)
286 BUG("shallow must be initialized by now");
288 if (!stat_validity_check(r->parsed_objects->shallow_stat,
289 git_path_shallow(r)))
290 die("shallow file has changed since we read it");
293 #define SEEN_ONLY 1
294 #define VERBOSE 2
295 #define QUICK 4
297 struct write_shallow_data {
298 struct strbuf *out;
299 int use_pack_protocol;
300 int count;
301 unsigned flags;
304 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
306 struct write_shallow_data *data = cb_data;
307 const char *hex = oid_to_hex(&graft->oid);
308 if (graft->nr_parent != -1)
309 return 0;
310 if (data->flags & QUICK) {
311 if (!repo_has_object_file(the_repository, &graft->oid))
312 return 0;
313 } else if (data->flags & SEEN_ONLY) {
314 struct commit *c = lookup_commit(the_repository, &graft->oid);
315 if (!c || !(c->object.flags & SEEN)) {
316 if (data->flags & VERBOSE)
317 printf("Removing %s from .git/shallow\n",
318 oid_to_hex(&c->object.oid));
319 return 0;
322 data->count++;
323 if (data->use_pack_protocol)
324 packet_buf_write(data->out, "shallow %s", hex);
325 else {
326 strbuf_addstr(data->out, hex);
327 strbuf_addch(data->out, '\n');
329 return 0;
332 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
333 const struct oid_array *extra,
334 unsigned flags)
336 struct write_shallow_data data;
337 int i;
338 data.out = out;
339 data.use_pack_protocol = use_pack_protocol;
340 data.count = 0;
341 data.flags = flags;
342 for_each_commit_graft(write_one_shallow, &data);
343 if (!extra)
344 return data.count;
345 for (i = 0; i < extra->nr; i++) {
346 strbuf_addstr(out, oid_to_hex(extra->oid + i));
347 strbuf_addch(out, '\n');
348 data.count++;
350 return data.count;
353 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
354 const struct oid_array *extra)
356 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
359 const char *setup_temporary_shallow(const struct oid_array *extra)
361 struct tempfile *temp;
362 struct strbuf sb = STRBUF_INIT;
364 if (write_shallow_commits(&sb, 0, extra)) {
365 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
367 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
368 close_tempfile_gently(temp) < 0)
369 die_errno("failed to write to %s",
370 get_tempfile_path(temp));
371 strbuf_release(&sb);
372 return get_tempfile_path(temp);
375 * is_repository_shallow() sees empty string as "no shallow
376 * file".
378 return "";
381 void setup_alternate_shallow(struct shallow_lock *shallow_lock,
382 const char **alternate_shallow_file,
383 const struct oid_array *extra)
385 struct strbuf sb = STRBUF_INIT;
386 int fd;
388 fd = hold_lock_file_for_update(&shallow_lock->lock,
389 git_path_shallow(the_repository),
390 LOCK_DIE_ON_ERROR);
391 check_shallow_file_for_update(the_repository);
392 if (write_shallow_commits(&sb, 0, extra)) {
393 if (write_in_full(fd, sb.buf, sb.len) < 0)
394 die_errno("failed to write to %s",
395 get_lock_file_path(&shallow_lock->lock));
396 *alternate_shallow_file = get_lock_file_path(&shallow_lock->lock);
397 } else
399 * is_repository_shallow() sees empty string as "no
400 * shallow file".
402 *alternate_shallow_file = "";
403 strbuf_release(&sb);
406 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
408 int fd = *(int *)cb;
409 if (graft->nr_parent == -1)
410 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
411 return 0;
414 void advertise_shallow_grafts(int fd)
416 if (!is_repository_shallow(the_repository))
417 return;
418 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
422 * mark_reachable_objects() should have been run prior to this and all
423 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
424 * in which case lines are excised from the shallow file if they refer to
425 * commits that do not exist (any longer).
427 void prune_shallow(unsigned options)
429 struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
430 struct strbuf sb = STRBUF_INIT;
431 unsigned flags = SEEN_ONLY;
432 int fd;
434 if (options & PRUNE_QUICK)
435 flags |= QUICK;
437 if (options & PRUNE_SHOW_ONLY) {
438 flags |= VERBOSE;
439 write_shallow_commits_1(&sb, 0, NULL, flags);
440 strbuf_release(&sb);
441 return;
443 fd = hold_lock_file_for_update(&shallow_lock.lock,
444 git_path_shallow(the_repository),
445 LOCK_DIE_ON_ERROR);
446 check_shallow_file_for_update(the_repository);
447 if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
448 if (write_in_full(fd, sb.buf, sb.len) < 0)
449 die_errno("failed to write to %s",
450 get_lock_file_path(&shallow_lock.lock));
451 commit_shallow_file(the_repository, &shallow_lock);
452 } else {
453 unlink(git_path_shallow(the_repository));
454 rollback_shallow_file(the_repository, &shallow_lock);
456 strbuf_release(&sb);
459 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
462 * Step 1, split sender shallow commits into "ours" and "theirs"
463 * Step 2, clean "ours" based on .git/shallow
465 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
467 int i;
468 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
469 memset(info, 0, sizeof(*info));
470 info->shallow = sa;
471 if (!sa)
472 return;
473 ALLOC_ARRAY(info->ours, sa->nr);
474 ALLOC_ARRAY(info->theirs, sa->nr);
475 for (i = 0; i < sa->nr; i++) {
476 if (repo_has_object_file(the_repository, sa->oid + i)) {
477 struct commit_graft *graft;
478 graft = lookup_commit_graft(the_repository,
479 &sa->oid[i]);
480 if (graft && graft->nr_parent < 0)
481 continue;
482 info->ours[info->nr_ours++] = i;
483 } else
484 info->theirs[info->nr_theirs++] = i;
488 void clear_shallow_info(struct shallow_info *info)
490 free(info->ours);
491 free(info->theirs);
494 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
496 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
498 struct object_id *oid = info->shallow->oid;
499 int i, dst;
500 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
501 for (i = dst = 0; i < info->nr_theirs; i++) {
502 if (i != dst)
503 info->theirs[dst] = info->theirs[i];
504 if (repo_has_object_file(the_repository, oid + info->theirs[i]))
505 dst++;
507 info->nr_theirs = dst;
510 define_commit_slab(ref_bitmap, uint32_t *);
512 #define POOL_SIZE (512 * 1024)
514 struct paint_info {
515 struct ref_bitmap ref_bitmap;
516 unsigned nr_bits;
517 char **pools;
518 char *free, *end;
519 unsigned pool_count;
522 static uint32_t *paint_alloc(struct paint_info *info)
524 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
525 unsigned size = nr * sizeof(uint32_t);
526 void *p;
527 if (!info->pool_count || size > info->end - info->free) {
528 if (size > POOL_SIZE)
529 BUG("pool size too small for %d in paint_alloc()",
530 size);
531 info->pool_count++;
532 REALLOC_ARRAY(info->pools, info->pool_count);
533 info->free = xmalloc(POOL_SIZE);
534 info->pools[info->pool_count - 1] = info->free;
535 info->end = info->free + POOL_SIZE;
537 p = info->free;
538 info->free += size;
539 return p;
543 * Given a commit SHA-1, walk down to parents until either SEEN,
544 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
545 * all walked commits.
547 static void paint_down(struct paint_info *info, const struct object_id *oid,
548 unsigned int id)
550 unsigned int i, nr;
551 struct commit_list *head = NULL;
552 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
553 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
554 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
556 uint32_t *tmp; /* to be freed before return */
557 uint32_t *bitmap;
559 if (!c)
560 return;
562 tmp = xmalloc(bitmap_size);
563 bitmap = paint_alloc(info);
564 memset(bitmap, 0, bitmap_size);
565 bitmap[id / 32] |= (1U << (id % 32));
566 commit_list_insert(c, &head);
567 while (head) {
568 struct commit_list *p;
569 struct commit *c = pop_commit(&head);
570 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
572 /* XXX check "UNINTERESTING" from pack bitmaps if available */
573 if (c->object.flags & (SEEN | UNINTERESTING))
574 continue;
575 else
576 c->object.flags |= SEEN;
578 if (!*refs)
579 *refs = bitmap;
580 else {
581 memcpy(tmp, *refs, bitmap_size);
582 for (i = 0; i < bitmap_nr; i++)
583 tmp[i] |= bitmap[i];
584 if (memcmp(tmp, *refs, bitmap_size)) {
585 *refs = paint_alloc(info);
586 memcpy(*refs, tmp, bitmap_size);
590 if (c->object.flags & BOTTOM)
591 continue;
593 if (repo_parse_commit(the_repository, c))
594 die("unable to parse commit %s",
595 oid_to_hex(&c->object.oid));
597 for (p = c->parents; p; p = p->next) {
598 if (p->item->object.flags & SEEN)
599 continue;
600 commit_list_insert(p->item, &head);
604 nr = get_max_object_index();
605 for (i = 0; i < nr; i++) {
606 struct object *o = get_indexed_object(i);
607 if (o && o->type == OBJ_COMMIT)
608 o->flags &= ~SEEN;
611 free(tmp);
614 static int mark_uninteresting(const char *refname UNUSED,
615 const struct object_id *oid,
616 int flags UNUSED,
617 void *cb_data UNUSED)
619 struct commit *commit = lookup_commit_reference_gently(the_repository,
620 oid, 1);
621 if (!commit)
622 return 0;
623 commit->object.flags |= UNINTERESTING;
624 mark_parents_uninteresting(NULL, commit);
625 return 0;
628 static void post_assign_shallow(struct shallow_info *info,
629 struct ref_bitmap *ref_bitmap,
630 int *ref_status);
632 * Step 6(+7), associate shallow commits with new refs
634 * info->ref must be initialized before calling this function.
636 * If used is not NULL, it's an array of info->shallow->nr
637 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
638 * m-th shallow commit from info->shallow.
640 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
641 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
642 * the ref needs some shallow commits from either info->ours or
643 * info->theirs.
645 void assign_shallow_commits_to_refs(struct shallow_info *info,
646 uint32_t **used, int *ref_status)
648 struct object_id *oid = info->shallow->oid;
649 struct oid_array *ref = info->ref;
650 unsigned int i, nr;
651 int *shallow, nr_shallow = 0;
652 struct paint_info pi;
654 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
655 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
656 for (i = 0; i < info->nr_ours; i++)
657 shallow[nr_shallow++] = info->ours[i];
658 for (i = 0; i < info->nr_theirs; i++)
659 shallow[nr_shallow++] = info->theirs[i];
662 * Prepare the commit graph to track what refs can reach what
663 * (new) shallow commits.
665 nr = get_max_object_index();
666 for (i = 0; i < nr; i++) {
667 struct object *o = get_indexed_object(i);
668 if (!o || o->type != OBJ_COMMIT)
669 continue;
671 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
674 memset(&pi, 0, sizeof(pi));
675 init_ref_bitmap(&pi.ref_bitmap);
676 pi.nr_bits = ref->nr;
679 * "--not --all" to cut short the traversal if new refs
680 * connect to old refs. If not (e.g. force ref updates) it'll
681 * have to go down to the current shallow commits.
683 refs_head_ref(get_main_ref_store(the_repository), mark_uninteresting,
684 NULL);
685 refs_for_each_ref(get_main_ref_store(the_repository),
686 mark_uninteresting, NULL);
688 /* Mark potential bottoms so we won't go out of bound */
689 for (i = 0; i < nr_shallow; i++) {
690 struct commit *c = lookup_commit(the_repository,
691 &oid[shallow[i]]);
692 c->object.flags |= BOTTOM;
695 for (i = 0; i < ref->nr; i++)
696 paint_down(&pi, ref->oid + i, i);
698 if (used) {
699 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
700 memset(used, 0, sizeof(*used) * info->shallow->nr);
701 for (i = 0; i < nr_shallow; i++) {
702 const struct commit *c = lookup_commit(the_repository,
703 &oid[shallow[i]]);
704 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
705 if (*map)
706 used[shallow[i]] = xmemdupz(*map, bitmap_size);
709 * unreachable shallow commits are not removed from
710 * "ours" and "theirs". The user is supposed to run
711 * step 7 on every ref separately and not trust "ours"
712 * and "theirs" any more.
714 } else
715 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
717 clear_ref_bitmap(&pi.ref_bitmap);
718 for (i = 0; i < pi.pool_count; i++)
719 free(pi.pools[i]);
720 free(pi.pools);
721 free(shallow);
724 struct commit_array {
725 struct commit **commits;
726 int nr, alloc;
729 static int add_ref(const char *refname UNUSED,
730 const struct object_id *oid,
731 int flags UNUSED,
732 void *cb_data)
734 struct commit_array *ca = cb_data;
735 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
736 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
737 oid, 1);
738 if (ca->commits[ca->nr])
739 ca->nr++;
740 return 0;
743 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
745 unsigned int i;
746 if (!ref_status)
747 return;
748 for (i = 0; i < nr; i++)
749 if (bitmap[i / 32] & (1U << (i % 32)))
750 ref_status[i]++;
754 * Step 7, reachability test on "ours" at commit level
756 static void post_assign_shallow(struct shallow_info *info,
757 struct ref_bitmap *ref_bitmap,
758 int *ref_status)
760 struct object_id *oid = info->shallow->oid;
761 struct commit *c;
762 uint32_t **bitmap;
763 int dst, i, j;
764 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
765 struct commit_array ca;
767 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
768 if (ref_status)
769 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
771 /* Remove unreachable shallow commits from "theirs" */
772 for (i = dst = 0; i < info->nr_theirs; i++) {
773 if (i != dst)
774 info->theirs[dst] = info->theirs[i];
775 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
776 bitmap = ref_bitmap_at(ref_bitmap, c);
777 if (!*bitmap)
778 continue;
779 for (j = 0; j < bitmap_nr; j++)
780 if (bitmap[0][j]) {
781 update_refstatus(ref_status, info->ref->nr, *bitmap);
782 dst++;
783 break;
786 info->nr_theirs = dst;
788 memset(&ca, 0, sizeof(ca));
789 refs_head_ref(get_main_ref_store(the_repository), add_ref, &ca);
790 refs_for_each_ref(get_main_ref_store(the_repository), add_ref, &ca);
792 /* Remove unreachable shallow commits from "ours" */
793 for (i = dst = 0; i < info->nr_ours; i++) {
794 if (i != dst)
795 info->ours[dst] = info->ours[i];
796 c = lookup_commit(the_repository, &oid[info->ours[i]]);
797 bitmap = ref_bitmap_at(ref_bitmap, c);
798 if (!*bitmap)
799 continue;
800 for (j = 0; j < bitmap_nr; j++)
801 if (bitmap[0][j]) {
802 /* Step 7, reachability test at commit level */
803 int ret = repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits, 1);
804 if (ret < 0)
805 exit(128);
806 if (!ret) {
807 update_refstatus(ref_status, info->ref->nr, *bitmap);
808 dst++;
809 break;
813 info->nr_ours = dst;
815 free(ca.commits);
818 /* (Delayed) step 7, reachability test at commit level */
819 int delayed_reachability_test(struct shallow_info *si, int c)
821 if (si->need_reachability_test[c]) {
822 struct commit *commit = lookup_commit(the_repository,
823 &si->shallow->oid[c]);
825 if (!si->commits) {
826 struct commit_array ca;
828 memset(&ca, 0, sizeof(ca));
829 refs_head_ref(get_main_ref_store(the_repository),
830 add_ref, &ca);
831 refs_for_each_ref(get_main_ref_store(the_repository),
832 add_ref, &ca);
833 si->commits = ca.commits;
834 si->nr_commits = ca.nr;
837 si->reachable[c] = repo_in_merge_bases_many(the_repository,
838 commit,
839 si->nr_commits,
840 si->commits,
842 if (si->reachable[c] < 0)
843 exit(128);
844 si->need_reachability_test[c] = 0;
846 return si->reachable[c];