]> git.ipfire.org Git - thirdparty/git.git/blame - commit-reach.c
Merge branch 'ab/config-multi-and-nonbool'
[thirdparty/git.git] / commit-reach.c
CommitLineData
36bf1958
EN
1#include "git-compat-util.h"
2#include "alloc.h"
5227c385 3#include "commit.h"
920f93ca 4#include "commit-graph.h"
1d614d41 5#include "decorate.h"
41771fa4 6#include "hex.h"
1d614d41
DS
7#include "prio-queue.h"
8#include "tree.h"
6621c838 9#include "ref-filter.h"
1d614d41
DS
10#include "revision.h"
11#include "tag.h"
5227c385 12#include "commit-reach.h"
fd67d149 13#include "ewah/ewok.h"
5227c385
DS
14
15/* Remember to update object flag allocation in object.h */
16#define PARENT1 (1u<<16)
17#define PARENT2 (1u<<17)
18#define STALE (1u<<18)
19#define RESULT (1u<<19)
20
21static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
22
c8d693e1
DS
23static int compare_commits_by_gen(const void *_a, const void *_b)
24{
25 const struct commit *a = *(const struct commit * const *)_a;
26 const struct commit *b = *(const struct commit * const *)_b;
27
28 timestamp_t generation_a = commit_graph_generation(a);
29 timestamp_t generation_b = commit_graph_generation(b);
30
31 if (generation_a < generation_b)
32 return -1;
33 if (generation_a > generation_b)
34 return 1;
36777733
DS
35 if (a->date < b->date)
36 return -1;
37 if (a->date > b->date)
38 return 1;
c8d693e1
DS
39 return 0;
40}
41
5227c385
DS
42static int queue_has_nonstale(struct prio_queue *queue)
43{
44 int i;
45 for (i = 0; i < queue->nr; i++) {
46 struct commit *commit = queue->array[i].data;
47 if (!(commit->object.flags & STALE))
48 return 1;
49 }
50 return 0;
51}
52
53/* all input commits in one and twos[] must have been parsed! */
c383830a
SB
54static struct commit_list *paint_down_to_common(struct repository *r,
55 struct commit *one, int n,
5227c385 56 struct commit **twos,
d7f92784 57 timestamp_t min_generation)
5227c385
DS
58{
59 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
60 struct commit_list *result = NULL;
61 int i;
d7f92784 62 timestamp_t last_gen = GENERATION_NUMBER_INFINITY;
5227c385 63
8d00d7c3 64 if (!min_generation && !corrected_commit_dates_enabled(r))
1b7a91da
JH
65 queue.compare = compare_commits_by_commit_date;
66
5227c385
DS
67 one->object.flags |= PARENT1;
68 if (!n) {
69 commit_list_append(one, &result);
70 return result;
71 }
72 prio_queue_put(&queue, one);
73
74 for (i = 0; i < n; i++) {
75 twos[i]->object.flags |= PARENT2;
76 prio_queue_put(&queue, twos[i]);
77 }
78
79 while (queue_has_nonstale(&queue)) {
80 struct commit *commit = prio_queue_get(&queue);
81 struct commit_list *parents;
82 int flags;
d7f92784 83 timestamp_t generation = commit_graph_generation(commit);
5227c385 84
c752ad09 85 if (min_generation && generation > last_gen)
d7f92784 86 BUG("bad generation skip %"PRItime" > %"PRItime" at %s",
c752ad09 87 generation, last_gen,
5227c385 88 oid_to_hex(&commit->object.oid));
c752ad09 89 last_gen = generation;
5227c385 90
c752ad09 91 if (generation < min_generation)
5227c385
DS
92 break;
93
94 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
95 if (flags == (PARENT1 | PARENT2)) {
96 if (!(commit->object.flags & RESULT)) {
97 commit->object.flags |= RESULT;
98 commit_list_insert_by_date(commit, &result);
99 }
100 /* Mark parents of a found merge stale */
101 flags |= STALE;
102 }
103 parents = commit->parents;
104 while (parents) {
105 struct commit *p = parents->item;
106 parents = parents->next;
107 if ((p->object.flags & flags) == flags)
108 continue;
c383830a 109 if (repo_parse_commit(r, p))
5227c385
DS
110 return NULL;
111 p->object.flags |= flags;
112 prio_queue_put(&queue, p);
113 }
114 }
115
116 clear_prio_queue(&queue);
117 return result;
118}
119
18256a91
SB
120static struct commit_list *merge_bases_many(struct repository *r,
121 struct commit *one, int n,
122 struct commit **twos)
5227c385
DS
123{
124 struct commit_list *list = NULL;
125 struct commit_list *result = NULL;
126 int i;
127
128 for (i = 0; i < n; i++) {
129 if (one == twos[i])
130 /*
131 * We do not mark this even with RESULT so we do not
132 * have to clean it up.
133 */
134 return commit_list_insert(one, &result);
135 }
136
18256a91 137 if (repo_parse_commit(r, one))
5227c385
DS
138 return NULL;
139 for (i = 0; i < n; i++) {
18256a91 140 if (repo_parse_commit(r, twos[i]))
5227c385
DS
141 return NULL;
142 }
143
18256a91 144 list = paint_down_to_common(r, one, n, twos, 0);
5227c385
DS
145
146 while (list) {
147 struct commit *commit = pop_commit(&list);
148 if (!(commit->object.flags & STALE))
149 commit_list_insert_by_date(commit, &result);
150 }
151 return result;
152}
153
154struct commit_list *get_octopus_merge_bases(struct commit_list *in)
155{
156 struct commit_list *i, *j, *k, *ret = NULL;
157
158 if (!in)
159 return ret;
160
161 commit_list_insert(in->item, &ret);
162
163 for (i = in->next; i; i = i->next) {
164 struct commit_list *new_commits = NULL, *end = NULL;
165
166 for (j = ret; j; j = j->next) {
167 struct commit_list *bases;
168 bases = get_merge_bases(i->item, j->item);
169 if (!new_commits)
170 new_commits = bases;
171 else
172 end->next = bases;
173 for (k = bases; k; k = k->next)
174 end = k;
175 }
176 ret = new_commits;
177 }
178 return ret;
179}
180
fbc21e3f
DS
181static int remove_redundant_no_gen(struct repository *r,
182 struct commit **array, int cnt)
5227c385 183{
5227c385
DS
184 struct commit **work;
185 unsigned char *redundant;
186 int *filled_index;
187 int i, j, filled;
188
ca56dadb 189 CALLOC_ARRAY(work, cnt);
5227c385
DS
190 redundant = xcalloc(cnt, 1);
191 ALLOC_ARRAY(filled_index, cnt - 1);
192
193 for (i = 0; i < cnt; i++)
ed8a0e3a 194 repo_parse_commit(r, array[i]);
5227c385
DS
195 for (i = 0; i < cnt; i++) {
196 struct commit_list *common;
d7f92784 197 timestamp_t min_generation = commit_graph_generation(array[i]);
5227c385
DS
198
199 if (redundant[i])
200 continue;
201 for (j = filled = 0; j < cnt; j++) {
d7f92784 202 timestamp_t curr_generation;
5227c385
DS
203 if (i == j || redundant[j])
204 continue;
205 filled_index[filled] = j;
206 work[filled++] = array[j];
207
c752ad09
AK
208 curr_generation = commit_graph_generation(array[j]);
209 if (curr_generation < min_generation)
210 min_generation = curr_generation;
5227c385 211 }
ed8a0e3a 212 common = paint_down_to_common(r, array[i], filled,
c383830a 213 work, min_generation);
5227c385
DS
214 if (array[i]->object.flags & PARENT2)
215 redundant[i] = 1;
216 for (j = 0; j < filled; j++)
217 if (work[j]->object.flags & PARENT1)
218 redundant[filled_index[j]] = 1;
219 clear_commit_marks(array[i], all_flags);
220 clear_commit_marks_many(filled, work, all_flags);
221 free_commit_list(common);
222 }
223
224 /* Now collect the result */
225 COPY_ARRAY(work, array, cnt);
226 for (i = filled = 0; i < cnt; i++)
227 if (!redundant[i])
228 array[filled++] = work[i];
5227c385
DS
229 free(work);
230 free(redundant);
231 free(filled_index);
232 return filled;
233}
234
fbc21e3f
DS
235static int remove_redundant_with_gen(struct repository *r,
236 struct commit **array, int cnt)
237{
36777733 238 int i, count_non_stale = 0, count_still_independent = cnt;
fbc21e3f 239 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
41f3c994 240 struct commit **walk_start, **sorted;
fbc21e3f 241 size_t walk_start_nr = 0, walk_start_alloc = cnt;
41f3c994
DS
242 int min_gen_pos = 0;
243
244 /*
245 * Sort the input by generation number, ascending. This allows
246 * us to increase the "min_generation" limit when we discover
247 * the commit with lowest generation is STALE. The index
248 * min_gen_pos points to the current position within 'array'
249 * that is not yet known to be STALE.
250 */
6e578410 251 DUP_ARRAY(sorted, array, cnt);
41f3c994
DS
252 QSORT(sorted, cnt, compare_commits_by_gen);
253 min_generation = commit_graph_generation(sorted[0]);
fbc21e3f
DS
254
255 ALLOC_ARRAY(walk_start, walk_start_alloc);
256
257 /* Mark all parents of the input as STALE */
258 for (i = 0; i < cnt; i++) {
259 struct commit_list *parents;
fbc21e3f
DS
260
261 repo_parse_commit(r, array[i]);
36777733 262 array[i]->object.flags |= RESULT;
fbc21e3f
DS
263 parents = array[i]->parents;
264
265 while (parents) {
266 repo_parse_commit(r, parents->item);
267 if (!(parents->item->object.flags & STALE)) {
268 parents->item->object.flags |= STALE;
269 ALLOC_GROW(walk_start, walk_start_nr + 1, walk_start_alloc);
270 walk_start[walk_start_nr++] = parents->item;
fbc21e3f
DS
271 }
272 parents = parents->next;
273 }
fbc21e3f
DS
274 }
275
36777733 276 QSORT(walk_start, walk_start_nr, compare_commits_by_gen);
fbc21e3f 277
36777733
DS
278 /* remove STALE bit for now to allow walking through parents */
279 for (i = 0; i < walk_start_nr; i++)
280 walk_start[i]->object.flags &= ~STALE;
fbc21e3f 281
36777733
DS
282 /*
283 * Start walking from the highest generation. Hopefully, it will
284 * find all other items during the first-parent walk, and we can
285 * terminate early. Otherwise, we will do the same amount of work
286 * as before.
287 */
288 for (i = walk_start_nr - 1; i >= 0 && count_still_independent > 1; i--) {
289 /* push the STALE bits up to min generation */
290 struct commit_list *stack = NULL;
fbc21e3f 291
36777733
DS
292 commit_list_insert(walk_start[i], &stack);
293 walk_start[i]->object.flags |= STALE;
294
295 while (stack) {
296 struct commit_list *parents;
297 struct commit *c = stack->item;
298
299 repo_parse_commit(r, c);
300
301 if (c->object.flags & RESULT) {
302 c->object.flags &= ~RESULT;
303 if (--count_still_independent <= 1)
304 break;
41f3c994
DS
305 if (oideq(&c->object.oid, &sorted[min_gen_pos]->object.oid)) {
306 while (min_gen_pos < cnt - 1 &&
307 (sorted[min_gen_pos]->object.flags & STALE))
308 min_gen_pos++;
309 min_generation = commit_graph_generation(sorted[min_gen_pos]);
310 }
fbc21e3f 311 }
36777733
DS
312
313 if (commit_graph_generation(c) < min_generation) {
314 pop_commit(&stack);
315 continue;
316 }
317
318 parents = c->parents;
319 while (parents) {
320 if (!(parents->item->object.flags & STALE)) {
321 parents->item->object.flags |= STALE;
322 commit_list_insert(parents->item, &stack);
323 break;
324 }
325 parents = parents->next;
326 }
327
328 /* pop if all parents have been visited already */
329 if (!parents)
330 pop_commit(&stack);
fbc21e3f 331 }
36777733 332 free_commit_list(stack);
fbc21e3f 333 }
41f3c994 334 free(sorted);
fbc21e3f 335
36777733
DS
336 /* clear result */
337 for (i = 0; i < cnt; i++)
338 array[i]->object.flags &= ~RESULT;
339
fbc21e3f
DS
340 /* rearrange array */
341 for (i = count_non_stale = 0; i < cnt; i++) {
342 if (!(array[i]->object.flags & STALE))
343 array[count_non_stale++] = array[i];
344 }
345
346 /* clear marks */
347 clear_commit_marks_many(walk_start_nr, walk_start, STALE);
348 free(walk_start);
349
350 return count_non_stale;
351}
352
353static int remove_redundant(struct repository *r, struct commit **array, int cnt)
354{
355 /*
356 * Some commit in the array may be an ancestor of
357 * another commit. Move the independent commits to the
358 * beginning of 'array' and return their number. Callers
359 * should not rely upon the contents of 'array' after
360 * that number.
361 */
362 if (generation_numbers_enabled(r)) {
363 int i;
364
365 /*
366 * If we have a single commit with finite generation
367 * number, then the _with_gen algorithm is preferred.
368 */
369 for (i = 0; i < cnt; i++) {
370 if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY)
371 return remove_redundant_with_gen(r, array, cnt);
372 }
373 }
374
375 return remove_redundant_no_gen(r, array, cnt);
376}
377
f28e87f5
SB
378static struct commit_list *get_merge_bases_many_0(struct repository *r,
379 struct commit *one,
5227c385
DS
380 int n,
381 struct commit **twos,
382 int cleanup)
383{
384 struct commit_list *list;
385 struct commit **rslt;
386 struct commit_list *result;
387 int cnt, i;
388
f28e87f5 389 result = merge_bases_many(r, one, n, twos);
5227c385
DS
390 for (i = 0; i < n; i++) {
391 if (one == twos[i])
392 return result;
393 }
394 if (!result || !result->next) {
395 if (cleanup) {
396 clear_commit_marks(one, all_flags);
397 clear_commit_marks_many(n, twos, all_flags);
398 }
399 return result;
400 }
401
402 /* There are more than one */
403 cnt = commit_list_count(result);
ca56dadb 404 CALLOC_ARRAY(rslt, cnt);
5227c385
DS
405 for (list = result, i = 0; list; list = list->next)
406 rslt[i++] = list->item;
407 free_commit_list(result);
408
409 clear_commit_marks(one, all_flags);
410 clear_commit_marks_many(n, twos, all_flags);
411
f28e87f5 412 cnt = remove_redundant(r, rslt, cnt);
5227c385
DS
413 result = NULL;
414 for (i = 0; i < cnt; i++)
415 commit_list_insert_by_date(rslt[i], &result);
416 free(rslt);
417 return result;
418}
419
21a9651b
SB
420struct commit_list *repo_get_merge_bases_many(struct repository *r,
421 struct commit *one,
422 int n,
423 struct commit **twos)
5227c385 424{
21a9651b 425 return get_merge_bases_many_0(r, one, n, twos, 1);
5227c385
DS
426}
427
21a9651b
SB
428struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
429 struct commit *one,
430 int n,
431 struct commit **twos)
5227c385 432{
21a9651b 433 return get_merge_bases_many_0(r, one, n, twos, 0);
5227c385
DS
434}
435
21a9651b
SB
436struct commit_list *repo_get_merge_bases(struct repository *r,
437 struct commit *one,
438 struct commit *two)
5227c385 439{
21a9651b 440 return get_merge_bases_many_0(r, one, 1, &two, 1);
5227c385
DS
441}
442
443/*
444 * Is "commit" a descendant of one of the elements on the "with_commit" list?
445 */
c1ea625f
CMAB
446int repo_is_descendant_of(struct repository *r,
447 struct commit *commit,
448 struct commit_list *with_commit)
5227c385
DS
449{
450 if (!with_commit)
451 return 1;
5227c385 452
6cc01743
DS
453 if (generation_numbers_enabled(the_repository)) {
454 struct commit_list *from_list = NULL;
455 int result;
456 commit_list_insert(commit, &from_list);
457 result = can_all_from_reach(from_list, with_commit, 0);
458 free_commit_list(from_list);
459 return result;
460 } else {
461 while (with_commit) {
462 struct commit *other;
463
464 other = with_commit->item;
465 with_commit = with_commit->next;
80b8ada5 466 if (repo_in_merge_bases_many(r, other, 1, &commit))
6cc01743
DS
467 return 1;
468 }
469 return 0;
5227c385 470 }
5227c385
DS
471}
472
473/*
474 * Is "commit" an ancestor of one of the "references"?
475 */
4d5430f7
SB
476int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
477 int nr_reference, struct commit **reference)
5227c385
DS
478{
479 struct commit_list *bases;
480 int ret = 0, i;
d7f92784 481 timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
5227c385 482
4d5430f7 483 if (repo_parse_commit(r, commit))
5227c385
DS
484 return ret;
485 for (i = 0; i < nr_reference; i++) {
4d5430f7 486 if (repo_parse_commit(r, reference[i]))
5227c385 487 return ret;
c752ad09
AK
488
489 generation = commit_graph_generation(reference[i]);
8791bf18
DS
490 if (generation > max_generation)
491 max_generation = generation;
5227c385
DS
492 }
493
c752ad09 494 generation = commit_graph_generation(commit);
8791bf18 495 if (generation > max_generation)
5227c385
DS
496 return ret;
497
4d5430f7 498 bases = paint_down_to_common(r, commit,
c383830a 499 nr_reference, reference,
c752ad09 500 generation);
5227c385
DS
501 if (commit->object.flags & PARENT2)
502 ret = 1;
503 clear_commit_marks(commit, all_flags);
504 clear_commit_marks_many(nr_reference, reference, all_flags);
505 free_commit_list(bases);
506 return ret;
507}
508
509/*
510 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
511 */
4d5430f7
SB
512int repo_in_merge_bases(struct repository *r,
513 struct commit *commit,
514 struct commit *reference)
5227c385 515{
80b8ada5
DS
516 int res;
517 struct commit_list *list = NULL;
518 struct commit_list **next = &list;
519
520 next = commit_list_append(commit, next);
521 res = repo_is_descendant_of(r, reference, list);
522 free_commit_list(list);
523
524 return res;
5227c385
DS
525}
526
527struct commit_list *reduce_heads(struct commit_list *heads)
528{
529 struct commit_list *p;
530 struct commit_list *result = NULL, **tail = &result;
531 struct commit **array;
532 int num_head, i;
533
534 if (!heads)
535 return NULL;
536
537 /* Uniquify */
538 for (p = heads; p; p = p->next)
539 p->item->object.flags &= ~STALE;
540 for (p = heads, num_head = 0; p; p = p->next) {
541 if (p->item->object.flags & STALE)
542 continue;
543 p->item->object.flags |= STALE;
544 num_head++;
545 }
ca56dadb 546 CALLOC_ARRAY(array, num_head);
5227c385
DS
547 for (p = heads, i = 0; p; p = p->next) {
548 if (p->item->object.flags & STALE) {
549 array[i++] = p->item;
550 p->item->object.flags &= ~STALE;
551 }
552 }
ed8a0e3a 553 num_head = remove_redundant(the_repository, array, num_head);
5227c385
DS
554 for (i = 0; i < num_head; i++)
555 tail = &commit_list_insert(array[i], tail)->next;
556 free(array);
557 return result;
558}
559
560void reduce_heads_replace(struct commit_list **heads)
561{
562 struct commit_list *result = reduce_heads(*heads);
563 free_commit_list(*heads);
564 *heads = result;
565}
1d614d41 566
1d614d41
DS
567int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
568{
569 struct object *o;
570 struct commit *old_commit, *new_commit;
1e3497a2 571 struct commit_list *old_commit_list = NULL;
d546fe28 572 int ret;
1d614d41
DS
573
574 /*
575 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
576 * old_commit. Otherwise we require --force.
577 */
578 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
579 NULL, 0);
580 if (!o || o->type != OBJ_COMMIT)
581 return 0;
582 old_commit = (struct commit *) o;
583
584 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
585 NULL, 0);
586 if (!o || o->type != OBJ_COMMIT)
587 return 0;
588 new_commit = (struct commit *) o;
589
590 if (parse_commit(new_commit) < 0)
591 return 0;
592
1e3497a2 593 commit_list_insert(old_commit, &old_commit_list);
0258ed1e 594 ret = repo_is_descendant_of(the_repository,
c1ea625f 595 new_commit, old_commit_list);
d546fe28
RS
596 free_commit_list(old_commit_list);
597 return ret;
1d614d41 598}
920f93ca
DS
599
600/*
601 * Mimicking the real stack, this stack lives on the heap, avoiding stack
602 * overflows.
603 *
604 * At each recursion step, the stack items points to the commits whose
605 * ancestors are to be inspected.
606 */
607struct contains_stack {
608 int nr, alloc;
609 struct contains_stack_entry {
610 struct commit *commit;
611 struct commit_list *parents;
612 } *contains_stack;
613};
614
615static int in_commit_list(const struct commit_list *want, struct commit *c)
616{
617 for (; want; want = want->next)
e43d2dcc 618 if (oideq(&want->item->object.oid, &c->object.oid))
920f93ca
DS
619 return 1;
620 return 0;
621}
622
623/*
624 * Test whether the candidate is contained in the list.
625 * Do not recurse to find out, though, but return -1 if inconclusive.
626 */
627static enum contains_result contains_test(struct commit *candidate,
628 const struct commit_list *want,
629 struct contains_cache *cache,
d7f92784 630 timestamp_t cutoff)
920f93ca
DS
631{
632 enum contains_result *cached = contains_cache_at(cache, candidate);
633
634 /* If we already have the answer cached, return that. */
635 if (*cached)
636 return *cached;
637
638 /* or are we it? */
639 if (in_commit_list(want, candidate)) {
640 *cached = CONTAINS_YES;
641 return CONTAINS_YES;
642 }
643
644 /* Otherwise, we don't know; prepare to recurse */
645 parse_commit_or_die(candidate);
646
c49c82aa 647 if (commit_graph_generation(candidate) < cutoff)
920f93ca
DS
648 return CONTAINS_NO;
649
650 return CONTAINS_UNKNOWN;
651}
652
653static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
654{
655 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
656 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
657 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
658}
659
660static enum contains_result contains_tag_algo(struct commit *candidate,
661 const struct commit_list *want,
662 struct contains_cache *cache)
663{
664 struct contains_stack contains_stack = { 0, 0, NULL };
665 enum contains_result result;
d7f92784 666 timestamp_t cutoff = GENERATION_NUMBER_INFINITY;
920f93ca
DS
667 const struct commit_list *p;
668
669 for (p = want; p; p = p->next) {
d7f92784 670 timestamp_t generation;
920f93ca
DS
671 struct commit *c = p->item;
672 load_commit_graph_info(the_repository, c);
c752ad09
AK
673 generation = commit_graph_generation(c);
674 if (generation < cutoff)
675 cutoff = generation;
920f93ca
DS
676 }
677
678 result = contains_test(candidate, want, cache, cutoff);
679 if (result != CONTAINS_UNKNOWN)
680 return result;
681
682 push_to_contains_stack(candidate, &contains_stack);
683 while (contains_stack.nr) {
684 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
685 struct commit *commit = entry->commit;
686 struct commit_list *parents = entry->parents;
687
688 if (!parents) {
689 *contains_cache_at(cache, commit) = CONTAINS_NO;
690 contains_stack.nr--;
691 }
692 /*
693 * If we just popped the stack, parents->item has been marked,
694 * therefore contains_test will return a meaningful yes/no.
695 */
696 else switch (contains_test(parents->item, want, cache, cutoff)) {
697 case CONTAINS_YES:
698 *contains_cache_at(cache, commit) = CONTAINS_YES;
699 contains_stack.nr--;
700 break;
701 case CONTAINS_NO:
702 entry->parents = parents->next;
703 break;
704 case CONTAINS_UNKNOWN:
705 push_to_contains_stack(parents->item, &contains_stack);
706 break;
707 }
708 }
709 free(contains_stack.contains_stack);
710 return contains_test(candidate, want, cache, cutoff);
711}
712
713int commit_contains(struct ref_filter *filter, struct commit *commit,
714 struct commit_list *list, struct contains_cache *cache)
715{
716 if (filter->with_commit_tag_algo)
717 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
c1ea625f 718 return repo_is_descendant_of(the_repository, commit, list);
920f93ca 719}
ba3ca1ed 720
ba3ca1ed
DS
721int can_all_from_reach_with_flag(struct object_array *from,
722 unsigned int with_flag,
723 unsigned int assign_flag,
4fbcca4e 724 time_t min_commit_date,
d7f92784 725 timestamp_t min_generation)
ba3ca1ed 726{
4fbcca4e 727 struct commit **list = NULL;
ba3ca1ed 728 int i;
b67f6b26 729 int nr_commits;
4fbcca4e 730 int result = 1;
ba3ca1ed 731
4fbcca4e 732 ALLOC_ARRAY(list, from->nr);
b67f6b26 733 nr_commits = 0;
ba3ca1ed 734 for (i = 0; i < from->nr; i++) {
b67f6b26 735 struct object *from_one = from->objects[i].item;
ba3ca1ed 736
b67f6b26
DS
737 if (!from_one || from_one->flags & assign_flag)
738 continue;
739
740 from_one = deref_tag(the_repository, from_one,
741 "a from object", 0);
742 if (!from_one || from_one->type != OBJ_COMMIT) {
85806440
DS
743 /*
744 * no way to tell if this is reachable by
b67f6b26
DS
745 * looking at the ancestry chain alone, so
746 * leave a note to ourselves not to worry about
747 * this object anymore.
748 */
749 from->objects[i].item->flags |= assign_flag;
750 continue;
751 }
752
753 list[nr_commits] = (struct commit *)from_one;
754 if (parse_commit(list[nr_commits]) ||
c49c82aa 755 commit_graph_generation(list[nr_commits]) < min_generation) {
b67f6b26
DS
756 result = 0;
757 goto cleanup;
758 }
759
760 nr_commits++;
ba3ca1ed 761 }
4fbcca4e 762
b67f6b26 763 QSORT(list, nr_commits, compare_commits_by_gen);
4fbcca4e 764
b67f6b26 765 for (i = 0; i < nr_commits; i++) {
4fbcca4e
DS
766 /* DFS from list[i] */
767 struct commit_list *stack = NULL;
768
769 list[i]->object.flags |= assign_flag;
770 commit_list_insert(list[i], &stack);
771
772 while (stack) {
773 struct commit_list *parent;
774
b6723e46 775 if (stack->item->object.flags & (with_flag | RESULT)) {
4fbcca4e 776 pop_commit(&stack);
b6723e46
DS
777 if (stack)
778 stack->item->object.flags |= RESULT;
4fbcca4e
DS
779 continue;
780 }
781
782 for (parent = stack->item->parents; parent; parent = parent->next) {
783 if (parent->item->object.flags & (with_flag | RESULT))
784 stack->item->object.flags |= RESULT;
785
786 if (!(parent->item->object.flags & assign_flag)) {
787 parent->item->object.flags |= assign_flag;
788
789 if (parse_commit(parent->item) ||
790 parent->item->date < min_commit_date ||
c49c82aa 791 commit_graph_generation(parent->item) < min_generation)
4fbcca4e
DS
792 continue;
793
794 commit_list_insert(parent->item, &stack);
795 break;
796 }
797 }
798
799 if (!parent)
800 pop_commit(&stack);
801 }
802
803 if (!(list[i]->object.flags & (with_flag | RESULT))) {
804 result = 0;
805 goto cleanup;
806 }
807 }
808
809cleanup:
85806440 810 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
4067a646
DS
811 free(list);
812
c5773dc0
EW
813 for (i = 0; i < from->nr; i++) {
814 struct object *from_one = from->objects[i].item;
815
816 if (from_one)
817 from_one->flags &= ~assign_flag;
818 }
4067a646 819
4fbcca4e 820 return result;
ba3ca1ed 821}
1792bc12
DS
822
823int can_all_from_reach(struct commit_list *from, struct commit_list *to,
824 int cutoff_by_min_date)
825{
826 struct object_array from_objs = OBJECT_ARRAY_INIT;
827 time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
828 struct commit_list *from_iter = from, *to_iter = to;
829 int result;
d7f92784 830 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
1792bc12
DS
831
832 while (from_iter) {
833 add_object_array(&from_iter->item->object, NULL, &from_objs);
834
835 if (!parse_commit(from_iter->item)) {
d7f92784 836 timestamp_t generation;
1792bc12
DS
837 if (from_iter->item->date < min_commit_date)
838 min_commit_date = from_iter->item->date;
4fbcca4e 839
c752ad09
AK
840 generation = commit_graph_generation(from_iter->item);
841 if (generation < min_generation)
842 min_generation = generation;
1792bc12
DS
843 }
844
845 from_iter = from_iter->next;
846 }
847
848 while (to_iter) {
849 if (!parse_commit(to_iter->item)) {
d7f92784 850 timestamp_t generation;
1792bc12
DS
851 if (to_iter->item->date < min_commit_date)
852 min_commit_date = to_iter->item->date;
4fbcca4e 853
c752ad09
AK
854 generation = commit_graph_generation(to_iter->item);
855 if (generation < min_generation)
856 min_generation = generation;
1792bc12
DS
857 }
858
859 to_iter->item->object.flags |= PARENT2;
860
861 to_iter = to_iter->next;
862 }
863
864 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
4fbcca4e 865 min_commit_date, min_generation);
1792bc12
DS
866
867 while (from) {
868 clear_commit_marks(from->item, PARENT1);
869 from = from->next;
870 }
871
872 while (to) {
873 clear_commit_marks(to->item, PARENT2);
874 to = to->next;
875 }
876
877 object_array_clear(&from_objs);
878 return result;
879}
fcb2c076
DS
880
881struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
882 struct commit **to, int nr_to,
883 unsigned int reachable_flag)
884{
885 struct commit **item;
886 struct commit *current;
887 struct commit_list *found_commits = NULL;
888 struct commit **to_last = to + nr_to;
889 struct commit **from_last = from + nr_from;
d7f92784 890 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
fcb2c076
DS
891 int num_to_find = 0;
892
893 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
894
895 for (item = to; item < to_last; item++) {
d7f92784 896 timestamp_t generation;
fcb2c076
DS
897 struct commit *c = *item;
898
899 parse_commit(c);
c752ad09
AK
900 generation = commit_graph_generation(c);
901 if (generation < min_generation)
902 min_generation = generation;
fcb2c076
DS
903
904 if (!(c->object.flags & PARENT1)) {
905 c->object.flags |= PARENT1;
906 num_to_find++;
907 }
908 }
909
910 for (item = from; item < from_last; item++) {
911 struct commit *c = *item;
912 if (!(c->object.flags & PARENT2)) {
913 c->object.flags |= PARENT2;
914 parse_commit(c);
915
916 prio_queue_put(&queue, *item);
917 }
918 }
919
920 while (num_to_find && (current = prio_queue_get(&queue)) != NULL) {
921 struct commit_list *parents;
922
923 if (current->object.flags & PARENT1) {
924 current->object.flags &= ~PARENT1;
925 current->object.flags |= reachable_flag;
926 commit_list_insert(current, &found_commits);
927 num_to_find--;
928 }
929
930 for (parents = current->parents; parents; parents = parents->next) {
931 struct commit *p = parents->item;
932
933 parse_commit(p);
934
c49c82aa 935 if (commit_graph_generation(p) < min_generation)
fcb2c076
DS
936 continue;
937
938 if (p->object.flags & PARENT2)
939 continue;
940
941 p->object.flags |= PARENT2;
942 prio_queue_put(&queue, p);
943 }
944 }
945
946 clear_commit_marks_many(nr_to, to, PARENT1);
947 clear_commit_marks_many(nr_from, from, PARENT2);
948
949 return found_commits;
950}
fd67d149
DS
951
952define_commit_slab(bit_arrays, struct bitmap *);
953static struct bit_arrays bit_arrays;
954
955static void insert_no_dup(struct prio_queue *queue, struct commit *c)
956{
957 if (c->object.flags & PARENT2)
958 return;
959 prio_queue_put(queue, c);
960 c->object.flags |= PARENT2;
961}
962
963static struct bitmap *get_bit_array(struct commit *c, int width)
964{
965 struct bitmap **bitmap = bit_arrays_at(&bit_arrays, c);
966 if (!*bitmap)
967 *bitmap = bitmap_word_alloc(width);
968 return *bitmap;
969}
970
971static void free_bit_array(struct commit *c)
972{
973 struct bitmap **bitmap = bit_arrays_at(&bit_arrays, c);
974 if (!*bitmap)
975 return;
976 bitmap_free(*bitmap);
977 *bitmap = NULL;
978}
979
980void ahead_behind(struct repository *r,
981 struct commit **commits, size_t commits_nr,
982 struct ahead_behind_count *counts, size_t counts_nr)
983{
984 struct prio_queue queue = { .compare = compare_commits_by_gen_then_commit_date };
985 size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD);
986
987 if (!commits_nr || !counts_nr)
988 return;
989
990 for (size_t i = 0; i < counts_nr; i++) {
991 counts[i].ahead = 0;
992 counts[i].behind = 0;
993 }
994
995 ensure_generations_valid(r, commits, commits_nr);
996
997 init_bit_arrays(&bit_arrays);
998
999 for (size_t i = 0; i < commits_nr; i++) {
1000 struct commit *c = commits[i];
1001 struct bitmap *bitmap = get_bit_array(c, width);
1002
1003 bitmap_set(bitmap, i);
1004 insert_no_dup(&queue, c);
1005 }
1006
1007 while (queue_has_nonstale(&queue)) {
1008 struct commit *c = prio_queue_get(&queue);
1009 struct commit_list *p;
1010 struct bitmap *bitmap_c = get_bit_array(c, width);
1011
1012 for (size_t i = 0; i < counts_nr; i++) {
1013 int reach_from_tip = !!bitmap_get(bitmap_c, counts[i].tip_index);
1014 int reach_from_base = !!bitmap_get(bitmap_c, counts[i].base_index);
1015
1016 if (reach_from_tip ^ reach_from_base) {
1017 if (reach_from_base)
1018 counts[i].behind++;
1019 else
1020 counts[i].ahead++;
1021 }
1022 }
1023
1024 for (p = c->parents; p; p = p->next) {
1025 struct bitmap *bitmap_p;
1026
1027 repo_parse_commit(r, p->item);
1028
1029 bitmap_p = get_bit_array(p->item, width);
1030 bitmap_or(bitmap_p, bitmap_c);
1031
1032 /*
1033 * If this parent is reachable from every starting
1034 * commit, then none of its ancestors can contribute
1035 * to the ahead/behind count. Mark it as STALE, so
1036 * we can stop the walk when every commit in the
1037 * queue is STALE.
1038 */
1039 if (bitmap_popcount(bitmap_p) == commits_nr)
1040 p->item->object.flags |= STALE;
1041
1042 insert_no_dup(&queue, p->item);
1043 }
1044
1045 free_bit_array(c);
1046 }
1047
1048 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1049 repo_clear_commit_marks(r, PARENT2 | STALE);
1050 clear_bit_arrays(&bit_arrays);
1051 clear_prio_queue(&queue);
1052}
cbfe360b
DS
1053
1054struct commit_and_index {
1055 struct commit *commit;
1056 unsigned int index;
1057 timestamp_t generation;
1058};
1059
1060static int compare_commit_and_index_by_generation(const void *va, const void *vb)
1061{
1062 const struct commit_and_index *a = (const struct commit_and_index *)va;
1063 const struct commit_and_index *b = (const struct commit_and_index *)vb;
1064
1065 if (a->generation > b->generation)
1066 return 1;
1067 if (a->generation < b->generation)
1068 return -1;
1069 return 0;
1070}
1071
1072void tips_reachable_from_bases(struct repository *r,
1073 struct commit_list *bases,
1074 struct commit **tips, size_t tips_nr,
1075 int mark)
1076{
1077 struct commit_and_index *commits;
1078 size_t min_generation_index = 0;
1079 timestamp_t min_generation;
1080 struct commit_list *stack = NULL;
1081
1082 if (!bases || !tips || !tips_nr)
1083 return;
1084
1085 /*
1086 * Do a depth-first search starting at 'bases' to search for the
1087 * tips. Stop at the lowest (un-found) generation number. When
1088 * finding the lowest commit, increase the minimum generation
1089 * number to the next lowest (un-found) generation number.
1090 */
1091
1092 CALLOC_ARRAY(commits, tips_nr);
1093
1094 for (size_t i = 0; i < tips_nr; i++) {
1095 commits[i].commit = tips[i];
1096 commits[i].index = i;
1097 commits[i].generation = commit_graph_generation(tips[i]);
1098 }
1099
1100 /* Sort with generation number ascending. */
1101 QSORT(commits, tips_nr, compare_commit_and_index_by_generation);
1102 min_generation = commits[0].generation;
1103
1104 while (bases) {
1105 repo_parse_commit(r, bases->item);
1106 commit_list_insert(bases->item, &stack);
1107 bases = bases->next;
1108 }
1109
1110 while (stack) {
1111 int explored_all_parents = 1;
1112 struct commit_list *p;
1113 struct commit *c = stack->item;
1114 timestamp_t c_gen = commit_graph_generation(c);
1115
1116 /* Does it match any of our tips? */
1117 for (size_t j = min_generation_index; j < tips_nr; j++) {
1118 if (c_gen < commits[j].generation)
1119 break;
1120
1121 if (commits[j].commit == c) {
1122 tips[commits[j].index]->object.flags |= mark;
1123
1124 if (j == min_generation_index) {
1125 unsigned int k = j + 1;
1126 while (k < tips_nr &&
1127 (tips[commits[k].index]->object.flags & mark))
1128 k++;
1129
1130 /* Terminate early if all found. */
1131 if (k >= tips_nr)
1132 goto done;
1133
1134 min_generation_index = k;
1135 min_generation = commits[k].generation;
1136 }
1137 }
1138 }
1139
1140 for (p = c->parents; p; p = p->next) {
1141 repo_parse_commit(r, p->item);
1142
1143 /* Have we already explored this parent? */
1144 if (p->item->object.flags & SEEN)
1145 continue;
1146
1147 /* Is it below the current minimum generation? */
1148 if (commit_graph_generation(p->item) < min_generation)
1149 continue;
1150
1151 /* Ok, we will explore from here on. */
1152 p->item->object.flags |= SEEN;
1153 explored_all_parents = 0;
1154 commit_list_insert(p->item, &stack);
1155 break;
1156 }
1157
1158 if (explored_all_parents)
1159 pop_commit(&stack);
1160 }
1161
1162done:
1163 free(commits);
1164 repo_clear_commit_marks(r, SEEN);
1165}