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