1 /* ui-refs.c: browse symbolic refs
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
9 #define USE_THE_REPOSITORY_VARIABLE
14 #include "ui-shared.h"
16 static inline int cmp_age(int age1, int age2)
18 /* age1 and age2 are assumed to be non-negative */
22 static int cmp_ref_name(const void *a, const void *b)
24 struct refinfo *r1 = *(struct refinfo **)a;
25 struct refinfo *r2 = *(struct refinfo **)b;
27 return strcmp(r1->refname, r2->refname);
30 static int cmp_branch_age(const void *a, const void *b)
32 struct refinfo *r1 = *(struct refinfo **)a;
33 struct refinfo *r2 = *(struct refinfo **)b;
35 return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
38 static int get_ref_age(struct refinfo *ref)
42 switch (ref->object->type) {
44 return ref->tag ? ref->tag->tagger_date : 0;
46 return ref->commit ? ref->commit->committer_date : 0;
51 static int cmp_tag_age(const void *a, const void *b)
53 struct refinfo *r1 = *(struct refinfo **)a;
54 struct refinfo *r2 = *(struct refinfo **)b;
56 return cmp_age(get_ref_age(r1), get_ref_age(r2));
59 static int print_branch(struct refinfo *ref)
61 struct commitinfo *info = ref->commit;
62 char *name = (char *)ref->refname;
67 cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL,
71 if (ref->object->type == OBJ_COMMIT) {
72 cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL);
74 cgit_open_filter(ctx.repo->email_filter, info->author_email, "refs");
75 html_txt(info->author);
76 cgit_close_filter(ctx.repo->email_filter);
77 html("</td><td colspan='2'>");
78 cgit_print_age(info->committer_date, info->committer_tz, -1);
80 html("</td><td></td><td>");
81 cgit_object_link(ref->object);
87 static void print_tag_header(void)
89 html("<tr class='nohover'><th class='left'>Tag</th>"
90 "<th class='left'>Download</th>"
91 "<th class='left'>Author</th>"
92 "<th class='left' colspan='2'>Age</th></tr>\n");
95 static int print_tag(struct refinfo *ref)
97 struct tag *tag = NULL;
98 struct taginfo *info = NULL;
99 char *name = (char *)ref->refname;
100 struct object *obj = ref->object;
102 if (obj->type == OBJ_TAG) {
103 tag = (struct tag *)obj;
111 cgit_tag_link(name, NULL, NULL, name);
113 if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
114 cgit_print_snapshot_links(ctx.repo, name, " ");
116 cgit_object_link(obj);
120 cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
121 html_txt(info->tagger);
122 cgit_close_filter(ctx.repo->email_filter);
124 } else if (ref->object->type == OBJ_COMMIT) {
125 cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
126 html_txt(ref->commit->author);
127 cgit_close_filter(ctx.repo->email_filter);
129 html("</td><td colspan='2'>");
131 if (info->tagger_date > 0)
132 cgit_print_age(info->tagger_date, info->tagger_tz, -1);
133 } else if (ref->object->type == OBJ_COMMIT) {
134 cgit_print_age(ref->commit->commit->date, 0, -1);
136 html("</td></tr>\n");
141 static void print_refs_link(const char *path)
143 html("<tr class='nohover'><td colspan='5'>");
144 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
148 void cgit_print_branches(int maxcount)
153 html("<tr class='nohover'><th class='left'>Branch</th>"
154 "<th class='left'>Commit message</th>"
155 "<th class='left'>Author</th>"
156 "<th class='left' colspan='2'>Age</th></tr>\n");
159 list.alloc = list.count = 0;
160 refs_for_each_branch_ref(get_main_ref_store(the_repository),
161 cgit_refs_cb, &list);
162 if (ctx.repo->enable_remote_branches)
163 refs_for_each_remote_ref(get_main_ref_store(the_repository),
164 cgit_refs_cb, &list);
166 if (maxcount == 0 || maxcount > list.count)
167 maxcount = list.count;
169 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
170 if (ctx.repo->branch_sort == 0)
171 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
173 for (i = 0; i < maxcount; i++)
174 print_branch(list.refs[i]);
176 if (maxcount < list.count)
177 print_refs_link("heads");
179 cgit_free_reflist_inner(&list);
182 void cgit_print_tags(int maxcount)
188 list.alloc = list.count = 0;
189 refs_for_each_tag_ref(get_main_ref_store(the_repository),
190 cgit_refs_cb, &list);
193 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
195 maxcount = list.count;
196 else if (maxcount > list.count)
197 maxcount = list.count;
199 for (i = 0; i < maxcount; i++)
200 print_tag(list.refs[i]);
202 if (maxcount < list.count)
203 print_refs_link("tags");
205 cgit_free_reflist_inner(&list);
208 void cgit_print_refs(void)
210 cgit_print_layout_start();
211 html("<table class='list nowrap'>");
213 if (ctx.qry.path && starts_with(ctx.qry.path, "heads"))
214 cgit_print_branches(0);
215 else if (ctx.qry.path && starts_with(ctx.qry.path, "tags"))
218 cgit_print_branches(0);
219 html("<tr class='nohover'><td colspan='5'> </td></tr>");
223 cgit_print_layout_end();