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