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 int paint_down_to_common(struct repository
*r
,
53 struct commit
*one
, int n
,
55 timestamp_t min_generation
,
56 int ignore_missing_commits
,
57 struct commit_list
**result
)
59 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
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
);
113 * At this stage, we know that the commit is
114 * missing: `repo_parse_commit()` uses
115 * `OBJECT_INFO_DIE_IF_CORRUPT` and therefore
116 * corrupt commits would already have been
117 * dispatched with a `die()`.
119 if (ignore_missing_commits
)
121 return error(_("could not parse commit %s"),
122 oid_to_hex(&p
->object
.oid
));
124 p
->object
.flags
|= flags
;
125 prio_queue_put(&queue
, p
);
129 clear_prio_queue(&queue
);
133 static struct commit_list
*merge_bases_many(struct repository
*r
,
134 struct commit
*one
, int n
,
135 struct commit
**twos
)
137 struct commit_list
*list
= NULL
;
138 struct commit_list
*result
= NULL
;
141 for (i
= 0; i
< n
; i
++) {
144 * We do not mark this even with RESULT so we do not
145 * have to clean it up.
147 return commit_list_insert(one
, &result
);
150 if (repo_parse_commit(r
, one
))
152 for (i
= 0; i
< n
; i
++) {
153 if (repo_parse_commit(r
, twos
[i
]))
157 if (paint_down_to_common(r
, one
, n
, twos
, 0, 0, &list
)) {
158 free_commit_list(list
);
163 struct commit
*commit
= pop_commit(&list
);
164 if (!(commit
->object
.flags
& STALE
))
165 commit_list_insert_by_date(commit
, &result
);
170 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
172 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
177 commit_list_insert(in
->item
, &ret
);
179 for (i
= in
->next
; i
; i
= i
->next
) {
180 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
182 for (j
= ret
; j
; j
= j
->next
) {
183 struct commit_list
*bases
;
184 bases
= repo_get_merge_bases(the_repository
, i
->item
,
190 for (k
= bases
; k
; k
= k
->next
)
193 free_commit_list(ret
);
199 static int remove_redundant_no_gen(struct repository
*r
,
200 struct commit
**array
, int cnt
)
202 struct commit
**work
;
203 unsigned char *redundant
;
207 CALLOC_ARRAY(work
, cnt
);
208 redundant
= xcalloc(cnt
, 1);
209 ALLOC_ARRAY(filled_index
, cnt
- 1);
211 for (i
= 0; i
< cnt
; i
++)
212 repo_parse_commit(r
, array
[i
]);
213 for (i
= 0; i
< cnt
; i
++) {
214 struct commit_list
*common
= NULL
;
215 timestamp_t min_generation
= commit_graph_generation(array
[i
]);
219 for (j
= filled
= 0; j
< cnt
; j
++) {
220 timestamp_t curr_generation
;
221 if (i
== j
|| redundant
[j
])
223 filled_index
[filled
] = j
;
224 work
[filled
++] = array
[j
];
226 curr_generation
= commit_graph_generation(array
[j
]);
227 if (curr_generation
< min_generation
)
228 min_generation
= curr_generation
;
230 if (paint_down_to_common(r
, array
[i
], filled
,
231 work
, min_generation
, 0, &common
)) {
232 clear_commit_marks(array
[i
], all_flags
);
233 clear_commit_marks_many(filled
, work
, all_flags
);
234 free_commit_list(common
);
240 if (array
[i
]->object
.flags
& PARENT2
)
242 for (j
= 0; j
< filled
; j
++)
243 if (work
[j
]->object
.flags
& PARENT1
)
244 redundant
[filled_index
[j
]] = 1;
245 clear_commit_marks(array
[i
], all_flags
);
246 clear_commit_marks_many(filled
, work
, all_flags
);
247 free_commit_list(common
);
250 /* Now collect the result */
251 COPY_ARRAY(work
, array
, cnt
);
252 for (i
= filled
= 0; i
< cnt
; i
++)
254 array
[filled
++] = work
[i
];
261 static int remove_redundant_with_gen(struct repository
*r
,
262 struct commit
**array
, int cnt
)
264 int i
, count_non_stale
= 0, count_still_independent
= cnt
;
265 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
266 struct commit
**walk_start
, **sorted
;
267 size_t walk_start_nr
= 0, walk_start_alloc
= cnt
;
271 * Sort the input by generation number, ascending. This allows
272 * us to increase the "min_generation" limit when we discover
273 * the commit with lowest generation is STALE. The index
274 * min_gen_pos points to the current position within 'array'
275 * that is not yet known to be STALE.
277 DUP_ARRAY(sorted
, array
, cnt
);
278 QSORT(sorted
, cnt
, compare_commits_by_gen
);
279 min_generation
= commit_graph_generation(sorted
[0]);
281 ALLOC_ARRAY(walk_start
, walk_start_alloc
);
283 /* Mark all parents of the input as STALE */
284 for (i
= 0; i
< cnt
; i
++) {
285 struct commit_list
*parents
;
287 repo_parse_commit(r
, array
[i
]);
288 array
[i
]->object
.flags
|= RESULT
;
289 parents
= array
[i
]->parents
;
292 repo_parse_commit(r
, parents
->item
);
293 if (!(parents
->item
->object
.flags
& STALE
)) {
294 parents
->item
->object
.flags
|= STALE
;
295 ALLOC_GROW(walk_start
, walk_start_nr
+ 1, walk_start_alloc
);
296 walk_start
[walk_start_nr
++] = parents
->item
;
298 parents
= parents
->next
;
302 QSORT(walk_start
, walk_start_nr
, compare_commits_by_gen
);
304 /* remove STALE bit for now to allow walking through parents */
305 for (i
= 0; i
< walk_start_nr
; i
++)
306 walk_start
[i
]->object
.flags
&= ~STALE
;
309 * Start walking from the highest generation. Hopefully, it will
310 * find all other items during the first-parent walk, and we can
311 * terminate early. Otherwise, we will do the same amount of work
314 for (i
= walk_start_nr
- 1; i
>= 0 && count_still_independent
> 1; i
--) {
315 /* push the STALE bits up to min generation */
316 struct commit_list
*stack
= NULL
;
318 commit_list_insert(walk_start
[i
], &stack
);
319 walk_start
[i
]->object
.flags
|= STALE
;
322 struct commit_list
*parents
;
323 struct commit
*c
= stack
->item
;
325 repo_parse_commit(r
, c
);
327 if (c
->object
.flags
& RESULT
) {
328 c
->object
.flags
&= ~RESULT
;
329 if (--count_still_independent
<= 1)
331 if (oideq(&c
->object
.oid
, &sorted
[min_gen_pos
]->object
.oid
)) {
332 while (min_gen_pos
< cnt
- 1 &&
333 (sorted
[min_gen_pos
]->object
.flags
& STALE
))
335 min_generation
= commit_graph_generation(sorted
[min_gen_pos
]);
339 if (commit_graph_generation(c
) < min_generation
) {
344 parents
= c
->parents
;
346 if (!(parents
->item
->object
.flags
& STALE
)) {
347 parents
->item
->object
.flags
|= STALE
;
348 commit_list_insert(parents
->item
, &stack
);
351 parents
= parents
->next
;
354 /* pop if all parents have been visited already */
358 free_commit_list(stack
);
363 for (i
= 0; i
< cnt
; i
++)
364 array
[i
]->object
.flags
&= ~RESULT
;
366 /* rearrange array */
367 for (i
= count_non_stale
= 0; i
< cnt
; i
++) {
368 if (!(array
[i
]->object
.flags
& STALE
))
369 array
[count_non_stale
++] = array
[i
];
373 clear_commit_marks_many(walk_start_nr
, walk_start
, STALE
);
376 return count_non_stale
;
379 static int remove_redundant(struct repository
*r
, struct commit
**array
, int cnt
)
382 * Some commit in the array may be an ancestor of
383 * another commit. Move the independent commits to the
384 * beginning of 'array' and return their number. Callers
385 * should not rely upon the contents of 'array' after
388 if (generation_numbers_enabled(r
)) {
392 * If we have a single commit with finite generation
393 * number, then the _with_gen algorithm is preferred.
395 for (i
= 0; i
< cnt
; i
++) {
396 if (commit_graph_generation(array
[i
]) < GENERATION_NUMBER_INFINITY
)
397 return remove_redundant_with_gen(r
, array
, cnt
);
401 return remove_redundant_no_gen(r
, array
, cnt
);
404 static struct commit_list
*get_merge_bases_many_0(struct repository
*r
,
407 struct commit
**twos
,
410 struct commit_list
*list
;
411 struct commit
**rslt
;
412 struct commit_list
*result
;
415 result
= merge_bases_many(r
, one
, n
, twos
);
416 for (i
= 0; i
< n
; i
++) {
420 if (!result
|| !result
->next
) {
422 clear_commit_marks(one
, all_flags
);
423 clear_commit_marks_many(n
, twos
, all_flags
);
428 /* There are more than one */
429 cnt
= commit_list_count(result
);
430 CALLOC_ARRAY(rslt
, cnt
);
431 for (list
= result
, i
= 0; list
; list
= list
->next
)
432 rslt
[i
++] = list
->item
;
433 free_commit_list(result
);
435 clear_commit_marks(one
, all_flags
);
436 clear_commit_marks_many(n
, twos
, all_flags
);
438 cnt
= remove_redundant(r
, rslt
, cnt
);
444 for (i
= 0; i
< cnt
; i
++)
445 commit_list_insert_by_date(rslt
[i
], &result
);
450 struct commit_list
*repo_get_merge_bases_many(struct repository
*r
,
453 struct commit
**twos
)
455 return get_merge_bases_many_0(r
, one
, n
, twos
, 1);
458 struct commit_list
*repo_get_merge_bases_many_dirty(struct repository
*r
,
461 struct commit
**twos
)
463 return get_merge_bases_many_0(r
, one
, n
, twos
, 0);
466 struct commit_list
*repo_get_merge_bases(struct repository
*r
,
470 return get_merge_bases_many_0(r
, one
, 1, &two
, 1);
474 * Is "commit" a descendant of one of the elements on the "with_commit" list?
476 int repo_is_descendant_of(struct repository
*r
,
477 struct commit
*commit
,
478 struct commit_list
*with_commit
)
483 if (generation_numbers_enabled(r
)) {
484 struct commit_list
*from_list
= NULL
;
486 commit_list_insert(commit
, &from_list
);
487 result
= can_all_from_reach(from_list
, with_commit
, 0);
488 free_commit_list(from_list
);
491 while (with_commit
) {
492 struct commit
*other
;
495 other
= with_commit
->item
;
496 with_commit
= with_commit
->next
;
497 ret
= repo_in_merge_bases_many(r
, other
, 1, &commit
, 0);
506 * Is "commit" an ancestor of one of the "references"?
508 int repo_in_merge_bases_many(struct repository
*r
, struct commit
*commit
,
509 int nr_reference
, struct commit
**reference
,
510 int ignore_missing_commits
)
512 struct commit_list
*bases
= NULL
;
514 timestamp_t generation
, max_generation
= GENERATION_NUMBER_ZERO
;
516 if (repo_parse_commit(r
, commit
))
517 return ignore_missing_commits
? 0 : -1;
518 for (i
= 0; i
< nr_reference
; i
++) {
519 if (repo_parse_commit(r
, reference
[i
]))
520 return ignore_missing_commits
? 0 : -1;
522 generation
= commit_graph_generation(reference
[i
]);
523 if (generation
> max_generation
)
524 max_generation
= generation
;
527 generation
= commit_graph_generation(commit
);
528 if (generation
> max_generation
)
531 if (paint_down_to_common(r
, commit
,
532 nr_reference
, reference
,
533 generation
, ignore_missing_commits
, &bases
))
535 else if (commit
->object
.flags
& PARENT2
)
537 clear_commit_marks(commit
, all_flags
);
538 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
539 free_commit_list(bases
);
544 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
546 int repo_in_merge_bases(struct repository
*r
,
547 struct commit
*commit
,
548 struct commit
*reference
)
551 struct commit_list
*list
= NULL
;
552 struct commit_list
**next
= &list
;
554 next
= commit_list_append(commit
, next
);
555 res
= repo_is_descendant_of(r
, reference
, list
);
556 free_commit_list(list
);
561 struct commit_list
*reduce_heads(struct commit_list
*heads
)
563 struct commit_list
*p
;
564 struct commit_list
*result
= NULL
, **tail
= &result
;
565 struct commit
**array
;
572 for (p
= heads
; p
; p
= p
->next
)
573 p
->item
->object
.flags
&= ~STALE
;
574 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
575 if (p
->item
->object
.flags
& STALE
)
577 p
->item
->object
.flags
|= STALE
;
580 CALLOC_ARRAY(array
, num_head
);
581 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
582 if (p
->item
->object
.flags
& STALE
) {
583 array
[i
++] = p
->item
;
584 p
->item
->object
.flags
&= ~STALE
;
587 num_head
= remove_redundant(the_repository
, array
, num_head
);
592 for (i
= 0; i
< num_head
; i
++)
593 tail
= &commit_list_insert(array
[i
], tail
)->next
;
598 void reduce_heads_replace(struct commit_list
**heads
)
600 struct commit_list
*result
= reduce_heads(*heads
);
601 free_commit_list(*heads
);
605 int ref_newer(const struct object_id
*new_oid
, const struct object_id
*old_oid
)
608 struct commit
*old_commit
, *new_commit
;
609 struct commit_list
*old_commit_list
= NULL
;
613 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
614 * old_commit. Otherwise we require --force.
616 o
= deref_tag(the_repository
, parse_object(the_repository
, old_oid
),
618 if (!o
|| o
->type
!= OBJ_COMMIT
)
620 old_commit
= (struct commit
*) o
;
622 o
= deref_tag(the_repository
, parse_object(the_repository
, new_oid
),
624 if (!o
|| o
->type
!= OBJ_COMMIT
)
626 new_commit
= (struct commit
*) o
;
628 if (repo_parse_commit(the_repository
, new_commit
) < 0)
631 commit_list_insert(old_commit
, &old_commit_list
);
632 ret
= repo_is_descendant_of(the_repository
,
633 new_commit
, old_commit_list
);
636 free_commit_list(old_commit_list
);
641 * Mimicking the real stack, this stack lives on the heap, avoiding stack
644 * At each recursion step, the stack items points to the commits whose
645 * ancestors are to be inspected.
647 struct contains_stack
{
649 struct contains_stack_entry
{
650 struct commit
*commit
;
651 struct commit_list
*parents
;
655 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
657 for (; want
; want
= want
->next
)
658 if (oideq(&want
->item
->object
.oid
, &c
->object
.oid
))
664 * Test whether the candidate is contained in the list.
665 * Do not recurse to find out, though, but return -1 if inconclusive.
667 static enum contains_result
contains_test(struct commit
*candidate
,
668 const struct commit_list
*want
,
669 struct contains_cache
*cache
,
672 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
674 /* If we already have the answer cached, return that. */
679 if (in_commit_list(want
, candidate
)) {
680 *cached
= CONTAINS_YES
;
684 /* Otherwise, we don't know; prepare to recurse */
685 parse_commit_or_die(candidate
);
687 if (commit_graph_generation(candidate
) < cutoff
)
690 return CONTAINS_UNKNOWN
;
693 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
695 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
696 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
697 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
700 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
701 const struct commit_list
*want
,
702 struct contains_cache
*cache
)
704 struct contains_stack contains_stack
= { 0, 0, NULL
};
705 enum contains_result result
;
706 timestamp_t cutoff
= GENERATION_NUMBER_INFINITY
;
707 const struct commit_list
*p
;
709 for (p
= want
; p
; p
= p
->next
) {
710 timestamp_t generation
;
711 struct commit
*c
= p
->item
;
712 load_commit_graph_info(the_repository
, c
);
713 generation
= commit_graph_generation(c
);
714 if (generation
< cutoff
)
718 result
= contains_test(candidate
, want
, cache
, cutoff
);
719 if (result
!= CONTAINS_UNKNOWN
)
722 push_to_contains_stack(candidate
, &contains_stack
);
723 while (contains_stack
.nr
) {
724 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
725 struct commit
*commit
= entry
->commit
;
726 struct commit_list
*parents
= entry
->parents
;
729 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
733 * If we just popped the stack, parents->item has been marked,
734 * therefore contains_test will return a meaningful yes/no.
736 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
738 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
742 entry
->parents
= parents
->next
;
744 case CONTAINS_UNKNOWN
:
745 push_to_contains_stack(parents
->item
, &contains_stack
);
749 free(contains_stack
.contains_stack
);
750 return contains_test(candidate
, want
, cache
, cutoff
);
753 int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
754 struct commit_list
*list
, struct contains_cache
*cache
)
756 if (filter
->with_commit_tag_algo
)
757 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
758 return repo_is_descendant_of(the_repository
, commit
, list
);
761 int can_all_from_reach_with_flag(struct object_array
*from
,
762 unsigned int with_flag
,
763 unsigned int assign_flag
,
764 time_t min_commit_date
,
765 timestamp_t min_generation
)
767 struct commit
**list
= NULL
;
772 ALLOC_ARRAY(list
, from
->nr
);
774 for (i
= 0; i
< from
->nr
; i
++) {
775 struct object
*from_one
= from
->objects
[i
].item
;
777 if (!from_one
|| from_one
->flags
& assign_flag
)
780 from_one
= deref_tag(the_repository
, from_one
,
782 if (!from_one
|| from_one
->type
!= OBJ_COMMIT
) {
784 * no way to tell if this is reachable by
785 * looking at the ancestry chain alone, so
786 * leave a note to ourselves not to worry about
787 * this object anymore.
789 from
->objects
[i
].item
->flags
|= assign_flag
;
793 list
[nr_commits
] = (struct commit
*)from_one
;
794 if (repo_parse_commit(the_repository
, list
[nr_commits
]) ||
795 commit_graph_generation(list
[nr_commits
]) < min_generation
) {
803 QSORT(list
, nr_commits
, compare_commits_by_gen
);
805 for (i
= 0; i
< nr_commits
; i
++) {
806 /* DFS from list[i] */
807 struct commit_list
*stack
= NULL
;
809 list
[i
]->object
.flags
|= assign_flag
;
810 commit_list_insert(list
[i
], &stack
);
813 struct commit_list
*parent
;
815 if (stack
->item
->object
.flags
& (with_flag
| RESULT
)) {
818 stack
->item
->object
.flags
|= RESULT
;
822 for (parent
= stack
->item
->parents
; parent
; parent
= parent
->next
) {
823 if (parent
->item
->object
.flags
& (with_flag
| RESULT
))
824 stack
->item
->object
.flags
|= RESULT
;
826 if (!(parent
->item
->object
.flags
& assign_flag
)) {
827 parent
->item
->object
.flags
|= assign_flag
;
829 if (repo_parse_commit(the_repository
, parent
->item
) ||
830 parent
->item
->date
< min_commit_date
||
831 commit_graph_generation(parent
->item
) < min_generation
)
834 commit_list_insert(parent
->item
, &stack
);
843 if (!(list
[i
]->object
.flags
& (with_flag
| RESULT
))) {
850 clear_commit_marks_many(nr_commits
, list
, RESULT
| assign_flag
);
853 for (i
= 0; i
< from
->nr
; i
++) {
854 struct object
*from_one
= from
->objects
[i
].item
;
857 from_one
->flags
&= ~assign_flag
;
863 int can_all_from_reach(struct commit_list
*from
, struct commit_list
*to
,
864 int cutoff_by_min_date
)
866 struct object_array from_objs
= OBJECT_ARRAY_INIT
;
867 time_t min_commit_date
= cutoff_by_min_date
? from
->item
->date
: 0;
868 struct commit_list
*from_iter
= from
, *to_iter
= to
;
870 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
873 add_object_array(&from_iter
->item
->object
, NULL
, &from_objs
);
875 if (!repo_parse_commit(the_repository
, from_iter
->item
)) {
876 timestamp_t generation
;
877 if (from_iter
->item
->date
< min_commit_date
)
878 min_commit_date
= from_iter
->item
->date
;
880 generation
= commit_graph_generation(from_iter
->item
);
881 if (generation
< min_generation
)
882 min_generation
= generation
;
885 from_iter
= from_iter
->next
;
889 if (!repo_parse_commit(the_repository
, to_iter
->item
)) {
890 timestamp_t generation
;
891 if (to_iter
->item
->date
< min_commit_date
)
892 min_commit_date
= to_iter
->item
->date
;
894 generation
= commit_graph_generation(to_iter
->item
);
895 if (generation
< min_generation
)
896 min_generation
= generation
;
899 to_iter
->item
->object
.flags
|= PARENT2
;
901 to_iter
= to_iter
->next
;
904 result
= can_all_from_reach_with_flag(&from_objs
, PARENT2
, PARENT1
,
905 min_commit_date
, min_generation
);
908 clear_commit_marks(from
->item
, PARENT1
);
913 clear_commit_marks(to
->item
, PARENT2
);
917 object_array_clear(&from_objs
);
921 struct commit_list
*get_reachable_subset(struct commit
**from
, int nr_from
,
922 struct commit
**to
, int nr_to
,
923 unsigned int reachable_flag
)
925 struct commit
**item
;
926 struct commit
*current
;
927 struct commit_list
*found_commits
= NULL
;
928 struct commit
**to_last
= to
+ nr_to
;
929 struct commit
**from_last
= from
+ nr_from
;
930 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
933 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
935 for (item
= to
; item
< to_last
; item
++) {
936 timestamp_t generation
;
937 struct commit
*c
= *item
;
939 repo_parse_commit(the_repository
, c
);
940 generation
= commit_graph_generation(c
);
941 if (generation
< min_generation
)
942 min_generation
= generation
;
944 if (!(c
->object
.flags
& PARENT1
)) {
945 c
->object
.flags
|= PARENT1
;
950 for (item
= from
; item
< from_last
; item
++) {
951 struct commit
*c
= *item
;
952 if (!(c
->object
.flags
& PARENT2
)) {
953 c
->object
.flags
|= PARENT2
;
954 repo_parse_commit(the_repository
, c
);
956 prio_queue_put(&queue
, *item
);
960 while (num_to_find
&& (current
= prio_queue_get(&queue
)) != NULL
) {
961 struct commit_list
*parents
;
963 if (current
->object
.flags
& PARENT1
) {
964 current
->object
.flags
&= ~PARENT1
;
965 current
->object
.flags
|= reachable_flag
;
966 commit_list_insert(current
, &found_commits
);
970 for (parents
= current
->parents
; parents
; parents
= parents
->next
) {
971 struct commit
*p
= parents
->item
;
973 repo_parse_commit(the_repository
, p
);
975 if (commit_graph_generation(p
) < min_generation
)
978 if (p
->object
.flags
& PARENT2
)
981 p
->object
.flags
|= PARENT2
;
982 prio_queue_put(&queue
, p
);
986 clear_prio_queue(&queue
);
988 clear_commit_marks_many(nr_to
, to
, PARENT1
);
989 clear_commit_marks_many(nr_from
, from
, PARENT2
);
991 return found_commits
;
994 define_commit_slab(bit_arrays
, struct bitmap
*);
995 static struct bit_arrays bit_arrays
;
997 static void insert_no_dup(struct prio_queue
*queue
, struct commit
*c
)
999 if (c
->object
.flags
& PARENT2
)
1001 prio_queue_put(queue
, c
);
1002 c
->object
.flags
|= PARENT2
;
1005 static struct bitmap
*get_bit_array(struct commit
*c
, int width
)
1007 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
1009 *bitmap
= bitmap_word_alloc(width
);
1013 static void free_bit_array(struct commit
*c
)
1015 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
1018 bitmap_free(*bitmap
);
1022 void ahead_behind(struct repository
*r
,
1023 struct commit
**commits
, size_t commits_nr
,
1024 struct ahead_behind_count
*counts
, size_t counts_nr
)
1026 struct prio_queue queue
= { .compare
= compare_commits_by_gen_then_commit_date
};
1027 size_t width
= DIV_ROUND_UP(commits_nr
, BITS_IN_EWORD
);
1029 if (!commits_nr
|| !counts_nr
)
1032 for (size_t i
= 0; i
< counts_nr
; i
++) {
1033 counts
[i
].ahead
= 0;
1034 counts
[i
].behind
= 0;
1037 ensure_generations_valid(r
, commits
, commits_nr
);
1039 init_bit_arrays(&bit_arrays
);
1041 for (size_t i
= 0; i
< commits_nr
; i
++) {
1042 struct commit
*c
= commits
[i
];
1043 struct bitmap
*bitmap
= get_bit_array(c
, width
);
1045 bitmap_set(bitmap
, i
);
1046 insert_no_dup(&queue
, c
);
1049 while (queue_has_nonstale(&queue
)) {
1050 struct commit
*c
= prio_queue_get(&queue
);
1051 struct commit_list
*p
;
1052 struct bitmap
*bitmap_c
= get_bit_array(c
, width
);
1054 for (size_t i
= 0; i
< counts_nr
; i
++) {
1055 int reach_from_tip
= !!bitmap_get(bitmap_c
, counts
[i
].tip_index
);
1056 int reach_from_base
= !!bitmap_get(bitmap_c
, counts
[i
].base_index
);
1058 if (reach_from_tip
^ reach_from_base
) {
1059 if (reach_from_base
)
1066 for (p
= c
->parents
; p
; p
= p
->next
) {
1067 struct bitmap
*bitmap_p
;
1069 repo_parse_commit(r
, p
->item
);
1071 bitmap_p
= get_bit_array(p
->item
, width
);
1072 bitmap_or(bitmap_p
, bitmap_c
);
1075 * If this parent is reachable from every starting
1076 * commit, then none of its ancestors can contribute
1077 * to the ahead/behind count. Mark it as STALE, so
1078 * we can stop the walk when every commit in the
1081 if (bitmap_popcount(bitmap_p
) == commits_nr
)
1082 p
->item
->object
.flags
|= STALE
;
1084 insert_no_dup(&queue
, p
->item
);
1090 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1091 repo_clear_commit_marks(r
, PARENT2
| STALE
);
1092 clear_bit_arrays(&bit_arrays
);
1093 clear_prio_queue(&queue
);
1096 struct commit_and_index
{
1097 struct commit
*commit
;
1099 timestamp_t generation
;
1102 static int compare_commit_and_index_by_generation(const void *va
, const void *vb
)
1104 const struct commit_and_index
*a
= (const struct commit_and_index
*)va
;
1105 const struct commit_and_index
*b
= (const struct commit_and_index
*)vb
;
1107 if (a
->generation
> b
->generation
)
1109 if (a
->generation
< b
->generation
)
1114 void tips_reachable_from_bases(struct repository
*r
,
1115 struct commit_list
*bases
,
1116 struct commit
**tips
, size_t tips_nr
,
1119 struct commit_and_index
*commits
;
1120 size_t min_generation_index
= 0;
1121 timestamp_t min_generation
;
1122 struct commit_list
*stack
= NULL
;
1124 if (!bases
|| !tips
|| !tips_nr
)
1128 * Do a depth-first search starting at 'bases' to search for the
1129 * tips. Stop at the lowest (un-found) generation number. When
1130 * finding the lowest commit, increase the minimum generation
1131 * number to the next lowest (un-found) generation number.
1134 CALLOC_ARRAY(commits
, tips_nr
);
1136 for (size_t i
= 0; i
< tips_nr
; i
++) {
1137 commits
[i
].commit
= tips
[i
];
1138 commits
[i
].index
= i
;
1139 commits
[i
].generation
= commit_graph_generation(tips
[i
]);
1142 /* Sort with generation number ascending. */
1143 QSORT(commits
, tips_nr
, compare_commit_and_index_by_generation
);
1144 min_generation
= commits
[0].generation
;
1147 repo_parse_commit(r
, bases
->item
);
1148 commit_list_insert(bases
->item
, &stack
);
1149 bases
= bases
->next
;
1153 int explored_all_parents
= 1;
1154 struct commit_list
*p
;
1155 struct commit
*c
= stack
->item
;
1156 timestamp_t c_gen
= commit_graph_generation(c
);
1158 /* Does it match any of our tips? */
1159 for (size_t j
= min_generation_index
; j
< tips_nr
; j
++) {
1160 if (c_gen
< commits
[j
].generation
)
1163 if (commits
[j
].commit
== c
) {
1164 tips
[commits
[j
].index
]->object
.flags
|= mark
;
1166 if (j
== min_generation_index
) {
1167 unsigned int k
= j
+ 1;
1168 while (k
< tips_nr
&&
1169 (tips
[commits
[k
].index
]->object
.flags
& mark
))
1172 /* Terminate early if all found. */
1176 min_generation_index
= k
;
1177 min_generation
= commits
[k
].generation
;
1182 for (p
= c
->parents
; p
; p
= p
->next
) {
1183 repo_parse_commit(r
, p
->item
);
1185 /* Have we already explored this parent? */
1186 if (p
->item
->object
.flags
& SEEN
)
1189 /* Is it below the current minimum generation? */
1190 if (commit_graph_generation(p
->item
) < min_generation
)
1193 /* Ok, we will explore from here on. */
1194 p
->item
->object
.flags
|= SEEN
;
1195 explored_all_parents
= 0;
1196 commit_list_insert(p
->item
, &stack
);
1200 if (explored_all_parents
)
1206 repo_clear_commit_marks(r
, SEEN
);