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