1 /* ui-repolist.c: functions for generating the repolist page
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 #include "ui-shared.h"
14 time_t read_agefile(char *path)
21 if (readfile(path, &buf, &size))
24 if (parse_date(buf, buf2, sizeof(buf2)) > 0)
25 result = strtoul(buf2, NULL, 10);
32 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
36 struct cgit_repo *r = (struct cgit_repo *)repo;
38 if (repo->mtime != -1) {
42 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
43 if (stat(path, &s) == 0) {
44 *mtime = read_agefile(path);
49 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
50 repo->defbranch : "master");
51 if (stat(path, &s) == 0) {
57 path = fmt("%s/%s", repo->path, "packed-refs");
58 if (stat(path, &s) == 0) {
66 return (r->mtime != 0);
69 static void print_modtime(struct cgit_repo *repo)
72 if (get_repo_modtime(repo, &t))
73 cgit_print_age(t, -1, NULL);
76 int is_match(struct cgit_repo *repo)
80 if (repo->url && strcasestr(repo->url, ctx.qry.search))
82 if (repo->name && strcasestr(repo->name, ctx.qry.search))
84 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
86 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
91 int is_in_url(struct cgit_repo *repo)
95 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
100 void print_sort_header(const char *title, const char *sort)
102 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
103 if (ctx.qry.search) {
105 html_url_arg(ctx.qry.search);
107 htmlf("'>%s</a></th>", title);
110 void print_header(int columns)
112 html("<tr class='nohover'>");
113 print_sort_header("Name", "name");
114 print_sort_header("Description", "desc");
115 print_sort_header("Owner", "owner");
116 print_sort_header("Idle", "idle");
117 if (ctx.cfg.enable_index_links)
118 html("<th class='left'>Links</th>");
123 void print_pager(int items, int pagelen, char *search, char *sort)
126 html("<div class='pager'>");
127 for(i = 0; i * pagelen < items; i++)
128 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
129 search, sort, i * pagelen);
133 static int cmp(const char *s1, const char *s2)
136 if (ctx.cfg.case_sensitive_sort)
137 return strcmp(s1, s2);
139 return strcasecmp(s1, s2);
148 static int sort_section(const void *a, const void *b)
150 const struct cgit_repo *r1 = a;
151 const struct cgit_repo *r2 = b;
155 result = cmp(r1->section, r2->section);
157 if (!strcmp(ctx.cfg.section_sort, "age")) {
158 // get_repo_modtime caches the value in r->mtime, so we don't
159 // have to worry about inefficiencies here.
160 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
161 result = r2->mtime - r1->mtime;
164 result = cmp(r1->name, r2->name);
169 static int sort_name(const void *a, const void *b)
171 const struct cgit_repo *r1 = a;
172 const struct cgit_repo *r2 = b;
174 return cmp(r1->name, r2->name);
177 static int sort_desc(const void *a, const void *b)
179 const struct cgit_repo *r1 = a;
180 const struct cgit_repo *r2 = b;
182 return cmp(r1->desc, r2->desc);
185 static int sort_owner(const void *a, const void *b)
187 const struct cgit_repo *r1 = a;
188 const struct cgit_repo *r2 = b;
190 return cmp(r1->owner, r2->owner);
193 static int sort_idle(const void *a, const void *b)
195 const struct cgit_repo *r1 = a;
196 const struct cgit_repo *r2 = b;
200 get_repo_modtime(r1, &t1);
201 get_repo_modtime(r2, &t2);
207 int (*fn)(const void *a, const void *b);
210 struct sortcolumn sortcolumn[] = {
211 {"section", sort_section},
214 {"owner", sort_owner},
219 int sort_repolist(char *field)
221 struct sortcolumn *column;
223 for (column = &sortcolumn[0]; column->name; column++) {
224 if (strcmp(field, column->name))
226 qsort(cgit_repolist.repos, cgit_repolist.count,
227 sizeof(struct cgit_repo), column->fn);
234 void cgit_print_repolist()
236 int i, columns = 4, hits = 0, header = 0;
237 char *last_section = NULL;
241 if (ctx.cfg.enable_index_links)
244 ctx.page.title = ctx.cfg.root_title;
245 cgit_print_http_headers(&ctx);
246 cgit_print_docstart(&ctx);
247 cgit_print_pageheader(&ctx);
249 if (ctx.cfg.index_header)
250 html_include(ctx.cfg.index_header);
253 sorted = sort_repolist(ctx.qry.sort);
255 sort_repolist("section");
257 html("<table summary='repository list' class='list nowrap'>");
258 for (i=0; i<cgit_repolist.count; i++) {
259 ctx.repo = &cgit_repolist.repos[i];
260 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
263 if (hits <= ctx.qry.ofs)
265 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
268 print_header(columns);
269 section = ctx.repo->section;
270 if (section && !strcmp(section, ""))
273 ((last_section == NULL && section != NULL) ||
274 (last_section != NULL && section == NULL) ||
275 (last_section != NULL && section != NULL &&
276 strcmp(section, last_section)))) {
277 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
281 last_section = section;
283 htmlf("<tr><td class='%s'>",
284 !sorted && section ? "sublevel-repo" : "toplevel-repo");
285 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
287 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
288 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
291 html_txt(ctx.repo->owner);
293 print_modtime(ctx.repo);
295 if (ctx.cfg.enable_index_links) {
297 cgit_summary_link("summary", NULL, "button", NULL);
298 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
299 0, NULL, NULL, ctx.qry.showmsg);
300 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
307 cgit_print_error("No repositories found");
308 else if (hits > ctx.cfg.max_repo_count)
309 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
313 void cgit_print_site_readme()
315 if (!ctx.cfg.root_readme)
317 if (ctx.cfg.about_filter)
318 cgit_open_filter(ctx.cfg.about_filter);
319 html_include(ctx.cfg.root_readme);
320 if (ctx.cfg.about_filter)
321 cgit_close_filter(ctx.cfg.about_filter);