Commit b60488e1d7564716dc2ae7ce078a415280bd7df6

Vicent Marti 2010-05-23T04:37:39

Added sort method for commit lists. Fixed bug when parsing time headers from commits. Signed-off-by: Vicent Marti <tanoku@gmail.com> Signed-off-by: Andreas Ericsson <ae@op5.se>

diff --git a/src/commit.c b/src/commit.c
index 1763ca8..dec93ef 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -97,7 +97,7 @@ git_commit *git_commit_lookup(git_revpool *pool, const git_oid *id)
 {
     git_commit *commit = NULL;
 
-    if (pool == NULL || pool->db == NULL)
+    if (pool == NULL)
         return NULL;
 
     commit = (git_commit *)git_revpool_table_lookup(pool->commits, id);
@@ -126,14 +126,14 @@ int git_commit__parse_time(time_t *commit_time, char *buffer, const char *buffer
         return -1;
 
     buffer = memchr(buffer, '\n', buffer_end - buffer);
-    if (buffer == 0 || buffer >= buffer_end)
+    if (buffer == 0 || ++buffer >= buffer_end)
         return -1;
 
     if (memcmp(buffer, "committer ", 10) != 0)
         return -1;
 
     buffer = memchr(buffer, '\n', buffer_end - buffer);
-    if (buffer == 0 || buffer >= buffer_end)
+    if (buffer == 0 || ++buffer >= buffer_end)
         return -1;
 
     *commit_time = strtol(buffer, &buffer, 10);
@@ -291,3 +291,57 @@ void git_commit_list_clear(git_commit_list *list, int free_commits)
     list->size = 0;
 }
 
+void git_commit_list_sort(git_commit_list *list)
+{
+    git_commit_node *p, *q, *e, *tail;
+    int in_size, p_size, q_size, merge_count, i;
+
+    if (list->head == NULL)
+        return;
+
+    in_size = 1;
+
+    do
+    {
+        p = list->head;
+        tail = NULL;
+        merge_count = 0;
+
+        while (p != NULL)
+        {
+            merge_count++;
+            q = p;
+            p_size = 0;
+            q_size = in_size;
+
+            for (i = 0; i < in_size && q; ++i, q = q->next)
+                p_size++;
+
+            while (p_size > 0 || (q_size > 0 && q))
+            {
+                if (p_size == 0)
+                    e = q, q = q->next, q_size--;
+
+                else if (q_size == 0 || q == NULL ||
+                    p->commit->commit_time <= q->commit->commit_time)
+                    e = p, p = p->next, p_size--;
+
+                else
+                    e = q, q = q->next, q_size--;
+
+                if (tail != NULL)
+                    tail->next = e;
+                else
+                    list->head = e;
+
+                tail = e;
+            }
+
+            p = q;
+        }
+
+        tail->next = NULL;
+        in_size *= 2;
+
+    } while (merge_count > 1);
+}
diff --git a/src/commit.h b/src/commit.h
index 818d98c..72ea4e9 100644
--- a/src/commit.h
+++ b/src/commit.h
@@ -47,5 +47,6 @@ void git_commit_list_clear(git_commit_list *list, int free_commits);
 void git_commit_list_append(git_commit_list *list, git_commit *commit);
 git_commit *git_commit_list_pop_back(git_commit_list *list);
 git_commit *git_commit_list_pop_front(git_commit_list *list);
+void git_commit_list_sort(git_commit_list *list);
 
 #endif