]> git.cameronkatri.com Git - cgit.git/blob - ui-repolist.c
README: times, they are a-changin
[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 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 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 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 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("&q=");
108 html_url_arg(ctx.qry.search);
109 }
110 htmlf("'>%s</a></th>", title);
111 }
112
113 void print_header(int columns)
114 {
115 html("<tr class='nohover'>");
116 print_sort_header("Name", "name");
117 print_sort_header("Description", "desc");
118 print_sort_header("Owner", "owner");
119 print_sort_header("Idle", "idle");
120 if (ctx.cfg.enable_index_links)
121 html("<th class='left'>Links</th>");
122 html("</tr>\n");
123 }
124
125
126 void print_pager(int items, int pagelen, char *search, char *sort)
127 {
128 int i, ofs;
129 char *class = NULL;
130 html("<div class='pager'>");
131 for(i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
132 class = (ctx.qry.ofs == ofs) ? "current" : NULL;
133 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), class,
134 search, sort, ofs);
135 }
136 html("</div>");
137 }
138
139 static int cmp(const char *s1, const char *s2)
140 {
141 if (s1 && s2) {
142 if (ctx.cfg.case_sensitive_sort)
143 return strcmp(s1, s2);
144 else
145 return strcasecmp(s1, s2);
146 }
147 if (s1 && !s2)
148 return -1;
149 if (s2 && !s1)
150 return 1;
151 return 0;
152 }
153
154 static int sort_section(const void *a, const void *b)
155 {
156 const struct cgit_repo *r1 = a;
157 const struct cgit_repo *r2 = b;
158 int result;
159 time_t t;
160
161 result = cmp(r1->section, r2->section);
162 if (!result) {
163 if (!strcmp(ctx.cfg.section_sort, "age")) {
164 // get_repo_modtime caches the value in r->mtime, so we don't
165 // have to worry about inefficiencies here.
166 if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
167 result = r2->mtime - r1->mtime;
168 }
169 if (!result)
170 result = cmp(r1->name, r2->name);
171 }
172 return result;
173 }
174
175 static int sort_name(const void *a, const void *b)
176 {
177 const struct cgit_repo *r1 = a;
178 const struct cgit_repo *r2 = b;
179
180 return cmp(r1->name, r2->name);
181 }
182
183 static int sort_desc(const void *a, const void *b)
184 {
185 const struct cgit_repo *r1 = a;
186 const struct cgit_repo *r2 = b;
187
188 return cmp(r1->desc, r2->desc);
189 }
190
191 static int sort_owner(const void *a, const void *b)
192 {
193 const struct cgit_repo *r1 = a;
194 const struct cgit_repo *r2 = b;
195
196 return cmp(r1->owner, r2->owner);
197 }
198
199 static int sort_idle(const void *a, const void *b)
200 {
201 const struct cgit_repo *r1 = a;
202 const struct cgit_repo *r2 = b;
203 time_t t1, t2;
204
205 t1 = t2 = 0;
206 get_repo_modtime(r1, &t1);
207 get_repo_modtime(r2, &t2);
208 return t2 - t1;
209 }
210
211 struct sortcolumn {
212 const char *name;
213 int (*fn)(const void *a, const void *b);
214 };
215
216 struct sortcolumn sortcolumn[] = {
217 {"section", sort_section},
218 {"name", sort_name},
219 {"desc", sort_desc},
220 {"owner", sort_owner},
221 {"idle", sort_idle},
222 {NULL, NULL}
223 };
224
225 int sort_repolist(char *field)
226 {
227 struct sortcolumn *column;
228
229 for (column = &sortcolumn[0]; column->name; column++) {
230 if (strcmp(field, column->name))
231 continue;
232 qsort(cgit_repolist.repos, cgit_repolist.count,
233 sizeof(struct cgit_repo), column->fn);
234 return 1;
235 }
236 return 0;
237 }
238
239
240 void cgit_print_repolist()
241 {
242 int i, columns = 4, hits = 0, header = 0;
243 char *last_section = NULL;
244 char *section;
245 int sorted = 0;
246
247 if (ctx.cfg.enable_index_links)
248 columns++;
249
250 ctx.page.title = ctx.cfg.root_title;
251 cgit_print_http_headers(&ctx);
252 cgit_print_docstart(&ctx);
253 cgit_print_pageheader(&ctx);
254
255 if (ctx.cfg.index_header)
256 html_include(ctx.cfg.index_header);
257
258 if(ctx.qry.sort)
259 sorted = sort_repolist(ctx.qry.sort);
260 else
261 sort_repolist("section");
262
263 html("<table summary='repository list' class='list nowrap'>");
264 for (i=0; i<cgit_repolist.count; i++) {
265 ctx.repo = &cgit_repolist.repos[i];
266 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
267 continue;
268 hits++;
269 if (hits <= ctx.qry.ofs)
270 continue;
271 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
272 continue;
273 if (!header++)
274 print_header(columns);
275 section = ctx.repo->section;
276 if (section && !strcmp(section, ""))
277 section = NULL;
278 if (!sorted &&
279 ((last_section == NULL && section != NULL) ||
280 (last_section != NULL && section == NULL) ||
281 (last_section != NULL && section != NULL &&
282 strcmp(section, last_section)))) {
283 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
284 columns);
285 html_txt(section);
286 html("</td></tr>");
287 last_section = section;
288 }
289 htmlf("<tr><td class='%s'>",
290 !sorted && section ? "sublevel-repo" : "toplevel-repo");
291 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
292 html("</td><td>");
293 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
294 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
295 html_link_close();
296 html("</td><td>");
297 html_txt(ctx.repo->owner);
298 html("</td><td>");
299 print_modtime(ctx.repo);
300 html("</td>");
301 if (ctx.cfg.enable_index_links) {
302 html("<td>");
303 cgit_summary_link("summary", NULL, "button", NULL);
304 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
305 0, NULL, NULL, ctx.qry.showmsg);
306 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
307 html("</td>");
308 }
309 html("</tr>\n");
310 }
311 html("</table>");
312 if (!hits)
313 cgit_print_error("No repositories found");
314 else if (hits > ctx.cfg.max_repo_count)
315 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
316 cgit_print_docend();
317 }
318
319 void cgit_print_site_readme()
320 {
321 if (!ctx.cfg.root_readme)
322 return;
323 if (ctx.cfg.about_filter)
324 cgit_open_filter(ctx.cfg.about_filter);
325 html_include(ctx.cfg.root_readme);
326 if (ctx.cfg.about_filter)
327 cgit_close_filter(ctx.cfg.about_filter);
328 }