all repos — cgit @ 5293c8b7992bf3211b6bde7acbbd4e74ffd926d4

a hyperfast web frontend for git written in c

ui-repolist.c (view raw)

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