all repos — cgit @ d350d34da963943db25469a8567c24e5e9ff1386

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