ui-repolist.c (view raw)
1/* ui-repolist.c: functions for generating the repolist page
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10#include "ui-repolist.h"
11#include "html.h"
12#include "ui-shared.h"
13
14static time_t read_agefile(char *path)
15{
16 time_t result;
17 size_t size;
18 char *buf;
19 struct strbuf date_buf = STRBUF_INIT;
20
21 if (readfile(path, &buf, &size)) {
22 free(buf);
23 return -1;
24 }
25
26 if (parse_date(buf, &date_buf) == 0)
27 result = strtoul(date_buf.buf, NULL, 10);
28 else
29 result = 0;
30 free(buf);
31 strbuf_release(&date_buf);
32 return result;
33}
34
35static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
36{
37 struct strbuf path = STRBUF_INIT;
38 struct stat s;
39 struct cgit_repo *r = (struct cgit_repo *)repo;
40
41 if (repo->mtime != -1) {
42 *mtime = repo->mtime;
43 return 1;
44 }
45 strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile);
46 if (stat(path.buf, &s) == 0) {
47 *mtime = read_agefile(path.buf);
48 if (*mtime) {
49 r->mtime = *mtime;
50 goto end;
51 }
52 }
53
54 strbuf_reset(&path);
55 strbuf_addf(&path, "%s/refs/heads/%s", repo->path,
56 repo->defbranch ? repo->defbranch : "master");
57 if (stat(path.buf, &s) == 0) {
58 *mtime = s.st_mtime;
59 r->mtime = *mtime;
60 goto end;
61 }
62
63 strbuf_reset(&path);
64 strbuf_addf(&path, "%s/%s", repo->path, "packed-refs");
65 if (stat(path.buf, &s) == 0) {
66 *mtime = s.st_mtime;
67 r->mtime = *mtime;
68 goto end;
69 }
70
71 *mtime = 0;
72 r->mtime = *mtime;
73end:
74 strbuf_release(&path);
75 return (r->mtime != 0);
76}
77
78static void print_modtime(struct cgit_repo *repo)
79{
80 time_t t;
81 if (get_repo_modtime(repo, &t))
82 cgit_print_age(t, -1, NULL);
83}
84
85static int is_match(struct cgit_repo *repo)
86{
87 if (!ctx.qry.search)
88 return 1;
89 if (repo->url && strcasestr(repo->url, ctx.qry.search))
90 return 1;
91 if (repo->name && strcasestr(repo->name, ctx.qry.search))
92 return 1;
93 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
94 return 1;
95 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
96 return 1;
97 return 0;
98}
99
100static int is_in_url(struct cgit_repo *repo)
101{
102 if (!ctx.qry.url)
103 return 1;
104 if (repo->url && starts_with(repo->url, ctx.qry.url))
105 return 1;
106 return 0;
107}
108
109static void print_sort_header(const char *title, const char *sort)
110{
111 html("<th class='left'><a href='");
112 html_attr(cgit_currenturl());
113 htmlf("?s=%s", sort);
114 if (ctx.qry.search) {
115 html("&q=");
116 html_url_arg(ctx.qry.search);
117 }
118 htmlf("'>%s</a></th>", title);
119}
120
121static void print_header(void)
122{
123 html("<tr class='nohover'>");
124 print_sort_header("Name", "name");
125 print_sort_header("Description", "desc");
126 if (ctx.cfg.enable_index_owner)
127 print_sort_header("Owner", "owner");
128 print_sort_header("Idle", "idle");
129 if (ctx.cfg.enable_index_links)
130 html("<th class='left'>Links</th>");
131 html("</tr>\n");
132}
133
134
135static void print_pager(int items, int pagelen, char *search, char *sort)
136{
137 int i, ofs;
138 char *class = NULL;
139 html("<ul class='pager'>");
140 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
141 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
142 html("<li>");
143 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
144 class, search, sort, ofs, 0);
145 html("</li>");
146 }
147 html("</ul>");
148}
149
150static int cmp(const char *s1, const char *s2)
151{
152 if (s1 && s2) {
153 if (ctx.cfg.case_sensitive_sort)
154 return strcmp(s1, s2);
155 else
156 return strcasecmp(s1, s2);
157 }
158 if (s1 && !s2)
159 return -1;
160 if (s2 && !s1)
161 return 1;
162 return 0;
163}
164
165static int sort_section(const void *a, const void *b)
166{
167 const struct cgit_repo *r1 = a;
168 const struct cgit_repo *r2 = b;
169 int result;
170 time_t t;
171
172 result = cmp(r1->section, r2->section);
173 if (!result) {
174 if (!strcmp(ctx.cfg.repository_sort, "age")) {
175 // get_repo_modtime caches the value in r->mtime, so we don't
176 // have to worry about inefficiencies here.
177 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
178 result = r2->mtime - r1->mtime;
179 }
180 if (!result)
181 result = cmp(r1->name, r2->name);
182 }
183 return result;
184}
185
186static int sort_name(const void *a, const void *b)
187{
188 const struct cgit_repo *r1 = a;
189 const struct cgit_repo *r2 = b;
190
191 return cmp(r1->name, r2->name);
192}
193
194static int sort_desc(const void *a, const void *b)
195{
196 const struct cgit_repo *r1 = a;
197 const struct cgit_repo *r2 = b;
198
199 return cmp(r1->desc, r2->desc);
200}
201
202static int sort_owner(const void *a, const void *b)
203{
204 const struct cgit_repo *r1 = a;
205 const struct cgit_repo *r2 = b;
206
207 return cmp(r1->owner, r2->owner);
208}
209
210static int sort_idle(const void *a, const void *b)
211{
212 const struct cgit_repo *r1 = a;
213 const struct cgit_repo *r2 = b;
214 time_t t1, t2;
215
216 t1 = t2 = 0;
217 get_repo_modtime(r1, &t1);
218 get_repo_modtime(r2, &t2);
219 return t2 - t1;
220}
221
222struct sortcolumn {
223 const char *name;
224 int (*fn)(const void *a, const void *b);
225};
226
227static const struct sortcolumn sortcolumn[] = {
228 {"section", sort_section},
229 {"name", sort_name},
230 {"desc", sort_desc},
231 {"owner", sort_owner},
232 {"idle", sort_idle},
233 {NULL, NULL}
234};
235
236static int sort_repolist(char *field)
237{
238 const struct sortcolumn *column;
239
240 for (column = &sortcolumn[0]; column->name; column++) {
241 if (strcmp(field, column->name))
242 continue;
243 qsort(cgit_repolist.repos, cgit_repolist.count,
244 sizeof(struct cgit_repo), column->fn);
245 return 1;
246 }
247 return 0;
248}
249
250
251void cgit_print_repolist(void)
252{
253 int i, columns = 3, hits = 0, header = 0;
254 char *last_section = NULL;
255 char *section;
256 int sorted = 0;
257
258 if (ctx.cfg.enable_index_links)
259 ++columns;
260 if (ctx.cfg.enable_index_owner)
261 ++columns;
262
263 ctx.page.title = ctx.cfg.root_title;
264 cgit_print_http_headers();
265 cgit_print_docstart();
266 cgit_print_pageheader();
267
268 if (ctx.cfg.index_header)
269 html_include(ctx.cfg.index_header);
270
271 if (ctx.qry.sort)
272 sorted = sort_repolist(ctx.qry.sort);
273 else if (ctx.cfg.section_sort)
274 sort_repolist("section");
275
276 html("<table summary='repository list' class='list nowrap'>");
277 for (i = 0; i < cgit_repolist.count; i++) {
278 ctx.repo = &cgit_repolist.repos[i];
279 if (ctx.repo->hide || ctx.repo->ignore)
280 continue;
281 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
282 continue;
283 hits++;
284 if (hits <= ctx.qry.ofs)
285 continue;
286 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
287 continue;
288 if (!header++)
289 print_header();
290 section = ctx.repo->section;
291 if (section && !strcmp(section, ""))
292 section = NULL;
293 if (!sorted &&
294 ((last_section == NULL && section != NULL) ||
295 (last_section != NULL && section == NULL) ||
296 (last_section != NULL && section != NULL &&
297 strcmp(section, last_section)))) {
298 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
299 columns);
300 html_txt(section);
301 html("</td></tr>");
302 last_section = section;
303 }
304 htmlf("<tr><td class='%s'>",
305 !sorted && section ? "sublevel-repo" : "toplevel-repo");
306 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
307 html("</td><td>");
308 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
309 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
310 html_link_close();
311 html("</td><td>");
312 if (ctx.cfg.enable_index_owner) {
313 if (ctx.repo->owner_filter) {
314 cgit_open_filter(ctx.repo->owner_filter);
315 html_txt(ctx.repo->owner);
316 cgit_close_filter(ctx.repo->owner_filter);
317 } else {
318 html("<a href='");
319 html_attr(cgit_currenturl());
320 html("?q=");
321 html_url_arg(ctx.repo->owner);
322 html("'>");
323 html_txt(ctx.repo->owner);
324 html("</a>");
325 }
326 html("</td><td>");
327 }
328 print_modtime(ctx.repo);
329 html("</td>");
330 if (ctx.cfg.enable_index_links) {
331 html("<td>");
332 cgit_summary_link("summary", NULL, "button", NULL);
333 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
334 0, NULL, NULL, ctx.qry.showmsg, 0);
335 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
336 html("</td>");
337 }
338 html("</tr>\n");
339 }
340 html("</table>");
341 if (!hits)
342 cgit_print_error("No repositories found");
343 else if (hits > ctx.cfg.max_repo_count)
344 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
345 cgit_print_docend();
346}
347
348void cgit_print_site_readme(void)
349{
350 cgit_print_layout_start();
351 if (!ctx.cfg.root_readme)
352 goto done;
353 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
354 html_include(ctx.cfg.root_readme);
355 cgit_close_filter(ctx.cfg.about_filter);
356done:
357 cgit_print_layout_end();
358}