commit 2d4975924ce5391b01ebd64d11499b485e19aace from: Stefan Sperling date: Sat Nov 20 10:40:33 2021 UTC implement got_reflist_sort() which sorts a ref list in-place commit - 0309152af410dec50680c7a54ae55ba8ebd7fcae commit + 2d4975924ce5391b01ebd64d11499b485e19aace blob - 927c10b5f2d5bd38831138167a2c77e989ca9afe blob + dedfb8f826bb8de777445aaff9b19bc8a855d36e --- include/got_reference.h +++ include/got_reference.h @@ -140,6 +140,11 @@ const struct got_error * got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs, struct got_reference *ref, got_ref_cmp_cb cmp_cb, void *cmp_arg); +/* Sort a list of references with the provided comparison callback. */ +const struct got_error * +got_reflist_sort(struct got_reflist_head *refs, got_ref_cmp_cb cmp_cb, + void *cmp_arg); + /* Indicate whether the provided reference is symbolic (points at another * refernce) or not (points at an object ID). */ int got_ref_is_symbolic(struct got_reference *); blob - 436888f0bb009a383e1d3509e00ed610638cf521 blob + f1b5c18ee6c6d5106abc8da5c362e522d47b099d --- lib/reference.c +++ lib/reference.c @@ -889,7 +889,32 @@ got_reflist_insert(struct got_reflist_entry **newp, st TAILQ_INSERT_HEAD(refs, new, entry); return NULL; } + +const struct got_error * +got_reflist_sort(struct got_reflist_head *refs, + got_ref_cmp_cb cmp_cb, void *cmp_arg) +{ + const struct got_error *err = NULL; + struct got_reflist_entry *re, *tmp, *new; + struct got_reflist_head sorted; + TAILQ_INIT(&sorted); + + TAILQ_FOREACH_SAFE(re, refs, entry, tmp) { + struct got_reference *ref = re->ref; + TAILQ_REMOVE(refs, re, entry); + free(re); + err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg); + if (err || new == NULL /* duplicate */) + got_ref_close(ref); + if (err) + return err; + } + + TAILQ_CONCAT(refs, &sorted, entry); + return NULL; +} + static const struct got_error * gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs, const char *subdir, struct got_repository *repo,