cgit: prepare repo before error pages
[cgit.git] / ui-refs.c
blob75f2789fc1653b76509f2ff3f8a9120051dd30e6
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)
7 */
9 #include "cgit.h"
10 #include "ui-refs.h"
11 #include "html.h"
12 #include "ui-shared.h"
14 static inline int cmp_age(int age1, int age2)
16 /* age1 and age2 are assumed to be non-negative */
17 return age2 - age1;
20 static int cmp_ref_name(const void *a, const void *b)
22 struct refinfo *r1 = *(struct refinfo **)a;
23 struct refinfo *r2 = *(struct refinfo **)b;
25 return strcmp(r1->refname, r2->refname);
28 static int cmp_branch_age(const void *a, const void *b)
30 struct refinfo *r1 = *(struct refinfo **)a;
31 struct refinfo *r2 = *(struct refinfo **)b;
33 return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
36 static int get_ref_age(struct refinfo *ref)
38 if (!ref->object)
39 return 0;
40 switch (ref->object->type) {
41 case OBJ_TAG:
42 return ref->tag ? ref->tag->tagger_date : 0;
43 case OBJ_COMMIT:
44 return ref->commit ? ref->commit->committer_date : 0;
46 return 0;
49 static int cmp_tag_age(const void *a, const void *b)
51 struct refinfo *r1 = *(struct refinfo **)a;
52 struct refinfo *r2 = *(struct refinfo **)b;
54 return cmp_age(get_ref_age(r1), get_ref_age(r2));
57 static int print_branch(struct refinfo *ref)
59 struct commitinfo *info = ref->commit;
60 char *name = (char *)ref->refname;
62 if (!info)
63 return 1;
64 html("<tr><td>");
65 cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL,
66 ctx.qry.showmsg, 0);
67 html("</td><td>");
69 if (ref->object->type == OBJ_COMMIT) {
70 cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL);
71 html("</td><td>");
72 cgit_open_filter(ctx.repo->email_filter, info->author_email, "refs");
73 html_txt(info->author);
74 cgit_close_filter(ctx.repo->email_filter);
75 html("</td><td colspan='2'>");
76 cgit_print_age(info->committer_date, info->committer_tz, -1);
77 } else {
78 html("</td><td></td><td>");
79 cgit_object_link(ref->object);
81 html("</td></tr>\n");
82 return 0;
85 static void print_tag_header(void)
87 html("<tr class='nohover'><th class='left'>Tag</th>"
88 "<th class='left'>Download</th>"
89 "<th class='left'>Author</th>"
90 "<th class='left' colspan='2'>Age</th></tr>\n");
93 static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
95 const struct cgit_snapshot_format* f;
96 const char *basename;
97 struct strbuf filename = STRBUF_INIT;
98 size_t prefixlen;
100 if (!ref || strlen(ref) < 1)
101 return;
103 basename = cgit_repobasename(repo->url);
104 if (starts_with(ref, basename))
105 strbuf_addstr(&filename, ref);
106 else
107 cgit_compose_snapshot_prefix(&filename, basename, ref);
108 prefixlen = filename.len;
109 for (f = cgit_snapshot_formats; f->suffix; f++) {
110 if (!(repo->snapshots & f->bit))
111 continue;
112 strbuf_setlen(&filename, prefixlen);
113 strbuf_addstr(&filename, f->suffix);
114 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL, filename.buf);
115 html("&nbsp;&nbsp;");
118 strbuf_release(&filename);
121 static int print_tag(struct refinfo *ref)
123 struct tag *tag = NULL;
124 struct taginfo *info = NULL;
125 char *name = (char *)ref->refname;
126 struct object *obj = ref->object;
128 if (obj->type == OBJ_TAG) {
129 tag = (struct tag *)obj;
130 obj = tag->tagged;
131 info = ref->tag;
132 if (!info)
133 return 1;
136 html("<tr><td>");
137 cgit_tag_link(name, NULL, NULL, name);
138 html("</td><td>");
139 if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
140 print_tag_downloads(ctx.repo, name);
141 else
142 cgit_object_link(obj);
143 html("</td><td>");
144 if (info) {
145 if (info->tagger) {
146 cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
147 html_txt(info->tagger);
148 cgit_close_filter(ctx.repo->email_filter);
150 } else if (ref->object->type == OBJ_COMMIT) {
151 cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
152 html_txt(ref->commit->author);
153 cgit_close_filter(ctx.repo->email_filter);
155 html("</td><td colspan='2'>");
156 if (info) {
157 if (info->tagger_date > 0)
158 cgit_print_age(info->tagger_date, info->tagger_tz, -1);
159 } else if (ref->object->type == OBJ_COMMIT) {
160 cgit_print_age(ref->commit->commit->date, 0, -1);
162 html("</td></tr>\n");
164 return 0;
167 static void print_refs_link(char *path)
169 html("<tr class='nohover'><td colspan='5'>");
170 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
171 html("</td></tr>");
174 void cgit_print_branches(int maxcount)
176 struct reflist list;
177 int i;
179 html("<tr class='nohover'><th class='left'>Branch</th>"
180 "<th class='left'>Commit message</th>"
181 "<th class='left'>Author</th>"
182 "<th class='left' colspan='2'>Age</th></tr>\n");
184 list.refs = NULL;
185 list.alloc = list.count = 0;
186 for_each_branch_ref(cgit_refs_cb, &list);
187 if (ctx.repo->enable_remote_branches)
188 for_each_remote_ref(cgit_refs_cb, &list);
190 if (maxcount == 0 || maxcount > list.count)
191 maxcount = list.count;
193 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
194 if (ctx.repo->branch_sort == 0)
195 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
197 for (i = 0; i < maxcount; i++)
198 print_branch(list.refs[i]);
200 if (maxcount < list.count)
201 print_refs_link("heads");
203 cgit_free_reflist_inner(&list);
206 void cgit_print_tags(int maxcount)
208 struct reflist list;
209 int i;
211 list.refs = NULL;
212 list.alloc = list.count = 0;
213 for_each_tag_ref(cgit_refs_cb, &list);
214 if (list.count == 0)
215 return;
216 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
217 if (!maxcount)
218 maxcount = list.count;
219 else if (maxcount > list.count)
220 maxcount = list.count;
221 print_tag_header();
222 for (i = 0; i < maxcount; i++)
223 print_tag(list.refs[i]);
225 if (maxcount < list.count)
226 print_refs_link("tags");
228 cgit_free_reflist_inner(&list);
231 void cgit_print_refs(void)
233 cgit_print_layout_start();
234 html("<table class='list nowrap'>");
236 if (ctx.qry.path && starts_with(ctx.qry.path, "heads"))
237 cgit_print_branches(0);
238 else if (ctx.qry.path && starts_with(ctx.qry.path, "tags"))
239 cgit_print_tags(0);
240 else {
241 cgit_print_branches(0);
242 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
243 cgit_print_tags(0);
245 html("</table>");
246 cgit_print_layout_end();