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);
52 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
53 repo->defbranch : "master");
54 if (stat(path, &s) == 0) {
60 path = fmt("%s/%s", repo->path, "packed-refs");
61 if (stat(path, &s) == 0) {
69 return (r->mtime != 0);
72 static void print_modtime(struct cgit_repo *repo)
75 if (get_repo_modtime(repo, &t))
76 cgit_print_age(t, -1, NULL);
79 int is_match(struct cgit_repo *repo)
83 if (repo->url && strcasestr(repo->url, ctx.qry.search))
85 if (repo->name && strcasestr(repo->name, ctx.qry.search))
87 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
89 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
94 int is_in_url(struct cgit_repo *repo)
98 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
103 void print_sort_header(const char *title, const char *sort)
105 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
106 if (ctx.qry.search) {
108 html_url_arg(ctx.qry.search);
110 htmlf("'>%s</a></th>", title);
113 void print_header(int columns)
115 html("<tr class='nohover'>");
116 print_sort_header("Name", "name");
117 print_sort_header("Description", "desc");
118 print_sort_header("Owner", "owner");
119 print_sort_header("Idle", "idle");
120 if (ctx.cfg.enable_index_links)
121 html("<th class='left'>Links</th>");
126 void print_pager(int items, int pagelen, char *search, char *sort)
130 html("<div class='pager'>");
131 for(i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
132 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
133 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), class,
139 static int cmp(const char *s1, const char *s2)
142 if (ctx.cfg.case_sensitive_sort)
143 return strcmp(s1, s2);
145 return strcasecmp(s1, s2);
154 static int sort_section(const void *a, const void *b)
156 const struct cgit_repo *r1 = a;
157 const struct cgit_repo *r2 = b;
161 result = cmp(r1->section, r2->section);
163 if (!strcmp(ctx.cfg.repository_sort, "age")) {
164 // get_repo_modtime caches the value in r->mtime, so we don't
165 // have to worry about inefficiencies here.
166 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
167 result = r2->mtime - r1->mtime;
170 result = cmp(r1->name, r2->name);
175 static int sort_name(const void *a, const void *b)
177 const struct cgit_repo *r1 = a;
178 const struct cgit_repo *r2 = b;
180 return cmp(r1->name, r2->name);
183 static int sort_desc(const void *a, const void *b)
185 const struct cgit_repo *r1 = a;
186 const struct cgit_repo *r2 = b;
188 return cmp(r1->desc, r2->desc);
191 static int sort_owner(const void *a, const void *b)
193 const struct cgit_repo *r1 = a;
194 const struct cgit_repo *r2 = b;
196 return cmp(r1->owner, r2->owner);
199 static int sort_idle(const void *a, const void *b)
201 const struct cgit_repo *r1 = a;
202 const struct cgit_repo *r2 = b;
206 get_repo_modtime(r1, &t1);
207 get_repo_modtime(r2, &t2);
213 int (*fn)(const void *a, const void *b);
216 struct sortcolumn sortcolumn[] = {
217 {"section", sort_section},
220 {"owner", sort_owner},
225 int sort_repolist(char *field)
227 struct sortcolumn *column;
229 for (column = &sortcolumn[0]; column->name; column++) {
230 if (strcmp(field, column->name))
232 qsort(cgit_repolist.repos, cgit_repolist.count,
233 sizeof(struct cgit_repo), column->fn);
240 void cgit_print_repolist()
242 int i, columns = 4, hits = 0, header = 0;
243 char *last_section = NULL;
247 if (ctx.cfg.enable_index_links)
250 ctx.page.title = ctx.cfg.root_title;
251 cgit_print_http_headers(&ctx);
252 cgit_print_docstart(&ctx);
253 cgit_print_pageheader(&ctx);
255 if (ctx.cfg.index_header)
256 html_include(ctx.cfg.index_header);
259 sorted = sort_repolist(ctx.qry.sort);
261 sort_repolist("section");
263 html("<table summary='repository list' class='list nowrap'>");
264 for (i=0; i<cgit_repolist.count; i++) {
265 ctx.repo = &cgit_repolist.repos[i];
266 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
269 if (hits <= ctx.qry.ofs)
271 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
274 print_header(columns);
275 section = ctx.repo->section;
276 if (section && !strcmp(section, ""))
279 ((last_section == NULL && section != NULL) ||
280 (last_section != NULL && section == NULL) ||
281 (last_section != NULL && section != NULL &&
282 strcmp(section, last_section)))) {
283 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
287 last_section = section;
289 htmlf("<tr><td class='%s'>",
290 !sorted && section ? "sublevel-repo" : "toplevel-repo");
291 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
293 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
294 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
297 html_txt(ctx.repo->owner);
299 print_modtime(ctx.repo);
301 if (ctx.cfg.enable_index_links) {
303 cgit_summary_link("summary", NULL, "button", NULL);
304 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
305 0, NULL, NULL, ctx.qry.showmsg);
306 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
313 cgit_print_error("No repositories found");
314 else if (hits > ctx.cfg.max_repo_count)
315 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
319 void cgit_print_site_readme()
321 if (!ctx.cfg.root_readme)
323 if (ctx.cfg.about_filter)
324 cgit_open_filter(ctx.cfg.about_filter);
325 html_include(ctx.cfg.root_readme);
326 if (ctx.cfg.about_filter)
327 cgit_close_filter(ctx.cfg.about_filter);