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