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)
13 #include "ui-shared.h"
15 time_t read_agefile(char *path)
18 static char buf[64], buf2[64];
20 if (!(f = fopen(path, "r")))
22 if (fgets(buf, sizeof(buf), f) == NULL)
25 if (parse_date(buf, buf2, sizeof(buf2)))
26 return strtoul(buf2, NULL, 10);
31 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
36 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
37 if (stat(path, &s) == 0) {
38 *mtime = read_agefile(path);
42 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
43 if (stat(path, &s) == 0) {
50 static void print_modtime(struct cgit_repo *repo)
53 if (get_repo_modtime(repo, &t))
54 cgit_print_age(t, -1, NULL);
57 int is_match(struct cgit_repo *repo)
61 if (repo->url && strcasestr(repo->url, ctx.qry.search))
63 if (repo->name && strcasestr(repo->name, ctx.qry.search))
65 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
67 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
72 int is_in_url(struct cgit_repo *repo)
76 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
81 void print_sort_header(const char *title, const char *sort)
83 htmlf("<th class='left'><a href='./?s=%s", sort);
86 html_url_arg(ctx.qry.search);
88 htmlf("'>%s</a></th>", title);
91 void print_header(int columns)
93 html("<tr class='nohover'>");
94 print_sort_header("Name", "name");
95 print_sort_header("Description", "desc");
96 print_sort_header("Owner", "owner");
97 print_sort_header("Idle", "idle");
98 if (ctx.cfg.enable_index_links)
99 html("<th class='left'>Links</th>");
104 void print_pager(int items, int pagelen, char *search)
107 html("<div class='pager'>");
108 for(i = 0; i * pagelen < items; i++)
109 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
110 search, i * pagelen);
114 static int cmp(const char *s1, const char *s2)
117 return strcmp(s1, s2);
125 static int sort_name(const void *a, const void *b)
127 const struct cgit_repo *r1 = a;
128 const struct cgit_repo *r2 = b;
130 return cmp(r1->name, r2->name);
133 static int sort_desc(const void *a, const void *b)
135 const struct cgit_repo *r1 = a;
136 const struct cgit_repo *r2 = b;
138 return cmp(r1->desc, r2->desc);
141 static int sort_owner(const void *a, const void *b)
143 const struct cgit_repo *r1 = a;
144 const struct cgit_repo *r2 = b;
146 return cmp(r1->owner, r2->owner);
149 static int sort_idle(const void *a, const void *b)
151 const struct cgit_repo *r1 = a;
152 const struct cgit_repo *r2 = b;
156 get_repo_modtime(r1, &t1);
157 get_repo_modtime(r2, &t2);
163 int (*fn)(const void *a, const void *b);
166 struct sortcolumn sortcolumn[] = {
169 {"owner", sort_owner},
174 int sort_repolist(char *field)
176 struct sortcolumn *column;
178 for (column = &sortcolumn[0]; column->name; column++) {
179 if (strcmp(field, column->name))
181 qsort(cgit_repolist.repos, cgit_repolist.count,
182 sizeof(struct cgit_repo), column->fn);
189 void cgit_print_repolist()
191 int i, columns = 4, hits = 0, header = 0;
192 char *last_group = NULL;
195 if (ctx.cfg.enable_index_links)
198 ctx.page.title = ctx.cfg.root_title;
199 cgit_print_http_headers(&ctx);
200 cgit_print_docstart(&ctx);
201 cgit_print_pageheader(&ctx);
203 if (ctx.cfg.index_header)
204 html_include(ctx.cfg.index_header);
207 sorted = sort_repolist(ctx.qry.sort);
209 html("<table summary='repository list' class='list nowrap'>");
210 for (i=0; i<cgit_repolist.count; i++) {
211 ctx.repo = &cgit_repolist.repos[i];
212 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
215 if (hits <= ctx.qry.ofs)
217 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
220 print_header(columns);
222 ((last_group == NULL && ctx.repo->group != NULL) ||
223 (last_group != NULL && ctx.repo->group == NULL) ||
224 (last_group != NULL && ctx.repo->group != NULL &&
225 strcmp(ctx.repo->group, last_group)))) {
226 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
228 html_txt(ctx.repo->group);
230 last_group = ctx.repo->group;
232 htmlf("<tr><td class='%s'>",
233 !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
234 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
236 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
237 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
240 html_txt(ctx.repo->owner);
242 print_modtime(ctx.repo);
244 if (ctx.cfg.enable_index_links) {
246 cgit_summary_link("summary", NULL, "button", NULL);
247 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
249 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
256 cgit_print_error("No repositories found");
257 else if (hits > ctx.cfg.max_repo_count)
258 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
262 void cgit_print_site_readme()
264 if (ctx.cfg.root_readme)
265 html_include(ctx.cfg.root_readme);