Merge branch 'ds/reachable-final-cleanup'
[gitweb.git] / commit-reach.c
index a6bc4781a63d118d989599241057ec95fa39da5b..67e1792b5c0f5d4d83794348941b66f77874f1ee 100644 (file)
@@ -1,13 +1,16 @@
 #include "cache.h"
 #include "commit.h"
+#include "commit-graph.h"
 #include "decorate.h"
 #include "prio-queue.h"
 #include "tree.h"
+#include "ref-filter.h"
 #include "revision.h"
 #include "tag.h"
 #include "commit-reach.h"
 
 /* Remember to update object flag allocation in object.h */
+#define REACHABLE       (1u<<15)
 #define PARENT1                (1u<<16)
 #define PARENT2                (1u<<17)
 #define STALE          (1u<<18)
@@ -36,6 +39,9 @@ static struct commit_list *paint_down_to_common(struct commit *one, int n,
        int i;
        uint32_t last_gen = GENERATION_NUMBER_INFINITY;
 
+       if (!min_generation)
+               queue.compare = compare_commits_by_commit_date;
+
        one->object.flags |= PARENT1;
        if (!n) {
                commit_list_append(one, &result);
@@ -53,7 +59,7 @@ static struct commit_list *paint_down_to_common(struct commit *one, int n,
                struct commit_list *parents;
                int flags;
 
-               if (commit->generation > last_gen)
+               if (min_generation && commit->generation > last_gen)
                        BUG("bad generation skip %8x > %8x at %s",
                            commit->generation, last_gen,
                            oid_to_hex(&commit->object.oid));
@@ -274,15 +280,25 @@ int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
 {
        if (!with_commit)
                return 1;
-       while (with_commit) {
-               struct commit *other;
 
-               other = with_commit->item;
-               with_commit = with_commit->next;
-               if (in_merge_bases(other, commit))
-                       return 1;
+       if (generation_numbers_enabled(the_repository)) {
+               struct commit_list *from_list = NULL;
+               int result;
+               commit_list_insert(commit, &from_list);
+               result = can_all_from_reach(from_list, with_commit, 0);
+               free_commit_list(from_list);
+               return result;
+       } else {
+               while (with_commit) {
+                       struct commit *other;
+
+                       other = with_commit->item;
+                       with_commit = with_commit->next;
+                       if (in_merge_bases(other, commit))
+                               return 1;
+               }
+               return 0;
        }
-       return 0;
 }
 
 /*
@@ -363,20 +379,11 @@ void reduce_heads_replace(struct commit_list **heads)
        *heads = result;
 }
 
-static void unmark_and_free(struct commit_list *list, unsigned int mark)
-{
-       while (list) {
-               struct commit *commit = pop_commit(&list);
-               commit->object.flags &= ~mark;
-       }
-}
-
 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
 {
        struct object *o;
        struct commit *old_commit, *new_commit;
-       struct commit_list *list, *used;
-       int found = 0;
+       struct commit_list *old_commit_list = NULL;
 
        /*
         * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
@@ -397,17 +404,287 @@ int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
        if (parse_commit(new_commit) < 0)
                return 0;
 
-       used = list = NULL;
-       commit_list_insert(new_commit, &list);
-       while (list) {
-               new_commit = pop_most_recent_commit(&list, TMP_MARK);
-               commit_list_insert(new_commit, &used);
-               if (new_commit == old_commit) {
-                       found = 1;
+       commit_list_insert(old_commit, &old_commit_list);
+       return is_descendant_of(new_commit, old_commit_list);
+}
+
+/*
+ * Mimicking the real stack, this stack lives on the heap, avoiding stack
+ * overflows.
+ *
+ * At each recursion step, the stack items points to the commits whose
+ * ancestors are to be inspected.
+ */
+struct contains_stack {
+       int nr, alloc;
+       struct contains_stack_entry {
+               struct commit *commit;
+               struct commit_list *parents;
+       } *contains_stack;
+};
+
+static int in_commit_list(const struct commit_list *want, struct commit *c)
+{
+       for (; want; want = want->next)
+               if (!oidcmp(&want->item->object.oid, &c->object.oid))
+                       return 1;
+       return 0;
+}
+
+/*
+ * Test whether the candidate is contained in the list.
+ * Do not recurse to find out, though, but return -1 if inconclusive.
+ */
+static enum contains_result contains_test(struct commit *candidate,
+                                         const struct commit_list *want,
+                                         struct contains_cache *cache,
+                                         uint32_t cutoff)
+{
+       enum contains_result *cached = contains_cache_at(cache, candidate);
+
+       /* If we already have the answer cached, return that. */
+       if (*cached)
+               return *cached;
+
+       /* or are we it? */
+       if (in_commit_list(want, candidate)) {
+               *cached = CONTAINS_YES;
+               return CONTAINS_YES;
+       }
+
+       /* Otherwise, we don't know; prepare to recurse */
+       parse_commit_or_die(candidate);
+
+       if (candidate->generation < cutoff)
+               return CONTAINS_NO;
+
+       return CONTAINS_UNKNOWN;
+}
+
+static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack)
+{
+       ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc);
+       contains_stack->contains_stack[contains_stack->nr].commit = candidate;
+       contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents;
+}
+
+static enum contains_result contains_tag_algo(struct commit *candidate,
+                                             const struct commit_list *want,
+                                             struct contains_cache *cache)
+{
+       struct contains_stack contains_stack = { 0, 0, NULL };
+       enum contains_result result;
+       uint32_t cutoff = GENERATION_NUMBER_INFINITY;
+       const struct commit_list *p;
+
+       for (p = want; p; p = p->next) {
+               struct commit *c = p->item;
+               load_commit_graph_info(the_repository, c);
+               if (c->generation < cutoff)
+                       cutoff = c->generation;
+       }
+
+       result = contains_test(candidate, want, cache, cutoff);
+       if (result != CONTAINS_UNKNOWN)
+               return result;
+
+       push_to_contains_stack(candidate, &contains_stack);
+       while (contains_stack.nr) {
+               struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1];
+               struct commit *commit = entry->commit;
+               struct commit_list *parents = entry->parents;
+
+               if (!parents) {
+                       *contains_cache_at(cache, commit) = CONTAINS_NO;
+                       contains_stack.nr--;
+               }
+               /*
+                * If we just popped the stack, parents->item has been marked,
+                * therefore contains_test will return a meaningful yes/no.
+                */
+               else switch (contains_test(parents->item, want, cache, cutoff)) {
+               case CONTAINS_YES:
+                       *contains_cache_at(cache, commit) = CONTAINS_YES;
+                       contains_stack.nr--;
+                       break;
+               case CONTAINS_NO:
+                       entry->parents = parents->next;
                        break;
+               case CONTAINS_UNKNOWN:
+                       push_to_contains_stack(parents->item, &contains_stack);
+                       break;
+               }
+       }
+       free(contains_stack.contains_stack);
+       return contains_test(candidate, want, cache, cutoff);
+}
+
+int commit_contains(struct ref_filter *filter, struct commit *commit,
+                   struct commit_list *list, struct contains_cache *cache)
+{
+       if (filter->with_commit_tag_algo)
+               return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
+       return is_descendant_of(commit, list);
+}
+
+static int compare_commits_by_gen(const void *_a, const void *_b)
+{
+       const struct commit *a = (const struct commit *)_a;
+       const struct commit *b = (const struct commit *)_b;
+
+       if (a->generation < b->generation)
+               return -1;
+       if (a->generation > b->generation)
+               return 1;
+       return 0;
+}
+
+int can_all_from_reach_with_flag(struct object_array *from,
+                                unsigned int with_flag,
+                                unsigned int assign_flag,
+                                time_t min_commit_date,
+                                uint32_t min_generation)
+{
+       struct commit **list = NULL;
+       int i;
+       int nr_commits;
+       int result = 1;
+
+       ALLOC_ARRAY(list, from->nr);
+       nr_commits = 0;
+       for (i = 0; i < from->nr; i++) {
+               struct object *from_one = from->objects[i].item;
+
+               if (!from_one || from_one->flags & assign_flag)
+                       continue;
+
+               from_one = deref_tag(the_repository, from_one,
+                                    "a from object", 0);
+               if (!from_one || from_one->type != OBJ_COMMIT) {
+                       /*
+                        * no way to tell if this is reachable by
+                        * looking at the ancestry chain alone, so
+                        * leave a note to ourselves not to worry about
+                        * this object anymore.
+                        */
+                       from->objects[i].item->flags |= assign_flag;
+                       continue;
+               }
+
+               list[nr_commits] = (struct commit *)from_one;
+               if (parse_commit(list[nr_commits]) ||
+                   list[nr_commits]->generation < min_generation) {
+                       result = 0;
+                       goto cleanup;
+               }
+
+               nr_commits++;
+       }
+
+       QSORT(list, nr_commits, compare_commits_by_gen);
+
+       for (i = 0; i < nr_commits; i++) {
+               /* DFS from list[i] */
+               struct commit_list *stack = NULL;
+
+               list[i]->object.flags |= assign_flag;
+               commit_list_insert(list[i], &stack);
+
+               while (stack) {
+                       struct commit_list *parent;
+
+                       if (stack->item->object.flags & with_flag) {
+                               pop_commit(&stack);
+                               continue;
+                       }
+
+                       for (parent = stack->item->parents; parent; parent = parent->next) {
+                               if (parent->item->object.flags & (with_flag | RESULT))
+                                       stack->item->object.flags |= RESULT;
+
+                               if (!(parent->item->object.flags & assign_flag)) {
+                                       parent->item->object.flags |= assign_flag;
+
+                                       if (parse_commit(parent->item) ||
+                                           parent->item->date < min_commit_date ||
+                                           parent->item->generation < min_generation)
+                                               continue;
+
+                                       commit_list_insert(parent->item, &stack);
+                                       break;
+                               }
+                       }
+
+                       if (!parent)
+                               pop_commit(&stack);
+               }
+
+               if (!(list[i]->object.flags & (with_flag | RESULT))) {
+                       result = 0;
+                       goto cleanup;
                }
        }
-       unmark_and_free(list, TMP_MARK);
-       unmark_and_free(used, TMP_MARK);
-       return found;
+
+cleanup:
+       clear_commit_marks_many(nr_commits, list, RESULT | assign_flag);
+       free(list);
+
+       for (i = 0; i < from->nr; i++)
+               from->objects[i].item->flags &= ~assign_flag;
+
+       return result;
+}
+
+int can_all_from_reach(struct commit_list *from, struct commit_list *to,
+                      int cutoff_by_min_date)
+{
+       struct object_array from_objs = OBJECT_ARRAY_INIT;
+       time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
+       struct commit_list *from_iter = from, *to_iter = to;
+       int result;
+       uint32_t min_generation = GENERATION_NUMBER_INFINITY;
+
+       while (from_iter) {
+               add_object_array(&from_iter->item->object, NULL, &from_objs);
+
+               if (!parse_commit(from_iter->item)) {
+                       if (from_iter->item->date < min_commit_date)
+                               min_commit_date = from_iter->item->date;
+
+                       if (from_iter->item->generation < min_generation)
+                               min_generation = from_iter->item->generation;
+               }
+
+               from_iter = from_iter->next;
+       }
+
+       while (to_iter) {
+               if (!parse_commit(to_iter->item)) {
+                       if (to_iter->item->date < min_commit_date)
+                               min_commit_date = to_iter->item->date;
+
+                       if (to_iter->item->generation < min_generation)
+                               min_generation = to_iter->item->generation;
+               }
+
+               to_iter->item->object.flags |= PARENT2;
+
+               to_iter = to_iter->next;
+       }
+
+       result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
+                                             min_commit_date, min_generation);
+
+       while (from) {
+               clear_commit_marks(from->item, PARENT1);
+               from = from->next;
+       }
+
+       while (to) {
+               clear_commit_marks(to->item, PARENT2);
+               to = to->next;
+       }
+
+       object_array_clear(&from_objs);
+       return result;
 }