Merge branch 'ak/restore-both-incompatible-with-conflicts'
[git/debian.git] / commit-reach.c
blob7c0c39fd2864c2db72b81adc4c0b1b6d7cf0337e
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "commit.h"
4 #include "commit-graph.h"
5 #include "decorate.h"
6 #include "hex.h"
7 #include "prio-queue.h"
8 #include "tree.h"
9 #include "ref-filter.h"
10 #include "revision.h"
11 #include "tag.h"
12 #include "commit-reach.h"
14 /* Remember to update object flag allocation in object.h */
15 #define PARENT1 (1u<<16)
16 #define PARENT2 (1u<<17)
17 #define STALE (1u<<18)
18 #define RESULT (1u<<19)
20 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
22 static int compare_commits_by_gen(const void *_a, const void *_b)
24 const struct commit *a = *(const struct commit * const *)_a;
25 const struct commit *b = *(const struct commit * const *)_b;
27 timestamp_t generation_a = commit_graph_generation(a);
28 timestamp_t generation_b = commit_graph_generation(b);
30 if (generation_a < generation_b)
31 return -1;
32 if (generation_a > generation_b)
33 return 1;
34 if (a->date < b->date)
35 return -1;
36 if (a->date > b->date)
37 return 1;
38 return 0;
41 static int queue_has_nonstale(struct prio_queue *queue)
43 int i;
44 for (i = 0; i < queue->nr; i++) {
45 struct commit *commit = queue->array[i].data;
46 if (!(commit->object.flags & STALE))
47 return 1;
49 return 0;
52 /* all input commits in one and twos[] must have been parsed! */
53 static struct commit_list *paint_down_to_common(struct repository *r,
54 struct commit *one, int n,
55 struct commit **twos,
56 timestamp_t min_generation)
58 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
59 struct commit_list *result = NULL;
60 int i;
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;
67 if (!n) {
68 commit_list_append(one, &result);
69 return 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;
81 int flags;
82 timestamp_t generation = commit_graph_generation(commit);
84 if (min_generation && generation > last_gen)
85 BUG("bad generation skip %"PRItime" > %"PRItime" at %s",
86 generation, last_gen,
87 oid_to_hex(&commit->object.oid));
88 last_gen = generation;
90 if (generation < min_generation)
91 break;
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 */
100 flags |= STALE;
102 parents = commit->parents;
103 while (parents) {
104 struct commit *p = parents->item;
105 parents = parents->next;
106 if ((p->object.flags & flags) == flags)
107 continue;
108 if (repo_parse_commit(r, p))
109 return NULL;
110 p->object.flags |= flags;
111 prio_queue_put(&queue, p);
115 clear_prio_queue(&queue);
116 return result;
119 static struct commit_list *merge_bases_many(struct repository *r,
120 struct commit *one, int n,
121 struct commit **twos)
123 struct commit_list *list = NULL;
124 struct commit_list *result = NULL;
125 int i;
127 for (i = 0; i < n; i++) {
128 if (one == twos[i])
130 * We do not mark this even with RESULT so we do not
131 * have to clean it up.
133 return commit_list_insert(one, &result);
136 if (repo_parse_commit(r, one))
137 return NULL;
138 for (i = 0; i < n; i++) {
139 if (repo_parse_commit(r, twos[i]))
140 return NULL;
143 list = paint_down_to_common(r, one, n, twos, 0);
145 while (list) {
146 struct commit *commit = pop_commit(&list);
147 if (!(commit->object.flags & STALE))
148 commit_list_insert_by_date(commit, &result);
150 return result;
153 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
155 struct commit_list *i, *j, *k, *ret = NULL;
157 if (!in)
158 return ret;
160 commit_list_insert(in->item, &ret);
162 for (i = in->next; i; i = i->next) {
163 struct commit_list *new_commits = NULL, *end = NULL;
165 for (j = ret; j; j = j->next) {
166 struct commit_list *bases;
167 bases = get_merge_bases(i->item, j->item);
168 if (!new_commits)
169 new_commits = bases;
170 else
171 end->next = bases;
172 for (k = bases; k; k = k->next)
173 end = k;
175 ret = new_commits;
177 return ret;
180 static int remove_redundant_no_gen(struct repository *r,
181 struct commit **array, int cnt)
183 struct commit **work;
184 unsigned char *redundant;
185 int *filled_index;
186 int i, j, filled;
188 CALLOC_ARRAY(work, cnt);
189 redundant = xcalloc(cnt, 1);
190 ALLOC_ARRAY(filled_index, cnt - 1);
192 for (i = 0; i < cnt; i++)
193 repo_parse_commit(r, array[i]);
194 for (i = 0; i < cnt; i++) {
195 struct commit_list *common;
196 timestamp_t min_generation = commit_graph_generation(array[i]);
198 if (redundant[i])
199 continue;
200 for (j = filled = 0; j < cnt; j++) {
201 timestamp_t curr_generation;
202 if (i == j || redundant[j])
203 continue;
204 filled_index[filled] = j;
205 work[filled++] = array[j];
207 curr_generation = commit_graph_generation(array[j]);
208 if (curr_generation < min_generation)
209 min_generation = curr_generation;
211 common = paint_down_to_common(r, array[i], filled,
212 work, min_generation);
213 if (array[i]->object.flags & PARENT2)
214 redundant[i] = 1;
215 for (j = 0; j < filled; j++)
216 if (work[j]->object.flags & PARENT1)
217 redundant[filled_index[j]] = 1;
218 clear_commit_marks(array[i], all_flags);
219 clear_commit_marks_many(filled, work, all_flags);
220 free_commit_list(common);
223 /* Now collect the result */
224 COPY_ARRAY(work, array, cnt);
225 for (i = filled = 0; i < cnt; i++)
226 if (!redundant[i])
227 array[filled++] = work[i];
228 free(work);
229 free(redundant);
230 free(filled_index);
231 return filled;
234 static int remove_redundant_with_gen(struct repository *r,
235 struct commit **array, int cnt)
237 int i, count_non_stale = 0, count_still_independent = cnt;
238 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
239 struct commit **walk_start, **sorted;
240 size_t walk_start_nr = 0, walk_start_alloc = cnt;
241 int min_gen_pos = 0;
244 * Sort the input by generation number, ascending. This allows
245 * us to increase the "min_generation" limit when we discover
246 * the commit with lowest generation is STALE. The index
247 * min_gen_pos points to the current position within 'array'
248 * that is not yet known to be STALE.
250 DUP_ARRAY(sorted, array, cnt);
251 QSORT(sorted, cnt, compare_commits_by_gen);
252 min_generation = commit_graph_generation(sorted[0]);
254 ALLOC_ARRAY(walk_start, walk_start_alloc);
256 /* Mark all parents of the input as STALE */
257 for (i = 0; i < cnt; i++) {
258 struct commit_list *parents;
260 repo_parse_commit(r, array[i]);
261 array[i]->object.flags |= RESULT;
262 parents = array[i]->parents;
264 while (parents) {
265 repo_parse_commit(r, parents->item);
266 if (!(parents->item->object.flags & STALE)) {
267 parents->item->object.flags |= STALE;
268 ALLOC_GROW(walk_start, walk_start_nr + 1, walk_start_alloc);
269 walk_start[walk_start_nr++] = parents->item;
271 parents = parents->next;
275 QSORT(walk_start, walk_start_nr, compare_commits_by_gen);
277 /* remove STALE bit for now to allow walking through parents */
278 for (i = 0; i < walk_start_nr; i++)
279 walk_start[i]->object.flags &= ~STALE;
282 * Start walking from the highest generation. Hopefully, it will
283 * find all other items during the first-parent walk, and we can
284 * terminate early. Otherwise, we will do the same amount of work
285 * as before.
287 for (i = walk_start_nr - 1; i >= 0 && count_still_independent > 1; i--) {
288 /* push the STALE bits up to min generation */
289 struct commit_list *stack = NULL;
291 commit_list_insert(walk_start[i], &stack);
292 walk_start[i]->object.flags |= STALE;
294 while (stack) {
295 struct commit_list *parents;
296 struct commit *c = stack->item;
298 repo_parse_commit(r, c);
300 if (c->object.flags & RESULT) {
301 c->object.flags &= ~RESULT;
302 if (--count_still_independent <= 1)
303 break;
304 if (oideq(&c->object.oid, &sorted[min_gen_pos]->object.oid)) {
305 while (min_gen_pos < cnt - 1 &&
306 (sorted[min_gen_pos]->object.flags & STALE))
307 min_gen_pos++;
308 min_generation = commit_graph_generation(sorted[min_gen_pos]);
312 if (commit_graph_generation(c) < min_generation) {
313 pop_commit(&stack);
314 continue;
317 parents = c->parents;
318 while (parents) {
319 if (!(parents->item->object.flags & STALE)) {
320 parents->item->object.flags |= STALE;
321 commit_list_insert(parents->item, &stack);
322 break;
324 parents = parents->next;
327 /* pop if all parents have been visited already */
328 if (!parents)
329 pop_commit(&stack);
331 free_commit_list(stack);
333 free(sorted);
335 /* clear result */
336 for (i = 0; i < cnt; i++)
337 array[i]->object.flags &= ~RESULT;
339 /* rearrange array */
340 for (i = count_non_stale = 0; i < cnt; i++) {
341 if (!(array[i]->object.flags & STALE))
342 array[count_non_stale++] = array[i];
345 /* clear marks */
346 clear_commit_marks_many(walk_start_nr, walk_start, STALE);
347 free(walk_start);
349 return count_non_stale;
352 static int remove_redundant(struct repository *r, struct commit **array, int cnt)
355 * Some commit in the array may be an ancestor of
356 * another commit. Move the independent commits to the
357 * beginning of 'array' and return their number. Callers
358 * should not rely upon the contents of 'array' after
359 * that number.
361 if (generation_numbers_enabled(r)) {
362 int i;
365 * If we have a single commit with finite generation
366 * number, then the _with_gen algorithm is preferred.
368 for (i = 0; i < cnt; i++) {
369 if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY)
370 return remove_redundant_with_gen(r, array, cnt);
374 return remove_redundant_no_gen(r, array, cnt);
377 static struct commit_list *get_merge_bases_many_0(struct repository *r,
378 struct commit *one,
379 int n,
380 struct commit **twos,
381 int cleanup)
383 struct commit_list *list;
384 struct commit **rslt;
385 struct commit_list *result;
386 int cnt, i;
388 result = merge_bases_many(r, one, n, twos);
389 for (i = 0; i < n; i++) {
390 if (one == twos[i])
391 return result;
393 if (!result || !result->next) {
394 if (cleanup) {
395 clear_commit_marks(one, all_flags);
396 clear_commit_marks_many(n, twos, all_flags);
398 return result;
401 /* There are more than one */
402 cnt = commit_list_count(result);
403 CALLOC_ARRAY(rslt, cnt);
404 for (list = result, i = 0; list; list = list->next)
405 rslt[i++] = list->item;
406 free_commit_list(result);
408 clear_commit_marks(one, all_flags);
409 clear_commit_marks_many(n, twos, all_flags);
411 cnt = remove_redundant(r, rslt, cnt);
412 result = NULL;
413 for (i = 0; i < cnt; i++)
414 commit_list_insert_by_date(rslt[i], &result);
415 free(rslt);
416 return result;
419 struct commit_list *repo_get_merge_bases_many(struct repository *r,
420 struct commit *one,
421 int n,
422 struct commit **twos)
424 return get_merge_bases_many_0(r, one, n, twos, 1);
427 struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
428 struct commit *one,
429 int n,
430 struct commit **twos)
432 return get_merge_bases_many_0(r, one, n, twos, 0);
435 struct commit_list *repo_get_merge_bases(struct repository *r,
436 struct commit *one,
437 struct commit *two)
439 return get_merge_bases_many_0(r, one, 1, &two, 1);
443 * Is "commit" a descendant of one of the elements on the "with_commit" list?
445 int repo_is_descendant_of(struct repository *r,
446 struct commit *commit,
447 struct commit_list *with_commit)
449 if (!with_commit)
450 return 1;
452 if (generation_numbers_enabled(the_repository)) {
453 struct commit_list *from_list = NULL;
454 int result;
455 commit_list_insert(commit, &from_list);
456 result = can_all_from_reach(from_list, with_commit, 0);
457 free_commit_list(from_list);
458 return result;
459 } else {
460 while (with_commit) {
461 struct commit *other;
463 other = with_commit->item;
464 with_commit = with_commit->next;
465 if (repo_in_merge_bases_many(r, other, 1, &commit))
466 return 1;
468 return 0;
473 * Is "commit" an ancestor of one of the "references"?
475 int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
476 int nr_reference, struct commit **reference)
478 struct commit_list *bases;
479 int ret = 0, i;
480 timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
482 if (repo_parse_commit(r, commit))
483 return ret;
484 for (i = 0; i < nr_reference; i++) {
485 if (repo_parse_commit(r, reference[i]))
486 return ret;
488 generation = commit_graph_generation(reference[i]);
489 if (generation > max_generation)
490 max_generation = generation;
493 generation = commit_graph_generation(commit);
494 if (generation > max_generation)
495 return ret;
497 bases = paint_down_to_common(r, commit,
498 nr_reference, reference,
499 generation);
500 if (commit->object.flags & PARENT2)
501 ret = 1;
502 clear_commit_marks(commit, all_flags);
503 clear_commit_marks_many(nr_reference, reference, all_flags);
504 free_commit_list(bases);
505 return ret;
509 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
511 int repo_in_merge_bases(struct repository *r,
512 struct commit *commit,
513 struct commit *reference)
515 int res;
516 struct commit_list *list = NULL;
517 struct commit_list **next = &list;
519 next = commit_list_append(commit, next);
520 res = repo_is_descendant_of(r, reference, list);
521 free_commit_list(list);
523 return res;
526 struct commit_list *reduce_heads(struct commit_list *heads)
528 struct commit_list *p;
529 struct commit_list *result = NULL, **tail = &result;
530 struct commit **array;
531 int num_head, i;
533 if (!heads)
534 return NULL;
536 /* Uniquify */
537 for (p = heads; p; p = p->next)
538 p->item->object.flags &= ~STALE;
539 for (p = heads, num_head = 0; p; p = p->next) {
540 if (p->item->object.flags & STALE)
541 continue;
542 p->item->object.flags |= STALE;
543 num_head++;
545 CALLOC_ARRAY(array, num_head);
546 for (p = heads, i = 0; p; p = p->next) {
547 if (p->item->object.flags & STALE) {
548 array[i++] = p->item;
549 p->item->object.flags &= ~STALE;
552 num_head = remove_redundant(the_repository, array, num_head);
553 for (i = 0; i < num_head; i++)
554 tail = &commit_list_insert(array[i], tail)->next;
555 free(array);
556 return result;
559 void reduce_heads_replace(struct commit_list **heads)
561 struct commit_list *result = reduce_heads(*heads);
562 free_commit_list(*heads);
563 *heads = result;
566 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
568 struct object *o;
569 struct commit *old_commit, *new_commit;
570 struct commit_list *old_commit_list = NULL;
571 int ret;
574 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
575 * old_commit. Otherwise we require --force.
577 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
578 NULL, 0);
579 if (!o || o->type != OBJ_COMMIT)
580 return 0;
581 old_commit = (struct commit *) o;
583 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
584 NULL, 0);
585 if (!o || o->type != OBJ_COMMIT)
586 return 0;
587 new_commit = (struct commit *) o;
589 if (parse_commit(new_commit) < 0)
590 return 0;
592 commit_list_insert(old_commit, &old_commit_list);
593 ret = repo_is_descendant_of(the_repository,
594 new_commit, old_commit_list);
595 free_commit_list(old_commit_list);
596 return ret;
600 * Mimicking the real stack, this stack lives on the heap, avoiding stack
601 * overflows.
603 * At each recursion step, the stack items points to the commits whose
604 * ancestors are to be inspected.
606 struct contains_stack {
607 int nr, alloc;
608 struct contains_stack_entry {
609 struct commit *commit;
610 struct commit_list *parents;
611 } *contains_stack;
614 static int in_commit_list(const struct commit_list *want, struct commit *c)
616 for (; want; want = want->next)
617 if (oideq(&want->item->object.oid, &c->object.oid))
618 return 1;
619 return 0;
623 * Test whether the candidate is contained in the list.
624 * Do not recurse to find out, though, but return -1 if inconclusive.
626 static enum contains_result contains_test(struct commit *candidate,
627 const struct commit_list *want,
628 struct contains_cache *cache,
629 timestamp_t cutoff)
631 enum contains_result *cached = contains_cache_at(cache, candidate);
633 /* If we already have the answer cached, return that. */
634 if (*cached)
635 return *cached;
637 /* or are we it? */
638 if (in_commit_list(want, candidate)) {
639 *cached = CONTAINS_YES;
640 return CONTAINS_YES;
643 /* Otherwise, we don't know; prepare to recurse */
644 parse_commit_or_die(candidate);
646 if (commit_graph_generation(candidate) < cutoff)
647 return CONTAINS_NO;
649 return CONTAINS_UNKNOWN;
652 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
654 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
655 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
656 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
659 static enum contains_result contains_tag_algo(struct commit *candidate,
660 const struct commit_list *want,
661 struct contains_cache *cache)
663 struct contains_stack contains_stack = { 0, 0, NULL };
664 enum contains_result result;
665 timestamp_t cutoff = GENERATION_NUMBER_INFINITY;
666 const struct commit_list *p;
668 for (p = want; p; p = p->next) {
669 timestamp_t generation;
670 struct commit *c = p->item;
671 load_commit_graph_info(the_repository, c);
672 generation = commit_graph_generation(c);
673 if (generation < cutoff)
674 cutoff = generation;
677 result = contains_test(candidate, want, cache, cutoff);
678 if (result != CONTAINS_UNKNOWN)
679 return result;
681 push_to_contains_stack(candidate, &contains_stack);
682 while (contains_stack.nr) {
683 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
684 struct commit *commit = entry->commit;
685 struct commit_list *parents = entry->parents;
687 if (!parents) {
688 *contains_cache_at(cache, commit) = CONTAINS_NO;
689 contains_stack.nr--;
692 * If we just popped the stack, parents->item has been marked,
693 * therefore contains_test will return a meaningful yes/no.
695 else switch (contains_test(parents->item, want, cache, cutoff)) {
696 case CONTAINS_YES:
697 *contains_cache_at(cache, commit) = CONTAINS_YES;
698 contains_stack.nr--;
699 break;
700 case CONTAINS_NO:
701 entry->parents = parents->next;
702 break;
703 case CONTAINS_UNKNOWN:
704 push_to_contains_stack(parents->item, &contains_stack);
705 break;
708 free(contains_stack.contains_stack);
709 return contains_test(candidate, want, cache, cutoff);
712 int commit_contains(struct ref_filter *filter, struct commit *commit,
713 struct commit_list *list, struct contains_cache *cache)
715 if (filter->with_commit_tag_algo)
716 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
717 return repo_is_descendant_of(the_repository, commit, list);
720 int can_all_from_reach_with_flag(struct object_array *from,
721 unsigned int with_flag,
722 unsigned int assign_flag,
723 time_t min_commit_date,
724 timestamp_t min_generation)
726 struct commit **list = NULL;
727 int i;
728 int nr_commits;
729 int result = 1;
731 ALLOC_ARRAY(list, from->nr);
732 nr_commits = 0;
733 for (i = 0; i < from->nr; i++) {
734 struct object *from_one = from->objects[i].item;
736 if (!from_one || from_one->flags & assign_flag)
737 continue;
739 from_one = deref_tag(the_repository, from_one,
740 "a from object", 0);
741 if (!from_one || from_one->type != OBJ_COMMIT) {
743 * no way to tell if this is reachable by
744 * looking at the ancestry chain alone, so
745 * leave a note to ourselves not to worry about
746 * this object anymore.
748 from->objects[i].item->flags |= assign_flag;
749 continue;
752 list[nr_commits] = (struct commit *)from_one;
753 if (parse_commit(list[nr_commits]) ||
754 commit_graph_generation(list[nr_commits]) < min_generation) {
755 result = 0;
756 goto cleanup;
759 nr_commits++;
762 QSORT(list, nr_commits, compare_commits_by_gen);
764 for (i = 0; i < nr_commits; i++) {
765 /* DFS from list[i] */
766 struct commit_list *stack = NULL;
768 list[i]->object.flags |= assign_flag;
769 commit_list_insert(list[i], &stack);
771 while (stack) {
772 struct commit_list *parent;
774 if (stack->item->object.flags & (with_flag | RESULT)) {
775 pop_commit(&stack);
776 if (stack)
777 stack->item->object.flags |= RESULT;
778 continue;
781 for (parent = stack->item->parents; parent; parent = parent->next) {
782 if (parent->item->object.flags & (with_flag | RESULT))
783 stack->item->object.flags |= RESULT;
785 if (!(parent->item->object.flags & assign_flag)) {
786 parent->item->object.flags |= assign_flag;
788 if (parse_commit(parent->item) ||
789 parent->item->date < min_commit_date ||
790 commit_graph_generation(parent->item) < min_generation)
791 continue;
793 commit_list_insert(parent->item, &stack);
794 break;
798 if (!parent)
799 pop_commit(&stack);
802 if (!(list[i]->object.flags & (with_flag | RESULT))) {
803 result = 0;
804 goto cleanup;
808 cleanup:
809 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
810 free(list);
812 for (i = 0; i < from->nr; i++) {
813 struct object *from_one = from->objects[i].item;
815 if (from_one)
816 from_one->flags &= ~assign_flag;
819 return result;
822 int can_all_from_reach(struct commit_list *from, struct commit_list *to,
823 int cutoff_by_min_date)
825 struct object_array from_objs = OBJECT_ARRAY_INIT;
826 time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
827 struct commit_list *from_iter = from, *to_iter = to;
828 int result;
829 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
831 while (from_iter) {
832 add_object_array(&from_iter->item->object, NULL, &from_objs);
834 if (!parse_commit(from_iter->item)) {
835 timestamp_t generation;
836 if (from_iter->item->date < min_commit_date)
837 min_commit_date = from_iter->item->date;
839 generation = commit_graph_generation(from_iter->item);
840 if (generation < min_generation)
841 min_generation = generation;
844 from_iter = from_iter->next;
847 while (to_iter) {
848 if (!parse_commit(to_iter->item)) {
849 timestamp_t generation;
850 if (to_iter->item->date < min_commit_date)
851 min_commit_date = to_iter->item->date;
853 generation = commit_graph_generation(to_iter->item);
854 if (generation < min_generation)
855 min_generation = generation;
858 to_iter->item->object.flags |= PARENT2;
860 to_iter = to_iter->next;
863 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
864 min_commit_date, min_generation);
866 while (from) {
867 clear_commit_marks(from->item, PARENT1);
868 from = from->next;
871 while (to) {
872 clear_commit_marks(to->item, PARENT2);
873 to = to->next;
876 object_array_clear(&from_objs);
877 return result;
880 struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
881 struct commit **to, int nr_to,
882 unsigned int reachable_flag)
884 struct commit **item;
885 struct commit *current;
886 struct commit_list *found_commits = NULL;
887 struct commit **to_last = to + nr_to;
888 struct commit **from_last = from + nr_from;
889 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
890 int num_to_find = 0;
892 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
894 for (item = to; item < to_last; item++) {
895 timestamp_t generation;
896 struct commit *c = *item;
898 parse_commit(c);
899 generation = commit_graph_generation(c);
900 if (generation < min_generation)
901 min_generation = generation;
903 if (!(c->object.flags & PARENT1)) {
904 c->object.flags |= PARENT1;
905 num_to_find++;
909 for (item = from; item < from_last; item++) {
910 struct commit *c = *item;
911 if (!(c->object.flags & PARENT2)) {
912 c->object.flags |= PARENT2;
913 parse_commit(c);
915 prio_queue_put(&queue, *item);
919 while (num_to_find && (current = prio_queue_get(&queue)) != NULL) {
920 struct commit_list *parents;
922 if (current->object.flags & PARENT1) {
923 current->object.flags &= ~PARENT1;
924 current->object.flags |= reachable_flag;
925 commit_list_insert(current, &found_commits);
926 num_to_find--;
929 for (parents = current->parents; parents; parents = parents->next) {
930 struct commit *p = parents->item;
932 parse_commit(p);
934 if (commit_graph_generation(p) < min_generation)
935 continue;
937 if (p->object.flags & PARENT2)
938 continue;
940 p->object.flags |= PARENT2;
941 prio_queue_put(&queue, p);
945 clear_commit_marks_many(nr_to, to, PARENT1);
946 clear_commit_marks_many(nr_from, from, PARENT2);
948 return found_commits;