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
;
468 other
= with_commit
->item
;
469 with_commit
= with_commit
->next
;
470 ret
= repo_in_merge_bases_many(r
, other
, 1, &commit
, 0);
479 * Is "commit" an ancestor of one of the "references"?
481 int repo_in_merge_bases_many(struct repository
*r
, struct commit
*commit
,
482 int nr_reference
, struct commit
**reference
,
483 int ignore_missing_commits
)
485 struct commit_list
*bases
;
487 timestamp_t generation
, max_generation
= GENERATION_NUMBER_ZERO
;
489 if (repo_parse_commit(r
, commit
))
490 return ignore_missing_commits
? 0 : -1;
491 for (i
= 0; i
< nr_reference
; i
++) {
492 if (repo_parse_commit(r
, reference
[i
]))
493 return ignore_missing_commits
? 0 : -1;
495 generation
= commit_graph_generation(reference
[i
]);
496 if (generation
> max_generation
)
497 max_generation
= generation
;
500 generation
= commit_graph_generation(commit
);
501 if (generation
> max_generation
)
504 bases
= paint_down_to_common(r
, commit
,
505 nr_reference
, reference
,
507 if (commit
->object
.flags
& PARENT2
)
509 clear_commit_marks(commit
, all_flags
);
510 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
511 free_commit_list(bases
);
516 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
518 int repo_in_merge_bases(struct repository
*r
,
519 struct commit
*commit
,
520 struct commit
*reference
)
523 struct commit_list
*list
= NULL
;
524 struct commit_list
**next
= &list
;
526 next
= commit_list_append(commit
, next
);
527 res
= repo_is_descendant_of(r
, reference
, list
);
528 free_commit_list(list
);
533 struct commit_list
*reduce_heads(struct commit_list
*heads
)
535 struct commit_list
*p
;
536 struct commit_list
*result
= NULL
, **tail
= &result
;
537 struct commit
**array
;
544 for (p
= heads
; p
; p
= p
->next
)
545 p
->item
->object
.flags
&= ~STALE
;
546 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
547 if (p
->item
->object
.flags
& STALE
)
549 p
->item
->object
.flags
|= STALE
;
552 CALLOC_ARRAY(array
, num_head
);
553 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
554 if (p
->item
->object
.flags
& STALE
) {
555 array
[i
++] = p
->item
;
556 p
->item
->object
.flags
&= ~STALE
;
559 num_head
= remove_redundant(the_repository
, array
, num_head
);
560 for (i
= 0; i
< num_head
; i
++)
561 tail
= &commit_list_insert(array
[i
], tail
)->next
;
566 void reduce_heads_replace(struct commit_list
**heads
)
568 struct commit_list
*result
= reduce_heads(*heads
);
569 free_commit_list(*heads
);
573 int ref_newer(const struct object_id
*new_oid
, const struct object_id
*old_oid
)
576 struct commit
*old_commit
, *new_commit
;
577 struct commit_list
*old_commit_list
= NULL
;
581 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
582 * old_commit. Otherwise we require --force.
584 o
= deref_tag(the_repository
, parse_object(the_repository
, old_oid
),
586 if (!o
|| o
->type
!= OBJ_COMMIT
)
588 old_commit
= (struct commit
*) o
;
590 o
= deref_tag(the_repository
, parse_object(the_repository
, new_oid
),
592 if (!o
|| o
->type
!= OBJ_COMMIT
)
594 new_commit
= (struct commit
*) o
;
596 if (repo_parse_commit(the_repository
, new_commit
) < 0)
599 commit_list_insert(old_commit
, &old_commit_list
);
600 ret
= repo_is_descendant_of(the_repository
,
601 new_commit
, old_commit_list
);
604 free_commit_list(old_commit_list
);
609 * Mimicking the real stack, this stack lives on the heap, avoiding stack
612 * At each recursion step, the stack items points to the commits whose
613 * ancestors are to be inspected.
615 struct contains_stack
{
617 struct contains_stack_entry
{
618 struct commit
*commit
;
619 struct commit_list
*parents
;
623 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
625 for (; want
; want
= want
->next
)
626 if (oideq(&want
->item
->object
.oid
, &c
->object
.oid
))
632 * Test whether the candidate is contained in the list.
633 * Do not recurse to find out, though, but return -1 if inconclusive.
635 static enum contains_result
contains_test(struct commit
*candidate
,
636 const struct commit_list
*want
,
637 struct contains_cache
*cache
,
640 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
642 /* If we already have the answer cached, return that. */
647 if (in_commit_list(want
, candidate
)) {
648 *cached
= CONTAINS_YES
;
652 /* Otherwise, we don't know; prepare to recurse */
653 parse_commit_or_die(candidate
);
655 if (commit_graph_generation(candidate
) < cutoff
)
658 return CONTAINS_UNKNOWN
;
661 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
663 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
664 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
665 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
668 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
669 const struct commit_list
*want
,
670 struct contains_cache
*cache
)
672 struct contains_stack contains_stack
= { 0, 0, NULL
};
673 enum contains_result result
;
674 timestamp_t cutoff
= GENERATION_NUMBER_INFINITY
;
675 const struct commit_list
*p
;
677 for (p
= want
; p
; p
= p
->next
) {
678 timestamp_t generation
;
679 struct commit
*c
= p
->item
;
680 load_commit_graph_info(the_repository
, c
);
681 generation
= commit_graph_generation(c
);
682 if (generation
< cutoff
)
686 result
= contains_test(candidate
, want
, cache
, cutoff
);
687 if (result
!= CONTAINS_UNKNOWN
)
690 push_to_contains_stack(candidate
, &contains_stack
);
691 while (contains_stack
.nr
) {
692 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
693 struct commit
*commit
= entry
->commit
;
694 struct commit_list
*parents
= entry
->parents
;
697 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
701 * If we just popped the stack, parents->item has been marked,
702 * therefore contains_test will return a meaningful yes/no.
704 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
706 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
710 entry
->parents
= parents
->next
;
712 case CONTAINS_UNKNOWN
:
713 push_to_contains_stack(parents
->item
, &contains_stack
);
717 free(contains_stack
.contains_stack
);
718 return contains_test(candidate
, want
, cache
, cutoff
);
721 int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
722 struct commit_list
*list
, struct contains_cache
*cache
)
724 if (filter
->with_commit_tag_algo
)
725 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
726 return repo_is_descendant_of(the_repository
, commit
, list
);
729 int can_all_from_reach_with_flag(struct object_array
*from
,
730 unsigned int with_flag
,
731 unsigned int assign_flag
,
732 time_t min_commit_date
,
733 timestamp_t min_generation
)
735 struct commit
**list
= NULL
;
740 ALLOC_ARRAY(list
, from
->nr
);
742 for (i
= 0; i
< from
->nr
; i
++) {
743 struct object
*from_one
= from
->objects
[i
].item
;
745 if (!from_one
|| from_one
->flags
& assign_flag
)
748 from_one
= deref_tag(the_repository
, from_one
,
750 if (!from_one
|| from_one
->type
!= OBJ_COMMIT
) {
752 * no way to tell if this is reachable by
753 * looking at the ancestry chain alone, so
754 * leave a note to ourselves not to worry about
755 * this object anymore.
757 from
->objects
[i
].item
->flags
|= assign_flag
;
761 list
[nr_commits
] = (struct commit
*)from_one
;
762 if (repo_parse_commit(the_repository
, list
[nr_commits
]) ||
763 commit_graph_generation(list
[nr_commits
]) < min_generation
) {
771 QSORT(list
, nr_commits
, compare_commits_by_gen
);
773 for (i
= 0; i
< nr_commits
; i
++) {
774 /* DFS from list[i] */
775 struct commit_list
*stack
= NULL
;
777 list
[i
]->object
.flags
|= assign_flag
;
778 commit_list_insert(list
[i
], &stack
);
781 struct commit_list
*parent
;
783 if (stack
->item
->object
.flags
& (with_flag
| RESULT
)) {
786 stack
->item
->object
.flags
|= RESULT
;
790 for (parent
= stack
->item
->parents
; parent
; parent
= parent
->next
) {
791 if (parent
->item
->object
.flags
& (with_flag
| RESULT
))
792 stack
->item
->object
.flags
|= RESULT
;
794 if (!(parent
->item
->object
.flags
& assign_flag
)) {
795 parent
->item
->object
.flags
|= assign_flag
;
797 if (repo_parse_commit(the_repository
, parent
->item
) ||
798 parent
->item
->date
< min_commit_date
||
799 commit_graph_generation(parent
->item
) < min_generation
)
802 commit_list_insert(parent
->item
, &stack
);
811 if (!(list
[i
]->object
.flags
& (with_flag
| RESULT
))) {
818 clear_commit_marks_many(nr_commits
, list
, RESULT
| assign_flag
);
821 for (i
= 0; i
< from
->nr
; i
++) {
822 struct object
*from_one
= from
->objects
[i
].item
;
825 from_one
->flags
&= ~assign_flag
;
831 int can_all_from_reach(struct commit_list
*from
, struct commit_list
*to
,
832 int cutoff_by_min_date
)
834 struct object_array from_objs
= OBJECT_ARRAY_INIT
;
835 time_t min_commit_date
= cutoff_by_min_date
? from
->item
->date
: 0;
836 struct commit_list
*from_iter
= from
, *to_iter
= to
;
838 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
841 add_object_array(&from_iter
->item
->object
, NULL
, &from_objs
);
843 if (!repo_parse_commit(the_repository
, from_iter
->item
)) {
844 timestamp_t generation
;
845 if (from_iter
->item
->date
< min_commit_date
)
846 min_commit_date
= from_iter
->item
->date
;
848 generation
= commit_graph_generation(from_iter
->item
);
849 if (generation
< min_generation
)
850 min_generation
= generation
;
853 from_iter
= from_iter
->next
;
857 if (!repo_parse_commit(the_repository
, to_iter
->item
)) {
858 timestamp_t generation
;
859 if (to_iter
->item
->date
< min_commit_date
)
860 min_commit_date
= to_iter
->item
->date
;
862 generation
= commit_graph_generation(to_iter
->item
);
863 if (generation
< min_generation
)
864 min_generation
= generation
;
867 to_iter
->item
->object
.flags
|= PARENT2
;
869 to_iter
= to_iter
->next
;
872 result
= can_all_from_reach_with_flag(&from_objs
, PARENT2
, PARENT1
,
873 min_commit_date
, min_generation
);
876 clear_commit_marks(from
->item
, PARENT1
);
881 clear_commit_marks(to
->item
, PARENT2
);
885 object_array_clear(&from_objs
);
889 struct commit_list
*get_reachable_subset(struct commit
**from
, int nr_from
,
890 struct commit
**to
, int nr_to
,
891 unsigned int reachable_flag
)
893 struct commit
**item
;
894 struct commit
*current
;
895 struct commit_list
*found_commits
= NULL
;
896 struct commit
**to_last
= to
+ nr_to
;
897 struct commit
**from_last
= from
+ nr_from
;
898 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
901 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
903 for (item
= to
; item
< to_last
; item
++) {
904 timestamp_t generation
;
905 struct commit
*c
= *item
;
907 repo_parse_commit(the_repository
, c
);
908 generation
= commit_graph_generation(c
);
909 if (generation
< min_generation
)
910 min_generation
= generation
;
912 if (!(c
->object
.flags
& PARENT1
)) {
913 c
->object
.flags
|= PARENT1
;
918 for (item
= from
; item
< from_last
; item
++) {
919 struct commit
*c
= *item
;
920 if (!(c
->object
.flags
& PARENT2
)) {
921 c
->object
.flags
|= PARENT2
;
922 repo_parse_commit(the_repository
, c
);
924 prio_queue_put(&queue
, *item
);
928 while (num_to_find
&& (current
= prio_queue_get(&queue
)) != NULL
) {
929 struct commit_list
*parents
;
931 if (current
->object
.flags
& PARENT1
) {
932 current
->object
.flags
&= ~PARENT1
;
933 current
->object
.flags
|= reachable_flag
;
934 commit_list_insert(current
, &found_commits
);
938 for (parents
= current
->parents
; parents
; parents
= parents
->next
) {
939 struct commit
*p
= parents
->item
;
941 repo_parse_commit(the_repository
, p
);
943 if (commit_graph_generation(p
) < min_generation
)
946 if (p
->object
.flags
& PARENT2
)
949 p
->object
.flags
|= PARENT2
;
950 prio_queue_put(&queue
, p
);
954 clear_prio_queue(&queue
);
956 clear_commit_marks_many(nr_to
, to
, PARENT1
);
957 clear_commit_marks_many(nr_from
, from
, PARENT2
);
959 return found_commits
;
962 define_commit_slab(bit_arrays
, struct bitmap
*);
963 static struct bit_arrays bit_arrays
;
965 static void insert_no_dup(struct prio_queue
*queue
, struct commit
*c
)
967 if (c
->object
.flags
& PARENT2
)
969 prio_queue_put(queue
, c
);
970 c
->object
.flags
|= PARENT2
;
973 static struct bitmap
*get_bit_array(struct commit
*c
, int width
)
975 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
977 *bitmap
= bitmap_word_alloc(width
);
981 static void free_bit_array(struct commit
*c
)
983 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
986 bitmap_free(*bitmap
);
990 void ahead_behind(struct repository
*r
,
991 struct commit
**commits
, size_t commits_nr
,
992 struct ahead_behind_count
*counts
, size_t counts_nr
)
994 struct prio_queue queue
= { .compare
= compare_commits_by_gen_then_commit_date
};
995 size_t width
= DIV_ROUND_UP(commits_nr
, BITS_IN_EWORD
);
997 if (!commits_nr
|| !counts_nr
)
1000 for (size_t i
= 0; i
< counts_nr
; i
++) {
1001 counts
[i
].ahead
= 0;
1002 counts
[i
].behind
= 0;
1005 ensure_generations_valid(r
, commits
, commits_nr
);
1007 init_bit_arrays(&bit_arrays
);
1009 for (size_t i
= 0; i
< commits_nr
; i
++) {
1010 struct commit
*c
= commits
[i
];
1011 struct bitmap
*bitmap
= get_bit_array(c
, width
);
1013 bitmap_set(bitmap
, i
);
1014 insert_no_dup(&queue
, c
);
1017 while (queue_has_nonstale(&queue
)) {
1018 struct commit
*c
= prio_queue_get(&queue
);
1019 struct commit_list
*p
;
1020 struct bitmap
*bitmap_c
= get_bit_array(c
, width
);
1022 for (size_t i
= 0; i
< counts_nr
; i
++) {
1023 int reach_from_tip
= !!bitmap_get(bitmap_c
, counts
[i
].tip_index
);
1024 int reach_from_base
= !!bitmap_get(bitmap_c
, counts
[i
].base_index
);
1026 if (reach_from_tip
^ reach_from_base
) {
1027 if (reach_from_base
)
1034 for (p
= c
->parents
; p
; p
= p
->next
) {
1035 struct bitmap
*bitmap_p
;
1037 repo_parse_commit(r
, p
->item
);
1039 bitmap_p
= get_bit_array(p
->item
, width
);
1040 bitmap_or(bitmap_p
, bitmap_c
);
1043 * If this parent is reachable from every starting
1044 * commit, then none of its ancestors can contribute
1045 * to the ahead/behind count. Mark it as STALE, so
1046 * we can stop the walk when every commit in the
1049 if (bitmap_popcount(bitmap_p
) == commits_nr
)
1050 p
->item
->object
.flags
|= STALE
;
1052 insert_no_dup(&queue
, p
->item
);
1058 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1059 repo_clear_commit_marks(r
, PARENT2
| STALE
);
1060 clear_bit_arrays(&bit_arrays
);
1061 clear_prio_queue(&queue
);
1064 struct commit_and_index
{
1065 struct commit
*commit
;
1067 timestamp_t generation
;
1070 static int compare_commit_and_index_by_generation(const void *va
, const void *vb
)
1072 const struct commit_and_index
*a
= (const struct commit_and_index
*)va
;
1073 const struct commit_and_index
*b
= (const struct commit_and_index
*)vb
;
1075 if (a
->generation
> b
->generation
)
1077 if (a
->generation
< b
->generation
)
1082 void tips_reachable_from_bases(struct repository
*r
,
1083 struct commit_list
*bases
,
1084 struct commit
**tips
, size_t tips_nr
,
1087 struct commit_and_index
*commits
;
1088 size_t min_generation_index
= 0;
1089 timestamp_t min_generation
;
1090 struct commit_list
*stack
= NULL
;
1092 if (!bases
|| !tips
|| !tips_nr
)
1096 * Do a depth-first search starting at 'bases' to search for the
1097 * tips. Stop at the lowest (un-found) generation number. When
1098 * finding the lowest commit, increase the minimum generation
1099 * number to the next lowest (un-found) generation number.
1102 CALLOC_ARRAY(commits
, tips_nr
);
1104 for (size_t i
= 0; i
< tips_nr
; i
++) {
1105 commits
[i
].commit
= tips
[i
];
1106 commits
[i
].index
= i
;
1107 commits
[i
].generation
= commit_graph_generation(tips
[i
]);
1110 /* Sort with generation number ascending. */
1111 QSORT(commits
, tips_nr
, compare_commit_and_index_by_generation
);
1112 min_generation
= commits
[0].generation
;
1115 repo_parse_commit(r
, bases
->item
);
1116 commit_list_insert(bases
->item
, &stack
);
1117 bases
= bases
->next
;
1121 int explored_all_parents
= 1;
1122 struct commit_list
*p
;
1123 struct commit
*c
= stack
->item
;
1124 timestamp_t c_gen
= commit_graph_generation(c
);
1126 /* Does it match any of our tips? */
1127 for (size_t j
= min_generation_index
; j
< tips_nr
; j
++) {
1128 if (c_gen
< commits
[j
].generation
)
1131 if (commits
[j
].commit
== c
) {
1132 tips
[commits
[j
].index
]->object
.flags
|= mark
;
1134 if (j
== min_generation_index
) {
1135 unsigned int k
= j
+ 1;
1136 while (k
< tips_nr
&&
1137 (tips
[commits
[k
].index
]->object
.flags
& mark
))
1140 /* Terminate early if all found. */
1144 min_generation_index
= k
;
1145 min_generation
= commits
[k
].generation
;
1150 for (p
= c
->parents
; p
; p
= p
->next
) {
1151 repo_parse_commit(r
, p
->item
);
1153 /* Have we already explored this parent? */
1154 if (p
->item
->object
.flags
& SEEN
)
1157 /* Is it below the current minimum generation? */
1158 if (commit_graph_generation(p
->item
) < min_generation
)
1161 /* Ok, we will explore from here on. */
1162 p
->item
->object
.flags
|= SEEN
;
1163 explored_all_parents
= 0;
1164 commit_list_insert(p
->item
, &stack
);
1168 if (explored_all_parents
)
1174 repo_clear_commit_marks(r
, SEEN
);