all repos — cgit @ d93cebaa18bc69933489bd76c5ce7c40a9a632a6

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