t1300: drop duplicate test
[alt-git.git] / commit-reach.c
blob2e33c599a82c81127b9a24547d0834e6c7a55bcf
1 #include "cache.h"
2 #include "commit.h"
3 #include "commit-graph.h"
4 #include "decorate.h"
5 #include "prio-queue.h"
6 #include "tree.h"
7 #include "ref-filter.h"
8 #include "revision.h"
9 #include "tag.h"
10 #include "commit-reach.h"
12 /* Remember to update object flag allocation in object.h */
13 #define PARENT1 (1u<<16)
14 #define PARENT2 (1u<<17)
15 #define STALE (1u<<18)
16 #define RESULT (1u<<19)
18 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
20 static int compare_commits_by_gen(const void *_a, const void *_b)
22 const struct commit *a = *(const struct commit * const *)_a;
23 const struct commit *b = *(const struct commit * const *)_b;
25 timestamp_t generation_a = commit_graph_generation(a);
26 timestamp_t generation_b = commit_graph_generation(b);
28 if (generation_a < generation_b)
29 return -1;
30 if (generation_a > generation_b)
31 return 1;
32 if (a->date < b->date)
33 return -1;
34 if (a->date > b->date)
35 return 1;
36 return 0;
39 static int queue_has_nonstale(struct prio_queue *queue)
41 int i;
42 for (i = 0; i < queue->nr; i++) {
43 struct commit *commit = queue->array[i].data;
44 if (!(commit->object.flags & STALE))
45 return 1;
47 return 0;
50 /* all input commits in one and twos[] must have been parsed! */
51 static struct commit_list *paint_down_to_common(struct repository *r,
52 struct commit *one, int n,
53 struct commit **twos,
54 timestamp_t min_generation)
56 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
57 struct commit_list *result = NULL;
58 int i;
59 timestamp_t last_gen = GENERATION_NUMBER_INFINITY;
61 if (!min_generation && !corrected_commit_dates_enabled(r))
62 queue.compare = compare_commits_by_commit_date;
64 one->object.flags |= PARENT1;
65 if (!n) {
66 commit_list_append(one, &result);
67 return result;
69 prio_queue_put(&queue, one);
71 for (i = 0; i < n; i++) {
72 twos[i]->object.flags |= PARENT2;
73 prio_queue_put(&queue, twos[i]);
76 while (queue_has_nonstale(&queue)) {
77 struct commit *commit = prio_queue_get(&queue);
78 struct commit_list *parents;
79 int flags;
80 timestamp_t generation = commit_graph_generation(commit);
82 if (min_generation && generation > last_gen)
83 BUG("bad generation skip %"PRItime" > %"PRItime" at %s",
84 generation, last_gen,
85 oid_to_hex(&commit->object.oid));
86 last_gen = generation;
88 if (generation < min_generation)
89 break;
91 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
92 if (flags == (PARENT1 | PARENT2)) {
93 if (!(commit->object.flags & RESULT)) {
94 commit->object.flags |= RESULT;
95 commit_list_insert_by_date(commit, &result);
97 /* Mark parents of a found merge stale */
98 flags |= STALE;
100 parents = commit->parents;
101 while (parents) {
102 struct commit *p = parents->item;
103 parents = parents->next;
104 if ((p->object.flags & flags) == flags)
105 continue;
106 if (repo_parse_commit(r, p))
107 return NULL;
108 p->object.flags |= flags;
109 prio_queue_put(&queue, p);
113 clear_prio_queue(&queue);
114 return result;
117 static struct commit_list *merge_bases_many(struct repository *r,
118 struct commit *one, int n,
119 struct commit **twos)
121 struct commit_list *list = NULL;
122 struct commit_list *result = NULL;
123 int i;
125 for (i = 0; i < n; i++) {
126 if (one == twos[i])
128 * We do not mark this even with RESULT so we do not
129 * have to clean it up.
131 return commit_list_insert(one, &result);
134 if (repo_parse_commit(r, one))
135 return NULL;
136 for (i = 0; i < n; i++) {
137 if (repo_parse_commit(r, twos[i]))
138 return NULL;
141 list = paint_down_to_common(r, one, n, twos, 0);
143 while (list) {
144 struct commit *commit = pop_commit(&list);
145 if (!(commit->object.flags & STALE))
146 commit_list_insert_by_date(commit, &result);
148 return result;
151 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
153 struct commit_list *i, *j, *k, *ret = NULL;
155 if (!in)
156 return ret;
158 commit_list_insert(in->item, &ret);
160 for (i = in->next; i; i = i->next) {
161 struct commit_list *new_commits = NULL, *end = NULL;
163 for (j = ret; j; j = j->next) {
164 struct commit_list *bases;
165 bases = get_merge_bases(i->item, j->item);
166 if (!new_commits)
167 new_commits = bases;
168 else
169 end->next = bases;
170 for (k = bases; k; k = k->next)
171 end = k;
173 ret = new_commits;
175 return ret;
178 static int remove_redundant_no_gen(struct repository *r,
179 struct commit **array, int cnt)
181 struct commit **work;
182 unsigned char *redundant;
183 int *filled_index;
184 int i, j, filled;
186 CALLOC_ARRAY(work, cnt);
187 redundant = xcalloc(cnt, 1);
188 ALLOC_ARRAY(filled_index, cnt - 1);
190 for (i = 0; i < cnt; i++)
191 repo_parse_commit(r, array[i]);
192 for (i = 0; i < cnt; i++) {
193 struct commit_list *common;
194 timestamp_t min_generation = commit_graph_generation(array[i]);
196 if (redundant[i])
197 continue;
198 for (j = filled = 0; j < cnt; j++) {
199 timestamp_t curr_generation;
200 if (i == j || redundant[j])
201 continue;
202 filled_index[filled] = j;
203 work[filled++] = array[j];
205 curr_generation = commit_graph_generation(array[j]);
206 if (curr_generation < min_generation)
207 min_generation = curr_generation;
209 common = paint_down_to_common(r, array[i], filled,
210 work, min_generation);
211 if (array[i]->object.flags & PARENT2)
212 redundant[i] = 1;
213 for (j = 0; j < filled; j++)
214 if (work[j]->object.flags & PARENT1)
215 redundant[filled_index[j]] = 1;
216 clear_commit_marks(array[i], all_flags);
217 clear_commit_marks_many(filled, work, all_flags);
218 free_commit_list(common);
221 /* Now collect the result */
222 COPY_ARRAY(work, array, cnt);
223 for (i = filled = 0; i < cnt; i++)
224 if (!redundant[i])
225 array[filled++] = work[i];
226 free(work);
227 free(redundant);
228 free(filled_index);
229 return filled;
232 static int remove_redundant_with_gen(struct repository *r,
233 struct commit **array, int cnt)
235 int i, count_non_stale = 0, count_still_independent = cnt;
236 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
237 struct commit **walk_start, **sorted;
238 size_t walk_start_nr = 0, walk_start_alloc = cnt;
239 int min_gen_pos = 0;
242 * Sort the input by generation number, ascending. This allows
243 * us to increase the "min_generation" limit when we discover
244 * the commit with lowest generation is STALE. The index
245 * min_gen_pos points to the current position within 'array'
246 * that is not yet known to be STALE.
248 DUP_ARRAY(sorted, array, cnt);
249 QSORT(sorted, cnt, compare_commits_by_gen);
250 min_generation = commit_graph_generation(sorted[0]);
252 ALLOC_ARRAY(walk_start, walk_start_alloc);
254 /* Mark all parents of the input as STALE */
255 for (i = 0; i < cnt; i++) {
256 struct commit_list *parents;
258 repo_parse_commit(r, array[i]);
259 array[i]->object.flags |= RESULT;
260 parents = array[i]->parents;
262 while (parents) {
263 repo_parse_commit(r, parents->item);
264 if (!(parents->item->object.flags & STALE)) {
265 parents->item->object.flags |= STALE;
266 ALLOC_GROW(walk_start, walk_start_nr + 1, walk_start_alloc);
267 walk_start[walk_start_nr++] = parents->item;
269 parents = parents->next;
273 QSORT(walk_start, walk_start_nr, compare_commits_by_gen);
275 /* remove STALE bit for now to allow walking through parents */
276 for (i = 0; i < walk_start_nr; i++)
277 walk_start[i]->object.flags &= ~STALE;
280 * Start walking from the highest generation. Hopefully, it will
281 * find all other items during the first-parent walk, and we can
282 * terminate early. Otherwise, we will do the same amount of work
283 * as before.
285 for (i = walk_start_nr - 1; i >= 0 && count_still_independent > 1; i--) {
286 /* push the STALE bits up to min generation */
287 struct commit_list *stack = NULL;
289 commit_list_insert(walk_start[i], &stack);
290 walk_start[i]->object.flags |= STALE;
292 while (stack) {
293 struct commit_list *parents;
294 struct commit *c = stack->item;
296 repo_parse_commit(r, c);
298 if (c->object.flags & RESULT) {
299 c->object.flags &= ~RESULT;
300 if (--count_still_independent <= 1)
301 break;
302 if (oideq(&c->object.oid, &sorted[min_gen_pos]->object.oid)) {
303 while (min_gen_pos < cnt - 1 &&
304 (sorted[min_gen_pos]->object.flags & STALE))
305 min_gen_pos++;
306 min_generation = commit_graph_generation(sorted[min_gen_pos]);
310 if (commit_graph_generation(c) < min_generation) {
311 pop_commit(&stack);
312 continue;
315 parents = c->parents;
316 while (parents) {
317 if (!(parents->item->object.flags & STALE)) {
318 parents->item->object.flags |= STALE;
319 commit_list_insert(parents->item, &stack);
320 break;
322 parents = parents->next;
325 /* pop if all parents have been visited already */
326 if (!parents)
327 pop_commit(&stack);
329 free_commit_list(stack);
331 free(sorted);
333 /* clear result */
334 for (i = 0; i < cnt; i++)
335 array[i]->object.flags &= ~RESULT;
337 /* rearrange array */
338 for (i = count_non_stale = 0; i < cnt; i++) {
339 if (!(array[i]->object.flags & STALE))
340 array[count_non_stale++] = array[i];
343 /* clear marks */
344 clear_commit_marks_many(walk_start_nr, walk_start, STALE);
345 free(walk_start);
347 return count_non_stale;
350 static int remove_redundant(struct repository *r, struct commit **array, int cnt)
353 * Some commit in the array may be an ancestor of
354 * another commit. Move the independent commits to the
355 * beginning of 'array' and return their number. Callers
356 * should not rely upon the contents of 'array' after
357 * that number.
359 if (generation_numbers_enabled(r)) {
360 int i;
363 * If we have a single commit with finite generation
364 * number, then the _with_gen algorithm is preferred.
366 for (i = 0; i < cnt; i++) {
367 if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY)
368 return remove_redundant_with_gen(r, array, cnt);
372 return remove_redundant_no_gen(r, array, cnt);
375 static struct commit_list *get_merge_bases_many_0(struct repository *r,
376 struct commit *one,
377 int n,
378 struct commit **twos,
379 int cleanup)
381 struct commit_list *list;
382 struct commit **rslt;
383 struct commit_list *result;
384 int cnt, i;
386 result = merge_bases_many(r, one, n, twos);
387 for (i = 0; i < n; i++) {
388 if (one == twos[i])
389 return result;
391 if (!result || !result->next) {
392 if (cleanup) {
393 clear_commit_marks(one, all_flags);
394 clear_commit_marks_many(n, twos, all_flags);
396 return result;
399 /* There are more than one */
400 cnt = commit_list_count(result);
401 CALLOC_ARRAY(rslt, cnt);
402 for (list = result, i = 0; list; list = list->next)
403 rslt[i++] = list->item;
404 free_commit_list(result);
406 clear_commit_marks(one, all_flags);
407 clear_commit_marks_many(n, twos, all_flags);
409 cnt = remove_redundant(r, rslt, cnt);
410 result = NULL;
411 for (i = 0; i < cnt; i++)
412 commit_list_insert_by_date(rslt[i], &result);
413 free(rslt);
414 return result;
417 struct commit_list *repo_get_merge_bases_many(struct repository *r,
418 struct commit *one,
419 int n,
420 struct commit **twos)
422 return get_merge_bases_many_0(r, one, n, twos, 1);
425 struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
426 struct commit *one,
427 int n,
428 struct commit **twos)
430 return get_merge_bases_many_0(r, one, n, twos, 0);
433 struct commit_list *repo_get_merge_bases(struct repository *r,
434 struct commit *one,
435 struct commit *two)
437 return get_merge_bases_many_0(r, one, 1, &two, 1);
441 * Is "commit" a descendant of one of the elements on the "with_commit" list?
443 int repo_is_descendant_of(struct repository *r,
444 struct commit *commit,
445 struct commit_list *with_commit)
447 if (!with_commit)
448 return 1;
450 if (generation_numbers_enabled(the_repository)) {
451 struct commit_list *from_list = NULL;
452 int result;
453 commit_list_insert(commit, &from_list);
454 result = can_all_from_reach(from_list, with_commit, 0);
455 free_commit_list(from_list);
456 return result;
457 } else {
458 while (with_commit) {
459 struct commit *other;
461 other = with_commit->item;
462 with_commit = with_commit->next;
463 if (repo_in_merge_bases_many(r, other, 1, &commit))
464 return 1;
466 return 0;
471 * Is "commit" an ancestor of one of the "references"?
473 int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
474 int nr_reference, struct commit **reference)
476 struct commit_list *bases;
477 int ret = 0, i;
478 timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
480 if (repo_parse_commit(r, commit))
481 return ret;
482 for (i = 0; i < nr_reference; i++) {
483 if (repo_parse_commit(r, reference[i]))
484 return ret;
486 generation = commit_graph_generation(reference[i]);
487 if (generation > max_generation)
488 max_generation = generation;
491 generation = commit_graph_generation(commit);
492 if (generation > max_generation)
493 return ret;
495 bases = paint_down_to_common(r, commit,
496 nr_reference, reference,
497 generation);
498 if (commit->object.flags & PARENT2)
499 ret = 1;
500 clear_commit_marks(commit, all_flags);
501 clear_commit_marks_many(nr_reference, reference, all_flags);
502 free_commit_list(bases);
503 return ret;
507 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
509 int repo_in_merge_bases(struct repository *r,
510 struct commit *commit,
511 struct commit *reference)
513 int res;
514 struct commit_list *list = NULL;
515 struct commit_list **next = &list;
517 next = commit_list_append(commit, next);
518 res = repo_is_descendant_of(r, reference, list);
519 free_commit_list(list);
521 return res;
524 struct commit_list *reduce_heads(struct commit_list *heads)
526 struct commit_list *p;
527 struct commit_list *result = NULL, **tail = &result;
528 struct commit **array;
529 int num_head, i;
531 if (!heads)
532 return NULL;
534 /* Uniquify */
535 for (p = heads; p; p = p->next)
536 p->item->object.flags &= ~STALE;
537 for (p = heads, num_head = 0; p; p = p->next) {
538 if (p->item->object.flags & STALE)
539 continue;
540 p->item->object.flags |= STALE;
541 num_head++;
543 CALLOC_ARRAY(array, num_head);
544 for (p = heads, i = 0; p; p = p->next) {
545 if (p->item->object.flags & STALE) {
546 array[i++] = p->item;
547 p->item->object.flags &= ~STALE;
550 num_head = remove_redundant(the_repository, array, num_head);
551 for (i = 0; i < num_head; i++)
552 tail = &commit_list_insert(array[i], tail)->next;
553 free(array);
554 return result;
557 void reduce_heads_replace(struct commit_list **heads)
559 struct commit_list *result = reduce_heads(*heads);
560 free_commit_list(*heads);
561 *heads = result;
564 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
566 struct object *o;
567 struct commit *old_commit, *new_commit;
568 struct commit_list *old_commit_list = NULL;
569 int ret;
572 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
573 * old_commit. Otherwise we require --force.
575 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
576 NULL, 0);
577 if (!o || o->type != OBJ_COMMIT)
578 return 0;
579 old_commit = (struct commit *) o;
581 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
582 NULL, 0);
583 if (!o || o->type != OBJ_COMMIT)
584 return 0;
585 new_commit = (struct commit *) o;
587 if (parse_commit(new_commit) < 0)
588 return 0;
590 commit_list_insert(old_commit, &old_commit_list);
591 ret = repo_is_descendant_of(the_repository,
592 new_commit, old_commit_list);
593 free_commit_list(old_commit_list);
594 return ret;
598 * Mimicking the real stack, this stack lives on the heap, avoiding stack
599 * overflows.
601 * At each recursion step, the stack items points to the commits whose
602 * ancestors are to be inspected.
604 struct contains_stack {
605 int nr, alloc;
606 struct contains_stack_entry {
607 struct commit *commit;
608 struct commit_list *parents;
609 } *contains_stack;
612 static int in_commit_list(const struct commit_list *want, struct commit *c)
614 for (; want; want = want->next)
615 if (oideq(&want->item->object.oid, &c->object.oid))
616 return 1;
617 return 0;
621 * Test whether the candidate is contained in the list.
622 * Do not recurse to find out, though, but return -1 if inconclusive.
624 static enum contains_result contains_test(struct commit *candidate,
625 const struct commit_list *want,
626 struct contains_cache *cache,
627 timestamp_t cutoff)
629 enum contains_result *cached = contains_cache_at(cache, candidate);
631 /* If we already have the answer cached, return that. */
632 if (*cached)
633 return *cached;
635 /* or are we it? */
636 if (in_commit_list(want, candidate)) {
637 *cached = CONTAINS_YES;
638 return CONTAINS_YES;
641 /* Otherwise, we don't know; prepare to recurse */
642 parse_commit_or_die(candidate);
644 if (commit_graph_generation(candidate) < cutoff)
645 return CONTAINS_NO;
647 return CONTAINS_UNKNOWN;
650 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
652 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
653 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
654 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
657 static enum contains_result contains_tag_algo(struct commit *candidate,
658 const struct commit_list *want,
659 struct contains_cache *cache)
661 struct contains_stack contains_stack = { 0, 0, NULL };
662 enum contains_result result;
663 timestamp_t cutoff = GENERATION_NUMBER_INFINITY;
664 const struct commit_list *p;
666 for (p = want; p; p = p->next) {
667 timestamp_t generation;
668 struct commit *c = p->item;
669 load_commit_graph_info(the_repository, c);
670 generation = commit_graph_generation(c);
671 if (generation < cutoff)
672 cutoff = generation;
675 result = contains_test(candidate, want, cache, cutoff);
676 if (result != CONTAINS_UNKNOWN)
677 return result;
679 push_to_contains_stack(candidate, &contains_stack);
680 while (contains_stack.nr) {
681 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
682 struct commit *commit = entry->commit;
683 struct commit_list *parents = entry->parents;
685 if (!parents) {
686 *contains_cache_at(cache, commit) = CONTAINS_NO;
687 contains_stack.nr--;
690 * If we just popped the stack, parents->item has been marked,
691 * therefore contains_test will return a meaningful yes/no.
693 else switch (contains_test(parents->item, want, cache, cutoff)) {
694 case CONTAINS_YES:
695 *contains_cache_at(cache, commit) = CONTAINS_YES;
696 contains_stack.nr--;
697 break;
698 case CONTAINS_NO:
699 entry->parents = parents->next;
700 break;
701 case CONTAINS_UNKNOWN:
702 push_to_contains_stack(parents->item, &contains_stack);
703 break;
706 free(contains_stack.contains_stack);
707 return contains_test(candidate, want, cache, cutoff);
710 int commit_contains(struct ref_filter *filter, struct commit *commit,
711 struct commit_list *list, struct contains_cache *cache)
713 if (filter->with_commit_tag_algo)
714 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
715 return repo_is_descendant_of(the_repository, commit, list);
718 int can_all_from_reach_with_flag(struct object_array *from,
719 unsigned int with_flag,
720 unsigned int assign_flag,
721 time_t min_commit_date,
722 timestamp_t min_generation)
724 struct commit **list = NULL;
725 int i;
726 int nr_commits;
727 int result = 1;
729 ALLOC_ARRAY(list, from->nr);
730 nr_commits = 0;
731 for (i = 0; i < from->nr; i++) {
732 struct object *from_one = from->objects[i].item;
734 if (!from_one || from_one->flags & assign_flag)
735 continue;
737 from_one = deref_tag(the_repository, from_one,
738 "a from object", 0);
739 if (!from_one || from_one->type != OBJ_COMMIT) {
741 * no way to tell if this is reachable by
742 * looking at the ancestry chain alone, so
743 * leave a note to ourselves not to worry about
744 * this object anymore.
746 from->objects[i].item->flags |= assign_flag;
747 continue;
750 list[nr_commits] = (struct commit *)from_one;
751 if (parse_commit(list[nr_commits]) ||
752 commit_graph_generation(list[nr_commits]) < min_generation) {
753 result = 0;
754 goto cleanup;
757 nr_commits++;
760 QSORT(list, nr_commits, compare_commits_by_gen);
762 for (i = 0; i < nr_commits; i++) {
763 /* DFS from list[i] */
764 struct commit_list *stack = NULL;
766 list[i]->object.flags |= assign_flag;
767 commit_list_insert(list[i], &stack);
769 while (stack) {
770 struct commit_list *parent;
772 if (stack->item->object.flags & (with_flag | RESULT)) {
773 pop_commit(&stack);
774 if (stack)
775 stack->item->object.flags |= RESULT;
776 continue;
779 for (parent = stack->item->parents; parent; parent = parent->next) {
780 if (parent->item->object.flags & (with_flag | RESULT))
781 stack->item->object.flags |= RESULT;
783 if (!(parent->item->object.flags & assign_flag)) {
784 parent->item->object.flags |= assign_flag;
786 if (parse_commit(parent->item) ||
787 parent->item->date < min_commit_date ||
788 commit_graph_generation(parent->item) < min_generation)
789 continue;
791 commit_list_insert(parent->item, &stack);
792 break;
796 if (!parent)
797 pop_commit(&stack);
800 if (!(list[i]->object.flags & (with_flag | RESULT))) {
801 result = 0;
802 goto cleanup;
806 cleanup:
807 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
808 free(list);
810 for (i = 0; i < from->nr; i++)
811 from->objects[i].item->flags &= ~assign_flag;
813 return result;
816 int can_all_from_reach(struct commit_list *from, struct commit_list *to,
817 int cutoff_by_min_date)
819 struct object_array from_objs = OBJECT_ARRAY_INIT;
820 time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
821 struct commit_list *from_iter = from, *to_iter = to;
822 int result;
823 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
825 while (from_iter) {
826 add_object_array(&from_iter->item->object, NULL, &from_objs);
828 if (!parse_commit(from_iter->item)) {
829 timestamp_t generation;
830 if (from_iter->item->date < min_commit_date)
831 min_commit_date = from_iter->item->date;
833 generation = commit_graph_generation(from_iter->item);
834 if (generation < min_generation)
835 min_generation = generation;
838 from_iter = from_iter->next;
841 while (to_iter) {
842 if (!parse_commit(to_iter->item)) {
843 timestamp_t generation;
844 if (to_iter->item->date < min_commit_date)
845 min_commit_date = to_iter->item->date;
847 generation = commit_graph_generation(to_iter->item);
848 if (generation < min_generation)
849 min_generation = generation;
852 to_iter->item->object.flags |= PARENT2;
854 to_iter = to_iter->next;
857 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
858 min_commit_date, min_generation);
860 while (from) {
861 clear_commit_marks(from->item, PARENT1);
862 from = from->next;
865 while (to) {
866 clear_commit_marks(to->item, PARENT2);
867 to = to->next;
870 object_array_clear(&from_objs);
871 return result;
874 struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
875 struct commit **to, int nr_to,
876 unsigned int reachable_flag)
878 struct commit **item;
879 struct commit *current;
880 struct commit_list *found_commits = NULL;
881 struct commit **to_last = to + nr_to;
882 struct commit **from_last = from + nr_from;
883 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
884 int num_to_find = 0;
886 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
888 for (item = to; item < to_last; item++) {
889 timestamp_t generation;
890 struct commit *c = *item;
892 parse_commit(c);
893 generation = commit_graph_generation(c);
894 if (generation < min_generation)
895 min_generation = generation;
897 if (!(c->object.flags & PARENT1)) {
898 c->object.flags |= PARENT1;
899 num_to_find++;
903 for (item = from; item < from_last; item++) {
904 struct commit *c = *item;
905 if (!(c->object.flags & PARENT2)) {
906 c->object.flags |= PARENT2;
907 parse_commit(c);
909 prio_queue_put(&queue, *item);
913 while (num_to_find && (current = prio_queue_get(&queue)) != NULL) {
914 struct commit_list *parents;
916 if (current->object.flags & PARENT1) {
917 current->object.flags &= ~PARENT1;
918 current->object.flags |= reachable_flag;
919 commit_list_insert(current, &found_commits);
920 num_to_find--;
923 for (parents = current->parents; parents; parents = parents->next) {
924 struct commit *p = parents->item;
926 parse_commit(p);
928 if (commit_graph_generation(p) < min_generation)
929 continue;
931 if (p->object.flags & PARENT2)
932 continue;
934 p->object.flags |= PARENT2;
935 prio_queue_put(&queue, p);
939 clear_commit_marks_many(nr_to, to, PARENT1);
940 clear_commit_marks_many(nr_from, from, PARENT2);
942 return found_commits;