{! int cgit_refs_cmp_branch_age(const void *a, const void *b); !}
{! int cgit_refs_cmp_ref_name(const void *a, const void *b); !}
{! int cgit_refs_cmp_tag_age(const void *a, const void *b); !}

{% page cgit_print_refs %}
{! page_start(); !}
{! repo_header(); !}
		<main class="max-w-[1280px] mx-auto py-4">{# Main content #}
{! repo_description_panel(); !}
{! repo_summary_bar(); !}
			{% if !ctx.qry.path || starts_with(ctx.qry.path, "heads") %}
			{!
				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);
				qsort(list.refs, list.count, sizeof(*list.refs), cgit_refs_cmp_branch_age);
				if (ctx.repo->branch_sort == 0) {
					qsort(list.refs, list.count, sizeof(*list.refs), cgit_refs_cmp_ref_name);
				}
			!}
			<div class="grid grid-cols-1 border border-gray-300 rounded-md divide-y divide-gray-300">
				{# Branches list #}
				<div class="rounded-t-md bg-gray-50 px-3 py-2 font-semibold text-sm">
					Branches
				</div>
				{% for int i = 0; i < list.count; i++ %}
				{! struct refinfo *ref = list.refs[i]; !}
				{! struct commitinfo *info = ref->commit; !}
				{! char *name = (char *)ref->refname; !}
				{! if (!info) { continue; } !}
				<div class="px-3 py-2">
					<div><a href="{! cgit_shared_repolink_url(NULL, name, NULL); !}" class="text-blue-500 hover:text-blue-600 hover:underline">{{ name }}</a></div>
					{% if ref->object->type == OBJ_COMMIT %}
					<div class="text-sm text-gray-500 flex gap-x-1">
						{{ info->subject }} &middot; Updated {! cgit_print_age(info->committer_date, info->committer_tz, -1); !} ago
						<img src="{! gravatar_url(info->author_email); !}?s=24" class="mt-[-0.2rem]">
						{{ info->author }}
					</div>
					{% endif %}
				</div>
				{% endfor %}
			</div>
			{! cgit_free_reflist_inner(&list); !}
			{% endif %}
			{% if !ctx.qry.path || starts_with(ctx.qry.path, "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);
			!}
			{% if list.count > 0 %}
			{! qsort(list.refs, list.count, sizeof(*list.refs), cgit_refs_cmp_tag_age); !}
			<div class="grid grid-cols-1 border border-gray-300 rounded-md divide-y divide-gray-300 mt-4">
				{# Tags list #}
				<div class="rounded-t-md bg-gray-50 px-3 py-2 font-semibold text-sm">
					Tags
				</div>
				{% for int i = 0; i < list.count; i++ %}
				{!
					struct refinfo *ref = list.refs[i];
					struct tag *tag = NULL;
					struct taginfo *info = NULL;
					char *name = (char *)ref->refname;
					struct object *obj = ref->object;
					
					if (obj->type == OBJ_TAG) {
						tag = (struct tag *)obj;
						obj = tag->tagged;
						info = ref->tag;
						if (!info) { continue; }
					}
				!}
				<div class="px-3 py-2">
					<div><a href="{! cgit_shared_repolink_url(NULL, name, NULL); !}" class="text-blue-500 hover:text-blue-600 hover:underline">{{ name }}</a></div>
					{% if info && (info->tagger_date > 0 || info->tagger) %}
					<div class="text-sm text-gray-500 flex gap-x-1">
						{% if info->tagger_date > 0 %}
						Updated {! cgit_print_age(info->tagger_date, info->tagger_tz, -1); !} ago
						{% endif %}
						{% if info->tagger %}
						<img src="{! gravatar_url(info->tagger_email); !}?s=24" class="mt-[-0.2rem]">
						{{ info->tagger }}
						{% endif %}
					</div>
					{% elif ref->object->type == OBJ_COMMIT %}
					<div class="text-sm text-gray-500 flex gap-x-1">
						Updated {! cgit_print_age(ref->commit->commit->date, 0, -1); !} ago
						<img src="{! gravatar_url(ref->commit->author_email); !}?s=24" class="mt-[-0.2rem]">
						{{ ref->commit->author }}
					</div>
					{% endif %}
				</div>
				{% endfor %}
			</div>
			{! cgit_free_reflist_inner(&list); !}
			{% endif %}{# if list.count > 0 #}
			{% endif %}{# if !ctx.qry.path || starts_with(ctx.qry.path, "tags") #}
		</main>
{! page_end(); !}
{% endpage %}