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