all repos — cgit @ v0.10

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