all repos — cgit @ eb14609dc46461728a065c0a243b338fc32fd762

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