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