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 html("<th class='left'><a href='");
110 html_attr(cgit_rooturl());
111 htmlf("?s=%s", sort);
112 if (ctx.qry.search) {
114 html_url_arg(ctx.qry.search);
116 htmlf("'>%s</a></th>", title);
119 static void print_header()
121 html("<tr class='nohover'>");
122 print_sort_header("Name", "name");
123 print_sort_header("Description", "desc");
124 if (ctx.cfg.enable_index_owner)
125 print_sort_header("Owner", "owner");
126 print_sort_header("Idle", "idle");
127 if (ctx.cfg.enable_index_links)
128 html("<th class='left'>Links</th>");
133 static void print_pager(int items, int pagelen, char *search, char *sort)
137 html("<ul class='pager'>");
138 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
139 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
141 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
142 class, search, sort, ofs);
148 static int cmp(const char *s1, const char *s2)
151 if (ctx.cfg.case_sensitive_sort)
152 return strcmp(s1, s2);
154 return strcasecmp(s1, s2);
163 static int sort_section(const void *a, const void *b)
165 const struct cgit_repo *r1 = a;
166 const struct cgit_repo *r2 = b;
170 result = cmp(r1->section, r2->section);
172 if (!strcmp(ctx.cfg.repository_sort, "age")) {
173 // get_repo_modtime caches the value in r->mtime, so we don't
174 // have to worry about inefficiencies here.
175 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
176 result = r2->mtime - r1->mtime;
179 result = cmp(r1->name, r2->name);
184 static int sort_name(const void *a, const void *b)
186 const struct cgit_repo *r1 = a;
187 const struct cgit_repo *r2 = b;
189 return cmp(r1->name, r2->name);
192 static int sort_desc(const void *a, const void *b)
194 const struct cgit_repo *r1 = a;
195 const struct cgit_repo *r2 = b;
197 return cmp(r1->desc, r2->desc);
200 static int sort_owner(const void *a, const void *b)
202 const struct cgit_repo *r1 = a;
203 const struct cgit_repo *r2 = b;
205 return cmp(r1->owner, r2->owner);
208 static int sort_idle(const void *a, const void *b)
210 const struct cgit_repo *r1 = a;
211 const struct cgit_repo *r2 = b;
215 get_repo_modtime(r1, &t1);
216 get_repo_modtime(r2, &t2);
222 int (*fn)(const void *a, const void *b);
225 struct sortcolumn sortcolumn[] = {
226 {"section", sort_section},
229 {"owner", sort_owner},
234 static int sort_repolist(char *field)
236 struct sortcolumn *column;
238 for (column = &sortcolumn[0]; column->name; column++) {
239 if (strcmp(field, column->name))
241 qsort(cgit_repolist.repos, cgit_repolist.count,
242 sizeof(struct cgit_repo), column->fn);
249 void cgit_print_repolist()
251 int i, columns = 3, hits = 0, header = 0;
252 char *last_section = NULL;
256 if (ctx.cfg.enable_index_links)
258 if (ctx.cfg.enable_index_owner)
261 ctx.page.title = ctx.cfg.root_title;
262 cgit_print_http_headers(&ctx);
263 cgit_print_docstart(&ctx);
264 cgit_print_pageheader(&ctx);
266 if (ctx.cfg.index_header)
267 html_include(ctx.cfg.index_header);
270 sorted = sort_repolist(ctx.qry.sort);
271 else if (ctx.cfg.section_sort)
272 sort_repolist("section");
274 html("<table summary='repository list' class='list nowrap'>");
275 for (i = 0; i < cgit_repolist.count; i++) {
276 ctx.repo = &cgit_repolist.repos[i];
277 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
280 if (hits <= ctx.qry.ofs)
282 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
286 section = ctx.repo->section;
287 if (section && !strcmp(section, ""))
290 ((last_section == NULL && section != NULL) ||
291 (last_section != NULL && section == NULL) ||
292 (last_section != NULL && section != NULL &&
293 strcmp(section, last_section)))) {
294 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
298 last_section = section;
300 htmlf("<tr><td class='%s'>",
301 !sorted && section ? "sublevel-repo" : "toplevel-repo");
302 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
304 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
305 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
308 if (ctx.cfg.enable_index_owner) {
309 html_txt(ctx.repo->owner);
312 print_modtime(ctx.repo);
314 if (ctx.cfg.enable_index_links) {
316 cgit_summary_link("summary", NULL, "button", NULL);
317 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
318 0, NULL, NULL, ctx.qry.showmsg);
319 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
326 cgit_print_error("No repositories found");
327 else if (hits > ctx.cfg.max_repo_count)
328 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
332 void cgit_print_site_readme()
334 if (!ctx.cfg.root_readme)
336 if (ctx.cfg.about_filter)
337 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
338 html_include(ctx.cfg.root_readme);
339 if (ctx.cfg.about_filter)
340 cgit_close_filter(ctx.cfg.about_filter);