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