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