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 commit *one, int n, struct commit **twos)
100 struct commit_list *list = NULL;
101 struct commit_list *result = NULL;
104 for (i = 0; i < n; i++) {
107 * We do not mark this even with RESULT so we do not
108 * have to clean it up.
110 return commit_list_insert(one, &result);
113 if (parse_commit(one))
115 for (i = 0; i < n; i++) {
116 if (parse_commit(twos[i]))
120 list = paint_down_to_common(the_repository, one, n, twos, 0);
123 struct commit *commit = pop_commit(&list);
124 if (!(commit->object.flags & STALE))
125 commit_list_insert_by_date(commit, &result);
130 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
132 struct commit_list *i, *j, *k, *ret = NULL;
137 commit_list_insert(in->item, &ret);
139 for (i = in->next; i; i = i->next) {
140 struct commit_list *new_commits = NULL, *end = NULL;
142 for (j = ret; j; j = j->next) {
143 struct commit_list *bases;
144 bases = get_merge_bases(i->item, j->item);
149 for (k = bases; k; k = k->next)
157 static int remove_redundant(struct commit **array, int cnt)
160 * Some commit in the array may be an ancestor of
161 * another commit. Move such commit to the end of
162 * the array, and return the number of commits that
163 * are independent from each other.
165 struct commit **work;
166 unsigned char *redundant;
170 work = xcalloc(cnt, sizeof(*work));
171 redundant = xcalloc(cnt, 1);
172 ALLOC_ARRAY(filled_index, cnt - 1);
174 for (i = 0; i < cnt; i++)
175 parse_commit(array[i]);
176 for (i = 0; i < cnt; i++) {
177 struct commit_list *common;
178 uint32_t min_generation = array[i]->generation;
182 for (j = filled = 0; j < cnt; j++) {
183 if (i == j || redundant[j])
185 filled_index[filled] = j;
186 work[filled++] = array[j];
188 if (array[j]->generation < min_generation)
189 min_generation = array[j]->generation;
191 common = paint_down_to_common(the_repository, array[i], filled,
192 work, min_generation);
193 if (array[i]->object.flags & PARENT2)
195 for (j = 0; j < filled; j++)
196 if (work[j]->object.flags & PARENT1)
197 redundant[filled_index[j]] = 1;
198 clear_commit_marks(array[i], all_flags);
199 clear_commit_marks_many(filled, work, all_flags);
200 free_commit_list(common);
203 /* Now collect the result */
204 COPY_ARRAY(work, array, cnt);
205 for (i = filled = 0; i < cnt; i++)
207 array[filled++] = work[i];
208 for (j = filled, i = 0; i < cnt; i++)
210 array[j++] = work[i];
217 static struct commit_list *get_merge_bases_many_0(struct commit *one,
219 struct commit **twos,
222 struct commit_list *list;
223 struct commit **rslt;
224 struct commit_list *result;
227 result = merge_bases_many(one, n, twos);
228 for (i = 0; i < n; i++) {
232 if (!result || !result->next) {
234 clear_commit_marks(one, all_flags);
235 clear_commit_marks_many(n, twos, all_flags);
240 /* There are more than one */
241 cnt = commit_list_count(result);
242 rslt = xcalloc(cnt, sizeof(*rslt));
243 for (list = result, i = 0; list; list = list->next)
244 rslt[i++] = list->item;
245 free_commit_list(result);
247 clear_commit_marks(one, all_flags);
248 clear_commit_marks_many(n, twos, all_flags);
250 cnt = remove_redundant(rslt, cnt);
252 for (i = 0; i < cnt; i++)
253 commit_list_insert_by_date(rslt[i], &result);
258 struct commit_list *get_merge_bases_many(struct commit *one,
260 struct commit **twos)
262 return get_merge_bases_many_0(one, n, twos, 1);
265 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
267 struct commit **twos)
269 return get_merge_bases_many_0(one, n, twos, 0);
272 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
274 return get_merge_bases_many_0(one, 1, &two, 1);
278 * Is "commit" a descendant of one of the elements on the "with_commit" list?
280 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
285 if (generation_numbers_enabled(the_repository)) {
286 struct commit_list *from_list = NULL;
288 commit_list_insert(commit, &from_list);
289 result = can_all_from_reach(from_list, with_commit, 0);
290 free_commit_list(from_list);
293 while (with_commit) {
294 struct commit *other;
296 other = with_commit->item;
297 with_commit = with_commit->next;
298 if (in_merge_bases(other, commit))
306 * Is "commit" an ancestor of one of the "references"?
308 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
310 struct commit_list *bases;
312 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
314 if (parse_commit(commit))
316 for (i = 0; i < nr_reference; i++) {
317 if (parse_commit(reference[i]))
319 if (reference[i]->generation < min_generation)
320 min_generation = reference[i]->generation;
323 if (commit->generation > min_generation)
326 bases = paint_down_to_common(the_repository, commit,
327 nr_reference, reference,
329 if (commit->object.flags & PARENT2)
331 clear_commit_marks(commit, all_flags);
332 clear_commit_marks_many(nr_reference, reference, all_flags);
333 free_commit_list(bases);
338 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
340 int in_merge_bases(struct commit *commit, struct commit *reference)
342 return in_merge_bases_many(commit, 1, &reference);
345 struct commit_list *reduce_heads(struct commit_list *heads)
347 struct commit_list *p;
348 struct commit_list *result = NULL, **tail = &result;
349 struct commit **array;
356 for (p = heads; p; p = p->next)
357 p->item->object.flags &= ~STALE;
358 for (p = heads, num_head = 0; p; p = p->next) {
359 if (p->item->object.flags & STALE)
361 p->item->object.flags |= STALE;
364 array = xcalloc(num_head, sizeof(*array));
365 for (p = heads, i = 0; p; p = p->next) {
366 if (p->item->object.flags & STALE) {
367 array[i++] = p->item;
368 p->item->object.flags &= ~STALE;
371 num_head = remove_redundant(array, num_head);
372 for (i = 0; i < num_head; i++)
373 tail = &commit_list_insert(array[i], tail)->next;
378 void reduce_heads_replace(struct commit_list **heads)
380 struct commit_list *result = reduce_heads(*heads);
381 free_commit_list(*heads);
385 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
388 struct commit *old_commit, *new_commit;
389 struct commit_list *old_commit_list = NULL;
392 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
393 * old_commit. Otherwise we require --force.
395 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
397 if (!o || o->type != OBJ_COMMIT)
399 old_commit = (struct commit *) o;
401 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
403 if (!o || o->type != OBJ_COMMIT)
405 new_commit = (struct commit *) o;
407 if (parse_commit(new_commit) < 0)
410 commit_list_insert(old_commit, &old_commit_list);
411 return is_descendant_of(new_commit, old_commit_list);
415 * Mimicking the real stack, this stack lives on the heap, avoiding stack
418 * At each recursion step, the stack items points to the commits whose
419 * ancestors are to be inspected.
421 struct contains_stack {
423 struct contains_stack_entry {
424 struct commit *commit;
425 struct commit_list *parents;
429 static int in_commit_list(const struct commit_list *want, struct commit *c)
431 for (; want; want = want->next)
432 if (oideq(&want->item->object.oid, &c->object.oid))
438 * Test whether the candidate is contained in the list.
439 * Do not recurse to find out, though, but return -1 if inconclusive.
441 static enum contains_result contains_test(struct commit *candidate,
442 const struct commit_list *want,
443 struct contains_cache *cache,
446 enum contains_result *cached = contains_cache_at(cache, candidate);
448 /* If we already have the answer cached, return that. */
453 if (in_commit_list(want, candidate)) {
454 *cached = CONTAINS_YES;
458 /* Otherwise, we don't know; prepare to recurse */
459 parse_commit_or_die(candidate);
461 if (candidate->generation < cutoff)
464 return CONTAINS_UNKNOWN;
467 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
469 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
470 contains_stack->contains_stack[contains_stack->nr].commit = candidate;
471 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
474 static enum contains_result contains_tag_algo(struct commit *candidate,
475 const struct commit_list *want,
476 struct contains_cache *cache)
478 struct contains_stack contains_stack = { 0, 0, NULL };
479 enum contains_result result;
480 uint32_t cutoff = GENERATION_NUMBER_INFINITY;
481 const struct commit_list *p;
483 for (p = want; p; p = p->next) {
484 struct commit *c = p->item;
485 load_commit_graph_info(the_repository, c);
486 if (c->generation < cutoff)
487 cutoff = c->generation;
490 result = contains_test(candidate, want, cache, cutoff);
491 if (result != CONTAINS_UNKNOWN)
494 push_to_contains_stack(candidate, &contains_stack);
495 while (contains_stack.nr) {
496 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
497 struct commit *commit = entry->commit;
498 struct commit_list *parents = entry->parents;
501 *contains_cache_at(cache, commit) = CONTAINS_NO;
505 * If we just popped the stack, parents->item has been marked,
506 * therefore contains_test will return a meaningful yes/no.
508 else switch (contains_test(parents->item, want, cache, cutoff)) {
510 *contains_cache_at(cache, commit) = CONTAINS_YES;
514 entry->parents = parents->next;
516 case CONTAINS_UNKNOWN:
517 push_to_contains_stack(parents->item, &contains_stack);
521 free(contains_stack.contains_stack);
522 return contains_test(candidate, want, cache, cutoff);
525 int commit_contains(struct ref_filter *filter, struct commit *commit,
526 struct commit_list *list, struct contains_cache *cache)
528 if (filter->with_commit_tag_algo)
529 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
530 return is_descendant_of(commit, list);
533 static int compare_commits_by_gen(const void *_a, const void *_b)
535 const struct commit *a = (const struct commit *)_a;
536 const struct commit *b = (const struct commit *)_b;
538 if (a->generation < b->generation)
540 if (a->generation > b->generation)
545 int can_all_from_reach_with_flag(struct object_array *from,
546 unsigned int with_flag,
547 unsigned int assign_flag,
548 time_t min_commit_date,
549 uint32_t min_generation)
551 struct commit **list = NULL;
556 ALLOC_ARRAY(list, from->nr);
558 for (i = 0; i < from->nr; i++) {
559 struct object *from_one = from->objects[i].item;
561 if (!from_one || from_one->flags & assign_flag)
564 from_one = deref_tag(the_repository, from_one,
566 if (!from_one || from_one->type != OBJ_COMMIT) {
568 * no way to tell if this is reachable by
569 * looking at the ancestry chain alone, so
570 * leave a note to ourselves not to worry about
571 * this object anymore.
573 from->objects[i].item->flags |= assign_flag;
577 list[nr_commits] = (struct commit *)from_one;
578 if (parse_commit(list[nr_commits]) ||
579 list[nr_commits]->generation < min_generation) {
587 QSORT(list, nr_commits, compare_commits_by_gen);
589 for (i = 0; i < nr_commits; i++) {
590 /* DFS from list[i] */
591 struct commit_list *stack = NULL;
593 list[i]->object.flags |= assign_flag;
594 commit_list_insert(list[i], &stack);
597 struct commit_list *parent;
599 if (stack->item->object.flags & with_flag) {
604 for (parent = stack->item->parents; parent; parent = parent->next) {
605 if (parent->item->object.flags & (with_flag | RESULT))
606 stack->item->object.flags |= RESULT;
608 if (!(parent->item->object.flags & assign_flag)) {
609 parent->item->object.flags |= assign_flag;
611 if (parse_commit(parent->item) ||
612 parent->item->date < min_commit_date ||
613 parent->item->generation < min_generation)
616 commit_list_insert(parent->item, &stack);
625 if (!(list[i]->object.flags & (with_flag | RESULT))) {
632 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
635 for (i = 0; i < from->nr; i++)
636 from->objects[i].item->flags &= ~assign_flag;
641 int can_all_from_reach(struct commit_list *from, struct commit_list *to,
642 int cutoff_by_min_date)
644 struct object_array from_objs = OBJECT_ARRAY_INIT;
645 time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
646 struct commit_list *from_iter = from, *to_iter = to;
648 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
651 add_object_array(&from_iter->item->object, NULL, &from_objs);
653 if (!parse_commit(from_iter->item)) {
654 if (from_iter->item->date < min_commit_date)
655 min_commit_date = from_iter->item->date;
657 if (from_iter->item->generation < min_generation)
658 min_generation = from_iter->item->generation;
661 from_iter = from_iter->next;
665 if (!parse_commit(to_iter->item)) {
666 if (to_iter->item->date < min_commit_date)
667 min_commit_date = to_iter->item->date;
669 if (to_iter->item->generation < min_generation)
670 min_generation = to_iter->item->generation;
673 to_iter->item->object.flags |= PARENT2;
675 to_iter = to_iter->next;
678 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
679 min_commit_date, min_generation);
682 clear_commit_marks(from->item, PARENT1);
687 clear_commit_marks(to->item, PARENT2);
691 object_array_clear(&from_objs);