get_main_worktree(): allow it to be called in the Git directory
[git.git] / commit-reach.c
blob4ca7e706a18ecf3c6cf862966b9d2f81d02bc964
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"
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)
18 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
20 static int queue_has_nonstale(struct prio_queue *queue)
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;
28 return 0;
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)
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;
42 if (!min_generation)
43 queue.compare = compare_commits_by_commit_date;
45 one->object.flags |= PARENT1;
46 if (!n) {
47 commit_list_append(one, &result);
48 return result;
50 prio_queue_put(&queue, one);
52 for (i = 0; i < n; i++) {
53 twos[i]->object.flags |= PARENT2;
54 prio_queue_put(&queue, twos[i]);
57 while (queue_has_nonstale(&queue)) {
58 struct commit *commit = prio_queue_get(&queue);
59 struct commit_list *parents;
60 int flags;
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;
68 if (commit->generation < min_generation)
69 break;
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);
77 /* Mark parents of a found merge stale */
78 flags |= STALE;
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);
93 clear_prio_queue(&queue);
94 return result;
97 static struct commit_list *merge_bases_many(struct repository *r,
98 struct commit *one, int n,
99 struct commit **twos)
101 struct commit_list *list = NULL;
102 struct commit_list *result = NULL;
103 int i;
105 for (i = 0; i < n; i++) {
106 if (one == twos[i])
108 * We do not mark this even with RESULT so we do not
109 * have to clean it up.
111 return commit_list_insert(one, &result);
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;
121 list = paint_down_to_common(r, one, n, twos, 0);
123 while (list) {
124 struct commit *commit = pop_commit(&list);
125 if (!(commit->object.flags & STALE))
126 commit_list_insert_by_date(commit, &result);
128 return result;
131 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
133 struct commit_list *i, *j, *k, *ret = NULL;
135 if (!in)
136 return ret;
138 commit_list_insert(in->item, &ret);
140 for (i = in->next; i; i = i->next) {
141 struct commit_list *new_commits = NULL, *end = NULL;
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;
153 ret = new_commits;
155 return ret;
158 static int remove_redundant(struct repository *r, struct commit **array, int cnt)
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.
166 struct commit **work;
167 unsigned char *redundant;
168 int *filled_index;
169 int i, j, filled;
171 work = xcalloc(cnt, sizeof(*work));
172 redundant = xcalloc(cnt, 1);
173 ALLOC_ARRAY(filled_index, cnt - 1);
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;
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];
189 if (array[j]->generation < min_generation)
190 min_generation = array[j]->generation;
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);
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;
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)
224 struct commit_list *list;
225 struct commit **rslt;
226 struct commit_list *result;
227 int cnt, i;
229 result = merge_bases_many(r, one, n, twos);
230 for (i = 0; i < n; i++) {
231 if (one == twos[i])
232 return result;
234 if (!result || !result->next) {
235 if (cleanup) {
236 clear_commit_marks(one, all_flags);
237 clear_commit_marks_many(n, twos, all_flags);
239 return result;
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);
249 clear_commit_marks(one, all_flags);
250 clear_commit_marks_many(n, twos, all_flags);
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;
260 struct commit_list *repo_get_merge_bases_many(struct repository *r,
261 struct commit *one,
262 int n,
263 struct commit **twos)
265 return get_merge_bases_many_0(r, one, n, twos, 1);
268 struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
269 struct commit *one,
270 int n,
271 struct commit **twos)
273 return get_merge_bases_many_0(r, one, n, twos, 0);
276 struct commit_list *repo_get_merge_bases(struct repository *r,
277 struct commit *one,
278 struct commit *two)
280 return get_merge_bases_many_0(r, one, 1, &two, 1);
284 * Is "commit" a descendant of one of the elements on the "with_commit" list?
286 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
288 if (!with_commit)
289 return 1;
291 if (generation_numbers_enabled(the_repository)) {
292 struct commit_list *from_list = NULL;
293 int result;
294 commit_list_insert(commit, &from_list);
295 result = can_all_from_reach(from_list, with_commit, 0);
296 free_commit_list(from_list);
297 return result;
298 } else {
299 while (with_commit) {
300 struct commit *other;
302 other = with_commit->item;
303 with_commit = with_commit->next;
304 if (in_merge_bases(other, commit))
305 return 1;
307 return 0;
312 * Is "commit" an ancestor of one of the "references"?
314 int repo_in_merge_bases_many(struct repository *r, struct commit *commit,
315 int nr_reference, struct commit **reference)
317 struct commit_list *bases;
318 int ret = 0, i;
319 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
321 if (repo_parse_commit(r, commit))
322 return ret;
323 for (i = 0; i < nr_reference; i++) {
324 if (repo_parse_commit(r, reference[i]))
325 return ret;
326 if (reference[i]->generation < min_generation)
327 min_generation = reference[i]->generation;
330 if (commit->generation > min_generation)
331 return ret;
333 bases = paint_down_to_common(r, commit,
334 nr_reference, reference,
335 commit->generation);
336 if (commit->object.flags & PARENT2)
337 ret = 1;
338 clear_commit_marks(commit, all_flags);
339 clear_commit_marks_many(nr_reference, reference, all_flags);
340 free_commit_list(bases);
341 return ret;
345 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
347 int repo_in_merge_bases(struct repository *r,
348 struct commit *commit,
349 struct commit *reference)
351 return repo_in_merge_bases_many(r, commit, 1, &reference);
354 struct commit_list *reduce_heads(struct commit_list *heads)
356 struct commit_list *p;
357 struct commit_list *result = NULL, **tail = &result;
358 struct commit **array;
359 int num_head, i;
361 if (!heads)
362 return NULL;
364 /* Uniquify */
365 for (p = heads; p; p = p->next)
366 p->item->object.flags &= ~STALE;
367 for (p = heads, num_head = 0; p; p = p->next) {
368 if (p->item->object.flags & STALE)
369 continue;
370 p->item->object.flags |= STALE;
371 num_head++;
373 array = xcalloc(num_head, sizeof(*array));
374 for (p = heads, i = 0; p; p = p->next) {
375 if (p->item->object.flags & STALE) {
376 array[i++] = p->item;
377 p->item->object.flags &= ~STALE;
380 num_head = remove_redundant(the_repository, array, num_head);
381 for (i = 0; i < num_head; i++)
382 tail = &commit_list_insert(array[i], tail)->next;
383 free(array);
384 return result;
387 void reduce_heads_replace(struct commit_list **heads)
389 struct commit_list *result = reduce_heads(*heads);
390 free_commit_list(*heads);
391 *heads = result;
394 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
396 struct object *o;
397 struct commit *old_commit, *new_commit;
398 struct commit_list *old_commit_list = NULL;
401 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
402 * old_commit. Otherwise we require --force.
404 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
405 NULL, 0);
406 if (!o || o->type != OBJ_COMMIT)
407 return 0;
408 old_commit = (struct commit *) o;
410 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
411 NULL, 0);
412 if (!o || o->type != OBJ_COMMIT)
413 return 0;
414 new_commit = (struct commit *) o;
416 if (parse_commit(new_commit) < 0)
417 return 0;
419 commit_list_insert(old_commit, &old_commit_list);
420 return is_descendant_of(new_commit, old_commit_list);
424 * Mimicking the real stack, this stack lives on the heap, avoiding stack
425 * overflows.
427 * At each recursion step, the stack items points to the commits whose
428 * ancestors are to be inspected.
430 struct contains_stack {
431 int nr, alloc;
432 struct contains_stack_entry {
433 struct commit *commit;
434 struct commit_list *parents;
435 } *contains_stack;
438 static int in_commit_list(const struct commit_list *want, struct commit *c)
440 for (; want; want = want->next)
441 if (oideq(&want->item->object.oid, &c->object.oid))
442 return 1;
443 return 0;
447 * Test whether the candidate is contained in the list.
448 * Do not recurse to find out, though, but return -1 if inconclusive.
450 static enum contains_result contains_test(struct commit *candidate,
451 const struct commit_list *want,
452 struct contains_cache *cache,
453 uint32_t cutoff)
455 enum contains_result *cached = contains_cache_at(cache, candidate);
457 /* If we already have the answer cached, return that. */
458 if (*cached)
459 return *cached;
461 /* or are we it? */
462 if (in_commit_list(want, candidate)) {
463 *cached = CONTAINS_YES;
464 return CONTAINS_YES;
467 /* Otherwise, we don't know; prepare to recurse */
468 parse_commit_or_die(candidate);
470 if (candidate->generation < cutoff)
471 return CONTAINS_NO;
473 return CONTAINS_UNKNOWN;
476 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
478 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
479 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
480 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
483 static enum contains_result contains_tag_algo(struct commit *candidate,
484 const struct commit_list *want,
485 struct contains_cache *cache)
487 struct contains_stack contains_stack = { 0, 0, NULL };
488 enum contains_result result;
489 uint32_t cutoff = GENERATION_NUMBER_INFINITY;
490 const struct commit_list *p;
492 for (p = want; p; p = p->next) {
493 struct commit *c = p->item;
494 load_commit_graph_info(the_repository, c);
495 if (c->generation < cutoff)
496 cutoff = c->generation;
499 result = contains_test(candidate, want, cache, cutoff);
500 if (result != CONTAINS_UNKNOWN)
501 return result;
503 push_to_contains_stack(candidate, &contains_stack);
504 while (contains_stack.nr) {
505 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
506 struct commit *commit = entry->commit;
507 struct commit_list *parents = entry->parents;
509 if (!parents) {
510 *contains_cache_at(cache, commit) = CONTAINS_NO;
511 contains_stack.nr--;
514 * If we just popped the stack, parents->item has been marked,
515 * therefore contains_test will return a meaningful yes/no.
517 else switch (contains_test(parents->item, want, cache, cutoff)) {
518 case CONTAINS_YES:
519 *contains_cache_at(cache, commit) = CONTAINS_YES;
520 contains_stack.nr--;
521 break;
522 case CONTAINS_NO:
523 entry->parents = parents->next;
524 break;
525 case CONTAINS_UNKNOWN:
526 push_to_contains_stack(parents->item, &contains_stack);
527 break;
530 free(contains_stack.contains_stack);
531 return contains_test(candidate, want, cache, cutoff);
534 int commit_contains(struct ref_filter *filter, struct commit *commit,
535 struct commit_list *list, struct contains_cache *cache)
537 if (filter->with_commit_tag_algo)
538 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
539 return is_descendant_of(commit, list);
542 static int compare_commits_by_gen(const void *_a, const void *_b)
544 const struct commit *a = *(const struct commit * const *)_a;
545 const struct commit *b = *(const struct commit * const *)_b;
547 if (a->generation < b->generation)
548 return -1;
549 if (a->generation > b->generation)
550 return 1;
551 return 0;
554 int can_all_from_reach_with_flag(struct object_array *from,
555 unsigned int with_flag,
556 unsigned int assign_flag,
557 time_t min_commit_date,
558 uint32_t min_generation)
560 struct commit **list = NULL;
561 int i;
562 int nr_commits;
563 int result = 1;
565 ALLOC_ARRAY(list, from->nr);
566 nr_commits = 0;
567 for (i = 0; i < from->nr; i++) {
568 struct object *from_one = from->objects[i].item;
570 if (!from_one || from_one->flags & assign_flag)
571 continue;
573 from_one = deref_tag(the_repository, from_one,
574 "a from object", 0);
575 if (!from_one || from_one->type != OBJ_COMMIT) {
577 * no way to tell if this is reachable by
578 * looking at the ancestry chain alone, so
579 * leave a note to ourselves not to worry about
580 * this object anymore.
582 from->objects[i].item->flags |= assign_flag;
583 continue;
586 list[nr_commits] = (struct commit *)from_one;
587 if (parse_commit(list[nr_commits]) ||
588 list[nr_commits]->generation < min_generation) {
589 result = 0;
590 goto cleanup;
593 nr_commits++;
596 QSORT(list, nr_commits, compare_commits_by_gen);
598 for (i = 0; i < nr_commits; i++) {
599 /* DFS from list[i] */
600 struct commit_list *stack = NULL;
602 list[i]->object.flags |= assign_flag;
603 commit_list_insert(list[i], &stack);
605 while (stack) {
606 struct commit_list *parent;
608 if (stack->item->object.flags & (with_flag | RESULT)) {
609 pop_commit(&stack);
610 if (stack)
611 stack->item->object.flags |= RESULT;
612 continue;
615 for (parent = stack->item->parents; parent; parent = parent->next) {
616 if (parent->item->object.flags & (with_flag | RESULT))
617 stack->item->object.flags |= RESULT;
619 if (!(parent->item->object.flags & assign_flag)) {
620 parent->item->object.flags |= assign_flag;
622 if (parse_commit(parent->item) ||
623 parent->item->date < min_commit_date ||
624 parent->item->generation < min_generation)
625 continue;
627 commit_list_insert(parent->item, &stack);
628 break;
632 if (!parent)
633 pop_commit(&stack);
636 if (!(list[i]->object.flags & (with_flag | RESULT))) {
637 result = 0;
638 goto cleanup;
642 cleanup:
643 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
644 free(list);
646 for (i = 0; i < from->nr; i++)
647 from->objects[i].item->flags &= ~assign_flag;
649 return result;
652 int can_all_from_reach(struct commit_list *from, struct commit_list *to,
653 int cutoff_by_min_date)
655 struct object_array from_objs = OBJECT_ARRAY_INIT;
656 time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
657 struct commit_list *from_iter = from, *to_iter = to;
658 int result;
659 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
661 while (from_iter) {
662 add_object_array(&from_iter->item->object, NULL, &from_objs);
664 if (!parse_commit(from_iter->item)) {
665 if (from_iter->item->date < min_commit_date)
666 min_commit_date = from_iter->item->date;
668 if (from_iter->item->generation < min_generation)
669 min_generation = from_iter->item->generation;
672 from_iter = from_iter->next;
675 while (to_iter) {
676 if (!parse_commit(to_iter->item)) {
677 if (to_iter->item->date < min_commit_date)
678 min_commit_date = to_iter->item->date;
680 if (to_iter->item->generation < min_generation)
681 min_generation = to_iter->item->generation;
684 to_iter->item->object.flags |= PARENT2;
686 to_iter = to_iter->next;
689 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
690 min_commit_date, min_generation);
692 while (from) {
693 clear_commit_marks(from->item, PARENT1);
694 from = from->next;
697 while (to) {
698 clear_commit_marks(to->item, PARENT2);
699 to = to->next;
702 object_array_clear(&from_objs);
703 return result;
706 struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
707 struct commit **to, int nr_to,
708 unsigned int reachable_flag)
710 struct commit **item;
711 struct commit *current;
712 struct commit_list *found_commits = NULL;
713 struct commit **to_last = to + nr_to;
714 struct commit **from_last = from + nr_from;
715 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
716 int num_to_find = 0;
718 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
720 for (item = to; item < to_last; item++) {
721 struct commit *c = *item;
723 parse_commit(c);
724 if (c->generation < min_generation)
725 min_generation = c->generation;
727 if (!(c->object.flags & PARENT1)) {
728 c->object.flags |= PARENT1;
729 num_to_find++;
733 for (item = from; item < from_last; item++) {
734 struct commit *c = *item;
735 if (!(c->object.flags & PARENT2)) {
736 c->object.flags |= PARENT2;
737 parse_commit(c);
739 prio_queue_put(&queue, *item);
743 while (num_to_find && (current = prio_queue_get(&queue)) != NULL) {
744 struct commit_list *parents;
746 if (current->object.flags & PARENT1) {
747 current->object.flags &= ~PARENT1;
748 current->object.flags |= reachable_flag;
749 commit_list_insert(current, &found_commits);
750 num_to_find--;
753 for (parents = current->parents; parents; parents = parents->next) {
754 struct commit *p = parents->item;
756 parse_commit(p);
758 if (p->generation < min_generation)
759 continue;
761 if (p->object.flags & PARENT2)
762 continue;
764 p->object.flags |= PARENT2;
765 prio_queue_put(&queue, p);
769 clear_commit_marks_many(nr_to, to, PARENT1);
770 clear_commit_marks_many(nr_from, from, PARENT2);
772 return found_commits;