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