all repos — cgit @ a420c7ce9b2c483c2f93b8a1a03cc80f3eeedb20

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-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-summary.h"
 11#include "html.h"
 12#include "ui-log.h"
 13#include "ui-refs.h"
 14#include "ui-blob.h"
 15#include "ui-shared.h"
 16
 17static int urls;
 18
 19static void print_url(const char *url)
 20{
 21	int columns = 3;
 22
 23	if (ctx.repo->enable_log_filecount)
 24		columns++;
 25	if (ctx.repo->enable_log_linecount)
 26		columns++;
 27
 28	if (urls++ == 0) {
 29		htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
 30		htmlf("<tr class='nohover'><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
 31	}
 32
 33	htmlf("<tr><td colspan='%d'><a rel='vcs-git' href='", columns);
 34	html_url_path(url);
 35	html("' title='");
 36	html_attr(ctx.repo->name);
 37	html(" Git repository'>");
 38	html_txt(url);
 39	html("</a></td></tr>\n");
 40}
 41
 42void cgit_print_summary(void)
 43{
 44	int columns = 3;
 45
 46	if (ctx.repo->enable_log_filecount)
 47		columns++;
 48	if (ctx.repo->enable_log_linecount)
 49		columns++;
 50
 51	cgit_print_layout_start();
 52	html("<table summary='repository info' class='list nowrap'>");
 53	cgit_print_branches(ctx.cfg.summary_branches);
 54	htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
 55	cgit_print_tags(ctx.cfg.summary_tags);
 56	if (ctx.cfg.summary_log > 0) {
 57		htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
 58		cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
 59			       NULL, NULL, 0, 0, 0);
 60	}
 61	urls = 0;
 62	cgit_add_clone_urls(print_url);
 63	html("</table>");
 64	cgit_print_layout_end();
 65}
 66
 67/* The caller must free the return value. */
 68static char* append_readme_path(const char *filename, const char *ref, const char *path)
 69{
 70	char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
 71	/* If a subpath is specified for the about page, make it relative
 72	 * to the directory containing the configured readme. */
 73
 74	file = xstrdup(filename);
 75	base_dir = dirname(file);
 76	if (!strcmp(base_dir, ".") || !strcmp(base_dir, "..")) {
 77		if (!ref) {
 78			free(file);
 79			return NULL;
 80		}
 81		full_path = xstrdup(path);
 82	} else
 83		full_path = fmtalloc("%s/%s", base_dir, path);
 84
 85	if (!ref) {
 86		resolved_base = realpath(base_dir, NULL);
 87		resolved_full = realpath(full_path, NULL);
 88		if (!resolved_base || !resolved_full || !starts_with(resolved_full, resolved_base)) {
 89			free(full_path);
 90			full_path = NULL;
 91		}
 92	}
 93
 94	free(file);
 95	free(resolved_base);
 96	free(resolved_full);
 97
 98	return full_path;
 99}
100
101void cgit_print_repo_readme(char *path)
102{
103	char *filename, *ref;
104	int free_filename = 0;
105
106	cgit_print_layout_start();
107	if (ctx.repo->readme.nr == 0)
108		goto done;
109
110	filename = ctx.repo->readme.items[0].string;
111	ref = ctx.repo->readme.items[0].util;
112
113	if (path) {
114		free_filename = 1;
115		filename = append_readme_path(filename, ref, path);
116		if (!filename)
117			goto done;
118	}
119
120	/* Print the calculated readme, either from the git repo or from the
121	 * filesystem, while applying the about-filter.
122	 */
123	html("<div id='summary'>");
124	cgit_open_filter(ctx.repo->about_filter, filename);
125	if (ref)
126		cgit_print_file(filename, ref, 1);
127	else
128		html_include(filename);
129	cgit_close_filter(ctx.repo->about_filter);
130
131	html("</div>");
132	if (free_filename)
133		free(filename);
134
135done:
136	cgit_print_layout_end();
137}