commit: use mergesort() in commit_list_sort_by_date()

Replace the insertion sort in commit_list_sort_by_date() with a call to the generic mergesort function. This sets the stage for using commit_list_sort_by_date() for larger lists, as shown in the next patch. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Apr 1, 2012 at 00:10 UTC 46905893b20ac2a044c06a0eecc12425a8405e69
1 file changed +23 -6
commit.c
+23 -6
@@ -7,6 +7,7 @@
7 #include "revision.h"
8 #include "notes.h"
9 #include "gpg-interface.h"
10 +#include "mergesort.h"
11
12 int save_commit_buffer = 1;
13
@@ -390,15 +391,31 @@ struct commit_list * commit_list_insert_by_date(struct commit *item, struct comm
391 return commit_list_insert(item, pp);
392 }
393
394 +static int commit_list_compare_by_date(const void *a, const void *b)
395 +{
396 + unsigned long a_date = ((const struct commit_list *)a)->item->date;
397 + unsigned long b_date = ((const struct commit_list *)b)->item->date;
398 + if (a_date < b_date)
399 + return 1;
400 + if (a_date > b_date)
401 + return -1;
402 + return 0;
403 +}
404 +
405 +static void *commit_list_get_next(const void *a)
406 +{
407 + return ((const struct commit_list *)a)->next;
408 +}
409 +
410 +static void commit_list_set_next(void *a, void *next)
411 +{
412 + ((struct commit_list *)a)->next = next;
413 +}
414
415 void commit_list_sort_by_date(struct commit_list **list)
416 {
396 - struct commit_list *ret = NULL;
397 - while (*list) {
398 - commit_list_insert_by_date((*list)->item, &ret);
399 - *list = (*list)->next;
400 - }
401 - *list = ret;
417 + *list = mergesort(*list, commit_list_get_next, commit_list_set_next,
418 + commit_list_compare_by_date);
419 }
420
421 struct commit *pop_most_recent_commit(struct commit_list **list,