toposort: rename "lifo" field
[gitweb.git] / commit.c
index 1a41757ee377d804782ccfb4e27c54b607dcf91d..11b9635df27ae2d6379cf70f5737c33d9d49e701 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -8,12 +8,14 @@
 #include "notes.h"
 #include "gpg-interface.h"
 #include "mergesort.h"
+#include "commit-slab.h"
 
 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
 
 int save_commit_buffer = 1;
 
 const char *commit_type = "commit";
+static int commit_count;
 
 static struct commit *check_commit(struct object *obj,
                                   const unsigned char *sha1,
@@ -58,8 +60,11 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_n
 struct commit *lookup_commit(const unsigned char *sha1)
 {
        struct object *obj = lookup_object(sha1);
-       if (!obj)
-               return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
+       if (!obj) {
+               struct commit *c = alloc_commit_node();
+               c->index = commit_count++;
+               return create_object(sha1, OBJ_COMMIT, c);
+       }
        if (!obj->type)
                obj->type = OBJ_COMMIT;
        return check_commit(obj, sha1, 0);
@@ -497,23 +502,33 @@ struct commit *pop_commit(struct commit_list **stack)
        return item;
 }
 
+/*
+ * Topological sort support
+ */
+
+/* count number of children that have not been emitted */
+define_commit_slab(indegree_slab, int);
+
 /*
  * Performs an in-place topological sort on the list supplied.
  */
-void sort_in_topological_order(struct commit_list ** list, int lifo)
+void sort_in_topological_order(struct commit_list ** list, enum rev_sort_order sort_order)
 {
        struct commit_list *next, *orig = *list;
        struct commit_list *work, **insert;
        struct commit_list **pptr;
+       struct indegree_slab indegree;
 
        if (!orig)
                return;
        *list = NULL;
 
+       init_indegree_slab(&indegree);
+
        /* Mark them and clear the indegree */
        for (next = orig; next; next = next->next) {
                struct commit *commit = next->item;
-               commit->indegree = 1;
+               *(indegree_slab_at(&indegree, commit)) = 1;
        }
 
        /* update the indegree */
@@ -521,9 +536,10 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
                struct commit_list * parents = next->item->parents;
                while (parents) {
                        struct commit *parent = parents->item;
+                       int *pi = indegree_slab_at(&indegree, parent);
 
-                       if (parent->indegree)
-                               parent->indegree++;
+                       if (*pi)
+                               (*pi)++;
                        parents = parents->next;
                }
        }
@@ -540,12 +556,12 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
        for (next = orig; next; next = next->next) {
                struct commit *commit = next->item;
 
-               if (commit->indegree == 1)
+               if (*(indegree_slab_at(&indegree, commit)) == 1)
                        insert = &commit_list_insert(commit, insert)->next;
        }
 
        /* process the list in topological order */
-       if (!lifo)
+       if (sort_order != REV_SORT_IN_GRAPH_ORDER)
                commit_list_sort_by_date(&work);
 
        pptr = list;
@@ -561,8 +577,9 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
                commit = work_item->item;
                for (parents = commit->parents; parents ; parents = parents->next) {
                        struct commit *parent = parents->item;
+                       int *pi = indegree_slab_at(&indegree, parent);
 
-                       if (!parent->indegree)
+                       if (!*pi)
                                continue;
 
                        /*
@@ -570,21 +587,27 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
                         * when all their children have been emitted thereby
                         * guaranteeing topological order.
                         */
-                       if (--parent->indegree == 1) {
-                               if (!lifo)
+                       if (--(*pi) == 1) {
+                               switch (sort_order) {
+                               case REV_SORT_BY_COMMIT_DATE:
                                        commit_list_insert_by_date(parent, &work);
-                               else
+                                       break;
+                               default: /* REV_SORT_IN_GRAPH_ORDER */
                                        commit_list_insert(parent, &work);
+                                       break;
+                               }
                        }
                }
                /*
                 * work_item is a commit all of whose children
                 * have already been emitted. we can emit it now.
                 */
-               commit->indegree = 0;
+               *(indegree_slab_at(&indegree, commit)) = 0;
                *pptr = work_item;
                pptr = &work_item->next;
        }
+
+       clear_indegree_slab(&indegree);
 }
 
 /* merge-base stuff */