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)
9 /* This is needed for strcasestr to be defined by <string.h> */
17 #include "ui-shared.h"
19 time_t read_agefile(char *path)
22 static char buf[64], buf2[64];
24 if (!(f = fopen(path, "r")))
27 if (fgets(buf, sizeof(buf), f) == NULL)
30 if (parse_date(buf, buf2, sizeof(buf2)))
31 return strtoul(buf2, NULL, 10);
36 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
40 struct cgit_repo *r = (struct cgit_repo *)repo;
42 if (repo->mtime != -1) {
46 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
47 if (stat(path, &s) == 0) {
48 *mtime = read_agefile(path);
53 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
54 if (stat(path, &s) == 0)
60 return (r->mtime != 0);
63 static void print_modtime(struct cgit_repo *repo)
66 if (get_repo_modtime(repo, &t))
67 cgit_print_age(t, -1, NULL);
70 int is_match(struct cgit_repo *repo)
74 if (repo->url && strcasestr(repo->url, ctx.qry.search))
76 if (repo->name && strcasestr(repo->name, ctx.qry.search))
78 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
80 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
85 int is_in_url(struct cgit_repo *repo)
89 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
94 void print_sort_header(const char *title, const char *sort)
96 htmlf("<th class='left'><a href='./?s=%s", sort);
99 html_url_arg(ctx.qry.search);
101 htmlf("'>%s</a></th>", title);
104 void print_header(int columns)
106 html("<tr class='nohover'>");
107 print_sort_header("Name", "name");
108 print_sort_header("Description", "desc");
109 print_sort_header("Owner", "owner");
110 print_sort_header("Idle", "idle");
111 if (ctx.cfg.enable_index_links)
112 html("<th class='left'>Links</th>");
117 void print_pager(int items, int pagelen, char *search)
120 html("<div class='pager'>");
121 for(i = 0; i * pagelen < items; i++)
122 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
123 search, i * pagelen);
127 static int cmp(const char *s1, const char *s2)
130 return strcmp(s1, s2);
138 static int sort_name(const void *a, const void *b)
140 const struct cgit_repo *r1 = a;
141 const struct cgit_repo *r2 = b;
143 return cmp(r1->name, r2->name);
146 static int sort_desc(const void *a, const void *b)
148 const struct cgit_repo *r1 = a;
149 const struct cgit_repo *r2 = b;
151 return cmp(r1->desc, r2->desc);
154 static int sort_owner(const void *a, const void *b)
156 const struct cgit_repo *r1 = a;
157 const struct cgit_repo *r2 = b;
159 return cmp(r1->owner, r2->owner);
162 static int sort_idle(const void *a, const void *b)
164 const struct cgit_repo *r1 = a;
165 const struct cgit_repo *r2 = b;
169 get_repo_modtime(r1, &t1);
170 get_repo_modtime(r2, &t2);
176 int (*fn)(const void *a, const void *b);
179 struct sortcolumn sortcolumn[] = {
182 {"owner", sort_owner},
187 int sort_repolist(char *field)
189 struct sortcolumn *column;
191 for (column = &sortcolumn[0]; column->name; column++) {
192 if (strcmp(field, column->name))
194 qsort(cgit_repolist.repos, cgit_repolist.count,
195 sizeof(struct cgit_repo), column->fn);
202 void cgit_print_repolist()
204 int i, columns = 4, hits = 0, header = 0;
205 char *last_group = NULL;
208 if (ctx.cfg.enable_index_links)
211 ctx.page.title = ctx.cfg.root_title;
212 cgit_print_http_headers(&ctx);
213 cgit_print_docstart(&ctx);
214 cgit_print_pageheader(&ctx);
216 if (ctx.cfg.index_header)
217 html_include(ctx.cfg.index_header);
220 sorted = sort_repolist(ctx.qry.sort);
222 html("<table summary='repository list' class='list nowrap'>");
223 for (i=0; i<cgit_repolist.count; i++) {
224 ctx.repo = &cgit_repolist.repos[i];
225 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
228 if (hits <= ctx.qry.ofs)
230 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
233 print_header(columns);
235 ((last_group == NULL && ctx.repo->group != NULL) ||
236 (last_group != NULL && ctx.repo->group == NULL) ||
237 (last_group != NULL && ctx.repo->group != NULL &&
238 strcmp(ctx.repo->group, last_group)))) {
239 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
241 html_txt(ctx.repo->group);
243 last_group = ctx.repo->group;
245 htmlf("<tr><td class='%s'>",
246 !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
247 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
249 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
250 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
253 html_txt(ctx.repo->owner);
255 print_modtime(ctx.repo);
257 if (ctx.cfg.enable_index_links) {
259 cgit_summary_link("summary", NULL, "button", NULL);
260 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
261 0, NULL, NULL, ctx.qry.showmsg);
262 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
269 cgit_print_error("No repositories found");
270 else if (hits > ctx.cfg.max_repo_count)
271 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
275 void cgit_print_site_readme()
277 if (!ctx.cfg.root_readme)
279 if (ctx.cfg.about_filter)
280 cgit_open_filter(ctx.cfg.about_filter);
281 html_include(ctx.cfg.root_readme);
282 if (ctx.cfg.about_filter)
283 cgit_close_filter(ctx.cfg.about_filter);