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 html("<th class='left'><a href='");
112 html_attr(cgit_currenturl());
113 htmlf("?s=%s", sort);
114 if (ctx.qry.search) {
116 html_url_arg(ctx.qry.search);
118 htmlf("'>%s</a></th>", title);
121 static void print_header(void)
123 html("<tr class='nohover'>");
124 print_sort_header("Name", "name");
125 print_sort_header("Description", "desc");
126 if (ctx.cfg.enable_index_owner)
127 print_sort_header("Owner", "owner");
128 print_sort_header("Idle", "idle");
129 if (ctx.cfg.enable_index_links)
130 html("<th class='left'>Links</th>");
135 static void print_pager(int items, int pagelen, char *search, char *sort)
139 html("<ul class='pager'>");
140 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
141 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
143 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
144 class, search, sort, ofs, 0);
150 static int cmp(const char *s1, const char *s2)
153 if (ctx.cfg.case_sensitive_sort)
154 return strcmp(s1, s2);
156 return strcasecmp(s1, s2);
165 static int sort_section(const void *a, const void *b)
167 const struct cgit_repo *r1 = a;
168 const struct cgit_repo *r2 = b;
172 result = cmp(r1->section, r2->section);
174 if (!strcmp(ctx.cfg.repository_sort, "age")) {
175 // get_repo_modtime caches the value in r->mtime, so we don't
176 // have to worry about inefficiencies here.
177 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
178 result = r2->mtime - r1->mtime;
181 result = cmp(r1->name, r2->name);
186 static int sort_name(const void *a, const void *b)
188 const struct cgit_repo *r1 = a;
189 const struct cgit_repo *r2 = b;
191 return cmp(r1->name, r2->name);
194 static int sort_desc(const void *a, const void *b)
196 const struct cgit_repo *r1 = a;
197 const struct cgit_repo *r2 = b;
199 return cmp(r1->desc, r2->desc);
202 static int sort_owner(const void *a, const void *b)
204 const struct cgit_repo *r1 = a;
205 const struct cgit_repo *r2 = b;
207 return cmp(r1->owner, r2->owner);
210 static int sort_idle(const void *a, const void *b)
212 const struct cgit_repo *r1 = a;
213 const struct cgit_repo *r2 = b;
217 get_repo_modtime(r1, &t1);
218 get_repo_modtime(r2, &t2);
224 int (*fn)(const void *a, const void *b);
227 static const struct sortcolumn sortcolumn[] = {
228 {"section", sort_section},
231 {"owner", sort_owner},
236 static int sort_repolist(char *field)
238 const struct sortcolumn *column;
240 for (column = &sortcolumn[0]; column->name; column++) {
241 if (strcmp(field, column->name))
243 qsort(cgit_repolist.repos, cgit_repolist.count,
244 sizeof(struct cgit_repo), column->fn);
251 void cgit_print_repolist(void)
253 int i, columns = 3, hits = 0, header = 0;
254 char *last_section = NULL;
258 if (ctx.cfg.enable_index_links)
260 if (ctx.cfg.enable_index_owner)
263 ctx.page.title = ctx.cfg.root_title;
264 cgit_print_http_headers();
265 cgit_print_docstart();
266 cgit_print_pageheader();
268 if (ctx.cfg.index_header)
269 html_include(ctx.cfg.index_header);
272 sorted = sort_repolist(ctx.qry.sort);
273 else if (ctx.cfg.section_sort)
274 sort_repolist("section");
276 html("<table summary='repository list' class='list nowrap'>");
277 for (i = 0; i < cgit_repolist.count; i++) {
278 ctx.repo = &cgit_repolist.repos[i];
279 if (ctx.repo->hide || ctx.repo->ignore)
281 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
284 if (hits <= ctx.qry.ofs)
286 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
290 section = ctx.repo->section;
291 if (section && !strcmp(section, ""))
294 ((last_section == NULL && section != NULL) ||
295 (last_section != NULL && section == NULL) ||
296 (last_section != NULL && section != NULL &&
297 strcmp(section, last_section)))) {
298 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
302 last_section = section;
304 htmlf("<tr><td class='%s'>",
305 !sorted && section ? "sublevel-repo" : "toplevel-repo");
306 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
308 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
309 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
312 if (ctx.cfg.enable_index_owner) {
313 if (ctx.repo->owner_filter) {
314 cgit_open_filter(ctx.repo->owner_filter);
315 html_txt(ctx.repo->owner);
316 cgit_close_filter(ctx.repo->owner_filter);
319 html_attr(cgit_currenturl());
321 html_url_arg(ctx.repo->owner);
323 html_txt(ctx.repo->owner);
328 print_modtime(ctx.repo);
330 if (ctx.cfg.enable_index_links) {
332 cgit_summary_link("summary", NULL, "button", NULL);
333 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
334 0, NULL, NULL, ctx.qry.showmsg, 0);
335 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
342 cgit_print_error("No repositories found");
343 else if (hits > ctx.cfg.max_repo_count)
344 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
348 void cgit_print_site_readme(void)
350 cgit_print_layout_start();
351 if (!ctx.cfg.root_readme)
353 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
354 html_include(ctx.cfg.root_readme);
355 cgit_close_filter(ctx.cfg.about_filter);
357 cgit_print_layout_end();