]> git.cameronkatri.com Git - cgit.git/blob - ui-repolist.c
Convert pager navigation into a unordered list
[cgit.git] / ui-repolist.c
1 /* ui-repolist.c: functions for generating the repolist page
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
5 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
10 #include "cgit.h"
11 #include "html.h"
12 #include "ui-shared.h"
13 #include <strings.h>
14
15 static time_t read_agefile(char *path)
16 {
17 time_t result;
18 size_t size;
19 char *buf;
20 static char buf2[64];
21
22 if (readfile(path, &buf, &size))
23 return -1;
24
25 if (parse_date(buf, buf2, sizeof(buf2)) > 0)
26 result = strtoul(buf2, NULL, 10);
27 else
28 result = 0;
29 free(buf);
30 return result;
31 }
32
33 static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
34 {
35 char *path;
36 struct stat s;
37 struct cgit_repo *r = (struct cgit_repo *)repo;
38
39 if (repo->mtime != -1) {
40 *mtime = repo->mtime;
41 return 1;
42 }
43 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
44 if (stat(path, &s) == 0) {
45 *mtime = read_agefile(path);
46 if (*mtime) {
47 r->mtime = *mtime;
48 return 1;
49 }
50 }
51
52 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
53 repo->defbranch : "master");
54 if (stat(path, &s) == 0) {
55 *mtime = s.st_mtime;
56 r->mtime = *mtime;
57 return 1;
58 }
59
60 path = fmt("%s/%s", repo->path, "packed-refs");
61 if (stat(path, &s) == 0) {
62 *mtime = s.st_mtime;
63 r->mtime = *mtime;
64 return 1;
65 }
66
67 *mtime = 0;
68 r->mtime = *mtime;
69 return (r->mtime != 0);
70 }
71
72 static void print_modtime(struct cgit_repo *repo)
73 {
74 time_t t;
75 if (get_repo_modtime(repo, &t))
76 cgit_print_age(t, -1, NULL);
77 }
78
79 static int is_match(struct cgit_repo *repo)
80 {
81 if (!ctx.qry.search)
82 return 1;
83 if (repo->url && strcasestr(repo->url, ctx.qry.search))
84 return 1;
85 if (repo->name && strcasestr(repo->name, ctx.qry.search))
86 return 1;
87 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
88 return 1;
89 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
90 return 1;
91 return 0;
92 }
93
94 static int is_in_url(struct cgit_repo *repo)
95 {
96 if (!ctx.qry.url)
97 return 1;
98 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
99 return 1;
100 return 0;
101 }
102
103 static void print_sort_header(const char *title, const char *sort)
104 {
105 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
106 if (ctx.qry.search) {
107 html("&amp;q=");
108 html_url_arg(ctx.qry.search);
109 }
110 htmlf("'>%s</a></th>", title);
111 }
112
113 static void print_header()
114 {
115 html("<tr class='nohover'>");
116 print_sort_header("Name", "name");
117 print_sort_header("Description", "desc");
118 if (ctx.cfg.enable_index_owner)
119 print_sort_header("Owner", "owner");
120 print_sort_header("Idle", "idle");
121 if (ctx.cfg.enable_index_links)
122 html("<th class='left'>Links</th>");
123 html("</tr>\n");
124 }
125
126
127 static void print_pager(int items, int pagelen, char *search, char *sort)
128 {
129 int i, ofs;
130 char *class = NULL;
131 html("<ul class='pager'>");
132 for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
133 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
134 html("<li>");
135 cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
136 class, search, sort, ofs);
137 html("</li>");
138 }
139 html("</ul>");
140 }
141
142 static int cmp(const char *s1, const char *s2)
143 {
144 if (s1 && s2) {
145 if (ctx.cfg.case_sensitive_sort)
146 return strcmp(s1, s2);
147 else
148 return strcasecmp(s1, s2);
149 }
150 if (s1 && !s2)
151 return -1;
152 if (s2 && !s1)
153 return 1;
154 return 0;
155 }
156
157 static int sort_section(const void *a, const void *b)
158 {
159 const struct cgit_repo *r1 = a;
160 const struct cgit_repo *r2 = b;
161 int result;
162 time_t t;
163
164 result = cmp(r1->section, r2->section);
165 if (!result) {
166 if (!strcmp(ctx.cfg.repository_sort, "age")) {
167 // get_repo_modtime caches the value in r->mtime, so we don't
168 // have to worry about inefficiencies here.
169 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
170 result = r2->mtime - r1->mtime;
171 }
172 if (!result)
173 result = cmp(r1->name, r2->name);
174 }
175 return result;
176 }
177
178 static int sort_name(const void *a, const void *b)
179 {
180 const struct cgit_repo *r1 = a;
181 const struct cgit_repo *r2 = b;
182
183 return cmp(r1->name, r2->name);
184 }
185
186 static int sort_desc(const void *a, const void *b)
187 {
188 const struct cgit_repo *r1 = a;
189 const struct cgit_repo *r2 = b;
190
191 return cmp(r1->desc, r2->desc);
192 }
193
194 static int sort_owner(const void *a, const void *b)
195 {
196 const struct cgit_repo *r1 = a;
197 const struct cgit_repo *r2 = b;
198
199 return cmp(r1->owner, r2->owner);
200 }
201
202 static int sort_idle(const void *a, const void *b)
203 {
204 const struct cgit_repo *r1 = a;
205 const struct cgit_repo *r2 = b;
206 time_t t1, t2;
207
208 t1 = t2 = 0;
209 get_repo_modtime(r1, &t1);
210 get_repo_modtime(r2, &t2);
211 return t2 - t1;
212 }
213
214 struct sortcolumn {
215 const char *name;
216 int (*fn)(const void *a, const void *b);
217 };
218
219 struct sortcolumn sortcolumn[] = {
220 {"section", sort_section},
221 {"name", sort_name},
222 {"desc", sort_desc},
223 {"owner", sort_owner},
224 {"idle", sort_idle},
225 {NULL, NULL}
226 };
227
228 static int sort_repolist(char *field)
229 {
230 struct sortcolumn *column;
231
232 for (column = &sortcolumn[0]; column->name; column++) {
233 if (strcmp(field, column->name))
234 continue;
235 qsort(cgit_repolist.repos, cgit_repolist.count,
236 sizeof(struct cgit_repo), column->fn);
237 return 1;
238 }
239 return 0;
240 }
241
242
243 void cgit_print_repolist()
244 {
245 int i, columns = 3, hits = 0, header = 0;
246 char *last_section = NULL;
247 char *section;
248 int sorted = 0;
249
250 if (ctx.cfg.enable_index_links)
251 ++columns;
252 if (ctx.cfg.enable_index_owner)
253 ++columns;
254
255 ctx.page.title = ctx.cfg.root_title;
256 cgit_print_http_headers(&ctx);
257 cgit_print_docstart(&ctx);
258 cgit_print_pageheader(&ctx);
259
260 if (ctx.cfg.index_header)
261 html_include(ctx.cfg.index_header);
262
263 if (ctx.qry.sort)
264 sorted = sort_repolist(ctx.qry.sort);
265 else if (ctx.cfg.section_sort)
266 sort_repolist("section");
267
268 html("<table summary='repository list' class='list nowrap'>");
269 for (i = 0; i < cgit_repolist.count; i++) {
270 ctx.repo = &cgit_repolist.repos[i];
271 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
272 continue;
273 hits++;
274 if (hits <= ctx.qry.ofs)
275 continue;
276 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
277 continue;
278 if (!header++)
279 print_header();
280 section = ctx.repo->section;
281 if (section && !strcmp(section, ""))
282 section = NULL;
283 if (!sorted &&
284 ((last_section == NULL && section != NULL) ||
285 (last_section != NULL && section == NULL) ||
286 (last_section != NULL && section != NULL &&
287 strcmp(section, last_section)))) {
288 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
289 columns);
290 html_txt(section);
291 html("</td></tr>");
292 last_section = section;
293 }
294 htmlf("<tr><td class='%s'>",
295 !sorted && section ? "sublevel-repo" : "toplevel-repo");
296 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
297 html("</td><td>");
298 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
299 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
300 html_link_close();
301 html("</td><td>");
302 if (ctx.cfg.enable_index_owner) {
303 html_txt(ctx.repo->owner);
304 html("</td><td>");
305 }
306 print_modtime(ctx.repo);
307 html("</td>");
308 if (ctx.cfg.enable_index_links) {
309 html("<td>");
310 cgit_summary_link("summary", NULL, "button", NULL);
311 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
312 0, NULL, NULL, ctx.qry.showmsg);
313 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
314 html("</td>");
315 }
316 html("</tr>\n");
317 }
318 html("</table>");
319 if (!hits)
320 cgit_print_error("No repositories found");
321 else if (hits > ctx.cfg.max_repo_count)
322 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
323 cgit_print_docend();
324 }
325
326 void cgit_print_site_readme()
327 {
328 if (!ctx.cfg.root_readme)
329 return;
330 if (ctx.cfg.about_filter)
331 cgit_open_filter(ctx.cfg.about_filter);
332 html_include(ctx.cfg.root_readme);
333 if (ctx.cfg.about_filter)
334 cgit_close_filter(ctx.cfg.about_filter);
335 }