]> git.ipfire.org Git - thirdparty/git.git/blob - commit-reach.c
commit-graph: minimize commit_graph_data_slab access
[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 uint32_t generation = commit_graph_generation(commit);
62
63 if (min_generation && generation > last_gen)
64 BUG("bad generation skip %8x > %8x at %s",
65 generation, last_gen,
66 oid_to_hex(&commit->object.oid));
67 last_gen = generation;
68
69 if (generation < min_generation)
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;
87 if (repo_parse_commit(r, p))
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
98 static struct commit_list *merge_bases_many(struct repository *r,
99 struct commit *one, int n,
100 struct commit **twos)
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
115 if (repo_parse_commit(r, one))
116 return NULL;
117 for (i = 0; i < n; i++) {
118 if (repo_parse_commit(r, twos[i]))
119 return NULL;
120 }
121
122 list = paint_down_to_common(r, one, n, twos, 0);
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
132 struct 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
159 static int remove_redundant(struct repository *r, struct commit **array, int cnt)
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++)
177 repo_parse_commit(r, array[i]);
178 for (i = 0; i < cnt; i++) {
179 struct commit_list *common;
180 uint32_t min_generation = commit_graph_generation(array[i]);
181
182 if (redundant[i])
183 continue;
184 for (j = filled = 0; j < cnt; j++) {
185 uint32_t curr_generation;
186 if (i == j || redundant[j])
187 continue;
188 filled_index[filled] = j;
189 work[filled++] = array[j];
190
191 curr_generation = commit_graph_generation(array[j]);
192 if (curr_generation < min_generation)
193 min_generation = curr_generation;
194 }
195 common = paint_down_to_common(r, array[i], filled,
196 work, min_generation);
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
221 static struct commit_list *get_merge_bases_many_0(struct repository *r,
222 struct commit *one,
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
232 result = merge_bases_many(r, one, n, twos);
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
255 cnt = remove_redundant(r, rslt, cnt);
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
263 struct commit_list *repo_get_merge_bases_many(struct repository *r,
264 struct commit *one,
265 int n,
266 struct commit **twos)
267 {
268 return get_merge_bases_many_0(r, one, n, twos, 1);
269 }
270
271 struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
272 struct commit *one,
273 int n,
274 struct commit **twos)
275 {
276 return get_merge_bases_many_0(r, one, n, twos, 0);
277 }
278
279 struct commit_list *repo_get_merge_bases(struct repository *r,
280 struct commit *one,
281 struct commit *two)
282 {
283 return get_merge_bases_many_0(r, one, 1, &two, 1);
284 }
285
286 /*
287 * Is "commit" a descendant of one of the elements on the "with_commit" list?
288 */
289 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
290 {
291 if (!with_commit)
292 return 1;
293
294 if (generation_numbers_enabled(the_repository)) {
295 struct commit_list *from_list = NULL;
296 int result;
297 commit_list_insert(commit, &from_list);
298 result = can_all_from_reach(from_list, with_commit, 0);
299 free_commit_list(from_list);
300 return result;
301 } else {
302 while (with_commit) {
303 struct commit *other;
304
305 other = with_commit->item;
306 with_commit = with_commit->next;
307 if (in_merge_bases(other, commit))
308 return 1;
309 }
310 return 0;
311 }
312 }
313
314 /*
315 * Is "commit" an ancestor of one of the "references"?
316 */
317 int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
318 int nr_reference, struct commit **reference)
319 {
320 struct commit_list *bases;
321 int ret = 0, i;
322 uint32_t generation, min_generation = GENERATION_NUMBER_INFINITY;
323
324 if (repo_parse_commit(r, commit))
325 return ret;
326 for (i = 0; i < nr_reference; i++) {
327 if (repo_parse_commit(r, reference[i]))
328 return ret;
329
330 generation = commit_graph_generation(reference[i]);
331 if (generation < min_generation)
332 min_generation = generation;
333 }
334
335 generation = commit_graph_generation(commit);
336 if (generation > min_generation)
337 return ret;
338
339 bases = paint_down_to_common(r, commit,
340 nr_reference, reference,
341 generation);
342 if (commit->object.flags & PARENT2)
343 ret = 1;
344 clear_commit_marks(commit, all_flags);
345 clear_commit_marks_many(nr_reference, reference, all_flags);
346 free_commit_list(bases);
347 return ret;
348 }
349
350 /*
351 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
352 */
353 int repo_in_merge_bases(struct repository *r,
354 struct commit *commit,
355 struct commit *reference)
356 {
357 return repo_in_merge_bases_many(r, commit, 1, &reference);
358 }
359
360 struct commit_list *reduce_heads(struct commit_list *heads)
361 {
362 struct commit_list *p;
363 struct commit_list *result = NULL, **tail = &result;
364 struct commit **array;
365 int num_head, i;
366
367 if (!heads)
368 return NULL;
369
370 /* Uniquify */
371 for (p = heads; p; p = p->next)
372 p->item->object.flags &= ~STALE;
373 for (p = heads, num_head = 0; p; p = p->next) {
374 if (p->item->object.flags & STALE)
375 continue;
376 p->item->object.flags |= STALE;
377 num_head++;
378 }
379 array = xcalloc(num_head, sizeof(*array));
380 for (p = heads, i = 0; p; p = p->next) {
381 if (p->item->object.flags & STALE) {
382 array[i++] = p->item;
383 p->item->object.flags &= ~STALE;
384 }
385 }
386 num_head = remove_redundant(the_repository, array, num_head);
387 for (i = 0; i < num_head; i++)
388 tail = &commit_list_insert(array[i], tail)->next;
389 free(array);
390 return result;
391 }
392
393 void reduce_heads_replace(struct commit_list **heads)
394 {
395 struct commit_list *result = reduce_heads(*heads);
396 free_commit_list(*heads);
397 *heads = result;
398 }
399
400 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
401 {
402 struct object *o;
403 struct commit *old_commit, *new_commit;
404 struct commit_list *old_commit_list = NULL;
405
406 /*
407 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
408 * old_commit. Otherwise we require --force.
409 */
410 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
411 NULL, 0);
412 if (!o || o->type != OBJ_COMMIT)
413 return 0;
414 old_commit = (struct commit *) o;
415
416 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
417 NULL, 0);
418 if (!o || o->type != OBJ_COMMIT)
419 return 0;
420 new_commit = (struct commit *) o;
421
422 if (parse_commit(new_commit) < 0)
423 return 0;
424
425 commit_list_insert(old_commit, &old_commit_list);
426 return is_descendant_of(new_commit, old_commit_list);
427 }
428
429 /*
430 * Mimicking the real stack, this stack lives on the heap, avoiding stack
431 * overflows.
432 *
433 * At each recursion step, the stack items points to the commits whose
434 * ancestors are to be inspected.
435 */
436 struct contains_stack {
437 int nr, alloc;
438 struct contains_stack_entry {
439 struct commit *commit;
440 struct commit_list *parents;
441 } *contains_stack;
442 };
443
444 static int in_commit_list(const struct commit_list *want, struct commit *c)
445 {
446 for (; want; want = want->next)
447 if (oideq(&want->item->object.oid, &c->object.oid))
448 return 1;
449 return 0;
450 }
451
452 /*
453 * Test whether the candidate is contained in the list.
454 * Do not recurse to find out, though, but return -1 if inconclusive.
455 */
456 static enum contains_result contains_test(struct commit *candidate,
457 const struct commit_list *want,
458 struct contains_cache *cache,
459 uint32_t cutoff)
460 {
461 enum contains_result *cached = contains_cache_at(cache, candidate);
462
463 /* If we already have the answer cached, return that. */
464 if (*cached)
465 return *cached;
466
467 /* or are we it? */
468 if (in_commit_list(want, candidate)) {
469 *cached = CONTAINS_YES;
470 return CONTAINS_YES;
471 }
472
473 /* Otherwise, we don't know; prepare to recurse */
474 parse_commit_or_die(candidate);
475
476 if (commit_graph_generation(candidate) < cutoff)
477 return CONTAINS_NO;
478
479 return CONTAINS_UNKNOWN;
480 }
481
482 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
483 {
484 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
485 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
486 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
487 }
488
489 static enum contains_result contains_tag_algo(struct commit *candidate,
490 const struct commit_list *want,
491 struct contains_cache *cache)
492 {
493 struct contains_stack contains_stack = { 0, 0, NULL };
494 enum contains_result result;
495 uint32_t cutoff = GENERATION_NUMBER_INFINITY;
496 const struct commit_list *p;
497
498 for (p = want; p; p = p->next) {
499 uint32_t generation;
500 struct commit *c = p->item;
501 load_commit_graph_info(the_repository, c);
502 generation = commit_graph_generation(c);
503 if (generation < cutoff)
504 cutoff = generation;
505 }
506
507 result = contains_test(candidate, want, cache, cutoff);
508 if (result != CONTAINS_UNKNOWN)
509 return result;
510
511 push_to_contains_stack(candidate, &contains_stack);
512 while (contains_stack.nr) {
513 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
514 struct commit *commit = entry->commit;
515 struct commit_list *parents = entry->parents;
516
517 if (!parents) {
518 *contains_cache_at(cache, commit) = CONTAINS_NO;
519 contains_stack.nr--;
520 }
521 /*
522 * If we just popped the stack, parents->item has been marked,
523 * therefore contains_test will return a meaningful yes/no.
524 */
525 else switch (contains_test(parents->item, want, cache, cutoff)) {
526 case CONTAINS_YES:
527 *contains_cache_at(cache, commit) = CONTAINS_YES;
528 contains_stack.nr--;
529 break;
530 case CONTAINS_NO:
531 entry->parents = parents->next;
532 break;
533 case CONTAINS_UNKNOWN:
534 push_to_contains_stack(parents->item, &contains_stack);
535 break;
536 }
537 }
538 free(contains_stack.contains_stack);
539 return contains_test(candidate, want, cache, cutoff);
540 }
541
542 int commit_contains(struct ref_filter *filter, struct commit *commit,
543 struct commit_list *list, struct contains_cache *cache)
544 {
545 if (filter->with_commit_tag_algo)
546 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
547 return is_descendant_of(commit, list);
548 }
549
550 static int compare_commits_by_gen(const void *_a, const void *_b)
551 {
552 const struct commit *a = *(const struct commit * const *)_a;
553 const struct commit *b = *(const struct commit * const *)_b;
554
555 uint32_t generation_a = commit_graph_generation(a);
556 uint32_t generation_b = commit_graph_generation(b);
557
558 if (generation_a < generation_b)
559 return -1;
560 if (generation_a > generation_b)
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 commit_graph_generation(list[nr_commits]) < 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 commit_graph_generation(parent->item) < 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 uint32_t generation;
677 if (from_iter->item->date < min_commit_date)
678 min_commit_date = from_iter->item->date;
679
680 generation = commit_graph_generation(from_iter->item);
681 if (generation < min_generation)
682 min_generation = generation;
683 }
684
685 from_iter = from_iter->next;
686 }
687
688 while (to_iter) {
689 if (!parse_commit(to_iter->item)) {
690 uint32_t generation;
691 if (to_iter->item->date < min_commit_date)
692 min_commit_date = to_iter->item->date;
693
694 generation = commit_graph_generation(to_iter->item);
695 if (generation < min_generation)
696 min_generation = generation;
697 }
698
699 to_iter->item->object.flags |= PARENT2;
700
701 to_iter = to_iter->next;
702 }
703
704 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
705 min_commit_date, min_generation);
706
707 while (from) {
708 clear_commit_marks(from->item, PARENT1);
709 from = from->next;
710 }
711
712 while (to) {
713 clear_commit_marks(to->item, PARENT2);
714 to = to->next;
715 }
716
717 object_array_clear(&from_objs);
718 return result;
719 }
720
721 struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
722 struct commit **to, int nr_to,
723 unsigned int reachable_flag)
724 {
725 struct commit **item;
726 struct commit *current;
727 struct commit_list *found_commits = NULL;
728 struct commit **to_last = to + nr_to;
729 struct commit **from_last = from + nr_from;
730 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
731 int num_to_find = 0;
732
733 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
734
735 for (item = to; item < to_last; item++) {
736 uint32_t generation;
737 struct commit *c = *item;
738
739 parse_commit(c);
740 generation = commit_graph_generation(c);
741 if (generation < min_generation)
742 min_generation = generation;
743
744 if (!(c->object.flags & PARENT1)) {
745 c->object.flags |= PARENT1;
746 num_to_find++;
747 }
748 }
749
750 for (item = from; item < from_last; item++) {
751 struct commit *c = *item;
752 if (!(c->object.flags & PARENT2)) {
753 c->object.flags |= PARENT2;
754 parse_commit(c);
755
756 prio_queue_put(&queue, *item);
757 }
758 }
759
760 while (num_to_find && (current = prio_queue_get(&queue)) != NULL) {
761 struct commit_list *parents;
762
763 if (current->object.flags & PARENT1) {
764 current->object.flags &= ~PARENT1;
765 current->object.flags |= reachable_flag;
766 commit_list_insert(current, &found_commits);
767 num_to_find--;
768 }
769
770 for (parents = current->parents; parents; parents = parents->next) {
771 struct commit *p = parents->item;
772
773 parse_commit(p);
774
775 if (commit_graph_generation(p) < min_generation)
776 continue;
777
778 if (p->object.flags & PARENT2)
779 continue;
780
781 p->object.flags |= PARENT2;
782 prio_queue_put(&queue, p);
783 }
784 }
785
786 clear_commit_marks_many(nr_to, to, PARENT1);
787 clear_commit_marks_many(nr_from, from, PARENT2);
788
789 return found_commits;
790 }