add sorting infrastructure for list refs

Since we store lists of refs as linked lists, we can use llist_mergesort to efficiently sort them. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 21, 2012 at 18:19 UTC ed81c76bc3a9440e37b3512c9c2b742c6ca92c6f
2 files changed +24
remote.c
+22
@@ -7,6 +7,7 @@
7 #include "dir.h"
8 #include "tag.h"
9 #include "string-list.h"
10 +#include "mergesort.h"
11
12 enum map_direction { FROM_SRC, FROM_DST };
13
@@ -918,6 +919,27 @@ void free_refs(struct ref *ref)
919 }
920 }
921
922 +int ref_compare_name(const void *va, const void *vb)
923 +{
924 + const struct ref *a = va, *b = vb;
925 + return strcmp(a->name, b->name);
926 +}
927 +
928 +static void *ref_list_get_next(const void *a)
929 +{
930 + return ((const struct ref *)a)->next;
931 +}
932 +
933 +static void ref_list_set_next(void *a, void *next)
934 +{
935 + ((struct ref *)a)->next = next;
936 +}
937 +
938 +void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
939 +{
940 + *l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
941 +}
942 +
943 static int count_refspec_match(const char *pattern,
944 struct ref *refs,
945 struct ref **matched_ref)
remote.h
+2
@@ -72,6 +72,8 @@ extern const struct refspec *tag_refspec;
72 struct ref *alloc_ref(const char *name);
73 struct ref *copy_ref(const struct ref *ref);
74 struct ref *copy_ref_list(const struct ref *ref);
75 +void sort_ref_list(struct ref **, int (*cmp)(const void *, const void *));
76 +int ref_compare_name(const void *, const void *);
77
78 int check_ref_type(const struct ref *ref, int flags);
79