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 = NULL;
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 int is_visible(struct cgit_repo *repo)
110{
111 if (repo->hide || repo->ignore)
112 return 0;
113 if (!(is_match(repo) && is_in_url(repo)))
114 return 0;
115 return 1;
116}
117
118static void print_sort_header(const char *title, const char *sort)
119{
120 char *currenturl = cgit_currenturl();
121 html("<th class='left'><a href='");
122 html_attr(currenturl);
123 htmlf("?s=%s", sort);
124 if (ctx.qry.search) {
125 html("&q=");
126 html_url_arg(ctx.qry.search);
127 }
128 htmlf("'>%s</a></th>", title);
129 free(currenturl);
130}
131
132static void print_header(void)
133{
134 html("<tr class='nohover'>");
135 print_sort_header("Name", "name");
136 print_sort_header("Description", "desc");
137 if (ctx.cfg.enable_index_owner)
138 print_sort_header("Owner", "owner");
139 print_sort_header("Idle", "idle");
140 if (ctx.cfg.enable_index_links)
141 html("<th class='left'>Links</th>");
142 html("</tr>\n");
143}
144
145
146static void print_pager(int items, int pagelen, char *search, char *sort)
147{
148 int i, ofs;
149 char *class = NULL;
150 html("<ul class='pager'>");
151 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
152 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
153 html("<li>");
154 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
155 class, search, sort, ofs, 0);
156 html("</li>");
157 }
158 html("</ul>");
159}
160
161static int cmp(const char *s1, const char *s2)
162{
163 if (s1 && s2) {
164 if (ctx.cfg.case_sensitive_sort)
165 return strcmp(s1, s2);
166 else
167 return strcasecmp(s1, s2);
168 }
169 if (s1 && !s2)
170 return -1;
171 if (s2 && !s1)
172 return 1;
173 return 0;
174}
175
176static int sort_section(const void *a, const void *b)
177{
178 const struct cgit_repo *r1 = a;
179 const struct cgit_repo *r2 = b;
180 int result;
181 time_t t;
182
183 result = cmp(r1->section, r2->section);
184 if (!result) {
185 if (!strcmp(ctx.cfg.repository_sort, "age")) {
186 // get_repo_modtime caches the value in r->mtime, so we don't
187 // have to worry about inefficiencies here.
188 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
189 result = r2->mtime - r1->mtime;
190 }
191 if (!result)
192 result = cmp(r1->name, r2->name);
193 }
194 return result;
195}
196
197static int sort_name(const void *a, const void *b)
198{
199 const struct cgit_repo *r1 = a;
200 const struct cgit_repo *r2 = b;
201
202 return cmp(r1->name, r2->name);
203}
204
205static int sort_desc(const void *a, const void *b)
206{
207 const struct cgit_repo *r1 = a;
208 const struct cgit_repo *r2 = b;
209
210 return cmp(r1->desc, r2->desc);
211}
212
213static int sort_owner(const void *a, const void *b)
214{
215 const struct cgit_repo *r1 = a;
216 const struct cgit_repo *r2 = b;
217
218 return cmp(r1->owner, r2->owner);
219}
220
221static int sort_idle(const void *a, const void *b)
222{
223 const struct cgit_repo *r1 = a;
224 const struct cgit_repo *r2 = b;
225 time_t t1, t2;
226
227 t1 = t2 = 0;
228 get_repo_modtime(r1, &t1);
229 get_repo_modtime(r2, &t2);
230 return t2 - t1;
231}
232
233struct sortcolumn {
234 const char *name;
235 int (*fn)(const void *a, const void *b);
236};
237
238static const struct sortcolumn sortcolumn[] = {
239 {"section", sort_section},
240 {"name", sort_name},
241 {"desc", sort_desc},
242 {"owner", sort_owner},
243 {"idle", sort_idle},
244 {NULL, NULL}
245};
246
247static int sort_repolist(char *field)
248{
249 const struct sortcolumn *column;
250
251 for (column = &sortcolumn[0]; column->name; column++) {
252 if (strcmp(field, column->name))
253 continue;
254 qsort(cgit_repolist.repos, cgit_repolist.count,
255 sizeof(struct cgit_repo), column->fn);
256 return 1;
257 }
258 return 0;
259}
260
261
262void cgit_print_repolist(void)
263{
264 int i, columns = 3, hits = 0, header = 0;
265 char *last_section = NULL;
266 char *section;
267 int sorted = 0;
268
269 if (ctx.cfg.enable_index_links)
270 ++columns;
271 if (ctx.cfg.enable_index_owner)
272 ++columns;
273
274 ctx.page.title = ctx.cfg.root_title;
275 cgit_print_http_headers();
276 cgit_print_docstart();
277 cgit_print_pageheader();
278
279 if (ctx.cfg.index_header)
280 html_include(ctx.cfg.index_header);
281
282 if (ctx.qry.sort)
283 sorted = sort_repolist(ctx.qry.sort);
284 else if (ctx.cfg.section_sort)
285 sort_repolist("section");
286
287 html("<table summary='repository list' class='list nowrap'>");
288 for (i = 0; i < cgit_repolist.count; i++) {
289 ctx.repo = &cgit_repolist.repos[i];
290 if (!is_visible(ctx.repo))
291 continue;
292 hits++;
293 if (hits <= ctx.qry.ofs)
294 continue;
295 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
296 continue;
297 if (!header++)
298 print_header();
299 section = ctx.repo->section;
300 if (section && !strcmp(section, ""))
301 section = NULL;
302 if (!sorted &&
303 ((last_section == NULL && section != NULL) ||
304 (last_section != NULL && section == NULL) ||
305 (last_section != NULL && section != NULL &&
306 strcmp(section, last_section)))) {
307 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
308 columns);
309 html_txt(section);
310 html("</td></tr>");
311 last_section = section;
312 }
313 htmlf("<tr><td class='%s'>",
314 !sorted && section ? "sublevel-repo" : "toplevel-repo");
315 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
316 html("</td><td>");
317 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
318 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
319 html_link_close();
320 html("</td><td>");
321 if (ctx.cfg.enable_index_owner) {
322 if (ctx.repo->owner_filter) {
323 cgit_open_filter(ctx.repo->owner_filter);
324 html_txt(ctx.repo->owner);
325 cgit_close_filter(ctx.repo->owner_filter);
326 } else {
327 html("<a href='");
328 html_attr(cgit_currenturl());
329 html("?q=");
330 html_url_arg(ctx.repo->owner);
331 html("'>");
332 html_txt(ctx.repo->owner);
333 html("</a>");
334 }
335 html("</td><td>");
336 }
337 print_modtime(ctx.repo);
338 html("</td>");
339 if (ctx.cfg.enable_index_links) {
340 html("<td>");
341 cgit_summary_link("summary", NULL, "button", NULL);
342 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
343 0, NULL, NULL, ctx.qry.showmsg, 0);
344 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
345 html("</td>");
346 }
347 html("</tr>\n");
348 }
349 html("</table>");
350 if (!hits)
351 cgit_print_error("No repositories found");
352 else if (hits > ctx.cfg.max_repo_count)
353 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
354 cgit_print_docend();
355}
356
357void cgit_print_site_readme(void)
358{
359 cgit_print_layout_start();
360 if (!ctx.cfg.root_readme)
361 goto done;
362 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
363 html_include(ctx.cfg.root_readme);
364 cgit_close_filter(ctx.cfg.about_filter);
365done:
366 cgit_print_layout_end();
367}