all repos — cgit @ c0dfaf1c281d0697ce43131343d7a9f170a61ff9

a hyperfast web frontend for git written in c

ui-summary.c (view raw)

  1/* ui-summary.c: functions for generating repo summary page
  2 *
  3 * Copyright (C) 2006 Lars Hjemli
  4 * Copyright (C) 2010 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 "ui-summary.h"
 12#include "html.h"
 13#include "ui-log.h"
 14#include "ui-refs.h"
 15#include "ui-blob.h"
 16
 17static void print_url(char *base, char *suffix)
 18{
 19	int columns = 3;
 20	struct strbuf basebuf = STRBUF_INIT;
 21
 22	if (ctx.repo->enable_log_filecount)
 23		columns++;
 24	if (ctx.repo->enable_log_linecount)
 25		columns++;
 26
 27	if (!base || !*base)
 28		return;
 29	if (suffix && *suffix) {
 30		strbuf_addf(&basebuf, "%s/%s", base, suffix);
 31		base = basebuf.buf;
 32	}
 33	htmlf("<tr><td colspan='%d'><a href='", columns);
 34	html_url_path(base);
 35	html("'>");
 36	html_txt(base);
 37	html("</a></td></tr>\n");
 38	strbuf_release(&basebuf);
 39}
 40
 41static void print_urls(char *txt, char *suffix)
 42{
 43	char *h = txt, *t, c;
 44	int urls = 0;
 45	int columns = 3;
 46
 47	if (ctx.repo->enable_log_filecount)
 48		columns++;
 49	if (ctx.repo->enable_log_linecount)
 50		columns++;
 51
 52
 53	while (h && *h) {
 54		while (h && *h == ' ')
 55			h++;
 56		if (!*h)
 57			break;
 58		t = h;
 59		while (t && *t && *t != ' ')
 60			t++;
 61		c = *t;
 62		*t = 0;
 63		if (urls++ == 0) {
 64			htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
 65			htmlf("<tr><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
 66		}
 67		print_url(h, suffix);
 68		*t = c;
 69		h = t;
 70	}
 71}
 72
 73void cgit_print_summary()
 74{
 75	int columns = 3;
 76
 77	if (ctx.repo->enable_log_filecount)
 78		columns++;
 79	if (ctx.repo->enable_log_linecount)
 80		columns++;
 81
 82	html("<table summary='repository info' class='list nowrap'>");
 83	cgit_print_branches(ctx.cfg.summary_branches);
 84	htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
 85	cgit_print_tags(ctx.cfg.summary_tags);
 86	if (ctx.cfg.summary_log > 0) {
 87		htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
 88		cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
 89			       NULL, NULL, 0, 0, 0);
 90	}
 91	if (ctx.repo->clone_url)
 92		print_urls(expand_macros(ctx.repo->clone_url), NULL);
 93	else if (ctx.cfg.clone_prefix)
 94		print_urls(ctx.cfg.clone_prefix, ctx.repo->url);
 95	html("</table>");
 96}
 97
 98void cgit_print_repo_readme(char *path)
 99{
100	char *slash, *tmp, *colon, *ref;
101	int free_filename = 0;
102
103	if (!ctx.repo->readme || !(*ctx.repo->readme))
104		return;
105
106	ref = NULL;
107
108	/* Check if the readme is tracked in the git repo. */
109	colon = strchr(ctx.repo->readme, ':');
110	if (colon && strlen(colon) > 1) {
111		*colon = '\0';
112		/* If it starts with a colon, we want to use
113		 * the default branch */
114		if (colon == ctx.repo->readme && ctx.repo->defbranch)
115			ref = ctx.repo->defbranch;
116		else
117			ref = ctx.repo->readme;
118		ctx.repo->readme = colon + 1;
119		if (!(*ctx.repo->readme))
120			return;
121	}
122
123	/* Prepend repo path to relative readme path unless tracked. */
124	if (!ref && *ctx.repo->readme != '/')
125		ctx.repo->readme = fmtalloc("%s/%s", ctx.repo->path,
126						ctx.repo->readme);
127
128	/* If a subpath is specified for the about page, make it relative
129	 * to the directory containing the configured readme.
130	 */
131	if (path) {
132		slash = strrchr(ctx.repo->readme, '/');
133		if (!slash) {
134			if (!colon)
135				return;
136			slash = colon;
137		}
138		free_filename = 1;
139		tmp = xmalloc(slash - ctx.repo->readme + 1 + strlen(path) + 1);
140		strncpy(tmp, ctx.repo->readme, slash - ctx.repo->readme + 1);
141		strcpy(tmp + (slash - ctx.repo->readme + 1), path);
142	} else
143		tmp = ctx.repo->readme;
144
145	/* Print the calculated readme, either from the git repo or from the
146	 * filesystem, while applying the about-filter.
147	 */
148	html("<div id='summary'>");
149	if (ctx.repo->about_filter) {
150		ctx.repo->about_filter->argv[1] = tmp;
151		cgit_open_filter(ctx.repo->about_filter);
152	}
153	if (ref)
154		cgit_print_file(tmp, ref);
155	else
156		html_include(tmp);
157	if (ctx.repo->about_filter) {
158		cgit_close_filter(ctx.repo->about_filter);
159		ctx.repo->about_filter->argv[1] = NULL;
160	}
161	html("</div>");
162	if (free_filename)
163		free(tmp);
164}