3 #include "commit-graph.h"
5 #include "prio-queue.h"
7 #include "ref-filter.h"
10 #include "commit-reach.h"
12 /* Remember to update object flag allocation in object.h */
13 #define REACHABLE (1u<<15)
14 #define PARENT1 (1u<<16)
15 #define PARENT2 (1u<<17)
16 #define STALE (1u<<18)
17 #define RESULT (1u<<19)
19 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
21 static int queue_has_nonstale(struct prio_queue *queue)
24 for (i = 0; i < queue->nr; i++) {
25 struct commit *commit = queue->array[i].data;
26 if (!(commit->object.flags & STALE))
32 /* all input commits in one and twos[] must have been parsed! */
33 static struct commit_list *paint_down_to_common(struct repository *r,
34 struct commit *one, int n,
38 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
39 struct commit_list *result = NULL;
41 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
44 queue.compare = compare_commits_by_commit_date;
46 one->object.flags |= PARENT1;
48 commit_list_append(one, &result);
51 prio_queue_put(&queue, one);
53 for (i = 0; i < n; i++) {
54 twos[i]->object.flags |= PARENT2;
55 prio_queue_put(&queue, twos[i]);
58 while (queue_has_nonstale(&queue)) {
59 struct commit *commit = prio_queue_get(&queue);
60 struct commit_list *parents;
63 if (min_generation && commit->generation > last_gen)
64 BUG("bad generation skip %8x > %8x at %s",
65 commit->generation, last_gen,
66 oid_to_hex(&commit->object.oid));
67 last_gen = commit->generation;
69 if (commit->generation < min_generation)
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);
78 /* Mark parents of a found merge stale */
81 parents = commit->parents;
83 struct commit *p = parents->item;
84 parents = parents->next;
85 if ((p->object.flags & flags) == flags)
87 if (repo_parse_commit(r, p))
89 p->object.flags |= flags;
90 prio_queue_put(&queue, p);
94 clear_prio_queue(&queue);
98 static struct commit_list *merge_bases_many(struct repository *r,
99 struct commit *one, int n,
100 struct commit **twos)
102 struct commit_list *list = NULL;
103 struct commit_list *result = NULL;
106 for (i = 0; i < n; i++) {
109 * We do not mark this even with RESULT so we do not
110 * have to clean it up.
112 return commit_list_insert(one, &result);
115 if (repo_parse_commit(r, one))
117 for (i = 0; i < n; i++) {
118 if (repo_parse_commit(r, twos[i]))
122 list = paint_down_to_common(r, one, n, twos, 0);
125 struct commit *commit = pop_commit(&list);
126 if (!(commit->object.flags & STALE))
127 commit_list_insert_by_date(commit, &result);
132 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
134 struct commit_list *i, *j, *k, *ret = NULL;
139 commit_list_insert(in->item, &ret);
141 for (i = in->next; i; i = i->next) {
142 struct commit_list *new_commits = NULL, *end = NULL;
144 for (j = ret; j; j = j->next) {
145 struct commit_list *bases;
146 bases = get_merge_bases(i->item, j->item);
151 for (k = bases; k; k = k->next)
159 static int remove_redundant(struct repository *r, struct commit **array, int cnt)
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.
167 struct commit **work;
168 unsigned char *redundant;
172 work = xcalloc(cnt, sizeof(*work));
173 redundant = xcalloc(cnt, 1);
174 ALLOC_ARRAY(filled_index, cnt - 1);
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 = array[i]->generation;
184 for (j = filled = 0; j < cnt; j++) {
185 if (i == j || redundant[j])
187 filled_index[filled] = j;
188 work[filled++] = array[j];
190 if (array[j]->generation < min_generation)
191 min_generation = array[j]->generation;
193 common = paint_down_to_common(r, array[i], filled,
194 work, min_generation);
195 if (array[i]->object.flags & PARENT2)
197 for (j = 0; j < filled; j++)
198 if (work[j]->object.flags & PARENT1)
199 redundant[filled_index[j]] = 1;
200 clear_commit_marks(array[i], all_flags);
201 clear_commit_marks_many(filled, work, all_flags);
202 free_commit_list(common);
205 /* Now collect the result */
206 COPY_ARRAY(work, array, cnt);
207 for (i = filled = 0; i < cnt; i++)
209 array[filled++] = work[i];
210 for (j = filled, i = 0; i < cnt; i++)
212 array[j++] = work[i];
219 static struct commit_list *get_merge_bases_many_0(struct repository *r,
222 struct commit **twos,
225 struct commit_list *list;
226 struct commit **rslt;
227 struct commit_list *result;
230 result = merge_bases_many(r, one, n, twos);
231 for (i = 0; i < n; i++) {
235 if (!result || !result->next) {
237 clear_commit_marks(one, all_flags);
238 clear_commit_marks_many(n, twos, all_flags);
243 /* There are more than one */
244 cnt = commit_list_count(result);
245 rslt = xcalloc(cnt, sizeof(*rslt));
246 for (list = result, i = 0; list; list = list->next)
247 rslt[i++] = list->item;
248 free_commit_list(result);
250 clear_commit_marks(one, all_flags);
251 clear_commit_marks_many(n, twos, all_flags);
253 cnt = remove_redundant(r, rslt, cnt);
255 for (i = 0; i < cnt; i++)
256 commit_list_insert_by_date(rslt[i], &result);
261 struct commit_list *repo_get_merge_bases_many(struct repository *r,
264 struct commit **twos)
266 return get_merge_bases_many_0(r, one, n, twos, 1);
269 struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
272 struct commit **twos)
274 return get_merge_bases_many_0(r, one, n, twos, 0);
277 struct commit_list *repo_get_merge_bases(struct repository *r,
281 return get_merge_bases_many_0(r, one, 1, &two, 1);
285 * Is "commit" a descendant of one of the elements on the "with_commit" list?
287 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
292 if (generation_numbers_enabled(the_repository)) {
293 struct commit_list *from_list = NULL;
295 commit_list_insert(commit, &from_list);
296 result = can_all_from_reach(from_list, with_commit, 0);
297 free_commit_list(from_list);
300 while (with_commit) {
301 struct commit *other;
303 other = with_commit->item;
304 with_commit = with_commit->next;
305 if (in_merge_bases(other, commit))
313 * Is "commit" an ancestor of one of the "references"?
315 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
317 struct commit_list *bases;
319 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
321 if (parse_commit(commit))
323 for (i = 0; i < nr_reference; i++) {
324 if (parse_commit(reference[i]))
326 if (reference[i]->generation < min_generation)
327 min_generation = reference[i]->generation;
330 if (commit->generation > min_generation)
333 bases = paint_down_to_common(the_repository, commit,
334 nr_reference, reference,
336 if (commit->object.flags & PARENT2)
338 clear_commit_marks(commit, all_flags);
339 clear_commit_marks_many(nr_reference, reference, all_flags);
340 free_commit_list(bases);
345 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
347 int in_merge_bases(struct commit *commit, struct commit *reference)
349 return in_merge_bases_many(commit, 1, &reference);
352 struct commit_list *reduce_heads(struct commit_list *heads)
354 struct commit_list *p;
355 struct commit_list *result = NULL, **tail = &result;
356 struct commit **array;
363 for (p = heads; p; p = p->next)
364 p->item->object.flags &= ~STALE;
365 for (p = heads, num_head = 0; p; p = p->next) {
366 if (p->item->object.flags & STALE)
368 p->item->object.flags |= STALE;
371 array = xcalloc(num_head, sizeof(*array));
372 for (p = heads, i = 0; p; p = p->next) {
373 if (p->item->object.flags & STALE) {
374 array[i++] = p->item;
375 p->item->object.flags &= ~STALE;
378 num_head = remove_redundant(the_repository, array, num_head);
379 for (i = 0; i < num_head; i++)
380 tail = &commit_list_insert(array[i], tail)->next;
385 void reduce_heads_replace(struct commit_list **heads)
387 struct commit_list *result = reduce_heads(*heads);
388 free_commit_list(*heads);
392 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
395 struct commit *old_commit, *new_commit;
396 struct commit_list *old_commit_list = NULL;
399 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
400 * old_commit. Otherwise we require --force.
402 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
404 if (!o || o->type != OBJ_COMMIT)
406 old_commit = (struct commit *) o;
408 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
410 if (!o || o->type != OBJ_COMMIT)
412 new_commit = (struct commit *) o;
414 if (parse_commit(new_commit) < 0)
417 commit_list_insert(old_commit, &old_commit_list);
418 return is_descendant_of(new_commit, old_commit_list);
422 * Mimicking the real stack, this stack lives on the heap, avoiding stack
425 * At each recursion step, the stack items points to the commits whose
426 * ancestors are to be inspected.
428 struct contains_stack {
430 struct contains_stack_entry {
431 struct commit *commit;
432 struct commit_list *parents;
436 static int in_commit_list(const struct commit_list *want, struct commit *c)
438 for (; want; want = want->next)
439 if (oideq(&want->item->object.oid, &c->object.oid))
445 * Test whether the candidate is contained in the list.
446 * Do not recurse to find out, though, but return -1 if inconclusive.
448 static enum contains_result contains_test(struct commit *candidate,
449 const struct commit_list *want,
450 struct contains_cache *cache,
453 enum contains_result *cached = contains_cache_at(cache, candidate);
455 /* If we already have the answer cached, return that. */
460 if (in_commit_list(want, candidate)) {
461 *cached = CONTAINS_YES;
465 /* Otherwise, we don't know; prepare to recurse */
466 parse_commit_or_die(candidate);
468 if (candidate->generation < cutoff)
471 return CONTAINS_UNKNOWN;
474 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
476 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
477 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
478 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
481 static enum contains_result contains_tag_algo(struct commit *candidate,
482 const struct commit_list *want,
483 struct contains_cache *cache)
485 struct contains_stack contains_stack = { 0, 0, NULL };
486 enum contains_result result;
487 uint32_t cutoff = GENERATION_NUMBER_INFINITY;
488 const struct commit_list *p;
490 for (p = want; p; p = p->next) {
491 struct commit *c = p->item;
492 load_commit_graph_info(the_repository, c);
493 if (c->generation < cutoff)
494 cutoff = c->generation;
497 result = contains_test(candidate, want, cache, cutoff);
498 if (result != CONTAINS_UNKNOWN)
501 push_to_contains_stack(candidate, &contains_stack);
502 while (contains_stack.nr) {
503 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
504 struct commit *commit = entry->commit;
505 struct commit_list *parents = entry->parents;
508 *contains_cache_at(cache, commit) = CONTAINS_NO;
512 * If we just popped the stack, parents->item has been marked,
513 * therefore contains_test will return a meaningful yes/no.
515 else switch (contains_test(parents->item, want, cache, cutoff)) {
517 *contains_cache_at(cache, commit) = CONTAINS_YES;
521 entry->parents = parents->next;
523 case CONTAINS_UNKNOWN:
524 push_to_contains_stack(parents->item, &contains_stack);
528 free(contains_stack.contains_stack);
529 return contains_test(candidate, want, cache, cutoff);
532 int commit_contains(struct ref_filter *filter, struct commit *commit,
533 struct commit_list *list, struct contains_cache *cache)
535 if (filter->with_commit_tag_algo)
536 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
537 return is_descendant_of(commit, list);
540 static int compare_commits_by_gen(const void *_a, const void *_b)
542 const struct commit *a = (const struct commit *)_a;
543 const struct commit *b = (const struct commit *)_b;
545 if (a->generation < b->generation)
547 if (a->generation > b->generation)
552 int can_all_from_reach_with_flag(struct object_array *from,
553 unsigned int with_flag,
554 unsigned int assign_flag,
555 time_t min_commit_date,
556 uint32_t min_generation)
558 struct commit **list = NULL;
563 ALLOC_ARRAY(list, from->nr);
565 for (i = 0; i < from->nr; i++) {
566 struct object *from_one = from->objects[i].item;
568 if (!from_one || from_one->flags & assign_flag)
571 from_one = deref_tag(the_repository, from_one,
573 if (!from_one || from_one->type != OBJ_COMMIT) {
575 * no way to tell if this is reachable by
576 * looking at the ancestry chain alone, so
577 * leave a note to ourselves not to worry about
578 * this object anymore.
580 from->objects[i].item->flags |= assign_flag;
584 list[nr_commits] = (struct commit *)from_one;
585 if (parse_commit(list[nr_commits]) ||
586 list[nr_commits]->generation < min_generation) {
594 QSORT(list, nr_commits, compare_commits_by_gen);
596 for (i = 0; i < nr_commits; i++) {
597 /* DFS from list[i] */
598 struct commit_list *stack = NULL;
600 list[i]->object.flags |= assign_flag;
601 commit_list_insert(list[i], &stack);
604 struct commit_list *parent;
606 if (stack->item->object.flags & with_flag) {
611 for (parent = stack->item->parents; parent; parent = parent->next) {
612 if (parent->item->object.flags & (with_flag | RESULT))
613 stack->item->object.flags |= RESULT;
615 if (!(parent->item->object.flags & assign_flag)) {
616 parent->item->object.flags |= assign_flag;
618 if (parse_commit(parent->item) ||
619 parent->item->date < min_commit_date ||
620 parent->item->generation < min_generation)
623 commit_list_insert(parent->item, &stack);
632 if (!(list[i]->object.flags & (with_flag | RESULT))) {
639 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
642 for (i = 0; i < from->nr; i++)
643 from->objects[i].item->flags &= ~assign_flag;
648 int can_all_from_reach(struct commit_list *from, struct commit_list *to,
649 int cutoff_by_min_date)
651 struct object_array from_objs = OBJECT_ARRAY_INIT;
652 time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
653 struct commit_list *from_iter = from, *to_iter = to;
655 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
658 add_object_array(&from_iter->item->object, NULL, &from_objs);
660 if (!parse_commit(from_iter->item)) {
661 if (from_iter->item->date < min_commit_date)
662 min_commit_date = from_iter->item->date;
664 if (from_iter->item->generation < min_generation)
665 min_generation = from_iter->item->generation;
668 from_iter = from_iter->next;
672 if (!parse_commit(to_iter->item)) {
673 if (to_iter->item->date < min_commit_date)
674 min_commit_date = to_iter->item->date;
676 if (to_iter->item->generation < min_generation)
677 min_generation = to_iter->item->generation;
680 to_iter->item->object.flags |= PARENT2;
682 to_iter = to_iter->next;
685 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
686 min_commit_date, min_generation);
689 clear_commit_marks(from->item, PARENT1);
694 clear_commit_marks(to->item, PARENT2);
698 object_array_clear(&from_objs);