l10n: es: update translation
[git/debian.git] / shallow.c
blob8cb768ee5f886fdafcf16221e5878b8fc080f253
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, const struct object_id *oid,
608 int flags, void *cb_data)
610 struct commit *commit = lookup_commit_reference_gently(the_repository,
611 oid, 1);
612 if (!commit)
613 return 0;
614 commit->object.flags |= UNINTERESTING;
615 mark_parents_uninteresting(NULL, commit);
616 return 0;
619 static void post_assign_shallow(struct shallow_info *info,
620 struct ref_bitmap *ref_bitmap,
621 int *ref_status);
623 * Step 6(+7), associate shallow commits with new refs
625 * info->ref must be initialized before calling this function.
627 * If used is not NULL, it's an array of info->shallow->nr
628 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
629 * m-th shallow commit from info->shallow.
631 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
632 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
633 * the ref needs some shallow commits from either info->ours or
634 * info->theirs.
636 void assign_shallow_commits_to_refs(struct shallow_info *info,
637 uint32_t **used, int *ref_status)
639 struct object_id *oid = info->shallow->oid;
640 struct oid_array *ref = info->ref;
641 unsigned int i, nr;
642 int *shallow, nr_shallow = 0;
643 struct paint_info pi;
645 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
646 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
647 for (i = 0; i < info->nr_ours; i++)
648 shallow[nr_shallow++] = info->ours[i];
649 for (i = 0; i < info->nr_theirs; i++)
650 shallow[nr_shallow++] = info->theirs[i];
653 * Prepare the commit graph to track what refs can reach what
654 * (new) shallow commits.
656 nr = get_max_object_index();
657 for (i = 0; i < nr; i++) {
658 struct object *o = get_indexed_object(i);
659 if (!o || o->type != OBJ_COMMIT)
660 continue;
662 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
665 memset(&pi, 0, sizeof(pi));
666 init_ref_bitmap(&pi.ref_bitmap);
667 pi.nr_bits = ref->nr;
670 * "--not --all" to cut short the traversal if new refs
671 * connect to old refs. If not (e.g. force ref updates) it'll
672 * have to go down to the current shallow commits.
674 head_ref(mark_uninteresting, NULL);
675 for_each_ref(mark_uninteresting, NULL);
677 /* Mark potential bottoms so we won't go out of bound */
678 for (i = 0; i < nr_shallow; i++) {
679 struct commit *c = lookup_commit(the_repository,
680 &oid[shallow[i]]);
681 c->object.flags |= BOTTOM;
684 for (i = 0; i < ref->nr; i++)
685 paint_down(&pi, ref->oid + i, i);
687 if (used) {
688 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
689 memset(used, 0, sizeof(*used) * info->shallow->nr);
690 for (i = 0; i < nr_shallow; i++) {
691 const struct commit *c = lookup_commit(the_repository,
692 &oid[shallow[i]]);
693 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
694 if (*map)
695 used[shallow[i]] = xmemdupz(*map, bitmap_size);
698 * unreachable shallow commits are not removed from
699 * "ours" and "theirs". The user is supposed to run
700 * step 7 on every ref separately and not trust "ours"
701 * and "theirs" any more.
703 } else
704 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
706 clear_ref_bitmap(&pi.ref_bitmap);
707 for (i = 0; i < pi.pool_count; i++)
708 free(pi.pools[i]);
709 free(pi.pools);
710 free(shallow);
713 struct commit_array {
714 struct commit **commits;
715 int nr, alloc;
718 static int add_ref(const char *refname, const struct object_id *oid,
719 int flags, void *cb_data)
721 struct commit_array *ca = cb_data;
722 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
723 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
724 oid, 1);
725 if (ca->commits[ca->nr])
726 ca->nr++;
727 return 0;
730 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
732 unsigned int i;
733 if (!ref_status)
734 return;
735 for (i = 0; i < nr; i++)
736 if (bitmap[i / 32] & (1U << (i % 32)))
737 ref_status[i]++;
741 * Step 7, reachability test on "ours" at commit level
743 static void post_assign_shallow(struct shallow_info *info,
744 struct ref_bitmap *ref_bitmap,
745 int *ref_status)
747 struct object_id *oid = info->shallow->oid;
748 struct commit *c;
749 uint32_t **bitmap;
750 int dst, i, j;
751 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
752 struct commit_array ca;
754 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
755 if (ref_status)
756 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
758 /* Remove unreachable shallow commits from "theirs" */
759 for (i = dst = 0; i < info->nr_theirs; i++) {
760 if (i != dst)
761 info->theirs[dst] = info->theirs[i];
762 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
763 bitmap = ref_bitmap_at(ref_bitmap, c);
764 if (!*bitmap)
765 continue;
766 for (j = 0; j < bitmap_nr; j++)
767 if (bitmap[0][j]) {
768 update_refstatus(ref_status, info->ref->nr, *bitmap);
769 dst++;
770 break;
773 info->nr_theirs = dst;
775 memset(&ca, 0, sizeof(ca));
776 head_ref(add_ref, &ca);
777 for_each_ref(add_ref, &ca);
779 /* Remove unreachable shallow commits from "ours" */
780 for (i = dst = 0; i < info->nr_ours; i++) {
781 if (i != dst)
782 info->ours[dst] = info->ours[i];
783 c = lookup_commit(the_repository, &oid[info->ours[i]]);
784 bitmap = ref_bitmap_at(ref_bitmap, c);
785 if (!*bitmap)
786 continue;
787 for (j = 0; j < bitmap_nr; j++)
788 if (bitmap[0][j] &&
789 /* Step 7, reachability test at commit level */
790 !in_merge_bases_many(c, ca.nr, ca.commits)) {
791 update_refstatus(ref_status, info->ref->nr, *bitmap);
792 dst++;
793 break;
796 info->nr_ours = dst;
798 free(ca.commits);
801 /* (Delayed) step 7, reachability test at commit level */
802 int delayed_reachability_test(struct shallow_info *si, int c)
804 if (si->need_reachability_test[c]) {
805 struct commit *commit = lookup_commit(the_repository,
806 &si->shallow->oid[c]);
808 if (!si->commits) {
809 struct commit_array ca;
811 memset(&ca, 0, sizeof(ca));
812 head_ref(add_ref, &ca);
813 for_each_ref(add_ref, &ca);
814 si->commits = ca.commits;
815 si->nr_commits = ca.nr;
818 si->reachable[c] = in_merge_bases_many(commit,
819 si->nr_commits,
820 si->commits);
821 si->need_reachability_test[c] = 0;
823 return si->reachable[c];