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