]> git.ipfire.org Git - thirdparty/git.git/blame - commit-reach.c
Merge branch 'cb/is-descendant-of'
[thirdparty/git.git] / commit-reach.c
CommitLineData
5227c385 1#include "cache.h"
5227c385 2#include "commit.h"
920f93ca 3#include "commit-graph.h"
1d614d41
DS
4#include "decorate.h"
5#include "prio-queue.h"
6#include "tree.h"
6621c838 7#include "ref-filter.h"
1d614d41
DS
8#include "revision.h"
9#include "tag.h"
5227c385
DS
10#include "commit-reach.h"
11
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)
17
18static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
19
20static int queue_has_nonstale(struct prio_queue *queue)
21{
22 int i;
23 for (i = 0; i < queue->nr; i++) {
24 struct commit *commit = queue->array[i].data;
25 if (!(commit->object.flags & STALE))
26 return 1;
27 }
28 return 0;
29}
30
31/* all input commits in one and twos[] must have been parsed! */
c383830a
SB
32static struct commit_list *paint_down_to_common(struct repository *r,
33 struct commit *one, int n,
5227c385
DS
34 struct commit **twos,
35 int min_generation)
36{
37 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
38 struct commit_list *result = NULL;
39 int i;
40 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
41
1b7a91da
JH
42 if (!min_generation)
43 queue.compare = compare_commits_by_commit_date;
44
5227c385
DS
45 one->object.flags |= PARENT1;
46 if (!n) {
47 commit_list_append(one, &result);
48 return result;
49 }
50 prio_queue_put(&queue, one);
51
52 for (i = 0; i < n; i++) {
53 twos[i]->object.flags |= PARENT2;
54 prio_queue_put(&queue, twos[i]);
55 }
56
57 while (queue_has_nonstale(&queue)) {
58 struct commit *commit = prio_queue_get(&queue);
59 struct commit_list *parents;
60 int flags;
c752ad09 61 uint32_t generation = commit_graph_generation(commit);
5227c385 62
c752ad09 63 if (min_generation && generation > last_gen)
5227c385 64 BUG("bad generation skip %8x > %8x at %s",
c752ad09 65 generation, last_gen,
5227c385 66 oid_to_hex(&commit->object.oid));
c752ad09 67 last_gen = generation;
5227c385 68
c752ad09 69 if (generation < min_generation)
5227c385
DS
70 break;
71
72 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
73 if (flags == (PARENT1 | PARENT2)) {
74 if (!(commit->object.flags & RESULT)) {
75 commit->object.flags |= RESULT;
76 commit_list_insert_by_date(commit, &result);
77 }
78 /* Mark parents of a found merge stale */
79 flags |= STALE;
80 }
81 parents = commit->parents;
82 while (parents) {
83 struct commit *p = parents->item;
84 parents = parents->next;
85 if ((p->object.flags & flags) == flags)
86 continue;
c383830a 87 if (repo_parse_commit(r, p))
5227c385
DS
88 return NULL;
89 p->object.flags |= flags;
90 prio_queue_put(&queue, p);
91 }
92 }
93
94 clear_prio_queue(&queue);
95 return result;
96}
97
18256a91
SB
98static struct commit_list *merge_bases_many(struct repository *r,
99 struct commit *one, int n,
100 struct commit **twos)
5227c385
DS
101{
102 struct commit_list *list = NULL;
103 struct commit_list *result = NULL;
104 int i;
105
106 for (i = 0; i < n; i++) {
107 if (one == twos[i])
108 /*
109 * We do not mark this even with RESULT so we do not
110 * have to clean it up.
111 */
112 return commit_list_insert(one, &result);
113 }
114
18256a91 115 if (repo_parse_commit(r, one))
5227c385
DS
116 return NULL;
117 for (i = 0; i < n; i++) {
18256a91 118 if (repo_parse_commit(r, twos[i]))
5227c385
DS
119 return NULL;
120 }
121
18256a91 122 list = paint_down_to_common(r, one, n, twos, 0);
5227c385
DS
123
124 while (list) {
125 struct commit *commit = pop_commit(&list);
126 if (!(commit->object.flags & STALE))
127 commit_list_insert_by_date(commit, &result);
128 }
129 return result;
130}
131
132struct commit_list *get_octopus_merge_bases(struct commit_list *in)
133{
134 struct commit_list *i, *j, *k, *ret = NULL;
135
136 if (!in)
137 return ret;
138
139 commit_list_insert(in->item, &ret);
140
141 for (i = in->next; i; i = i->next) {
142 struct commit_list *new_commits = NULL, *end = NULL;
143
144 for (j = ret; j; j = j->next) {
145 struct commit_list *bases;
146 bases = get_merge_bases(i->item, j->item);
147 if (!new_commits)
148 new_commits = bases;
149 else
150 end->next = bases;
151 for (k = bases; k; k = k->next)
152 end = k;
153 }
154 ret = new_commits;
155 }
156 return ret;
157}
158
ed8a0e3a 159static int remove_redundant(struct repository *r, struct commit **array, int cnt)
5227c385
DS
160{
161 /*
162 * Some commit in the array may be an ancestor of
163 * another commit. Move such commit to the end of
164 * the array, and return the number of commits that
165 * are independent from each other.
166 */
167 struct commit **work;
168 unsigned char *redundant;
169 int *filled_index;
170 int i, j, filled;
171
172 work = xcalloc(cnt, sizeof(*work));
173 redundant = xcalloc(cnt, 1);
174 ALLOC_ARRAY(filled_index, cnt - 1);
175
176 for (i = 0; i < cnt; i++)
ed8a0e3a 177 repo_parse_commit(r, array[i]);
5227c385
DS
178 for (i = 0; i < cnt; i++) {
179 struct commit_list *common;
c49c82aa 180 uint32_t min_generation = commit_graph_generation(array[i]);
5227c385
DS
181
182 if (redundant[i])
183 continue;
184 for (j = filled = 0; j < cnt; j++) {
c752ad09 185 uint32_t curr_generation;
5227c385
DS
186 if (i == j || redundant[j])
187 continue;
188 filled_index[filled] = j;
189 work[filled++] = array[j];
190
c752ad09
AK
191 curr_generation = commit_graph_generation(array[j]);
192 if (curr_generation < min_generation)
193 min_generation = curr_generation;
5227c385 194 }
ed8a0e3a 195 common = paint_down_to_common(r, array[i], filled,
c383830a 196 work, min_generation);
5227c385
DS
197 if (array[i]->object.flags & PARENT2)
198 redundant[i] = 1;
199 for (j = 0; j < filled; j++)
200 if (work[j]->object.flags & PARENT1)
201 redundant[filled_index[j]] = 1;
202 clear_commit_marks(array[i], all_flags);
203 clear_commit_marks_many(filled, work, all_flags);
204 free_commit_list(common);
205 }
206
207 /* Now collect the result */
208 COPY_ARRAY(work, array, cnt);
209 for (i = filled = 0; i < cnt; i++)
210 if (!redundant[i])
211 array[filled++] = work[i];
212 for (j = filled, i = 0; i < cnt; i++)
213 if (redundant[i])
214 array[j++] = work[i];
215 free(work);
216 free(redundant);
217 free(filled_index);
218 return filled;
219}
220
f28e87f5
SB
221static struct commit_list *get_merge_bases_many_0(struct repository *r,
222 struct commit *one,
5227c385
DS
223 int n,
224 struct commit **twos,
225 int cleanup)
226{
227 struct commit_list *list;
228 struct commit **rslt;
229 struct commit_list *result;
230 int cnt, i;
231
f28e87f5 232 result = merge_bases_many(r, one, n, twos);
5227c385
DS
233 for (i = 0; i < n; i++) {
234 if (one == twos[i])
235 return result;
236 }
237 if (!result || !result->next) {
238 if (cleanup) {
239 clear_commit_marks(one, all_flags);
240 clear_commit_marks_many(n, twos, all_flags);
241 }
242 return result;
243 }
244
245 /* There are more than one */
246 cnt = commit_list_count(result);
247 rslt = xcalloc(cnt, sizeof(*rslt));
248 for (list = result, i = 0; list; list = list->next)
249 rslt[i++] = list->item;
250 free_commit_list(result);
251
252 clear_commit_marks(one, all_flags);
253 clear_commit_marks_many(n, twos, all_flags);
254
f28e87f5 255 cnt = remove_redundant(r, rslt, cnt);
5227c385
DS
256 result = NULL;
257 for (i = 0; i < cnt; i++)
258 commit_list_insert_by_date(rslt[i], &result);
259 free(rslt);
260 return result;
261}
262
21a9651b
SB
263struct commit_list *repo_get_merge_bases_many(struct repository *r,
264 struct commit *one,
265 int n,
266 struct commit **twos)
5227c385 267{
21a9651b 268 return get_merge_bases_many_0(r, one, n, twos, 1);
5227c385
DS
269}
270
21a9651b
SB
271struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
272 struct commit *one,
273 int n,
274 struct commit **twos)
5227c385 275{
21a9651b 276 return get_merge_bases_many_0(r, one, n, twos, 0);
5227c385
DS
277}
278
21a9651b
SB
279struct commit_list *repo_get_merge_bases(struct repository *r,
280 struct commit *one,
281 struct commit *two)
5227c385 282{
21a9651b 283 return get_merge_bases_many_0(r, one, 1, &two, 1);
5227c385
DS
284}
285
286/*
287 * Is "commit" a descendant of one of the elements on the "with_commit" list?
288 */
c1ea625f
CMAB
289int repo_is_descendant_of(struct repository *r,
290 struct commit *commit,
291 struct commit_list *with_commit)
5227c385
DS
292{
293 if (!with_commit)
294 return 1;
5227c385 295
6cc01743
DS
296 if (generation_numbers_enabled(the_repository)) {
297 struct commit_list *from_list = NULL;
298 int result;
299 commit_list_insert(commit, &from_list);
300 result = can_all_from_reach(from_list, with_commit, 0);
301 free_commit_list(from_list);
302 return result;
303 } else {
304 while (with_commit) {
305 struct commit *other;
306
307 other = with_commit->item;
308 with_commit = with_commit->next;
80b8ada5 309 if (repo_in_merge_bases_many(r, other, 1, &commit))
6cc01743
DS
310 return 1;
311 }
312 return 0;
5227c385 313 }
5227c385
DS
314}
315
316/*
317 * Is "commit" an ancestor of one of the "references"?
318 */
4d5430f7
SB
319int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
320 int nr_reference, struct commit **reference)
5227c385
DS
321{
322 struct commit_list *bases;
323 int ret = 0, i;
c752ad09 324 uint32_t generation, min_generation = GENERATION_NUMBER_INFINITY;
5227c385 325
4d5430f7 326 if (repo_parse_commit(r, commit))
5227c385
DS
327 return ret;
328 for (i = 0; i < nr_reference; i++) {
4d5430f7 329 if (repo_parse_commit(r, reference[i]))
5227c385 330 return ret;
c752ad09
AK
331
332 generation = commit_graph_generation(reference[i]);
333 if (generation < min_generation)
334 min_generation = generation;
5227c385
DS
335 }
336
c752ad09
AK
337 generation = commit_graph_generation(commit);
338 if (generation > min_generation)
5227c385
DS
339 return ret;
340
4d5430f7 341 bases = paint_down_to_common(r, commit,
c383830a 342 nr_reference, reference,
c752ad09 343 generation);
5227c385
DS
344 if (commit->object.flags & PARENT2)
345 ret = 1;
346 clear_commit_marks(commit, all_flags);
347 clear_commit_marks_many(nr_reference, reference, all_flags);
348 free_commit_list(bases);
349 return ret;
350}
351
352/*
353 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
354 */
4d5430f7
SB
355int repo_in_merge_bases(struct repository *r,
356 struct commit *commit,
357 struct commit *reference)
5227c385 358{
80b8ada5
DS
359 int res;
360 struct commit_list *list = NULL;
361 struct commit_list **next = &list;
362
363 next = commit_list_append(commit, next);
364 res = repo_is_descendant_of(r, reference, list);
365 free_commit_list(list);
366
367 return res;
5227c385
DS
368}
369
370struct commit_list *reduce_heads(struct commit_list *heads)
371{
372 struct commit_list *p;
373 struct commit_list *result = NULL, **tail = &result;
374 struct commit **array;
375 int num_head, i;
376
377 if (!heads)
378 return NULL;
379
380 /* Uniquify */
381 for (p = heads; p; p = p->next)
382 p->item->object.flags &= ~STALE;
383 for (p = heads, num_head = 0; p; p = p->next) {
384 if (p->item->object.flags & STALE)
385 continue;
386 p->item->object.flags |= STALE;
387 num_head++;
388 }
389 array = xcalloc(num_head, sizeof(*array));
390 for (p = heads, i = 0; p; p = p->next) {
391 if (p->item->object.flags & STALE) {
392 array[i++] = p->item;
393 p->item->object.flags &= ~STALE;
394 }
395 }
ed8a0e3a 396 num_head = remove_redundant(the_repository, array, num_head);
5227c385
DS
397 for (i = 0; i < num_head; i++)
398 tail = &commit_list_insert(array[i], tail)->next;
399 free(array);
400 return result;
401}
402
403void reduce_heads_replace(struct commit_list **heads)
404{
405 struct commit_list *result = reduce_heads(*heads);
406 free_commit_list(*heads);
407 *heads = result;
408}
1d614d41 409
1d614d41
DS
410int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
411{
412 struct object *o;
413 struct commit *old_commit, *new_commit;
1e3497a2 414 struct commit_list *old_commit_list = NULL;
d546fe28 415 int ret;
1d614d41
DS
416
417 /*
418 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
419 * old_commit. Otherwise we require --force.
420 */
421 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
422 NULL, 0);
423 if (!o || o->type != OBJ_COMMIT)
424 return 0;
425 old_commit = (struct commit *) o;
426
427 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
428 NULL, 0);
429 if (!o || o->type != OBJ_COMMIT)
430 return 0;
431 new_commit = (struct commit *) o;
432
433 if (parse_commit(new_commit) < 0)
434 return 0;
435
1e3497a2 436 commit_list_insert(old_commit, &old_commit_list);
0258ed1e 437 ret = repo_is_descendant_of(the_repository,
c1ea625f 438 new_commit, old_commit_list);
d546fe28
RS
439 free_commit_list(old_commit_list);
440 return ret;
1d614d41 441}
920f93ca
DS
442
443/*
444 * Mimicking the real stack, this stack lives on the heap, avoiding stack
445 * overflows.
446 *
447 * At each recursion step, the stack items points to the commits whose
448 * ancestors are to be inspected.
449 */
450struct contains_stack {
451 int nr, alloc;
452 struct contains_stack_entry {
453 struct commit *commit;
454 struct commit_list *parents;
455 } *contains_stack;
456};
457
458static int in_commit_list(const struct commit_list *want, struct commit *c)
459{
460 for (; want; want = want->next)
e43d2dcc 461 if (oideq(&want->item->object.oid, &c->object.oid))
920f93ca
DS
462 return 1;
463 return 0;
464}
465
466/*
467 * Test whether the candidate is contained in the list.
468 * Do not recurse to find out, though, but return -1 if inconclusive.
469 */
470static enum contains_result contains_test(struct commit *candidate,
471 const struct commit_list *want,
472 struct contains_cache *cache,
473 uint32_t cutoff)
474{
475 enum contains_result *cached = contains_cache_at(cache, candidate);
476
477 /* If we already have the answer cached, return that. */
478 if (*cached)
479 return *cached;
480
481 /* or are we it? */
482 if (in_commit_list(want, candidate)) {
483 *cached = CONTAINS_YES;
484 return CONTAINS_YES;
485 }
486
487 /* Otherwise, we don't know; prepare to recurse */
488 parse_commit_or_die(candidate);
489
c49c82aa 490 if (commit_graph_generation(candidate) < cutoff)
920f93ca
DS
491 return CONTAINS_NO;
492
493 return CONTAINS_UNKNOWN;
494}
495
496static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
497{
498 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
499 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
500 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
501}
502
503static enum contains_result contains_tag_algo(struct commit *candidate,
504 const struct commit_list *want,
505 struct contains_cache *cache)
506{
507 struct contains_stack contains_stack = { 0, 0, NULL };
508 enum contains_result result;
509 uint32_t cutoff = GENERATION_NUMBER_INFINITY;
510 const struct commit_list *p;
511
512 for (p = want; p; p = p->next) {
c752ad09 513 uint32_t generation;
920f93ca
DS
514 struct commit *c = p->item;
515 load_commit_graph_info(the_repository, c);
c752ad09
AK
516 generation = commit_graph_generation(c);
517 if (generation < cutoff)
518 cutoff = generation;
920f93ca
DS
519 }
520
521 result = contains_test(candidate, want, cache, cutoff);
522 if (result != CONTAINS_UNKNOWN)
523 return result;
524
525 push_to_contains_stack(candidate, &contains_stack);
526 while (contains_stack.nr) {
527 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
528 struct commit *commit = entry->commit;
529 struct commit_list *parents = entry->parents;
530
531 if (!parents) {
532 *contains_cache_at(cache, commit) = CONTAINS_NO;
533 contains_stack.nr--;
534 }
535 /*
536 * If we just popped the stack, parents->item has been marked,
537 * therefore contains_test will return a meaningful yes/no.
538 */
539 else switch (contains_test(parents->item, want, cache, cutoff)) {
540 case CONTAINS_YES:
541 *contains_cache_at(cache, commit) = CONTAINS_YES;
542 contains_stack.nr--;
543 break;
544 case CONTAINS_NO:
545 entry->parents = parents->next;
546 break;
547 case CONTAINS_UNKNOWN:
548 push_to_contains_stack(parents->item, &contains_stack);
549 break;
550 }
551 }
552 free(contains_stack.contains_stack);
553 return contains_test(candidate, want, cache, cutoff);
554}
555
556int commit_contains(struct ref_filter *filter, struct commit *commit,
557 struct commit_list *list, struct contains_cache *cache)
558{
559 if (filter->with_commit_tag_algo)
560 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
c1ea625f 561 return repo_is_descendant_of(the_repository, commit, list);
920f93ca 562}
ba3ca1ed 563
4fbcca4e 564static int compare_commits_by_gen(const void *_a, const void *_b)
ba3ca1ed 565{
8628ace2
RS
566 const struct commit *a = *(const struct commit * const *)_a;
567 const struct commit *b = *(const struct commit * const *)_b;
ba3ca1ed 568
c752ad09
AK
569 uint32_t generation_a = commit_graph_generation(a);
570 uint32_t generation_b = commit_graph_generation(b);
571
572 if (generation_a < generation_b)
4fbcca4e 573 return -1;
c752ad09 574 if (generation_a > generation_b)
4fbcca4e
DS
575 return 1;
576 return 0;
ba3ca1ed
DS
577}
578
579int can_all_from_reach_with_flag(struct object_array *from,
580 unsigned int with_flag,
581 unsigned int assign_flag,
4fbcca4e
DS
582 time_t min_commit_date,
583 uint32_t min_generation)
ba3ca1ed 584{
4fbcca4e 585 struct commit **list = NULL;
ba3ca1ed 586 int i;
b67f6b26 587 int nr_commits;
4fbcca4e 588 int result = 1;
ba3ca1ed 589
4fbcca4e 590 ALLOC_ARRAY(list, from->nr);
b67f6b26 591 nr_commits = 0;
ba3ca1ed 592 for (i = 0; i < from->nr; i++) {
b67f6b26 593 struct object *from_one = from->objects[i].item;
ba3ca1ed 594
b67f6b26
DS
595 if (!from_one || from_one->flags & assign_flag)
596 continue;
597
598 from_one = deref_tag(the_repository, from_one,
599 "a from object", 0);
600 if (!from_one || from_one->type != OBJ_COMMIT) {
85806440
DS
601 /*
602 * no way to tell if this is reachable by
b67f6b26
DS
603 * looking at the ancestry chain alone, so
604 * leave a note to ourselves not to worry about
605 * this object anymore.
606 */
607 from->objects[i].item->flags |= assign_flag;
608 continue;
609 }
610
611 list[nr_commits] = (struct commit *)from_one;
612 if (parse_commit(list[nr_commits]) ||
c49c82aa 613 commit_graph_generation(list[nr_commits]) < min_generation) {
b67f6b26
DS
614 result = 0;
615 goto cleanup;
616 }
617
618 nr_commits++;
ba3ca1ed 619 }
4fbcca4e 620
b67f6b26 621 QSORT(list, nr_commits, compare_commits_by_gen);
4fbcca4e 622
b67f6b26 623 for (i = 0; i < nr_commits; i++) {
4fbcca4e
DS
624 /* DFS from list[i] */
625 struct commit_list *stack = NULL;
626
627 list[i]->object.flags |= assign_flag;
628 commit_list_insert(list[i], &stack);
629
630 while (stack) {
631 struct commit_list *parent;
632
b6723e46 633 if (stack->item->object.flags & (with_flag | RESULT)) {
4fbcca4e 634 pop_commit(&stack);
b6723e46
DS
635 if (stack)
636 stack->item->object.flags |= RESULT;
4fbcca4e
DS
637 continue;
638 }
639
640 for (parent = stack->item->parents; parent; parent = parent->next) {
641 if (parent->item->object.flags & (with_flag | RESULT))
642 stack->item->object.flags |= RESULT;
643
644 if (!(parent->item->object.flags & assign_flag)) {
645 parent->item->object.flags |= assign_flag;
646
647 if (parse_commit(parent->item) ||
648 parent->item->date < min_commit_date ||
c49c82aa 649 commit_graph_generation(parent->item) < min_generation)
4fbcca4e
DS
650 continue;
651
652 commit_list_insert(parent->item, &stack);
653 break;
654 }
655 }
656
657 if (!parent)
658 pop_commit(&stack);
659 }
660
661 if (!(list[i]->object.flags & (with_flag | RESULT))) {
662 result = 0;
663 goto cleanup;
664 }
665 }
666
667cleanup:
85806440 668 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
4067a646
DS
669 free(list);
670
671 for (i = 0; i < from->nr; i++)
672 from->objects[i].item->flags &= ~assign_flag;
673
4fbcca4e 674 return result;
ba3ca1ed 675}
1792bc12
DS
676
677int can_all_from_reach(struct commit_list *from, struct commit_list *to,
678 int cutoff_by_min_date)
679{
680 struct object_array from_objs = OBJECT_ARRAY_INIT;
681 time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
682 struct commit_list *from_iter = from, *to_iter = to;
683 int result;
4fbcca4e 684 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
1792bc12
DS
685
686 while (from_iter) {
687 add_object_array(&from_iter->item->object, NULL, &from_objs);
688
689 if (!parse_commit(from_iter->item)) {
c752ad09 690 uint32_t generation;
1792bc12
DS
691 if (from_iter->item->date < min_commit_date)
692 min_commit_date = from_iter->item->date;
4fbcca4e 693
c752ad09
AK
694 generation = commit_graph_generation(from_iter->item);
695 if (generation < min_generation)
696 min_generation = generation;
1792bc12
DS
697 }
698
699 from_iter = from_iter->next;
700 }
701
702 while (to_iter) {
703 if (!parse_commit(to_iter->item)) {
c752ad09 704 uint32_t generation;
1792bc12
DS
705 if (to_iter->item->date < min_commit_date)
706 min_commit_date = to_iter->item->date;
4fbcca4e 707
c752ad09
AK
708 generation = commit_graph_generation(to_iter->item);
709 if (generation < min_generation)
710 min_generation = generation;
1792bc12
DS
711 }
712
713 to_iter->item->object.flags |= PARENT2;
714
715 to_iter = to_iter->next;
716 }
717
718 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
4fbcca4e 719 min_commit_date, min_generation);
1792bc12
DS
720
721 while (from) {
722 clear_commit_marks(from->item, PARENT1);
723 from = from->next;
724 }
725
726 while (to) {
727 clear_commit_marks(to->item, PARENT2);
728 to = to->next;
729 }
730
731 object_array_clear(&from_objs);
732 return result;
733}
fcb2c076
DS
734
735struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
736 struct commit **to, int nr_to,
737 unsigned int reachable_flag)
738{
739 struct commit **item;
740 struct commit *current;
741 struct commit_list *found_commits = NULL;
742 struct commit **to_last = to + nr_to;
743 struct commit **from_last = from + nr_from;
744 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
745 int num_to_find = 0;
746
747 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
748
749 for (item = to; item < to_last; item++) {
c752ad09 750 uint32_t generation;
fcb2c076
DS
751 struct commit *c = *item;
752
753 parse_commit(c);
c752ad09
AK
754 generation = commit_graph_generation(c);
755 if (generation < min_generation)
756 min_generation = generation;
fcb2c076
DS
757
758 if (!(c->object.flags & PARENT1)) {
759 c->object.flags |= PARENT1;
760 num_to_find++;
761 }
762 }
763
764 for (item = from; item < from_last; item++) {
765 struct commit *c = *item;
766 if (!(c->object.flags & PARENT2)) {
767 c->object.flags |= PARENT2;
768 parse_commit(c);
769
770 prio_queue_put(&queue, *item);
771 }
772 }
773
774 while (num_to_find && (current = prio_queue_get(&queue)) != NULL) {
775 struct commit_list *parents;
776
777 if (current->object.flags & PARENT1) {
778 current->object.flags &= ~PARENT1;
779 current->object.flags |= reachable_flag;
780 commit_list_insert(current, &found_commits);
781 num_to_find--;
782 }
783
784 for (parents = current->parents; parents; parents = parents->next) {
785 struct commit *p = parents->item;
786
787 parse_commit(p);
788
c49c82aa 789 if (commit_graph_generation(p) < min_generation)
fcb2c076
DS
790 continue;
791
792 if (p->object.flags & PARENT2)
793 continue;
794
795 p->object.flags |= PARENT2;
796 prio_queue_put(&queue, p);
797 }
798 }
799
800 clear_commit_marks_many(nr_to, to, PARENT1);
801 clear_commit_marks_many(nr_from, from, PARENT2);
802
803 return found_commits;
804}