all repos — cgit @ bbfa006e6eb93d56842c1d90bbba1c5484afb855

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