Skip to content

Commit

Permalink
add sorting infrastructure for list refs
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff King authored and Junio C Hamano committed May 22, 2012
1 parent 7db8d53 commit ed81c76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "dir.h"
#include "tag.h"
#include "string-list.h"
#include "mergesort.h"

enum map_direction { FROM_SRC, FROM_DST };

Expand Down Expand Up @@ -918,6 +919,27 @@ void free_refs(struct ref *ref)
}
}

int ref_compare_name(const void *va, const void *vb)
{
const struct ref *a = va, *b = vb;
return strcmp(a->name, b->name);
}

static void *ref_list_get_next(const void *a)
{
return ((const struct ref *)a)->next;
}

static void ref_list_set_next(void *a, void *next)
{
((struct ref *)a)->next = next;
}

void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
{
*l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
}

static int count_refspec_match(const char *pattern,
struct ref *refs,
struct ref **matched_ref)
Expand Down
2 changes: 2 additions & 0 deletions remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ extern const struct refspec *tag_refspec;
struct ref *alloc_ref(const char *name);
struct ref *copy_ref(const struct ref *ref);
struct ref *copy_ref_list(const struct ref *ref);
void sort_ref_list(struct ref **, int (*cmp)(const void *, const void *));
int ref_compare_name(const void *, const void *);

int check_ref_type(const struct ref *ref, int flags);

Expand Down

0 comments on commit ed81c76

Please sign in to comment.