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"
13 time_t read_agefile(char *path)
20 if (readfile(path, &buf, &size))
23 if (parse_date(buf, buf2, sizeof(buf2)) > 0)
24 result = strtoul(buf2, NULL, 10);
31 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
35 struct cgit_repo *r = (struct cgit_repo *)repo;
37 if (repo->mtime != -1) {
41 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
42 if (stat(path, &s) == 0) {
43 *mtime = read_agefile(path);
48 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
49 if (stat(path, &s) == 0) {
55 path = fmt("%s/%s", repo->path, "packed-refs");
56 if (stat(path, &s) == 0) {
64 return (r->mtime != 0);
67 static void print_modtime(struct cgit_repo *repo)
70 if (get_repo_modtime(repo, &t))
71 cgit_print_age(t, -1, NULL);
74 int is_match(struct cgit_repo *repo)
78 if (repo->url && strcasestr(repo->url, ctx.qry.search))
80 if (repo->name && strcasestr(repo->name, ctx.qry.search))
82 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
84 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
89 int is_in_url(struct cgit_repo *repo)
93 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
98 void print_sort_header(const char *title, const char *sort)
100 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
101 if (ctx.qry.search) {
103 html_url_arg(ctx.qry.search);
105 htmlf("'>%s</a></th>", title);
108 void print_header(int columns)
110 html("<tr class='nohover'>");
111 print_sort_header("Name", "name");
112 print_sort_header("Description", "desc");
113 print_sort_header("Owner", "owner");
114 print_sort_header("Idle", "idle");
115 if (ctx.cfg.enable_index_links)
116 html("<th class='left'>Links</th>");
121 void print_pager(int items, int pagelen, char *search)
124 html("<div class='pager'>");
125 for(i = 0; i * pagelen < items; i++)
126 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
127 search, i * pagelen);
131 static int cmp(const char *s1, const char *s2)
134 return strcmp(s1, s2);
142 static int sort_section(const void *a, const void *b)
144 const struct cgit_repo *r1 = a;
145 const struct cgit_repo *r2 = b;
148 result = cmp(r1->section, r2->section);
150 result = cmp(r1->name, r2->name);
154 static int sort_name(const void *a, const void *b)
156 const struct cgit_repo *r1 = a;
157 const struct cgit_repo *r2 = b;
159 return cmp(r1->name, r2->name);
162 static int sort_desc(const void *a, const void *b)
164 const struct cgit_repo *r1 = a;
165 const struct cgit_repo *r2 = b;
167 return cmp(r1->desc, r2->desc);
170 static int sort_owner(const void *a, const void *b)
172 const struct cgit_repo *r1 = a;
173 const struct cgit_repo *r2 = b;
175 return cmp(r1->owner, r2->owner);
178 static int sort_idle(const void *a, const void *b)
180 const struct cgit_repo *r1 = a;
181 const struct cgit_repo *r2 = b;
185 get_repo_modtime(r1, &t1);
186 get_repo_modtime(r2, &t2);
192 int (*fn)(const void *a, const void *b);
195 struct sortcolumn sortcolumn[] = {
196 {"section", sort_section},
199 {"owner", sort_owner},
204 int sort_repolist(char *field)
206 struct sortcolumn *column;
208 for (column = &sortcolumn[0]; column->name; column++) {
209 if (strcmp(field, column->name))
211 qsort(cgit_repolist.repos, cgit_repolist.count,
212 sizeof(struct cgit_repo), column->fn);
219 void cgit_print_repolist()
221 int i, columns = 4, hits = 0, header = 0;
222 char *last_section = NULL;
226 if (ctx.cfg.enable_index_links)
229 ctx.page.title = ctx.cfg.root_title;
230 cgit_print_http_headers(&ctx);
231 cgit_print_docstart(&ctx);
232 cgit_print_pageheader(&ctx);
234 if (ctx.cfg.index_header)
235 html_include(ctx.cfg.index_header);
238 sorted = sort_repolist(ctx.qry.sort);
240 sort_repolist("section");
242 html("<table summary='repository list' class='list nowrap'>");
243 for (i=0; i<cgit_repolist.count; i++) {
244 ctx.repo = &cgit_repolist.repos[i];
245 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
248 if (hits <= ctx.qry.ofs)
250 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
253 print_header(columns);
254 section = ctx.repo->section;
255 if (section && !strcmp(section, ""))
258 ((last_section == NULL && section != NULL) ||
259 (last_section != NULL && section == NULL) ||
260 (last_section != NULL && section != NULL &&
261 strcmp(section, last_section)))) {
262 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
266 last_section = section;
268 htmlf("<tr><td class='%s'>",
269 !sorted && section ? "sublevel-repo" : "toplevel-repo");
270 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
272 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
273 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
276 html_txt(ctx.repo->owner);
278 print_modtime(ctx.repo);
280 if (ctx.cfg.enable_index_links) {
282 cgit_summary_link("summary", NULL, "button", NULL);
283 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
284 0, NULL, NULL, ctx.qry.showmsg);
285 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
292 cgit_print_error("No repositories found");
293 else if (hits > ctx.cfg.max_repo_count)
294 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
298 void cgit_print_site_readme()
300 if (!ctx.cfg.root_readme)
302 if (ctx.cfg.about_filter)
303 cgit_open_filter(ctx.cfg.about_filter, NULL);
304 html_include(ctx.cfg.root_readme);
305 if (ctx.cfg.about_filter)
306 cgit_close_filter(ctx.cfg.about_filter);