all repos — cgit @ ef8a97d9c6983e4fc3710bdbe771edd4e3550dba

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