sha1_file: use llist_mergesort() for sorting packs

Sort the linked list of packs directly using llist_mergesort() instead of building an array, calling qsort(3) and fixing up the list pointers. This is shorter and less complicated. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 13, 2016 at 19:54 UTC c4c6effa9bdb14e65b147e4e96be55860b73df05
1 file changed +15 -24
sha1_file.c
+15 -24
@@ -25,6 +25,7 @@
25 #include "dir.h"
26 #include "mru.h"
27 #include "list.h"
28 +#include "mergesort.h"
29
30 #ifndef O_NOATIME
31 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
@@ -1380,10 +1381,20 @@ static void prepare_packed_git_one(char *objdir, int local)
1381 strbuf_release(&path);
1382 }
1383
1384 +static void *get_next_packed_git(const void *p)
1385 +{
1386 + return ((const struct packed_git *)p)->next;
1387 +}
1388 +
1389 +static void set_next_packed_git(void *p, void *next)
1390 +{
1391 + ((struct packed_git *)p)->next = next;
1392 +}
1393 +
1394 static int sort_pack(const void *a_, const void *b_)
1395 {
1385 - struct packed_git *a = *((struct packed_git **)a_);
1386 - struct packed_git *b = *((struct packed_git **)b_);
1396 + const struct packed_git *a = a_;
1397 + const struct packed_git *b = b_;
1398 int st;
1399
1400 /*
@@ -1410,28 +1421,8 @@ static int sort_pack(const void *a_, const void *b_)
1421
1422 static void rearrange_packed_git(void)
1423 {
1413 - struct packed_git **ary, *p;
1414 - int i, n;
1415 -
1416 - for (n = 0, p = packed_git; p; p = p->next)
1417 - n++;
1418 - if (n < 2)
1419 - return;
1420 -
1421 - /* prepare an array of packed_git for easier sorting */
1422 - ary = xcalloc(n, sizeof(struct packed_git *));
1423 - for (n = 0, p = packed_git; p; p = p->next)
1424 - ary[n++] = p;
1425 -
1426 - qsort(ary, n, sizeof(struct packed_git *), sort_pack);
1427 -
1428 - /* link them back again */
1429 - for (i = 0; i < n - 1; i++)
1430 - ary[i]->next = ary[i + 1];
1431 - ary[n - 1]->next = NULL;
1432 - packed_git = ary[0];
1433 -
1434 - free(ary);
1424 + packed_git = llist_mergesort(packed_git, get_next_packed_git,
1425 + set_next_packed_git, sort_pack);
1426 }
1427
1428 static void prepare_packed_git_mru(void)