Diff options
Diffstat (limited to 'source/ui-refs.c')
| -rw-r--r-- | source/ui-refs.c | 92 |
1 file changed, 85 insertions, 7 deletions
diff --git a/source/ui-refs.c b/source/ui-refs.c index 4099225..a99ba65 100644 --- a/source/ui-refs.c +++ b/source/ui-refs.c @@ -12,10 +12,26 @@ #include "cgit.h" #include "filter.h" #include "html.h" +#include "parsing.h" #include "shared.h" #include "ui-refs.h" #include "ui-shared.h" +struct refinfo { + const char *refname; + struct object *object; + union { + struct taginfo *tag; + struct commitinfo *commit; + }; +}; + +struct reflist { + struct refinfo **refs; + int alloc; + int count; +}; + // The slice of a sorted ref list that one page shows, with end one past the // last row. size is what a full page holds, so it also decides whether the // list needs a pager. @@ -25,6 +41,68 @@ struct ref_page { int end; }; +static void free_refinfo(struct refinfo *ref) +{ + free((char *)ref->refname); + switch (ref->object->type) { + case OBJ_TAG: + cgit_free_taginfo(ref->tag); + break; + case OBJ_COMMIT: + cgit_free_commitinfo(ref->commit); + break; + } + free(ref); +} + +static struct refinfo *make_refinfo(const char *refname, const struct object_id *oid) +{ + struct refinfo *ref; + + ref = xcalloc(1, sizeof(struct refinfo)); + ref->refname = xstrdup(refname); + ref->object = parse_object(the_repository, oid); + if (!ref->object) { + free((char *)ref->refname); + free(ref); + return NULL; + } + switch (ref->object->type) { + case OBJ_TAG: + ref->tag = cgit_parse_tag((struct tag *)ref->object); + break; + case OBJ_COMMIT: + ref->commit = cgit_parse_commit((struct commit *)ref->object); + break; + } + return ref; +} + +static void free_reflist_inner(struct reflist *list) +{ + int i; + + for (i = 0; i < list->count; i++) + free_refinfo(list->refs[i]); + free(list->refs); +} + +/* + * Matches git's reference iteration callback, and appends each ref it is given + * to the struct reflist passed as cb_data. + */ +static int refs_cb(const struct reference *ref, void *cb_data) +{ + struct reflist *list = (struct reflist *)cb_data; + struct refinfo *info = make_refinfo(ref->name, ref->oid); + + if (info) { + ALLOC_GROW(list->refs, list->count + 1, list->alloc); + list->refs[list->count++] = info; + } + return 0; +} + /* * The tagger date and the committer date live in a union in struct refinfo and * only the member matching the object type is ever filled, so assuming a ref @@ -69,9 +147,9 @@ static void collect_branches(struct reflist *list) { list->refs = NULL; list->alloc = list->count = 0; - refs_for_each_branch_ref(get_main_ref_store(the_repository), cgit_refs_cb, list); + refs_for_each_branch_ref(get_main_ref_store(the_repository), refs_cb, list); if (ctx.repo->enable_remote_branches) - refs_for_each_remote_ref(get_main_ref_store(the_repository), cgit_refs_cb, list); + refs_for_each_remote_ref(get_main_ref_store(the_repository), refs_cb, list); } /* @@ -132,7 +210,7 @@ static void collect_tags(struct reflist *list) { list->refs = NULL; list->alloc = list->count = 0; - refs_for_each_tag_ref(get_main_ref_store(the_repository), cgit_refs_cb, list); + refs_for_each_tag_ref(get_main_ref_store(the_repository), refs_cb, list); } static void print_tag_header(int columns) @@ -266,7 +344,7 @@ static void print_branches_page(int pagesize) print_ref_pager(page.start, page.size, list.count, "heads"); html("</tbody>\n"); - cgit_free_reflist_inner(&list); + free_reflist_inner(&list); } static void print_tags_page(int pagesize) @@ -290,7 +368,7 @@ static void print_tags_page(int pagesize) print_ref_pager(page.start, page.size, list.count, "tags"); html("</tbody>\n"); - cgit_free_reflist_inner(&list); + free_reflist_inner(&list); } void cgit_print_branches(int maxcount, int columns) @@ -318,7 +396,7 @@ void cgit_print_branches(int maxcount, int columns) print_refs_link("heads", columns); html("</tbody>\n"); - cgit_free_reflist_inner(&list); + free_reflist_inner(&list); } void cgit_print_tags(int maxcount, int columns) @@ -340,7 +418,7 @@ void cgit_print_tags(int maxcount, int columns) print_refs_link("tags", columns); html("</tbody>\n"); - cgit_free_reflist_inner(&list); + free_reflist_inner(&list); } void cgit_print_refs(void) |
