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)
20 struct strbuf date_buf = STRBUF_INIT;
22 if (readfile(path, &buf, &size))
25 if (parse_date(buf, &date_buf) == 0)
26 result = strtoul(date_buf.buf, NULL, 10);
30 strbuf_release(&date_buf);
34 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
36 struct strbuf path = STRBUF_INIT;
38 struct cgit_repo *r = (struct cgit_repo *)repo;
40 if (repo->mtime != -1) {
44 strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile);
45 if (stat(path.buf, &s) == 0) {
46 *mtime = read_agefile(path.buf);
54 strbuf_addf(&path, "%s/refs/heads/%s", repo->path,
55 repo->defbranch ? repo->defbranch : "master");
56 if (stat(path.buf, &s) == 0) {
63 strbuf_addf(&path, "%s/%s", repo->path, "packed-refs");
64 if (stat(path.buf, &s) == 0) {
73 strbuf_release(&path);
74 return (r->mtime != 0);
77 static void print_modtime(struct cgit_repo *repo)
80 if (get_repo_modtime(repo, &t))
81 cgit_print_age(t, -1, NULL);
84 static int is_match(struct cgit_repo *repo)
88 if (repo->url && strcasestr(repo->url, ctx.qry.search))
90 if (repo->name && strcasestr(repo->name, ctx.qry.search))
92 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
94 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
99 static int is_in_url(struct cgit_repo *repo)
103 if (repo->url && starts_with(repo->url, ctx.qry.url))
108 static void print_sort_header(const char *title, const char *sort)
110 html("<th class='left'><a href='");
111 html_attr(cgit_currenturl());
112 htmlf("?s=%s", sort);
113 if (ctx.qry.search) {
115 html_url_arg(ctx.qry.search);
117 htmlf("'>%s</a></th>", title);
120 static void print_header()
122 html("<tr class='nohover'>");
123 print_sort_header("Name", "name");
124 print_sort_header("Description", "desc");
125 if (ctx.cfg.enable_index_owner)
126 print_sort_header("Owner", "owner");
127 print_sort_header("Idle", "idle");
128 if (ctx.cfg.enable_index_links)
129 html("<th class='left'>Links</th>");
134 static void print_pager(int items, int pagelen, char *search, char *sort)
138 html("<ul class='pager'>");
139 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
140 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
142 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
143 class, search, sort, ofs, 0);
149 static int cmp(const char *s1, const char *s2)
152 if (ctx.cfg.case_sensitive_sort)
153 return strcmp(s1, s2);
155 return strcasecmp(s1, s2);
164 static int sort_section(const void *a, const void *b)
166 const struct cgit_repo *r1 = a;
167 const struct cgit_repo *r2 = b;
171 result = cmp(r1->section, r2->section);
173 if (!strcmp(ctx.cfg.repository_sort, "age")) {
174 // get_repo_modtime caches the value in r->mtime, so we don't
175 // have to worry about inefficiencies here.
176 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
177 result = r2->mtime - r1->mtime;
180 result = cmp(r1->name, r2->name);
185 static int sort_name(const void *a, const void *b)
187 const struct cgit_repo *r1 = a;
188 const struct cgit_repo *r2 = b;
190 return cmp(r1->name, r2->name);
193 static int sort_desc(const void *a, const void *b)
195 const struct cgit_repo *r1 = a;
196 const struct cgit_repo *r2 = b;
198 return cmp(r1->desc, r2->desc);
201 static int sort_owner(const void *a, const void *b)
203 const struct cgit_repo *r1 = a;
204 const struct cgit_repo *r2 = b;
206 return cmp(r1->owner, r2->owner);
209 static int sort_idle(const void *a, const void *b)
211 const struct cgit_repo *r1 = a;
212 const struct cgit_repo *r2 = b;
216 get_repo_modtime(r1, &t1);
217 get_repo_modtime(r2, &t2);
223 int (*fn)(const void *a, const void *b);
226 struct sortcolumn sortcolumn[] = {
227 {"section", sort_section},
230 {"owner", sort_owner},
235 static int sort_repolist(char *field)
237 struct sortcolumn *column;
239 for (column = &sortcolumn[0]; column->name; column++) {
240 if (strcmp(field, column->name))
242 qsort(cgit_repolist.repos, cgit_repolist.count,
243 sizeof(struct cgit_repo), column->fn);
250 void cgit_print_repolist()
252 int i, columns = 3, hits = 0, header = 0;
253 char *last_section = NULL;
257 if (ctx.cfg.enable_index_links)
259 if (ctx.cfg.enable_index_owner)
262 ctx.page.title = ctx.cfg.root_title;
263 cgit_print_http_headers();
264 cgit_print_docstart();
265 cgit_print_pageheader();
267 if (ctx.cfg.index_header)
268 html_include(ctx.cfg.index_header);
271 sorted = sort_repolist(ctx.qry.sort);
272 else if (ctx.cfg.section_sort)
273 sort_repolist("section");
275 html("<table summary='repository list' class='list nowrap'>");
276 for (i = 0; i < cgit_repolist.count; i++) {
277 ctx.repo = &cgit_repolist.repos[i];
278 if (ctx.repo->hide || ctx.repo->ignore)
280 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
283 if (hits <= ctx.qry.ofs)
285 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
289 section = ctx.repo->section;
290 if (section && !strcmp(section, ""))
293 ((last_section == NULL && section != NULL) ||
294 (last_section != NULL && section == NULL) ||
295 (last_section != NULL && section != NULL &&
296 strcmp(section, last_section)))) {
297 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
301 last_section = section;
303 htmlf("<tr><td class='%s'>",
304 !sorted && section ? "sublevel-repo" : "toplevel-repo");
305 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
307 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
308 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
311 if (ctx.cfg.enable_index_owner) {
312 if (ctx.repo->owner_filter) {
313 cgit_open_filter(ctx.repo->owner_filter);
314 html_txt(ctx.repo->owner);
315 cgit_close_filter(ctx.repo->owner_filter);
318 html_attr(cgit_currenturl());
320 html_url_arg(ctx.repo->owner);
322 html_txt(ctx.repo->owner);
327 print_modtime(ctx.repo);
329 if (ctx.cfg.enable_index_links) {
331 cgit_summary_link("summary", NULL, "button", NULL);
332 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
333 0, NULL, NULL, ctx.qry.showmsg);
334 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
341 cgit_print_error("No repositories found");
342 else if (hits > ctx.cfg.max_repo_count)
343 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
347 void cgit_print_site_readme()
349 if (!ctx.cfg.root_readme)
351 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
352 html_include(ctx.cfg.root_readme);
353 cgit_close_filter(ctx.cfg.about_filter);