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"
15 static time_t read_agefile(char *path)
22 if (readfile(path, &buf, &size))
25 if (parse_date(buf, buf2, sizeof(buf2)) > 0)
26 result = strtoul(buf2, NULL, 10);
33 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
35 struct strbuf path = STRBUF_INIT;
37 struct cgit_repo *r = (struct cgit_repo *)repo;
39 if (repo->mtime != -1) {
43 strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile);
44 if (stat(path.buf, &s) == 0) {
45 *mtime = read_agefile(path.buf);
53 strbuf_addf(&path, "%s/refs/heads/%s", repo->path,
54 repo->defbranch ? repo->defbranch : "master");
55 if (stat(path.buf, &s) == 0) {
62 strbuf_addf(&path, "%s/%s", repo->path, "packed-refs");
63 if (stat(path.buf, &s) == 0) {
72 strbuf_release(&path);
73 return (r->mtime != 0);
76 static void print_modtime(struct cgit_repo *repo)
79 if (get_repo_modtime(repo, &t))
80 cgit_print_age(t, -1, NULL);
83 static int is_match(struct cgit_repo *repo)
87 if (repo->url && strcasestr(repo->url, ctx.qry.search))
89 if (repo->name && strcasestr(repo->name, ctx.qry.search))
91 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
93 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
98 static int is_in_url(struct cgit_repo *repo)
102 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
107 static void print_sort_header(const char *title, const char *sort)
109 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
110 if (ctx.qry.search) {
112 html_url_arg(ctx.qry.search);
114 htmlf("'>%s</a></th>", title);
117 static void print_header()
119 html("<tr class='nohover'>");
120 print_sort_header("Name", "name");
121 print_sort_header("Description", "desc");
122 if (ctx.cfg.enable_index_owner)
123 print_sort_header("Owner", "owner");
124 print_sort_header("Idle", "idle");
125 if (ctx.cfg.enable_index_links)
126 html("<th class='left'>Links</th>");
131 static void print_pager(int items, int pagelen, char *search, char *sort)
135 html("<ul class='pager'>");
136 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
137 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
139 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
140 class, search, sort, ofs);
146 static int cmp(const char *s1, const char *s2)
149 if (ctx.cfg.case_sensitive_sort)
150 return strcmp(s1, s2);
152 return strcasecmp(s1, s2);
161 static int sort_section(const void *a, const void *b)
163 const struct cgit_repo *r1 = a;
164 const struct cgit_repo *r2 = b;
168 result = cmp(r1->section, r2->section);
170 if (!strcmp(ctx.cfg.repository_sort, "age")) {
171 // get_repo_modtime caches the value in r->mtime, so we don't
172 // have to worry about inefficiencies here.
173 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
174 result = r2->mtime - r1->mtime;
177 result = cmp(r1->name, r2->name);
182 static int sort_name(const void *a, const void *b)
184 const struct cgit_repo *r1 = a;
185 const struct cgit_repo *r2 = b;
187 return cmp(r1->name, r2->name);
190 static int sort_desc(const void *a, const void *b)
192 const struct cgit_repo *r1 = a;
193 const struct cgit_repo *r2 = b;
195 return cmp(r1->desc, r2->desc);
198 static int sort_owner(const void *a, const void *b)
200 const struct cgit_repo *r1 = a;
201 const struct cgit_repo *r2 = b;
203 return cmp(r1->owner, r2->owner);
206 static int sort_idle(const void *a, const void *b)
208 const struct cgit_repo *r1 = a;
209 const struct cgit_repo *r2 = b;
213 get_repo_modtime(r1, &t1);
214 get_repo_modtime(r2, &t2);
220 int (*fn)(const void *a, const void *b);
223 struct sortcolumn sortcolumn[] = {
224 {"section", sort_section},
227 {"owner", sort_owner},
232 static int sort_repolist(char *field)
234 struct sortcolumn *column;
236 for (column = &sortcolumn[0]; column->name; column++) {
237 if (strcmp(field, column->name))
239 qsort(cgit_repolist.repos, cgit_repolist.count,
240 sizeof(struct cgit_repo), column->fn);
247 void cgit_print_repolist()
249 int i, columns = 3, hits = 0, header = 0;
250 char *last_section = NULL;
254 if (ctx.cfg.enable_index_links)
256 if (ctx.cfg.enable_index_owner)
259 ctx.page.title = ctx.cfg.root_title;
260 cgit_print_http_headers(&ctx);
261 cgit_print_docstart(&ctx);
262 cgit_print_pageheader(&ctx);
264 if (ctx.cfg.index_header)
265 html_include(ctx.cfg.index_header);
268 sorted = sort_repolist(ctx.qry.sort);
269 else if (ctx.cfg.section_sort)
270 sort_repolist("section");
272 html("<table summary='repository list' class='list nowrap'>");
273 for (i = 0; i < cgit_repolist.count; i++) {
274 ctx.repo = &cgit_repolist.repos[i];
275 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
278 if (hits <= ctx.qry.ofs)
280 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
284 section = ctx.repo->section;
285 if (section && !strcmp(section, ""))
288 ((last_section == NULL && section != NULL) ||
289 (last_section != NULL && section == NULL) ||
290 (last_section != NULL && section != NULL &&
291 strcmp(section, last_section)))) {
292 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
296 last_section = section;
298 htmlf("<tr><td class='%s'>",
299 !sorted && section ? "sublevel-repo" : "toplevel-repo");
300 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
302 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
303 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
306 if (ctx.cfg.enable_index_owner) {
307 html_txt(ctx.repo->owner);
310 print_modtime(ctx.repo);
312 if (ctx.cfg.enable_index_links) {
314 cgit_summary_link("summary", NULL, "button", NULL);
315 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
316 0, NULL, NULL, ctx.qry.showmsg);
317 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
324 cgit_print_error("No repositories found");
325 else if (hits > ctx.cfg.max_repo_count)
326 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
330 void cgit_print_site_readme()
332 if (!ctx.cfg.root_readme)
334 if (ctx.cfg.about_filter)
335 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
336 html_include(ctx.cfg.root_readme);
337 if (ctx.cfg.about_filter)
338 cgit_close_filter(ctx.cfg.about_filter);