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