blob: e945f6733270f83f654a3204a75edb42a0156f68 [file] [log] [blame]
Lars Hjemli74620f12006-12-11 16:48:03 +01001/* ui-repolist.c: functions for generating the repolist page
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli74620f12006-12-11 16:48:03 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01009#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-repolist.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Jason A. Donenfeld184c5652012-07-12 19:13:39 +020013#include <strings.h>
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020014
Lukas Fleischerbafab422013-03-04 08:52:33 +010015static time_t read_agefile(char *path)
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020016{
Lars Hjemlie16f1782009-08-18 17:17:41 +020017 time_t result;
18 size_t size;
19 char *buf;
Christian Hesse17838ec2014-12-19 00:28:34 -070020 struct strbuf date_buf = STRBUF_INIT;
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020021
Lars Hjemlie16f1782009-08-18 17:17:41 +020022 if (readfile(path, &buf, &size))
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020023 return -1;
Lars Hjemlie16f1782009-08-18 17:17:41 +020024
Christian Hesse17838ec2014-12-19 00:28:34 -070025 if (parse_date(buf, &date_buf) == 0)
26 result = strtoul(date_buf.buf, NULL, 10);
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020027 else
Lars Hjemlie16f1782009-08-18 17:17:41 +020028 result = 0;
29 free(buf);
Christian Hesse17838ec2014-12-19 00:28:34 -070030 strbuf_release(&date_buf);
Lars Hjemlie16f1782009-08-18 17:17:41 +020031 return result;
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020032}
33
Lars Hjemlicbac02c2008-11-29 13:33:02 +010034static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020035{
John Keepingfb3655d2013-04-06 10:28:57 +010036 struct strbuf path = STRBUF_INIT;
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020037 struct stat s;
Lars Hjemli88131702008-11-29 16:46:37 +010038 struct cgit_repo *r = (struct cgit_repo *)repo;
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020039
Lars Hjemli88131702008-11-29 16:46:37 +010040 if (repo->mtime != -1) {
41 *mtime = repo->mtime;
42 return 1;
43 }
John Keepingfb3655d2013-04-06 10:28:57 +010044 strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile);
45 if (stat(path.buf, &s) == 0) {
46 *mtime = read_agefile(path.buf);
Ferry Hubertse52a5222012-10-09 13:09:58 +020047 if (*mtime) {
48 r->mtime = *mtime;
John Keepingfb3655d2013-04-06 10:28:57 +010049 goto end;
Ferry Hubertse52a5222012-10-09 13:09:58 +020050 }
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020051 }
52
John Keepingfb3655d2013-04-06 10:28:57 +010053 strbuf_reset(&path);
54 strbuf_addf(&path, "%s/refs/heads/%s", repo->path,
55 repo->defbranch ? repo->defbranch : "master");
56 if (stat(path.buf, &s) == 0) {
Lars Hjemlicbac02c2008-11-29 13:33:02 +010057 *mtime = s.st_mtime;
Ferry Huberts21e0e0b2011-05-13 23:09:34 +020058 r->mtime = *mtime;
John Keepingfb3655d2013-04-06 10:28:57 +010059 goto end;
Ferry Huberts21e0e0b2011-05-13 23:09:34 +020060 }
Lars Hjemli88131702008-11-29 16:46:37 +010061
John Keepingfb3655d2013-04-06 10:28:57 +010062 strbuf_reset(&path);
63 strbuf_addf(&path, "%s/%s", repo->path, "packed-refs");
64 if (stat(path.buf, &s) == 0) {
Ferry Huberts21e0e0b2011-05-13 23:09:34 +020065 *mtime = s.st_mtime;
66 r->mtime = *mtime;
John Keepingfb3655d2013-04-06 10:28:57 +010067 goto end;
Ferry Huberts21e0e0b2011-05-13 23:09:34 +020068 }
69
70 *mtime = 0;
Lars Hjemli88131702008-11-29 16:46:37 +010071 r->mtime = *mtime;
John Keepingfb3655d2013-04-06 10:28:57 +010072end:
73 strbuf_release(&path);
Lars Hjemli88131702008-11-29 16:46:37 +010074 return (r->mtime != 0);
Lars Hjemlicbac02c2008-11-29 13:33:02 +010075}
76
77static void print_modtime(struct cgit_repo *repo)
78{
79 time_t t;
80 if (get_repo_modtime(repo, &t))
81 cgit_print_age(t, -1, NULL);
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020082}
Lars Hjemli74620f12006-12-11 16:48:03 +010083
Lukas Fleischerbafab422013-03-04 08:52:33 +010084static int is_match(struct cgit_repo *repo)
Lars Hjemli74620f12006-12-11 16:48:03 +010085{
Lars Hjemli536b0542008-04-13 11:57:10 +020086 if (!ctx.qry.search)
87 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020088 if (repo->url && strcasestr(repo->url, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020089 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020090 if (repo->name && strcasestr(repo->name, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020091 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020092 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020093 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020094 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020095 return 1;
96 return 0;
97}
Lars Hjemli74620f12006-12-11 16:48:03 +010098
Lukas Fleischerbafab422013-03-04 08:52:33 +010099static int is_in_url(struct cgit_repo *repo)
Lars Hjemlidd80ef52008-09-14 20:18:10 +0200100{
101 if (!ctx.qry.url)
102 return 1;
Christian Hesse79c985e2014-05-29 17:35:46 +0200103 if (repo->url && starts_with(repo->url, ctx.qry.url))
Lars Hjemlidd80ef52008-09-14 20:18:10 +0200104 return 1;
105 return 0;
106}
107
Lukas Fleischerbafab422013-03-04 08:52:33 +0100108static void print_sort_header(const char *title, const char *sort)
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100109{
John Keeping1de65912014-01-12 19:45:17 +0000110 html("<th class='left'><a href='");
Jason A. Donenfeld6bcda2f2015-03-03 17:18:42 +0100111 html_attr(cgit_currenturl());
John Keeping1de65912014-01-12 19:45:17 +0000112 htmlf("?s=%s", sort);
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100113 if (ctx.qry.search) {
William Bellc366bd62012-10-09 20:45:58 +0200114 html("&amp;q=");
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100115 html_url_arg(ctx.qry.search);
116 }
117 htmlf("'>%s</a></th>", title);
118}
119
Lukas Fleischerbafab422013-03-04 08:52:33 +0100120static void print_header()
Lars Hjemli536b0542008-04-13 11:57:10 +0200121{
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100122 html("<tr class='nohover'>");
123 print_sort_header("Name", "name");
124 print_sort_header("Description", "desc");
Florian Pritzb1e172a2013-02-01 10:59:13 +0100125 if (ctx.cfg.enable_index_owner)
126 print_sort_header("Owner", "owner");
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100127 print_sort_header("Idle", "idle");
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100128 if (ctx.cfg.enable_index_links)
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200129 html("<th class='left'>Links</th>");
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200130 html("</tr>\n");
Lars Hjemli536b0542008-04-13 11:57:10 +0200131}
Lars Hjemli74620f12006-12-11 16:48:03 +0100132
Lars Hjemlic6078b82008-05-03 10:54:39 +0200133
Lukas Fleischerbafab422013-03-04 08:52:33 +0100134static void print_pager(int items, int pagelen, char *search, char *sort)
Lars Hjemlic6078b82008-05-03 10:54:39 +0200135{
Jamie Couture4675b912012-10-08 12:49:34 -0400136 int i, ofs;
137 char *class = NULL;
Lukas Fleischerb60e6bf2013-03-07 08:56:22 +0100138 html("<ul class='pager'>");
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500139 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
Jamie Couture4675b912012-10-08 12:49:34 -0400140 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
Lukas Fleischerb60e6bf2013-03-07 08:56:22 +0100141 html("<li>");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100142 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100143 class, search, sort, ofs, 0);
Lukas Fleischerb60e6bf2013-03-07 08:56:22 +0100144 html("</li>");
Jamie Couture4675b912012-10-08 12:49:34 -0400145 }
Lukas Fleischerb60e6bf2013-03-07 08:56:22 +0100146 html("</ul>");
Lars Hjemlic6078b82008-05-03 10:54:39 +0200147}
148
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100149static int cmp(const char *s1, const char *s2)
150{
Jason A. Donenfeld184c5652012-07-12 19:13:39 +0200151 if (s1 && s2) {
152 if (ctx.cfg.case_sensitive_sort)
153 return strcmp(s1, s2);
154 else
155 return strcasecmp(s1, s2);
156 }
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100157 if (s1 && !s2)
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100158 return -1;
Lars Hjemli54272e62008-11-29 14:27:35 +0100159 if (s2 && !s1)
160 return 1;
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100161 return 0;
162}
163
Lars Hjemli63816ec2009-08-23 23:09:31 +0200164static int sort_section(const void *a, const void *b)
165{
166 const struct cgit_repo *r1 = a;
167 const struct cgit_repo *r2 = b;
168 int result;
Jason A. Donenfeld184c5652012-07-12 19:13:39 +0200169 time_t t;
Lars Hjemli63816ec2009-08-23 23:09:31 +0200170
171 result = cmp(r1->section, r2->section);
Jason A. Donenfeld184c5652012-07-12 19:13:39 +0200172 if (!result) {
Jason A. Donenfeldfdfb6a62012-10-16 23:32:40 +0200173 if (!strcmp(ctx.cfg.repository_sort, "age")) {
Jason A. Donenfeld184c5652012-07-12 19:13:39 +0200174 // get_repo_modtime caches the value in r->mtime, so we don't
175 // have to worry about inefficiencies here.
176 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
177 result = r2->mtime - r1->mtime;
178 }
179 if (!result)
180 result = cmp(r1->name, r2->name);
181 }
Lars Hjemli63816ec2009-08-23 23:09:31 +0200182 return result;
183}
184
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100185static int sort_name(const void *a, const void *b)
186{
187 const struct cgit_repo *r1 = a;
188 const struct cgit_repo *r2 = b;
189
190 return cmp(r1->name, r2->name);
191}
192
193static int sort_desc(const void *a, const void *b)
194{
195 const struct cgit_repo *r1 = a;
196 const struct cgit_repo *r2 = b;
197
198 return cmp(r1->desc, r2->desc);
199}
200
201static int sort_owner(const void *a, const void *b)
202{
203 const struct cgit_repo *r1 = a;
204 const struct cgit_repo *r2 = b;
205
206 return cmp(r1->owner, r2->owner);
207}
208
209static int sort_idle(const void *a, const void *b)
Benjamin Closed71c0c72008-11-25 06:25:35 -0800210{
211 const struct cgit_repo *r1 = a;
212 const struct cgit_repo *r2 = b;
Benjamin Closed71c0c72008-11-25 06:25:35 -0800213 time_t t1, t2;
Benjamin Closed71c0c72008-11-25 06:25:35 -0800214
Lars Hjemlicbac02c2008-11-29 13:33:02 +0100215 t1 = t2 = 0;
216 get_repo_modtime(r1, &t1);
217 get_repo_modtime(r2, &t2);
218 return t2 - t1;
Benjamin Closed71c0c72008-11-25 06:25:35 -0800219}
220
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100221struct sortcolumn {
222 const char *name;
223 int (*fn)(const void *a, const void *b);
224};
225
226struct sortcolumn sortcolumn[] = {
Lars Hjemli63816ec2009-08-23 23:09:31 +0200227 {"section", sort_section},
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100228 {"name", sort_name},
229 {"desc", sort_desc},
230 {"owner", sort_owner},
231 {"idle", sort_idle},
232 {NULL, NULL}
233};
234
Lukas Fleischerbafab422013-03-04 08:52:33 +0100235static int sort_repolist(char *field)
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100236{
237 struct sortcolumn *column;
238
239 for (column = &sortcolumn[0]; column->name; column++) {
240 if (strcmp(field, column->name))
241 continue;
242 qsort(cgit_repolist.repos, cgit_repolist.count,
243 sizeof(struct cgit_repo), column->fn);
244 return 1;
245 }
246 return 0;
247}
248
249
Lars Hjemli536b0542008-04-13 11:57:10 +0200250void cgit_print_repolist()
251{
Florian Pritzb1e172a2013-02-01 10:59:13 +0100252 int i, columns = 3, hits = 0, header = 0;
Lars Hjemlie7af0022009-08-23 22:58:39 +0200253 char *last_section = NULL;
Lars Hjemlie01229c2009-08-24 07:42:03 +0200254 char *section;
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100255 int sorted = 0;
Lars Hjemli536b0542008-04-13 11:57:10 +0200256
257 if (ctx.cfg.enable_index_links)
Florian Pritzb1e172a2013-02-01 10:59:13 +0100258 ++columns;
259 if (ctx.cfg.enable_index_owner)
260 ++columns;
Lars Hjemli536b0542008-04-13 11:57:10 +0200261
262 ctx.page.title = ctx.cfg.root_title;
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100263 cgit_print_http_headers();
264 cgit_print_docstart();
265 cgit_print_pageheader();
Lars Hjemli536b0542008-04-13 11:57:10 +0200266
Lars Hjemli80628172008-04-29 00:35:49 +0200267 if (ctx.cfg.index_header)
268 html_include(ctx.cfg.index_header);
269
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500270 if (ctx.qry.sort)
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100271 sorted = sort_repolist(ctx.qry.sort);
Tobias Bieniek7a4e7c82012-10-09 20:53:29 +0200272 else if (ctx.cfg.section_sort)
Lars Hjemli63816ec2009-08-23 23:09:31 +0200273 sort_repolist("section");
Benjamin Closed71c0c72008-11-25 06:25:35 -0800274
Lars Hjemli536b0542008-04-13 11:57:10 +0200275 html("<table summary='repository list' class='list nowrap'>");
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100276 for (i = 0; i < cgit_repolist.count; i++) {
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100277 ctx.repo = &cgit_repolist.repos[i];
Lukas Fleischerc58cec92015-01-29 12:52:49 +0100278 if (ctx.repo->hide || ctx.repo->ignore)
279 continue;
Lars Hjemlidd80ef52008-09-14 20:18:10 +0200280 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
Lars Hjemli536b0542008-04-13 11:57:10 +0200281 continue;
Lars Hjemlic6078b82008-05-03 10:54:39 +0200282 hits++;
283 if (hits <= ctx.qry.ofs)
284 continue;
285 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
286 continue;
Lars Hjemli536b0542008-04-13 11:57:10 +0200287 if (!header++)
Florian Pritzb1e172a2013-02-01 10:59:13 +0100288 print_header();
Lars Hjemlie01229c2009-08-24 07:42:03 +0200289 section = ctx.repo->section;
290 if (section && !strcmp(section, ""))
291 section = NULL;
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100292 if (!sorted &&
Lars Hjemlie01229c2009-08-24 07:42:03 +0200293 ((last_section == NULL && section != NULL) ||
294 (last_section != NULL && section == NULL) ||
295 (last_section != NULL && section != NULL &&
296 strcmp(section, last_section)))) {
Lars Hjemlie7af0022009-08-23 22:58:39 +0200297 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200298 columns);
Lars Hjemlie01229c2009-08-24 07:42:03 +0200299 html_txt(section);
Lars Hjemli5877c492007-05-18 22:48:22 +0200300 html("</td></tr>");
Lars Hjemlie01229c2009-08-24 07:42:03 +0200301 last_section = section;
Lars Hjemli5877c492007-05-18 22:48:22 +0200302 }
Lars Hjemli0b8b6a32007-05-21 00:14:28 +0200303 htmlf("<tr><td class='%s'>",
Lars Hjemlie01229c2009-08-24 07:42:03 +0200304 !sorted && section ? "sublevel-repo" : "toplevel-repo");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200305 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
Lars Hjemli74620f12006-12-11 16:48:03 +0100306 html("</td><td>");
Lars Hjemlie9a70422008-04-14 22:23:48 +0200307 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100308 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
Lars Hjemlie9a70422008-04-14 22:23:48 +0200309 html_link_close();
Lars Hjemli74620f12006-12-11 16:48:03 +0100310 html("</td><td>");
Florian Pritzb1e172a2013-02-01 10:59:13 +0100311 if (ctx.cfg.enable_index_owner) {
Chris Burroughs96ceb9a2014-08-04 09:23:08 -0400312 if (ctx.repo->owner_filter) {
313 cgit_open_filter(ctx.repo->owner_filter);
314 html_txt(ctx.repo->owner);
315 cgit_close_filter(ctx.repo->owner_filter);
316 } else {
317 html("<a href='");
Jason A. Donenfeld6bcda2f2015-03-03 17:18:42 +0100318 html_attr(cgit_currenturl());
Jason A. Donenfelde14eee92015-03-03 16:53:11 +0100319 html("?q=");
Chris Burroughs96ceb9a2014-08-04 09:23:08 -0400320 html_url_arg(ctx.repo->owner);
321 html("'>");
322 html_txt(ctx.repo->owner);
323 html("</a>");
324 }
Florian Pritzb1e172a2013-02-01 10:59:13 +0100325 html("</td><td>");
326 }
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100327 print_modtime(ctx.repo);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200328 html("</td>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100329 if (ctx.cfg.enable_index_links) {
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200330 html("<td>");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200331 cgit_summary_link("summary", NULL, "button", NULL);
Lars Hjemli51140312007-11-03 10:42:37 +0100332 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
Lars Hjemli0274b572008-11-29 18:39:41 +0100333 0, NULL, NULL, ctx.qry.showmsg);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200334 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
335 html("</td>");
336 }
337 html("</tr>\n");
Lars Hjemli74620f12006-12-11 16:48:03 +0100338 }
Lars Hjemli74620f12006-12-11 16:48:03 +0100339 html("</table>");
Lars Hjemli536b0542008-04-13 11:57:10 +0200340 if (!hits)
341 cgit_print_error("No repositories found");
Lars Hjemlic6078b82008-05-03 10:54:39 +0200342 else if (hits > ctx.cfg.max_repo_count)
Tobias Grimm7530d942011-07-31 02:44:05 +0200343 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
Lars Hjemli74620f12006-12-11 16:48:03 +0100344 cgit_print_docend();
345}
Lars Hjemlic6431a72008-04-29 01:06:30 +0200346
347void cgit_print_site_readme()
348{
Lars Hjemli537c05f2009-08-09 13:27:21 +0200349 if (!ctx.cfg.root_readme)
350 return;
Jason A. Donenfeld800380d2014-01-13 03:56:50 +0100351 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
Lars Hjemli537c05f2009-08-09 13:27:21 +0200352 html_include(ctx.cfg.root_readme);
Jason A. Donenfeld800380d2014-01-13 03:56:50 +0100353 cgit_close_filter(ctx.cfg.about_filter);
Lars Hjemlic6431a72008-04-29 01:06:30 +0200354}