commit-reach.c: allow paint_down_to_common to handle any repo
[git/git.git] / commit-reach.c
CommitLineData
5227c385 1#include "cache.h"
5227c385 2#include "commit.h"
920f93ca 3#include "commit-graph.h"
1d614d41
DS
4#include "decorate.h"
5#include "prio-queue.h"
6#include "tree.h"
6621c838 7#include "ref-filter.h"
1d614d41
DS
8#include "revision.h"
9#include "tag.h"
5227c385
DS
10#include "commit-reach.h"
11
12/* Remember to update object flag allocation in object.h */
ba3ca1ed 13#define REACHABLE (1u<<15)
5227c385
DS
14#define PARENT1 (1u<<16)
15#define PARENT2 (1u<<17)
16#define STALE (1u<<18)
17#define RESULT (1u<<19)
18
19static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
20
21static int queue_has_nonstale(struct prio_queue *queue)
22{
23 int i;
24 for (i = 0; i < queue->nr; i++) {
25 struct commit *commit = queue->array[i].data;
26 if (!(commit->object.flags & STALE))
27 return 1;
28 }
29 return 0;
30}
31
32/* all input commits in one and twos[] must have been parsed! */
c383830a
SB
33static struct commit_list *paint_down_to_common(struct repository *r,
34 struct commit *one, int n,
5227c385
DS
35 struct commit **twos,
36 int min_generation)
37{
38 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
39 struct commit_list *result = NULL;
40 int i;
41 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
42
1b7a91da
JH
43 if (!min_generation)
44 queue.compare = compare_commits_by_commit_date;
45
5227c385
DS
46 one->object.flags |= PARENT1;
47 if (!n) {
48 commit_list_append(one, &result);
49 return result;
50 }
51 prio_queue_put(&queue, one);
52
53 for (i = 0; i < n; i++) {
54 twos[i]->object.flags |= PARENT2;
55 prio_queue_put(&queue, twos[i]);
56 }
57
58 while (queue_has_nonstale(&queue)) {
59 struct commit *commit = prio_queue_get(&queue);
60 struct commit_list *parents;
61 int flags;
62
1b7a91da 63 if (min_generation && commit->generation > last_gen)
5227c385
DS
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;
68
69 if (commit->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;
c383830a 87 if (repo_parse_commit(r, p))
5227c385
DS
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
98static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
99{
100 struct commit_list *list = NULL;
101 struct commit_list *result = NULL;
102 int i;
103
104 for (i = 0; i < n; i++) {
105 if (one == twos[i])
106 /*
107 * We do not mark this even with RESULT so we do not
108 * have to clean it up.
109 */
110 return commit_list_insert(one, &result);
111 }
112
113 if (parse_commit(one))
114 return NULL;
115 for (i = 0; i < n; i++) {
116 if (parse_commit(twos[i]))
117 return NULL;
118 }
119
c383830a 120 list = paint_down_to_common(the_repository, one, n, twos, 0);
5227c385
DS
121
122 while (list) {
123 struct commit *commit = pop_commit(&list);
124 if (!(commit->object.flags & STALE))
125 commit_list_insert_by_date(commit, &result);
126 }
127 return result;
128}
129
130struct commit_list *get_octopus_merge_bases(struct commit_list *in)
131{
132 struct commit_list *i, *j, *k, *ret = NULL;
133
134 if (!in)
135 return ret;
136
137 commit_list_insert(in->item, &ret);
138
139 for (i = in->next; i; i = i->next) {
140 struct commit_list *new_commits = NULL, *end = NULL;
141
142 for (j = ret; j; j = j->next) {
143 struct commit_list *bases;
144 bases = get_merge_bases(i->item, j->item);
145 if (!new_commits)
146 new_commits = bases;
147 else
148 end->next = bases;
149 for (k = bases; k; k = k->next)
150 end = k;
151 }
152 ret = new_commits;
153 }
154 return ret;
155}
156
157static int remove_redundant(struct commit **array, int cnt)
158{
159 /*
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.
164 */
165 struct commit **work;
166 unsigned char *redundant;
167 int *filled_index;
168 int i, j, filled;
169
170 work = xcalloc(cnt, sizeof(*work));
171 redundant = xcalloc(cnt, 1);
172 ALLOC_ARRAY(filled_index, cnt - 1);
173
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;
179
180 if (redundant[i])
181 continue;
182 for (j = filled = 0; j < cnt; j++) {
183 if (i == j || redundant[j])
184 continue;
185 filled_index[filled] = j;
186 work[filled++] = array[j];
187
188 if (array[j]->generation < min_generation)
189 min_generation = array[j]->generation;
190 }
c383830a
SB
191 common = paint_down_to_common(the_repository, array[i], filled,
192 work, min_generation);
5227c385
DS
193 if (array[i]->object.flags & PARENT2)
194 redundant[i] = 1;
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);
201 }
202
203 /* Now collect the result */
204 COPY_ARRAY(work, array, cnt);
205 for (i = filled = 0; i < cnt; i++)
206 if (!redundant[i])
207 array[filled++] = work[i];
208 for (j = filled, i = 0; i < cnt; i++)
209 if (redundant[i])
210 array[j++] = work[i];
211 free(work);
212 free(redundant);
213 free(filled_index);
214 return filled;
215}
216
217static struct commit_list *get_merge_bases_many_0(struct commit *one,
218 int n,
219 struct commit **twos,
220 int cleanup)
221{
222 struct commit_list *list;
223 struct commit **rslt;
224 struct commit_list *result;
225 int cnt, i;
226
227 result = merge_bases_many(one, n, twos);
228 for (i = 0; i < n; i++) {
229 if (one == twos[i])
230 return result;
231 }
232 if (!result || !result->next) {
233 if (cleanup) {
234 clear_commit_marks(one, all_flags);
235 clear_commit_marks_many(n, twos, all_flags);
236 }
237 return result;
238 }
239
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);
246
247 clear_commit_marks(one, all_flags);
248 clear_commit_marks_many(n, twos, all_flags);
249
250 cnt = remove_redundant(rslt, cnt);
251 result = NULL;
252 for (i = 0; i < cnt; i++)
253 commit_list_insert_by_date(rslt[i], &result);
254 free(rslt);
255 return result;
256}
257
258struct commit_list *get_merge_bases_many(struct commit *one,
259 int n,
260 struct commit **twos)
261{
262 return get_merge_bases_many_0(one, n, twos, 1);
263}
264
265struct commit_list *get_merge_bases_many_dirty(struct commit *one,
266 int n,
267 struct commit **twos)
268{
269 return get_merge_bases_many_0(one, n, twos, 0);
270}
271
272struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
273{
274 return get_merge_bases_many_0(one, 1, &two, 1);
275}
276
277/*
278 * Is "commit" a descendant of one of the elements on the "with_commit" list?
279 */
280int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
281{
282 if (!with_commit)
283 return 1;
5227c385 284
6cc01743
DS
285 if (generation_numbers_enabled(the_repository)) {
286 struct commit_list *from_list = NULL;
287 int result;
288 commit_list_insert(commit, &from_list);
289 result = can_all_from_reach(from_list, with_commit, 0);
290 free_commit_list(from_list);
291 return result;
292 } else {
293 while (with_commit) {
294 struct commit *other;
295
296 other = with_commit->item;
297 with_commit = with_commit->next;
298 if (in_merge_bases(other, commit))
299 return 1;
300 }
301 return 0;
5227c385 302 }
5227c385
DS
303}
304
305/*
306 * Is "commit" an ancestor of one of the "references"?
307 */
308int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
309{
310 struct commit_list *bases;
311 int ret = 0, i;
312 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
313
314 if (parse_commit(commit))
315 return ret;
316 for (i = 0; i < nr_reference; i++) {
317 if (parse_commit(reference[i]))
318 return ret;
319 if (reference[i]->generation < min_generation)
320 min_generation = reference[i]->generation;
321 }
322
323 if (commit->generation > min_generation)
324 return ret;
325
c383830a
SB
326 bases = paint_down_to_common(the_repository, commit,
327 nr_reference, reference,
328 commit->generation);
5227c385
DS
329 if (commit->object.flags & PARENT2)
330 ret = 1;
331 clear_commit_marks(commit, all_flags);
332 clear_commit_marks_many(nr_reference, reference, all_flags);
333 free_commit_list(bases);
334 return ret;
335}
336
337/*
338 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
339 */
340int in_merge_bases(struct commit *commit, struct commit *reference)
341{
342 return in_merge_bases_many(commit, 1, &reference);
343}
344
345struct commit_list *reduce_heads(struct commit_list *heads)
346{
347 struct commit_list *p;
348 struct commit_list *result = NULL, **tail = &result;
349 struct commit **array;
350 int num_head, i;
351
352 if (!heads)
353 return NULL;
354
355 /* Uniquify */
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)
360 continue;
361 p->item->object.flags |= STALE;
362 num_head++;
363 }
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;
369 }
370 }
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;
374 free(array);
375 return result;
376}
377
378void reduce_heads_replace(struct commit_list **heads)
379{
380 struct commit_list *result = reduce_heads(*heads);
381 free_commit_list(*heads);
382 *heads = result;
383}
1d614d41 384
1d614d41
DS
385int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
386{
387 struct object *o;
388 struct commit *old_commit, *new_commit;
1e3497a2 389 struct commit_list *old_commit_list = NULL;
1d614d41
DS
390
391 /*
392 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
393 * old_commit. Otherwise we require --force.
394 */
395 o = deref_tag(the_repository, parse_object(the_repository, old_oid),
396 NULL, 0);
397 if (!o || o->type != OBJ_COMMIT)
398 return 0;
399 old_commit = (struct commit *) o;
400
401 o = deref_tag(the_repository, parse_object(the_repository, new_oid),
402 NULL, 0);
403 if (!o || o->type != OBJ_COMMIT)
404 return 0;
405 new_commit = (struct commit *) o;
406
407 if (parse_commit(new_commit) < 0)
408 return 0;
409
1e3497a2
DS
410 commit_list_insert(old_commit, &old_commit_list);
411 return is_descendant_of(new_commit, old_commit_list);
1d614d41 412}
920f93ca
DS
413
414/*
415 * Mimicking the real stack, this stack lives on the heap, avoiding stack
416 * overflows.
417 *
418 * At each recursion step, the stack items points to the commits whose
419 * ancestors are to be inspected.
420 */
421struct contains_stack {
422 int nr, alloc;
423 struct contains_stack_entry {
424 struct commit *commit;
425 struct commit_list *parents;
426 } *contains_stack;
427};
428
429static int in_commit_list(const struct commit_list *want, struct commit *c)
430{
431 for (; want; want = want->next)
e43d2dcc 432 if (oideq(&want->item->object.oid, &c->object.oid))
920f93ca
DS
433 return 1;
434 return 0;
435}
436
437/*
438 * Test whether the candidate is contained in the list.
439 * Do not recurse to find out, though, but return -1 if inconclusive.
440 */
441static enum contains_result contains_test(struct commit *candidate,
442 const struct commit_list *want,
443 struct contains_cache *cache,
444 uint32_t cutoff)
445{
446 enum contains_result *cached = contains_cache_at(cache, candidate);
447
448 /* If we already have the answer cached, return that. */
449 if (*cached)
450 return *cached;
451
452 /* or are we it? */
453 if (in_commit_list(want, candidate)) {
454 *cached = CONTAINS_YES;
455 return CONTAINS_YES;
456 }
457
458 /* Otherwise, we don't know; prepare to recurse */
459 parse_commit_or_die(candidate);
460
461 if (candidate->generation < cutoff)
462 return CONTAINS_NO;
463
464 return CONTAINS_UNKNOWN;
465}
466
467static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
468{
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;
472}
473
474static enum contains_result contains_tag_algo(struct commit *candidate,
475 const struct commit_list *want,
476 struct contains_cache *cache)
477{
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;
482
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;
488 }
489
490 result = contains_test(candidate, want, cache, cutoff);
491 if (result != CONTAINS_UNKNOWN)
492 return result;
493
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;
499
500 if (!parents) {
501 *contains_cache_at(cache, commit) = CONTAINS_NO;
502 contains_stack.nr--;
503 }
504 /*
505 * If we just popped the stack, parents->item has been marked,
506 * therefore contains_test will return a meaningful yes/no.
507 */
508 else switch (contains_test(parents->item, want, cache, cutoff)) {
509 case CONTAINS_YES:
510 *contains_cache_at(cache, commit) = CONTAINS_YES;
511 contains_stack.nr--;
512 break;
513 case CONTAINS_NO:
514 entry->parents = parents->next;
515 break;
516 case CONTAINS_UNKNOWN:
517 push_to_contains_stack(parents->item, &contains_stack);
518 break;
519 }
520 }
521 free(contains_stack.contains_stack);
522 return contains_test(candidate, want, cache, cutoff);
523}
524
525int commit_contains(struct ref_filter *filter, struct commit *commit,
526 struct commit_list *list, struct contains_cache *cache)
527{
528 if (filter->with_commit_tag_algo)
529 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
530 return is_descendant_of(commit, list);
531}
ba3ca1ed 532
4fbcca4e 533static int compare_commits_by_gen(const void *_a, const void *_b)
ba3ca1ed 534{
4fbcca4e
DS
535 const struct commit *a = (const struct commit *)_a;
536 const struct commit *b = (const struct commit *)_b;
ba3ca1ed 537
4fbcca4e
DS
538 if (a->generation < b->generation)
539 return -1;
540 if (a->generation > b->generation)
541 return 1;
542 return 0;
ba3ca1ed
DS
543}
544
545int can_all_from_reach_with_flag(struct object_array *from,
546 unsigned int with_flag,
547 unsigned int assign_flag,
4fbcca4e
DS
548 time_t min_commit_date,
549 uint32_t min_generation)
ba3ca1ed 550{
4fbcca4e 551 struct commit **list = NULL;
ba3ca1ed 552 int i;
b67f6b26 553 int nr_commits;
4fbcca4e 554 int result = 1;
ba3ca1ed 555
4fbcca4e 556 ALLOC_ARRAY(list, from->nr);
b67f6b26 557 nr_commits = 0;
ba3ca1ed 558 for (i = 0; i < from->nr; i++) {
b67f6b26 559 struct object *from_one = from->objects[i].item;
ba3ca1ed 560
b67f6b26
DS
561 if (!from_one || from_one->flags & assign_flag)
562 continue;
563
564 from_one = deref_tag(the_repository, from_one,
565 "a from object", 0);
566 if (!from_one || from_one->type != OBJ_COMMIT) {
85806440
DS
567 /*
568 * no way to tell if this is reachable by
b67f6b26
DS
569 * looking at the ancestry chain alone, so
570 * leave a note to ourselves not to worry about
571 * this object anymore.
572 */
573 from->objects[i].item->flags |= assign_flag;
574 continue;
575 }
576
577 list[nr_commits] = (struct commit *)from_one;
578 if (parse_commit(list[nr_commits]) ||
579 list[nr_commits]->generation < min_generation) {
580 result = 0;
581 goto cleanup;
582 }
583
584 nr_commits++;
ba3ca1ed 585 }
4fbcca4e 586
b67f6b26 587 QSORT(list, nr_commits, compare_commits_by_gen);
4fbcca4e 588
b67f6b26 589 for (i = 0; i < nr_commits; i++) {
4fbcca4e
DS
590 /* DFS from list[i] */
591 struct commit_list *stack = NULL;
592
593 list[i]->object.flags |= assign_flag;
594 commit_list_insert(list[i], &stack);
595
596 while (stack) {
597 struct commit_list *parent;
598
599 if (stack->item->object.flags & with_flag) {
600 pop_commit(&stack);
601 continue;
602 }
603
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;
607
608 if (!(parent->item->object.flags & assign_flag)) {
609 parent->item->object.flags |= assign_flag;
610
611 if (parse_commit(parent->item) ||
612 parent->item->date < min_commit_date ||
613 parent->item->generation < min_generation)
614 continue;
615
616 commit_list_insert(parent->item, &stack);
617 break;
618 }
619 }
620
621 if (!parent)
622 pop_commit(&stack);
623 }
624
625 if (!(list[i]->object.flags & (with_flag | RESULT))) {
626 result = 0;
627 goto cleanup;
628 }
629 }
630
631cleanup:
85806440 632 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
4067a646
DS
633 free(list);
634
635 for (i = 0; i < from->nr; i++)
636 from->objects[i].item->flags &= ~assign_flag;
637
4fbcca4e 638 return result;
ba3ca1ed 639}
1792bc12
DS
640
641int can_all_from_reach(struct commit_list *from, struct commit_list *to,
642 int cutoff_by_min_date)
643{
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;
647 int result;
4fbcca4e 648 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
1792bc12
DS
649
650 while (from_iter) {
651 add_object_array(&from_iter->item->object, NULL, &from_objs);
652
653 if (!parse_commit(from_iter->item)) {
654 if (from_iter->item->date < min_commit_date)
655 min_commit_date = from_iter->item->date;
4fbcca4e
DS
656
657 if (from_iter->item->generation < min_generation)
658 min_generation = from_iter->item->generation;
1792bc12
DS
659 }
660
661 from_iter = from_iter->next;
662 }
663
664 while (to_iter) {
665 if (!parse_commit(to_iter->item)) {
666 if (to_iter->item->date < min_commit_date)
667 min_commit_date = to_iter->item->date;
4fbcca4e
DS
668
669 if (to_iter->item->generation < min_generation)
670 min_generation = to_iter->item->generation;
1792bc12
DS
671 }
672
673 to_iter->item->object.flags |= PARENT2;
674
675 to_iter = to_iter->next;
676 }
677
678 result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
4fbcca4e 679 min_commit_date, min_generation);
1792bc12
DS
680
681 while (from) {
682 clear_commit_marks(from->item, PARENT1);
683 from = from->next;
684 }
685
686 while (to) {
687 clear_commit_marks(to->item, PARENT2);
688 to = to->next;
689 }
690
691 object_array_clear(&from_objs);
692 return result;
693}