neocgit

a more 'modern' version of cgit
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/neocgit

ui-summary.c (3866B)


      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-blob.h"
     13 #include "ui-log.h"
     14 #include "ui-plain.h"
     15 #include "ui-refs.h"
     16 #include "ui-shared.h"
     17 
     18 static int urls;
     19 
     20 static 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 class='nohover'><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
     32 	}
     33 
     34 	htmlf("<tr><td colspan='%d'><a rel='vcs-git' href='", columns);
     35 	html_url_path(url);
     36 	html("' title='");
     37 	html_attr(ctx.repo->name);
     38 	html(" Git repository'>");
     39 	html_txt(url);
     40 	html("</a></td></tr>\n");
     41 }
     42 
     43 void cgit_print_summary(void)
     44 {
     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         cgit_print_layout_start();
     53 
     54         cgit_print_tree(ctx.qry.sha1, ctx.qry.path, false);
     55         cgit_print_repo_readme(ctx.qry.path, false);
     56 
     57         cgit_print_layout_end();
     58 }
     59 
     60 /* The caller must free the return value. */
     61 static char* append_readme_path(const char *filename, const char *ref, const char *path)
     62 {
     63 	char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
     64 	/* If a subpath is specified for the about page, make it relative
     65 	 * to the directory containing the configured readme. */
     66 
     67 	file = xstrdup(filename);
     68 	base_dir = dirname(file);
     69 	if (!strcmp(base_dir, ".") || !strcmp(base_dir, "..")) {
     70 		if (!ref) {
     71 			free(file);
     72 			return NULL;
     73 		}
     74 		full_path = xstrdup(path);
     75 	} else
     76 		full_path = fmtalloc("%s/%s", base_dir, path);
     77 
     78 	if (!ref) {
     79 		resolved_base = realpath(base_dir, NULL);
     80 		resolved_full = realpath(full_path, NULL);
     81 		if (!resolved_base || !resolved_full || !starts_with(resolved_full, resolved_base)) {
     82 			free(full_path);
     83 			full_path = NULL;
     84 		}
     85 	}
     86 
     87 	free(file);
     88 	free(resolved_base);
     89 	free(resolved_full);
     90 
     91 	return full_path;
     92 }
     93 
     94 void cgit_print_repo_readme(char *path, bool layout)
     95 {
     96 	char *filename, *ref, *mimetype;
     97 	int free_filename = 0;
     98 
     99 	mimetype = get_mimetype_for_filename(path);
    100 	if (mimetype && (!strncmp(mimetype, "image/", 6) || !strncmp(mimetype, "video/", 6))) {
    101 		ctx.page.mimetype = mimetype;
    102 		ctx.page.charset = NULL;
    103 		cgit_print_plain();
    104 		free(mimetype);
    105 		return;
    106 	}
    107 	free(mimetype);
    108  
    109         if (layout)
    110             cgit_print_layout_start();
    111 	if (ctx.repo->readme.nr == 0)
    112 		goto done;
    113 
    114 	filename = ctx.repo->readme.items[0].string;
    115 	ref = ctx.repo->readme.items[0].util;
    116 
    117         _html("<div class='repo-file'>") {
    118             _html("<div class='header'>") {
    119                 htmlf("%s", filename);
    120             } _html("</div>");
    121 
    122             _html("<div class='content'>") {
    123 
    124                 if (path) {
    125                     free_filename = 1;
    126                     filename = append_readme_path(filename, ref, path);
    127                     if (!filename)
    128                         goto done;
    129                 }
    130 
    131                 /* Print the calculated readme, either from the git repo or from the
    132                  * filesystem, while applying the about-filter.
    133                  */
    134                 cgit_open_filter(ctx.repo->about_filter, filename);
    135                 if (ref)
    136                 {
    137                     cgit_print_file(filename, ref, 1);
    138                 }
    139                 else
    140                     html_include(filename);
    141                 cgit_close_filter(ctx.repo->about_filter);
    142             } _html("</div>");
    143 
    144         } _html("</div>");
    145 
    146 	if (free_filename)
    147 		free(filename);
    148 
    149 done:
    150         if (layout)
    151             cgit_print_layout_end();
    152 }