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 repo->defbranch : "master");
50 if (stat(path, &s) == 0) {
56 path = fmt("%s/%s", repo->path, "packed-refs");
57 if (stat(path, &s) == 0) {
65 return (r->mtime != 0);
68 static void print_modtime(struct cgit_repo *repo)
71 if (get_repo_modtime(repo, &t))
72 cgit_print_age(t, -1, NULL);
75 int is_match(struct cgit_repo *repo)
79 if (repo->url && strcasestr(repo->url, ctx.qry.search))
81 if (repo->name && strcasestr(repo->name, ctx.qry.search))
83 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
85 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
90 int is_in_url(struct cgit_repo *repo)
94 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
99 void print_sort_header(const char *title, const char *sort)
101 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
102 if (ctx.qry.search) {
104 html_url_arg(ctx.qry.search);
106 htmlf("'>%s</a></th>", title);
109 void print_header(int columns)
111 html("<tr class='nohover'>");
112 print_sort_header("Name", "name");
113 print_sort_header("Description", "desc");
114 print_sort_header("Owner", "owner");
115 print_sort_header("Idle", "idle");
116 if (ctx.cfg.enable_index_links)
117 html("<th class='left'>Links</th>");
122 void print_pager(int items, int pagelen, char *search)
125 html("<div class='pager'>");
126 for(i = 0; i * pagelen < items; i++)
127 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
128 search, i * pagelen);
132 static int cmp(const char *s1, const char *s2)
135 return strcmp(s1, s2);
143 static int sort_section(const void *a, const void *b)
145 const struct cgit_repo *r1 = a;
146 const struct cgit_repo *r2 = b;
149 result = cmp(r1->section, r2->section);
151 result = cmp(r1->name, r2->name);
155 static int sort_name(const void *a, const void *b)
157 const struct cgit_repo *r1 = a;
158 const struct cgit_repo *r2 = b;
160 return cmp(r1->name, r2->name);
163 static int sort_desc(const void *a, const void *b)
165 const struct cgit_repo *r1 = a;
166 const struct cgit_repo *r2 = b;
168 return cmp(r1->desc, r2->desc);
171 static int sort_owner(const void *a, const void *b)
173 const struct cgit_repo *r1 = a;
174 const struct cgit_repo *r2 = b;
176 return cmp(r1->owner, r2->owner);
179 static int sort_idle(const void *a, const void *b)
181 const struct cgit_repo *r1 = a;
182 const struct cgit_repo *r2 = b;
186 get_repo_modtime(r1, &t1);
187 get_repo_modtime(r2, &t2);
193 int (*fn)(const void *a, const void *b);
196 struct sortcolumn sortcolumn[] = {
197 {"section", sort_section},
200 {"owner", sort_owner},
205 int sort_repolist(char *field)
207 struct sortcolumn *column;
209 for (column = &sortcolumn[0]; column->name; column++) {
210 if (strcmp(field, column->name))
212 qsort(cgit_repolist.repos, cgit_repolist.count,
213 sizeof(struct cgit_repo), column->fn);
220 void cgit_print_repolist()
222 int i, columns = 4, hits = 0, header = 0;
223 char *last_section = NULL;
227 if (ctx.cfg.enable_index_links)
230 ctx.page.title = ctx.cfg.root_title;
231 cgit_print_http_headers(&ctx);
232 cgit_print_docstart(&ctx);
233 cgit_print_pageheader(&ctx);
235 if (ctx.cfg.index_header)
236 html_include(ctx.cfg.index_header);
239 sorted = sort_repolist(ctx.qry.sort);
241 sort_repolist("section");
243 html("<table summary='repository list' class='list nowrap'>");
244 for (i=0; i<cgit_repolist.count; i++) {
245 ctx.repo = &cgit_repolist.repos[i];
246 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
249 if (hits <= ctx.qry.ofs)
251 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
254 print_header(columns);
255 section = ctx.repo->section;
256 if (section && !strcmp(section, ""))
259 ((last_section == NULL && section != NULL) ||
260 (last_section != NULL && section == NULL) ||
261 (last_section != NULL && section != NULL &&
262 strcmp(section, last_section)))) {
263 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
267 last_section = section;
269 htmlf("<tr><td class='%s'>",
270 !sorted && section ? "sublevel-repo" : "toplevel-repo");
271 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
273 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
274 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
277 html_txt(ctx.repo->owner);
279 print_modtime(ctx.repo);
281 if (ctx.cfg.enable_index_links) {
283 cgit_summary_link("summary", NULL, "button", NULL);
284 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
285 0, NULL, NULL, ctx.qry.showmsg);
286 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
293 cgit_print_error("No repositories found");
294 else if (hits > ctx.cfg.max_repo_count)
295 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
299 void cgit_print_site_readme()
301 if (!ctx.cfg.root_readme)
303 if (ctx.cfg.about_filter)
304 cgit_open_filter(ctx.cfg.about_filter);
305 html_include(ctx.cfg.root_readme);
306 if (ctx.cfg.about_filter)
307 cgit_close_filter(ctx.cfg.about_filter);