1 /* ui-repolist.c: functions for generating the repolist page
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)
10 #include "ui-repolist.h"
12 #include "ui-shared.h"
14 static time_t read_agefile(char *path)
19 struct strbuf date_buf = STRBUF_INIT;
21 if (readfile(path, &buf, &size)) {
26 if (parse_date(buf, &date_buf) == 0)
27 result = strtoul(date_buf.buf, NULL, 10);
31 strbuf_release(&date_buf);
35 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
37 struct strbuf path = STRBUF_INIT;
39 struct cgit_repo *r = (struct cgit_repo *)repo;
41 if (repo->mtime != -1) {
45 strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile);
46 if (stat(path.buf, &s) == 0) {
47 *mtime = read_agefile(path.buf);
55 strbuf_addf(&path, "%s/refs/heads/%s", repo->path,
56 repo->defbranch ? repo->defbranch : "master");
57 if (stat(path.buf, &s) == 0) {
64 strbuf_addf(&path, "%s/%s", repo->path, "packed-refs");
65 if (stat(path.buf, &s) == 0) {
74 strbuf_release(&path);
75 return (r->mtime != 0);
78 static void print_modtime(struct cgit_repo *repo)
81 if (get_repo_modtime(repo, &t))
82 cgit_print_age(t, -1, NULL);
85 static int is_match(struct cgit_repo *repo)
89 if (repo->url && strcasestr(repo->url, ctx.qry.search))
91 if (repo->name && strcasestr(repo->name, ctx.qry.search))
93 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
95 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
100 static int is_in_url(struct cgit_repo *repo)
104 if (repo->url && starts_with(repo->url, ctx.qry.url))
109 static void print_sort_header(const char *title, const char *sort)
111 char *currenturl = cgit_currenturl();
112 html("<th class='left'><a href='");
113 html_attr(currenturl);
114 htmlf("?s=%s", sort);
115 if (ctx.qry.search) {
117 html_url_arg(ctx.qry.search);
119 htmlf("'>%s</a></th>", title);
123 static void print_header(void)
125 html("<tr class='nohover'>");
126 print_sort_header("Name", "name");
127 print_sort_header("Description", "desc");
128 if (ctx.cfg.enable_index_owner)
129 print_sort_header("Owner", "owner");
130 print_sort_header("Idle", "idle");
131 if (ctx.cfg.enable_index_links)
132 html("<th class='left'>Links</th>");
137 static void print_pager(int items, int pagelen, char *search, char *sort)
141 html("<ul class='pager'>");
142 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
143 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
145 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
146 class, search, sort, ofs, 0);
152 static int cmp(const char *s1, const char *s2)
155 if (ctx.cfg.case_sensitive_sort)
156 return strcmp(s1, s2);
158 return strcasecmp(s1, s2);
167 static int sort_section(const void *a, const void *b)
169 const struct cgit_repo *r1 = a;
170 const struct cgit_repo *r2 = b;
174 result = cmp(r1->section, r2->section);
176 if (!strcmp(ctx.cfg.repository_sort, "age")) {
177 // get_repo_modtime caches the value in r->mtime, so we don't
178 // have to worry about inefficiencies here.
179 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
180 result = r2->mtime - r1->mtime;
183 result = cmp(r1->name, r2->name);
188 static int sort_name(const void *a, const void *b)
190 const struct cgit_repo *r1 = a;
191 const struct cgit_repo *r2 = b;
193 return cmp(r1->name, r2->name);
196 static int sort_desc(const void *a, const void *b)
198 const struct cgit_repo *r1 = a;
199 const struct cgit_repo *r2 = b;
201 return cmp(r1->desc, r2->desc);
204 static int sort_owner(const void *a, const void *b)
206 const struct cgit_repo *r1 = a;
207 const struct cgit_repo *r2 = b;
209 return cmp(r1->owner, r2->owner);
212 static int sort_idle(const void *a, const void *b)
214 const struct cgit_repo *r1 = a;
215 const struct cgit_repo *r2 = b;
219 get_repo_modtime(r1, &t1);
220 get_repo_modtime(r2, &t2);
226 int (*fn)(const void *a, const void *b);
229 static const struct sortcolumn sortcolumn[] = {
230 {"section", sort_section},
233 {"owner", sort_owner},
238 static int sort_repolist(char *field)
240 const struct sortcolumn *column;
242 for (column = &sortcolumn[0]; column->name; column++) {
243 if (strcmp(field, column->name))
245 qsort(cgit_repolist.repos, cgit_repolist.count,
246 sizeof(struct cgit_repo), column->fn);
253 void cgit_print_repolist(void)
255 int i, columns = 3, hits = 0, header = 0;
256 char *last_section = NULL;
260 if (ctx.cfg.enable_index_links)
262 if (ctx.cfg.enable_index_owner)
265 ctx.page.title = ctx.cfg.root_title;
266 cgit_print_http_headers();
267 cgit_print_docstart();
268 cgit_print_pageheader();
270 if (ctx.cfg.index_header)
271 html_include(ctx.cfg.index_header);
274 sorted = sort_repolist(ctx.qry.sort);
275 else if (ctx.cfg.section_sort)
276 sort_repolist("section");
278 html("<table summary='repository list' class='list nowrap'>");
279 for (i = 0; i < cgit_repolist.count; i++) {
280 ctx.repo = &cgit_repolist.repos[i];
281 if (ctx.repo->hide || ctx.repo->ignore)
283 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
286 if (hits <= ctx.qry.ofs)
288 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
292 section = ctx.repo->section;
293 if (section && !strcmp(section, ""))
296 ((last_section == NULL && section != NULL) ||
297 (last_section != NULL && section == NULL) ||
298 (last_section != NULL && section != NULL &&
299 strcmp(section, last_section)))) {
300 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
304 last_section = section;
306 htmlf("<tr><td class='%s'>",
307 !sorted && section ? "sublevel-repo" : "toplevel-repo");
308 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
310 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
311 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
314 if (ctx.cfg.enable_index_owner) {
315 if (ctx.repo->owner_filter) {
316 cgit_open_filter(ctx.repo->owner_filter);
317 html_txt(ctx.repo->owner);
318 cgit_close_filter(ctx.repo->owner_filter);
321 html_attr(cgit_currenturl());
323 html_url_arg(ctx.repo->owner);
325 html_txt(ctx.repo->owner);
330 print_modtime(ctx.repo);
332 if (ctx.cfg.enable_index_links) {
334 cgit_summary_link("summary", NULL, "button", NULL);
335 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
336 0, NULL, NULL, ctx.qry.showmsg, 0);
337 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
344 cgit_print_error("No repositories found");
345 else if (hits > ctx.cfg.max_repo_count)
346 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
350 void cgit_print_site_readme(void)
352 cgit_print_layout_start();
353 if (!ctx.cfg.root_readme)
355 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
356 html_include(ctx.cfg.root_readme);
357 cgit_close_filter(ctx.cfg.about_filter);
359 cgit_print_layout_end();