-rw-r--r-- | ui-repolist.c | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/ui-repolist.c b/ui-repolist.c index 3aedde5..3ef2e99 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -5,45 +5,46 @@ | |||
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> */ | 9 | /* This is needed for strcasestr to be defined by <string.h> */ |
10 | #define _GNU_SOURCE 1 | 10 | #define _GNU_SOURCE 1 |
11 | #include <string.h> | 11 | #include <string.h> |
12 | 12 | ||
13 | #include <time.h> | 13 | #include <time.h> |
14 | 14 | ||
15 | #include "cgit.h" | 15 | #include "cgit.h" |
16 | #include "html.h" | 16 | #include "html.h" |
17 | #include "ui-shared.h" | 17 | #include "ui-shared.h" |
18 | 18 | ||
19 | time_t read_agefile(char *path) | 19 | time_t read_agefile(char *path) |
20 | { | 20 | { |
21 | FILE *f; | 21 | time_t result; |
22 | static char buf[64], buf2[64]; | 22 | size_t size; |
23 | char *buf; | ||
24 | static char buf2[64]; | ||
23 | 25 | ||
24 | if (!(f = fopen(path, "r"))) | 26 | if (readfile(path, &buf, &size)) |
25 | return -1; | 27 | return -1; |
26 | buf[0] = 0; | 28 | |
27 | if (fgets(buf, sizeof(buf), f) == NULL) | ||
28 | return -1; | ||
29 | fclose(f); | ||
30 | if (parse_date(buf, buf2, sizeof(buf2))) | 29 | if (parse_date(buf, buf2, sizeof(buf2))) |
31 | return strtoul(buf2, NULL, 10); | 30 | result = strtoul(buf2, NULL, 10); |
32 | else | 31 | else |
33 | return 0; | 32 | result = 0; |
33 | free(buf); | ||
34 | return result; | ||
34 | } | 35 | } |
35 | 36 | ||
36 | static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) | 37 | static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) |
37 | { | 38 | { |
38 | char *path; | 39 | char *path; |
39 | struct stat s; | 40 | struct stat s; |
40 | struct cgit_repo *r = (struct cgit_repo *)repo; | 41 | struct cgit_repo *r = (struct cgit_repo *)repo; |
41 | 42 | ||
42 | if (repo->mtime != -1) { | 43 | if (repo->mtime != -1) { |
43 | *mtime = repo->mtime; | 44 | *mtime = repo->mtime; |
44 | return 1; | 45 | return 1; |
45 | } | 46 | } |
46 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); | 47 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); |
47 | if (stat(path, &s) == 0) { | 48 | if (stat(path, &s) == 0) { |
48 | *mtime = read_agefile(path); | 49 | *mtime = read_agefile(path); |
49 | r->mtime = *mtime; | 50 | r->mtime = *mtime; |
@@ -122,32 +123,44 @@ void print_pager(int items, int pagelen, char *search) | |||
122 | cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, | 123 | cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, |
123 | search, i * pagelen); | 124 | search, i * pagelen); |
124 | html("</div>"); | 125 | html("</div>"); |
125 | } | 126 | } |
126 | 127 | ||
127 | static int cmp(const char *s1, const char *s2) | 128 | static int cmp(const char *s1, const char *s2) |
128 | { | 129 | { |
129 | if (s1 && s2) | 130 | if (s1 && s2) |
130 | return strcmp(s1, s2); | 131 | return strcmp(s1, s2); |
131 | if (s1 && !s2) | 132 | if (s1 && !s2) |
132 | return -1; | 133 | return -1; |
133 | if (s2 && !s1) | 134 | if (s2 && !s1) |
134 | return 1; | 135 | return 1; |
135 | return 0; | 136 | return 0; |
136 | } | 137 | } |
137 | 138 | ||
139 | static int sort_section(const void *a, const void *b) | ||
140 | { | ||
141 | const struct cgit_repo *r1 = a; | ||
142 | const struct cgit_repo *r2 = b; | ||
143 | int result; | ||
144 | |||
145 | result = cmp(r1->section, r2->section); | ||
146 | if (!result) | ||
147 | result = cmp(r1->name, r2->name); | ||
148 | return result; | ||
149 | } | ||
150 | |||
138 | static int sort_name(const void *a, const void *b) | 151 | static int sort_name(const void *a, const void *b) |
139 | { | 152 | { |
140 | const struct cgit_repo *r1 = a; | 153 | const struct cgit_repo *r1 = a; |
141 | const struct cgit_repo *r2 = b; | 154 | const struct cgit_repo *r2 = b; |
142 | 155 | ||
143 | return cmp(r1->name, r2->name); | 156 | return cmp(r1->name, r2->name); |
144 | } | 157 | } |
145 | 158 | ||
146 | static int sort_desc(const void *a, const void *b) | 159 | static int sort_desc(const void *a, const void *b) |
147 | { | 160 | { |
148 | const struct cgit_repo *r1 = a; | 161 | const struct cgit_repo *r1 = a; |
149 | const struct cgit_repo *r2 = b; | 162 | const struct cgit_repo *r2 = b; |
150 | 163 | ||
151 | return cmp(r1->desc, r2->desc); | 164 | return cmp(r1->desc, r2->desc); |
152 | } | 165 | } |
153 | 166 | ||
@@ -164,116 +177,128 @@ static int sort_idle(const void *a, const void *b) | |||
164 | const struct cgit_repo *r1 = a; | 177 | const struct cgit_repo *r1 = a; |
165 | const struct cgit_repo *r2 = b; | 178 | const struct cgit_repo *r2 = b; |
166 | time_t t1, t2; | 179 | time_t t1, t2; |
167 | 180 | ||
168 | t1 = t2 = 0; | 181 | t1 = t2 = 0; |
169 | get_repo_modtime(r1, &t1); | 182 | get_repo_modtime(r1, &t1); |
170 | get_repo_modtime(r2, &t2); | 183 | get_repo_modtime(r2, &t2); |
171 | return t2 - t1; | 184 | return t2 - t1; |
172 | } | 185 | } |
173 | 186 | ||
174 | struct sortcolumn { | 187 | struct sortcolumn { |
175 | const char *name; | 188 | const char *name; |
176 | int (*fn)(const void *a, const void *b); | 189 | int (*fn)(const void *a, const void *b); |
177 | }; | 190 | }; |
178 | 191 | ||
179 | struct sortcolumn sortcolumn[] = { | 192 | struct sortcolumn sortcolumn[] = { |
193 | {"section", sort_section}, | ||
180 | {"name", sort_name}, | 194 | {"name", sort_name}, |
181 | {"desc", sort_desc}, | 195 | {"desc", sort_desc}, |
182 | {"owner", sort_owner}, | 196 | {"owner", sort_owner}, |
183 | {"idle", sort_idle}, | 197 | {"idle", sort_idle}, |
184 | {NULL, NULL} | 198 | {NULL, NULL} |
185 | }; | 199 | }; |
186 | 200 | ||
187 | int sort_repolist(char *field) | 201 | int sort_repolist(char *field) |
188 | { | 202 | { |
189 | struct sortcolumn *column; | 203 | struct sortcolumn *column; |
190 | 204 | ||
191 | for (column = &sortcolumn[0]; column->name; column++) { | 205 | for (column = &sortcolumn[0]; column->name; column++) { |
192 | if (strcmp(field, column->name)) | 206 | if (strcmp(field, column->name)) |
193 | continue; | 207 | continue; |
194 | qsort(cgit_repolist.repos, cgit_repolist.count, | 208 | qsort(cgit_repolist.repos, cgit_repolist.count, |
195 | sizeof(struct cgit_repo), column->fn); | 209 | sizeof(struct cgit_repo), column->fn); |
196 | return 1; | 210 | return 1; |
197 | } | 211 | } |
198 | return 0; | 212 | return 0; |
199 | } | 213 | } |
200 | 214 | ||
201 | 215 | ||
202 | void cgit_print_repolist() | 216 | void cgit_print_repolist() |
203 | { | 217 | { |
204 | int i, columns = 4, hits = 0, header = 0; | 218 | int i, columns = 4, hits = 0, header = 0; |
205 | char *last_group = NULL; | 219 | char *last_section = NULL; |
220 | char *section; | ||
206 | int sorted = 0; | 221 | int sorted = 0; |
207 | 222 | ||
208 | if (ctx.cfg.enable_index_links) | 223 | if (ctx.cfg.enable_index_links) |
209 | columns++; | 224 | columns++; |
210 | 225 | ||
211 | ctx.page.title = ctx.cfg.root_title; | 226 | ctx.page.title = ctx.cfg.root_title; |
212 | cgit_print_http_headers(&ctx); | 227 | cgit_print_http_headers(&ctx); |
213 | cgit_print_docstart(&ctx); | 228 | cgit_print_docstart(&ctx); |
214 | cgit_print_pageheader(&ctx); | 229 | cgit_print_pageheader(&ctx); |
215 | 230 | ||
216 | if (ctx.cfg.index_header) | 231 | if (ctx.cfg.index_header) |
217 | html_include(ctx.cfg.index_header); | 232 | html_include(ctx.cfg.index_header); |
218 | 233 | ||
219 | if(ctx.qry.sort) | 234 | if(ctx.qry.sort) |
220 | sorted = sort_repolist(ctx.qry.sort); | 235 | sorted = sort_repolist(ctx.qry.sort); |
236 | else | ||
237 | sort_repolist("section"); | ||
221 | 238 | ||
222 | html("<table summary='repository list' class='list nowrap'>"); | 239 | html("<table summary='repository list' class='list nowrap'>"); |
223 | for (i=0; i<cgit_repolist.count; i++) { | 240 | for (i=0; i<cgit_repolist.count; i++) { |
224 | ctx.repo = &cgit_repolist.repos[i]; | 241 | ctx.repo = &cgit_repolist.repos[i]; |
225 | if (!(is_match(ctx.repo) && is_in_url(ctx.repo))) | 242 | if (!(is_match(ctx.repo) && is_in_url(ctx.repo))) |
226 | continue; | 243 | continue; |
227 | hits++; | 244 | hits++; |
228 | if (hits <= ctx.qry.ofs) | 245 | if (hits <= ctx.qry.ofs) |
229 | continue; | 246 | continue; |
230 | if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count) | 247 | if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count) |
231 | continue; | 248 | continue; |
232 | if (!header++) | 249 | if (!header++) |
233 | print_header(columns); | 250 | print_header(columns); |
251 | section = ctx.repo->section; | ||
252 | if (section && !strcmp(section, "")) | ||
253 | section = NULL; | ||
234 | if (!sorted && | 254 | if (!sorted && |
235 | ((last_group == NULL && ctx.repo->group != NULL) || | 255 | ((last_section == NULL && section != NULL) || |
236 | (last_group != NULL && ctx.repo->group == NULL) || | 256 | (last_section != NULL && section == NULL) || |
237 | (last_group != NULL && ctx.repo->group != NULL && | 257 | (last_section != NULL && section != NULL && |
238 | strcmp(ctx.repo->group, last_group)))) { | 258 | strcmp(section, last_section)))) { |
239 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", | 259 | htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>", |
240 | columns); | 260 | columns); |
241 | html_txt(ctx.repo->group); | 261 | html_txt(section); |
242 | html("</td></tr>"); | 262 | html("</td></tr>"); |
243 | last_group = ctx.repo->group; | 263 | last_section = section; |
244 | } | 264 | } |
245 | htmlf("<tr><td class='%s'>", | 265 | htmlf("<tr><td class='%s'>", |
246 | !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); | 266 | !sorted && section ? "sublevel-repo" : "toplevel-repo"); |
247 | cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); | 267 | cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); |
248 | html("</td><td>"); | 268 | html("</td><td>"); |
249 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); | 269 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); |
250 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); | 270 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); |
251 | html_link_close(); | 271 | html_link_close(); |
252 | html("</td><td>"); | 272 | html("</td><td>"); |
253 | html_txt(ctx.repo->owner); | 273 | html_txt(ctx.repo->owner); |
254 | html("</td><td>"); | 274 | html("</td><td>"); |
255 | print_modtime(ctx.repo); | 275 | print_modtime(ctx.repo); |
256 | html("</td>"); | 276 | html("</td>"); |
257 | if (ctx.cfg.enable_index_links) { | 277 | if (ctx.cfg.enable_index_links) { |
258 | html("<td>"); | 278 | html("<td>"); |
259 | cgit_summary_link("summary", NULL, "button", NULL); | 279 | cgit_summary_link("summary", NULL, "button", NULL); |
260 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, | 280 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, |
261 | 0, NULL, NULL, ctx.qry.showmsg); | 281 | 0, NULL, NULL, ctx.qry.showmsg); |
262 | cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); | 282 | cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); |
263 | html("</td>"); | 283 | html("</td>"); |
264 | } | 284 | } |
265 | html("</tr>\n"); | 285 | html("</tr>\n"); |
266 | } | 286 | } |
267 | html("</table>"); | 287 | html("</table>"); |
268 | if (!hits) | 288 | if (!hits) |
269 | cgit_print_error("No repositories found"); | 289 | cgit_print_error("No repositories found"); |
270 | else if (hits > ctx.cfg.max_repo_count) | 290 | else if (hits > ctx.cfg.max_repo_count) |
271 | print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search); | 291 | print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search); |
272 | cgit_print_docend(); | 292 | cgit_print_docend(); |
273 | } | 293 | } |
274 | 294 | ||
275 | void cgit_print_site_readme() | 295 | void cgit_print_site_readme() |
276 | { | 296 | { |
277 | if (ctx.cfg.root_readme) | 297 | if (!ctx.cfg.root_readme) |
278 | html_include(ctx.cfg.root_readme); | 298 | return; |
299 | if (ctx.cfg.about_filter) | ||
300 | cgit_open_filter(ctx.cfg.about_filter); | ||
301 | html_include(ctx.cfg.root_readme); | ||
302 | if (ctx.cfg.about_filter) | ||
303 | cgit_close_filter(ctx.cfg.about_filter); | ||
279 | } | 304 | } |