]> git.ipfire.org Git - thirdparty/git.git/blame - commit-reach.c
Git 2.44-rc2
[thirdparty/git.git] / commit-reach.c
CommitLineData
36bf1958 1#include "git-compat-util.h"
5227c385 2#include "commit.h"
920f93ca 3#include "commit-graph.h"
1d614d41 4#include "decorate.h"
41771fa4 5#include "hex.h"
1d614d41 6#include "prio-queue.h"
6621c838 7#include "ref-filter.h"
1d614d41
DS
8#include "revision.h"
9#include "tag.h"
5227c385 10#include "commit-reach.h"
fd67d149 11#include "ewah/ewok.h"
5227c385
DS
12
13/* Remember to update object flag allocation in object.h */
14#define PARENT1 (1u<<16)
15#define PARENT2 (1u<<17)
16#define STALE (1u<<18)
17#define RESULT (1u<<19)
18
19static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
20
c8d693e1
DS
21static int compare_commits_by_gen(const void *_a, const void *_b)
22{
23 const struct commit *a = *(const struct commit * const *)_a;
24 const struct commit *b = *(const struct commit * const *)_b;
25
26 timestamp_t generation_a = commit_graph_generation(a);
27 timestamp_t generation_b = commit_graph_generation(b);
28
29 if (generation_a < generation_b)
30 return -1;
31 if (generation_a > generation_b)
32 return 1;
36777733
DS
33 if (a->date < b->date)
34 return -1;
35 if (a->date > b->date)
36 return 1;
c8d693e1
DS
37 return 0;
38}
39
5227c385
DS
40static int queue_has_nonstale(struct prio_queue *queue)
41{
42 int i;
43 for (i = 0; i < queue->nr; i++) {
44 struct commit *commit = queue->array[i].data;
45 if (!(commit->object.flags & STALE))
46 return 1;
47 }
48 return 0;
49}
50
51/* all input commits in one and twos[] must have been parsed! */
c383830a
SB
52static struct commit_list *paint_down_to_common(struct repository *r,
53 struct commit *one, int n,
5227c385 54 struct commit **twos,
d7f92784 55 timestamp_t min_generation)
5227c385
DS
56{
57 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
58 struct commit_list *result = NULL;
59 int i;
d7f92784 60 timestamp_t last_gen = GENERATION_NUMBER_INFINITY;
5227c385 61
8d00d7c3 62 if (!min_generation && !corrected_commit_dates_enabled(r))
1b7a91da
JH
63 queue.compare = compare_commits_by_commit_date;
64
5227c385
DS
65 one->object.flags |= PARENT1;
66 if (!n) {
67 commit_list_append(one, &result);
68 return result;
69 }
70 prio_queue_put(&queue, one);
71
72 for (i = 0; i < n; i++) {
73 twos[i]->object.flags |= PARENT2;
74 prio_queue_put(&queue, twos[i]);
75 }
76
77 while (queue_has_nonstale(&queue)) {
78 struct commit *commit = prio_queue_get(&queue);
79 struct commit_list *parents;
80 int flags;
d7f92784 81 timestamp_t generation = commit_graph_generation(commit);
5227c385 82
c752ad09 83 if (min_generation && generation > last_gen)
d7f92784 84 BUG("bad generation skip %"PRItime" > %"PRItime" at %s",
c752ad09 85 generation, last_gen,
5227c385 86 oid_to_hex(&commit->object.oid));
c752ad09 87 last_gen = generation;
5227c385 88
c752ad09 89 if (generation < min_generation)
5227c385
DS
90 break;
91
92 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
93 if (flags == (PARENT1 | PARENT2)) {
94 if (!(commit->object.flags & RESULT)) {
95 commit->object.flags |= RESULT;
96 commit_list_insert_by_date(commit, &result);
97 }
98 /* Mark parents of a found merge stale */
99 flags |= STALE;
100 }
101 parents = commit->parents;
102 while (parents) {
103 struct commit *p = parents->item;
104 parents = parents->next;
105 if ((p->object.flags & flags) == flags)
106 continue;
c383830a 107 if (repo_parse_commit(r, p))
5227c385
DS
108 return NULL;
109 p->object.flags |= flags;
110 prio_queue_put(&queue, p);
111 }
112 }
113
114 clear_prio_queue(&queue);
115 return result;
116}
117
18256a91
SB
118static struct commit_list *merge_bases_many(struct repository *r,
119 struct commit *one, int n,
120 struct commit **twos)
5227c385
DS
121{
122 struct commit_list *list = NULL;
123 struct commit_list *result = NULL;
124 int i;
125
126 for (i = 0; i < n; i++) {
127 if (one == twos[i])
128 /*
129 * We do not mark this even with RESULT so we do not
130 * have to clean it up.
131 */
132 return commit_list_insert(one, &result);
133 }
134
18256a91 135 if (repo_parse_commit(r, one))
5227c385
DS
136 return NULL;
137 for (i = 0; i < n; i++) {
18256a91 138 if (repo_parse_commit(r, twos[i]))
5227c385
DS
139 return NULL;
140 }
141
18256a91 142 list = paint_down_to_common(r, one, n, twos, 0);
5227c385
DS
143
144 while (list) {
145 struct commit *commit = pop_commit(&list);
146 if (!(commit->object.flags & STALE))
147 commit_list_insert_by_date(commit, &result);
148 }
149 return result;
150}
151
152struct commit_list *get_octopus_merge_bases(struct commit_list *in)
153{
154 struct commit_list *i, *j, *k, *ret = NULL;
155
156 if (!in)
157 return ret;
158
159 commit_list_insert(in->item, &ret);
160
161 for (i = in->next; i; i = i->next) {
162 struct commit_list *new_commits = NULL, *end = NULL;
163
164 for (j = ret; j; j = j->next) {
165 struct commit_list *bases;
cb338c23
ÆAB
166 bases = repo_get_merge_bases(the_repository, i->item,
167 j->item);
5227c385
DS
168 if (!new_commits)
169 new_commits = bases;
170 else
171 end->next = bases;
172 for (k = bases; k; k = k->next)
173 end = k;
174 }
ec97ad12 175 free_commit_list(ret);
5227c385
DS
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
4a93b899 453 if (generation_numbers_enabled(r)) {
6cc01743
DS
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
ecb5091f 590 if (repo_parse_commit(the_repository, new_commit) < 0)
1d614d41
DS
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;
ecb5091f 754 if (repo_parse_commit(the_repository, 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
ecb5091f 789 if (repo_parse_commit(the_repository, parent->item) ||
4fbcca4e 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
ecb5091f 835 if (!repo_parse_commit(the_repository, 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) {
ecb5091f 849 if (!repo_parse_commit(the_repository, 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
ecb5091f 899 repo_parse_commit(the_repository, 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;
ecb5091f 914 repo_parse_commit(the_repository, c);
fcb2c076
DS
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
ecb5091f 933 repo_parse_commit(the_repository, p);
fcb2c076 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
68b51172
MH
946 clear_prio_queue(&queue);
947
fcb2c076
DS
948 clear_commit_marks_many(nr_to, to, PARENT1);
949 clear_commit_marks_many(nr_from, from, PARENT2);
950
951 return found_commits;
952}
fd67d149
DS
953
954define_commit_slab(bit_arrays, struct bitmap *);
955static struct bit_arrays bit_arrays;
956
957static void insert_no_dup(struct prio_queue *queue, struct commit *c)
958{
959 if (c->object.flags & PARENT2)
960 return;
961 prio_queue_put(queue, c);
962 c->object.flags |= PARENT2;
963}
964
965static struct bitmap *get_bit_array(struct commit *c, int width)
966{
967 struct bitmap **bitmap = bit_arrays_at(&bit_arrays, c);
968 if (!*bitmap)
969 *bitmap = bitmap_word_alloc(width);
970 return *bitmap;
971}
972
973static void free_bit_array(struct commit *c)
974{
975 struct bitmap **bitmap = bit_arrays_at(&bit_arrays, c);
976 if (!*bitmap)
977 return;
978 bitmap_free(*bitmap);
979 *bitmap = NULL;
980}
981
982void ahead_behind(struct repository *r,
983 struct commit **commits, size_t commits_nr,
984 struct ahead_behind_count *counts, size_t counts_nr)
985{
986 struct prio_queue queue = { .compare = compare_commits_by_gen_then_commit_date };
987 size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD);
988
989 if (!commits_nr || !counts_nr)
990 return;
991
992 for (size_t i = 0; i < counts_nr; i++) {
993 counts[i].ahead = 0;
994 counts[i].behind = 0;
995 }
996
997 ensure_generations_valid(r, commits, commits_nr);
998
999 init_bit_arrays(&bit_arrays);
1000
1001 for (size_t i = 0; i < commits_nr; i++) {
1002 struct commit *c = commits[i];
1003 struct bitmap *bitmap = get_bit_array(c, width);
1004
1005 bitmap_set(bitmap, i);
1006 insert_no_dup(&queue, c);
1007 }
1008
1009 while (queue_has_nonstale(&queue)) {
1010 struct commit *c = prio_queue_get(&queue);
1011 struct commit_list *p;
1012 struct bitmap *bitmap_c = get_bit_array(c, width);
1013
1014 for (size_t i = 0; i < counts_nr; i++) {
1015 int reach_from_tip = !!bitmap_get(bitmap_c, counts[i].tip_index);
1016 int reach_from_base = !!bitmap_get(bitmap_c, counts[i].base_index);
1017
1018 if (reach_from_tip ^ reach_from_base) {
1019 if (reach_from_base)
1020 counts[i].behind++;
1021 else
1022 counts[i].ahead++;
1023 }
1024 }
1025
1026 for (p = c->parents; p; p = p->next) {
1027 struct bitmap *bitmap_p;
1028
1029 repo_parse_commit(r, p->item);
1030
1031 bitmap_p = get_bit_array(p->item, width);
1032 bitmap_or(bitmap_p, bitmap_c);
1033
1034 /*
1035 * If this parent is reachable from every starting
1036 * commit, then none of its ancestors can contribute
1037 * to the ahead/behind count. Mark it as STALE, so
1038 * we can stop the walk when every commit in the
1039 * queue is STALE.
1040 */
1041 if (bitmap_popcount(bitmap_p) == commits_nr)
1042 p->item->object.flags |= STALE;
1043
1044 insert_no_dup(&queue, p->item);
1045 }
1046
1047 free_bit_array(c);
1048 }
1049
1050 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1051 repo_clear_commit_marks(r, PARENT2 | STALE);
1052 clear_bit_arrays(&bit_arrays);
1053 clear_prio_queue(&queue);
1054}
cbfe360b
DS
1055
1056struct commit_and_index {
1057 struct commit *commit;
1058 unsigned int index;
1059 timestamp_t generation;
1060};
1061
1062static int compare_commit_and_index_by_generation(const void *va, const void *vb)
1063{
1064 const struct commit_and_index *a = (const struct commit_and_index *)va;
1065 const struct commit_and_index *b = (const struct commit_and_index *)vb;
1066
1067 if (a->generation > b->generation)
1068 return 1;
1069 if (a->generation < b->generation)
1070 return -1;
1071 return 0;
1072}
1073
1074void tips_reachable_from_bases(struct repository *r,
1075 struct commit_list *bases,
1076 struct commit **tips, size_t tips_nr,
1077 int mark)
1078{
1079 struct commit_and_index *commits;
1080 size_t min_generation_index = 0;
1081 timestamp_t min_generation;
1082 struct commit_list *stack = NULL;
1083
1084 if (!bases || !tips || !tips_nr)
1085 return;
1086
1087 /*
1088 * Do a depth-first search starting at 'bases' to search for the
1089 * tips. Stop at the lowest (un-found) generation number. When
1090 * finding the lowest commit, increase the minimum generation
1091 * number to the next lowest (un-found) generation number.
1092 */
1093
1094 CALLOC_ARRAY(commits, tips_nr);
1095
1096 for (size_t i = 0; i < tips_nr; i++) {
1097 commits[i].commit = tips[i];
1098 commits[i].index = i;
1099 commits[i].generation = commit_graph_generation(tips[i]);
1100 }
1101
1102 /* Sort with generation number ascending. */
1103 QSORT(commits, tips_nr, compare_commit_and_index_by_generation);
1104 min_generation = commits[0].generation;
1105
1106 while (bases) {
1107 repo_parse_commit(r, bases->item);
1108 commit_list_insert(bases->item, &stack);
1109 bases = bases->next;
1110 }
1111
1112 while (stack) {
1113 int explored_all_parents = 1;
1114 struct commit_list *p;
1115 struct commit *c = stack->item;
1116 timestamp_t c_gen = commit_graph_generation(c);
1117
1118 /* Does it match any of our tips? */
1119 for (size_t j = min_generation_index; j < tips_nr; j++) {
1120 if (c_gen < commits[j].generation)
1121 break;
1122
1123 if (commits[j].commit == c) {
1124 tips[commits[j].index]->object.flags |= mark;
1125
1126 if (j == min_generation_index) {
1127 unsigned int k = j + 1;
1128 while (k < tips_nr &&
1129 (tips[commits[k].index]->object.flags & mark))
1130 k++;
1131
1132 /* Terminate early if all found. */
1133 if (k >= tips_nr)
1134 goto done;
1135
1136 min_generation_index = k;
1137 min_generation = commits[k].generation;
1138 }
1139 }
1140 }
1141
1142 for (p = c->parents; p; p = p->next) {
1143 repo_parse_commit(r, p->item);
1144
1145 /* Have we already explored this parent? */
1146 if (p->item->object.flags & SEEN)
1147 continue;
1148
1149 /* Is it below the current minimum generation? */
1150 if (commit_graph_generation(p->item) < min_generation)
1151 continue;
1152
1153 /* Ok, we will explore from here on. */
1154 p->item->object.flags |= SEEN;
1155 explored_all_parents = 0;
1156 commit_list_insert(p->item, &stack);
1157 break;
1158 }
1159
1160 if (explored_all_parents)
1161 pop_commit(&stack);
1162 }
1163
1164done:
1165 free(commits);
1166 repo_clear_commit_marks(r, SEEN);
1167}