]> git.ipfire.org Git - thirdparty/git.git/blob - commit-reach.c
Merge branch 'jc/fake-lstat'
[thirdparty/git.git] / commit-reach.c
1 #include "git-compat-util.h"
2 #include "commit.h"
3 #include "commit-graph.h"
4 #include "decorate.h"
5 #include "hex.h"
6 #include "prio-queue.h"
7 #include "tree.h"
8 #include "ref-filter.h"
9 #include "revision.h"
10 #include "tag.h"
11 #include "commit-reach.h"
12 #include "ewah/ewok.h"
13
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)
19
20 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
21
22 static int compare_commits_by_gen(const void *_a, const void *_b)
23 {
24 const struct commit *a = *(const struct commit * const *)_a;
25 const struct commit *b = *(const struct commit * const *)_b;
26
27 timestamp_t generation_a = commit_graph_generation(a);
28 timestamp_t generation_b = commit_graph_generation(b);
29
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;
39 }
40
41 static int queue_has_nonstale(struct prio_queue *queue)
42 {
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;
48 }
49 return 0;
50 }
51
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)
57 {
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;
62
63 if (!min_generation && !corrected_commit_dates_enabled(r))
64 queue.compare = compare_commits_by_commit_date;
65
66 one->object.flags |= PARENT1;
67 if (!n) {
68 commit_list_append(one, &result);
69 return result;
70 }
71 prio_queue_put(&queue, one);
72
73 for (i = 0; i < n; i++) {
74 twos[i]->object.flags |= PARENT2;
75 prio_queue_put(&queue, twos[i]);
76 }
77
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);
83
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;
89
90 if (generation < min_generation)
91 break;
92
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);
98 }
99 /* Mark parents of a found merge stale */
100 flags |= STALE;
101 }
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);
112 }
113 }
114
115 clear_prio_queue(&queue);
116 return result;
117 }
118
119 static struct commit_list *merge_bases_many(struct repository *r,
120 struct commit *one, int n,
121 struct commit **twos)
122 {
123 struct commit_list *list = NULL;
124 struct commit_list *result = NULL;
125 int i;
126
127 for (i = 0; i < n; i++) {
128 if (one == twos[i])
129 /*
130 * We do not mark this even with RESULT so we do not
131 * have to clean it up.
132 */
133 return commit_list_insert(one, &result);
134 }
135
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;
141 }
142
143 list = paint_down_to_common(r, one, n, twos, 0);
144
145 while (list) {
146 struct commit *commit = pop_commit(&list);
147 if (!(commit->object.flags & STALE))
148 commit_list_insert_by_date(commit, &result);
149 }
150 return result;
151 }
152
153 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
154 {
155 struct commit_list *i, *j, *k, *ret = NULL;
156
157 if (!in)
158 return ret;
159
160 commit_list_insert(in->item, &ret);
161
162 for (i = in->next; i; i = i->next) {
163 struct commit_list *new_commits = NULL, *end = NULL;
164
165 for (j = ret; j; j = j->next) {
166 struct commit_list *bases;
167 bases = repo_get_merge_bases(the_repository, i->item,
168 j->item);
169 if (!new_commits)
170 new_commits = bases;
171 else
172 end->next = bases;
173 for (k = bases; k; k = k->next)
174 end = k;
175 }
176 free_commit_list(ret);
177 ret = new_commits;
178 }
179 return ret;
180 }
181
182 static int remove_redundant_no_gen(struct repository *r,
183 struct commit **array, int cnt)
184 {
185 struct commit **work;
186 unsigned char *redundant;
187 int *filled_index;
188 int i, j, filled;
189
190 CALLOC_ARRAY(work, cnt);
191 redundant = xcalloc(cnt, 1);
192 ALLOC_ARRAY(filled_index, cnt - 1);
193
194 for (i = 0; i < cnt; i++)
195 repo_parse_commit(r, array[i]);
196 for (i = 0; i < cnt; i++) {
197 struct commit_list *common;
198 timestamp_t min_generation = commit_graph_generation(array[i]);
199
200 if (redundant[i])
201 continue;
202 for (j = filled = 0; j < cnt; j++) {
203 timestamp_t curr_generation;
204 if (i == j || redundant[j])
205 continue;
206 filled_index[filled] = j;
207 work[filled++] = array[j];
208
209 curr_generation = commit_graph_generation(array[j]);
210 if (curr_generation < min_generation)
211 min_generation = curr_generation;
212 }
213 common = paint_down_to_common(r, array[i], filled,
214 work, min_generation);
215 if (array[i]->object.flags & PARENT2)
216 redundant[i] = 1;
217 for (j = 0; j < filled; j++)
218 if (work[j]->object.flags & PARENT1)
219 redundant[filled_index[j]] = 1;
220 clear_commit_marks(array[i], all_flags);
221 clear_commit_marks_many(filled, work, all_flags);
222 free_commit_list(common);
223 }
224
225 /* Now collect the result */
226 COPY_ARRAY(work, array, cnt);
227 for (i = filled = 0; i < cnt; i++)
228 if (!redundant[i])
229 array[filled++] = work[i];
230 free(work);
231 free(redundant);
232 free(filled_index);
233 return filled;
234 }
235
236 static int remove_redundant_with_gen(struct repository *r,
237 struct commit **array, int cnt)
238 {
239 int i, count_non_stale = 0, count_still_independent = cnt;
240 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
241 struct commit **walk_start, **sorted;
242 size_t walk_start_nr = 0, walk_start_alloc = cnt;
243 int min_gen_pos = 0;
244
245 /*
246 * Sort the input by generation number, ascending. This allows
247 * us to increase the "min_generation" limit when we discover
248 * the commit with lowest generation is STALE. The index
249 * min_gen_pos points to the current position within 'array'
250 * that is not yet known to be STALE.
251 */
252 DUP_ARRAY(sorted, array, cnt);
253 QSORT(sorted, cnt, compare_commits_by_gen);
254 min_generation = commit_graph_generation(sorted[0]);
255
256 ALLOC_ARRAY(walk_start, walk_start_alloc);
257
258 /* Mark all parents of the input as STALE */
259 for (i = 0; i < cnt; i++) {
260 struct commit_list *parents;
261
262 repo_parse_commit(r, array[i]);
263 array[i]->object.flags |= RESULT;
264 parents = array[i]->parents;
265
266 while (parents) {
267 repo_parse_commit(r, parents->item);
268 if (!(parents->item->object.flags & STALE)) {
269 parents->item->object.flags |= STALE;
270 ALLOC_GROW(walk_start, walk_start_nr + 1, walk_start_alloc);
271 walk_start[walk_start_nr++] = parents->item;
272 }
273 parents = parents->next;
274 }
275 }
276
277 QSORT(walk_start, walk_start_nr, compare_commits_by_gen);
278
279 /* remove STALE bit for now to allow walking through parents */
280 for (i = 0; i < walk_start_nr; i++)
281 walk_start[i]->object.flags &= ~STALE;
282
283 /*
284 * Start walking from the highest generation. Hopefully, it will
285 * find all other items during the first-parent walk, and we can
286 * terminate early. Otherwise, we will do the same amount of work
287 * as before.
288 */
289 for (i = walk_start_nr - 1; i >= 0 && count_still_independent > 1; i--) {
290 /* push the STALE bits up to min generation */
291 struct commit_list *stack = NULL;
292
293 commit_list_insert(walk_start[i], &stack);
294 walk_start[i]->object.flags |= STALE;
295
296 while (stack) {
297 struct commit_list *parents;
298 struct commit *c = stack->item;
299
300 repo_parse_commit(r, c);
301
302 if (c->object.flags & RESULT) {
303 c->object.flags &= ~RESULT;
304 if (--count_still_independent <= 1)
305 break;
306 if (oideq(&c->object.oid, &sorted[min_gen_pos]->object.oid)) {
307 while (min_gen_pos < cnt - 1 &&
308 (sorted[min_gen_pos]->object.flags & STALE))
309 min_gen_pos++;
310 min_generation = commit_graph_generation(sorted[min_gen_pos]);
311 }
312 }
313
314 if (commit_graph_generation(c) < min_generation) {
315 pop_commit(&stack);
316 continue;
317 }
318
319 parents = c->parents;
320 while (parents) {
321 if (!(parents->item->object.flags & STALE)) {
322 parents->item->object.flags |= STALE;
323 commit_list_insert(parents->item, &stack);
324 break;
325 }
326 parents = parents->next;
327 }
328
329 /* pop if all parents have been visited already */
330 if (!parents)
331 pop_commit(&stack);
332 }
333 free_commit_list(stack);
334 }
335 free(sorted);
336
337 /* clear result */
338 for (i = 0; i < cnt; i++)
339 array[i]->object.flags &= ~RESULT;
340
341 /* rearrange array */
342 for (i = count_non_stale = 0; i < cnt; i++) {
343 if (!(array[i]->object.flags & STALE))
344 array[count_non_stale++] = array[i];
345 }
346
347 /* clear marks */
348 clear_commit_marks_many(walk_start_nr, walk_start, STALE);
349 free(walk_start);
350
351 return count_non_stale;
352 }
353
354 static int remove_redundant(struct repository *r, struct commit **array, int cnt)
355 {
356 /*
357 * Some commit in the array may be an ancestor of
358 * another commit. Move the independent commits to the
359 * beginning of 'array' and return their number. Callers
360 * should not rely upon the contents of 'array' after
361 * that number.
362 */
363 if (generation_numbers_enabled(r)) {
364 int i;
365
366 /*
367 * If we have a single commit with finite generation
368 * number, then the _with_gen algorithm is preferred.
369 */
370 for (i = 0; i < cnt; i++) {
371 if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY)
372 return remove_redundant_with_gen(r, array, cnt);
373 }
374 }
375
376 return remove_redundant_no_gen(r, array, cnt);
377 }
378
379 static struct commit_list *get_merge_bases_many_0(struct repository *r,
380 struct commit *one,
381 int n,
382 struct commit **twos,
383 int cleanup)
384 {
385 struct commit_list *list;
386 struct commit **rslt;
387 struct commit_list *result;
388 int cnt, i;
389
390 result = merge_bases_many(r, one, n, twos);
391 for (i = 0; i < n; i++) {
392 if (one == twos[i])
393 return result;
394 }
395 if (!result || !result->next) {
396 if (cleanup) {
397 clear_commit_marks(one, all_flags);
398 clear_commit_marks_many(n, twos, all_flags);
399 }
400 return result;
401 }
402
403 /* There are more than one */
404 cnt = commit_list_count(result);
405 CALLOC_ARRAY(rslt, cnt);
406 for (list = result, i = 0; list; list = list->next)
407 rslt[i++] = list->item;
408 free_commit_list(result);
409
410 clear_commit_marks(one, all_flags);
411 clear_commit_marks_many(n, twos, all_flags);
412
413 cnt = remove_redundant(r, rslt, cnt);
414 result = NULL;
415 for (i = 0; i < cnt; i++)
416 commit_list_insert_by_date(rslt[i], &result);
417 free(rslt);
418 return result;
419 }
420
421 struct commit_list *repo_get_merge_bases_many(struct repository *r,
422 struct commit *one,
423 int n,
424 struct commit **twos)
425 {
426 return get_merge_bases_many_0(r, one, n, twos, 1);
427 }
428
429 struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
430 struct commit *one,
431 int n,
432 struct commit **twos)
433 {
434 return get_merge_bases_many_0(r, one, n, twos, 0);
435 }
436
437 struct commit_list *repo_get_merge_bases(struct repository *r,
438 struct commit *one,
439 struct commit *two)
440 {
441 return get_merge_bases_many_0(r, one, 1, &two, 1);
442 }
443
444 /*
445 * Is "commit" a descendant of one of the elements on the "with_commit" list?
446 */
447 int repo_is_descendant_of(struct repository *r,
448 struct commit *commit,
449 struct commit_list *with_commit)
450 {
451 if (!with_commit)
452 return 1;
453
454 if (generation_numbers_enabled(r)) {
455 struct commit_list *from_list = NULL;
456 int result;
457 commit_list_insert(commit, &from_list);
458 result = can_all_from_reach(from_list, with_commit, 0);
459 free_commit_list(from_list);
460 return result;
461 } else {
462 while (with_commit) {
463 struct commit *other;
464
465 other = with_commit->item;
466 with_commit = with_commit->next;
467 if (repo_in_merge_bases_many(r, other, 1, &commit))
468 return 1;
469 }
470 return 0;
471 }
472 }
473
474 /*
475 * Is "commit" an ancestor of one of the "references"?
476 */
477 int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
478 int nr_reference, struct commit **reference)
479 {
480 struct commit_list *bases;
481 int ret = 0, i;
482 timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO;
483
484 if (repo_parse_commit(r, commit))
485 return ret;
486 for (i = 0; i < nr_reference; i++) {
487 if (repo_parse_commit(r, reference[i]))
488 return ret;
489
490 generation = commit_graph_generation(reference[i]);
491 if (generation > max_generation)
492 max_generation = generation;
493 }
494
495 generation = commit_graph_generation(commit);
496 if (generation > max_generation)
497 return ret;
498
499 bases = paint_down_to_common(r, commit,
500 nr_reference, reference,
501 generation);
502 if (commit->object.flags & PARENT2)
503 ret = 1;
504 clear_commit_marks(commit, all_flags);
505 clear_commit_marks_many(nr_reference, reference, all_flags);
506 free_commit_list(bases);
507 return ret;
508 }
509
510 /*
511 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
512 */
513 int repo_in_merge_bases(struct repository *r,
514 struct commit *commit,
515 struct commit *reference)
516 {
517 int res;
518 struct commit_list *list = NULL;
519 struct commit_list **next = &list;
520
521 next = commit_list_append(commit, next);
522 res = repo_is_descendant_of(r, reference, list);
523 free_commit_list(list);
524
525 return res;
526 }
527
528 struct commit_list *reduce_heads(struct commit_list *heads)
529 {
530 struct commit_list *p;
531 struct commit_list *result = NULL, **tail = &result;
532 struct commit **array;
533 int num_head, i;
534
535 if (!heads)
536 return NULL;
537
538 /* Uniquify */
539 for (p = heads; p; p = p->next)
540 p->item->object.flags &= ~STALE;
541 for (p = heads, num_head = 0; p; p = p->next) {
542 if (p->item->object.flags & STALE)
543 continue;
544 p->item->object.flags |= STALE;
545 num_head++;
546 }
547 CALLOC_ARRAY(array, num_head);
548 for (p = heads, i = 0; p; p = p->next) {
549 if (p->item->object.flags & STALE) {
550 array[i++] = p->item;
551 p->item->object.flags &= ~STALE;
552 }
553 }
554 num_head = remove_redundant(the_repository, array, num_head);
555 for (i = 0; i < num_head; i++)
556 tail = &commit_list_insert(array[i], tail)->next;
557 free(array);
558 return result;
559 }
560
561 void reduce_heads_replace(struct commit_list **heads)
562 {
563 struct commit_list *result = reduce_heads(*heads);
564 free_commit_list(*heads);
565 *heads = result;
566 }
567
568 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
569 {
570 struct object *o;
571 struct commit *old_commit, *new_commit;
572 struct commit_list *old_commit_list = NULL;
573 int ret;
574
575 /*
576 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
577 * old_commit. Otherwise we require --force.
578 */
579 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
580 NULL, 0);
581 if (!o || o->type != OBJ_COMMIT)
582 return 0;
583 old_commit = (struct commit *) o;
584
585 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
586 NULL, 0);
587 if (!o || o->type != OBJ_COMMIT)
588 return 0;
589 new_commit = (struct commit *) o;
590
591 if (repo_parse_commit(the_repository, new_commit) < 0)
592 return 0;
593
594 commit_list_insert(old_commit, &old_commit_list);
595 ret = repo_is_descendant_of(the_repository,
596 new_commit, old_commit_list);
597 free_commit_list(old_commit_list);
598 return ret;
599 }
600
601 /*
602 * Mimicking the real stack, this stack lives on the heap, avoiding stack
603 * overflows.
604 *
605 * At each recursion step, the stack items points to the commits whose
606 * ancestors are to be inspected.
607 */
608 struct contains_stack {
609 int nr, alloc;
610 struct contains_stack_entry {
611 struct commit *commit;
612 struct commit_list *parents;
613 } *contains_stack;
614 };
615
616 static int in_commit_list(const struct commit_list *want, struct commit *c)
617 {
618 for (; want; want = want->next)
619 if (oideq(&want->item->object.oid, &c->object.oid))
620 return 1;
621 return 0;
622 }
623
624 /*
625 * Test whether the candidate is contained in the list.
626 * Do not recurse to find out, though, but return -1 if inconclusive.
627 */
628 static enum contains_result contains_test(struct commit *candidate,
629 const struct commit_list *want,
630 struct contains_cache *cache,
631 timestamp_t cutoff)
632 {
633 enum contains_result *cached = contains_cache_at(cache, candidate);
634
635 /* If we already have the answer cached, return that. */
636 if (*cached)
637 return *cached;
638
639 /* or are we it? */
640 if (in_commit_list(want, candidate)) {
641 *cached = CONTAINS_YES;
642 return CONTAINS_YES;
643 }
644
645 /* Otherwise, we don't know; prepare to recurse */
646 parse_commit_or_die(candidate);
647
648 if (commit_graph_generation(candidate) < cutoff)
649 return CONTAINS_NO;
650
651 return CONTAINS_UNKNOWN;
652 }
653
654 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
655 {
656 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
657 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
658 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
659 }
660
661 static enum contains_result contains_tag_algo(struct commit *candidate,
662 const struct commit_list *want,
663 struct contains_cache *cache)
664 {
665 struct contains_stack contains_stack = { 0, 0, NULL };
666 enum contains_result result;
667 timestamp_t cutoff = GENERATION_NUMBER_INFINITY;
668 const struct commit_list *p;
669
670 for (p = want; p; p = p->next) {
671 timestamp_t generation;
672 struct commit *c = p->item;
673 load_commit_graph_info(the_repository, c);
674 generation = commit_graph_generation(c);
675 if (generation < cutoff)
676 cutoff = generation;
677 }
678
679 result = contains_test(candidate, want, cache, cutoff);
680 if (result != CONTAINS_UNKNOWN)
681 return result;
682
683 push_to_contains_stack(candidate, &contains_stack);
684 while (contains_stack.nr) {
685 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
686 struct commit *commit = entry->commit;
687 struct commit_list *parents = entry->parents;
688
689 if (!parents) {
690 *contains_cache_at(cache, commit) = CONTAINS_NO;
691 contains_stack.nr--;
692 }
693 /*
694 * If we just popped the stack, parents->item has been marked,
695 * therefore contains_test will return a meaningful yes/no.
696 */
697 else switch (contains_test(parents->item, want, cache, cutoff)) {
698 case CONTAINS_YES:
699 *contains_cache_at(cache, commit) = CONTAINS_YES;
700 contains_stack.nr--;
701 break;
702 case CONTAINS_NO:
703 entry->parents = parents->next;
704 break;
705 case CONTAINS_UNKNOWN:
706 push_to_contains_stack(parents->item, &contains_stack);
707 break;
708 }
709 }
710 free(contains_stack.contains_stack);
711 return contains_test(candidate, want, cache, cutoff);
712 }
713
714 int commit_contains(struct ref_filter *filter, struct commit *commit,
715 struct commit_list *list, struct contains_cache *cache)
716 {
717 if (filter->with_commit_tag_algo)
718 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
719 return repo_is_descendant_of(the_repository, commit, list);
720 }
721
722 int can_all_from_reach_with_flag(struct object_array *from,
723 unsigned int with_flag,
724 unsigned int assign_flag,
725 time_t min_commit_date,
726 timestamp_t min_generation)
727 {
728 struct commit **list = NULL;
729 int i;
730 int nr_commits;
731 int result = 1;
732
733 ALLOC_ARRAY(list, from->nr);
734 nr_commits = 0;
735 for (i = 0; i < from->nr; i++) {
736 struct object *from_one = from->objects[i].item;
737
738 if (!from_one || from_one->flags & assign_flag)
739 continue;
740
741 from_one = deref_tag(the_repository, from_one,
742 "a from object", 0);
743 if (!from_one || from_one->type != OBJ_COMMIT) {
744 /*
745 * no way to tell if this is reachable by
746 * looking at the ancestry chain alone, so
747 * leave a note to ourselves not to worry about
748 * this object anymore.
749 */
750 from->objects[i].item->flags |= assign_flag;
751 continue;
752 }
753
754 list[nr_commits] = (struct commit *)from_one;
755 if (repo_parse_commit(the_repository, list[nr_commits]) ||
756 commit_graph_generation(list[nr_commits]) < min_generation) {
757 result = 0;
758 goto cleanup;
759 }
760
761 nr_commits++;
762 }
763
764 QSORT(list, nr_commits, compare_commits_by_gen);
765
766 for (i = 0; i < nr_commits; i++) {
767 /* DFS from list[i] */
768 struct commit_list *stack = NULL;
769
770 list[i]->object.flags |= assign_flag;
771 commit_list_insert(list[i], &stack);
772
773 while (stack) {
774 struct commit_list *parent;
775
776 if (stack->item->object.flags & (with_flag | RESULT)) {
777 pop_commit(&stack);
778 if (stack)
779 stack->item->object.flags |= RESULT;
780 continue;
781 }
782
783 for (parent = stack->item->parents; parent; parent = parent->next) {
784 if (parent->item->object.flags & (with_flag | RESULT))
785 stack->item->object.flags |= RESULT;
786
787 if (!(parent->item->object.flags & assign_flag)) {
788 parent->item->object.flags |= assign_flag;
789
790 if (repo_parse_commit(the_repository, parent->item) ||
791 parent->item->date < min_commit_date ||
792 commit_graph_generation(parent->item) < min_generation)
793 continue;
794
795 commit_list_insert(parent->item, &stack);
796 break;
797 }
798 }
799
800 if (!parent)
801 pop_commit(&stack);
802 }
803
804 if (!(list[i]->object.flags & (with_flag | RESULT))) {
805 result = 0;
806 goto cleanup;
807 }
808 }
809
810 cleanup:
811 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
812 free(list);
813
814 for (i = 0; i < from->nr; i++) {
815 struct object *from_one = from->objects[i].item;
816
817 if (from_one)
818 from_one->flags &= ~assign_flag;
819 }
820
821 return result;
822 }
823
824 int can_all_from_reach(struct commit_list *from, struct commit_list *to,
825 int cutoff_by_min_date)
826 {
827 struct object_array from_objs = OBJECT_ARRAY_INIT;
828 time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
829 struct commit_list *from_iter = from, *to_iter = to;
830 int result;
831 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
832
833 while (from_iter) {
834 add_object_array(&from_iter->item->object, NULL, &from_objs);
835
836 if (!repo_parse_commit(the_repository, from_iter->item)) {
837 timestamp_t generation;
838 if (from_iter->item->date < min_commit_date)
839 min_commit_date = from_iter->item->date;
840
841 generation = commit_graph_generation(from_iter->item);
842 if (generation < min_generation)
843 min_generation = generation;
844 }
845
846 from_iter = from_iter->next;
847 }
848
849 while (to_iter) {
850 if (!repo_parse_commit(the_repository, to_iter->item)) {
851 timestamp_t generation;
852 if (to_iter->item->date < min_commit_date)
853 min_commit_date = to_iter->item->date;
854
855 generation = commit_graph_generation(to_iter->item);
856 if (generation < min_generation)
857 min_generation = generation;
858 }
859
860 to_iter->item->object.flags |= PARENT2;
861
862 to_iter = to_iter->next;
863 }
864
865 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
866 min_commit_date, min_generation);
867
868 while (from) {
869 clear_commit_marks(from->item, PARENT1);
870 from = from->next;
871 }
872
873 while (to) {
874 clear_commit_marks(to->item, PARENT2);
875 to = to->next;
876 }
877
878 object_array_clear(&from_objs);
879 return result;
880 }
881
882 struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
883 struct commit **to, int nr_to,
884 unsigned int reachable_flag)
885 {
886 struct commit **item;
887 struct commit *current;
888 struct commit_list *found_commits = NULL;
889 struct commit **to_last = to + nr_to;
890 struct commit **from_last = from + nr_from;
891 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
892 int num_to_find = 0;
893
894 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
895
896 for (item = to; item < to_last; item++) {
897 timestamp_t generation;
898 struct commit *c = *item;
899
900 repo_parse_commit(the_repository, c);
901 generation = commit_graph_generation(c);
902 if (generation < min_generation)
903 min_generation = generation;
904
905 if (!(c->object.flags & PARENT1)) {
906 c->object.flags |= PARENT1;
907 num_to_find++;
908 }
909 }
910
911 for (item = from; item < from_last; item++) {
912 struct commit *c = *item;
913 if (!(c->object.flags & PARENT2)) {
914 c->object.flags |= PARENT2;
915 repo_parse_commit(the_repository, c);
916
917 prio_queue_put(&queue, *item);
918 }
919 }
920
921 while (num_to_find && (current = prio_queue_get(&queue)) != NULL) {
922 struct commit_list *parents;
923
924 if (current->object.flags & PARENT1) {
925 current->object.flags &= ~PARENT1;
926 current->object.flags |= reachable_flag;
927 commit_list_insert(current, &found_commits);
928 num_to_find--;
929 }
930
931 for (parents = current->parents; parents; parents = parents->next) {
932 struct commit *p = parents->item;
933
934 repo_parse_commit(the_repository, p);
935
936 if (commit_graph_generation(p) < min_generation)
937 continue;
938
939 if (p->object.flags & PARENT2)
940 continue;
941
942 p->object.flags |= PARENT2;
943 prio_queue_put(&queue, p);
944 }
945 }
946
947 clear_prio_queue(&queue);
948
949 clear_commit_marks_many(nr_to, to, PARENT1);
950 clear_commit_marks_many(nr_from, from, PARENT2);
951
952 return found_commits;
953 }
954
955 define_commit_slab(bit_arrays, struct bitmap *);
956 static struct bit_arrays bit_arrays;
957
958 static void insert_no_dup(struct prio_queue *queue, struct commit *c)
959 {
960 if (c->object.flags & PARENT2)
961 return;
962 prio_queue_put(queue, c);
963 c->object.flags |= PARENT2;
964 }
965
966 static struct bitmap *get_bit_array(struct commit *c, int width)
967 {
968 struct bitmap **bitmap = bit_arrays_at(&bit_arrays, c);
969 if (!*bitmap)
970 *bitmap = bitmap_word_alloc(width);
971 return *bitmap;
972 }
973
974 static void free_bit_array(struct commit *c)
975 {
976 struct bitmap **bitmap = bit_arrays_at(&bit_arrays, c);
977 if (!*bitmap)
978 return;
979 bitmap_free(*bitmap);
980 *bitmap = NULL;
981 }
982
983 void ahead_behind(struct repository *r,
984 struct commit **commits, size_t commits_nr,
985 struct ahead_behind_count *counts, size_t counts_nr)
986 {
987 struct prio_queue queue = { .compare = compare_commits_by_gen_then_commit_date };
988 size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD);
989
990 if (!commits_nr || !counts_nr)
991 return;
992
993 for (size_t i = 0; i < counts_nr; i++) {
994 counts[i].ahead = 0;
995 counts[i].behind = 0;
996 }
997
998 ensure_generations_valid(r, commits, commits_nr);
999
1000 init_bit_arrays(&bit_arrays);
1001
1002 for (size_t i = 0; i < commits_nr; i++) {
1003 struct commit *c = commits[i];
1004 struct bitmap *bitmap = get_bit_array(c, width);
1005
1006 bitmap_set(bitmap, i);
1007 insert_no_dup(&queue, c);
1008 }
1009
1010 while (queue_has_nonstale(&queue)) {
1011 struct commit *c = prio_queue_get(&queue);
1012 struct commit_list *p;
1013 struct bitmap *bitmap_c = get_bit_array(c, width);
1014
1015 for (size_t i = 0; i < counts_nr; i++) {
1016 int reach_from_tip = !!bitmap_get(bitmap_c, counts[i].tip_index);
1017 int reach_from_base = !!bitmap_get(bitmap_c, counts[i].base_index);
1018
1019 if (reach_from_tip ^ reach_from_base) {
1020 if (reach_from_base)
1021 counts[i].behind++;
1022 else
1023 counts[i].ahead++;
1024 }
1025 }
1026
1027 for (p = c->parents; p; p = p->next) {
1028 struct bitmap *bitmap_p;
1029
1030 repo_parse_commit(r, p->item);
1031
1032 bitmap_p = get_bit_array(p->item, width);
1033 bitmap_or(bitmap_p, bitmap_c);
1034
1035 /*
1036 * If this parent is reachable from every starting
1037 * commit, then none of its ancestors can contribute
1038 * to the ahead/behind count. Mark it as STALE, so
1039 * we can stop the walk when every commit in the
1040 * queue is STALE.
1041 */
1042 if (bitmap_popcount(bitmap_p) == commits_nr)
1043 p->item->object.flags |= STALE;
1044
1045 insert_no_dup(&queue, p->item);
1046 }
1047
1048 free_bit_array(c);
1049 }
1050
1051 /* STALE is used here, PARENT2 is used by insert_no_dup(). */
1052 repo_clear_commit_marks(r, PARENT2 | STALE);
1053 clear_bit_arrays(&bit_arrays);
1054 clear_prio_queue(&queue);
1055 }
1056
1057 struct commit_and_index {
1058 struct commit *commit;
1059 unsigned int index;
1060 timestamp_t generation;
1061 };
1062
1063 static int compare_commit_and_index_by_generation(const void *va, const void *vb)
1064 {
1065 const struct commit_and_index *a = (const struct commit_and_index *)va;
1066 const struct commit_and_index *b = (const struct commit_and_index *)vb;
1067
1068 if (a->generation > b->generation)
1069 return 1;
1070 if (a->generation < b->generation)
1071 return -1;
1072 return 0;
1073 }
1074
1075 void tips_reachable_from_bases(struct repository *r,
1076 struct commit_list *bases,
1077 struct commit **tips, size_t tips_nr,
1078 int mark)
1079 {
1080 struct commit_and_index *commits;
1081 size_t min_generation_index = 0;
1082 timestamp_t min_generation;
1083 struct commit_list *stack = NULL;
1084
1085 if (!bases || !tips || !tips_nr)
1086 return;
1087
1088 /*
1089 * Do a depth-first search starting at 'bases' to search for the
1090 * tips. Stop at the lowest (un-found) generation number. When
1091 * finding the lowest commit, increase the minimum generation
1092 * number to the next lowest (un-found) generation number.
1093 */
1094
1095 CALLOC_ARRAY(commits, tips_nr);
1096
1097 for (size_t i = 0; i < tips_nr; i++) {
1098 commits[i].commit = tips[i];
1099 commits[i].index = i;
1100 commits[i].generation = commit_graph_generation(tips[i]);
1101 }
1102
1103 /* Sort with generation number ascending. */
1104 QSORT(commits, tips_nr, compare_commit_and_index_by_generation);
1105 min_generation = commits[0].generation;
1106
1107 while (bases) {
1108 repo_parse_commit(r, bases->item);
1109 commit_list_insert(bases->item, &stack);
1110 bases = bases->next;
1111 }
1112
1113 while (stack) {
1114 int explored_all_parents = 1;
1115 struct commit_list *p;
1116 struct commit *c = stack->item;
1117 timestamp_t c_gen = commit_graph_generation(c);
1118
1119 /* Does it match any of our tips? */
1120 for (size_t j = min_generation_index; j < tips_nr; j++) {
1121 if (c_gen < commits[j].generation)
1122 break;
1123
1124 if (commits[j].commit == c) {
1125 tips[commits[j].index]->object.flags |= mark;
1126
1127 if (j == min_generation_index) {
1128 unsigned int k = j + 1;
1129 while (k < tips_nr &&
1130 (tips[commits[k].index]->object.flags & mark))
1131 k++;
1132
1133 /* Terminate early if all found. */
1134 if (k >= tips_nr)
1135 goto done;
1136
1137 min_generation_index = k;
1138 min_generation = commits[k].generation;
1139 }
1140 }
1141 }
1142
1143 for (p = c->parents; p; p = p->next) {
1144 repo_parse_commit(r, p->item);
1145
1146 /* Have we already explored this parent? */
1147 if (p->item->object.flags & SEEN)
1148 continue;
1149
1150 /* Is it below the current minimum generation? */
1151 if (commit_graph_generation(p->item) < min_generation)
1152 continue;
1153
1154 /* Ok, we will explore from here on. */
1155 p->item->object.flags |= SEEN;
1156 explored_all_parents = 0;
1157 commit_list_insert(p->item, &stack);
1158 break;
1159 }
1160
1161 if (explored_all_parents)
1162 pop_commit(&stack);
1163 }
1164
1165 done:
1166 free(commits);
1167 repo_clear_commit_marks(r, SEEN);
1168 }