fetch: centralize logic to print remote URL
[alt-git.git] / shallow.c
blob17f9bcdb5f38270c4f5910a2e1c930432ba350e5
1 #include "cache.h"
2 #include "repository.h"
3 #include "tempfile.h"
4 #include "lockfile.h"
5 #include "object-store.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "pkt-line.h"
9 #include "remote.h"
10 #include "refs.h"
11 #include "oid-array.h"
12 #include "diff.h"
13 #include "revision.h"
14 #include "commit-slab.h"
15 #include "list-objects.h"
16 #include "commit-reach.h"
17 #include "shallow.h"
19 void set_alternate_shallow_file(struct repository *r, const char *path, int override)
21 if (r->parsed_objects->is_shallow != -1)
22 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
23 if (r->parsed_objects->alternate_shallow_file && !override)
24 return;
25 free(r->parsed_objects->alternate_shallow_file);
26 r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
29 int register_shallow(struct repository *r, const struct object_id *oid)
31 struct commit_graft *graft =
32 xmalloc(sizeof(struct commit_graft));
33 struct commit *commit = lookup_commit(the_repository, oid);
35 oidcpy(&graft->oid, oid);
36 graft->nr_parent = -1;
37 if (commit && commit->object.parsed)
38 commit->parents = NULL;
39 return register_commit_graft(r, graft, 0);
42 int unregister_shallow(const struct object_id *oid)
44 int pos = commit_graft_pos(the_repository, oid);
45 if (pos < 0)
46 return -1;
47 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
48 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
49 the_repository->parsed_objects->grafts + pos + 1,
50 the_repository->parsed_objects->grafts_nr - pos - 1);
51 the_repository->parsed_objects->grafts_nr--;
52 return 0;
55 int is_repository_shallow(struct repository *r)
57 FILE *fp;
58 char buf[1024];
59 const char *path = r->parsed_objects->alternate_shallow_file;
61 if (r->parsed_objects->is_shallow >= 0)
62 return r->parsed_objects->is_shallow;
64 if (!path)
65 path = git_path_shallow(r);
67 * fetch-pack sets '--shallow-file ""' as an indicator that no
68 * shallow file should be used. We could just open it and it
69 * will likely fail. But let's do an explicit check instead.
71 if (!*path || (fp = fopen(path, "r")) == NULL) {
72 stat_validity_clear(r->parsed_objects->shallow_stat);
73 r->parsed_objects->is_shallow = 0;
74 return r->parsed_objects->is_shallow;
76 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
77 r->parsed_objects->is_shallow = 1;
79 while (fgets(buf, sizeof(buf), fp)) {
80 struct object_id oid;
81 if (get_oid_hex(buf, &oid))
82 die("bad shallow line: %s", buf);
83 register_shallow(r, &oid);
85 fclose(fp);
86 return r->parsed_objects->is_shallow;
89 static void reset_repository_shallow(struct repository *r)
91 r->parsed_objects->is_shallow = -1;
92 stat_validity_clear(r->parsed_objects->shallow_stat);
93 reset_commit_grafts(r);
96 int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
98 int res = commit_lock_file(&lk->lock);
99 reset_repository_shallow(r);
102 * Update in-memory data structures with the new shallow information,
103 * including unparsing all commits that now have grafts.
105 is_repository_shallow(r);
107 return res;
110 void rollback_shallow_file(struct repository *r, struct shallow_lock *lk)
112 rollback_lock_file(&lk->lock);
113 reset_repository_shallow(r);
117 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
118 * supports a "valid" flag.
120 define_commit_slab(commit_depth, int *);
121 static void free_depth_in_slab(int **ptr)
123 FREE_AND_NULL(*ptr);
125 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
126 int shallow_flag, int not_shallow_flag)
128 int i = 0, cur_depth = 0;
129 struct commit_list *result = NULL;
130 struct object_array stack = OBJECT_ARRAY_INIT;
131 struct commit *commit = NULL;
132 struct commit_graft *graft;
133 struct commit_depth depths;
135 init_commit_depth(&depths);
136 while (commit || i < heads->nr || stack.nr) {
137 struct commit_list *p;
138 if (!commit) {
139 if (i < heads->nr) {
140 int **depth_slot;
141 commit = (struct commit *)
142 deref_tag(the_repository,
143 heads->objects[i++].item,
144 NULL, 0);
145 if (!commit || commit->object.type != OBJ_COMMIT) {
146 commit = NULL;
147 continue;
149 depth_slot = commit_depth_at(&depths, commit);
150 if (!*depth_slot)
151 *depth_slot = xmalloc(sizeof(int));
152 **depth_slot = 0;
153 cur_depth = 0;
154 } else {
155 commit = (struct commit *)
156 object_array_pop(&stack);
157 cur_depth = **commit_depth_at(&depths, commit);
160 parse_commit_or_die(commit);
161 cur_depth++;
162 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
163 (is_repository_shallow(the_repository) && !commit->parents &&
164 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
165 graft->nr_parent < 0)) {
166 commit_list_insert(commit, &result);
167 commit->object.flags |= shallow_flag;
168 commit = NULL;
169 continue;
171 commit->object.flags |= not_shallow_flag;
172 for (p = commit->parents, commit = NULL; p; p = p->next) {
173 int **depth_slot = commit_depth_at(&depths, p->item);
174 if (!*depth_slot) {
175 *depth_slot = xmalloc(sizeof(int));
176 **depth_slot = cur_depth;
177 } else {
178 if (cur_depth >= **depth_slot)
179 continue;
180 **depth_slot = cur_depth;
182 if (p->next)
183 add_object_array(&p->item->object,
184 NULL, &stack);
185 else {
186 commit = p->item;
187 cur_depth = **commit_depth_at(&depths, commit);
191 deep_clear_commit_depth(&depths, free_depth_in_slab);
193 return result;
196 static void show_commit(struct commit *commit, void *data)
198 commit_list_insert(commit, data);
202 * Given rev-list arguments, run rev-list. All reachable commits
203 * except border ones are marked with not_shallow_flag. Border commits
204 * are marked with shallow_flag. The list of border/shallow commits
205 * are also returned.
207 struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
208 int shallow_flag,
209 int not_shallow_flag)
211 struct commit_list *result = NULL, *p;
212 struct commit_list *not_shallow_list = NULL;
213 struct rev_info revs;
214 int both_flags = shallow_flag | not_shallow_flag;
217 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
218 * set at this point. But better be safe than sorry.
220 clear_object_flags(both_flags);
222 is_repository_shallow(the_repository); /* make sure shallows are read */
224 repo_init_revisions(the_repository, &revs, NULL);
225 save_commit_buffer = 0;
226 setup_revisions(ac, av, &revs, NULL);
228 if (prepare_revision_walk(&revs))
229 die("revision walk setup failed");
230 traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
232 if (!not_shallow_list)
233 die("no commits selected for shallow requests");
235 /* Mark all reachable commits as NOT_SHALLOW */
236 for (p = not_shallow_list; p; p = p->next)
237 p->item->object.flags |= not_shallow_flag;
240 * mark border commits SHALLOW + NOT_SHALLOW.
241 * We cannot clear NOT_SHALLOW right now. Imagine border
242 * commit A is processed first, then commit B, whose parent is
243 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
244 * itself is considered border at step 2, which is incorrect.
246 for (p = not_shallow_list; p; p = p->next) {
247 struct commit *c = p->item;
248 struct commit_list *parent;
250 if (parse_commit(c))
251 die("unable to parse commit %s",
252 oid_to_hex(&c->object.oid));
254 for (parent = c->parents; parent; parent = parent->next)
255 if (!(parent->item->object.flags & not_shallow_flag)) {
256 c->object.flags |= shallow_flag;
257 commit_list_insert(c, &result);
258 break;
261 free_commit_list(not_shallow_list);
264 * Now we can clean up NOT_SHALLOW on border commits. Having
265 * both flags set can confuse the caller.
267 for (p = result; p; p = p->next) {
268 struct object *o = &p->item->object;
269 if ((o->flags & both_flags) == both_flags)
270 o->flags &= ~not_shallow_flag;
272 release_revisions(&revs);
273 return result;
276 static void check_shallow_file_for_update(struct repository *r)
278 if (r->parsed_objects->is_shallow == -1)
279 BUG("shallow must be initialized by now");
281 if (!stat_validity_check(r->parsed_objects->shallow_stat,
282 git_path_shallow(r)))
283 die("shallow file has changed since we read it");
286 #define SEEN_ONLY 1
287 #define VERBOSE 2
288 #define QUICK 4
290 struct write_shallow_data {
291 struct strbuf *out;
292 int use_pack_protocol;
293 int count;
294 unsigned flags;
297 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
299 struct write_shallow_data *data = cb_data;
300 const char *hex = oid_to_hex(&graft->oid);
301 if (graft->nr_parent != -1)
302 return 0;
303 if (data->flags & QUICK) {
304 if (!has_object_file(&graft->oid))
305 return 0;
306 } else if (data->flags & SEEN_ONLY) {
307 struct commit *c = lookup_commit(the_repository, &graft->oid);
308 if (!c || !(c->object.flags & SEEN)) {
309 if (data->flags & VERBOSE)
310 printf("Removing %s from .git/shallow\n",
311 oid_to_hex(&c->object.oid));
312 return 0;
315 data->count++;
316 if (data->use_pack_protocol)
317 packet_buf_write(data->out, "shallow %s", hex);
318 else {
319 strbuf_addstr(data->out, hex);
320 strbuf_addch(data->out, '\n');
322 return 0;
325 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
326 const struct oid_array *extra,
327 unsigned flags)
329 struct write_shallow_data data;
330 int i;
331 data.out = out;
332 data.use_pack_protocol = use_pack_protocol;
333 data.count = 0;
334 data.flags = flags;
335 for_each_commit_graft(write_one_shallow, &data);
336 if (!extra)
337 return data.count;
338 for (i = 0; i < extra->nr; i++) {
339 strbuf_addstr(out, oid_to_hex(extra->oid + i));
340 strbuf_addch(out, '\n');
341 data.count++;
343 return data.count;
346 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
347 const struct oid_array *extra)
349 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
352 const char *setup_temporary_shallow(const struct oid_array *extra)
354 struct tempfile *temp;
355 struct strbuf sb = STRBUF_INIT;
357 if (write_shallow_commits(&sb, 0, extra)) {
358 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
360 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
361 close_tempfile_gently(temp) < 0)
362 die_errno("failed to write to %s",
363 get_tempfile_path(temp));
364 strbuf_release(&sb);
365 return get_tempfile_path(temp);
368 * is_repository_shallow() sees empty string as "no shallow
369 * file".
371 return "";
374 void setup_alternate_shallow(struct shallow_lock *shallow_lock,
375 const char **alternate_shallow_file,
376 const struct oid_array *extra)
378 struct strbuf sb = STRBUF_INIT;
379 int fd;
381 fd = hold_lock_file_for_update(&shallow_lock->lock,
382 git_path_shallow(the_repository),
383 LOCK_DIE_ON_ERROR);
384 check_shallow_file_for_update(the_repository);
385 if (write_shallow_commits(&sb, 0, extra)) {
386 if (write_in_full(fd, sb.buf, sb.len) < 0)
387 die_errno("failed to write to %s",
388 get_lock_file_path(&shallow_lock->lock));
389 *alternate_shallow_file = get_lock_file_path(&shallow_lock->lock);
390 } else
392 * is_repository_shallow() sees empty string as "no
393 * shallow file".
395 *alternate_shallow_file = "";
396 strbuf_release(&sb);
399 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
401 int fd = *(int *)cb;
402 if (graft->nr_parent == -1)
403 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
404 return 0;
407 void advertise_shallow_grafts(int fd)
409 if (!is_repository_shallow(the_repository))
410 return;
411 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
415 * mark_reachable_objects() should have been run prior to this and all
416 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
417 * in which case lines are excised from the shallow file if they refer to
418 * commits that do not exist (any longer).
420 void prune_shallow(unsigned options)
422 struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
423 struct strbuf sb = STRBUF_INIT;
424 unsigned flags = SEEN_ONLY;
425 int fd;
427 if (options & PRUNE_QUICK)
428 flags |= QUICK;
430 if (options & PRUNE_SHOW_ONLY) {
431 flags |= VERBOSE;
432 write_shallow_commits_1(&sb, 0, NULL, flags);
433 strbuf_release(&sb);
434 return;
436 fd = hold_lock_file_for_update(&shallow_lock.lock,
437 git_path_shallow(the_repository),
438 LOCK_DIE_ON_ERROR);
439 check_shallow_file_for_update(the_repository);
440 if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
441 if (write_in_full(fd, sb.buf, sb.len) < 0)
442 die_errno("failed to write to %s",
443 get_lock_file_path(&shallow_lock.lock));
444 commit_shallow_file(the_repository, &shallow_lock);
445 } else {
446 unlink(git_path_shallow(the_repository));
447 rollback_shallow_file(the_repository, &shallow_lock);
449 strbuf_release(&sb);
452 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
455 * Step 1, split sender shallow commits into "ours" and "theirs"
456 * Step 2, clean "ours" based on .git/shallow
458 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
460 int i;
461 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
462 memset(info, 0, sizeof(*info));
463 info->shallow = sa;
464 if (!sa)
465 return;
466 ALLOC_ARRAY(info->ours, sa->nr);
467 ALLOC_ARRAY(info->theirs, sa->nr);
468 for (i = 0; i < sa->nr; i++) {
469 if (has_object_file(sa->oid + i)) {
470 struct commit_graft *graft;
471 graft = lookup_commit_graft(the_repository,
472 &sa->oid[i]);
473 if (graft && graft->nr_parent < 0)
474 continue;
475 info->ours[info->nr_ours++] = i;
476 } else
477 info->theirs[info->nr_theirs++] = i;
481 void clear_shallow_info(struct shallow_info *info)
483 free(info->ours);
484 free(info->theirs);
487 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
489 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
491 struct object_id *oid = info->shallow->oid;
492 int i, dst;
493 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
494 for (i = dst = 0; i < info->nr_theirs; i++) {
495 if (i != dst)
496 info->theirs[dst] = info->theirs[i];
497 if (has_object_file(oid + info->theirs[i]))
498 dst++;
500 info->nr_theirs = dst;
503 define_commit_slab(ref_bitmap, uint32_t *);
505 #define POOL_SIZE (512 * 1024)
507 struct paint_info {
508 struct ref_bitmap ref_bitmap;
509 unsigned nr_bits;
510 char **pools;
511 char *free, *end;
512 unsigned pool_count;
515 static uint32_t *paint_alloc(struct paint_info *info)
517 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
518 unsigned size = nr * sizeof(uint32_t);
519 void *p;
520 if (!info->pool_count || size > info->end - info->free) {
521 if (size > POOL_SIZE)
522 BUG("pool size too small for %d in paint_alloc()",
523 size);
524 info->pool_count++;
525 REALLOC_ARRAY(info->pools, info->pool_count);
526 info->free = xmalloc(POOL_SIZE);
527 info->pools[info->pool_count - 1] = info->free;
528 info->end = info->free + POOL_SIZE;
530 p = info->free;
531 info->free += size;
532 return p;
536 * Given a commit SHA-1, walk down to parents until either SEEN,
537 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
538 * all walked commits.
540 static void paint_down(struct paint_info *info, const struct object_id *oid,
541 unsigned int id)
543 unsigned int i, nr;
544 struct commit_list *head = NULL;
545 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
546 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
547 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
549 uint32_t *tmp; /* to be freed before return */
550 uint32_t *bitmap;
552 if (!c)
553 return;
555 tmp = xmalloc(bitmap_size);
556 bitmap = paint_alloc(info);
557 memset(bitmap, 0, bitmap_size);
558 bitmap[id / 32] |= (1U << (id % 32));
559 commit_list_insert(c, &head);
560 while (head) {
561 struct commit_list *p;
562 struct commit *c = pop_commit(&head);
563 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
565 /* XXX check "UNINTERESTING" from pack bitmaps if available */
566 if (c->object.flags & (SEEN | UNINTERESTING))
567 continue;
568 else
569 c->object.flags |= SEEN;
571 if (!*refs)
572 *refs = bitmap;
573 else {
574 memcpy(tmp, *refs, bitmap_size);
575 for (i = 0; i < bitmap_nr; i++)
576 tmp[i] |= bitmap[i];
577 if (memcmp(tmp, *refs, bitmap_size)) {
578 *refs = paint_alloc(info);
579 memcpy(*refs, tmp, bitmap_size);
583 if (c->object.flags & BOTTOM)
584 continue;
586 if (parse_commit(c))
587 die("unable to parse commit %s",
588 oid_to_hex(&c->object.oid));
590 for (p = c->parents; p; p = p->next) {
591 if (p->item->object.flags & SEEN)
592 continue;
593 commit_list_insert(p->item, &head);
597 nr = get_max_object_index();
598 for (i = 0; i < nr; i++) {
599 struct object *o = get_indexed_object(i);
600 if (o && o->type == OBJ_COMMIT)
601 o->flags &= ~SEEN;
604 free(tmp);
607 static int mark_uninteresting(const char *refname UNUSED,
608 const struct object_id *oid,
609 int flags UNUSED,
610 void *cb_data UNUSED)
612 struct commit *commit = lookup_commit_reference_gently(the_repository,
613 oid, 1);
614 if (!commit)
615 return 0;
616 commit->object.flags |= UNINTERESTING;
617 mark_parents_uninteresting(NULL, commit);
618 return 0;
621 static void post_assign_shallow(struct shallow_info *info,
622 struct ref_bitmap *ref_bitmap,
623 int *ref_status);
625 * Step 6(+7), associate shallow commits with new refs
627 * info->ref must be initialized before calling this function.
629 * If used is not NULL, it's an array of info->shallow->nr
630 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
631 * m-th shallow commit from info->shallow.
633 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
634 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
635 * the ref needs some shallow commits from either info->ours or
636 * info->theirs.
638 void assign_shallow_commits_to_refs(struct shallow_info *info,
639 uint32_t **used, int *ref_status)
641 struct object_id *oid = info->shallow->oid;
642 struct oid_array *ref = info->ref;
643 unsigned int i, nr;
644 int *shallow, nr_shallow = 0;
645 struct paint_info pi;
647 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
648 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
649 for (i = 0; i < info->nr_ours; i++)
650 shallow[nr_shallow++] = info->ours[i];
651 for (i = 0; i < info->nr_theirs; i++)
652 shallow[nr_shallow++] = info->theirs[i];
655 * Prepare the commit graph to track what refs can reach what
656 * (new) shallow commits.
658 nr = get_max_object_index();
659 for (i = 0; i < nr; i++) {
660 struct object *o = get_indexed_object(i);
661 if (!o || o->type != OBJ_COMMIT)
662 continue;
664 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
667 memset(&pi, 0, sizeof(pi));
668 init_ref_bitmap(&pi.ref_bitmap);
669 pi.nr_bits = ref->nr;
672 * "--not --all" to cut short the traversal if new refs
673 * connect to old refs. If not (e.g. force ref updates) it'll
674 * have to go down to the current shallow commits.
676 head_ref(mark_uninteresting, NULL);
677 for_each_ref(mark_uninteresting, NULL);
679 /* Mark potential bottoms so we won't go out of bound */
680 for (i = 0; i < nr_shallow; i++) {
681 struct commit *c = lookup_commit(the_repository,
682 &oid[shallow[i]]);
683 c->object.flags |= BOTTOM;
686 for (i = 0; i < ref->nr; i++)
687 paint_down(&pi, ref->oid + i, i);
689 if (used) {
690 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
691 memset(used, 0, sizeof(*used) * info->shallow->nr);
692 for (i = 0; i < nr_shallow; i++) {
693 const struct commit *c = lookup_commit(the_repository,
694 &oid[shallow[i]]);
695 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
696 if (*map)
697 used[shallow[i]] = xmemdupz(*map, bitmap_size);
700 * unreachable shallow commits are not removed from
701 * "ours" and "theirs". The user is supposed to run
702 * step 7 on every ref separately and not trust "ours"
703 * and "theirs" any more.
705 } else
706 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
708 clear_ref_bitmap(&pi.ref_bitmap);
709 for (i = 0; i < pi.pool_count; i++)
710 free(pi.pools[i]);
711 free(pi.pools);
712 free(shallow);
715 struct commit_array {
716 struct commit **commits;
717 int nr, alloc;
720 static int add_ref(const char *refname UNUSED,
721 const struct object_id *oid,
722 int flags UNUSED,
723 void *cb_data)
725 struct commit_array *ca = cb_data;
726 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
727 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
728 oid, 1);
729 if (ca->commits[ca->nr])
730 ca->nr++;
731 return 0;
734 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
736 unsigned int i;
737 if (!ref_status)
738 return;
739 for (i = 0; i < nr; i++)
740 if (bitmap[i / 32] & (1U << (i % 32)))
741 ref_status[i]++;
745 * Step 7, reachability test on "ours" at commit level
747 static void post_assign_shallow(struct shallow_info *info,
748 struct ref_bitmap *ref_bitmap,
749 int *ref_status)
751 struct object_id *oid = info->shallow->oid;
752 struct commit *c;
753 uint32_t **bitmap;
754 int dst, i, j;
755 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
756 struct commit_array ca;
758 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
759 if (ref_status)
760 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
762 /* Remove unreachable shallow commits from "theirs" */
763 for (i = dst = 0; i < info->nr_theirs; i++) {
764 if (i != dst)
765 info->theirs[dst] = info->theirs[i];
766 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
767 bitmap = ref_bitmap_at(ref_bitmap, c);
768 if (!*bitmap)
769 continue;
770 for (j = 0; j < bitmap_nr; j++)
771 if (bitmap[0][j]) {
772 update_refstatus(ref_status, info->ref->nr, *bitmap);
773 dst++;
774 break;
777 info->nr_theirs = dst;
779 memset(&ca, 0, sizeof(ca));
780 head_ref(add_ref, &ca);
781 for_each_ref(add_ref, &ca);
783 /* Remove unreachable shallow commits from "ours" */
784 for (i = dst = 0; i < info->nr_ours; i++) {
785 if (i != dst)
786 info->ours[dst] = info->ours[i];
787 c = lookup_commit(the_repository, &oid[info->ours[i]]);
788 bitmap = ref_bitmap_at(ref_bitmap, c);
789 if (!*bitmap)
790 continue;
791 for (j = 0; j < bitmap_nr; j++)
792 if (bitmap[0][j] &&
793 /* Step 7, reachability test at commit level */
794 !in_merge_bases_many(c, ca.nr, ca.commits)) {
795 update_refstatus(ref_status, info->ref->nr, *bitmap);
796 dst++;
797 break;
800 info->nr_ours = dst;
802 free(ca.commits);
805 /* (Delayed) step 7, reachability test at commit level */
806 int delayed_reachability_test(struct shallow_info *si, int c)
808 if (si->need_reachability_test[c]) {
809 struct commit *commit = lookup_commit(the_repository,
810 &si->shallow->oid[c]);
812 if (!si->commits) {
813 struct commit_array ca;
815 memset(&ca, 0, sizeof(ca));
816 head_ref(add_ref, &ca);
817 for_each_ref(add_ref, &ca);
818 si->commits = ca.commits;
819 si->nr_commits = ca.nr;
822 si->reachable[c] = in_merge_bases_many(commit,
823 si->nr_commits,
824 si->commits);
825 si->need_reachability_test[c] = 0;
827 return si->reachable[c];