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
,
56 int ignore_missing_commits
)
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
)) {
109 clear_prio_queue(&queue
);
110 free_commit_list(result
);
112 * At this stage, we know that the commit is
113 * missing: `repo_parse_commit()` uses
114 * `OBJECT_INFO_DIE_IF_CORRUPT` and therefore
115 * corrupt commits would already have been
116 * dispatched with a `die()`.
120 p
->object
.flags
|= flags
;
121 prio_queue_put(&queue
, p
);
125 clear_prio_queue(&queue
);
129 static struct commit_list
*merge_bases_many(struct repository
*r
,
130 struct commit
*one
, int n
,
131 struct commit
**twos
)
133 struct commit_list
*list
= NULL
;
134 struct commit_list
*result
= NULL
;
137 for (i
= 0; i
< n
; i
++) {
140 * We do not mark this even with RESULT so we do not
141 * have to clean it up.
143 return commit_list_insert(one
, &result
);
146 if (repo_parse_commit(r
, one
))
148 for (i
= 0; i
< n
; i
++) {
149 if (repo_parse_commit(r
, twos
[i
]))
153 list
= paint_down_to_common(r
, one
, n
, twos
, 0, 0);
156 struct commit
*commit
= pop_commit(&list
);
157 if (!(commit
->object
.flags
& STALE
))
158 commit_list_insert_by_date(commit
, &result
);
163 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
165 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
170 commit_list_insert(in
->item
, &ret
);
172 for (i
= in
->next
; i
; i
= i
->next
) {
173 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
175 for (j
= ret
; j
; j
= j
->next
) {
176 struct commit_list
*bases
;
177 bases
= repo_get_merge_bases(the_repository
, i
->item
,
183 for (k
= bases
; k
; k
= k
->next
)
186 free_commit_list(ret
);
192 static int remove_redundant_no_gen(struct repository
*r
,
193 struct commit
**array
, int cnt
)
195 struct commit
**work
;
196 unsigned char *redundant
;
200 CALLOC_ARRAY(work
, cnt
);
201 redundant
= xcalloc(cnt
, 1);
202 ALLOC_ARRAY(filled_index
, cnt
- 1);
204 for (i
= 0; i
< cnt
; i
++)
205 repo_parse_commit(r
, array
[i
]);
206 for (i
= 0; i
< cnt
; i
++) {
207 struct commit_list
*common
;
208 timestamp_t min_generation
= commit_graph_generation(array
[i
]);
212 for (j
= filled
= 0; j
< cnt
; j
++) {
213 timestamp_t curr_generation
;
214 if (i
== j
|| redundant
[j
])
216 filled_index
[filled
] = j
;
217 work
[filled
++] = array
[j
];
219 curr_generation
= commit_graph_generation(array
[j
]);
220 if (curr_generation
< min_generation
)
221 min_generation
= curr_generation
;
223 common
= paint_down_to_common(r
, array
[i
], filled
,
224 work
, min_generation
, 0);
225 if (array
[i
]->object
.flags
& PARENT2
)
227 for (j
= 0; j
< filled
; j
++)
228 if (work
[j
]->object
.flags
& PARENT1
)
229 redundant
[filled_index
[j
]] = 1;
230 clear_commit_marks(array
[i
], all_flags
);
231 clear_commit_marks_many(filled
, work
, all_flags
);
232 free_commit_list(common
);
235 /* Now collect the result */
236 COPY_ARRAY(work
, array
, cnt
);
237 for (i
= filled
= 0; i
< cnt
; i
++)
239 array
[filled
++] = work
[i
];
246 static int remove_redundant_with_gen(struct repository
*r
,
247 struct commit
**array
, int cnt
)
249 int i
, count_non_stale
= 0, count_still_independent
= cnt
;
250 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
251 struct commit
**walk_start
, **sorted
;
252 size_t walk_start_nr
= 0, walk_start_alloc
= cnt
;
256 * Sort the input by generation number, ascending. This allows
257 * us to increase the "min_generation" limit when we discover
258 * the commit with lowest generation is STALE. The index
259 * min_gen_pos points to the current position within 'array'
260 * that is not yet known to be STALE.
262 DUP_ARRAY(sorted
, array
, cnt
);
263 QSORT(sorted
, cnt
, compare_commits_by_gen
);
264 min_generation
= commit_graph_generation(sorted
[0]);
266 ALLOC_ARRAY(walk_start
, walk_start_alloc
);
268 /* Mark all parents of the input as STALE */
269 for (i
= 0; i
< cnt
; i
++) {
270 struct commit_list
*parents
;
272 repo_parse_commit(r
, array
[i
]);
273 array
[i
]->object
.flags
|= RESULT
;
274 parents
= array
[i
]->parents
;
277 repo_parse_commit(r
, parents
->item
);
278 if (!(parents
->item
->object
.flags
& STALE
)) {
279 parents
->item
->object
.flags
|= STALE
;
280 ALLOC_GROW(walk_start
, walk_start_nr
+ 1, walk_start_alloc
);
281 walk_start
[walk_start_nr
++] = parents
->item
;
283 parents
= parents
->next
;
287 QSORT(walk_start
, walk_start_nr
, compare_commits_by_gen
);
289 /* remove STALE bit for now to allow walking through parents */
290 for (i
= 0; i
< walk_start_nr
; i
++)
291 walk_start
[i
]->object
.flags
&= ~STALE
;
294 * Start walking from the highest generation. Hopefully, it will
295 * find all other items during the first-parent walk, and we can
296 * terminate early. Otherwise, we will do the same amount of work
299 for (i
= walk_start_nr
- 1; i
>= 0 && count_still_independent
> 1; i
--) {
300 /* push the STALE bits up to min generation */
301 struct commit_list
*stack
= NULL
;
303 commit_list_insert(walk_start
[i
], &stack
);
304 walk_start
[i
]->object
.flags
|= STALE
;
307 struct commit_list
*parents
;
308 struct commit
*c
= stack
->item
;
310 repo_parse_commit(r
, c
);
312 if (c
->object
.flags
& RESULT
) {
313 c
->object
.flags
&= ~RESULT
;
314 if (--count_still_independent
<= 1)
316 if (oideq(&c
->object
.oid
, &sorted
[min_gen_pos
]->object
.oid
)) {
317 while (min_gen_pos
< cnt
- 1 &&
318 (sorted
[min_gen_pos
]->object
.flags
& STALE
))
320 min_generation
= commit_graph_generation(sorted
[min_gen_pos
]);
324 if (commit_graph_generation(c
) < min_generation
) {
329 parents
= c
->parents
;
331 if (!(parents
->item
->object
.flags
& STALE
)) {
332 parents
->item
->object
.flags
|= STALE
;
333 commit_list_insert(parents
->item
, &stack
);
336 parents
= parents
->next
;
339 /* pop if all parents have been visited already */
343 free_commit_list(stack
);
348 for (i
= 0; i
< cnt
; i
++)
349 array
[i
]->object
.flags
&= ~RESULT
;
351 /* rearrange array */
352 for (i
= count_non_stale
= 0; i
< cnt
; i
++) {
353 if (!(array
[i
]->object
.flags
& STALE
))
354 array
[count_non_stale
++] = array
[i
];
358 clear_commit_marks_many(walk_start_nr
, walk_start
, STALE
);
361 return count_non_stale
;
364 static int remove_redundant(struct repository
*r
, struct commit
**array
, int cnt
)
367 * Some commit in the array may be an ancestor of
368 * another commit. Move the independent commits to the
369 * beginning of 'array' and return their number. Callers
370 * should not rely upon the contents of 'array' after
373 if (generation_numbers_enabled(r
)) {
377 * If we have a single commit with finite generation
378 * number, then the _with_gen algorithm is preferred.
380 for (i
= 0; i
< cnt
; i
++) {
381 if (commit_graph_generation(array
[i
]) < GENERATION_NUMBER_INFINITY
)
382 return remove_redundant_with_gen(r
, array
, cnt
);
386 return remove_redundant_no_gen(r
, array
, cnt
);
389 static struct commit_list
*get_merge_bases_many_0(struct repository
*r
,
392 struct commit
**twos
,
395 struct commit_list
*list
;
396 struct commit
**rslt
;
397 struct commit_list
*result
;
400 result
= merge_bases_many(r
, one
, n
, twos
);
401 for (i
= 0; i
< n
; i
++) {
405 if (!result
|| !result
->next
) {
407 clear_commit_marks(one
, all_flags
);
408 clear_commit_marks_many(n
, twos
, all_flags
);
413 /* There are more than one */
414 cnt
= commit_list_count(result
);
415 CALLOC_ARRAY(rslt
, cnt
);
416 for (list
= result
, i
= 0; list
; list
= list
->next
)
417 rslt
[i
++] = list
->item
;
418 free_commit_list(result
);
420 clear_commit_marks(one
, all_flags
);
421 clear_commit_marks_many(n
, twos
, all_flags
);
423 cnt
= remove_redundant(r
, rslt
, cnt
);
425 for (i
= 0; i
< cnt
; i
++)
426 commit_list_insert_by_date(rslt
[i
], &result
);
431 struct commit_list
*repo_get_merge_bases_many(struct repository
*r
,
434 struct commit
**twos
)
436 return get_merge_bases_many_0(r
, one
, n
, twos
, 1);
439 struct commit_list
*repo_get_merge_bases_many_dirty(struct repository
*r
,
442 struct commit
**twos
)
444 return get_merge_bases_many_0(r
, one
, n
, twos
, 0);
447 struct commit_list
*repo_get_merge_bases(struct repository
*r
,
451 return get_merge_bases_many_0(r
, one
, 1, &two
, 1);
455 * Is "commit" a descendant of one of the elements on the "with_commit" list?
457 int repo_is_descendant_of(struct repository
*r
,
458 struct commit
*commit
,
459 struct commit_list
*with_commit
)
464 if (generation_numbers_enabled(r
)) {
465 struct commit_list
*from_list
= NULL
;
467 commit_list_insert(commit
, &from_list
);
468 result
= can_all_from_reach(from_list
, with_commit
, 0);
469 free_commit_list(from_list
);
472 while (with_commit
) {
473 struct commit
*other
;
476 other
= with_commit
->item
;
477 with_commit
= with_commit
->next
;
478 ret
= repo_in_merge_bases_many(r
, other
, 1, &commit
, 0);
487 * Is "commit" an ancestor of one of the "references"?
489 int repo_in_merge_bases_many(struct repository
*r
, struct commit
*commit
,
490 int nr_reference
, struct commit
**reference
,
491 int ignore_missing_commits
)
493 struct commit_list
*bases
;
495 timestamp_t generation
, max_generation
= GENERATION_NUMBER_ZERO
;
497 if (repo_parse_commit(r
, commit
))
498 return ignore_missing_commits
? 0 : -1;
499 for (i
= 0; i
< nr_reference
; i
++) {
500 if (repo_parse_commit(r
, reference
[i
]))
501 return ignore_missing_commits
? 0 : -1;
503 generation
= commit_graph_generation(reference
[i
]);
504 if (generation
> max_generation
)
505 max_generation
= generation
;
508 generation
= commit_graph_generation(commit
);
509 if (generation
> max_generation
)
512 bases
= paint_down_to_common(r
, commit
,
513 nr_reference
, reference
,
514 generation
, ignore_missing_commits
);
515 if (commit
->object
.flags
& PARENT2
)
517 clear_commit_marks(commit
, all_flags
);
518 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
519 free_commit_list(bases
);
524 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
526 int repo_in_merge_bases(struct repository
*r
,
527 struct commit
*commit
,
528 struct commit
*reference
)
531 struct commit_list
*list
= NULL
;
532 struct commit_list
**next
= &list
;
534 next
= commit_list_append(commit
, next
);
535 res
= repo_is_descendant_of(r
, reference
, list
);
536 free_commit_list(list
);
541 struct commit_list
*reduce_heads(struct commit_list
*heads
)
543 struct commit_list
*p
;
544 struct commit_list
*result
= NULL
, **tail
= &result
;
545 struct commit
**array
;
552 for (p
= heads
; p
; p
= p
->next
)
553 p
->item
->object
.flags
&= ~STALE
;
554 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
555 if (p
->item
->object
.flags
& STALE
)
557 p
->item
->object
.flags
|= STALE
;
560 CALLOC_ARRAY(array
, num_head
);
561 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
562 if (p
->item
->object
.flags
& STALE
) {
563 array
[i
++] = p
->item
;
564 p
->item
->object
.flags
&= ~STALE
;
567 num_head
= remove_redundant(the_repository
, array
, num_head
);
568 for (i
= 0; i
< num_head
; i
++)
569 tail
= &commit_list_insert(array
[i
], tail
)->next
;
574 void reduce_heads_replace(struct commit_list
**heads
)
576 struct commit_list
*result
= reduce_heads(*heads
);
577 free_commit_list(*heads
);
581 int ref_newer(const struct object_id
*new_oid
, const struct object_id
*old_oid
)
584 struct commit
*old_commit
, *new_commit
;
585 struct commit_list
*old_commit_list
= NULL
;
589 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
590 * old_commit. Otherwise we require --force.
592 o
= deref_tag(the_repository
, parse_object(the_repository
, old_oid
),
594 if (!o
|| o
->type
!= OBJ_COMMIT
)
596 old_commit
= (struct commit
*) o
;
598 o
= deref_tag(the_repository
, parse_object(the_repository
, new_oid
),
600 if (!o
|| o
->type
!= OBJ_COMMIT
)
602 new_commit
= (struct commit
*) o
;
604 if (repo_parse_commit(the_repository
, new_commit
) < 0)
607 commit_list_insert(old_commit
, &old_commit_list
);
608 ret
= repo_is_descendant_of(the_repository
,
609 new_commit
, old_commit_list
);
612 free_commit_list(old_commit_list
);
617 * Mimicking the real stack, this stack lives on the heap, avoiding stack
620 * At each recursion step, the stack items points to the commits whose
621 * ancestors are to be inspected.
623 struct contains_stack
{
625 struct contains_stack_entry
{
626 struct commit
*commit
;
627 struct commit_list
*parents
;
631 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
633 for (; want
; want
= want
->next
)
634 if (oideq(&want
->item
->object
.oid
, &c
->object
.oid
))
640 * Test whether the candidate is contained in the list.
641 * Do not recurse to find out, though, but return -1 if inconclusive.
643 static enum contains_result
contains_test(struct commit
*candidate
,
644 const struct commit_list
*want
,
645 struct contains_cache
*cache
,
648 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
650 /* If we already have the answer cached, return that. */
655 if (in_commit_list(want
, candidate
)) {
656 *cached
= CONTAINS_YES
;
660 /* Otherwise, we don't know; prepare to recurse */
661 parse_commit_or_die(candidate
);
663 if (commit_graph_generation(candidate
) < cutoff
)
666 return CONTAINS_UNKNOWN
;
669 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
671 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
672 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
673 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
676 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
677 const struct commit_list
*want
,
678 struct contains_cache
*cache
)
680 struct contains_stack contains_stack
= { 0, 0, NULL
};
681 enum contains_result result
;
682 timestamp_t cutoff
= GENERATION_NUMBER_INFINITY
;
683 const struct commit_list
*p
;
685 for (p
= want
; p
; p
= p
->next
) {
686 timestamp_t generation
;
687 struct commit
*c
= p
->item
;
688 load_commit_graph_info(the_repository
, c
);
689 generation
= commit_graph_generation(c
);
690 if (generation
< cutoff
)
694 result
= contains_test(candidate
, want
, cache
, cutoff
);
695 if (result
!= CONTAINS_UNKNOWN
)
698 push_to_contains_stack(candidate
, &contains_stack
);
699 while (contains_stack
.nr
) {
700 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
701 struct commit
*commit
= entry
->commit
;
702 struct commit_list
*parents
= entry
->parents
;
705 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
709 * If we just popped the stack, parents->item has been marked,
710 * therefore contains_test will return a meaningful yes/no.
712 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
714 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
718 entry
->parents
= parents
->next
;
720 case CONTAINS_UNKNOWN
:
721 push_to_contains_stack(parents
->item
, &contains_stack
);
725 free(contains_stack
.contains_stack
);
726 return contains_test(candidate
, want
, cache
, cutoff
);
729 int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
730 struct commit_list
*list
, struct contains_cache
*cache
)
732 if (filter
->with_commit_tag_algo
)
733 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
734 return repo_is_descendant_of(the_repository
, commit
, list
);
737 int can_all_from_reach_with_flag(struct object_array
*from
,
738 unsigned int with_flag
,
739 unsigned int assign_flag
,
740 time_t min_commit_date
,
741 timestamp_t min_generation
)
743 struct commit
**list
= NULL
;
748 ALLOC_ARRAY(list
, from
->nr
);
750 for (i
= 0; i
< from
->nr
; i
++) {
751 struct object
*from_one
= from
->objects
[i
].item
;
753 if (!from_one
|| from_one
->flags
& assign_flag
)
756 from_one
= deref_tag(the_repository
, from_one
,
758 if (!from_one
|| from_one
->type
!= OBJ_COMMIT
) {
760 * no way to tell if this is reachable by
761 * looking at the ancestry chain alone, so
762 * leave a note to ourselves not to worry about
763 * this object anymore.
765 from
->objects
[i
].item
->flags
|= assign_flag
;
769 list
[nr_commits
] = (struct commit
*)from_one
;
770 if (repo_parse_commit(the_repository
, list
[nr_commits
]) ||
771 commit_graph_generation(list
[nr_commits
]) < min_generation
) {
779 QSORT(list
, nr_commits
, compare_commits_by_gen
);
781 for (i
= 0; i
< nr_commits
; i
++) {
782 /* DFS from list[i] */
783 struct commit_list
*stack
= NULL
;
785 list
[i
]->object
.flags
|= assign_flag
;
786 commit_list_insert(list
[i
], &stack
);
789 struct commit_list
*parent
;
791 if (stack
->item
->object
.flags
& (with_flag
| RESULT
)) {
794 stack
->item
->object
.flags
|= RESULT
;
798 for (parent
= stack
->item
->parents
; parent
; parent
= parent
->next
) {
799 if (parent
->item
->object
.flags
& (with_flag
| RESULT
))
800 stack
->item
->object
.flags
|= RESULT
;
802 if (!(parent
->item
->object
.flags
& assign_flag
)) {
803 parent
->item
->object
.flags
|= assign_flag
;
805 if (repo_parse_commit(the_repository
, parent
->item
) ||
806 parent
->item
->date
< min_commit_date
||
807 commit_graph_generation(parent
->item
) < min_generation
)
810 commit_list_insert(parent
->item
, &stack
);
819 if (!(list
[i
]->object
.flags
& (with_flag
| RESULT
))) {
826 clear_commit_marks_many(nr_commits
, list
, RESULT
| assign_flag
);
829 for (i
= 0; i
< from
->nr
; i
++) {
830 struct object
*from_one
= from
->objects
[i
].item
;
833 from_one
->flags
&= ~assign_flag
;
839 int can_all_from_reach(struct commit_list
*from
, struct commit_list
*to
,
840 int cutoff_by_min_date
)
842 struct object_array from_objs
= OBJECT_ARRAY_INIT
;
843 time_t min_commit_date
= cutoff_by_min_date
? from
->item
->date
: 0;
844 struct commit_list
*from_iter
= from
, *to_iter
= to
;
846 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
849 add_object_array(&from_iter
->item
->object
, NULL
, &from_objs
);
851 if (!repo_parse_commit(the_repository
, from_iter
->item
)) {
852 timestamp_t generation
;
853 if (from_iter
->item
->date
< min_commit_date
)
854 min_commit_date
= from_iter
->item
->date
;
856 generation
= commit_graph_generation(from_iter
->item
);
857 if (generation
< min_generation
)
858 min_generation
= generation
;
861 from_iter
= from_iter
->next
;
865 if (!repo_parse_commit(the_repository
, to_iter
->item
)) {
866 timestamp_t generation
;
867 if (to_iter
->item
->date
< min_commit_date
)
868 min_commit_date
= to_iter
->item
->date
;
870 generation
= commit_graph_generation(to_iter
->item
);
871 if (generation
< min_generation
)
872 min_generation
= generation
;
875 to_iter
->item
->object
.flags
|= PARENT2
;
877 to_iter
= to_iter
->next
;
880 result
= can_all_from_reach_with_flag(&from_objs
, PARENT2
, PARENT1
,
881 min_commit_date
, min_generation
);
884 clear_commit_marks(from
->item
, PARENT1
);
889 clear_commit_marks(to
->item
, PARENT2
);
893 object_array_clear(&from_objs
);
897 struct commit_list
*get_reachable_subset(struct commit
**from
, int nr_from
,
898 struct commit
**to
, int nr_to
,
899 unsigned int reachable_flag
)
901 struct commit
**item
;
902 struct commit
*current
;
903 struct commit_list
*found_commits
= NULL
;
904 struct commit
**to_last
= to
+ nr_to
;
905 struct commit
**from_last
= from
+ nr_from
;
906 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
909 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
911 for (item
= to
; item
< to_last
; item
++) {
912 timestamp_t generation
;
913 struct commit
*c
= *item
;
915 repo_parse_commit(the_repository
, c
);
916 generation
= commit_graph_generation(c
);
917 if (generation
< min_generation
)
918 min_generation
= generation
;
920 if (!(c
->object
.flags
& PARENT1
)) {
921 c
->object
.flags
|= PARENT1
;
926 for (item
= from
; item
< from_last
; item
++) {
927 struct commit
*c
= *item
;
928 if (!(c
->object
.flags
& PARENT2
)) {
929 c
->object
.flags
|= PARENT2
;
930 repo_parse_commit(the_repository
, c
);
932 prio_queue_put(&queue
, *item
);
936 while (num_to_find
&& (current
= prio_queue_get(&queue
)) != NULL
) {
937 struct commit_list
*parents
;
939 if (current
->object
.flags
& PARENT1
) {
940 current
->object
.flags
&= ~PARENT1
;
941 current
->object
.flags
|= reachable_flag
;
942 commit_list_insert(current
, &found_commits
);
946 for (parents
= current
->parents
; parents
; parents
= parents
->next
) {
947 struct commit
*p
= parents
->item
;
949 repo_parse_commit(the_repository
, p
);
951 if (commit_graph_generation(p
) < min_generation
)
954 if (p
->object
.flags
& PARENT2
)
957 p
->object
.flags
|= PARENT2
;
958 prio_queue_put(&queue
, p
);
962 clear_prio_queue(&queue
);
964 clear_commit_marks_many(nr_to
, to
, PARENT1
);
965 clear_commit_marks_many(nr_from
, from
, PARENT2
);
967 return found_commits
;
970 define_commit_slab(bit_arrays
, struct bitmap
*);
971 static struct bit_arrays bit_arrays
;
973 static void insert_no_dup(struct prio_queue
*queue
, struct commit
*c
)
975 if (c
->object
.flags
& PARENT2
)
977 prio_queue_put(queue
, c
);
978 c
->object
.flags
|= PARENT2
;
981 static struct bitmap
*get_bit_array(struct commit
*c
, int width
)
983 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
985 *bitmap
= bitmap_word_alloc(width
);
989 static void free_bit_array(struct commit
*c
)
991 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
994 bitmap_free(*bitmap
);
998 void ahead_behind(struct repository
*r
,
999 struct commit
**commits
, size_t commits_nr
,
1000 struct ahead_behind_count
*counts
, size_t counts_nr
)
1002 struct prio_queue queue
= { .compare
= compare_commits_by_gen_then_commit_date
};
1003 size_t width
= DIV_ROUND_UP(commits_nr
, BITS_IN_EWORD
);
1005 if (!commits_nr
|| !counts_nr
)
1008 for (size_t i
= 0; i
< counts_nr
; i
++) {
1009 counts
[i
].ahead
= 0;
1010 counts
[i
].behind
= 0;
1013 ensure_generations_valid(r
, commits
, commits_nr
);
1015 init_bit_arrays(&bit_arrays
);
1017 for (size_t i
= 0; i
< commits_nr
; i
++) {
1018 struct commit
*c
= commits
[i
];
1019 struct bitmap
*bitmap
= get_bit_array(c
, width
);
1021 bitmap_set(bitmap
, i
);
1022 insert_no_dup(&queue
, c
);
1025 while (queue_has_nonstale(&queue
)) {
1026 struct commit
*c
= prio_queue_get(&queue
);
1027 struct commit_list
*p
;
1028 struct bitmap
*bitmap_c
= get_bit_array(c
, width
);
1030 for (size_t i
= 0; i
< counts_nr
; i
++) {
1031 int reach_from_tip
= !!bitmap_get(bitmap_c
, counts
[i
].tip_index
);
1032 int reach_from_base
= !!bitmap_get(bitmap_c
, counts
[i
].base_index
);
1034 if (reach_from_tip
^ reach_from_base
) {
1035 if (reach_from_base
)
1042 for (p
= c
->parents
; p
; p
= p
->next
) {
1043 struct bitmap
*bitmap_p
;
1045 repo_parse_commit(r
, p
->item
);
1047 bitmap_p
= get_bit_array(p
->item
, width
);
1048 bitmap_or(bitmap_p
, bitmap_c
);
1051 * If this parent is reachable from every starting
1052 * commit, then none of its ancestors can contribute
1053 * to the ahead/behind count. Mark it as STALE, so
1054 * we can stop the walk when every commit in the
1057 if (bitmap_popcount(bitmap_p
) == commits_nr
)
1058 p
->item
->object
.flags
|= STALE
;
1060 insert_no_dup(&queue
, p
->item
);
1066 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1067 repo_clear_commit_marks(r
, PARENT2
| STALE
);
1068 clear_bit_arrays(&bit_arrays
);
1069 clear_prio_queue(&queue
);
1072 struct commit_and_index
{
1073 struct commit
*commit
;
1075 timestamp_t generation
;
1078 static int compare_commit_and_index_by_generation(const void *va
, const void *vb
)
1080 const struct commit_and_index
*a
= (const struct commit_and_index
*)va
;
1081 const struct commit_and_index
*b
= (const struct commit_and_index
*)vb
;
1083 if (a
->generation
> b
->generation
)
1085 if (a
->generation
< b
->generation
)
1090 void tips_reachable_from_bases(struct repository
*r
,
1091 struct commit_list
*bases
,
1092 struct commit
**tips
, size_t tips_nr
,
1095 struct commit_and_index
*commits
;
1096 size_t min_generation_index
= 0;
1097 timestamp_t min_generation
;
1098 struct commit_list
*stack
= NULL
;
1100 if (!bases
|| !tips
|| !tips_nr
)
1104 * Do a depth-first search starting at 'bases' to search for the
1105 * tips. Stop at the lowest (un-found) generation number. When
1106 * finding the lowest commit, increase the minimum generation
1107 * number to the next lowest (un-found) generation number.
1110 CALLOC_ARRAY(commits
, tips_nr
);
1112 for (size_t i
= 0; i
< tips_nr
; i
++) {
1113 commits
[i
].commit
= tips
[i
];
1114 commits
[i
].index
= i
;
1115 commits
[i
].generation
= commit_graph_generation(tips
[i
]);
1118 /* Sort with generation number ascending. */
1119 QSORT(commits
, tips_nr
, compare_commit_and_index_by_generation
);
1120 min_generation
= commits
[0].generation
;
1123 repo_parse_commit(r
, bases
->item
);
1124 commit_list_insert(bases
->item
, &stack
);
1125 bases
= bases
->next
;
1129 int explored_all_parents
= 1;
1130 struct commit_list
*p
;
1131 struct commit
*c
= stack
->item
;
1132 timestamp_t c_gen
= commit_graph_generation(c
);
1134 /* Does it match any of our tips? */
1135 for (size_t j
= min_generation_index
; j
< tips_nr
; j
++) {
1136 if (c_gen
< commits
[j
].generation
)
1139 if (commits
[j
].commit
== c
) {
1140 tips
[commits
[j
].index
]->object
.flags
|= mark
;
1142 if (j
== min_generation_index
) {
1143 unsigned int k
= j
+ 1;
1144 while (k
< tips_nr
&&
1145 (tips
[commits
[k
].index
]->object
.flags
& mark
))
1148 /* Terminate early if all found. */
1152 min_generation_index
= k
;
1153 min_generation
= commits
[k
].generation
;
1158 for (p
= c
->parents
; p
; p
= p
->next
) {
1159 repo_parse_commit(r
, p
->item
);
1161 /* Have we already explored this parent? */
1162 if (p
->item
->object
.flags
& SEEN
)
1165 /* Is it below the current minimum generation? */
1166 if (commit_graph_generation(p
->item
) < min_generation
)
1169 /* Ok, we will explore from here on. */
1170 p
->item
->object
.flags
|= SEEN
;
1171 explored_all_parents
= 0;
1172 commit_list_insert(p
->item
, &stack
);
1176 if (explored_all_parents
)
1182 repo_clear_commit_marks(r
, SEEN
);