1 #include "git-compat-util.h"
3 #include "commit-graph.h"
6 #include "prio-queue.h"
8 #include "ref-filter.h"
11 #include "commit-reach.h"
12 #include "ewah/ewok.h"
14 /* Remember to update object flag allocation in object.h */
15 #define PARENT1 (1u<<16)
16 #define PARENT2 (1u<<17)
17 #define STALE (1u<<18)
18 #define RESULT (1u<<19)
20 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
22 static int compare_commits_by_gen(const void *_a
, const void *_b
)
24 const struct commit
*a
= *(const struct commit
* const *)_a
;
25 const struct commit
*b
= *(const struct commit
* const *)_b
;
27 timestamp_t generation_a
= commit_graph_generation(a
);
28 timestamp_t generation_b
= commit_graph_generation(b
);
30 if (generation_a
< generation_b
)
32 if (generation_a
> generation_b
)
34 if (a
->date
< b
->date
)
36 if (a
->date
> b
->date
)
41 static int queue_has_nonstale(struct prio_queue
*queue
)
44 for (i
= 0; i
< queue
->nr
; i
++) {
45 struct commit
*commit
= queue
->array
[i
].data
;
46 if (!(commit
->object
.flags
& STALE
))
52 /* all input commits in one and twos[] must have been parsed! */
53 static struct commit_list
*paint_down_to_common(struct repository
*r
,
54 struct commit
*one
, int n
,
56 timestamp_t min_generation
)
58 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
59 struct commit_list
*result
= NULL
;
61 timestamp_t last_gen
= GENERATION_NUMBER_INFINITY
;
63 if (!min_generation
&& !corrected_commit_dates_enabled(r
))
64 queue
.compare
= compare_commits_by_commit_date
;
66 one
->object
.flags
|= PARENT1
;
68 commit_list_append(one
, &result
);
71 prio_queue_put(&queue
, one
);
73 for (i
= 0; i
< n
; i
++) {
74 twos
[i
]->object
.flags
|= PARENT2
;
75 prio_queue_put(&queue
, twos
[i
]);
78 while (queue_has_nonstale(&queue
)) {
79 struct commit
*commit
= prio_queue_get(&queue
);
80 struct commit_list
*parents
;
82 timestamp_t generation
= commit_graph_generation(commit
);
84 if (min_generation
&& generation
> last_gen
)
85 BUG("bad generation skip %"PRItime
" > %"PRItime
" at %s",
87 oid_to_hex(&commit
->object
.oid
));
88 last_gen
= generation
;
90 if (generation
< min_generation
)
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
);
99 /* Mark parents of a found merge stale */
102 parents
= commit
->parents
;
104 struct commit
*p
= parents
->item
;
105 parents
= parents
->next
;
106 if ((p
->object
.flags
& flags
) == flags
)
108 if (repo_parse_commit(r
, p
))
110 p
->object
.flags
|= flags
;
111 prio_queue_put(&queue
, p
);
115 clear_prio_queue(&queue
);
119 static struct commit_list
*merge_bases_many(struct repository
*r
,
120 struct commit
*one
, int n
,
121 struct commit
**twos
)
123 struct commit_list
*list
= NULL
;
124 struct commit_list
*result
= NULL
;
127 for (i
= 0; i
< n
; i
++) {
130 * We do not mark this even with RESULT so we do not
131 * have to clean it up.
133 return commit_list_insert(one
, &result
);
136 if (repo_parse_commit(r
, one
))
138 for (i
= 0; i
< n
; i
++) {
139 if (repo_parse_commit(r
, twos
[i
]))
143 list
= paint_down_to_common(r
, one
, n
, twos
, 0);
146 struct commit
*commit
= pop_commit(&list
);
147 if (!(commit
->object
.flags
& STALE
))
148 commit_list_insert_by_date(commit
, &result
);
153 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
155 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
160 commit_list_insert(in
->item
, &ret
);
162 for (i
= in
->next
; i
; i
= i
->next
) {
163 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
165 for (j
= ret
; j
; j
= j
->next
) {
166 struct commit_list
*bases
;
167 bases
= repo_get_merge_bases(the_repository
, i
->item
,
173 for (k
= bases
; k
; k
= k
->next
)
181 static int remove_redundant_no_gen(struct repository
*r
,
182 struct commit
**array
, int cnt
)
184 struct commit
**work
;
185 unsigned char *redundant
;
189 CALLOC_ARRAY(work
, cnt
);
190 redundant
= xcalloc(cnt
, 1);
191 ALLOC_ARRAY(filled_index
, cnt
- 1);
193 for (i
= 0; i
< cnt
; i
++)
194 repo_parse_commit(r
, array
[i
]);
195 for (i
= 0; i
< cnt
; i
++) {
196 struct commit_list
*common
;
197 timestamp_t min_generation
= commit_graph_generation(array
[i
]);
201 for (j
= filled
= 0; j
< cnt
; j
++) {
202 timestamp_t curr_generation
;
203 if (i
== j
|| redundant
[j
])
205 filled_index
[filled
] = j
;
206 work
[filled
++] = array
[j
];
208 curr_generation
= commit_graph_generation(array
[j
]);
209 if (curr_generation
< min_generation
)
210 min_generation
= curr_generation
;
212 common
= paint_down_to_common(r
, array
[i
], filled
,
213 work
, min_generation
);
214 if (array
[i
]->object
.flags
& PARENT2
)
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
);
224 /* Now collect the result */
225 COPY_ARRAY(work
, array
, cnt
);
226 for (i
= filled
= 0; i
< cnt
; i
++)
228 array
[filled
++] = work
[i
];
235 static int remove_redundant_with_gen(struct repository
*r
,
236 struct commit
**array
, int cnt
)
238 int i
, count_non_stale
= 0, count_still_independent
= cnt
;
239 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
240 struct commit
**walk_start
, **sorted
;
241 size_t walk_start_nr
= 0, walk_start_alloc
= cnt
;
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.
251 DUP_ARRAY(sorted
, array
, cnt
);
252 QSORT(sorted
, cnt
, compare_commits_by_gen
);
253 min_generation
= commit_graph_generation(sorted
[0]);
255 ALLOC_ARRAY(walk_start
, walk_start_alloc
);
257 /* Mark all parents of the input as STALE */
258 for (i
= 0; i
< cnt
; i
++) {
259 struct commit_list
*parents
;
261 repo_parse_commit(r
, array
[i
]);
262 array
[i
]->object
.flags
|= RESULT
;
263 parents
= array
[i
]->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
;
272 parents
= parents
->next
;
276 QSORT(walk_start
, walk_start_nr
, compare_commits_by_gen
);
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
;
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
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
;
292 commit_list_insert(walk_start
[i
], &stack
);
293 walk_start
[i
]->object
.flags
|= STALE
;
296 struct commit_list
*parents
;
297 struct commit
*c
= stack
->item
;
299 repo_parse_commit(r
, c
);
301 if (c
->object
.flags
& RESULT
) {
302 c
->object
.flags
&= ~RESULT
;
303 if (--count_still_independent
<= 1)
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
))
309 min_generation
= commit_graph_generation(sorted
[min_gen_pos
]);
313 if (commit_graph_generation(c
) < min_generation
) {
318 parents
= c
->parents
;
320 if (!(parents
->item
->object
.flags
& STALE
)) {
321 parents
->item
->object
.flags
|= STALE
;
322 commit_list_insert(parents
->item
, &stack
);
325 parents
= parents
->next
;
328 /* pop if all parents have been visited already */
332 free_commit_list(stack
);
337 for (i
= 0; i
< cnt
; i
++)
338 array
[i
]->object
.flags
&= ~RESULT
;
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
];
347 clear_commit_marks_many(walk_start_nr
, walk_start
, STALE
);
350 return count_non_stale
;
353 static int remove_redundant(struct repository
*r
, struct commit
**array
, int cnt
)
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
362 if (generation_numbers_enabled(r
)) {
366 * If we have a single commit with finite generation
367 * number, then the _with_gen algorithm is preferred.
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
);
375 return remove_redundant_no_gen(r
, array
, cnt
);
378 static struct commit_list
*get_merge_bases_many_0(struct repository
*r
,
381 struct commit
**twos
,
384 struct commit_list
*list
;
385 struct commit
**rslt
;
386 struct commit_list
*result
;
389 result
= merge_bases_many(r
, one
, n
, twos
);
390 for (i
= 0; i
< n
; i
++) {
394 if (!result
|| !result
->next
) {
396 clear_commit_marks(one
, all_flags
);
397 clear_commit_marks_many(n
, twos
, all_flags
);
402 /* There are more than one */
403 cnt
= commit_list_count(result
);
404 CALLOC_ARRAY(rslt
, cnt
);
405 for (list
= result
, i
= 0; list
; list
= list
->next
)
406 rslt
[i
++] = list
->item
;
407 free_commit_list(result
);
409 clear_commit_marks(one
, all_flags
);
410 clear_commit_marks_many(n
, twos
, all_flags
);
412 cnt
= remove_redundant(r
, rslt
, cnt
);
414 for (i
= 0; i
< cnt
; i
++)
415 commit_list_insert_by_date(rslt
[i
], &result
);
420 struct commit_list
*repo_get_merge_bases_many(struct repository
*r
,
423 struct commit
**twos
)
425 return get_merge_bases_many_0(r
, one
, n
, twos
, 1);
428 struct commit_list
*repo_get_merge_bases_many_dirty(struct repository
*r
,
431 struct commit
**twos
)
433 return get_merge_bases_many_0(r
, one
, n
, twos
, 0);
436 struct commit_list
*repo_get_merge_bases(struct repository
*r
,
440 return get_merge_bases_many_0(r
, one
, 1, &two
, 1);
444 * Is "commit" a descendant of one of the elements on the "with_commit" list?
446 int repo_is_descendant_of(struct repository
*r
,
447 struct commit
*commit
,
448 struct commit_list
*with_commit
)
453 if (generation_numbers_enabled(r
)) {
454 struct commit_list
*from_list
= NULL
;
456 commit_list_insert(commit
, &from_list
);
457 result
= can_all_from_reach(from_list
, with_commit
, 0);
458 free_commit_list(from_list
);
461 while (with_commit
) {
462 struct commit
*other
;
464 other
= with_commit
->item
;
465 with_commit
= with_commit
->next
;
466 if (repo_in_merge_bases_many(r
, other
, 1, &commit
))
474 * Is "commit" an ancestor of one of the "references"?
476 int repo_in_merge_bases_many(struct repository
*r
, struct commit
*commit
,
477 int nr_reference
, struct commit
**reference
)
479 struct commit_list
*bases
;
481 timestamp_t generation
, max_generation
= GENERATION_NUMBER_ZERO
;
483 if (repo_parse_commit(r
, commit
))
485 for (i
= 0; i
< nr_reference
; i
++) {
486 if (repo_parse_commit(r
, reference
[i
]))
489 generation
= commit_graph_generation(reference
[i
]);
490 if (generation
> max_generation
)
491 max_generation
= generation
;
494 generation
= commit_graph_generation(commit
);
495 if (generation
> max_generation
)
498 bases
= paint_down_to_common(r
, commit
,
499 nr_reference
, reference
,
501 if (commit
->object
.flags
& PARENT2
)
503 clear_commit_marks(commit
, all_flags
);
504 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
505 free_commit_list(bases
);
510 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
512 int repo_in_merge_bases(struct repository
*r
,
513 struct commit
*commit
,
514 struct commit
*reference
)
517 struct commit_list
*list
= NULL
;
518 struct commit_list
**next
= &list
;
520 next
= commit_list_append(commit
, next
);
521 res
= repo_is_descendant_of(r
, reference
, list
);
522 free_commit_list(list
);
527 struct commit_list
*reduce_heads(struct commit_list
*heads
)
529 struct commit_list
*p
;
530 struct commit_list
*result
= NULL
, **tail
= &result
;
531 struct commit
**array
;
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
)
543 p
->item
->object
.flags
|= STALE
;
546 CALLOC_ARRAY(array
, num_head
);
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
;
553 num_head
= remove_redundant(the_repository
, array
, num_head
);
554 for (i
= 0; i
< num_head
; i
++)
555 tail
= &commit_list_insert(array
[i
], tail
)->next
;
560 void reduce_heads_replace(struct commit_list
**heads
)
562 struct commit_list
*result
= reduce_heads(*heads
);
563 free_commit_list(*heads
);
567 int ref_newer(const struct object_id
*new_oid
, const struct object_id
*old_oid
)
570 struct commit
*old_commit
, *new_commit
;
571 struct commit_list
*old_commit_list
= NULL
;
575 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
576 * old_commit. Otherwise we require --force.
578 o
= deref_tag(the_repository
, parse_object(the_repository
, old_oid
),
580 if (!o
|| o
->type
!= OBJ_COMMIT
)
582 old_commit
= (struct commit
*) o
;
584 o
= deref_tag(the_repository
, parse_object(the_repository
, new_oid
),
586 if (!o
|| o
->type
!= OBJ_COMMIT
)
588 new_commit
= (struct commit
*) o
;
590 if (repo_parse_commit(the_repository
, new_commit
) < 0)
593 commit_list_insert(old_commit
, &old_commit_list
);
594 ret
= repo_is_descendant_of(the_repository
,
595 new_commit
, old_commit_list
);
596 free_commit_list(old_commit_list
);
601 * Mimicking the real stack, this stack lives on the heap, avoiding stack
604 * At each recursion step, the stack items points to the commits whose
605 * ancestors are to be inspected.
607 struct contains_stack
{
609 struct contains_stack_entry
{
610 struct commit
*commit
;
611 struct commit_list
*parents
;
615 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
617 for (; want
; want
= want
->next
)
618 if (oideq(&want
->item
->object
.oid
, &c
->object
.oid
))
624 * Test whether the candidate is contained in the list.
625 * Do not recurse to find out, though, but return -1 if inconclusive.
627 static enum contains_result
contains_test(struct commit
*candidate
,
628 const struct commit_list
*want
,
629 struct contains_cache
*cache
,
632 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
634 /* If we already have the answer cached, return that. */
639 if (in_commit_list(want
, candidate
)) {
640 *cached
= CONTAINS_YES
;
644 /* Otherwise, we don't know; prepare to recurse */
645 parse_commit_or_die(candidate
);
647 if (commit_graph_generation(candidate
) < cutoff
)
650 return CONTAINS_UNKNOWN
;
653 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
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
;
660 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
661 const struct commit_list
*want
,
662 struct contains_cache
*cache
)
664 struct contains_stack contains_stack
= { 0, 0, NULL
};
665 enum contains_result result
;
666 timestamp_t cutoff
= GENERATION_NUMBER_INFINITY
;
667 const struct commit_list
*p
;
669 for (p
= want
; p
; p
= p
->next
) {
670 timestamp_t generation
;
671 struct commit
*c
= p
->item
;
672 load_commit_graph_info(the_repository
, c
);
673 generation
= commit_graph_generation(c
);
674 if (generation
< cutoff
)
678 result
= contains_test(candidate
, want
, cache
, cutoff
);
679 if (result
!= CONTAINS_UNKNOWN
)
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
;
689 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
693 * If we just popped the stack, parents->item has been marked,
694 * therefore contains_test will return a meaningful yes/no.
696 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
698 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
702 entry
->parents
= parents
->next
;
704 case CONTAINS_UNKNOWN
:
705 push_to_contains_stack(parents
->item
, &contains_stack
);
709 free(contains_stack
.contains_stack
);
710 return contains_test(candidate
, want
, cache
, cutoff
);
713 int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
714 struct commit_list
*list
, struct contains_cache
*cache
)
716 if (filter
->with_commit_tag_algo
)
717 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
718 return repo_is_descendant_of(the_repository
, commit
, list
);
721 int can_all_from_reach_with_flag(struct object_array
*from
,
722 unsigned int with_flag
,
723 unsigned int assign_flag
,
724 time_t min_commit_date
,
725 timestamp_t min_generation
)
727 struct commit
**list
= NULL
;
732 ALLOC_ARRAY(list
, from
->nr
);
734 for (i
= 0; i
< from
->nr
; i
++) {
735 struct object
*from_one
= from
->objects
[i
].item
;
737 if (!from_one
|| from_one
->flags
& assign_flag
)
740 from_one
= deref_tag(the_repository
, from_one
,
742 if (!from_one
|| from_one
->type
!= OBJ_COMMIT
) {
744 * no way to tell if this is reachable by
745 * looking at the ancestry chain alone, so
746 * leave a note to ourselves not to worry about
747 * this object anymore.
749 from
->objects
[i
].item
->flags
|= assign_flag
;
753 list
[nr_commits
] = (struct commit
*)from_one
;
754 if (repo_parse_commit(the_repository
, list
[nr_commits
]) ||
755 commit_graph_generation(list
[nr_commits
]) < min_generation
) {
763 QSORT(list
, nr_commits
, compare_commits_by_gen
);
765 for (i
= 0; i
< nr_commits
; i
++) {
766 /* DFS from list[i] */
767 struct commit_list
*stack
= NULL
;
769 list
[i
]->object
.flags
|= assign_flag
;
770 commit_list_insert(list
[i
], &stack
);
773 struct commit_list
*parent
;
775 if (stack
->item
->object
.flags
& (with_flag
| RESULT
)) {
778 stack
->item
->object
.flags
|= RESULT
;
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
;
786 if (!(parent
->item
->object
.flags
& assign_flag
)) {
787 parent
->item
->object
.flags
|= assign_flag
;
789 if (repo_parse_commit(the_repository
, parent
->item
) ||
790 parent
->item
->date
< min_commit_date
||
791 commit_graph_generation(parent
->item
) < min_generation
)
794 commit_list_insert(parent
->item
, &stack
);
803 if (!(list
[i
]->object
.flags
& (with_flag
| RESULT
))) {
810 clear_commit_marks_many(nr_commits
, list
, RESULT
| assign_flag
);
813 for (i
= 0; i
< from
->nr
; i
++) {
814 struct object
*from_one
= from
->objects
[i
].item
;
817 from_one
->flags
&= ~assign_flag
;
823 int can_all_from_reach(struct commit_list
*from
, struct commit_list
*to
,
824 int cutoff_by_min_date
)
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
;
830 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
833 add_object_array(&from_iter
->item
->object
, NULL
, &from_objs
);
835 if (!repo_parse_commit(the_repository
, from_iter
->item
)) {
836 timestamp_t generation
;
837 if (from_iter
->item
->date
< min_commit_date
)
838 min_commit_date
= from_iter
->item
->date
;
840 generation
= commit_graph_generation(from_iter
->item
);
841 if (generation
< min_generation
)
842 min_generation
= generation
;
845 from_iter
= from_iter
->next
;
849 if (!repo_parse_commit(the_repository
, to_iter
->item
)) {
850 timestamp_t generation
;
851 if (to_iter
->item
->date
< min_commit_date
)
852 min_commit_date
= to_iter
->item
->date
;
854 generation
= commit_graph_generation(to_iter
->item
);
855 if (generation
< min_generation
)
856 min_generation
= generation
;
859 to_iter
->item
->object
.flags
|= PARENT2
;
861 to_iter
= to_iter
->next
;
864 result
= can_all_from_reach_with_flag(&from_objs
, PARENT2
, PARENT1
,
865 min_commit_date
, min_generation
);
868 clear_commit_marks(from
->item
, PARENT1
);
873 clear_commit_marks(to
->item
, PARENT2
);
877 object_array_clear(&from_objs
);
881 struct commit_list
*get_reachable_subset(struct commit
**from
, int nr_from
,
882 struct commit
**to
, int nr_to
,
883 unsigned int reachable_flag
)
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
;
890 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
893 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
895 for (item
= to
; item
< to_last
; item
++) {
896 timestamp_t generation
;
897 struct commit
*c
= *item
;
899 repo_parse_commit(the_repository
, c
);
900 generation
= commit_graph_generation(c
);
901 if (generation
< min_generation
)
902 min_generation
= generation
;
904 if (!(c
->object
.flags
& PARENT1
)) {
905 c
->object
.flags
|= PARENT1
;
910 for (item
= from
; item
< from_last
; item
++) {
911 struct commit
*c
= *item
;
912 if (!(c
->object
.flags
& PARENT2
)) {
913 c
->object
.flags
|= PARENT2
;
914 repo_parse_commit(the_repository
, c
);
916 prio_queue_put(&queue
, *item
);
920 while (num_to_find
&& (current
= prio_queue_get(&queue
)) != NULL
) {
921 struct commit_list
*parents
;
923 if (current
->object
.flags
& PARENT1
) {
924 current
->object
.flags
&= ~PARENT1
;
925 current
->object
.flags
|= reachable_flag
;
926 commit_list_insert(current
, &found_commits
);
930 for (parents
= current
->parents
; parents
; parents
= parents
->next
) {
931 struct commit
*p
= parents
->item
;
933 repo_parse_commit(the_repository
, p
);
935 if (commit_graph_generation(p
) < min_generation
)
938 if (p
->object
.flags
& PARENT2
)
941 p
->object
.flags
|= PARENT2
;
942 prio_queue_put(&queue
, p
);
946 clear_prio_queue(&queue
);
948 clear_commit_marks_many(nr_to
, to
, PARENT1
);
949 clear_commit_marks_many(nr_from
, from
, PARENT2
);
951 return found_commits
;
954 define_commit_slab(bit_arrays
, struct bitmap
*);
955 static struct bit_arrays bit_arrays
;
957 static void insert_no_dup(struct prio_queue
*queue
, struct commit
*c
)
959 if (c
->object
.flags
& PARENT2
)
961 prio_queue_put(queue
, c
);
962 c
->object
.flags
|= PARENT2
;
965 static struct bitmap
*get_bit_array(struct commit
*c
, int width
)
967 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
969 *bitmap
= bitmap_word_alloc(width
);
973 static void free_bit_array(struct commit
*c
)
975 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
978 bitmap_free(*bitmap
);
982 void ahead_behind(struct repository
*r
,
983 struct commit
**commits
, size_t commits_nr
,
984 struct ahead_behind_count
*counts
, size_t counts_nr
)
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
);
989 if (!commits_nr
|| !counts_nr
)
992 for (size_t i
= 0; i
< counts_nr
; i
++) {
994 counts
[i
].behind
= 0;
997 ensure_generations_valid(r
, commits
, commits_nr
);
999 init_bit_arrays(&bit_arrays
);
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
);
1005 bitmap_set(bitmap
, i
);
1006 insert_no_dup(&queue
, c
);
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
);
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
);
1018 if (reach_from_tip
^ reach_from_base
) {
1019 if (reach_from_base
)
1026 for (p
= c
->parents
; p
; p
= p
->next
) {
1027 struct bitmap
*bitmap_p
;
1029 repo_parse_commit(r
, p
->item
);
1031 bitmap_p
= get_bit_array(p
->item
, width
);
1032 bitmap_or(bitmap_p
, bitmap_c
);
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
1041 if (bitmap_popcount(bitmap_p
) == commits_nr
)
1042 p
->item
->object
.flags
|= STALE
;
1044 insert_no_dup(&queue
, p
->item
);
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
);
1056 struct commit_and_index
{
1057 struct commit
*commit
;
1059 timestamp_t generation
;
1062 static int compare_commit_and_index_by_generation(const void *va
, const void *vb
)
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
;
1067 if (a
->generation
> b
->generation
)
1069 if (a
->generation
< b
->generation
)
1074 void tips_reachable_from_bases(struct repository
*r
,
1075 struct commit_list
*bases
,
1076 struct commit
**tips
, size_t tips_nr
,
1079 struct commit_and_index
*commits
;
1080 size_t min_generation_index
= 0;
1081 timestamp_t min_generation
;
1082 struct commit_list
*stack
= NULL
;
1084 if (!bases
|| !tips
|| !tips_nr
)
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.
1094 CALLOC_ARRAY(commits
, tips_nr
);
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
]);
1102 /* Sort with generation number ascending. */
1103 QSORT(commits
, tips_nr
, compare_commit_and_index_by_generation
);
1104 min_generation
= commits
[0].generation
;
1107 repo_parse_commit(r
, bases
->item
);
1108 commit_list_insert(bases
->item
, &stack
);
1109 bases
= bases
->next
;
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
);
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
)
1123 if (commits
[j
].commit
== c
) {
1124 tips
[commits
[j
].index
]->object
.flags
|= mark
;
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
))
1132 /* Terminate early if all found. */
1136 min_generation_index
= k
;
1137 min_generation
= commits
[k
].generation
;
1142 for (p
= c
->parents
; p
; p
= p
->next
) {
1143 repo_parse_commit(r
, p
->item
);
1145 /* Have we already explored this parent? */
1146 if (p
->item
->object
.flags
& SEEN
)
1149 /* Is it below the current minimum generation? */
1150 if (commit_graph_generation(p
->item
) < min_generation
)
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
);
1160 if (explored_all_parents
)
1166 repo_clear_commit_marks(r
, SEEN
);