debian: new upstream release
[git/debian.git] / shallow.c
blobac728cdd77892dab1c323fc81adaf240d988c6d9
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 free_commit_list(commit->parents);
43 commit->parents = NULL;
45 return register_commit_graft(r, graft, 0);
48 int unregister_shallow(const struct object_id *oid)
50 int pos = commit_graft_pos(the_repository, oid);
51 if (pos < 0)
52 return -1;
53 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
54 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
55 the_repository->parsed_objects->grafts + pos + 1,
56 the_repository->parsed_objects->grafts_nr - pos - 1);
57 the_repository->parsed_objects->grafts_nr--;
58 return 0;
61 int is_repository_shallow(struct repository *r)
63 FILE *fp;
64 char buf[1024];
65 const char *path = r->parsed_objects->alternate_shallow_file;
67 if (r->parsed_objects->is_shallow >= 0)
68 return r->parsed_objects->is_shallow;
70 if (!path)
71 path = git_path_shallow(r);
73 * fetch-pack sets '--shallow-file ""' as an indicator that no
74 * shallow file should be used. We could just open it and it
75 * will likely fail. But let's do an explicit check instead.
77 if (!*path || (fp = fopen(path, "r")) == NULL) {
78 stat_validity_clear(r->parsed_objects->shallow_stat);
79 r->parsed_objects->is_shallow = 0;
80 return r->parsed_objects->is_shallow;
82 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
83 r->parsed_objects->is_shallow = 1;
85 while (fgets(buf, sizeof(buf), fp)) {
86 struct object_id oid;
87 if (get_oid_hex(buf, &oid))
88 die("bad shallow line: %s", buf);
89 register_shallow(r, &oid);
91 fclose(fp);
92 return r->parsed_objects->is_shallow;
95 static void reset_repository_shallow(struct repository *r)
97 r->parsed_objects->is_shallow = -1;
98 stat_validity_clear(r->parsed_objects->shallow_stat);
99 reset_commit_grafts(r);
102 int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
104 int res = commit_lock_file(&lk->lock);
105 reset_repository_shallow(r);
108 * Update in-memory data structures with the new shallow information,
109 * including unparsing all commits that now have grafts.
111 is_repository_shallow(r);
113 return res;
116 void rollback_shallow_file(struct repository *r, struct shallow_lock *lk)
118 rollback_lock_file(&lk->lock);
119 reset_repository_shallow(r);
123 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
124 * supports a "valid" flag.
126 define_commit_slab(commit_depth, int *);
127 static void free_depth_in_slab(int **ptr)
129 FREE_AND_NULL(*ptr);
131 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
132 int shallow_flag, int not_shallow_flag)
134 int i = 0, cur_depth = 0;
135 struct commit_list *result = NULL;
136 struct object_array stack = OBJECT_ARRAY_INIT;
137 struct commit *commit = NULL;
138 struct commit_graft *graft;
139 struct commit_depth depths;
141 init_commit_depth(&depths);
142 while (commit || i < heads->nr || stack.nr) {
143 struct commit_list *p;
144 if (!commit) {
145 if (i < heads->nr) {
146 int **depth_slot;
147 commit = (struct commit *)
148 deref_tag(the_repository,
149 heads->objects[i++].item,
150 NULL, 0);
151 if (!commit || commit->object.type != OBJ_COMMIT) {
152 commit = NULL;
153 continue;
155 depth_slot = commit_depth_at(&depths, commit);
156 if (!*depth_slot)
157 *depth_slot = xmalloc(sizeof(int));
158 **depth_slot = 0;
159 cur_depth = 0;
160 } else {
161 commit = (struct commit *)
162 object_array_pop(&stack);
163 cur_depth = **commit_depth_at(&depths, commit);
166 parse_commit_or_die(commit);
167 cur_depth++;
168 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
169 (is_repository_shallow(the_repository) && !commit->parents &&
170 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
171 graft->nr_parent < 0)) {
172 commit_list_insert(commit, &result);
173 commit->object.flags |= shallow_flag;
174 commit = NULL;
175 continue;
177 commit->object.flags |= not_shallow_flag;
178 for (p = commit->parents, commit = NULL; p; p = p->next) {
179 int **depth_slot = commit_depth_at(&depths, p->item);
180 if (!*depth_slot) {
181 *depth_slot = xmalloc(sizeof(int));
182 **depth_slot = cur_depth;
183 } else {
184 if (cur_depth >= **depth_slot)
185 continue;
186 **depth_slot = cur_depth;
188 if (p->next)
189 add_object_array(&p->item->object,
190 NULL, &stack);
191 else {
192 commit = p->item;
193 cur_depth = **commit_depth_at(&depths, commit);
197 deep_clear_commit_depth(&depths, free_depth_in_slab);
199 return result;
202 static void show_commit(struct commit *commit, void *data)
204 commit_list_insert(commit, data);
208 * Given rev-list arguments, run rev-list. All reachable commits
209 * except border ones are marked with not_shallow_flag. Border commits
210 * are marked with shallow_flag. The list of border/shallow commits
211 * are also returned.
213 struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
214 int shallow_flag,
215 int not_shallow_flag)
217 struct commit_list *result = NULL, *p;
218 struct commit_list *not_shallow_list = NULL;
219 struct rev_info revs;
220 int both_flags = shallow_flag | not_shallow_flag;
223 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
224 * set at this point. But better be safe than sorry.
226 clear_object_flags(both_flags);
228 is_repository_shallow(the_repository); /* make sure shallows are read */
230 repo_init_revisions(the_repository, &revs, NULL);
231 save_commit_buffer = 0;
232 setup_revisions(ac, av, &revs, NULL);
234 if (prepare_revision_walk(&revs))
235 die("revision walk setup failed");
236 traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
238 if (!not_shallow_list)
239 die("no commits selected for shallow requests");
241 /* Mark all reachable commits as NOT_SHALLOW */
242 for (p = not_shallow_list; p; p = p->next)
243 p->item->object.flags |= not_shallow_flag;
246 * mark border commits SHALLOW + NOT_SHALLOW.
247 * We cannot clear NOT_SHALLOW right now. Imagine border
248 * commit A is processed first, then commit B, whose parent is
249 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
250 * itself is considered border at step 2, which is incorrect.
252 for (p = not_shallow_list; p; p = p->next) {
253 struct commit *c = p->item;
254 struct commit_list *parent;
256 if (repo_parse_commit(the_repository, c))
257 die("unable to parse commit %s",
258 oid_to_hex(&c->object.oid));
260 for (parent = c->parents; parent; parent = parent->next)
261 if (!(parent->item->object.flags & not_shallow_flag)) {
262 c->object.flags |= shallow_flag;
263 commit_list_insert(c, &result);
264 break;
267 free_commit_list(not_shallow_list);
270 * Now we can clean up NOT_SHALLOW on border commits. Having
271 * both flags set can confuse the caller.
273 for (p = result; p; p = p->next) {
274 struct object *o = &p->item->object;
275 if ((o->flags & both_flags) == both_flags)
276 o->flags &= ~not_shallow_flag;
278 release_revisions(&revs);
279 return result;
282 static void check_shallow_file_for_update(struct repository *r)
284 if (r->parsed_objects->is_shallow == -1)
285 BUG("shallow must be initialized by now");
287 if (!stat_validity_check(r->parsed_objects->shallow_stat,
288 git_path_shallow(r)))
289 die("shallow file has changed since we read it");
292 #define SEEN_ONLY 1
293 #define VERBOSE 2
294 #define QUICK 4
296 struct write_shallow_data {
297 struct strbuf *out;
298 int use_pack_protocol;
299 int count;
300 unsigned flags;
303 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
305 struct write_shallow_data *data = cb_data;
306 const char *hex = oid_to_hex(&graft->oid);
307 if (graft->nr_parent != -1)
308 return 0;
309 if (data->flags & QUICK) {
310 if (!repo_has_object_file(the_repository, &graft->oid))
311 return 0;
312 } else if (data->flags & SEEN_ONLY) {
313 struct commit *c = lookup_commit(the_repository, &graft->oid);
314 if (!c || !(c->object.flags & SEEN)) {
315 if (data->flags & VERBOSE)
316 printf("Removing %s from .git/shallow\n",
317 oid_to_hex(&c->object.oid));
318 return 0;
321 data->count++;
322 if (data->use_pack_protocol)
323 packet_buf_write(data->out, "shallow %s", hex);
324 else {
325 strbuf_addstr(data->out, hex);
326 strbuf_addch(data->out, '\n');
328 return 0;
331 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
332 const struct oid_array *extra,
333 unsigned flags)
335 struct write_shallow_data data;
336 int i;
337 data.out = out;
338 data.use_pack_protocol = use_pack_protocol;
339 data.count = 0;
340 data.flags = flags;
341 for_each_commit_graft(write_one_shallow, &data);
342 if (!extra)
343 return data.count;
344 for (i = 0; i < extra->nr; i++) {
345 strbuf_addstr(out, oid_to_hex(extra->oid + i));
346 strbuf_addch(out, '\n');
347 data.count++;
349 return data.count;
352 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
353 const struct oid_array *extra)
355 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
358 const char *setup_temporary_shallow(const struct oid_array *extra)
360 struct tempfile *temp;
361 struct strbuf sb = STRBUF_INIT;
363 if (write_shallow_commits(&sb, 0, extra)) {
364 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
366 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
367 close_tempfile_gently(temp) < 0)
368 die_errno("failed to write to %s",
369 get_tempfile_path(temp));
370 strbuf_release(&sb);
371 return get_tempfile_path(temp);
374 * is_repository_shallow() sees empty string as "no shallow
375 * file".
377 return "";
380 void setup_alternate_shallow(struct shallow_lock *shallow_lock,
381 const char **alternate_shallow_file,
382 const struct oid_array *extra)
384 struct strbuf sb = STRBUF_INIT;
385 int fd;
387 fd = hold_lock_file_for_update(&shallow_lock->lock,
388 git_path_shallow(the_repository),
389 LOCK_DIE_ON_ERROR);
390 check_shallow_file_for_update(the_repository);
391 if (write_shallow_commits(&sb, 0, extra)) {
392 if (write_in_full(fd, sb.buf, sb.len) < 0)
393 die_errno("failed to write to %s",
394 get_lock_file_path(&shallow_lock->lock));
395 *alternate_shallow_file = get_lock_file_path(&shallow_lock->lock);
396 } else
398 * is_repository_shallow() sees empty string as "no
399 * shallow file".
401 *alternate_shallow_file = "";
402 strbuf_release(&sb);
405 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
407 int fd = *(int *)cb;
408 if (graft->nr_parent == -1)
409 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
410 return 0;
413 void advertise_shallow_grafts(int fd)
415 if (!is_repository_shallow(the_repository))
416 return;
417 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
421 * mark_reachable_objects() should have been run prior to this and all
422 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
423 * in which case lines are excised from the shallow file if they refer to
424 * commits that do not exist (any longer).
426 void prune_shallow(unsigned options)
428 struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
429 struct strbuf sb = STRBUF_INIT;
430 unsigned flags = SEEN_ONLY;
431 int fd;
433 if (options & PRUNE_QUICK)
434 flags |= QUICK;
436 if (options & PRUNE_SHOW_ONLY) {
437 flags |= VERBOSE;
438 write_shallow_commits_1(&sb, 0, NULL, flags);
439 strbuf_release(&sb);
440 return;
442 fd = hold_lock_file_for_update(&shallow_lock.lock,
443 git_path_shallow(the_repository),
444 LOCK_DIE_ON_ERROR);
445 check_shallow_file_for_update(the_repository);
446 if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
447 if (write_in_full(fd, sb.buf, sb.len) < 0)
448 die_errno("failed to write to %s",
449 get_lock_file_path(&shallow_lock.lock));
450 commit_shallow_file(the_repository, &shallow_lock);
451 } else {
452 unlink(git_path_shallow(the_repository));
453 rollback_shallow_file(the_repository, &shallow_lock);
455 strbuf_release(&sb);
458 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
461 * Step 1, split sender shallow commits into "ours" and "theirs"
462 * Step 2, clean "ours" based on .git/shallow
464 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
466 int i;
467 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
468 memset(info, 0, sizeof(*info));
469 info->shallow = sa;
470 if (!sa)
471 return;
472 ALLOC_ARRAY(info->ours, sa->nr);
473 ALLOC_ARRAY(info->theirs, sa->nr);
474 for (i = 0; i < sa->nr; i++) {
475 if (repo_has_object_file(the_repository, sa->oid + i)) {
476 struct commit_graft *graft;
477 graft = lookup_commit_graft(the_repository,
478 &sa->oid[i]);
479 if (graft && graft->nr_parent < 0)
480 continue;
481 info->ours[info->nr_ours++] = i;
482 } else
483 info->theirs[info->nr_theirs++] = i;
487 void clear_shallow_info(struct shallow_info *info)
489 free(info->ours);
490 free(info->theirs);
493 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
495 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
497 struct object_id *oid = info->shallow->oid;
498 int i, dst;
499 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
500 for (i = dst = 0; i < info->nr_theirs; i++) {
501 if (i != dst)
502 info->theirs[dst] = info->theirs[i];
503 if (repo_has_object_file(the_repository, oid + info->theirs[i]))
504 dst++;
506 info->nr_theirs = dst;
509 define_commit_slab(ref_bitmap, uint32_t *);
511 #define POOL_SIZE (512 * 1024)
513 struct paint_info {
514 struct ref_bitmap ref_bitmap;
515 unsigned nr_bits;
516 char **pools;
517 char *free, *end;
518 unsigned pool_count;
521 static uint32_t *paint_alloc(struct paint_info *info)
523 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
524 unsigned size = nr * sizeof(uint32_t);
525 void *p;
526 if (!info->pool_count || size > info->end - info->free) {
527 if (size > POOL_SIZE)
528 BUG("pool size too small for %d in paint_alloc()",
529 size);
530 info->pool_count++;
531 REALLOC_ARRAY(info->pools, info->pool_count);
532 info->free = xmalloc(POOL_SIZE);
533 info->pools[info->pool_count - 1] = info->free;
534 info->end = info->free + POOL_SIZE;
536 p = info->free;
537 info->free += size;
538 return p;
542 * Given a commit SHA-1, walk down to parents until either SEEN,
543 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
544 * all walked commits.
546 static void paint_down(struct paint_info *info, const struct object_id *oid,
547 unsigned int id)
549 unsigned int i, nr;
550 struct commit_list *head = NULL;
551 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
552 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
553 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
555 uint32_t *tmp; /* to be freed before return */
556 uint32_t *bitmap;
558 if (!c)
559 return;
561 tmp = xmalloc(bitmap_size);
562 bitmap = paint_alloc(info);
563 memset(bitmap, 0, bitmap_size);
564 bitmap[id / 32] |= (1U << (id % 32));
565 commit_list_insert(c, &head);
566 while (head) {
567 struct commit_list *p;
568 struct commit *c = pop_commit(&head);
569 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
571 /* XXX check "UNINTERESTING" from pack bitmaps if available */
572 if (c->object.flags & (SEEN | UNINTERESTING))
573 continue;
574 else
575 c->object.flags |= SEEN;
577 if (!*refs)
578 *refs = bitmap;
579 else {
580 memcpy(tmp, *refs, bitmap_size);
581 for (i = 0; i < bitmap_nr; i++)
582 tmp[i] |= bitmap[i];
583 if (memcmp(tmp, *refs, bitmap_size)) {
584 *refs = paint_alloc(info);
585 memcpy(*refs, tmp, bitmap_size);
589 if (c->object.flags & BOTTOM)
590 continue;
592 if (repo_parse_commit(the_repository, c))
593 die("unable to parse commit %s",
594 oid_to_hex(&c->object.oid));
596 for (p = c->parents; p; p = p->next) {
597 if (p->item->object.flags & SEEN)
598 continue;
599 commit_list_insert(p->item, &head);
603 nr = get_max_object_index();
604 for (i = 0; i < nr; i++) {
605 struct object *o = get_indexed_object(i);
606 if (o && o->type == OBJ_COMMIT)
607 o->flags &= ~SEEN;
610 free(tmp);
613 static int mark_uninteresting(const char *refname UNUSED,
614 const struct object_id *oid,
615 int flags UNUSED,
616 void *cb_data UNUSED)
618 struct commit *commit = lookup_commit_reference_gently(the_repository,
619 oid, 1);
620 if (!commit)
621 return 0;
622 commit->object.flags |= UNINTERESTING;
623 mark_parents_uninteresting(NULL, commit);
624 return 0;
627 static void post_assign_shallow(struct shallow_info *info,
628 struct ref_bitmap *ref_bitmap,
629 int *ref_status);
631 * Step 6(+7), associate shallow commits with new refs
633 * info->ref must be initialized before calling this function.
635 * If used is not NULL, it's an array of info->shallow->nr
636 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
637 * m-th shallow commit from info->shallow.
639 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
640 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
641 * the ref needs some shallow commits from either info->ours or
642 * info->theirs.
644 void assign_shallow_commits_to_refs(struct shallow_info *info,
645 uint32_t **used, int *ref_status)
647 struct object_id *oid = info->shallow->oid;
648 struct oid_array *ref = info->ref;
649 unsigned int i, nr;
650 int *shallow, nr_shallow = 0;
651 struct paint_info pi;
653 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
654 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
655 for (i = 0; i < info->nr_ours; i++)
656 shallow[nr_shallow++] = info->ours[i];
657 for (i = 0; i < info->nr_theirs; i++)
658 shallow[nr_shallow++] = info->theirs[i];
661 * Prepare the commit graph to track what refs can reach what
662 * (new) shallow commits.
664 nr = get_max_object_index();
665 for (i = 0; i < nr; i++) {
666 struct object *o = get_indexed_object(i);
667 if (!o || o->type != OBJ_COMMIT)
668 continue;
670 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
673 memset(&pi, 0, sizeof(pi));
674 init_ref_bitmap(&pi.ref_bitmap);
675 pi.nr_bits = ref->nr;
678 * "--not --all" to cut short the traversal if new refs
679 * connect to old refs. If not (e.g. force ref updates) it'll
680 * have to go down to the current shallow commits.
682 head_ref(mark_uninteresting, NULL);
683 for_each_ref(mark_uninteresting, NULL);
685 /* Mark potential bottoms so we won't go out of bound */
686 for (i = 0; i < nr_shallow; i++) {
687 struct commit *c = lookup_commit(the_repository,
688 &oid[shallow[i]]);
689 c->object.flags |= BOTTOM;
692 for (i = 0; i < ref->nr; i++)
693 paint_down(&pi, ref->oid + i, i);
695 if (used) {
696 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
697 memset(used, 0, sizeof(*used) * info->shallow->nr);
698 for (i = 0; i < nr_shallow; i++) {
699 const struct commit *c = lookup_commit(the_repository,
700 &oid[shallow[i]]);
701 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
702 if (*map)
703 used[shallow[i]] = xmemdupz(*map, bitmap_size);
706 * unreachable shallow commits are not removed from
707 * "ours" and "theirs". The user is supposed to run
708 * step 7 on every ref separately and not trust "ours"
709 * and "theirs" any more.
711 } else
712 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
714 clear_ref_bitmap(&pi.ref_bitmap);
715 for (i = 0; i < pi.pool_count; i++)
716 free(pi.pools[i]);
717 free(pi.pools);
718 free(shallow);
721 struct commit_array {
722 struct commit **commits;
723 int nr, alloc;
726 static int add_ref(const char *refname UNUSED,
727 const struct object_id *oid,
728 int flags UNUSED,
729 void *cb_data)
731 struct commit_array *ca = cb_data;
732 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
733 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
734 oid, 1);
735 if (ca->commits[ca->nr])
736 ca->nr++;
737 return 0;
740 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
742 unsigned int i;
743 if (!ref_status)
744 return;
745 for (i = 0; i < nr; i++)
746 if (bitmap[i / 32] & (1U << (i % 32)))
747 ref_status[i]++;
751 * Step 7, reachability test on "ours" at commit level
753 static void post_assign_shallow(struct shallow_info *info,
754 struct ref_bitmap *ref_bitmap,
755 int *ref_status)
757 struct object_id *oid = info->shallow->oid;
758 struct commit *c;
759 uint32_t **bitmap;
760 int dst, i, j;
761 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
762 struct commit_array ca;
764 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
765 if (ref_status)
766 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
768 /* Remove unreachable shallow commits from "theirs" */
769 for (i = dst = 0; i < info->nr_theirs; i++) {
770 if (i != dst)
771 info->theirs[dst] = info->theirs[i];
772 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
773 bitmap = ref_bitmap_at(ref_bitmap, c);
774 if (!*bitmap)
775 continue;
776 for (j = 0; j < bitmap_nr; j++)
777 if (bitmap[0][j]) {
778 update_refstatus(ref_status, info->ref->nr, *bitmap);
779 dst++;
780 break;
783 info->nr_theirs = dst;
785 memset(&ca, 0, sizeof(ca));
786 head_ref(add_ref, &ca);
787 for_each_ref(add_ref, &ca);
789 /* Remove unreachable shallow commits from "ours" */
790 for (i = dst = 0; i < info->nr_ours; i++) {
791 if (i != dst)
792 info->ours[dst] = info->ours[i];
793 c = lookup_commit(the_repository, &oid[info->ours[i]]);
794 bitmap = ref_bitmap_at(ref_bitmap, c);
795 if (!*bitmap)
796 continue;
797 for (j = 0; j < bitmap_nr; j++)
798 if (bitmap[0][j] &&
799 /* Step 7, reachability test at commit level */
800 !repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits)) {
801 update_refstatus(ref_status, info->ref->nr, *bitmap);
802 dst++;
803 break;
806 info->nr_ours = dst;
808 free(ca.commits);
811 /* (Delayed) step 7, reachability test at commit level */
812 int delayed_reachability_test(struct shallow_info *si, int c)
814 if (si->need_reachability_test[c]) {
815 struct commit *commit = lookup_commit(the_repository,
816 &si->shallow->oid[c]);
818 if (!si->commits) {
819 struct commit_array ca;
821 memset(&ca, 0, sizeof(ca));
822 head_ref(add_ref, &ca);
823 for_each_ref(add_ref, &ca);
824 si->commits = ca.commits;
825 si->nr_commits = ca.nr;
828 si->reachable[c] = repo_in_merge_bases_many(the_repository,
829 commit,
830 si->nr_commits,
831 si->commits);
832 si->need_reachability_test[c] = 0;
834 return si->reachable[c];