1 #include "git-compat-util.h"
4 #include "commit-graph.h"
7 #include "prio-queue.h"
9 #include "ref-filter.h"
12 #include "commit-reach.h"
13 #include "ewah/ewok.h"
15 /* Remember to update object flag allocation in object.h */
16 #define PARENT1 (1u<<16)
17 #define PARENT2 (1u<<17)
18 #define STALE (1u<<18)
19 #define RESULT (1u<<19)
21 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
23 static int compare_commits_by_gen(const void *_a
, const void *_b
)
25 const struct commit
*a
= *(const struct commit
* const *)_a
;
26 const struct commit
*b
= *(const struct commit
* const *)_b
;
28 timestamp_t generation_a
= commit_graph_generation(a
);
29 timestamp_t generation_b
= commit_graph_generation(b
);
31 if (generation_a
< generation_b
)
33 if (generation_a
> generation_b
)
35 if (a
->date
< b
->date
)
37 if (a
->date
> b
->date
)
42 static int queue_has_nonstale(struct prio_queue
*queue
)
45 for (i
= 0; i
< queue
->nr
; i
++) {
46 struct commit
*commit
= queue
->array
[i
].data
;
47 if (!(commit
->object
.flags
& STALE
))
53 /* all input commits in one and twos[] must have been parsed! */
54 static struct commit_list
*paint_down_to_common(struct repository
*r
,
55 struct commit
*one
, int n
,
57 timestamp_t min_generation
)
59 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
60 struct commit_list
*result
= NULL
;
62 timestamp_t last_gen
= GENERATION_NUMBER_INFINITY
;
64 if (!min_generation
&& !corrected_commit_dates_enabled(r
))
65 queue
.compare
= compare_commits_by_commit_date
;
67 one
->object
.flags
|= PARENT1
;
69 commit_list_append(one
, &result
);
72 prio_queue_put(&queue
, one
);
74 for (i
= 0; i
< n
; i
++) {
75 twos
[i
]->object
.flags
|= PARENT2
;
76 prio_queue_put(&queue
, twos
[i
]);
79 while (queue_has_nonstale(&queue
)) {
80 struct commit
*commit
= prio_queue_get(&queue
);
81 struct commit_list
*parents
;
83 timestamp_t generation
= commit_graph_generation(commit
);
85 if (min_generation
&& generation
> last_gen
)
86 BUG("bad generation skip %"PRItime
" > %"PRItime
" at %s",
88 oid_to_hex(&commit
->object
.oid
));
89 last_gen
= generation
;
91 if (generation
< min_generation
)
94 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
95 if (flags
== (PARENT1
| PARENT2
)) {
96 if (!(commit
->object
.flags
& RESULT
)) {
97 commit
->object
.flags
|= RESULT
;
98 commit_list_insert_by_date(commit
, &result
);
100 /* Mark parents of a found merge stale */
103 parents
= commit
->parents
;
105 struct commit
*p
= parents
->item
;
106 parents
= parents
->next
;
107 if ((p
->object
.flags
& flags
) == flags
)
109 if (repo_parse_commit(r
, p
))
111 p
->object
.flags
|= flags
;
112 prio_queue_put(&queue
, p
);
116 clear_prio_queue(&queue
);
120 static struct commit_list
*merge_bases_many(struct repository
*r
,
121 struct commit
*one
, int n
,
122 struct commit
**twos
)
124 struct commit_list
*list
= NULL
;
125 struct commit_list
*result
= NULL
;
128 for (i
= 0; i
< n
; i
++) {
131 * We do not mark this even with RESULT so we do not
132 * have to clean it up.
134 return commit_list_insert(one
, &result
);
137 if (repo_parse_commit(r
, one
))
139 for (i
= 0; i
< n
; i
++) {
140 if (repo_parse_commit(r
, twos
[i
]))
144 list
= paint_down_to_common(r
, one
, n
, twos
, 0);
147 struct commit
*commit
= pop_commit(&list
);
148 if (!(commit
->object
.flags
& STALE
))
149 commit_list_insert_by_date(commit
, &result
);
154 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
156 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
161 commit_list_insert(in
->item
, &ret
);
163 for (i
= in
->next
; i
; i
= i
->next
) {
164 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
166 for (j
= ret
; j
; j
= j
->next
) {
167 struct commit_list
*bases
;
168 bases
= repo_get_merge_bases(the_repository
, i
->item
,
174 for (k
= bases
; k
; k
= k
->next
)
182 static int remove_redundant_no_gen(struct repository
*r
,
183 struct commit
**array
, int cnt
)
185 struct commit
**work
;
186 unsigned char *redundant
;
190 CALLOC_ARRAY(work
, cnt
);
191 redundant
= xcalloc(cnt
, 1);
192 ALLOC_ARRAY(filled_index
, cnt
- 1);
194 for (i
= 0; i
< cnt
; i
++)
195 repo_parse_commit(r
, array
[i
]);
196 for (i
= 0; i
< cnt
; i
++) {
197 struct commit_list
*common
;
198 timestamp_t min_generation
= commit_graph_generation(array
[i
]);
202 for (j
= filled
= 0; j
< cnt
; j
++) {
203 timestamp_t curr_generation
;
204 if (i
== j
|| redundant
[j
])
206 filled_index
[filled
] = j
;
207 work
[filled
++] = array
[j
];
209 curr_generation
= commit_graph_generation(array
[j
]);
210 if (curr_generation
< min_generation
)
211 min_generation
= curr_generation
;
213 common
= paint_down_to_common(r
, array
[i
], filled
,
214 work
, min_generation
);
215 if (array
[i
]->object
.flags
& PARENT2
)
217 for (j
= 0; j
< filled
; j
++)
218 if (work
[j
]->object
.flags
& PARENT1
)
219 redundant
[filled_index
[j
]] = 1;
220 clear_commit_marks(array
[i
], all_flags
);
221 clear_commit_marks_many(filled
, work
, all_flags
);
222 free_commit_list(common
);
225 /* Now collect the result */
226 COPY_ARRAY(work
, array
, cnt
);
227 for (i
= filled
= 0; i
< cnt
; i
++)
229 array
[filled
++] = work
[i
];
236 static int remove_redundant_with_gen(struct repository
*r
,
237 struct commit
**array
, int cnt
)
239 int i
, count_non_stale
= 0, count_still_independent
= cnt
;
240 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
241 struct commit
**walk_start
, **sorted
;
242 size_t walk_start_nr
= 0, walk_start_alloc
= cnt
;
246 * Sort the input by generation number, ascending. This allows
247 * us to increase the "min_generation" limit when we discover
248 * the commit with lowest generation is STALE. The index
249 * min_gen_pos points to the current position within 'array'
250 * that is not yet known to be STALE.
252 DUP_ARRAY(sorted
, array
, cnt
);
253 QSORT(sorted
, cnt
, compare_commits_by_gen
);
254 min_generation
= commit_graph_generation(sorted
[0]);
256 ALLOC_ARRAY(walk_start
, walk_start_alloc
);
258 /* Mark all parents of the input as STALE */
259 for (i
= 0; i
< cnt
; i
++) {
260 struct commit_list
*parents
;
262 repo_parse_commit(r
, array
[i
]);
263 array
[i
]->object
.flags
|= RESULT
;
264 parents
= array
[i
]->parents
;
267 repo_parse_commit(r
, parents
->item
);
268 if (!(parents
->item
->object
.flags
& STALE
)) {
269 parents
->item
->object
.flags
|= STALE
;
270 ALLOC_GROW(walk_start
, walk_start_nr
+ 1, walk_start_alloc
);
271 walk_start
[walk_start_nr
++] = parents
->item
;
273 parents
= parents
->next
;
277 QSORT(walk_start
, walk_start_nr
, compare_commits_by_gen
);
279 /* remove STALE bit for now to allow walking through parents */
280 for (i
= 0; i
< walk_start_nr
; i
++)
281 walk_start
[i
]->object
.flags
&= ~STALE
;
284 * Start walking from the highest generation. Hopefully, it will
285 * find all other items during the first-parent walk, and we can
286 * terminate early. Otherwise, we will do the same amount of work
289 for (i
= walk_start_nr
- 1; i
>= 0 && count_still_independent
> 1; i
--) {
290 /* push the STALE bits up to min generation */
291 struct commit_list
*stack
= NULL
;
293 commit_list_insert(walk_start
[i
], &stack
);
294 walk_start
[i
]->object
.flags
|= STALE
;
297 struct commit_list
*parents
;
298 struct commit
*c
= stack
->item
;
300 repo_parse_commit(r
, c
);
302 if (c
->object
.flags
& RESULT
) {
303 c
->object
.flags
&= ~RESULT
;
304 if (--count_still_independent
<= 1)
306 if (oideq(&c
->object
.oid
, &sorted
[min_gen_pos
]->object
.oid
)) {
307 while (min_gen_pos
< cnt
- 1 &&
308 (sorted
[min_gen_pos
]->object
.flags
& STALE
))
310 min_generation
= commit_graph_generation(sorted
[min_gen_pos
]);
314 if (commit_graph_generation(c
) < min_generation
) {
319 parents
= c
->parents
;
321 if (!(parents
->item
->object
.flags
& STALE
)) {
322 parents
->item
->object
.flags
|= STALE
;
323 commit_list_insert(parents
->item
, &stack
);
326 parents
= parents
->next
;
329 /* pop if all parents have been visited already */
333 free_commit_list(stack
);
338 for (i
= 0; i
< cnt
; i
++)
339 array
[i
]->object
.flags
&= ~RESULT
;
341 /* rearrange array */
342 for (i
= count_non_stale
= 0; i
< cnt
; i
++) {
343 if (!(array
[i
]->object
.flags
& STALE
))
344 array
[count_non_stale
++] = array
[i
];
348 clear_commit_marks_many(walk_start_nr
, walk_start
, STALE
);
351 return count_non_stale
;
354 static int remove_redundant(struct repository
*r
, struct commit
**array
, int cnt
)
357 * Some commit in the array may be an ancestor of
358 * another commit. Move the independent commits to the
359 * beginning of 'array' and return their number. Callers
360 * should not rely upon the contents of 'array' after
363 if (generation_numbers_enabled(r
)) {
367 * If we have a single commit with finite generation
368 * number, then the _with_gen algorithm is preferred.
370 for (i
= 0; i
< cnt
; i
++) {
371 if (commit_graph_generation(array
[i
]) < GENERATION_NUMBER_INFINITY
)
372 return remove_redundant_with_gen(r
, array
, cnt
);
376 return remove_redundant_no_gen(r
, array
, cnt
);
379 static struct commit_list
*get_merge_bases_many_0(struct repository
*r
,
382 struct commit
**twos
,
385 struct commit_list
*list
;
386 struct commit
**rslt
;
387 struct commit_list
*result
;
390 result
= merge_bases_many(r
, one
, n
, twos
);
391 for (i
= 0; i
< n
; i
++) {
395 if (!result
|| !result
->next
) {
397 clear_commit_marks(one
, all_flags
);
398 clear_commit_marks_many(n
, twos
, all_flags
);
403 /* There are more than one */
404 cnt
= commit_list_count(result
);
405 CALLOC_ARRAY(rslt
, cnt
);
406 for (list
= result
, i
= 0; list
; list
= list
->next
)
407 rslt
[i
++] = list
->item
;
408 free_commit_list(result
);
410 clear_commit_marks(one
, all_flags
);
411 clear_commit_marks_many(n
, twos
, all_flags
);
413 cnt
= remove_redundant(r
, rslt
, cnt
);
415 for (i
= 0; i
< cnt
; i
++)
416 commit_list_insert_by_date(rslt
[i
], &result
);
421 struct commit_list
*repo_get_merge_bases_many(struct repository
*r
,
424 struct commit
**twos
)
426 return get_merge_bases_many_0(r
, one
, n
, twos
, 1);
429 struct commit_list
*repo_get_merge_bases_many_dirty(struct repository
*r
,
432 struct commit
**twos
)
434 return get_merge_bases_many_0(r
, one
, n
, twos
, 0);
437 struct commit_list
*repo_get_merge_bases(struct repository
*r
,
441 return get_merge_bases_many_0(r
, one
, 1, &two
, 1);
445 * Is "commit" a descendant of one of the elements on the "with_commit" list?
447 int repo_is_descendant_of(struct repository
*r
,
448 struct commit
*commit
,
449 struct commit_list
*with_commit
)
454 if (generation_numbers_enabled(r
)) {
455 struct commit_list
*from_list
= NULL
;
457 commit_list_insert(commit
, &from_list
);
458 result
= can_all_from_reach(from_list
, with_commit
, 0);
459 free_commit_list(from_list
);
462 while (with_commit
) {
463 struct commit
*other
;
465 other
= with_commit
->item
;
466 with_commit
= with_commit
->next
;
467 if (repo_in_merge_bases_many(r
, other
, 1, &commit
))
475 * Is "commit" an ancestor of one of the "references"?
477 int repo_in_merge_bases_many(struct repository
*r
, struct commit
*commit
,
478 int nr_reference
, struct commit
**reference
)
480 struct commit_list
*bases
;
482 timestamp_t generation
, max_generation
= GENERATION_NUMBER_ZERO
;
484 if (repo_parse_commit(r
, commit
))
486 for (i
= 0; i
< nr_reference
; i
++) {
487 if (repo_parse_commit(r
, reference
[i
]))
490 generation
= commit_graph_generation(reference
[i
]);
491 if (generation
> max_generation
)
492 max_generation
= generation
;
495 generation
= commit_graph_generation(commit
);
496 if (generation
> max_generation
)
499 bases
= paint_down_to_common(r
, commit
,
500 nr_reference
, reference
,
502 if (commit
->object
.flags
& PARENT2
)
504 clear_commit_marks(commit
, all_flags
);
505 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
506 free_commit_list(bases
);
511 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
513 int repo_in_merge_bases(struct repository
*r
,
514 struct commit
*commit
,
515 struct commit
*reference
)
518 struct commit_list
*list
= NULL
;
519 struct commit_list
**next
= &list
;
521 next
= commit_list_append(commit
, next
);
522 res
= repo_is_descendant_of(r
, reference
, list
);
523 free_commit_list(list
);
528 struct commit_list
*reduce_heads(struct commit_list
*heads
)
530 struct commit_list
*p
;
531 struct commit_list
*result
= NULL
, **tail
= &result
;
532 struct commit
**array
;
539 for (p
= heads
; p
; p
= p
->next
)
540 p
->item
->object
.flags
&= ~STALE
;
541 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
542 if (p
->item
->object
.flags
& STALE
)
544 p
->item
->object
.flags
|= STALE
;
547 CALLOC_ARRAY(array
, num_head
);
548 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
549 if (p
->item
->object
.flags
& STALE
) {
550 array
[i
++] = p
->item
;
551 p
->item
->object
.flags
&= ~STALE
;
554 num_head
= remove_redundant(the_repository
, array
, num_head
);
555 for (i
= 0; i
< num_head
; i
++)
556 tail
= &commit_list_insert(array
[i
], tail
)->next
;
561 void reduce_heads_replace(struct commit_list
**heads
)
563 struct commit_list
*result
= reduce_heads(*heads
);
564 free_commit_list(*heads
);
568 int ref_newer(const struct object_id
*new_oid
, const struct object_id
*old_oid
)
571 struct commit
*old_commit
, *new_commit
;
572 struct commit_list
*old_commit_list
= NULL
;
576 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
577 * old_commit. Otherwise we require --force.
579 o
= deref_tag(the_repository
, parse_object(the_repository
, old_oid
),
581 if (!o
|| o
->type
!= OBJ_COMMIT
)
583 old_commit
= (struct commit
*) o
;
585 o
= deref_tag(the_repository
, parse_object(the_repository
, new_oid
),
587 if (!o
|| o
->type
!= OBJ_COMMIT
)
589 new_commit
= (struct commit
*) o
;
591 if (repo_parse_commit(the_repository
, new_commit
) < 0)
594 commit_list_insert(old_commit
, &old_commit_list
);
595 ret
= repo_is_descendant_of(the_repository
,
596 new_commit
, old_commit_list
);
597 free_commit_list(old_commit_list
);
602 * Mimicking the real stack, this stack lives on the heap, avoiding stack
605 * At each recursion step, the stack items points to the commits whose
606 * ancestors are to be inspected.
608 struct contains_stack
{
610 struct contains_stack_entry
{
611 struct commit
*commit
;
612 struct commit_list
*parents
;
616 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
618 for (; want
; want
= want
->next
)
619 if (oideq(&want
->item
->object
.oid
, &c
->object
.oid
))
625 * Test whether the candidate is contained in the list.
626 * Do not recurse to find out, though, but return -1 if inconclusive.
628 static enum contains_result
contains_test(struct commit
*candidate
,
629 const struct commit_list
*want
,
630 struct contains_cache
*cache
,
633 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
635 /* If we already have the answer cached, return that. */
640 if (in_commit_list(want
, candidate
)) {
641 *cached
= CONTAINS_YES
;
645 /* Otherwise, we don't know; prepare to recurse */
646 parse_commit_or_die(candidate
);
648 if (commit_graph_generation(candidate
) < cutoff
)
651 return CONTAINS_UNKNOWN
;
654 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
656 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
657 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
658 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
661 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
662 const struct commit_list
*want
,
663 struct contains_cache
*cache
)
665 struct contains_stack contains_stack
= { 0, 0, NULL
};
666 enum contains_result result
;
667 timestamp_t cutoff
= GENERATION_NUMBER_INFINITY
;
668 const struct commit_list
*p
;
670 for (p
= want
; p
; p
= p
->next
) {
671 timestamp_t generation
;
672 struct commit
*c
= p
->item
;
673 load_commit_graph_info(the_repository
, c
);
674 generation
= commit_graph_generation(c
);
675 if (generation
< cutoff
)
679 result
= contains_test(candidate
, want
, cache
, cutoff
);
680 if (result
!= CONTAINS_UNKNOWN
)
683 push_to_contains_stack(candidate
, &contains_stack
);
684 while (contains_stack
.nr
) {
685 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
686 struct commit
*commit
= entry
->commit
;
687 struct commit_list
*parents
= entry
->parents
;
690 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
694 * If we just popped the stack, parents->item has been marked,
695 * therefore contains_test will return a meaningful yes/no.
697 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
699 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
703 entry
->parents
= parents
->next
;
705 case CONTAINS_UNKNOWN
:
706 push_to_contains_stack(parents
->item
, &contains_stack
);
710 free(contains_stack
.contains_stack
);
711 return contains_test(candidate
, want
, cache
, cutoff
);
714 int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
715 struct commit_list
*list
, struct contains_cache
*cache
)
717 if (filter
->with_commit_tag_algo
)
718 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
719 return repo_is_descendant_of(the_repository
, commit
, list
);
722 int can_all_from_reach_with_flag(struct object_array
*from
,
723 unsigned int with_flag
,
724 unsigned int assign_flag
,
725 time_t min_commit_date
,
726 timestamp_t min_generation
)
728 struct commit
**list
= NULL
;
733 ALLOC_ARRAY(list
, from
->nr
);
735 for (i
= 0; i
< from
->nr
; i
++) {
736 struct object
*from_one
= from
->objects
[i
].item
;
738 if (!from_one
|| from_one
->flags
& assign_flag
)
741 from_one
= deref_tag(the_repository
, from_one
,
743 if (!from_one
|| from_one
->type
!= OBJ_COMMIT
) {
745 * no way to tell if this is reachable by
746 * looking at the ancestry chain alone, so
747 * leave a note to ourselves not to worry about
748 * this object anymore.
750 from
->objects
[i
].item
->flags
|= assign_flag
;
754 list
[nr_commits
] = (struct commit
*)from_one
;
755 if (repo_parse_commit(the_repository
, list
[nr_commits
]) ||
756 commit_graph_generation(list
[nr_commits
]) < min_generation
) {
764 QSORT(list
, nr_commits
, compare_commits_by_gen
);
766 for (i
= 0; i
< nr_commits
; i
++) {
767 /* DFS from list[i] */
768 struct commit_list
*stack
= NULL
;
770 list
[i
]->object
.flags
|= assign_flag
;
771 commit_list_insert(list
[i
], &stack
);
774 struct commit_list
*parent
;
776 if (stack
->item
->object
.flags
& (with_flag
| RESULT
)) {
779 stack
->item
->object
.flags
|= RESULT
;
783 for (parent
= stack
->item
->parents
; parent
; parent
= parent
->next
) {
784 if (parent
->item
->object
.flags
& (with_flag
| RESULT
))
785 stack
->item
->object
.flags
|= RESULT
;
787 if (!(parent
->item
->object
.flags
& assign_flag
)) {
788 parent
->item
->object
.flags
|= assign_flag
;
790 if (repo_parse_commit(the_repository
, parent
->item
) ||
791 parent
->item
->date
< min_commit_date
||
792 commit_graph_generation(parent
->item
) < min_generation
)
795 commit_list_insert(parent
->item
, &stack
);
804 if (!(list
[i
]->object
.flags
& (with_flag
| RESULT
))) {
811 clear_commit_marks_many(nr_commits
, list
, RESULT
| assign_flag
);
814 for (i
= 0; i
< from
->nr
; i
++) {
815 struct object
*from_one
= from
->objects
[i
].item
;
818 from_one
->flags
&= ~assign_flag
;
824 int can_all_from_reach(struct commit_list
*from
, struct commit_list
*to
,
825 int cutoff_by_min_date
)
827 struct object_array from_objs
= OBJECT_ARRAY_INIT
;
828 time_t min_commit_date
= cutoff_by_min_date
? from
->item
->date
: 0;
829 struct commit_list
*from_iter
= from
, *to_iter
= to
;
831 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
834 add_object_array(&from_iter
->item
->object
, NULL
, &from_objs
);
836 if (!repo_parse_commit(the_repository
, from_iter
->item
)) {
837 timestamp_t generation
;
838 if (from_iter
->item
->date
< min_commit_date
)
839 min_commit_date
= from_iter
->item
->date
;
841 generation
= commit_graph_generation(from_iter
->item
);
842 if (generation
< min_generation
)
843 min_generation
= generation
;
846 from_iter
= from_iter
->next
;
850 if (!repo_parse_commit(the_repository
, to_iter
->item
)) {
851 timestamp_t generation
;
852 if (to_iter
->item
->date
< min_commit_date
)
853 min_commit_date
= to_iter
->item
->date
;
855 generation
= commit_graph_generation(to_iter
->item
);
856 if (generation
< min_generation
)
857 min_generation
= generation
;
860 to_iter
->item
->object
.flags
|= PARENT2
;
862 to_iter
= to_iter
->next
;
865 result
= can_all_from_reach_with_flag(&from_objs
, PARENT2
, PARENT1
,
866 min_commit_date
, min_generation
);
869 clear_commit_marks(from
->item
, PARENT1
);
874 clear_commit_marks(to
->item
, PARENT2
);
878 object_array_clear(&from_objs
);
882 struct commit_list
*get_reachable_subset(struct commit
**from
, int nr_from
,
883 struct commit
**to
, int nr_to
,
884 unsigned int reachable_flag
)
886 struct commit
**item
;
887 struct commit
*current
;
888 struct commit_list
*found_commits
= NULL
;
889 struct commit
**to_last
= to
+ nr_to
;
890 struct commit
**from_last
= from
+ nr_from
;
891 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
894 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
896 for (item
= to
; item
< to_last
; item
++) {
897 timestamp_t generation
;
898 struct commit
*c
= *item
;
900 repo_parse_commit(the_repository
, c
);
901 generation
= commit_graph_generation(c
);
902 if (generation
< min_generation
)
903 min_generation
= generation
;
905 if (!(c
->object
.flags
& PARENT1
)) {
906 c
->object
.flags
|= PARENT1
;
911 for (item
= from
; item
< from_last
; item
++) {
912 struct commit
*c
= *item
;
913 if (!(c
->object
.flags
& PARENT2
)) {
914 c
->object
.flags
|= PARENT2
;
915 repo_parse_commit(the_repository
, c
);
917 prio_queue_put(&queue
, *item
);
921 while (num_to_find
&& (current
= prio_queue_get(&queue
)) != NULL
) {
922 struct commit_list
*parents
;
924 if (current
->object
.flags
& PARENT1
) {
925 current
->object
.flags
&= ~PARENT1
;
926 current
->object
.flags
|= reachable_flag
;
927 commit_list_insert(current
, &found_commits
);
931 for (parents
= current
->parents
; parents
; parents
= parents
->next
) {
932 struct commit
*p
= parents
->item
;
934 repo_parse_commit(the_repository
, p
);
936 if (commit_graph_generation(p
) < min_generation
)
939 if (p
->object
.flags
& PARENT2
)
942 p
->object
.flags
|= PARENT2
;
943 prio_queue_put(&queue
, p
);
947 clear_commit_marks_many(nr_to
, to
, PARENT1
);
948 clear_commit_marks_many(nr_from
, from
, PARENT2
);
950 return found_commits
;
953 define_commit_slab(bit_arrays
, struct bitmap
*);
954 static struct bit_arrays bit_arrays
;
956 static void insert_no_dup(struct prio_queue
*queue
, struct commit
*c
)
958 if (c
->object
.flags
& PARENT2
)
960 prio_queue_put(queue
, c
);
961 c
->object
.flags
|= PARENT2
;
964 static struct bitmap
*get_bit_array(struct commit
*c
, int width
)
966 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
968 *bitmap
= bitmap_word_alloc(width
);
972 static void free_bit_array(struct commit
*c
)
974 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
977 bitmap_free(*bitmap
);
981 void ahead_behind(struct repository
*r
,
982 struct commit
**commits
, size_t commits_nr
,
983 struct ahead_behind_count
*counts
, size_t counts_nr
)
985 struct prio_queue queue
= { .compare
= compare_commits_by_gen_then_commit_date
};
986 size_t width
= DIV_ROUND_UP(commits_nr
, BITS_IN_EWORD
);
988 if (!commits_nr
|| !counts_nr
)
991 for (size_t i
= 0; i
< counts_nr
; i
++) {
993 counts
[i
].behind
= 0;
996 ensure_generations_valid(r
, commits
, commits_nr
);
998 init_bit_arrays(&bit_arrays
);
1000 for (size_t i
= 0; i
< commits_nr
; i
++) {
1001 struct commit
*c
= commits
[i
];
1002 struct bitmap
*bitmap
= get_bit_array(c
, width
);
1004 bitmap_set(bitmap
, i
);
1005 insert_no_dup(&queue
, c
);
1008 while (queue_has_nonstale(&queue
)) {
1009 struct commit
*c
= prio_queue_get(&queue
);
1010 struct commit_list
*p
;
1011 struct bitmap
*bitmap_c
= get_bit_array(c
, width
);
1013 for (size_t i
= 0; i
< counts_nr
; i
++) {
1014 int reach_from_tip
= !!bitmap_get(bitmap_c
, counts
[i
].tip_index
);
1015 int reach_from_base
= !!bitmap_get(bitmap_c
, counts
[i
].base_index
);
1017 if (reach_from_tip
^ reach_from_base
) {
1018 if (reach_from_base
)
1025 for (p
= c
->parents
; p
; p
= p
->next
) {
1026 struct bitmap
*bitmap_p
;
1028 repo_parse_commit(r
, p
->item
);
1030 bitmap_p
= get_bit_array(p
->item
, width
);
1031 bitmap_or(bitmap_p
, bitmap_c
);
1034 * If this parent is reachable from every starting
1035 * commit, then none of its ancestors can contribute
1036 * to the ahead/behind count. Mark it as STALE, so
1037 * we can stop the walk when every commit in the
1040 if (bitmap_popcount(bitmap_p
) == commits_nr
)
1041 p
->item
->object
.flags
|= STALE
;
1043 insert_no_dup(&queue
, p
->item
);
1049 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1050 repo_clear_commit_marks(r
, PARENT2
| STALE
);
1051 clear_bit_arrays(&bit_arrays
);
1052 clear_prio_queue(&queue
);
1055 struct commit_and_index
{
1056 struct commit
*commit
;
1058 timestamp_t generation
;
1061 static int compare_commit_and_index_by_generation(const void *va
, const void *vb
)
1063 const struct commit_and_index
*a
= (const struct commit_and_index
*)va
;
1064 const struct commit_and_index
*b
= (const struct commit_and_index
*)vb
;
1066 if (a
->generation
> b
->generation
)
1068 if (a
->generation
< b
->generation
)
1073 void tips_reachable_from_bases(struct repository
*r
,
1074 struct commit_list
*bases
,
1075 struct commit
**tips
, size_t tips_nr
,
1078 struct commit_and_index
*commits
;
1079 size_t min_generation_index
= 0;
1080 timestamp_t min_generation
;
1081 struct commit_list
*stack
= NULL
;
1083 if (!bases
|| !tips
|| !tips_nr
)
1087 * Do a depth-first search starting at 'bases' to search for the
1088 * tips. Stop at the lowest (un-found) generation number. When
1089 * finding the lowest commit, increase the minimum generation
1090 * number to the next lowest (un-found) generation number.
1093 CALLOC_ARRAY(commits
, tips_nr
);
1095 for (size_t i
= 0; i
< tips_nr
; i
++) {
1096 commits
[i
].commit
= tips
[i
];
1097 commits
[i
].index
= i
;
1098 commits
[i
].generation
= commit_graph_generation(tips
[i
]);
1101 /* Sort with generation number ascending. */
1102 QSORT(commits
, tips_nr
, compare_commit_and_index_by_generation
);
1103 min_generation
= commits
[0].generation
;
1106 repo_parse_commit(r
, bases
->item
);
1107 commit_list_insert(bases
->item
, &stack
);
1108 bases
= bases
->next
;
1112 int explored_all_parents
= 1;
1113 struct commit_list
*p
;
1114 struct commit
*c
= stack
->item
;
1115 timestamp_t c_gen
= commit_graph_generation(c
);
1117 /* Does it match any of our tips? */
1118 for (size_t j
= min_generation_index
; j
< tips_nr
; j
++) {
1119 if (c_gen
< commits
[j
].generation
)
1122 if (commits
[j
].commit
== c
) {
1123 tips
[commits
[j
].index
]->object
.flags
|= mark
;
1125 if (j
== min_generation_index
) {
1126 unsigned int k
= j
+ 1;
1127 while (k
< tips_nr
&&
1128 (tips
[commits
[k
].index
]->object
.flags
& mark
))
1131 /* Terminate early if all found. */
1135 min_generation_index
= k
;
1136 min_generation
= commits
[k
].generation
;
1141 for (p
= c
->parents
; p
; p
= p
->next
) {
1142 repo_parse_commit(r
, p
->item
);
1144 /* Have we already explored this parent? */
1145 if (p
->item
->object
.flags
& SEEN
)
1148 /* Is it below the current minimum generation? */
1149 if (commit_graph_generation(p
->item
) < min_generation
)
1152 /* Ok, we will explore from here on. */
1153 p
->item
->object
.flags
|= SEEN
;
1154 explored_all_parents
= 0;
1155 commit_list_insert(p
->item
, &stack
);
1159 if (explored_all_parents
)
1165 repo_clear_commit_marks(r
, SEEN
);