Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 4690589

Browse files
René Scharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0db71e0 commit 4690589

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

commit.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "revision.h"
88
#include "notes.h"
99
#include "gpg-interface.h"
10+
#include "mergesort.h"
1011

1112
int save_commit_buffer = 1;
1213

@@ -390,15 +391,31 @@ struct commit_list * commit_list_insert_by_date(struct commit *item, struct comm
390391
return commit_list_insert(item, pp);
391392
}
392393

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+
}
393414

394415
void commit_list_sort_by_date(struct commit_list **list)
395416
{
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);
402419
}
403420

404421
struct commit *pop_most_recent_commit(struct commit_list **list,

0 commit comments

Comments
 (0)