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