all repos — cgit @ cbac02c8b056e47b3e0092949c480c7ec64a3e0f

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 *
  5 * Licensed under GNU General Public License v2
  6 *   (see COPYING for full license text)
  7 */
  8
  9#include <time.h>
 10
 11#include "cgit.h"
 12#include "html.h"
 13#include "ui-shared.h"
 14
 15time_t read_agefile(char *path)
 16{
 17	FILE *f;
 18	static char buf[64], buf2[64];
 19
 20	if (!(f = fopen(path, "r")))
 21		return -1;
 22	if (fgets(buf, sizeof(buf), f) == NULL)
 23		return -1;
 24	fclose(f);
 25	if (parse_date(buf, buf2, sizeof(buf2)))
 26		return strtoul(buf2, NULL, 10);
 27	else
 28		return 0;
 29}
 30
 31static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
 32{
 33	char *path;
 34	struct stat s;
 35
 36	path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
 37	if (stat(path, &s) == 0) {
 38		*mtime = read_agefile(path);
 39		return 1;
 40	}
 41
 42	path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
 43	if (stat(path, &s) == 0) {
 44		*mtime = s.st_mtime;
 45		return 1;
 46	}
 47	return 0;
 48}
 49
 50static void print_modtime(struct cgit_repo *repo)
 51{
 52	time_t t;
 53	if (get_repo_modtime(repo, &t))
 54		cgit_print_age(t, -1, NULL);
 55}
 56
 57int is_match(struct cgit_repo *repo)
 58{
 59	if (!ctx.qry.search)
 60		return 1;
 61	if (repo->url && strcasestr(repo->url, ctx.qry.search))
 62		return 1;
 63	if (repo->name && strcasestr(repo->name, ctx.qry.search))
 64		return 1;
 65	if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
 66		return 1;
 67	if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
 68		return 1;
 69	return 0;
 70}
 71
 72int is_in_url(struct cgit_repo *repo)
 73{
 74	if (!ctx.qry.url)
 75		return 1;
 76	if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
 77		return 1;
 78	return 0;
 79}
 80
 81void print_header(int columns)
 82{
 83	html("<tr class='nohover'>"
 84	     "<th class='left'>Name</th>"
 85	     "<th class='left'>Description</th>"
 86	     "<th class='left'>Owner</th>"
 87	     "<th class='left'><a href=\"?s=1\">Idle</a></th>");
 88	if (ctx.cfg.enable_index_links)
 89		html("<th class='left'>Links</th>");
 90	html("</tr>\n");
 91}
 92
 93
 94void print_pager(int items, int pagelen, char *search)
 95{
 96	int i;
 97	html("<div class='pager'>");
 98	for(i = 0; i * pagelen < items; i++)
 99		cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
100				search, i * pagelen);
101	html("</div>");
102}
103
104static int cgit_reposort_modtime(const void *a, const void *b)
105{
106	const struct cgit_repo *r1 = a;
107	const struct cgit_repo *r2 = b;
108	time_t t1, t2;
109
110	t1 = t2 = 0;
111	get_repo_modtime(r1, &t1);
112	get_repo_modtime(r2, &t2);
113	return t2 - t1;
114}
115
116void cgit_print_repolist()
117{
118	int i, columns = 4, hits = 0, header = 0;
119	char *last_group = NULL;
120
121	if (ctx.cfg.enable_index_links)
122		columns++;
123
124	ctx.page.title = ctx.cfg.root_title;
125	cgit_print_http_headers(&ctx);
126	cgit_print_docstart(&ctx);
127	cgit_print_pageheader(&ctx);
128
129	if (ctx.cfg.index_header)
130		html_include(ctx.cfg.index_header);
131
132	if(ctx.qry.sort)
133		qsort(cgit_repolist.repos,cgit_repolist.count,sizeof(struct cgit_repo),cgit_reposort_modtime);
134
135	html("<table summary='repository list' class='list nowrap'>");
136	for (i=0; i<cgit_repolist.count; i++) {
137		ctx.repo = &cgit_repolist.repos[i];
138		if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
139			continue;
140		hits++;
141		if (hits <= ctx.qry.ofs)
142			continue;
143		if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
144			continue;
145		if (!header++)
146			print_header(columns);
147		if (!ctx.qry.sort &&
148		    ((last_group == NULL && ctx.repo->group != NULL) ||
149		    (last_group != NULL && ctx.repo->group == NULL) ||
150		    (last_group != NULL && ctx.repo->group != NULL &&
151		     strcmp(ctx.repo->group, last_group)))) {
152			htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
153			      columns);
154			html_txt(ctx.repo->group);
155			html("</td></tr>");
156			last_group = ctx.repo->group;
157		}
158		htmlf("<tr><td class='%s'>",
159		      ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
160		cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
161		html("</td><td>");
162		html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
163		html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
164		html_link_close();
165		html("</td><td>");
166		html_txt(ctx.repo->owner);
167		html("</td><td>");
168		print_modtime(ctx.repo);
169		html("</td>");
170		if (ctx.cfg.enable_index_links) {
171			html("<td>");
172			cgit_summary_link("summary", NULL, "button", NULL);
173			cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
174				      0, NULL, NULL);
175			cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
176			html("</td>");
177		}
178		html("</tr>\n");
179	}
180	html("</table>");
181	if (!hits)
182		cgit_print_error("No repositories found");
183	else if (hits > ctx.cfg.max_repo_count)
184		print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
185	cgit_print_docend();
186}
187
188void cgit_print_site_readme()
189{
190	if (ctx.cfg.root_readme)
191		html_include(ctx.cfg.root_readme);
192}