blob: 3aedde5456f761fe58926bfef7e28f389975aa41 [file] [log] [blame]
Lars Hjemli74620f12006-12-11 16:48:03 +01001/* 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
Lars Hjemli7115f7d2008-12-05 19:04:17 +01009/* This is needed for strcasestr to be defined by <string.h> */
10#define _GNU_SOURCE 1
11#include <string.h>
12
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020013#include <time.h>
14
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010015#include "cgit.h"
16#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010017#include "ui-shared.h"
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020018
19time_t read_agefile(char *path)
20{
21 FILE *f;
22 static char buf[64], buf2[64];
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020023
24 if (!(f = fopen(path, "r")))
25 return -1;
Simon Arlott011f2e92009-03-07 00:06:24 +000026 buf[0] = 0;
Lars Hjemlic7d14022008-11-14 09:29:22 +010027 if (fgets(buf, sizeof(buf), f) == NULL)
28 return -1;
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020029 fclose(f);
30 if (parse_date(buf, buf2, sizeof(buf2)))
31 return strtoul(buf2, NULL, 10);
32 else
33 return 0;
34}
35
Lars Hjemlicbac02c2008-11-29 13:33:02 +010036static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020037{
38 char *path;
39 struct stat s;
Lars Hjemli88131702008-11-29 16:46:37 +010040 struct cgit_repo *r = (struct cgit_repo *)repo;
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020041
Lars Hjemli88131702008-11-29 16:46:37 +010042 if (repo->mtime != -1) {
43 *mtime = repo->mtime;
44 return 1;
45 }
Lars Hjemlib228d4f2008-02-16 13:07:13 +010046 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020047 if (stat(path, &s) == 0) {
Lars Hjemlicbac02c2008-11-29 13:33:02 +010048 *mtime = read_agefile(path);
Lars Hjemli88131702008-11-29 16:46:37 +010049 r->mtime = *mtime;
Lars Hjemlicbac02c2008-11-29 13:33:02 +010050 return 1;
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020051 }
52
53 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
Lars Hjemli88131702008-11-29 16:46:37 +010054 if (stat(path, &s) == 0)
Lars Hjemlicbac02c2008-11-29 13:33:02 +010055 *mtime = s.st_mtime;
Lars Hjemli88131702008-11-29 16:46:37 +010056 else
57 *mtime = 0;
58
59 r->mtime = *mtime;
60 return (r->mtime != 0);
Lars Hjemlicbac02c2008-11-29 13:33:02 +010061}
62
63static void print_modtime(struct cgit_repo *repo)
64{
65 time_t t;
66 if (get_repo_modtime(repo, &t))
67 cgit_print_age(t, -1, NULL);
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020068}
Lars Hjemli74620f12006-12-11 16:48:03 +010069
Lars Hjemli536b0542008-04-13 11:57:10 +020070int is_match(struct cgit_repo *repo)
Lars Hjemli74620f12006-12-11 16:48:03 +010071{
Lars Hjemli536b0542008-04-13 11:57:10 +020072 if (!ctx.qry.search)
73 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020074 if (repo->url && strcasestr(repo->url, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020075 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020076 if (repo->name && strcasestr(repo->name, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020077 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020078 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020079 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020080 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020081 return 1;
82 return 0;
83}
Lars Hjemli74620f12006-12-11 16:48:03 +010084
Lars Hjemlidd80ef52008-09-14 20:18:10 +020085int is_in_url(struct cgit_repo *repo)
86{
87 if (!ctx.qry.url)
88 return 1;
89 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
90 return 1;
91 return 0;
92}
93
Lars Hjemlif250c1c2008-11-29 14:08:51 +010094void print_sort_header(const char *title, const char *sort)
95{
96 htmlf("<th class='left'><a href='./?s=%s", sort);
97 if (ctx.qry.search) {
98 html("&q=");
99 html_url_arg(ctx.qry.search);
100 }
101 htmlf("'>%s</a></th>", title);
102}
103
Lars Hjemli536b0542008-04-13 11:57:10 +0200104void print_header(int columns)
105{
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100106 html("<tr class='nohover'>");
107 print_sort_header("Name", "name");
108 print_sort_header("Description", "desc");
109 print_sort_header("Owner", "owner");
110 print_sort_header("Idle", "idle");
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100111 if (ctx.cfg.enable_index_links)
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200112 html("<th class='left'>Links</th>");
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200113 html("</tr>\n");
Lars Hjemli536b0542008-04-13 11:57:10 +0200114}
Lars Hjemli74620f12006-12-11 16:48:03 +0100115
Lars Hjemlic6078b82008-05-03 10:54:39 +0200116
117void print_pager(int items, int pagelen, char *search)
118{
119 int i;
120 html("<div class='pager'>");
121 for(i = 0; i * pagelen < items; i++)
122 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
123 search, i * pagelen);
124 html("</div>");
125}
126
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100127static int cmp(const char *s1, const char *s2)
128{
129 if (s1 && s2)
130 return strcmp(s1, s2);
131 if (s1 && !s2)
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100132 return -1;
Lars Hjemli54272e62008-11-29 14:27:35 +0100133 if (s2 && !s1)
134 return 1;
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100135 return 0;
136}
137
138static int sort_name(const void *a, const void *b)
139{
140 const struct cgit_repo *r1 = a;
141 const struct cgit_repo *r2 = b;
142
143 return cmp(r1->name, r2->name);
144}
145
146static int sort_desc(const void *a, const void *b)
147{
148 const struct cgit_repo *r1 = a;
149 const struct cgit_repo *r2 = b;
150
151 return cmp(r1->desc, r2->desc);
152}
153
154static int sort_owner(const void *a, const void *b)
155{
156 const struct cgit_repo *r1 = a;
157 const struct cgit_repo *r2 = b;
158
159 return cmp(r1->owner, r2->owner);
160}
161
162static int sort_idle(const void *a, const void *b)
Benjamin Closed71c0c72008-11-25 06:25:35 -0800163{
164 const struct cgit_repo *r1 = a;
165 const struct cgit_repo *r2 = b;
Benjamin Closed71c0c72008-11-25 06:25:35 -0800166 time_t t1, t2;
Benjamin Closed71c0c72008-11-25 06:25:35 -0800167
Lars Hjemlicbac02c2008-11-29 13:33:02 +0100168 t1 = t2 = 0;
169 get_repo_modtime(r1, &t1);
170 get_repo_modtime(r2, &t2);
171 return t2 - t1;
Benjamin Closed71c0c72008-11-25 06:25:35 -0800172}
173
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100174struct sortcolumn {
175 const char *name;
176 int (*fn)(const void *a, const void *b);
177};
178
179struct sortcolumn sortcolumn[] = {
180 {"name", sort_name},
181 {"desc", sort_desc},
182 {"owner", sort_owner},
183 {"idle", sort_idle},
184 {NULL, NULL}
185};
186
187int sort_repolist(char *field)
188{
189 struct sortcolumn *column;
190
191 for (column = &sortcolumn[0]; column->name; column++) {
192 if (strcmp(field, column->name))
193 continue;
194 qsort(cgit_repolist.repos, cgit_repolist.count,
195 sizeof(struct cgit_repo), column->fn);
196 return 1;
197 }
198 return 0;
199}
200
201
Lars Hjemli536b0542008-04-13 11:57:10 +0200202void cgit_print_repolist()
203{
204 int i, columns = 4, hits = 0, header = 0;
205 char *last_group = NULL;
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100206 int sorted = 0;
Lars Hjemli536b0542008-04-13 11:57:10 +0200207
208 if (ctx.cfg.enable_index_links)
209 columns++;
210
211 ctx.page.title = ctx.cfg.root_title;
212 cgit_print_http_headers(&ctx);
213 cgit_print_docstart(&ctx);
214 cgit_print_pageheader(&ctx);
215
Lars Hjemli80628172008-04-29 00:35:49 +0200216 if (ctx.cfg.index_header)
217 html_include(ctx.cfg.index_header);
218
Benjamin Closed71c0c72008-11-25 06:25:35 -0800219 if(ctx.qry.sort)
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100220 sorted = sort_repolist(ctx.qry.sort);
Benjamin Closed71c0c72008-11-25 06:25:35 -0800221
Lars Hjemli536b0542008-04-13 11:57:10 +0200222 html("<table summary='repository list' class='list nowrap'>");
Lars Hjemlice1c7332007-02-03 15:02:55 +0100223 for (i=0; i<cgit_repolist.count; i++) {
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100224 ctx.repo = &cgit_repolist.repos[i];
Lars Hjemlidd80ef52008-09-14 20:18:10 +0200225 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
Lars Hjemli536b0542008-04-13 11:57:10 +0200226 continue;
Lars Hjemlic6078b82008-05-03 10:54:39 +0200227 hits++;
228 if (hits <= ctx.qry.ofs)
229 continue;
230 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
231 continue;
Lars Hjemli536b0542008-04-13 11:57:10 +0200232 if (!header++)
233 print_header(columns);
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100234 if (!sorted &&
Benjamin Closed71c0c72008-11-25 06:25:35 -0800235 ((last_group == NULL && ctx.repo->group != NULL) ||
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100236 (last_group != NULL && ctx.repo->group == NULL) ||
237 (last_group != NULL && ctx.repo->group != NULL &&
Benjamin Closed71c0c72008-11-25 06:25:35 -0800238 strcmp(ctx.repo->group, last_group)))) {
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200239 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
240 columns);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100241 html_txt(ctx.repo->group);
Lars Hjemli5877c492007-05-18 22:48:22 +0200242 html("</td></tr>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100243 last_group = ctx.repo->group;
Lars Hjemli5877c492007-05-18 22:48:22 +0200244 }
Lars Hjemli0b8b6a32007-05-21 00:14:28 +0200245 htmlf("<tr><td class='%s'>",
Lars Hjemlif250c1c2008-11-29 14:08:51 +0100246 !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200247 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
Lars Hjemli74620f12006-12-11 16:48:03 +0100248 html("</td><td>");
Lars Hjemlie9a70422008-04-14 22:23:48 +0200249 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100250 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
Lars Hjemlie9a70422008-04-14 22:23:48 +0200251 html_link_close();
Lars Hjemli74620f12006-12-11 16:48:03 +0100252 html("</td><td>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100253 html_txt(ctx.repo->owner);
Lars Hjemli8f6f8c32007-05-16 02:12:06 +0200254 html("</td><td>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100255 print_modtime(ctx.repo);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200256 html("</td>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100257 if (ctx.cfg.enable_index_links) {
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200258 html("<td>");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200259 cgit_summary_link("summary", NULL, "button", NULL);
Lars Hjemli51140312007-11-03 10:42:37 +0100260 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
Lars Hjemli0274b572008-11-29 18:39:41 +0100261 0, NULL, NULL, ctx.qry.showmsg);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200262 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
263 html("</td>");
264 }
265 html("</tr>\n");
Lars Hjemli74620f12006-12-11 16:48:03 +0100266 }
Lars Hjemli74620f12006-12-11 16:48:03 +0100267 html("</table>");
Lars Hjemli536b0542008-04-13 11:57:10 +0200268 if (!hits)
269 cgit_print_error("No repositories found");
Lars Hjemlic6078b82008-05-03 10:54:39 +0200270 else if (hits > ctx.cfg.max_repo_count)
271 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
Lars Hjemli74620f12006-12-11 16:48:03 +0100272 cgit_print_docend();
273}
Lars Hjemlic6431a72008-04-29 01:06:30 +0200274
275void cgit_print_site_readme()
276{
277 if (ctx.cfg.root_readme)
278 html_include(ctx.cfg.root_readme);
279}