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