all repos — cgit @ c58cec9dff273b44c428cfaee24e5e3743c0034e

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	struct strbuf date_buf = STRBUF_INIT;
 21
 22	if (readfile(path, &buf, &size))
 23		return -1;
 24
 25	if (parse_date(buf, &date_buf) == 0)
 26		result = strtoul(date_buf.buf, NULL, 10);
 27	else
 28		result = 0;
 29	free(buf);
 30	strbuf_release(&date_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 && starts_with(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	html("<th class='left'><a href='");
111	html_attr(cgit_rooturl());
112	htmlf("?s=%s", sort);
113	if (ctx.qry.search) {
114		html("&amp;q=");
115		html_url_arg(ctx.qry.search);
116	}
117	htmlf("'>%s</a></th>", title);
118}
119
120static void print_header()
121{
122	html("<tr class='nohover'>");
123	print_sort_header("Name", "name");
124	print_sort_header("Description", "desc");
125	if (ctx.cfg.enable_index_owner)
126		print_sort_header("Owner", "owner");
127	print_sort_header("Idle", "idle");
128	if (ctx.cfg.enable_index_links)
129		html("<th class='left'>Links</th>");
130	html("</tr>\n");
131}
132
133
134static void print_pager(int items, int pagelen, char *search, char *sort)
135{
136	int i, ofs;
137	char *class = NULL;
138	html("<ul class='pager'>");
139	for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
140		class = (ctx.qry.ofs == ofs) ? "current" : NULL;
141		html("<li>");
142		cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
143				class, search, sort, ofs);
144		html("</li>");
145	}
146	html("</ul>");
147}
148
149static int cmp(const char *s1, const char *s2)
150{
151	if (s1 && s2) {
152		if (ctx.cfg.case_sensitive_sort)
153			return strcmp(s1, s2);
154		else
155			return strcasecmp(s1, s2);
156	}
157	if (s1 && !s2)
158		return -1;
159	if (s2 && !s1)
160		return 1;
161	return 0;
162}
163
164static int sort_section(const void *a, const void *b)
165{
166	const struct cgit_repo *r1 = a;
167	const struct cgit_repo *r2 = b;
168	int result;
169	time_t t;
170
171	result = cmp(r1->section, r2->section);
172	if (!result) {
173		if (!strcmp(ctx.cfg.repository_sort, "age")) {
174			// get_repo_modtime caches the value in r->mtime, so we don't
175			// have to worry about inefficiencies here.
176			if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
177				result = r2->mtime - r1->mtime;
178		}
179		if (!result)
180			result = cmp(r1->name, r2->name);
181	}
182	return result;
183}
184
185static int sort_name(const void *a, const void *b)
186{
187	const struct cgit_repo *r1 = a;
188	const struct cgit_repo *r2 = b;
189
190	return cmp(r1->name, r2->name);
191}
192
193static int sort_desc(const void *a, const void *b)
194{
195	const struct cgit_repo *r1 = a;
196	const struct cgit_repo *r2 = b;
197
198	return cmp(r1->desc, r2->desc);
199}
200
201static int sort_owner(const void *a, const void *b)
202{
203	const struct cgit_repo *r1 = a;
204	const struct cgit_repo *r2 = b;
205
206	return cmp(r1->owner, r2->owner);
207}
208
209static int sort_idle(const void *a, const void *b)
210{
211	const struct cgit_repo *r1 = a;
212	const struct cgit_repo *r2 = b;
213	time_t t1, t2;
214
215	t1 = t2 = 0;
216	get_repo_modtime(r1, &t1);
217	get_repo_modtime(r2, &t2);
218	return t2 - t1;
219}
220
221struct sortcolumn {
222	const char *name;
223	int (*fn)(const void *a, const void *b);
224};
225
226struct sortcolumn sortcolumn[] = {
227	{"section", sort_section},
228	{"name", sort_name},
229	{"desc", sort_desc},
230	{"owner", sort_owner},
231	{"idle", sort_idle},
232	{NULL, NULL}
233};
234
235static int sort_repolist(char *field)
236{
237	struct sortcolumn *column;
238
239	for (column = &sortcolumn[0]; column->name; column++) {
240		if (strcmp(field, column->name))
241			continue;
242		qsort(cgit_repolist.repos, cgit_repolist.count,
243			sizeof(struct cgit_repo), column->fn);
244		return 1;
245	}
246	return 0;
247}
248
249
250void cgit_print_repolist()
251{
252	int i, columns = 3, hits = 0, header = 0;
253	char *last_section = NULL;
254	char *section;
255	int sorted = 0;
256
257	if (ctx.cfg.enable_index_links)
258		++columns;
259	if (ctx.cfg.enable_index_owner)
260		++columns;
261
262	ctx.page.title = ctx.cfg.root_title;
263	cgit_print_http_headers();
264	cgit_print_docstart();
265	cgit_print_pageheader();
266
267	if (ctx.cfg.index_header)
268		html_include(ctx.cfg.index_header);
269
270	if (ctx.qry.sort)
271		sorted = sort_repolist(ctx.qry.sort);
272	else if (ctx.cfg.section_sort)
273		sort_repolist("section");
274
275	html("<table summary='repository list' class='list nowrap'>");
276	for (i = 0; i < cgit_repolist.count; i++) {
277		ctx.repo = &cgit_repolist.repos[i];
278		if (ctx.repo->hide || ctx.repo->ignore)
279			continue;
280		if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
281			continue;
282		hits++;
283		if (hits <= ctx.qry.ofs)
284			continue;
285		if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
286			continue;
287		if (!header++)
288			print_header();
289		section = ctx.repo->section;
290		if (section && !strcmp(section, ""))
291			section = NULL;
292		if (!sorted &&
293		    ((last_section == NULL && section != NULL) ||
294		    (last_section != NULL && section == NULL) ||
295		    (last_section != NULL && section != NULL &&
296		     strcmp(section, last_section)))) {
297			htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
298			      columns);
299			html_txt(section);
300			html("</td></tr>");
301			last_section = section;
302		}
303		htmlf("<tr><td class='%s'>",
304		      !sorted && section ? "sublevel-repo" : "toplevel-repo");
305		cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
306		html("</td><td>");
307		html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
308		html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
309		html_link_close();
310		html("</td><td>");
311		if (ctx.cfg.enable_index_owner) {
312			if (ctx.repo->owner_filter) {
313				cgit_open_filter(ctx.repo->owner_filter);
314				html_txt(ctx.repo->owner);
315				cgit_close_filter(ctx.repo->owner_filter);
316			} else {
317				html("<a href='");
318				html_attr(cgit_rooturl());
319				html("?=");
320				html_url_arg(ctx.repo->owner);
321				html("'>");
322				html_txt(ctx.repo->owner);
323				html("</a>");
324			}
325			html("</td><td>");
326		}
327		print_modtime(ctx.repo);
328		html("</td>");
329		if (ctx.cfg.enable_index_links) {
330			html("<td>");
331			cgit_summary_link("summary", NULL, "button", NULL);
332			cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
333				      0, NULL, NULL, ctx.qry.showmsg);
334			cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
335			html("</td>");
336		}
337		html("</tr>\n");
338	}
339	html("</table>");
340	if (!hits)
341		cgit_print_error("No repositories found");
342	else if (hits > ctx.cfg.max_repo_count)
343		print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
344	cgit_print_docend();
345}
346
347void cgit_print_site_readme()
348{
349	if (!ctx.cfg.root_readme)
350		return;
351	cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
352	html_include(ctx.cfg.root_readme);
353	cgit_close_filter(ctx.cfg.about_filter);
354}