make got_ref_list() return a sorted list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
diff --git a/lib/reference.c b/lib/reference.c
index 653c077..617c921 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -460,32 +460,39 @@ insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
{
const struct got_error *err;
struct got_object_id *id;
- struct got_reflist_entry *entry;
+ struct got_reflist_entry *re, *prev = NULL;
+ int cmp;
/*
* We must de-duplicate entries on insert because packed-refs may
* contain redundant entries. On-disk refs take precedence.
* This code assumes that on-disk revs are read before packed-refs.
+ * We're iterating the list anyway, so insert elements sorted by name.
*/
- SIMPLEQ_FOREACH(entry, refs, entry) {
- /* Check for duplicates. */
- if (strcmp(got_ref_get_name(entry->ref),
- got_ref_get_name(ref)) == 0) {
+ SIMPLEQ_FOREACH(re, refs, entry) {
+ cmp = strcmp(got_ref_get_name(re->ref), got_ref_get_name(ref));
+ if (cmp == 0) {
free(ref);
return NULL;
- }
- /* TODO: sort list while here */
+ } else if (cmp > 0)
+ break;
+ else
+ prev = re;
}
err = got_ref_resolve(&id, repo, ref);
if (err)
return err;
- entry = malloc(sizeof(*entry));
- if (entry == NULL)
+ re = malloc(sizeof(*re));
+ if (re == NULL)
return got_error_from_errno();
- entry->ref = ref;
- entry->id = id;
- SIMPLEQ_INSERT_TAIL(refs, entry, entry);
+ re->ref = ref;
+ re->id = id;
+ if (prev)
+ SIMPLEQ_INSERT_AFTER(refs, prev, re, entry);
+ else
+ SIMPLEQ_INSERT_HEAD(refs, re, entry);
+
return NULL;
}
@@ -610,7 +617,6 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
}
}
}
-
done:
free(path_refs);
if (f)