1 /* ui-repolist.c: functions for generating the repolist page
3 * Copyright (C) 2006 Lars Hjemli
4 * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
11 #include "ui-repolist.h"
13 #include "ui-shared.h"
16 static time_t read_agefile(char *path)
23 if (readfile(path, &buf, &size))
26 if (parse_date(buf, buf2, sizeof(buf2)) > 0)
27 result = strtoul(buf2, NULL, 10);
34 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
38 struct cgit_repo *r = (struct cgit_repo *)repo;
40 if (repo->mtime != -1) {
44 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
45 if (stat(path, &s) == 0) {
46 *mtime = read_agefile(path);
53 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
54 repo->defbranch : "master");
55 if (stat(path, &s) == 0) {
61 path = fmt("%s/%s", repo->path, "packed-refs");
62 if (stat(path, &s) == 0) {
70 return (r->mtime != 0);
73 static void print_modtime(struct cgit_repo *repo)
76 if (get_repo_modtime(repo, &t))
77 cgit_print_age(t, -1, NULL);
80 static int is_match(struct cgit_repo *repo)
84 if (repo->url && strcasestr(repo->url, ctx.qry.search))
86 if (repo->name && strcasestr(repo->name, ctx.qry.search))
88 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
90 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
95 static int is_in_url(struct cgit_repo *repo)
99 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
104 static void print_sort_header(const char *title, const char *sort)
106 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
107 if (ctx.qry.search) {
109 html_url_arg(ctx.qry.search);
111 htmlf("'>%s</a></th>", title);
114 static void print_header()
116 html("<tr class='nohover'>");
117 print_sort_header("Name", "name");
118 print_sort_header("Description", "desc");
119 if (ctx.cfg.enable_index_owner)
120 print_sort_header("Owner", "owner");
121 print_sort_header("Idle", "idle");
122 if (ctx.cfg.enable_index_links)
123 html("<th class='left'>Links</th>");
128 static void print_pager(int items, int pagelen, char *search, char *sort)
132 html("<ul class='pager'>");
133 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
134 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
136 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
137 class, search, sort, ofs);
143 static int cmp(const char *s1, const char *s2)
146 if (ctx.cfg.case_sensitive_sort)
147 return strcmp(s1, s2);
149 return strcasecmp(s1, s2);
158 static int sort_section(const void *a, const void *b)
160 const struct cgit_repo *r1 = a;
161 const struct cgit_repo *r2 = b;
165 result = cmp(r1->section, r2->section);
167 if (!strcmp(ctx.cfg.repository_sort, "age")) {
168 // get_repo_modtime caches the value in r->mtime, so we don't
169 // have to worry about inefficiencies here.
170 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
171 result = r2->mtime - r1->mtime;
174 result = cmp(r1->name, r2->name);
179 static int sort_name(const void *a, const void *b)
181 const struct cgit_repo *r1 = a;
182 const struct cgit_repo *r2 = b;
184 return cmp(r1->name, r2->name);
187 static int sort_desc(const void *a, const void *b)
189 const struct cgit_repo *r1 = a;
190 const struct cgit_repo *r2 = b;
192 return cmp(r1->desc, r2->desc);
195 static int sort_owner(const void *a, const void *b)
197 const struct cgit_repo *r1 = a;
198 const struct cgit_repo *r2 = b;
200 return cmp(r1->owner, r2->owner);
203 static int sort_idle(const void *a, const void *b)
205 const struct cgit_repo *r1 = a;
206 const struct cgit_repo *r2 = b;
210 get_repo_modtime(r1, &t1);
211 get_repo_modtime(r2, &t2);
217 int (*fn)(const void *a, const void *b);
220 struct sortcolumn sortcolumn[] = {
221 {"section", sort_section},
224 {"owner", sort_owner},
229 static int sort_repolist(char *field)
231 struct sortcolumn *column;
233 for (column = &sortcolumn[0]; column->name; column++) {
234 if (strcmp(field, column->name))
236 qsort(cgit_repolist.repos, cgit_repolist.count,
237 sizeof(struct cgit_repo), column->fn);
244 void cgit_print_repolist()
246 int i, columns = 3, hits = 0, header = 0;
247 char *last_section = NULL;
251 if (ctx.cfg.enable_index_links)
253 if (ctx.cfg.enable_index_owner)
256 ctx.page.title = ctx.cfg.root_title;
257 cgit_print_http_headers(&ctx);
258 cgit_print_docstart(&ctx);
259 cgit_print_pageheader(&ctx);
261 if (ctx.cfg.index_header)
262 html_include(ctx.cfg.index_header);
265 sorted = sort_repolist(ctx.qry.sort);
266 else if (ctx.cfg.section_sort)
267 sort_repolist("section");
269 html("<table summary='repository list' class='list nowrap'>");
270 for (i = 0; i < cgit_repolist.count; i++) {
271 ctx.repo = &cgit_repolist.repos[i];
272 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
275 if (hits <= ctx.qry.ofs)
277 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
281 section = ctx.repo->section;
282 if (section && !strcmp(section, ""))
285 ((last_section == NULL && section != NULL) ||
286 (last_section != NULL && section == NULL) ||
287 (last_section != NULL && section != NULL &&
288 strcmp(section, last_section)))) {
289 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
293 last_section = section;
295 htmlf("<tr><td class='%s'>",
296 !sorted && section ? "sublevel-repo" : "toplevel-repo");
297 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
299 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
300 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
303 if (ctx.cfg.enable_index_owner) {
304 html_txt(ctx.repo->owner);
307 print_modtime(ctx.repo);
309 if (ctx.cfg.enable_index_links) {
311 cgit_summary_link("summary", NULL, "button", NULL);
312 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
313 0, NULL, NULL, ctx.qry.showmsg);
314 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
321 cgit_print_error("No repositories found");
322 else if (hits > ctx.cfg.max_repo_count)
323 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
327 void cgit_print_site_readme()
329 if (!ctx.cfg.root_readme)
331 if (ctx.cfg.about_filter)
332 cgit_open_filter(ctx.cfg.about_filter);
333 html_include(ctx.cfg.root_readme);
334 if (ctx.cfg.about_filter)
335 cgit_close_filter(ctx.cfg.about_filter);