all repos — cgit @ f03e3cb8a5c6b597b87321e1f082d3ab177e8baa

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	html("<table summary='repository info' class='list nowrap'>");
 52	cgit_print_branches(ctx.cfg.summary_branches);
 53	htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
 54	cgit_print_tags(ctx.cfg.summary_tags);
 55	if (ctx.cfg.summary_log > 0) {
 56		htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
 57		cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
 58			       NULL, NULL, 0, 0, 0);
 59	}
 60	urls = 0;
 61	cgit_add_clone_urls(print_url);
 62	html("</table>");
 63}
 64
 65/* The caller must free the return value. */
 66static char* append_readme_path(const char *filename, const char *ref, const char *path)
 67{
 68	char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
 69	/* If a subpath is specified for the about page, make it relative
 70	 * to the directory containing the configured readme. */
 71
 72	file = xstrdup(filename);
 73	base_dir = dirname(file);
 74	if (!strcmp(base_dir, ".") || !strcmp(base_dir, "..")) {
 75		if (!ref) {
 76			free(file);
 77			return NULL;
 78		}
 79		full_path = xstrdup(path);
 80	} else
 81		full_path = fmtalloc("%s/%s", base_dir, path);
 82
 83	if (!ref) {
 84		resolved_base = realpath(base_dir, NULL);
 85		resolved_full = realpath(full_path, NULL);
 86		if (!resolved_base || !resolved_full || !starts_with(resolved_full, resolved_base)) {
 87			free(full_path);
 88			full_path = NULL;
 89		}
 90	}
 91
 92	free(file);
 93	free(resolved_base);
 94	free(resolved_full);
 95
 96	return full_path;
 97}
 98
 99void cgit_print_repo_readme(char *path)
100{
101	char *filename, *ref;
102	int free_filename = 0;
103
104	if (ctx.repo->readme.nr == 0)
105		return;
106
107	filename = ctx.repo->readme.items[0].string;
108	ref = ctx.repo->readme.items[0].util;
109
110	if (path) {
111		free_filename = 1;
112		filename = append_readme_path(filename, ref, path);
113		if (!filename)
114			return;
115	}
116
117	/* Print the calculated readme, either from the git repo or from the
118	 * filesystem, while applying the about-filter.
119	 */
120	html("<div id='summary'>");
121	cgit_open_filter(ctx.repo->about_filter, filename);
122	if (ref)
123		cgit_print_file(filename, ref, 1);
124	else
125		html_include(filename);
126	cgit_close_filter(ctx.repo->about_filter);
127
128	html("</div>");
129	if (free_filename)
130		free(filename);
131}