all repos — cgit @ 979db79a80f1926c677691b68eb22b4852a77b50

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