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 int is_visible(struct cgit_repo *repo)
111 if (repo->hide || repo->ignore)
113 if (!(is_match(repo) && is_in_url(repo)))
118 static void print_sort_header(const char *title, const char *sort)
120 char *currenturl = cgit_currenturl();
121 html("<th class='left'><a href='");
122 html_attr(currenturl);
123 htmlf("?s=%s", sort);
124 if (ctx.qry.search) {
126 html_url_arg(ctx.qry.search);
128 htmlf("'>%s</a></th>", title);
132 static void print_header(void)
134 html("<tr class='nohover'>");
135 print_sort_header("Name", "name");
136 print_sort_header("Description", "desc");
137 if (ctx.cfg.enable_index_owner)
138 print_sort_header("Owner", "owner");
139 print_sort_header("Idle", "idle");
140 if (ctx.cfg.enable_index_links)
141 html("<th class='left'>Links</th>");
146 static void print_pager(int items, int pagelen, char *search, char *sort)
150 html("<ul class='pager'>");
151 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
152 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
154 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
155 class, search, sort, ofs, 0);
161 static int cmp(const char *s1, const char *s2)
164 if (ctx.cfg.case_sensitive_sort)
165 return strcmp(s1, s2);
167 return strcasecmp(s1, s2);
176 static int sort_section(const void *a, const void *b)
178 const struct cgit_repo *r1 = a;
179 const struct cgit_repo *r2 = b;
183 result = cmp(r1->section, r2->section);
185 if (!strcmp(ctx.cfg.repository_sort, "age")) {
186 // get_repo_modtime caches the value in r->mtime, so we don't
187 // have to worry about inefficiencies here.
188 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
189 result = r2->mtime - r1->mtime;
192 result = cmp(r1->name, r2->name);
197 static int sort_name(const void *a, const void *b)
199 const struct cgit_repo *r1 = a;
200 const struct cgit_repo *r2 = b;
202 return cmp(r1->name, r2->name);
205 static int sort_desc(const void *a, const void *b)
207 const struct cgit_repo *r1 = a;
208 const struct cgit_repo *r2 = b;
210 return cmp(r1->desc, r2->desc);
213 static int sort_owner(const void *a, const void *b)
215 const struct cgit_repo *r1 = a;
216 const struct cgit_repo *r2 = b;
218 return cmp(r1->owner, r2->owner);
221 static int sort_idle(const void *a, const void *b)
223 const struct cgit_repo *r1 = a;
224 const struct cgit_repo *r2 = b;
228 get_repo_modtime(r1, &t1);
229 get_repo_modtime(r2, &t2);
235 int (*fn)(const void *a, const void *b);
238 static const struct sortcolumn sortcolumn[] = {
239 {"section", sort_section},
242 {"owner", sort_owner},
247 static int sort_repolist(char *field)
249 const struct sortcolumn *column;
251 for (column = &sortcolumn[0]; column->name; column++) {
252 if (strcmp(field, column->name))
254 qsort(cgit_repolist.repos, cgit_repolist.count,
255 sizeof(struct cgit_repo), column->fn);
262 void cgit_print_repolist(void)
264 int i, columns = 3, hits = 0, header = 0;
265 char *last_section = NULL;
269 if (ctx.cfg.enable_index_links)
271 if (ctx.cfg.enable_index_owner)
274 ctx.page.title = ctx.cfg.root_title;
275 cgit_print_http_headers();
276 cgit_print_docstart();
277 cgit_print_pageheader();
279 if (ctx.cfg.index_header)
280 html_include(ctx.cfg.index_header);
283 sorted = sort_repolist(ctx.qry.sort);
284 else if (ctx.cfg.section_sort)
285 sort_repolist("section");
287 html("<table summary='repository list' class='list nowrap'>");
288 for (i = 0; i < cgit_repolist.count; i++) {
289 ctx.repo = &cgit_repolist.repos[i];
290 if (!is_visible(ctx.repo))
293 if (hits <= ctx.qry.ofs)
295 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
299 section = ctx.repo->section;
300 if (section && !strcmp(section, ""))
303 ((last_section == NULL && section != NULL) ||
304 (last_section != NULL && section == NULL) ||
305 (last_section != NULL && section != NULL &&
306 strcmp(section, last_section)))) {
307 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
311 last_section = section;
313 htmlf("<tr><td class='%s'>",
314 !sorted && section ? "sublevel-repo" : "toplevel-repo");
315 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
317 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
318 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
321 if (ctx.cfg.enable_index_owner) {
322 if (ctx.repo->owner_filter) {
323 cgit_open_filter(ctx.repo->owner_filter);
324 html_txt(ctx.repo->owner);
325 cgit_close_filter(ctx.repo->owner_filter);
328 html_attr(cgit_currenturl());
330 html_url_arg(ctx.repo->owner);
332 html_txt(ctx.repo->owner);
337 print_modtime(ctx.repo);
339 if (ctx.cfg.enable_index_links) {
341 cgit_summary_link("summary", NULL, "button", NULL);
342 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
343 0, NULL, NULL, ctx.qry.showmsg, 0);
344 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
351 cgit_print_error("No repositories found");
352 else if (hits > ctx.cfg.max_repo_count)
353 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
357 void cgit_print_site_readme(void)
359 cgit_print_layout_start();
360 if (!ctx.cfg.root_readme)
362 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
363 html_include(ctx.cfg.root_readme);
364 cgit_close_filter(ctx.cfg.about_filter);
366 cgit_print_layout_end();