3 #include "commit-graph.h"
5 #include "prio-queue.h"
7 #include "ref-filter.h"
10 #include "commit-reach.h"
11 #include "ewah/ewok.h"
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)
19 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
21 static int compare_commits_by_gen(const void *_a
, const void *_b
)
23 const struct commit
*a
= *(const struct commit
* const *)_a
;
24 const struct commit
*b
= *(const struct commit
* const *)_b
;
26 timestamp_t generation_a
= commit_graph_generation(a
);
27 timestamp_t generation_b
= commit_graph_generation(b
);
29 if (generation_a
< generation_b
)
31 if (generation_a
> generation_b
)
33 if (a
->date
< b
->date
)
35 if (a
->date
> b
->date
)
40 static int queue_has_nonstale(struct prio_queue
*queue
)
43 for (i
= 0; i
< queue
->nr
; i
++) {
44 struct commit
*commit
= queue
->array
[i
].data
;
45 if (!(commit
->object
.flags
& STALE
))
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
,
55 timestamp_t min_generation
)
57 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
58 struct commit_list
*result
= NULL
;
60 timestamp_t last_gen
= GENERATION_NUMBER_INFINITY
;
62 if (!min_generation
&& !corrected_commit_dates_enabled(r
))
63 queue
.compare
= compare_commits_by_commit_date
;
65 one
->object
.flags
|= PARENT1
;
67 commit_list_append(one
, &result
);
70 prio_queue_put(&queue
, one
);
72 for (i
= 0; i
< n
; i
++) {
73 twos
[i
]->object
.flags
|= PARENT2
;
74 prio_queue_put(&queue
, twos
[i
]);
77 while (queue_has_nonstale(&queue
)) {
78 struct commit
*commit
= prio_queue_get(&queue
);
79 struct commit_list
*parents
;
81 timestamp_t generation
= commit_graph_generation(commit
);
83 if (min_generation
&& generation
> last_gen
)
84 BUG("bad generation skip %"PRItime
" > %"PRItime
" at %s",
86 oid_to_hex(&commit
->object
.oid
));
87 last_gen
= generation
;
89 if (generation
< min_generation
)
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
);
98 /* Mark parents of a found merge stale */
101 parents
= commit
->parents
;
103 struct commit
*p
= parents
->item
;
104 parents
= parents
->next
;
105 if ((p
->object
.flags
& flags
) == flags
)
107 if (repo_parse_commit(r
, p
))
109 p
->object
.flags
|= flags
;
110 prio_queue_put(&queue
, p
);
114 clear_prio_queue(&queue
);
118 static struct commit_list
*merge_bases_many(struct repository
*r
,
119 struct commit
*one
, int n
,
120 struct commit
**twos
)
122 struct commit_list
*list
= NULL
;
123 struct commit_list
*result
= NULL
;
126 for (i
= 0; i
< n
; i
++) {
129 * We do not mark this even with RESULT so we do not
130 * have to clean it up.
132 return commit_list_insert(one
, &result
);
135 if (repo_parse_commit(r
, one
))
137 for (i
= 0; i
< n
; i
++) {
138 if (repo_parse_commit(r
, twos
[i
]))
142 list
= paint_down_to_common(r
, one
, n
, twos
, 0);
145 struct commit
*commit
= pop_commit(&list
);
146 if (!(commit
->object
.flags
& STALE
))
147 commit_list_insert_by_date(commit
, &result
);
152 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
154 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
159 commit_list_insert(in
->item
, &ret
);
161 for (i
= in
->next
; i
; i
= i
->next
) {
162 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
164 for (j
= ret
; j
; j
= j
->next
) {
165 struct commit_list
*bases
;
166 bases
= get_merge_bases(i
->item
, j
->item
);
171 for (k
= bases
; k
; k
= k
->next
)
179 static int remove_redundant_no_gen(struct repository
*r
,
180 struct commit
**array
, int cnt
)
182 struct commit
**work
;
183 unsigned char *redundant
;
187 CALLOC_ARRAY(work
, cnt
);
188 redundant
= xcalloc(cnt
, 1);
189 ALLOC_ARRAY(filled_index
, cnt
- 1);
191 for (i
= 0; i
< cnt
; i
++)
192 repo_parse_commit(r
, array
[i
]);
193 for (i
= 0; i
< cnt
; i
++) {
194 struct commit_list
*common
;
195 timestamp_t min_generation
= commit_graph_generation(array
[i
]);
199 for (j
= filled
= 0; j
< cnt
; j
++) {
200 timestamp_t curr_generation
;
201 if (i
== j
|| redundant
[j
])
203 filled_index
[filled
] = j
;
204 work
[filled
++] = array
[j
];
206 curr_generation
= commit_graph_generation(array
[j
]);
207 if (curr_generation
< min_generation
)
208 min_generation
= curr_generation
;
210 common
= paint_down_to_common(r
, array
[i
], filled
,
211 work
, min_generation
);
212 if (array
[i
]->object
.flags
& PARENT2
)
214 for (j
= 0; j
< filled
; j
++)
215 if (work
[j
]->object
.flags
& PARENT1
)
216 redundant
[filled_index
[j
]] = 1;
217 clear_commit_marks(array
[i
], all_flags
);
218 clear_commit_marks_many(filled
, work
, all_flags
);
219 free_commit_list(common
);
222 /* Now collect the result */
223 COPY_ARRAY(work
, array
, cnt
);
224 for (i
= filled
= 0; i
< cnt
; i
++)
226 array
[filled
++] = work
[i
];
233 static int remove_redundant_with_gen(struct repository
*r
,
234 struct commit
**array
, int cnt
)
236 int i
, count_non_stale
= 0, count_still_independent
= cnt
;
237 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
238 struct commit
**walk_start
, **sorted
;
239 size_t walk_start_nr
= 0, walk_start_alloc
= cnt
;
243 * Sort the input by generation number, ascending. This allows
244 * us to increase the "min_generation" limit when we discover
245 * the commit with lowest generation is STALE. The index
246 * min_gen_pos points to the current position within 'array'
247 * that is not yet known to be STALE.
249 DUP_ARRAY(sorted
, array
, cnt
);
250 QSORT(sorted
, cnt
, compare_commits_by_gen
);
251 min_generation
= commit_graph_generation(sorted
[0]);
253 ALLOC_ARRAY(walk_start
, walk_start_alloc
);
255 /* Mark all parents of the input as STALE */
256 for (i
= 0; i
< cnt
; i
++) {
257 struct commit_list
*parents
;
259 repo_parse_commit(r
, array
[i
]);
260 array
[i
]->object
.flags
|= RESULT
;
261 parents
= array
[i
]->parents
;
264 repo_parse_commit(r
, parents
->item
);
265 if (!(parents
->item
->object
.flags
& STALE
)) {
266 parents
->item
->object
.flags
|= STALE
;
267 ALLOC_GROW(walk_start
, walk_start_nr
+ 1, walk_start_alloc
);
268 walk_start
[walk_start_nr
++] = parents
->item
;
270 parents
= parents
->next
;
274 QSORT(walk_start
, walk_start_nr
, compare_commits_by_gen
);
276 /* remove STALE bit for now to allow walking through parents */
277 for (i
= 0; i
< walk_start_nr
; i
++)
278 walk_start
[i
]->object
.flags
&= ~STALE
;
281 * Start walking from the highest generation. Hopefully, it will
282 * find all other items during the first-parent walk, and we can
283 * terminate early. Otherwise, we will do the same amount of work
286 for (i
= walk_start_nr
- 1; i
>= 0 && count_still_independent
> 1; i
--) {
287 /* push the STALE bits up to min generation */
288 struct commit_list
*stack
= NULL
;
290 commit_list_insert(walk_start
[i
], &stack
);
291 walk_start
[i
]->object
.flags
|= STALE
;
294 struct commit_list
*parents
;
295 struct commit
*c
= stack
->item
;
297 repo_parse_commit(r
, c
);
299 if (c
->object
.flags
& RESULT
) {
300 c
->object
.flags
&= ~RESULT
;
301 if (--count_still_independent
<= 1)
303 if (oideq(&c
->object
.oid
, &sorted
[min_gen_pos
]->object
.oid
)) {
304 while (min_gen_pos
< cnt
- 1 &&
305 (sorted
[min_gen_pos
]->object
.flags
& STALE
))
307 min_generation
= commit_graph_generation(sorted
[min_gen_pos
]);
311 if (commit_graph_generation(c
) < min_generation
) {
316 parents
= c
->parents
;
318 if (!(parents
->item
->object
.flags
& STALE
)) {
319 parents
->item
->object
.flags
|= STALE
;
320 commit_list_insert(parents
->item
, &stack
);
323 parents
= parents
->next
;
326 /* pop if all parents have been visited already */
330 free_commit_list(stack
);
335 for (i
= 0; i
< cnt
; i
++)
336 array
[i
]->object
.flags
&= ~RESULT
;
338 /* rearrange array */
339 for (i
= count_non_stale
= 0; i
< cnt
; i
++) {
340 if (!(array
[i
]->object
.flags
& STALE
))
341 array
[count_non_stale
++] = array
[i
];
345 clear_commit_marks_many(walk_start_nr
, walk_start
, STALE
);
348 return count_non_stale
;
351 static int remove_redundant(struct repository
*r
, struct commit
**array
, int cnt
)
354 * Some commit in the array may be an ancestor of
355 * another commit. Move the independent commits to the
356 * beginning of 'array' and return their number. Callers
357 * should not rely upon the contents of 'array' after
360 if (generation_numbers_enabled(r
)) {
364 * If we have a single commit with finite generation
365 * number, then the _with_gen algorithm is preferred.
367 for (i
= 0; i
< cnt
; i
++) {
368 if (commit_graph_generation(array
[i
]) < GENERATION_NUMBER_INFINITY
)
369 return remove_redundant_with_gen(r
, array
, cnt
);
373 return remove_redundant_no_gen(r
, array
, cnt
);
376 static struct commit_list
*get_merge_bases_many_0(struct repository
*r
,
379 struct commit
**twos
,
382 struct commit_list
*list
;
383 struct commit
**rslt
;
384 struct commit_list
*result
;
387 result
= merge_bases_many(r
, one
, n
, twos
);
388 for (i
= 0; i
< n
; i
++) {
392 if (!result
|| !result
->next
) {
394 clear_commit_marks(one
, all_flags
);
395 clear_commit_marks_many(n
, twos
, all_flags
);
400 /* There are more than one */
401 cnt
= commit_list_count(result
);
402 CALLOC_ARRAY(rslt
, cnt
);
403 for (list
= result
, i
= 0; list
; list
= list
->next
)
404 rslt
[i
++] = list
->item
;
405 free_commit_list(result
);
407 clear_commit_marks(one
, all_flags
);
408 clear_commit_marks_many(n
, twos
, all_flags
);
410 cnt
= remove_redundant(r
, rslt
, cnt
);
412 for (i
= 0; i
< cnt
; i
++)
413 commit_list_insert_by_date(rslt
[i
], &result
);
418 struct commit_list
*repo_get_merge_bases_many(struct repository
*r
,
421 struct commit
**twos
)
423 return get_merge_bases_many_0(r
, one
, n
, twos
, 1);
426 struct commit_list
*repo_get_merge_bases_many_dirty(struct repository
*r
,
429 struct commit
**twos
)
431 return get_merge_bases_many_0(r
, one
, n
, twos
, 0);
434 struct commit_list
*repo_get_merge_bases(struct repository
*r
,
438 return get_merge_bases_many_0(r
, one
, 1, &two
, 1);
442 * Is "commit" a descendant of one of the elements on the "with_commit" list?
444 int repo_is_descendant_of(struct repository
*r
,
445 struct commit
*commit
,
446 struct commit_list
*with_commit
)
451 if (generation_numbers_enabled(the_repository
)) {
452 struct commit_list
*from_list
= NULL
;
454 commit_list_insert(commit
, &from_list
);
455 result
= can_all_from_reach(from_list
, with_commit
, 0);
456 free_commit_list(from_list
);
459 while (with_commit
) {
460 struct commit
*other
;
462 other
= with_commit
->item
;
463 with_commit
= with_commit
->next
;
464 if (repo_in_merge_bases_many(r
, other
, 1, &commit
))
472 * Is "commit" an ancestor of one of the "references"?
474 int repo_in_merge_bases_many(struct repository
*r
, struct commit
*commit
,
475 int nr_reference
, struct commit
**reference
)
477 struct commit_list
*bases
;
479 timestamp_t generation
, max_generation
= GENERATION_NUMBER_ZERO
;
481 if (repo_parse_commit(r
, commit
))
483 for (i
= 0; i
< nr_reference
; i
++) {
484 if (repo_parse_commit(r
, reference
[i
]))
487 generation
= commit_graph_generation(reference
[i
]);
488 if (generation
> max_generation
)
489 max_generation
= generation
;
492 generation
= commit_graph_generation(commit
);
493 if (generation
> max_generation
)
496 bases
= paint_down_to_common(r
, commit
,
497 nr_reference
, reference
,
499 if (commit
->object
.flags
& PARENT2
)
501 clear_commit_marks(commit
, all_flags
);
502 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
503 free_commit_list(bases
);
508 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
510 int repo_in_merge_bases(struct repository
*r
,
511 struct commit
*commit
,
512 struct commit
*reference
)
515 struct commit_list
*list
= NULL
;
516 struct commit_list
**next
= &list
;
518 next
= commit_list_append(commit
, next
);
519 res
= repo_is_descendant_of(r
, reference
, list
);
520 free_commit_list(list
);
525 struct commit_list
*reduce_heads(struct commit_list
*heads
)
527 struct commit_list
*p
;
528 struct commit_list
*result
= NULL
, **tail
= &result
;
529 struct commit
**array
;
536 for (p
= heads
; p
; p
= p
->next
)
537 p
->item
->object
.flags
&= ~STALE
;
538 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
539 if (p
->item
->object
.flags
& STALE
)
541 p
->item
->object
.flags
|= STALE
;
544 CALLOC_ARRAY(array
, num_head
);
545 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
546 if (p
->item
->object
.flags
& STALE
) {
547 array
[i
++] = p
->item
;
548 p
->item
->object
.flags
&= ~STALE
;
551 num_head
= remove_redundant(the_repository
, array
, num_head
);
552 for (i
= 0; i
< num_head
; i
++)
553 tail
= &commit_list_insert(array
[i
], tail
)->next
;
558 void reduce_heads_replace(struct commit_list
**heads
)
560 struct commit_list
*result
= reduce_heads(*heads
);
561 free_commit_list(*heads
);
565 int ref_newer(const struct object_id
*new_oid
, const struct object_id
*old_oid
)
568 struct commit
*old_commit
, *new_commit
;
569 struct commit_list
*old_commit_list
= NULL
;
573 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
574 * old_commit. Otherwise we require --force.
576 o
= deref_tag(the_repository
, parse_object(the_repository
, old_oid
),
578 if (!o
|| o
->type
!= OBJ_COMMIT
)
580 old_commit
= (struct commit
*) o
;
582 o
= deref_tag(the_repository
, parse_object(the_repository
, new_oid
),
584 if (!o
|| o
->type
!= OBJ_COMMIT
)
586 new_commit
= (struct commit
*) o
;
588 if (parse_commit(new_commit
) < 0)
591 commit_list_insert(old_commit
, &old_commit_list
);
592 ret
= repo_is_descendant_of(the_repository
,
593 new_commit
, old_commit_list
);
594 free_commit_list(old_commit_list
);
599 * Mimicking the real stack, this stack lives on the heap, avoiding stack
602 * At each recursion step, the stack items points to the commits whose
603 * ancestors are to be inspected.
605 struct contains_stack
{
607 struct contains_stack_entry
{
608 struct commit
*commit
;
609 struct commit_list
*parents
;
613 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
615 for (; want
; want
= want
->next
)
616 if (oideq(&want
->item
->object
.oid
, &c
->object
.oid
))
622 * Test whether the candidate is contained in the list.
623 * Do not recurse to find out, though, but return -1 if inconclusive.
625 static enum contains_result
contains_test(struct commit
*candidate
,
626 const struct commit_list
*want
,
627 struct contains_cache
*cache
,
630 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
632 /* If we already have the answer cached, return that. */
637 if (in_commit_list(want
, candidate
)) {
638 *cached
= CONTAINS_YES
;
642 /* Otherwise, we don't know; prepare to recurse */
643 parse_commit_or_die(candidate
);
645 if (commit_graph_generation(candidate
) < cutoff
)
648 return CONTAINS_UNKNOWN
;
651 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
653 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
654 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
655 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
658 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
659 const struct commit_list
*want
,
660 struct contains_cache
*cache
)
662 struct contains_stack contains_stack
= { 0, 0, NULL
};
663 enum contains_result result
;
664 timestamp_t cutoff
= GENERATION_NUMBER_INFINITY
;
665 const struct commit_list
*p
;
667 for (p
= want
; p
; p
= p
->next
) {
668 timestamp_t generation
;
669 struct commit
*c
= p
->item
;
670 load_commit_graph_info(the_repository
, c
);
671 generation
= commit_graph_generation(c
);
672 if (generation
< cutoff
)
676 result
= contains_test(candidate
, want
, cache
, cutoff
);
677 if (result
!= CONTAINS_UNKNOWN
)
680 push_to_contains_stack(candidate
, &contains_stack
);
681 while (contains_stack
.nr
) {
682 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
683 struct commit
*commit
= entry
->commit
;
684 struct commit_list
*parents
= entry
->parents
;
687 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
691 * If we just popped the stack, parents->item has been marked,
692 * therefore contains_test will return a meaningful yes/no.
694 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
696 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
700 entry
->parents
= parents
->next
;
702 case CONTAINS_UNKNOWN
:
703 push_to_contains_stack(parents
->item
, &contains_stack
);
707 free(contains_stack
.contains_stack
);
708 return contains_test(candidate
, want
, cache
, cutoff
);
711 int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
712 struct commit_list
*list
, struct contains_cache
*cache
)
714 if (filter
->with_commit_tag_algo
)
715 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
716 return repo_is_descendant_of(the_repository
, commit
, list
);
719 int can_all_from_reach_with_flag(struct object_array
*from
,
720 unsigned int with_flag
,
721 unsigned int assign_flag
,
722 time_t min_commit_date
,
723 timestamp_t min_generation
)
725 struct commit
**list
= NULL
;
730 ALLOC_ARRAY(list
, from
->nr
);
732 for (i
= 0; i
< from
->nr
; i
++) {
733 struct object
*from_one
= from
->objects
[i
].item
;
735 if (!from_one
|| from_one
->flags
& assign_flag
)
738 from_one
= deref_tag(the_repository
, from_one
,
740 if (!from_one
|| from_one
->type
!= OBJ_COMMIT
) {
742 * no way to tell if this is reachable by
743 * looking at the ancestry chain alone, so
744 * leave a note to ourselves not to worry about
745 * this object anymore.
747 from
->objects
[i
].item
->flags
|= assign_flag
;
751 list
[nr_commits
] = (struct commit
*)from_one
;
752 if (parse_commit(list
[nr_commits
]) ||
753 commit_graph_generation(list
[nr_commits
]) < min_generation
) {
761 QSORT(list
, nr_commits
, compare_commits_by_gen
);
763 for (i
= 0; i
< nr_commits
; i
++) {
764 /* DFS from list[i] */
765 struct commit_list
*stack
= NULL
;
767 list
[i
]->object
.flags
|= assign_flag
;
768 commit_list_insert(list
[i
], &stack
);
771 struct commit_list
*parent
;
773 if (stack
->item
->object
.flags
& (with_flag
| RESULT
)) {
776 stack
->item
->object
.flags
|= RESULT
;
780 for (parent
= stack
->item
->parents
; parent
; parent
= parent
->next
) {
781 if (parent
->item
->object
.flags
& (with_flag
| RESULT
))
782 stack
->item
->object
.flags
|= RESULT
;
784 if (!(parent
->item
->object
.flags
& assign_flag
)) {
785 parent
->item
->object
.flags
|= assign_flag
;
787 if (parse_commit(parent
->item
) ||
788 parent
->item
->date
< min_commit_date
||
789 commit_graph_generation(parent
->item
) < min_generation
)
792 commit_list_insert(parent
->item
, &stack
);
801 if (!(list
[i
]->object
.flags
& (with_flag
| RESULT
))) {
808 clear_commit_marks_many(nr_commits
, list
, RESULT
| assign_flag
);
811 for (i
= 0; i
< from
->nr
; i
++)
812 from
->objects
[i
].item
->flags
&= ~assign_flag
;
817 int can_all_from_reach(struct commit_list
*from
, struct commit_list
*to
,
818 int cutoff_by_min_date
)
820 struct object_array from_objs
= OBJECT_ARRAY_INIT
;
821 time_t min_commit_date
= cutoff_by_min_date
? from
->item
->date
: 0;
822 struct commit_list
*from_iter
= from
, *to_iter
= to
;
824 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
827 add_object_array(&from_iter
->item
->object
, NULL
, &from_objs
);
829 if (!parse_commit(from_iter
->item
)) {
830 timestamp_t generation
;
831 if (from_iter
->item
->date
< min_commit_date
)
832 min_commit_date
= from_iter
->item
->date
;
834 generation
= commit_graph_generation(from_iter
->item
);
835 if (generation
< min_generation
)
836 min_generation
= generation
;
839 from_iter
= from_iter
->next
;
843 if (!parse_commit(to_iter
->item
)) {
844 timestamp_t generation
;
845 if (to_iter
->item
->date
< min_commit_date
)
846 min_commit_date
= to_iter
->item
->date
;
848 generation
= commit_graph_generation(to_iter
->item
);
849 if (generation
< min_generation
)
850 min_generation
= generation
;
853 to_iter
->item
->object
.flags
|= PARENT2
;
855 to_iter
= to_iter
->next
;
858 result
= can_all_from_reach_with_flag(&from_objs
, PARENT2
, PARENT1
,
859 min_commit_date
, min_generation
);
862 clear_commit_marks(from
->item
, PARENT1
);
867 clear_commit_marks(to
->item
, PARENT2
);
871 object_array_clear(&from_objs
);
875 struct commit_list
*get_reachable_subset(struct commit
**from
, int nr_from
,
876 struct commit
**to
, int nr_to
,
877 unsigned int reachable_flag
)
879 struct commit
**item
;
880 struct commit
*current
;
881 struct commit_list
*found_commits
= NULL
;
882 struct commit
**to_last
= to
+ nr_to
;
883 struct commit
**from_last
= from
+ nr_from
;
884 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
887 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
889 for (item
= to
; item
< to_last
; item
++) {
890 timestamp_t generation
;
891 struct commit
*c
= *item
;
894 generation
= commit_graph_generation(c
);
895 if (generation
< min_generation
)
896 min_generation
= generation
;
898 if (!(c
->object
.flags
& PARENT1
)) {
899 c
->object
.flags
|= PARENT1
;
904 for (item
= from
; item
< from_last
; item
++) {
905 struct commit
*c
= *item
;
906 if (!(c
->object
.flags
& PARENT2
)) {
907 c
->object
.flags
|= PARENT2
;
910 prio_queue_put(&queue
, *item
);
914 while (num_to_find
&& (current
= prio_queue_get(&queue
)) != NULL
) {
915 struct commit_list
*parents
;
917 if (current
->object
.flags
& PARENT1
) {
918 current
->object
.flags
&= ~PARENT1
;
919 current
->object
.flags
|= reachable_flag
;
920 commit_list_insert(current
, &found_commits
);
924 for (parents
= current
->parents
; parents
; parents
= parents
->next
) {
925 struct commit
*p
= parents
->item
;
929 if (commit_graph_generation(p
) < min_generation
)
932 if (p
->object
.flags
& PARENT2
)
935 p
->object
.flags
|= PARENT2
;
936 prio_queue_put(&queue
, p
);
940 clear_commit_marks_many(nr_to
, to
, PARENT1
);
941 clear_commit_marks_many(nr_from
, from
, PARENT2
);
943 return found_commits
;
946 define_commit_slab(bit_arrays
, struct bitmap
*);
947 static struct bit_arrays bit_arrays
;
949 static void insert_no_dup(struct prio_queue
*queue
, struct commit
*c
)
951 if (c
->object
.flags
& PARENT2
)
953 prio_queue_put(queue
, c
);
954 c
->object
.flags
|= PARENT2
;
957 static struct bitmap
*get_bit_array(struct commit
*c
, int width
)
959 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
961 *bitmap
= bitmap_word_alloc(width
);
965 static void free_bit_array(struct commit
*c
)
967 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
970 bitmap_free(*bitmap
);
974 void ahead_behind(struct repository
*r
,
975 struct commit
**commits
, size_t commits_nr
,
976 struct ahead_behind_count
*counts
, size_t counts_nr
)
978 struct prio_queue queue
= { .compare
= compare_commits_by_gen_then_commit_date
};
979 size_t width
= DIV_ROUND_UP(commits_nr
, BITS_IN_EWORD
);
981 if (!commits_nr
|| !counts_nr
)
984 for (size_t i
= 0; i
< counts_nr
; i
++) {
986 counts
[i
].behind
= 0;
989 ensure_generations_valid(r
, commits
, commits_nr
);
991 init_bit_arrays(&bit_arrays
);
993 for (size_t i
= 0; i
< commits_nr
; i
++) {
994 struct commit
*c
= commits
[i
];
995 struct bitmap
*bitmap
= get_bit_array(c
, width
);
997 bitmap_set(bitmap
, i
);
998 insert_no_dup(&queue
, c
);
1001 while (queue_has_nonstale(&queue
)) {
1002 struct commit
*c
= prio_queue_get(&queue
);
1003 struct commit_list
*p
;
1004 struct bitmap
*bitmap_c
= get_bit_array(c
, width
);
1006 for (size_t i
= 0; i
< counts_nr
; i
++) {
1007 int reach_from_tip
= !!bitmap_get(bitmap_c
, counts
[i
].tip_index
);
1008 int reach_from_base
= !!bitmap_get(bitmap_c
, counts
[i
].base_index
);
1010 if (reach_from_tip
^ reach_from_base
) {
1011 if (reach_from_base
)
1018 for (p
= c
->parents
; p
; p
= p
->next
) {
1019 struct bitmap
*bitmap_p
;
1021 repo_parse_commit(r
, p
->item
);
1023 bitmap_p
= get_bit_array(p
->item
, width
);
1024 bitmap_or(bitmap_p
, bitmap_c
);
1027 * If this parent is reachable from every starting
1028 * commit, then none of its ancestors can contribute
1029 * to the ahead/behind count. Mark it as STALE, so
1030 * we can stop the walk when every commit in the
1033 if (bitmap_popcount(bitmap_p
) == commits_nr
)
1034 p
->item
->object
.flags
|= STALE
;
1036 insert_no_dup(&queue
, p
->item
);
1042 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1043 repo_clear_commit_marks(r
, PARENT2
| STALE
);
1044 clear_bit_arrays(&bit_arrays
);
1045 clear_prio_queue(&queue
);
1048 struct commit_and_index
{
1049 struct commit
*commit
;
1051 timestamp_t generation
;
1054 static int compare_commit_and_index_by_generation(const void *va
, const void *vb
)
1056 const struct commit_and_index
*a
= (const struct commit_and_index
*)va
;
1057 const struct commit_and_index
*b
= (const struct commit_and_index
*)vb
;
1059 if (a
->generation
> b
->generation
)
1061 if (a
->generation
< b
->generation
)
1066 void tips_reachable_from_bases(struct repository
*r
,
1067 struct commit_list
*bases
,
1068 struct commit
**tips
, size_t tips_nr
,
1071 struct commit_and_index
*commits
;
1072 size_t min_generation_index
= 0;
1073 timestamp_t min_generation
;
1074 struct commit_list
*stack
= NULL
;
1076 if (!bases
|| !tips
|| !tips_nr
)
1080 * Do a depth-first search starting at 'bases' to search for the
1081 * tips. Stop at the lowest (un-found) generation number. When
1082 * finding the lowest commit, increase the minimum generation
1083 * number to the next lowest (un-found) generation number.
1086 CALLOC_ARRAY(commits
, tips_nr
);
1088 for (size_t i
= 0; i
< tips_nr
; i
++) {
1089 commits
[i
].commit
= tips
[i
];
1090 commits
[i
].index
= i
;
1091 commits
[i
].generation
= commit_graph_generation(tips
[i
]);
1094 /* Sort with generation number ascending. */
1095 QSORT(commits
, tips_nr
, compare_commit_and_index_by_generation
);
1096 min_generation
= commits
[0].generation
;
1099 repo_parse_commit(r
, bases
->item
);
1100 commit_list_insert(bases
->item
, &stack
);
1101 bases
= bases
->next
;
1105 int explored_all_parents
= 1;
1106 struct commit_list
*p
;
1107 struct commit
*c
= stack
->item
;
1108 timestamp_t c_gen
= commit_graph_generation(c
);
1110 /* Does it match any of our tips? */
1111 for (size_t j
= min_generation_index
; j
< tips_nr
; j
++) {
1112 if (c_gen
< commits
[j
].generation
)
1115 if (commits
[j
].commit
== c
) {
1116 tips
[commits
[j
].index
]->object
.flags
|= mark
;
1118 if (j
== min_generation_index
) {
1119 unsigned int k
= j
+ 1;
1120 while (k
< tips_nr
&&
1121 (tips
[commits
[k
].index
]->object
.flags
& mark
))
1124 /* Terminate early if all found. */
1128 min_generation_index
= k
;
1129 min_generation
= commits
[k
].generation
;
1134 for (p
= c
->parents
; p
; p
= p
->next
) {
1135 repo_parse_commit(r
, p
->item
);
1137 /* Have we already explored this parent? */
1138 if (p
->item
->object
.flags
& SEEN
)
1141 /* Is it below the current minimum generation? */
1142 if (commit_graph_generation(p
->item
) < min_generation
)
1145 /* Ok, we will explore from here on. */
1146 p
->item
->object
.flags
|= SEEN
;
1147 explored_all_parents
= 0;
1148 commit_list_insert(p
->item
, &stack
);
1152 if (explored_all_parents
)
1158 repo_clear_commit_marks(r
, SEEN
);