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)
12 #include "ui-shared.h"
15 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)
37 struct cgit_repo *r = (struct cgit_repo *)repo;
39 if (repo->mtime != -1) {
43 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
44 if (stat(path, &s) == 0) {
45 *mtime = read_agefile(path);
50 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
51 repo->defbranch : "master");
52 if (stat(path, &s) == 0) {
58 path = fmt("%s/%s", repo->path, "packed-refs");
59 if (stat(path, &s) == 0) {
67 return (r->mtime != 0);
70 static void print_modtime(struct cgit_repo *repo)
73 if (get_repo_modtime(repo, &t))
74 cgit_print_age(t, -1, NULL);
77 int is_match(struct cgit_repo *repo)
81 if (repo->url && strcasestr(repo->url, ctx.qry.search))
83 if (repo->name && strcasestr(repo->name, ctx.qry.search))
85 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
87 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
92 int is_in_url(struct cgit_repo *repo)
96 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
101 void print_sort_header(const char *title, const char *sort)
103 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
104 if (ctx.qry.search) {
106 html_url_arg(ctx.qry.search);
108 htmlf("'>%s</a></th>", title);
111 void print_header(int columns)
113 html("<tr class='nohover'>");
114 print_sort_header("Name", "name");
115 print_sort_header("Description", "desc");
116 print_sort_header("Owner", "owner");
117 print_sort_header("Idle", "idle");
118 if (ctx.cfg.enable_index_links)
119 html("<th class='left'>Links</th>");
124 void print_pager(int items, int pagelen, char *search, char *sort)
127 html("<div class='pager'>");
128 for(i = 0; i * pagelen < items; i++)
129 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
130 search, sort, i * pagelen);
134 static int cmp(const char *s1, const char *s2)
137 if (ctx.cfg.case_sensitive_sort)
138 return strcmp(s1, s2);
140 return strcasecmp(s1, s2);
149 static int sort_section(const void *a, const void *b)
151 const struct cgit_repo *r1 = a;
152 const struct cgit_repo *r2 = b;
156 result = cmp(r1->section, r2->section);
158 if (!strcmp(ctx.cfg.section_sort, "age")) {
159 // get_repo_modtime caches the value in r->mtime, so we don't
160 // have to worry about inefficiencies here.
161 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
162 result = r2->mtime - r1->mtime;
165 result = cmp(r1->name, r2->name);
170 static int sort_name(const void *a, const void *b)
172 const struct cgit_repo *r1 = a;
173 const struct cgit_repo *r2 = b;
175 return cmp(r1->name, r2->name);
178 static int sort_desc(const void *a, const void *b)
180 const struct cgit_repo *r1 = a;
181 const struct cgit_repo *r2 = b;
183 return cmp(r1->desc, r2->desc);
186 static int sort_owner(const void *a, const void *b)
188 const struct cgit_repo *r1 = a;
189 const struct cgit_repo *r2 = b;
191 return cmp(r1->owner, r2->owner);
194 static int sort_idle(const void *a, const void *b)
196 const struct cgit_repo *r1 = a;
197 const struct cgit_repo *r2 = b;
201 get_repo_modtime(r1, &t1);
202 get_repo_modtime(r2, &t2);
208 int (*fn)(const void *a, const void *b);
211 struct sortcolumn sortcolumn[] = {
212 {"section", sort_section},
215 {"owner", sort_owner},
220 int sort_repolist(char *field)
222 struct sortcolumn *column;
224 for (column = &sortcolumn[0]; column->name; column++) {
225 if (strcmp(field, column->name))
227 qsort(cgit_repolist.repos, cgit_repolist.count,
228 sizeof(struct cgit_repo), column->fn);
235 void cgit_print_repolist()
237 int i, columns = 4, hits = 0, header = 0;
238 char *last_section = NULL;
242 if (ctx.cfg.enable_index_links)
245 ctx.page.title = ctx.cfg.root_title;
246 cgit_print_http_headers(&ctx);
247 cgit_print_docstart(&ctx);
248 cgit_print_pageheader(&ctx);
250 if (ctx.cfg.index_header)
251 html_include(ctx.cfg.index_header);
254 sorted = sort_repolist(ctx.qry.sort);
256 sort_repolist("section");
258 html("<table summary='repository list' class='list nowrap'>");
259 for (i=0; i<cgit_repolist.count; i++) {
260 ctx.repo = &cgit_repolist.repos[i];
261 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
264 if (hits <= ctx.qry.ofs)
266 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
269 print_header(columns);
270 section = ctx.repo->section;
271 if (section && !strcmp(section, ""))
274 ((last_section == NULL && section != NULL) ||
275 (last_section != NULL && section == NULL) ||
276 (last_section != NULL && section != NULL &&
277 strcmp(section, last_section)))) {
278 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
282 last_section = section;
284 htmlf("<tr><td class='%s'>",
285 !sorted && section ? "sublevel-repo" : "toplevel-repo");
286 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
288 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
289 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
292 html_txt(ctx.repo->owner);
294 print_modtime(ctx.repo);
296 if (ctx.cfg.enable_index_links) {
298 cgit_summary_link("summary", NULL, "button", NULL);
299 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
300 0, NULL, NULL, ctx.qry.showmsg);
301 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
308 cgit_print_error("No repositories found");
309 else if (hits > ctx.cfg.max_repo_count)
310 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
314 void cgit_print_site_readme()
316 if (!ctx.cfg.root_readme)
318 if (ctx.cfg.about_filter)
319 cgit_open_filter(ctx.cfg.about_filter);
320 html_include(ctx.cfg.root_readme);
321 if (ctx.cfg.about_filter)
322 cgit_close_filter(ctx.cfg.about_filter);