commit-reach: make can_all_from_reach... linear
[gitweb.git] / commit-reach.c
index 01d796f0116163af0b0b877d528b282160a5287e..bc522d6840f3c9fc4ae79adb0f2fc339fe37df38 100644 (file)
@@ -10,6 +10,7 @@
 #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)
@@ -365,20 +366,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
@@ -399,19 +391,8 @@ 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;
-                       break;
-               }
-       }
-       unmark_and_free(list, TMP_MARK);
-       unmark_and_free(used, TMP_MARK);
-       return found;
+       commit_list_insert(old_commit, &old_commit_list);
+       return is_descendant_of(new_commit, old_commit_list);
 }
 
 /*
@@ -532,3 +513,140 @@ int commit_contains(struct ref_filter *filter, struct commit *commit,
                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 result = 1;
+
+       ALLOC_ARRAY(list, from->nr);
+       for (i = 0; i < from->nr; i++) {
+               list[i] = (struct commit *)from->objects[i].item;
+
+               if (parse_commit(list[i]) ||
+                   list[i]->generation < min_generation)
+                       return 0;
+       }
+
+       QSORT(list, from->nr, compare_commits_by_gen);
+
+       for (i = 0; i < from->nr; 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;
+               }
+       }
+
+cleanup:
+       for (i = 0; i < from->nr; i++) {
+               clear_commit_marks(list[i], RESULT);
+               clear_commit_marks(list[i], 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;
+}