all repos — cgit @ 5a8862723f6609b916a8550e190087b47f1e5f38

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