1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "commit-graph.h"
8 #include "prio-queue.h"
9 #include "ref-filter.h"
12 #include "commit-reach.h"
13 #include "ewah/ewok.h"
15 /* Remember to update object flag allocation in object.h */
16 #define PARENT1 (1u<<16)
17 #define PARENT2 (1u<<17)
18 #define STALE (1u<<18)
19 #define RESULT (1u<<19)
21 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
23 static int compare_commits_by_gen(const void *_a
, const void *_b
)
25 const struct commit
*a
= *(const struct commit
* const *)_a
;
26 const struct commit
*b
= *(const struct commit
* const *)_b
;
28 timestamp_t generation_a
= commit_graph_generation(a
);
29 timestamp_t generation_b
= commit_graph_generation(b
);
31 if (generation_a
< generation_b
)
33 if (generation_a
> generation_b
)
35 if (a
->date
< b
->date
)
37 if (a
->date
> b
->date
)
42 static int queue_has_nonstale(struct prio_queue
*queue
)
45 for (i
= 0; i
< queue
->nr
; i
++) {
46 struct commit
*commit
= queue
->array
[i
].data
;
47 if (!(commit
->object
.flags
& STALE
))
53 /* all input commits in one and twos[] must have been parsed! */
54 static int paint_down_to_common(struct repository
*r
,
55 struct commit
*one
, int n
,
57 timestamp_t min_generation
,
58 int ignore_missing_commits
,
59 struct commit_list
**result
)
61 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
63 timestamp_t last_gen
= GENERATION_NUMBER_INFINITY
;
65 if (!min_generation
&& !corrected_commit_dates_enabled(r
))
66 queue
.compare
= compare_commits_by_commit_date
;
68 one
->object
.flags
|= PARENT1
;
70 commit_list_append(one
, result
);
73 prio_queue_put(&queue
, one
);
75 for (i
= 0; i
< n
; i
++) {
76 twos
[i
]->object
.flags
|= PARENT2
;
77 prio_queue_put(&queue
, twos
[i
]);
80 while (queue_has_nonstale(&queue
)) {
81 struct commit
*commit
= prio_queue_get(&queue
);
82 struct commit_list
*parents
;
84 timestamp_t generation
= commit_graph_generation(commit
);
86 if (min_generation
&& generation
> last_gen
)
87 BUG("bad generation skip %"PRItime
" > %"PRItime
" at %s",
89 oid_to_hex(&commit
->object
.oid
));
90 last_gen
= generation
;
92 if (generation
< min_generation
)
95 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
96 if (flags
== (PARENT1
| PARENT2
)) {
97 if (!(commit
->object
.flags
& RESULT
)) {
98 commit
->object
.flags
|= RESULT
;
99 commit_list_insert_by_date(commit
, result
);
101 /* Mark parents of a found merge stale */
104 parents
= commit
->parents
;
106 struct commit
*p
= parents
->item
;
107 parents
= parents
->next
;
108 if ((p
->object
.flags
& flags
) == flags
)
110 if (repo_parse_commit(r
, p
)) {
111 clear_prio_queue(&queue
);
112 free_commit_list(*result
);
115 * At this stage, we know that the commit is
116 * missing: `repo_parse_commit()` uses
117 * `OBJECT_INFO_DIE_IF_CORRUPT` and therefore
118 * corrupt commits would already have been
119 * dispatched with a `die()`.
121 if (ignore_missing_commits
)
123 return error(_("could not parse commit %s"),
124 oid_to_hex(&p
->object
.oid
));
126 p
->object
.flags
|= flags
;
127 prio_queue_put(&queue
, p
);
131 clear_prio_queue(&queue
);
135 static int merge_bases_many(struct repository
*r
,
136 struct commit
*one
, int n
,
137 struct commit
**twos
,
138 struct commit_list
**result
)
140 struct commit_list
*list
= NULL
;
143 for (i
= 0; i
< n
; i
++) {
144 if (one
== twos
[i
]) {
146 * We do not mark this even with RESULT so we do not
147 * have to clean it up.
149 *result
= commit_list_insert(one
, result
);
156 if (repo_parse_commit(r
, one
))
157 return error(_("could not parse commit %s"),
158 oid_to_hex(&one
->object
.oid
));
159 for (i
= 0; i
< n
; i
++) {
162 if (repo_parse_commit(r
, twos
[i
]))
163 return error(_("could not parse commit %s"),
164 oid_to_hex(&twos
[i
]->object
.oid
));
167 if (paint_down_to_common(r
, one
, n
, twos
, 0, 0, &list
)) {
168 free_commit_list(list
);
173 struct commit
*commit
= pop_commit(&list
);
174 if (!(commit
->object
.flags
& STALE
))
175 commit_list_insert_by_date(commit
, result
);
180 int get_octopus_merge_bases(struct commit_list
*in
, struct commit_list
**result
)
182 struct commit_list
*i
, *j
, *k
;
187 commit_list_insert(in
->item
, result
);
189 for (i
= in
->next
; i
; i
= i
->next
) {
190 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
192 for (j
= *result
; j
; j
= j
->next
) {
193 struct commit_list
*bases
= NULL
;
194 if (repo_get_merge_bases(the_repository
, i
->item
,
195 j
->item
, &bases
) < 0) {
196 free_commit_list(bases
);
197 free_commit_list(*result
);
205 for (k
= bases
; k
; k
= k
->next
)
208 free_commit_list(*result
);
209 *result
= new_commits
;
214 static int remove_redundant_no_gen(struct repository
*r
,
215 struct commit
**array
, int cnt
)
217 struct commit
**work
;
218 unsigned char *redundant
;
222 CALLOC_ARRAY(work
, cnt
);
223 redundant
= xcalloc(cnt
, 1);
224 ALLOC_ARRAY(filled_index
, cnt
- 1);
226 for (i
= 0; i
< cnt
; i
++)
227 repo_parse_commit(r
, array
[i
]);
228 for (i
= 0; i
< cnt
; i
++) {
229 struct commit_list
*common
= NULL
;
230 timestamp_t min_generation
= commit_graph_generation(array
[i
]);
234 for (j
= filled
= 0; j
< cnt
; j
++) {
235 timestamp_t curr_generation
;
236 if (i
== j
|| redundant
[j
])
238 filled_index
[filled
] = j
;
239 work
[filled
++] = array
[j
];
241 curr_generation
= commit_graph_generation(array
[j
]);
242 if (curr_generation
< min_generation
)
243 min_generation
= curr_generation
;
245 if (paint_down_to_common(r
, array
[i
], filled
,
246 work
, min_generation
, 0, &common
)) {
247 clear_commit_marks(array
[i
], all_flags
);
248 clear_commit_marks_many(filled
, work
, all_flags
);
249 free_commit_list(common
);
255 if (array
[i
]->object
.flags
& PARENT2
)
257 for (j
= 0; j
< filled
; j
++)
258 if (work
[j
]->object
.flags
& PARENT1
)
259 redundant
[filled_index
[j
]] = 1;
260 clear_commit_marks(array
[i
], all_flags
);
261 clear_commit_marks_many(filled
, work
, all_flags
);
262 free_commit_list(common
);
265 /* Now collect the result */
266 COPY_ARRAY(work
, array
, cnt
);
267 for (i
= filled
= 0; i
< cnt
; i
++)
269 array
[filled
++] = work
[i
];
276 static int remove_redundant_with_gen(struct repository
*r
,
277 struct commit
**array
, int cnt
)
279 int i
, count_non_stale
= 0, count_still_independent
= cnt
;
280 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
281 struct commit
**walk_start
, **sorted
;
282 size_t walk_start_nr
= 0, walk_start_alloc
= cnt
;
286 * Sort the input by generation number, ascending. This allows
287 * us to increase the "min_generation" limit when we discover
288 * the commit with lowest generation is STALE. The index
289 * min_gen_pos points to the current position within 'array'
290 * that is not yet known to be STALE.
292 DUP_ARRAY(sorted
, array
, cnt
);
293 QSORT(sorted
, cnt
, compare_commits_by_gen
);
294 min_generation
= commit_graph_generation(sorted
[0]);
296 ALLOC_ARRAY(walk_start
, walk_start_alloc
);
298 /* Mark all parents of the input as STALE */
299 for (i
= 0; i
< cnt
; i
++) {
300 struct commit_list
*parents
;
302 repo_parse_commit(r
, array
[i
]);
303 array
[i
]->object
.flags
|= RESULT
;
304 parents
= array
[i
]->parents
;
307 repo_parse_commit(r
, parents
->item
);
308 if (!(parents
->item
->object
.flags
& STALE
)) {
309 parents
->item
->object
.flags
|= STALE
;
310 ALLOC_GROW(walk_start
, walk_start_nr
+ 1, walk_start_alloc
);
311 walk_start
[walk_start_nr
++] = parents
->item
;
313 parents
= parents
->next
;
317 QSORT(walk_start
, walk_start_nr
, compare_commits_by_gen
);
319 /* remove STALE bit for now to allow walking through parents */
320 for (i
= 0; i
< walk_start_nr
; i
++)
321 walk_start
[i
]->object
.flags
&= ~STALE
;
324 * Start walking from the highest generation. Hopefully, it will
325 * find all other items during the first-parent walk, and we can
326 * terminate early. Otherwise, we will do the same amount of work
329 for (i
= walk_start_nr
- 1; i
>= 0 && count_still_independent
> 1; i
--) {
330 /* push the STALE bits up to min generation */
331 struct commit_list
*stack
= NULL
;
333 commit_list_insert(walk_start
[i
], &stack
);
334 walk_start
[i
]->object
.flags
|= STALE
;
337 struct commit_list
*parents
;
338 struct commit
*c
= stack
->item
;
340 repo_parse_commit(r
, c
);
342 if (c
->object
.flags
& RESULT
) {
343 c
->object
.flags
&= ~RESULT
;
344 if (--count_still_independent
<= 1)
346 if (oideq(&c
->object
.oid
, &sorted
[min_gen_pos
]->object
.oid
)) {
347 while (min_gen_pos
< cnt
- 1 &&
348 (sorted
[min_gen_pos
]->object
.flags
& STALE
))
350 min_generation
= commit_graph_generation(sorted
[min_gen_pos
]);
354 if (commit_graph_generation(c
) < min_generation
) {
359 parents
= c
->parents
;
361 if (!(parents
->item
->object
.flags
& STALE
)) {
362 parents
->item
->object
.flags
|= STALE
;
363 commit_list_insert(parents
->item
, &stack
);
366 parents
= parents
->next
;
369 /* pop if all parents have been visited already */
373 free_commit_list(stack
);
378 for (i
= 0; i
< cnt
; i
++)
379 array
[i
]->object
.flags
&= ~RESULT
;
381 /* rearrange array */
382 for (i
= count_non_stale
= 0; i
< cnt
; i
++) {
383 if (!(array
[i
]->object
.flags
& STALE
))
384 array
[count_non_stale
++] = array
[i
];
388 clear_commit_marks_many(walk_start_nr
, walk_start
, STALE
);
391 return count_non_stale
;
394 static int remove_redundant(struct repository
*r
, struct commit
**array
, int cnt
)
397 * Some commit in the array may be an ancestor of
398 * another commit. Move the independent commits to the
399 * beginning of 'array' and return their number. Callers
400 * should not rely upon the contents of 'array' after
403 if (generation_numbers_enabled(r
)) {
407 * If we have a single commit with finite generation
408 * number, then the _with_gen algorithm is preferred.
410 for (i
= 0; i
< cnt
; i
++) {
411 if (commit_graph_generation(array
[i
]) < GENERATION_NUMBER_INFINITY
)
412 return remove_redundant_with_gen(r
, array
, cnt
);
416 return remove_redundant_no_gen(r
, array
, cnt
);
419 static int get_merge_bases_many_0(struct repository
*r
,
422 struct commit
**twos
,
424 struct commit_list
**result
)
426 struct commit_list
*list
;
427 struct commit
**rslt
;
430 if (merge_bases_many(r
, one
, n
, twos
, result
) < 0)
432 for (i
= 0; i
< n
; i
++) {
436 if (!*result
|| !(*result
)->next
) {
438 clear_commit_marks(one
, all_flags
);
439 clear_commit_marks_many(n
, twos
, all_flags
);
444 /* There are more than one */
445 cnt
= commit_list_count(*result
);
446 CALLOC_ARRAY(rslt
, cnt
);
447 for (list
= *result
, i
= 0; list
; list
= list
->next
)
448 rslt
[i
++] = list
->item
;
449 free_commit_list(*result
);
452 clear_commit_marks(one
, all_flags
);
453 clear_commit_marks_many(n
, twos
, all_flags
);
455 cnt
= remove_redundant(r
, rslt
, cnt
);
460 for (i
= 0; i
< cnt
; i
++)
461 commit_list_insert_by_date(rslt
[i
], result
);
466 int repo_get_merge_bases_many(struct repository
*r
,
469 struct commit
**twos
,
470 struct commit_list
**result
)
472 return get_merge_bases_many_0(r
, one
, n
, twos
, 1, result
);
475 int repo_get_merge_bases_many_dirty(struct repository
*r
,
478 struct commit
**twos
,
479 struct commit_list
**result
)
481 return get_merge_bases_many_0(r
, one
, n
, twos
, 0, result
);
484 int repo_get_merge_bases(struct repository
*r
,
487 struct commit_list
**result
)
489 return get_merge_bases_many_0(r
, one
, 1, &two
, 1, result
);
493 * Is "commit" a descendant of one of the elements on the "with_commit" list?
495 int repo_is_descendant_of(struct repository
*r
,
496 struct commit
*commit
,
497 struct commit_list
*with_commit
)
502 if (generation_numbers_enabled(r
)) {
503 struct commit_list
*from_list
= NULL
;
505 commit_list_insert(commit
, &from_list
);
506 result
= can_all_from_reach(from_list
, with_commit
, 0);
507 free_commit_list(from_list
);
510 while (with_commit
) {
511 struct commit
*other
;
514 other
= with_commit
->item
;
515 with_commit
= with_commit
->next
;
516 ret
= repo_in_merge_bases_many(r
, other
, 1, &commit
, 0);
525 * Is "commit" an ancestor of one of the "references"?
527 int repo_in_merge_bases_many(struct repository
*r
, struct commit
*commit
,
528 int nr_reference
, struct commit
**reference
,
529 int ignore_missing_commits
)
531 struct commit_list
*bases
= NULL
;
533 timestamp_t generation
, max_generation
= GENERATION_NUMBER_ZERO
;
535 if (repo_parse_commit(r
, commit
))
536 return ignore_missing_commits
? 0 : -1;
537 for (i
= 0; i
< nr_reference
; i
++) {
538 if (repo_parse_commit(r
, reference
[i
]))
539 return ignore_missing_commits
? 0 : -1;
541 generation
= commit_graph_generation(reference
[i
]);
542 if (generation
> max_generation
)
543 max_generation
= generation
;
546 generation
= commit_graph_generation(commit
);
547 if (generation
> max_generation
)
550 if (paint_down_to_common(r
, commit
,
551 nr_reference
, reference
,
552 generation
, ignore_missing_commits
, &bases
))
554 else if (commit
->object
.flags
& PARENT2
)
556 clear_commit_marks(commit
, all_flags
);
557 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
558 free_commit_list(bases
);
563 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
565 int repo_in_merge_bases(struct repository
*r
,
566 struct commit
*commit
,
567 struct commit
*reference
)
570 struct commit_list
*list
= NULL
;
571 struct commit_list
**next
= &list
;
573 next
= commit_list_append(commit
, next
);
574 res
= repo_is_descendant_of(r
, reference
, list
);
575 free_commit_list(list
);
580 struct commit_list
*reduce_heads(struct commit_list
*heads
)
582 struct commit_list
*p
;
583 struct commit_list
*result
= NULL
, **tail
= &result
;
584 struct commit
**array
;
591 for (p
= heads
; p
; p
= p
->next
)
592 p
->item
->object
.flags
&= ~STALE
;
593 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
594 if (p
->item
->object
.flags
& STALE
)
596 p
->item
->object
.flags
|= STALE
;
599 CALLOC_ARRAY(array
, num_head
);
600 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
601 if (p
->item
->object
.flags
& STALE
) {
602 array
[i
++] = p
->item
;
603 p
->item
->object
.flags
&= ~STALE
;
606 num_head
= remove_redundant(the_repository
, array
, num_head
);
611 for (i
= 0; i
< num_head
; i
++)
612 tail
= &commit_list_insert(array
[i
], tail
)->next
;
617 void reduce_heads_replace(struct commit_list
**heads
)
619 struct commit_list
*result
= reduce_heads(*heads
);
620 free_commit_list(*heads
);
624 int ref_newer(const struct object_id
*new_oid
, const struct object_id
*old_oid
)
627 struct commit
*old_commit
, *new_commit
;
628 struct commit_list
*old_commit_list
= NULL
;
632 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
633 * old_commit. Otherwise we require --force.
635 o
= deref_tag(the_repository
, parse_object(the_repository
, old_oid
),
637 if (!o
|| o
->type
!= OBJ_COMMIT
)
639 old_commit
= (struct commit
*) o
;
641 o
= deref_tag(the_repository
, parse_object(the_repository
, new_oid
),
643 if (!o
|| o
->type
!= OBJ_COMMIT
)
645 new_commit
= (struct commit
*) o
;
647 if (repo_parse_commit(the_repository
, new_commit
) < 0)
650 commit_list_insert(old_commit
, &old_commit_list
);
651 ret
= repo_is_descendant_of(the_repository
,
652 new_commit
, old_commit_list
);
655 free_commit_list(old_commit_list
);
660 * Mimicking the real stack, this stack lives on the heap, avoiding stack
663 * At each recursion step, the stack items points to the commits whose
664 * ancestors are to be inspected.
666 struct contains_stack
{
668 struct contains_stack_entry
{
669 struct commit
*commit
;
670 struct commit_list
*parents
;
674 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
676 for (; want
; want
= want
->next
)
677 if (oideq(&want
->item
->object
.oid
, &c
->object
.oid
))
683 * Test whether the candidate is contained in the list.
684 * Do not recurse to find out, though, but return -1 if inconclusive.
686 static enum contains_result
contains_test(struct commit
*candidate
,
687 const struct commit_list
*want
,
688 struct contains_cache
*cache
,
691 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
693 /* If we already have the answer cached, return that. */
698 if (in_commit_list(want
, candidate
)) {
699 *cached
= CONTAINS_YES
;
703 /* Otherwise, we don't know; prepare to recurse */
704 parse_commit_or_die(candidate
);
706 if (commit_graph_generation(candidate
) < cutoff
)
709 return CONTAINS_UNKNOWN
;
712 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
714 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
715 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
716 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
719 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
720 const struct commit_list
*want
,
721 struct contains_cache
*cache
)
723 struct contains_stack contains_stack
= { 0, 0, NULL
};
724 enum contains_result result
;
725 timestamp_t cutoff
= GENERATION_NUMBER_INFINITY
;
726 const struct commit_list
*p
;
728 for (p
= want
; p
; p
= p
->next
) {
729 timestamp_t generation
;
730 struct commit
*c
= p
->item
;
731 load_commit_graph_info(the_repository
, c
);
732 generation
= commit_graph_generation(c
);
733 if (generation
< cutoff
)
737 result
= contains_test(candidate
, want
, cache
, cutoff
);
738 if (result
!= CONTAINS_UNKNOWN
)
741 push_to_contains_stack(candidate
, &contains_stack
);
742 while (contains_stack
.nr
) {
743 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
744 struct commit
*commit
= entry
->commit
;
745 struct commit_list
*parents
= entry
->parents
;
748 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
752 * If we just popped the stack, parents->item has been marked,
753 * therefore contains_test will return a meaningful yes/no.
755 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
757 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
761 entry
->parents
= parents
->next
;
763 case CONTAINS_UNKNOWN
:
764 push_to_contains_stack(parents
->item
, &contains_stack
);
768 free(contains_stack
.contains_stack
);
769 return contains_test(candidate
, want
, cache
, cutoff
);
772 int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
773 struct commit_list
*list
, struct contains_cache
*cache
)
775 if (filter
->with_commit_tag_algo
)
776 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
777 return repo_is_descendant_of(the_repository
, commit
, list
);
780 int can_all_from_reach_with_flag(struct object_array
*from
,
781 unsigned int with_flag
,
782 unsigned int assign_flag
,
783 time_t min_commit_date
,
784 timestamp_t min_generation
)
786 struct commit
**list
= NULL
;
791 ALLOC_ARRAY(list
, from
->nr
);
793 for (i
= 0; i
< from
->nr
; i
++) {
794 struct object
*from_one
= from
->objects
[i
].item
;
796 if (!from_one
|| from_one
->flags
& assign_flag
)
799 from_one
= deref_tag(the_repository
, from_one
,
801 if (!from_one
|| from_one
->type
!= OBJ_COMMIT
) {
803 * no way to tell if this is reachable by
804 * looking at the ancestry chain alone, so
805 * leave a note to ourselves not to worry about
806 * this object anymore.
808 from
->objects
[i
].item
->flags
|= assign_flag
;
812 list
[nr_commits
] = (struct commit
*)from_one
;
813 if (repo_parse_commit(the_repository
, list
[nr_commits
]) ||
814 commit_graph_generation(list
[nr_commits
]) < min_generation
) {
822 QSORT(list
, nr_commits
, compare_commits_by_gen
);
824 for (i
= 0; i
< nr_commits
; i
++) {
825 /* DFS from list[i] */
826 struct commit_list
*stack
= NULL
;
828 list
[i
]->object
.flags
|= assign_flag
;
829 commit_list_insert(list
[i
], &stack
);
832 struct commit_list
*parent
;
834 if (stack
->item
->object
.flags
& (with_flag
| RESULT
)) {
837 stack
->item
->object
.flags
|= RESULT
;
841 for (parent
= stack
->item
->parents
; parent
; parent
= parent
->next
) {
842 if (parent
->item
->object
.flags
& (with_flag
| RESULT
))
843 stack
->item
->object
.flags
|= RESULT
;
845 if (!(parent
->item
->object
.flags
& assign_flag
)) {
846 parent
->item
->object
.flags
|= assign_flag
;
848 if (repo_parse_commit(the_repository
, parent
->item
) ||
849 parent
->item
->date
< min_commit_date
||
850 commit_graph_generation(parent
->item
) < min_generation
)
853 commit_list_insert(parent
->item
, &stack
);
862 if (!(list
[i
]->object
.flags
& (with_flag
| RESULT
))) {
869 clear_commit_marks_many(nr_commits
, list
, RESULT
| assign_flag
);
872 for (i
= 0; i
< from
->nr
; i
++) {
873 struct object
*from_one
= from
->objects
[i
].item
;
876 from_one
->flags
&= ~assign_flag
;
882 int can_all_from_reach(struct commit_list
*from
, struct commit_list
*to
,
883 int cutoff_by_min_date
)
885 struct object_array from_objs
= OBJECT_ARRAY_INIT
;
886 time_t min_commit_date
= cutoff_by_min_date
? from
->item
->date
: 0;
887 struct commit_list
*from_iter
= from
, *to_iter
= to
;
889 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
892 add_object_array(&from_iter
->item
->object
, NULL
, &from_objs
);
894 if (!repo_parse_commit(the_repository
, from_iter
->item
)) {
895 timestamp_t generation
;
896 if (from_iter
->item
->date
< min_commit_date
)
897 min_commit_date
= from_iter
->item
->date
;
899 generation
= commit_graph_generation(from_iter
->item
);
900 if (generation
< min_generation
)
901 min_generation
= generation
;
904 from_iter
= from_iter
->next
;
908 if (!repo_parse_commit(the_repository
, to_iter
->item
)) {
909 timestamp_t generation
;
910 if (to_iter
->item
->date
< min_commit_date
)
911 min_commit_date
= to_iter
->item
->date
;
913 generation
= commit_graph_generation(to_iter
->item
);
914 if (generation
< min_generation
)
915 min_generation
= generation
;
918 to_iter
->item
->object
.flags
|= PARENT2
;
920 to_iter
= to_iter
->next
;
923 result
= can_all_from_reach_with_flag(&from_objs
, PARENT2
, PARENT1
,
924 min_commit_date
, min_generation
);
927 clear_commit_marks(from
->item
, PARENT1
);
932 clear_commit_marks(to
->item
, PARENT2
);
936 object_array_clear(&from_objs
);
940 struct commit_list
*get_reachable_subset(struct commit
**from
, int nr_from
,
941 struct commit
**to
, int nr_to
,
942 unsigned int reachable_flag
)
944 struct commit
**item
;
945 struct commit
*current
;
946 struct commit_list
*found_commits
= NULL
;
947 struct commit
**to_last
= to
+ nr_to
;
948 struct commit
**from_last
= from
+ nr_from
;
949 timestamp_t min_generation
= GENERATION_NUMBER_INFINITY
;
952 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
954 for (item
= to
; item
< to_last
; item
++) {
955 timestamp_t generation
;
956 struct commit
*c
= *item
;
958 repo_parse_commit(the_repository
, c
);
959 generation
= commit_graph_generation(c
);
960 if (generation
< min_generation
)
961 min_generation
= generation
;
963 if (!(c
->object
.flags
& PARENT1
)) {
964 c
->object
.flags
|= PARENT1
;
969 for (item
= from
; item
< from_last
; item
++) {
970 struct commit
*c
= *item
;
971 if (!(c
->object
.flags
& PARENT2
)) {
972 c
->object
.flags
|= PARENT2
;
973 repo_parse_commit(the_repository
, c
);
975 prio_queue_put(&queue
, *item
);
979 while (num_to_find
&& (current
= prio_queue_get(&queue
)) != NULL
) {
980 struct commit_list
*parents
;
982 if (current
->object
.flags
& PARENT1
) {
983 current
->object
.flags
&= ~PARENT1
;
984 current
->object
.flags
|= reachable_flag
;
985 commit_list_insert(current
, &found_commits
);
989 for (parents
= current
->parents
; parents
; parents
= parents
->next
) {
990 struct commit
*p
= parents
->item
;
992 repo_parse_commit(the_repository
, p
);
994 if (commit_graph_generation(p
) < min_generation
)
997 if (p
->object
.flags
& PARENT2
)
1000 p
->object
.flags
|= PARENT2
;
1001 prio_queue_put(&queue
, p
);
1005 clear_prio_queue(&queue
);
1007 clear_commit_marks_many(nr_to
, to
, PARENT1
);
1008 clear_commit_marks_many(nr_from
, from
, PARENT2
);
1010 return found_commits
;
1013 define_commit_slab(bit_arrays
, struct bitmap
*);
1014 static struct bit_arrays bit_arrays
;
1016 static void insert_no_dup(struct prio_queue
*queue
, struct commit
*c
)
1018 if (c
->object
.flags
& PARENT2
)
1020 prio_queue_put(queue
, c
);
1021 c
->object
.flags
|= PARENT2
;
1024 static struct bitmap
*get_bit_array(struct commit
*c
, int width
)
1026 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
1028 *bitmap
= bitmap_word_alloc(width
);
1032 static void free_bit_array(struct commit
*c
)
1034 struct bitmap
**bitmap
= bit_arrays_at(&bit_arrays
, c
);
1037 bitmap_free(*bitmap
);
1041 void ahead_behind(struct repository
*r
,
1042 struct commit
**commits
, size_t commits_nr
,
1043 struct ahead_behind_count
*counts
, size_t counts_nr
)
1045 struct prio_queue queue
= { .compare
= compare_commits_by_gen_then_commit_date
};
1046 size_t width
= DIV_ROUND_UP(commits_nr
, BITS_IN_EWORD
);
1048 if (!commits_nr
|| !counts_nr
)
1051 for (size_t i
= 0; i
< counts_nr
; i
++) {
1052 counts
[i
].ahead
= 0;
1053 counts
[i
].behind
= 0;
1056 ensure_generations_valid(r
, commits
, commits_nr
);
1058 init_bit_arrays(&bit_arrays
);
1060 for (size_t i
= 0; i
< commits_nr
; i
++) {
1061 struct commit
*c
= commits
[i
];
1062 struct bitmap
*bitmap
= get_bit_array(c
, width
);
1064 bitmap_set(bitmap
, i
);
1065 insert_no_dup(&queue
, c
);
1068 while (queue_has_nonstale(&queue
)) {
1069 struct commit
*c
= prio_queue_get(&queue
);
1070 struct commit_list
*p
;
1071 struct bitmap
*bitmap_c
= get_bit_array(c
, width
);
1073 for (size_t i
= 0; i
< counts_nr
; i
++) {
1074 int reach_from_tip
= !!bitmap_get(bitmap_c
, counts
[i
].tip_index
);
1075 int reach_from_base
= !!bitmap_get(bitmap_c
, counts
[i
].base_index
);
1077 if (reach_from_tip
^ reach_from_base
) {
1078 if (reach_from_base
)
1085 for (p
= c
->parents
; p
; p
= p
->next
) {
1086 struct bitmap
*bitmap_p
;
1088 repo_parse_commit(r
, p
->item
);
1090 bitmap_p
= get_bit_array(p
->item
, width
);
1091 bitmap_or(bitmap_p
, bitmap_c
);
1094 * If this parent is reachable from every starting
1095 * commit, then none of its ancestors can contribute
1096 * to the ahead/behind count. Mark it as STALE, so
1097 * we can stop the walk when every commit in the
1100 if (bitmap_popcount(bitmap_p
) == commits_nr
)
1101 p
->item
->object
.flags
|= STALE
;
1103 insert_no_dup(&queue
, p
->item
);
1109 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1110 repo_clear_commit_marks(r
, PARENT2
| STALE
);
1111 while (prio_queue_peek(&queue
)) {
1112 struct commit
*c
= prio_queue_get(&queue
);
1115 clear_bit_arrays(&bit_arrays
);
1116 clear_prio_queue(&queue
);
1119 struct commit_and_index
{
1120 struct commit
*commit
;
1122 timestamp_t generation
;
1125 static int compare_commit_and_index_by_generation(const void *va
, const void *vb
)
1127 const struct commit_and_index
*a
= (const struct commit_and_index
*)va
;
1128 const struct commit_and_index
*b
= (const struct commit_and_index
*)vb
;
1130 if (a
->generation
> b
->generation
)
1132 if (a
->generation
< b
->generation
)
1137 void tips_reachable_from_bases(struct repository
*r
,
1138 struct commit_list
*bases
,
1139 struct commit
**tips
, size_t tips_nr
,
1142 struct commit_and_index
*commits
;
1143 size_t min_generation_index
= 0;
1144 timestamp_t min_generation
;
1145 struct commit_list
*stack
= NULL
;
1147 if (!bases
|| !tips
|| !tips_nr
)
1151 * Do a depth-first search starting at 'bases' to search for the
1152 * tips. Stop at the lowest (un-found) generation number. When
1153 * finding the lowest commit, increase the minimum generation
1154 * number to the next lowest (un-found) generation number.
1157 CALLOC_ARRAY(commits
, tips_nr
);
1159 for (size_t i
= 0; i
< tips_nr
; i
++) {
1160 commits
[i
].commit
= tips
[i
];
1161 commits
[i
].index
= i
;
1162 commits
[i
].generation
= commit_graph_generation(tips
[i
]);
1165 /* Sort with generation number ascending. */
1166 QSORT(commits
, tips_nr
, compare_commit_and_index_by_generation
);
1167 min_generation
= commits
[0].generation
;
1170 repo_parse_commit(r
, bases
->item
);
1171 commit_list_insert(bases
->item
, &stack
);
1172 bases
= bases
->next
;
1176 int explored_all_parents
= 1;
1177 struct commit_list
*p
;
1178 struct commit
*c
= stack
->item
;
1179 timestamp_t c_gen
= commit_graph_generation(c
);
1181 /* Does it match any of our tips? */
1182 for (size_t j
= min_generation_index
; j
< tips_nr
; j
++) {
1183 if (c_gen
< commits
[j
].generation
)
1186 if (commits
[j
].commit
== c
) {
1187 tips
[commits
[j
].index
]->object
.flags
|= mark
;
1189 if (j
== min_generation_index
) {
1190 unsigned int k
= j
+ 1;
1191 while (k
< tips_nr
&&
1192 (tips
[commits
[k
].index
]->object
.flags
& mark
))
1195 /* Terminate early if all found. */
1199 min_generation_index
= k
;
1200 min_generation
= commits
[k
].generation
;
1205 for (p
= c
->parents
; p
; p
= p
->next
) {
1206 repo_parse_commit(r
, p
->item
);
1208 /* Have we already explored this parent? */
1209 if (p
->item
->object
.flags
& SEEN
)
1212 /* Is it below the current minimum generation? */
1213 if (commit_graph_generation(p
->item
) < min_generation
)
1216 /* Ok, we will explore from here on. */
1217 p
->item
->object
.flags
|= SEEN
;
1218 explored_all_parents
= 0;
1219 commit_list_insert(p
->item
, &stack
);
1223 if (explored_all_parents
)
1229 repo_clear_commit_marks(r
, SEEN
);
1230 free_commit_list(stack
);
1234 * This slab initializes integers to zero, so use "-1" for "tip is best" and
1235 * "i + 1" for "bases[i] is best".
1237 define_commit_slab(best_branch_base
, int);
1238 static struct best_branch_base best_branch_base
;
1239 #define get_best(c) (*best_branch_base_at(&best_branch_base, (c)))
1240 #define set_best(c,v) (*best_branch_base_at(&best_branch_base, (c)) = (v))
1242 int get_branch_base_for_tip(struct repository
*r
,
1244 struct commit
**bases
,
1247 int best_index
= -1;
1248 struct commit
*branch_point
= NULL
;
1249 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
1250 int found_missing_gen
= 0;
1255 repo_parse_commit(r
, tip
);
1256 if (commit_graph_generation(tip
) == GENERATION_NUMBER_INFINITY
)
1257 found_missing_gen
= 1;
1259 /* Check for missing generation numbers. */
1260 for (size_t i
= 0; i
< bases_nr
; i
++) {
1261 struct commit
*c
= bases
[i
];
1262 repo_parse_commit(r
, c
);
1263 if (commit_graph_generation(c
) == GENERATION_NUMBER_INFINITY
)
1264 found_missing_gen
= 1;
1267 if (found_missing_gen
) {
1268 struct commit
**commits
;
1269 size_t commits_nr
= bases_nr
+ 1;
1271 CALLOC_ARRAY(commits
, commits_nr
);
1272 COPY_ARRAY(commits
, bases
, bases_nr
);
1273 commits
[bases_nr
] = tip
;
1274 ensure_generations_valid(r
, commits
, commits_nr
);
1278 /* Initialize queue and slab now that generations are guaranteed. */
1279 init_best_branch_base(&best_branch_base
);
1281 prio_queue_put(&queue
, tip
);
1283 for (size_t i
= 0; i
< bases_nr
; i
++) {
1284 struct commit
*c
= bases
[i
];
1285 int best
= get_best(c
);
1287 /* Has this already been marked as best by another commit? */
1290 /* We agree at this position. Stop now. */
1298 prio_queue_put(&queue
, c
);
1302 struct commit
*c
= prio_queue_get(&queue
);
1303 int best_for_c
= get_best(c
);
1304 int best_for_p
, positive
;
1305 struct commit
*parent
;
1307 /* Have we reached a known branch point? It's optimal. */
1308 if (c
== branch_point
)
1311 repo_parse_commit(r
, c
);
1315 parent
= c
->parents
->item
;
1316 repo_parse_commit(r
, parent
);
1317 best_for_p
= get_best(parent
);
1320 /* 'parent' is new, so pass along best_for_c. */
1321 set_best(parent
, best_for_c
);
1322 prio_queue_put(&queue
, parent
);
1326 if (best_for_p
> 0 && best_for_c
> 0) {
1327 /* Collision among bases. Minimize. */
1328 if (best_for_c
< best_for_p
)
1329 set_best(parent
, best_for_c
);
1334 * At this point, we have reached a commit that is reachable
1335 * from the tip, either from 'c' or from an earlier commit to
1336 * have 'parent' as its first parent.
1338 * Update 'best_index' to match the minimum of all base indices
1339 * to reach 'parent'.
1342 /* Exactly one is positive due to initial conditions. */
1343 positive
= (best_for_c
< 0) ? best_for_p
: best_for_c
;
1345 if (best_index
< 0 || positive
< best_index
)
1346 best_index
= positive
;
1348 /* No matter what, track that the parent is reachable from tip. */
1349 set_best(parent
, -1);
1350 branch_point
= parent
;
1354 clear_best_branch_base(&best_branch_base
);
1355 clear_prio_queue(&queue
);
1356 return best_index
> 0 ? best_index
- 1 : -1;