1 #include "git-compat-util.h"
3 #include "commit-graph.h"
6 #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
)) {
108 clear_prio_queue(&queue
);
109 free_commit_list(result
);
112 p
->object
.flags
|= flags
;
113 prio_queue_put(&queue
, p
);
117 clear_prio_queue(&queue
);
121 static struct commit_list
*merge_bases_many(struct repository
*r
,
122 struct commit
*one
, int n
,
123 struct commit
**twos
)
125 struct commit_list
*list
= NULL
;
126 struct commit_list
*result
= NULL
;
129 for (i
= 0; i
< n
; i
++) {
132 * We do not mark this even with RESULT so we do not
133 * have to clean it up.
135 return commit_list_insert(one
, &result
);
138 if (repo_parse_commit(r
, one
))
140 for (i
= 0; i
< n
; i
++) {
141 if (repo_parse_commit(r
, twos
[i
]))
145 list
= paint_down_to_common(r
, one
, n
, twos
, 0);
148 struct commit
*commit
= pop_commit(&list
);
149 if (!(commit
->object
.flags
& STALE
))
150 commit_list_insert_by_date(commit
, &result
);
155 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
157 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
162 commit_list_insert(in
->item
, &ret
);
164 for (i
= in
->next
; i
; i
= i
->next
) {
165 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
167 for (j
= ret
; j
; j
= j
->next
) {
168 struct commit_list
*bases
;
169 bases
= repo_get_merge_bases(the_repository
, i
->item
,
175 for (k
= bases
; k
; k
= k
->next
)
178 free_commit_list(ret
);
184 static int remove_redundant_no_gen(struct repository
*r
,
185 struct commit
**array
, int cnt
)
187 struct commit
**work
;
188 unsigned char *redundant
;
192 CALLOC_ARRAY(work
, cnt
);
193 redundant
= xcalloc(cnt
, 1);
194 ALLOC_ARRAY(filled_index
, cnt
- 1);
196 for (i
= 0; i
< cnt
; i
++)
197 repo_parse_commit(r
, array
[i
]);
198 for (i
= 0; i
< cnt
; i
++) {
199 struct commit_list
*common
;
200 timestamp_t min_generation
= commit_graph_generation(array
[i
]);
204 for (j
= filled
= 0; j
< cnt
; j
++) {
205 timestamp_t curr_generation
;
206 if (i
== j
|| redundant
[j
])
208 filled_index
[filled
] = j
;
209 work
[filled
++] = array
[j
];
211 curr_generation
= commit_graph_generation(array
[j
]);
212 if (curr_generation
< min_generation
)
213 min_generation
= curr_generation
;
215 common
= paint_down_to_common(r
, array
[i
], filled
,
216 work
, min_generation
);
217 if (array
[i
]->object
.flags
& PARENT2
)
219 for (j
= 0; j
< filled
; j
++)
220 if (work
[j
]->object
.flags
& PARENT1
)
221 redundant
[filled_index
[j
]] = 1;
222 clear_commit_marks(array
[i
], all_flags
);
223 clear_commit_marks_many(filled
, work
, all_flags
);
224 free_commit_list(common
);
227 /* Now collect the result */
228 COPY_ARRAY(work
, array
, cnt
);
229 for (i
= filled
= 0; i
< cnt
; i
++)
231 array
[filled
++] = work
[i
];
238 static int remove_redundant_with_gen(struct repository
*r
,
239 struct commit
**array
, int cnt
)
241 int i
, count_non_stale
= 0, count_still_independent
= cnt
;
242 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
243 struct commit
**walk_start
, **sorted
;
244 size_t walk_start_nr
= 0, walk_start_alloc
= cnt
;
248 * Sort the input by generation number, ascending. This allows
249 * us to increase the "min_generation" limit when we discover
250 * the commit with lowest generation is STALE. The index
251 * min_gen_pos points to the current position within 'array'
252 * that is not yet known to be STALE.
254 DUP_ARRAY(sorted
, array
, cnt
);
255 QSORT(sorted
, cnt
, compare_commits_by_gen
);
256 min_generation
= commit_graph_generation(sorted
[0]);
258 ALLOC_ARRAY(walk_start
, walk_start_alloc
);
260 /* Mark all parents of the input as STALE */
261 for (i
= 0; i
< cnt
; i
++) {
262 struct commit_list
*parents
;
264 repo_parse_commit(r
, array
[i
]);
265 array
[i
]->object
.flags
|= RESULT
;
266 parents
= array
[i
]->parents
;
269 repo_parse_commit(r
, parents
->item
);
270 if (!(parents
->item
->object
.flags
& STALE
)) {
271 parents
->item
->object
.flags
|= STALE
;
272 ALLOC_GROW(walk_start
, walk_start_nr
+ 1, walk_start_alloc
);
273 walk_start
[walk_start_nr
++] = parents
->item
;
275 parents
= parents
->next
;
279 QSORT(walk_start
, walk_start_nr
, compare_commits_by_gen
);
281 /* remove STALE bit for now to allow walking through parents */
282 for (i
= 0; i
< walk_start_nr
; i
++)
283 walk_start
[i
]->object
.flags
&= ~STALE
;
286 * Start walking from the highest generation. Hopefully, it will
287 * find all other items during the first-parent walk, and we can
288 * terminate early. Otherwise, we will do the same amount of work
291 for (i
= walk_start_nr
- 1; i
>= 0 && count_still_independent
> 1; i
--) {
292 /* push the STALE bits up to min generation */
293 struct commit_list
*stack
= NULL
;
295 commit_list_insert(walk_start
[i
], &stack
);
296 walk_start
[i
]->object
.flags
|= STALE
;
299 struct commit_list
*parents
;
300 struct commit
*c
= stack
->item
;
302 repo_parse_commit(r
, c
);
304 if (c
->object
.flags
& RESULT
) {
305 c
->object
.flags
&= ~RESULT
;
306 if (--count_still_independent
<= 1)
308 if (oideq(&c
->object
.oid
, &sorted
[min_gen_pos
]->object
.oid
)) {
309 while (min_gen_pos
< cnt
- 1 &&
310 (sorted
[min_gen_pos
]->object
.flags
& STALE
))
312 min_generation
= commit_graph_generation(sorted
[min_gen_pos
]);
316 if (commit_graph_generation(c
) < min_generation
) {
321 parents
= c
->parents
;
323 if (!(parents
->item
->object
.flags
& STALE
)) {
324 parents
->item
->object
.flags
|= STALE
;
325 commit_list_insert(parents
->item
, &stack
);
328 parents
= parents
->next
;
331 /* pop if all parents have been visited already */
335 free_commit_list(stack
);
340 for (i
= 0; i
< cnt
; i
++)
341 array
[i
]->object
.flags
&= ~RESULT
;
343 /* rearrange array */
344 for (i
= count_non_stale
= 0; i
< cnt
; i
++) {
345 if (!(array
[i
]->object
.flags
& STALE
))
346 array
[count_non_stale
++] = array
[i
];
350 clear_commit_marks_many(walk_start_nr
, walk_start
, STALE
);
353 return count_non_stale
;
356 static int remove_redundant(struct repository
*r
, struct commit
**array
, int cnt
)
359 * Some commit in the array may be an ancestor of
360 * another commit. Move the independent commits to the
361 * beginning of 'array' and return their number. Callers
362 * should not rely upon the contents of 'array' after
365 if (generation_numbers_enabled(r
)) {
369 * If we have a single commit with finite generation
370 * number, then the _with_gen algorithm is preferred.
372 for (i
= 0; i
< cnt
; i
++) {
373 if (commit_graph_generation(array
[i
]) < GENERATION_NUMBER_INFINITY
)
374 return remove_redundant_with_gen(r
, array
, cnt
);
378 return remove_redundant_no_gen(r
, array
, cnt
);
381 static struct commit_list
*get_merge_bases_many_0(struct repository
*r
,
384 struct commit
**twos
,
387 struct commit_list
*list
;
388 struct commit
**rslt
;
389 struct commit_list
*result
;
392 result
= merge_bases_many(r
, one
, n
, twos
);
393 for (i
= 0; i
< n
; i
++) {
397 if (!result
|| !result
->next
) {
399 clear_commit_marks(one
, all_flags
);
400 clear_commit_marks_many(n
, twos
, all_flags
);
405 /* There are more than one */
406 cnt
= commit_list_count(result
);
407 CALLOC_ARRAY(rslt
, cnt
);
408 for (list
= result
, i
= 0; list
; list
= list
->next
)
409 rslt
[i
++] = list
->item
;
410 free_commit_list(result
);
412 clear_commit_marks(one
, all_flags
);
413 clear_commit_marks_many(n
, twos
, all_flags
);
415 cnt
= remove_redundant(r
, rslt
, cnt
);
417 for (i
= 0; i
< cnt
; i
++)
418 commit_list_insert_by_date(rslt
[i
], &result
);
423 struct commit_list
*repo_get_merge_bases_many(struct repository
*r
,
426 struct commit
**twos
)
428 return get_merge_bases_many_0(r
, one
, n
, twos
, 1);
431 struct commit_list
*repo_get_merge_bases_many_dirty(struct repository
*r
,
434 struct commit
**twos
)
436 return get_merge_bases_many_0(r
, one
, n
, twos
, 0);
439 struct commit_list
*repo_get_merge_bases(struct repository
*r
,
443 return get_merge_bases_many_0(r
, one
, 1, &two
, 1);
447 * Is "commit" a descendant of one of the elements on the "with_commit" list?
449 int repo_is_descendant_of(struct repository
*r
,
450 struct commit
*commit
,
451 struct commit_list
*with_commit
)
456 if (generation_numbers_enabled(r
)) {
457 struct commit_list
*from_list
= NULL
;
459 commit_list_insert(commit
, &from_list
);
460 result
= can_all_from_reach(from_list
, with_commit
, 0);
461 free_commit_list(from_list
);
464 while (with_commit
) {
465 struct commit
*other
;
467 other
= with_commit
->item
;
468 with_commit
= with_commit
->next
;
469 if (repo_in_merge_bases_many(r
, other
, 1, &commit
))
477 * Is "commit" an ancestor of one of the "references"?
479 int repo_in_merge_bases_many(struct repository
*r
, struct commit
*commit
,
480 int nr_reference
, struct commit
**reference
)
482 struct commit_list
*bases
;
484 timestamp_t generation
, max_generation
= GENERATION_NUMBER_ZERO
;
486 if (repo_parse_commit(r
, commit
))
488 for (i
= 0; i
< nr_reference
; i
++) {
489 if (repo_parse_commit(r
, reference
[i
]))
492 generation
= commit_graph_generation(reference
[i
]);
493 if (generation
> max_generation
)
494 max_generation
= generation
;
497 generation
= commit_graph_generation(commit
);
498 if (generation
> max_generation
)
501 bases
= paint_down_to_common(r
, commit
,
502 nr_reference
, reference
,
504 if (commit
->object
.flags
& PARENT2
)
506 clear_commit_marks(commit
, all_flags
);
507 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
508 free_commit_list(bases
);
513 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
515 int repo_in_merge_bases(struct repository
*r
,
516 struct commit
*commit
,
517 struct commit
*reference
)
520 struct commit_list
*list
= NULL
;
521 struct commit_list
**next
= &list
;
523 next
= commit_list_append(commit
, next
);
524 res
= repo_is_descendant_of(r
, reference
, list
);
525 free_commit_list(list
);
530 struct commit_list
*reduce_heads(struct commit_list
*heads
)
532 struct commit_list
*p
;
533 struct commit_list
*result
= NULL
, **tail
= &result
;
534 struct commit
**array
;
541 for (p
= heads
; p
; p
= p
->next
)
542 p
->item
->object
.flags
&= ~STALE
;
543 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
544 if (p
->item
->object
.flags
& STALE
)
546 p
->item
->object
.flags
|= STALE
;
549 CALLOC_ARRAY(array
, num_head
);
550 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
551 if (p
->item
->object
.flags
& STALE
) {
552 array
[i
++] = p
->item
;
553 p
->item
->object
.flags
&= ~STALE
;
556 num_head
= remove_redundant(the_repository
, array
, num_head
);
557 for (i
= 0; i
< num_head
; i
++)
558 tail
= &commit_list_insert(array
[i
], tail
)->next
;
563 void reduce_heads_replace(struct commit_list
**heads
)
565 struct commit_list
*result
= reduce_heads(*heads
);
566 free_commit_list(*heads
);
570 int ref_newer(const struct object_id
*new_oid
, const struct object_id
*old_oid
)
573 struct commit
*old_commit
, *new_commit
;
574 struct commit_list
*old_commit_list
= NULL
;
578 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
579 * old_commit. Otherwise we require --force.
581 o
= deref_tag(the_repository
, parse_object(the_repository
, old_oid
),
583 if (!o
|| o
->type
!= OBJ_COMMIT
)
585 old_commit
= (struct commit
*) o
;
587 o
= deref_tag(the_repository
, parse_object(the_repository
, new_oid
),
589 if (!o
|| o
->type
!= OBJ_COMMIT
)
591 new_commit
= (struct commit
*) o
;
593 if (repo_parse_commit(the_repository
, new_commit
) < 0)
596 commit_list_insert(old_commit
, &old_commit_list
);
597 ret
= repo_is_descendant_of(the_repository
,
598 new_commit
, old_commit_list
);
599 free_commit_list(old_commit_list
);
604 * Mimicking the real stack, this stack lives on the heap, avoiding stack
607 * At each recursion step, the stack items points to the commits whose
608 * ancestors are to be inspected.
610 struct contains_stack
{
612 struct contains_stack_entry
{
613 struct commit
*commit
;
614 struct commit_list
*parents
;
618 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
620 for (; want
; want
= want
->next
)
621 if (oideq(&want
->item
->object
.oid
, &c
->object
.oid
))
627 * Test whether the candidate is contained in the list.
628 * Do not recurse to find out, though, but return -1 if inconclusive.
630 static enum contains_result
contains_test(struct commit
*candidate
,
631 const struct commit_list
*want
,
632 struct contains_cache
*cache
,
635 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
637 /* If we already have the answer cached, return that. */
642 if (in_commit_list(want
, candidate
)) {
643 *cached
= CONTAINS_YES
;
647 /* Otherwise, we don't know; prepare to recurse */
648 parse_commit_or_die(candidate
);
650 if (commit_graph_generation(candidate
) < cutoff
)
653 return CONTAINS_UNKNOWN
;
656 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
658 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
659 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
660 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
663 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
664 const struct commit_list
*want
,
665 struct contains_cache
*cache
)
667 struct contains_stack contains_stack
= { 0, 0, NULL
};
668 enum contains_result result
;
669 timestamp_t cutoff
= GENERATION_NUMBER_INFINITY
;
670 const struct commit_list
*p
;
672 for (p
= want
; p
; p
= p
->next
) {
673 timestamp_t generation
;
674 struct commit
*c
= p
->item
;
675 load_commit_graph_info(the_repository
, c
);
676 generation
= commit_graph_generation(c
);
677 if (generation
< cutoff
)
681 result
= contains_test(candidate
, want
, cache
, cutoff
);
682 if (result
!= CONTAINS_UNKNOWN
)
685 push_to_contains_stack(candidate
, &contains_stack
);
686 while (contains_stack
.nr
) {
687 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
688 struct commit
*commit
= entry
->commit
;
689 struct commit_list
*parents
= entry
->parents
;
692 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
696 * If we just popped the stack, parents->item has been marked,
697 * therefore contains_test will return a meaningful yes/no.
699 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
701 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
705 entry
->parents
= parents
->next
;
707 case CONTAINS_UNKNOWN
:
708 push_to_contains_stack(parents
->item
, &contains_stack
);
712 free(contains_stack
.contains_stack
);
713 return contains_test(candidate
, want
, cache
, cutoff
);
716 int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
717 struct commit_list
*list
, struct contains_cache
*cache
)
719 if (filter
->with_commit_tag_algo
)
720 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
721 return repo_is_descendant_of(the_repository
, commit
, list
);
724 int can_all_from_reach_with_flag(struct object_array
*from
,
725 unsigned int with_flag
,
726 unsigned int assign_flag
,
727 time_t min_commit_date
,
728 timestamp_t min_generation
)
730 struct commit
**list
= NULL
;
735 ALLOC_ARRAY(list
, from
->nr
);
737 for (i
= 0; i
< from
->nr
; i
++) {
738 struct object
*from_one
= from
->objects
[i
].item
;
740 if (!from_one
|| from_one
->flags
& assign_flag
)
743 from_one
= deref_tag(the_repository
, from_one
,
745 if (!from_one
|| from_one
->type
!= OBJ_COMMIT
) {
747 * no way to tell if this is reachable by
748 * looking at the ancestry chain alone, so
749 * leave a note to ourselves not to worry about
750 * this object anymore.
752 from
->objects
[i
].item
->flags
|= assign_flag
;
756 list
[nr_commits
] = (struct commit
*)from_one
;
757 if (repo_parse_commit(the_repository
, list
[nr_commits
]) ||
758 commit_graph_generation(list
[nr_commits
]) < min_generation
) {
766 QSORT(list
, nr_commits
, compare_commits_by_gen
);
768 for (i
= 0; i
< nr_commits
; i
++) {
769 /* DFS from list[i] */
770 struct commit_list
*stack
= NULL
;
772 list
[i
]->object
.flags
|= assign_flag
;
773 commit_list_insert(list
[i
], &stack
);
776 struct commit_list
*parent
;
778 if (stack
->item
->object
.flags
& (with_flag
| RESULT
)) {
781 stack
->item
->object
.flags
|= RESULT
;
785 for (parent
= stack
->item
->parents
; parent
; parent
= parent
->next
) {
786 if (parent
->item
->object
.flags
& (with_flag
| RESULT
))
787 stack
->item
->object
.flags
|= RESULT
;
789 if (!(parent
->item
->object
.flags
& assign_flag
)) {
790 parent
->item
->object
.flags
|= assign_flag
;
792 if (repo_parse_commit(the_repository
, parent
->item
) ||
793 parent
->item
->date
< min_commit_date
||
794 commit_graph_generation(parent
->item
) < min_generation
)
797 commit_list_insert(parent
->item
, &stack
);
806 if (!(list
[i
]->object
.flags
& (with_flag
| RESULT
))) {
813 clear_commit_marks_many(nr_commits
, list
, RESULT
| assign_flag
);
816 for (i
= 0; i
< from
->nr
; i
++) {
817 struct object
*from_one
= from
->objects
[i
].item
;
820 from_one
->flags
&= ~assign_flag
;
826 int can_all_from_reach(struct commit_list
*from
, struct commit_list
*to
,
827 int cutoff_by_min_date
)
829 struct object_array from_objs
= OBJECT_ARRAY_INIT
;
830 time_t min_commit_date
= cutoff_by_min_date
? from
->item
->date
: 0;
831 struct commit_list
*from_iter
= from
, *to_iter
= to
;
833 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
836 add_object_array(&from_iter
->item
->object
, NULL
, &from_objs
);
838 if (!repo_parse_commit(the_repository
, from_iter
->item
)) {
839 timestamp_t generation
;
840 if (from_iter
->item
->date
< min_commit_date
)
841 min_commit_date
= from_iter
->item
->date
;
843 generation
= commit_graph_generation(from_iter
->item
);
844 if (generation
< min_generation
)
845 min_generation
= generation
;
848 from_iter
= from_iter
->next
;
852 if (!repo_parse_commit(the_repository
, to_iter
->item
)) {
853 timestamp_t generation
;
854 if (to_iter
->item
->date
< min_commit_date
)
855 min_commit_date
= to_iter
->item
->date
;
857 generation
= commit_graph_generation(to_iter
->item
);
858 if (generation
< min_generation
)
859 min_generation
= generation
;
862 to_iter
->item
->object
.flags
|= PARENT2
;
864 to_iter
= to_iter
->next
;
867 result
= can_all_from_reach_with_flag(&from_objs
, PARENT2
, PARENT1
,
868 min_commit_date
, min_generation
);
871 clear_commit_marks(from
->item
, PARENT1
);
876 clear_commit_marks(to
->item
, PARENT2
);
880 object_array_clear(&from_objs
);
884 struct commit_list
*get_reachable_subset(struct commit
**from
, int nr_from
,
885 struct commit
**to
, int nr_to
,
886 unsigned int reachable_flag
)
888 struct commit
**item
;
889 struct commit
*current
;
890 struct commit_list
*found_commits
= NULL
;
891 struct commit
**to_last
= to
+ nr_to
;
892 struct commit
**from_last
= from
+ nr_from
;
893 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
896 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
898 for (item
= to
; item
< to_last
; item
++) {
899 timestamp_t generation
;
900 struct commit
*c
= *item
;
902 repo_parse_commit(the_repository
, c
);
903 generation
= commit_graph_generation(c
);
904 if (generation
< min_generation
)
905 min_generation
= generation
;
907 if (!(c
->object
.flags
& PARENT1
)) {
908 c
->object
.flags
|= PARENT1
;
913 for (item
= from
; item
< from_last
; item
++) {
914 struct commit
*c
= *item
;
915 if (!(c
->object
.flags
& PARENT2
)) {
916 c
->object
.flags
|= PARENT2
;
917 repo_parse_commit(the_repository
, c
);
919 prio_queue_put(&queue
, *item
);
923 while (num_to_find
&& (current
= prio_queue_get(&queue
)) != NULL
) {
924 struct commit_list
*parents
;
926 if (current
->object
.flags
& PARENT1
) {
927 current
->object
.flags
&= ~PARENT1
;
928 current
->object
.flags
|= reachable_flag
;
929 commit_list_insert(current
, &found_commits
);
933 for (parents
= current
->parents
; parents
; parents
= parents
->next
) {
934 struct commit
*p
= parents
->item
;
936 repo_parse_commit(the_repository
, p
);
938 if (commit_graph_generation(p
) < min_generation
)
941 if (p
->object
.flags
& PARENT2
)
944 p
->object
.flags
|= PARENT2
;
945 prio_queue_put(&queue
, p
);
949 clear_prio_queue(&queue
);
951 clear_commit_marks_many(nr_to
, to
, PARENT1
);
952 clear_commit_marks_many(nr_from
, from
, PARENT2
);
954 return found_commits
;
957 define_commit_slab(bit_arrays
, struct bitmap
*);
958 static struct bit_arrays bit_arrays
;
960 static void insert_no_dup(struct prio_queue
*queue
, struct commit
*c
)
962 if (c
->object
.flags
& PARENT2
)
964 prio_queue_put(queue
, c
);
965 c
->object
.flags
|= PARENT2
;
968 static struct bitmap
*get_bit_array(struct commit
*c
, int width
)
970 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
972 *bitmap
= bitmap_word_alloc(width
);
976 static void free_bit_array(struct commit
*c
)
978 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
981 bitmap_free(*bitmap
);
985 void ahead_behind(struct repository
*r
,
986 struct commit
**commits
, size_t commits_nr
,
987 struct ahead_behind_count
*counts
, size_t counts_nr
)
989 struct prio_queue queue
= { .compare
= compare_commits_by_gen_then_commit_date
};
990 size_t width
= DIV_ROUND_UP(commits_nr
, BITS_IN_EWORD
);
992 if (!commits_nr
|| !counts_nr
)
995 for (size_t i
= 0; i
< counts_nr
; i
++) {
997 counts
[i
].behind
= 0;
1000 ensure_generations_valid(r
, commits
, commits_nr
);
1002 init_bit_arrays(&bit_arrays
);
1004 for (size_t i
= 0; i
< commits_nr
; i
++) {
1005 struct commit
*c
= commits
[i
];
1006 struct bitmap
*bitmap
= get_bit_array(c
, width
);
1008 bitmap_set(bitmap
, i
);
1009 insert_no_dup(&queue
, c
);
1012 while (queue_has_nonstale(&queue
)) {
1013 struct commit
*c
= prio_queue_get(&queue
);
1014 struct commit_list
*p
;
1015 struct bitmap
*bitmap_c
= get_bit_array(c
, width
);
1017 for (size_t i
= 0; i
< counts_nr
; i
++) {
1018 int reach_from_tip
= !!bitmap_get(bitmap_c
, counts
[i
].tip_index
);
1019 int reach_from_base
= !!bitmap_get(bitmap_c
, counts
[i
].base_index
);
1021 if (reach_from_tip
^ reach_from_base
) {
1022 if (reach_from_base
)
1029 for (p
= c
->parents
; p
; p
= p
->next
) {
1030 struct bitmap
*bitmap_p
;
1032 repo_parse_commit(r
, p
->item
);
1034 bitmap_p
= get_bit_array(p
->item
, width
);
1035 bitmap_or(bitmap_p
, bitmap_c
);
1038 * If this parent is reachable from every starting
1039 * commit, then none of its ancestors can contribute
1040 * to the ahead/behind count. Mark it as STALE, so
1041 * we can stop the walk when every commit in the
1044 if (bitmap_popcount(bitmap_p
) == commits_nr
)
1045 p
->item
->object
.flags
|= STALE
;
1047 insert_no_dup(&queue
, p
->item
);
1053 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1054 repo_clear_commit_marks(r
, PARENT2
| STALE
);
1055 clear_bit_arrays(&bit_arrays
);
1056 clear_prio_queue(&queue
);
1059 struct commit_and_index
{
1060 struct commit
*commit
;
1062 timestamp_t generation
;
1065 static int compare_commit_and_index_by_generation(const void *va
, const void *vb
)
1067 const struct commit_and_index
*a
= (const struct commit_and_index
*)va
;
1068 const struct commit_and_index
*b
= (const struct commit_and_index
*)vb
;
1070 if (a
->generation
> b
->generation
)
1072 if (a
->generation
< b
->generation
)
1077 void tips_reachable_from_bases(struct repository
*r
,
1078 struct commit_list
*bases
,
1079 struct commit
**tips
, size_t tips_nr
,
1082 struct commit_and_index
*commits
;
1083 size_t min_generation_index
= 0;
1084 timestamp_t min_generation
;
1085 struct commit_list
*stack
= NULL
;
1087 if (!bases
|| !tips
|| !tips_nr
)
1091 * Do a depth-first search starting at 'bases' to search for the
1092 * tips. Stop at the lowest (un-found) generation number. When
1093 * finding the lowest commit, increase the minimum generation
1094 * number to the next lowest (un-found) generation number.
1097 CALLOC_ARRAY(commits
, tips_nr
);
1099 for (size_t i
= 0; i
< tips_nr
; i
++) {
1100 commits
[i
].commit
= tips
[i
];
1101 commits
[i
].index
= i
;
1102 commits
[i
].generation
= commit_graph_generation(tips
[i
]);
1105 /* Sort with generation number ascending. */
1106 QSORT(commits
, tips_nr
, compare_commit_and_index_by_generation
);
1107 min_generation
= commits
[0].generation
;
1110 repo_parse_commit(r
, bases
->item
);
1111 commit_list_insert(bases
->item
, &stack
);
1112 bases
= bases
->next
;
1116 int explored_all_parents
= 1;
1117 struct commit_list
*p
;
1118 struct commit
*c
= stack
->item
;
1119 timestamp_t c_gen
= commit_graph_generation(c
);
1121 /* Does it match any of our tips? */
1122 for (size_t j
= min_generation_index
; j
< tips_nr
; j
++) {
1123 if (c_gen
< commits
[j
].generation
)
1126 if (commits
[j
].commit
== c
) {
1127 tips
[commits
[j
].index
]->object
.flags
|= mark
;
1129 if (j
== min_generation_index
) {
1130 unsigned int k
= j
+ 1;
1131 while (k
< tips_nr
&&
1132 (tips
[commits
[k
].index
]->object
.flags
& mark
))
1135 /* Terminate early if all found. */
1139 min_generation_index
= k
;
1140 min_generation
= commits
[k
].generation
;
1145 for (p
= c
->parents
; p
; p
= p
->next
) {
1146 repo_parse_commit(r
, p
->item
);
1148 /* Have we already explored this parent? */
1149 if (p
->item
->object
.flags
& SEEN
)
1152 /* Is it below the current minimum generation? */
1153 if (commit_graph_generation(p
->item
) < min_generation
)
1156 /* Ok, we will explore from here on. */
1157 p
->item
->object
.flags
|= SEEN
;
1158 explored_all_parents
= 0;
1159 commit_list_insert(p
->item
, &stack
);
1163 if (explored_all_parents
)
1169 repo_clear_commit_marks(r
, SEEN
);