ui-repolist.c (view raw)
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
15time_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
33static 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 r->mtime = *mtime;
47 return 1;
48 }
49
50 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
51 repo->defbranch : "master");
52 if (stat(path, &s) == 0) {
53 *mtime = s.st_mtime;
54 r->mtime = *mtime;
55 return 1;
56 }
57
58 path = fmt("%s/%s", repo->path, "packed-refs");
59 if (stat(path, &s) == 0) {
60 *mtime = s.st_mtime;
61 r->mtime = *mtime;
62 return 1;
63 }
64
65 *mtime = 0;
66 r->mtime = *mtime;
67 return (r->mtime != 0);
68}
69
70static void print_modtime(struct cgit_repo *repo)
71{
72 time_t t;
73 if (get_repo_modtime(repo, &t))
74 cgit_print_age(t, -1, NULL);
75}
76
77int is_match(struct cgit_repo *repo)
78{
79 if (!ctx.qry.search)
80 return 1;
81 if (repo->url && strcasestr(repo->url, ctx.qry.search))
82 return 1;
83 if (repo->name && strcasestr(repo->name, ctx.qry.search))
84 return 1;
85 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
86 return 1;
87 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
88 return 1;
89 return 0;
90}
91
92int is_in_url(struct cgit_repo *repo)
93{
94 if (!ctx.qry.url)
95 return 1;
96 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
97 return 1;
98 return 0;
99}
100
101void print_sort_header(const char *title, const char *sort)
102{
103 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
104 if (ctx.qry.search) {
105 html("&q=");
106 html_url_arg(ctx.qry.search);
107 }
108 htmlf("'>%s</a></th>", title);
109}
110
111void print_header(int columns)
112{
113 html("<tr class='nohover'>");
114 print_sort_header("Name", "name");
115 print_sort_header("Description", "desc");
116 print_sort_header("Owner", "owner");
117 print_sort_header("Idle", "idle");
118 if (ctx.cfg.enable_index_links)
119 html("<th class='left'>Links</th>");
120 html("</tr>\n");
121}
122
123
124void print_pager(int items, int pagelen, char *search, char *sort)
125{
126 int i;
127 html("<div class='pager'>");
128 for(i = 0; i * pagelen < items; i++)
129 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
130 search, sort, i * pagelen);
131 html("</div>");
132}
133
134static int cmp(const char *s1, const char *s2)
135{
136 if (s1 && s2) {
137 if (ctx.cfg.case_sensitive_sort)
138 return strcmp(s1, s2);
139 else
140 return strcasecmp(s1, s2);
141 }
142 if (s1 && !s2)
143 return -1;
144 if (s2 && !s1)
145 return 1;
146 return 0;
147}
148
149static int sort_section(const void *a, const void *b)
150{
151 const struct cgit_repo *r1 = a;
152 const struct cgit_repo *r2 = b;
153 int result;
154 time_t t;
155
156 result = cmp(r1->section, r2->section);
157 if (!result) {
158 if (!strcmp(ctx.cfg.section_sort, "age")) {
159 // get_repo_modtime caches the value in r->mtime, so we don't
160 // have to worry about inefficiencies here.
161 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
162 result = r2->mtime - r1->mtime;
163 }
164 if (!result)
165 result = cmp(r1->name, r2->name);
166 }
167 return result;
168}
169
170static int sort_name(const void *a, const void *b)
171{
172 const struct cgit_repo *r1 = a;
173 const struct cgit_repo *r2 = b;
174
175 return cmp(r1->name, r2->name);
176}
177
178static int sort_desc(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->desc, r2->desc);
184}
185
186static int sort_owner(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->owner, r2->owner);
192}
193
194static int sort_idle(const void *a, const void *b)
195{
196 const struct cgit_repo *r1 = a;
197 const struct cgit_repo *r2 = b;
198 time_t t1, t2;
199
200 t1 = t2 = 0;
201 get_repo_modtime(r1, &t1);
202 get_repo_modtime(r2, &t2);
203 return t2 - t1;
204}
205
206struct sortcolumn {
207 const char *name;
208 int (*fn)(const void *a, const void *b);
209};
210
211struct sortcolumn sortcolumn[] = {
212 {"section", sort_section},
213 {"name", sort_name},
214 {"desc", sort_desc},
215 {"owner", sort_owner},
216 {"idle", sort_idle},
217 {NULL, NULL}
218};
219
220int sort_repolist(char *field)
221{
222 struct sortcolumn *column;
223
224 for (column = &sortcolumn[0]; column->name; column++) {
225 if (strcmp(field, column->name))
226 continue;
227 qsort(cgit_repolist.repos, cgit_repolist.count,
228 sizeof(struct cgit_repo), column->fn);
229 return 1;
230 }
231 return 0;
232}
233
234
235void cgit_print_repolist()
236{
237 int i, columns = 4, hits = 0, header = 0;
238 char *last_section = NULL;
239 char *section;
240 int sorted = 0;
241
242 if (ctx.cfg.enable_index_links)
243 columns++;
244
245 ctx.page.title = ctx.cfg.root_title;
246 cgit_print_http_headers(&ctx);
247 cgit_print_docstart(&ctx);
248 cgit_print_pageheader(&ctx);
249
250 if (ctx.cfg.index_header)
251 html_include(ctx.cfg.index_header);
252
253 if(ctx.qry.sort)
254 sorted = sort_repolist(ctx.qry.sort);
255 else
256 sort_repolist("section");
257
258 html("<table summary='repository list' class='list nowrap'>");
259 for (i=0; i<cgit_repolist.count; i++) {
260 ctx.repo = &cgit_repolist.repos[i];
261 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
262 continue;
263 hits++;
264 if (hits <= ctx.qry.ofs)
265 continue;
266 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
267 continue;
268 if (!header++)
269 print_header(columns);
270 section = ctx.repo->section;
271 if (section && !strcmp(section, ""))
272 section = NULL;
273 if (!sorted &&
274 ((last_section == NULL && section != NULL) ||
275 (last_section != NULL && section == NULL) ||
276 (last_section != NULL && section != NULL &&
277 strcmp(section, last_section)))) {
278 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
279 columns);
280 html_txt(section);
281 html("</td></tr>");
282 last_section = section;
283 }
284 htmlf("<tr><td class='%s'>",
285 !sorted && section ? "sublevel-repo" : "toplevel-repo");
286 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
287 html("</td><td>");
288 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
289 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
290 html_link_close();
291 html("</td><td>");
292 html_txt(ctx.repo->owner);
293 html("</td><td>");
294 print_modtime(ctx.repo);
295 html("</td>");
296 if (ctx.cfg.enable_index_links) {
297 html("<td>");
298 cgit_summary_link("summary", NULL, "button", NULL);
299 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
300 0, NULL, NULL, ctx.qry.showmsg);
301 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
302 html("</td>");
303 }
304 html("</tr>\n");
305 }
306 html("</table>");
307 if (!hits)
308 cgit_print_error("No repositories found");
309 else if (hits > ctx.cfg.max_repo_count)
310 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
311 cgit_print_docend();
312}
313
314void cgit_print_site_readme()
315{
316 if (!ctx.cfg.root_readme)
317 return;
318 if (ctx.cfg.about_filter)
319 cgit_open_filter(ctx.cfg.about_filter);
320 html_include(ctx.cfg.root_readme);
321 if (ctx.cfg.about_filter)
322 cgit_close_filter(ctx.cfg.about_filter);
323}