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