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 return (r->mtime != 0);
58 static void print_modtime(struct cgit_repo *repo)
61 if (get_repo_modtime(repo, &t))
62 cgit_print_age(t, -1, NULL);
65 int is_match(struct cgit_repo *repo)
69 if (repo->url && strcasestr(repo->url, ctx.qry.search))
71 if (repo->name && strcasestr(repo->name, ctx.qry.search))
73 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
75 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
80 int is_in_url(struct cgit_repo *repo)
84 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
89 void print_sort_header(const char *title, const char *sort)
91 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
94 html_url_arg(ctx.qry.search);
96 htmlf("'>%s</a></th>", title);
99 void print_header(int columns)
101 html("<tr class='nohover'>");
102 print_sort_header("Name", "name");
103 print_sort_header("Description", "desc");
104 print_sort_header("Owner", "owner");
105 print_sort_header("Idle", "idle");
106 if (ctx.cfg.enable_index_links)
107 html("<th class='left'>Links</th>");
112 void print_pager(int items, int pagelen, char *search, char *sort)
115 html("<div class='pager'>");
116 for(i = 0; i * pagelen < items; i++)
117 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
118 search, sort, i * pagelen);
122 static int cmp(const char *s1, const char *s2)
125 return strcmp(s1, s2);
133 static int sort_section(const void *a, const void *b)
135 const struct cgit_repo *r1 = a;
136 const struct cgit_repo *r2 = b;
139 result = cmp(r1->section, r2->section);
141 result = cmp(r1->name, r2->name);
145 static int sort_name(const void *a, const void *b)
147 const struct cgit_repo *r1 = a;
148 const struct cgit_repo *r2 = b;
150 return cmp(r1->name, r2->name);
153 static int sort_desc(const void *a, const void *b)
155 const struct cgit_repo *r1 = a;
156 const struct cgit_repo *r2 = b;
158 return cmp(r1->desc, r2->desc);
161 static int sort_owner(const void *a, const void *b)
163 const struct cgit_repo *r1 = a;
164 const struct cgit_repo *r2 = b;
166 return cmp(r1->owner, r2->owner);
169 static int sort_idle(const void *a, const void *b)
171 const struct cgit_repo *r1 = a;
172 const struct cgit_repo *r2 = b;
176 get_repo_modtime(r1, &t1);
177 get_repo_modtime(r2, &t2);
183 int (*fn)(const void *a, const void *b);
186 struct sortcolumn sortcolumn[] = {
187 {"section", sort_section},
190 {"owner", sort_owner},
195 int sort_repolist(char *field)
197 struct sortcolumn *column;
199 for (column = &sortcolumn[0]; column->name; column++) {
200 if (strcmp(field, column->name))
202 qsort(cgit_repolist.repos, cgit_repolist.count,
203 sizeof(struct cgit_repo), column->fn);
210 void cgit_print_repolist()
212 int i, columns = 4, hits = 0, header = 0;
213 char *last_section = NULL;
217 if (ctx.cfg.enable_index_links)
220 ctx.page.title = ctx.cfg.root_title;
221 cgit_print_http_headers(&ctx);
222 cgit_print_docstart(&ctx);
223 cgit_print_pageheader(&ctx);
225 if (ctx.cfg.index_header)
226 html_include(ctx.cfg.index_header);
229 sorted = sort_repolist(ctx.qry.sort);
231 sort_repolist("section");
233 html("<table summary='repository list' class='list nowrap'>");
234 for (i=0; i<cgit_repolist.count; i++) {
235 ctx.repo = &cgit_repolist.repos[i];
236 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
239 if (hits <= ctx.qry.ofs)
241 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
244 print_header(columns);
245 section = ctx.repo->section;
246 if (section && !strcmp(section, ""))
249 ((last_section == NULL && section != NULL) ||
250 (last_section != NULL && section == NULL) ||
251 (last_section != NULL && section != NULL &&
252 strcmp(section, last_section)))) {
253 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
257 last_section = section;
259 htmlf("<tr><td class='%s'>",
260 !sorted && section ? "sublevel-repo" : "toplevel-repo");
261 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
263 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
264 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
267 html_txt(ctx.repo->owner);
269 print_modtime(ctx.repo);
271 if (ctx.cfg.enable_index_links) {
273 cgit_summary_link("summary", NULL, "button", NULL);
274 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
275 0, NULL, NULL, ctx.qry.showmsg);
276 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
283 cgit_print_error("No repositories found");
284 else if (hits > ctx.cfg.max_repo_count)
285 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
289 void cgit_print_site_readme()
291 if (!ctx.cfg.root_readme)
293 if (ctx.cfg.about_filter)
294 cgit_open_filter(ctx.cfg.about_filter);
295 html_include(ctx.cfg.root_readme);
296 if (ctx.cfg.about_filter)
297 cgit_close_filter(ctx.cfg.about_filter);