cocci: remove 'unused.cocci'
[git.git] / shallow.c
blobb4d726bd5954c4cbf4156c9ea8c98822471c9db4
1 #include "cache.h"
2 #include "alloc.h"
3 #include "hex.h"
4 #include "repository.h"
5 #include "tempfile.h"
6 #include "lockfile.h"
7 #include "object-store.h"
8 #include "commit.h"
9 #include "tag.h"
10 #include "pkt-line.h"
11 #include "remote.h"
12 #include "refs.h"
13 #include "oid-array.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 "wrapper.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 commit->parents = NULL;
42 return register_commit_graft(r, graft, 0);
45 int unregister_shallow(const struct object_id *oid)
47 int pos = commit_graft_pos(the_repository, oid);
48 if (pos < 0)
49 return -1;
50 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
51 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
52 the_repository->parsed_objects->grafts + pos + 1,
53 the_repository->parsed_objects->grafts_nr - pos - 1);
54 the_repository->parsed_objects->grafts_nr--;
55 return 0;
58 int is_repository_shallow(struct repository *r)
60 FILE *fp;
61 char buf[1024];
62 const char *path = r->parsed_objects->alternate_shallow_file;
64 if (r->parsed_objects->is_shallow >= 0)
65 return r->parsed_objects->is_shallow;
67 if (!path)
68 path = git_path_shallow(r);
70 * fetch-pack sets '--shallow-file ""' as an indicator that no
71 * shallow file should be used. We could just open it and it
72 * will likely fail. But let's do an explicit check instead.
74 if (!*path || (fp = fopen(path, "r")) == NULL) {
75 stat_validity_clear(r->parsed_objects->shallow_stat);
76 r->parsed_objects->is_shallow = 0;
77 return r->parsed_objects->is_shallow;
79 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
80 r->parsed_objects->is_shallow = 1;
82 while (fgets(buf, sizeof(buf), fp)) {
83 struct object_id oid;
84 if (get_oid_hex(buf, &oid))
85 die("bad shallow line: %s", buf);
86 register_shallow(r, &oid);
88 fclose(fp);
89 return r->parsed_objects->is_shallow;
92 static void reset_repository_shallow(struct repository *r)
94 r->parsed_objects->is_shallow = -1;
95 stat_validity_clear(r->parsed_objects->shallow_stat);
96 reset_commit_grafts(r);
99 int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
101 int res = commit_lock_file(&lk->lock);
102 reset_repository_shallow(r);
105 * Update in-memory data structures with the new shallow information,
106 * including unparsing all commits that now have grafts.
108 is_repository_shallow(r);
110 return res;
113 void rollback_shallow_file(struct repository *r, struct shallow_lock *lk)
115 rollback_lock_file(&lk->lock);
116 reset_repository_shallow(r);
120 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
121 * supports a "valid" flag.
123 define_commit_slab(commit_depth, int *);
124 static void free_depth_in_slab(int **ptr)
126 FREE_AND_NULL(*ptr);
128 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
129 int shallow_flag, int not_shallow_flag)
131 int i = 0, cur_depth = 0;
132 struct commit_list *result = NULL;
133 struct object_array stack = OBJECT_ARRAY_INIT;
134 struct commit *commit = NULL;
135 struct commit_graft *graft;
136 struct commit_depth depths;
138 init_commit_depth(&depths);
139 while (commit || i < heads->nr || stack.nr) {
140 struct commit_list *p;
141 if (!commit) {
142 if (i < heads->nr) {
143 int **depth_slot;
144 commit = (struct commit *)
145 deref_tag(the_repository,
146 heads->objects[i++].item,
147 NULL, 0);
148 if (!commit || commit->object.type != OBJ_COMMIT) {
149 commit = NULL;
150 continue;
152 depth_slot = commit_depth_at(&depths, commit);
153 if (!*depth_slot)
154 *depth_slot = xmalloc(sizeof(int));
155 **depth_slot = 0;
156 cur_depth = 0;
157 } else {
158 commit = (struct commit *)
159 object_array_pop(&stack);
160 cur_depth = **commit_depth_at(&depths, commit);
163 parse_commit_or_die(commit);
164 cur_depth++;
165 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
166 (is_repository_shallow(the_repository) && !commit->parents &&
167 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
168 graft->nr_parent < 0)) {
169 commit_list_insert(commit, &result);
170 commit->object.flags |= shallow_flag;
171 commit = NULL;
172 continue;
174 commit->object.flags |= not_shallow_flag;
175 for (p = commit->parents, commit = NULL; p; p = p->next) {
176 int **depth_slot = commit_depth_at(&depths, p->item);
177 if (!*depth_slot) {
178 *depth_slot = xmalloc(sizeof(int));
179 **depth_slot = cur_depth;
180 } else {
181 if (cur_depth >= **depth_slot)
182 continue;
183 **depth_slot = cur_depth;
185 if (p->next)
186 add_object_array(&p->item->object,
187 NULL, &stack);
188 else {
189 commit = p->item;
190 cur_depth = **commit_depth_at(&depths, commit);
194 deep_clear_commit_depth(&depths, free_depth_in_slab);
196 return result;
199 static void show_commit(struct commit *commit, void *data)
201 commit_list_insert(commit, data);
205 * Given rev-list arguments, run rev-list. All reachable commits
206 * except border ones are marked with not_shallow_flag. Border commits
207 * are marked with shallow_flag. The list of border/shallow commits
208 * are also returned.
210 struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
211 int shallow_flag,
212 int not_shallow_flag)
214 struct commit_list *result = NULL, *p;
215 struct commit_list *not_shallow_list = NULL;
216 struct rev_info revs;
217 int both_flags = shallow_flag | not_shallow_flag;
220 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
221 * set at this point. But better be safe than sorry.
223 clear_object_flags(both_flags);
225 is_repository_shallow(the_repository); /* make sure shallows are read */
227 repo_init_revisions(the_repository, &revs, NULL);
228 save_commit_buffer = 0;
229 setup_revisions(ac, av, &revs, NULL);
231 if (prepare_revision_walk(&revs))
232 die("revision walk setup failed");
233 traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
235 if (!not_shallow_list)
236 die("no commits selected for shallow requests");
238 /* Mark all reachable commits as NOT_SHALLOW */
239 for (p = not_shallow_list; p; p = p->next)
240 p->item->object.flags |= not_shallow_flag;
243 * mark border commits SHALLOW + NOT_SHALLOW.
244 * We cannot clear NOT_SHALLOW right now. Imagine border
245 * commit A is processed first, then commit B, whose parent is
246 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
247 * itself is considered border at step 2, which is incorrect.
249 for (p = not_shallow_list; p; p = p->next) {
250 struct commit *c = p->item;
251 struct commit_list *parent;
253 if (repo_parse_commit(the_repository, c))
254 die("unable to parse commit %s",
255 oid_to_hex(&c->object.oid));
257 for (parent = c->parents; parent; parent = parent->next)
258 if (!(parent->item->object.flags & not_shallow_flag)) {
259 c->object.flags |= shallow_flag;
260 commit_list_insert(c, &result);
261 break;
264 free_commit_list(not_shallow_list);
267 * Now we can clean up NOT_SHALLOW on border commits. Having
268 * both flags set can confuse the caller.
270 for (p = result; p; p = p->next) {
271 struct object *o = &p->item->object;
272 if ((o->flags & both_flags) == both_flags)
273 o->flags &= ~not_shallow_flag;
275 release_revisions(&revs);
276 return result;
279 static void check_shallow_file_for_update(struct repository *r)
281 if (r->parsed_objects->is_shallow == -1)
282 BUG("shallow must be initialized by now");
284 if (!stat_validity_check(r->parsed_objects->shallow_stat,
285 git_path_shallow(r)))
286 die("shallow file has changed since we read it");
289 #define SEEN_ONLY 1
290 #define VERBOSE 2
291 #define QUICK 4
293 struct write_shallow_data {
294 struct strbuf *out;
295 int use_pack_protocol;
296 int count;
297 unsigned flags;
300 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
302 struct write_shallow_data *data = cb_data;
303 const char *hex = oid_to_hex(&graft->oid);
304 if (graft->nr_parent != -1)
305 return 0;
306 if (data->flags & QUICK) {
307 if (!repo_has_object_file(the_repository, &graft->oid))
308 return 0;
309 } else if (data->flags & SEEN_ONLY) {
310 struct commit *c = lookup_commit(the_repository, &graft->oid);
311 if (!c || !(c->object.flags & SEEN)) {
312 if (data->flags & VERBOSE)
313 printf("Removing %s from .git/shallow\n",
314 oid_to_hex(&c->object.oid));
315 return 0;
318 data->count++;
319 if (data->use_pack_protocol)
320 packet_buf_write(data->out, "shallow %s", hex);
321 else {
322 strbuf_addstr(data->out, hex);
323 strbuf_addch(data->out, '\n');
325 return 0;
328 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
329 const struct oid_array *extra,
330 unsigned flags)
332 struct write_shallow_data data;
333 int i;
334 data.out = out;
335 data.use_pack_protocol = use_pack_protocol;
336 data.count = 0;
337 data.flags = flags;
338 for_each_commit_graft(write_one_shallow, &data);
339 if (!extra)
340 return data.count;
341 for (i = 0; i < extra->nr; i++) {
342 strbuf_addstr(out, oid_to_hex(extra->oid + i));
343 strbuf_addch(out, '\n');
344 data.count++;
346 return data.count;
349 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
350 const struct oid_array *extra)
352 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
355 const char *setup_temporary_shallow(const struct oid_array *extra)
357 struct tempfile *temp;
358 struct strbuf sb = STRBUF_INIT;
360 if (write_shallow_commits(&sb, 0, extra)) {
361 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
363 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
364 close_tempfile_gently(temp) < 0)
365 die_errno("failed to write to %s",
366 get_tempfile_path(temp));
367 strbuf_release(&sb);
368 return get_tempfile_path(temp);
371 * is_repository_shallow() sees empty string as "no shallow
372 * file".
374 return "";
377 void setup_alternate_shallow(struct shallow_lock *shallow_lock,
378 const char **alternate_shallow_file,
379 const struct oid_array *extra)
381 struct strbuf sb = STRBUF_INIT;
382 int fd;
384 fd = hold_lock_file_for_update(&shallow_lock->lock,
385 git_path_shallow(the_repository),
386 LOCK_DIE_ON_ERROR);
387 check_shallow_file_for_update(the_repository);
388 if (write_shallow_commits(&sb, 0, extra)) {
389 if (write_in_full(fd, sb.buf, sb.len) < 0)
390 die_errno("failed to write to %s",
391 get_lock_file_path(&shallow_lock->lock));
392 *alternate_shallow_file = get_lock_file_path(&shallow_lock->lock);
393 } else
395 * is_repository_shallow() sees empty string as "no
396 * shallow file".
398 *alternate_shallow_file = "";
399 strbuf_release(&sb);
402 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
404 int fd = *(int *)cb;
405 if (graft->nr_parent == -1)
406 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
407 return 0;
410 void advertise_shallow_grafts(int fd)
412 if (!is_repository_shallow(the_repository))
413 return;
414 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
418 * mark_reachable_objects() should have been run prior to this and all
419 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
420 * in which case lines are excised from the shallow file if they refer to
421 * commits that do not exist (any longer).
423 void prune_shallow(unsigned options)
425 struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
426 struct strbuf sb = STRBUF_INIT;
427 unsigned flags = SEEN_ONLY;
428 int fd;
430 if (options & PRUNE_QUICK)
431 flags |= QUICK;
433 if (options & PRUNE_SHOW_ONLY) {
434 flags |= VERBOSE;
435 write_shallow_commits_1(&sb, 0, NULL, flags);
436 strbuf_release(&sb);
437 return;
439 fd = hold_lock_file_for_update(&shallow_lock.lock,
440 git_path_shallow(the_repository),
441 LOCK_DIE_ON_ERROR);
442 check_shallow_file_for_update(the_repository);
443 if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
444 if (write_in_full(fd, sb.buf, sb.len) < 0)
445 die_errno("failed to write to %s",
446 get_lock_file_path(&shallow_lock.lock));
447 commit_shallow_file(the_repository, &shallow_lock);
448 } else {
449 unlink(git_path_shallow(the_repository));
450 rollback_shallow_file(the_repository, &shallow_lock);
452 strbuf_release(&sb);
455 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
458 * Step 1, split sender shallow commits into "ours" and "theirs"
459 * Step 2, clean "ours" based on .git/shallow
461 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
463 int i;
464 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
465 memset(info, 0, sizeof(*info));
466 info->shallow = sa;
467 if (!sa)
468 return;
469 ALLOC_ARRAY(info->ours, sa->nr);
470 ALLOC_ARRAY(info->theirs, sa->nr);
471 for (i = 0; i < sa->nr; i++) {
472 if (repo_has_object_file(the_repository, sa->oid + i)) {
473 struct commit_graft *graft;
474 graft = lookup_commit_graft(the_repository,
475 &sa->oid[i]);
476 if (graft && graft->nr_parent < 0)
477 continue;
478 info->ours[info->nr_ours++] = i;
479 } else
480 info->theirs[info->nr_theirs++] = i;
484 void clear_shallow_info(struct shallow_info *info)
486 free(info->ours);
487 free(info->theirs);
490 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
492 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
494 struct object_id *oid = info->shallow->oid;
495 int i, dst;
496 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
497 for (i = dst = 0; i < info->nr_theirs; i++) {
498 if (i != dst)
499 info->theirs[dst] = info->theirs[i];
500 if (repo_has_object_file(the_repository, oid + info->theirs[i]))
501 dst++;
503 info->nr_theirs = dst;
506 define_commit_slab(ref_bitmap, uint32_t *);
508 #define POOL_SIZE (512 * 1024)
510 struct paint_info {
511 struct ref_bitmap ref_bitmap;
512 unsigned nr_bits;
513 char **pools;
514 char *free, *end;
515 unsigned pool_count;
518 static uint32_t *paint_alloc(struct paint_info *info)
520 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
521 unsigned size = nr * sizeof(uint32_t);
522 void *p;
523 if (!info->pool_count || size > info->end - info->free) {
524 if (size > POOL_SIZE)
525 BUG("pool size too small for %d in paint_alloc()",
526 size);
527 info->pool_count++;
528 REALLOC_ARRAY(info->pools, info->pool_count);
529 info->free = xmalloc(POOL_SIZE);
530 info->pools[info->pool_count - 1] = info->free;
531 info->end = info->free + POOL_SIZE;
533 p = info->free;
534 info->free += size;
535 return p;
539 * Given a commit SHA-1, walk down to parents until either SEEN,
540 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
541 * all walked commits.
543 static void paint_down(struct paint_info *info, const struct object_id *oid,
544 unsigned int id)
546 unsigned int i, nr;
547 struct commit_list *head = NULL;
548 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
549 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
550 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
552 uint32_t *tmp; /* to be freed before return */
553 uint32_t *bitmap;
555 if (!c)
556 return;
558 tmp = xmalloc(bitmap_size);
559 bitmap = paint_alloc(info);
560 memset(bitmap, 0, bitmap_size);
561 bitmap[id / 32] |= (1U << (id % 32));
562 commit_list_insert(c, &head);
563 while (head) {
564 struct commit_list *p;
565 struct commit *c = pop_commit(&head);
566 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
568 /* XXX check "UNINTERESTING" from pack bitmaps if available */
569 if (c->object.flags & (SEEN | UNINTERESTING))
570 continue;
571 else
572 c->object.flags |= SEEN;
574 if (!*refs)
575 *refs = bitmap;
576 else {
577 memcpy(tmp, *refs, bitmap_size);
578 for (i = 0; i < bitmap_nr; i++)
579 tmp[i] |= bitmap[i];
580 if (memcmp(tmp, *refs, bitmap_size)) {
581 *refs = paint_alloc(info);
582 memcpy(*refs, tmp, bitmap_size);
586 if (c->object.flags & BOTTOM)
587 continue;
589 if (repo_parse_commit(the_repository, c))
590 die("unable to parse commit %s",
591 oid_to_hex(&c->object.oid));
593 for (p = c->parents; p; p = p->next) {
594 if (p->item->object.flags & SEEN)
595 continue;
596 commit_list_insert(p->item, &head);
600 nr = get_max_object_index();
601 for (i = 0; i < nr; i++) {
602 struct object *o = get_indexed_object(i);
603 if (o && o->type == OBJ_COMMIT)
604 o->flags &= ~SEEN;
607 free(tmp);
610 static int mark_uninteresting(const char *refname UNUSED,
611 const struct object_id *oid,
612 int flags UNUSED,
613 void *cb_data UNUSED)
615 struct commit *commit = lookup_commit_reference_gently(the_repository,
616 oid, 1);
617 if (!commit)
618 return 0;
619 commit->object.flags |= UNINTERESTING;
620 mark_parents_uninteresting(NULL, commit);
621 return 0;
624 static void post_assign_shallow(struct shallow_info *info,
625 struct ref_bitmap *ref_bitmap,
626 int *ref_status);
628 * Step 6(+7), associate shallow commits with new refs
630 * info->ref must be initialized before calling this function.
632 * If used is not NULL, it's an array of info->shallow->nr
633 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
634 * m-th shallow commit from info->shallow.
636 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
637 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
638 * the ref needs some shallow commits from either info->ours or
639 * info->theirs.
641 void assign_shallow_commits_to_refs(struct shallow_info *info,
642 uint32_t **used, int *ref_status)
644 struct object_id *oid = info->shallow->oid;
645 struct oid_array *ref = info->ref;
646 unsigned int i, nr;
647 int *shallow, nr_shallow = 0;
648 struct paint_info pi;
650 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
651 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
652 for (i = 0; i < info->nr_ours; i++)
653 shallow[nr_shallow++] = info->ours[i];
654 for (i = 0; i < info->nr_theirs; i++)
655 shallow[nr_shallow++] = info->theirs[i];
658 * Prepare the commit graph to track what refs can reach what
659 * (new) shallow commits.
661 nr = get_max_object_index();
662 for (i = 0; i < nr; i++) {
663 struct object *o = get_indexed_object(i);
664 if (!o || o->type != OBJ_COMMIT)
665 continue;
667 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
670 memset(&pi, 0, sizeof(pi));
671 init_ref_bitmap(&pi.ref_bitmap);
672 pi.nr_bits = ref->nr;
675 * "--not --all" to cut short the traversal if new refs
676 * connect to old refs. If not (e.g. force ref updates) it'll
677 * have to go down to the current shallow commits.
679 head_ref(mark_uninteresting, NULL);
680 for_each_ref(mark_uninteresting, NULL);
682 /* Mark potential bottoms so we won't go out of bound */
683 for (i = 0; i < nr_shallow; i++) {
684 struct commit *c = lookup_commit(the_repository,
685 &oid[shallow[i]]);
686 c->object.flags |= BOTTOM;
689 for (i = 0; i < ref->nr; i++)
690 paint_down(&pi, ref->oid + i, i);
692 if (used) {
693 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
694 memset(used, 0, sizeof(*used) * info->shallow->nr);
695 for (i = 0; i < nr_shallow; i++) {
696 const struct commit *c = lookup_commit(the_repository,
697 &oid[shallow[i]]);
698 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
699 if (*map)
700 used[shallow[i]] = xmemdupz(*map, bitmap_size);
703 * unreachable shallow commits are not removed from
704 * "ours" and "theirs". The user is supposed to run
705 * step 7 on every ref separately and not trust "ours"
706 * and "theirs" any more.
708 } else
709 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
711 clear_ref_bitmap(&pi.ref_bitmap);
712 for (i = 0; i < pi.pool_count; i++)
713 free(pi.pools[i]);
714 free(pi.pools);
715 free(shallow);
718 struct commit_array {
719 struct commit **commits;
720 int nr, alloc;
723 static int add_ref(const char *refname UNUSED,
724 const struct object_id *oid,
725 int flags UNUSED,
726 void *cb_data)
728 struct commit_array *ca = cb_data;
729 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
730 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
731 oid, 1);
732 if (ca->commits[ca->nr])
733 ca->nr++;
734 return 0;
737 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
739 unsigned int i;
740 if (!ref_status)
741 return;
742 for (i = 0; i < nr; i++)
743 if (bitmap[i / 32] & (1U << (i % 32)))
744 ref_status[i]++;
748 * Step 7, reachability test on "ours" at commit level
750 static void post_assign_shallow(struct shallow_info *info,
751 struct ref_bitmap *ref_bitmap,
752 int *ref_status)
754 struct object_id *oid = info->shallow->oid;
755 struct commit *c;
756 uint32_t **bitmap;
757 int dst, i, j;
758 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
759 struct commit_array ca;
761 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
762 if (ref_status)
763 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
765 /* Remove unreachable shallow commits from "theirs" */
766 for (i = dst = 0; i < info->nr_theirs; i++) {
767 if (i != dst)
768 info->theirs[dst] = info->theirs[i];
769 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
770 bitmap = ref_bitmap_at(ref_bitmap, c);
771 if (!*bitmap)
772 continue;
773 for (j = 0; j < bitmap_nr; j++)
774 if (bitmap[0][j]) {
775 update_refstatus(ref_status, info->ref->nr, *bitmap);
776 dst++;
777 break;
780 info->nr_theirs = dst;
782 memset(&ca, 0, sizeof(ca));
783 head_ref(add_ref, &ca);
784 for_each_ref(add_ref, &ca);
786 /* Remove unreachable shallow commits from "ours" */
787 for (i = dst = 0; i < info->nr_ours; i++) {
788 if (i != dst)
789 info->ours[dst] = info->ours[i];
790 c = lookup_commit(the_repository, &oid[info->ours[i]]);
791 bitmap = ref_bitmap_at(ref_bitmap, c);
792 if (!*bitmap)
793 continue;
794 for (j = 0; j < bitmap_nr; j++)
795 if (bitmap[0][j] &&
796 /* Step 7, reachability test at commit level */
797 !repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits)) {
798 update_refstatus(ref_status, info->ref->nr, *bitmap);
799 dst++;
800 break;
803 info->nr_ours = dst;
805 free(ca.commits);
808 /* (Delayed) step 7, reachability test at commit level */
809 int delayed_reachability_test(struct shallow_info *si, int c)
811 if (si->need_reachability_test[c]) {
812 struct commit *commit = lookup_commit(the_repository,
813 &si->shallow->oid[c]);
815 if (!si->commits) {
816 struct commit_array ca;
818 memset(&ca, 0, sizeof(ca));
819 head_ref(add_ref, &ca);
820 for_each_ref(add_ref, &ca);
821 si->commits = ca.commits;
822 si->nr_commits = ca.nr;
825 si->reachable[c] = repo_in_merge_bases_many(the_repository,
826 commit,
827 si->nr_commits,
828 si->commits);
829 si->need_reachability_test[c] = 0;
831 return si->reachable[c];