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 "sha1-array.h"
12#include "diff.h"
13#include "revision.h"
14#include "commit-slab.h"
15#include "revision.h"
16#include "list-objects.h"
17#include "commit-slab.h"
18#include "repository.h"
19
20void set_alternate_shallow_file(struct repository *r, const char *path, int override)
21{
22 if (r->parsed_objects->is_shallow != -1)
23 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
24 if (r->parsed_objects->alternate_shallow_file && !override)
25 return;
26 free(r->parsed_objects->alternate_shallow_file);
27 r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
28}
29
30int register_shallow(struct repository *r, const struct object_id *oid)
31{
32 struct commit_graft *graft =
33 xmalloc(sizeof(struct commit_graft));
34 struct commit *commit = lookup_commit(oid);
35
36 oidcpy(&graft->oid, oid);
37 graft->nr_parent = -1;
38 if (commit && commit->object.parsed)
39 commit->parents = NULL;
40 return register_commit_graft(r, graft, 0);
41}
42
43int is_repository_shallow(struct repository *r)
44{
45 FILE *fp;
46 char buf[1024];
47 const char *path = r->parsed_objects->alternate_shallow_file;
48
49 if (r->parsed_objects->is_shallow >= 0)
50 return r->parsed_objects->is_shallow;
51
52 if (!path)
53 path = git_path_shallow(r);
54 /*
55 * fetch-pack sets '--shallow-file ""' as an indicator that no
56 * shallow file should be used. We could just open it and it
57 * will likely fail. But let's do an explicit check instead.
58 */
59 if (!*path || (fp = fopen(path, "r")) == NULL) {
60 stat_validity_clear(r->parsed_objects->shallow_stat);
61 r->parsed_objects->is_shallow = 0;
62 return r->parsed_objects->is_shallow;
63 }
64 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
65 r->parsed_objects->is_shallow = 1;
66
67 while (fgets(buf, sizeof(buf), fp)) {
68 struct object_id oid;
69 if (get_oid_hex(buf, &oid))
70 die("bad shallow line: %s", buf);
71 register_shallow(r, &oid);
72 }
73 fclose(fp);
74 return r->parsed_objects->is_shallow;
75}
76
77/*
78 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
79 * supports a "valid" flag.
80 */
81define_commit_slab(commit_depth, int *);
82struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
83 int shallow_flag, int not_shallow_flag)
84{
85 int i = 0, cur_depth = 0;
86 struct commit_list *result = NULL;
87 struct object_array stack = OBJECT_ARRAY_INIT;
88 struct commit *commit = NULL;
89 struct commit_graft *graft;
90 struct commit_depth depths;
91
92 init_commit_depth(&depths);
93 while (commit || i < heads->nr || stack.nr) {
94 struct commit_list *p;
95 if (!commit) {
96 if (i < heads->nr) {
97 int **depth_slot;
98 commit = (struct commit *)
99 deref_tag(heads->objects[i++].item, NULL, 0);
100 if (!commit || commit->object.type != OBJ_COMMIT) {
101 commit = NULL;
102 continue;
103 }
104 depth_slot = commit_depth_at(&depths, commit);
105 if (!*depth_slot)
106 *depth_slot = xmalloc(sizeof(int));
107 **depth_slot = 0;
108 cur_depth = 0;
109 } else {
110 commit = (struct commit *)
111 object_array_pop(&stack);
112 cur_depth = **commit_depth_at(&depths, commit);
113 }
114 }
115 parse_commit_or_die(commit);
116 cur_depth++;
117 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
118 (is_repository_shallow(the_repository) && !commit->parents &&
119 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
120 graft->nr_parent < 0)) {
121 commit_list_insert(commit, &result);
122 commit->object.flags |= shallow_flag;
123 commit = NULL;
124 continue;
125 }
126 commit->object.flags |= not_shallow_flag;
127 for (p = commit->parents, commit = NULL; p; p = p->next) {
128 int **depth_slot = commit_depth_at(&depths, p->item);
129 if (!*depth_slot) {
130 *depth_slot = xmalloc(sizeof(int));
131 **depth_slot = cur_depth;
132 } else {
133 if (cur_depth >= **depth_slot)
134 continue;
135 **depth_slot = cur_depth;
136 }
137 if (p->next)
138 add_object_array(&p->item->object,
139 NULL, &stack);
140 else {
141 commit = p->item;
142 cur_depth = **commit_depth_at(&depths, commit);
143 }
144 }
145 }
146 for (i = 0; i < depths.slab_count; i++) {
147 int j;
148
149 for (j = 0; j < depths.slab_size; j++)
150 free(depths.slab[i][j]);
151 }
152 clear_commit_depth(&depths);
153
154 return result;
155}
156
157static void show_commit(struct commit *commit, void *data)
158{
159 commit_list_insert(commit, data);
160}
161
162/*
163 * Given rev-list arguments, run rev-list. All reachable commits
164 * except border ones are marked with not_shallow_flag. Border commits
165 * are marked with shallow_flag. The list of border/shallow commits
166 * are also returned.
167 */
168struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
169 int shallow_flag,
170 int not_shallow_flag)
171{
172 struct commit_list *result = NULL, *p;
173 struct commit_list *not_shallow_list = NULL;
174 struct rev_info revs;
175 int both_flags = shallow_flag | not_shallow_flag;
176
177 /*
178 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
179 * set at this point. But better be safe than sorry.
180 */
181 clear_object_flags(both_flags);
182
183 is_repository_shallow(the_repository); /* make sure shallows are read */
184
185 init_revisions(&revs, NULL);
186 save_commit_buffer = 0;
187 setup_revisions(ac, av, &revs, NULL);
188
189 if (prepare_revision_walk(&revs))
190 die("revision walk setup failed");
191 traverse_commit_list(&revs, show_commit, NULL, ¬_shallow_list);
192
193 if (!not_shallow_list)
194 die("no commits selected for shallow requests");
195
196 /* Mark all reachable commits as NOT_SHALLOW */
197 for (p = not_shallow_list; p; p = p->next)
198 p->item->object.flags |= not_shallow_flag;
199
200 /*
201 * mark border commits SHALLOW + NOT_SHALLOW.
202 * We cannot clear NOT_SHALLOW right now. Imagine border
203 * commit A is processed first, then commit B, whose parent is
204 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
205 * itself is considered border at step 2, which is incorrect.
206 */
207 for (p = not_shallow_list; p; p = p->next) {
208 struct commit *c = p->item;
209 struct commit_list *parent;
210
211 if (parse_commit(c))
212 die("unable to parse commit %s",
213 oid_to_hex(&c->object.oid));
214
215 for (parent = c->parents; parent; parent = parent->next)
216 if (!(parent->item->object.flags & not_shallow_flag)) {
217 c->object.flags |= shallow_flag;
218 commit_list_insert(c, &result);
219 break;
220 }
221 }
222 free_commit_list(not_shallow_list);
223
224 /*
225 * Now we can clean up NOT_SHALLOW on border commits. Having
226 * both flags set can confuse the caller.
227 */
228 for (p = result; p; p = p->next) {
229 struct object *o = &p->item->object;
230 if ((o->flags & both_flags) == both_flags)
231 o->flags &= ~not_shallow_flag;
232 }
233 return result;
234}
235
236static void check_shallow_file_for_update(struct repository *r)
237{
238 if (r->parsed_objects->is_shallow == -1)
239 BUG("shallow must be initialized by now");
240
241 if (!stat_validity_check(r->parsed_objects->shallow_stat, git_path_shallow(the_repository)))
242 die("shallow file has changed since we read it");
243}
244
245#define SEEN_ONLY 1
246#define VERBOSE 2
247
248struct write_shallow_data {
249 struct strbuf *out;
250 int use_pack_protocol;
251 int count;
252 unsigned flags;
253};
254
255static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
256{
257 struct write_shallow_data *data = cb_data;
258 const char *hex = oid_to_hex(&graft->oid);
259 if (graft->nr_parent != -1)
260 return 0;
261 if (data->flags & SEEN_ONLY) {
262 struct commit *c = lookup_commit(&graft->oid);
263 if (!c || !(c->object.flags & SEEN)) {
264 if (data->flags & VERBOSE)
265 printf("Removing %s from .git/shallow\n",
266 oid_to_hex(&c->object.oid));
267 return 0;
268 }
269 }
270 data->count++;
271 if (data->use_pack_protocol)
272 packet_buf_write(data->out, "shallow %s", hex);
273 else {
274 strbuf_addstr(data->out, hex);
275 strbuf_addch(data->out, '\n');
276 }
277 return 0;
278}
279
280static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
281 const struct oid_array *extra,
282 unsigned flags)
283{
284 struct write_shallow_data data;
285 int i;
286 data.out = out;
287 data.use_pack_protocol = use_pack_protocol;
288 data.count = 0;
289 data.flags = flags;
290 for_each_commit_graft(write_one_shallow, &data);
291 if (!extra)
292 return data.count;
293 for (i = 0; i < extra->nr; i++) {
294 strbuf_addstr(out, oid_to_hex(extra->oid + i));
295 strbuf_addch(out, '\n');
296 data.count++;
297 }
298 return data.count;
299}
300
301int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
302 const struct oid_array *extra)
303{
304 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
305}
306
307const char *setup_temporary_shallow(const struct oid_array *extra)
308{
309 struct tempfile *temp;
310 struct strbuf sb = STRBUF_INIT;
311
312 if (write_shallow_commits(&sb, 0, extra)) {
313 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
314
315 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
316 close_tempfile_gently(temp) < 0)
317 die_errno("failed to write to %s",
318 get_tempfile_path(temp));
319 strbuf_release(&sb);
320 return get_tempfile_path(temp);
321 }
322 /*
323 * is_repository_shallow() sees empty string as "no shallow
324 * file".
325 */
326 return "";
327}
328
329void setup_alternate_shallow(struct lock_file *shallow_lock,
330 const char **alternate_shallow_file,
331 const struct oid_array *extra)
332{
333 struct strbuf sb = STRBUF_INIT;
334 int fd;
335
336 fd = hold_lock_file_for_update(shallow_lock,
337 git_path_shallow(the_repository),
338 LOCK_DIE_ON_ERROR);
339 check_shallow_file_for_update(the_repository);
340 if (write_shallow_commits(&sb, 0, extra)) {
341 if (write_in_full(fd, sb.buf, sb.len) < 0)
342 die_errno("failed to write to %s",
343 get_lock_file_path(shallow_lock));
344 *alternate_shallow_file = get_lock_file_path(shallow_lock);
345 } else
346 /*
347 * is_repository_shallow() sees empty string as "no
348 * shallow file".
349 */
350 *alternate_shallow_file = "";
351 strbuf_release(&sb);
352}
353
354static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
355{
356 int fd = *(int *)cb;
357 if (graft->nr_parent == -1)
358 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
359 return 0;
360}
361
362void advertise_shallow_grafts(int fd)
363{
364 if (!is_repository_shallow(the_repository))
365 return;
366 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
367}
368
369/*
370 * mark_reachable_objects() should have been run prior to this and all
371 * reachable commits marked as "SEEN".
372 */
373void prune_shallow(int show_only)
374{
375 struct lock_file shallow_lock = LOCK_INIT;
376 struct strbuf sb = STRBUF_INIT;
377 int fd;
378
379 if (show_only) {
380 write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
381 strbuf_release(&sb);
382 return;
383 }
384 fd = hold_lock_file_for_update(&shallow_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_1(&sb, 0, NULL, SEEN_ONLY)) {
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));
392 commit_lock_file(&shallow_lock);
393 } else {
394 unlink(git_path_shallow(the_repository));
395 rollback_lock_file(&shallow_lock);
396 }
397 strbuf_release(&sb);
398}
399
400struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
401
402/*
403 * Step 1, split sender shallow commits into "ours" and "theirs"
404 * Step 2, clean "ours" based on .git/shallow
405 */
406void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
407{
408 int i;
409 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
410 memset(info, 0, sizeof(*info));
411 info->shallow = sa;
412 if (!sa)
413 return;
414 ALLOC_ARRAY(info->ours, sa->nr);
415 ALLOC_ARRAY(info->theirs, sa->nr);
416 for (i = 0; i < sa->nr; i++) {
417 if (has_object_file(sa->oid + i)) {
418 struct commit_graft *graft;
419 graft = lookup_commit_graft(the_repository,
420 &sa->oid[i]);
421 if (graft && graft->nr_parent < 0)
422 continue;
423 info->ours[info->nr_ours++] = i;
424 } else
425 info->theirs[info->nr_theirs++] = i;
426 }
427}
428
429void clear_shallow_info(struct shallow_info *info)
430{
431 free(info->ours);
432 free(info->theirs);
433}
434
435/* Step 4, remove non-existent ones in "theirs" after getting the pack */
436
437void remove_nonexistent_theirs_shallow(struct shallow_info *info)
438{
439 struct object_id *oid = info->shallow->oid;
440 int i, dst;
441 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
442 for (i = dst = 0; i < info->nr_theirs; i++) {
443 if (i != dst)
444 info->theirs[dst] = info->theirs[i];
445 if (has_object_file(oid + info->theirs[i]))
446 dst++;
447 }
448 info->nr_theirs = dst;
449}
450
451define_commit_slab(ref_bitmap, uint32_t *);
452
453#define POOL_SIZE (512 * 1024)
454
455struct paint_info {
456 struct ref_bitmap ref_bitmap;
457 unsigned nr_bits;
458 char **pools;
459 char *free, *end;
460 unsigned pool_count;
461};
462
463static uint32_t *paint_alloc(struct paint_info *info)
464{
465 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
466 unsigned size = nr * sizeof(uint32_t);
467 void *p;
468 if (!info->pool_count || size > info->end - info->free) {
469 if (size > POOL_SIZE)
470 BUG("pool size too small for %d in paint_alloc()",
471 size);
472 info->pool_count++;
473 REALLOC_ARRAY(info->pools, info->pool_count);
474 info->free = xmalloc(POOL_SIZE);
475 info->pools[info->pool_count - 1] = info->free;
476 info->end = info->free + POOL_SIZE;
477 }
478 p = info->free;
479 info->free += size;
480 return p;
481}
482
483/*
484 * Given a commit SHA-1, walk down to parents until either SEEN,
485 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
486 * all walked commits.
487 */
488static void paint_down(struct paint_info *info, const struct object_id *oid,
489 unsigned int id)
490{
491 unsigned int i, nr;
492 struct commit_list *head = NULL;
493 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
494 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
495 struct commit *c = lookup_commit_reference_gently(oid, 1);
496 uint32_t *tmp; /* to be freed before return */
497 uint32_t *bitmap;
498
499 if (!c)
500 return;
501
502 tmp = xmalloc(bitmap_size);
503 bitmap = paint_alloc(info);
504 memset(bitmap, 0, bitmap_size);
505 bitmap[id / 32] |= (1U << (id % 32));
506 commit_list_insert(c, &head);
507 while (head) {
508 struct commit_list *p;
509 struct commit *c = pop_commit(&head);
510 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
511
512 /* XXX check "UNINTERESTING" from pack bitmaps if available */
513 if (c->object.flags & (SEEN | UNINTERESTING))
514 continue;
515 else
516 c->object.flags |= SEEN;
517
518 if (*refs == NULL)
519 *refs = bitmap;
520 else {
521 memcpy(tmp, *refs, bitmap_size);
522 for (i = 0; i < bitmap_nr; i++)
523 tmp[i] |= bitmap[i];
524 if (memcmp(tmp, *refs, bitmap_size)) {
525 *refs = paint_alloc(info);
526 memcpy(*refs, tmp, bitmap_size);
527 }
528 }
529
530 if (c->object.flags & BOTTOM)
531 continue;
532
533 if (parse_commit(c))
534 die("unable to parse commit %s",
535 oid_to_hex(&c->object.oid));
536
537 for (p = c->parents; p; p = p->next) {
538 if (p->item->object.flags & SEEN)
539 continue;
540 commit_list_insert(p->item, &head);
541 }
542 }
543
544 nr = get_max_object_index();
545 for (i = 0; i < nr; i++) {
546 struct object *o = get_indexed_object(i);
547 if (o && o->type == OBJ_COMMIT)
548 o->flags &= ~SEEN;
549 }
550
551 free(tmp);
552}
553
554static int mark_uninteresting(const char *refname, const struct object_id *oid,
555 int flags, void *cb_data)
556{
557 struct commit *commit = lookup_commit_reference_gently(oid, 1);
558 if (!commit)
559 return 0;
560 commit->object.flags |= UNINTERESTING;
561 mark_parents_uninteresting(commit);
562 return 0;
563}
564
565static void post_assign_shallow(struct shallow_info *info,
566 struct ref_bitmap *ref_bitmap,
567 int *ref_status);
568/*
569 * Step 6(+7), associate shallow commits with new refs
570 *
571 * info->ref must be initialized before calling this function.
572 *
573 * If used is not NULL, it's an array of info->shallow->nr
574 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
575 * m-th shallow commit from info->shallow.
576 *
577 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
578 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
579 * the ref needs some shallow commits from either info->ours or
580 * info->theirs.
581 */
582void assign_shallow_commits_to_refs(struct shallow_info *info,
583 uint32_t **used, int *ref_status)
584{
585 struct object_id *oid = info->shallow->oid;
586 struct oid_array *ref = info->ref;
587 unsigned int i, nr;
588 int *shallow, nr_shallow = 0;
589 struct paint_info pi;
590
591 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
592 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
593 for (i = 0; i < info->nr_ours; i++)
594 shallow[nr_shallow++] = info->ours[i];
595 for (i = 0; i < info->nr_theirs; i++)
596 shallow[nr_shallow++] = info->theirs[i];
597
598 /*
599 * Prepare the commit graph to track what refs can reach what
600 * (new) shallow commits.
601 */
602 nr = get_max_object_index();
603 for (i = 0; i < nr; i++) {
604 struct object *o = get_indexed_object(i);
605 if (!o || o->type != OBJ_COMMIT)
606 continue;
607
608 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
609 }
610
611 memset(&pi, 0, sizeof(pi));
612 init_ref_bitmap(&pi.ref_bitmap);
613 pi.nr_bits = ref->nr;
614
615 /*
616 * "--not --all" to cut short the traversal if new refs
617 * connect to old refs. If not (e.g. force ref updates) it'll
618 * have to go down to the current shallow commits.
619 */
620 head_ref(mark_uninteresting, NULL);
621 for_each_ref(mark_uninteresting, NULL);
622
623 /* Mark potential bottoms so we won't go out of bound */
624 for (i = 0; i < nr_shallow; i++) {
625 struct commit *c = lookup_commit(&oid[shallow[i]]);
626 c->object.flags |= BOTTOM;
627 }
628
629 for (i = 0; i < ref->nr; i++)
630 paint_down(&pi, ref->oid + i, i);
631
632 if (used) {
633 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
634 memset(used, 0, sizeof(*used) * info->shallow->nr);
635 for (i = 0; i < nr_shallow; i++) {
636 const struct commit *c = lookup_commit(&oid[shallow[i]]);
637 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
638 if (*map)
639 used[shallow[i]] = xmemdupz(*map, bitmap_size);
640 }
641 /*
642 * unreachable shallow commits are not removed from
643 * "ours" and "theirs". The user is supposed to run
644 * step 7 on every ref separately and not trust "ours"
645 * and "theirs" any more.
646 */
647 } else
648 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
649
650 clear_ref_bitmap(&pi.ref_bitmap);
651 for (i = 0; i < pi.pool_count; i++)
652 free(pi.pools[i]);
653 free(pi.pools);
654 free(shallow);
655}
656
657struct commit_array {
658 struct commit **commits;
659 int nr, alloc;
660};
661
662static int add_ref(const char *refname, const struct object_id *oid,
663 int flags, void *cb_data)
664{
665 struct commit_array *ca = cb_data;
666 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
667 ca->commits[ca->nr] = lookup_commit_reference_gently(oid, 1);
668 if (ca->commits[ca->nr])
669 ca->nr++;
670 return 0;
671}
672
673static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
674{
675 unsigned int i;
676 if (!ref_status)
677 return;
678 for (i = 0; i < nr; i++)
679 if (bitmap[i / 32] & (1U << (i % 32)))
680 ref_status[i]++;
681}
682
683/*
684 * Step 7, reachability test on "ours" at commit level
685 */
686static void post_assign_shallow(struct shallow_info *info,
687 struct ref_bitmap *ref_bitmap,
688 int *ref_status)
689{
690 struct object_id *oid = info->shallow->oid;
691 struct commit *c;
692 uint32_t **bitmap;
693 int dst, i, j;
694 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
695 struct commit_array ca;
696
697 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
698 if (ref_status)
699 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
700
701 /* Remove unreachable shallow commits from "theirs" */
702 for (i = dst = 0; i < info->nr_theirs; i++) {
703 if (i != dst)
704 info->theirs[dst] = info->theirs[i];
705 c = lookup_commit(&oid[info->theirs[i]]);
706 bitmap = ref_bitmap_at(ref_bitmap, c);
707 if (!*bitmap)
708 continue;
709 for (j = 0; j < bitmap_nr; j++)
710 if (bitmap[0][j]) {
711 update_refstatus(ref_status, info->ref->nr, *bitmap);
712 dst++;
713 break;
714 }
715 }
716 info->nr_theirs = dst;
717
718 memset(&ca, 0, sizeof(ca));
719 head_ref(add_ref, &ca);
720 for_each_ref(add_ref, &ca);
721
722 /* Remove unreachable shallow commits from "ours" */
723 for (i = dst = 0; i < info->nr_ours; i++) {
724 if (i != dst)
725 info->ours[dst] = info->ours[i];
726 c = lookup_commit(&oid[info->ours[i]]);
727 bitmap = ref_bitmap_at(ref_bitmap, c);
728 if (!*bitmap)
729 continue;
730 for (j = 0; j < bitmap_nr; j++)
731 if (bitmap[0][j] &&
732 /* Step 7, reachability test at commit level */
733 !in_merge_bases_many(c, ca.nr, ca.commits)) {
734 update_refstatus(ref_status, info->ref->nr, *bitmap);
735 dst++;
736 break;
737 }
738 }
739 info->nr_ours = dst;
740
741 free(ca.commits);
742}
743
744/* (Delayed) step 7, reachability test at commit level */
745int delayed_reachability_test(struct shallow_info *si, int c)
746{
747 if (si->need_reachability_test[c]) {
748 struct commit *commit = lookup_commit(&si->shallow->oid[c]);
749
750 if (!si->commits) {
751 struct commit_array ca;
752
753 memset(&ca, 0, sizeof(ca));
754 head_ref(add_ref, &ca);
755 for_each_ref(add_ref, &ca);
756 si->commits = ca.commits;
757 si->nr_commits = ca.nr;
758 }
759
760 si->reachable[c] = in_merge_bases_many(commit,
761 si->nr_commits,
762 si->commits);
763 si->need_reachability_test[c] = 0;
764 }
765 return si->reachable[c];
766}