all repos — cgit @ 57d09bf448990b3a67436e928807e854e491756f

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