Add compat/setenv.c, use in git.c.
[gitweb.git] / epoch.c
diff --git a/epoch.c b/epoch.c
index 82becf677ba852cf103a9f9a1131dc54141f019f..db44f5ca9f73a75cc266d9b45eda0aca56dd1ede 100644 (file)
--- a/epoch.c
+++ b/epoch.c
@@ -28,7 +28,7 @@ static BN_CTX *context = NULL;
 static struct fraction *one = NULL;
 static struct fraction *zero = NULL;
 
-static BN_CTX *get_BN_CTX()
+static BN_CTX *get_BN_CTX(void)
 {
        if (!context) {
                context = BN_CTX_new();
@@ -36,7 +36,7 @@ static BN_CTX *get_BN_CTX()
        return context;
 }
 
-static struct fraction *new_zero()
+static struct fraction *new_zero(void)
 {
        struct fraction *result = xmalloc(sizeof(*result));
        BN_init(&result->numerator);
@@ -75,7 +75,7 @@ static struct fraction *init_fraction(struct fraction *fraction)
        return fraction;
 }
 
-static struct fraction *get_one()
+static struct fraction *get_one(void)
 {
        if (!one) {
                one = new_zero();
@@ -84,7 +84,7 @@ static struct fraction *get_one()
        return one;
 }
 
-static struct fraction *get_zero()
+static struct fraction *get_zero(void)
 {
        if (!zero) {
                zero = new_zero();
@@ -224,17 +224,13 @@ static int find_base_for_list(struct commit_list *list, struct commit **boundary
        for (; list; list = list->next) {
                struct commit *item = list->item;
 
-               if (item->object.util) {
-                       die("%s:%d:%s: logic error: this should not have happened - commit %s",
-                           __FILE__, __LINE__, __FUNCTION__,
-                           sha1_to_hex(item->object.sha1));
-               }
-
-               new_mass_counter(list->item, get_one());
-               add(&injected, &injected, get_one());
+               if (!item->object.util) {
+                       new_mass_counter(list->item, get_one());
+                       add(&injected, &injected, get_one());
 
-               commit_list_insert(list->item, &cleaner);
-               commit_list_insert(list->item, &pending);
+                       commit_list_insert(list->item, &cleaner);
+                       commit_list_insert(list->item, &pending);
+               }
        }
 
        while (!*boundary && pending && !ret) {
@@ -259,11 +255,11 @@ static int find_base_for_list(struct commit_list *list, struct commit **boundary
 
                                if (!parent_node) {
                                        parent_node = new_mass_counter(parent, &distribution);
-                                       insert_by_date(&pending, parent);
+                                       insert_by_date(parent, &pending);
                                        commit_list_insert(parent, &cleaner);
                                } else {
                                        if (!compare(&parent_node->pending, get_zero()))
-                                               insert_by_date(&pending, parent);
+                                               insert_by_date(parent, &pending);
                                        add(&parent_node->pending, &parent_node->pending, &distribution);
                                }
                        }
@@ -428,18 +424,9 @@ static void mark_ancestors_uninteresting(struct commit *commit)
 static void sort_first_epoch(struct commit *head, struct commit_list **stack)
 {
        struct commit_list *parents;
-       struct commit_list *reversed_parents = NULL;
 
        head->object.flags |= VISITED;
 
-       /*
-        * parse_commit() builds the parent list in reverse order with respect
-        * to the order of the git-commit-tree arguments. So we need to reverse
-        * this list to output the oldest (or most "local") commits last.
-        */
-       for (parents = head->parents; parents; parents = parents->next)
-               commit_list_insert(parents->item, &reversed_parents);
-
        /*
         * TODO: By sorting the parents in a different order, we can alter the
         * merge order to show contemporaneous changes in parallel branches
@@ -449,8 +436,8 @@ static void sort_first_epoch(struct commit *head, struct commit_list **stack)
         * changes.
         */
 
-       while (reversed_parents) {
-               struct commit *parent = pop_commit(&reversed_parents);
+       for (parents = head->parents; parents; parents = parents->next) {
+               struct commit *parent = parents->item;
 
                if (head->object.flags & UNINTERESTING) {
                        /*
@@ -474,7 +461,7 @@ static void sort_first_epoch(struct commit *head, struct commit_list **stack)
 
                        } else {
                                sort_first_epoch(parent, stack);
-                               if (reversed_parents) {
+                               if (parents) {
                                        /*
                                         * This indicates a possible
                                         * discontinuity it may not be be
@@ -501,7 +488,7 @@ static void sort_first_epoch(struct commit *head, struct commit_list **stack)
  *
  * Sets the return value to STOP if no further output should be generated.
  */
-static int emit_stack(struct commit_list **stack, emitter_func emitter)
+static int emit_stack(struct commit_list **stack, emitter_func emitter, int include_last)
 {
        unsigned int seen = 0;
        int action = CONTINUE;
@@ -509,8 +496,11 @@ static int emit_stack(struct commit_list **stack, emitter_func emitter)
        while (*stack && (action != STOP)) {
                struct commit *next = pop_commit(stack);
                seen |= next->object.flags;
-               if (*stack)
-                       action = (*emitter) (next);
+               if (*stack || include_last) {
+                       if (!*stack) 
+                               next->object.flags |= BOUNDARY;
+                       action = emitter(next);
+               }
        }
 
        if (*stack) {
@@ -555,7 +545,7 @@ static int sort_in_merge_order(struct commit *head_of_epoch, emitter_func emitte
                                if (next->object.flags & UNINTERESTING) {
                                        action = STOP;
                                } else {
-                                       action = (*emitter) (next);
+                                       action = emitter(next);
                                }
                                if (action != STOP) {
                                        next = next->parents->item;
@@ -566,13 +556,13 @@ static int sort_in_merge_order(struct commit *head_of_epoch, emitter_func emitte
                } else {
                        struct commit_list *stack = NULL;
                        sort_first_epoch(next, &stack);
-                       action = emit_stack(&stack, emitter);
+                       action = emit_stack(&stack, emitter, (base == NULL));
                        next = base;
                }
        }
 
        if (next && (action != STOP) && !ret) {
-               (*emitter) (next);
+               emitter(next);
        }
 
        return ret;
@@ -592,19 +582,8 @@ int sort_list_in_merge_order(struct commit_list *list, emitter_func emitter)
        int action = CONTINUE;
        struct commit_list *reversed = NULL;
 
-       for (; list; list = list->next) {
-               struct commit *next = list->item;
-
-               if (!(next->object.flags & UNINTERESTING)) {
-                       if (next->object.flags & DUPCHECK) {
-                               fprintf(stderr, "%s: duplicate commit %s ignored\n",
-                                       __FUNCTION__, sha1_to_hex(next->object.sha1));
-                       } else {
-                               next->object.flags |= DUPCHECK;
-                               commit_list_insert(list->item, &reversed);
-                       }
-               }
-       }
+       for (; list; list = list->next)
+               commit_list_insert(list->item, &reversed);
 
        if (!reversed)
                return ret;
@@ -625,24 +604,31 @@ int sort_list_in_merge_order(struct commit_list *list, emitter_func emitter)
                        base->object.flags |= BOUNDARY;
 
                while (reversed) {
-                       sort_first_epoch(pop_commit(&reversed), &stack);
-                       if (reversed) {
-                               /*
-                                * If we have more commits to push, then the
-                                * first push for the next parent may (or may
-                                * not) represent a discontinuity with respect
-                                * to the parent currently on the top of
-                                * the stack.
-                                *
-                                * Mark it for checking here, and check it
-                                * with the next push. See sort_first_epoch()
-                                * for more details.
-                                */
-                               stack->item->object.flags |= DISCONTINUITY;
+                       struct commit * next = pop_commit(&reversed);
+
+                       if (!(next->object.flags & VISITED) && next!=base) {
+                               sort_first_epoch(next, &stack);
+                               if (reversed) {
+                                       /*
+                                        * If we have more commits 
+                                        * to push, then the first
+                                        * push for the next parent may 
+                                        * (or may * not) represent a 
+                                        * discontinuity with respect
+                                        * to the parent currently on 
+                                        * the top of the stack.
+                                        *
+                                        * Mark it for checking here, 
+                                        * and check it with the next 
+                                        * push. See sort_first_epoch()
+                                        * for more details.
+                                        */
+                                       stack->item->object.flags |= DISCONTINUITY;
+                               }
                        }
                }
 
-               action = emit_stack(&stack, emitter);
+               action = emit_stack(&stack, emitter, (base==NULL));
        }
 
        if (base && (action != STOP)) {