commit-reach: replace ref_newer logic
[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.c"
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 commit *one, int n,
34                                                 struct commit **twos,
35                                                 int min_generation)
36 {
37         struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
38         struct commit_list *result = NULL;
39         int i;
40         uint32_t last_gen = GENERATION_NUMBER_INFINITY;
41
42         one->object.flags |= PARENT1;
43         if (!n) {
44                 commit_list_append(one, &result);
45                 return result;
46         }
47         prio_queue_put(&queue, one);
48
49         for (i = 0; i < n; i++) {
50                 twos[i]->object.flags |= PARENT2;
51                 prio_queue_put(&queue, twos[i]);
52         }
53
54         while (queue_has_nonstale(&queue)) {
55                 struct commit *commit = prio_queue_get(&queue);
56                 struct commit_list *parents;
57                 int flags;
58
59                 if (commit->generation > last_gen)
60                         BUG("bad generation skip %8x > %8x at %s",
61                             commit->generation, last_gen,
62                             oid_to_hex(&commit->object.oid));
63                 last_gen = commit->generation;
64
65                 if (commit->generation < min_generation)
66                         break;
67
68                 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
69                 if (flags == (PARENT1 | PARENT2)) {
70                         if (!(commit->object.flags & RESULT)) {
71                                 commit->object.flags |= RESULT;
72                                 commit_list_insert_by_date(commit, &result);
73                         }
74                         /* Mark parents of a found merge stale */
75                         flags |= STALE;
76                 }
77                 parents = commit->parents;
78                 while (parents) {
79                         struct commit *p = parents->item;
80                         parents = parents->next;
81                         if ((p->object.flags & flags) == flags)
82                                 continue;
83                         if (parse_commit(p))
84                                 return NULL;
85                         p->object.flags |= flags;
86                         prio_queue_put(&queue, p);
87                 }
88         }
89
90         clear_prio_queue(&queue);
91         return result;
92 }
93
94 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
95 {
96         struct commit_list *list = NULL;
97         struct commit_list *result = NULL;
98         int i;
99
100         for (i = 0; i < n; i++) {
101                 if (one == twos[i])
102                         /*
103                          * We do not mark this even with RESULT so we do not
104                          * have to clean it up.
105                          */
106                         return commit_list_insert(one, &result);
107         }
108
109         if (parse_commit(one))
110                 return NULL;
111         for (i = 0; i < n; i++) {
112                 if (parse_commit(twos[i]))
113                         return NULL;
114         }
115
116         list = paint_down_to_common(one, n, twos, 0);
117
118         while (list) {
119                 struct commit *commit = pop_commit(&list);
120                 if (!(commit->object.flags & STALE))
121                         commit_list_insert_by_date(commit, &result);
122         }
123         return result;
124 }
125
126 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
127 {
128         struct commit_list *i, *j, *k, *ret = NULL;
129
130         if (!in)
131                 return ret;
132
133         commit_list_insert(in->item, &ret);
134
135         for (i = in->next; i; i = i->next) {
136                 struct commit_list *new_commits = NULL, *end = NULL;
137
138                 for (j = ret; j; j = j->next) {
139                         struct commit_list *bases;
140                         bases = get_merge_bases(i->item, j->item);
141                         if (!new_commits)
142                                 new_commits = bases;
143                         else
144                                 end->next = bases;
145                         for (k = bases; k; k = k->next)
146                                 end = k;
147                 }
148                 ret = new_commits;
149         }
150         return ret;
151 }
152
153 static int remove_redundant(struct commit **array, int cnt)
154 {
155         /*
156          * Some commit in the array may be an ancestor of
157          * another commit.  Move such commit to the end of
158          * the array, and return the number of commits that
159          * are independent from each other.
160          */
161         struct commit **work;
162         unsigned char *redundant;
163         int *filled_index;
164         int i, j, filled;
165
166         work = xcalloc(cnt, sizeof(*work));
167         redundant = xcalloc(cnt, 1);
168         ALLOC_ARRAY(filled_index, cnt - 1);
169
170         for (i = 0; i < cnt; i++)
171                 parse_commit(array[i]);
172         for (i = 0; i < cnt; i++) {
173                 struct commit_list *common;
174                 uint32_t min_generation = array[i]->generation;
175
176                 if (redundant[i])
177                         continue;
178                 for (j = filled = 0; j < cnt; j++) {
179                         if (i == j || redundant[j])
180                                 continue;
181                         filled_index[filled] = j;
182                         work[filled++] = array[j];
183
184                         if (array[j]->generation < min_generation)
185                                 min_generation = array[j]->generation;
186                 }
187                 common = paint_down_to_common(array[i], filled, work,
188                                               min_generation);
189                 if (array[i]->object.flags & PARENT2)
190                         redundant[i] = 1;
191                 for (j = 0; j < filled; j++)
192                         if (work[j]->object.flags & PARENT1)
193                                 redundant[filled_index[j]] = 1;
194                 clear_commit_marks(array[i], all_flags);
195                 clear_commit_marks_many(filled, work, all_flags);
196                 free_commit_list(common);
197         }
198
199         /* Now collect the result */
200         COPY_ARRAY(work, array, cnt);
201         for (i = filled = 0; i < cnt; i++)
202                 if (!redundant[i])
203                         array[filled++] = work[i];
204         for (j = filled, i = 0; i < cnt; i++)
205                 if (redundant[i])
206                         array[j++] = work[i];
207         free(work);
208         free(redundant);
209         free(filled_index);
210         return filled;
211 }
212
213 static struct commit_list *get_merge_bases_many_0(struct commit *one,
214                                                   int n,
215                                                   struct commit **twos,
216                                                   int cleanup)
217 {
218         struct commit_list *list;
219         struct commit **rslt;
220         struct commit_list *result;
221         int cnt, i;
222
223         result = merge_bases_many(one, n, twos);
224         for (i = 0; i < n; i++) {
225                 if (one == twos[i])
226                         return result;
227         }
228         if (!result || !result->next) {
229                 if (cleanup) {
230                         clear_commit_marks(one, all_flags);
231                         clear_commit_marks_many(n, twos, all_flags);
232                 }
233                 return result;
234         }
235
236         /* There are more than one */
237         cnt = commit_list_count(result);
238         rslt = xcalloc(cnt, sizeof(*rslt));
239         for (list = result, i = 0; list; list = list->next)
240                 rslt[i++] = list->item;
241         free_commit_list(result);
242
243         clear_commit_marks(one, all_flags);
244         clear_commit_marks_many(n, twos, all_flags);
245
246         cnt = remove_redundant(rslt, cnt);
247         result = NULL;
248         for (i = 0; i < cnt; i++)
249                 commit_list_insert_by_date(rslt[i], &result);
250         free(rslt);
251         return result;
252 }
253
254 struct commit_list *get_merge_bases_many(struct commit *one,
255                                          int n,
256                                          struct commit **twos)
257 {
258         return get_merge_bases_many_0(one, n, twos, 1);
259 }
260
261 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
262                                                int n,
263                                                struct commit **twos)
264 {
265         return get_merge_bases_many_0(one, n, twos, 0);
266 }
267
268 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
269 {
270         return get_merge_bases_many_0(one, 1, &two, 1);
271 }
272
273 /*
274  * Is "commit" a descendant of one of the elements on the "with_commit" list?
275  */
276 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
277 {
278         if (!with_commit)
279                 return 1;
280         while (with_commit) {
281                 struct commit *other;
282
283                 other = with_commit->item;
284                 with_commit = with_commit->next;
285                 if (in_merge_bases(other, commit))
286                         return 1;
287         }
288         return 0;
289 }
290
291 /*
292  * Is "commit" an ancestor of one of the "references"?
293  */
294 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
295 {
296         struct commit_list *bases;
297         int ret = 0, i;
298         uint32_t min_generation = GENERATION_NUMBER_INFINITY;
299
300         if (parse_commit(commit))
301                 return ret;
302         for (i = 0; i < nr_reference; i++) {
303                 if (parse_commit(reference[i]))
304                         return ret;
305                 if (reference[i]->generation < min_generation)
306                         min_generation = reference[i]->generation;
307         }
308
309         if (commit->generation > min_generation)
310                 return ret;
311
312         bases = paint_down_to_common(commit, nr_reference, reference, commit->generation);
313         if (commit->object.flags & PARENT2)
314                 ret = 1;
315         clear_commit_marks(commit, all_flags);
316         clear_commit_marks_many(nr_reference, reference, all_flags);
317         free_commit_list(bases);
318         return ret;
319 }
320
321 /*
322  * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
323  */
324 int in_merge_bases(struct commit *commit, struct commit *reference)
325 {
326         return in_merge_bases_many(commit, 1, &reference);
327 }
328
329 struct commit_list *reduce_heads(struct commit_list *heads)
330 {
331         struct commit_list *p;
332         struct commit_list *result = NULL, **tail = &result;
333         struct commit **array;
334         int num_head, i;
335
336         if (!heads)
337                 return NULL;
338
339         /* Uniquify */
340         for (p = heads; p; p = p->next)
341                 p->item->object.flags &= ~STALE;
342         for (p = heads, num_head = 0; p; p = p->next) {
343                 if (p->item->object.flags & STALE)
344                         continue;
345                 p->item->object.flags |= STALE;
346                 num_head++;
347         }
348         array = xcalloc(num_head, sizeof(*array));
349         for (p = heads, i = 0; p; p = p->next) {
350                 if (p->item->object.flags & STALE) {
351                         array[i++] = p->item;
352                         p->item->object.flags &= ~STALE;
353                 }
354         }
355         num_head = remove_redundant(array, num_head);
356         for (i = 0; i < num_head; i++)
357                 tail = &commit_list_insert(array[i], tail)->next;
358         free(array);
359         return result;
360 }
361
362 void reduce_heads_replace(struct commit_list **heads)
363 {
364         struct commit_list *result = reduce_heads(*heads);
365         free_commit_list(*heads);
366         *heads = result;
367 }
368
369 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
370 {
371         struct object *o;
372         struct commit *old_commit, *new_commit;
373         struct commit_list *old_commit_list = NULL;
374
375         /*
376          * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
377          * old_commit.  Otherwise we require --force.
378          */
379         o = deref_tag(the_repository, parse_object(the_repository, old_oid),
380                       NULL, 0);
381         if (!o || o->type != OBJ_COMMIT)
382                 return 0;
383         old_commit = (struct commit *) o;
384
385         o = deref_tag(the_repository, parse_object(the_repository, new_oid),
386                       NULL, 0);
387         if (!o || o->type != OBJ_COMMIT)
388                 return 0;
389         new_commit = (struct commit *) o;
390
391         if (parse_commit(new_commit) < 0)
392                 return 0;
393
394         commit_list_insert(old_commit, &old_commit_list);
395         return is_descendant_of(new_commit, old_commit_list);
396 }
397
398 /*
399  * Mimicking the real stack, this stack lives on the heap, avoiding stack
400  * overflows.
401  *
402  * At each recursion step, the stack items points to the commits whose
403  * ancestors are to be inspected.
404  */
405 struct contains_stack {
406         int nr, alloc;
407         struct contains_stack_entry {
408                 struct commit *commit;
409                 struct commit_list *parents;
410         } *contains_stack;
411 };
412
413 static int in_commit_list(const struct commit_list *want, struct commit *c)
414 {
415         for (; want; want = want->next)
416                 if (!oidcmp(&want->item->object.oid, &c->object.oid))
417                         return 1;
418         return 0;
419 }
420
421 /*
422  * Test whether the candidate is contained in the list.
423  * Do not recurse to find out, though, but return -1 if inconclusive.
424  */
425 static enum contains_result contains_test(struct commit *candidate,
426                                           const struct commit_list *want,
427                                           struct contains_cache *cache,
428                                           uint32_t cutoff)
429 {
430         enum contains_result *cached = contains_cache_at(cache, candidate);
431
432         /* If we already have the answer cached, return that. */
433         if (*cached)
434                 return *cached;
435
436         /* or are we it? */
437         if (in_commit_list(want, candidate)) {
438                 *cached = CONTAINS_YES;
439                 return CONTAINS_YES;
440         }
441
442         /* Otherwise, we don't know; prepare to recurse */
443         parse_commit_or_die(candidate);
444
445         if (candidate->generation < cutoff)
446                 return CONTAINS_NO;
447
448         return CONTAINS_UNKNOWN;
449 }
450
451 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
452 {
453         ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
454         contains_stack->contains_stack[contains_stack->nr].commit = candidate;
455         contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
456 }
457
458 static enum contains_result contains_tag_algo(struct commit *candidate,
459                                               const struct commit_list *want,
460                                               struct contains_cache *cache)
461 {
462         struct contains_stack contains_stack = { 0, 0, NULL };
463         enum contains_result result;
464         uint32_t cutoff = GENERATION_NUMBER_INFINITY;
465         const struct commit_list *p;
466
467         for (p = want; p; p = p->next) {
468                 struct commit *c = p->item;
469                 load_commit_graph_info(the_repository, c);
470                 if (c->generation < cutoff)
471                         cutoff = c->generation;
472         }
473
474         result = contains_test(candidate, want, cache, cutoff);
475         if (result != CONTAINS_UNKNOWN)
476                 return result;
477
478         push_to_contains_stack(candidate, &contains_stack);
479         while (contains_stack.nr) {
480                 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
481                 struct commit *commit = entry->commit;
482                 struct commit_list *parents = entry->parents;
483
484                 if (!parents) {
485                         *contains_cache_at(cache, commit) = CONTAINS_NO;
486                         contains_stack.nr--;
487                 }
488                 /*
489                  * If we just popped the stack, parents->item has been marked,
490                  * therefore contains_test will return a meaningful yes/no.
491                  */
492                 else switch (contains_test(parents->item, want, cache, cutoff)) {
493                 case CONTAINS_YES:
494                         *contains_cache_at(cache, commit) = CONTAINS_YES;
495                         contains_stack.nr--;
496                         break;
497                 case CONTAINS_NO:
498                         entry->parents = parents->next;
499                         break;
500                 case CONTAINS_UNKNOWN:
501                         push_to_contains_stack(parents->item, &contains_stack);
502                         break;
503                 }
504         }
505         free(contains_stack.contains_stack);
506         return contains_test(candidate, want, cache, cutoff);
507 }
508
509 int commit_contains(struct ref_filter *filter, struct commit *commit,
510                     struct commit_list *list, struct contains_cache *cache)
511 {
512         if (filter->with_commit_tag_algo)
513                 return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
514         return is_descendant_of(commit, list);
515 }
516
517 int reachable(struct commit *from, unsigned int with_flag,
518               unsigned int assign_flag, time_t min_commit_date)
519 {
520         struct prio_queue work = { compare_commits_by_commit_date };
521
522         prio_queue_put(&work, from);
523         while (work.nr) {
524                 struct commit_list *list;
525                 struct commit *commit = prio_queue_get(&work);
526
527                 if (commit->object.flags & with_flag) {
528                         from->object.flags |= assign_flag;
529                         break;
530                 }
531                 if (!commit->object.parsed)
532                         parse_object(the_repository, &commit->object.oid);
533                 if (commit->object.flags & REACHABLE)
534                         continue;
535                 commit->object.flags |= REACHABLE;
536                 if (commit->date < min_commit_date)
537                         continue;
538                 for (list = commit->parents; list; list = list->next) {
539                         struct commit *parent = list->item;
540                         if (!(parent->object.flags & REACHABLE))
541                                 prio_queue_put(&work, parent);
542                 }
543         }
544         from->object.flags |= REACHABLE;
545         clear_commit_marks(from, REACHABLE);
546         clear_prio_queue(&work);
547         return (from->object.flags & assign_flag);
548 }
549
550 int can_all_from_reach_with_flag(struct object_array *from,
551                                  unsigned int with_flag,
552                                  unsigned int assign_flag,
553                                  time_t min_commit_date)
554 {
555         int i;
556
557         for (i = 0; i < from->nr; i++) {
558                 struct object *from_one = from->objects[i].item;
559
560                 if (from_one->flags & assign_flag)
561                         continue;
562                 from_one = deref_tag(the_repository, from_one, "a from object", 0);
563                 if (!from_one || from_one->type != OBJ_COMMIT) {
564                         /* no way to tell if this is reachable by
565                          * looking at the ancestry chain alone, so
566                          * leave a note to ourselves not to worry about
567                          * this object anymore.
568                          */
569                         from->objects[i].item->flags |= assign_flag;
570                         continue;
571                 }
572                 if (!reachable((struct commit *)from_one, with_flag, assign_flag,
573                                min_commit_date))
574                         return 0;
575         }
576         return 1;
577 }
578
579 int can_all_from_reach(struct commit_list *from, struct commit_list *to,
580                        int cutoff_by_min_date)
581 {
582         struct object_array from_objs = OBJECT_ARRAY_INIT;
583         time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
584         struct commit_list *from_iter = from, *to_iter = to;
585         int result;
586
587         while (from_iter) {
588                 add_object_array(&from_iter->item->object, NULL, &from_objs);
589
590                 if (!parse_commit(from_iter->item)) {
591                         if (from_iter->item->date < min_commit_date)
592                                 min_commit_date = from_iter->item->date;
593                 }
594
595                 from_iter = from_iter->next;
596         }
597
598         while (to_iter) {
599                 if (!parse_commit(to_iter->item)) {
600                         if (to_iter->item->date < min_commit_date)
601                                 min_commit_date = to_iter->item->date;
602                 }
603
604                 to_iter->item->object.flags |= PARENT2;
605
606                 to_iter = to_iter->next;
607         }
608
609         result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
610                                               min_commit_date);
611
612         while (from) {
613                 clear_commit_marks(from->item, PARENT1);
614                 from = from->next;
615         }
616
617         while (to) {
618                 clear_commit_marks(to->item, PARENT2);
619                 to = to->next;
620         }
621
622         object_array_clear(&from_objs);
623         return result;
624 }